Skip to content

soc: apple: sep: order the mailbox buffers, harden the hwrng sleep hold; trusted-keys slot rule - #357

Merged
RyanTheTide merged 7 commits into
aurora-wipfrom
fix/sep-followups-20261018
Oct 17, 2026
Merged

RyanTheTide merged 7 commits into
aurora-wipfrom
fix/sep-followups-20261018

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

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 logical
change. The four that fix a real defect carry a Fixes: tag.

Mailbox ordering (from the SBIO header review, MINOR-3)

  1. rust: sync: barrier: Add dma_wmb() and dma_rmb()
    • Rust bindings for the two barriers, next to the existing smp_*() ones.
    • Unlike those, they do not depend on SMP.
  2. soc: apple: sep: Order the shared buffers against the mailbox
    • Adds dma_wmb() before every mailbox send, and dma_rmb() in the receive callback. This is the same contract RTKit has with the same mailbox.
    • Why dma_wmb():
      • The buffers are dma::Coherent, so there is no dma_sync_*_for_device() call to order them. A streaming mapping would get its ordering from that call.
      • The SEP node is not dma-coherent, so the buffers are mapped Normal-NC and need no cache maintenance. Only the order of the stores matters.
      • On arm64, dma_wmb() is dmb oshst. That is the barrier writel() itself places before its MMIO store (__io_bw()). So dma_wmb() followed by the relaxed doorbell is exactly writeq(), without the full dsb of wmb().
    • Fixes: bac0846c9087

hwrng sleep hold (from the hwrng sleep-hold review, MINOR-1..3)

  1. soc: apple: sep: Test the hwrng and sleep glue (MINOR-3)
    • Moves each glue decision into a small function and tests it in KUnit. The decisions covered are:
      • the drain on the way to sleep, and its limit;
      • the order of the sleep holds, and how they roll back;
      • the mapping of a mailbox timeout to "full";
      • the hwrng read's result;
      • the survey's attempt count.
    • The sleep-event order is tested through a trait that a test fake implements.
    • There is no behaviour change.
  2. soc: apple: sep: Write no entropy request once the system starts to sleep (MINOR-1)
    • The hold is now checked again with the mailbox lock held, just before the request is written.
    • Without this, a sender that queued behind other senders, such as a Touch ID capture, could outlast the 3 s drain and then write its request into the suspend.
    • With the check, only a request already being written can still go out. That request's 500 ms room wait plus its 2 s reply wait fit inside the 3 s drain, so the bound now holds.
    • Fixes: 1b0451249175
  3. soc: apple: sep: Let a blocking hwrng read wait while requests are held (MINOR-2)
    • Before, a blocking /dev/hwrng read got -EAGAIN at once, for up to 60 s.
    • Now it waits for the hold to end, for at most the 2 s request timeout. It is woken by the retry time, by the resume, or by the enclave answering again.
    • If the hold has not ended by then, the read fails with -ETIMEDOUT, as an unanswered request does. A signal returns -ERESTARTSYS.
    • The enclave is still never asked while requests are held.
    • Fixes: f771659aba5e, 1b0451249175

trusted-keys init order (from the trusted-keys init-order review)

  1. KEYS: trusted: Skip after_core_init for the reason it cannot run (MINOR-2)
    • The test now looks at the init's result before it decides to skip.
    • It skips with the real reason: trusted.rng= rules the fake out, trusted.source= rules it out, or a source holds the slot.
    • Any other result is a failure.
    • Fixes: 4f15946bdd60
  2. KEYS: trusted: Give the slot to a built-in source only from init_trusted() (MINOR-1)
    • On this 7.1 tree migratable is writable, so the fault the review found on 7.2 does not happen here.
    • There is still a latent hazard on 7.1. Called again from a test, a built-in source that initialises would take the slot for good, because unregister withdraws only a registered source. The dispatch would then be left on freed test memory.
    • With this change, only the first call can give a built-in source the slot. A later call returns -EBUSY.
    • The same check comes before the __ro_after_init writes on 7.2.
    • No Fixes: tag, since no caller hits this today.

Testing

  • KUnit, run with the SEP Rust r-config plus TRUSTED_SOURCE_KUNIT_TEST=y on arm64 QEMU with LLVM:
    • the head passes all cases;
    • each behaviour fix has a mutant that brings back the old behaviour, and that mutant fails the new case.
  • The Neo build uses the published config. It has 0 warnings, and 0 warnings with W=1 on drivers/soc/apple and security/keys/trusted-keys.
  • checkpatch --strict: 0 errors.
  • The leak scan of the real diff: 0 hits.
  • No agent files in the diff.
  • Not done: the Neo boot. It is still to come; the run kit is PROTOCOL.md in the artifacts.

The exact numbers are in the report.

🤖 Generated with Claude Code

Qualification

  • Review: independent, PASS (t-2509). 0 BLOCKER/MAJOR, 2 MINOR (doc-comment wording), 4 NIT.
    • The barriers are equivalent to writeq() and match RTKit's contract on the same mailbox. Every SEP send goes through SepData::ring().
    • The lock order is mbox → control, with no cycle. The 3 s drain bound holds: 500 ms mailbox room + 2000 ms control < 3000 ms.
    • The blocking wait is interruptible and freezer-safe.
  • MacBook Neo (7.1.12-aurora-sepfu-g75890a68, one-shot):
    • Touch ID restored on the first attempt;
    • hwrng registered after the ready-phase survey;
    • trusted-key add/pipe/load OK;
    • 4096-byte blocking reads OK;
    • a background /dev/hwrng reader got 37/37 reads through 3× s2idle plus 3× pm_test=platform;
    • 0 WARN, 0 in-flight/no-reply/Malformed/status-NONE lines.
  • Build: the published config builds with 0 warnings, and W=1 is clean. KUnit: 181/0. Each of 6 mutants fails only its target case.
  • M1 Pro, the original hwrng repro machine: the rerun is scheduled as a follow-up check.

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
RyanTheTide merged commit 23ff1ec into aurora-wip Oct 17, 2026
@RyanTheTide
RyanTheTide deleted the fix/sep-followups-20261018 branch October 17, 2026 15:55
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant