Skip to contentSkip to conversation

can: mcba_usb: fix memory leak in mcba_usb

PATCH In review Yasushi SHOJI 4 participants 16 messages Jul 25, 2021 – Jul 27, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi Pavel,

Apologize for the late reply.

Since 6bd3d80d1f019cef, my Microchip CAN Analyzer stopped working, more precisely I can't capture any data with it and repeated messages from the driver flod the syslog. I usually use the Debian kernel image and linux 5.10.46-2 migrated to unstable on July 20th. I noticed my device stopped working a few days later but didn't have time to bisect.

Does your device work with the patch?
Does the patch work on the main line?

I've posted some report with my hardware configuration at debian mailing list:
https://bugs.debian.org/990850

Please let me know if you need any more information.

Best,
--
yashi

#1 lore raw
Quoted 2 lines · On Sun, 25 Jul 2021 16:42:49 +0900 Yasushi SHOJI <[email protected]>…

Hi Pavel,

Hi, Yasushi!

Quoted 9 lines

Apologize for the late reply.

Since 6bd3d80d1f019cef, my Microchip CAN Analyzer stopped working, more precisely I can't capture any data with it and repeated messages from the driver flod the syslog. I usually use the Debian kernel image and linux 5.10.46-2 migrated to unstable on July 20th. I noticed my device stopped working a few days later but didn't have time to bisect.

Quoted 3 lines

Does your device work with the patch?
Does the patch work on the main line?

I don't have this device, I just fixed this syzbot bug report:
https://syzkaller.appspot.com/bug?id=c94c1c23e829d5ac97995d51219f0c5a0cd1fa54.

I think, I found the root case. In this patch I saved dma_buff to local variable and then to array, but forgot to assign it to urb->transfer_buf. This log

[   33.862175] DMAR: [DMA Write] Request device [00:14.0] PASID ffffffff fault addr 0 [fault reason 05] PTE Write access is not set

points exactly to this problem.

Can You try the following patch?

drivers/net/can/usb/mcba_usb.c +2 -0
index a45865bd7254..a1a154c08b7f 100644
@@ -653,6 +653,8 @@ static int mcba_usb_start(struct mcba_priv *priv)
 			break;
 		}
 
