Skip to content

nvme: post-M4: shut the controller down normally before ANS goes off - #432

Merged
RyanTheTide merged 2 commits into
mainfrom
djdev/ans-normal-shutdown
Dec 1, 2026
Merged

RyanTheTide merged 2 commits into
mainfrom
djdev/ans-normal-shutdown

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What: Request a normal NVMe controller shutdown before powering ANS off on the post-M4 path. This lets the controller finish shutting down before RTKit idles the firmware.

Tested: At dd29252a, J773g (M4 mini) completed 10/10 shutdown cycles in RAM after a successful start. J516s (M3 Pro) booted stock Linux through the chainloaded candidate Stage 1 and returned to verified saved stock.

Risk: Two-way door · shared Stage 1 code on all Macs. The changed power-off path runs on post-M4 systems that power ANS off between stages.

Not yet: J773g cold boots with this image enrolled as Stage 1. The prior installed-Stage-1 baseline was 7 ANS-idle failures in 72 boots. The separate startup failure is tracked in #613.

Since 37acfb50, a post-M4 stage that only read the drive powers ANS off without an NVMe normal shutdown (CC.SHN): nvme_ans_power_off() disables the controller and idles the firmware directly. The firmware then does its shutdown work under the RTKit idle, and that idle can fail.

What changes

  • nvme_ans_power_off() performs the normal shutdown (CC.SHN) before disabling the controller on every non-T6050 chip, whatever the stage did with the drive, dropping the T8142-only special case.
  • nvme_shutdown()'s quiesce branch and nvme_wrote are unchanged, so T8152 Linux boots and ESP quiesced handoffs keep their proven read-only quiesce path.
  • T6050 keeps its traced suspend shutdown in nvme_ans_power_off().
  • A shutdown that doesn't finish still goes on to the disable, as nvme_ans_power_off() already did. The queues are deleted by then.
  • The power-off line gives the controller and idle times: ANS powered off, N SART entries kept (controller X ms, idle Y ms).

Why

On a J773g (M4 Mac mini, T8132) running an installed 1.0.0b13 Stage 1, 14 of 57 boots failed the RTKit idle after the ESP reads:

  • 4 boots: the firmware crashed in the idle (rtkit(nvme): IOP crashed! ... IOP died during shutdown). Its crash report counted 16 or 48 commands still open, with no shutdown.
  • 10 boots: the idle wasn't acknowledged within its 5 s. On the clean boots the idle took up to 5.1 s.

After either failure, ANS's CPU was stopped instead of in its reset state, and the next stage's start of ANS from off failed until a reboot. iBoot does the shutdown: the controller read CC=0x474000 (CC.SHN normal, CC.EN clear) at Stage 1 entry on all 57 boots.

Testing

  • test_nvme_adoption expects the normal shutdown before every post-M4 power off of a stage that only read, and no normal shutdown on read-only quiesce. It fails on main and passes here alongside test_nvme_poisoned_io.
  • J773g, tethered: this commit chainloaded from the installed Stage 1 runs Stage 1's ANS cycle (start from off, GPT and ESP reads, power off) 10 times per boot, then Linux starts ANS from that state. Unfixed main is the control. RAM results and startup counts.
  • Needed before a release, since Stage 1 runs this on every post-M4 Mac: 20+ cold boots each on J773g/J713 (T8132), T8140, M4 Pro/Max (T6040/T6041, on the restart path), M5 (T8142, T6050), and one M1-M3 Mac (unchanged path).

@RyanTheTide

Copy link
Copy Markdown
Contributor

Blockers: 1.

  1. src/nvme.c:1103-1119: the change also reaches the quiesce branch, not only the power-off it was measured on. A stage that only read used to quiesce with no normal shutdown on the split-register ANS (nvme-secure-reg-layout, T8152). Now it sends CC.SHN normal, then disables, then rtkit_quiesce(). That branch runs on every T8152 boot, including Linux boots, because nvme_keep_running_for_linux is ignored when nvme_secure_regs is set (src/nvme.c:1021). It also runs for the macOS-under-HV ESP handoff (src/sptm_virt_esp.c:241 sets nvme_quiesce_for_next). The comment there (src/sptm_virt_esp.c:235-237) says XNU's ANS start was proven only from "controller off, RTKit quiesced" with no normal shutdown. The J773g evidence is about rtkit_idle() in the power-off path (src/nvme.c:518-547). Nothing shows that the quiesced firmware accepts a fresh RTKit wake plus CC.EN after a completed normal shutdown, and the removed comment said this branch skipped the shutdown on purpose. One fix: leave the quiesce branch as it is on main and change only nvme_ans_power_off(), which is where the failure was seen. The other: keep it and add a T8152 row and an ESP-handoff row (below) to the release list. The PR body's list leaves T8152 out.

