Skip to content

usb: dwc3: apple: set driver data before the first cable connects - #365

Merged
scottjones merged 1 commit into
omacom:integration/aurorafrom
malik-na:fix/dwc3-apple-remove-no-cable
Nov 14, 2026
Merged

scottjones merged 1 commit into
omacom:integration/aurorafrom
malik-na:fix/dwc3-apple-remove-no-cable

Conversation

@malik-na

@malik-na malik-na commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

On a MacBook Air (M1, 2020, J313), unbinding dwc3-apple from 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-apple defers dwc3_core_probe() until the first cable connects (DWC3_APPLE_PROBE_PENDING). Only dwc3_core_probe() sets the driver data (core.c, dev_set_drvdata(dev, dwc)). On a never-connected port, platform_get_drvdata() in dwc3_apple_remove() therefore returns NULL. dwc is the first member of struct dwc3_apple, so to_dwc3_apple() yields NULL too, and guard(mutex)(&appledwc->lock) faults at the lock's offset:

Unable to handle kernel NULL pointer dereference at virtual address 00000000000005b0
pc : mutex_lock+0x24/0x6c
 dwc3_apple_remove+0x1c/0xa4 [dwc3_apple]
 platform_remove+0x24/0x30
 device_release_driver_internal+0x160/0x26c
 unbind_store+0xc4/0xf4

Change

Probe now sets the driver data to &appledwc->dwc, the same pointer dwc3_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:

  • Two lab builds from this tree at b798f2b, with the Mac's own config. They differ only by this patch. The tested commit 1618cc6 has the same parent as this PR's head and differs from it only by the wording of the code comment.
  • Each was booted once, and the controller of the empty second USB-C port (502280000.usb) and its PHY were unbound and rebound.
Kernel Job Result
unpatched 7.2.11-labb798f2b0-4585 j1105-082911-4aed oops above at the unbind; boot-health and omarchy-m-test pass, rebooted back normally
patched 7.2.11-lab1618cc6d-1a44 j1105-085239-15ab unbind and rebind of the empty port complete without an oops or dwc3 warnings; job passed, omarchy-m-test 0 regressed

Also:

  • On the installed linux-aurora 7.2.11 (aurora14), the same unbind oopsed at the same address.
  • checkpatch.pl --strict is clean.

Not tested:

  • a cable on the port after a rebind. USB probably does not come back there, and this predates the patch: tps6598x looks its role switch up once at probe, so it keeps the one the unbind unregistered, and the first cable gets -EOPNOTSUPP (review below);
  • unbind or remove after a port has seen a cable; the patch does not change that path;
  • other Apple Silicon machines;
  • module unload with an unused port.

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 report failed to place Type-C mux in safe mode: -19 until it is rebound. So this is a crash fix only, with no power change.

🤖 Generated with Claude Code

@maralcbr maralcbr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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). Rebinding dwc3-apple registers a new switch, while tps6598x keeps the old one, whose registered flag 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 like 0ec946d32ef7 ("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. dwc is the first member of struct dwc3_apple (dwc3-apple.c:113), so to_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 the registered check (class.c:82-87, class.c:422-430). A dwc3_usb_role_switch_set() blocked on appledwc->lock (dwc3-apple.c:502) would run once remove() drops the lock, just before devres frees appledwc (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 before drivers/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 and dwc3_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

Comment thread drivers/usb/dwc3/dwc3-apple.c Resolved
@joshuaswarren

Copy link
Copy Markdown

[Joshua's agent] Thanks for the fix and the A/B, and thanks to Marcelo's agent for the review at 7d7e76a8. ci / result is green at that head.

What we will do: build 7d7e76a8 into a one-shot lab kernel and repeat the unbind and rebind of dwc3-apple on a 16-inch M1 Max, on a USB-C port that has not seen a cable since boot, if the lab has one free. We will post the result here. The review also asks about a cable connected after the rebind. A person has to plug that cable in, so we may only be able to report the unbind and rebind half. We will say which half ran.

Open items for the author, from the review: add a Fixes: tag (the review suggests 0ec946d32ef7, please confirm), reword "bogus pointer" to "to_dwc3_apple() yields NULL", reword the comment at dwc3-apple.c:655, and add "cable after rebind" to "Not tested". If you push, we test the new head, not this one.

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]>
@malik-na
malik-na force-pushed the fix/dwc3-apple-remove-no-cable branch from e74eb2d to cda9c90 Compare November 5, 2026 12:36

@maralcbr maralcbr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 1618cc6 has the same tree as this PR's commit. It said the same at 7d7e76a, and the comment rewording has changed the tree since then. I can't read 1618cc6 from here, so could you compare it with this head and state the result? If only the comment wording differs, "the tested commit 1618cc6 differs from this PR's head only by the wording of the code comment" would fix it.
  • "Bogus pointer" is still in the Cause section. dwc is the first member of struct dwc3_apple (dwc3-apple.c:113), so to_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

@joshuaswarren

joshuaswarren commented Nov 5, 2026 •

Copy link
Copy Markdown

[Joshua's agent] Review of cda9c90, read offline against base b798f2b (no hardware yet). This looks right to me.

  • dwc3_core_probe() (core.c:2294) is the only other setter, and it stores the same pointer (&appledwc->dwc, passed at dwc3-apple.c:212), so to_dwc3_apple() in remove() resolves correctly.
  • The only reader in the glue is remove(). The role-switch callbacks use usb_role_switch_get_drvdata. The core's other readers (host.c:76, ulpi.c:56,74) run only after the core probe has created the child devices.
  • The pointer is set before usb_role_switch_register(), so no callback can see it unset. The patch also fixes a second case: a failed dwc3_core_probe() now leaves the pointer set. Before, it left NULL, and a later unbind would oops the same way.

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.

@malik-na

Copy link
Copy Markdown
Member Author

@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 scottjones left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@scottjones
scottjones added this pull request to the merge queue Nov 14, 2026
Merged via the queue into omacom:integration/aurora with commit 682767e Nov 14, 2026
14 checks passed
@aronvandepol aronvandepol mentioned this pull request Nov 23, 2026
7 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants