From: Pavel Skripkin <paskripkin@gmail.com> To: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net, socketcan@hartkopp.net, mailhol.vincent@wanadoo.fr, haas@ems-wuensche.com Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin <paskripkin@gmail.com> Subject: [PATCH 2/3] can: ems_usb: fix memory leak Date: Mon, 26 Jul 2021 18:30:36 +0300 [thread overview] Message-ID: <4e89570da4ce94bfbc18911a8bfa65b23f873e6e.1627311383.git.paskripkin@gmail.com> (raw) In-Reply-To: <cover.1627311383.git.paskripkin@gmail.com> In ems_usb_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 ems_usb_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: 702171adeed3 ("ems_usb: Added support for EMS CPC-USB/ARM7 CAN/USB interface") Signed-off-by: Pavel Skripkin <paskripkin@gmail.com> --- drivers/net/can/usb/ems_usb.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c index 0a37af4a3fa4..2b5302e72435 100644 --- a/drivers/net/can/usb/ems_usb.c +++ b/drivers/net/can/usb/ems_usb.c @@ -255,6 +255,8 @@ struct ems_usb { unsigned int free_slots; /* remember number of available slots */ struct ems_cpc_msg active_params; /* active controller parameters */ + void *rxbuf[MAX_RX_URBS]; + dma_addr_t rxbuf_dma[MAX_RX_URBS]; }; static void ems_usb_read_interrupt_callback(struct urb *urb) @@ -587,6 +589,7 @@ static int ems_usb_start(struct ems_usb *dev) for (i = 0; i < MAX_RX_URBS; i++) { struct urb *urb = NULL; u8 *buf = NULL; + dma_addr_t buf_dma; /* create a URB, and a buffer for it */ urb = usb_alloc_urb(0, GFP_KERNEL); @@ -596,7 +599,7 @@ static int ems_usb_start(struct ems_usb *dev) } buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL, - &urb->transfer_dma); + &buf_dma); if (!buf) { netdev_err(netdev, "No memory left for USB buffer\n"); usb_free_urb(urb); @@ -604,6 +607,8 @@ static int ems_usb_start(struct ems_usb *dev) break; } + urb->transfer_dma = buf_dma; + usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2), buf, RX_BUFFER_SIZE, ems_usb_read_bulk_callback, dev); @@ -619,6 +624,9 @@ static int ems_usb_start(struct ems_usb *dev) break; } + dev->rxbuf[i] = buf; + dev->rxbuf_dma[i] = buf_dma; + /* Drop reference, USB core will take care of freeing it */ usb_free_urb(urb); } @@ -684,6 +692,10 @@ static void unlink_all_urbs(struct ems_usb *dev) 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]); + usb_kill_anchored_urbs(&dev->tx_submitted); atomic_set(&dev->active_tx_urbs, 0); -- 2.32.0
next prev parent reply other threads:[~2021-07-26 15:30 UTC|newest] Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-07-26 15:29 [PATCH 0/3] can: fix same memory leaks in can drivers Pavel Skripkin 2021-07-26 15:30 ` [PATCH 1/3] can: usb_8dev: fix memory leak Pavel Skripkin 2021-07-26 15:30 ` Pavel Skripkin [this message] 2021-07-26 15:31 ` [PATCH 3/3] can: esd_usb2: " Pavel Skripkin 2021-07-26 19:51 ` kernel test robot 2021-07-28 20:38 ` kernel test robot 2021-07-26 17:29 ` [PATCH 0/3] can: fix same memory leaks in can drivers Pavel Skripkin 2021-07-26 20:02 ` Marc Kleine-Budde 2021-07-27 16:59 ` [PATCH v2 " Pavel Skripkin 2021-07-27 16:59 ` [PATCH v2 1/3] can: usb_8dev: fix memory leak Pavel Skripkin 2021-07-27 17:00 ` [PATCH v2 2/3] can: ems_usb: " Pavel Skripkin 2021-07-27 17:00 ` [PATCH v2 3/3] can: esd_usb2: " Pavel Skripkin 2021-07-28 7:56 ` [PATCH v2 0/3] can: fix same memory leaks in can drivers Marc Kleine-Budde
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=4e89570da4ce94bfbc18911a8bfa65b23f873e6e.1627311383.git.paskripkin@gmail.com \ --to=paskripkin@gmail.com \ --cc=davem@davemloft.net \ --cc=haas@ems-wuensche.com \ --cc=linux-can@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=mailhol.vincent@wanadoo.fr \ --cc=mkl@pengutronix.de \ --cc=netdev@vger.kernel.org \ --cc=socketcan@hartkopp.net \ --cc=wg@grandegger.com \ --subject='Re: [PATCH 2/3] can: ems_usb: fix memory leak' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).