mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] alarmtimer: Drop the spinlock around the rtcdev variable
@ 2026-11-02  6:11 Thomas Weißschuh
  2026-11-02  6:11 ` [PATCH 1/3] " Thomas Weißschuh
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2026-11-02  6:11 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, Thomas Weißschuh

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@weissschuh.net>
---
Thomas Weißschuh (3):
      alarmtimer: Drop the spinlock around the rtcdev variable
      alarmtimer: Return early if platform_device_register_data() fails
      alarmtimer: Return early if try_module_get() fails

 kernel/time/alarmtimer.c | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261015-alarmtimer-locking-2e7abe32a23a

Best regards,
--  
Thomas Weißschuh <linux@weissschuh.net>


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

* [PATCH 1/3] alarmtimer: Drop the spinlock around the rtcdev variable
  2026-11-02  6:11 [PATCH 0/3] alarmtimer: Drop the spinlock around the rtcdev variable Thomas Weißschuh
@ 2026-11-02  6:11 ` Thomas Weißschuh
  2026-11-02  6:11 ` [PATCH 2/3] alarmtimer: Return early if platform_device_register_data() fails Thomas Weißschuh
  2026-11-02  6:11 ` [PATCH 3/3] alarmtimer: Return early if try_module_get() fails Thomas Weißschuh
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2026-11-02  6:11 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, Thomas Weißschuh

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@weissschuh.net>
---
 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


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

* [PATCH 2/3] alarmtimer: Return early if platform_device_register_data() fails
  2026-11-02  6:11 [PATCH 0/3] alarmtimer: Drop the spinlock around the rtcdev variable Thomas Weißschuh
  2026-11-02  6:11 ` [PATCH 1/3] " Thomas Weißschuh
@ 2026-11-02  6:11 ` Thomas Weißschuh
  2026-11-02  6:11 ` [PATCH 3/3] alarmtimer: Return early if try_module_get() fails Thomas Weißschuh
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2026-11-02  6:11 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, Thomas Weißschuh

Simplify the logic by returning on error instead of having to handle the
potential error in each following operation.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 kernel/time/alarmtimer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index e60c626e326b..9ab1b053a2fb 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -89,10 +89,12 @@ static int alarmtimer_rtc_add_device(struct device *dev)
 
 	pdev = platform_device_register_data(dev, "alarmtimer",
 					     PLATFORM_DEVID_AUTO, NULL, 0);
-	if (!IS_ERR(pdev))
-		device_init_wakeup(&pdev->dev, true);
+	if (IS_ERR(pdev))
+		return PTR_ERR(pdev);
 
-	if (!IS_ERR(pdev) && try_module_get(rtc->owner)) {
+	device_init_wakeup(&pdev->dev, true);
+
+	if (try_module_get(rtc->owner)) {
 		WRITE_ONCE(rtcdev, rtc);
 		/* hold a reference so it doesn't go away */
 		get_device(dev);

-- 
2.56.0


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

* [PATCH 3/3] alarmtimer: Return early if try_module_get() fails
  2026-11-02  6:11 [PATCH 0/3] alarmtimer: Drop the spinlock around the rtcdev variable Thomas Weißschuh
  2026-11-02  6:11 ` [PATCH 1/3] " Thomas Weißschuh
  2026-11-02  6:11 ` [PATCH 2/3] alarmtimer: Return early if platform_device_register_data() fails Thomas Weißschuh
@ 2026-11-02  6:11 ` Thomas Weißschuh
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2026-11-02  6:11 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, Thomas Weißschuh

Simplify the logic by returning early if try_module_get() fails.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 kernel/time/alarmtimer.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 9ab1b053a2fb..b1ae0d2c6a7d 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -77,7 +77,6 @@ static int alarmtimer_rtc_add_device(struct device *dev)
 {
 	struct rtc_device *rtc = to_rtc_device(dev);
 	struct platform_device *pdev;
-	int ret = 0;
 
 	if (rtcdev)
 		return -EBUSY;
@@ -94,17 +93,16 @@ static int alarmtimer_rtc_add_device(struct device *dev)
 
 	device_init_wakeup(&pdev->dev, true);
 
-	if (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;
+	if (!try_module_get(rtc->owner)) {
+		platform_device_unregister(pdev);
+		return -1;
 	}
 
-	platform_device_unregister(pdev);
-	return ret;
+	WRITE_ONCE(rtcdev, rtc);
+	/* hold a reference so it doesn't go away */
+	get_device(dev);
+
+	return 0;
 }
 
 static inline void alarmtimer_rtc_timer_init(void)

-- 
2.56.0


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

end of thread, other threads:[~2026-11-02  6:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-02  6:11 [PATCH 0/3] alarmtimer: Drop the spinlock around the rtcdev variable Thomas Weißschuh
2026-11-02  6:11 ` [PATCH 1/3] " Thomas Weißschuh
2026-11-02  6:11 ` [PATCH 2/3] alarmtimer: Return early if platform_device_register_data() fails Thomas Weißschuh
2026-11-02  6:11 ` [PATCH 3/3] alarmtimer: Return early if try_module_get() fails Thomas Weißschuh

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

all inboxes | Powered by JetHome®