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

From: Tzung-Bi Shih

Date: Mon May 04 2026 - 22:40:25 EST


On Mon, May 04, 2026 at 01:08:44PM +0300, Vastargazing wrote:
> When platform_device_register() fails in cros_ec_lpc_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:
>
> cros_ec_lpc_init()
> -> platform_device_register(&cros_ec_lpc_device)
> -> device_initialize(&cros_ec_lpc_device.dev) /* kref = 1 */
> -> platform_device_add(&cros_ec_lpc_device) /* 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: 5f454bdf6353 ("platform/chrome: cros_ec_lpc: Register the driver if ACPI entry is missing.")
> Cc: stable@xxxxxxxxxxxxxxx
> Assisted-by: GitHub Copilot (Claude Sonnet 4.5)
> Signed-off-by: Vastargazing <vebohr@xxxxxxxxx>
> ---
> 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);
> }
> }

The patch is identical to [1]. See also [2].

[1] https://lore.kernel.org/chrome-platform/20260415175707.3640225-1-lgs201920130244@xxxxxxxxx/
[2] https://lore.kernel.org/chrome-platform/CANUHTR9_c=msO7mAax=Aj4U--GB=2ozserOvrTU8WueqdSMN8Q@xxxxxxxxxxxxxx/