Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: dwc3: gadget: destroy connect_mutex after usb_del_gadget
@ 2026-11-23 20:13 Konrad Dybcio
  2026-11-23 20:30 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Konrad Dybcio @ 2026-11-23 20:13 UTC (permalink / raw)
  To: Thinh Nguyen, Greg Kroah-Hartman; +Cc: linux-usb, usb4-upstream, Konrad Dybcio

usb_del_gadget() unbinds the function driver, which may still call
dwc3_gadget_pullup() and take connect_mutex. Destroying the mutex
before that point leaves a live lock behind during teardown.

Avoid it.

Signed-off-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
---
 drivers/usb/dwc3/gadget.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index a76f669a796c2e0eb955969a88523affcb672ae2..673f4bffb8ca85778d084820ecfed49f50d1cac3 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -5027,9 +5027,9 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
 		return;
 
 	cancel_work_sync(&dwc->err_recovery_work);
-	mutex_destroy(&dwc->connect_mutex);
 	dwc3_enable_susphy(dwc, true);
 	usb_del_gadget(dwc->gadget);
+	mutex_destroy(&dwc->connect_mutex);
 	dwc3_gadget_free_endpoints(dwc);
 	usb_put_gadget(dwc->gadget);
 	dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,

---
base-commit: 749a4de370409464cf8501ec5b2451df45c84eb4
change-id: 20261123-topic-dwc3_gadget_fix-2d05048742cf

Best regards,
-- 
Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: dwc3: gadget: destroy connect_mutex after usb_del_gadget
  2026-11-23 20:13 [PATCH] usb: dwc3: gadget: destroy connect_mutex after usb_del_gadget Konrad Dybcio
@ 2026-11-23 20:30 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-23 20:30 UTC (permalink / raw)
  To: Konrad Dybcio; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] usb: dwc3: gadget: missing Fixes trailer for commit 1ff77cbefe5a
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] usb: dwc3: gadget: premature connect_mutex and work
  teardown in dwc3_gadget_exit() [1]
--

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

> usb: dwc3: gadget: destroy connect_mutex after usb_del_gadget
>
> usb_del_gadget() unbinds the function driver, which may still call
> dwc3_gadget_pullup() and take connect_mutex. Destroying the mutex
> before that point leaves a live lock behind during teardown.
>
> Avoid it.
>
> Signed-off-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>

[Severity: Low]
Should this include a Fixes tag for the commit that introduced
connect_mutex and placed mutex_destroy() before usb_del_gadget()?

  Fixes: 1ff77cbefe5a ("usb: dwc3: gadget: fix IRQ storm on invalid event buffer count")

> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index a76f669a796c2..673f4bffb8ca8 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -5027,9 +5027,9 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
>  		return;
>  
>  	cancel_work_sync(&dwc->err_recovery_work);
> -	mutex_destroy(&dwc->connect_mutex);
>  	dwc3_enable_susphy(dwc, true);
>  	usb_del_gadget(dwc->gadget);
> +	mutex_destroy(&dwc->connect_mutex);
>  	dwc3_gadget_free_endpoints(dwc);
>  	usb_put_gadget(dwc->gadget);
>  	dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,

[Severity: Critical]
This problem wasn't introduced by this patch, but can
dwc->err_recovery_work still be rescheduled before usb_del_gadget()
finishes [1]?

dwc->irq_gadget remains active until usb_del_gadget() stops the UDC and
calls dwc3_gadget_stop() -> free_irq():

CPU0                                    <hardirq>
dwc3_gadget_exit()
  cancel_work_sync(&dwc->err_recovery_work)
                                        dwc3_check_event_buf()
                                          schedule_work(&dwc->err_recovery_work)
  usb_del_gadget()
    ...
    dwc3_gadget_stop()
      free_irq(dwc->irq_gadget, ...)
  mutex_destroy(&dwc->connect_mutex)
  dwc3_gadget_free_endpoints(dwc)
  usb_put_gadget(dwc->gadget)
                                        dwc3_err_recovery_work()
                                          mutex_lock(&dwc->connect_mutex)
                                          ...  <-- use-after-free

Should cancel_work_sync(&dwc->err_recovery_work) also be moved after
usb_del_gadget() (right before mutex_destroy())?

[1] https://sashiko.dev/#/bug/linux-daa55f7b-cbeb-467d-b442-1d3982b1d885

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-topic-dwc3_gadget_fix-v1-1-dec7679b81b7@oss.qualcomm.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23 20:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 20:13 [PATCH] usb: dwc3: gadget: destroy connect_mutex after usb_del_gadget Konrad Dybcio
2026-11-23 20:30 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox