Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1638571pxy; Mon, 2 Aug 2021 06:50:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDGuWwkUmNE2uBWsd6jnSrDRVFguCcdzPxkoEL+2U1kp75aOhRee37qUFo3qG0NoZ0llFC X-Received: by 2002:a5e:a908:: with SMTP id c8mr199212iod.116.1627912228683; Mon, 02 Aug 2021 06:50:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912228; cv=none; d=google.com; s=arc-20160816; b=fw75kiNT1eSQnvwBFZJf0zx3Freq3IWYq+9QFzTPgwuVtU9f8qTkYjDQEUmWArsbUQ 3lv+nojj9EnNxNePbKZhTM/5Be6DyWziUerBoC5T374vOOC98tAQr63uzodBeZ9t2Nwy BsbbZgOS2X1NAFnNNPGdUpIDO6D9+waW9DRSBPWCyl3aJXns/lWSzo0Ak5NCOEYMajnc +p9Mjw+oH9NdNaVNA3ZB/1CLnNJDMNHeITg2o6ENTC6p5Gnh5A4Uerot22NhNAC4y7Xa QAb9jet3eumsb/oYeWoQrcWszHQ2aaXc2hmkvSz7ZpbwkgX1x2aXs14JGJD9QDzvPx9E bnWA== 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=nqs7ufQLxVcqYoLCzczRGTCA0pXPtCvm9kwYA70j6x4=; b=fSlKGkTovqxEqvXPRjJUsjrERMXx9LCqsxQaq5tvMPOO1wCBRj8TeN39jMSPlXm/zB WZGIpsfQv+mGPoJtR5/CNBetg/NX37a8KTzESNKGCdLlal15eMkoekBXXJJVsEh0uME2 0zMkf9qSpZ3jk1JfgXNoMeu31a+jN0P49nRbGe8+mrlW3Y2kk4FlHe8GxpsKfpQ1Mb93 GcVp8XlZh4+bUmZr+xu4VYlOksn6fzGXEXPMT9xOTp6NcknxSOv3LeNr/JWZ+YUegF5o ngnNgUmiEVTzBLBngUGEb8dYUUgVeSUmNgirjNIlkSGVybxgyHAT4LgvWGz6hhtCTqqR ds1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="b/n4Ykdl"; 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 g6si11556123ioc.2.2021.08.02.06.50.15; Mon, 02 Aug 2021 06:50:28 -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="b/n4Ykdl"; 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 S234410AbhHBNte (ORCPT + 99 others); Mon, 2 Aug 2021 09:49:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:57350 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234355AbhHBNrf (ORCPT ); Mon, 2 Aug 2021 09:47:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6169A61151; Mon, 2 Aug 2021 13:47:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912034; bh=amS3J7JYAEwsQ82JVWTBmpY6Hz62KTOrP+34DQ2bFW8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b/n4YkdltyvK/wmnHdq1/we5cx1xl3kIsOHigfhjDJV8TAd8JtrdK6XT3Y4L/tt3h aavwmSH7F0DGkoOo7Zpk5vnSMIz5r8NRx3g+hUCEA5YAGKZGGm197OoEEDnlREa2V8 IiH6Uk1dFbNZwqSkwW3XOcylt75N7Gy02wCEqRf0= 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 19/32] can: ems_usb: fix memory leak Date: Mon, 2 Aug 2021 15:44:39 +0200 Message-Id: <20210802134333.531900720@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 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) @@ -598,6 +600,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); @@ -607,7 +610,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); @@ -615,6 +618,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); @@ -630,6 +635,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); } @@ -695,6 +703,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);