Skip to contentSkip to conversation

usb: roles: serialize registered checks and unregister under sw->lock

PATCH New Ilia Snezhkov 2 participants 2 messages Nov 25 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: roles: serialize registered checks and unregister under sw->lock

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(), and clear sw->registered under sw->lock in usb_role_switch_unregister() so new callbacks are rejected and in-flight callbacks finish before the provider continues teardown.

Found and drafted with an AI coding assistant.

Fixes b787a3e78175 ("usb: roles: don't get/set_role() when usb_role_switch is unregistered")
---
 drivers/usb/roles/class.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/usb/roles/class.c +12 -5
index edec139..3370294 100644
@@ -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;
@@ -423,7 +428,9 @@ 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);
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] usb: roles: ABBA deadlock between usb_role_switch_unregister() and dwc3-apple --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] usb: roles: ABBA deadlock between usb_role_switch_unregister() and dwc3-apple
--

commit e0b16766d03a43b431963aa2b14fba31f31089de
Author: Ilia Snezhkov <[email protected]>

usb: roles: serialize registered checks and unregister under sw->lock

This commit moves the sw->registered check inside sw->lock in usb_role_switch_set_role() and usb_role_switch_get_role(), and acquires sw->lock when clearing sw->registered in usb_role_switch_unregister().

Quoted 4 lines
drivers/usb/roles/class.c +0 -0
index 8ea703e63ffbd..0a13c871c71f2 100644

[ ... ]

drivers/usb/roles/class.c void usb_role_switch_unregister(struct usb_role_switch *sw) · line 436

Quoted 10 lines
drivers/usb/roles/class.c +2 -0
@@ -423,7 +428,9 @@ 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);

[Severity: High]
Can taking sw->lock in usb_role_switch_unregister() cause an ABBA deadlock with appledwc->lock in dwc3-apple?

usb_role_switch_set_role() and usb_role_switch_get_role() invoke sw->set() and sw->get() under sw->lock, and the dwc3-apple callbacks acquire appledwc->lock. Conversely, dwc3_apple_remove() calls usb_role_switch_unregister() while already holding appledwc->lock:

drivers/usb/dwc3/dwc3-apple.c:dwc3_apple_remove() {
	...
	guard(mutex)(&appledwc->lock);

	usb_role_switch_unregister(appledwc->role_sw);
	...
}

CPU0 CPU1
---- ----
lock(&appledwc->lock)

                                  lock(&sw->lock)
lock(&sw->lock)
                                  lock(&appledwc->lock)

*** DEADLOCK ***

CPU0: dwc3_apple_remove() -> usb_role_switch_unregister()
CPU1: usb_role_switch_set_role() -> dwc3_usb_role_switch_set()

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1