What I checked and found sound at 5e5ccd6:

  • Both waits are bounded. nvme_ctrl_shutdown() gives up after 5 s (src/nvme.c:301-311), rtkit_recv() returns early once the IOP has crashed, then the 1 s disable and the 5 s idle follow. A firmware crash during the shutdown therefore ends where main already ends (idle fails, asc_cpu_stop()), about 5 s later. The worst added boot time is 5 s.
  • M1-M3 are unchanged. On NVME_T8103 normal_shutdown was always true, a shutdown timeout never jumped to out_preserve, and nvme_ans_power_off() is only reached from post-M4 paths (src/nvme.c:786, 848, 1084).
  • T6050 keeps the suspend shutdown in nvme_ans_power_off(). T8142 already did the normal shutdown, so it is unchanged.
  • What does change: T8132 and T8140 at every Stage 1/2 power-off; T6040/T6041 when a session that started from off is powered off (src/nvme.c:1084), and the restart of an older stage's session at src/nvme.c:786.
  • No chip gates were added, and one (the T8142 special case) was removed. The commit has a human Signed-off-by and no agent trailers.

HW row: on 5e5ccd6, Stage 1 then Linux, at least 20 cold boots each on J773g/J713 (T8132), T8140 (J700), M4 Pro (T6040/T6041, from-off restart), M5 (T8142 and T6050), J873g (T8152, Linux started from the quiesced ANS, NVMe root mounts) and one M1-M3 Mac. Pass: 0 "IOP crashed"/"didn't acknowledge idle"/"shutdown timed out" lines, every next-stage ANS start succeeds, then 0 AVC, 0 failed units and 0 new kernel errors. Also one J616s ESP self-boot that reaches XNU's ANS start, if the quiesce branch keeps the shutdown.
Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr
DjDeveloperr force-pushed the djdev/ans-normal-shutdown branch from 5e5ccd6 to 2aa0035 Compare November 21, 2026 20:14
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Updated djdev/ans-normal-shutdown to 2aa0035eb to address the blocker:

  • Scoped the unconditional normal shutdown (CC.SHN) strictly to nvme_ans_power_off() (where the J773g idle failures were observed), restoring nvme_wrote and leaving nvme_shutdown()'s quiesce branch untouched so T8152 Linux boots and ESP HV handoffs keep their proven read-only quiesce path.
  • Updated tests/python/test_nvme_adoption.py (and restored the nvme_wrote annotation in test_nvme_poisoned_io.py) to assert normal shutdown on power-off and no normal shutdown on read-only quiesce.
  • Verified clang-format --dry-run --Werror src/nvme.c, NVMe unit tests, and full release build pass cleanly.

