Skip to contentSkip to conversation

usb: dwc3: core: remove lock of otg mode during gadget suspend/resume to avoid deadlock

PATCH In review Meng Li 2 participants 4 messages Jun 13, 2024

Subsystems (2)
lore
Share & tools
lore

Meng Li

author
#0 lore raw

PATCH 1/1 usb: dwc3: core: remove lock of otg mode during gadget suspend/resume to avoid deadlock

When config CONFIG_USB_DWC3_DUAL_ROLE is selected, and trigger system to enter suspend status with below command:
echo mem > /sys/power/state
There will be a deadlock issue occurring. Because dwc3_gadget_suspend() also try to get the lock again when previous invoked dwc3_suspend_common() has got the lock . This issue is introduced by commit c7ebd8149ee5 ("usb: dwc3:
gadget: Fix NULL pointer dereference in dwc3_gadget_suspend") that removes the code of checking whether dwc->gadget_driver is NULL or not. It causes the following code is executed and deadlock occurs when trying to get the spinlock. To fix the deadlock issue, refer to commit 5265397f9442("usb: dwc3: Remove DWC3 locking during gadget suspend/resume"), remove lock of otg mode during gadget suspend/resume.

Fixes c7ebd8149ee5 ("Fix NULL pointer dereference in dwc3_gadget_suspend")
---
 drivers/usb/dwc3/core.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/usb/dwc3/core.c +0 -6
index a8fb10cc65bc..9bb4ab409bbb 100644
@@ -2112,7 +2112,6 @@ static int dwc3_core_init_for_resume(struct dwc3 *dwc)
 
 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
 {
-	unsigned long	flags;
 	u32 reg;
 
 	switch (dwc->current_dr_role) {
@@ -2150,9 +2149,7 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
 			break;
 
 		if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
-			spin_lock_irqsave(&dwc->lock, flags);
 			dwc3_gadget_suspend(dwc);
-			spin_unlock_irqrestore(&dwc->lock, flags);
 			synchronize_irq(dwc->irq_gadget);
 		}
 
@@ -2169,7 +2166,6 @@ static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
 
 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
 {
-	unsigned long	flags;
 	int		ret;
 	u32		reg;
 
@@ -2227,9 +2223,7 @@ static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)
 		if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
 			dwc3_otg_host_init(dwc);
 		} else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
-			spin_lock_irqsave(&dwc->lock, flags);
 			dwc3_gadget_resume(dwc);
-			spin_unlock_irqrestore(&dwc->lock, flags);
 		}
 
 		break;
Signature · 1 line
2.34.1
author
#1 lore raw

Sorry! please ignore this patch that is for the linux-stable kernel. I will create another one for mainline upstream.

Thanks,
LImeng

Quoted 78 lines

-----Original Message-----
From: Li, Meng <[email protected]>
Sent: Thursday, June 13, 2024 3:23 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Cc: Li, Meng <[email protected]>
Subject: [PATCH] usb: dwc3: core: remove lock of otg mode during gadget suspend/resume to avoid deadlock

When config CONFIG_USB_DWC3_DUAL_ROLE is selected, and trigger system to enter suspend status with below command:
echo mem > /sys/power/state
There will be a deadlock issue occurring. Because dwc3_gadget_suspend() also try to get the lock again when previous invoked dwc3_suspend_common() has got the lock . This issue is introduced by commit c7ebd8149ee5 ("usb:
dwc3:
gadget: Fix NULL pointer dereference in dwc3_gadget_suspend") that removes the code of checking whether dwc->gadget_driver is NULL or not. It causes the following code is executed and deadlock occurs when trying to get the spinlock.
To fix the deadlock issue, refer to commit 5265397f9442("usb: dwc3: Remove DWC3 locking during gadget suspend/resume"), remove lock of otg mode during gadget suspend/resume.

Fixes c7ebd8149ee5 ("Fix NULL pointer dereference in

dwc3_gadget_suspend")

---
 drivers/usb/dwc3/core.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/usb/dwc3/core.c index renamed from drivers/usb/dwc3/core.c +0 -0

a8fb10cc65bc..9bb4ab409bbb 100644

drivers/usb/dwc3/core.c +0 -6
@@ -2112,7 +2112,6 @@ static int dwc3_core_init_for_resume(struct dwc3
 *dwc)
 
 static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)  {
-	unsigned long	flags;
 	u32 reg;
 
 	switch (dwc->current_dr_role) {
@@ -2150,9 +2149,7 @@ static int dwc3_suspend_common(struct dwc3
 *dwc, pm_message_t msg)
 			break;
 
 		if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) {
-			spin_lock_irqsave(&dwc->lock, flags);
 			dwc3_gadget_suspend(dwc);
-			spin_unlock_irqrestore(&dwc->lock, flags);
 			synchronize_irq(dwc->irq_gadget);
 		}
@@ -2169,7 +2166,6 @@ static int dwc3_suspend_common(struct dwc3
 *dwc, pm_message_t msg)
 
 static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg)  {
-	unsigned long	flags;
 	int		ret;
 	u32		reg;
 
@@ -2227,9 +2223,7 @@ static int dwc3_resume_common(struct dwc3
 *dwc, pm_message_t msg)
 		if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) {
 			dwc3_otg_host_init(dwc);
 		} else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE)
 {
-			spin_lock_irqsave(&dwc->lock, flags);
 			dwc3_gadget_resume(dwc);
-			spin_unlock_irqrestore(&dwc->lock, flags);
 		}

break;
--
2.34.1

#2 lore raw
In reply to Meng Li
Quoted 4 lines · Meng Li

When config CONFIG_USB_DWC3_DUAL_ROLE is selected, and trigger system to enter suspend status with below command:
echo mem > /sys/power/state
There will be a deadlock issue occurring. Because dwc3_gadget_suspend() also

Quoted 8 lines

try to get the lock again when previous invoked dwc3_suspend_common() has got the lock . This issue is introduced by commit c7ebd8149ee5 ("usb: dwc3:
gadget: Fix NULL pointer dereference in dwc3_gadget_suspend") that removes the code of checking whether dwc->gadget_driver is NULL or not. It causes the following code is executed and deadlock occurs when trying to get the spinlock. To fix the deadlock issue, refer to commit 5265397f9442("usb: dwc3: Remove DWC3 locking during gadget suspend/resume"), remove lock of otg mode during gadget suspend/resume.

That's a funny way of fixing deadlocks: remove the lock. Of course it could be correct way with some justification why locking is not needed. No such justification here, so following your logic, let's remove locking everywhere and then no deadlocks possible!

Let me prepare patches for that...

Best regards,
Krzysztof

author
#3 lore raw
Quoted 33 lines

-----Original Message-----
From: Krzysztof Kozlowski <[email protected]>
Sent: Thursday, June 13, 2024 4:53 PM
To: Li, Meng <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] usb: dwc3: core: remove lock of otg mode during gadget suspend/resume to avoid deadlock

CAUTION: This email comes from a non Wind River email account! Do not click links or open attachments unless you recognize the sender and know the content is safe.

Quoted 1 line · Meng Li

When config CONFIG_USB_DWC3_DUAL_ROLE is selected, and trigger

system

Quoted 5 lines

to enter suspend status with below command:
echo mem > /sys/power/state
There will be a deadlock issue occurring. Because dwc3_gadget_suspend() also try to get the lock again when previous invoked dwc3_suspend_common() has got the lock . This issue is introduced

by commit c7ebd8149ee5 ("usb: dwc3:

Quoted 3 lines

gadget: Fix NULL pointer dereference in dwc3_gadget_suspend") that removes the code of checking whether dwc->gadget_driver is NULL or not. It causes the following code is executed and deadlock occurs when

trying to get the spinlock.

Quoted 4 lines

To fix the deadlock issue, refer to commit 5265397f9442("usb: dwc3:
Remove
DWC3 locking during gadget suspend/resume"), remove lock of otg mode during gadget suspend/resume.

Quoted 5 lines

That's a funny way of fixing deadlocks: remove the lock. Of course it could be correct way with some justification why locking is not needed.
No such justification here, so following your logic, let's remove locking everywhere and then no deadlocks possible!

I don't remove lock arbitrarily. I have show the commit 5265397f9442. It move the spinlock from dwc3_suspend_common() to dwc3_gadget_suspend() for device mode, but maybe forgot the case of otg move. So, cause deadlock when dwc3_gadget_suspend() try to get the spinlock that has been got by dwc3_suspend_common(). So, I think it also needs to remove the spinlock for otg mode.

Quoted 1 line

Let me prepare patches for that...

I think you can comment on patch, propose some advice, rather than negate the effort of others without any comment directly.
As a maintainer of some parts of kernel, I think your communication style is not friendly, and not conducive to developers contributing to the community, because their enthusiasm was dampened. If you create patch, please don't forgot to send to me, I want to learn what good solution you will show, and how much better than this patch.

Regards,
Meng

Quoted 3 lines

Best regards,
Krzysztof