* [PATCH v2] can: mcba_usb: Fix memory leak when cancelling urb
@ 2021-01-11 10:49 Bui Quang Minh
2021-01-11 12:00 ` Oliver Neukum
0 siblings, 1 reply; 5+ messages in thread
From: Bui Quang Minh @ 2021-01-11 10:49 UTC (permalink / raw)
To: linux-usb
Cc: a.darwish, bigeasy, gregkh, linux-kernel, minhquangbui99, stern,
syzkaller-bugs, tglx
In mcba_usb_read_bulk_callback(), when we don't resubmit or fails to
resubmit the urb, we need to deallocate the transfer buffer that is
allocated in mcba_usb_start().
Reported-by: syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com
Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
---
v1: add memory leak fix when not resubmitting urb
v2: add memory leak fix when failing to resubmit urb
drivers/net/can/usb/mcba_usb.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/drivers/net/can/usb/mcba_usb.c b/drivers/net/can/usb/mcba_usb.c
index df54eb7d4b36..30236e640116 100644
--- a/drivers/net/can/usb/mcba_usb.c
+++ b/drivers/net/can/usb/mcba_usb.c
@@ -584,6 +584,8 @@ static void mcba_usb_read_bulk_callback(struct urb *urb)
case -EPIPE:
case -EPROTO:
case -ESHUTDOWN:
+ usb_free_coherent(urb->dev, urb->transfer_buffer_length,
+ urb->transfer_buffer, urb->transfer_dma);
return;
default:
@@ -615,11 +617,14 @@ static void mcba_usb_read_bulk_callback(struct urb *urb)
retval = usb_submit_urb(urb, GFP_ATOMIC);
- if (retval == -ENODEV)
- netif_device_detach(netdev);
- else if (retval)
+ if (retval < 0) {
netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
retval);
+ usb_free_coherent(urb->dev, urb->transfer_buffer_length,
+ urb->transfer_buffer, urb->transfer_dma);
+ if (retval == -ENODEV)
+ netif_device_detach(netdev);
+ }
}
/* Start USB device */
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] can: mcba_usb: Fix memory leak when cancelling urb
2021-01-11 10:49 [PATCH v2] can: mcba_usb: Fix memory leak when cancelling urb Bui Quang Minh
@ 2021-01-11 12:00 ` Oliver Neukum
2021-01-11 14:31 ` Bui Quang Minh
0 siblings, 1 reply; 5+ messages in thread
From: Oliver Neukum @ 2021-01-11 12:00 UTC (permalink / raw)
To: Bui Quang Minh, linux-usb
Cc: a.darwish, bigeasy, gregkh, linux-kernel, stern, syzkaller-bugs, tglx
Am Montag, den 11.01.2021, 10:49 +0000 schrieb Bui Quang Minh:
> In mcba_usb_read_bulk_callback(), when we don't resubmit or fails to
> resubmit the urb, we need to deallocate the transfer buffer that is
> allocated in mcba_usb_start().
>
> Reported-by: syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com
> Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
> ---
> v1: add memory leak fix when not resubmitting urb
> v2: add memory leak fix when failing to resubmit urb
>
> drivers/net/can/usb/mcba_usb.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/net/can/usb/mcba_usb.c b/drivers/net/can/usb/mcba_usb.c
> index df54eb7d4b36..30236e640116 100644
> --- a/drivers/net/can/usb/mcba_usb.c
> +++ b/drivers/net/can/usb/mcba_usb.c
> @@ -584,6 +584,8 @@ static void mcba_usb_read_bulk_callback(struct urb *urb)
> case -EPIPE:
> case -EPROTO:
> case -ESHUTDOWN:
> + usb_free_coherent(urb->dev, urb->transfer_buffer_length,
> + urb->transfer_buffer, urb->transfer_dma);
> return;
>
Can you call usb_free_coherent() in what can be hard IRQ context?
Regards
Oliver
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] can: mcba_usb: Fix memory leak when cancelling urb
2021-01-11 12:00 ` Oliver Neukum
@ 2021-01-11 14:31 ` Bui Quang Minh
2021-01-12 6:42 ` Minh Bùi Quang
0 siblings, 1 reply; 5+ messages in thread
From: Bui Quang Minh @ 2021-01-11 14:31 UTC (permalink / raw)
To: Oliver Neukum
Cc: linux-usb, a.darwish, bigeasy, gregkh, linux-kernel, stern,
syzkaller-bugs, tglx
On Mon, Jan 11, 2021 at 01:00:31PM +0100, Oliver Neukum wrote:
> Am Montag, den 11.01.2021, 10:49 +0000 schrieb Bui Quang Minh:
> > In mcba_usb_read_bulk_callback(), when we don't resubmit or fails to
> > resubmit the urb, we need to deallocate the transfer buffer that is
> > allocated in mcba_usb_start().
> >
> > Reported-by: syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com
> > Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
> > ---
> > v1: add memory leak fix when not resubmitting urb
> > v2: add memory leak fix when failing to resubmit urb
> >
> > drivers/net/can/usb/mcba_usb.c | 11 ++++++++---
> > 1 file changed, 8 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/net/can/usb/mcba_usb.c b/drivers/net/can/usb/mcba_usb.c
> > index df54eb7d4b36..30236e640116 100644
> > --- a/drivers/net/can/usb/mcba_usb.c
> > +++ b/drivers/net/can/usb/mcba_usb.c
> > @@ -584,6 +584,8 @@ static void mcba_usb_read_bulk_callback(struct urb *urb)
> > case -EPIPE:
> > case -EPROTO:
> > case -ESHUTDOWN:
> > + usb_free_coherent(urb->dev, urb->transfer_buffer_length,
> > + urb->transfer_buffer, urb->transfer_dma);
> > return;
> >
>
> Can you call usb_free_coherent() in what can be hard IRQ context?
You are right, I digged in the code and saw some comments that on some
architectures, usb_free_coherent() cannot be called in hard IRQ context.
I see the usb_free_coherent() is called in write_bulk_callback too. I will
send a patch that uses usb_anchor to keep track of these urbs and cleanup
the transfer buffer later in disconnect().
Thank you for your review,
Quang Minh.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] can: mcba_usb: Fix memory leak when cancelling urb
2021-01-11 14:31 ` Bui Quang Minh
@ 2021-01-12 6:42 ` Minh Bùi Quang
2021-01-21 15:19 ` Bui Quang Minh
0 siblings, 1 reply; 5+ messages in thread
From: Minh Bùi Quang @ 2021-01-12 6:42 UTC (permalink / raw)
To: Oliver Neukum
Cc: linux-usb, a.darwish, bigeasy, Greg Kroah-Hartman, linux-kernel,
Alan Stern, syzkaller-bugs, Thomas Gleixner
On Mon, Jan 11, 2021 at 9:31 PM Bui Quang Minh <minhquangbui99@gmail.com> wrote:
>
> On Mon, Jan 11, 2021 at 01:00:31PM +0100, Oliver Neukum wrote:
> > Am Montag, den 11.01.2021, 10:49 +0000 schrieb Bui Quang Minh:
> > > In mcba_usb_read_bulk_callback(), when we don't resubmit or fails to
> > > resubmit the urb, we need to deallocate the transfer buffer that is
> > > allocated in mcba_usb_start().
> > >
> > > Reported-by: syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com
> > > Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
> > > ---
> > > v1: add memory leak fix when not resubmitting urb
> > > v2: add memory leak fix when failing to resubmit urb
> > >
> > > drivers/net/can/usb/mcba_usb.c | 11 ++++++++---
> > > 1 file changed, 8 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/net/can/usb/mcba_usb.c b/drivers/net/can/usb/mcba_usb.c
> > > index df54eb7d4b36..30236e640116 100644
> > > --- a/drivers/net/can/usb/mcba_usb.c
> > > +++ b/drivers/net/can/usb/mcba_usb.c
> > > @@ -584,6 +584,8 @@ static void mcba_usb_read_bulk_callback(struct urb *urb)
> > > case -EPIPE:
> > > case -EPROTO:
> > > case -ESHUTDOWN:
> > > + usb_free_coherent(urb->dev, urb->transfer_buffer_length,
> > > + urb->transfer_buffer, urb->transfer_dma);
> > > return;
> > >
> >
> > Can you call usb_free_coherent() in what can be hard IRQ context?
>
> You are right, I digged in the code and saw some comments that on some
> architectures, usb_free_coherent() cannot be called in hard IRQ context.
> I see the usb_free_coherent() is called in write_bulk_callback too. I will
> send a patch that uses usb_anchor to keep track of these urbs and cleanup
> the transfer buffer later in disconnect().
Hi, I have sent a version 3 patch. However, I found out that usb_free_coherent()
is ok in this situation. In usb_free_coherent(), if the buffer is allocated via
dma_alloc_coherent() in usb_alloc_coherent(), dma_free_coherent() is called.
In dma_free_coherent(), ops->free() may be called in some cases which may
contains calls to vunmap() that is not permitted in interrupt context. However,
in usb_alloc_coherent(), buffer can be allocated from dma pool if the
size is less
than 2048 and the buffer size in mcba_usb is obviously less than 2048.
As a result,
usb_free_coherent() will at most fall in the path that calls
dma_pool_free(), which is
safe. Am I right?
Thanks,
Quang Minh.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] can: mcba_usb: Fix memory leak when cancelling urb
2021-01-12 6:42 ` Minh Bùi Quang
@ 2021-01-21 15:19 ` Bui Quang Minh
0 siblings, 0 replies; 5+ messages in thread
From: Bui Quang Minh @ 2021-01-21 15:19 UTC (permalink / raw)
To: linux-usb
Cc: a.darwish, bigeasy, Greg Kroah-Hartman, linux-kernel, Alan Stern,
syzkaller-bugs, Thomas Gleixner, Oliver Neukum,
Wolfgang Grandegger, Marc Kleine-Budde
On Tue, Jan 12, 2021 at 01:42:33PM +0700, Minh Bùi Quang wrote:
> On Mon, Jan 11, 2021 at 9:31 PM Bui Quang Minh <minhquangbui99@gmail.com> wrote:
> >
> > On Mon, Jan 11, 2021 at 01:00:31PM +0100, Oliver Neukum wrote:
> > > Am Montag, den 11.01.2021, 10:49 +0000 schrieb Bui Quang Minh:
> > > > In mcba_usb_read_bulk_callback(), when we don't resubmit or fails to
> > > > resubmit the urb, we need to deallocate the transfer buffer that is
> > > > allocated in mcba_usb_start().
> > > >
> > > > Reported-by: syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com
> > > > Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
> > > > ---
> > > > v1: add memory leak fix when not resubmitting urb
> > > > v2: add memory leak fix when failing to resubmit urb
> > > >
> > > > drivers/net/can/usb/mcba_usb.c | 11 ++++++++---
> > > > 1 file changed, 8 insertions(+), 3 deletions(-)
> > > >
> > > > diff --git a/drivers/net/can/usb/mcba_usb.c b/drivers/net/can/usb/mcba_usb.c
> > > > index df54eb7d4b36..30236e640116 100644
> > > > --- a/drivers/net/can/usb/mcba_usb.c
> > > > +++ b/drivers/net/can/usb/mcba_usb.c
> > > > @@ -584,6 +584,8 @@ static void mcba_usb_read_bulk_callback(struct urb *urb)
> > > > case -EPIPE:
> > > > case -EPROTO:
> > > > case -ESHUTDOWN:
> > > > + usb_free_coherent(urb->dev, urb->transfer_buffer_length,
> > > > + urb->transfer_buffer, urb->transfer_dma);
> > > > return;
> > > >
> > >
> > > Can you call usb_free_coherent() in what can be hard IRQ context?
> >
> > You are right, I digged in the code and saw some comments that on some
> > architectures, usb_free_coherent() cannot be called in hard IRQ context.
> > I see the usb_free_coherent() is called in write_bulk_callback too. I will
> > send a patch that uses usb_anchor to keep track of these urbs and cleanup
> > the transfer buffer later in disconnect().
>
> Hi, I have sent a version 3 patch. However, I found out that usb_free_coherent()
> is ok in this situation. In usb_free_coherent(), if the buffer is allocated via
> dma_alloc_coherent() in usb_alloc_coherent(), dma_free_coherent() is called.
> In dma_free_coherent(), ops->free() may be called in some cases which may
> contains calls to vunmap() that is not permitted in interrupt context. However,
> in usb_alloc_coherent(), buffer can be allocated from dma pool if the
> size is less
> than 2048 and the buffer size in mcba_usb is obviously less than 2048.
> As a result,
> usb_free_coherent() will at most fall in the path that calls
> dma_pool_free(), which is
> safe. Am I right?
Hi, I'm CC'ing CAN network driver maintainers so we can discuss the patch properly.
I'm so sorry for spamming emails.
Thanks,
Quang Minh.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-01-21 15:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-11 10:49 [PATCH v2] can: mcba_usb: Fix memory leak when cancelling urb Bui Quang Minh
2021-01-11 12:00 ` Oliver Neukum
2021-01-11 14:31 ` Bui Quang Minh
2021-01-12 6:42 ` Minh Bùi Quang
2021-01-21 15:19 ` Bui Quang Minh
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.