Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3178053imw; Mon, 11 Jul 2022 03:38:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u6qe4QYR72Ur1s13XF1b21BFvgVJ0So/1a9XBRFTVPQ4DUJ1sIUpipBR7Ml44CWVOZzJQY X-Received: by 2002:a17:906:58d5:b0:722:fc0b:305a with SMTP id e21-20020a17090658d500b00722fc0b305amr17626532ejs.251.1657535927990; Mon, 11 Jul 2022 03:38:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657535927; cv=none; d=google.com; s=arc-20160816; b=vQFD5x8W0Ge5iD41mbwNfLDPxTQH3rMW638n9Jw5Ye3pyv+Pl4XNIs4sZeS/sn95+7 2MBiGcebJc6LJNZ8dTo2qwThc3nzdbi0fz7x/QbHRH7BPMZNdQ51hNfWJyZc3ABPcNBS ZJ9W8JL1ARh2QE8w3Gi1yW5mjq67INLn4aT4bBsShEu8IDPI/GbUnzfmjyFuFlsCLJO+ sgOiDI9ieMK6qCAg4EpRAAijy9frkfaALEHfOOOXGLPthBEwIhA1iMIBTnCakplzNY8g p5xsK6YsUQyuBKnAMCyReUQPW8nGL2gtlbFNDhZ+ChbizyrJcsn6+4714iFsxRpSSLZ1 KnJw== 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=ycSY+odRALDNNPYYQZtzLMLVcWPc93pm44Jnc9ZNA04=; b=INcyMKf3AvDWpfSB4giSynWvtuW1ntoHZ56foG8tl9fAOC7rqeLJmZCMY+M1uC74/V x6H6ta2ffwvuJjYVisKLWeuWgGvosozNEpO6O5Oc4NMKW81Dbgx6B7RKYURBJ+lzcO2r OFGEf/wgjgmfdhpUUgcHcgz3H+A9NLt3l16eQWsWIqubHuszcXOf/Z+LOnMl90fpL4ai nm0WUGYQpzoPrkIXrF5l1CmOLkAhdN7vaiqnQeA9EAYXmfuf+DUZ3cKeqOXAoHn2KAtD IKN0B9GxGdsW3edczEIYtaDDRtpAtqpnn5JJz2GWz9epaXvXX89CLhuwHhSKP+CrZaN1 h23g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="goqlL/Ge"; 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 e22-20020a17090658d600b0072a6779a9afsi11467450ejs.249.2022.07.11.03.38.22; Mon, 11 Jul 2022 03:38:47 -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="goqlL/Ge"; 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 S230450AbiGKJHH (ORCPT + 99 others); Mon, 11 Jul 2022 05:07:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230093AbiGKJG4 (ORCPT ); Mon, 11 Jul 2022 05:06:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1286C220F5; Mon, 11 Jul 2022 02:06:56 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 9E57B6115B; Mon, 11 Jul 2022 09:06:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A74EFC34115; Mon, 11 Jul 2022 09:06:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657530415; bh=w/r9T7KTZFZxh+QFvGAZJ4PpK5bboTkIC5b0hwiZBHo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=goqlL/GeG1emI0h/p1T01qQzSo2DgUpywBFS4pbcIOzr93BYczzARPugMKMWIA/kI 1ENWtpl30vfS+jp/To/RJ4tnS4Id3ys95KCVVMsHAVGBkgY2mE+EWbcjkpKjGLMAnl 7b8wOdoWRuxklLx5fcLx339YPHYQhwRESGCuEiKI= 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 4.9 03/14] can: gs_usb: gs_usb_open/close(): fix memory leak Date: Mon, 11 Jul 2022 11:06:22 +0200 Message-Id: <20220711090535.623905339@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220711090535.517697227@linuxfoundation.org> References: <20220711090535.517697227@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 @@ -192,6 +192,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 */ @@ -601,6 +603,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); @@ -611,7 +614,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"); @@ -619,6 +622,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, @@ -642,10 +647,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 */ @@ -710,13 +722,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);