[PATCH] regulator: wm8400: fix reference leak on failed device registration
From: Guangshuo Li
Date: Wed Apr 15 2026 - 14:14:16 EST
When platform_device_register() fails in wm8400_register_regulator(),
the embedded struct device in wm8400->regulators[reg] has already been
initialized by device_initialize(), but the failure path returns the
error without dropping the device reference for the current platform
device:
wm8400_register_regulator()
-> platform_device_register(&wm8400->regulators[reg])
-> device_initialize(&wm8400->regulators[reg].dev)
-> setup_pdev_dma_masks(&wm8400->regulators[reg])
-> platform_device_add(&wm8400->regulators[reg])
This leads to a reference leak when platform_device_register() fails.
Fix this by calling platform_device_put() before returning the error.
The issue was identified by a static analysis tool I developed and
confirmed by manual review.
Fixes: 42fad570b6662 ("regulator: Add WM8400 regulator support")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
---
drivers/regulator/wm8400-regulator.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/regulator/wm8400-regulator.c b/drivers/regulator/wm8400-regulator.c
index fb3ca7956d00..5849bb051d2a 100644
--- a/drivers/regulator/wm8400-regulator.c
+++ b/drivers/regulator/wm8400-regulator.c
@@ -243,6 +243,7 @@ int wm8400_register_regulator(struct device *dev, int reg,
struct regulator_init_data *initdata)
{
struct wm8400 *wm8400 = dev_get_drvdata(dev);
+ int ret;
if (wm8400->regulators[reg].name)
return -EBUSY;
@@ -254,7 +255,12 @@ int wm8400_register_regulator(struct device *dev, int reg,
wm8400->regulators[reg].dev.parent = dev;
wm8400->regulators[reg].dev.platform_data = initdata;
- return platform_device_register(&wm8400->regulators[reg]);
+ ret = platform_device_register(&wm8400->regulators[reg]);
+ if (ret)
+ platform_device_put(&wm8400->regulators[reg]);
+
+ return ret;
+
}
EXPORT_SYMBOL_GPL(wm8400_register_regulator);
--
2.43.0