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 4/4] usb: dwc3: gadget: cancel err_recovery_work after usb_del_gadget
Date: Thu, 26 Nov 2026 13:15:45 +0000	[thread overview]
Message-ID: <sashiko-outbox-235585@kernel.org> (raw)
In-Reply-To: <20261126-topic-usb_dwc3_fixups-v2-4-c0cc2b75c518@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: dwc3: gadget: pullup(0) bypasses connect_mutex during
  err_recovery_work
--

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

usb: dwc3: gadget: cancel err_recovery_work after usb_del_gadget

This commit moves cancel_work_sync(&dwc->err_recovery_work) to after
usb_del_gadget() in dwc3_gadget_exit() so that work queued during gadget
unbind is canceled before freeing endpoints.

> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index 8f40e8dfa3f21..3c20f2ef64576 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -5047,9 +5047,9 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
>  	if (!dwc->gadget)
>  		return;
>  
> -	cancel_work_sync(&dwc->err_recovery_work);
>  	dwc3_enable_susphy(dwc, true);
>  	usb_del_gadget(dwc->gadget);
> +	cancel_work_sync(&dwc->err_recovery_work);
>  	dwc3_gadget_free_endpoints(dwc);
>  	usb_put_gadget(dwc->gadget);
>  	dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,

[Severity: Medium]
Can an in-flight dwc3_err_recovery_work() restart the controller and EP0
DMA after usb_del_gadget()'s pullup(0) returns early?

kworker                           CPU0
dwc3_err_recovery_work()
  mutex_lock(&dwc->connect_mutex)
  dwc3_gadget_soft_disconnect()
    dwc->pullups_connected = false
  if (dwc->softconnect)
                                  dwc3_gadget_exit()
                                    usb_del_gadget()
                                      ...
                                      dwc3_gadget_pullup(g, 0)
                                        dwc->softconnect = false
                                        if (pullups_connected == is_on)
                                          return 0;
  dwc3_gadget_soft_connect()
    __dwc3_gadget_start()
    dwc3_gadget_run_stop(dwc, true)
  mutex_unlock(&dwc->connect_mutex)
                                    cancel_work_sync(&dwc->err_recovery_work)
                                    dwc3_gadget_free_endpoints(dwc)
                                    dma_free_coherent(..., dwc->ep0_trb, ...)
                                      <-- controller and EP0 DMA active

In dwc3_gadget_pullup(), dwc->pullups_connected == is_on is checked before
acquiring dwc->connect_mutex:

drivers/usb/dwc3/gadget.c:dwc3_gadget_pullup() {
    ...
	if (dwc->pullups_connected == is_on) {
		pm_runtime_put(dwc->dev);
		return 0;
	}

	synchronize_irq(dwc->irq_gadget);

	/* Serialize against dwc3_err_recovery_work() */
	mutex_lock(&dwc->connect_mutex);
    ...
}

Because cancel_work_sync() now runs after usb_del_gadget(),
dwc3_err_recovery_work() can run concurrently with gadget unbind.  Once
dwc3_err_recovery_work() clears dwc->pullups_connected and passes the
dwc->softconnect check, dwc3_gadget_pullup(g, 0) sees
pullups_connected == false and returns early without waiting on
dwc->connect_mutex.  dwc3_err_recovery_work() then calls
dwc3_gadget_soft_connect(), leaving the controller running with active DMA
on dwc->ep0_trb when dwc3_gadget_exit() frees the endpoints and coherent
buffers.

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

  reply	other threads:[~2026-11-26 13:15 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
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 [this message]
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-235585@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