[PATCH 3/4] alarmtimer: Reject imminent-alarm suspend in PM_SUSPEND_PREPARE

From: Samuel Wu

Date: Tue Dec 01 2026 - 01:13:35 EST


The 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)

Suggested-by: Rafael J. Wysocki <rafael@xxxxxxxxxx>
Signed-off-by: Samuel Wu <wusamuel@xxxxxxxxxx>
---
kernel/time/alarmtimer.c | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 175548ab66ed..e582c1c3127a 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -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();
--
2.56.1.911.g883d8bcb20-goog