selftests: timers: Fix integer overflows in 32-bit mode
Subsystems (3)
PATCH 1/1 selftests: timers: Fix integer overflows in 32-bit mode
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.
--- 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(-)
tools/testing/selftests/timers/alarmtimer-suspend.c +2 -2
@@ -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 15int 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);
tools/testing/selftests/timers/nanosleep.c +1 -1
@@ -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));
tools/testing/selftests/timers/nsleep-lat.c +3 -3
@@ -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 ;
tools/testing/selftests/timers/valid-adjtimex.c +4 -4
@@ -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 ))
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
John Stultz
maintainerQuoted 10 lines · Wake Liu
Several timer tests use
NSEC_PER_SECfor arithmetic. SinceNSEC_PER_SECis defined as 1000000000L invdso/time64.h, it is 32-bit on 32-bit architectures. Multiplications likeNSEC_PER_SEC* 10 or 5 *NSEC_PER_SECoverflow 32-bit signed long.Fix this by using LL suffixes or casting
NSEC_PER_SECto long long to force 64-bit multiplication.Signed-off-by Wake Liu <[email protected]>
I feel like this has come up a few ways since commit 80fa614e2fbc ("selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines"), and I definitely missed in that patch that the include/vdso/time64.h NSEC_PER_SEC is defined as a long instead of a long long.
Setting include/vdso/time64.h NSEC_PER_SEC to a long long probably isn't a great idea as its a value that gets divided & used as a divisor, so we want to keep it cheaper on 32bit systems, but anywhere its multiplied you are really close to an overflow without explicit casts (which has been a pretty good foot gun over the years).
So I'm wondering if it would be simpler to just revert 80fa614e2fbc for the selftests as we don't have to worry about the performance cost of doing the 64bit math everywhere there?
thanks
-john
PATCH 1/1 Revert "selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
This reverts commit 80fa614e2fbcf11069f0995e1601fb2e5702e2f4.
The reverted commit replaced local definitions of NSEC_PER_SEC and USEC_PER_SEC with inclusion of <include/vdso/time64.h>. However, NSEC_PER_SEC in vdso/time64.h is defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic like NSEC_PER_SEC * 10.
Reverting this change restores the local definitions which use unsigned long long (ULL) or long long (LL) suffixes, ensuring 64-bit multiplication and avoiding overflows on 32-bit systems. This also decouples the userspace selftests from internal kernel headers.
--- tools/testing/selftests/timers/Makefile | 2 +- tools/testing/selftests/timers/adjtick.c | 4 +++- .../testing/selftests/timers/alarmtimer-suspend.c | 2 +- .../selftests/timers/inconsistency-check.c | 2 +- tools/testing/selftests/timers/leap-a-day.c | 2 +- tools/testing/selftests/timers/mqueue-lat.c | 2 +- tools/testing/selftests/timers/nanosleep.c | 3 +-- tools/testing/selftests/timers/nsleep-lat.c | 3 ++- tools/testing/selftests/timers/posix_timers.c | 15 ++++++++------- tools/testing/selftests/timers/raw_skew.c | 3 ++- tools/testing/selftests/timers/set-2038.c | 3 ++- tools/testing/selftests/timers/set-timer-lat.c | 3 ++- tools/testing/selftests/timers/valid-adjtimex.c | 4 +++- 13 files changed, 28 insertions(+), 20 deletions(-)
tools/testing/selftests/timers/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir) +CFLAGS += -O3 -Wl,-no-as-needed -Wall LDLIBS += -lrt -lpthread -lm # these are all "safe" tests that don't modify
tools/testing/selftests/timers/adjtick.c +3 -1
@@ -22,10 +22,12 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000 +#define MILLION 1000000long systick;
tools/testing/selftests/timers/alarmtimer-suspend.c +1 -1
@@ -28,10 +28,10 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include <errno.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000ULL#define UNREASONABLE_LAT (NSEC_PER_SEC * 5)/* hopefully we resume in 5 secs */ #define SUSPEND_SECS 15
tools/testing/selftests/timers/inconsistency-check.c +1 -1
@@ -28,13 +28,13 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10#define CALLS_PER_LOOP 64 +#define NSEC_PER_SEC 1000000000ULLchar *clockstring (int clockid) {
tools/testing/selftests/timers/leap-a-day.c +1 -1
@@ -48,9 +48,9 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000ULL#define CLOCK_TAI 11time_t next_leap;
tools/testing/selftests/timers/mqueue-lat.c +1 -1
@@ -29,9 +29,9 @@#include <signal.h> #include <errno.h> #include <mqueue.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000ULL#define TARGET_TIMEOUT 100000000/* 100ms in nanoseconds */ #define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */
tools/testing/selftests/timers/nanosleep.c +1 -2
@@ -27,10 +27,9 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" -/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ +#define NSEC_PER_SEC 1000000000ULL#define CLOCK_HWSPECIFIC 10#define UNSUPPORTED 0xf00f
tools/testing/selftests/timers/nsleep-lat.c +2 -1
@@ -24,9 +24,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000ULL +#define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */ /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
tools/testing/selftests/timers/posix_timers.c +8 -7
@@ -16,13 +16,14 @@#include <string.h> #include <unistd.h> #include <time.h> -#include <include/vdso/time64.h> #include <pthread.h> #include <stdbool.h> #include "kselftest.h" #define DELAY 2 +#define USECS_PER_SEC 1000000 +#define NSECS_PER_SEC 1000000000static void __fatal_error (const char *test,const char *name,const char *what) {
@@ -87,9 +88,9 @@ static int check_diff(struct timeval start, struct timeval end)long long diff; diff = end.tv_usec - start.tv_usec; - diff += (end.tv_sec - start.tv_sec) *USEC_PER_SEC ; + diff += (end.tv_sec - start.tv_sec) *USECS_PER_SEC ; -if (llabs (diff -DELAY *USEC_PER_SEC ) >USEC_PER_SEC /2 ) { +if (llabs (diff -DELAY *USECS_PER_SEC ) >USECS_PER_SEC /2 ) {printf ("Diff too high: %lld.." , diff);return -1 ; }
@@ -449,7 +450,7 @@ static inline int64_t calcdiff_ns(struct timespec t1, struct timespec t2) {int64_t diff; - diff =NSEC_PER_SEC * (int64_t )((int ) t1.tv_sec - (int ) t2.tv_sec); + diff =NSECS_PER_SEC * (int64_t )((int ) t1.tv_sec - (int ) t2.tv_sec); diff += ((int ) t1.tv_nsec - (int ) t2.tv_nsec);return diff; }
@@ -480,7 +481,7 @@ static void check_sigev_none(int which, const char *name)do {if (clock_gettime (which, &now))fatal_error (name,"clock_gettime()" ); - }while (calcdiff_ns (now, start) <NSEC_PER_SEC ); + }while (calcdiff_ns (now, start) <NSECS_PER_SEC );if (timer_gettime (timerid, &its))fatal_error (name,"timer_gettime()" );
@@ -537,7 +538,7 @@ static void check_gettime(int which, const char *name) wraps++; prev = its; - }while (calcdiff_ns (now, start) <NSEC_PER_SEC ); + }while (calcdiff_ns (now, start) <NSECS_PER_SEC );if (timer_delete (timerid))fatal_error (name,"timer_delete()" );
@@ -588,7 +589,7 @@ static void check_overrun(int which, const char *name)do {if (clock_gettime (which, &now))fatal_error (name,"clock_gettime()" ); - }while (calcdiff_ns (now, start) <NSEC_PER_SEC ); + }while (calcdiff_ns (now, start) <NSECS_PER_SEC );/* Unblock it, which should deliver a signal */ if (sigprocmask (SIG_UNBLOCK , &set,NULL ))
tools/testing/selftests/timers/raw_skew.c +2 -1
@@ -25,9 +25,10 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define shift_right (x, s) ({ \ __typeof__(x) __x = (x); \ __typeof__(s) __s = (s); \
tools/testing/selftests/timers/set-2038.c +2 -1
@@ -27,9 +27,10 @@#include <unistd.h> #include <time.h> #include <sys/time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define KTIME_MAX ((long long)~((unsigned long long)1 << 63))#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
tools/testing/selftests/timers/set-timer-lat.c +2 -1
@@ -28,12 +28,13 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include "kselftest.h" /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 + +#define NSEC_PER_SEC 1000000000ULL#define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */ #define TIMER_SECS 1
tools/testing/selftests/timers/valid-adjtimex.c +3 -1
@@ -29,9 +29,11 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define ADJ_SETOFFSET 0x0100#include <sys/syscall.h>
Signature · 1 line
2.54.0.1064.gd145956f57-goog
John Stultz
maintainerRe: [PATCH] Revert "selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
Quoted 9 lines · Wake Liu
This reverts commit
80fa614e2fbcf11069f0995e1601fb2e5702e2f4.The reverted commit replaced local definitions of
NSEC_PER_SECandUSEC_PER_SECwith inclusion of <include/vdso/time64.h>. However,NSEC_PER_SECinvdso/time64.his defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic likeNSEC_PER_SEC* 10.
Quoted 5 lines
Reverting this change restores the local definitions which use unsigned long long (ULL) or long long (LL) suffixes, ensuring 64-bit multiplication and avoiding overflows on 32-bit systems. This also decouples the userspace selftests from internal kernel headers.
Hey Wake,
Thanks for sending this out. While I agree reverting back to local
definitions to ensure we're using 64bit values is better then adding lots of casting, I think a straight revert isn't maybe the best, since the original patch did have value in cleaning up the inconsistencies between tests.
Maybe a "partial" revert would be best, re-adding the local definitions but avoiding the quirks and inconsistencies between the tests.
Quoted 23 lines
tools/testing/selftests/timers/adjtick.c +3
-1@@ -22,10 +22,12 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000 +#define MILLION 1000000long systick;tools/testing/selftests/timers/alarmtimer-suspend.c +0
-0
tools/testing/selftests/timers/alarmtimer-suspend.c line 34
Quoted 8 lines
tools/testing/selftests/timers/alarmtimer-suspend.c +1
-1@@ -28,10 +28,10 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include <errno.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000ULL
It seems like picking LL across the board would probably be a good idea.
Quoted 22 lines
tools/testing/selftests/timers/posix_timers.c +2
-1@@ -16,13 +16,14 @@#include <string.h> #include <unistd.h> #include <time.h> -#include <include/vdso/time64.h> #include <pthread.h> #include <stdbool.h> #include "kselftest.h" #define DELAY 2 +#define USECS_PER_SEC 1000000 +#define NSECS_PER_SEC 1000000000static void __fatal_error (const char *test,const char *name,const char *what) {@@ -87,9 +88,9 @@ static int check_diff(struct timeval start, struct timeval end)long long diff;
tools/testing/selftests/timers/posix_timers.c static int check_diff(struct timeval start, struct timeval end) · line 96
Quoted 10 lines
tools/testing/selftests/timers/posix_timers.c +2
-2diff = end.tv_usec - start.tv_usec; - diff += (end.tv_sec - start.tv_sec) *USEC_PER_SEC ; + diff += (end.tv_sec - start.tv_sec) *USECS_PER_SEC ; -if (llabs (diff -DELAY *USEC_PER_SEC ) >USEC_PER_SEC /2 ) { +if (llabs (diff -DELAY *USECS_PER_SEC ) >USECS_PER_SEC /2 ) {printf ("Diff too high: %lld.." , diff);return -1 ; }
Similarly I don't think re-introducing the plural definitions to the code here is useful. Lets keep the cleanups, but just re-add the local definition.
thanks
-john
PATCH v2 1/1 selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
This partially reverts commit 80fa614e2fbc ("selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines").
The original commit removed local definitions of NSEC_PER_SEC and USEC_PER_SEC in favor of including <include/vdso/time64.h>. However, NSEC_PER_SEC in vdso/time64.h is defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic like NSEC_PER_SEC * 10 on 32-bit systems.
To fix this, restore the local definitions of NSEC_PER_SEC and USEC_PER_SEC in the test files, but use "LL" suffix consistently (1000000000LL and 1000000LL) to ensure 64-bit arithmetic and avoid overflows.
We keep the cleanup from the original commit that renamed plural definitions (NSECS_PER_SEC/USECS_PER_SEC) to singular ones in posix_timers.c, but we now define them locally there as well. This also removes the dependency of the selftests on the internal kernel header <include/vdso/time64.h>.
--- tools/testing/selftests/timers/Makefile | 2 +- tools/testing/selftests/timers/adjtick.c | 5 +++-- tools/testing/selftests/timers/alarmtimer-suspend.c | 3 ++- tools/testing/selftests/timers/inconsistency-check.c | 3 ++- tools/testing/selftests/timers/leap-a-day.c | 3 ++- tools/testing/selftests/timers/mqueue-lat.c | 3 ++- tools/testing/selftests/timers/nanosleep.c | 3 ++- tools/testing/selftests/timers/nsleep-lat.c | 3 ++- tools/testing/selftests/timers/posix_timers.c | 4 +++- tools/testing/selftests/timers/raw_skew.c | 3 ++- tools/testing/selftests/timers/set-2038.c | 3 ++- tools/testing/selftests/timers/set-timer-lat.c | 3 ++- tools/testing/selftests/timers/valid-adjtimex.c | 4 +++- 13 files changed, 28 insertions(+), 14 deletions(-)
tools/testing/selftests/timers/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir) +CFLAGS += -O3 -Wl,-no-as-needed -Wall LDLIBS += -lrt -lpthread -lm # these are all "safe" tests that don't modify
tools/testing/selftests/timers/adjtick.c +3 -2
@@ -22,10 +22,11 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> -#include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define MILLION 1000000long systick;
tools/testing/selftests/timers/alarmtimer-suspend.c +2 -1
@@ -28,10 +28,11 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include <errno.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define UNREASONABLE_LAT (NSEC_PER_SEC * 5)/* hopefully we resume in 5 secs */ #define SUSPEND_SECS 15
tools/testing/selftests/timers/inconsistency-check.c +2 -1
@@ -28,9 +28,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/leap-a-day.c +2 -1
@@ -48,9 +48,10 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define CLOCK_TAI 11time_t next_leap;
tools/testing/selftests/timers/mqueue-lat.c +2 -1
@@ -29,9 +29,10 @@#include <signal.h> #include <errno.h> #include <mqueue.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define TARGET_TIMEOUT 100000000/* 100ms in nanoseconds */ #define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */
tools/testing/selftests/timers/nanosleep.c +2 -1
@@ -27,9 +27,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/nsleep-lat.c +2 -1
@@ -24,9 +24,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */ /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
tools/testing/selftests/timers/posix_timers.c +3 -1
@@ -16,12 +16,14 @@#include <string.h> #include <unistd.h> #include <time.h> -#include <include/vdso/time64.h> #include <pthread.h> #include <stdbool.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define DELAY 2static void __fatal_error (const char *test,const char *name,const char *what)
tools/testing/selftests/timers/raw_skew.c +2 -1
@@ -25,9 +25,10 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define shift_right (x, s) ({ \ __typeof__(x) __x = (x); \ __typeof__(s) __s = (s); \
tools/testing/selftests/timers/set-2038.c +2 -1
@@ -27,9 +27,10 @@#include <unistd.h> #include <time.h> #include <sys/time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define KTIME_MAX ((long long)~((unsigned long long)1 << 63))#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
tools/testing/selftests/timers/set-timer-lat.c +2 -1
@@ -28,9 +28,10 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/valid-adjtimex.c +3 -1
@@ -29,9 +29,11 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define ADJ_SETOFFSET 0x0100#include <sys/syscall.h>
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
Re: [PATCH v2] selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
Quoted 16 lines · Wake Liu
This partially reverts commit
80fa614e2fbc("selftests: timers: Remove localNSEC_PER_SECandUSEC_PER_SECdefines").The original commit removed local definitions of
NSEC_PER_SECandUSEC_PER_SECin favor of including <include/vdso/time64.h>. However,NSEC_PER_SECinvdso/time64.his defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic likeNSEC_PER_SEC* 10 on 32-bit systems.To fix this, restore the local definitions of
NSEC_PER_SECandUSEC_PER_SECin the test files, but use "LL" suffix consistently (1000000000LL and 1000000LL) to ensure 64-bit arithmetic and avoid overflows.
Quoted 7 lines
We keep the cleanup from the original commit that renamed plural definitions (NSECS_PER_SEC/USECS_PER_SEC) to singular ones in
posix_timers.c, but we now define them locally there as well. This also removes the dependency of the selftests on the internal kernel header <include/vdso/time64.h>.Signed-off-by Wake Liu <[email protected]>
This looks ok to me. Thanks for putting this together!
Re: [PATCH v2] selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
Hi John,
Thanks for the review and Ack!
Quoted 24 lines · John Stultz
Quoted 22 lines · Wake Liu
This partially reverts commit
80fa614e2fbc("selftests: timers: Remove localNSEC_PER_SECandUSEC_PER_SECdefines").The original commit removed local definitions of
NSEC_PER_SECandUSEC_PER_SECin favor of including <include/vdso/time64.h>. However,NSEC_PER_SECinvdso/time64.his defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic likeNSEC_PER_SEC* 10 on 32-bit systems.To fix this, restore the local definitions of
NSEC_PER_SECandUSEC_PER_SECin the test files, but use "LL" suffix consistently (1000000000LL and 1000000LL) to ensure 64-bit arithmetic and avoid overflows.We keep the cleanup from the original commit that renamed plural definitions (NSECS_PER_SEC/USECS_PER_SEC) to singular ones in
posix_timers.c, but we now define them locally there as well. This also removes the dependency of the selftests on the internal kernel header <include/vdso/time64.h>.
Quoted 5 lines
Quoted 1 line
Signed-off-by Wake Liu <[email protected]>This looks ok to me. Thanks for putting this together!
Acked-by John Stultz <[email protected]>
--
Best Regards,
Wake Liu
[tip: timers/core] selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
In reply to Wake LiuThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: aca60c717492c858814f74d4403aeb37643eef78 Gitweb: https://git.kernel.org/tip/aca60c717492c858814f74d4403aeb37643eef78 Author: Wake Liu <[email protected]> AuthorDate: Wed, 10 Jun 2026 01:47:21 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 08 Jul 2026 00:21:19 +02:00
selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
This partially reverts commit 80fa614e2fbc ("selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines").
The original commit removed local definitions of NSEC_PER_SEC and USEC_PER_SEC in favor of including <include/vdso/time64.h>. However, NSEC_PER_SEC in vdso/time64.h is defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic like NSEC_PER_SEC * 10 on 32-bit systems.
To fix this, restore the local definitions of NSEC_PER_SEC and USEC_PER_SEC in the test files, but use "LL" suffix consistently (1000000000LL and 1000000LL) to ensure 64-bit arithmetic and avoid overflows.
We keep the cleanup from the original commit that renamed plural definitions (NSECS_PER_SEC/USECS_PER_SEC) to singular ones in posix_timers.c, but we now define them locally there as well. This also removes the dependency of the selftests on the internal kernel header <include/vdso/time64.h>.
--- tools/testing/selftests/timers/Makefile | 2 +- tools/testing/selftests/timers/adjtick.c | 5 +++-- tools/testing/selftests/timers/alarmtimer-suspend.c | 3 ++- tools/testing/selftests/timers/inconsistency-check.c | 3 ++- tools/testing/selftests/timers/leap-a-day.c | 3 ++- tools/testing/selftests/timers/mqueue-lat.c | 3 ++- tools/testing/selftests/timers/nanosleep.c | 3 ++- tools/testing/selftests/timers/nsleep-lat.c | 3 ++- tools/testing/selftests/timers/posix_timers.c | 4 +++- tools/testing/selftests/timers/raw_skew.c | 3 ++- tools/testing/selftests/timers/set-2038.c | 3 ++- tools/testing/selftests/timers/set-timer-lat.c | 3 ++- tools/testing/selftests/timers/valid-adjtimex.c | 4 +++- 13 files changed, 28 insertions(+), 14 deletions(-)
tools/testing/selftests/timers/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir) +CFLAGS += -O3 -Wl,-no-as-needed -Wall LDLIBS += -lrt -lpthread -lm # these are all "safe" tests that don't modify
tools/testing/selftests/timers/adjtick.c +3 -2
@@ -22,10 +22,11 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> -#include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define MILLION 1000000long systick;
tools/testing/selftests/timers/alarmtimer-suspend.c +2 -1
@@ -28,10 +28,11 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include <errno.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define UNREASONABLE_LAT (NSEC_PER_SEC * 5)/* hopefully we resume in 5 secs */ #define SUSPEND_SECS 15
tools/testing/selftests/timers/inconsistency-check.c +2 -1
@@ -28,9 +28,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/leap-a-day.c +2 -1
@@ -51,9 +51,10 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define CLOCK_TAI 11time_t next_leap;
tools/testing/selftests/timers/mqueue-lat.c +2 -1
@@ -29,9 +29,10 @@#include <signal.h> #include <errno.h> #include <mqueue.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define TARGET_TIMEOUT 100000000/* 100ms in nanoseconds */ #define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */
tools/testing/selftests/timers/nanosleep.c +2 -1
@@ -27,9 +27,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/nsleep-lat.c +2 -1
@@ -24,9 +24,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */ /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
tools/testing/selftests/timers/posix_timers.c +3 -1
@@ -16,12 +16,14 @@#include <string.h> #include <unistd.h> #include <time.h> -#include <include/vdso/time64.h> #include <pthread.h> #include <stdbool.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define DELAY 2static void __fatal_error (const char *test,const char *name,const char *what)
tools/testing/selftests/timers/raw_skew.c +2 -1
@@ -25,9 +25,10 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define shift_right (x, s) ({ \ __typeof__(x) __x = (x); \ __typeof__(s) __s = (s); \
tools/testing/selftests/timers/set-2038.c +2 -1
@@ -27,9 +27,10 @@#include <unistd.h> #include <time.h> #include <sys/time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define KTIME_MAX ((long long)~((unsigned long long)1 << 63))#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
tools/testing/selftests/timers/set-timer-lat.c +2 -1
@@ -28,9 +28,10 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/valid-adjtimex.c +3 -1
@@ -29,9 +29,11 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define ADJ_SETOFFSET 0x0100#include <sys/syscall.h>
[tip: timers/core] selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
In reply to Wake LiuThe following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: f44ce7fdbdd00a596455b8689b62c12d5530d610 Gitweb: https://git.kernel.org/tip/f44ce7fdbdd00a596455b8689b62c12d5530d610 Author: Wake Liu <[email protected]> AuthorDate: Wed, 10 Jun 2026 01:47:21 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 10 Jul 2026 09:20:55 +02:00
selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines"
This partially reverts commit 80fa614e2fbc ("selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines").
The original commit removed local definitions of NSEC_PER_SEC and USEC_PER_SEC in favor of including <include/vdso/time64.h>. However, NSEC_PER_SEC in vdso/time64.h is defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic like NSEC_PER_SEC * 10 on 32-bit systems.
To fix this, restore the local definitions of NSEC_PER_SEC and USEC_PER_SEC in the test files, but use "LL" suffix consistently (1000000000LL and 1000000LL) to ensure 64-bit arithmetic and avoid overflows.
We keep the cleanup from the original commit that renamed plural definitions (NSECS_PER_SEC/USECS_PER_SEC) to singular ones in posix_timers.c, but we now define them locally there as well. This also removes the dependency of the selftests on the internal kernel header <include/vdso/time64.h>.
--- tools/testing/selftests/timers/Makefile | 2 +- tools/testing/selftests/timers/adjtick.c | 5 +++-- tools/testing/selftests/timers/alarmtimer-suspend.c | 3 ++- tools/testing/selftests/timers/inconsistency-check.c | 3 ++- tools/testing/selftests/timers/leap-a-day.c | 3 ++- tools/testing/selftests/timers/mqueue-lat.c | 3 ++- tools/testing/selftests/timers/nanosleep.c | 3 ++- tools/testing/selftests/timers/nsleep-lat.c | 3 ++- tools/testing/selftests/timers/posix_timers.c | 4 +++- tools/testing/selftests/timers/raw_skew.c | 3 ++- tools/testing/selftests/timers/set-2038.c | 3 ++- tools/testing/selftests/timers/set-timer-lat.c | 3 ++- tools/testing/selftests/timers/valid-adjtimex.c | 4 +++- 13 files changed, 28 insertions(+), 14 deletions(-)
tools/testing/selftests/timers/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir) +CFLAGS += -O3 -Wl,-no-as-needed -Wall LDLIBS += -lrt -lpthread -lm # these are all "safe" tests that don't modify
tools/testing/selftests/timers/adjtick.c +3 -2
@@ -22,10 +22,11 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> -#include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define MILLION 1000000long systick;
tools/testing/selftests/timers/alarmtimer-suspend.c +2 -1
@@ -28,10 +28,11 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include <errno.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define UNREASONABLE_LAT (NSEC_PER_SEC * 5)/* hopefully we resume in 5 secs */ #define SUSPEND_SECS 15
tools/testing/selftests/timers/inconsistency-check.c +2 -1
@@ -28,9 +28,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/leap-a-day.c +2 -1
@@ -51,9 +51,10 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define CLOCK_TAI 11time_t next_leap;
tools/testing/selftests/timers/mqueue-lat.c +2 -1
@@ -29,9 +29,10 @@#include <signal.h> #include <errno.h> #include <mqueue.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define TARGET_TIMEOUT 100000000/* 100ms in nanoseconds */ #define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */
tools/testing/selftests/timers/nanosleep.c +2 -1
@@ -27,9 +27,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/nsleep-lat.c +2 -1
@@ -24,9 +24,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */ /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
tools/testing/selftests/timers/posix_timers.c +3 -1
@@ -16,12 +16,14 @@#include <string.h> #include <unistd.h> #include <time.h> -#include <include/vdso/time64.h> #include <pthread.h> #include <stdbool.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define DELAY 2static void __fatal_error (const char *test,const char *name,const char *what)
tools/testing/selftests/timers/raw_skew.c +2 -1
@@ -25,9 +25,10 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define shift_right (x, s) ({ \ __typeof__(x) __x = (x); \ __typeof__(s) __s = (s); \
tools/testing/selftests/timers/set-2038.c +2 -1
@@ -27,9 +27,10 @@#include <unistd.h> #include <time.h> #include <sys/time.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define KTIME_MAX ((long long)~((unsigned long long)1 << 63))#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
tools/testing/selftests/timers/set-timer-lat.c +2 -1
@@ -28,9 +28,10 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10
tools/testing/selftests/timers/valid-adjtimex.c +3 -1
@@ -29,9 +29,11 @@#include <string.h> #include <signal.h> #include <unistd.h> -#include <include/vdso/time64.h> #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL +#define ADJ_SETOFFSET 0x0100#include <sys/syscall.h>