[PATCH v2 2/8] alarmtimer: Move the wakeup state into struct rtc_wakeup

From: Igor Velkov via B4 Relay

Date: Sun Nov 15 2026 - 19:25:40 EST


From: Igor Velkov <iav@xxxxxx>

The RTC and the RTC timer which alarmtimer programs on suspend and the
record of the clock_nanosleep() call the freezer interrupted are separate
static variables.

Collect them in one struct to prepare for an RTC per suspend state. No
functional change.

Signed-off-by: Igor Velkov <iav@xxxxxx>
---
kernel/time/alarmtimer.c | 60 +++++++++++++++++++++++++++++-------------------
1 file changed, 36 insertions(+), 24 deletions(-)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 6c663f7d93c2..35630f0ff566 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -50,17 +50,29 @@ static struct alarm_base {
} alarm_bases[ALARM_NUMTYPE];

#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
-/* freezer information to handle clock_nanosleep triggered wakeups */
-static enum alarmtimer_type freezer_alarmtype;
-static ktime_t freezer_expires;
-static ktime_t freezer_delta;
+/**
+ * struct rtc_wakeup - State for waking the system from suspend on alarms
+ * @rtc: RTC programmed on suspend, NULL if none
+ * @timer: RTC timer started on suspend
+ * @freezer_delta: Least remaining time of the clock_nanosleep() calls
+ * which the freezer interrupted, 0 if none
+ * @freezer_expires: Expiry of that call
+ * @freezer_type: Alarm type of that call
+ */
+struct rtc_wakeup {
+ struct rtc_device *rtc;
+ struct rtc_timer timer;
+ ktime_t freezer_delta;
+ ktime_t freezer_expires;
+ enum alarmtimer_type freezer_type;
+};
+
+static struct rtc_wakeup wakeup_ctl;
+
static DEFINE_SPINLOCK(freezer_delta_lock);
#endif

#ifdef CONFIG_RTC_CLASS
-/* rtc timer and device for setting alarm wakeups at suspend */
-static struct rtc_timer rtctimer;
-static struct rtc_device *rtcdev;

/**
* alarmtimer_has_rtcdev - Check whether an RTC can wake the system for alarms
@@ -69,7 +81,7 @@ static struct rtc_device *rtcdev;
*/
bool alarmtimer_has_rtcdev(void)
{
- return !!READ_ONCE(rtcdev);
+ return !!READ_ONCE(wakeup_ctl.rtc);
}
EXPORT_SYMBOL_GPL(alarmtimer_has_rtcdev);

@@ -78,7 +90,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)
struct rtc_device *rtc = to_rtc_device(dev);
struct platform_device *pdev;

- if (rtcdev)
+ if (wakeup_ctl.rtc)
return -EBUSY;

if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
@@ -98,7 +110,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)
return -1;
}

- WRITE_ONCE(rtcdev, rtc);
+ WRITE_ONCE(wakeup_ctl.rtc, rtc);
/* Hold a reference so it doesn't go away */
get_device(dev);

@@ -107,7 +119,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)

static inline void alarmtimer_rtc_timer_init(void)
{
- rtc_timer_init(&rtctimer, NULL, NULL);
+ rtc_timer_init(&wakeup_ctl.timer, NULL, NULL);
}

static struct class_interface alarmtimer_rtc_interface = {
@@ -215,13 +227,13 @@ static int alarmtimer_suspend(struct device *dev)
int i, ret, type;

scoped_guard(spinlock_irqsave, &freezer_delta_lock) {
- min = freezer_delta;
- expires = freezer_expires;
- type = freezer_alarmtype;
- freezer_delta = 0;
+ min = wakeup_ctl.freezer_delta;
+ expires = wakeup_ctl.freezer_expires;
+ type = wakeup_ctl.freezer_type;
+ wakeup_ctl.freezer_delta = 0;
}

- rtc = READ_ONCE(rtcdev);
+ rtc = READ_ONCE(wakeup_ctl.rtc);
/* If we have no rtcdev, just return */
if (!rtc)
return 0;
@@ -258,7 +270,7 @@ static int alarmtimer_suspend(struct device *dev)
trace_alarmtimer_suspend(expires, type);

/* Setup an rtc timer to fire that far in the future */
- rtc_timer_cancel(rtc, &rtctimer);
+ rtc_timer_cancel(rtc, &wakeup_ctl.timer);
rtc_read_time(rtc, &tm);
now = rtc_tm_to_ktime(tm);

@@ -275,7 +287,7 @@ static int alarmtimer_suspend(struct device *dev)
now = ktime_add(now, min);

/* Set alarm, if in the past reject suspend briefly to handle */
- ret = rtc_timer_start(rtc, &rtctimer, now, 0);
+ ret = rtc_timer_start(rtc, &wakeup_ctl.timer, now, 0);
if (ret < 0)
pm_wakeup_event(dev, MSEC_PER_SEC);
return ret;
@@ -285,9 +297,9 @@ static int alarmtimer_resume(struct device *dev)
{
struct rtc_device *rtc;

- rtc = READ_ONCE(rtcdev);
+ rtc = READ_ONCE(wakeup_ctl.rtc);
if (rtc)
- rtc_timer_cancel(rtc, &rtctimer);
+ rtc_timer_cancel(rtc, &wakeup_ctl.timer);
return 0;
}

@@ -462,10 +474,10 @@ static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
delta = ktime_sub(absexp, base->get_ktime());

guard(spinlock_irqsave)(&freezer_delta_lock);
- if (!freezer_delta || (delta < freezer_delta)) {
- freezer_delta = delta;
- freezer_expires = absexp;
- freezer_alarmtype = type;
+ if (!wakeup_ctl.freezer_delta || (delta < wakeup_ctl.freezer_delta)) {
+ wakeup_ctl.freezer_delta = delta;
+ wakeup_ctl.freezer_expires = absexp;
+ wakeup_ctl.freezer_type = type;
}
}


--
2.43.0