Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1648496pxy; Mon, 2 Aug 2021 07:03:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzD32LYkILnDIMo3ZTJdQoGQDBqkAFAuyCoxLwq/BwOgxMEBvaQ8d73KNVwWwWN8uvublBP X-Received: by 2002:a17:906:af86:: with SMTP id mj6mr12389148ejb.144.1627913021726; Mon, 02 Aug 2021 07:03:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627913021; cv=none; d=google.com; s=arc-20160816; b=RrMQjCb4WlbqdZV4CluatflIhGxeMjgK473/RJo4MVIxBfY2x/sNavw0/vxZftZmYD c25Unn+cn9b+dDMxU9Qnn8ZYjtl9lW0RLkF5uSl4xxg8AvpRBDQsDi+WtrPW2Tx3+H2K YBAtBQmtfSml6hWlTBRVRGbB9OI2QzSx0M/C4rHMG/4GyihXHy+fUu9HPUrI+SvDuoMX 1aZ4h7GPwVR5r+JIdw2RILCAQmXDGka3uplCwLdZ0MmHOl1tXhB/PwFrVkcJtfbhNYnE 0vxg2B/kfMOmobYGGvN/fOaZnCHE76lki3ZmhY9vFdAc3NkMbEr3WVHvA8OSi9JOrdYq gOWA== 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=me2TCq07dXZjUxp7tJkfapnJHs5zslLik8b5fcFbPDB+dPfI62OJI9OWOo0/+cmVW8 YWZeykUhVbaiasNCd5UQO6O8kYBRXKFyfBklHsaSu3ArtxINdIvG9wAmvcVquoVQiblE 5YHlHitRmTNuRR6C4FocvQIl3mAMv9kwqky59IZWcfjEwxqDPOn6tcGW5PAP+fNL04cl e3nQkwdOVAfGL3z8TLgAm7rjtnf8s/RrhBlJp8+gEWd+qXXavXe/YZzlwsMy+1OjXCn/ m6DwauQHUgrmR/LwAlBS3iaXDYJ4NVfR8lRa058nAiHp5JK+xiazmZTyDPRMqDLiKbqu MNkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="v5U/vP7c"; 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 s11si10951990edh.361.2021.08.02.07.03.19; Mon, 02 Aug 2021 07:03:41 -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="v5U/vP7c"; 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 S235926AbhHBN7H (ORCPT + 99 others); Mon, 2 Aug 2021 09:59:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:33500 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234661AbhHBNxB (ORCPT ); Mon, 2 Aug 2021 09:53:01 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3112A60FC4; Mon, 2 Aug 2021 13:51:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912319; bh=aD7IiEmTI7yrt/iGBkXHMfaZrzPwI37ZRx3xDpsq6lc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v5U/vP7cJaF9a+cmdJegn+OOSmhZPabGUkV9ofdasfRUZPguamRlBGwX4ZWx57WRh cV9iBLOpqQiDEEAxHuPkLW4V/5pLh3CAebuV7GwwlMqSuVoBu+06UoFKsjXx1lSqAO TPbsdf/9MIDvlag9cT0Pd5Di4Oe7G44FW9dSqmI0= 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 14/40] can: ems_usb: fix memory leak Date: Mon, 2 Aug 2021 15:44:54 +0200 Message-Id: <20210802134335.852354660@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 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);