Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3162362imw; Mon, 11 Jul 2022 03:15:34 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uUq9PsvurGfL0k9KbVHe1+U9ENSoiwUrOcClcQVzgIiH7Vz5QBGGqbICqVAKi1z9rN4t/N X-Received: by 2002:a63:55:0:b0:40c:a2e4:c328 with SMTP id 82-20020a630055000000b0040ca2e4c328mr15273032pga.499.1657534533906; Mon, 11 Jul 2022 03:15:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657534533; cv=none; d=google.com; s=arc-20160816; b=jBhAzOM1MQ0lIhpDajjobsC0ALyCnFH4fQ1l4qOUL0NdEUv3uB+7HVX+oWVXh5MDAh jcC+bRsFxQLeMiZ3B5niz6msfjDqChrUTS4RME/m25Femkwj3H28kqweJpCoIvJ2U5vz SeEwU8hOoSkrT/cwRZ0PXwLQVkDhtoWJRWT72Xz3z1z7sK3XsBTWymu3jMTfD07FOviW 8GO4/B74WZoZfcS3mVvQf1geQGzmBEovnQcn+FHlYXWLRtezZCrFq9nuTo7I8eakiGm6 b4sNUkpDo6n3ClpDckJq8qQHNVUJus2xkOCEXxYZdKFinNfsdekR3m97bFNPnbJTaSnD Tlow== 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=ZAodBYWnU9nxAVLfFKWbVXgwbs+RcHjytVsnATHjVxg=; b=yylIJEcanNgSQd04lToNlziNzpmpn5iNKZ5Tv9fRwX1Y82fApAlma8WU5Pv2bZRk/t lrALY/qCCRHgJIk+PkCLyofEVN8Z7suMzR4rlzyWf9V/krg98YjxTBxfKlRcHJUiILAo WgjaQfV0XXdx+scMTNZL8fWcooimaj/yl9vqkiebTQeav1hTusSILeJIYeBVTkb/X5at TKEvdCU+Zx1KEw+0EhOIrylKW7TuoB8AG3OxYm4kz0bgFXzhxU6HLm0NoFxG+X5njUxr HX18rcR2EnOO3zvs7bjWJe7+hMBbpvFywE79dA//FKz5OnviLwE4w5syUvZgEa7/OxXB 87Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tfIfRfiF; 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 c123-20020a633581000000b0040cbcf5f997si10780056pga.209.2022.07.11.03.15.21; Mon, 11 Jul 2022 03:15:33 -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=tfIfRfiF; 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 S231243AbiGKJJy (ORCPT + 99 others); Mon, 11 Jul 2022 05:09:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230053AbiGKJJQ (ORCPT ); Mon, 11 Jul 2022 05:09:16 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0412427FCC; Mon, 11 Jul 2022 02:08:05 -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 ams.source.kernel.org (Postfix) with ESMTPS id A3522B80E5E; Mon, 11 Jul 2022 09:08:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0705C34115; Mon, 11 Jul 2022 09:08:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657530482; bh=pILSbY4XJmMlzVTApIp6ncXPXmKzjXMYUZqolYEqo2U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tfIfRfiF/whRsFkem9o3KunOR1qIPajmjIEB+DtghkDRMMYVFymGPdOYHRkiQgd3g t5ddbQmtkeWLNo5Z8bj4axIBKtRgAaPVIr7VTwubOqVV3Fpy0O4IMKIhK26Oi26Gsl C04AC4KW5LsS4STf48oVUm41ldugrDE3j8RWe1H8= 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.14 04/17] can: gs_usb: gs_usb_open/close(): fix memory leak Date: Mon, 11 Jul 2022 11:06:29 +0200 Message-Id: <20220711090536.387211041@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220711090536.245939953@linuxfoundation.org> References: <20220711090536.245939953@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 */ @@ -600,6 +602,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); @@ -610,7 +613,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"); @@ -618,6 +621,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, @@ -641,10 +646,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 */ @@ -709,13 +721,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);