提交 bc508ec8 编写于 作者: P Pavel Skripkin 提交者: Yang Yingliang

can: esd_usb2: fix memory leak

stable inclusion
from linux-4.19.201
commit 9c9e45112c2331d1a873fba92678b497977fb1e5

--------------------------------

commit 928150fa upstream.

In esd_usb2_setup_rx_urbs() MAX_RX_URBS coherent buffers are allocated
and there is nothing, that frees them:

1) In callback function the urb is resubmitted and that's all
2) In disconnect function urbs are simply killed, but URB_FREE_BUFFER
   is not set (see esd_usb2_setup_rx_urbs) and this flag cannot be used
   with coherent buffers.

So, all allocated buffers should be freed with usb_free_coherent()
explicitly.

Side note: This code looks like a copy-paste of other can drivers. The
same patch was applied to mcba_usb driver and it works nice with real
hardware. There is no change in functionality, only clean-up code for
coherent buffers.

Fixes: 96d8e903 ("can: Add driver for esd CAN-USB/2 device")
Link: https://lore.kernel.org/r/b31b096926dcb35998ad0271aac4b51770ca7cc8.1627404470.git.paskripkin@gmail.com
Cc: linux-stable <stable@vger.kernel.org>
Signed-off-by: NPavel Skripkin <paskripkin@gmail.com>
Signed-off-by: NMarc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
上级 2d25a9c6
...@@ -207,6 +207,8 @@ struct esd_usb2 { ...@@ -207,6 +207,8 @@ struct esd_usb2 {
int net_count; int net_count;
u32 version; u32 version;
int rxinitdone; int rxinitdone;
void *rxbuf[MAX_RX_URBS];
dma_addr_t rxbuf_dma[MAX_RX_URBS];
}; };
struct esd_usb2_net_priv { struct esd_usb2_net_priv {
...@@ -556,6 +558,7 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) ...@@ -556,6 +558,7 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
for (i = 0; i < MAX_RX_URBS; i++) { for (i = 0; i < MAX_RX_URBS; i++) {
struct urb *urb = NULL; struct urb *urb = NULL;
u8 *buf = NULL; u8 *buf = NULL;
dma_addr_t buf_dma;
/* create a URB, and a buffer for it */ /* create a URB, and a buffer for it */
urb = usb_alloc_urb(0, GFP_KERNEL); urb = usb_alloc_urb(0, GFP_KERNEL);
...@@ -565,7 +568,7 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) ...@@ -565,7 +568,7 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
} }
buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL, buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
&urb->transfer_dma); &buf_dma);
if (!buf) { if (!buf) {
dev_warn(dev->udev->dev.parent, dev_warn(dev->udev->dev.parent,
"No memory left for USB buffer\n"); "No memory left for USB buffer\n");
...@@ -573,6 +576,8 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) ...@@ -573,6 +576,8 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
goto freeurb; goto freeurb;
} }
urb->transfer_dma = buf_dma;
usb_fill_bulk_urb(urb, dev->udev, usb_fill_bulk_urb(urb, dev->udev,
usb_rcvbulkpipe(dev->udev, 1), usb_rcvbulkpipe(dev->udev, 1),
buf, RX_BUFFER_SIZE, buf, RX_BUFFER_SIZE,
...@@ -585,8 +590,12 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) ...@@ -585,8 +590,12 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
usb_unanchor_urb(urb); usb_unanchor_urb(urb);
usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
urb->transfer_dma); urb->transfer_dma);
goto freeurb;
} }
dev->rxbuf[i] = buf;
dev->rxbuf_dma[i] = buf_dma;
freeurb: freeurb:
/* Drop reference, USB core will take care of freeing it */ /* Drop reference, USB core will take care of freeing it */
usb_free_urb(urb); usb_free_urb(urb);
...@@ -674,6 +683,11 @@ static void unlink_all_urbs(struct esd_usb2 *dev) ...@@ -674,6 +683,11 @@ static void unlink_all_urbs(struct esd_usb2 *dev)
int i, j; int i, j;
usb_kill_anchored_urbs(&dev->rx_submitted); usb_kill_anchored_urbs(&dev->rx_submitted);
for (i = 0; i < MAX_RX_URBS; ++i)
usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
dev->rxbuf[i], dev->rxbuf_dma[i]);
for (i = 0; i < dev->net_count; i++) { for (i = 0; i < dev->net_count; i++) {
priv = dev->nets[i]; priv = dev->nets[i];
if (priv) { if (priv) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册