Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1642580pxy; Mon, 2 Aug 2021 06:56:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpVE+Md6vTUUGHz7zK8hl8PMsdfcx0K789r8pCCK1sR82yquSnFbtUvRHcGlDNLtjLgIJg X-Received: by 2002:a5e:8401:: with SMTP id h1mr306646ioj.75.1627912590726; Mon, 02 Aug 2021 06:56:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912590; cv=none; d=google.com; s=arc-20160816; b=EeEl772AMsujRvkGdPeENfMZPxhm0aXXtzJ3kw8R4G7WzsZKJBN96F8K8HpHGddjIj 75gF0ySWC4DoRTnqjKWrUCTgW8cEon7pbaoAPCJUqES4wUmF3KFuvtDeqMNKj+9VqP0t DEzRa6QfR6KnUIDShABqbQ4JmimGwn6UySE2SYLvQA+4Zs5cZPe/4bxiIdDIP11g1C8n GO7PGbEgnbqaTxUvHgjHaupOIQ9JgetDhCv3ewVT+PFlO3JNF4y3M3W8b+Iy7563/baC FrLTN30Us1Z0U1fxWhtxxUnaN90ECr03SCcjOCzLgqIRVgWewSJZQm5V0T1QwwKc4DVR /7SQ== 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=lEf5BgIahrK3u97EKFOJ6/YEDkISm0m81CjoGHJ2K4g8QXfmfvxu+cQsLywEy88MbN IYjpwmzya4U6rCvKjd9X9SUk8xme7KQo7fRGedX/RtSgQ5GnHyRWUDHqCNoyTQMyzBgL bk5imZ0fRLVaD8HRcXCRCG1ZqrXcJe00ZeDN5QdPU/x/PssI7iZPUcPbI/zLQHKOEQ3J FZR6DrlIvhLT/wK08xLJybx/K1x1eYSYv4LgR2ldCWR1ktP/3iizinZENEiPyA32FctB e2Q/c9LOtSX304QFV9PyOVzdRuIn8+ZMpLA8urE0dHwMUvedcbl5MZd6fHw0BzBRlOxW 7Rkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QqMbRMIU; 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 d10si1945634jaq.37.2021.08.02.06.56.18; Mon, 02 Aug 2021 06:56:30 -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=QqMbRMIU; 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 S235047AbhHBNzd (ORCPT + 99 others); Mon, 2 Aug 2021 09:55:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:34094 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234844AbhHBNuU (ORCPT ); Mon, 2 Aug 2021 09:50:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4F646610CC; Mon, 2 Aug 2021 13:50:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912210; bh=PTKHef7/1KwNBx3M/xRqz3zbDFV1TQC+jLV4zizl9T8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QqMbRMIUwWkEovmRL8NY8HWisfIeJMkWMsyiyR1JdKP0MOP1axKSFV9NNo61aPb0L zDMlzcSLpCAyklR0KGLDbMcyJ6TUE8g8v8VSOoXkFRj/UuFfHHctYktN/6tGgKtLhy vIaQMMLiecpbi7aq9Z3T2VDzZAVJeTNeMg5RYQ5U= 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.19 10/30] can: usb_8dev: fix memory leak Date: Mon, 2 Aug 2021 15:44:48 +0200 Message-Id: <20210802134334.409098788@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134334.081433902@linuxfoundation.org> References: <20210802134334.081433902@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);