Skip to content

Commit ec738ca

Browse files
committed
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 <[email protected]> Cc: Pratyush Yadav <[email protected]> Cc: Miquel Raynal <[email protected]> Cc: Richard Weinberger <[email protected]> Cc: Vignesh Raghavendra <[email protected]> Cc: [email protected] Reviewed-by: Michael Walle <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent fe15c26 commit ec738ca

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

drivers/mtd/spi-nor/core.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3343,7 +3343,19 @@ static struct spi_mem_driver spi_nor_driver = {
33433343
.remove = spi_nor_remove,
33443344
.shutdown = spi_nor_shutdown,
33453345
};
3346-
module_spi_mem_driver(spi_nor_driver);
3346+
3347+
static int __init spi_nor_module_init(void)
3348+
{
3349+
return spi_mem_driver_register(&spi_nor_driver);
3350+
}
3351+
module_init(spi_nor_module_init);
3352+
3353+
static void __exit spi_nor_module_exit(void)
3354+
{
3355+
spi_mem_driver_unregister(&spi_nor_driver);
3356+
spi_nor_debugfs_shutdown();
3357+
}
3358+
module_exit(spi_nor_module_exit);
33473359

33483360
MODULE_LICENSE("GPL v2");
33493361
MODULE_AUTHOR("Huang Shijie <[email protected]>");

drivers/mtd/spi-nor/core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,10 @@ static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
711711

712712
#ifdef CONFIG_DEBUG_FS
713713
void spi_nor_debugfs_register(struct spi_nor *nor);
714+
void spi_nor_debugfs_shutdown(void);
714715
#else
715716
static inline void spi_nor_debugfs_register(struct spi_nor *nor) {}
717+
static inline void spi_nor_debugfs_shutdown(void) {}
716718
#endif
717719

718720
#endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */

drivers/mtd/spi-nor/debugfs.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ static void spi_nor_debugfs_unregister(void *data)
226226
nor->debugfs_root = NULL;
227227
}
228228

229+
static struct dentry *rootdir;
230+
229231
void spi_nor_debugfs_register(struct spi_nor *nor)
230232
{
231-
struct dentry *rootdir, *d;
233+
struct dentry *d;
232234
int ret;
233235

234-
/* Create rootdir once. Will never be deleted again. */
235-
rootdir = debugfs_lookup(SPI_NOR_DEBUGFS_ROOT, NULL);
236236
if (!rootdir)
237237
rootdir = debugfs_create_dir(SPI_NOR_DEBUGFS_ROOT, NULL);
238238

@@ -247,3 +247,8 @@ void spi_nor_debugfs_register(struct spi_nor *nor)
247247
debugfs_create_file("capabilities", 0444, d, nor,
248248
&spi_nor_capabilities_fops);
249249
}
250+
251+
void spi_nor_debugfs_shutdown(void)
252+
{
253+
debugfs_remove(rootdir);
254+
}

0 commit comments

Comments
 (0)