public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/rtc: fix failed fallback RTC device registration handling
@ 2026-04-15 19:34 Guangshuo Li
  2026-04-29 22:39 ` kernel test robot
  2026-04-29 22:40 ` kernel test robot
  0 siblings, 2 replies; 4+ messages in thread
From: Guangshuo Li @ 2026-04-15 19:34 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Rafael J. Wysocki, Guangshuo Li, Andrew Morton,
	Stas Sergeev, linux-kernel
  Cc: stable

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-04-30  5:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-15 19:34 [PATCH] x86/rtc: fix failed fallback RTC device registration handling Guangshuo Li
2026-04-29 22:39 ` kernel test robot
2026-04-29 22:40 ` kernel test robot
2026-04-30  5:18   ` Guangshuo Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox