All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop
@ 2021-01-05 16:56 Thinh Nguyen
  2021-01-05 16:56 ` [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started Thinh Nguyen
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Thinh Nguyen @ 2021-01-05 16:56 UTC (permalink / raw)
  To: Felipe Balbi, Greg Kroah-Hartman, Thinh.Nguyen, linux-usb
  Cc: John Youn, stable

Add some checks to avoid going through the start/stop sequence if the gadget
had already started/stopped. This series base-commit is Greg's usb-linus
branch.



Thinh Nguyen (2):
  usb: dwc3: gadget: Check if the gadget had started
  usb: dwc3: gadget: Check if the gadget had stopped

 drivers/usb/dwc3/gadget.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)


base-commit: 96ebc9c871d8a28fb22aa758dd9188a4732df482
-- 
2.28.0


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

* [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started
  2021-01-05 16:56 [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Thinh Nguyen
@ 2021-01-05 16:56 ` Thinh Nguyen
  2021-01-06  7:53   ` Felipe Balbi
  2021-01-05 16:56 ` [PATCH 2/2] usb: dwc3: gadget: Check if the gadget had stopped Thinh Nguyen
  2021-01-08  2:36 ` [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Peter Chen
  2 siblings, 1 reply; 9+ messages in thread
From: Thinh Nguyen @ 2021-01-05 16:56 UTC (permalink / raw)
  To: Felipe Balbi, Greg Kroah-Hartman, Thinh.Nguyen, linux-usb
  Cc: John Youn, stable

If the gadget had already started, don't try to start again. Otherwise,
we may request the same threaded irq with the same dev_id, it will mess
up the interrupt freeing logic. This can happen if a user tries to
trigger a soft-connect from soft_connect sysfs multiple times. Check to
make sure that the gadget had started before proceeding to request
threaded irq. Fix this by checking if there's bounded gadget driver.

Cc: stable@vger.kernel.org
Fixes: b0d7ffd44ba9 ("usb: dwc3: gadget: don't request IRQs in atomic")
Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
---
 drivers/usb/dwc3/gadget.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 25f654b79e48..51d81a32ce78 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -2303,35 +2303,27 @@ static int dwc3_gadget_start(struct usb_gadget *g,
 	int			ret = 0;
 	int			irq;
 
+	if (dwc->gadget_driver) {
+		dev_err(dwc->dev, "%s is already bound to %s\n",
+				dwc->gadget->name,
+				dwc->gadget_driver->driver.name);
+		return -EBUSY;
+	}
+
 	irq = dwc->irq_gadget;
 	ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
 			IRQF_SHARED, "dwc3", dwc->ev_buf);
 	if (ret) {
 		dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
 				irq, ret);
-		goto err0;
+		return ret;
 	}
 
 	spin_lock_irqsave(&dwc->lock, flags);
-	if (dwc->gadget_driver) {
-		dev_err(dwc->dev, "%s is already bound to %s\n",
-				dwc->gadget->name,
-				dwc->gadget_driver->driver.name);
-		ret = -EBUSY;
-		goto err1;
-	}
-
 	dwc->gadget_driver	= driver;
 	spin_unlock_irqrestore(&dwc->lock, flags);
 
 	return 0;
-
-err1:
-	spin_unlock_irqrestore(&dwc->lock, flags);
-	free_irq(irq, dwc);
-
-err0:
-	return ret;
 }
 
 static void __dwc3_gadget_stop(struct dwc3 *dwc)
-- 
2.28.0


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

* [PATCH 2/2] usb: dwc3: gadget: Check if the gadget had stopped
  2021-01-05 16:56 [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Thinh Nguyen
  2021-01-05 16:56 ` [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started Thinh Nguyen
@ 2021-01-05 16:56 ` Thinh Nguyen
  2021-01-06  7:54   ` Felipe Balbi
  2021-01-08  2:36 ` [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Peter Chen
  2 siblings, 1 reply; 9+ messages in thread
From: Thinh Nguyen @ 2021-01-05 16:56 UTC (permalink / raw)
  To: Felipe Balbi, Greg Kroah-Hartman, Thinh.Nguyen, linux-usb
  Cc: John Youn, stable

If the gadget had already stopped, don't try to stop again. Otherwise
we'd get a warning for trying to free an already freed irq. This can
happen if a user tries to trigger a soft-disconnect from soft_connect
sysfs multiple times. The fix is to check if there's a bounded gadget
driver to determined if the gadget had stopped.

Cc: stable@vger.kernel.org
Fixes: 8698e2acf3a5 ("usb: dwc3: gadget: introduce and use enable/disable irq methods")
Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
---
 drivers/usb/dwc3/gadget.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 51d81a32ce78..9ec70282e610 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -2338,6 +2338,10 @@ static int dwc3_gadget_stop(struct usb_gadget *g)
 	struct dwc3		*dwc = gadget_to_dwc(g);
 	unsigned long		flags;
 
+	/* The controller is already stopped if there's no gadget driver */
+	if (!dwc->gadget_driver)
+		return 0;
+
 	spin_lock_irqsave(&dwc->lock, flags);
 	dwc->gadget_driver	= NULL;
 	spin_unlock_irqrestore(&dwc->lock, flags);
-- 
2.28.0


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

* Re: [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started
  2021-01-05 16:56 ` [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started Thinh Nguyen
@ 2021-01-06  7:53   ` Felipe Balbi
  2021-01-06  9:35     ` Thinh Nguyen
  0 siblings, 1 reply; 9+ messages in thread
From: Felipe Balbi @ 2021-01-06  7:53 UTC (permalink / raw)
  To: Thinh Nguyen, Greg Kroah-Hartman, Thinh.Nguyen, linux-usb
  Cc: John Youn, stable


Hi,

Thinh Nguyen <Thinh.Nguyen@synopsys.com> writes:
> If the gadget had already started, don't try to start again. Otherwise,
> we may request the same threaded irq with the same dev_id, it will mess
> up the interrupt freeing logic. This can happen if a user tries to
> trigger a soft-connect from soft_connect sysfs multiple times. Check to
> make sure that the gadget had started before proceeding to request
> threaded irq. Fix this by checking if there's bounded gadget driver.

Looks like this should be fixed at the framework level, otherwise we
will have to patch every single UDC. After that is done, we can remove
the dwc->gadget_driver check from here.

-- 
balbi

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

* Re: [PATCH 2/2] usb: dwc3: gadget: Check if the gadget had stopped
  2021-01-05 16:56 ` [PATCH 2/2] usb: dwc3: gadget: Check if the gadget had stopped Thinh Nguyen
@ 2021-01-06  7:54   ` Felipe Balbi
  0 siblings, 0 replies; 9+ messages in thread
From: Felipe Balbi @ 2021-01-06  7:54 UTC (permalink / raw)
  To: Thinh Nguyen, Greg Kroah-Hartman, Thinh.Nguyen, linux-usb
  Cc: John Youn, stable


Hi,

Thinh Nguyen <Thinh.Nguyen@synopsys.com> writes:
> If the gadget had already stopped, don't try to stop again. Otherwise
> we'd get a warning for trying to free an already freed irq. This can
> happen if a user tries to trigger a soft-disconnect from soft_connect
> sysfs multiple times. The fix is to check if there's a bounded gadget
> driver to determined if the gadget had stopped.
>
> Cc: stable@vger.kernel.org
> Fixes: 8698e2acf3a5 ("usb: dwc3: gadget: introduce and use enable/disable irq methods")
> Signed-off-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
> ---
>  drivers/usb/dwc3/gadget.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index 51d81a32ce78..9ec70282e610 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -2338,6 +2338,10 @@ static int dwc3_gadget_stop(struct usb_gadget *g)
>  	struct dwc3		*dwc = gadget_to_dwc(g);
>  	unsigned long		flags;
>  
> +	/* The controller is already stopped if there's no gadget driver */
> +	if (!dwc->gadget_driver)
> +		return 0;

same here. Better done at the framework

-- 
balbi

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

* Re: [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started
  2021-01-06  7:53   ` Felipe Balbi
@ 2021-01-06  9:35     ` Thinh Nguyen
  0 siblings, 0 replies; 9+ messages in thread
From: Thinh Nguyen @ 2021-01-06  9:35 UTC (permalink / raw)
  To: Felipe Balbi, Thinh Nguyen, Greg Kroah-Hartman, linux-usb
  Cc: John Youn, stable

Hi,

Felipe Balbi wrote:
> Hi,
>
> Thinh Nguyen <Thinh.Nguyen@synopsys.com> writes:
>> If the gadget had already started, don't try to start again. Otherwise,
>> we may request the same threaded irq with the same dev_id, it will mess
>> up the interrupt freeing logic. This can happen if a user tries to
>> trigger a soft-connect from soft_connect sysfs multiple times. Check to
>> make sure that the gadget had started before proceeding to request
>> threaded irq. Fix this by checking if there's bounded gadget driver.
> Looks like this should be fixed at the framework level, otherwise we
> will have to patch every single UDC. After that is done, we can remove
> the dwc->gadget_driver check from here.
>

Sure. We can do that.

Thanks,
Thinh

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

* Re: [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop
  2021-01-05 16:56 [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Thinh Nguyen
  2021-01-05 16:56 ` [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started Thinh Nguyen
  2021-01-05 16:56 ` [PATCH 2/2] usb: dwc3: gadget: Check if the gadget had stopped Thinh Nguyen
@ 2021-01-08  2:36 ` Peter Chen
  2021-01-08  2:40   ` Thinh Nguyen
  2 siblings, 1 reply; 9+ messages in thread
From: Peter Chen @ 2021-01-08  2:36 UTC (permalink / raw)
  To: Thinh Nguyen
  Cc: Felipe Balbi, Greg Kroah-Hartman, linux-usb, John Youn, stable

On 21-01-05 08:56:28, Thinh Nguyen wrote:
> Add some checks to avoid going through the start/stop sequence if the gadget
> had already started/stopped. This series base-commit is Greg's usb-linus
> branch.
> 

Hi Thinh,

What's the sequence your could reproduce it?

Peter
> 
> 
> Thinh Nguyen (2):
>   usb: dwc3: gadget: Check if the gadget had started
>   usb: dwc3: gadget: Check if the gadget had stopped
> 
>  drivers/usb/dwc3/gadget.c | 28 ++++++++++++----------------
>  1 file changed, 12 insertions(+), 16 deletions(-)
> 
> 
> base-commit: 96ebc9c871d8a28fb22aa758dd9188a4732df482
> -- 
> 2.28.0
> 

-- 

Thanks,
Peter Chen


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

* Re: [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop
  2021-01-08  2:36 ` [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Peter Chen
@ 2021-01-08  2:40   ` Thinh Nguyen
  2021-01-08  9:40     ` Peter Chen
  0 siblings, 1 reply; 9+ messages in thread
From: Thinh Nguyen @ 2021-01-08  2:40 UTC (permalink / raw)
  To: Peter Chen, Thinh Nguyen
  Cc: Felipe Balbi, Greg Kroah-Hartman, linux-usb, John Youn, stable

Hi Peter,

Peter Chen wrote:
> On 21-01-05 08:56:28, Thinh Nguyen wrote:
>> Add some checks to avoid going through the start/stop sequence if the gadget
>> had already started/stopped. This series base-commit is Greg's usb-linus
>> branch.
>>
> Hi Thinh,
>
> What's the sequence your could reproduce it?
>
> Peter

You can test as follow:

# echo connect > /sys/class/udc/<UDC>/soft_connect
# echo connect > /sys/class/udc/<UDC>/soft_connect

and

# echo disconnect > /sys/class/udc/<UDC>/soft_connect
# echo disconnect > /sys/class/udc/<UDC>/soft_connect

Thinh

>>
>> Thinh Nguyen (2):
>>   usb: dwc3: gadget: Check if the gadget had started
>>   usb: dwc3: gadget: Check if the gadget had stopped
>>
>>  drivers/usb/dwc3/gadget.c | 28 ++++++++++++----------------
>>  1 file changed, 12 insertions(+), 16 deletions(-)
>>
>>
>> base-commit: 96ebc9c871d8a28fb22aa758dd9188a4732df482
>> -- 
>> 2.28.0
>>


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

* Re: [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop
  2021-01-08  2:40   ` Thinh Nguyen
@ 2021-01-08  9:40     ` Peter Chen
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Chen @ 2021-01-08  9:40 UTC (permalink / raw)
  To: Thinh Nguyen
  Cc: Felipe Balbi, Greg Kroah-Hartman, linux-usb, John Youn, stable

On 21-01-08 02:40:55, Thinh Nguyen wrote:
> Hi Peter,
> 
> Peter Chen wrote:
> > On 21-01-05 08:56:28, Thinh Nguyen wrote:
> >> Add some checks to avoid going through the start/stop sequence if the gadget
> >> had already started/stopped. This series base-commit is Greg's usb-linus
> >> branch.
> >>
> > Hi Thinh,
> >
> > What's the sequence your could reproduce it?
> >
> > Peter
> 
> You can test as follow:
> 
> # echo connect > /sys/class/udc/<UDC>/soft_connect
> # echo connect > /sys/class/udc/<UDC>/soft_connect
> 
> and
> 
> # echo disconnect > /sys/class/udc/<UDC>/soft_connect
> # echo disconnect > /sys/class/udc/<UDC>/soft_connect
> 
> Thinh
> 

Thanks, now I reproduce the issue. Another improvement you
might consider adding is checking return value for usb_gadget_udc_start
at soft_connect_store.

-- 

Thanks,
Peter Chen


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

end of thread, other threads:[~2021-01-08  9:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-05 16:56 [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Thinh Nguyen
2021-01-05 16:56 ` [PATCH 1/2] usb: dwc3: gadget: Check if the gadget had started Thinh Nguyen
2021-01-06  7:53   ` Felipe Balbi
2021-01-06  9:35     ` Thinh Nguyen
2021-01-05 16:56 ` [PATCH 2/2] usb: dwc3: gadget: Check if the gadget had stopped Thinh Nguyen
2021-01-06  7:54   ` Felipe Balbi
2021-01-08  2:36 ` [PATCH 0/2] usb: dwc3: gadget: Check for multiple start/stop Peter Chen
2021-01-08  2:40   ` Thinh Nguyen
2021-01-08  9:40     ` Peter Chen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.