Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1643590pxy; Mon, 2 Aug 2021 06:58:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5XWYsNprc2HFoiNWN7VZTy+N1GNP6ZucWMdV59wteib/Hu3dzbIn0qLQvIYHLdbojvyqI X-Received: by 2002:a17:906:14d0:: with SMTP id y16mr15644239ejc.248.1627912692669; Mon, 02 Aug 2021 06:58:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912692; cv=none; d=google.com; s=arc-20160816; b=otiOELIK5RJVIi8aVLjB2thrC9TbAB83VdE1QUPpYmEm1CNga6SyFzdTX9z23b3462 JNKb637PirOlPntTlDnMHA9qV8rKB3iU8t1F6gK093uVAvCPVmeWz/a7EYOY9D7xVfOK s8AMwR5xuHGZ6qOmMwTOeTpspuBtcpjSxHtbteO6+l833tvqE6RpNpVlsLUgZQxA2+EI SlNWNnHBtatvIuEE4wvPuZBpr9/f1jTEjgg2KaViyQMc5QVvGE3SlHZp0iRCg28wE0KZ do7szm4jZoCbu3I32f+3oOMgDprCUHDnuNlbsZXToOVbQEORMwp6n+YmVGyIblUMH6Jm n/bQ== 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=243Bb05b29phzZbZe5qqZjxUiBDGXJboOMRCXB2TqLs=; b=p6KdJp28MmVcmrHW4gE5yc6GwX4ilSMXPOBKxzIbJ3I1Lb2oLLpmlUbdcjRUYyGCEg VLb6nXRnVfrXjkHB4EnM5sZjMr+Dq20pTEwxKNsTxsxzplqukgRTTGNwCNgZyCYyruKm 3w74TYXVbVAPgJjBSNNJ6m64mgPVuqBy7NZDqlrazfTrKHqARpi4h8D02yyqADju9DQ5 DQvUqlIhDfZzHNFqgqkCx+hMfJ8gdeo1meLVlbADrkVFNuKC7XbiYxmalK0jXz7bLN8Y 3aHlMlLUzdyLyw8xs7PqzR+gQIwDtP/K5vEKlM/PqY/twjbwzkZor6/KMnig/0Y3UQyj 0UtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MvEKtjYr; 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 l33si13090424ede.457.2021.08.02.06.57.49; Mon, 02 Aug 2021 06:58:12 -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=MvEKtjYr; 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 S233956AbhHBN4g (ORCPT + 99 others); Mon, 2 Aug 2021 09:56:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:34364 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234724AbhHBNu3 (ORCPT ); Mon, 2 Aug 2021 09:50:29 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0A6B760FA0; Mon, 2 Aug 2021 13:50:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912219; bh=nuP+WiA0uVGut78Jn/GpJ7F/mzSfs+be3prq/tIE1mA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MvEKtjYrShu8Uj3zyI/oPUP2VQaJtSrcHkVKLw9s5kn1oOhA6T38aeV83pUq1AhxC CzoOxx4U+Qyol84U7uQx/83PDCPNGq8a+jpi+/RLeEY9BAZq1thI2RZYs3gYLVLL6I IbEUp2Zrwy9v03CTiGI368tNJhWqIg9yKPeptKe4= 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 11/30] can: ems_usb: fix memory leak Date: Mon, 2 Aug 2021 15:44:49 +0200 Message-Id: <20210802134334.446157656@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 9969e3c5f40c166e3396acc36c34f9de502929f6 upstream. In ems_usb_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 ems_usb_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: 702171adeed3 ("ems_usb: Added support for EMS CPC-USB/ARM7 CAN/USB interface") Link: https://lore.kernel.org/r/59aa9fbc9a8cbf9af2bbd2f61a659c480b415800.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/ems_usb.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) --- a/drivers/net/can/usb/ems_usb.c +++ b/drivers/net/can/usb/ems_usb.c @@ -267,6 +267,8 @@ struct ems_usb { unsigned int free_slots; /* remember number of available slots */ struct ems_cpc_msg active_params; /* active controller parameters */ + void *rxbuf[MAX_RX_URBS]; + dma_addr_t rxbuf_dma[MAX_RX_URBS]; }; static void ems_usb_read_interrupt_callback(struct urb *urb) @@ -599,6 +601,7 @@ static int ems_usb_start(struct ems_usb 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); @@ -608,7 +611,7 @@ static int ems_usb_start(struct ems_usb } buf = usb_alloc_coherent(dev->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); @@ -616,6 +619,8 @@ static int ems_usb_start(struct ems_usb break; } + urb->transfer_dma = buf_dma; + usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2), buf, RX_BUFFER_SIZE, ems_usb_read_bulk_callback, dev); @@ -631,6 +636,9 @@ static int ems_usb_start(struct ems_usb break; } + dev->rxbuf[i] = buf; + dev->rxbuf_dma[i] = buf_dma; + /* Drop reference, USB core will take care of freeing it */ usb_free_urb(urb); } @@ -696,6 +704,10 @@ static void unlink_all_urbs(struct ems_u 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]); + usb_kill_anchored_urbs(&dev->tx_submitted); atomic_set(&dev->active_tx_urbs, 0);