[PATCH 2/2] rtc: interface: round timer alarms down to the minute

From: Igor Velkov via B4 Relay

Date: Tue Dec 01 2026 - 17:18:54 EST


From: Igor Velkov <iav@xxxxxx>

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@xxxxxxxxxxxxxxx
Signed-off-by: Igor Velkov <iav@xxxxxx>
---
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