Skip to content

Commit 30e19d0

Browse files
pskrgaggregkh
authored andcommitted
can: esd_usb2: fix memory leak
commit 928150f 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: 96d8e90 ("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: Pavel Skripkin <paskripkin@gmail.com> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 88b4025 commit 30e19d0

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎drivers/net/can/usb/esd_usb2.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ struct esd_usb2 {
195195
int net_count;
196196
u32 version;
197197
int rxinitdone;
198+
void *rxbuf[MAX_RX_URBS];
199+
dma_addr_t rxbuf_dma[MAX_RX_URBS];
198200
};
199201

200202
struct esd_usb2_net_priv {
@@ -544,6 +546,7 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
544546
for (i = 0; i < MAX_RX_URBS; i++) {
545547
struct urb *urb = NULL;
546548
u8 *buf = NULL;
549+
dma_addr_t buf_dma;
547550

548551
/* create a URB, and a buffer for it */
549552
urb = usb_alloc_urb(0, GFP_KERNEL);
@@ -553,14 +556,16 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
553556
}
554557

555558
buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
556-
&urb->transfer_dma);
559+
&buf_dma);
557560
if (!buf) {
558561
dev_warn(dev->udev->dev.parent,
559562
"No memory left for USB buffer\n");
560563
err = -ENOMEM;
561564
goto freeurb;
562565
}
563566

567+
urb->transfer_dma = buf_dma;
568+
564569
usb_fill_bulk_urb(urb, dev->udev,
565570
usb_rcvbulkpipe(dev->udev, 1),
566571
buf, RX_BUFFER_SIZE,
@@ -573,8 +578,12 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
573578
usb_unanchor_urb(urb);
574579
usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
575580
urb->transfer_dma);
581+
goto freeurb;
576582
}
577583

584+
dev->rxbuf[i] = buf;
585+
dev->rxbuf_dma[i] = buf_dma;
586+
578587
freeurb:
579588
/* Drop reference, USB core will take care of freeing it */
580589
usb_free_urb(urb);
@@ -662,6 +671,11 @@ static void unlink_all_urbs(struct esd_usb2 *dev)
662671
int i, j;
663672

664673
usb_kill_anchored_urbs(&dev->rx_submitted);
674+
675+
for (i = 0; i < MAX_RX_URBS; ++i)
676+
usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
677+
dev->rxbuf[i], dev->rxbuf_dma[i]);
678+
665679
for (i = 0; i < dev->net_count; i++) {
666680
priv = dev->nets[i];
667681
if (priv) {

0 commit comments

Comments
 (0)