Message ID | 20220609204714.2715188-4-rhett.aultman@samsara.com |
---|---|
State | New |
Headers | show
Return-Path: <linux-usb-owner@kernel.org> X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 297F0C43334 for <linux-usb@archiver.kernel.org>; Thu, 9 Jun 2022 20:48:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345261AbiFIUsN (ORCPT <rfc822;linux-usb@archiver.kernel.org>); Thu, 9 Jun 2022 16:48:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244709AbiFIUsM (ORCPT <rfc822;linux-usb@vger.kernel.org>); Thu, 9 Jun 2022 16:48:12 -0400 Received: from mx0a-0054a801.pphosted.com (mx0a-0054a801.pphosted.com [205.220.160.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EFDFC966F for <linux-usb@vger.kernel.org>; Thu, 9 Jun 2022 13:48:10 -0700 (PDT) Received: from pps.filterd (m0208802.ppops.net [127.0.0.1]) by mx0b-0054a801.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 259DBEBp005061 for <linux-usb@vger.kernel.org>; Thu, 9 Jun 2022 13:48:10 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsara.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=samsaracom06092020; bh=oOBUUa5Xferxj7ROAF97BuBNgotz6S04JATthgo88jY=; b=rsFKGpvF+xtXJ3FRTNf1t5dGRUP67yfKqyUNsIFRTqYqwxq34dTHXJY82nV+i9kG+kky zURDXBKOF7HBFWXOXwLbkoeF5BwqZOarDWdbqe3g9JpnKAsdh6FIQ2bBdkMaBUwjyuu1 1l9F+GqEQCPUGdIERTl26aMvJ3LIidPAQGh3QOxISkaoMEtbSBwiywbEGfdt3OO+sgrG R+s9tud186Tfscarc8prZ9jvs6+krWnO/Nys/XIPWfQcPhv214wjlMtZ+eqpK9Ip5Xpz vklNHftfhpiCN8mnhqxfQnW1Q2Z0gEMmkuqnQIqVCJoTWs9xEShNCUJXChw6VllM/Vff ag== Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by mx0b-0054a801.pphosted.com (PPS) with ESMTPS id 3gg5324hgk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for <linux-usb@vger.kernel.org>; Thu, 09 Jun 2022 13:48:09 -0700 Received: by mail-qv1-f70.google.com with SMTP id dg8-20020a056214084800b004645cecc145so15813714qvb.6 for <linux-usb@vger.kernel.org>; Thu, 09 Jun 2022 13:48:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsara.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oOBUUa5Xferxj7ROAF97BuBNgotz6S04JATthgo88jY=; b=IaFHFyWGrl8Uw8RXWCb2AjJygHC9jjDYejHyJnYSRNoxxWaq8qPLhEFICkpfTb0adt HG2Zv99zapxlfqFgxdOGuSlQ425ISWN+cM3NxsQPJNWwm9wLNoawKKnczUZEhuUHFXRm d9h/6XKKyXg4Pdy0hOr1PeDFz+xZfLcTw1g8KIKrjL2lP62RvOc14rUfORn/KrLzZat6 ZlBKmNuJBc13jwBcVi9LujXGu72n1lefYnc8f08sWMJ3YHl22BDCErTZu/s4Zx0qplX3 Ds4Bz9IbbKNIKkhP3TekpH1qF+PRn3+fqjhyV70k4TF6ex1cVew68ppzFUU+F1WRXG4c T1Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oOBUUa5Xferxj7ROAF97BuBNgotz6S04JATthgo88jY=; b=zQ/yzoyV2zOA9TWs3tYaYBXhr+W+Db1VthpgEfByoWodEn0/wGqYzfzmZNxXkfzCLu XjrlU9nZkkUAYx4+pi8BglfMhloumL61MAapPLFyqhkwtaBXSWwmFYm/yMI2Vd7r5BcV Wp6MZaOEisj83o+m2t8jyq4rEfTNNstuFwYSMYYrq8Dfk0CfpX2L/hnrUT5MM1Ymo+7K /klWTZdnXMUCAft8Dv8EhRt6Z1q5/tou34s7aniM2boU5+zC5800Sj7CPv/546RGxyte /eVg3SHi6nEEJqek5IzdGzN2597qKD4HgB37VRnbWR5hSiEBfcpLfh7mjPo7IwP6rQuV zCag== X-Gm-Message-State: AOAM530SR9akBYhc1GPtyqKmVJLlku14wWyKHA6+0peZ/20JrbGhla90 EV5YzYEQR/LEfutZ8ZInpKX5xJs+scDseYnk92m7aaHeqje/qobNsMkPwHwkdaFam0kea1bNcjm 4f9JpWbUdCtrtpOZESMSH X-Received: by 2002:a05:622a:1004:b0:304:eb0c:19d1 with SMTP id d4-20020a05622a100400b00304eb0c19d1mr20270160qte.674.1654807688327; Thu, 09 Jun 2022 13:48:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBtjBBGe3lkd6vgYMI93ll0Q+0vJlLGRW/mRDn3XEgNK1nfOuWcGLKrF3Rwyz0/m587HwXOA== X-Received: by 2002:a05:622a:1004:b0:304:eb0c:19d1 with SMTP id d4-20020a05622a100400b00304eb0c19d1mr20270139qte.674.1654807688032; Thu, 09 Jun 2022 13:48:08 -0700 (PDT) Received: from downhome.. (ec2-3-215-55-7.compute-1.amazonaws.com. [3.215.55.7]) by smtp.gmail.com with ESMTPSA id d5-20020a05620a240500b006a6b1630e95sm5428568qkn.45.2022.06.09.13.48.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 13:48:07 -0700 (PDT) From: Rhett Aultman <rhett.aultman@samsara.com> To: linux-usb@vger.kernel.org, linux-can <linux-can@vger.kernel.org> Cc: Oliver Neukum <oneukum@suse.org>, Alan Stern <stern@roland.harvard.edu>, Marc Kleine-Budde <mkl@pengutronix.de>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Rhett Aultman <rhett.aultman@samsara.com> Subject: [PATCH v2 3/3] can: gs_usb: fix DMA memory leak on close Date: Thu, 9 Jun 2022 16:47:14 -0400 Message-Id: <20220609204714.2715188-4-rhett.aultman@samsara.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220609204714.2715188-1-rhett.aultman@samsara.com> References: <alpine.DEB.2.22.394.2206031547001.1630869@thelappy> <20220609204714.2715188-1-rhett.aultman@samsara.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-GUID: 7lqwMMVw-66B40VfHo2zDcYr0JvcpAeS X-Proofpoint-ORIG-GUID: 7lqwMMVw-66B40VfHo2zDcYr0JvcpAeS X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.517,FMLib:17.11.64.514 definitions=2022-06-09_15,2022-06-09_02,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 bulkscore=0 spamscore=0 impostorscore=0 adultscore=0 mlxlogscore=576 suspectscore=0 phishscore=0 priorityscore=1501 mlxscore=0 lowpriorityscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206090077 Precedence: bulk List-ID: <linux-usb.vger.kernel.org> X-Mailing-List: linux-usb@vger.kernel.org |
Series |
URB_FREE_COHERENT gs_usb memory leak fix
|
expand
|
diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c index b29ba9138866..3ded3e14c830 100644 --- a/drivers/net/can/usb/gs_usb.c +++ b/drivers/net/can/usb/gs_usb.c @@ -768,7 +768,7 @@ static int gs_can_open(struct net_device *netdev) buf, dev->parent->hf_size_rx, gs_usb_receive_bulk_callback, parent); - urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_FREE_COHERENT); usb_anchor_urb(urb, &parent->rx_submitted);
The gs_usb driver allocates DMA memory with usb_alloc_coherent() in gs_can_open() and then keeps this memory in an URB, with the expectation that the memory will be freed when the URB is killed in gs_can_close(). Memory allocated with usb_alloc_coherent() cannot be freed in this way and much be freed using usb_free_coherent() instead. This means that repeated cycles of calling gs_can_open() and gs_can_close() will lead to a memory leak. Historically, drivers have handled this by keeping an array of pointers to their DMA rx buffers and explicitly freeing them. For an example of this technique used in the esd_usb2 driver, see here: https://www.spinics.net/lists/linux-can/msg08203.html While the above method works, the conditions that cause this leak are not apparent to driver writers and the method for solving it at the driver level has been piecemeal. This patch makes use of a new URB_FREE_COHERENT flag on the URB, reducing the solution of this memory leak down to a single line of code. Signed-off-by: Rhett Aultman <rhett.aultman@samsara.com> --- drivers/net/can/usb/gs_usb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)