mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Igor Velkov via B4 Relay <devnull+iav.iav.lv@kernel.org>
To: Alexandre Belloni <alexandre.belloni@bootlin.com>,
	 John Stultz <jstultz@google.com>,
	Thomas Gleixner <tglx@kernel.org>
Cc: Stephen Boyd <sboyd@kernel.org>,
	Miroslav Lichvar <mlichvar@redhat.com>,
	 linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org,
	 stable@vger.kernel.org, Igor Velkov <iav@iav.lv>
Subject: [PATCH 2/2] rtc: interface: round timer alarms down to the minute
Date: Tue, 01 Dec 2026 22:18:41 +0000	[thread overview]
Message-ID: <20261201-b4-rtc-timer-round-down-v1-2-4ac60b362a54@iav.lv> (raw)
In-Reply-To: <20261201-b4-rtc-timer-round-down-v1-0-4ac60b362a54@iav.lv>

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



      parent reply	other threads:[~2026-12-01 22:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201-b4-rtc-timer-round-down-v1-2-4ac60b362a54@iav.lv \
    --to=devnull+iav.iav.lv@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=iav@iav.lv \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=mlichvar@redhat.com \
    --cc=sboyd@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®