[PATCH] platform: fix reference leak in platform_add_devices() on register failure

From: Guangshuo Li

Date: Wed Apr 15 2026 - 10:05:53 EST


When platform_device_register() fails in platform_add_devices(), the
embedded struct device in devs[i] has already been initialized by
device_initialize(), but the failure path only unregisters previously
registered platform devices and does not drop the device reference for
the current one.

Previously registered platform devices are cleaned up in the rollback
loop, but the platform device that fails registration is not, leading to
a reference leak.

The issue was identified by a static analysis tool I developed and
confirmed by manual review. Fix this by calling platform_device_put()
for the current platform device before unregistering the previously
registered ones.

Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
---
drivers/base/platform.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 75b4698d0e58..9a4ae2100401 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -548,6 +548,7 @@ int platform_add_devices(struct platform_device **devs, int num)

for (i = 0; i < num; i++) {
ret = platform_device_register(devs[i]);
+ platform_device_put(devs[i]);
if (ret) {
while (--i >= 0)
platform_device_unregister(devs[i]);
--
2.43.0