Skip to content

mfd: macsmc: Initialise the completions before the RTKit - #2935

Merged
RyanTheTide merged 3 commits into
mainfrom
hardening/macsmc-init-20261123
Nov 27, 2026
Merged

RyanTheTide merged 3 commits into
mainfrom
hardening/macsmc-init-20261123

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What: Initialise the Apple SMC completions, synchronization state and notification chain before either RTKit transport can call the driver. A message can arrive during probe.

Tested: ARM64 QEMU macsmc-parser at source identical to c9aa492328dc passed 21/21, including the early-message case and the four main-branch WKTP cases. The previous 38d93aa124a9 J316s standard transport boot bound all six children, returned 142 readings and passed a short s2idle. See the exact receipts below.

Risk: two-way door · any Mac using the Apple SMC, including Macs with the saved RTKit transport.

Not yet: Current-head saved-transport startup on J714s or J813, including normal sensor reads and event notifications. Current-head standard transport refresh on J316s or the closest shared Mac.

Evidence

The normal forward merge retains main's wake-reason and WKTP changes and the original early-initialisation behavior. The QEMU test source and committed source differ only in .github/CODEOWNERS. The staged diff passed strict checkpatch with zero findings. The old standard-transport row is documented in this PR's comments and is the baseline for the refresh.

apple_smc_probe() initialised init_done and cmd_done only after the
RTKit was initialised, woken and the SMC endpoint started. The receive
callback is live from the RTKit's initialisation on, and it completes
init_done for the first message on the SMC endpoint, so a message that
arrives before the probe got that far completes a completion that is
still all zeroes.

Initialise both completions together with the mutex, the spinlock and
the boot stage the receive callback checks, before the RTKit is
initialised, in a helper, apple_smc_init_state().

Add a KUnit case that takes the first message with only that state set
up.

Fixes: e038d98 ("mfd: Add Apple Silicon System Management Controller")
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at c7e6527.

  1. This resolves Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 finding 3. macsmc-test.c:414-415 now checks boot_stage == APPLE_SMC_BOOTING and !completion_done(), and the swait task_list asserts are gone.
  2. The patch changed from Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 (patch-id 5ba1dc97 vs 2fc51db9), and only in one respect: boot_stage = APPLE_SMC_BOOTING moved from after RTKit init into apple_smc_init_state() (macsmc.c:648). It now holds before either RTKit init, which is when apple_smc_rtkit_recv_early() (macsmc.c:526) can first run. The zeroed devm_kzalloc already held BOOTING (enum value 0), so behaviour is unchanged.
  3. Read, refusal and timeout paths are untouched, so fan, power and thermal protection are unaffected.

Rows owed at this head (none posted): a boot with SMC sensors and fans up on J616s and on one saved_enabled board (J714s or J813), as the body already lists.

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR checklist review at c7e6527a587ff12701034ea85ce639d37d49fad7.

Checklist Summary

  • §1 No model gating: Pass (pre-existing saved_enabled logic untouched).
  • §2 Production-ready: Pass.
  • §3 Safety: Blocker — RTKit callback state initialization is still incomplete (see below).
  • §4 Clean-room & attribution: Pass — human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by:, zero AI trailers in commit.
  • §5 Evidence: Pending — KUnit macsmc_early_message_test passes (swait internal asserts cleanly removed), but hardware rows (fleet82 boots + saved_enabled board J714s/J813) are still owed.
  • §6 Naming: Pass.
  • CI (at review time): host-tests green.

