From patchwork Tue Jul 27 16:59:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12403203 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B5C4C4320A for ; Tue, 27 Jul 2021 17:00:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3B50360E09 for ; Tue, 27 Jul 2021 17:00:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230271AbhG0RAR (ORCPT ); Tue, 27 Jul 2021 13:00:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbhG0RAJ (ORCPT ); Tue, 27 Jul 2021 13:00:09 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2AA9C061757; Tue, 27 Jul 2021 10:00:07 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id f18so22833233lfu.10; Tue, 27 Jul 2021 10:00:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=m6QF1tp2oEexOjNTXdUF+9gO2IJD1K9qh4FZF8+8zOk=; b=LnAFTJSZxgZ6CkbIhBYWJcq7DZWBPIYCiJ3JjUgJufHLT0UMZfK9UhEw8f6gHra2gh o2KWX8Nd65nRW4jEA2QxjLLp4CqlPMGffXvTs4HBPeBYUNUbvuDw7yWSEzxreAAvrOqY 78di+SvO6Nnwb6pKTcpvpuMB3KejaZLBXPOY2scMLiebPrYmkuhAMuxEwqWpdelytI1R xhDqqzbY2qN2boHkx2aPN/1tqrs8dvmxdwQPo8v5YHkcXk95H4InYBNGFCyU/NVLZSp5 /dmuG0K77ds3gzqIy/3bZLfW/1h07nP9lp+dB9o2ijSWNGmM9r1ZcA5o8Y7vnby/nrZV tVsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=m6QF1tp2oEexOjNTXdUF+9gO2IJD1K9qh4FZF8+8zOk=; b=bjWJQhfFm/31PWsij1vTivOWdKesYGib501yNgvibOcAZaS+/Rb9HLU+bZZAzqRgzw VGcEonPOZqHwK86EII1xkmU4u8LvUDYTN7GlwO5Ody294p261aYBKROM7hFNXNMWXHLC hn9mviPZPjILN3zYtVz0/Up/6acSxbwYzhMDBw7BrFI0HwamOaKbt5EoPl9ECGjWqF5I vU5tn6dzFN89XTSgkT/okpHfYjmKCfscBnwstrIjt8bO3AlRFA9gyMty+3uRRTes9y/H No9lmpJTUswy+YzI4Wn5kjP3h23q/HGiPA4RXByqhD1Y4gmADYUs37r7s2xGZTDvizr2 DfOg== X-Gm-Message-State: AOAM532pqakgKnLX8xt3vDGvxjfGwjHdgpXN7AbNbT07rkNLeDj0K5Wi 5TjFGNJygH+ExnCMiLi9x04= X-Google-Smtp-Source: ABdhPJwMSlhTafv0V0AAiRft7/oOQ7TQGi7do9NszuwPhKzU1VHmd8MciOx/9v+pyPW5z4MWIX5gLQ== X-Received: by 2002:a05:6512:714:: with SMTP id b20mr16909046lfs.488.1627405206120; Tue, 27 Jul 2021 10:00:06 -0700 (PDT) Received: from localhost.localdomain ([94.103.227.213]) by smtp.gmail.com with ESMTPSA id d17sm341950lfl.174.2021.07.27.10.00.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jul 2021 10:00:05 -0700 (PDT) From: Pavel Skripkin To: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net, kuba@kernel.org, mailhol.vincent@wanadoo.fr, socketcan@hartkopp.net, b.krumboeck@gmail.com Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin Subject: [PATCH v2 1/3] can: usb_8dev: fix memory leak Date: Tue, 27 Jul 2021 19:59:57 +0300 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org 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: 0024d8ad1639 ("can: usb_8dev: Add support for USB2CAN interface from 8 devices") Signed-off-by: Pavel Skripkin --- drivers/net/can/usb/usb_8dev.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/net/can/usb/usb_8dev.c b/drivers/net/can/usb/usb_8dev.c index b6e7ef0d5bc6..d1b83bd1b3cb 100644 --- a/drivers/net/can/usb/usb_8dev.c +++ b/drivers/net/can/usb/usb_8dev.c @@ -137,7 +137,8 @@ struct usb_8dev_priv { u8 *cmd_msg_buffer; struct mutex usb_8dev_cmd_lock; - + void *rxbuf[MAX_RX_URBS]; + dma_addr_t rxbuf_dma[MAX_RX_URBS]; }; /* tx frame */ @@ -733,6 +734,7 @@ static int usb_8dev_start(struct usb_8dev_priv *priv) for (i = 0; i < MAX_RX_URBS; i++) { struct urb *urb = NULL; u8 *buf; + dma_addr_t buf_dma; /* create a URB, and a buffer for it */ urb = usb_alloc_urb(0, GFP_KERNEL); @@ -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, - &urb->transfer_dma); + &buf_dma); if (!buf) { netdev_err(netdev, "No memory left for USB buffer\n"); usb_free_urb(urb); @@ -750,6 +752,8 @@ static int usb_8dev_start(struct usb_8dev_priv *priv) break; } + urb->transfer_dma = buf_dma; + usb_fill_bulk_urb(urb, priv->udev, usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX), @@ -767,6 +771,9 @@ static int usb_8dev_start(struct usb_8dev_priv *priv) break; } + priv->rxbuf[i] = buf; + priv->rxbuf_dma[i] = buf_dma; + /* Drop reference, USB core will take care of freeing it */ usb_free_urb(urb); } @@ -836,6 +843,10 @@ static void unlink_all_urbs(struct usb_8dev_priv *priv) 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); atomic_set(&priv->active_tx_urbs, 0); From patchwork Tue Jul 27 17:00:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12403205 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEE2DC4320A for ; Tue, 27 Jul 2021 17:00:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A012661029 for ; Tue, 27 Jul 2021 17:00:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231190AbhG0RAj (ORCPT ); Tue, 27 Jul 2021 13:00:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229815AbhG0RAi (ORCPT ); Tue, 27 Jul 2021 13:00:38 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A42AC061757; Tue, 27 Jul 2021 10:00:37 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id f12so16818429ljn.1; Tue, 27 Jul 2021 10:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iea5qO2fdy0HoiQckUUb63itdyCRWHqDzn/tBtz3LPI=; b=ikx+EkaxmQPKic+ZYbqVI31C19KJGwcKqcci1GiDPmra0M5dhUicEVZaaUPM07IwxY MMHY0To6iHgGpj0xRhW3f4UjTw49a8f1civ/IAw10d0v5haHkOs/9dDMG6sFkRqqivku 0XJ68+pTppyzTqfzt0VPYM87QWFKzQmYX/pYYCftuG+3gvpxVg2Auzx4MEMIXZTvcjIJ NwxPr2iE9VJ3jOBWt+x86xXoxfG9/j3uZiKlrpdvLbSGQa4UIA4CutIhGvzIRqmezf4/ g251BKhV0+g5+ISrvcDDHdYOgeGyZXVPDkWthemwPhlUfmAyMRhvFeuETSq42HPxd4eV 9UwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iea5qO2fdy0HoiQckUUb63itdyCRWHqDzn/tBtz3LPI=; b=auodyEq6ZZ1+dnVCHAhuQUPMjL8TY9GH3hh9WS4VghOl9JgM22i0VHR9jUQUDkYrLV R5ko+biYxr8D9sSgrmCtAwKG8K3latzgkHga7U7zcSsQK5xdpGt/+oAH+mB4pRBBJjS1 jDme55ZVVTrU+H3mclfZIFpWGKK/ZV4DfwMqJRDB6zy8Pvu8HI/Byv40J3M4eLXPDlTv oSj3RBs1o1cZ1F6YSUKdaS6YPmNfMkvCYHUOoYE75GAbZdUJzB+6cLvVZMSj/1wdnifZ tzheCAZMhVfV/gM+Ruo/4wlaB+B6s9k3gTxTXrOL8f6EKjlG9VXI+feBy/gYkwfPXQuJ RbUg== X-Gm-Message-State: AOAM531eeNq1xTv1xtftRIefzKZSxnKOfnXgFUOZQmkapZj0Qet8OXXl VyrUVDaDk8PiZgy3dU02A28= X-Google-Smtp-Source: ABdhPJwr0xpBUdfhotYvfAxDVuJ/+K0+BbI59nJA5HRweEhXPXvNtAICp2RMVOy9yFAqbkuWyRzGoQ== X-Received: by 2002:a05:651c:110:: with SMTP id a16mr15390468ljb.241.1627405235915; Tue, 27 Jul 2021 10:00:35 -0700 (PDT) Received: from localhost.localdomain ([94.103.227.213]) by smtp.gmail.com with ESMTPSA id t24sm323896ljj.97.2021.07.27.10.00.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jul 2021 10:00:35 -0700 (PDT) From: Pavel Skripkin To: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net, kuba@kernel.org, mailhol.vincent@wanadoo.fr, socketcan@hartkopp.net, haas@ems-wuensche.com Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin Subject: [PATCH v2 2/3] can: ems_usb: fix memory leak Date: Tue, 27 Jul 2021 20:00:33 +0300 Message-Id: <59aa9fbc9a8cbf9af2bbd2f61a659c480b415800.1627404470.git.paskripkin@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org 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 --- 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); From patchwork Tue Jul 27 17:00:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12403207 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55880C432BE for ; Tue, 27 Jul 2021 17:00:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 39113600D4 for ; Tue, 27 Jul 2021 17:00:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230435AbhG0RAy (ORCPT ); Tue, 27 Jul 2021 13:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229532AbhG0RAx (ORCPT ); Tue, 27 Jul 2021 13:00:53 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E73BC061757; Tue, 27 Jul 2021 10:00:52 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id a7so16798229ljq.11; Tue, 27 Jul 2021 10:00:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/XSno7xDkhJl7h1dC9M9HNfKcC/pY9lVoxnVJ+wRmsQ=; b=oXCOkiHc3nI+0iNZigrxDC9pKWA3g/2bnqUhd7BQr0su28RVzShszZHa4B0ZxJV0zE b2QP+paUcAvqob/SRuAN7af+Usp2AknEC176kAN6H5x8DL8rMNUaXDGj56rxI2C94yeN /g94PaeCK4U3KqcBuLDysRk1vIzktrnwQFp7D5lYjQn58hAPTGNsA59Tc6iZtinaaRlU SnYXH3iHQedc6FMteKlhKO5G+RAWnhnLuSeHfHMCfiMIOf4I1/zIYldMU3TB5CRhWLbd GqylGIoWbqKLrV092MHZPXUUv+6Igav0so73yDwAeee89ITL1u++EVySs3jdWncsVUaH C99w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/XSno7xDkhJl7h1dC9M9HNfKcC/pY9lVoxnVJ+wRmsQ=; b=Sh8uCSIRTzOfyUSB7RCZZK0Z3wHLq7Ur4tPjHkO2QIyDrc3a5PJhK0UDpRLjN+d9ZN DXIPhZdhsFyNltKGZmRMWGDmX7dy+7etygizIhutdM8JDvb54kcCEcKFE7MZ+V0FW/H+ 9bkaSurC9X5+6Pr5IaBV4/oPAZxXVUQRP1ZVuYwWGW9k0A0iYCsE7suioNVxmUuyodvy FT/ZtofleLzfIyXRp1SrLWz4X+rD8dhIHtAoNwGUVVfrysIhOxj4d454zotJKYIBs6rK UkFswUVXXt5cw6hlF5JgSKdMvCJMm+8f9zxzx1b0jJr3lkCtvuC/lURq36eY+TpqiyGa Xjzw== X-Gm-Message-State: AOAM533Nk44TAcpq2J8xWk+brb2cgfMRj8o9S8wfZn7ec+DcI9KMg3Zs 5F28SBxmiS2hoOtzFx2Kooc= X-Google-Smtp-Source: ABdhPJwbFzoqQHMsxKvNBcw/NZG+zZBq+uvXJ0D0ZvrQuxYBS7wigq7jHvP5mPHtSGj5gGZ3uG6LIA== X-Received: by 2002:a2e:8812:: with SMTP id x18mr15723789ljh.441.1627405250266; Tue, 27 Jul 2021 10:00:50 -0700 (PDT) Received: from localhost.localdomain ([94.103.227.213]) by smtp.gmail.com with ESMTPSA id q10sm339595ljp.108.2021.07.27.10.00.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jul 2021 10:00:49 -0700 (PDT) From: Pavel Skripkin To: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net, kuba@kernel.org, mailhol.vincent@wanadoo.fr, socketcan@hartkopp.net, Stefan.Maetje@esd.eu Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin Subject: [PATCH v2 3/3] can: esd_usb2: fix memory leak Date: Tue, 27 Jul 2021 20:00:46 +0300 Message-Id: X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org 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: 96d8e90382dc ("can: Add driver for esd CAN-USB/2 device") Signed-off-by: Pavel Skripkin --- drivers/net/can/usb/esd_usb2.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/usb/esd_usb2.c b/drivers/net/can/usb/esd_usb2.c index 65b58f8fc328..66fa8b07c2e6 100644 --- a/drivers/net/can/usb/esd_usb2.c +++ b/drivers/net/can/usb/esd_usb2.c @@ -195,6 +195,8 @@ struct esd_usb2 { int net_count; u32 version; int rxinitdone; + void *rxbuf[MAX_RX_URBS]; + dma_addr_t rxbuf_dma[MAX_RX_URBS]; }; struct esd_usb2_net_priv { @@ -545,6 +547,7 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *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); @@ -554,7 +557,7 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) } buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL, - &urb->transfer_dma); + &buf_dma); if (!buf) { dev_warn(dev->udev->dev.parent, "No memory left for USB buffer\n"); @@ -562,6 +565,8 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) goto freeurb; } + urb->transfer_dma = buf_dma; + usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), buf, RX_BUFFER_SIZE, @@ -574,8 +579,12 @@ static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) usb_unanchor_urb(urb); usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, urb->transfer_dma); + goto freeurb; } + dev->rxbuf[i] = buf; + dev->rxbuf_dma[i] = buf_dma; + freeurb: /* Drop reference, USB core will take care of freeing it */ usb_free_urb(urb); @@ -663,6 +672,11 @@ static void unlink_all_urbs(struct esd_usb2 *dev) int i, j; 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++) { priv = dev->nets[i]; if (priv) {