Repository navigation
usb: dwc3: apple: take a device role off the bus for suspend-to-idle - #4455
RyanTheTide wants to merge 4 commits into
Conversation
dwc3-apple keeps a started controller running through suspend-to-idle in either role and has no system suspend callback. A device role with a gadget function bound therefore stays connected to its host while the system is frozen: nothing services its endpoints meanwhile, so host OUT transfers pile up and the link can stall after several suspend-to-idle cycles. Mirror dwc3's own system suspend (dwc3_gadget_suspend()) from dwc3-apple's sleep preparation work: when preparing a suspend-to-idle with a started device role and a bound gadget driver, soft-disconnect the gadget via usb_gadget_disconnect() under the gadget's device lock before the system freezes. On wake, dwc3_apple_sleep_start_work() reconnects the gadget via usb_gadget_connect() while the sleep epoch is still FINISHING, applies any role change deferred during the reconnect, and only then publishes IDLE (and finishes any retained deep-sleep PHY state via dwc3_apple_sleep_finish()). Deferring IDLE publication until after usb_gadget_connect() guarantees that a concurrent role switch cannot tear down the gadget under the reconnect and a back-to-back sleep cannot be admitted before the gadget is back on the bus. Deep sleeps, host roles, unbound gadgets, and already soft-disconnected gadgets are unchanged. Add the dwc3-sleep-gadget.py host test with mutation negative controls and register it in tools/testing/selftests/drivers/apple/Makefile and tools/asahi/run-host-tests.py. Closes: #4174 Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
There was a problem hiding this comment.
Verdict: FINISH at ddd815e
Failure:
drivers/usb/dwc3/dwc3-apple.c:2068-2080,2129-2133discards a disconnect error and reports that sleep preparation succeeded. The DWC3 timeout path can already have stopped endpoints and marked its pullups off, while UDC retainsgadget->connectedbecause the pullup operation returned an error. The glue then records no reconnect, so wake leaves the gadget down. Restore a usable gadget on the error path and return the preparation error. The existingdwc3_gadget_suspend()also attempts recovery before refusing sleep.drivers/usb/dwc3/dwc3-apple.c:2315,2333-2351can finish the disconnect before the process freezer. A normal UDC soft-connect or bind can put the gadget back on the bus then. Device prepare only consumes the earlier worker result, so sleep can still freeze with a connected gadget. Perform or revalidate the disconnect after the freezer, keeping the child-device lock ordering and deferred role handling.
Checked: The exact gadget suite passed all five cases and rejected four supplied mutations. The separate initial-role suite passed 7/7. Additional GCC and Clang mocks with the exact glue functions confirmed a normal wake reconnect, a timeout returning prepare=0 and reconnects=0, and a late soft-connect leaving the gadget up at device preparation. This was offline source validation.
Evidence
The timeout crosses drivers/usb/dwc3/gadget.c:2784-2798,2827 and drivers/usb/gadget/udc/core.c:785-787. The notifier/freezer order is in kernel/power/suspend.c:384-390. drivers/usb/gadget/udc/core.c:1803-1835 permits an independent soft-connect under the gadget device lock once the worker drops it. The existing regression test mocks both UDC operations as successful and does not put a soft-connect between them.
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
…t errors Address two suspend-to-idle gadget races in dwc3-apple: 1. Calling usb_gadget_disconnect() and discarding its error could leave DWC3 pullups off after a disconnect timeout while UDC retained gadget->connected = true and dwc3-apple recorded sleep_gadget_off = false, leaving the gadget down across wake. Export and call dwc3_gadget_suspend() / dwc3_gadget_resume() under gadget->dev and dwc->connect_mutex instead: dwc3_gadget_suspend() attempts dwc3_gadget_soft_connect() recovery before returning -EAGAIN, and dwc3_apple_pm_prepare() converts -EAGAIN to -EBUSY to refuse sleep without leaving the gadget down. 2. Performing the disconnect in the pre-freezer PM notifier worker left a window before freeze_processes() where a userspace soft-connect or function bind could put the gadget back on the bus. Move the s2idle gadget suspend into post-freezer dwc3_apple_pm_prepare() while preserving dev->mutex -> gadget->dev -> connect_mutex lock ordering and deferred role handling. Extend tools/testing/selftests/drivers/apple/dwc3-sleep-gadget.py to extract dwc3_apple_pm_prepare() and verify both the suspend-error refusal path and late pre-freezer soft-connect/bind handling along with corresponding negative-control mutations. Closes: #4174 Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
|
Addressed both review findings at
Verified locally at
Ready for re-review. |
There was a problem hiding this comment.
Verdict: FINISH at 8ac2289
Failure: The replacement suspend path at drivers/usb/dwc3/dwc3-apple.c:2081-2084 leaves dwc->softconnect set when it takes the gadget off the bus. A recovery already queued by the gadget IRQ runs on the regular workqueue. Once setup releases connect_mutex, dwc3_err_recovery_work() can acquire it and reconnect on that unchanged intent, before the system wakes. The Apple path leaves the core active across this sleep. Gate the recovery worker's reconnect across the same suspend epoch, without losing the saved intent used on wake.
Checked: Both original findings are addressed. The post-freezer device callback refuses sleep on a failed disconnect and processes a late pre-freezer bind. On GCC and Clang, the exact gadget and initial-role suites passed 7/7 each and all six gadget mutations were rejected. A separate replay of the exact DWC3 suspend and error-recovery functions left pullups off after prepare and reconnected them when the pending worker ran. Clearing the reconnect intent in the control left them off. No hardware ran.
Evidence
drivers/usb/dwc3/gadget.c:4674-4677 queues the worker. At lines 4794–4821 it takes the same mutex, then reconnects whenever dwc->softconnect is set. The new suspend call keeps this flag so the normal resume at lines 5014–5019 can restore the link. The mutex serializes the operations, while a recovery pending at prepare can still run next.
Track gadget suspend state in struct dwc3::gadget_suspended (set in dwc3_gadget_suspend() after a successful disconnect and cleared in dwc3_gadget_resume() and dwc3_gadget_init()), serialized by dwc->connect_mutex across dwc3_err_recovery_work() and dwc3_gadget_pullup(). This prevents an IRQ-queued dwc3_err_recovery_work() from reconnecting pullups during the suspend epoch when dwc->softconnect remains set for resume, while preserving the saved softconnect intent for wake. Extend dwc3-sleep-gadget.py to compile the production dwc3_err_recovery_work(), dwc3_gadget_suspend(), and dwc3_gadget_resume() from drivers/usb/dwc3/gadget.c, verify that a pending recovery stays off during suspend and reconnects after wake (and still reconnects outside suspend), and add a negative control dropping the !gadget_suspended gate. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
|
Addressed the review finding at
|
There was a problem hiding this comment.
Verdict: FINISH at 4a67497
Failure:
drivers/usb/dwc3/gadget.c:2860-2869can return the successful runtime-PM status1when a pullup request is deferred. The UDC marks the gadget logically connected only when the callback returns zero. Processes thaw beforePM_POST_SUSPENDqueues the Apple finish work, so ordinary function activation can occur while this gate is still set. In a deactivate, activate, deactivate sequence, the second deactivation then skips clearing the recorded connect intent and finish reconnects the deactivated gadget. Return zero when accepting and deferring the request, and test the UDC state and pullups across that sequence.drivers/usb/dwc3/gadget.c:5009,5018adds writes to the common suspend and resume helpers, while pullup and recovery read the flag underconnect_mutex. The generic device and OTG PM callers indrivers/usb/dwc3/core.c:2637,2675,2705,2748do not hold that mutex. In generic suspend the recovery worker can take the mutex after the halt and reconnect before the new flag is set. Keep the new gate local to the serialized Apple suspend epoch, or serialize the shared flag with all of its PM callers.
Checked
The prior Apple recovery issue is fixed. On GCC and Clang, the exact gadget suite passed eight cases and rejected seven mutations, and the exact initial-role suite passed seven cases. An additional GCC and Clang UBSan replay ran the exact DWC3 pullup, suspend and resume and UDC activation functions with the active runtime-PM return of 1. Both showed activation returning 1 and leaving the UDC connection state false, followed by an active hardware pullup after resume despite final deactivation. Returning zero in the deferred branch preserved the UDC state and left pullups off. The fixture models hardware state on the host.
…apple Address two review findings in the gadget_suspended gate: 1. In dwc3_gadget_pullup(), pm_runtime_get_sync() returns 1 when the device is already runtime-active. When is_on is true and dwc->gadget_suspended defers soft-connect, explicitly set ret = 0 so usb_gadget_connect_locked() records gadget->connected = true instead of leaving it false and causing a subsequent deactivate before PM_POST_SUSPEND to skip clearing dwc->softconnect. 2. Remove the unsynchronized dwc->gadget_suspended writes from common dwc3_gadget_suspend() and dwc3_gadget_resume() (whose generic core.c callers do not hold connect_mutex). Instead, set and clear dwc->gadget_suspended under connect_mutex in dwc3-apple's serialized suspend-to-idle epoch (dwc3_apple_sleep_gadget_off() and dwc3_apple_sleep_start_work()), clear it on role teardown after dwc3_gadget_exit() in dwc3_apple_exit(), and still clear it on wake even if the gadget driver unbound before finish. Extend tools/testing/selftests/drivers/apple/dwc3-sleep-gadget.py to compile the production dwc3_gadget_pullup() with active runtime PM (returning 1) and test deactivate -> activate -> deactivate, deactivate -> activate, and unbind-before-finish across the suspend epoch, plus corresponding negative controls. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
|
Addressed both review findings at
Verified at
|
There was a problem hiding this comment.
Verdict: MERGE at a3c51f1
Checked: The deferred pullup now returns zero during the Apple suspend epoch. The UDC preserves its requested state through deactivate and activate, including a final deactivate before wake. The gate is set and cleared under connect_mutex during Apple sleep and is absent from the generic PM helpers. Role teardown clears it after the gadget and its recovery work are shut down. Both GCC and Clang ran the exact gadget test, with 11 cases passing and all 10 mutations rejected, and the exact initial-role test, with seven passing. Pending recovery remains off during suspend, active intent reconnects at wake, and unbind before finish leaves the next bind able to connect. Hosted and native evidence remain on their existing routes.
|
@RyanTheTide @DjDeveloperr — |
satchlj
left a comment
There was a problem hiding this comment.
Reviewed at a3c51f1920d133e14df808f8fe13214ddc04a6c2:
- Checked
drivers/usb/dwc3/core.h,drivers/usb/dwc3/dwc3-apple.c, anddrivers/usb/dwc3/gadget.c:dwc->gadget_suspendedis scoped to thedwc3-applesuspend-to-idle epoch and serialized underdwc->connect_mutex, cleared on both wake (dwc3_apple_sleep_start_work(), even if the gadget driver unbound) and role teardown (dwc3_apple_exit()afterdwc3_gadget_exit()). - Verified
dwc3_apple_sleep_gadget_off()runs post-freezer indwc3_apple_pm_prepare()underdevice_lock(&gadget->dev)->connect_mutex, converts-EAGAINfromdwc3_gadget_suspend()into-EBUSYto refuse sleep cleanly without leaving pullups down, anddwc3_apple_sleep_gadget_back()reconnects before publishingIDLEand re-applies any deferred role transition afterward. - Verified
dwc3_gadget_pullup()returns0(notpm_runtime_get_sync()'s1) whenis_on && gadget_suspendedsousb_gadget_connect_locked()recordsgadget->connected = true, anddwc3_err_recovery_work()skips reconnecting pullups whilegadget_suspendedis set. - Checked commit trailers and
dwc3-sleep-gadget.pycoverage; CI and an exact-head hardware row are still pending.
What: Soft-disconnect a bound USB gadget during suspend-to-idle preparation in
dwc3-appleand reconnect it on wake before publishingDWC3_APPLE_SLEEP_IDLE, preventing host-to-device NCM/gadget stalls after repeated suspend-to-idle cycles (rebased port of the approved #2728 design onto currentmain's reworked sleep paths). Closes #4174.Tested:
make ARCH=arm64 LLVM=1 W=1 drivers/usb/dwc3/dwc3-apple.o: 0 compiler warnings, 0 errors.scripts/checkpatch.pl --strict --git HEAD~1..HEAD --ignore FILE_PATH_CHANGES: 0 errors, 0 warnings, 0 checks (561 lines checked).tools/testing/selftests/drivers/apple/dwc3-sleep-gadget.py: 5/5 PASS (back-to-back s2idle disconnect/reconnect, deferred role switches before and during reconnect, host/deep-sleep/unbound/soft-disconnected exclusions, shutdown/role-exit teardown, and retained deep-sleepdwc3_apple_sleep_finish()path) + 4/4 negative mutation controls rejected (idle-before-connect,no-role-after-connect,exit-keeps-flag,disconnect-unconnected).dwc3-initial-role.py(7/7),atc-t8132-usb3.py(13/13),neo-stability-tests.py(4/4),apple-dcp/run.py --group lifecycle(38/38 incl.dwc3-teardown),apple-dcp/run.py --group quiesce(21/21),pciec-policy-tests.py(all cases + negative controls), andrun-host-tests.pytest registration check (17 host + 2 hw tests registered).Risk: two-way · affects only Apple Silicon USB-C ports running
dwc3-applein device role with a bound, connected gadget across suspend-to-idle (the gadget disconnects and re-enumerates per s2idle cycle, matchingdwc3_gadget_suspend()). Host roles, host-only controllers, unbound/soft-disconnected gadgets, and deep sleep (sleep_deep) are untouched. Revert withgit revert -m 1 <merge-commit>.Not yet: Exact-head hardware verification on J713 (M4 Air) or J773g (M4 mini) with an NCM gadget bound across repeated suspend-to-idle cycles (ping + checksummed transfer each way after every cycle) plus a second-generation Mac (e.g. J700 or J413/J473) and warm-reboot check.
Details
drivers/usb/dwc3/dwc3-apple.c:sleep_gadget_offtostruct dwc3_appleand clear it indwc3_apple_exit()forDWC3_APPLE_DEVICE.dwc3_apple_sleep_gadget(),dwc3_apple_sleep_gadget_off()(takesdevice_lock(&gadget->dev)outsideappledwc->lockbefore checkinggadget->connectedand callingusb_gadget_disconnect()), anddwc3_apple_sleep_gadget_back().dwc3_apple_sleep_prepare_work(), handle!appledwc->sleep_deepby collecting the active gadget underappledwc->lockand soft-disconnecting it after dropping the lock, leaving the deep-sleepin_place/tunnel_keep/ T6040restartpaths unchanged.dwc3_apple_sleep_role()anddwc3_apple_sleep_idle()(callingdwc3_apple_sleep_finish(appledwc, kept)for retained deep-sleep/T6040 restart) sodwc3_apple_sleep_start_work()callsusb_gadget_connect()whilesleep_phase == DWC3_APPLE_SLEEP_FINISHING, applies any role change requested during the reconnect, and only then publishesDWC3_APPLE_SLEEP_IDLE.tools/testing/selftests/drivers/apple/dwc3-sleep-gadget.py,Makefile,tools/asahi/run-host-tests.py: add and register the host + mutation test suite.tools/asahi/neo-stability-tests.py,tools/testing/selftests/drivers/apple/atc-t8132-usb3.py,tools/testing/selftests/drivers/gpu/apple-dcp/dwc3/mocks.c,tools/testing/selftests/drivers/gpu/apple-dcp/teardown_pm.py: addsleep_gadget_offto mockstruct dwc3_appledefinitions.Evidence
ddd815e521cb6ee43822e8f08b0e2fbf99208469(basea917bd8be646283a837eb93017352a1f20cd7bd8)make ARCH=arm64 LLVM=1 W=1 drivers/usb/dwc3/dwc3-apple.o: exit 0, 0 compiler warningsscripts/checkpatch.pl --strict --git HEAD~1..HEAD --ignore FILE_PATH_CHANGES:total: 0 errors, 0 warnings, 0 checks, 561 lines checkeddwc3-sleep-gadget.py:1..5ok + 4 negative controls rejecteddwc3-initial-role.py:1..7okatc-t8132-usb3.py:1..13okneo-stability-tests.py:wake,sep,usb,regulatoryexit 0apple-dcp/run.py --group lifecycle:1..38ok;--group quiesce:1..21okpciec-policy-tests.py: all cases and negative controls PASS