Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1638822pxy; Mon, 2 Aug 2021 06:50:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4DyrsdX7bav4BYxLME4iL80iuZ+e1dIz57Dsnf/hNY5IYZ4wJJzwwzkqa3PPOsox0vYC+ X-Received: by 2002:a02:c6d5:: with SMTP id r21mr3322639jan.71.1627912249816; Mon, 02 Aug 2021 06:50:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912249; cv=none; d=google.com; s=arc-20160816; b=K4V9zW9WzA5SnzdX9bK+xzoLGJ6qpKElSbKKI6m+spRR+cSYa1fiAniQ/1mjp7/6cT fe0/oDQWyScGhU8QrmTY3uO6xZ0bmVS0Qo1wACcm/t5hCIAnNKSCfSKBq1ME7m8tyoYg pp7hEgQBefh1AGRLeDgmQdFYhkMZdFDtUdsijfstqnFixoz09gOIMQt7S3Q2FmmszPeE KVZ4zDvSmzePkagulvvpEohZbR1h06IwOiidFljxPkq0QIDHDfTw9ceRumMVUiP12JWN uJY4zEIETqezn95NIoOxRfYFaSAziZoRProDlsNiPQOMNsL023/RKilwc0u7/bswH2pi 3y1Q== 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=95XnE/QXXX8K2SUzubJH8PpN1I+Ksjbolp9adty01SM=; b=v3NHl/7ziGgAuJoFkwkb7vMdGOcFwqG8+r0YjolZF+OcZ6GRuk/wOw75mamoxZgwBJ GWlvXysW9mbwbwZH2tyjZngklFBZjkVPvOBs/jG+c+bsOkqSheFwTasNNT6fg66TSyoe 0sBaSjqsexmKCENVLx/WnhiatLeriG6uYWcv+tr3OFczXr1h13AgQDkQoajnVYmdWst2 OtjLMBxQi3enwgrT/WFpiUHsF0Royq4NagQyeHsfaDu+AU7U/DQJUJ4B4s34Vj/6MtzF ZT3mF+2T6mUu89OBFIqCuKRNTxWI9rzi/9ffTrcPtz2Jur+2Vfkz1EDhTPcIzUWUBV7H yP0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zXG35PkT; 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 15si13523669ilz.158.2021.08.02.06.50.36; Mon, 02 Aug 2021 06:50:49 -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=zXG35PkT; 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 S234393AbhHBNtx (ORCPT + 99 others); Mon, 2 Aug 2021 09:49:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:57410 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234365AbhHBNrf (ORCPT ); Mon, 2 Aug 2021 09:47:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 89FD060F6D; Mon, 2 Aug 2021 13:47:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912037; bh=NJOEvPpoLwptY8+Dkqrp/K+sE5xEPdIjyk6kEAlIF/M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zXG35PkT+yMDD/b9PO6WoQte9vozSfnNz0BaeTfzk0mSOOCtS43SaxHNL6t1EU+yo AvVKmzfTp7/TaAzRLqKjiM7hxIm4Br8d7Qi9edWbe0U+81M5lRJRqEfiqnrXNRhUz7 th8HT1hE1LIJps4MmoPmd8g4NhgIXsxTBn3BZgTw= 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 20/32] can: esd_usb2: fix memory leak Date: Mon, 2 Aug 2021 15:44:40 +0200 Message-Id: <20210802134333.562182241@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 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 @@ -207,6 +207,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 { @@ -556,6 +558,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); @@ -565,7 +568,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"); @@ -573,6 +576,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, @@ -585,8 +590,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); @@ -674,6 +683,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) {