Skip to content

soc: apple: sep: hold hwrng requests across system sleep and recover a timed-out entropy exchange - #351

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
fix/sep-hwrng-suspend
Oct 17, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
fix/sep-hwrng-suspend

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

After a suspend that overlapped an SEP entropy request, /dev/hwrng stopped working and the SEP mailbox stopped answering until reboot. That mailbox is the same inbox the key store, Touch ID and xART use. It was seen on the M1 Pro during pm_test=platform.

Causes (from code and the M1 Pro trace)

  1. The hwrng fill kthread isn't freezable, and nothing held it back. It could send GET_ENTROPY while devices were suspended. On the M1 Pro, the request timed out exactly inside the platform hold.
  2. Entropy replies weren't paired with requests. A reply that arrived after its request had timed out was taken as the answer to the next request, so every answer after that was one request late.
  3. Nothing backed off. The core retried every 10 s, and each retry added another message to the shared inbox. Once the inbox was full, every send to the enclave timed out after 500 ms.

Fix (2 commits)

  1. Recover the entropy exchange after a request times out.
    • Requests not yet answered are counted. Only the reply that settles the newest request is used, and earlier late replies are dropped.
    • After a timeout or a full mailbox, requests are held back with -EAGAIN, starting at 1 s and doubling up to 60 s. A late reply lifts the hold at once.
    • KUnit apple_sep_control covers it, 9 cases.
  2. Hold entropy requests while the system sleeps.
    • On the existing PM notifier (suspend, hibernate, restore), new requests are refused, and one in flight gets up to 3 s to be answered while the enclave can still answer.
    • The hold is released on POST and on a failed prepare.
    • The PM notifier is used because the Rust platform driver binding has no PM ops. The notifier window contains the whole device-suspend window. 2 more KUnit cases.

Testing

  • Review: independent, PASS (0 BLOCKER/MAJOR). Four MINOR follow-ups:
    • the 3 s drain bound isn't guaranteed;
    • blocking readers get -EAGAIN during backoff;
    • the glue code has no tests;
    • rustfmt, which is fixed in these commits.
  • M1 Pro, the original repro:
    • 3 × pm_test=platform with the SEP feeding the kernel RNG: afterwards /dev/hwrng gives 64 B in 27 ms and the SEP mailbox answers. Before the fix, every read timed out in 0.5 s and the mailbox was silent.
    • 5 more series (15 suspend cycles): /dev/hwrng and the mailbox stay healthy.
    • Touch ID set up on 5 of 5 cold boots, and the xART ledger shows 0 of 14 changed.
  • MacBook Neo:
    • a background /dev/hwrng reader ran through 3 × 60 s s2idle and 3 × pm_test=platform: 97/97 reads returned 32 B;
    • 0 hwrng error or backoff lines, the mailbox kept answering, and Touch ID stayed OK;
    • 0 WARN.
  • Tree: both machines ran the SEP fix integration kernel (aurora-wip 545d7e45 + this + the trusted-key init-order fix + the SBIO header-landing fix).
  • Build: the published config builds with 0 warnings. KUnit r passes 142/144 (2 deliberate skips), and apple-dcp run.py passes 55/55.

🤖 Generated with Claude Code

Every GET_ENTROPY request carries the same reserved tag, and the exchange
ends as soon as its waiter gives up. A reply that comes after its request
timed out is then taken as the answer to the next request, and from there
on every answer is one request late. Nothing stops the hwrng fill thread
from asking again either. Each retry is one more message the enclave is
not reading, in the inbox it shares with the key store, Touch ID and the
xART service, and once that is full every send to the enclave waits
500 ms for room and fails.

Count the requests that the mailbox took and the enclave has not
answered. The enclave answers in order, so only the reply that settles
the newest one is its answer, and earlier replies are dropped. After a
request goes unanswered, or the mailbox has no room for it, hold further
requests back with EAGAIN for 1 s, doubling with each failure up to 60 s,
and resume as soon as a late reply shows that the enclave is answering
again. A request made once the wait is over counts what is still owed as
lost, so a reply that never comes cannot stop the channel for good.

A held-back hwrng read returns -EAGAIN and is not logged as a failure;
the core tries again 10 s later. A held-back survey attempt is not
counted against the five the survey gets.

KUnit tests (apple_sep_control) cover a reply in time, a late reply, a
reply that comes after the retry, a lost reply, a full mailbox, a failed
send, the back-off and a request abandoned for removal.

Fixes: bac0846 ("soc: apple: add Apple SEP driver (Touch ID and key sealing)")
Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The hwrng fill thread is not frozen for system sleep, and nothing in the
driver held it back, so it could ask the enclave for entropy while devices
were suspended. On an M1 Pro (T6000) one such request, sent during the
5 s pm_test=platform hold, was never answered. After the resume the
enclave sent nothing more and did not read its mailbox again: every send
timed out waiting for room, so /dev/hwrng stayed dead until reboot, and
no other enclave service could be reached either. With the hwrng source
deselected over three suspend cycles the enclave kept working, so it
survives the sleep itself; it does not survive a request in that window.

Hold entropy requests from the start of a suspend or hibernation until
its end is reported, on the PM notifier that already holds Touch ID
captures. Its window contains the whole of device suspend. A request
already sent is first given time to be answered, which the enclave can
still do then. A read in the window returns -EAGAIN, which the core treats
as a source with nothing to give, and it tries again after the resume.

KUnit tests cover a read while the system sleeps and a request in flight
when the sleep begins.

Fixes: bac0846 ("soc: apple: add Apple SEP driver (Touch ID and key sealing)")
Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide merged commit a6d2a12 into aurora-wip Oct 17, 2026
@RyanTheTide
RyanTheTide deleted the fix/sep-hwrng-suspend branch October 17, 2026 10:01
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 #6
assembly, stacked on top-up #5 f34b5ae so that it is a
fast-forward of the published branch. The second parent is that
canonical assembly; the tree is identical to it.

Ported from aurora-wip, each with cherry-pick -x and its original
author:
- #347 KUNIT_ALL_TESTS=m modpost (3): the xHCI platform tests call the
  interrupt handler through the HC driver (170-usb), and module
  descriptions for the CD321x PM tests (170-usb) and the drm/apple
  KUnit test modules (200-dcp).
- #349 the CD321x IRQ test waits for the resume read before checking
  its retry (170-usb).
- #350 trusted keys keep a source that registers before
  init_trusted() (100-sep). Its test commit is already carried as
  76d70cc and is not duplicated.
- #351 SEP hwrng: recover the entropy exchange after a request times
  out, and hold entropy requests while the system sleeps (100-sep).
- #352 SEP SBIO: take a reply header only once it has landed, and
  poison it again once read and before each host send (100-sep).

Adaptations, each with a [ryan: ...] note in its commit:
- trusted-type.h: context only, the declaration goes above the
  7.2.10 CONFIG_TRUSTED_KEYS_DEBUG block;
- the CD321x IRQ test takes its data from tipd_hw_variants[], and its
  retry loop replaces the 7.2.10-only fix of the same race in
  11e0ce6.

The Co-Authored-By lines of the three #347 commits are dropped.

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