* [PATCH 1/2] rtc: interface: let rtc_timer_start() fail for an expired timer
2026-12-01 22:18 [PATCH 0/2] rtc: interface: keep alarmtimer from sleeping through a minute alarm Igor Velkov via B4 Relay
@ 2026-12-01 22:18 ` Igor Velkov via B4 Relay
2026-12-01 22:18 ` [PATCH 2/2] rtc: interface: round timer alarms down to the minute Igor Velkov via B4 Relay
2026-12-01 22:23 ` [PATCH 0/2] rtc: interface: keep alarmtimer from sleeping through a minute alarm sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-12-01 22:18 UTC (permalink / raw)
To: Alexandre Belloni, John Stultz, Thomas Gleixner
Cc: Stephen Boyd, Miroslav Lichvar, linux-rtc, linux-kernel, stable,
Igor Velkov
From: Igor Velkov <iav@iav.lv>
When __rtc_set_alarm() finds that the alarm time has already passed,
rtc_timer_enqueue() keeps the system awake with pm_stay_awake(),
expires the timer from the work queue and returns 0. pm_stay_awake()
aborts a suspend in progress only when the wakeup event check is armed,
through /sys/power/wakeup_count or by autosleep. Otherwise the suspend
goes on with no alarm set.
alarmtimer_suspend() rejects the suspend when rtc_timer_start() fails
("if in the past reject suspend briefly to handle"), but
rtc_timer_start() never fails for an expired timer. alarmtimer passes
an expiry at least 2 seconds ahead, so this shows once rtc_timer_start()
rounds the expiry down to the minute.
Return -ETIME from rtc_timer_start() and leave the timer stopped when
its expiry has already passed, whether before queueing it or when
programming the alarm. Removing the timer reprograms the alarm for the
next timer on the queue, as rtc_timer_cancel() does. The alarm and
update interrupt timers are still expired from the work queue when late.
Fixes: 59a93c27c489 ("alarmtimer: Implement minimum alarm interval for allowing suspend")
Cc: stable@vger.kernel.org
Signed-off-by: Igor Velkov <iav@iav.lv>
---
drivers/rtc/interface.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 96626f8068f9..bc281037c67c 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -856,9 +856,11 @@ int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
}
/**
- * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
+ * __rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
* @rtc: rtc device
* @timer: timer being added.
+ * @fail_if_past: return -ETIME for a timer that has already expired
+ * instead of expiring it from the work queue
*
* Enqueues a timer onto the rtc devices timerqueue and sets
* the next alarm event appropriately.
@@ -867,7 +869,8 @@ int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
*
* Must hold ops_lock for proper serialization of timerqueue
*/
-static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
+static int __rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer,
+ bool fail_if_past)
{
struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
struct rtc_time tm;
@@ -878,8 +881,11 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
if (err)
return err;
- timer->enabled = 1;
now = rtc_tm_to_ktime(tm);
+ if (fail_if_past && timer->node.expires <= now)
+ return -ETIME;
+
+ timer->enabled = 1;
/* Skip over expired timers */
while (next) {
@@ -897,6 +903,10 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
alarm.enabled = 1;
err = __rtc_set_alarm(rtc, &alarm);
if (err == -ETIME) {
+ if (fail_if_past) {
+ rtc_timer_remove(rtc, timer);
+ return err;
+ }
pm_stay_awake(rtc->dev.parent);
schedule_work(&rtc->irqwork);
} else if (err) {
@@ -909,6 +919,11 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
return 0;
}
+static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
+{
+ return __rtc_timer_enqueue(rtc, timer, false);
+}
+
static void rtc_alarm_disable(struct rtc_device *rtc)
{
if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
@@ -1060,7 +1075,8 @@ void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
* @ expires: time at which to expire the timer
* @ period: period that the timer will recur
*
- * Kernel interface to set an rtc_timer
+ * Kernel interface to set an rtc_timer. Returns -ETIME and leaves the
+ * timer stopped if @expires has already passed.
*/
int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
ktime_t expires, ktime_t period)
@@ -1074,7 +1090,7 @@ int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
timer->node.expires = expires;
timer->period = period;
- ret = rtc_timer_enqueue(rtc, timer);
+ ret = __rtc_timer_enqueue(rtc, timer, true);
mutex_unlock(&rtc->ops_lock);
return ret;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] rtc: interface: round timer alarms down to the minute
2026-12-01 22:18 [PATCH 0/2] rtc: interface: keep alarmtimer from sleeping through a minute alarm Igor Velkov via B4 Relay
2026-12-01 22:18 ` [PATCH 1/2] rtc: interface: let rtc_timer_start() fail for an expired timer Igor Velkov via B4 Relay
@ 2026-12-01 22:18 ` Igor Velkov via B4 Relay
2026-12-01 22:23 ` [PATCH 0/2] rtc: interface: keep alarmtimer from sleeping through a minute alarm sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: Igor Velkov via B4 Relay @ 2026-12-01 22:18 UTC (permalink / raw)
To: Alexandre Belloni, John Stultz, Thomas Gleixner
Cc: Stephen Boyd, Miroslav Lichvar, linux-rtc, linux-kernel, stable,
Igor Velkov
From: Igor Velkov <iav@iav.lv>
An RTC with RTC_FEATURE_ALARM_RES_MINUTE has no seconds in its alarm.
rtc_set_alarm() rounds the alarm down to the minute, but
rtc_timer_start() keeps the seconds, so an expiry later in the current
minute passes for a future one.
alarmtimer runs into this after the minute alarm wakes the system
early. Suspending again for the rest of the interval, as autosleep
does, programs the same minute. On an ODROID-HC4 with a PCF8563, a
timer due at 19:23:10 woke the board at 19:23:00, a suspend at 19:23:01
wrote minute 23 again, the alarm registers kept their value, and the
board slept until Wake-on-LAN woke it at 19:24:55, 105 seconds late.
Round the expiry down in rtc_timer_start() as rtc_set_alarm() does. An
expiry in the current minute is then in the past, rtc_timer_start()
returns -ETIME, and alarmtimer_suspend() rejects the suspend, as it
already does for a timer less than 2 seconds away. On the same board the
same sequence now ends with the second suspend rejected and the timer
firing on time.
Fixes: d87f741dddab ("rtc: handle alarms with a minute resolution")
Cc: stable@vger.kernel.org
Signed-off-by: Igor Velkov <iav@iav.lv>
---
drivers/rtc/interface.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index bc281037c67c..cfcc32cf7885 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -1076,7 +1076,8 @@ void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
* @ period: period that the timer will recur
*
* Kernel interface to set an rtc_timer. Returns -ETIME and leaves the
- * timer stopped if @expires has already passed.
+ * timer stopped if @expires, rounded down to the minute on an RTC with
+ * minute alarm resolution, has already passed.
*/
int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
ktime_t expires, ktime_t period)
@@ -1087,6 +1088,17 @@ int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
if (timer->enabled)
rtc_timer_remove(rtc, timer);
+ /* Round down, as rtc_set_alarm() does, so the alarm never fires late */
+ if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features)) {
+ struct timespec64 ts = ktime_to_timespec64(expires);
+ s32 rem;
+
+ div_s64_rem(ts.tv_sec, 60, &rem);
+ if (rem < 0)
+ rem += 60;
+ expires = ktime_set(ts.tv_sec - rem, 0);
+ }
+
timer->node.expires = expires;
timer->period = period;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread