Re: [PATCH 1/4] alarmtimer: Bound wakeup source timeout on suspend abort
From: John Stultz
Date: Wed Dec 02 2026 - 14:08:42 EST
On Mon, Nov 30, 2026 at 10:13 PM Samuel Wu <wusamuel@xxxxxxxxxx> wrote:
>
> 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@xxxxxxxxxx>
> ---
> 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
I suspect the MARGIN may want some tuning on different types of
hardware, but it seems like a reasonable start.
> +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;
> - }
Generally looks ok to me.
Acked-by: John Stultz <jstultz@xxxxxxxxxx>