mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Karl Mehltretter <kmehltretter@gmail.com>
To: John Stultz <jstultz@google.com>, Thomas Gleixner <tglx@kernel.org>
Cc: "Karl Mehltretter" <kmehltretter@gmail.com>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Miroslav Lichvar" <mlichvar@redhat.com>,
	"Deepa Dinamani" <deepa.kernel@gmail.com>,
	"Cassio Neri" <cassio.neri@gmail.com>,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	"David Laight" <david.laight.linux@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 0/2] time: Prevent time64_to_tm() day truncation on 32-bit
Date: Fri, 25 Sep 2026 18:57:00 +0200	[thread overview]
Message-ID: <20260925165702.51677-1-kmehltretter@gmail.com> (raw)

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)


             reply	other threads:[~2026-09-25 16:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25 16:57 Karl Mehltretter [this message]
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

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=20260925165702.51677-1-kmehltretter@gmail.com \
    --to=kmehltretter@gmail.com \
    --cc=cassio.neri@gmail.com \
    --cc=david.laight.linux@gmail.com \
    --cc=deepa.kernel@gmail.com \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mlichvar@redhat.com \
    --cc=sboyd@kernel.org \
    --cc=tglx@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    /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

all inboxes | Powered by JetHome®