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

Guangshuo Li posted 1 patch 2 months ago
drivers/mtd/maps/physmap-core.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] mtd: maps: physmap: fix reference leak on failed device registration
Posted by Guangshuo Li 2 months ago
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@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@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 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
Re: [PATCH] mtd: maps: physmap: fix reference leak on failed device registration
Posted by Guangshuo Li 1 month, 3 weeks ago
Hi,

Please disregard this patch.

On Thu, 16 Apr 2026 at 01:00, Guangshuo Li <lgs201920130244@gmail.com> wrote:
>
> 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@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@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 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
>

After re-checking it, physmap_flash is a static platform_device and it
does not provide a dev.release callback. Therefore calling
platform_device_put() on the platform_device_register() failure path is
not appropriate here and can trigger the missing release callback
warning.

This falls into the same static platform_device pattern pointed out in
the other reviews, so I will drop this patch.

Sorry for the noise.

Best regards,
Guangshuo Li