Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 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>

+23 -4
+13 -1
drivers/mtd/spi-nor/core.c
··· 3343 3343 .remove = spi_nor_remove, 3344 3344 .shutdown = spi_nor_shutdown, 3345 3345 }; 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); 3347 3359 3348 3360 MODULE_LICENSE("GPL v2"); 3349 3361 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
+2
drivers/mtd/spi-nor/core.h
··· 711 711 712 712 #ifdef CONFIG_DEBUG_FS 713 713 void spi_nor_debugfs_register(struct spi_nor *nor); 714 + void spi_nor_debugfs_shutdown(void); 714 715 #else 715 716 static inline void spi_nor_debugfs_register(struct spi_nor *nor) {} 717 + static inline void spi_nor_debugfs_shutdown(void) {} 716 718 #endif 717 719 718 720 #endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */
+8 -3
drivers/mtd/spi-nor/debugfs.c
··· 226 226 nor->debugfs_root = NULL; 227 227 } 228 228 229 + static struct dentry *rootdir; 230 + 229 231 void spi_nor_debugfs_register(struct spi_nor *nor) 230 232 { 231 - struct dentry *rootdir, *d; 233 + struct dentry *d; 232 234 int ret; 233 235 234 - /* Create rootdir once. Will never be deleted again. */ 235 - rootdir = debugfs_lookup(SPI_NOR_DEBUGFS_ROOT, NULL); 236 236 if (!rootdir) 237 237 rootdir = debugfs_create_dir(SPI_NOR_DEBUGFS_ROOT, NULL); 238 238 ··· 246 246 debugfs_create_file("params", 0444, d, nor, &spi_nor_params_fops); 247 247 debugfs_create_file("capabilities", 0444, d, nor, 248 248 &spi_nor_capabilities_fops); 249 + } 250 + 251 + void spi_nor_debugfs_shutdown(void) 252 + { 253 + debugfs_remove(rootdir); 249 254 }