[PATCH] platform/chrome: cros_ec_lpc: fix reference leak on failed device registration

Guangshuo Li posted 1 patch 2 months ago
drivers/platform/chrome/cros_ec_lpc.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] platform/chrome: cros_ec_lpc: fix reference leak on failed device registration
Posted by Guangshuo Li 2 months ago
When platform_device_register() fails in cros_ec_lpc_init(), the
embedded struct device in cros_ec_lpc_device has already been
initialized by device_initialize(), but the failure path only reports
the error and unregisters the platform driver without dropping the
device reference for the current platform device:

  cros_ec_lpc_init()
    -> platform_device_register(&cros_ec_lpc_device)
       -> device_initialize(&cros_ec_lpc_device.dev)
       -> setup_pdev_dma_masks(&cros_ec_lpc_device)
       -> platform_device_add(&cros_ec_lpc_device)

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: 5f454bdf63536 ("platform/chrome: cros_ec_lpc: Register the driver if ACPI entry is missing.")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/platform/chrome/cros_ec_lpc.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/platform/chrome/cros_ec_lpc.c b/drivers/platform/chrome/cros_ec_lpc.c
index 78cfff80cdea..cb3ff76d29e9 100644
--- a/drivers/platform/chrome/cros_ec_lpc.c
+++ b/drivers/platform/chrome/cros_ec_lpc.c
@@ -892,6 +892,7 @@ static int __init cros_ec_lpc_init(void)
 		ret = platform_device_register(&cros_ec_lpc_device);
 		if (ret) {
 			pr_err(DRV_NAME ": can't register device: %d\n", ret);
+			platform_device_put(&cros_ec_lpc_device);
 			platform_driver_unregister(&cros_ec_lpc_driver);
 		}
 	}
-- 
2.43.0
Re: [PATCH] platform/chrome: cros_ec_lpc: 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:57, Guangshuo Li <lgs201920130244@gmail.com> wrote:
>
> When platform_device_register() fails in cros_ec_lpc_init(), the
> embedded struct device in cros_ec_lpc_device has already been
> initialized by device_initialize(), but the failure path only reports
> the error and unregisters the platform driver without dropping the
> device reference for the current platform device:
>
>   cros_ec_lpc_init()
>     -> platform_device_register(&cros_ec_lpc_device)
>        -> device_initialize(&cros_ec_lpc_device.dev)
>        -> setup_pdev_dma_masks(&cros_ec_lpc_device)
>        -> platform_device_add(&cros_ec_lpc_device)
>
> 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: 5f454bdf63536 ("platform/chrome: cros_ec_lpc: Register the driver if ACPI entry is missing.")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
>  drivers/platform/chrome/cros_ec_lpc.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/drivers/platform/chrome/cros_ec_lpc.c b/drivers/platform/chrome/cros_ec_lpc.c
> index 78cfff80cdea..cb3ff76d29e9 100644
> --- a/drivers/platform/chrome/cros_ec_lpc.c
> +++ b/drivers/platform/chrome/cros_ec_lpc.c
> @@ -892,6 +892,7 @@ static int __init cros_ec_lpc_init(void)
>                 ret = platform_device_register(&cros_ec_lpc_device);
>                 if (ret) {
>                         pr_err(DRV_NAME ": can't register device: %d\n", ret);
> +                       platform_device_put(&cros_ec_lpc_device);
>                         platform_driver_unregister(&cros_ec_lpc_driver);
>                 }
>         }
> --
> 2.43.0
>

After re-checking it, cros_ec_lpc_device 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