From: Igor Velkov via B4 Relay <devnull+iav.iav.lv@kernel.org>
To: John Stultz <jstultz@google.com>,
Thomas Gleixner <tglx@kernel.org>,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Neil Armstrong <neil.armstrong@linaro.org>,
Kevin Hilman <khilman@baylibre.com>
Cc: Stephen Boyd <sboyd@kernel.org>,
Miroslav Lichvar <mlichvar@redhat.com>,
Jerome Brunet <jbrunet@baylibre.com>,
Martin Blumenstingl <martin.blumenstingl@googlemail.com>,
linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-amlogic@lists.infradead.org, linux-kernel@vger.kernel.org,
Igor Velkov <iav@iav.lv>
Subject: [PATCH v2 3/8] alarmtimer: Replace the freezer delta with per type expiry times
Date: Mon, 16 Nov 2026 00:25:02 +0000 [thread overview]
Message-ID: <20261116-b4-alarmtimer-rtc-priority-v2-3-b2c69721e489@iav.lv> (raw)
In-Reply-To: <20261116-b4-alarmtimer-rtc-priority-v2-0-b2c69721e489@iav.lv>
From: Igor Velkov <iav@iav.lv>
When the freezer interrupts an alarm clock_nanosleep(),
alarmtimer_freezerset() records the remaining time, the expiry and the
alarm type and keeps the call with the least remaining time.
alarmtimer_suspend() starts its scan with that.
The remaining time is computed when the task is frozen, so it is stale
when the system suspends, and a clock_settime() in between is not taken
into account for REALTIME. A remaining time of exactly 0 counts as no
timer.
Record the earliest expiry per alarm type instead, KTIME_MAX for none, and
compare it with the queue head of its base in alarmtimer_suspend(), so the
remaining time is computed when it is used. The scan also takes KTIME_MAX
instead of 0 as no timer.
Signed-off-by: Igor Velkov <iav@iav.lv>
---
Unlike your sketch and what I wrote, this keeps the frozen expiry per
type and compares it with the queue head in the suspend scan instead of
queueing alarms: such an alarm would exist only to be read back, and if
it fires in between, alarmtimer_fired() drops it and suspend proceeds
without the wakeup. A version with alarms came out 44 lines longer.
---
kernel/time/alarmtimer.c | 75 ++++++++++++++++++------------------------------
1 file changed, 28 insertions(+), 47 deletions(-)
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 35630f0ff566..066d980fa6d5 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -54,22 +54,20 @@ static struct alarm_base {
* 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
+ * @freezer_expires: Earliest expiry per alarm type of the clock_nanosleep()
+ * calls which the freezer interrupted, KTIME_MAX if none
*/
struct rtc_wakeup {
struct rtc_device *rtc;
struct rtc_timer timer;
- ktime_t freezer_delta;
- ktime_t freezer_expires;
- enum alarmtimer_type freezer_type;
+ ktime_t freezer_expires[ALARM_NUMTYPE];
};
-static struct rtc_wakeup wakeup_ctl;
+static struct rtc_wakeup wakeup_ctl = {
+ .freezer_expires = { [0 ... ALARM_NUMTYPE - 1] = KTIME_MAX },
+};
-static DEFINE_SPINLOCK(freezer_delta_lock);
+static DEFINE_SPINLOCK(freezer_lock);
#endif
#ifdef CONFIG_RTC_CLASS
@@ -221,28 +219,22 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining);
*/
static int alarmtimer_suspend(struct device *dev)
{
- ktime_t min, now, expires;
+ ktime_t min = KTIME_MAX, expires = 0, now;
struct rtc_device *rtc;
+ int i, ret, type = 0;
struct rtc_time tm;
- int i, ret, type;
-
- scoped_guard(spinlock_irqsave, &freezer_delta_lock) {
- min = wakeup_ctl.freezer_delta;
- expires = wakeup_ctl.freezer_expires;
- type = wakeup_ctl.freezer_type;
- wakeup_ctl.freezer_delta = 0;
- }
rtc = READ_ONCE(wakeup_ctl.rtc);
/* If we have no rtcdev, just return */
if (!rtc)
return 0;
- /* Find the soonest timer to expire */
+ /* Find the soonest timer to expire, frozen clock_nanosleep() included */
for (i = 0; i < ALARM_NUMTYPE; i++) {
struct alarm_base *base = &alarm_bases[i];
+ ktime_t next_expires = KTIME_MAX;
struct timerqueue_node *next;
- ktime_t next_expires;
+ int next_type = i;
ktime_t delta;
scoped_guard(spinlock_irqsave, &base->lock) {
@@ -250,16 +242,24 @@ static int alarmtimer_suspend(struct device *dev)
if (next)
next_expires = next->expires;
}
- if (!next)
+ scoped_guard(spinlock_irqsave, &freezer_lock) {
+ if (wakeup_ctl.freezer_expires[i] < next_expires) {
+ next_expires = wakeup_ctl.freezer_expires[i];
+ next_type = i == ALARM_REALTIME ?
+ ALARM_REALTIME_FREEZER : ALARM_BOOTTIME_FREEZER;
+ }
+ wakeup_ctl.freezer_expires[i] = KTIME_MAX;
+ }
+ if (next_expires == KTIME_MAX)
continue;
delta = ktime_sub(next_expires, base->get_ktime());
- if (!min || (delta < min)) {
+ if (delta < min) {
expires = next_expires;
min = delta;
- type = i;
+ type = next_type;
}
}
- if (min == 0)
+ if (min == KTIME_MAX)
return 0;
if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
@@ -454,31 +454,12 @@ EXPORT_SYMBOL_GPL(alarm_forward_now);
static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
{
- struct alarm_base *base;
- ktime_t delta;
-
- switch(type) {
- case ALARM_REALTIME:
- base = &alarm_bases[ALARM_REALTIME];
- type = ALARM_REALTIME_FREEZER;
- break;
- case ALARM_BOOTTIME:
- base = &alarm_bases[ALARM_BOOTTIME];
- type = ALARM_BOOTTIME_FREEZER;
- break;
- default:
- WARN_ONCE(1, "Invalid alarm type: %d\n", type);
+ if (WARN_ONCE(type >= ALARM_NUMTYPE, "Invalid alarm type: %d\n", type))
return;
- }
- delta = ktime_sub(absexp, base->get_ktime());
-
- guard(spinlock_irqsave)(&freezer_delta_lock);
- 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;
- }
+ guard(spinlock_irqsave)(&freezer_lock);
+ if (absexp < wakeup_ctl.freezer_expires[type])
+ wakeup_ctl.freezer_expires[type] = absexp;
}
/**
--
2.43.0
next prev parent reply other threads:[~2026-11-16 0:25 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 0:24 [PATCH v2 0/8] alarmtimer: Pick the wakeup RTC per suspend state Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 1/8] alarmtimer: Replace alarmtimer_get_rtcdev() with alarmtimer_has_rtcdev() Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 2/8] alarmtimer: Move the wakeup state into struct rtc_wakeup Igor Velkov via B4 Relay
2026-11-16 0:25 ` Igor Velkov via B4 Relay [this message]
2026-11-16 0:25 ` [PATCH v2 4/8] alarmtimer: Register the platform device without a parent Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 5/8] alarmtimer: Keep an RTC per suspend state Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 6/8] dt-bindings: rtc: Add linux,wakeup-states Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 7/8] alarmtimer: Read the wakeup states of an RTC from linux,wakeup-states Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 8/8] arm64: dts: amlogic: meson-sm1-odroid-hc4: Describe which RTC wakes the board Igor Velkov via B4 Relay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116-b4-alarmtimer-rtc-priority-v2-3-b2c69721e489@iav.lv \
--to=devnull+iav.iav.lv@kernel.org \
--cc=alexandre.belloni@bootlin.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=iav@iav.lv \
--cc=jbrunet@baylibre.com \
--cc=jstultz@google.com \
--cc=khilman@baylibre.com \
--cc=krzk+dt@kernel.org \
--cc=linux-amlogic@lists.infradead.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=martin.blumenstingl@googlemail.com \
--cc=mlichvar@redhat.com \
--cc=neil.armstrong@linaro.org \
--cc=robh@kernel.org \
--cc=sboyd@kernel.org \
--cc=tglx@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®