Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3163022imw; Mon, 11 Jul 2022 03:16:23 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vsePzibk5Dt5RmNvwvKnnMJMpPG9hXeMcIxLAi9zCIMlijwX0YgwCuTbuGbafjz3eRTv4w X-Received: by 2002:a05:6a00:1a:b0:52a:dec1:308c with SMTP id h26-20020a056a00001a00b0052adec1308cmr36135pfk.52.1657534583738; Mon, 11 Jul 2022 03:16:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657534583; cv=none; d=google.com; s=arc-20160816; b=Q6+4GracZvch4J+zYuUclCI+IpUMnPD3D3VmtNSQnbX4rNwJqzJ4coun8+IKjCDjPq ZS+qPrgwT830Lr0ZdlDk3vwqFo2mUzLrg/AYzNOlikil+JliFf3EEOnKxeUYt2c8gYQ6 NXdHah30siOBsAx7Rev8L2648iauZj1A4ZjKtkFK+eATNJVa2VDmvDsXgoNjIhGCIyA5 HRgsqQkIE1srBRXfghSO1FtAH6uYxX1vJ0Mx10/U6FsqDNO4jeChhS1rUqhp2urSRCTC YkGQQs7SV41BwERM8Kna1tzhUNwjZqoVkYMqaXYZj4Qvecez+kqgRLGNM5QCDaR1hl+a ToWQ== 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=pvVqzswWDhoIcCRmNDzsHhbl51LhguG+NgghAhRnh0U=; b=N6hOVfZ8qckarEdzgLMcdP8ZS8hT1CYMBLYGm1S0FJznD9MkVez8knCjqvnZZryag+ VwGnPuEpYZ95qtTL3VPN3jYV6blyeFZlYWImg86ZMD89JTt8ET93QYOWo25ce2TCwQWU 2H3fOiCdLTEm41aO8mWiqEWoaAzBJ8UPqqpH9rn4Fs9Jp7YXd/ZbcT9trmXYOsvtr9er Dy4Sng20kQPRkhiEREIc6g8YNgrkFVar3rQnCAFvH/zfnfUNuL90ewGzoJ8JKVseKPu9 U7v0xsedA36K+oQXEmN+JhS5hqm12gwr4CELgl9v/oFL8IFtzsyBq/y9L3RBfuwbbAa5 AoYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MPJ2hHNc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a2-20020a170902ecc200b0016c46ef4762si4393776plh.315.2022.07.11.03.16.11; Mon, 11 Jul 2022 03:16:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MPJ2hHNc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231795AbiGKJUA (ORCPT + 99 others); Mon, 11 Jul 2022 05:20:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231893AbiGKJTM (ORCPT ); Mon, 11 Jul 2022 05:19:12 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 723BD4E86D; Mon, 11 Jul 2022 02:12:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 6A205CE125D; Mon, 11 Jul 2022 09:12:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69874C34115; Mon, 11 Jul 2022 09:12:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657530723; bh=9KilP7OB14MW6KxOfwyS+L8S9vkInApzgoS28Yry5UA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MPJ2hHNcSqZ2507Ov0ZZKZG/2Jwf+OvDAPGE2Yyssaz8hhCzFWxr/uvTjdV1fMaYo xKr6cPb9UKq8TAmH0juir1RooieHtJcDfdq/MSiNNWZuva5REVPmnUecKUtroO/E/R r9Fmmp+bbS026NxyA/ZPmL0B052MPKeAak6iJ3e8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Rhett Aultman , Marc Kleine-Budde Subject: [PATCH 5.10 05/55] can: gs_usb: gs_usb_open/close(): fix memory leak Date: Mon, 11 Jul 2022 11:06:53 +0200 Message-Id: <20220711090541.923831295@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220711090541.764895984@linuxfoundation.org> References: <20220711090541.764895984@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rhett Aultman commit 2bda24ef95c0311ab93bda00db40486acf30bd0a upstream. The gs_usb driver appears to suffer from a malady common to many USB CAN adapter drivers in that it performs usb_alloc_coherent() to allocate a number of USB request blocks (URBs) for RX, and then later relies on usb_kill_anchored_urbs() to free them, but this doesn't actually free them. As a result, this may be leaking DMA memory that's been used by the driver. This commit is an adaptation of the techniques found in the esd_usb2 driver where a similar design pattern led to a memory leak. It explicitly frees the RX URBs and their DMA memory via a call to usb_free_coherent(). Since the RX URBs were allocated in the gs_can_open(), we remove them in gs_can_close() rather than in the disconnect function as was done in esd_usb2. For more information, see the 928150fad41b ("can: esd_usb2: fix memory leak"). Link: https://lore.kernel.org/all/alpine.DEB.2.22.394.2206031547001.1630869@thelappy Fixes: d08e973a77d1 ("can: gs_usb: Added support for the GS_USB CAN devices") Cc: stable@vger.kernel.org Signed-off-by: Rhett Aultman Signed-off-by: Marc Kleine-Budde Signed-off-by: Greg Kroah-Hartman --- drivers/net/can/usb/gs_usb.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) --- a/drivers/net/can/usb/gs_usb.c +++ b/drivers/net/can/usb/gs_usb.c @@ -184,6 +184,8 @@ struct gs_can { struct usb_anchor tx_submitted; atomic_t active_tx_urbs; + void *rxbuf[GS_MAX_RX_URBS]; + dma_addr_t rxbuf_dma[GS_MAX_RX_URBS]; }; /* usb interface struct */ @@ -592,6 +594,7 @@ static int gs_can_open(struct net_device for (i = 0; i < GS_MAX_RX_URBS; i++) { struct urb *urb; u8 *buf; + dma_addr_t buf_dma; /* alloc rx urb */ urb = usb_alloc_urb(0, GFP_KERNEL); @@ -602,7 +605,7 @@ static int gs_can_open(struct net_device buf = usb_alloc_coherent(dev->udev, sizeof(struct gs_host_frame), GFP_KERNEL, - &urb->transfer_dma); + &buf_dma); if (!buf) { netdev_err(netdev, "No memory left for USB buffer\n"); @@ -610,6 +613,8 @@ static int gs_can_open(struct net_device return -ENOMEM; } + urb->transfer_dma = buf_dma; + /* fill, anchor, and submit rx urb */ usb_fill_bulk_urb(urb, dev->udev, @@ -633,10 +638,17 @@ static int gs_can_open(struct net_device rc); usb_unanchor_urb(urb); + usb_free_coherent(dev->udev, + sizeof(struct gs_host_frame), + buf, + buf_dma); usb_free_urb(urb); break; } + dev->rxbuf[i] = buf; + dev->rxbuf_dma[i] = buf_dma; + /* Drop reference, * USB core will take care of freeing it */ @@ -701,13 +713,20 @@ static int gs_can_close(struct net_devic int rc; struct gs_can *dev = netdev_priv(netdev); struct gs_usb *parent = dev->parent; + unsigned int i; netif_stop_queue(netdev); /* Stop polling */ parent->active_channels--; - if (!parent->active_channels) + if (!parent->active_channels) { usb_kill_anchored_urbs(&parent->rx_submitted); + for (i = 0; i < GS_MAX_RX_URBS; i++) + usb_free_coherent(dev->udev, + sizeof(struct gs_host_frame), + dev->rxbuf[i], + dev->rxbuf_dma[i]); + } /* Stop sending URBs */ usb_kill_anchored_urbs(&dev->tx_submitted);