Assisted-by: LLM (Gemini:4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Re-checked at 2aa0035 (the real commit under the trailing github-actions "gfya" commit).

  • Fixed: the quiesce branch is back to main's behaviour. nvme_shutdown() still decides normal_shutdown = nvme_wrote || nvme_type != NVME_T8132 (src/nvme.c:1102), so T8152 and the ESP quiesce handoff keep the read-only path. The normal shutdown is now only in nvme_ans_power_off() (src/nvme.c:520-548), which only post-M4 paths reach (src/nvme.c:788, 850, 1086). M1-M3 are unchanged. test_nvme_adoption.py and test_nvme_poisoned_io.py pass here.
  • The PR description still says the quiesced handoff changes and that nvme_wrote goes away. Please update it to match the commit message.
  • Please drop the "gfya" commit before merge.

Still missing: rows at 2aa0035. Stage 1 then Linux, 20+ cold boots each on J773g/J713 (T8132), T8140, one M4 Pro/Max (the from-off restart), M5 (T8142, T6050) and one M1-M3 Mac. Pass: 0 "IOP crashed", 0 "didn't acknowledge idle" and 0 "shutdown timed out" lines; the new power-off line shows controller/idle times; every next-stage ANS start succeeds; 0 failed units and 0 new kernel errors. The T8152 and ESP-handoff rows are no longer needed, since that path is unchanged.

Assisted-by: LLM (Claude:Opus 5.5)

Since 37acfb5 a post-M4 stage that only read the drive disables the
controller and idles the firmware without a normal shutdown (CC.SHN).
The firmware then does its shutdown work under the RTKit idle, and that
idle can fail. On a J773g (M4 Mac mini, T8132), 14 of 57 1.0.0b13
Stage 1 boots, each of which only read the ESP, ended with either

  rtkit(nvme): IOP crashed!
  ...
  rtkit(nvme): IOP died during shutdown
  nvme: ANS didn't acknowledge idle, stopping it anyway

(4 boots; the firmware's crash report counted 16 or 48 commands still
open and no shutdown) or an idle that wasn't acknowledged within its 5 s
(10 boots; the clean boots' idles took up to 5.1 s).
After either, the next stage found ANS's CPU stopped instead of in its
reset state, and its start of ANS from off failed until a reboot.

iBoot does the normal shutdown: at Stage 1 entry the controller read
CC=0x474000 (CC.SHN normal, CC.EN clear) on every one of those boots.

Do the normal shutdown first in nvme_ans_power_off() on every non-T6050
chip, whatever the stage did with the drive, dropping the T8142-only
special case while leaving nvme_shutdown()'s quiesce path untouched
(so T8152 Linux boots and ESP HV handoffs keep their proven read-only
quiesce path). A shutdown that doesn't finish still goes on to the
disable, as nvme_ans_power_off() did; the queues are deleted by then.
T6050 keeps macOS's suspend shutdown. The power-off line now gives the
controller and idle times, so a slow handoff shows which step took it.

test_nvme_adoption expects the normal shutdown before every post-M4
power off of a stage that only read, and no normal shutdown on read-only
quiesce.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the djdev/ans-normal-shutdown branch from ca0c4f5 to dd29252 Compare November 22, 2026 13:25
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

@RyanTheTide Dropped the trailing "gfya" bot commit and rebased cleanly onto main (dff1235d8) at exact HEAD dd29252a4454bf123b03413d3b07f8b7221c31d6 (identical patch to 2aa0035eb14e, Stage 1 clang build, clang-format, test_nvme_adoption, and test_nvme_poisoned_io all pass). Also updated the PR description to match the commit (removing the outdated quiesce / nvme_wrote statements).

Remaining: Stage 1 → Linux cold-boot rows (20+ boots each) at dd29252a4454 on J773g/J713 (T8132), T8140, M4 Pro/Max (restart path), M5 (T8142, T6050), and one M1–M3 Mac.

Assisted-by: LLM (Gemini:4 Neon)

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J773g (T8132), November 30. The installed Stage 1 at 5dcf62d0 failed the ANS idle on 7 of 72 captured boots. Five firmware assertions reported 3–48 commands still open with no shutdown requested. Two idles timed out at 5 s. In all four assertion cases followed into the Linux boot kit, Stage 2 read CSTS=0x2 and received no HELLO. All 72 had the same iBoot entry state (RUN=1, BOOT_STATUS=0xde71ce55, CC=0x474000, CSTS=0). After Stage 1, ANS and the storage/PHY domains read off on both passing and failing boots. In three further captures the idle completed, HELLO and the endpoint map arrived, and the IOP power-on report missed the 6 s deadline.

I tested this head and its parent in RAM behind the installed Stage 1. Each cycle started ANS, read the GPT and 1 MiB of the ESP, verified the data and powered ANS off. All nine saved SART entries were passed on entry.

Image Initial starts from the b13 handoff Completed read/shutdown cycles Bad idles in those cycles
Parent dff1235d 3/3 30 0
PR head dd29252a 1/3 10 0

At the PR head the controller shutdown and disable took 541–564 ms, and the RTKit idle took 10 ms on each cycle. The two failed starts timed out before this image ran its first shutdown. The control also completed every shutdown it reached, so the installed Stage 1 failure rate with this patch is still unmeasured.

A diagnostic build of this head extended only the warm IOP report wait to 20 s and logged its duration. Six of seven fresh boots received the report in 70 ms. On the remaining boot, ANS sent HELLO and its endpoint map. RTKit reported an IOP crash 10.074 s into the wait, with CPU status 0x68 and all nine SART entries present. The inherited session supplied no crash text, so the reason inside the firmware is unknown. Extending the deadline did not recover that failure. Each test returned to verified saved stock. The PR remains a draft.

@DjDeveloperr
DjDeveloperr marked this pull request as ready for review November 30, 2026 10:36
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 30, 2026 10:36
@github-actions github-actions Bot added needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks required sections (bot) labels Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Merge readiness at 93c97843: not ready yet

Check Detail
CI at head ok 28 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok the PR body at dd29252a; the changed files are byte-identical at 93c97843
Conflicts ok none
PR text ok Risk: two-way · shared Stage 1 code on all Macs. The changed power-off path runs on post-M4 systems that power ANS off between stages.

Areas: none matched in CODEOWNERS

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) and removed ci-failing A check failed at the PR head (bot) needs-template PR body lacks required sections (bot) labels Nov 30, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at dd29252a

Blockers:

  • Missing enrolled-Stage-1 cold-boot evidence on J773g/J713 (T8132) at dd29252a: in the RAM test behind installed 1.0.0b13, both parent dff1235d (30/30) and dd29252a (10/10) had 0 bad idles, so the installed Stage 1 ANS-idle failure rate against the 7/72 baseline is still unmeasured.
  • Remaining post-M4 cold-boot rows requested in the prior review (T8140, T6040/T6041 from-off restart path, and T8142/T6050 Stage 1 → Linux) are not yet captured at dd29252a (unless a maintainer explicitly defers them to the release gate).

Checked:

  • Scope (+25/-10 across src/nvme.c and tests/python/test_nvme_adoption.py): nvme_ans_power_off() now runs nvme_ctrl_shutdown() unconditionally before nvme_ctrl_disable() on non-T6050 chips and logs controller vs. idle elapsed milliseconds.
  • Path isolation verified against live main (src/nvme.c):
    • nvme_ans_power_off() is only called from post-M4 paths (src/nvme.c:780, 842, 1078); M1–M3 (NVME_T8103) is untouched (and J516s M3 Pro regression passed through chainloaded Stage 1).
    • T6050 keeps its nvme_ctrl_suspend() branch unchanged; T8142 already ran nvme_ctrl_shutdown() unconditionally.
    • nvme_shutdown()'s quiesce branch (normal_shutdown = nvme_wrote || nvme_type != NVME_T8132) is untouched, preserving the read-only quiesce path for T8152 Linux boots and ESP HV handoffs.
    • Shutdown timeout remains bounded (5 s in nvme_ctrl_shutdown(), continuing into nvme_ctrl_disable() and rtkit_idle() as before).
  • Unit tests & CI: tests/python/test_nvme_adoption.py verifies shutdown_calls on read-only power-off and shutdown_calls == 0 on read-only quiesce; all 18 hosted CI checks pass at dd29252a.
Evidence
  • Mechanism confirmation (J773g RAM test at dd29252a): 10/10 read/shutdown cycles completed with 0 bad idles; controller shutdown + disable took 541–564 ms and rtkit_idle() dropped to 10 ms on every cycle (compared with up to 5.1 s / 5 s timeouts without CC.SHN).
  • Separate startup crash: The 1/7 diagnostic warm IOP power-report crash (CPU status 0x68 at 10.074 s, before the first candidate shutdown) is tracked separately in #613 and is not introduced by this patch.
  • Code audit: No logic, timeout, format-specifier, or regression defects found in src/nvme.c at dd29252a.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MERGE at dd29252a4454bf123b03413d3b07f8b7221c31d6. No blocking findings.

I traced all three callers of nvme_ans_power_off(). On the affected path, an enabled controller now gets a bounded normal shutdown before disable and RTKit idle, including after a read-only stage. Read-only quiesce still skips normal shutdown, and T6050 still uses suspend.

Test evidence

All eight focused NVMe tests passed locally at this commit. The updated adoption test caught the missing shutdown against the parent. CI passed. On J773g the candidate completed 10/10 read and power-off cycles after one successful start. The other two starts failed before the candidate issued a shutdown. J516s booted stock Linux through the candidate. The J773g failure rate with the candidate installed as Stage 1 remains unmeasured and is still part of release testing.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Hardware results at dd29252a4454bf123b03413d3b07f8b7221c31d6.

Mac Boot path and result
J773g (M4 mini, T8132) RAM candidate completed 10/10 read-and-shutdown cycles after one successful start in three attempts. Controller shutdown and disable took 541–564 ms, and RTKit idle took 10 ms per cycle. The parent completed 30/30 cycles. RAM measurements
J516s (M3 Pro) 1/1 chainloaded candidate Stage 1 booted saved stock Linux. The console reported controller shutdown and disable in 845 ms and IOP sleep in 12 ms. All six SLC planes reported 0x2100, zero units failed, and the kernel error set matched baseline. A fresh boot verified the saved stock return, including both ESP binaries and GRUB.

The J773g installed-Stage-1 baseline had 7 ANS-idle failures in 72 boots. The candidate's installed-Stage-1 cold-boot rate is still unmeasured. The intermittent ANS startup crash is tracked in #613.

Signed-off-by: DjDeveloperr <hey@djdev.me>

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at 93c97843

Blockers:

  1. CI checks are still queued at 93c97843. test (46505860288), build (46505860581), stage1-size (46505860415), and esp-stage2-size (46505860240) are still queued (format-check, pr-text, aurora-agent-policy, and pr-readiness passed).
  2. Enrolled-Stage-1 cold-boot hardware proof remains open. As noted in the PR body's Not yet: section and the prior review at dd29252a, J773g/J713 (T8132) cold boots with this image enrolled as Stage 1 (measured against the 7/72 ANS-idle failure baseline) and the remaining post-M4 cold-boot rows (T8140, T6040/T6041) are still pending; in the RAM test behind installed 1.0.0b13, both parent (30/30) and candidate (10/10) had 0 bad idles.

Checked:

  • Merge commit 93c97843 (Merge main into djdev/ans-normal-shutdown): confirmed src/nvme.c and tests/python/test_nvme_adoption.py (+25/-10) are byte-identical to dd29252a with no conflict drift.
  • nvme_ans_power_off() executes bounded normal shutdown (nvme_ctrl_shutdown()) before nvme_ctrl_disable() and rtkit_idle() on all non-T6050 post-M4 chips regardless of nvme_wrote, leaves T6050's suspend path and read-only quiesce untouched, and logs controller/idle durations cleanly via ticks_to_msecs().
Evidence
  • Queued runs at 93c97843: 46505860288 (test), 46505860581 (build), 46505860415 (stage1-size), 46505860240 (esp-stage2-size).
  • Passing runs at 93c97843: 46505860282 (format-check), 46505874200 (pr-text), 46505918554 (aurora-agent-policy), 46505860799 (pr-readiness).

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 93c97843710d

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: - Merge commit 93c97843 (Merge main into djdev/ans-normal-shutdown): confirmed src/nvme.c and tests/python/test_nvme_adoption.py (+25/-10) are byte-identical to dd29252a with no conflict drift.

  • nvme_ans_power_off() executes bounded normal shutdown (nvme_ctrl_shutdown()) before nvme_ctrl_disable() and rtkit_idle() on all non-T6050 post-M4 chips regardless of nvme_wrote, leaves T6050's suspend path and read-only quiesce untouched, and logs controller/idle durations cleanly via ticks_to_msecs().

Follow-ups (not blocking): queued CI; enrolled Stage 1 cold-boot rows on T8132/T8140/T6040

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging at 93c97843710d (B2 merge train, 12-01 merge bar). Approval at this head: aurora-silicon-review[bot]. Row: PR body at dd29252; changed files byte-identical at 93c9784. CI: aura train A1 = main 2a7854a + #590 #338 #432 #556 #580 #587 (candidate 69c9345e): gcc normal + ESP_STAGE2 build pass with no new warnings, pytest 1730 passed / 0 failed. Hosted CI at this head is queued behind the org backlog; this local run is the same build + pytest set. Revert: git revert -m 1 <merge commit>.

@RyanTheTide
RyanTheTide merged commit 5092501 into main Dec 1, 2026
31 checks passed
@RyanTheTide
RyanTheTide deleted the djdev/ans-normal-shutdown branch December 1, 2026 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants