Skip to content

nvme: apple: keep the RTKit buffers of ANS across a firmware sleep - #3381

Merged
RyanTheTide merged 2 commits into
aurora-silicon:mainfrom
olivergreg:fix/nvme-ans-rtkit-buffers-firmware-sleep
Dec 1, 2026
Merged

RyanTheTide merged 2 commits into
aurora-silicon:mainfrom
olivergreg:fix/nvme-ans-rtkit-buffers-firmware-sleep

Conversation

@olivergreg

@olivergreg olivergreg commented Nov 27, 2026 •

Copy link
Copy Markdown

What: Fixes #3379. Carries Fixes: 7dfc208abc18 (from PR #3172). On a firmware suspend-to-RAM, apple_nvme_suspend() idles a pre-M4 ANS without the warm sleep sequence and calls apple_rtkit_reinit(). That frees the shared RTKit buffers and removes their SART ranges while the idled firmware keeps its state. On wake, the saved SART entries are written back, re-admitting the freed ranges, and ANS keeps writing its syslog to freed memory. This adds apple_rtkit_reinit_retaining_buffers(), which resets the session's handshake state and leaves the shared buffers, their SART ranges, and the syslog geometry in place. ANS uses it for the idle before sleep and for the restart after wake. Ordinary controller resets still call apple_rtkit_reinit() and free the buffers as before. Nothing is allocated per sleep. The T8122 warm path does not reinitialise and is unchanged.

Tested: MacBook Pro 14" M1 Pro (T6000 / J314s), aura 1.0.0b17-g26c1a85b (aura main). Head b1ac1a3c adds a host-test fix with no behaviour change. The same code, as carried in #3534, ran the third row and more than 150 deep sleeps. Three rows:

Row Build, cmdline Ran Result
Clean boot control PR head 782fdd6c; pcie_aspm=off boot, then 3 × 1 GiB write + fsync + O_DIRECT read-back, sha256 compared 3/3 match (4.4–4.5 s). 0 SELinux denials, 0 failed units, 0 new kernel errors (only watchdog did not stop!, also on the stock kernel). 0 nvme errors or syslog_msg_buffer lines
Suspend/resume + I/O, stand-in for T8103 / J313 (same ans2 block and path; only T8103 reaches it on plain main) this commit, unchanged, on an out-of-tree T6000 suspend-to-RAM series on main cec0503a5a02; apple_sleep.deep=1 apple_sleep.untested_commit=1 pcie_aspm=off 5 RTC deep sleeps of 30 s, 1 GiB write + fsync + O_DIRECT read-back after each wake 5/5 match (4.7–5.2 s). 11 wakes on that boot incl. 1 h on battery; each logged ANS: on after the sleep. 0 syslog_msg_buffer lines (10 over 2 cycles without this commit), 0 nvme errors, 0 Bad page map, 0 SELinux denials. Other kernel errors and one failed desktop unit come from the out-of-tree series, none from NVMe or RTKit
Refused sleep after ANS idled, stand-in for T8103 this change as carried in #3534, on the T6000 series plus a test-only switch that refuses one sleep at .enter; default cmdline + apple_sart.dyndbg=+p 9 deep sleeps refused after .suspend had idled ANS (6 at .enter, 3 in a later device's suspend), 256 MiB write + fsync + O_DIRECT read-back after each 9/9: entries written back after a sleep that didn't happen, then ANS: on after the sleep. No apple-sart ... wrote line after any of them, so ANS asked for no new buffers. Read-back matched every time. 0 syslog_msg_buffer lines, 0 nvme errors

Risk: Two-way door. A plain revert restores the previous behaviour, with no UAPI, device-tree, on-disk, or firmware state changes. On main, T8103 (M1) is the only SoC that reaches the changed branch in drivers/nvme/host/apple.c; other pre-M4 SoCs using apple_nvme_t8103_hw will share it once they gain a sleep controller. T8122 (warm_sleep) and M4-and-later SoCs (needs_ioq_registers) do not run the changed lines. drivers/soc/apple/rtkit.c is shared by every Mac. apple_rtkit_reinit() keeps its signature and behaviour (keep_buffers = false), apple_rtkit_cold_restart() passes false, and only the two firmware-sleep call sites in apple.c call apple_rtkit_reinit_retaining_buffers(). Ordinary controller resets, suspend-to-idle, shutdown, removal, and crash-kernel takeover still free buffers as before. On T8103 (not yet run on hardware), the ANS buffers stay allocated and in the SART across sleep, a firmware that asks again receives its existing buffers, and ANS syslog records are printed again rather than dropped. The failure mode to watch for on T8103 is ANS requesting a buffer of a different size after wake, which apple_rtkit_common_rx_get_buffer() refuses with -EBUSY instead of allocating a new buffer.

Not yet: a T8103 (M1, J313) run and a row on another NVMe family (e.g. T8122, M4), since drivers/soc/apple/rtkit.c is shared by every Mac. I don't have that hardware. Also not exercised: a second ANS firmware version and a KUnit case for the retaining reinit.

Details

Evidence: On main's non-warm firmware-sleep path, running on an M1 Pro (T6000, J314s) with a T6000 sleep series, RTKit: received syslog message but no syslog_msg_buffer appeared at every sleep and wake (10 times over 2 sleep cycles). No oops was captured on main's path itself, though the code frees the buffers with dma_free_coherent() and re-admits their SART ranges on wake. On the same machine running an out-of-tree T6000 sleep sequence that freed and re-admitted the RTKit buffers the same way, repeated deep sleeps produced an oops with BUG: Bad page map and page-table entries holding ASCII text from the ANS firmware's syslog.

Code changes:

  • drivers/soc/apple/rtkit.c: the buffer frees stay in apple_rtkit_reset_session() under a new keep_buffers argument.
  • drivers/nvme/host/apple.c: call apple_rtkit_reinit_retaining_buffers() after apple_rtkit_idle() in the non-warm firmware-sleep branch of apple_nvme_suspend(), and in apple_nvme_reset_work_impl() when fw_sleep (anv->fw_sleep_restart) is set.
  • include/linux/soc/apple/rtkit.h: declare apple_rtkit_reinit_retaining_buffers().

Post-wake behaviour and design notes:

  • A firmware that resumes with its state sends no buffer request and writes to memory that is still allocated and admitted. apple_rtkit_syslog_rx_log() reads and prints its records again.
  • A firmware that starts from scratch asks again. apple_rtkit_common_rx_get_buffer() answers a matching request for an endpoint that already has a buffer with that buffer, and SYSLOG_INIT replaces the geometry in apple_rtkit_syslog_rx_init().
  • apple_rtkit_warm_restart() was not reused because it sets rtk->warm_restart, keeps endpoint bitmaps, and does not restart endpoints, whereas this ANS wake performs a cold start with a full endpoint map.
  • No SoC, board, or firmware gate is added; the choice follows fw_sleep_restart.
  • A repeated CoreAnalytics buffer request would return -EBUSY in apple_rtkit_coreanalytics_get_buffer(); ANS does not set APPLE_RTKIT_NEEDS_COREANALYTICS.

For a suspend-to-RAM that goes through firmware, a controller without
the warm sleep sequence is put in IDLE, not shut down:
apple_nvme_suspend() saves the SART's entries, calls
apple_rtkit_idle() and then apple_rtkit_reinit(). The reinit frees
every buffer the firmware was given (syslog, crash log, ioreport,
oslog) with dma_free_coherent() and takes its range out of the SART.
On the wake the reset work calls apple_rtkit_reinit() once more,
writes the saved SART entries back, the freed ranges among them, and
starts ANS.

An idled ANS keeps its state. On T6000 (M1 Pro, J314s), after the
wake it sends HELLO, the kernel logs "RTKit: Initializing (protocol
version 12)", the endpoints are started, and the firmware then sends
syslog records without a buffer request or a SYSLOG_INIT before them:

  RTKit: received syslog message but no syslog_msg_buffer

logged at each sleep and each wake. The firmware still writes the
records' text to the buffer it had before the sleep, which the page
allocator has handed to someone else by then, and which the SART
admits again. With an out-of-tree T6000 sleep sequence that freed and
re-admitted the buffers the same way, ANS's syslog text was found in
a process's page tables ("Bad page map"), followed by an oops.

Keep the buffers for as long as the firmware may use them. Add
apple_rtkit_reinit_retaining_buffers(), which resets a session's
handshake state as apple_rtkit_reinit() does and leaves the shared
buffers, their SART ranges and the syslog geometry alone, and use it
for the idle before the sleep and for the restart after it.

Both ways the firmware can come back are then served by the code
that is there:

 - a firmware that resumes with its state never asks, keeps writing
   to memory that is still its own, and its syslog records are read
   and printed again;
 - a firmware that starts from scratch asks again. A request of the
   same size for an endpoint that has a buffer is answered with that
   buffer by apple_rtkit_common_rx_get_buffer(), and a SYSLOG_INIT
   replaces the geometry.