Blocking Finding

  1. Incomplete RTKit callback state initialization (drivers/mfd/macsmc.c:642-649 vs 715 and 582):
    apple_smc_init_state() states "The state the RTKit callbacks use. A message can arrive as soon as the RTKit is initialised, so this is set up before that." It moves mutex, lock, init_done, cmd_done, and boot_stage before devm_apple_rtkit_init*(), but leaves BLOCKING_INIT_NOTIFIER_HEAD(&smc->event_handlers); at macsmc.c:715 — after RTKit init, apple_rtkit_wake(), apple_rtkit_start_ep(), SMC_MSG_INITIALIZE, and wait_for_completion_timeout(&smc->init_done).

    Once the first SMC message sets smc->boot_stage = APPLE_SMC_INITIALIZED and calls complete(&smc->init_done), any asynchronous SMC_MSG_NOTIFICATION arriving before probe executes line 715 returns false from apple_smc_rtkit_recv_early() (macsmc.c:539-541), queues the RTKit worker, and runs apple_smc_rtkit_recv() -> blocking_notifier_call_chain(&smc->event_handlers, ...) (macsmc.c:582) on an uninitialized (all-zero) struct blocking_notifier_head (rw_semaphore), risking an oops/wait-list corruption or lockdep splat.

    Please move BLOCKING_INIT_NOTIFIER_HEAD(&smc->event_handlers); into apple_smc_init_state() (and optionally exercise an SMC_MSG_NOTIFICATION through apple_smc_rtkit_recv() in macsmc_early_message_test).

Verdict

REQUEST_CHANGES — close the remaining event_handlers init-order window in apple_smc_init_state(), and attach the owed §5 hardware rows.

@RyanTheTide RyanTheTide 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.

Independent source review at c7e6527a587f. The current initialization helper still leaves the notification chain for later in probe, while endpoint setup can already deliver asynchronous notifications. Initialize the notifier head with the other callback state and add a test that exercises the first notification before child devices are registered. Check the normal SMC sensor enumeration on J473 and J413.

The receive worker can dispatch a notification once the SMC sends its initial reply. Initialize the notifier chain with the rest of the callback state so it is ready before either RTKit path installs the callbacks.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

The follow-up at 38d93aa124a9b9dbb3e01c8269f2c08e40053c75 moves the blocking notifier initialization into apple_smc_init_state(). Both probe paths initialize the notifier before registering RTKit receive callbacks. The targeted arm64 QEMU suite passed 17/17 and strict checkpatch passed. Physical boots covering the standard and saved RTKit paths remain open.

@RyanTheTide RyanTheTide 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.

Re-review at 38d93aa. The added commit moves the blocking notifier head into the same initialization helper as the receive-side completions and stage. Both saved-session and ordinary RTKit initialization now occur after every field touched by an immediate event is initialized. The old post-endpoint init is removed, so it cannot discard an early notification chain. The early-reply test covers the boot transition. Keep the exact-head live SMC probe rows and the CI result distinct from the software check.

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR checklist re-review at verified live head 38d93aa124a9b9dbb3e01c8269f2c08e40053c75 (CI 1/1 green: Host tests / host-tests).

Prior Finding Verification & Technical Audit

  • Uninitialized event_handlers notifier head (Prior Blocker 1) — FIXED: 38d93aa124a9 moves BLOCKING_INIT_NOTIFIER_HEAD(&smc->event_handlers) into apple_smc_init_state() (macsmc.c:642-650), removing the late initialization after wait_for_completion_timeout(&smc->init_done).
  • Full RTKit callback pre-init audit: Verified every field touched by apple_smc_rtkit_crashed(), apple_smc_rtkit_shmem_setup(), apple_smc_rtkit_recv_early(), and apple_smc_rtkit_recv() (mutex, lock, init_done, cmd_done, event_handlers, boot_stage, dev, saved_enabled, sram, sram_base, and zero-initialized atomic_*/saved_*/msg_id) is initialized prior to both devm_apple_rtkit_init_m5_saved() and devm_apple_rtkit_init() (macsmc.c:681-684).
  • Resume/reset & error unwind: macsmc keeps the SMC running across sleep (cmd_done is re-initialized per command via reinit_completion() under smc->mutex; init_done is probe-only). Probe failures after apple_rtkit_wake() cleanly quiesce RTKit via devm_add_action_or_reset(dev, apple_smc_rtkit_shutdown, smc).
  • KUnit: Author reports exact-head macsmc-parser arm64 QEMU pass (17/17) at 38d93aa124a9, including macsmc_early_message_test.

Checklist Summary

  • §1 No model gating: Pass (pre-existing saved_enabled logic untouched).
  • §2 Production-ready: Pass.
  • §3 Safety: Pass (0 code blockers).
  • §4 Clean-room & attribution: Pass (both commits c7e6527a, 38d93aa1 carry human author/committer Ryan Murray <[email protected]> and human Signed-off-by: only, zero AI trailers in commits; PR body disclosure present).
  • §5 Evidence: Blocked — see below.
  • §6 Naming: Pass.

