Skip to content

HID: spi-hid-apple: stop the IRQ before the device's state goes - #3277

Merged
RyanTheTide merged 1 commit into
mainfrom
fix/spi-hid-apple-teardown-order
Dec 1, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
fix/spi-hid-apple-teardown-order

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

What: The Apple SPI keyboard/trackpad transport now stops its IRQ before its state goes away, on a failed probe and on remove. The IRQ is requested before the core's device-managed state, so a probe that failed after enabling it returned with the IRQ still live while devres freed the state and buffers. Remove destroyed the HID devices before disabling the IRQ.

Tested: The spi-hid-apple KUnit suite on arm64 QEMU passes 15/15 at 542bbdcd0bb1, including 3 new cases; the previous code fails those 3. spi-hid-apple-core.o and spi-hid-apple-of.o build with W=1 (arm64, 16K pages). No hardware row yet.

Risk: two-way · Macs with the SPI keyboard and trackpad: J293, J313, J314, J316. Only probe failure and remove change.

Not yet: A hardware row on one of the SPI-keyboard Macs: boot, keyboard and trackpad input, a sleep, and an unbind/bind of the transport, with no new kernel errors.

Details
  • spihid_apple_of_probe() calls devm_request_threaded_irq() before spihid_apple_core_probe() makes its devm_ allocations (state, RX/TX/status buffers, descriptors). Devres releases in reverse order, so on a probe failure the core's state is freed before the IRQ. Probe enables the IRQ, then waits for the boot message and the device information; when either wait failed it returned without disabling the IRQ or powering the controller off. The threaded handler reads into rx_buf with spi_sync() and works on the driver state.

  • spihid_apple_core_stop() is now the one stop sequence:

    1. set the power state off, so new transfers (including led_work's from hid_destroy_device()) are refused;
    2. disable the IRQ through the transport, which waits for the threaded handler (it also registers the HID devices, so none can appear after this);
    3. destroy the HID devices;
    4. power the controller off.
  • Probe runs it on both failures after the IRQ is enabled. Remove is now just this sequence, so no report reaches a HID device while hid_destroy_device() runs. Shutdown already disabled the IRQ and powered off, and creates nothing to destroy, so it is unchanged.

  • The KUnit cases use a transport whose ops log each call:

    • spihid_test_probe_boot_timeout: no boot message;
    • spihid_test_probe_info_timeout: booted, but the device information never comes.

    In both, probe must return with the IRQ disabled and the controller off: the log is on irq-on irq-off off, and power was already off when the IRQ was disabled.

    • spihid_test_remove_irq_first: remove must disable the IRQ while the HID devices still exist, then destroy them and power off.
  • HID: spi-hid-apple: bound reassembled reports and refuse a descriptor without a buffer #2986 changes report reassembly in the same file; the two don't overlap.

Evidence
  • ./tools/testing/kunit/kunit.py run --arch=arm64 --make_options LLVM=1 --kunitconfig=drivers/hid/spi-hid/.kunitconfig: Ran 15 tests: passed: 15. The raw log shows waiting for device boot failed: -19 in the first new case and waiting for device info failed: -19 in the second.
  • The same with main's spi-hid-apple-core.c and the new tests: passed: 12, failed: 3.
    • probe_boot_timeout and probe_info_timeout: t->log == "on irq-on ", t->irq_enabled == 1.
    • remove_irq_first: "Expected t->hid_at_irq_off to be true, but is false".
  • ./linux-build/build kernel … -- -j4 W=1 drivers/hid/spi-hid/spi-hid-apple-core.o drivers/hid/spi-hid/spi-hid-apple-of.o: no warnings. A W=1 compile of the core with the KUnit cases included is also clean.
  • scripts/checkpatch.pl --strict: clean.
  • Base: main 859e8a29796c.

The OF glue requests the threaded IRQ before the core allocates its
state and buffers, all device-managed, so when probe fails devres frees
that state first and the IRQ last. A probe that failed after enabling
the IRQ (no boot message, or no device information) returned with the
IRQ still enabled and the controller still powered: the handler could
run on the freed state and its receive buffer until devres released the
IRQ.

Remove destroyed the HID devices while the IRQ was still enabled, so a
report could be delivered to a device being destroyed, and disabled the
IRQ only afterwards.

Both now use one stop sequence: refuse transfers, disable the IRQ (which
waits for the handler), destroy the HID devices the handler registered,
and power the controller off. Probe runs it on every failure after it
has enabled the IRQ.

The KUnit suite gets three cases. Probe failing at the boot wait, and at
the device information wait, must return with the IRQ disabled and the
controller off. Remove must disable the IRQ while the HID devices still
exist. The previous code fails all three.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 16:56
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at 542bbdcd0bb1: ready (code). The SPI-keyboard Mac row listed under "Not yet" is still owed.

Blockers: none

Checked

  • spihid_apple_core_stop() (drivers/hid/spi-hid/spi-hid-apple-core.c:1163). The order is right:

    1. Power state off, under tx_lock. spihid_apple_request() then refuses new transfers, led_work's included, and waiters are woken.
    2. disable_irq() through the transport. The OF op is the synchronous disable_irq(), so it waits out a running threaded handler.
    3. Destroy the HID devices.
    4. power_off().

    The threaded handler is what registers the HID devices (spihid_register_hid_device() from the descriptor reply, 800). So disabling the IRQ before destroying them means none can appear after the destroy. A HID driver probing inside the handler at that moment gets -ENODEV from any request and returns, so the wait is bounded. hid_bus_type sets no need_parent_lock, so a hid_add_device() in the handler doesn't need the SPI device's lock that remove() holds.

  • Probe unwind. spihid_apple_of_probe() requests the IRQ (devm_request_threaded_irq(…, IRQF_NO_AUTOEN), spi-hid-apple-of.c:112) before spihid_apple_core_probe() (121) makes its devm_ allocations. Devres frees in reverse, so the state and buffers go before free_irq().

    • Both failures after enable_irq(), the boot wait and the device-info wait, now go through err_stop (1303). The IRQ is already disabled by the time devres frees what the handler uses, and the controller powered on by spihid_apple_setup_spi() is powered off again.
    • The interface and descriptor loops after that don't fail the probe, and the failures before enable_irq() still go to error with nothing to stop.
    • A rebind requests a fresh, not-yet-enabled IRQ, so the disable depth left behind doesn't carry over.
  • Remove (1310) is now that same sequence. No report reaches a HID device while hid_destroy_device() runs, and led_work's transfer is still refused. The one cost: a request already waiting for a reply when remove starts now times out instead of completing, which is bounded.

  • Shutdown (spihid_apple_core_shutdown()) is unchanged: power state off, disable_irq(), power_off(), and nothing to destroy.

  • KUnit. The three new cases log the transport ops:

    • spihid_test_probe_boot_timeout and spihid_test_probe_info_timeout require on irq-on irq-off off, with power already off at the IRQ disable;
    • spihid_test_remove_irq_first requires the IRQ off while the HID devices still exist.
      They test exactly the order above.
  • No runtime switches, no new constants, and no board gates.

Evidence
  • kunit.py run --arch=arm64 --make_options LLVM=1 --kunitconfig=drivers/hid/spi-hid/.kunitconfig at 542bbdcd0bb1: 15/15 passed, with no BUG, WARNING or lockdep lines in the raw log.
  • Negative control: the same suite with main's spi-hid-apple-core.c gives 12 passed and 3 failed, the three new cases.
  • make W=1 drivers/hid/spi-hid/spi-hid-apple-core.o drivers/hid/spi-hid/spi-hid-apple-of.o (asahi profile, LLVM=1, arm64): no warnings. checkpatch.pl --strict: clean.

@github-actions github-actions Bot added door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at 542bbdcd: not ready yet

Check Detail
CI at head no failing: apple-build, host-tests, KUnit ane-boot, KUnit ane-pm, KUnit dcp-fabric, KUnit dcp-topology
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at 542bbdcd
Conflicts ok none
PR text ok Risk: two-way · Macs with the SPI keyboard and trackpad: J293, J313, J314, J316. Only probe failure and remove change.

Areas: none matched in CODEOWNERS

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

@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: Code approved at 542bbdcd0bb1. Hardware hold remains.

Checked: The OF transport uses synchronous disable_irq(). Both new probe error exits follow IRQ enable, and teardown waits for the threaded handler before destroying HID devices or allowing devres to release their backing state. The logical power gate also rejects HID LED work during destruction. Reviewed the reported 15/15 KUnit results and the three failing controls on the old code.

Before merge: Attach the requested supported SPI-keyboard Mac row with boot, keyboard and trackpad input, sleep/wake, unbind/rebind, and kernel error counts.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge-train review at 542bbdcd: Verdict: merge after a hardware row. Checked: spihid_apple_core_stop() now disables the IRQ before destroying the HID devices; the old remove destroyed them first and left the threaded handler free to feed a dead device. Power is refused before hid_destroy_device() cancels led_work, and the controller is powered off last. The probe failure paths after the IRQ is live now go through the same stop, so devres no longer frees state under a running handler. spihid_destroy_hid_device() is safe on an interface that never got a HID device. KUnit 15/15, with 3 new cases that fail on the old code. Still needed: one internal-keyboard Mac at this head (J413 or J313: keyboard, trackpad and backlight work; modprobe -r / modprobe of the spi-hid driver x3 with typing during removal; a short s2idle), plus green CI.

@RyanTheTide

Copy link
Copy Markdown
Contributor

542bbdc J316s (M1 Pro, T6000) PASS: combined guarded one-shot 7.3.0-aurora-peer1+ on main 498d53fd (spi-hid-apple-core.c byte-identical to 542bbdcd0bb1, default cmdline, saved Stage 2 unchanged); spi-hid-apple-of/spi2.0 bound with Apple SPI Keyboard, Apple SPI Trackpad, and Apple SPI Trackpad Force Click intact before and after s2idle 1/1 (rtc1) and across live unbind/bind, 0 spi-hid-apple errors, Enforcing, 0 AVC, 0 failed units; restored saved 7.3.0-aurora-edge.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Row (J316s, M1 Pro, T6000) at exact head 542bbdc in combined candidate 77b87daa (main de965eb + #3277 542bbdc + #3231 c955307 + #3210 69f3a4a), kernel 7.3.0-aurora-crj7+ (fleet edge config), kernel-only one-shot with the saved Stage 2 and the default command line -> PASS.

  • spi-hid-apple-of bound to spi2.0 at boot with Apple SPI Keyboard, Apple SPI Trackpad and Apple SPI Trackpad Force Click, and all three were still there after one s2idle (PMU RTC wake, 1 success / 0 fail).
  • Unbind/rebind of spi2.0 three times: rc 0 each time, 0 inputs while unbound, 3 inputs after each bind. 0 spi-hid error lines (only the usual "limiting the number of devices to 4" on each probe). 0 WARN/BUG/Oops. The trial boot was Enforcing with 0 failed units and the saved command line.

Evidence: traces/kernel/20261130-closeout-k114s-j316s-combo/m1pro/. Saved kernel 7.3.0-aurora-edge restored and verified.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging at 542bbdcd0bb1 (B2 merge train, 12-01 merge bar). Approval at this head: aurora-silicon-review[bot]. Row: the hardware row at 542bbdc 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 ed110fb into main Dec 1, 2026
19 of 37 checks passed
@RyanTheTide
RyanTheTide deleted the fix/spi-hid-apple-teardown-order branch December 1, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants