Linux RTC
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arun Iyengar" <aiyengar1990@gmail.com>
Cc: linux-rtc@vger.kernel.org,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>
Subject: Re: [PATCH] rtc: Don't lose a minute-resolution alarm that rounds down into the past
Date: Tue, 10 Nov 2026 07:29:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-206494@kernel.org> (raw)
In-Reply-To: <20261110-rtc-minute-alarm-v1-1-be9d7c5227a1@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] rtc: 32-bit signed long overflow in 60 * NSEC_PER_SEC in
  rtc_set_alarm()
--

commit 4251dabe39af884c0054960d9c538feb4fe7bb5e
Author: Arun Iyengar <aiyengar1990@gmail.com>

rtc: Don't lose a minute-resolution alarm that rounds down into the past

This commit rounds minute-resolution RTC alarms up to the next minute when
rounding down to the start of the minute would move a future alarm into the
past.

> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> index 96626f8068f93..004819c9a846f 100644
> --- a/drivers/rtc/interface.c
> +++ b/drivers/rtc/interface.c
> @@ -535,18 +535,39 @@ int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
>  		rtc_timer_remove(rtc, &rtc->aie_timer);
>  
>  	alarm_time = rtc_tm_to_ktime(alarm->time);
> -	/*
> -	 * Round down so we never miss a deadline, checking for past deadline is
> -	 * done in __rtc_set_alarm
> -	 */
> -	if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features))
> -		alarm_time = ktime_sub_ns(alarm_time, (u64)alarm->time.tm_sec * NSEC_PER_SEC);
> +	if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features) &&
> +	    alarm->time.tm_sec) {
> +		ktime_t down = ktime_sub_ns(alarm_time,
> +					    (u64)alarm->time.tm_sec * NSEC_PER_SEC);
> +		struct rtc_time tm;
> +		ktime_t now;
> +
> +		err = __rtc_read_time(rtc, &tm);
> +		if (err)
> +			goto unlock;
> +		now = rtc_tm_to_ktime(tm);
> +
> +		/*
> +		 * Round down so we never miss a deadline. If that puts an alarm
> +		 * that is still in the future into the past, the timer would
> +		 * expire right away and the hardware alarm would never be
> +		 * armed: whoever relies on it, e.g. to wake the system from
> +		 * suspend, would wait forever. Round up to the next minute
> +		 * instead. An alarm already in the past is left alone, it is
> +		 * handled by __rtc_set_alarm.
> +		 */
> +		if (ktime_after(down, now) || !ktime_after(alarm_time, now))
> +			alarm_time = down;
> +		else
> +			alarm_time = ktime_add_ns(down, 60 * NSEC_PER_SEC);

[Severity: High]
Does 60 * NSEC_PER_SEC overflow a 32-bit signed long on 32-bit
architectures?

NSEC_PER_SEC is 1000000000L and ktime_add_ns() is a plain macro addition,
so 60 * NSEC_PER_SEC is evaluated in signed long arithmetic before promotion
to ktime_t.  On 32-bit builds this triggers a -Woverflow build warning (or
error under CONFIG_WERROR) and wraps to a negative offset at runtime,
leaving alarm_time in the past.  The ktime_sub_ns() call above casts to u64
to avoid this; using 60ULL * NSEC_PER_SEC here would match it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261110-rtc-minute-alarm-v1-1-be9d7c5227a1@gmail.com?part=1

  reply	other threads:[~2026-11-10  7:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  7:13 [PATCH] rtc: Don't lose a minute-resolution alarm that rounds down into the past Arun Iyengar via B4 Relay
2026-11-10  7:29 ` sashiko-bot [this message]
2026-11-10  7:48 ` Alexandre Belloni
2026-11-12  9:00   ` Arun Iyengar
2026-11-10 11:01 ` kernel test robot
2026-11-10 14:31 ` kernel test robot

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=sashiko-outbox-206494@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aiyengar1990@gmail.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-rtc@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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