public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Guangshuo Li <lgs201920130244@gmail.com>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	"Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
	Guangshuo Li <lgs201920130244@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Stas Sergeev <stsp@aknet.ru>,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH] x86/rtc: fix failed fallback RTC device registration handling
Date: Thu, 16 Apr 2026 03:34:55 +0800	[thread overview]
Message-ID: <20260415193455.3869807-1-lgs201920130244@gmail.com> (raw)

When platform_device_register() fails in add_rtc_cmos(), the embedded
struct device in rtc_device has already been initialized by
device_initialize(), but the failure path ignores the error without
dropping the device reference for the current platform device:

  add_rtc_cmos()
    -> platform_device_register(&rtc_device)
       -> device_initialize(&rtc_device.dev)
       -> setup_pdev_dma_masks(&rtc_device)
       -> platform_device_add(&rtc_device)

This leads to a reference leak when platform_device_register() fails.
It also causes add_rtc_cmos() to report success unconditionally and log
that the fallback platform RTC device was registered even when the
registration failed.

Fix this by checking the return value, calling platform_device_put() on
failure, and only printing the success message after successful
registration.

The issue was identified by a static analysis tool I developed and
confirmed by manual review.

Fixes: 1da2e3d679a8e ("provide rtc_cmos platform device")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 arch/x86/kernel/rtc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/rtc.c b/arch/x86/kernel/rtc.c
index 314b062a15de..4761c5b0234f 100644
--- a/arch/x86/kernel/rtc.c
+++ b/arch/x86/kernel/rtc.c
@@ -139,7 +139,11 @@ static __init int add_rtc_cmos(void)
 	if (!x86_platform.legacy.rtc)
 		return -ENODEV;
 
-	platform_device_register(&rtc_device);
+	ret = platform_device_register(&rtc_device);
+	if (ret) {
+		platform_device_put(&rtc_device);
+		return ret;
+	}
 	dev_info(&rtc_device.dev, "registered fallback platform RTC device\n");
 
 	return 0;
-- 
2.43.0


             reply	other threads:[~2026-04-15 19:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-15 19:34 Guangshuo Li [this message]
2026-04-29 22:39 ` [PATCH] x86/rtc: fix failed fallback RTC device registration handling kernel test robot
2026-04-29 22:40 ` kernel test robot
2026-04-30  5:18   ` Guangshuo Li

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=20260415193455.3869807-1-lgs201920130244@gmail.com \
    --to=lgs201920130244@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rafael.j.wysocki@intel.com \
    --cc=stable@vger.kernel.org \
    --cc=stsp@aknet.ru \
    --cc=tglx@kernel.org \
    --cc=x86@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