From mboxrd@z Thu Jan 1 00:00:00 1970 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 B1E2BC433F5 for ; Fri, 11 Feb 2022 21:38:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234237AbiBKViz (ORCPT ); Fri, 11 Feb 2022 16:38:55 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:52642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233490AbiBKViy (ORCPT ); Fri, 11 Feb 2022 16:38:54 -0500 X-Greylist: delayed 65 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 11 Feb 2022 13:38:52 PST Received: from smtp1.axis.com (smtp1.axis.com [195.60.68.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30E82C62 for ; Fri, 11 Feb 2022 13:38:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1644615532; x=1676151532; h=date:to:cc:subject:message-id:references:mime-version: content-transfer-encoding:in-reply-to:from; bh=0J3T62tHpNbnzNp1hgUrzls4YpC9l9dNSsSpldfxStE=; b=n3vDiJRd+1srXmMX6/el3rHkcrjlYl2bWrVtV9PuPJYPYBHxxkk24m8o KneX+LFUQ+0Fzgxj2p8hIz8CZMrlHdBuPIyhp7sZJqfonO5GDqiq7ZS/R B1M+hMTS6znCSgKQ4JxVuUKe54H0PUkeWwvxikQfbXgek4JK5RJUqXVji cBv63yLYTUoBlFww8nvh92Vi8+03Je1aCQVusO3wSOUoOQObckPoQMz8W s4u5O4GB6/JJ4n9SWfOjeB3AzVeqiJY9D+XALbZ+k4ORhS+JimwM8mM6F 6OdXPXHMxjR2XXyzguavA9lkioTNg+TRvfjHBNXiLNCgLQfxhOKt6TaeW Q==; Date: Fri, 11 Feb 2022 22:37:45 +0100 To: Rob Herring CC: =?iso-8859-1?Q?M=E5rten?= Lindahl , Frank Rowand , kernel , "devicetree@vger.kernel.org" Subject: Re: [PATCH] of: dma_configure: Free old DMA map range Message-ID: References: <20220204084556.3033351-1-marten.lindahl@axis.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: From: Marten Lindahl Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org On Fri, Feb 11, 2022 at 03:48:06PM +0100, Rob Herring wrote: > On Fri, Feb 04, 2022 at 09:45:56AM +0100, Mårten Lindahl wrote: > > When unbinding/binding a driver with DMA mapped memory, the DMA map is > > not freed when 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 = <0x2>; > > #size-cells = <0x2>; > > ranges; > > dma-ranges = <...>; > > ... > > }; > > > > 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 > > > > Prevent overwriting the dma_range_map by freeing it before saving the > > new map. > > > > Signed-off-by: Mårten Lindahl > > --- > > drivers/of/device.c | 6 ++++++ > > 1 file changed, 6 insertions(+) > > > > diff --git a/drivers/of/device.c b/drivers/of/device.c > > index 874f031442dc..a67703d09bfd 100644 > > --- a/drivers/of/device.c > > +++ b/drivers/of/device.c > > @@ -156,6 +156,12 @@ int of_dma_configure_id(struct device *dev, struct device_node *np, > > kfree(map); > > return -EINVAL; > > } > > + > > + /* > > + * Since we are about to set a new range map we should make sure we > > + * do not overwrite any existing one without having freed it first. > > + */ > > + kfree(dev->dma_range_map); > Hi Rob! Thanks for looking at this. > > The map can't change, can't we just reuse the existing map. Or just bail > early? I suppose the driver could have modified the dma masks. Or the > IOMMU config could have changed. Yes, we can reuse the existing map. I will make a check for that instead. Kind regards Mårten > > Rob