Skip to contentSkip to conversation

rtc: sh: stop resetting time to epoch

PATCH In review Alexandre Belloni 2 participants 8 messages Mar 20, 2019 – Mar 22, 2019 Real time clock (RTC)

lore
Share & tools
lore

Alexandre Belloni

author maintainer
#0 lore raw

PATCH 1/3 rtc: sh: stop resetting time to epoch

There is no point in resetting the time to epoch as this means that userspace will never get the valuable information that time is actually invalid.

---
 drivers/rtc/rtc-sh.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/rtc/rtc-sh.c +3 -6
index d417b203cbc5..f4ac9ec8fbb6 100644
@@ -276,6 +276,9 @@ static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	struct sh_rtc *rtc = dev_get_drvdata(dev);
 	unsigned int sec128, sec2, yr, yr100, cf_bit;
 
+	if (!(readb(rtc->regbase + RCR2) & RCR2_RTCEN))
+		return -EINVAL;
+
 	do {
 		unsigned int tmp;
 
@@ -600,12 +603,6 @@ static int __init sh_rtc_probe(struct platform_device *pdev)
 
 	rtc->rtc_dev->max_user_freq = 256;
 
-	/* reset rtc to epoch 0 if time is invalid */
-	if (rtc_read_time(rtc->rtc_dev, &r) < 0) {
-		rtc_time_to_tm(0, &r);
-		rtc_set_time(rtc->rtc_dev, &r);
-	}
-
 	device_init_wakeup(&pdev->dev, 1);
 	return 0;
 
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

PATCH 2/3 rtc: sh: fix possible race condition

The IRQ is requested before the struct rtc is allocated and registered, but this struct is used in the IRQ handler. This may lead to a NULL pointer dereference.

Switch to devm_rtc_allocate_device/rtc_register_device to allocate the rtc struct before requesting the IRQ.

---
 drivers/rtc/rtc-sh.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/rtc/rtc-sh.c +9 -7
index f4ac9ec8fbb6..b582af10ddb5 100644
@@ -531,6 +531,10 @@ static int __init sh_rtc_probe(struct platform_device *pdev)
 		rtc->clk = NULL;
 	}
 
+	rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(rtc->rtc_dev))
+		return PTR_ERR(rtc->rtc_dev);
+
 	clk_enable(rtc->clk);
 
 	rtc->capabilities = RTC_DEF_CAPABILITIES;
@@ -594,15 +598,13 @@ static int __init sh_rtc_probe(struct platform_device *pdev)
 	sh_rtc_setaie(&pdev->dev, 0);
 	sh_rtc_setcie(&pdev->dev, 0);
 
-	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "sh",
-					   &sh_rtc_ops, THIS_MODULE);
-	if (IS_ERR(rtc->rtc_dev)) {
-		ret = PTR_ERR(rtc->rtc_dev);
-		goto err_unmap;
-	}
-
+	rtc->rtc_dev->ops = &sh_rtc_ops;
 	rtc->rtc_dev->max_user_freq = 256;
 
+	ret = rtc_register_device(rtc->rtc_dev);
+	if (ret)
+		goto err_unmap;
+
 	device_init_wakeup(&pdev->dev, 1);
 	return 0;
 
Signature · 1 line
2.20.1
Reviewed-by Tested-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] rtc: sh: fix possible race condition

Quoted 8 lines · Alexandre Belloni

The IRQ is requested before the struct rtc is allocated and registered, but this struct is used in the IRQ handler. This may lead to a NULL pointer dereference.

Switch to devm_rtc_allocate_device/rtc_register_device to allocate the rtc struct before requesting the IRQ.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#3 lore raw

PATCH 3/3 rtc: sh: set range

In reply to Alexandre Belloni

The SH RTC is a BCD RTC with some version having 4 digits for the year.

The range for the RTCs with only 2 digits for the year was unfortunately shifted to handle 1999 to 2098.

---
 drivers/rtc/rtc-sh.c | 8 ++++++++
 include/linux/rtc.h  | 1 +
 2 files changed, 9 insertions(+)
drivers/rtc/rtc-sh.c +8 -0
index b582af10ddb5..37964a867de7 100644
@@ -601,6 +601,14 @@ static int __init sh_rtc_probe(struct platform_device *pdev)
 	rtc->rtc_dev->ops = &sh_rtc_ops;
 	rtc->rtc_dev->max_user_freq = 256;
 
