rtc: backport support for handling dates before 1970
Subsystems (2)
cover rtc: backport support for handling dates before 1970
Hello,
as described in the commit log of the two commits the rtc-mt6397 driver relies on these fixes as soon as it should store dates later than 2027-12-31. On one of the patches has a Fixes line, so this submission is done to ensure that both patches are backported.
The patches sent in reply to this mail are (trivial) backports to v6.15.1, they should get backported to the older stable kernels, too, to (somewhat) ensure that in 2028 no surprises happen. git am is able to apply the patches as is to 6.14.y, 6.12.y, 6.6.y, 6.1.y and 5.15.y.
5.10 and 5.4 need an adaption, I didn't look into that yet but can follow up with backports for these.
The two fixes were accompanied by 3 test updates:
46351921cbe1 ("rtc: test: Emit the seconds-since-1970 value instead of days-since-1970")
da62b49830f8 ("rtc: test: Also test time and wday outcome of rtc_time64_to_tm()")
ccb2dba3c19f ("rtc: test: Test date conversion for dates starting in 1900")that cover one of the patches. Would you consider it sensible to backport these, too?
Best regards
Uwe
Alexandre Mergnat (2):
rtc: Make rtc_time64_to_tm() support dates before 1970
rtc: Fix offset calculation for .start_secs < 0
drivers/rtc/class.c | 2 +- drivers/rtc/lib.c | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-)
base-commit: 3ef49626da6dd67013fc2cf0a4e4c9e158bb59f7
Signature · 1 line
2.47.2
PATCH 1/2 rtc: Make rtc_time64_to_tm() support dates before 1970
From: Alexandre Mergnat <[email protected]>
commit 7df4cfef8b351fec3156160bedfc7d6d29de4cce upstream.
Conversion of dates before 1970 is still relevant today because these dates are reused on some hardwares to store dates bigger than the maximal date that is representable in the device's native format. This prominently and very soon affects the hardware covered by the rtc-mt6397 driver that can only natively store dates in the interval 1900-01-01 up to 2027-12-31. So to store the date 2028-01-01 00:00:00 to such a device, rtc_time64_to_tm() must do the right thing for time=-2208988800.
--- drivers/rtc/lib.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/rtc/lib.c +19 -5
@@ -46,24 +46,38 @@ EXPORT_SYMBOL(rtc_year_days);* rtc_time64_to_tm - converts time64_t to rtc_time. * * @time: The number of seconds since 01-01-1970 00:00:00. -* (Must be positive.) +* Works for values since at least 1900 * @tm: Pointer to the struct rtc_time. */ void rtc_time64_to_tm (time64_t time,struct rtc_time *tm) { -unsigned int secs; -int days; +int days, secs;u64 u64tmp;u32 u32tmp, udays, century, day_of_century, year_of_century, year, day_of_year, month, day;bool is_Jan_or_Feb, is_leap_year; -/* time must be positive */ +/* +* Get days and seconds while preserving the sign to +* handle negative time values (dates before 1970-01-01) +*/ days =div_s64_rem (time,86400 , &secs); +/* +* We need 0 <= secs < 86400 which isn't given for negative +* values of time. Fixup accordingly. +*/ +if (secs <0 ) { + days -=1 ; + secs +=86400 ; + } +/* day of the week, 1970-01-01 was a Thursday */ tm->tm_wday = (days +4 ) %7 ; +/* Ensure tm_wday is always positive */ +if (tm->tm_wday <0 ) + tm->tm_wday +=7 ;/* * The following algorithm is, basically, Proposition 6.3 of Neri
@@ -93,7 +107,7 @@ void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)* thus, is slightly different from [1]. */ - udays = ((u32 ) days) +719468 ; + udays = days +719468 ; u32tmp =4 * udays +3 ; century = u32tmp /146097 ;
Signature · 1 line
2.47.2
Re: [PATCH 6.15.y 1/2] rtc: Make rtc_time64_to_tm() support dates before 1970
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: 7df4cfef8b351fec3156160bedfc7d6d29de4cce
WARNING: Author mismatch between patch and upstream commit:
Backport author: <[email protected]> Commit author: Alexandre Mergnat<[email protected]>
Note: The patch differs from the upstream commit:
---
1: 7df4cfef8b351 ! 1: 6da0666c8846e rtc: Make rtc_time64_to_tm() support dates before 1970
@@ Metadata
## Commit message ##
rtc: Make rtc_time64_to_tm() support dates before 1970
+ commit 7df4cfef8b351fec3156160bedfc7d6d29de4cce upstream.
+
Conversion of dates before 1970 is still relevant today because these dates are reused on some hardwares to store dates bigger than the maximal date that is representable in the device's native format.
@@ Commit message
+ Signed-off-by: Uwe Kleine-König <[email protected]>
## drivers/rtc/lib.c ##
@@ drivers/rtc/lib.c: EXPORT_SYMBOL(rtc_year_days);
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.15.y | Success | Success |
PATCH 2/2 rtc: Fix offset calculation for .start_secs < 0
In reply to Uwe Kleine-KönigFrom: Alexandre Mergnat <[email protected]>
commit fe9f5f96cfe8b82d0f24cbfa93718925560f4f8d upstream.
The comparison
rtc->start_secs > rtc->range_max
has a signed left-hand side and an unsigned right-hand side. So the comparison might become true for negative start_secs which is interpreted as a (possibly very large) positive value.
As a negative value can never be bigger than an unsigned value the correct representation of the (mathematical) comparison
rtc->start_secs > rtc->range_max
in C is:
rtc->start_secs >= 0 && rtc->start_secs > rtc->range_max
Use that to fix the offset calculation currently used in the rtc-mt6397 driver.
--- drivers/rtc/class.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rtc/class.c +1 -1
@@ -326,7 +326,7 @@ static void rtc_device_get_offset(struct rtc_device *rtc)* * Otherwise the offset seconds should be 0. */ -if (rtc->start_secs > rtc->range_max || +if ((rtc->start_secs >=0 && rtc->start_secs > rtc->range_max) || rtc->start_secs + range_secs -1 < rtc->range_min) rtc->offset_secs = rtc->start_secs - rtc->range_min;else if (rtc->start_secs > rtc->range_min)
Signature · 1 line
2.47.2
Re: [PATCH 6.15.y 2/2] rtc: Fix offset calculation for .start_secs < 0
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: fe9f5f96cfe8b82d0f24cbfa93718925560f4f8d
WARNING: Author mismatch between patch and upstream commit:
Backport author: <[email protected]> Commit author: Alexandre Mergnat<[email protected]>
Note: The patch differs from the upstream commit:
---
1: fe9f5f96cfe8b ! 1: 6c5107e2d5c38 rtc: Fix offset calculation for .start_secs < 0
@@ Metadata
## Commit message ##
rtc: Fix offset calculation for .start_secs < 0
+ commit fe9f5f96cfe8b82d0f24cbfa93718925560f4f8d upstream.
+
The comparison
rtc->start_secs > rtc->range_max
@@ Commit message
+ Signed-off-by: Uwe Kleine-König <[email protected]>
## drivers/rtc/class.c ##
@@ drivers/rtc/class.c: static void rtc_device_get_offset(struct rtc_device *rtc)
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-5.4.y | Success | Success |
Greg KH
maintainerQuoted 15 lines · Uwe Kleine-König
Hello,
as described in the commit log of the two commits the rtc-mt6397 driver relies on these fixes as soon as it should store dates later than 2027-12-31. On one of the patches has a Fixes line, so this submission is done to ensure that both patches are backported.
The patches sent in reply to this mail are (trivial) backports to v6.15.1, they should get backported to the older stable kernels, too, to (somewhat) ensure that in 2028 no surprises happen.
git amis able to apply the patches as is to 6.14.y, 6.12.y, 6.6.y, 6.1.y and 5.15.y.5.10 and 5.4 need an adaption, I didn't look into that yet but can follow up with backports for these.
Quoted 8 lines
The two fixes were accompanied by 3 test updates:
46351921cbe1 ("rtc: test: Emit the seconds-since-1970 value instead of days-since-1970") da62b49830f8 ("rtc: test: Also test time and wday outcome of rtc_time64_to_tm()") ccb2dba3c19f ("rtc: test: Test date conversion for dates starting in 1900")that cover one of the patches. Would you consider it sensible to backport these, too?
If you want to, sure, but normally people run the latest in-kernel selftests for older kernel trees.
thanks,
greg k-h