Repository navigation
soc: apple: sep: order the mailbox buffers, harden the hwrng sleep hold; trusted-keys slot rule - #357
Merged
Merged
Conversation
A driver that fills a coherent DMA buffer and then rings the device with a relaxed MMIO write has to order the two itself, as C drivers do with dma_wmb(). On receive it is the other way round, with dma_rmb(). The barrier module has only the smp_*() barriers. Those order accesses between CPUs, not against a device, and compile away on !SMP. Add both, through helpers as for the others. They do not depend on SMP. Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
The host writes a buffer and then sends the message that points the enclave at it: a request payload, the poison that refills an SBIO reply header, the shared memory a registration hands over. The buffers are coherent allocations. The SEP node is not dma-coherent, so on arm64 they are mapped Normal-NC, and the mailbox write is writeq_relaxed() to Device memory. Nothing orders the two, so the enclave can be told to look before the bytes are there. It would then read a stale request, or write its reply's header and have the poison refill land over it. Receiving is the same the other way round: the message comes in through readq_relaxed(), and nothing keeps the reads of the buffer it points to from going ahead of it. RTKit has the same contract with the same mailbox. It puts dma_wmb() before apple_mbox_send() and dma_rmb() after a message is read. The SEP driver calls the mailbox directly and has neither. Add them: dma_wmb() before every send, in the one place sends reach the mailbox, and dma_rmb() in the receive callback, before the message is queued for the work item that reads the buffers. dma_wmb() is the right barrier for this mapping. A coherent buffer has no dma_sync_*_for_device() call, which would order it as part of the cache maintenance a streaming mapping needs. Here the barrier has to be explicit. Normal-NC needs no cache maintenance either, only the ordering of the CPU's stores to the buffer before its store to the mailbox, as the enclave sees them. On arm64 dma_wmb() is dmb(oshst), the barrier writel() itself puts before the MMIO store (__io_bw()). So dma_wmb() followed by the relaxed doorbell is what writeq() would do, with no wmb()'s full dsb. dma_rmb() is dmb(oshld), as in readq(). It also orders the MMIO read before the stores that queue the message, so the work item's later reads are ordered after it. Fixes: bac0846 ("soc: apple: add Apple SEP driver (Touch ID and key sealing)") Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
KUnit covers ControlState, the entropy exchange's bookkeeping. The code that drives it from the hwrng core and the PM notifier has no test: - the drain on the way to sleep, and its limit; - the order of the sleep holds, and their release when Touch ID refuses the suspend; - the mapping of a mailbox timeout to a full mailbox, which holds the next request back; - the hwrng read's result when requests are held back, which is not logged as a failure; - the ready-phase survey, which does not count an attempt that asked the enclave nothing. Move each decision into a small function the driver calls, with the locking, sleeping and logging left where they are. Test them there: the drain, the read's result, the survey and the mapping in control.rs, and the sleep event in pm.rs, through a trait that SepData implements and the test records calls on. Nothing changes in what the driver does. Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
…leep Going to sleep sets the hold and then waits for a request in flight, for up to 3 s. That was meant to cover the mailbox's 500 ms wait for room and then the 2 s wait for the reply. But the hold is checked when the exchange is claimed, before the sender takes the mailbox lock. A sender claimed in time can then queue for the lock behind other senders, each of which may wait 500 ms for room. One of them can be a Touch ID capture, which is ended only after the entropy drain. So the 3 s bound did not hold. Once it ran out, the drain warned and the suspend went on, and the sender could write its request later in the window. That is what the hold exists to stop: on a T6000 a request sent while devices were suspended was never answered. Check the hold again with the mailbox lock held, just before the request is written. A request claimed before the sleep began but not yet written ends there as not sent, owing nothing and holding nothing back after the wake. Only a request already being written can still go out. Its 500 ms wait for room and 2 s wait for the reply are within the drain's 3 s, so the bound now holds. The drain still only warns when it runs out, as there is then nothing left that it could stop. Add a KUnit case for a request that sleep overtakes before it is written. It fails when the new check always allows the send. Fixes: 1b04512 ("soc: apple: sep: Hold entropy requests while the system sleeps") Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
While entropy requests are held back, after one went unanswered or while the system goes to sleep, the read refuses at once with EAGAIN. The hwrng core passes that straight to a reader of /dev/hwrng, even one that blocks. Before the hold, such a reader blocked for the 2 s request timeout and then got ETIMEDOUT. Now it gets EAGAIN from a blocking file descriptor, which is unusual. A reader that tries again on EAGAIN spins for the whole backoff, up to 60 s. The fill thread is not affected: it sleeps 10 s after any read that returns nothing. hwrng_fill() cannot tell the fill thread from a user reader, but both read with wait set. So when a blocking read has nothing yet, let it wait for the hold to end, for at most the time a request waits for its reply. That is until the retry time, the wake, or the enclave answering again, each of which now wakes it. If the hold ends, the read asks then. If it does not, the read fails with ETIMEDOUT, as a request that went unanswered does, and that is not logged as a failure. A signal ends the wait with ERESTARTSYS, so a reader being frozen for the suspend restarts its read after the resume. The enclave is still not asked while requests are held. A read that already has bytes still returns them at once. The wait holds the hwrng core's reading_mutex for up to 2 s. That is no longer than a request that timed out held it before the hold existed. Add KUnit cases for how long a held read waits and for what it returns when the hold outlasts it. The second fails with the old EAGAIN mapping. Fixes: f771659 ("soc: apple: sep: Recover the entropy exchange after a request times out") Fixes: 1b04512 ("soc: apple: sep: Hold entropy requests while the system sleeps") Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
trusted_source_after_core_init expects the core's init to succeed and then skips when the built-in fake was not tried, blaming another source for holding the slot. With trusted.rng=trusted_source_kunit the init fails with -EINVAL, as it should, since that RNG is no built-in source's. The expectation fails, and the skip that follows overwrites the failure, so the case is reported as skipped for a slot nobody holds. With trusted.source= naming another source, the fake is not tried either, and the skip blames another source too. Look at the result first, and skip with the reason the fake was not tried: trusted.rng= rules it out, trusted.source= rules it out, or a source holds the slot. Which of the last two applies is found by trying to register the runtime fake, which trusted.source= refuses with -ENODEV and a held slot with -EBUSY. Any other result of the init is still a failure. Let the built-in fake's init() result be set by a test, for the next change. It stays -ENODEV here. Fixes: 4f15946 ("KEYS: trusted: Keep a source that registers before init_trusted()") Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
…ted() trusted_key_core_init() is exported for KUnit so that the tests can run the core's init again, after init_trusted(), with synthetic built-in sources. When such a source's init() succeeds, the core gives it the slot as init_trusted() would. But a source that takes the slot this way is never withdrawn: unregister_trusted_key_source() withdraws only a registered source, and this one is not. A test's source is freed when the test ends, so the dispatch would be left on it, and every later seal would call into freed memory. Today's tests pass only a built-in source that fails with -ENODEV, so nothing has hit this. Let only the first call, init_trusted()'s, give the slot to a built-in source. A later call whose source initialises calls the source's exit() and returns -EBUSY, leaving the slot as it was. init_trusted() runs once, at late_initcall, and built-in KUnit runs after the initcalls, so no real source is affected. Nothing changes without KUnit. Add a KUnit case for a built-in source that initialises after the core's init. It fails without the check. Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
RyanTheTide
added a commit
that referenced
this pull request
Oct 17, 2026
Bring the 7.2.10 assembly to the tree of the canonical top-up #7 assembly, stacked on top-up #6a 40a2ec8 so that it is a fast-forward of that publish head. The second parent is that canonical assembly; the tree is identical to it. Ported from aurora-wip, each commit with cherry-pick -x and its original author: - #306: aurora_j700_defconfig, regenerated for 7.2.10 with savedefconfig from the published 7.2.10 J700 configuration (900-config-tooling); - #354 (j700/clamshell-mic-v4), 3 commits: the LPAI pointer published after its frames, the spin_lock_irqsave() Rust helpers, and the mics reported unplugged with the lid closed (070-audio); - #355 (j700/tipd-hpd-pulse-v2), 9 commits: the CD321x DisplayPort hotplug decisions, their trace events and KUnit tests (170-usb), and the apple-dcp selftests that compile them (200-dcp); - #356 (fix/rust-dmabuf-xarray-20261018): the dma_buf stub gated on built-in DRM and the xarray KUnit values allocated before xa_lock (210-gpu, with the Fixes: tags of the 7.2.10 base); its dma-resv backport is already in asahi-wip; - #357 (fix/sep-followups-20261018), 7 commits: dma_wmb()/dma_rmb() for Rust, the SEP shared buffers ordered against the mailbox, the hwrng and sleep glue and its tests, and the trusted-keys built-in source fixes (100-sep). Our own 7.2-native commits, not on aurora-wip (100-sep): the Rust system_dfl_wq wrapper, and the SEP, Asahi DRM and binder work moved off the deprecated system_wq. Their AOP change is not carried: the 070-audio AOP driver already registers its services on its own ordered queue. Adaptations, each with a [ryan: ...] note in its commit: - the AOP lid tests go after the parser tests, where 7.2.10 has the name tests (context only); - trusted_key_core_done goes next to 7.2.10's trusted_key_source_migratable (context only); - the defconfig is regenerated, not picked; its note lists the 62 lines that differ from the 7.1 file, none of them a J700 setting. Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SEP review follow-ups: mailbox DMA ordering, hwrng sleep hold, trusted-keys init
These are follow-ups to the merged SEP work in #350, #351 and #352. They
close the MINOR findings left open by the reviews of those PRs. There are
seven small commits on aurora-wip
f001934351ed, each a single logicalchange. The four that fix a real defect carry a
Fixes:tag.Mailbox ordering (from the SBIO header review, MINOR-3)
smp_*()ones.dma_wmb()before every mailbox send, anddma_rmb()in the receive callback. This is the same contract RTKit has with the same mailbox.dma_wmb():dma::Coherent, so there is nodma_sync_*_for_device()call to order them. A streaming mapping would get its ordering from that call.dma-coherent, so the buffers are mapped Normal-NC and need no cache maintenance. Only the order of the stores matters.dma_wmb()isdmb oshst. That is the barrierwritel()itself places before its MMIO store (__io_bw()). Sodma_wmb()followed by the relaxed doorbell is exactlywriteq(), without the fulldsbofwmb().Fixes: bac0846c9087hwrng sleep hold (from the hwrng sleep-hold review, MINOR-1..3)
Fixes: 1b0451249175/dev/hwrngread got-EAGAINat once, for up to 60 s.-ETIMEDOUT, as an unanswered request does. A signal returns-ERESTARTSYS.Fixes: f771659aba5e,1b0451249175trusted-keys init order (from the trusted-keys init-order review)
trusted.rng=rules the fake out,trusted.source=rules it out, or a source holds the slot.Fixes: 4f15946bdd60migratableis writable, so the fault the review found on 7.2 does not happen here.-EBUSY.__ro_after_initwrites on 7.2.Fixes:tag, since no caller hits this today.Testing
TRUSTED_SOURCE_KUNIT_TEST=yon arm64 QEMU with LLVM:drivers/soc/appleandsecurity/keys/trusted-keys.--strict: 0 errors.PROTOCOL.mdin the artifacts.The exact numbers are in the report.
🤖 Generated with Claude Code
Qualification
writeq()and match RTKit's contract on the same mailbox. Every SEP send goes throughSepData::ring()./dev/hwrngreader got 37/37 reads through 3× s2idle plus 3×pm_test=platform;