+	if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
+		rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900;
+		rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_9999;
+	} else {
+		rtc->rtc_dev->range_min = mktime64(1999, 1, 1, 0, 0, 0);
+		rtc->rtc_dev->range_max = mktime64(2098, 12, 31, 23, 59, 59);
+	}
+
 	ret = rtc_register_device(rtc->rtc_dev);
 	if (ret)
 		goto err_unmap;
include/linux/rtc.h +1 -0
index f89bfbb54902..588120ba372c 100644
@@ -165,6 +165,7 @@ struct rtc_device {
 #define RTC_TIMESTAMP_BEGIN_1900	-2208989361LL /* 1900-01-01 00:00:00 */
 #define RTC_TIMESTAMP_BEGIN_2000	946684800LL /* 2000-01-01 00:00:00 */
 #define RTC_TIMESTAMP_END_2099		4102444799LL /* 2099-12-31 23:59:59 */
+#define RTC_TIMESTAMP_END_9999		253402300799LL /* 9999-12-31 23:59:59 */
 
 extern struct rtc_device *devm_rtc_device_register(struct device *dev,
 					const char *name,
Signature · 1 line
2.20.1
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] rtc: sh: set range

Quoted 6 lines · Alexandre Belloni

The SH RTC is a BCD RTC with some version having 4 digits for the year.

The range for the RTCs with only 2 digits for the year was unfortunately shifted to handle 1999 to 2098.

include/linux/rtc.h struct rtc_device { · line 165

Quoted 4 lines
include/linux/rtc.h +0 -0
@@ -165,6 +165,7 @@ struct rtc_device {
 #define RTC_TIMESTAMP_BEGIN_1900       -2208989361LL /* 1900-01-01 00:00:00 */

mktime64(1900, 1, 1, 0, 0, 0) = -2208988800 ??

Is this due to leap seconds, and mktime64() is valid for 1970 and later only?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#5 lore raw

Re: [PATCH 3/3] rtc: sh: set range

Quoted 11 lines · Geert Uytterhoeven
Quoted 6 lines · Alexandre Belloni

The SH RTC is a BCD RTC with some version having 4 digits for the year.

The range for the RTCs with only 2 digits for the year was unfortunately shifted to handle 1999 to 2098.

Quoted 9 lines

include/linux/rtc.h struct rtc_device { · line 165

Quoted 4 lines
include/linux/rtc.h +0 -0
@@ -165,6 +165,7 @@ struct rtc_device {
 #define RTC_TIMESTAMP_BEGIN_1900       -2208989361LL /* 1900-01-01 00:00:00 */

mktime64(1900, 1, 1, 0, 0, 0) = -2208988800 ??

Is this due to leap seconds, and mktime64() is valid for 1970 and later only?

That's a bug, it seems I didn't use the correct timezone when doing the calculation. Thanks for spotting that, you can send a patch to correct it or I can do it.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

Re: [PATCH 3/3] rtc: sh: set range

Hi Alexander,

Quoted 19 lines · Alexandre Belloni
Quoted 18 lines · Geert Uytterhoeven
Quoted 6 lines · Alexandre Belloni

The SH RTC is a BCD RTC with some version having 4 digits for the year.

The range for the RTCs with only 2 digits for the year was unfortunately shifted to handle 1999 to 2098.

include/linux/rtc.h struct rtc_device { · line 165

Quoted 4 lines
include/linux/rtc.h +0 -0
@@ -165,6 +165,7 @@ struct rtc_device {
 #define RTC_TIMESTAMP_BEGIN_1900       -2208989361LL /* 1900-01-01 00:00:00 */

mktime64(1900, 1, 1, 0, 0, 0) = -2208988800 ??

Quoted 6 lines
Quoted 2 lines

Is this due to leap seconds, and mktime64() is valid for 1970 and later only?

That's a bug, it seems I didn't use the correct timezone when doing the calculation. Thanks for spotting that, you can send a patch to correct it or I can do it.

Thanks for confirming, done.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Reviewed-by Tested-by on v1 1/3
#7 lore raw
In reply to Alexandre Belloni

Hi Alexandre,

Quoted 5 lines · Alexandre Belloni

There is no point in resetting the time to epoch as this means that userspace will never get the valuable information that time is actually invalid.

Thanks for your patch!

Impact before/after (cold boot):

-sh-rtc fcff1000.rtc: setting system clock to 1970-01-01T00:00:00 UTC (0) +sh-rtc fcff1000.rtc: hctosys: unable to read the hardware clock

After warm boot (Linux reboot command):

    sh-rtc fcff1000.rtc: setting system clock to 2019-03-21T08:47:54

UTC (1553158074)

After reset (reset switch):

sh-rtc fcff1000.rtc: hctosys: unable to read the hardware clock

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds