From nobody Sun Jun 28 01:52:36 2026 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 A5749C433EF for ; Wed, 16 Feb 2022 09:43:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232394AbiBPJna (ORCPT ); Wed, 16 Feb 2022 04:43:30 -0500 Received: from gmail-smtp-in.l.google.com ([23.128.96.19]:57896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232381AbiBPJnZ (ORCPT ); Wed, 16 Feb 2022 04:43:25 -0500 X-Greylist: delayed 66 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 16 Feb 2022 01:43:09 PST Received: from smtp1.axis.com (smtp1.axis.com [195.60.68.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 750C429412B for ; Wed, 16 Feb 2022 01:43:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1645004589; x=1676540589; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=e0DJx/LBwDBcnKXR7T/m2VbehHGOddblLmaGHQmo1QM=; b=dDKk5rDjYPhma2/qxpiUqbv1/GIY4vba28yGzdfkP532EqzdSmFPh5iB OpV7LVq3AM7aIvuueMNgU9j0eoHHSyC4e55cpCQCYXBCbofnMbe3PA0fx xTWiEVEzutmV4QTaSnY4GWCIVMVM59lqVdd4ueFvIfCt5BgZKiqfR+SRO ztXf9ia6tJKtM2dhDyWraPWaq8RLgkUJvwIC0WM/cc+9LZHW1C9S7vWqQ 9uABs2eQmfCGvibCMgNiqc7hI46SO1m05Otp9YncB9APrAFZnya+DqSi4 ufAFG/qYG3HPe0lowrpvSbcMXsWwyhfLeVsSs6N7w9+hvPt6Hut920Xve Q==; From: =?UTF-8?q?M=C3=A5rten=20Lindahl?= To: Greg Kroah-Hartman , "Rafael J. Wysocki" CC: Rob Herring , , , =?UTF-8?q?M=C3=A5rten=20Lindahl?= Subject: [PATCH] driver core: Free DMA range map when device is released Date: Wed, 16 Feb 2022 10:41:28 +0100 Message-ID: <20220216094128.4025861-1-marten.lindahl@axis.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When unbinding/binding a driver with DMA mapped memory, the DMA map is not freed before the driver is reloaded. This leads to a memory leak when the DMA map is overwritten when reprobing the driver. This can be reproduced with a platform driver having a dma-range: dummy { ... #address-cells =3D <0x2>; #size-cells =3D <0x2>; ranges; dma-ranges =3D <...>; ... }; and then unbinding/binding it: ~# echo soc:dummy >/sys/bus/platform/drivers//unbind DMA map object 0xffffff800b0ae540 still being held by &pdev->dev ~# echo soc:dummy >/sys/bus/platform/drivers//bind ~# echo scan > /sys/kernel/debug/kmemleak ~# cat /sys/kernel/debug/kmemleak unreferenced object 0xffffff800b0ae540 (size 64): comm "sh", pid 833, jiffies 4295174550 (age 2535.352s) hex dump (first 32 bytes): 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 80 00 00 00 00 00 00 00 80 00 00 00 00 ................ backtrace: [] create_object.isra.0+0x108/0x344 [] kmemleak_alloc+0x8c/0xd0 [] __kmalloc+0x440/0x6f0 [] of_dma_get_range+0x124/0x220 [] of_dma_configure_id+0x40/0x2d0 [] platform_dma_configure+0x5c/0xa4 [] really_probe+0x8c/0x514 [] __driver_probe_device+0x9c/0x19c [] device_driver_attach+0x54/0xbc [] bind_store+0xc4/0x120 [] drv_attr_store+0x30/0x44 [] sysfs_kf_write+0x50/0x60 [] kernfs_fop_write_iter+0x124/0x1b4 [] new_sync_write+0xdc/0x160 [] vfs_write+0x23c/0x2a0 [] ksys_write+0x64/0xec To prevent this we should free the dma_range_map when the device is released. Signed-off-by: M=C3=A5rten Lindahl Reviewed-by: Rob Herring Suggested-by: Rob Herring --- drivers/base/dd.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 9eaaff2f556c..f47cab21430f 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -629,6 +629,9 @@ static int really_probe(struct device *dev, struct devi= ce_driver *drv) drv->remove(dev); =20 devres_release_all(dev); + arch_teardown_dma_ops(dev); + kfree(dev->dma_range_map); + dev->dma_range_map =3D NULL; driver_sysfs_remove(dev); dev->driver =3D NULL; dev_set_drvdata(dev, NULL); @@ -1209,6 +1212,8 @@ static void __device_release_driver(struct device *de= v, struct device *parent) =20 devres_release_all(dev); arch_teardown_dma_ops(dev); + kfree(dev->dma_range_map); + dev->dma_range_map =3D NULL; dev->driver =3D NULL; dev_set_drvdata(dev, NULL); if (dev->pm_domain && dev->pm_domain->dismiss) --=20 2.30.2