* [PATCH] selftests/timers: Fix integer overlow errors on 32 bit systems
@ 2025-06-03 20:10 Terry Tritton
2025-06-03 20:27 ` John Stultz
0 siblings, 1 reply; 3+ messages in thread
From: Terry Tritton @ 2025-06-03 20:10 UTC (permalink / raw)
To: John Stultz, Thomas Gleixner, Stephen Boyd, Shuah Khan
Cc: ttritton, linux-kselftest, linux-kernel, Terry Tritton
The use of NSEC_PER_SEC (1000000000L) as defined in include/vdso/time64.h
causes several integer overflow warnings and test errors on 32 bit
architectures.
Use a long long instead of long to prevent integer overflow when
converting seconds to nanoseconds.
Signed-off-by: Terry Tritton <terry.tritton@linaro.org>
---
tools/testing/selftests/timers/adjtick.c | 5 ++++-
tools/testing/selftests/timers/alarmtimer-suspend.c | 4 +++-
tools/testing/selftests/timers/inconsistency-check.c | 4 +++-
tools/testing/selftests/timers/leap-a-day.c | 4 +++-
tools/testing/selftests/timers/mqueue-lat.c | 3 ++-
tools/testing/selftests/timers/nanosleep.c | 4 +++-
tools/testing/selftests/timers/nsleep-lat.c | 4 +++-
tools/testing/selftests/timers/posix_timers.c | 5 ++++-
tools/testing/selftests/timers/raw_skew.c | 4 +++-
tools/testing/selftests/timers/set-2038.c | 4 +++-
tools/testing/selftests/timers/set-timer-lat.c | 4 +++-
tools/testing/selftests/timers/valid-adjtimex.c | 5 ++++-
12 files changed, 38 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/timers/adjtick.c b/tools/testing/selftests/timers/adjtick.c
index 777d9494b683..b5929c33b632 100644
--- a/tools/testing/selftests/timers/adjtick.c
+++ b/tools/testing/selftests/timers/adjtick.c
@@ -22,10 +22,13 @@
#include <sys/time.h>
#include <sys/timex.h>
#include <time.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+#define USEC_PER_SEC 1000000LL
+
#define MILLION 1000000
long systick;
diff --git a/tools/testing/selftests/timers/alarmtimer-suspend.c b/tools/testing/selftests/timers/alarmtimer-suspend.c
index a9ef76ea6051..b5799df271ae 100644
--- a/tools/testing/selftests/timers/alarmtimer-suspend.c
+++ b/tools/testing/selftests/timers/alarmtimer-suspend.c
@@ -28,10 +28,12 @@
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
-#include <include/vdso/time64.h>
#include <errno.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
#define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
#define SUSPEND_SECS 15
diff --git a/tools/testing/selftests/timers/inconsistency-check.c b/tools/testing/selftests/timers/inconsistency-check.c
index 9d1573769d55..2b2d7293b313 100644
--- a/tools/testing/selftests/timers/inconsistency-check.c
+++ b/tools/testing/selftests/timers/inconsistency-check.c
@@ -28,9 +28,11 @@
#include <sys/timex.h>
#include <string.h>
#include <signal.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
#define CLOCK_HWSPECIFIC 10
diff --git a/tools/testing/selftests/timers/leap-a-day.c b/tools/testing/selftests/timers/leap-a-day.c
index 04004a7c0934..008c38ce4b2f 100644
--- a/tools/testing/selftests/timers/leap-a-day.c
+++ b/tools/testing/selftests/timers/leap-a-day.c
@@ -48,9 +48,11 @@
#include <string.h>
#include <signal.h>
#include <unistd.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
#define CLOCK_TAI 11
time_t next_leap;
diff --git a/tools/testing/selftests/timers/mqueue-lat.c b/tools/testing/selftests/timers/mqueue-lat.c
index 63de2334a291..1a6d26f86137 100644
--- a/tools/testing/selftests/timers/mqueue-lat.c
+++ b/tools/testing/selftests/timers/mqueue-lat.c
@@ -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 as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
#define TARGET_TIMEOUT 100000000 /* 100ms in nanoseconds */
#define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c
index 252c6308c569..55ea67478fdd 100644
--- a/tools/testing/selftests/timers/nanosleep.c
+++ b/tools/testing/selftests/timers/nanosleep.c
@@ -27,9 +27,11 @@
#include <sys/timex.h>
#include <string.h>
#include <signal.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
#define CLOCK_HWSPECIFIC 10
diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c
index de23dc0c9f97..347d622987c8 100644
--- a/tools/testing/selftests/timers/nsleep-lat.c
+++ b/tools/testing/selftests/timers/nsleep-lat.c
@@ -24,9 +24,11 @@
#include <sys/timex.h>
#include <string.h>
#include <signal.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
#define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c
index f0eceb0faf34..555bf161f420 100644
--- a/tools/testing/selftests/timers/posix_timers.c
+++ b/tools/testing/selftests/timers/posix_timers.c
@@ -16,11 +16,14 @@
#include <string.h>
#include <unistd.h>
#include <time.h>
-#include <include/vdso/time64.h>
#include <pthread.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+#define USEC_PER_SEC 1000000LL
+
#define DELAY 2
static void __fatal_error(const char *test, const char *name, const char *what)
diff --git a/tools/testing/selftests/timers/raw_skew.c b/tools/testing/selftests/timers/raw_skew.c
index 957f7cd29cb1..ff7675d98560 100644
--- a/tools/testing/selftests/timers/raw_skew.c
+++ b/tools/testing/selftests/timers/raw_skew.c
@@ -25,9 +25,11 @@
#include <sys/time.h>
#include <sys/timex.h>
#include <time.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
#define shift_right(x, s) ({ \
__typeof__(x) __x = (x); \
__typeof__(s) __s = (s); \
diff --git a/tools/testing/selftests/timers/set-2038.c b/tools/testing/selftests/timers/set-2038.c
index ed244315e11c..8130d551a11c 100644
--- a/tools/testing/selftests/timers/set-2038.c
+++ b/tools/testing/selftests/timers/set-2038.c
@@ -27,9 +27,11 @@
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
#define KTIME_MAX ((long long)~((unsigned long long)1 << 63))
#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
diff --git a/tools/testing/selftests/timers/set-timer-lat.c b/tools/testing/selftests/timers/set-timer-lat.c
index 9d8437c13929..79a6a6cba186 100644
--- a/tools/testing/selftests/timers/set-timer-lat.c
+++ b/tools/testing/selftests/timers/set-timer-lat.c
@@ -28,9 +28,11 @@
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+
/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
#define CLOCK_HWSPECIFIC 10
diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
index 6b7801055ad1..e4f31e678630 100644
--- a/tools/testing/selftests/timers/valid-adjtimex.c
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -29,9 +29,12 @@
#include <string.h>
#include <signal.h>
#include <unistd.h>
-#include <include/vdso/time64.h>
#include "../kselftest.h"
+/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
+#define NSEC_PER_SEC 1000000000LL
+#define USEC_PER_SEC 1000000LL
+
#define ADJ_SETOFFSET 0x0100
#include <sys/syscall.h>
--
2.39.5
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/timers: Fix integer overlow errors on 32 bit systems
2025-06-03 20:10 [PATCH] selftests/timers: Fix integer overlow errors on 32 bit systems Terry Tritton
@ 2025-06-03 20:27 ` John Stultz
2025-06-04 8:34 ` Terry Tritton
0 siblings, 1 reply; 3+ messages in thread
From: John Stultz @ 2025-06-03 20:27 UTC (permalink / raw)
To: Terry Tritton
Cc: Thomas Gleixner, Stephen Boyd, Shuah Khan, ttritton,
linux-kselftest, linux-kernel
On Tue, Jun 3, 2025 at 1:10 PM Terry Tritton <terry.tritton@linaro.org> wrote:
>
> The use of NSEC_PER_SEC (1000000000L) as defined in include/vdso/time64.h
> causes several integer overflow warnings and test errors on 32 bit
> architectures.
>
> Use a long long instead of long to prevent integer overflow when
> converting seconds to nanoseconds.
>
> Signed-off-by: Terry Tritton <terry.tritton@linaro.org>
Needs a Fixes: tag?
> ---
> tools/testing/selftests/timers/adjtick.c | 5 ++++-
> tools/testing/selftests/timers/alarmtimer-suspend.c | 4 +++-
> tools/testing/selftests/timers/inconsistency-check.c | 4 +++-
> tools/testing/selftests/timers/leap-a-day.c | 4 +++-
> tools/testing/selftests/timers/mqueue-lat.c | 3 ++-
> tools/testing/selftests/timers/nanosleep.c | 4 +++-
> tools/testing/selftests/timers/nsleep-lat.c | 4 +++-
> tools/testing/selftests/timers/posix_timers.c | 5 ++++-
> tools/testing/selftests/timers/raw_skew.c | 4 +++-
> tools/testing/selftests/timers/set-2038.c | 4 +++-
> tools/testing/selftests/timers/set-timer-lat.c | 4 +++-
> tools/testing/selftests/timers/valid-adjtimex.c | 5 ++++-
> 12 files changed, 38 insertions(+), 12 deletions(-)
>
> diff --git a/tools/testing/selftests/timers/adjtick.c b/tools/testing/selftests/timers/adjtick.c
> index 777d9494b683..b5929c33b632 100644
> --- a/tools/testing/selftests/timers/adjtick.c
> +++ b/tools/testing/selftests/timers/adjtick.c
> @@ -22,10 +22,13 @@
> #include <sys/time.h>
> #include <sys/timex.h>
> #include <time.h>
> -#include <include/vdso/time64.h>
>
> #include "../kselftest.h"
>
> +/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
> +#define NSEC_PER_SEC 1000000000LL
> +#define USEC_PER_SEC 1000000LL
> +
> #define MILLION 1000000
>
> long systick;
> diff --git a/tools/testing/selftests/timers/alarmtimer-suspend.c b/tools/testing/selftests/timers/alarmtimer-suspend.c
> index a9ef76ea6051..b5799df271ae 100644
> --- a/tools/testing/selftests/timers/alarmtimer-suspend.c
> +++ b/tools/testing/selftests/timers/alarmtimer-suspend.c
> @@ -28,10 +28,12 @@
> #include <signal.h>
> #include <stdlib.h>
> #include <pthread.h>
> -#include <include/vdso/time64.h>
> #include <errno.h>
> #include "../kselftest.h"
>
> +/* define NSEC_PER_SEC as long long to avoid overflow on 32 bit architectures*/
> +#define NSEC_PER_SEC 1000000000LL
> +
> #define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
So this seems to be undoing commit 80fa614e2fbc ("selftests: timers:
Remove local NSEC_PER_SEC and USEC_PER_SEC defines")
Would it make more sense to fix the NSEC_PER_SEC definition in time64.h to a LL?
thanks
-john
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/timers: Fix integer overlow errors on 32 bit systems
2025-06-03 20:27 ` John Stultz
@ 2025-06-04 8:34 ` Terry Tritton
0 siblings, 0 replies; 3+ messages in thread
From: Terry Tritton @ 2025-06-04 8:34 UTC (permalink / raw)
To: John Stultz
Cc: Thomas Gleixner, Stephen Boyd, Shuah Khan, ttritton,
linux-kselftest, linux-kernel
> So this seems to be undoing commit 80fa614e2fbc ("selftests: timers:
> Remove local NSEC_PER_SEC and USEC_PER_SEC defines")
Thanks John, I somehow missed 80fa614e2fbc and was wondering how
this had got in.
> Would it make more sense to fix the NSEC_PER_SEC definition in time64.h to a LL?
I was just thinking the same this morning but wasn't sure if this would cause
issues anywhere else.
I'll send a patch to change NSEC_PER_SEC in time64.h as its a lot cleaner
that way.
Thanks
Terry
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-06-04 8:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-03 20:10 [PATCH] selftests/timers: Fix integer overlow errors on 32 bit systems Terry Tritton
2025-06-03 20:27 ` John Stultz
2025-06-04 8:34 ` Terry Tritton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).