Linux USB
 help / color / mirror / Atom feed
From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
To: Keisuke Tsukuda <tkdkei@outlook.jp>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sven Peter <sven@kernel.org>, Janne Grunau <j@jannau.net>,
	Neal Gompa <neal@gompa.dev>,
	Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
	"asahi@lists.linux.dev" <asahi@lists.linux.dev>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] usb: roles: wait for role callbacks on unregister
Date: Tue, 17 Nov 2026 12:19:44 +0100	[thread overview]
Message-ID: <avw4UE8H7t0Ybq6_@black.igk.intel.com> (raw)
In-Reply-To: <20261112024101.83321-1-tkdkei@outlook.jp>

Hi,

On Thu, Nov 12, 2026 at 02:41:24AM +0000, Keisuke Tsukuda wrote:
> 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 Apple DWC3 remove path currently holds its private mutex around
> unregister. Its role callbacks take that mutex under the switch lock, so
> waiting on the switch lock in unregister would reverse the lock order.
> Unregister before taking the Apple mutex.
> 
> Fixes: fde0aa6c175a ("usb: common: Small class for USB role switches")
> Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
> ---
> Tested on ARM64 QEMU with Linux 7.3.4 and mainline 7.4-rc2
> (067c5c24cef2), with KASAN and lockdep enabled. Synthetic providers
> trigger KASAN use-after-free reports in both set and get callbacks before
> this change. With the change, unregister waits for the callbacks and
> neither test reports KASAN.
> 
> A lockdep test modeling the Apple callback/remove lock order warns for the
> original order and not for the new one. Apple DWC3 hardware was not tested.
> 
>  drivers/usb/roles/class.c     |   18 ++++++++++++++----
>  drivers/usb/dwc3/dwc3-apple.c |    4 ++--
>  2 files changed, 16 insertions(+), 6 deletions(-)

If you get this reply for the second time, then I'm sorry for the
noise. There's something wrong with our SMTP. I don't see all my
replies on the usb ml.

Please split this, one patch for the class and another for dwc3.

Also, don't forget to CC stable. This is a fix:
https://docs.kernel.org/process/submitting-patches.html#select-the-recipients-for-your-patch

Thanks,

> 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);
> 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

-- 
heikki

      parent reply	other threads:[~2026-11-17 11:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  2:41 [PATCH] usb: roles: wait for role callbacks on unregister Keisuke Tsukuda
2026-11-12  2:51 ` sashiko-bot
2026-11-17 11:19 ` Heikki Krogerus [this message]

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=avw4UE8H7t0Ybq6_@black.igk.intel.com \
    --to=heikki.krogerus@linux.intel.com \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=asahi@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=j@jannau.net \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=neal@gompa.dev \
    --cc=sven@kernel.org \
    --cc=tkdkei@outlook.jp \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox