Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1646946pxy; Mon, 2 Aug 2021 07:02:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxc6KsVOWtW7k4HbjD7SjPjJu9dHPzx04dbvJrrrjoJMFJ6MYGrCzv1+8WoDw2kAxiTOShs X-Received: by 2002:a05:6402:524b:: with SMTP id t11mr4582269edd.330.1627912925604; Mon, 02 Aug 2021 07:02:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912925; cv=none; d=google.com; s=arc-20160816; b=N/2IGn7zAILXs6wIQojedT3qmr+9mD8rKEFHwqW+rhIuS06KH7SKmSpBsG6ASJuMdk tkkzkPmA8QrZfxaxZBN9bNRln2YrarCl321oT4Ox7oLJ4DRLGLGPYOU+asEP8CsT/+xi x3b8/8sA0apHV1irZvJkrQLCRZd/mZsVrg8ldc2Hm9T84pF6BzZMKutq1EIJGks4WMJV 3YzXY1af2q194F9DzybCI+s5GGoeskG6RC4jDj50sn+SeGhpWR9qIY9kI7iqLK3nZ23c agrXrUZfRoTXD1WMHct3ojq4dWDaGwbHu8RTR6DGjxLeqhyB2zKJTvIzhGIZadmQZnji vpQQ== 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=XPDHlUPTzmtdi/Ml/Vty6MYz+htMQ3Pb0eiV+3DqXq4=; b=cG1GMnq1N2N5LEQ6o3IX2bAnMS6/JlnzlRTZfITcWz18MGaPmd+scXDVGfqPceZ6MU FHTIRj/fZ9gA18uAl49CuA3cdNw+R3MiZ8DnsiutWPhWeZPrZ5mZMAowXLDmJ9R73krW r2Eivh4P6f0p3xHAoTFIjUyZUmuLmdrqG5A9ftwql64sU0Loj9J4GVrdjnOqPIKKu/Cl j6ZtST8yo1Do+87wL9mAudxpUfKlqu/De4l0YWSi/cblJ9c/2bJnjGPZSDEu1WSV7HPL UQfiWsN/5FstfpKxB5Q8nWZJ35caFvKKRD+W7UNiynMStlbZ+AEkNYnYRvLXgHQLHDI1 UYnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2AUOqIYr; 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 bq15si9542491ejb.128.2021.08.02.07.01.38; Mon, 02 Aug 2021 07:02:05 -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=2AUOqIYr; 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 S236975AbhHBOAH (ORCPT + 99 others); Mon, 2 Aug 2021 10:00:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:33086 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235645AbhHBNyP (ORCPT ); Mon, 2 Aug 2021 09:54:15 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EF56D61163; Mon, 2 Aug 2021 13:52:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912343; bh=aD7IiEmTI7yrt/iGBkXHMfaZrzPwI37ZRx3xDpsq6lc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2AUOqIYrbadbPS1XD80eNF7ZOP55lSQ/YmBp8/QbO2BYc2Pn9CladFU63piB0kZYM zEJ/fYVJ7M4fCXyPauWOZ9lX/WmPt2ZEaxl2ygLTvBWmBKXngLAPb3u7z+3WyGkNc+ n64j/ScB3oSPN56127YOUArBxeky5tulvlRHv4PQ= 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.10 17/67] can: ems_usb: fix memory leak Date: Mon, 2 Aug 2021 15:44:40 +0200 Message-Id: <20210802134339.602503223@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134339.023067817@linuxfoundation.org> References: <20210802134339.023067817@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 @@ -255,6 +255,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) @@ -587,6 +589,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); @@ -596,7 +599,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); @@ -604,6 +607,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); @@ -619,6 +624,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); } @@ -684,6 +692,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);