[PATCH] usb: musb: Get the musb_qh poniter after musb_giveback

Xingxing Luo posted 1 patch 2 years, 5 months ago
There is a newer version of this series
drivers/usb/musb/musb_host.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
[PATCH] usb: musb: Get the musb_qh poniter after musb_giveback
Posted by Xingxing Luo 2 years, 5 months ago
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
Re: [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback
Posted by Sergey Shtylyov 2 years, 5 months ago
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
Re: [PATCH] usb: musb: Get the musb_qh poniter after musb_giveback
Posted by Sergey Shtylyov 2 years, 5 months ago
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