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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 53846C636CC for ; Wed, 8 Feb 2023 16:02:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=+OtFFbTFcUFgkv5pg7t8gJrQGgg4axkMSMamNyzeSmk=; b=JeRn9BqEfNnwqC tHC6xI75PJ/xSGlzNWkvfbLrge4JJzDg20AhzLQGoakkP8hZHRdBocIXZUFOe/vKKV/6AH7ORn6sJ Ev/DJA3U23TwKI0jBBPXeajxBWJs1jnBWOjH7vBcS35MjDP8X9N94MnxzoPKJhrBJsGA5pg5sno2g PB+xDhLrVPP82Mv52l3t/pZXxo8PVWVuWYLP5INOzTbcP6d7e4bJMrubo68vRqRzEJOXQvnKOMkRe 0xJd0QF3U/eDHJPE3s2fbbOWUftgyt2pEc0DnA3tpkEi35lBMMntjlyduHLwy1CNJBYrqS96zgCev K4Qd7yvsJlTdFbmfX4+Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPmu9-00GBTu-Ic; Wed, 08 Feb 2023 16:02:41 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pPmu6-00GBSa-3j for linux-mtd@lists.infradead.org; Wed, 08 Feb 2023 16:02:39 +0000 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 65A46B81D1C; Wed, 8 Feb 2023 16:02:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60543C433EF; Wed, 8 Feb 2023 16:02:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1675872155; bh=o0Ba0J+K3TeR4IycI4k6APl8pW+pR0JuMkeNS8uka64=; h=From:To:Cc:Subject:Date:From; b=AOTDEfp0F8PB7Arihfhk/am8NxmNxPm8qPiM0P2aFJK5XFxp3uBvCZRnZx57B3aKY YY6giOt1ES0SmwTKjHQsYozA3YTjVrD3y68lnEsQclSpV8T2FKYv5kzDCPP5t4B5k5 ZzYMKQyZoTXG8zYmPz6Nls73ZsNV+fxpTZYPOUlQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , Tudor Ambarus , Pratyush Yadav , Michael Walle , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org Subject: [PATCH v4] mtd: spi-nor: fix memory leak when using debugfs_lookup() Date: Wed, 8 Feb 2023 17:02:30 +0100 Message-Id: <20230208160230.2179905-1-gregkh@linuxfoundation.org> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3500; i=gregkh@linuxfoundation.org; h=from:subject; bh=o0Ba0J+K3TeR4IycI4k6APl8pW+pR0JuMkeNS8uka64=; b=owGbwMvMwCRo6H6F97bub03G02pJDMmPj0+pP/3H5uiihxF/WHTUZa9Kb9vwnNfa7LfruQM56mXK mdnHO2JZGASZGGTFFFm+bOM5ur/ikKKXoe1pmDmsTCBDGLg4BWAiet8Y5vAdl+ft+HrpQf/q3VutbR ZPXH1kkTvDgp4M7RrrtUzn44Q1mSKnnXmcs/JGNAA= X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230208_080238_467902_D2C955CC X-CRM114-Status: GOOD ( 17.85 ) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" Errors-To: linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org When calling debugfs_lookup() the result must have dput() called on it, otherwise the memory will leak over time. To solve this, remove the lookup and create the directory on the first device found, and then remove it when the module is unloaded. Cc: Tudor Ambarus Cc: Pratyush Yadav Cc: Michael Walle Cc: Miquel Raynal Cc: Richard Weinberger Cc: Vignesh Raghavendra Cc: linux-mtd@lists.infradead.org Signed-off-by: Greg Kroah-Hartman --- v4: reverse the teardown order when the module is unloaded to ensure that the debugfs directory is still around when the device directory is removed. v3: create the directory the first time it is used, and then remove it if it is present when the module is unloaded. More complex than v2, but correct. v2: fix up to work when module is removed and added, making the fix much simpler. drivers/mtd/spi-nor/core.c | 14 +++++++++++++- drivers/mtd/spi-nor/core.h | 2 ++ drivers/mtd/spi-nor/debugfs.c | 12 +++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c index d67c926bca8b..1452ed6fe5d1 100644 --- a/drivers/mtd/spi-nor/core.c +++ b/drivers/mtd/spi-nor/core.c @@ -3335,7 +3335,19 @@ static struct spi_mem_driver spi_nor_driver = { .remove = spi_nor_remove, .shutdown = spi_nor_shutdown, }; -module_spi_mem_driver(spi_nor_driver); + +static int __init spi_nor_module_init(void) +{ + return spi_mem_driver_register(&spi_nor_driver); +} +module_init(spi_nor_module_init); + +static void __exit spi_nor_module_exit(void) +{ + spi_mem_driver_unregister(&spi_nor_driver); + spi_nor_debugfs_shutdown(); +} +module_exit(spi_nor_module_exit); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("Huang Shijie "); diff --git a/drivers/mtd/spi-nor/core.h b/drivers/mtd/spi-nor/core.h index f03b55cf7e6f..e62cd9964456 100644 --- a/drivers/mtd/spi-nor/core.h +++ b/drivers/mtd/spi-nor/core.h @@ -713,8 +713,10 @@ static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd) #ifdef CONFIG_DEBUG_FS void spi_nor_debugfs_register(struct spi_nor *nor); +void spi_nor_debugfs_shutdown(void); #else static inline void spi_nor_debugfs_register(struct spi_nor *nor) {} +static inline void spi_nor_debugfs_shutdown(void) {} #endif #endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */ diff --git a/drivers/mtd/spi-nor/debugfs.c b/drivers/mtd/spi-nor/debugfs.c index ff895f6758ea..cd9c2dc07509 100644 --- a/drivers/mtd/spi-nor/debugfs.c +++ b/drivers/mtd/spi-nor/debugfs.c @@ -226,13 +226,13 @@ static void spi_nor_debugfs_unregister(void *data) nor->debugfs_root = NULL; } +static struct dentry *rootdir; + void spi_nor_debugfs_register(struct spi_nor *nor) { - struct dentry *rootdir, *d; + struct dentry *d; int ret; - /* Create rootdir once. Will never be deleted again. */ - rootdir = debugfs_lookup(SPI_NOR_DEBUGFS_ROOT, NULL); if (!rootdir) rootdir = debugfs_create_dir(SPI_NOR_DEBUGFS_ROOT, NULL); @@ -247,3 +247,9 @@ void spi_nor_debugfs_register(struct spi_nor *nor) debugfs_create_file("capabilities", 0444, d, nor, &spi_nor_capabilities_fops); } + +void spi_nor_debugfs_shutdown(void) +{ + if (rootdir) + debugfs_remove(rootdir); +} -- 2.39.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/