Commit ec738ca1 authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

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.  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: default avatarMichael Walle <michael@walle.cc>
Link: https://lore.kernel.org/r/20230208160230.2179905-1-gregkh@linuxfoundation.orgSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent fe15c26e
...@@ -3343,7 +3343,19 @@ static struct spi_mem_driver spi_nor_driver = { ...@@ -3343,7 +3343,19 @@ static struct spi_mem_driver spi_nor_driver = {
.remove = spi_nor_remove, .remove = spi_nor_remove,
.shutdown = spi_nor_shutdown, .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_LICENSE("GPL v2");
MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>"); MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
......
...@@ -711,8 +711,10 @@ static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd) ...@@ -711,8 +711,10 @@ static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
#ifdef CONFIG_DEBUG_FS #ifdef CONFIG_DEBUG_FS
void spi_nor_debugfs_register(struct spi_nor *nor); void spi_nor_debugfs_register(struct spi_nor *nor);
void spi_nor_debugfs_shutdown(void);
#else #else
static inline void spi_nor_debugfs_register(struct spi_nor *nor) {} static inline void spi_nor_debugfs_register(struct spi_nor *nor) {}
static inline void spi_nor_debugfs_shutdown(void) {}
#endif #endif
#endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */ #endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */
...@@ -226,13 +226,13 @@ static void spi_nor_debugfs_unregister(void *data) ...@@ -226,13 +226,13 @@ static void spi_nor_debugfs_unregister(void *data)
nor->debugfs_root = NULL; nor->debugfs_root = NULL;
} }
static struct dentry *rootdir;
void spi_nor_debugfs_register(struct spi_nor *nor) void spi_nor_debugfs_register(struct spi_nor *nor)
{ {
struct dentry *rootdir, *d; struct dentry *d;
int ret; int ret;
/* Create rootdir once. Will never be deleted again. */
rootdir = debugfs_lookup(SPI_NOR_DEBUGFS_ROOT, NULL);
if (!rootdir) if (!rootdir)
rootdir = debugfs_create_dir(SPI_NOR_DEBUGFS_ROOT, NULL); rootdir = debugfs_create_dir(SPI_NOR_DEBUGFS_ROOT, NULL);
...@@ -247,3 +247,8 @@ void spi_nor_debugfs_register(struct spi_nor *nor) ...@@ -247,3 +247,8 @@ void spi_nor_debugfs_register(struct spi_nor *nor)
debugfs_create_file("capabilities", 0444, d, nor, debugfs_create_file("capabilities", 0444, d, nor,
&spi_nor_capabilities_fops); &spi_nor_capabilities_fops);
} }
void spi_nor_debugfs_shutdown(void)
{
debugfs_remove(rootdir);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment