From: Ilia Snezhkov <iasnezhkov@gmail.com>
To: heikki.krogerus@linux.intel.com, gregkh@linuxfoundation.org
Cc: hdegoede@redhat.com, sven@kernel.org, j@jannau.net,
Thinh.Nguyen@synopsys.com, asahi@lists.linux.dev,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 2/2] usb: roles: serialize registered checks and unregister under sw->lock
Date: Mon, 30 Nov 2026 15:43:06 +0000 [thread overview]
Message-ID: <20261130154304.90482-3-iasnezhkov@gmail.com> (raw)
In-Reply-To: <20261130154304.90482-1-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().
Fixes: b787a3e78175 ("usb: roles: don't get/set_role() when usb_role_switch is unregistered")
Cc: stable@vger.kernel.org
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v3:
- Add Cc: stable@vger.kernel.org (per Heikki Krogerus).
- Move the AI coding assistant note below ---.
Found and drafted with an AI coding assistant.
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)
WARNING: multiple messages have this Message-ID (diff)
From: Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@kernel.org>
To: heikki.krogerus@linux.intel.com, gregkh@linuxfoundation.org
Cc: hdegoede@redhat.com, sven@kernel.org, j@jannau.net,
Thinh.Nguyen@synopsys.com, asahi@lists.linux.dev,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 2/2] usb: roles: serialize registered checks and unregister under sw->lock
Date: Mon, 30 Nov 2026 15:43:06 +0000 [thread overview]
Message-ID: <20261130154304.90482-3-iasnezhkov@gmail.com> (raw)
In-Reply-To: <20261130154304.90482-1-iasnezhkov@gmail.com>
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().
Fixes: b787a3e78175 ("usb: roles: don't get/set_role() when usb_role_switch is unregistered")
Cc: stable@vger.kernel.org
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v3:
- Add Cc: stable@vger.kernel.org (per Heikki Krogerus).
- Move the AI coding assistant note below ---.
Found and drafted with an AI coding assistant.
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)
next prev parent reply other threads:[~2026-11-30 15:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 15:43 [PATCH v3 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order Ilia Snezhkov
2026-11-30 15:43 ` Ilia Snezhkov via B4 Relay
2026-11-30 15:43 ` [PATCH v3 1/2] usb: dwc3: apple: unregister role switch outside appledwc->lock Ilia Snezhkov
2026-11-30 15:43 ` Ilia Snezhkov via B4 Relay
2026-11-30 15:43 ` Ilia Snezhkov [this message]
2026-11-30 15:43 ` [PATCH v3 2/2] usb: roles: serialize registered checks and unregister under sw->lock Ilia Snezhkov via B4 Relay
2026-11-30 15:56 ` [PATCH v3 0/2] usb: roles: serialize unregister against callbacks and fix dwc3-apple lock order sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130154304.90482-3-iasnezhkov@gmail.com \
--to=iasnezhkov@gmail.com \
--cc=Thinh.Nguyen@synopsys.com \
--cc=asahi@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=hdegoede@redhat.com \
--cc=heikki.krogerus@linux.intel.com \
--cc=j@jannau.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=sven@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.