Nothing is allocated per sleep. The buffers are released as before
by apple_rtkit_free(), or by the apple_rtkit_reinit() of a
controller reset, which follows a shutdown the firmware
acknowledged.

The path is not specific to T6000: T8103 (M1) takes it as well. The
T8122 warm sequence does not reinitialize and is unchanged.

Fixes: 7dfc208 ("nvme-apple: idle ANS for a firmware sleep and start it cold after the wake")
Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com>
tools/asahi/rtkit-oslog-tests.py builds apple_rtkit_reset_session() and
apple_rtkit_reinit() on their own. The previous commit moved the buffer
frees and the mailbox start into two new static helpers, which the test
does not take, so it stopped linking.

Free the buffers inside apple_rtkit_reset_session() again, under
!keep_buffers, and start the mailbox in apple_rtkit_reinit() and
apple_rtkit_reinit_retaining_buffers() directly. No change in behaviour.

Signed-off-by: Oliver Gregory <59419317+olivergreg@users.noreply.github.com>
@olivergreg
olivergreg marked this pull request as ready for review November 28, 2026 21:11
@olivergreg
olivergreg requested review from a team as code owners November 28, 2026 21:11

@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: FINISH, then merge at b1ac1a3c

Blockers:

  • Hosted CI at b1ac1a3c: Pull-request CI workflows (apple-build, host-tests, kunit, pr-text) still need maintainer approval/run and a green pass at b1ac1a3c.
  • Target & cross-family lab hardware rows (noted under **Not yet:**): Still awaits a T8103 (J313) deep-sleep row (the SoC reaching the non-warm fw_sleep path on main) and a regression row on a second NVMe/RTKit family (T8122 or M4, since drivers/soc/apple/rtkit.c is shared across all Macs and T8122 refused warm sleep on main also enters if (fw_sleep) in apple_nvme_reset_work_impl()).

Checked:

  • Audited all three touched files (drivers/soc/apple/rtkit.c, include/linux/soc/apple/rtkit.h, drivers/nvme/host/apple.c) across both commits (782fdd6c, b1ac1a3c); zero code blockers found.
  • Verified apple_rtkit_reset_session(rtk, keep_buffers) cleanly retains all eight shared buffers, syslog_msg_buffer, syslog_n_entries, syslog_msg_size, and {syslog,crashlog,oslog}_inherited when keep_buffers == true, while resetting endpoints, power states, and completions; verified apple_rtkit_common_rx_get_buffer() re-answers matching repeated requests (goto reply), apple_rtkit_syslog_rx_init() safely replaces syslog_msg_buffer, and ordinary apple_rtkit_reinit() (keep_buffers = false) / apple_rtkit_free() release all retained buffers without leaks or double-frees.
  • Checked interaction with recently merged #3445 on main (t8122_warm_refused / anv->shmem_defer in drivers/nvme/host/apple.c): anv->shmem_defer is still cleared to false on both normal completion and out: error exit, so retaining buffers inside rtk on if (fw_sleep) is compatible with T8122 refused warm sleep as well.
  • Verified PR text (What/Tested/Risk Two-way door/Not yet), Fixes: 7dfc208abc18, and the thorough T6000 (J314s) stand-in boot, deep-sleep, and refused-sleep O_DIRECT SHA-256 verification rows.
Evidence
  • drivers/soc/apple/rtkit.c: apple_rtkit_reinit() and apple_rtkit_cold_restart() pass keep_buffers = false; apple_rtkit_reinit_retaining_buffers() passes keep_buffers = true and detaches RX / flushes rtk->wq if apple_mbox_start() fails.
  • drivers/nvme/host/apple.c: calls apple_rtkit_reinit_retaining_buffers() only in the !anv->hw->warm_sleep firmware-sleep suspend path and when fw_sleep is set in apple_nvme_reset_work_impl(); ordinary resets continue calling apple_rtkit_reinit().
  • Compatibility with main (#3445): on t8122_warm_refused, anv->fw_sleep_restart is set alongside anv->t8122_warm_refused, so apple_rtkit_reinit_retaining_buffers() keeps buffers inside rtk without invoking shmem_destroy while anv->shmem_defer is still cleared at exit.

@github-actions github-actions Bot added area:platform Area from CODEOWNERS (bot) 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) labels Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Merge readiness at b1ac1a3c: not ready yet

