Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1645800pxy; Mon, 2 Aug 2021 07:00:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTU47WrIag/m5kPvhLfuWpAla2eOG7Gkyi+NHXJfbcUsdGmDObOT3EuxILZvH3BGE94wKF X-Received: by 2002:a05:6638:14d6:: with SMTP id l22mr14922843jak.99.1627912859417; Mon, 02 Aug 2021 07:00:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912859; cv=none; d=google.com; s=arc-20160816; b=OUrcdboM8z4/mYnRGu7238lg1pcEHinnRVjw2AmdESXQNVuj/TA5WFiUSmwICALlmt 0CYI4a/d2S3eRGKayWm4yEqdnJlsui4NmcSSgdfhikN7qtJYN66prQkRDnNFWjjeEszp S/2Z4qrUEzxnKhlQOzdYltIrwFknJu3KMmy1g6m40AAN5HdLheSuF7MHZrQ0tA88MN1M 3SVO5IFChghdQ4GB0XHfsyGVd2p7NqDL4Wma4oDc+bmSgGywEk8btXx52gEw0i72B6eF TrV9Dy35+Lfnb/Bek5HUiWhAiLMMn4EmiA5QlaDnPaudgi5ZE66wTPVZEFdFwG5GE8ei haxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2QLpdgy2bsS5CceztXKDxCzc/ghG71LOH1zi2yA4hdE=; b=DP3yST9uldBmFVvGAxns9mH0fslDFm4pBSBJhvSnUNzKfRCtDslOmSoGeN/j3Kw0CQ e/fdsa+vIufhuj9Wdb+izpvzB/6fwoz5sdNWPyjSR1KQic+6UCTOs0+OqQUp7k0cFM63 8sfOhIcToBkmqbg4C5x4U0XmulzQ40WtQ2LL851/OuK+BSTGa+DwCF+7dJHFIvpiyZGu FnzojdvbKrkikwBvOWEQTszMOfJaT/Cm4uZdYQuCRmJ22cqO7n5nsHqvtx1/2/yGQaG4 UwdEiQIsPrZMQZZq3v+7lv2nFRDFcHwSbTSCU2zAxhyjBQOF9OEMVp881cOP3OgQuN5d Ourg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tV4Lp5rP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g6si11649228ilf.57.2021.08.02.07.00.46; Mon, 02 Aug 2021 07:00:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tV4Lp5rP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236230AbhHBN7P (ORCPT + 99 others); Mon, 2 Aug 2021 09:59:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:33536 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234675AbhHBNxB (ORCPT ); Mon, 2 Aug 2021 09:53:01 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5DD526113C; Mon, 2 Aug 2021 13:52:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912321; bh=M1dnuu1326fDTRjfoPgMtMUakHQlTPMsA7VBwe345h4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tV4Lp5rP+zWTw6GW5Z3UTfYwZl4W/kTuFqJ2sqTClZ0QZO+GSDjrV0wyGvhaCh4js DOq4OvzWSOsuln1v5aqRMrgj4Nqo/LaCAyFXNQOmWrJG1hfOQPJdikPm76b9ll6MEA s9XkGVTsDzx4gn7Nyb4o5/q8kPVmsQ57V7fqyX50= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Skripkin , Marc Kleine-Budde Subject: [PATCH 5.4 15/40] can: esd_usb2: fix memory leak Date: Mon, 2 Aug 2021 15:44:55 +0200 Message-Id: <20210802134335.881595766@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134335.408294521@linuxfoundation.org> References: <20210802134335.408294521@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Skripkin commit 928150fad41ba16df7fcc9f7f945747d0f56cbb6 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: 96d8e90382dc ("can: Add driver for esd CAN-USB/2 device") Link: https://lore.kernel.org/r/b31b096926dcb35998ad0271aac4b51770ca7cc8.1627404470.git.paskripkin@gmail.com Cc: linux-stable Signed-off-by: Pavel Skripkin Signed-off-by: Marc Kleine-Budde Signed-off-by: Greg Kroah-Hartman --- drivers/net/can/usb/esd_usb2.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) --- 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 { @@ -544,6 +546,7 @@ static int esd_usb2_setup_rx_urbs(struct 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); @@ -553,7 +556,7 @@ static int esd_usb2_setup_rx_urbs(struct } 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"); @@ -561,6 +564,8 @@ static int esd_usb2_setup_rx_urbs(struct goto freeurb; } + urb->transfer_dma = buf_dma; + usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), buf, RX_BUFFER_SIZE, @@ -573,8 +578,12 @@ static int esd_usb2_setup_rx_urbs(struct 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); @@ -662,6 +671,11 @@ static void unlink_all_urbs(struct esd_u 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) {