From patchwork Mon Jul 26 15:30:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12399941 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 CAF02C4320A for ; Mon, 26 Jul 2021 15:30:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B6C64603E9 for ; Mon, 26 Jul 2021 15:30:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235063AbhGZOt6 (ORCPT ); Mon, 26 Jul 2021 10:49:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234546AbhGZOt5 (ORCPT ); Mon, 26 Jul 2021 10:49:57 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB679C061757; Mon, 26 Jul 2021 08:30:25 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id bp1so16230573lfb.3; Mon, 26 Jul 2021 08:30:25 -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=M2ahqcsAPFIMPPM8R3AQtSCIcD8xQ691fWWGwSBZe0clTrURhNfpXwhLzpDfgiHwiT AlI/aw7DmFApa7WrksFcwRgsjO7LYLPMyBBTyXfDYGZYj/LBhHbeiCTkG8sUtezRf6fs /1l/z5a9DNfCOJ8s8/rufOn+Fcs1A8/F0L3paIgLQPbAKhdHItLKbwbR/fv7xz7LgKWr ewvBtV65q+5O53rHaD5Gaww6+wGfCBsVSF2AvMnsiT0lxQUlNQT94AEU8mViX+yrfPvI NSnN/zhv6B52sLRRxPOnca3gja7QApIqlGtcinZgrS8zwKaV14eESRy36lyvQ3LwFujo hafA== 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=K1twJY19XpDz3kR/PAXv0pJ9HOqUDm4CnKgcFxoD8AGTprH4E1+S0MIzfAU2eUtlUK iSWLah9Tl++9VsSkAtwJ/8Dq6Kb7VYP0aFRI3xvwHOfNhj5ZvSKOdi7NdhoCqSqVu04t 8h8w51XhL+SFLhI58KmJQ1H57O8Lj1xGr7RrlcyQswJnMWH2YBvzGlbC48TWLxxYuMK0 v0l6gY4zTEqDqYGdTDvmwtI6WiGLNOU0G7IKUOcLGFhVSmxN6SqJ+2BsF2idosIeWKmz crDF3NuLTobI1awB5Y4OeNEjepu6OCNa61gBdjlKtFpfXfJVHhJ4PGcLo5/9DYXgAXb0 Z2ZQ== X-Gm-Message-State: AOAM5337q7xcCEyR7vSnJ8B3/g8iVLYAARiduTUDwaSupUuRcIxtseLW MVku0J7Rb2q7lP+9yJMQEMM= X-Google-Smtp-Source: ABdhPJwUEKtnIPwRGnHBP1N66CPbwnE0w6NqQ4CT37se67Ch93WGWuqy3wQmIMbFdCQQhx/8Ir+Q4g== X-Received: by 2002:a05:6512:230b:: with SMTP id o11mr13961981lfu.292.1627313424007; Mon, 26 Jul 2021 08:30:24 -0700 (PDT) Received: from localhost.localdomain ([46.61.204.59]) by smtp.gmail.com with ESMTPSA id f10sm31408lfk.84.2021.07.26.08.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 08:30:23 -0700 (PDT) From: Pavel Skripkin To: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net, socketcan@hartkopp.net, mailhol.vincent@wanadoo.fr, b.krumboeck@gmail.com Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin Subject: [PATCH 1/3] can: usb_8dev: fix memory leak Date: Mon, 26 Jul 2021 18:30:18 +0300 Message-Id: <57ea53a8ba4687fd75045edb89489ca2a8ba4d60.1627311383.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 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 Mon Jul 26 15:30:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12399943 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 371B7C432BE for ; Mon, 26 Jul 2021 15:30:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1B16E60F5C for ; Mon, 26 Jul 2021 15:30:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234546AbhGZOuQ (ORCPT ); Mon, 26 Jul 2021 10:50:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234766AbhGZOuP (ORCPT ); Mon, 26 Jul 2021 10:50:15 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 218A3C061760; Mon, 26 Jul 2021 08:30:44 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id b21so11724143ljo.13; Mon, 26 Jul 2021 08:30:44 -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=pVAsW7BQ8LuEZ7HmlokQ76GqUDg6cqWPHk+XOFxeXzzGnYjSVfUa8y+NWOYlwl/3k/ hbAcskWdmoSROwW9mttYnQRsTJTKKELKu3HVziS90pLqHY0KbIT9KHppsT9jnnbhc7nn jBp/s1epf8m65fdYluxB0wIwoxGIb9MDU6VEJW3qmoC35qpHY6PT2Jc8+pjn9hzdlaPH FhFhzZCSm6sylB70p9JmLY3frk2QOnFyBStBf3LiMEQd5l0pQJys1DhTEOzNE24pAxqb /ZKvWjhEXCi7rqo0HPkJMtm27KCbm9RG5eK2vUfVMxxij5pI52z0+9QQKXh3ZZAcNWUB 8Gcw== 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=KHKf+e2dAq9BB64WtG/afFkjo0UGDAP3pBDyALeSZ/eQnJj6gMCvz7/ORmsY0OQBJZ +ZAbz/U/7Ct62iOnZ1vjx2uJg8iNqx6wlcsTp2o/kkS52w1zJddctr4C5ZeqTStM7qJ0 txMm+eVNqKOpimVI363kfTPPjdLcm+JM/gdYioQ6T64CUSdwB8BClm/7bgzgCywkzMtg r65IIUA5eSE8Slcu6dlYN2iVs9LhxIIUzhi1r5zUlOgjksdkhiIQcVnWuUXoCQIdqRPv UU7pg/uPgQGKzwlN/FBCfwjXyFrBx317qQBgd2KlxVCZb04yMxvfcxF02q2UTliP5RLs rbmA== X-Gm-Message-State: AOAM530gMx5k7dSZ15QRZl9wAKoN1DfOPrk5VRE8IPHd9hCRvfTZ3aYr pj7qygfVv7fVv4w15Lnod0s= X-Google-Smtp-Source: ABdhPJzg1QcDyIOIqACGVUmMfrd0Cl79fl5oQCQMP+Hv8VzC842YeH+1wvPyy3KBIES5TcFlsuqV5A== X-Received: by 2002:a05:651c:246:: with SMTP id x6mr12245086ljn.452.1627313441061; Mon, 26 Jul 2021 08:30:41 -0700 (PDT) Received: from localhost.localdomain ([46.61.204.59]) by smtp.gmail.com with ESMTPSA id u13sm30799lfq.142.2021.07.26.08.30.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 08:30:40 -0700 (PDT) From: Pavel Skripkin 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 Subject: [PATCH 2/3] can: ems_usb: fix memory leak Date: Mon, 26 Jul 2021 18:30:36 +0300 Message-Id: <4e89570da4ce94bfbc18911a8bfa65b23f873e6e.1627311383.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 Mon Jul 26 15:31:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Skripkin X-Patchwork-Id: 12399945 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 0F36DC432BE for ; Mon, 26 Jul 2021 15:31:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EC2AC6023B for ; Mon, 26 Jul 2021 15:31:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234851AbhGZOui (ORCPT ); Mon, 26 Jul 2021 10:50:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234766AbhGZOuh (ORCPT ); Mon, 26 Jul 2021 10:50:37 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF15BC061764; Mon, 26 Jul 2021 08:31:05 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id l4so11769082ljq.4; Mon, 26 Jul 2021 08:31:05 -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=O/A0BOjIm3z4R6uvHm/L/KPzVr+aE6zy1njqB67lKvI=; b=matyVmWB9x6rQG2ibSGAIqTE9ZYLQxC9cofwJ/WSe5eMlOYJwApNSHUmN7ob/QmYzZ Zf4L+DLxYLSi9IP1/fTFN4Fvq9Dj3oA+q0TtLk/nLV0AvbVnB+mAGIy+OHxU54Slrx+5 4ZYOazlPl8hjBfZssrtpKqBcCapV1xll7BQc1wop35z2WespNpYdz9TluRWJJjWzotWa NGIj6PaQmywdRyZjC+FlH/VG49fqnXPxXBbRCqH01kHT06NBAA/wjodIybcv8agXZ1ZM O0y9Nk6YQFO7ZlALrVxDcq5jw/mpNKc0h+VJCrVEMf6uPp4Piex61RFZDLuNp9x/EZGI 6T7g== 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=O/A0BOjIm3z4R6uvHm/L/KPzVr+aE6zy1njqB67lKvI=; b=RzY4rOqShocaHMvYtEVKBgL7k+B6EgODFGYHQetzfBnT3FxXXn6fNNDCSl3cP1TCGn S+n6ygPrt9LP2myW6ehVBFlmiWVWdCMBgsBPQ4yLscuC4KPfYDrQsw4lgSYlS69xohRP 8i9tJbZpIOdmgaNi+Vii4AhLx98MqS0KAc4o3jKGPm3kxCPwkVKHjS5/joJd9iDc6Zcd mv/EaIzta3AHXuGOxhRCyI8Bvc0jFfqdM4MbOIUFliAYAMwt/GTkBpgWf51hhZeIiL9R GrdeD8S3Ew0oO3o2FAbMOH+gK+67KXTdYupLjzGvl454DYxkytfG7JIoXHwQZ1ngYKhC PXHg== X-Gm-Message-State: AOAM5326vR7PTbyHVnbswpTvF7I65qJEbICIcZCm80pqsRB6WMqlmBf6 Orn5ZizhakEoNhtcGYBY93c= X-Google-Smtp-Source: ABdhPJwda2EQ55vKJ2lD86vBnBC1lEUgq1+UiH+MLlPORmQWoyKNNldrmwI2osCgdCO8uvMd8rCIGQ== X-Received: by 2002:a2e:a90b:: with SMTP id j11mr12663613ljq.338.1627313464111; Mon, 26 Jul 2021 08:31:04 -0700 (PDT) Received: from localhost.localdomain ([46.61.204.59]) by smtp.gmail.com with ESMTPSA id m11sm2623lji.8.2021.07.26.08.31.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 08:31:03 -0700 (PDT) From: Pavel Skripkin To: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net, socketcan@hartkopp.net, mailhol.vincent@wanadoo.fr, Stefan.Maetje@esd.eu, matthias.fuchs@esd.eu Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin Subject: [PATCH 3/3] can: esd_usb2: fix memory leak Date: Mon, 26 Jul 2021 18:31:01 +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 Reported-by: kernel test robot Reported-by: kernel test robot --- 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..303560abe2b0 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 freeusrb; } + 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) {