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

Miquel Raynal miquel.raynal at bootlin.com
Mon May 4 06:58:56 PDT 2026


On 04/05/2026 at 13:08:45 +03, Vastargazing <vebohr at gmail.com> wrote:

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

Somehow b4 applied another patch from that series, I'm not sure why. I
just dropped it. Please resend this patch alone.

Also, your SoB line seems to be incorrect, we need a proper name, please
have a loot at the DCO.

Thanks,
Miquèl



More information about the linux-mtd mailing list