Remaining Blocker

  1. §5 — Owed physical hardware boot rows: Zero hardware rows have been posted yet. As noted in ## Still to run and your comment at 38d93aa124a9, physical boots covering both standard RTKit Macs (e.g. M1 Pro / M2 Air / M2 mini / M4 Pro) and a saved_enabled board (J714s or J813) with SMC sensor/notification verification and the full §5 regression triad (SELinux Enforcing, 0 AVC denials, 0 failed units, 0 new kernel errors) remain open.

Non-blocking Note

  • Optionally consider also delivering an SMC_MSG_NOTIFICATION through apple_smc_rtkit_recv() in macsmc_early_message_test to directly exercise the initialized event_handlers chain.

Verdict

REQUEST_CHANGES — code is clean and the prior init-order blocker is resolved; ready to approve once the owed §5 hardware boot rows and regression triad are posted.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

J316s (M1 Pro, standard RTKit) booted the kernel built at 38d93aa124a9b9dbb3e01c8269f2c08e40053c75 with the fleet85 configuration. It used the production Stage 2 and the same kernel command line as the stock control.

The stock and trial kernels each bound the SMC and its six child drivers and returned 142 numeric hwmon readings, including both fan tachometers. The trial resumed from one 12-second PMU-RTC s2idle on the same boot. SELinux stayed Enforcing with 0 AVCs, 0 failed units and 0 new kernel error signatures. The two DWC3 prepare errors match the stock RTC control. The production kernel was restored and the trial entry and modules were removed.

The RTKit notification trace saw zero callbacks during the observation window and RTC cycle. A live notification event and a physical boot on a saved-transport board such as J714s or J813 remain open.

Assisted-by: LLM (Codex)

@github-actions github-actions Bot added area:power Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at c9aa4923: not ready yet

Check Detail
CI at head no failing: apple-build, host-tests, pr-text / pr-text, pr-text / pr-text, KUnit ane-boot, KUnit ane-pm
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok in the PR body at c9aa4923
Conflicts ok none
PR text ok Risk: two-way · any Mac using the Apple SMC, including Macs with the saved RTKit transport.

Areas: area:power

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

@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Nov 24, 2026
DjDeveloperr
DjDeveloperr previously approved these changes Nov 25, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

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 38d93aa124a9: the code is ready; it needs only a row at this head.

Blockers: none in the code. Missing row: the J316s row covers standard RTKit (the SMC and its six children bound, 142 hwmon readings). Needed: one saved_enabled board at this head (the J714s, or a J813) with the SMC sensors and notifications working. The M5 lead can run it on the J714s.

Checked: no Apple material, no gates: the completions are set up before the RTKit starts. CI green.

@DjDeveloperr
DjDeveloperr dismissed stale reviews from themself November 25, 2026 04:32

Superseded by the review at 38d93aa

@github-actions github-actions Bot removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 25, 2026
Keep the probe state initialized before either RTKit transport starts. Retain the wake-reason and WKTP changes alongside the early-message and WKTP KUnit cases.

Signed-off-by: Ryan Murray <[email protected]>
@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) and removed merge-conflict Conflicts with the base branch (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 25, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

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 c9aa492328: the code is ready; it needs only rows at this head.

Blockers: none in the code. Missing rows, as your Not yet says. The change since 38d93aa1 is only the forward merge.

  • Saved transport: startup on a J714s or J813 at this head, with sensor reads and an event notification. I'm asking the M5 lead for the J714s.
  • Standard transport: a refresh of your J316s row at this head.

Checked: the mutex, spinlock, both completions, the notifier head and boot_stage are all set before devm_apple_rtkit_init(), so an early message finds valid state. Nothing is initialised twice later in probe, and the new KUnit case covers the early INITIALIZE reply. No Apple material, no gates, no new parameters.

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Nov 26, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

J714s (MacBook Pro 14" M5 Pro, T6050) row at c9aa492328dc: PASS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:power Area from CODEOWNERS (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants