From patchwork Thu Jun 9 20:47:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rhett Aultman X-Patchwork-Id: 580564 Return-Path: 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 4DDC1C43334 for ; Thu, 9 Jun 2022 21:14:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231879AbiFIVOz (ORCPT ); Thu, 9 Jun 2022 17:14:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230184AbiFIVOy (ORCPT ); Thu, 9 Jun 2022 17:14:54 -0400 X-Greylist: delayed 1635 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 09 Jun 2022 14:14:52 PDT Received: from mx0b-0054a801.pphosted.com (mx0b-0054a801.pphosted.com [205.220.172.231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACD0D15FD1 for ; Thu, 9 Jun 2022 14:14:49 -0700 (PDT) Received: from pps.filterd (m0208805.ppops.net [127.0.0.1]) by mx0b-0054a801.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 259CFnQd004175 for ; Thu, 9 Jun 2022 20:47:55 GMT 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=mj2hkIW09jwJqJWJfmer2pDeLN4yDLA8jWIhlIEjSDI=; b=JLGky5crQYBVa86DxRvp1R6A8zpHV/YX5DLlL8Pim4X38oer0TsS6FSWORxP92/LRP2w KK4dw8+azjDbuUs9C1BYwuu2vLUhrcL/5+cg4bdAj8UM+KOB4jmU0lexgRST4sdXHETo jADR+JhYkxXenP4PzV+QUJoSzt11HIEI6Roh4WCFAFiA+AKAt51qSL+xxLhfUbq2TQTx gd1dcKX1M9ieFfkLRWwL4o0lZpFkAr6miy09r48SbFqDlVqWp2SVwfQcr4YhoBvatXNw /306HS+Pmoc/qrzlQ4pxSTCJ9hZpicVltbsRUn6FbpbEXmH+QaqUeEz7azvvAvluZGkQ gA== Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by mx0b-0054a801.pphosted.com (PPS) with ESMTPS id 3gg0g9mm6m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 09 Jun 2022 20:47:54 +0000 Received: by mail-qk1-f200.google.com with SMTP id h16-20020a05620a401000b006a6b8ac9c64so10788945qko.8 for ; Thu, 09 Jun 2022 13:47:54 -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=mj2hkIW09jwJqJWJfmer2pDeLN4yDLA8jWIhlIEjSDI=; b=NHjyBaTTUg507tR6XfCJdh17a1tcWIKL8h70vvjhANdFCpKSJApscm67SQ4zDllUkK YW08julLAUbGJXCfOMeUORGP3De43M4PU3r5RsM1pGwhRO6CXul799NUf4zx0Rc/D2l9 8X3g0eQZkkVfCZWdX7IjXM2GmhRkn5PWhwW5GwGg35IImMHehzdysl/9wkUJbizWfh3m LNgg0CfLbkCwsSdm25n1oBRTB0H1n/x/bGZkkSQuRLZsnqpFGmMKfTFGE6BkrFUl+3Gd LM8SCWCitWlsC+2jbIyDIULgzVAimF2OVBWvg72HOENxsT3X6sQR+Wc1etcC+XQujzHk Ki6Q== 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=mj2hkIW09jwJqJWJfmer2pDeLN4yDLA8jWIhlIEjSDI=; b=NwvYNKlWVMfnlk7t0Q5/0+590nFOxfDz1+mgoJx2aNVOvcdPb8o5foQIPWRjFjLkBG 5hmCY0d3/64jEGej5ICb7eUPzN6VRE6FB9S5vG/Vlq7aATMPuRCMlNVK0vZpVWa5+sZ1 gOoetvsM0VN9T0GFqlQ59vR2+otFLJDqsR2P/8EdftsRb1SoYPW4eX6dJRf6UF2T+Gcz YQQb3w4JNRh/OE1+jsOrV6JukfXUEc+GRiNFHjpiNhDs4ow+adIQ+5jRyY4IOwShEQUC EhkUJyOTsUqP+vblvsB/AcVI6fymVt7AQCwkTzQqmQuXuJEK1PkoQO4yCgE1YM7LgvCW lYJg== X-Gm-Message-State: AOAM532bFxsAr8qVgmyqGDUtKpPLHWs4qi5FlvyVvUXByvxPbeJEEIZ2 ogUimsQwCjG6MuvDTK6loZJDhAAx0b+rQ2zwjOXdh4IdKmFPG0erLZgN479Qk33gz0tIlWJkNMc IZQ3zfznj3H1RsiUM5JO4 X-Received: by 2002:a05:622a:15c7:b0:304:ec84:7718 with SMTP id d7-20020a05622a15c700b00304ec847718mr19464684qty.585.1654807673742; Thu, 09 Jun 2022 13:47:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLFMQSDU/UmbhQvQneb/4hszTQeS7e4wh9XttOkJ3dySL256BHi4OFlgXNNFe5HhCzp3TBGg== X-Received: by 2002:a05:622a:15c7:b0:304:ec84:7718 with SMTP id d7-20020a05622a15c700b00304ec847718mr19464659qty.585.1654807673455; Thu, 09 Jun 2022 13:47:53 -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.47.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 13:47:52 -0700 (PDT) From: Rhett Aultman To: linux-usb@vger.kernel.org, linux-can Cc: Oliver Neukum , Alan Stern , Marc Kleine-Budde , Greg Kroah-Hartman , Rhett Aultman , Vincent Mailhol Subject: [PATCH v2 1/3] drivers: usb/core/urb: Add URB_FREE_COHERENT Date: Thu, 9 Jun 2022 16:47:12 -0400 Message-Id: <20220609204714.2715188-2-rhett.aultman@samsara.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220609204714.2715188-1-rhett.aultman@samsara.com> References: <20220609204714.2715188-1-rhett.aultman@samsara.com> MIME-Version: 1.0 X-Proofpoint-GUID: ka7L38P-uVN8aikH7lWUETzw97ZRc0eW X-Proofpoint-ORIG-GUID: ka7L38P-uVN8aikH7lWUETzw97ZRc0eW 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 mlxscore=0 priorityscore=1501 phishscore=0 clxscore=1015 adultscore=0 lowpriorityscore=0 mlxlogscore=711 suspectscore=0 impostorscore=0 bulkscore=0 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206090077 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org When allocating URB memory with kmalloc(), drivers can simply set the URB_FREE_BUFFER flag in urb::transfer_flags and that way, the memory will be freed in the background when killing the URB (for example with usb_kill_anchored_urbs()). However, there are no equivalent mechanism when allocating DMA memory (with usb_alloc_coherent()). This patch adds a new flag: URB_FREE_COHERENT. Setting this flag will cause the kernel to automatically call usb_free_coherent() on the transfer buffer when the URB is killed, similarly to how URB_FREE_BUFFER triggers a call to kfree(). In order to have all the flags in numerical order, URB_DIR_IN is renumbered from 0x0200 to 0x0400 so that URB_FREE_COHERENT can reuse value 0x0200. From: Vincent Mailhol Signed-off-by: Vincent Mailhol --- drivers/usb/core/urb.c | 3 +++ include/linux/usb.h | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c index 33d62d7e3929..1460fdac0b18 100644 --- a/drivers/usb/core/urb.c +++ b/drivers/usb/core/urb.c @@ -22,6 +22,9 @@ static void urb_destroy(struct kref *kref) if (urb->transfer_flags & URB_FREE_BUFFER) kfree(urb->transfer_buffer); + else if (urb->transfer_flags & URB_FREE_COHERENT) + usb_free_coherent(urb->dev, urb->transfer_buffer_length, + urb->transfer_buffer, urb->transfer_dma); kfree(urb); } diff --git a/include/linux/usb.h b/include/linux/usb.h index 60bee864d897..945d68ea1d76 100644 --- a/include/linux/usb.h +++ b/include/linux/usb.h @@ -1328,9 +1328,10 @@ extern int usb_disabled(void); #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt * needed */ #define URB_FREE_BUFFER 0x0100 /* Free transfer buffer with the URB */ +#define URB_FREE_COHERENT 0x0200 /* Free DMA memory of transfer buffer */ /* The following flags are used internally by usbcore and HCDs */ -#define URB_DIR_IN 0x0200 /* Transfer from device to host */ +#define URB_DIR_IN 0x0400 /* Transfer from device to host */ #define URB_DIR_OUT 0 #define URB_DIR_MASK URB_DIR_IN From patchwork Fri Jun 10 21:33:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rhett Aultman X-Patchwork-Id: 581109 Return-Path: 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 B4D13CCA47B for ; Fri, 10 Jun 2022 21:34:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349424AbiFJVem (ORCPT ); Fri, 10 Jun 2022 17:34:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349148AbiFJVek (ORCPT ); Fri, 10 Jun 2022 17:34:40 -0400 Received: from mx0a-0054a801.pphosted.com (mx0a-0054a801.pphosted.com [205.220.160.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3994A1F2F1 for ; Fri, 10 Jun 2022 14:34:36 -0700 (PDT) Received: from pps.filterd (m0208801.ppops.net [127.0.0.1]) by mx0b-0054a801.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25AHMfRn015066 for ; Fri, 10 Jun 2022 14:34:35 -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=fVl0ZKee2vfFzhBKf4HZ3Mkp/PMJaRz+ojYbgSnU3Ps=; b=cnbQBbm+ZQYJprIF1CvpuvTE9HAQENNX2Sa1qPwXIl4myh9SUnlMN+/l9Z3jrvoMF9Ru sNxqk4b5M2Uu/o8gSx+Z9iNOVX9It9pXF74/l8mXpZ/orZU3Gk43mimuG/j+jewQlPO6 hMg41h1Mec83PcQN+kl8I+4Z5XVElNN2sUnv9FoIMvxVoweYTCQ3ACLpNbQ8DEtYoHnq GVYSWUqfMeHjavudM52r7vhKsHb8dy8FH4R+B3PLvt3bFCsve1JrBu0zJJ2sY0VjNInz TVYHSawzjLw5PIOS1qvVrcMtY65EwFB1qGcaZ+583Iy24dlsIh7Oqbx156q8tXzuZX6V NA== Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by mx0b-0054a801.pphosted.com (PPS) with ESMTPS id 3gkuvh8ssn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 10 Jun 2022 14:34:35 -0700 Received: by mail-qk1-f197.google.com with SMTP id j11-20020a37c24b000000b006a6bd97578aso121549qkm.15 for ; Fri, 10 Jun 2022 14:34:35 -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=fVl0ZKee2vfFzhBKf4HZ3Mkp/PMJaRz+ojYbgSnU3Ps=; b=MuB6LMebqQtVf3BsrAVlMLpHFcDJSLPHhRP2znNjP9ToiCy/m8WUdr95JkUQZRE4nL 6yoVWQzDeiGHqJ5F9VEMAJQQqp6qxOsep8XqLUEcG422wpJcA5+3nwOH+X/9+a6t71QW E8/YcPMCxarSSj2uviDhG7i1mZlXc0d1NDZYsBB2nEE2/RlprrPQ1ouj9DsEurKy3j0T 7Tf6qA8hvkAq//7nxTr1IimhmRiuyQ4Uee6gxR8UIqT0OgzRN97FUx/vwoeeHMpYL+Vw 1WL3aiSNpF34LzCPkuu2mhFKGd+wFln3kdLYtAbxloDEfu3veOLIq+Oj63m0pfMBH88h uUcw== 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=fVl0ZKee2vfFzhBKf4HZ3Mkp/PMJaRz+ojYbgSnU3Ps=; b=hW5FGAPh3wQTkiLBpgeloFP8iZFIWFmPlcvSpm1BOijNlKzCQh4jV9Nj69Xz6c+Uuc O/Ez4WxS4KPk4ya/RbpQ+1KfXekKzWurDtUAvuURzwfY7ZOmiBzvvqdZBN7MbuGbxomQ K98xoamzpx28NT2bPydXYcVHT4hI7tEPHSogWlrs9T1Il8YFNV830J97iznmaRmj8nxu AD2JrQ62kJXkgLdPloEE13NYyVsNasK6wrMEyndMocfWhfr5bc1iKlux8BU2cplc1ZFx MWd5kUiK31xOEffbPgi8cbF0H+oKZ7CSVHd6MUxqOC+yc/0aCF6LAWArs3WQV6RGED9t F2ag== X-Gm-Message-State: AOAM533BnMK1JfzkpI1+iNTm3/Fn3/zqzsgXwXA2kjJiFgRxBqWJoDif w0hRxUIOVyJPeSMJ2U0QXWDGQFYQicU73JEPKZ7vY0vJHSkKhxeEzRHQ/F3rSlQYAyMGwKCJO7j GrxBw7+4Az6UibiLMBJ3l X-Received: by 2002:a05:622a:185:b0:305:962:1a2d with SMTP id s5-20020a05622a018500b0030509621a2dmr11134044qtw.368.1654896874135; Fri, 10 Jun 2022 14:34:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzeD1I1YSxnfEnj2tibCp63A0+RAQCG/rNjhcsEX/n3qOmDmpLb8muSvMebXj3O+tRyQGMROQ== X-Received: by 2002:a05:622a:185:b0:305:962:1a2d with SMTP id s5-20020a05622a018500b0030509621a2dmr11134027qtw.368.1654896873830; Fri, 10 Jun 2022 14:34:33 -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 b11-20020a05622a020b00b00304d8cdddfesm139742qtx.86.2022.06.10.14.34.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jun 2022 14:34:33 -0700 (PDT) From: Rhett Aultman To: linux-usb@vger.kernel.org, linux-can Cc: --cc=Oliver Neukum , Alan Stern , Marc Kleine-Budde , Greg Kroah-Hartman , Rhett Aultman , Vincent Mailhol Subject: [PATCH v3 2/2] can: gs_usb: fix DMA memory leak on close Date: Fri, 10 Jun 2022 17:33:37 -0400 Message-Id: <20220610213335.3077375-3-rhett.aultman@samsara.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220610213335.3077375-1-rhett.aultman@samsara.com> References: <20220609204714.2715188-1-rhett.aultman@samsara.com> <20220610213335.3077375-1-rhett.aultman@samsara.com> MIME-Version: 1.0 X-Proofpoint-GUID: KKyMAuMUbTNxCD6QRvWlK7huzJImvQv1 X-Proofpoint-ORIG-GUID: KKyMAuMUbTNxCD6QRvWlK7huzJImvQv1 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-10_09,2022-06-09_02,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 mlxscore=0 clxscore=1015 bulkscore=0 lowpriorityscore=0 suspectscore=0 mlxlogscore=634 priorityscore=1501 phishscore=0 malwarescore=0 spamscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206100082 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org 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 must 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 Reviewed-by: Vincent Mailhol --- drivers/net/can/usb/gs_usb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c index b29ba9138866..4e7e6a7b961a 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); From patchwork Thu Jun 9 20:47:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rhett Aultman X-Patchwork-Id: 581398 Return-Path: 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 ; Thu, 9 Jun 2022 20:48:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345261AbiFIUsN (ORCPT ); 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 ); 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 ; 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 ; 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 ; Thu, 09 Jun 2022 13:48:09 -0700 Received: by mail-qv1-f70.google.com with SMTP id dg8-20020a056214084800b004645cecc145so15813714qvb.6 for ; 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 To: linux-usb@vger.kernel.org, linux-can Cc: Oliver Neukum , Alan Stern , Marc Kleine-Budde , Greg Kroah-Hartman , Rhett Aultman 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: <20220609204714.2715188-1-rhett.aultman@samsara.com> MIME-Version: 1.0 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: X-Mailing-List: linux-usb@vger.kernel.org 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 --- drivers/net/can/usb/gs_usb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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);