alarmtimer: Reject suspend earlier when alarm is imminent
cover alarmtimer: Reject suspend earlier when alarm is imminent
alarmtimer_suspend() aborts suspend with -EBUSY when the next alarm is due in less than 2 seconds, and holds a wakeup source for a fixed 2 seconds. This is suboptimal:
- The check runs quite late in the suspend sequence. An abort means kernel spent time doing work it now has to undo.
- The fixed 2 second timeout has no relation to the time at which the alarm is due.
This patchseries addresses both problems:
- 1/4: Hold wakeup source with grace period (100ms) after alarm is due.
- 2/4: Move the lookup of soonest alarm into helper function
- 3/4: Add a PM notifier (earlier in suspend sequence) to do an imminent alarm check. This is the same idea as [1], but without the sysfs node.
- 4/4: Add alarm check in resume path, to handle wakeups that are earlier than the actual alarm, since RTC has up to 1s error compared to an alarm on hrtimer
With patch 3, the time spent on a suspend attempt that is aborted by an imminent alarm drops from:
- ~220 ms to ~20 ms on Pixel 9 (v7.3)
- ~90 ms to ~0.1 ms on Pixel Watch (v6.12)
With patch 4, aborted suspend attempts due to alarmtimer are noticeably lower.
[1]: https://lore.kernel.org/lkml/[email protected]/
Samuel Wu (4):
alarmtimer: Bound wakeup source timeout on suspend abort
alarmtimer: Extract alarmtimer_get_next_delta() helper
alarmtimer: Reject imminent-alarm suspend in PM_SUSPEND_PREPARE
alarmtimer: Hold wakeup source on resume for imminent alarms
kernel/time/alarmtimer.c | 124 ++++++++++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 27 deletions(-)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 1/4 alarmtimer: Bound wakeup source timeout on suspend abort
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.
--- kernel/time/alarmtimer.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
kernel/time/alarmtimer.c +18 -3
@@ -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);
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 2/4 alarmtimer: Extract alarmtimer_get_next_delta() helper
In reply to Samuel WuAdd 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.
--- kernel/time/alarmtimer.c | 67 ++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 23 deletions(-)
kernel/time/alarmtimer.c +44 -23
@@ -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))
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 3/4 alarmtimer: Reject imminent-alarm suspend in PM_SUSPEND_PREPARE
In reply to Samuel WuThe imminent alarm check is in the device suspend stage, which occurs after freezing tasks, preparing devices, and suspend devices. For a suspend attempt aborted by an imminent alarm, the kernel goes through a relatively expensive operation of processing several suspend stages and then unwinding those same operations.
Register a PM notifier to check for imminent alarm earlier in the suspend sequence, at the PM_SUSPEND_PREPARE stage of suspend. This earlier imminent alarm check reduces redundant work such as freeze, prepare, and thaw.
This significantly drops imminent-alarm suspend abort time:
- ~220 ms to ~20 ms on Pixel 9 (v7.3)
- ~90 ms to ~0.1 ms on Pixel Watch (v6.12)
--- kernel/time/alarmtimer.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
kernel/time/alarmtimer.c +32 -0
@@ -24,6 +24,7 @@#include <linux/posix-timers.h> #include <linux/workqueue.h> #include <linux/freezer.h> +#include <linux/suspend.h> #include <linux/compat.h> #include <linux/module.h> #include <linux/time_namespace.h>
@@ -61,6 +62,7 @@ static DEFINE_SPINLOCK(freezer_delta_lock);/* rtc timer and device for setting alarm wakeups at suspend */ static struct rtc_timer rtctimer;static struct rtc_device *rtcdev; +static struct device *alarmtimer_dev;/** * alarmtimer_get_rtcdev - Return selected rtcdevice
@@ -100,6 +102,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)WRITE_ONCE (rtcdev, rtc);/* Hold a reference so it doesn't go away */ get_device (dev); +WRITE_ONCE (alarmtimer_dev, &pdev->dev);return 0 ; }
@@ -326,7 +329,34 @@ static int alarmtimer_resume(struct device *dev) return 0; } +static int alarmtimer_pm_notify (struct notifier_block *nb, +unsigned long mode,void *unused) +{ +struct device *dev; +ktime_t delta; + +if (mode !=PM_SUSPEND_PREPARE ) +return NOTIFY_DONE ; + + dev =READ_ONCE (alarmtimer_dev); +if (!dev) +return NOTIFY_DONE ; + + delta =alarmtimer_get_next_delta (NULL ,NULL ); +if (alarmtimer_wakeup_if_soon (dev, delta)) +return notifier_from_errno (-EBUSY ); + +return NOTIFY_DONE ; +} + +static void __init alarmtimer_pm_notifier_setup (void ) +{ +pm_notifier (alarmtimer_pm_notify,0 ); +} +#else +static inline void alarmtimer_pm_notifier_setup (void ) { } +static int alarmtimer_suspend (struct device *dev) {return 0 ;
@@ -904,6 +934,8 @@ static int __init alarmtimer_init(void)if (error) goto out_if; +alarmtimer_pm_notifier_setup (); +return 0 ; out_if:alarmtimer_rtc_interface_remove ();
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 4/4 alarmtimer: Hold wakeup source on resume for imminent alarms
In reply to Samuel WuThe RTC can wake a device from suspend up to 1s before the alarm is due to fire, because of the RTC's 1s granularity. This means during the resume path of suspend, an alarm can be due within the next second.
So to prevent even starting a doomed suspend attempt, check for imminent alarms and hold a wakeup source to appropriately gate future suspend attempts. This also saves an unnecessary filesystem sync.
Failed suspend attempts due to alarmtimer are significantly reduced on a Pixel 9 (v7.3) running Android, and QEMU x86_64 (v7.4) with kernel autosleep.
--- kernel/time/alarmtimer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/time/alarmtimer.c +3 -1
@@ -324,8 +324,10 @@ static int alarmtimer_resume(struct device *dev)struct rtc_device *rtc; rtc =alarmtimer_get_rtcdev (); -if (rtc) +if (rtc) {rtc_timer_cancel (rtc, &rtctimer); +alarmtimer_wakeup_if_soon (dev,alarmtimer_get_next_delta (NULL ,NULL )); + }return 0 ; }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog