All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests: timers: Fix integer overflows in 32-bit mode
@ 2026-06-03  7:55 Wake Liu
  2026-06-03 17:41 ` John Stultz
  0 siblings, 1 reply; 7+ messages in thread
From: Wake Liu @ 2026-06-03  7:55 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Anna-Maria Behnsen,
	Frederic Weisbecker, Shuah Khan
  Cc: Stephen Boyd, linux-kernel, linux-kselftest, cmllamas

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-06-10  3:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-03  7:55 [PATCH] selftests: timers: Fix integer overflows in 32-bit mode Wake Liu
2026-06-03 17:41 ` 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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.