[PATCH] mtd: maps: physmap: fix reference leak on failed device registration

From: Guangshuo Li

Date: Wed Apr 15 2026 - 13:08:21 EST


When platform_device_register() fails in physmap_init(), the embedded
struct device in physmap_flash has already been initialized by
device_initialize(), but the failure path only unregisters the platform
driver and does not drop the device reference for the current platform
device:

physmap_init()
-> platform_device_register(&physmap_flash)
-> device_initialize(&physmap_flash.dev)
-> setup_pdev_dma_masks(&physmap_flash)
-> platform_device_add(&physmap_flash)

This leads to a reference leak when platform_device_register() fails.
Fix this by calling platform_device_put() before unregistering the
platform driver.

The issue was identified by a static analysis tool I developed and
confirmed by manual review.

Fixes: 1ca5d2f0196cf ("mtd/maps/physmap: catch failure to register MTD_PHYSMAP_COMPAT device")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
---
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 0dcc25b7ff98..6299a741e65b 100644
--- a/drivers/mtd/maps/physmap-core.c
+++ b/drivers/mtd/maps/physmap-core.c
@@ -659,8 +659,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.43.0