Skip to contentSkip to conversation

usb: dwc2: exit clock_gating when stopping udc caused deadlock

PATCH In review xiaoxiao_li 2 participants 3 messages Mar 10 – Apr 10

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: dwc2: exit clock_gating when stopping udc caused deadlock

dwc2_gadget_exit_clock_gating invoke call_gadget to hold hsotg->lock, causing dwc2_hsotg_ep_disable_lock unable to acquire the lock, lead to a deadlock.

To ensure that enabling clock gating before stopping UDC does not cause the deadlock, we lock exit_clock_gating using spin_lock_irqsave and spin_unlock_irqrestore.

---
 drivers/usb/dwc2/gadget.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/dwc2/gadget.c +2 -0
index d216e26c7..c8b02c27d 100644
@@ -4607,7 +4607,9 @@ static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
 	/* Exit clock gating when driver is stopped. */
 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
 	    hsotg->bus_suspended && !hsotg->params.no_clock_gating) {
+		spin_lock_irqsave(&hsotg->lock, flags);
 		dwc2_gadget_exit_clock_gating(hsotg, 0);
+		spin_unlock_irqrestore(&hsotg->lock, flags);
 	}
 
 	/* all endpoints should be shutdown */
Signature · 1 line
2.35.1
maintainer
#1 lore raw
Quoted 9 lines · xiaoxiao_li

dwc2_gadget_exit_clock_gating invoke call_gadget to hold hsotg->lock, causing dwc2_hsotg_ep_disable_lock unable to acquire the lock, lead to a deadlock.

To ensure that enabling clock gating before stopping UDC does not cause the deadlock, we lock exit_clock_gating using spin_lock_irqsave and spin_unlock_irqrestore.

Please use your real name for your From: and Signed-off-by: line.

Quoted 9 lines
---
 drivers/usb/dwc2/gadget.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/dwc2/gadget.c +0 -0
index d216e26c7..c8b02c27d 100644

drivers/usb/dwc2/gadget.c static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget) · line 4612

Quoted 6 lines
drivers/usb/dwc2/gadget.c +2 -0
@@ -4607,7 +4607,9 @@ static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
 	/* Exit clock gating when driver is stopped. */
 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
 	    hsotg->bus_suspended && !hsotg->params.no_clock_gating) {
+		spin_lock_irqsave(&hsotg->lock, flags);
 		dwc2_gadget_exit_clock_gating(hsotg, 0);
+		spin_unlock_irqrestore(&hsotg->lock, flags);

If the call to dwc2_gadget_exit_clock_gating() wants the lock, how will calling it outside of it fix the problem? I'm confused by your changelog text, what am I missing here? And what changed to cause this to not work properly?

thanks,

greg k-h

#2 lore raw

Sorry for the unclear explanation.
This is caused by the patch modification ba78c2b3254c usb: dwc2: also exit clock_gating when stopping udc while suspended.

before this patch, the dwc2_gadget_exit_clock_gating was used in the IRQ handler,
and the handler would immediately acquire the lock with a spin_lock upon entering the function. However, dwc2_gadget_exit_clock_gating do unlocks first and then relocks after done. This modification is used in a normal flow, causing a deadlock to occur whenever this function is called.