Re: [PATCH v4 03/15] rtc: rzn1: Fix weekday underflow when alarm crosses month boundary

"Lad, Prabhakar" <[email protected]>
Newsgroups dev.linux.lists.sashiko-reviews,org.kernel.vger.linux-devicetree,org.kernel.vger.linux-rtc
Message-ID <CA+V-a8v23+zevx_xRS-GeHG2yk2k-M2UkPxoC76t-0JewRk9Dw@mail.gmail.com>
On Wed, Aug 19, 2026 at 2:01 PM Wolfram Sang
<[email protected]> wrote:
>
>
> > [Severity: High]
> > This is a pre-existing issue, but does this time_after() comparison
> > allow bypassing the 1-week hardware limit on 32-bit systems?
>
> This is fixed later.
>
> > [Severity: Medium]
> > This is a pre-existing issue, but what happens if the RZN1_RTC_ALW register
> > is uninitialized and wday is 0?
> >
> > Calling fls(0) - 1 would evaluate to -1. If the current time is a Saturday
> > (tm_now.tm_wday == 6), delta_days evaluates to 0, which bypasses
> > the if (delta_days) block and leaves tm->tm_wday = -1.
> >
> > Since the core function rtc_valid_tm() does not check tm_wday, would this
> > propagate a bogus alarm date to the caller?
>
> This looks worth investigating to me. On RZN1, ALW is default 0.
>
On T2H/N2H too.

I'll create a below patch for it:
diff --git a/drivers/rtc/rtc-rzn1.c b/drivers/rtc/rtc-rzn1.c
index 8c70dbf8c7ec..e0fdb591ba91 100644
--- a/drivers/rtc/rtc-rzn1.c
+++ b/drivers/rtc/rtc-rzn1.c
@@ -234,13 +234,24 @@ static int rzn1_rtc_read_alarm(struct device
*dev, struct rtc_wkalrm *alrm)
        if (ret)
                return ret;

+       ctl1 = readl(rtc->base + RZN1_RTC_CTL1);
+       alrm->enabled = !!(ctl1 & (RZN1_RTC_CTL1_ALME | RZN1_RTC_CTL1_1SE));
+
        min = readl(rtc->base + RZN1_RTC_ALM);
        hour = readl(rtc->base + RZN1_RTC_ALH);
-       wday = readl(rtc->base + RZN1_RTC_ALW);

        tm->tm_sec = 0;
        tm->tm_min = bcd2bin(min);
        tm->tm_hour = bcd2bin(hour);
+
+       /*
+        * If wday is zero, no bit is set in RZN1_RTC_ALW. This is the
+        * register's power-on reset value.
+        */
+       wday = readl(rtc->base + RZN1_RTC_ALW);
+       if (!wday)
+               return 0;
+
        delta_days = ((fls(wday) - 1) - tm->tm_wday + 7) % 7;
        tm->tm_wday = fls(wday) - 1;

@@ -249,9 +260,6 @@ static int rzn1_rtc_read_alarm(struct device *dev,
struct rtc_wkalrm *alrm)
                rtc_time64_to_tm(alarm, tm);
        }

-       ctl1 = readl(rtc->base + RZN1_RTC_CTL1);
-       alrm->enabled = !!(ctl1 & (RZN1_RTC_CTL1_ALME | RZN1_RTC_CTL1_1SE));
-
        return 0;
 }

Logs:
    On a cold-booted board with no alarm configured, the following was
    observed before the fix:

        root@rzn2h-evk:~# cat /proc/driver/rtc
        rtc_time        : 00:00:19
        rtc_date        : 2000-01-01
        alrm_time       : 00:00:00
        alrm_date       : 2000-01-07
        alarm_IRQ       : no
        alrm_pending    : no
        update IRQ enabled      : no
        periodic IRQ enabled    : no
        periodic IRQ frequency  : 1
        max user IRQ frequency  : 64
        24hr            : yes

    After the fix:

        root@rzn2h-evk:~# cat /proc/driver/rtc
        rtc_time        : 00:00:25
        rtc_date        : 2000-01-01
        alrm_time       : 00:00:00
        alrm_date       : 2000-01-01
        alarm_IRQ       : no
        alrm_pending    : no
        update IRQ enabled      : no
        periodic IRQ enabled    : no
        periodic IRQ frequency  : 1
        max user IRQ frequency  : 64
        24hr            : yes


Cheers,
Prabhakar
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.