Skip to contentSkip to conversation

mtd: spi-nor: fix memory leak when using debugfs_lookup()

PATCH Superseded Greg Kroah-Hartman 2 participants 4 messages Sep 2, 2022 – Feb 8, 2023

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: spi-nor: fix memory leak when using debugfs_lookup()

When calling debugfs_lookup() the result must have dput() called on it, otherwise the memory will leak over time. Fix this up to be much simpler logic and only create the root debugfs directory once when the driver is first accessed. That resolves the memory leak and makes things more obvious as to what the intent is.

Cc Tudor Ambarus <[email protected]>
Cc Pratyush Yadav <[email protected]>
Cc Michael Walle <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
Cc stable <[email protected]>
---
 drivers/mtd/spi-nor/debugfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/debugfs.c +2 -2
index df76cb5de3f9..3aab595e82d1 100644
@@ -228,11 +228,11 @@ static void spi_nor_debugfs_unregister(void *data)
 
 void spi_nor_debugfs_register(struct spi_nor *nor)
 {
-	struct dentry *rootdir, *d;
+	static struct dentry *rootdir;
+	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);
 
Signature · 6 lines
2.37.3


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#1 lore raw

Hi,

Am 2022-09-02 15:37, schrieb Greg Kroah-Hartman:

Quoted 25 lines

When calling debugfs_lookup() the result must have dput() called on it, otherwise the memory will leak over time. Fix this up to be much simpler logic and only create the root debugfs directory once when the driver is first accessed. That resolves the memory leak and makes things more obvious as to what the intent is.

Cc Tudor Ambarus <[email protected]>
Cc Pratyush Yadav <[email protected]>
Cc Michael Walle <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
Cc stable <[email protected]>
---
 drivers/mtd/spi-nor/debugfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/debugfs.c +0 -0
index df76cb5de3f9..3aab595e82d1 100644
@@ -228,11 +228,11 @@ static void spi_nor_debugfs_unregister(void 
 *data)

drivers/mtd/spi-nor/debugfs.c static void spi_nor_debugfs_unregister(void · line 236

Quoted 10 lines
drivers/mtd/spi-nor/debugfs.c +2 -2
 
 void spi_nor_debugfs_register(struct spi_nor *nor)
 {
-	struct dentry *rootdir, *d;
+	static struct dentry *rootdir;
+	struct dentry *d;
 	int ret;
 
 	/* Create rootdir once. Will never be deleted again. */
-	rootdir = debugfs_lookup(SPI_NOR_DEBUGFS_ROOT, NULL);

IIRC I had that one and it didn't work with spi-nor as a module. Wouldn't it try to create the root dir twice if you remove the module and load it again?

-michael

drivers/mtd/spi-nor/debugfs.c static void spi_nor_debugfs_unregister(void *data) · line 237

Quoted 2 lines
drivers/mtd/spi-nor/debugfs.c +0 -0
 	if (!rootdir)
 		rootdir = debugfs_create_dir(SPI_NOR_DEBUGFS_ROOT, NULL);

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#2 lore raw
Quoted 36 lines · Michael Walle

Hi,

Am 2022-09-02 15:37, schrieb Greg Kroah-Hartman:

Quoted 33 lines

When calling debugfs_lookup() the result must have dput() called on it, otherwise the memory will leak over time. Fix this up to be much simpler logic and only create the root debugfs directory once when the driver is first accessed. That resolves the memory leak and makes things more obvious as to what the intent is.

Cc Tudor Ambarus <[email protected]>
Cc Pratyush Yadav <[email protected]>
Cc Michael Walle <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
Cc stable <[email protected]>
---
 drivers/mtd/spi-nor/debugfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/debugfs.c +2 -1
index df76cb5de3f9..3aab595e82d1 100644
@@ -228,11 +228,11 @@ static void spi_nor_debugfs_unregister(void *data)
 
 void spi_nor_debugfs_register(struct spi_nor *nor)
 {
-	struct dentry *rootdir, *d;
+	static struct dentry *rootdir;
+	struct dentry *d;
 	int ret;
 
Quoted 6 lines

drivers/mtd/spi-nor/debugfs.c static void spi_nor_debugfs_unregister(void *data) · line 235

Quoted 2 lines
drivers/mtd/spi-nor/debugfs.c +0 -1
 	/* Create rootdir once. Will never be deleted again. */
-	rootdir = debugfs_lookup(SPI_NOR_DEBUGFS_ROOT, NULL);

IIRC I had that one and it didn't work with spi-nor as a module. Wouldn't it try to create the root dir twice if you remove the module and load it again?

Yes it would, that is a use-model I did not consider at all, thanks. I'll rework this.

greg k-h

#3 lore raw
Quoted 44 lines · Greg Kroah-Hartman
Quoted 36 lines · Michael Walle

Hi,

Am 2022-09-02 15:37, schrieb Greg Kroah-Hartman:

Quoted 33 lines

When calling debugfs_lookup() the result must have dput() called on it, otherwise the memory will leak over time. Fix this up to be much simpler logic and only create the root debugfs directory once when the driver is first accessed. That resolves the memory leak and makes things more obvious as to what the intent is.

Cc Tudor Ambarus <[email protected]>
Cc Pratyush Yadav <[email protected]>
Cc Michael Walle <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Richard Weinberger <[email protected]>
Cc Vignesh Raghavendra <[email protected]>
Cc stable <[email protected]>
---
 drivers/mtd/spi-nor/debugfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/debugfs.c +2 -1
index df76cb5de3f9..3aab595e82d1 100644
@@ -228,11 +228,11 @@ static void spi_nor_debugfs_unregister(void *data)
 
 void spi_nor_debugfs_register(struct spi_nor *nor)
 {
-	struct dentry *rootdir, *d;
+	static struct dentry *rootdir;
+	struct dentry *d;
 	int ret;
 
Quoted 6 lines

drivers/mtd/spi-nor/debugfs.c static void spi_nor_debugfs_unregister(void *data) · line 235

Quoted 2 lines
drivers/mtd/spi-nor/debugfs.c +0 -1
 	/* Create rootdir once. Will never be deleted again. */
-	rootdir = debugfs_lookup(SPI_NOR_DEBUGFS_ROOT, NULL);

IIRC I had that one and it didn't work with spi-nor as a module. Wouldn't it try to create the root dir twice if you remove the module and load it again?

Quoted 2 lines

Yes it would, that is a use-model I did not consider at all, thanks. I'll rework this.

v2 with this fixed up now sent, thanks.

greg k-h