Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1638464pxy; Mon, 2 Aug 2021 06:50:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxalbQXWiT8bLZ7fJhZ8Ym+vquaWipEuwwhtPvJQiPHrqta9kUKsxRvgX+kisa5yodvGEM8 X-Received: by 2002:a6b:f416:: with SMTP id i22mr1040831iog.162.1627912218825; Mon, 02 Aug 2021 06:50:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912218; cv=none; d=google.com; s=arc-20160816; b=YAekF5sdTnTm+XFmZ1u6yoK41q2s+0WXea8bLh7gkUovMrjeuAqYNAFHhWudnhlltP mcvLJmrhUjENEqnPE1IsqU7vdrO78VtavVJIyx/gEseP9iMtZ2BmoHwh9BjGULFcHaC0 +ddg9xLYYSmHXqb4lyZOs8qUY/IUQNAcfxD/pD/TgDPV/Vrmb2N9E0LPijojFDejxpk+ SQ7DdmBrqRa5Yt+fZXLByDMBJm2z3ONF6GHQu3Wd6I6WPXKfOBEbdbwgtv6CfjNTw4Ae Ij38N/rMNTXWwoL2gjnCOcwZxnL7qMm77Ez4XpqvAHqcRS4LjrTXduZqB/SjblgHNK0o I3AQ== 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=u6nruwGrx7yM8EjpEXC/QrG4tFdLpFIcp+lSCzCqbD4=; b=dDW3UwCaBHGz2cYcvpUF7iDU36qlExQuHX/yomOPhpSoPDSp50M7F0bi0daU41Y1rz HS1VLO9p+gUWxybsu84+Rm2URQL2jdCm9zG0EScK/zzgckJxhiUq68fRCNNaCLCAJCX/ 2pQ6VaqTZJFznHIcja7gTeKOHqZSQFYs0R2zBlNdxJhZmzbV1EO7sZhhj3hqN70MaMXR weosHdkUrR2TpVm5qcY3HHF8Mn8L0n+Q70OpUV0nRfuAvIVy63N+/d1fLt2Sym82vRvL M2SzeZOX2mZwqd45eMv4HnWltuN6UuLhKxonYzHkYsE9QA6XRfBo0FY1gpYIxMEeTvH0 +EHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="DQyEB/2q"; 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 i16si13504761jam.109.2021.08.02.06.50.07; Mon, 02 Aug 2021 06:50:18 -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="DQyEB/2q"; 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 S234106AbhHBNtb (ORCPT + 99 others); Mon, 2 Aug 2021 09:49:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:57254 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234129AbhHBNrW (ORCPT ); Mon, 2 Aug 2021 09:47:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4005360527; Mon, 2 Aug 2021 13:47:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912032; bh=PTKHef7/1KwNBx3M/xRqz3zbDFV1TQC+jLV4zizl9T8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DQyEB/2q8gBjtpi1kUluAjmb+yenmIQ7NPcS5NQohEZExm14hdeftfpSdan5cnH4b kSfi25dtuPW/+S1nz3bAZoNNXML9jOqEyTYv4pFSjPn1l0LdBmJq2kJ+Z9tMdeN4aH lwQBHo+mqV6ojMbiEaNOY3sows+8RCDg//qb82Sg= 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 4.9 18/32] can: usb_8dev: fix memory leak Date: Mon, 2 Aug 2021 15:44:38 +0200 Message-Id: <20210802134333.502877440@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134332.931915241@linuxfoundation.org> References: <20210802134332.931915241@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 0e865f0c31928d6a313269ef624907eec55287c4 upstream. 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") Link: https://lore.kernel.org/r/d39b458cd425a1cf7f512f340224e6e9563b07bd.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/usb_8dev.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) --- a/drivers/net/can/usb/usb_8dev.c +++ b/drivers/net/can/usb/usb_8dev.c @@ -148,7 +148,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 */ @@ -744,6 +745,7 @@ static int usb_8dev_start(struct usb_8de 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); @@ -753,7 +755,7 @@ static int usb_8dev_start(struct usb_8de } 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); @@ -761,6 +763,8 @@ static int usb_8dev_start(struct usb_8de break; } + urb->transfer_dma = buf_dma; + usb_fill_bulk_urb(urb, priv->udev, usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX), @@ -778,6 +782,9 @@ static int usb_8dev_start(struct usb_8de 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); } @@ -847,6 +854,10 @@ static void unlink_all_urbs(struct usb_8 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);