Repository navigation
usb: dwc3: apple: take a device role off the bus for suspend-to-idle - #2728
RyanTheTide wants to merge 3 commits into
Conversation
dwc3-apple keeps a started controller running through suspend-to-idle
in either role and has no system suspend callback, so a device role
stays connected to its host while the system is frozen. Nothing
services its endpoints meanwhile: the host's OUT transfers pile up and
the link need not recover. On a J713 with an NCM gadget bound, IPv6
ping over the link passed after the 3rd and 4th suspend-to-idle, and
after the 5th the host's transmissions never completed again
('cdc_ncm ... NETDEV WATCHDOG: transmit queue 0 timed out') while
device-to-host traffic still flowed.
dwc3's own system suspend soft-disconnects a gadget for the sleep
(dwc3_gadget_suspend()). Do the same from the sleep preparation: for a
suspend-to-idle with the controller started in device role and a
gadget driver bound, disconnect the gadget before the system freezes,
and connect it again from the work that runs after the wake. The host
sees a disconnect and enumerates the device again. Deep sleeps stop
the controller anyway and are unchanged; host roles are unchanged.
Signed-off-by: Ryan Murray <[email protected]>
|
Merge-train adversarial review (MERGETRAIN-LINUX2) at ee83542: BLOCKERS. Not merging until the blockers below are fixed or answered; the details have file:line and a failure case. Full review#2728 usb: dwc3: apple: take a device role off the bus for suspend-to-idle @ee83542fVERDICT: BLOCKERS
Nits:
Missing rows:
|
There was a problem hiding this comment.
Adversarial pre-PR checklist review at ee83542f3a26adf500a2b21a322eef3c432d9af2 (REQUEST_CHANGES).
Checklist Summary
- §1 (No model gating): Pass — gated on
!sleep_deep,state == DWC3_APPLE_DEVICE,core_active, anddwc.gadget_driver; no board/SoC/firmware gate. - §2 (No hacks / default cmdline): Pass.
- §3 (Correctness / locking / s2idle): Blockers found (see below).
- §4 (Clean-room & attribution): Pass — human author/committer (
Ryan Murray <[email protected]>) withSigned-off-by:only in commit; PR body includesAssisted-by:. - §5 (Evidence): Incomplete — missing non-J713 device-role s2idle row and negative/race rows (see below).
- §6 (Naming): Pass.
- CI: 7/7 checks PASS (
host-tests,source,rust-build,kunit,DWC3/ATC builtin-no-atc,DWC3/ATC modular-no-atc,DWC3/ATC builtin-modular-atc).
Blockers
-
Use-after-free of
gadget/gadget->udcindwc3_apple_sleep_gadget_on()(drivers/usb/dwc3/dwc3-apple.c:1797-1814, called from:1924):dwc3_apple_sleep_start_work()publishesappledwc->sleep_phase = DWC3_APPLE_SLEEP_IDLEat:1916-1919and releasesappledwc->lockat:1921before callingdwc3_apple_sleep_gadget_on()at:1924.dwc3_apple_sleep_gadget_on()copiesgadget = appledwc->dwc.gadgetunderappledwc->lock(:1810), drops the lock at:1811, and then callsusb_gadget_connect(gadget)at:1812.- Once
DWC3_APPLE_SLEEP_IDLEis visible,dwc3_usb_role_switch_set()(:1478) no longer defers role changes. A cable unplug or role swap delivered by the port controller right after wake can enterdwc3_apple_exit()(:1431-1433) ->dwc3_gadget_exit()(drivers/usb/dwc3/gadget.c:4982-4984) ->usb_del_gadget()+usb_put_gadget(), freeinggadgetandgadget->udcin the window between:1811and the completion ofusb_gadget_connect().usb_gadget_connect()then locks freedgadget->udc->connect_lockand invokesdwc3_gadget_pullup()on a torn-down gadget. - Fix: Reconnect while
sleep_phaseis stillDWC3_APPLE_SLEEP_FINISHING(before publishingDWC3_APPLE_SLEEP_IDLE), so role switches remain deferred untilusb_gadget_connect()returns.
-
Back-to-back suspend race can leave the gadget connected through the next s2idle (
drivers/usb/dwc3/dwc3-apple.c:1916-1924,:1965-1982,:1790):- Because
DWC3_APPLE_SLEEP_IDLEis published beforedwc3_apple_sleep_gadget_on()runs,dwc3_apple_pm_notify(PM_SUSPEND_PREPARE)can immediately admit a new suspend epoch and queuesleep_stop_workconcurrently onsystem_dfl_wq. - The new epoch's
dwc3_apple_sleep_gadget_off()can observe!gadget->connected(or race onsleep_gadget_offbeforegadget_on()clears it at:1806) and return without recordingsleep_gadget_off = true, after which the previous epoch'sdwc3_apple_sleep_gadget_on()callsusb_gadget_connect(), reconnecting the gadget into the second s2idle (or concurrently withdpm_prepare/dpm_suspend). - Fixing Blocker 1 by completing
dwc3_apple_sleep_gadget_on()before publishingDWC3_APPLE_SLEEP_IDLEalso closes this race.
- Because
Secondary Issues / Nits
- Unlocked
gadget->connectedread (dwc3-apple.c:1790):PM_SUSPEND_PREPAREruns before tasks are frozen, so userspace can still togglesoft_connector unbind configfsUDCconcurrently.gadget->connectedis protected bygadget->udc->connect_lock. - Call placement in
dwc3_apple_sleep_prepare_work()(dwc3-apple.c:1823):dwc3_apple_sleep_gadget_off()runs before theappledwc->shutting_downcheck (:1826) and beforedwc3_apple_sleep_start()(:1833); moving it after those checks (for!sleep_deep) avoids acting during teardown or missing a device role just started bydwc3_apple_sleep_start(). - Comment / state hygiene: Clearing
sleep_gadget_offindwc3_apple_exit()keeps the flag consistent across deferredDEVICE -> DEVICEre-init (:1905-1908), and the commit message should note that s2idle now drops and re-enumerates USB gadget links (e.g., CDC-ACM/NCM debug consoles).
Required §5 Rows After Fix
- Device-role s2idle row on a second generation (M1/M2-class Mac running
dwc3-applein device role). - Negative/race rows: cable unplug / role swap during s2idle (ideally with KASAN), back-to-back s2idle loop, and aborted suspend (
pm_test=devicesor prepare-phase wakeup). - Explicit SELinux Enforcing / 0 AVC denials / 0 failed units confirmation on the regression run.
The start work after a suspend-to-idle published the IDLE phase, dropped the glue lock and only then connected the gadget it had taken off its host. Once IDLE is visible, two things can run under that connect: - Role changes are no longer deferred. A cable pull or role swap that the port controller reports right after the wake goes through dwc3_apple_exit() and dwc3_gadget_exit(), which frees the gadget and its UDC while usb_gadget_connect() is still using them. - A new sleep can be admitted. Its preparation sees a gadget that is not connected yet and leaves it alone, then the old epoch connects it, so the gadget stays on the bus through the next suspend-to-idle. Connect the gadget while the epoch is still FINISHING and publish IDLE afterwards. A role change that arrives during the connect is deferred as before and applied under the glue lock before IDLE. Split the deferred-role and IDLE steps of the start work into helpers so both paths share them. Also: - Take the gadget off its host after the shutdown check and the controller start in the preparation, for suspend-to-idle only. - Read gadget->connected and disconnect under the gadget's device lock, which soft_connect writes and function driver binding also hold, so a racing soft_connect write is not missed. The glue lock is not held there, since dwc3_gadget_exit() takes the device lock under it. - Forget a pending reconnect in dwc3_apple_exit(): a gadget that is set up again connects when its function driver binds. With this, every suspend-to-idle in device role drops the gadget link and the host enumerates the device again after the wake. A CDC-ACM or NCM console on the gadget loses whatever is sent while it is disconnected. dwc3's own system suspend does the same. A new host test, dwc3-sleep-gadget.py, builds the sleep works, the role switch and dwc3_apple_exit() against mocks. It checks that the connect runs while the epoch still defers role changes and admits no sleep, across back-to-back sleeps, role changes before and during the reconnect, and teardown. Negative controls publish IDLE before the connect, skip the role re-check, keep the flag across an exit and disconnect a gadget that is not connected. The harnesses that stub struct dwc3_apple carry the new field. Signed-off-by: Ryan Murray <[email protected]>
|
Both blockers and the nits are fixed in 1a91e65, a new commit on top of ee83542.
Tests: a new host test,
Four negative controls fail as they should: IDLE before the connect, no role re-check after it, the flag kept across an exit, and disconnecting a gadget that is not connected. The other harnesses that stub Hardware rows still to run:
Assisted-by: LLM (Claude:Opus 5.5) |
|
Review at 1a91e65: no blockers. Both blockers from the review at ee83542 and its three nits are fixed in 1a91e65. I found no new locking, lifetime or error-path bug in the reworked sleep works. The hardware rows asked for in that review (a second-generation device-role s2idle row, back-to-back and aborted-suspend rows, cable pull or role swap during s2idle, SELinux/AVC/failed-unit confirmation) are still to run. They are evidence still owed, not code findings. One non-blocking nit: Full reviewHead 1a91e65 (2 commits on a84637d: ee83542, 1a91e65). Findings from the ee83542 review
Other paths checked
Other Macs, parameters, delays, trailers, merge
Tests at the head (buildbox)
Still to run (hardware)
Assisted-by: LLM (Claude:Opus 5.5) |
|
Hardware blocker at 1a91e65: on the J713 (13-inch M4 Air) a warm reboot hangs with this commit and doesn't without it. I ran
#2700 at da78109 is in the second row, so the hang follows this commit. Netconsole over Wi-Fi stops when NetworkManager goes down, so there's no kernel log from the late shutdown. The shutdown path that changed here is the gadget disconnect under This blocks the merge. The fix and a reboot row will follow on this PR. Assisted-by: LLM (Claude:Opus 5.5) |
|
Follow-up on the J713 warm-reboot hang I reported against 1a91e65: after reading the code and the built module, I can't confirm that this commit causes it, so I'm not pushing a fix. What 1a91e65 runs on a reboot. Nothing new runs on a boot that goes straight to Same machine code. I compared dwc3-apple.ko from the two test kernels (with and without only this merge), per function, with addresses and relocation offsets masked:
Why the A/B looked conclusive.
Next steps. These would settle it. None needs a debug kernel:
Still to run on the J713: those reboot rows. I'll withdraw or confirm the blocker once they're done. The PR head is unchanged at 1a91e65. Assisted-by: LLM (Claude:Opus 5.5) |
|
Matched reboot A/B on the J713 (13-inch M4 Air), following up on the analysis above. Each boot was a pure one-shot: boot, wait 20 s, then
That is 4 of 5 reboots hung with 1a91e65 and 0 of 8 without it. The like-for-like pair (fleet82 vs the same tree minus only this commit) is 2/3 against 0/3. The other Macs on the same kernel (M1 Pro, M2 Air, M2 mini, M4 Neo) reboot cleanly, which fits the T8132-only So the blocker stands, even though the new code mostly sits in the sleep works. The change that does reach shutdown is the Assisted-by: LLM (Claude:Opus 5.5) |
There was a problem hiding this comment.
Formal §1–§6 pre-PR re-review at live head 1a91e6509800db898569998e884217e269989395 against live integration/aurora (70a00e583b03).
Status & Code Fixes Since ee83542f
- Still needed on
integration/aurora(70a00e583b03):drivers/usb/dwc3/dwc3-apple.chas no s2idle gadget disconnect/reconnect path. - Prior code blockers resolved in
1a91e650: Reconnectingusb_gadget_connect()before publishingDWC3_APPLE_SLEEP_IDLE(with the seconddwc3_apple_sleep_role()re-check underappledwc->lock) fixes both the gadget UAF and the back-to-back suspend race fromee83542f. Readinggadget->connected+ callingusb_gadget_disconnect()underdevice_lock(&gadget->dev), moving disconnect aftershutting_down/dwc3_apple_sleep_start(), clearingsleep_gadget_offindwc3_apple_exit(), and addingdwc3-sleep-gadget.py(4/4 + 4 negative controls) are all clean. - §1, §2, §4, §6 & CI: Pass on policy/attribution/CI. Both commits (
ee83542f3a26,1a91e6509800) carry human author/committer and humanSigned-off-by: Ryan Murray <[email protected]>only (no AI commit trailers); PR body carriesAssisted-by:. Live CI is 7/7 green (host-tests,source,rust-build,kunit, and all threeDWC3/ATCmatrices).
Blockers (Active Hardware Regression + §5 Evidence)
- Confirmed J713 warm-reboot hang regression at
1a91e650: As reported in the 2026-11-22 matched A/B on J713, 4 of 5 warm reboots hung with1a91e650vs 0 of 8 without it (2/3 vs 0/3 in the exact like-for-like pair reverting only1a91e650). This active hardware blocker must be root-caused (e.g., viainitcall_debug+ dynamic debug shutdown trace), fixed, and re-verified with clean warm reboots before merge. - Missing §5 hardware qualification on exact head
1a91e650(or its follow-up fix):- J713 device-role NCM on default cmdline: back-to-back
rtcwake -m freeze -s 20x5 with no idle gap, aborted suspend (pm_test=devices), ping/iperf3 after each, plus SELinux Enforcing, 0 AVC denials, 0 failed units, and 0 new kernel errors. - Second-generation Mac (
dwc3-applein device role, e.g., M1/M2-class): 3x s2idle with host + NCM bound and ping/iperf3 after each. - Negative/race row: cable pull or role swap during s2idle in device role (ideally on a KASAN build).
- J713 device-role NCM on default cmdline: back-to-back
Minor Nit (non-blocking)
- Update the PR body (
## Changeand<details>) to match1a91e650's connect-before-IDLEflow (it still describesee83542f).
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Verdict: FINISH, then merge at 1a91e6509800
Blockers:
- A measured regression: on the J713, 4 of 5 warm reboots hung with
1a91e650against 0 of 8 without it, and in the like-for-like pair that reverts only this commit, 2 of 3 against 0 of 3 (your 11-22 A/B). Taking the device role off the bus probably leaves the controller in a state the shutdown path doesn't expect. Fix: find where the reboot stops (initcall_debugplus dynamic debug on the dwc3-apple shutdown), fix it, and show clean warm reboots (10 or more) alongside the s2idle rows.
Checked: no Apple material. The s2idle result is good: 6 of 6 suspend-to-idle cycles with an NCM gadget, ping 10/10 and iperf3 280 Mbit/s after each, and a clean host-side disconnect. The new selftests cover the gadget across sleep. CI green. This replaces my earlier review.
Evidence
Read 1a91e650 and the 11-22 J713 reboot A/B.
Superseded by the review at 1a91e65
Signed-off-by: Ryan Murray <[email protected]>
|
Reply to the review at 1a91e65. New head 39d3c01 merges main 88ddea8 in (clean, after the #3302 revert). The PR's own code is unchanged: there is no shutdown bug in it to fix, and the warm-reboot hang has a different cause. Where the reboot stops. On the J713 the hang comes after Linux has shut down, in aura Stage 2 on the next boot. In every hung sample I have, the journal ends with a complete shutdown ( That's the known post-M4 failure where ANS doesn't start from off on a warm boot, tracked by aurora-silicon/aura#480 and aurora-silicon/aura#449. It happens before any kernel runs. A VDM reboot recovers it, and sometimes the recovery boot strands too. The 1-minute hardware watchdog never fires because Linux is already gone. Why the 11-22 counts pointed at this commit. The same strand hits kernels without 1a91e65. Examples: fleet83 with this PR at ee83542 (FLEET-CUT3, 11-23), the fleet85 production kernel (11-26, root-caused the same way), and tonight main a561e89 + #2797 (03:14, another lane). Another lane measured about 1 in 4 warm reboots tonight on any kernel. 4/5 against 0/8 was a small sample of that rate. On a boot with no system sleep, this commit runs no new code: the sleep works only run from the PM notifier. The only new instruction on the shutdown path is clearing Rows (J713, default cmdline, Stage 2 1.0.0b15, production unchanged). These ran on fleet85 + 1a91e65 (fleet85 already carries ee83542 and #2700), not on the merge head. dwc3-apple.c at 39d3c01 is main's file plus the same delta.
Per item
Tested: J713 fleet85 + 1a91e65: 15 warm reboots (13 clean, 2 Stage 2 ANS strands after a complete Linux shutdown), NCM gadget s2idle 6/6 with traffic after each of the last 3; host tests dwc3 2/2 + atc 15/15 at 39d3c01. |
There was a problem hiding this comment.
Verdict: MERGE at 39d3c018
Blockers: none (re-run the cancelled pr-text and readiness checks before landing; 26/26 build/host/KUnit checks green).
Checked:
drivers/usb/dwc3/dwc3-apple.c: s2idle gadget disconnect/reconnect path verified sound in code (1a91e650), cleanly merged ontomainat39d3c018(MERGEABLE).- Prior warm-reboot hang blocker explained with Stage 2 proxy console evidence: Linux completed
systemd-shutdowncleanly and the next boot stalled in Stage 2 (nvme: ANS didn't start from off, tracked inaura#480/aura#449) before any kernel ran. - CI & policy: 26/26 build, host-test, and KUnit checks green; no Apple material, tool attribution, private serials, or board gating; human
Signed-off-bypresent.
Evidence
- Head:
39d3c0182d5e(base: main,mergeable: MERGEABLE,+421/-36across 8 files).
Problem
dwc3-apple keeps a started controller running through suspend-to-idle in either role, and it has no system suspend callback. A device role (the Mac as a USB device, with a gadget function bound) therefore stays connected to its host while the system is frozen. Nothing services its endpoints meanwhile, so the host's OUT transfers pile up, and the link does not always recover. On an M4 MacBook Air (J713) with an NCM gadget bound, IPv6 ping over the link passed after the 3rd and 4th suspend-to-idle. After the 5th, the host's transmissions never completed again (
cdc_ncm ... NETDEV WATCHDOG: transmit queue 0 timed outon the host), while device-to-host traffic still flowed.Change
dwc3's own system suspend soft-disconnects a gadget for the sleep (
dwc3_gadget_suspend()). dwc3-apple now does the same from its sleep preparation. For a suspend-to-idle with the controller started in device role and a gadget driver bound, the preparation work disconnects the gadget (usb_gadget_disconnect(), under the gadget's device lock) before the system freezes. The start work after the wake connects it again (usb_gadget_connect()) while the sleep epoch is still FINISHING, applies any role change that came in meanwhile, and only then publishes IDLE. Role changes stay deferred until then, so the gadget can't be torn down under the connect, and no new sleep is admitted before it is back on the bus. The host sees a disconnect and enumerates the device again, so a CDC-ACM/NCM console loses output while the Mac sleeps. Deep sleeps stop the controller anyway and are unchanged, as are host roles. No parameters, nothing keyed on a board or SoC.Tested: J713 (M4 Air), default cmdline, fleet85 + 1a91e65: device-role NCM gadget s2idle 6/6, ping 10/10 + iperf3 280 Mbit/s after each of the last 3; 15 warm reboots, 13 back in 28-33 s, 2 parked in aura Stage 2's ANS warm-start failure after a complete Linux shutdown (same as kernels without this PR), 0 Linux shutdown hangs. Host tests dwc3 2/2 (incl. dwc3-sleep-gadget) + atc 15/15 at 39d3c01.
Risk: Macs whose dwc3-apple runs in device role (all Apple Silicon USB-C ports with a gadget bound) across suspend-to-idle: the gadget link drops and re-enumerates per sleep. Host roles, host-only controllers and deep sleep are untouched.
Testing
cdc_ncm(high-speed):USB disconnectat each sleep and enumerates the gadget again after each wake. Enforcing, 0 AVC, 0 failed units.--only dwc32/2 (dwc3-sleep-gadget.py: back-to-back sleeps, role change during the connect, deep/host/unbound/soft-disconnected gadgets, teardown before/after preparation; 4 negative controls),--only atc15/15.Still to run
Details for reviewers
!sleep_deep && state == DWC3_APPLE_DEVICE && core_active && dwc.gadget_driverand the gadget is connected. Host roles, host-only controllers (T6040/T6050,host_only) and deep sleeps never reach it.gadget->connectedunderdevice_lock(&gadget->dev)(the locksoft_connectand function bind/unbind take), without the glue lock, becausedwc3_gadget_exit()takes that device lock under the glue lock. dwc3-apple has nopre_run_stopglue op, so pullup never re-enters the glue lock.dwc3_apple_pm_quiesce()setsshutting_downand cancels both sleep works beforedwc3_apple_exit(), so a connect in flight finishes first. On a boot with no system sleep the sleep works never run; the only new instruction on the shutdown path is clearingsleep_gadget_offindwc3_apple_exit().