+		urb->transfer_dma = buf_dma;
+
 		usb_fill_bulk_urb(urb, priv->udev,
 				  usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
 				  buf, MCBA_USB_RX_BUFF_SIZE,

I've added Marc to this discussion, I believe, he can help us, since he is CAN maintainer.

I am sorry for breaking your device :(

Quoted 8 lines

I've posted some report with my hardware configuration at debian mailing list: https://bugs.debian.org/990850

Please let me know if you need any more information.

Best,
--
yashi

With regards,
Pavel Skripkin

#2 lore raw
Quoted 1 line · Pavel Skripkin

Can You try the following patch?

Can you create a proper patch with you S-o-b?

Quoted 16 lines
drivers/net/can/usb/mcba_usb.c +2 -0
index a45865bd7254..a1a154c08b7f 100644
@@ -653,6 +653,8 @@ static int mcba_usb_start(struct mcba_priv *priv)
 			break;
 		}
 
+		urb->transfer_dma = buf_dma;
+
 		usb_fill_bulk_urb(urb, priv->udev,
 				  usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
 				  buf, MCBA_USB_RX_BUFF_SIZE,
Quoted 2 lines

I've added Marc to this discussion, I believe, he can help us, since he is CAN maintainer.

Yasushi, please test and post your Tested-by here. After Pavel posts a proper patch and you tested it, I'll forward it to net/master, then it will be applied to the stable kernels. Debian can even pick up the patch earlier.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#3 lore raw
Quoted 5 lines · On Sun, 25 Jul 2021 11:42:46 +0200 Marc Kleine-Budde <[email protected]> wrote:
Quoted 1 line · Pavel Skripkin

Can You try the following patch?

Can you create a proper patch with you S-o-b?

Hi, Marc.

Sure! I thought about a bit diffrent fix path like:
diff -> Yasushi's test -> patch with Tested-by tag. I guess, your approach is more standard, so, I will send pacth as reply to your email.

Thanks!

Quoted 24 lines
Quoted 16 lines
(quoted diff) +0 -0

b/drivers/net/can/usb/mcba_usb.c index a45865bd7254..a1a154c08b7f 100644 --- a/drivers/net/can/usb/mcba_usb.c
+++ b/drivers/net/can/usb/mcba_usb.c

drivers/net/can/usb/mcba_usb.c +2 -0
@@ -653,6 +653,8 @@ static int mcba_usb_start(struct mcba_priv
 *priv) break;
 		}
 
+		urb->transfer_dma = buf_dma;
+
 		usb_fill_bulk_urb(urb, priv->udev,
 				  usb_rcvbulkpipe(priv->udev,
 MCBA_USB_EP_IN), buf, MCBA_USB_RX_BUFF_SIZE,
Quoted 2 lines

I've added Marc to this discussion, I believe, he can help us, since he is CAN maintainer.

Yasushi, please test and post your Tested-by here. After Pavel posts a proper patch and you tested it, I'll forward it to net/master, then it will be applied to the stable kernels. Debian can even pick up the patch earlier.

Quoted 3 lines

regards,
Marc

With regards,
Pavel Skripkin

#4 lore raw

PATCH 1/1 net: can: add missing urb->transfer_dma initialization

In reply to Marc Kleine-Budde

Yasushi reported, that his Microchip CAN Analyzer stopped working since commit 91c02557174b ("can: mcba_usb: fix memory leak in mcba_usb"). The problem was in missing urb->transfer_dma initialization.

In my previous patch to this driver I refactored mcba_usb_start() code to avoid leaking usb coherent buffers. To achive it, I passed local stack variable to usb_alloc_coherent() and then saved it to private array to correctly free all coherent buffers on ->close() call. But I forgot to inialize urb->transfer_dma with variable passed to usb_alloc_coherent().

All of this was causing device to not work, since dma addr 0 is not valid and following log can be found on bug report page, which points exactly to problem described above.

[   33.862175] DMAR: [DMA Write] Request device [00:14.0] PASID ffffffff fault addr 0 [fault reason 05] PTE Write access is not set

Bug report: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=990850

Fixes 91c02557174b ("can: mcba_usb: fix memory leak in mcba_usb")
---
 drivers/net/can/usb/mcba_usb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/can/usb/mcba_usb.c +2 -0
index a45865bd7254..a1a154c08b7f 100644
@@ -653,6 +653,8 @@ static int mcba_usb_start(struct mcba_priv *priv)
 			break;
 		}
 
+		urb->transfer_dma = buf_dma;
+
 		usb_fill_bulk_urb(urb, priv->udev,
 				  usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
 				  buf, MCBA_USB_RX_BUFF_SIZE,
Signature · 1 line
2.32.0
author Tested-by on v1 1/1
#5 lore raw

Re: [PATCH] net: can: add missing urb->transfer_dma initialization

Hi Pavel,

I've tested this patch on top of v5.14-rc2. All good.

Some nitpicks.

Quoted 7 lines · Pavel Skripkin

Yasushi reported, that his Microchip CAN Analyzer stopped working since commit 91c02557174b ("can: mcba_usb: fix memory leak in mcba_usb"). The problem was in missing urb->transfer_dma initialization.

In my previous patch to this driver I refactored mcba_usb_start() code to avoid leaking usb coherent buffers. To achive it, I passed local stack

achieve

Quoted 3 lines

variable to usb_alloc_coherent() and then saved it to private array to correctly free all coherent buffers on ->close() call. But I forgot to inialize urb->transfer_dma with variable passed to usb_alloc_coherent().

initialize

Quoted 29 lines

All of this was causing device to not work, since dma addr 0 is not valid and following log can be found on bug report page, which points exactly to problem described above.

[   33.862175] DMAR: [DMA Write] Request device [00:14.0] PASID ffffffff fault addr 0 [fault reason 05] PTE Write access is not set

Bug report: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=990850

Fixes 91c02557174b ("can: mcba_usb: fix memory leak in mcba_usb")
---
 drivers/net/can/usb/mcba_usb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/can/usb/mcba_usb.c +2 -1
index a45865bd7254..a1a154c08b7f 100644
@@ -653,6 +653,8 @@ static int mcba_usb_start(struct mcba_priv *priv)
                        break;
                }
 
+               urb->transfer_dma = buf_dma;
+
                usb_fill_bulk_urb(urb, priv->udev,
                                  usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
                                  buf, MCBA_USB_RX_BUFF_SIZE,
--
Quoted 1 line

2.32.0

Pavel, thanks again for your quick fix. :-)

Best,
--
yashi

#6 lore raw

Re: [PATCH] net: can: add missing urb->transfer_dma initialization

Quoted 17 lines · Yasushi SHOJI

Hi Pavel,

I've tested this patch on top of v5.14-rc2. All good.

Some nitpicks.

Quoted 7 lines · Pavel Skripkin

Yasushi reported, that his Microchip CAN Analyzer stopped working since commit 91c02557174b ("can: mcba_usb: fix memory leak in mcba_usb"). The problem was in missing urb->transfer_dma initialization.

In my previous patch to this driver I refactored mcba_usb_start() code to avoid leaking usb coherent buffers. To achive it, I passed local stack

Quoted 7 lines

achieve

Quoted 3 lines

variable to usb_alloc_coherent() and then saved it to private array to correctly free all coherent buffers on ->close() call. But I forgot to inialize urb->transfer_dma with variable passed to usb_alloc_coherent().

initialize

Fixed while applying.

Thanks,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
author Tested-by
#7 lore raw
In reply to Pavel Skripkin

Hi Pavel,

Quoted 7 lines · Pavel Skripkin

Can You try the following patch?

drivers/net/can/usb/mcba_usb.c +0 -0
index a45865bd7254..a1a154c08b7f 100644

drivers/net/can/usb/mcba_usb.c static int mcba_usb_start(struct mcba_priv *priv) · line 660

Quoted 8 lines
drivers/net/can/usb/mcba_usb.c +2 -0
@@ -653,6 +653,8 @@ static int mcba_usb_start(struct mcba_priv *priv)
                        break;
                }
 
