linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* usb: gadget: function: add spinlock for rndis response list
       [not found] <CGME20220218053230epcas2p1c63c8b28e7448988727221e0265c64fc@epcas2p1.samsung.com>
@ 2022-02-18  5:29 ` Daehwan Jung
  2022-02-18  7:57   ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Daehwan Jung @ 2022-02-18  5:29 UTC (permalink / raw)
  To: Felipe Balbi, Greg Kroah-Hartman, Muchun Song, Andrew Morton,
	Christian Brauner, Stephen Rothwell
  Cc: linux-usb, open list, Daehwan Jung

There's no lock for rndis response list. It could cause list corruption
if two different list_add at the same time like below.
It's better to add in rndis_add_response / rndis_free_response
/ rndis_get_next_response to prevent any race condition on response list.

[  361.894299] [1:   irq/191-dwc3:16979] list_add corruption.
next->prev should be prev (ffffff80651764d0),
but was ffffff883dc36f80. (next=ffffff80651764d0).

[  361.904380] [1:   irq/191-dwc3:16979] Call trace:
[  361.904391] [1:   irq/191-dwc3:16979]  __list_add_valid+0x74/0x90
[  361.904401] [1:   irq/191-dwc3:16979]  rndis_msg_parser+0x168/0x8c0
[  361.904409] [1:   irq/191-dwc3:16979]  rndis_command_complete+0x24/0x84
[  361.904417] [1:   irq/191-dwc3:16979]  usb_gadget_giveback_request+0x20/0xe4
[  361.904426] [1:   irq/191-dwc3:16979]  dwc3_gadget_giveback+0x44/0x60
[  361.904434] [1:   irq/191-dwc3:16979]  dwc3_ep0_complete_data+0x1e8/0x3a0
[  361.904442] [1:   irq/191-dwc3:16979]  dwc3_ep0_interrupt+0x29c/0x3dc
[  361.904450] [1:   irq/191-dwc3:16979]  dwc3_process_event_entry+0x78/0x6cc
[  361.904457] [1:   irq/191-dwc3:16979]  dwc3_process_event_buf+0xa0/0x1ec
[  361.904465] [1:   irq/191-dwc3:16979]  dwc3_thread_interrupt+0x34/0x5c

Fixes: f6281af9d62e ("usb: gadget: rndis: use list_for_each_entry_safe")
Signed-off-by: Daehwan Jung <dh10.jung@samsung.com>
---
 drivers/usb/gadget/function/rndis.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/usb/gadget/function/rndis.c b/drivers/usb/gadget/function/rndis.c
index b7ccf1803656..b4d58324e2d2 100644
--- a/drivers/usb/gadget/function/rndis.c
+++ b/drivers/usb/gadget/function/rndis.c
@@ -1011,16 +1011,21 @@ void rndis_add_hdr(struct sk_buff *skb)
 }
 EXPORT_SYMBOL_GPL(rndis_add_hdr);
 
+/* add spinlock to prevent race condition for rndis response list */
+static DEFINE_SPINLOCK(resp_lock);
+
 void rndis_free_response(struct rndis_params *params, u8 *buf)
 {
 	rndis_resp_t *r, *n;
 
+	spin_lock(&resp_lock);
 	list_for_each_entry_safe(r, n, &params->resp_queue, list) {
 		if (r->buf == buf) {
 			list_del(&r->list);
 			kfree(r);
 		}
 	}
+	spin_unlock(&resp_lock);
 }
 EXPORT_SYMBOL_GPL(rndis_free_response);
 
@@ -1030,14 +1035,17 @@ u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
 
 	if (!length) return NULL;
 
+	spin_lock(&resp_lock);
 	list_for_each_entry_safe(r, n, &params->resp_queue, list) {
 		if (!r->send) {
 			r->send = 1;
 			*length = r->length;
+			spin_unlock(&resp_lock);
 			return r->buf;
 		}
 	}
 
+	spin_unlock(&resp_lock);
 	return NULL;
 }
 EXPORT_SYMBOL_GPL(rndis_get_next_response);
@@ -1054,7 +1062,9 @@ static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
 	r->length = length;
 	r->send = 0;
 
+	spin_lock(&resp_lock);
 	list_add_tail(&r->list, &params->resp_queue);
+	spin_unlock(&resp_lock);
 	return r;
 }
 
-- 
2.31.1


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

* Re: usb: gadget: function: add spinlock for rndis response list
  2022-02-18  5:29 ` usb: gadget: function: add spinlock for rndis response list Daehwan Jung
@ 2022-02-18  7:57   ` Greg Kroah-Hartman
  2022-02-21  8:28     ` Jung Daehwan
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2022-02-18  7:57 UTC (permalink / raw)
  To: Daehwan Jung
  Cc: Felipe Balbi, Muchun Song, Andrew Morton, Christian Brauner,
	Stephen Rothwell, linux-usb, open list

On Fri, Feb 18, 2022 at 02:29:55PM +0900, Daehwan Jung wrote:
> There's no lock for rndis response list. It could cause list corruption
> if two different list_add at the same time like below.
> It's better to add in rndis_add_response / rndis_free_response
> / rndis_get_next_response to prevent any race condition on response list.
> 
> [  361.894299] [1:   irq/191-dwc3:16979] list_add corruption.
> next->prev should be prev (ffffff80651764d0),
> but was ffffff883dc36f80. (next=ffffff80651764d0).
> 
> [  361.904380] [1:   irq/191-dwc3:16979] Call trace:
> [  361.904391] [1:   irq/191-dwc3:16979]  __list_add_valid+0x74/0x90
> [  361.904401] [1:   irq/191-dwc3:16979]  rndis_msg_parser+0x168/0x8c0
> [  361.904409] [1:   irq/191-dwc3:16979]  rndis_command_complete+0x24/0x84
> [  361.904417] [1:   irq/191-dwc3:16979]  usb_gadget_giveback_request+0x20/0xe4
> [  361.904426] [1:   irq/191-dwc3:16979]  dwc3_gadget_giveback+0x44/0x60
> [  361.904434] [1:   irq/191-dwc3:16979]  dwc3_ep0_complete_data+0x1e8/0x3a0
> [  361.904442] [1:   irq/191-dwc3:16979]  dwc3_ep0_interrupt+0x29c/0x3dc
> [  361.904450] [1:   irq/191-dwc3:16979]  dwc3_process_event_entry+0x78/0x6cc
> [  361.904457] [1:   irq/191-dwc3:16979]  dwc3_process_event_buf+0xa0/0x1ec
> [  361.904465] [1:   irq/191-dwc3:16979]  dwc3_thread_interrupt+0x34/0x5c
> 
> Fixes: f6281af9d62e ("usb: gadget: rndis: use list_for_each_entry_safe")
> Signed-off-by: Daehwan Jung <dh10.jung@samsung.com>
> ---
>  drivers/usb/gadget/function/rndis.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/drivers/usb/gadget/function/rndis.c b/drivers/usb/gadget/function/rndis.c
> index b7ccf1803656..b4d58324e2d2 100644
> --- a/drivers/usb/gadget/function/rndis.c
> +++ b/drivers/usb/gadget/function/rndis.c
> @@ -1011,16 +1011,21 @@ void rndis_add_hdr(struct sk_buff *skb)
>  }
>  EXPORT_SYMBOL_GPL(rndis_add_hdr);
>  
> +/* add spinlock to prevent race condition for rndis response list */
> +static DEFINE_SPINLOCK(resp_lock);

Shouldn't this be one lock per rndis_params structure, and not a global
one for the whole driver?

thanks,

greg k-h

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

* Re: usb: gadget: function: add spinlock for rndis response list
  2022-02-18  7:57   ` Greg Kroah-Hartman
@ 2022-02-21  8:28     ` Jung Daehwan
  0 siblings, 0 replies; 3+ messages in thread
From: Jung Daehwan @ 2022-02-21  8:28 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Felipe Balbi, Muchun Song, Andrew Morton, Christian Brauner,
	Stephen Rothwell, linux-usb, open list

[-- Attachment #1: Type: text/plain, Size: 2397 bytes --]

On Fri, Feb 18, 2022 at 08:57:45AM +0100, Greg Kroah-Hartman wrote:
> On Fri, Feb 18, 2022 at 02:29:55PM +0900, Daehwan Jung wrote:
> > There's no lock for rndis response list. It could cause list corruption
> > if two different list_add at the same time like below.
> > It's better to add in rndis_add_response / rndis_free_response
> > / rndis_get_next_response to prevent any race condition on response list.
> > 
> > [  361.894299] [1:   irq/191-dwc3:16979] list_add corruption.
> > next->prev should be prev (ffffff80651764d0),
> > but was ffffff883dc36f80. (next=ffffff80651764d0).
> > 
> > [  361.904380] [1:   irq/191-dwc3:16979] Call trace:
> > [  361.904391] [1:   irq/191-dwc3:16979]  __list_add_valid+0x74/0x90
> > [  361.904401] [1:   irq/191-dwc3:16979]  rndis_msg_parser+0x168/0x8c0
> > [  361.904409] [1:   irq/191-dwc3:16979]  rndis_command_complete+0x24/0x84
> > [  361.904417] [1:   irq/191-dwc3:16979]  usb_gadget_giveback_request+0x20/0xe4
> > [  361.904426] [1:   irq/191-dwc3:16979]  dwc3_gadget_giveback+0x44/0x60
> > [  361.904434] [1:   irq/191-dwc3:16979]  dwc3_ep0_complete_data+0x1e8/0x3a0
> > [  361.904442] [1:   irq/191-dwc3:16979]  dwc3_ep0_interrupt+0x29c/0x3dc
> > [  361.904450] [1:   irq/191-dwc3:16979]  dwc3_process_event_entry+0x78/0x6cc
> > [  361.904457] [1:   irq/191-dwc3:16979]  dwc3_process_event_buf+0xa0/0x1ec
> > [  361.904465] [1:   irq/191-dwc3:16979]  dwc3_thread_interrupt+0x34/0x5c
> > 
> > Fixes: f6281af9d62e ("usb: gadget: rndis: use list_for_each_entry_safe")
> > Signed-off-by: Daehwan Jung <dh10.jung@samsung.com>
> > ---
> >  drivers/usb/gadget/function/rndis.c | 10 ++++++++++
> >  1 file changed, 10 insertions(+)
> > 
> > diff --git a/drivers/usb/gadget/function/rndis.c b/drivers/usb/gadget/function/rndis.c
> > index b7ccf1803656..b4d58324e2d2 100644
> > --- a/drivers/usb/gadget/function/rndis.c
> > +++ b/drivers/usb/gadget/function/rndis.c
> > @@ -1011,16 +1011,21 @@ void rndis_add_hdr(struct sk_buff *skb)
> >  }
> >  EXPORT_SYMBOL_GPL(rndis_add_hdr);
> >  
> > +/* add spinlock to prevent race condition for rndis response list */
> > +static DEFINE_SPINLOCK(resp_lock);
> 
> Shouldn't this be one lock per rndis_params structure, and not a global
> one for the whole driver?
> 
I agree with your comment. I'm going to modify and push it again.
Thanks for the comment.

Best Regards,
Jung Daehwan

> thanks,
> 
> greg k-h
> 

[-- Attachment #2: Type: text/plain, Size: 0 bytes --]



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

end of thread, other threads:[~2022-02-21  8:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20220218053230epcas2p1c63c8b28e7448988727221e0265c64fc@epcas2p1.samsung.com>
2022-02-18  5:29 ` usb: gadget: function: add spinlock for rndis response list Daehwan Jung
2022-02-18  7:57   ` Greg Kroah-Hartman
2022-02-21  8:28     ` Jung Daehwan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).