Revert "selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines"

This reverts commit 80fa614e2f.

This commit introduced several integer overflow issues in kselftests
involving arithmetic with NSEC_PER_SEC for 32-bit architectures.
Furthermore, it replaced the local definitions in favor of a kernel's
internal header, which is not a good practice. Revert it here now since
the generated warnings block our "spotless" presubmit. I'll follow up
with a possible revert upstream (or some other solution).

  $ make -C tools/testing/selftests/ LLVM=1 ARCH=arm
  [...]
  timers/nanosleep.c:191:34: warning: overflow in expression; result is 1'410'065'408 with type 'long' [-Winteger-overflow]
    191 |                 while (length <= (NSEC_PER_SEC * 10)) {
        |

Resolves trivial revert conflicts in:
	tools/testing/selftests/timers/alarmtimer-suspend.c
	tools/testing/selftests/timers/inconsistency-check.c
	tools/testing/selftests/timers/leap-a-day.c
	tools/testing/selftests/timers/mqueue-lat.c
	tools/testing/selftests/timers/nanosleep.c
	tools/testing/selftests/timers/nsleep-lat.c
	tools/testing/selftests/timers/raw_skew.c
	tools/testing/selftests/timers/set-2038.c
	tools/testing/selftests/timers/set-timer-lat.c
	tools/testing/selftests/timers/valid-adjtimex.c

Bug: 444295917
Cc: Wake Liu <wakel@google.com>
Cc: John Stultz <jstultz@google.com>
Change-Id: Iaa862f2d24f9edd07dc30691fd4c54c28f9dc693
Signed-off-by: Carlos Llamas <cmllamas@google.com>
This commit is contained in:
Carlos Llamas committed 2026-01-07 23:56:39 +00:00
1 parent 1c1b9bfb4a
commit 127c77fc4e
13 files changed
+29 -19

No files matched your search

+1 -1
View File
@@ -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
+3 -1
View File
@@ -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 1000000
long systick;
@@ -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
@@ -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 1000000000ULL
char *clockstring(int clockid)
{
+1 -1
View File
@@ -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 11
time_t next_leap;
+1 -1
View File
@@ -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 */
+2 -1
View File
@@ -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 1000000000ULL
/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
#define CLOCK_HWSPECIFIC 10
+2 -1
View File
@@ -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) */
@@ -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 1000000000
static 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))
+2 -1
View File
@@ -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); \
+2 -1
View File
@@ -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)
@@ -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
@@ -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>