[PATCH 2/5] platform/chrome: cros_ec_lpc: fix reference leak on failed device registration
From: Vastargazing
Date: Mon May 04 2026 - 06:11:48 EST
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);
}
}
--
2.51.0