* [PATCH v2 0/2] usb: roles: wait for callbacks during unregister
@ 2026-11-17 12:59 Keisuke Tsukuda
2026-11-17 13:00 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch before taking lock Keisuke Tsukuda
2026-11-17 13:00 ` [PATCH v2 2/2] usb: roles: wait for callbacks on unregister Keisuke Tsukuda
0 siblings, 2 replies; 5+ messages in thread
From: Keisuke Tsukuda @ 2026-11-17 12:59 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Sven Peter, Janne Grunau, Neal Gompa,
Thinh Nguyen, asahi@lists.linux.dev,
linux-arm-kernel@lists.infradead.org, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
A role-switch provider can be unregistered while a set/get callback is
running. The core checks the registered flag before taking the switch lock
and clears it without taking that lock, so unregister can return while the
callback still uses provider-owned memory. A synthetic provider reproduces
use-after-free reports in both callback paths with KASAN.
The core fix makes unregister wait for callbacks on the switch lock. The
Apple DWC3 role callbacks take a private lock under the switch lock, while
its remove path currently holds the private lock around unregister. Patch 1
moves Apple's unregister call ahead of that lock; patch 2 adds the core
synchronization. This order keeps intermediate states free of the new lock
inversion.
On ARM64 QEMU with Linux 7.3.4 and mainline 7.4-rc2 (067c5c24cef2),
KASAN reported use-after-free in set and get callbacks before the change.
After both patches, unregister waited for the callbacks and neither test
reported KASAN. A lockdep test modeling Apple's callback/remove lock order
warned for the original order and not for the new one. Apple DWC3 hardware
was not tested.
Changes in v2:
- Split the Apple lock-order change and the role-switch core fix into two
patches, with the Apple change first, as requested by Heikki Krogerus.
- Add Cc: stable@vger.kernel.org tags to both patch descriptions.
Thanks for the review, Heikki.
v1: https://lore.kernel.org/r/20261112024101.83321-1-tkdkei@outlook.jp
Keisuke Tsukuda (2):
usb: dwc3: apple: unregister role switch before taking lock
usb: roles: wait for callbacks on unregister
---
drivers/usb/dwc3/dwc3-apple.c | 4 ++--
drivers/usb/roles/class.c | 18 ++++++++++++++----
2 files changed, 16 insertions(+), 6 deletions(-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] usb: dwc3: apple: unregister role switch before taking lock
2026-11-17 12:59 [PATCH v2 0/2] usb: roles: wait for callbacks during unregister Keisuke Tsukuda
@ 2026-11-17 13:00 ` Keisuke Tsukuda
2026-11-17 13:12 ` sashiko-bot
2026-11-17 13:00 ` [PATCH v2 2/2] usb: roles: wait for callbacks on unregister Keisuke Tsukuda
1 sibling, 1 reply; 5+ messages in thread
From: Keisuke Tsukuda @ 2026-11-17 13:00 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Sven Peter, Janne Grunau, Neal Gompa,
Thinh Nguyen, asahi@lists.linux.dev,
linux-arm-kernel@lists.infradead.org, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
The Apple DWC3 role-switch callbacks take appledwc->lock while the
role-switch core holds sw->lock. The core needs to take sw->lock during
unregister to wait for callbacks. With the current remove order, this
would invert the locks: remove would hold appledwc->lock and wait for
sw->lock, while a callback could hold sw->lock and wait for
appledwc->lock.
Unregister the role switch before taking appledwc->lock. This lets the
core wait for callbacks before the device is torn down, without creating
a lock inversion when the role-switch core change is applied.
Cc: stable@vger.kernel.org
Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
---
drivers/usb/dwc3/dwc3-apple.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c
--- a/drivers/usb/dwc3/dwc3-apple.c
+++ b/drivers/usb/dwc3/dwc3-apple.c
@@ -487,9 +487,9 @@
struct dwc3 *dwc = platform_get_drvdata(pdev);
struct dwc3_apple *appledwc = to_dwc3_apple(dwc);
+ usb_role_switch_unregister(appledwc->role_sw);
+
guard(mutex)(&appledwc->lock);
-
- usb_role_switch_unregister(appledwc->role_sw);
/*
* If we're still in DWC3_APPLE_PROBE_PENDING we never got any cable connected event and
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] usb: dwc3: apple: unregister role switch before taking lock
2026-11-17 13:00 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch before taking lock Keisuke Tsukuda
@ 2026-11-17 13:12 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-17 13:12 UTC (permalink / raw)
To: Keisuke Tsukuda; +Cc: linux-usb@vger.kernel.org
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] usb: dwc3: apple: missing Fixes trailer for dwc3_apple_remove()
lock fix
--
commit 96c6a6a050705a0ebc8be7bb5bb8a9f2787d72a7
Author: Keisuke Tsukuda <tkdkei@outlook.jp>
> usb: dwc3: apple: unregister role switch before taking lock
>
> The Apple DWC3 role-switch callbacks take appledwc->lock while the
> role-switch core holds sw->lock. The core needs to take sw->lock during
> unregister to wait for callbacks. With the current remove order, this
> would invert the locks: remove would hold appledwc->lock and wait for
> sw->lock, while a callback could hold sw->lock and wait for
> appledwc->lock.
>
> Unregister the role switch before taking appledwc->lock. This lets the
> core wait for callbacks before the device is torn down, without creating
> a lock inversion when the role-switch core change is applied.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
[Severity: Low]
This fixes the lock ordering in dwc3_apple_remove() and carries a stable
tag. Should this include:
Fixes: 0ec946d32ef7 ("usb: dwc3: Add Apple Silicon DWC3 glue layer driver")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117125825.27629-1-tkdkei@outlook.jp?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] usb: roles: wait for callbacks on unregister
2026-11-17 12:59 [PATCH v2 0/2] usb: roles: wait for callbacks during unregister Keisuke Tsukuda
2026-11-17 13:00 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch before taking lock Keisuke Tsukuda
@ 2026-11-17 13:00 ` Keisuke Tsukuda
2026-11-17 13:19 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Keisuke Tsukuda @ 2026-11-17 13:00 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Sven Peter, Janne Grunau, Neal Gompa,
Thinh Nguyen, asahi@lists.linux.dev,
linux-arm-kernel@lists.infradead.org, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
usb_role_switch_set_role() and usb_role_switch_get_role() check the
registered flag before taking the switch lock. If a provider is
unregistered during a role request, unregister can return while its
callback is still running. The provider may then free memory used by
the callback, causing a use-after-free. A caller that passed the check
can also enter a callback after unregister returns.
Move the registered checks under the switch lock and clear the flag while
holding the same lock on unregister. This waits for active callbacks and
rejects callers that acquire the lock after the flag is cleared. The
preceding Apple DWC3 change avoids a lock inversion in its remove path.
Fixes: fde0aa6c175a ("usb: common: Small class for USB role switches")
Cc: stable@vger.kernel.org
Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
---
drivers/usb/roles/class.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
--- a/drivers/usb/roles/class.c
+++ b/drivers/usb/roles/class.c
@@ -79,10 +79,11 @@
if (IS_ERR_OR_NULL(sw))
return 0;
- if (!sw->registered)
- return -EOPNOTSUPP;
-
mutex_lock(&sw->lock);
+ if (!sw->registered) {
+ ret = -EOPNOTSUPP;
+ goto unlock;
+ }
ret = sw->set(sw, role);
if (!ret) {
@@ -90,6 +91,7 @@
kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
}
+unlock:
mutex_unlock(&sw->lock);
return ret;
@@ -107,16 +109,21 @@
{
enum usb_role role;
- if (IS_ERR_OR_NULL(sw) || !sw->registered)
+ if (IS_ERR_OR_NULL(sw))
return USB_ROLE_NONE;
mutex_lock(&sw->lock);
+ if (!sw->registered) {
+ role = USB_ROLE_NONE;
+ goto unlock;
+ }
if (sw->get)
role = sw->get(sw);
else
role = sw->role;
+unlock:
mutex_unlock(&sw->lock);
return role;
@@ -423,7 +430,10 @@
{
if (IS_ERR_OR_NULL(sw))
return;
+ /* Wait for in-flight set/get callbacks before unregistering. */
+ mutex_lock(&sw->lock);
sw->registered = false;
+ mutex_unlock(&sw->lock);
if (dev_fwnode(&sw->dev))
component_del(&sw->dev, &connector_ops);
device_unregister(&sw->dev);
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2 2/2] usb: roles: wait for callbacks on unregister
2026-11-17 13:00 ` [PATCH v2 2/2] usb: roles: wait for callbacks on unregister Keisuke Tsukuda
@ 2026-11-17 13:19 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-17 13:19 UTC (permalink / raw)
To: Keisuke Tsukuda; +Cc: linux-usb@vger.kernel.org
> usb_role_switch_set_role() and usb_role_switch_get_role() check the
> registered flag before taking the switch lock. If a provider is
> unregistered during a role request, unregister can return while its
> callback is still running. The provider may then free memory used by
> the callback, causing a use-after-free. A caller that passed the check
> can also enter a callback after unregister returns.
>
> Move the registered checks under the switch lock and clear the flag while
> holding the same lock on unregister. This waits for active callbacks and
> rejects callers that acquire the lock after the flag is cleared. The
> preceding Apple DWC3 change avoids a lock inversion in its remove path.
>
> Fixes: fde0aa6c175a ("usb: common: Small class for USB role switches")
> Cc: stable@vger.kernel.org
> Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117125825.27629-1-tkdkei@outlook.jp?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-17 13:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 12:59 [PATCH v2 0/2] usb: roles: wait for callbacks during unregister Keisuke Tsukuda
2026-11-17 13:00 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch before taking lock Keisuke Tsukuda
2026-11-17 13:12 ` sashiko-bot
2026-11-17 13:00 ` [PATCH v2 2/2] usb: roles: wait for callbacks on unregister Keisuke Tsukuda
2026-11-17 13:19 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox