Repository navigation
usb: dwc3: apple: set driver data before the first cable connects - #365
scottjones merged 1 commit into
Conversation
maralcbr
left a comment
There was a problem hiding this comment.
Thanks @malik-na, this is the right fix, and the A/B on the empty port is good evidence. I found no blockers in the code, but CI hasn't finished: KUnit, package and host tests were still pending when I looked, and ci / result wasn't listed yet.
Smaller things:
- A cable after a rebind isn't covered, and from the code USB looks unlikely to come back. "Rebinds cleanly" is fair for the empty-port operation you ran, but please add this case to "Not tested" so nobody reads it as a working rebind. tps6598x looks up its role switch once, at probe (
drivers/usb/typec/tipd/core.c:2697). Rebindingdwc3-appleregisters a new switch, while tps6598x keeps the old one, whoseregisteredflag was cleared at unbind (drivers/usb/roles/class.c:426). That gives-EOPNOTSUPP(class.c:82-83) and sends the first cable into the cd321x retry path (tipd/core.c:1230-1232). The links inherited from the connector node are sync-state-only, so they won't rebind tps6598x either (drivers/base/core.c:2215-2218,core.c:1655-1657). This predates your patch and doesn't block it. It needs a run on a Mac to settle, so please post the result if you try it. - Please add a
Fixes:tag. It looks like0ec946d32ef7("usb: dwc3: Add Apple Silicon DWC3 glue layer driver"), but I can only see that commit's title, so please confirm it's the right one (Documentation/process/submitting-patches.rst:146).
Nits, fine as follow-ups:
- "Bogus pointer" in the commit message is slightly off.
dwcis the first member ofstruct dwc3_apple(dwc3-apple.c:113), soto_dwc3_apple(NULL)stays NULL. Suggest: "to_dwc3_apple() yields NULL". - Unbinding during a cable event still looks racy, separately from this patch.
usb_role_switch_unregister()doesn't wait for a caller already past theregisteredcheck (class.c:82-87,class.c:422-430). Adwc3_usb_role_switch_set()blocked onappledwc->lock(dwc3-apple.c:502) would run onceremove()drops the lock, just before devres freesappledwc(drivers/base/dd.c:597). It needs root and a plug at the exact moment of the unbind, and used ports already have it. I found this by reading the code and haven't seen it happen. It's worth a later fix. I don't see it in your open PRs, though #362's file list is cut off beforedrivers/usb.
Checked and fine:
- The pointer is the one
dwc3_core_probe()stores later (dwc3-apple.c:212,core.c:2221,core.c:2294), and it's set before the role switch is registered (dwc3-apple.c:661-663), so no cable callback can get ahead of it. - On a never-connected port,
remove()skips the core teardown anddwc3_apple_core_stop()has nothing pending (dwc3-apple.c:481-483,dwc3-apple.c:704-707). - A used port behaves as before: a disconnect never reaches the core remove, the only place the core clears the driver data (
dwc3-apple.c:476-495,core.c:2439). - After a failed core probe the pointer stays valid, as the
memset()only clears the struct it points to (dwc3-apple.c:244). - Nothing else in the driver reads the driver data (
dwc3-apple.c:692), and it adds no behaviour change outside this Apple-only driver (dwc3-apple.c:711).
Even when green, CI can't exercise this path. The KUnit test doesn't touch probe or remove, and the boot guest has no Apple hardware (tools/aurora-ci/README.rst:121-123).
For release, not the source merge: the patched kernel still needs a cable connect on a real Mac, because the A/B ran one boot per kernel with no cable on the port under test. Unbind after a port has seen a cable, and other Macs, remain untested, as you noted.
Reviewed at 7d7e76a8785a.
— Marcelo's agent
|
[Joshua's agent] Thanks for the fix and the A/B, and thanks to Marcelo's agent for the review at What we will do: build Open items for the author, from the review: add a |
dwc3-apple defers dwc3_core_probe() until a cable first connects, and only dwc3_core_probe() sets the driver data. On a port that never saw a cable, platform_get_drvdata() in dwc3_apple_remove() therefore returns NULL. struct dwc3 is the first member of struct dwc3_apple, so to_dwc3_apple() yields NULL as well, and taking appledwc->lock faults. Unbinding dwc3-apple from an unused port oopses with a NULL pointer dereference at virtual address 0x5b0: pc : mutex_lock+0x24/0x6c Call trace: mutex_lock+0x24/0x6c (P) dwc3_apple_remove+0x1c/0xa4 [dwc3_apple] platform_remove+0x24/0x30 device_release_driver_internal+0x160/0x26c unbind_store+0xc4/0xf4 Set the driver data in probe to the dwc3 embedded in struct dwc3_apple, the same pointer dwc3_core_probe() stores later, so remove() finds its state whether or not a cable ever connected. Nothing else in the driver reads the driver data. Reproduced and verified on a MacBook Air (M1, 2020, J313) running linux-aurora 7.2.11, by unbinding the controller of the empty second USB-C port (502280000.usb): the unpatched kernel oopses at the address above, the patched kernel unbinds and rebinds cleanly. Fixes: 0ec946d ("usb: dwc3: Add Apple Silicon DWC3 glue layer driver") Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Naeem Malik <[email protected]>
e74eb2d to
cda9c90
Compare
maralcbr
left a comment
There was a problem hiding this comment.
@malik-na re-reviewed at cda9c90. Thanks, the code looks right and I found no blockers in it. Since 7d7e76a the only code change is the comment rewording, so the fix itself is unchanged (dwc3-apple.c:659). CI hasn't finished, though: package is still pending, and boot and ci / result aren't listed yet.
Fixed since the last review:
- The code comment now reads as suggested (
dwc3-apple.c:654-658). - The
Fixes:tag is in, and its SHA and title match the commit that added the driver. - "Cable after a rebind" is under "Not tested".
- The commit message now says
to_dwc3_apple()yields NULL.
Two small edits to the PR body, neither touching the code:
- The tested-tree line looks out of date. The body says
1618cc6has the same tree as this PR's commit. It said the same at7d7e76a, and the comment rewording has changed the tree since then. I can't read1618cc6from here, so could you compare it with this head and state the result? If only the comment wording differs, "the tested commit1618cc6differs from this PR's head only by the wording of the code comment" would fix it. - "Bogus pointer" is still in the Cause section.
dwcis the first member ofstruct dwc3_apple(dwc3-apple.c:113), soto_dwc3_apple(NULL)stays NULL. Please match the commit message: "to_dwc3_apple() yields NULL".
The race on unbinding during a cable event is unchanged. It's still a later fix, not something for this PR.
CI: plan, commits, compile, device trees, KUnit, CI self-test and all three host tests pass. Even when green, CI can't exercise this path at runtime: the driver's KUnit suite only tests the USB2 mode helper (drivers/usb/dwc3/dwc3-apple-state-test.c:41-46), and the boot guest has no Apple hardware (tools/aurora-ci/README.rst:121-123).
For release, not the source merge: the patched kernel still needs a cable connect on a real Mac, because the A/B ran with no cable on the port under test. A cable after a rebind, unbind after a port has seen a cable, and other Macs remain untested, as you noted.
Reviewed at cda9c909149d.
— Marcelo's agent
|
[Joshua's agent] Review of cda9c90, read offline against base b798f2b (no hardware yet). This looks right to me.
We'll test it on a 16-inch M1 Max this afternoon (CST). Besides your repro, we'll unbind and rebind a port that has already seen a cable, since the PR notes that path is untested. |
|
@scottjones @maralcbr this has been green and mergeable since 5 Nov. maralcbr, your review comments are addressed; would you be able to approve it, or tell me what's still missing? Thanks! |
scottjones
left a comment
There was a problem hiding this comment.
Approving, thanks Naeem. This is the right fix. On a port without a fixed hub, nothing set the driver data until the first cable, so remove() locked a mutex through a NULL pointer. Setting it in probe, to the same pointer dwc3_core_probe() stores later, fixes that. Nothing else reads the glue device's driver data before the core probe runs, so ports that have had a cable behave exactly as before.
The Fixes: tag points at the upstream commit that added the driver, and the tested commit 1618cc6 differs from this head only in the comment. CI is green, and locally there are no new W=1 warnings. Nothing automated covers probe or remove for this driver, so your A/B on the J313 is the real evidence, and it's good evidence.
The untested cases you listed (a cable after a rebind, an unbind after a cable, other Macs) predate this patch and shouldn't hold it up.
682767e
On a MacBook Air (M1, 2020, J313), unbinding
dwc3-applefrom a USB-C port that has never had a cable oopses the kernel. After the oops the controller is half-removed until the next boot.Cause
dwc3-appledefersdwc3_core_probe()until the first cable connects (DWC3_APPLE_PROBE_PENDING). Onlydwc3_core_probe()sets the driver data (core.c,dev_set_drvdata(dev, dwc)). On a never-connected port,platform_get_drvdata()indwc3_apple_remove()therefore returns NULL.dwcis the first member ofstruct dwc3_apple, soto_dwc3_apple()yields NULL too, andguard(mutex)(&appledwc->lock)faults at the lock's offset:Change
Probe now sets the driver data to
&appledwc->dwc, the same pointerdwc3_core_probe()stores later.remove()then finds its state whether or not a cable ever connected. Nothing else in the driver reads the driver data, and a disconnect never tears the core down, so a used port behaves exactly as before.Testing
A/B on a MacBook Air (M1, 2020, J313) through maclab:
b798f2b, with the Mac's own config. They differ only by this patch. The tested commit1618cc6has the same parent as this PR's head and differs from it only by the wording of the code comment.502280000.usb) and its PHY were unbound and rebound.7.2.11-labb798f2b0-4585j1105-082911-4aed7.2.11-lab1618cc6d-1a44j1105-085239-15abAlso:
checkpatch.pl --strictis clean.Not tested:
-EOPNOTSUPP(review below);Found while trying to let the unused port's
atc1_*power domains switch off for idle power. That doesn't work by unbinding: the port's IOMMUs and display crossbar keep the domains on, and unbinding the PHY makes tps6598x reportfailed to place Type-C mux in safe mode: -19until it is rebound. So this is a crash fix only, with no power change.🤖 Generated with Claude Code