Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Konrad Dybcio" <konradybcio@kernel.org>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 2/4] usb: dwc3: gadget: clear softconnect on stop and failed pullup
Date: Thu, 26 Nov 2026 13:22:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-235592@kernel.org> (raw)
In-Reply-To: <20261126-topic-usb_dwc3_fixups-v2-2-c0cc2b75c518@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: dwc3: gadget: race with dwc3_err_recovery_work() on
  softconnect
--

commit a9997a16426be60f4fb31e8b8d1b87b342415fd2
Author: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>

usb: dwc3: gadget: clear softconnect on stop and failed pullup

This commit clears dwc->softconnect when dwc3_gadget_pullup() fails and when
dwc3_gadget_stop() stops the gadget under connect_mutex.

> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index 4fab2d57e7d3b..d7e22b5598768 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -2884,8 +2884,12 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
>  	ret = pm_runtime_get_sync(dwc->dev);
>  	if (!ret || ret < 0) {
>  		pm_runtime_put(dwc->dev);
> -		if (ret < 0)
> +		if (ret < 0) {
>  			pm_runtime_set_suspended(dwc->dev);
> +			spin_lock_irqsave(&dwc->lock, flags);
> +			dwc->softconnect = 0;
> +			spin_unlock_irqrestore(&dwc->lock, flags);
> +		}
>  		return ret;
>  	}
>  
> @@ -2902,6 +2906,11 @@ static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
>  		ret = dwc3_gadget_soft_disconnect(dwc);
>  	else
>  		ret = dwc3_gadget_soft_connect(dwc);
> +	if (ret) {
> +		spin_lock_irqsave(&dwc->lock, flags);
> +		dwc->softconnect = 0;
> +		spin_unlock_irqrestore(&dwc->lock, flags);
> +	}
>  	mutex_unlock(&dwc->connect_mutex);
>  
>  	pm_runtime_put(dwc->dev);
> @@ -3130,10 +3139,13 @@ static int dwc3_gadget_stop(struct usb_gadget *g)
>  	if (dwc->sys_wakeup)
>  		device_wakeup_disable(dwc->sysdev);
>  
> +	mutex_lock(&dwc->connect_mutex);
>  	spin_lock_irqsave(&dwc->lock, flags);
>  	dwc->gadget_driver	= NULL;
> +	dwc->softconnect = 0;
>  	dwc->max_cfg_eps = 0;
>  	spin_unlock_irqrestore(&dwc->lock, flags);
> +	mutex_unlock(&dwc->connect_mutex);
>  
>  	free_irq(dwc->irq_gadget, dwc->ev_buf);
>  

[Severity: Medium]
Can dwc3_err_recovery_work() still reconnect the controller during unbind,
or act on a failed pullup(1), because dwc3_gadget_pullup() updates
dwc->softconnect and checks dwc->pullups_connected outside
dwc->connect_mutex?

On the unbind path, dwc3_err_recovery_work() temporarily clears
dwc->pullups_connected before reconnecting:

CPU0 (kworker)                    CPU1 (gadget unbind)
dwc3_err_recovery_work()
  mutex_lock(&dwc->connect_mutex)
  dwc3_gadget_soft_disconnect()
    dwc->pullups_connected = false
  if (dwc->softconnect)
                                  dwc3_gadget_pullup(g, 0)
                                    dwc->softconnect = 0
                                    if (dwc->pullups_connected == 0)
                                      return 0
    dwc3_gadget_soft_connect()
      dwc->pullups_connected = true
  mutex_unlock(&dwc->connect_mutex)
                                  dwc3_gadget_stop(g)
                                    mutex_lock(&dwc->connect_mutex)
                                    dwc->gadget_driver = NULL
                                    dwc->softconnect = 0
                                    mutex_unlock(&dwc->connect_mutex)
                                    free_irq(dwc->irq_gadget, ...)
                                      <-- controller left running

Similarly, because dwc3_gadget_pullup(g, 1) sets dwc->softconnect = 1
before pm_runtime_get_sync() and before taking connect_mutex, an
in-progress dwc3_err_recovery_work() can observe dwc->softconnect == 1 and
call dwc3_gadget_soft_connect() even if pm_runtime_get_sync() subsequently
fails and rolls dwc->softconnect back to 0.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-topic-usb_dwc3_fixups-v2-0-c0cc2b75c518@oss.qualcomm.com?part=2

  reply	other threads:[~2026-11-26 13:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 13:01 [PATCH v2 0/4] DWC3 concurrency fixes Konrad Dybcio
2026-11-26 13:01 ` [PATCH v2 1/4] usb: dwc3: gadget: serialize connection bitfield writes Konrad Dybcio
2026-11-26 13:01 ` [PATCH v2 2/4] usb: dwc3: gadget: clear softconnect on stop and failed pullup Konrad Dybcio
2026-11-26 13:22   ` sashiko-bot [this message]
2026-11-26 13:01 ` [PATCH v2 3/4] usb: dwc3: gadget: serialize resume against error recovery Konrad Dybcio
2026-11-26 13:22   ` sashiko-bot
2026-11-26 13:01 ` [PATCH v2 4/4] usb: dwc3: gadget: cancel err_recovery_work after usb_del_gadget Konrad Dybcio
2026-11-26 13:15   ` sashiko-bot
2026-11-26 13:22 ` [PATCH v2 0/4] DWC3 concurrency fixes 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=sashiko-outbox-235592@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=konradybcio@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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