From: Wake Liu <wakel@google.com>
To: John Stultz <jstultz@google.com>,
Thomas Gleixner <tglx@kernel.org>,
Anna-Maria Behnsen <anna-maria@linutronix.de>,
Frederic Weisbecker <frederic@kernel.org>,
Shuah Khan <shuah@kernel.org>
Cc: Stephen Boyd <sboyd@kernel.org>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
cmllamas@google.com
Subject: [PATCH] selftests: timers: Fix integer overflows in 32-bit mode
Date: Wed, 3 Jun 2026 07:55:40 +0000 [thread overview]
Message-ID: <20260603075540.659524-1-wakel@google.com> (raw)
Several timer tests use NSEC_PER_SEC for arithmetic. Since NSEC_PER_SEC
is defined as 1000000000L in vdso/time64.h, it is 32-bit on 32-bit
architectures. Multiplications like NSEC_PER_SEC * 10 or 5 * NSEC_PER_SEC
overflow 32-bit signed long.
Fix this by using LL suffixes or casting NSEC_PER_SEC to long long to
force 64-bit multiplication.
Signed-off-by: Wake Liu <wakel@google.com>
---
tools/testing/selftests/timers/alarmtimer-suspend.c | 4 ++--
tools/testing/selftests/timers/nanosleep.c | 2 +-
tools/testing/selftests/timers/nsleep-lat.c | 6 +++---
tools/testing/selftests/timers/valid-adjtimex.c | 8 ++++----
4 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/timers/alarmtimer-suspend.c b/tools/testing/selftests/timers/alarmtimer-suspend.c
index aa66c805f6a4..a000688d5e23 100644
--- a/tools/testing/selftests/timers/alarmtimer-suspend.c
+++ b/tools/testing/selftests/timers/alarmtimer-suspend.c
@@ -32,7 +32,7 @@
#include <errno.h>
#include "kselftest.h"
-#define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
+#define UNREASONABLE_LAT (NSEC_PER_SEC * 5LL) /* hopefully we resume in 5 secs */
#define SUSPEND_SECS 15
int alarmcount;
@@ -89,7 +89,7 @@ void sigalarm(int signo)
alarmcount++;
delta_ns = timespec_sub(start_time, ts);
- delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
+ delta_ns -= (long long)NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount, ts.tv_sec,
ts.tv_nsec, delta_ns);
diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c
index a054680b3372..fcdc649ec7b4 100644
--- a/tools/testing/selftests/timers/nanosleep.c
+++ b/tools/testing/selftests/timers/nanosleep.c
@@ -188,7 +188,7 @@ int main(int argc, char **argv)
fflush(stdout);
length = 10;
- while (length <= (NSEC_PER_SEC * 10)) {
+ while (length <= (NSEC_PER_SEC * 10LL)) {
ret = nanosleep_test(clockid, length);
if (ret == UNSUPPORTED) {
ksft_test_result_skip("%-31s\n", clockstring(clockid));
diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c
index a7ba1eb1e21b..c49133afe270 100644
--- a/tools/testing/selftests/timers/nsleep-lat.c
+++ b/tools/testing/selftests/timers/nsleep-lat.c
@@ -76,9 +76,9 @@ struct timespec timespec_add(struct timespec ts, unsigned long long ns)
long long timespec_sub(struct timespec a, struct timespec b)
{
- long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
+ long long ret = (long long)NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
- ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
+ ret -= (long long)NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
return ret;
}
@@ -146,7 +146,7 @@ int main(int argc, char **argv)
continue;
length = 10;
- while (length <= (NSEC_PER_SEC * 10)) {
+ while (length <= (NSEC_PER_SEC * 10LL)) {
ret = nanosleep_lat_test(clockid, length);
if (ret)
break;
diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
index e1e56d3097d6..8f10d64f0c96 100644
--- a/tools/testing/selftests/timers/valid-adjtimex.c
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -260,16 +260,16 @@ int validate_set_offset(void)
if (set_offset(-NSEC_PER_SEC - 1, 1))
return -1;
- if (set_offset(5 * NSEC_PER_SEC, 1))
+ if (set_offset(5LL * NSEC_PER_SEC, 1))
return -1;
- if (set_offset(-5 * NSEC_PER_SEC, 1))
+ if (set_offset(-5LL * NSEC_PER_SEC, 1))
return -1;
- if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+ if (set_offset(5LL * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
return -1;
- if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+ if (set_offset(-5LL * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
return -1;
if (set_offset(USEC_PER_SEC - 1, 0))
--
2.54.0.1013.g208068f2d8-goog
next reply other threads:[~2026-06-03 7:55 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-03 7:55 Wake Liu [this message]
2026-06-03 17:41 ` [PATCH] selftests: timers: Fix integer overflows in 32-bit mode John Stultz
2026-06-09 7:56 ` [PATCH] Revert "selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines" Wake Liu
2026-06-10 1:28 ` John Stultz
2026-06-10 1:47 ` [PATCH v2] selftests: timers: Partially revert "Remove " Wake Liu
2026-06-10 2:00 ` John Stultz
2026-06-10 3:13 ` Wake Liu
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=20260603075540.659524-1-wakel@google.com \
--to=wakel@google.com \
--cc=anna-maria@linutronix.de \
--cc=cmllamas@google.com \
--cc=frederic@kernel.org \
--cc=jstultz@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=shuah@kernel.org \
--cc=tglx@kernel.org \
/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