[PATCH 1/3] alarmtimer: Drop the spinlock around the rtcdev variable
From: Thomas Weißschuh
Date: Mon Nov 02 2026 - 01:15:27 EST
The spinlock serves multiple purposes. It serializes calls to
alarmtimer_rtc_add_device(), making sure the 'rtcdev' variable is
written only once. It also makes the variables values is read in a
consistent state in alarmtimer_get_rtcdev().
But alarmtimer_rtc_add_device() is already synchronized externally
through the driver core's subsystem lock. So additional spinlock can be
dropped at replaced with WRITE_ONCE()/READ_ONCE() for consistent access.
This removes lock/unlock operations from the alarmtimer's clock
callbacks.
Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
kernel/time/alarmtimer.c | 24 ++++++++----------------
1 file changed, 8 insertions(+), 16 deletions(-)
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index ea5be5870e51..e60c626e326b 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -61,7 +61,6 @@ static DEFINE_SPINLOCK(freezer_delta_lock);
/* rtc timer and device for setting alarm wakeups at suspend */
static struct rtc_timer rtctimer;
static struct rtc_device *rtcdev;
-static DEFINE_SPINLOCK(rtcdev_lock);
/**
* alarmtimer_get_rtcdev - Return selected rtcdevice
@@ -70,12 +69,7 @@ static DEFINE_SPINLOCK(rtcdev_lock);
*/
struct rtc_device *alarmtimer_get_rtcdev(void)
{
- struct rtc_device *ret;
-
- guard(spinlock_irqsave)(&rtcdev_lock);
- ret = rtcdev;
-
- return ret;
+ return READ_ONCE(rtcdev);
}
EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
@@ -98,15 +92,13 @@ static int alarmtimer_rtc_add_device(struct device *dev)
if (!IS_ERR(pdev))
device_init_wakeup(&pdev->dev, true);
- scoped_guard(spinlock_irqsave, &rtcdev_lock) {
- if (!IS_ERR(pdev) && !rtcdev && try_module_get(rtc->owner)) {
- rtcdev = rtc;
- /* hold a reference so it doesn't go away */
- get_device(dev);
- pdev = NULL;
- } else {
- ret = -1;
- }
+ if (!IS_ERR(pdev) && try_module_get(rtc->owner)) {
+ WRITE_ONCE(rtcdev, rtc);
+ /* hold a reference so it doesn't go away */
+ get_device(dev);
+ pdev = NULL;
+ } else {
+ ret = -1;
}
platform_device_unregister(pdev);
--
2.56.0