public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tudor Ambarus <tudor.ambarus@microchip.com>,
	Pratyush Yadav <pratyush@kernel.org>,
	Miquel Raynal <miquel.raynal@bootlin.com>,
	Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	linux-mtd@lists.infradead.org, Michael Walle <michael@walle.cc>,
	Sasha Levin <sashal@kernel.org>,
	tudor.ambarus@linaro.org
Subject: [PATCH AUTOSEL 6.1 17/17] mtd: spi-nor: fix memory leak when using debugfs_lookup()
Date: Thu,  6 Apr 2023 07:32:11 -0400	[thread overview]
Message-ID: <20230406113211.648424-17-sashal@kernel.org> (raw)
In-Reply-To: <20230406113211.648424-1-sashal@kernel.org>

From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

[ Upstream commit ec738ca127d07ecac6afae36e2880341ec89150e ]

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 <tudor.ambarus@microchip.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Miquel Raynal <miquel.raynal@bootlin.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Vignesh Raghavendra <vigneshr@ti.com>
Cc: linux-mtd@lists.infradead.org
Reviewed-by: Michael Walle <michael@walle.cc>
Link: https://lore.kernel.org/r/20230208160230.2179905-1-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/mtd/spi-nor/core.c    | 14 +++++++++++++-
 drivers/mtd/spi-nor/core.h    |  2 ++
 drivers/mtd/spi-nor/debugfs.c | 11 ++++++++---
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
index cda57cb863089..75e694791d8d9 100644
--- a/drivers/mtd/spi-nor/core.c
+++ b/drivers/mtd/spi-nor/core.c
@@ -3272,7 +3272,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 <shijie8@gmail.com>");
diff --git a/drivers/mtd/spi-nor/core.h b/drivers/mtd/spi-nor/core.h
index d18dafeb020ab..00bf0d0e955a0 100644
--- a/drivers/mtd/spi-nor/core.h
+++ b/drivers/mtd/spi-nor/core.h
@@ -709,8 +709,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 df76cb5de3f93..5f56b23205d8b 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,8 @@ 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)
+{
+	debugfs_remove(rootdir);
+}
-- 
2.39.2


      parent reply	other threads:[~2023-04-06 11:34 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06 11:31 [PATCH AUTOSEL 6.1 01/17] f2fs: Fix f2fs_truncate_partial_nodes ftrace event Sasha Levin
2023-04-06 11:31 ` [PATCH AUTOSEL 6.1 02/17] platform/x86/intel: vsec: Fix a memory leak in intel_vsec_add_aux Sasha Levin
2023-04-06 11:31 ` [PATCH AUTOSEL 6.1 03/17] platform/x86 (gigabyte-wmi): Add support for A320M-S2H V2 Sasha Levin
2023-04-06 11:31 ` [PATCH AUTOSEL 6.1 04/17] selftests: sigaltstack: fix -Wuninitialized Sasha Levin
2023-04-06 11:31 ` [PATCH AUTOSEL 6.1 05/17] scsi: megaraid_sas: Fix fw_crash_buffer_show() Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 06/17] scsi: core: Improve scsi_vpd_inquiry() checks Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 07/17] net: dsa: b53: mmap: add phy ops Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 08/17] platform/x86: gigabyte-wmi: add support for B650 AORUS ELITE AX Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 09/17] s390/ptrace: fix PTRACE_GET_LAST_BREAK error handling Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 10/17] nvme-pci: mark Lexar NM760 as IGNORE_DEV_SUBNQN Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 11/17] drm: buddy_allocator: Fix buddy allocator init on 32-bit systems Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 12/17] drm: test: Fix 32-bit issue in drm_buddy_test Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 13/17] nvme-tcp: fix a possible UAF when failing to allocate an io queue Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 14/17] xen/netback: use same error messages for same errors Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 15/17] platform/x86: gigabyte-wmi: add support for X570S AORUS ELITE Sasha Levin
2023-04-06 11:32 ` [PATCH AUTOSEL 6.1 16/17] platform/x86: asus-nb-wmi: Add quirk_asus_tablet_mode to other ROG Flow X13 models Sasha Levin
2023-04-06 11:32 ` Sasha Levin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230406113211.648424-17-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=michael@walle.cc \
    --cc=miquel.raynal@bootlin.com \
    --cc=pratyush@kernel.org \
    --cc=richard@nod.at \
    --cc=stable@vger.kernel.org \
    --cc=tudor.ambarus@linaro.org \
    --cc=tudor.ambarus@microchip.com \
    --cc=vigneshr@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox