Linux USB
 help / color / mirror / Atom feed
* [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order
@ 2026-11-26  0:53 Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch outside appledwc->lock Ilia Snezhkov via B4 Relay
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: heikki.krogerus, gregkh
  Cc: hdegoede, sven, j, Thinh.Nguyen, asahi, linux-usb, linux-kernel

This two-patch v2 series fixes the race between
usb_role_switch_unregister() and concurrent usb_role_switch_{get,set}_role()
callbacks without introducing an ABBA lock-order inversion in callers:

1. Move usb_role_switch_unregister() outside appledwc->lock in
   dwc3_apple_remove(), the only in-tree caller holding a mutex taken by
   its role switch callbacks.
2. Serialize sw->registered checks and teardown under sw->lock in
   drivers/usb/roles/class.c and document the locking contract on
   usb_role_switch_unregister().

Changes in v2:
- Audited all 15 in-tree usb_role_switch_unregister() callers and
  confirmed drivers/usb/dwc3/dwc3-apple.c is the sole caller holding a
  lock also acquired by ->set()/->get().
- Added patch 1/2 fixing dwc3_apple_remove() before patch 2/2 takes
  sw->lock in usb_role_switch_unregister().
- Documented the lock-ordering requirement in the kerneldoc of
  usb_role_switch_unregister().
- Link to v1: https://lore.kernel.org/r/20261125234825.94733-1-iasnezhkov@gmail.com

Found and drafted with an AI coding assistant.

Ilia Snezhkov (2):
  usb: dwc3: apple: unregister role switch outside appledwc->lock
  usb: roles: serialize registered checks and unregister under sw->lock

 drivers/usb/dwc3/dwc3-apple.c |  4 ++--
 drivers/usb/roles/class.c     | 19 ++++++++++++++-----
 2 files changed, 16 insertions(+), 7 deletions(-)

-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] usb: dwc3: apple: unregister role switch outside appledwc->lock
  2026-11-26  0:53 [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 2/2] usb: roles: serialize registered checks and unregister under sw->lock Ilia Snezhkov via B4 Relay
  2026-11-26  1:04 ` [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: heikki.krogerus, gregkh
  Cc: hdegoede, sven, j, Thinh.Nguyen, asahi, linux-usb, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

dwc3_apple_remove() holds appledwc->lock across
usb_role_switch_unregister(), while both dwc3_usb_role_switch_set() and
dwc3_usb_role_switch_get() acquire appledwc->lock.

Calling usb_role_switch_unregister() under appledwc->lock creates an
ABBA lock-order inversion against both sysfs attribute teardown
(device_unregister() -> kernfs_drain() waiting on an active role_show /
role_store callback) and synchronizing usb_role_switch_unregister()
against in-flight callbacks under sw->lock.

appledwc->role_sw is assigned once during probe and never modified
afterwards. Unregister the role switch before acquiring appledwc->lock
for hardware teardown.

Found and drafted with an AI coding assistant.

Fixes: 0ec946d32ef7 ("usb: dwc3: Add Apple Silicon DWC3 glue layer driver")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
 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
index 1f474a8..035aeeb 100644
--- a/drivers/usb/dwc3/dwc3-apple.c
+++ b/drivers/usb/dwc3/dwc3-apple.c
@@ -487,10 +487,10 @@ static void dwc3_apple_remove(struct platform_device *pdev)
 	struct dwc3 *dwc = platform_get_drvdata(pdev);
 	struct dwc3_apple *appledwc = to_dwc3_apple(dwc);
 
-	guard(mutex)(&appledwc->lock);
-
 	usb_role_switch_unregister(appledwc->role_sw);
 
+	guard(mutex)(&appledwc->lock);
+
 	/*
 	 * If we're still in DWC3_APPLE_PROBE_PENDING we never got any cable connected event and
 	 * dwc3_core_probe was never called and there's hence no need to call dwc3_core_remove.
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] usb: roles: serialize registered checks and unregister under sw->lock
  2026-11-26  0:53 [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch outside appledwc->lock Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  2026-11-26  1:04 ` [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: heikki.krogerus, gregkh
  Cc: hdegoede, sven, j, Thinh.Nguyen, asahi, linux-usb, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

usb_role_switch_set_role() and usb_role_switch_get_role() check
sw->registered outside sw->lock, while usb_role_switch_unregister()
clears sw->registered without acquiring sw->lock.

A consumer can observe sw->registered == true, be preempted before
acquiring sw->lock, and still invoke sw->set() or sw->get() after
usb_role_switch_unregister() has returned and the provider driver has
torn down its state. Likewise, an in-flight callback already holding
sw->lock is not drained before usb_role_switch_unregister() returns.

Move the sw->registered check inside sw->lock in both
usb_role_switch_set_role() and usb_role_switch_get_role(), clear
sw->registered under sw->lock in usb_role_switch_unregister() so new
callbacks are rejected and in-flight callbacks finish before teardown
proceeds, and document the lock-ordering contract on
usb_role_switch_unregister().

Found and drafted with an AI coding assistant.

Fixes: b787a3e78175 ("usb: roles: don't get/set_role() when usb_role_switch is unregistered")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
 drivers/usb/roles/class.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
index edec139..ce23e95 100644
--- a/drivers/usb/roles/class.c
+++ b/drivers/usb/roles/class.c
@@ -79,10 +79,11 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
 	if (IS_ERR_OR_NULL(sw))
 		return 0;
 
-	if (!sw->registered)
-		return -EOPNOTSUPP;
-
 	mutex_lock(&sw->lock);
+	if (!sw->registered) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
 
 	ret = sw->set(sw, role);
 	if (!ret) {
@@ -90,6 +91,7 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
 		kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
 	}
 
+out:
 	mutex_unlock(&sw->lock);
 
 	return ret;
@@ -105,18 +107,21 @@ EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
  */
 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
 {
-	enum usb_role role;
+	enum usb_role role = USB_ROLE_NONE;
 
-	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)
+		goto out;
 
 	if (sw->get)
 		role = sw->get(sw);
 	else
 		role = sw->role;
 
+out:
 	mutex_unlock(&sw->lock);
 
 	return role;
@@ -418,12 +423,16 @@ EXPORT_SYMBOL_GPL(usb_role_switch_register);
  * @sw: USB Role Switch
  *
  * Unregister switch that was registered with usb_role_switch_register().
+ * Callers must not hold locks acquired by the switch's ->set() or ->get()
+ * callbacks.
  */
 void usb_role_switch_unregister(struct usb_role_switch *sw)
 {
 	if (IS_ERR_OR_NULL(sw))
 		return;
+	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);
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order
  2026-11-26  0:53 [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch outside appledwc->lock Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH v2 2/2] usb: roles: serialize registered checks and unregister under sw->lock Ilia Snezhkov via B4 Relay
@ 2026-11-26  1:04 ` sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-26  1:04 UTC (permalink / raw)
  To: Ilia Snezhkov; +Cc: linux-usb

Sashiko has reviewed this 2-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126005318.71705-1-iasnezhkov@gmail.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-26  1:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  0:53 [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH v2 1/2] usb: dwc3: apple: unregister role switch outside appledwc->lock Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH v2 2/2] usb: roles: serialize registered checks and unregister under sw->lock Ilia Snezhkov via B4 Relay
2026-11-26  1:04 ` [PATCH v2 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox