linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress
@ 2022-08-14 14:25 Gautam Menghani
  2022-08-14 14:45 ` Greg KH
  2022-09-02 18:12 ` Gautam Menghani
  0 siblings, 2 replies; 7+ messages in thread
From: Gautam Menghani @ 2022-08-14 14:25 UTC (permalink / raw)
  To: sean, mchehab
  Cc: Gautam Menghani, linux-media, linux-kernel, linux-kernel-mentees,
	syzbot+0c3cb6dc05fbbdc3ad66, hdanton

The warning "URB submitted while active" is reported if the function
send_packet() in imon.c is called if a write is already is in progress.
Add a check to return -EBUSY in case a write is already is in progress.
Also, mark tx.busy as false after transmission is completed.

Fixes: 21677cfc562a ("V4L/DVB: ir-core: add imon driver")
Cc: hdanton@sina.com
Suggested-by: hdanton@sina.com
Link: https://syzkaller.appspot.com/bug?id=e378e6a51fbe6c5cc43e34f131cc9a315ef0337e
Reported-by: syzbot+0c3cb6dc05fbbdc3ad66@syzkaller.appspotmail.com
Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
---
 drivers/media/rc/imon.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
index 735b925da998..a5b997c2c7e2 100644
--- a/drivers/media/rc/imon.c
+++ b/drivers/media/rc/imon.c
@@ -598,6 +598,8 @@ static int send_packet(struct imon_context *ictx)
 	int retval = 0;
 	struct usb_ctrlrequest *control_req = NULL;
 
+	if (ictx->tx.busy)
+		return -EBUSY;
 	/* Check if we need to use control or interrupt urb */
 	if (!ictx->tx_control) {
 		pipe = usb_sndintpipe(ictx->usbdev_intf0,
@@ -654,6 +656,7 @@ static int send_packet(struct imon_context *ictx)
 			pr_err_ratelimited("task interrupted\n");
 		}
 		mutex_lock(&ictx->lock);
+		ictx->tx.busy = false;
 
 		retval = ictx->tx.status;
 		if (retval)
-- 
2.34.1


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

* Re: [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress
  2022-08-14 14:25 [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress Gautam Menghani
@ 2022-08-14 14:45 ` Greg KH
  2022-08-14 16:44   ` Gautam Menghani
  2022-09-02 18:12 ` Gautam Menghani
  1 sibling, 1 reply; 7+ messages in thread
From: Greg KH @ 2022-08-14 14:45 UTC (permalink / raw)
  To: Gautam Menghani
  Cc: sean, mchehab, hdanton, linux-kernel,
	syzbot+0c3cb6dc05fbbdc3ad66, linux-kernel-mentees, linux-media

On Sun, Aug 14, 2022 at 07:55:42PM +0530, Gautam Menghani wrote:
> The warning "URB submitted while active" is reported if the function
> send_packet() in imon.c is called if a write is already is in progress.
> Add a check to return -EBUSY in case a write is already is in progress.
> Also, mark tx.busy as false after transmission is completed.
> 
> Fixes: 21677cfc562a ("V4L/DVB: ir-core: add imon driver")
> Cc: hdanton@sina.com
> Suggested-by: hdanton@sina.com
> Link: https://syzkaller.appspot.com/bug?id=e378e6a51fbe6c5cc43e34f131cc9a315ef0337e
> Reported-by: syzbot+0c3cb6dc05fbbdc3ad66@syzkaller.appspotmail.com
> Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
> ---
>  drivers/media/rc/imon.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
> index 735b925da998..a5b997c2c7e2 100644
> --- a/drivers/media/rc/imon.c
> +++ b/drivers/media/rc/imon.c
> @@ -598,6 +598,8 @@ static int send_packet(struct imon_context *ictx)
>  	int retval = 0;
>  	struct usb_ctrlrequest *control_req = NULL;
>  
> +	if (ictx->tx.busy)
> +		return -EBUSY;

What happens if we go busy right after this check?  Where is the locking
here to protect this?

thanks,

greg k-h

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

* Re: [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress
  2022-08-14 14:45 ` Greg KH
@ 2022-08-14 16:44   ` Gautam Menghani
  0 siblings, 0 replies; 7+ messages in thread
From: Gautam Menghani @ 2022-08-14 16:44 UTC (permalink / raw)
  To: Greg KH
  Cc: sean, mchehab, hdanton, linux-kernel,
	syzbot+0c3cb6dc05fbbdc3ad66, linux-kernel-mentees, linux-media

On Sun, Aug 14, 2022 at 04:45:08PM +0200, Greg KH wrote:
> On Sun, Aug 14, 2022 at 07:55:42PM +0530, Gautam Menghani wrote:
> > The warning "URB submitted while active" is reported if the function
> > send_packet() in imon.c is called if a write is already is in progress.
> > Add a check to return -EBUSY in case a write is already is in progress.
> > Also, mark tx.busy as false after transmission is completed.
> > 
> > Fixes: 21677cfc562a ("V4L/DVB: ir-core: add imon driver")
> > Cc: hdanton@sina.com
> > Suggested-by: hdanton@sina.com
> > Link: https://syzkaller.appspot.com/bug?id=e378e6a51fbe6c5cc43e34f131cc9a315ef0337e
> > Reported-by: syzbot+0c3cb6dc05fbbdc3ad66@syzkaller.appspotmail.com
> > Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
> > ---
> >  drivers/media/rc/imon.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
> > index 735b925da998..a5b997c2c7e2 100644
> > --- a/drivers/media/rc/imon.c
> > +++ b/drivers/media/rc/imon.c
> > @@ -598,6 +598,8 @@ static int send_packet(struct imon_context *ictx)
> >  	int retval = 0;
> >  	struct usb_ctrlrequest *control_req = NULL;
> >  
> > +	if (ictx->tx.busy)
> > +		return -EBUSY;
> 
> What happens if we go busy right after this check?  Where is the locking
> here to protect this?

All calls to send_packet() are protected with ictx->lock() held. Are you referring 
to something else?

Also, if we return busy, the task is interrupted and the packet transaction fails, 
just like the current behaviour. With the above patch, warning is not triggered.
Here's the log from running the reproducer (with patch applied).

imon 1-1:0.0: Looks like you're trying to use an IR protocol this device does not support
imon 1-1:0.0: Unsupported IR protocol specified, overriding to iMON IR protocol
rc rc0: iMON Remote (15c2:0040) as /devices/platform/dummy_hcd.0/usb1/1-1/1-1:0.0/rc/rc0
input: iMON Remote (15c2:0040) as /devices/platform/dummy_hcd.0/usb1/1-1/1-1:0.0/rc/rc0/input5
imon 1-1:0.0: iMON device (15c2:0040, intf0) on usb<1:2> initialized
imon:vfd_write: send packet #0 failed
imon:send_packet: task interrupted
imon:send_packet: packet tx failed (-2)
imon:vfd_write: send packet #0 failed
usb 1-1: USB disconnect, device number 2

Thanks,
Gautam

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

* Re: [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress
  2022-08-14 14:25 [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress Gautam Menghani
  2022-08-14 14:45 ` Greg KH
@ 2022-09-02 18:12 ` Gautam Menghani
  2022-09-03  7:49   ` Sean Young
  1 sibling, 1 reply; 7+ messages in thread
From: Gautam Menghani @ 2022-09-02 18:12 UTC (permalink / raw)
  To: sean, mchehab
  Cc: linux-media, linux-kernel, linux-kernel-mentees,
	syzbot+0c3cb6dc05fbbdc3ad66, hdanton

On Sun, Aug 14, 2022 at 07:55:42PM +0530, Gautam Menghani wrote:
> The warning "URB submitted while active" is reported if the function
> send_packet() in imon.c is called if a write is already is in progress.
> Add a check to return -EBUSY in case a write is already is in progress.
> Also, mark tx.busy as false after transmission is completed.
> 
> Fixes: 21677cfc562a ("V4L/DVB: ir-core: add imon driver")
> Cc: hdanton@sina.com
> Suggested-by: hdanton@sina.com
> Link: https://syzkaller.appspot.com/bug?id=e378e6a51fbe6c5cc43e34f131cc9a315ef0337e
> Reported-by: syzbot+0c3cb6dc05fbbdc3ad66@syzkaller.appspotmail.com
> Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
> ---
>  drivers/media/rc/imon.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
> index 735b925da998..a5b997c2c7e2 100644
> --- a/drivers/media/rc/imon.c
> +++ b/drivers/media/rc/imon.c
> @@ -598,6 +598,8 @@ static int send_packet(struct imon_context *ictx)
>  	int retval = 0;
>  	struct usb_ctrlrequest *control_req = NULL;
>  
> +	if (ictx->tx.busy)
> +		return -EBUSY;
>  	/* Check if we need to use control or interrupt urb */
>  	if (!ictx->tx_control) {
>  		pipe = usb_sndintpipe(ictx->usbdev_intf0,
> @@ -654,6 +656,7 @@ static int send_packet(struct imon_context *ictx)
>  			pr_err_ratelimited("task interrupted\n");
>  		}
>  		mutex_lock(&ictx->lock);
> +		ictx->tx.busy = false;
>  
>  		retval = ictx->tx.status;
>  		if (retval)
> -- 
> 2.34.1
> 
Hi,

Please review the above fix and let me know if any changes are needed.

Thanks,
Gautam

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

* Re: [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress
  2022-09-02 18:12 ` Gautam Menghani
@ 2022-09-03  7:49   ` Sean Young
  2022-09-03  9:38     ` Gautam Menghani
  0 siblings, 1 reply; 7+ messages in thread
From: Sean Young @ 2022-09-03  7:49 UTC (permalink / raw)
  To: Gautam Menghani
  Cc: mchehab, linux-media, linux-kernel, linux-kernel-mentees,
	syzbot+0c3cb6dc05fbbdc3ad66, hdanton

On Fri, Sep 02, 2022 at 11:42:41PM +0530, Gautam Menghani wrote:
> On Sun, Aug 14, 2022 at 07:55:42PM +0530, Gautam Menghani wrote:
> > The warning "URB submitted while active" is reported if the function
> > send_packet() in imon.c is called if a write is already is in progress.
> > Add a check to return -EBUSY in case a write is already is in progress.
> > Also, mark tx.busy as false after transmission is completed.
> > 
> > Fixes: 21677cfc562a ("V4L/DVB: ir-core: add imon driver")
> > Cc: hdanton@sina.com
> > Suggested-by: hdanton@sina.com
> > Link: https://syzkaller.appspot.com/bug?id=e378e6a51fbe6c5cc43e34f131cc9a315ef0337e
> > Reported-by: syzbot+0c3cb6dc05fbbdc3ad66@syzkaller.appspotmail.com
> > Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
> > ---
> >  drivers/media/rc/imon.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
> > index 735b925da998..a5b997c2c7e2 100644
> > --- a/drivers/media/rc/imon.c
> > +++ b/drivers/media/rc/imon.c
> > @@ -598,6 +598,8 @@ static int send_packet(struct imon_context *ictx)
> >  	int retval = 0;
> >  	struct usb_ctrlrequest *control_req = NULL;
> >  
> > +	if (ictx->tx.busy)
> > +		return -EBUSY;
> >  	/* Check if we need to use control or interrupt urb */
> >  	if (!ictx->tx_control) {
> >  		pipe = usb_sndintpipe(ictx->usbdev_intf0,
> > @@ -654,6 +656,7 @@ static int send_packet(struct imon_context *ictx)
> >  			pr_err_ratelimited("task interrupted\n");
> >  		}
> >  		mutex_lock(&ictx->lock);
> > +		ictx->tx.busy = false;
> >  
> >  		retval = ictx->tx.status;
> >  		if (retval)
> > -- 
> > 2.34.1
> > 
> Hi,
> 
> Please review the above fix and let me know if any changes are needed.

Greg has pointed out issues with this patch: there is no locking.

Thanks,

Sean

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

* Re: [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress
  2022-09-03  7:49   ` Sean Young
@ 2022-09-03  9:38     ` Gautam Menghani
  2022-09-03 10:04       ` Greg KH
  0 siblings, 1 reply; 7+ messages in thread
From: Gautam Menghani @ 2022-09-03  9:38 UTC (permalink / raw)
  To: Sean Young
  Cc: mchehab, linux-media, linux-kernel, linux-kernel-mentees,
	syzbot+0c3cb6dc05fbbdc3ad66, hdanton

On Sat, Sep 03, 2022 at 08:49:56AM +0100, Sean Young wrote:
> On Fri, Sep 02, 2022 at 11:42:41PM +0530, Gautam Menghani wrote:
> > On Sun, Aug 14, 2022 at 07:55:42PM +0530, Gautam Menghani wrote:
> > > The warning "URB submitted while active" is reported if the function
> > > send_packet() in imon.c is called if a write is already is in progress.
> > > Add a check to return -EBUSY in case a write is already is in progress.
> > > Also, mark tx.busy as false after transmission is completed.
> > > 
> > > Fixes: 21677cfc562a ("V4L/DVB: ir-core: add imon driver")
> > > Cc: hdanton@sina.com
> > > Suggested-by: hdanton@sina.com
> > > Link: https://syzkaller.appspot.com/bug?id=e378e6a51fbe6c5cc43e34f131cc9a315ef0337e
> > > Reported-by: syzbot+0c3cb6dc05fbbdc3ad66@syzkaller.appspotmail.com
> > > Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
> > > ---
> > >  drivers/media/rc/imon.c | 3 +++
> > >  1 file changed, 3 insertions(+)
> > > 
> > > diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
> > > index 735b925da998..a5b997c2c7e2 100644
> > > --- a/drivers/media/rc/imon.c
> > > +++ b/drivers/media/rc/imon.c
> > > @@ -598,6 +598,8 @@ static int send_packet(struct imon_context *ictx)
> > >  	int retval = 0;
> > >  	struct usb_ctrlrequest *control_req = NULL;
> > >  
> > > +	if (ictx->tx.busy)
> > > +		return -EBUSY;
> > >  	/* Check if we need to use control or interrupt urb */
> > >  	if (!ictx->tx_control) {
> > >  		pipe = usb_sndintpipe(ictx->usbdev_intf0,
> > > @@ -654,6 +656,7 @@ static int send_packet(struct imon_context *ictx)
> > >  			pr_err_ratelimited("task interrupted\n");
> > >  		}
> > >  		mutex_lock(&ictx->lock);
> > > +		ictx->tx.busy = false;
> > >  
> > >  		retval = ictx->tx.status;
> > >  		if (retval)
> > > -- 
> > > 2.34.1
> > > 
> > Hi,
> > 
> > Please review the above fix and let me know if any changes are needed.
> 
> Greg has pointed out issues with this patch: there is no locking.
> 
> Thanks,
> 
> Sean

Hi,

I am a bit confused about the locking part. All the calls to send_packet() are
made with ictx->lock held. So will the following be sufficient?

lockdep_assert_held(&ictx->lock);
if (ictx->tx.busy)
	return -EBUSY;

Or do we need to add a mutex/spin lock inside ictx->tx structure? 

Or should we instead wait for the the tx to be completed as follows:
wait_for_completion(&ictx->tx.finished);

Please advise.

Thanks,
Gautam

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

* Re: [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress
  2022-09-03  9:38     ` Gautam Menghani
@ 2022-09-03 10:04       ` Greg KH
  0 siblings, 0 replies; 7+ messages in thread
From: Greg KH @ 2022-09-03 10:04 UTC (permalink / raw)
  To: Gautam Menghani
  Cc: Sean Young, hdanton, linux-kernel, syzbot+0c3cb6dc05fbbdc3ad66,
	mchehab, linux-kernel-mentees, linux-media

On Sat, Sep 03, 2022 at 03:08:23PM +0530, Gautam Menghani wrote:
> On Sat, Sep 03, 2022 at 08:49:56AM +0100, Sean Young wrote:
> > On Fri, Sep 02, 2022 at 11:42:41PM +0530, Gautam Menghani wrote:
> > > On Sun, Aug 14, 2022 at 07:55:42PM +0530, Gautam Menghani wrote:
> > > > The warning "URB submitted while active" is reported if the function
> > > > send_packet() in imon.c is called if a write is already is in progress.
> > > > Add a check to return -EBUSY in case a write is already is in progress.
> > > > Also, mark tx.busy as false after transmission is completed.
> > > > 
> > > > Fixes: 21677cfc562a ("V4L/DVB: ir-core: add imon driver")
> > > > Cc: hdanton@sina.com
> > > > Suggested-by: hdanton@sina.com
> > > > Link: https://syzkaller.appspot.com/bug?id=e378e6a51fbe6c5cc43e34f131cc9a315ef0337e
> > > > Reported-by: syzbot+0c3cb6dc05fbbdc3ad66@syzkaller.appspotmail.com
> > > > Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
> > > > ---
> > > >  drivers/media/rc/imon.c | 3 +++
> > > >  1 file changed, 3 insertions(+)
> > > > 
> > > > diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
> > > > index 735b925da998..a5b997c2c7e2 100644
> > > > --- a/drivers/media/rc/imon.c
> > > > +++ b/drivers/media/rc/imon.c
> > > > @@ -598,6 +598,8 @@ static int send_packet(struct imon_context *ictx)
> > > >  	int retval = 0;
> > > >  	struct usb_ctrlrequest *control_req = NULL;
> > > >  
> > > > +	if (ictx->tx.busy)
> > > > +		return -EBUSY;
> > > >  	/* Check if we need to use control or interrupt urb */
> > > >  	if (!ictx->tx_control) {
> > > >  		pipe = usb_sndintpipe(ictx->usbdev_intf0,
> > > > @@ -654,6 +656,7 @@ static int send_packet(struct imon_context *ictx)
> > > >  			pr_err_ratelimited("task interrupted\n");
> > > >  		}
> > > >  		mutex_lock(&ictx->lock);
> > > > +		ictx->tx.busy = false;
> > > >  
> > > >  		retval = ictx->tx.status;
> > > >  		if (retval)
> > > > -- 
> > > > 2.34.1
> > > > 
> > > Hi,
> > > 
> > > Please review the above fix and let me know if any changes are needed.
> > 
> > Greg has pointed out issues with this patch: there is no locking.
> > 
> > Thanks,
> > 
> > Sean
> 
> Hi,
> 
> I am a bit confused about the locking part. All the calls to send_packet() are
> made with ictx->lock held. So will the following be sufficient?
> 
> lockdep_assert_held(&ictx->lock);
> if (ictx->tx.busy)
> 	return -EBUSY;
> 
> Or do we need to add a mutex/spin lock inside ictx->tx structure? 

If a lock is held, how can this function be called at the same time
through different ways?  That should not happen.

> Or should we instead wait for the the tx to be completed as follows:
> wait_for_completion(&ictx->tx.finished);

It depends on what you are trying to protect from here.  What is the
real issue that is allowing multiple submissions of the same urb to
happen?  Fix that issue instead of trying to paper over it in this lower
level function please.

thanks,

greg k-h

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

end of thread, other threads:[~2022-09-03 10:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-14 14:25 [PATCH] drivers/media/rc: Ensure usb_submit_urb() is not called if write is in progress Gautam Menghani
2022-08-14 14:45 ` Greg KH
2022-08-14 16:44   ` Gautam Menghani
2022-09-02 18:12 ` Gautam Menghani
2022-09-03  7:49   ` Sean Young
2022-09-03  9:38     ` Gautam Menghani
2022-09-03 10:04       ` Greg KH

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).