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 5BE84C4332F for ; Tue, 22 Feb 2022 07:41:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229812AbiBVHlS (ORCPT ); Tue, 22 Feb 2022 02:41:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbiBVHlO (ORCPT ); Tue, 22 Feb 2022 02:41:14 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EFD2128589 for ; Mon, 21 Feb 2022 23:40:48 -0800 (PST) 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 sin.source.kernel.org (Postfix) with ESMTPS id 57F05CE12FE for ; Tue, 22 Feb 2022 07:40:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 440B9C340E8; Tue, 22 Feb 2022 07:40:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645515645; bh=W2j8A9jc87ifeEDwebVUMF/iX4lmFyZxax0K5tYjNFo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=qwOMP8C9Yl/c0BR6UKgAJY3K0w4qpyyt6OP7KB24Jsyt7yWSoi6jOhkUboKMNciqx Ha38MZheNrSAuJAyvIGwjNlKdvmZbydzySt3iKZG/qkwhV7DCsOoXzF0MtFqMwF5rd f0nlNzmoLPXYypP0UhJrJ4P5CxdI3TEEPZDtWTLY= Date: Tue, 22 Feb 2022 08:40:42 +0100 From: Greg Kroah-Hartman To: Rob Herring Cc: =?iso-8859-1?Q?M=E5rten?= Lindahl , "Rafael J. Wysocki" , kernel , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] driver core: Free DMA range map when device is released Message-ID: References: <20220216094128.4025861-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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 16, 2022 at 09:45:09AM -0600, Rob Herring wrote: > On Wed, Feb 16, 2022 at 3:42 AM Mårten Lindahl wrote: > > > > 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 = <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 > > > > To prevent this we should free the dma_range_map when the device is > > released. > > > > Signed-off-by: Mårten Lindahl > > Suggested-by: Rob Herring > Fixes: e0d072782c73 ("dma-mapping: introduce DMA range map, > supplanting dma_pfn_offset") > Reviewed-by: Rob Herring > > I've got a follow-up patch to refactor the 3 occurrences of this same > deinit code. Now queued up, so feel free to send the follow-up patch too. thanks, greg k-h