Commit 0e865f0c authored by Pavel Skripkin's avatar Pavel Skripkin Committed by Marc Kleine-Budde

can: usb_8dev: fix memory leak

In usb_8dev_start() 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 usb_8dev_start) 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: 0024d8ad ("can: usb_8dev: Add support for USB2CAN interface from 8 devices")
Link: https://lore.kernel.org/r/d39b458cd425a1cf7f512f340224e6e9563b07bd.1627404470.git.paskripkin@gmail.com
Cc: linux-stable <stable@vger.kernel.org>
Signed-off-by: default avatarPavel Skripkin <paskripkin@gmail.com>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent fc43fb69
...@@ -137,7 +137,8 @@ struct usb_8dev_priv { ...@@ -137,7 +137,8 @@ struct usb_8dev_priv {
u8 *cmd_msg_buffer; u8 *cmd_msg_buffer;
struct mutex usb_8dev_cmd_lock; struct mutex usb_8dev_cmd_lock;
void *rxbuf[MAX_RX_URBS];
dma_addr_t rxbuf_dma[MAX_RX_URBS];
}; };
/* tx frame */ /* tx frame */
...@@ -733,6 +734,7 @@ static int usb_8dev_start(struct usb_8dev_priv *priv) ...@@ -733,6 +734,7 @@ static int usb_8dev_start(struct usb_8dev_priv *priv)
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; u8 *buf;
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);
...@@ -742,7 +744,7 @@ static int usb_8dev_start(struct usb_8dev_priv *priv) ...@@ -742,7 +744,7 @@ static int usb_8dev_start(struct usb_8dev_priv *priv)
} }
buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL, buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL,
&urb->transfer_dma); &buf_dma);
if (!buf) { if (!buf) {
netdev_err(netdev, "No memory left for USB buffer\n"); netdev_err(netdev, "No memory left for USB buffer\n");
usb_free_urb(urb); usb_free_urb(urb);
...@@ -750,6 +752,8 @@ static int usb_8dev_start(struct usb_8dev_priv *priv) ...@@ -750,6 +752,8 @@ static int usb_8dev_start(struct usb_8dev_priv *priv)
break; break;
} }
urb->transfer_dma = buf_dma;
usb_fill_bulk_urb(urb, priv->udev, usb_fill_bulk_urb(urb, priv->udev,
usb_rcvbulkpipe(priv->udev, usb_rcvbulkpipe(priv->udev,
USB_8DEV_ENDP_DATA_RX), USB_8DEV_ENDP_DATA_RX),
...@@ -767,6 +771,9 @@ static int usb_8dev_start(struct usb_8dev_priv *priv) ...@@ -767,6 +771,9 @@ static int usb_8dev_start(struct usb_8dev_priv *priv)
break; break;
} }
priv->rxbuf[i] = buf;
priv->rxbuf_dma[i] = buf_dma;
/* 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);
} }
...@@ -836,6 +843,10 @@ static void unlink_all_urbs(struct usb_8dev_priv *priv) ...@@ -836,6 +843,10 @@ static void unlink_all_urbs(struct usb_8dev_priv *priv)
usb_kill_anchored_urbs(&priv->rx_submitted); usb_kill_anchored_urbs(&priv->rx_submitted);
for (i = 0; i < MAX_RX_URBS; ++i)
usb_free_coherent(priv->udev, RX_BUFFER_SIZE,
priv->rxbuf[i], priv->rxbuf_dma[i]);
usb_kill_anchored_urbs(&priv->tx_submitted); usb_kill_anchored_urbs(&priv->tx_submitted);
atomic_set(&priv->active_tx_urbs, 0); atomic_set(&priv->active_tx_urbs, 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment