From: Samuel Wu <wusamuel@google.com>
To: John Stultz <jstultz@google.com>,
Thomas Gleixner <tglx@kernel.org>,
Stephen Boyd <sboyd@kernel.org>,
Miroslav Lichvar <mlichvar@redhat.com>
Cc: linux-pm@vger.kernel.org, rafael@kernel.org,
Samuel Wu <wusamuel@google.com>,
kernel-team@android.com, linux-kernel@vger.kernel.org
Subject: [PATCH 2/4] alarmtimer: Extract alarmtimer_get_next_delta() helper
Date: Mon, 30 Nov 2026 22:13:06 -0800 [thread overview]
Message-ID: <20261201061304.2825419-8-wusamuel@google.com> (raw)
In-Reply-To: <20261201061304.2825419-6-wusamuel@google.com>
Add a small helper, alarmtimer_get_next_delta(), that does the lookup of
the soonest alarm, and returns the delta time to it. Additionally, use
KTIME_MAX instead of 0 as the sentinel value which means "no alarm queued",
so expiries of 0 delta can be handled.
Behaviorally, this means that a delta of exactly 0 returned with no RTC
alarm set. Now, it returns -EBUSY.
Signed-off-by: Samuel Wu <wusamuel@google.com>
---
kernel/time/alarmtimer.c | 67 ++++++++++++++++++++++++++--------------
1 file changed, 44 insertions(+), 23 deletions(-)
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 686fa12c0cfe..175548ab66ed 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -214,6 +214,41 @@ static bool alarmtimer_wakeup_if_soon(struct device *dev, ktime_t delta)
return true;
}
+/*
+ * Return the delta to the soonest queued alarm, or KTIME_MAX if none is queued.
+ * expires and type are written only when returning a value < KTIME_MAX.
+ */
+static ktime_t alarmtimer_get_next_delta(ktime_t *expires, int *type)
+{
+ ktime_t min = KTIME_MAX;
+ int i;
+
+ for (i = 0; i < ALARM_NUMTYPE; i++) {
+ struct alarm_base *base = &alarm_bases[i];
+ struct timerqueue_node *next;
+ ktime_t next_expires;
+ ktime_t delta;
+
+ scoped_guard(spinlock_irqsave, &base->lock) {
+ next = timerqueue_getnext(&base->timerqueue);
+ if (next)
+ next_expires = next->expires;
+ }
+ if (!next)
+ continue;
+ delta = ktime_sub(next_expires, base->get_ktime());
+ if (delta < min) {
+ min = delta;
+ if (expires)
+ *expires = next_expires;
+ if (type)
+ *type = i;
+ }
+ }
+
+ return min;
+}
+
/**
* alarmtimer_suspend - Suspend time callback
* @dev: unused
@@ -225,13 +260,13 @@ static bool alarmtimer_wakeup_if_soon(struct device *dev, ktime_t delta)
*/
static int alarmtimer_suspend(struct device *dev)
{
- ktime_t min, now, expires;
+ ktime_t min, now, expires, delta, next_expires;
+ int ret, type, next_type;
struct rtc_device *rtc;
struct rtc_time tm;
- int i, ret, type;
scoped_guard(spinlock_irqsave, &freezer_delta_lock) {
- min = freezer_delta;
+ min = freezer_delta ?: KTIME_MAX;
expires = freezer_expires;
type = freezer_alarmtype;
freezer_delta = 0;
@@ -243,27 +278,13 @@ static int alarmtimer_suspend(struct device *dev)
return 0;
/* Find the soonest timer to expire */
- for (i = 0; i < ALARM_NUMTYPE; i++) {
- struct alarm_base *base = &alarm_bases[i];
- struct timerqueue_node *next;
- ktime_t next_expires;
- ktime_t delta;
-
- scoped_guard(spinlock_irqsave, &base->lock) {
- next = timerqueue_getnext(&base->timerqueue);
- if (next)
- next_expires = next->expires;
- }
- if (!next)
- continue;
- delta = ktime_sub(next_expires, base->get_ktime());
- if (!min || (delta < min)) {
- expires = next_expires;
- min = delta;
- type = i;
- }
+ delta = alarmtimer_get_next_delta(&next_expires, &next_type);
+ if (delta < min) {
+ min = delta;
+ expires = next_expires;
+ type = next_type;
}
- if (min == 0)
+ if (min == KTIME_MAX)
return 0;
if (alarmtimer_wakeup_if_soon(dev, min))
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-01 6:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 6:13 [PATCH 0/4] alarmtimer: Reject suspend earlier when alarm is imminent Samuel Wu
2026-12-01 6:13 ` [PATCH 1/4] alarmtimer: Bound wakeup source timeout on suspend abort Samuel Wu
2026-12-01 6:13 ` Samuel Wu [this message]
2026-12-01 6:13 ` [PATCH 3/4] alarmtimer: Reject imminent-alarm suspend in PM_SUSPEND_PREPARE Samuel Wu
2026-12-01 6:13 ` [PATCH 4/4] alarmtimer: Hold wakeup source on resume for imminent alarms Samuel Wu
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=20261201061304.2825419-8-wusamuel@google.com \
--to=wusamuel@google.com \
--cc=jstultz@google.com \
--cc=kernel-team@android.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=mlichvar@redhat.com \
--cc=rafael@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