[PATCH] x86/rtc: fix failed fallback RTC device registration handling

From: Guangshuo Li

Date: Wed Apr 15 2026 - 15:35:24 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
---
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