* [PATCH v2 0/2] time: Prevent time64_to_tm() day truncation on 32-bit
@ 2026-09-25 16:57 Karl Mehltretter
2026-09-25 16:57 ` [PATCH v2 1/2] " Karl Mehltretter
2026-09-25 16:57 ` [PATCH v2 2/2] time/kunit: Add time64_to_tm() case beyond 32-bit day count Karl Mehltretter
0 siblings, 2 replies; 5+ messages in thread
From: Karl Mehltretter @ 2026-09-25 16:57 UTC (permalink / raw)
To: John Stultz, Thomas Gleixner
Cc: Karl Mehltretter, Stephen Boyd, Miroslav Lichvar, Deepa Dinamani,
Cassio Neri, Thomas Weißschuh, David Laight, linux-kernel
time64_to_tm() keeps the day count in a long, which wraps on 32-bit
kernels once it reaches 2^31 days (about 5.88 million years after the
epoch). Patch 1 widens it to s64; patch 2 adds a KUnit case at that
boundary.
Why years beyond LONG_MAX are not handled: with this series the
calculation is correct on 32-bit for every year that fits struct tm's
long tm_year. Timestamps whose year - 1900 exceeds LONG_MAX (about 2.1
billion years; time64_t reaches about 292 billion) still truncate, but
only in the final assignment to tm_year. Fixing that means widening
tm_year for every struct tm user, so I have left it out of this fix.
Changes in v2:
- Split the KUnit case into its own patch so the fix can be backported
on its own (Thomas).
- Declare 'long rem;' before 's64 days;' (Thomas).
- Carried Thomas' Reviewed-by on both patches; the code is unchanged
apart from the declaration order.
Testing: time_test_cases KUnit suite under QEMU, i386 and arm, built
with clang 22 (LLVM=1); arm also with gcc 13.3. With the series both cases
pass. With only patch 2 applied the new case fails on both architectures
with tm_year=-5879541 and wrong tm_mon, tm_mday, tm_yday and tm_wday.
timeconv.o references only div_s64_rem() and div64_u64_rem(), no compiler
runtime division helpers.
v1: https://lore.kernel.org/r/8d421725051450cecf0a7e6d2c514b6926df43b1.1788019619.git.kmehltretter@gmail.com
Karl Mehltretter (2):
time: Prevent time64_to_tm() day truncation on 32-bit
time/kunit: Add time64_to_tm() case beyond 32-bit day count
kernel/time/time_test.c | 16 ++++++++++++++++
kernel/time/timeconv.c | 6 ++++--
2 files changed, 20 insertions(+), 2 deletions(-)
base-commit: 50d05c7c76c96b90462f24debacca971d2e86713
--
2.39.5 (Apple Git-154)
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v2 1/2] time: Prevent time64_to_tm() day truncation on 32-bit 2026-09-25 16:57 [PATCH v2 0/2] time: Prevent time64_to_tm() day truncation on 32-bit Karl Mehltretter @ 2026-09-25 16:57 ` Karl Mehltretter 2026-09-29 19:22 ` [tip: timers/core] " tip-bot2 for Karl Mehltretter 2026-09-25 16:57 ` [PATCH v2 2/2] time/kunit: Add time64_to_tm() case beyond 32-bit day count Karl Mehltretter 1 sibling, 1 reply; 5+ messages in thread From: Karl Mehltretter @ 2026-09-25 16:57 UTC (permalink / raw) To: John Stultz, Thomas Gleixner Cc: Karl Mehltretter, Stephen Boyd, Miroslav Lichvar, Deepa Dinamani, Cassio Neri, Thomas Weißschuh, David Laight, linux-kernel time64_to_tm() accepts a 64-bit seconds value, but stores the quotient in long. On a 32-bit kernel the day count therefore wraps when it reaches 2^31, even though the corresponding year remains representable in struct tm. This is reachable when formatting externally supplied timestamps. For example, NILFS recovery copies the little-endian 64-bit ss_create field from an on-disk segment summary into time64_t. Its sysfs attributes then print that value with %ptTs, whose formatter calls time64_to_tm(). A corrupted image can consequently produce an architecture-dependent printed date. Keep the day quotient in s64 while leaving the seconds-within-day remainder as long. Use div_s64_rem() for the weekday calculation so 32-bit builds do not require compiler runtime division helpers. Fixes: e6c2682a1da3 ("time: Add time64_to_tm()") Assisted-by: LLM Reviewed-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com> --- kernel/time/timeconv.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/time/timeconv.c b/kernel/time/timeconv.c index 59b922c826e7..f1cb61e0b114 100644 --- a/kernel/time/timeconv.c +++ b/kernel/time/timeconv.c @@ -49,7 +49,8 @@ void time64_to_tm(time64_t totalsecs, int offset, struct tm *result) u32 u32tmp, day_of_century, year_of_century, day_of_year, month, day; u64 u64tmp, udays, century, year; bool is_Jan_or_Feb, is_leap_year; - long days, rem; + long rem; + s64 days; int remainder; days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder); @@ -70,7 +71,8 @@ void time64_to_tm(time64_t totalsecs, int offset, struct tm *result) result->tm_sec = rem % 60; /* January 1, 1970 was a Thursday. */ - result->tm_wday = (4 + days) % 7; + div_s64_rem(days + 4, 7, &remainder); + result->tm_wday = remainder; if (result->tm_wday < 0) result->tm_wday += 7; -- 2.39.5 (Apple Git-154) ^ permalink raw reply [flat|nested] 5+ messages in thread
* [tip: timers/core] time: Prevent time64_to_tm() day truncation on 32-bit 2026-09-25 16:57 ` [PATCH v2 1/2] " Karl Mehltretter @ 2026-09-29 19:22 ` tip-bot2 for Karl Mehltretter 0 siblings, 0 replies; 5+ messages in thread From: tip-bot2 for Karl Mehltretter @ 2026-09-29 19:22 UTC (permalink / raw) To: linux-tip-commits Cc: Karl Mehltretter, Thomas Gleixner, thomas.weissschuh, x86, linux-kernel The following commit has been merged into the timers/core branch of tip: Commit-ID: 2927f7ca78448781e11a4623d347cd653c28adf6 Gitweb: https://git.kernel.org/tip/2927f7ca78448781e11a4623d347cd653c28adf6 Author: Karl Mehltretter <kmehltretter@gmail.com> AuthorDate: Fri, 25 Sep 2026 18:57:01 +02:00 Committer: Thomas Gleixner <tglx@kernel.org> CommitterDate: Tue, 29 Sep 2026 21:18:42 +02:00 time: Prevent time64_to_tm() day truncation on 32-bit time64_to_tm() accepts a 64-bit seconds value, but stores the quotient in long. On a 32-bit kernel the day count therefore wraps when it reaches 2^31, even though the corresponding year remains representable in struct tm. This is reachable when formatting externally supplied timestamps. For example, NILFS recovery copies the little-endian 64-bit ss_create field from an on-disk segment summary into time64_t. Its sysfs attributes then print that value with %ptTs, whose formatter calls time64_to_tm(). A corrupted image can consequently produce an architecture-dependent printed date. Keep the day quotient in s64 while leaving the seconds-within-day remainder as long. Use div_s64_rem() for the weekday calculation so 32-bit builds do not require compiler runtime division helpers. Fixes: e6c2682a1da3 ("time: Add time64_to_tm()") Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Assisted-by: LLM Reviewed-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Link: https://patch.msgid.link/20260925165702.51677-2-kmehltretter@gmail.com --- kernel/time/timeconv.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/time/timeconv.c b/kernel/time/timeconv.c index 59b922c..aed3af9 100644 --- a/kernel/time/timeconv.c +++ b/kernel/time/timeconv.c @@ -49,8 +49,9 @@ void time64_to_tm(time64_t totalsecs, int offset, struct tm *result) u32 u32tmp, day_of_century, year_of_century, day_of_year, month, day; u64 u64tmp, udays, century, year; bool is_Jan_or_Feb, is_leap_year; - long days, rem; int remainder; + long rem; + s64 days; days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder); rem = remainder; @@ -70,7 +71,8 @@ void time64_to_tm(time64_t totalsecs, int offset, struct tm *result) result->tm_sec = rem % 60; /* January 1, 1970 was a Thursday. */ - result->tm_wday = (4 + days) % 7; + div_s64_rem(days + 4, 7, &remainder); + result->tm_wday = remainder; if (result->tm_wday < 0) result->tm_wday += 7; ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] time/kunit: Add time64_to_tm() case beyond 32-bit day count 2026-09-25 16:57 [PATCH v2 0/2] time: Prevent time64_to_tm() day truncation on 32-bit Karl Mehltretter 2026-09-25 16:57 ` [PATCH v2 1/2] " Karl Mehltretter @ 2026-09-25 16:57 ` Karl Mehltretter 2026-09-29 19:22 ` [tip: timers/core] " tip-bot2 for Karl Mehltretter 1 sibling, 1 reply; 5+ messages in thread From: Karl Mehltretter @ 2026-09-25 16:57 UTC (permalink / raw) To: John Stultz, Thomas Gleixner Cc: Karl Mehltretter, Stephen Boyd, Miroslav Lichvar, Deepa Dinamani, Cassio Neri, Thomas Weißschuh, David Laight, linux-kernel The existing date range test covers +/-80,000 years around the epoch, far below the day count at which a 32-bit long wraps (2^31 days, roughly 5.88 million years). Add a case at exactly 2^31 days after the epoch. Without the preceding fix a 32-bit kernel returns tm_year=-5879541 and wrong month, day, day-of-year and weekday; with it the result matches 64-bit kernels. Assisted-by: LLM Reviewed-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com> --- kernel/time/time_test.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kernel/time/time_test.c b/kernel/time/time_test.c index 1b99180da288..8b718767b3ba 100644 --- a/kernel/time/time_test.c +++ b/kernel/time/time_test.c @@ -87,8 +87,24 @@ static void time64_to_tm_test_date_range(struct kunit *test) } } +static void time64_to_tm_test_wide_day_count(struct kunit *test) +{ + /* 2^31 days: the first count that does not fit in a 32-bit long. */ + time64_t timestamp = (1LL << 31) * 86400; + struct tm result; + + time64_to_tm(timestamp, 0, &result); + + KUNIT_EXPECT_EQ(test, result.tm_year, 5879680); + KUNIT_EXPECT_EQ(test, result.tm_mon, 6); + KUNIT_EXPECT_EQ(test, result.tm_mday, 12); + KUNIT_EXPECT_EQ(test, result.tm_yday, 193); + KUNIT_EXPECT_EQ(test, result.tm_wday, 6); +} + static struct kunit_case time_test_cases[] = { KUNIT_CASE_SLOW(time64_to_tm_test_date_range), + KUNIT_CASE(time64_to_tm_test_wide_day_count), {} }; -- 2.39.5 (Apple Git-154) ^ permalink raw reply [flat|nested] 5+ messages in thread
* [tip: timers/core] time/kunit: Add time64_to_tm() case beyond 32-bit day count 2026-09-25 16:57 ` [PATCH v2 2/2] time/kunit: Add time64_to_tm() case beyond 32-bit day count Karl Mehltretter @ 2026-09-29 19:22 ` tip-bot2 for Karl Mehltretter 0 siblings, 0 replies; 5+ messages in thread From: tip-bot2 for Karl Mehltretter @ 2026-09-29 19:22 UTC (permalink / raw) To: linux-tip-commits Cc: Karl Mehltretter, Thomas Gleixner, thomas.weissschuh, x86, linux-kernel The following commit has been merged into the timers/core branch of tip: Commit-ID: 3945c4a3ea151f777b4b098ebeecd2f85c20244f Gitweb: https://git.kernel.org/tip/3945c4a3ea151f777b4b098ebeecd2f85c20244f Author: Karl Mehltretter <kmehltretter@gmail.com> AuthorDate: Fri, 25 Sep 2026 18:57:02 +02:00 Committer: Thomas Gleixner <tglx@kernel.org> CommitterDate: Tue, 29 Sep 2026 21:18:42 +02:00 time/kunit: Add time64_to_tm() case beyond 32-bit day count The existing date range test covers +/-80,000 years around the epoch, far below the day count at which a 32-bit long wraps (2^31 days, roughly 5.88 million years). Add a case at exactly 2^31 days after the epoch. Without the preceding fix a 32-bit kernel returns tm_year=-5879541 and wrong month, day, day-of-year and weekday; with it the result matches 64-bit kernels. Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Assisted-by: LLM Reviewed-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Link: https://patch.msgid.link/20260925165702.51677-3-kmehltretter@gmail.com --- kernel/time/time_test.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kernel/time/time_test.c b/kernel/time/time_test.c index 1b99180..8b71876 100644 --- a/kernel/time/time_test.c +++ b/kernel/time/time_test.c @@ -87,8 +87,24 @@ static void time64_to_tm_test_date_range(struct kunit *test) } } +static void time64_to_tm_test_wide_day_count(struct kunit *test) +{ + /* 2^31 days: the first count that does not fit in a 32-bit long. */ + time64_t timestamp = (1LL << 31) * 86400; + struct tm result; + + time64_to_tm(timestamp, 0, &result); + + KUNIT_EXPECT_EQ(test, result.tm_year, 5879680); + KUNIT_EXPECT_EQ(test, result.tm_mon, 6); + KUNIT_EXPECT_EQ(test, result.tm_mday, 12); + KUNIT_EXPECT_EQ(test, result.tm_yday, 193); + KUNIT_EXPECT_EQ(test, result.tm_wday, 6); +} + static struct kunit_case time_test_cases[] = { KUNIT_CASE_SLOW(time64_to_tm_test_date_range), + KUNIT_CASE(time64_to_tm_test_wide_day_count), {} }; ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-09-29 19:22 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-09-25 16:57 [PATCH v2 0/2] time: Prevent time64_to_tm() day truncation on 32-bit Karl Mehltretter 2026-09-25 16:57 ` [PATCH v2 1/2] " Karl Mehltretter 2026-09-29 19:22 ` [tip: timers/core] " tip-bot2 for Karl Mehltretter 2026-09-25 16:57 ` [PATCH v2 2/2] time/kunit: Add time64_to_tm() case beyond 32-bit day count Karl Mehltretter 2026-09-29 19:22 ` [tip: timers/core] " tip-bot2 for Karl Mehltretter
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®