Check Detail
CI at head running running: KUnit jpeg, KUnit prores, KUnit scaler, KUnit macsmc, KUnit clpc, KUnit apple-spmi-nvmem
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok in the PR body at b1ac1a3c
Conflicts ok none
PR text ok Risk: two-way · . A plain revert restores the previous behaviour, with no UAPI, device-tree, on-disk, or firmware state changes. On main, T8103 (M1) is the only SoC that reaches the changed branch in drivers/nvme/host/apple.c; other pre-M4 SoCs using applenvmet8103hw will share it once they gain a sleep controller. T8122 (warmsleep) and M4-and-later SoCs (needsioqregisters) do not run the changed lines. drivers/soc/apple/rtkit.c is shared by every Mac. applertkitreinit() keeps its signature and behaviour (keepbuffers = false), applertkitcoldrestart() passes false, and only the two firmware-sleep call sites in apple.c call applertkitreinitretainingbuffers(). Ordinary controller resets, suspend-to-idle, shutdown, removal, and crash-kernel takeover still free buffers as before. On T8103 (not yet run on hardware), the ANS buffers stay allocated and in the SART across sleep, a firmware that asks again receives its existing buffers, and ANS syslog records are printed again rather than dropped. The failure mode to watch for on T8103 is ANS requesting a buffer of a different size after wake, which applertkitcommonrxgetbuffer() refuses with -EBUSY instead of allocating a new buffer.

Areas: area:platform

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

@RyanTheTide

Copy link
Copy Markdown
Contributor

I tested b1ac1a3c merged locally into Linux main 45d7f828. The merge was clean, touched only the three PR files and passed all 28 RTKit host cases, including the retained-session, restart, log handling and regression controls. The hosted run and J313 and second-family hardware checks remain open.

@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 b1ac1a3c079c

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: - Audited all three touched files (drivers/soc/apple/rtkit.c, include/linux/soc/apple/rtkit.h, drivers/nvme/host/apple.c) across both commits (782fdd6c, b1ac1a3c); zero code blockers found.

  • Verified apple_rtkit_reset_session(rtk, keep_buffers) cleanly retains all eight shared buffers, syslog_msg_buffer, syslog_n_entries, syslog_msg_size, and {syslog,crashlog,oslog}_inherited when keep_buffers == true, while resetting endpoints, power states, and completions; verified apple_rtkit_common_rx_get_buffer() re-answers matching repeated requests (goto reply), apple_rtkit_syslog_rx_init() safely replaces syslog_msg_buffer, and ordinary apple_rtkit_reinit() (keep_buffers = false) / apple_rtkit_free() release all retained buffers without leaks or double-frees.
  • Checked interaction with recently merged #3445 on main (t8122_warm_refused / anv->shmem_defer in drivers/nvme/host/apple.c): anv->shmem_defer is still cleared to false on both normal completion and out: error exit, so retaining buffers inside rtk on if (fw_sleep) is compatible with T8122 refused warm sleep as well.
  • Verified PR text (What/Tested/Risk Two-way door/Not yet), Fixes: 7dfc208abc18, and the thorough T6000 (J314s) stand-in boot, deep-sleep, and refused-sleep O_DIRECT SHA-256 verification rows.

Follow-ups (not blocking): hosted CI approval; J313 deep-sleep row (2nd family not required)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging at b1ac1a3c079c (B2 merge train, 12-01 merge bar). Approval at this head: aurora-silicon-review[bot]. Row: the hardware row at b1ac1a3 that the readiness check found (PR body or comment). CI: linux train L3 = main 429e7e1 + 28 approved PRs (candidate 45c2fa4b): Apple driver compile pass, Apple DTBs W=1 pass, planned KUnit dcp-fabric/dcp-topology/dcp-display/macsmc/spi-hid-apple/mt7932/asahi/sep pass; ave failed to build only because of #3153 (unused static helper under -Werror; #3153 held out) and passes with #3153 reverted from the candidate; host tests apple-dcp and apple-scaler-recovery pass. The rest of the hosted run is queued behind the backlog. Revert: git revert -m 1 <merge commit>.

@RyanTheTide
RyanTheTide merged commit fc0c76e into aurora-silicon:main Dec 1, 2026
30 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform Area from CODEOWNERS (bot) 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.

nvme: apple: firmware sleep frees the RTKit buffers ANS keeps writing to

2 participants