[PATCH 3/5] mtd: maps: physmap: fix reference leak on failed device registration
Vastargazing
vebohr at gmail.com
Mon May 4 03:08:45 PDT 2026
When platform_device_register() fails in physmap_init(), the embedded
struct device has already been initialized by device_initialize() inside
platform_device_register(). The error path unregisters the driver but
returns without dropping the device reference:
physmap_init()
-> platform_device_register(&physmap_flash)
-> device_initialize(&physmap_flash.dev) /* kref = 1 */
-> platform_device_add(&physmap_flash) /* fails */
<- platform_driver_unregister() called, but kref still 1
Per platform_device_register() kernel-doc:
NOTE: _Never_ directly free @pdev after calling this function, even if
it returned an error! Always use platform_device_put() to give up the
reference initialised in this function instead.
Fix this by calling platform_device_put() before unregistering the driver.
Fixes: 73566edf9b91 ("[MTD] Convert physmap to platform driver")
Cc: stable at vger.kernel.org
Assisted-by: GitHub Copilot (Claude Sonnet 4.5)
Signed-off-by: Vastargazing <vebohr at gmail.com>
---
drivers/mtd/maps/physmap-core.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/maps/physmap-core.c b/drivers/mtd/maps/physmap-core.c
index dcda7685fc99..45d79ca622c1 100644
--- a/drivers/mtd/maps/physmap-core.c
+++ b/drivers/mtd/maps/physmap-core.c
@@ -654,8 +654,10 @@ static int __init physmap_init(void)
#ifdef CONFIG_MTD_PHYSMAP_COMPAT
if (err == 0) {
err = platform_device_register(&physmap_flash);
- if (err)
+ if (err) {
+ platform_device_put(&physmap_flash);
platform_driver_unregister(&physmap_flash_driver);
+ }
}
#endif
--
2.51.0
More information about the linux-mtd
mailing list