From: Guangshuo Li <lgs201920130244@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
driver-core@lists.linux.dev, linux-kernel@vger.kernel.org
Cc: Guangshuo Li <lgs201920130244@gmail.com>, stable@vger.kernel.org
Subject: [PATCH] platform: fix reference leak in platform_add_devices() on register failure
Date: Wed, 15 Apr 2026 22:05:26 +0800 [thread overview]
Message-ID: <20260415140526.3290729-1-lgs201920130244@gmail.com> (raw)
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@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
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
reply other threads:[~2026-04-15 14:05 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260415140526.3290729-1-lgs201920130244@gmail.com \
--to=lgs201920130244@gmail.com \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox