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

Guangshuo Li posted 1 patch 2 months ago
arch/x86/kernel/rtc.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH] x86/rtc: fix failed fallback RTC device registration handling
Posted by Guangshuo Li 2 months ago
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
Re: [PATCH] x86/rtc: fix failed fallback RTC device registration handling
Posted by kernel test robot 1 month, 2 weeks ago
Hi Guangshuo,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/master]
[also build test ERROR on linus/master v7.1-rc1 next-20260429]
[cannot apply to tip/auto-latest tip/x86/core bp/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Guangshuo-Li/x86-rtc-fix-failed-fallback-RTC-device-registration-handling/20260416-130623
base:   tip/master
patch link:    https://lore.kernel.org/r/20260415193455.3869807-1-lgs201920130244%40gmail.com
patch subject: [PATCH] x86/rtc: fix failed fallback RTC device registration handling
config: i386-allnoconfig (https://download.01.org/0day-ci/archive/20260430/202604300638.5ysk7jy1-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260430/202604300638.5ysk7jy1-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202604300638.5ysk7jy1-lkp@intel.com/

All errors (new ones prefixed by >>):

   arch/x86/kernel/rtc.c: In function 'add_rtc_cmos':
>> arch/x86/kernel/rtc.c:142:9: error: 'ret' undeclared (first use in this function); did you mean 'net'?
     142 |         ret = platform_device_register(&rtc_device);
         |         ^~~
         |         net
   arch/x86/kernel/rtc.c:142:9: note: each undeclared identifier is reported only once for each function it appears in


vim +142 arch/x86/kernel/rtc.c

   133	
   134	static __init int add_rtc_cmos(void)
   135	{
   136		if (cmos_rtc_platform_device_present)
   137			return 0;
   138	
   139		if (!x86_platform.legacy.rtc)
   140			return -ENODEV;
   141	
 > 142		ret = platform_device_register(&rtc_device);

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] x86/rtc: fix failed fallback RTC device registration handling
Posted by Guangshuo Li 1 month, 2 weeks ago
Hi,

Please drop this patch.

On Thu, 30 Apr 2026 at 06:41, kernel test robot <lkp@intel.com> wrote:
>
> Hi Guangshuo,
>
> kernel test robot noticed the following build errors:
>
> [auto build test ERROR on tip/master]
> [also build test ERROR on linus/master v7.1-rc1 next-20260429]
> [cannot apply to tip/auto-latest tip/x86/core bp/for-next]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
>
> url:    https://github.com/intel-lab-lkp/linux/commits/Guangshuo-Li/x86-rtc-fix-failed-fallback-RTC-device-registration-handling/20260416-130623
> base:   tip/master
> patch link:    https://lore.kernel.org/r/20260415193455.3869807-1-lgs201920130244%40gmail.com
> patch subject: [PATCH] x86/rtc: fix failed fallback RTC device registration handling
> config: i386-allnoconfig (https://download.01.org/0day-ci/archive/20260430/202604300638.5ysk7jy1-lkp@intel.com/config)
> compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260430/202604300638.5ysk7jy1-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202604300638.5ysk7jy1-lkp@intel.com/
>
> All errors (new ones prefixed by >>):
>
>    arch/x86/kernel/rtc.c: In function 'add_rtc_cmos':
> >> arch/x86/kernel/rtc.c:142:9: error: 'ret' undeclared (first use in this function); did you mean 'net'?
>      142 |         ret = platform_device_register(&rtc_device);
>          |         ^~~
>          |         net
>    arch/x86/kernel/rtc.c:142:9: note: each undeclared identifier is reported only once for each function it appears in
>
>
> vim +142 arch/x86/kernel/rtc.c
>
>    133
>    134  static __init int add_rtc_cmos(void)
>    135  {
>    136          if (cmos_rtc_platform_device_present)
>    137                  return 0;
>    138
>    139          if (!x86_platform.legacy.rtc)
>    140                  return -ENODEV;
>    141
>  > 142          ret = platform_device_register(&rtc_device);
>
> --
> 0-DAY CI Kernel Test Service
> https://github.com/intel/lkp-tests/wiki

After re-checking the code, rtc_device is a static platform_device and it
does not provide a dev.release callback. Therefore calling
platform_device_put() on the platform_device_register() failure path is
not appropriate here and can trigger the missing release callback warning.

The build failure reported by the kernel test robot is only due to the
missing ret declaration, but the patch itself is not correct because of
the static platform_device lifetime issue.

Sorry for the noise.

Thanks,
Guangshuo
Re: [PATCH] x86/rtc: fix failed fallback RTC device registration handling
Posted by kernel test robot 1 month, 2 weeks ago
Hi Guangshuo,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/master]
[also build test ERROR on linus/master v7.1-rc1 next-20260429]
[cannot apply to tip/auto-latest tip/x86/core bp/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Guangshuo-Li/x86-rtc-fix-failed-fallback-RTC-device-registration-handling/20260416-130623
base:   tip/master
patch link:    https://lore.kernel.org/r/20260415193455.3869807-1-lgs201920130244%40gmail.com
patch subject: [PATCH] x86/rtc: fix failed fallback RTC device registration handling
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20260430/202604300652.WxTbaLDu-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260430/202604300652.WxTbaLDu-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202604300652.WxTbaLDu-lkp@intel.com/

All errors (new ones prefixed by >>):

>> arch/x86/kernel/rtc.c:142:2: error: use of undeclared identifier 'ret'
     142 |         ret = platform_device_register(&rtc_device);
         |         ^
   arch/x86/kernel/rtc.c:143:6: error: use of undeclared identifier 'ret'
     143 |         if (ret) {
         |             ^
   arch/x86/kernel/rtc.c:145:10: error: use of undeclared identifier 'ret'
     145 |                 return ret;
         |                        ^
   3 errors generated.


vim +/ret +142 arch/x86/kernel/rtc.c

   133	
   134	static __init int add_rtc_cmos(void)
   135	{
   136		if (cmos_rtc_platform_device_present)
   137			return 0;
   138	
   139		if (!x86_platform.legacy.rtc)
   140			return -ENODEV;
   141	
 > 142		ret = platform_device_register(&rtc_device);

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki