mtd: spi-nor: fix memory leak when using debugfs_lookup()
Subsystems (2)
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.
--- drivers/mtd/spi-nor/debugfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/debugfs.c +2 -2
@@ -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/
Hi,
Am 2022-09-02 15:37, schrieb Greg Kroah-Hartman:
Quoted 25 lines
When calling
debugfs_lookup()the result must havedput()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]>Signed-off-by Greg Kroah-Hartman <[email protected]>--- drivers/mtd/spi-nor/debugfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/mtd/spi-nor/debugfs.c +0
-0@@ -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
-2void 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
-0if (!rootdir) rootdir =debugfs_create_dir (SPI_NOR_DEBUGFS_ROOT ,NULL );
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
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 havedput()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]>Signed-off-by Greg Kroah-Hartman <[email protected]>--- drivers/mtd/spi-nor/debugfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/mtd/spi-nor/debugfs.c +2
-1@@ -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
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 havedput()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]>Signed-off-by Greg Kroah-Hartman <[email protected]>--- drivers/mtd/spi-nor/debugfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/mtd/spi-nor/debugfs.c +2
-1@@ -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