Repository navigation
usb: dwc3: apple: take a device role off the bus for suspend-to-idle - #2728
RyanTheTide wants to merge 2 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 <ryan@aurorasilicon.org>
|
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:
|
DjDeveloperr
left a comment
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 <ryan@aurorasilicon.org>) 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.
Assisted-by: LLM (Gemini 4 Neon)
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 <ryan@aurorasilicon.org>
|
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) |
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, it disconnects the gadget (usb_gadget_disconnect()) before the system freezes. It connects it again (usb_gadget_connect()) 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, as are host roles. Role changes are already held off while a sleep is being prepared, so the gadget stays bound across the two calls. No parameters, nothing keyed on a board or SoC.Testing
cdc_ncm:USB disconnectat each sleep and a new enumeration after each wake, with 0 NETDEV WATCHDOG. 0 kernel errors on the J713.drivers/usb/dwc3/. Host tests--only dwc3,--only apple-dcp,--only atcpass.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.usb_gadget_disconnect()/usb_gadget_connect()are called outside the glue lock: dwc3's pullup path has no dwc3-apple glue op (onlypre_host_resumeis set), so there is no re-entry.dwc3_apple_sleep_start_work(). If a role change after the wake already took the gadget down, the flag is just cleared.tx_packetsfrozen at 89, devicerx_packetsfrozen at 89, device-to-host still incrementing).Assisted-by: LLM (Claude:Opus 5.5)