Skip to content

drm/apple: audio: no reports to a card that is gone - #3265

Open
themartiano wants to merge 1 commit into
aurora-silicon:mainfrom
themartiano:fix/dcp-audio-card-gone
Open

themartiano wants to merge 1 commit into
aurora-silicon:mainfrom
themartiano:fix/dcp-audio-card-gone

Conversation

@themartiano

@themartiano themartiano commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: Components unbind in reverse order, so a DP audio interface unbinds before its DCP. dcpaud_comp_unbind() frees the card but leaves dcpaud->card set, while the DCP's AV endpoint still runs and can report a sink connect or disconnect to it. The shutdown at every reboot takes this path too. Reports now need the card: it is taken away under data_lock before it is freed, and a report holds a reference on it. stale_work is no longer queued again after the unbind cancelled it.

Tested: No Mac runs this code at 32b705c yet (see Not yet). Host side at 32b705c: new KUnit suite apple-dcpaud-card 1/1 and apple-dcpaud-* 24/24 on arm64 QEMU with KASAN. With the pointer left set, the kernel reports refcount_t: underflow; use-after-free and the case fails. Merged with today's main 859e8a2 (no conflict): KUnit apple-dcpaud-* 29/29. Found by reading the code, not seen on hardware. Regression only: J713 M4 Air (26.6.2) at the same diff (04b2e294907e), kernel = main ba29027 plus all our open branches combined: boots, 0 failed units, 0 kernel errors, 10 of 10 deep sleeps. This code does not run there, because main has no DP audio on the J713.

Rebased onto main 9177b2d474f8 on 2026-12-02; head now 40a07087428f. One conflict in dcpaud_unbind(): main now stops the replaced-link work there first; this PR's taking of the card under data_lock follows it, unchanged. Host checks re-run at the new head: W=1 arm64 build of drivers/gpu/drm/apple/, 0 warnings; checkpatch --strict only notes the new test file (MAINTAINERS). KUnit dcp-fabric, dcp-topology, dcp-display (the suites CI picks for these files) on arm64 QEMU: no failures (one case skips in each of two suites). With audio enabled, apple-dcpaud-* 31/32 (the new apple-dcpaud-card passes; no-sio skips). The hardware rows above are from before the rebase.

Risk: two-way · every Mac with DCP audio (an apple,dpaudio node): M1, M2, M1/M2 Pro/Max/Ultra, M3 Pro/Max, M4 Pro/Max, J714s and Neo in main's trees. The paths are unbind, reboot/shutdown and the connect/disconnect reports.

Other Macs: This is shared DCP audio code, not M4 code. On every Mac above it stops a use-after-free when the audio component unbinds before its DCP, which happens at every reboot. While no card exists, a sink report is now dropped instead of going to freed memory. The sink state goes with the card, so a card made by a later bind starts unplugged and the next connect reports the jack as plugged. While the card is bound, reports behave as before, now under the lock and with a reference held.

Not yet: hardware rows. The J713 cannot give one on main today: DP audio there needs a newer aura boot loader and the DT in #3137. Any Mac with working DCP/HDMI audio can: 10 reboots with a monitor that has audio connected and the output off (DPMS), no oops or KASAN report; then a hotplug and a playback check.

Details
  • Why it can happen: component_unbind_all() goes in reverse, so the audio device unbinds before its DCP. The AV audio endpoint keeps running until the DCP's own teardown, and the firmware can publish or withdraw its audio service in that window. dcpaud_connect(), dcpaud_disconnect() and dcpaud_refresh_eld() then use the freed card's controls and jack. dcpaud_shutdown() (every reboot) takes the same path.
  • The fix: take the card away under data_lock before freeing it, report with a reference held, drop the sink with the card, and queue stale_work only while there is a card.
  • Not covered: dcpaud_sleep_quiesce() still uses dcpaud->substream if a system sleep runs while the component is unbound but the driver is still bound.
  • checkpatch --strict: one warning, "does MAINTAINERS need updating?", for the new test file drivers/gpu/drm/apple/audio-card-test.c.
  • The test file's #include line touches the same spot as drm: apple: audio: legacy path: 256 KiB audio elements, PM reference held until the DMA stopped #3128; a one-line conflict if both land.
Evidence

Components unbind in reverse order, so a DP audio interface unbinds
before its DCP, whose AV endpoint still runs then: the firmware can
publish or withdraw its audio service, and the DCP's own teardown ends
it. dcpaud_comp_unbind() frees the card but leaves dcpaud->card set, so
such a report goes to a freed card's controls and jack. The shutdown of
the audio device takes this path too. A disconnect also queues
stale_work again after the unbind cancelled it.

Take the card away under data_lock before it is freed, and report only
with a reference on it. The sink goes with the card: otherwise a card
made by a later bind would keep its jack unplugged, as the next connect
finds the sink connected already. Queue stale_work only while there is a
card.

Found by reading the code, not seen on hardware.

Signed-off-by: Eduardo <git@martiano.me>
@themartiano
themartiano force-pushed the fix/dcp-audio-card-gone branch from 32b705c to 40a0708 Compare December 2, 2026 12:08
@themartiano
themartiano marked this pull request as ready for review December 2, 2026 16:15
@themartiano
themartiano requested review from a team as code owners December 2, 2026 16:15
@github-actions github-actions Bot added area:display Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) 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 Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Merge readiness at 40a07087: not ready yet

Check Detail
CI at head ok 9 checks passed
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head no no line with the head SHA 40a07087, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · every Mac with DCP audio (an apple,dpaudio node): M1, M2, M1/M2 Pro/Max/Ultra, M3 Pro/Max, M4 Pro/Max, J714s and Neo in main's trees. The paths are unbind, reboot/shutdown and the connect/disconnect reports. Other Macs: This is shared DCP audio code, not M4 code. On every Mac above it stops a use-after-free when the audio component unbinds before its DCP, which happens at every reboot. While no card exists, a sink report is now dropped instead of going to freed memory. The sink state goes with the card, so a card made by a later bind starts unplugged and the next connect reports the jack as plugged. While the card is bound, reports behave as before, now under the lock and with a reference held.

Areas: area:display

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

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

Approve at 40a07087428f. Reports now hold a card reference taken under data_lock (dcpaud_card_get() pairs with the snd_card_unref cleanup), and unbind clears card, connected and the ELD under the same lock before snd_card_free_when_closed(). stale_work can only be queued under the lock while the card exists, so the later cancel_work_sync() in unbind catches anything queued before that. The jack and ELD control a report touches belong to the card, so the reference keeps them alive.

Checked on pc, merged with main 1b95b711bbc2 (no conflict): W=1 drivers/gpu/drm/apple/audio.o (asahi profile) builds with no warnings. I approved the fork CI runs at this head, so the hosted KUnit plan will run apple-dcpaud-card.

Before merge: rows on two generations with DCP audio, as in your Not yet. Do 10 reboots with an audio-capable monitor connected and DPMS off, with no oops or KASAN report, then a hotplug and a playback check.

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

This branch has not been deployed

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

Labels

area:display Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants