Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback
@ 2023-09-14  1:56 Xingxing Luo
  2023-09-14 10:06 ` Sergey Shtylyov
  0 siblings, 1 reply; 4+ messages in thread
From: Xingxing Luo @ 2023-09-14  1:56 UTC (permalink / raw)
  To: b-liu, gregkh
  Cc: linux-usb, linux-kernel, xingxing0070.luo, Zhiyong.Liu,
	Cixi.Geng1, Orson.Zhai, zhang.lyra

When multiple threads are performing USB transmission, musb->lock will be
unlocked when musb_giveback is executed. At this time, qh may be released
in the dequeue process in other threads, resulting in a wild pointer, so
it needs to be here get qh again, and judge whether qh is NULL, and when
dequeue, you need to set qh to NULL.

Fixes: dbac5d07d13e ("usb: musb: host: don't start next rx urb if current one failed")
Signed-off-by: Xingxing Luo <xingxing.luo@unisoc.com>
---
 drivers/usb/musb/musb_host.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index a02c29216955..9df27db5847a 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -321,10 +321,16 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
 	musb_giveback(musb, urb, status);
 	qh->is_ready = ready;
 
+	/*
+	 * musb->lock had been unlocked in musb_giveback, so somtimes qh
+	 * may freed, need get it again
+	 */
+	qh = musb_ep_get_qh(hw_ep, is_in);
+
 	/* reclaim resources (and bandwidth) ASAP; deschedule it, and
 	 * invalidate qh as soon as list_empty(&hep->urb_list)
 	 */
-	if (list_empty(&qh->hep->urb_list)) {
+	if (qh != NULL && list_empty(&qh->hep->urb_list)) {
 		struct list_head	*head;
 		struct dma_controller	*dma = musb->dma_controller;
 
@@ -2398,6 +2404,7 @@ static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 		 * and its URB list has emptied, recycle this qh.
 		 */
 		if (ready && list_empty(&qh->hep->urb_list)) {
+			musb_ep_set_qh(qh->hw_ep, is_in, NULL);
 			qh->hep->hcpriv = NULL;
 			list_del(&qh->ring);
 			kfree(qh);
-- 
2.17.1


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

* Re: [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback
  2023-09-14  1:56 [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback Xingxing Luo
@ 2023-09-14 10:06 ` Sergey Shtylyov
  2023-09-14 10:15   ` Sergey Shtylyov
       [not found]   ` <CANnj+8S6AXjkdO3sQnK-H7a-TSOxbqUQi3U5H+q_8wA-JcAV5g@mail.gmail.com>
  0 siblings, 2 replies; 4+ messages in thread
From: Sergey Shtylyov @ 2023-09-14 10:06 UTC (permalink / raw)
  To: Xingxing Luo, b-liu, gregkh
  Cc: linux-usb, linux-kernel, xingxing0070.luo, Zhiyong.Liu,
	Cixi.Geng1, Orson.Zhai, zhang.lyra

Hello!

On 9/14/23 4:56 AM, Xingxing Luo wrote:

> When multiple threads are performing USB transmission, musb->lock will be
> unlocked when musb_giveback is executed. At this time, qh may be released
> in the dequeue process in other threads, resulting in a wild pointer, so
> it needs to be here get qh again, and judge whether qh is NULL, and when
> dequeue, you need to set qh to NULL.
> 
> Fixes: dbac5d07d13e ("usb: musb: host: don't start next rx urb if current one failed")
> Signed-off-by: Xingxing Luo <xingxing.luo@unisoc.com>
> ---
>  drivers/usb/musb/musb_host.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
> index a02c29216955..9df27db5847a 100644
> --- a/drivers/usb/musb/musb_host.c
> +++ b/drivers/usb/musb/musb_host.c
> @@ -321,10 +321,16 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
>  	musb_giveback(musb, urb, status);
>  	qh->is_ready = ready;
>  
> +	/*
> +	 * musb->lock had been unlocked in musb_giveback, so somtimes qh

   Sometimes?

> +	 * may freed, need get it again
> +	 */
> +	qh = musb_ep_get_qh(hw_ep, is_in);
> +
>  	/* reclaim resources (and bandwidth) ASAP; deschedule it, and
>  	 * invalidate qh as soon as list_empty(&hep->urb_list)
>  	 */
> -	if (list_empty(&qh->hep->urb_list)) {
> +	if (qh != NULL && list_empty(&qh->hep->urb_list)) {

   Just qh, perhaps?

[...]

MBR, Sergey

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

* Re: [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback
  2023-09-14 10:06 ` Sergey Shtylyov
@ 2023-09-14 10:15   ` Sergey Shtylyov
       [not found]   ` <CANnj+8S6AXjkdO3sQnK-H7a-TSOxbqUQi3U5H+q_8wA-JcAV5g@mail.gmail.com>
  1 sibling, 0 replies; 4+ messages in thread
From: Sergey Shtylyov @ 2023-09-14 10:15 UTC (permalink / raw)
  To: Xingxing Luo, b-liu, gregkh
  Cc: linux-usb, linux-kernel, xingxing0070.luo, Zhiyong.Liu,
	Cixi.Geng1, Orson.Zhai, zhang.lyra

On 9/14/23 1:06 PM, Sergey Shtylyov wrote:
[...]

>> When multiple threads are performing USB transmission, musb->lock will be
>> unlocked when musb_giveback is executed. At this time, qh may be released
>> in the dequeue process in other threads, resulting in a wild pointer, so
>> it needs to be here get qh again, and judge whether qh is NULL, and when
>> dequeue, you need to set qh to NULL.
>>
>> Fixes: dbac5d07d13e ("usb: musb: host: don't start next rx urb if current one failed")
>> Signed-off-by: Xingxing Luo <xingxing.luo@unisoc.com>
>> ---
>>  drivers/usb/musb/musb_host.c | 9 ++++++++-
>>  1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
>> index a02c29216955..9df27db5847a 100644
>> --- a/drivers/usb/musb/musb_host.c
>> +++ b/drivers/usb/musb/musb_host.c
>> @@ -321,10 +321,16 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
>>  	musb_giveback(musb, urb, status);
>>  	qh->is_ready = ready;
>>  
>> +	/*
>> +	 * musb->lock had been unlocked in musb_giveback, so somtimes qh
> 
>    Sometimes?
> 
>> +	 * may freed, need get it again

+	 * may be freed, need to get it again

   Overlooked it in the 1st review, sorry...

[...]

MBR, Sergey

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

* Re: [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback
       [not found]       ` <CANnj+8R8ueWOOE=yA-pug7EEs0DRyh65dyRV8owQae1pZv5b9g@mail.gmail.com>
@ 2023-09-18  5:50         ` xingxing luo
  0 siblings, 0 replies; 4+ messages in thread
From: xingxing luo @ 2023-09-18  5:50 UTC (permalink / raw)
  To: Sergey Shtylyov, b-liu
  Cc: linux-usb, linux-kernel, xingxing luo, Zhiyong.Liu, Cixi.Geng1,
	Orson.Zhai, Chunyan Zhang

Add more.

On Mon, Sep 18, 2023 at 1:22 PM xingxing luo <xingxing0070.luo@gmail.com> wrote:
>
> On Fri, Sep 15, 2023 at 4:48 PM Sergey Shtylyov <s.shtylyov@omp.ru> wrote:
> >
> > On 9/15/23 5:59 AM, xingxing luo wrote:
> > [...]
> >
> > >>> When multiple threads are performing USB transmission, musb->lock will be
> > >>> unlocked when musb_giveback is executed. At this time, qh may be released
> > >>> in the dequeue process in other threads, resulting in a wild pointer, so
> > >>> it needs to be here get qh again, and judge whether qh is NULL, and when
> > >>> dequeue, you need to set qh to NULL.
> > >>>
> > >>> Fixes: dbac5d07d13e ("usb: musb: host: don't start next rx urb if current one failed")
> > >>> Signed-off-by: Xingxing Luo <xingxing.luo@unisoc.com>
> > >>> ---
> > >>>  drivers/usb/musb/musb_host.c | 9 ++++++++-
> > >>>  1 file changed, 8 insertions(+), 1 deletion(-)
> > >>>
> > >>> diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
> > >>> index a02c29216955..9df27db5847a 100644
> > >>> --- a/drivers/usb/musb/musb_host.c
> > >>> +++ b/drivers/usb/musb/musb_host.c
> > >>> @@ -321,10 +321,16 @@ static void musb_advance_schedule(struct musb *musb, struct urb *urb,
> > >>>       musb_giveback(musb, urb, status);
> > >>>       qh->is_ready = ready;
> > >>>
> > >>> +     /*
> > >>> +      * musb->lock had been unlocked in musb_giveback, so somtimes qh
> > >>
> > >>    Sometimes?
> >
> >    You have a typo...
> >
> > >>
> > >>> +      * may freed, need get it again
> > >>> +      */
> > >>> +     qh = musb_ep_get_qh(hw_ep, is_in);
> > >>> +
> > >>>       /* reclaim resources (and bandwidth) ASAP; deschedule it, and
> > >>>        * invalidate qh as soon as list_empty(&hep->urb_list)
> > >>>        */
> > >>> -     if (list_empty(&qh->hep->urb_list)) {
> > >>> +     if (qh != NULL && list_empty(&qh->hep->urb_list)) {
> > >>
> > >>    Just qh, perhaps?
> > >
> > > Could you elaborate a little more?
> > > Thanks.
> >
> >    Just 'qh' gives you the same as 'qh != NULL'.
>
> Ok, I will address this in the next version.
>
> >
> > [...]
> >
> > MBR, Sergey

B.R Xingxing.Luo

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

end of thread, other threads:[~2023-09-18  5:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-14  1:56 [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback Xingxing Luo
2023-09-14 10:06 ` Sergey Shtylyov
2023-09-14 10:15   ` Sergey Shtylyov
     [not found]   ` <CANnj+8S6AXjkdO3sQnK-H7a-TSOxbqUQi3U5H+q_8wA-JcAV5g@mail.gmail.com>
     [not found]     ` <f3463cff-c6d5-07e7-2f94-cdcbc76d3957@omp.ru>
     [not found]       ` <CANnj+8R8ueWOOE=yA-pug7EEs0DRyh65dyRV8owQae1pZv5b9g@mail.gmail.com>
2023-09-18  5:50         ` xingxing luo

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