Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1645750pxy; Mon, 2 Aug 2021 07:00:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAO4CtBsNgFHw64pn0I9qxS3yxlnWK4Ii6KY14C8z23dOzvRAOg3uFR5mm+K79oUs/dGWQ X-Received: by 2002:a05:6638:306:: with SMTP id w6mr6983093jap.132.1627912856923; Mon, 02 Aug 2021 07:00:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912856; cv=none; d=google.com; s=arc-20160816; b=O/2ZBhf0Ka6XQfFF4UFbOhi+WHEIL6k6QrS5GVguY6D+SUVAdev75MTpR3lAJqNwks 8oblu0HPp7FC7cVOO9IIg0ZzilE3QV7LF8jLaJIASIXItygOdlXlh9VvtDqbEl6p013e OdqGHLffTBXOeyoVsx6W5Hqv0hn5QhDZdbW3k3NRMZQ9W2EWyee84x+vXkTtzdtl6ipB ZVAReOVkRcmwAfoJ25aNPjhxocGjoyoqMs/95N9edGFKfcQWFyGHvfjiq1ggBtojNigK nGyozMsAqts4AfxCoBqFwPS59R9zAL1YYnzpGByCVONzP8a8aaVHstozqWQrFgocjGks 4OlQ== 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=30g9Ak07lvE9mxsa7egk3VI0ybOyt8BXh+zNBt4ANmI=; b=wlv0jmGuKwZ9chcuDzO5ND2shauQYYKOzA1BFB9SsPCJBIYMKCqOwiLoF/gXu6xua9 USsnRwISVlCriIQkwMyQ1yCrF/04Ou1D5cFMc72qHMiBtI5CeoowiHQtOmMUW2h1PDHq GPWvl+4a2wz0a5+KNRIb72uRWgZ45nzO9qzjPnEhnmqiBT2R7b/bBV4MgbiiYOp7ai73 +L6pspxEIRrx7JFo7/nmxpt3LOcX1fWZeOEYL8UbK4Ghy0AFT8WusexdqypgBFnbBoU3 GwSSUAJaLKp7hNVO2rmiPxVued8WumpTnkw9m7qfeOZzoR6haEc3PWPSWGGlzK9yvEuY xwnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=l1x7I6if; 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 s13si13486801ilp.102.2021.08.02.07.00.45; Mon, 02 Aug 2021 07:00:56 -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=l1x7I6if; 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 S236107AbhHBN7N (ORCPT + 99 others); Mon, 2 Aug 2021 09:59:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:33468 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234358AbhHBNxB (ORCPT ); Mon, 2 Aug 2021 09:53:01 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 073DA60FC2; Mon, 2 Aug 2021 13:51:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912317; bh=kIzYnuZFMrxLllh/y9GtgzQuBkfdQkXRMCDWVGkflII=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l1x7I6if52nyWVKQNn8jF1C5UzqSBpv3rCvO3Yqrbx+1ClJaCHSFyGYH8j7bo7wxl cMtTwsL55A08eHRWKuRrF8O3fuHxKd3pTsvppVU8b0KOc9GjHwpGW/Kk6aWMYMGLj6 PqEVrPVBlYcN0ky3mCqwczsctWeo2PIzSzmMI90o= 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 13/40] can: usb_8dev: fix memory leak Date: Mon, 2 Aug 2021 15:44:53 +0200 Message-Id: <20210802134335.815516209@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 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 @@ -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_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); @@ -742,7 +744,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); @@ -750,6 +752,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), @@ -767,6 +771,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); } @@ -836,6 +843,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);