Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3119974pxm; Mon, 28 Feb 2022 12:26:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3SFNBKwd3cH3EUzkXn1Zgg38cV4iLkQ4D2t9aQY3ApqCqphyLQLlBiZjjZFw5KCGFosz+ X-Received: by 2002:a17:902:ccd2:b0:14f:8182:96c4 with SMTP id z18-20020a170902ccd200b0014f818296c4mr22645858ple.67.1646080008113; Mon, 28 Feb 2022 12:26:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646080008; cv=none; d=google.com; s=arc-20160816; b=GEwHdUY/xOxpBGgqzh3Gt9CN7IjB/sd3JIr/ddYqyLgcInIoRdN9HE6dQc0qcrceW9 jgDZ5jKZ7zVFFzboklc38Ddr59zhYSJ5RaAdTIVIBtb/rIQgK9uh7K7M4JrS98DwPsEs vSMXqQiPNoZ328PPa3M/Ip0JD9LG5sHBAiNhA5viKAEYXhNQ5W0+adtcDmY3l0z34Z/v xXVjhUvX8oZTAlF3AXjBOmLNxCt+FKnH6iSCdTmkdjk2HtWJdxLq6T9grcSSq70fmsKz coB35isgMs2cfNjTvr2Y89VhKZ8H/UF7xcs5yXxcTI2HfhfiWpwtbMIC5Z+xVTp8s6hA daXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=6srABGoaetBMAkilVKQ1MHNL+j+/ncjHKadNGPdiiLA=; b=SGCTg1w5t/MN3mxxMqaRa7+20TSp6F+4R1xccM4y3izPsf4B461VZtwND192q7Ictl dbDzSvPrBqXVvF8dFomifKkoO/u+4ozL4WIxSWtHIJ5/1NLUug867wXy8Yl+7A8dcrx6 Hc0ZFbflDV5q+ptj/QvMJWQ3lDbUeS7w/oIztrt10hc7pq8+2k3itsfwBHgvUryNzfFy gaqAouZF0hInRAsJX3/yFon6qIDEVxPKIwZ5JN4GBrF4FWEaJGKQZDM8e+Jjp9hvYJ4A df4/PSSQDQTHxMD1b5a2+D1mMNKrdCFH4SdWjf2T8AX89/IXXYa0Zv7xczfF7eIdKFNz 3cWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OGQNgsdE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id t6-20020a170902bc4600b0015157d5812asi5710256plz.447.2022.02.28.12.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 12:26:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OGQNgsdE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 057251EEFB0; Mon, 28 Feb 2022 11:44:43 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240093AbiB1SOg (ORCPT + 99 others); Mon, 28 Feb 2022 13:14:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240828AbiB1SJN (ORCPT ); Mon, 28 Feb 2022 13:09:13 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0E6A5F8E3; Mon, 28 Feb 2022 09:49:09 -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 ams.source.kernel.org (Postfix) with ESMTPS id 9894CB815C3; Mon, 28 Feb 2022 17:48:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE98EC340F0; Mon, 28 Feb 2022 17:48:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646070519; bh=jmCHv4Pcr66fmoJD8G80iizX87HKTRbZ0Vbs/HTggEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OGQNgsdENr1wLa0x+VzzNShzWfQPyZwks+8nbPwq4RzNsrcldc4zahMLJgEs+s9yS UQDV39Z3GvD0dtHTeRu8Ylh9FXcGYkY3RVebLj4v4+X4beRimjrvi5uR8sh5hZTz+R o+TPVhx24h++ejXHIXL+fgUPdQutVbqCYjXXX2No= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Rob Herring , =?UTF-8?q?M=C3=A5rten=20Lindahl?= Subject: [PATCH 5.16 137/164] driver core: Free DMA range map when device is released Date: Mon, 28 Feb 2022 18:24:59 +0100 Message-Id: <20220228172412.413019616@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220228172359.567256961@linuxfoundation.org> References: <20220228172359.567256961@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mårten Lindahl commit d8f7a5484f2188e9af2d9e4e587587d724501b12 upstream. 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. Fixes: e0d072782c73 ("dma-mapping: introduce DMA range map, supplanting dma_pfn_offset") Cc: stable Suggested-by: Rob Herring Reviewed-by: Rob Herring Signed-off-by: Mårten Lindahl Link: https://lore.kernel.org/r/20220216094128.4025861-1-marten.lindahl@axis.com Signed-off-by: Greg Kroah-Hartman --- drivers/base/dd.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -629,6 +629,9 @@ re_probe: drv->remove(dev); devres_release_all(dev); + arch_teardown_dma_ops(dev); + kfree(dev->dma_range_map); + dev->dma_range_map = NULL; driver_sysfs_remove(dev); dev->driver = NULL; dev_set_drvdata(dev, NULL); @@ -1208,6 +1211,8 @@ static void __device_release_driver(stru devres_release_all(dev); arch_teardown_dma_ops(dev); + kfree(dev->dma_range_map); + dev->dma_range_map = NULL; dev->driver = NULL; dev_set_drvdata(dev, NULL); if (dev->pm_domain && dev->pm_domain->dismiss)