Linux Power Management development
 help / color / mirror / Atom feed
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 1/4] alarmtimer: Bound wakeup source timeout on suspend abort
Date: Mon, 30 Nov 2026 22:13:05 -0800	[thread overview]
Message-ID: <20261201061304.2825419-7-wusamuel@google.com> (raw)
In-Reply-To: <20261201061304.2825419-6-wusamuel@google.com>

When an alarm is due within 2 seconds during suspend, alarmtimer_suspend()
returns -EBUSY and holds a wakeup source for a fixed 2 seconds. This fixed
2 seconds is suboptimal, as the system is kept awake long after a
soon-to-fire alarm fires, and a far off alarm (e.g. close to 2s) has no
wakeup margin to process the alarm.

Add a helper function, alarmtimer_wakeup_if_soon(), to hold the wakeup
source for 100ms after the next alarm is due. This gives callers some
margin to process the alarm, while minimizing unnecessary time holding a
wakeup source.

100ms is chosen as the margin because it covers ktime_to_ms() truncation,
jiffy timer rounding, while leaving ample headroom for userspace delivery
(this was ~1-2ms on a low-powered wearable device) and for userspace to
acquire a wakeup source.

Signed-off-by: Samuel Wu <wusamuel@google.com>
---
 kernel/time/alarmtimer.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index da6b3024406c..686fa12c0cfe 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -197,6 +197,23 @@ ktime_t alarm_expires_remaining(const struct alarm *alarm)
 EXPORT_SYMBOL_GPL(alarm_expires_remaining);
 
 #ifdef CONFIG_RTC_CLASS
+#define ALARMTIMER_MIN_SUSPEND_DELTA_NS	(2 * NSEC_PER_SEC)
+#define ALARMTIMER_WAKEUP_MARGIN_MS	100U
+
+static bool alarmtimer_wakeup_if_soon(struct device *dev, ktime_t delta)
+{
+	unsigned int timeout_ms = ALARMTIMER_WAKEUP_MARGIN_MS;
+
+	if (ktime_to_ns(delta) >= ALARMTIMER_MIN_SUSPEND_DELTA_NS)
+		return false;
+
+	if (delta > 0)
+		timeout_ms += ktime_to_ms(delta);
+
+	pm_wakeup_event(dev, timeout_ms);
+	return true;
+}
+
 /**
  * alarmtimer_suspend - Suspend time callback
  * @dev: unused
@@ -249,10 +266,8 @@ static int alarmtimer_suspend(struct device *dev)
 	if (min == 0)
 		return 0;
 
-	if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
-		pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
+	if (alarmtimer_wakeup_if_soon(dev, min))
 		return -EBUSY;
-	}
 
 	trace_alarmtimer_suspend(expires, type);
 
-- 
2.56.1.911.g883d8bcb20-goog


  reply	other threads:[~2026-12-01  6:13 UTC|newest]

Thread overview: 3+ 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 ` Samuel Wu [this message]
2026-12-01  6:13 ` [PATCH 3/4] alarmtimer: Reject imminent-alarm suspend in PM_SUSPEND_PREPARE 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-7-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