+               urb->transfer_dma = buf_dma;
+
                usb_fill_bulk_urb(urb, priv->udev,
                                  usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
                                  buf, MCBA_USB_RX_BUFF_SIZE,

Yup, this patch fixed it. I've tested on top of v5.10.52.

Quoted 1 line

I am sorry for breaking your device :(

No problem. It'd be nice if we'd test with real hardware before merging into the stable tree, though.
Anyway, thank you for your quick fix!

Best regards,
--
yashi

#8 lore raw
Quoted 21 lines · Yasushi SHOJI

Hi Pavel,

Quoted 7 lines · Pavel Skripkin

Can You try the following patch?

drivers/net/can/usb/mcba_usb.c +0 -0
index a45865bd7254..a1a154c08b7f 100644

drivers/net/can/usb/mcba_usb.c static int mcba_usb_start(struct mcba_priv *priv) · line 660

Quoted 8 lines
drivers/net/can/usb/mcba_usb.c +2 -0
@@ -653,6 +653,8 @@ static int mcba_usb_start(struct mcba_priv *priv)
                        break;
                }
 
+               urb->transfer_dma = buf_dma;
+
                usb_fill_bulk_urb(urb, priv->udev,
                                  usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
                                  buf, MCBA_USB_RX_BUFF_SIZE,

Yup, this patch fixed it. I've tested on top of v5.10.52.

Quoted 6 lines
Quoted 1 line

I am sorry for breaking your device :(

No problem. It'd be nice if we'd test with real hardware before merging into the stable tree, though.

Yes absolutely - but I don't have access to that hardware. Since you're interested in that hardware, what about adding you as a reviewer to the kernel. Then for every change on this driver, you'll be added on Cc and can test it. What do you think?

Quoted 1 line

Anyway, thank you for your quick fix!

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#9 lore raw

Hi,

Quoted 4 lines · Marc Kleine-Budde

Yes absolutely - but I don't have access to that hardware. Since you're interested in that hardware, what about adding you as a reviewer to the kernel. Then for every change on this driver, you'll be added on Cc and can test it. What do you think?

Sure, I'm happy to help.

Signature · 1 line
               yashi
#10 lore raw
Quoted 7 lines · Yasushi SHOJI
Quoted 4 lines · Marc Kleine-Budde

Yes absolutely - but I don't have access to that hardware. Since you're interested in that hardware, what about adding you as a reviewer to the kernel. Then for every change on this driver, you'll be added on Cc and can test it. What do you think?

Sure, I'm happy to help.

Great! I've send an update to the MAINTAINERs file, Can you reply with your "Acked-by:"?

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#11 lore raw
Quoted 11 lines · Marc Kleine-Budde
Quoted 7 lines · Yasushi SHOJI
Quoted 4 lines · Marc Kleine-Budde

Yes absolutely - but I don't have access to that hardware. Since you're interested in that hardware, what about adding you as a reviewer to the kernel. Then for every change on this driver, you'll be added on Cc and can test it. What do you think?

Sure, I'm happy to help.

Great! I've send an update to the MAINTAINERs file, Can you reply with your "Acked-by:"?

BTW: is this the device we're talking about?

https://www.microchip.com/Developmenttools/ProductDetails/APGDT002

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#14 lore raw
Quoted 7 lines · Yasushi SHOJI

Hi Marc,

Quoted 2 lines · Marc Kleine-Budde

Great! I've send an update to the MAINTAINERs file, Can you reply with your "Acked-by:"?

Sorry for not being clear - I mean reply to the patch I've send.

However, I've updated your email address, add your Acked-by and sent a v2.

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
reviewer
#15 lore raw
Quoted 4 lines · Marc Kleine-Budde

Sorry for not being clear - I mean reply to the patch I've send.

However, I've updated your email address, add your Acked-by and sent a v2.

Thanks!

Signature · 1 line
             yashi