FROMLIST: Fix an integer overflow issue in the NSEC_PER_SEC constant

Bug: 444295917
Link: https://lore.kernel.org/all/20250915191944.9779-1-wakel@google.com/
Signed-off-by: Wake Liu <wakel@google.com>
Test: bazel build //common:kernel_aarch64_dist
Change-Id: I75737c02f96d686b8e9c1697a0af1fe4332e3cc6
This commit is contained in:
Wake Liu authored and Treehugger Robot committed 2025-09-17 01:56:00 -07:00
1 parent 51c5f5f870
commit 062cbc4876
6 files changed
+44 -25

No files matched your search

+1
View File
@@ -2409,6 +2409,7 @@ cc_library(
name = "kselftest_timers_common_hdrs",
hdrs = [
"include/vdso/time64.h",
"tools/testing/selftests/timers/helpers.h",
],
copts = _KSELFTEST_COPTS,
includes = ["."],
@@ -31,8 +31,9 @@
#include <include/vdso/time64.h>
#include <errno.h>
#include "../kselftest.h"
#include "helpers.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;
@@ -70,14 +71,6 @@ char *clockstring(int clockid)
}
long long timespec_sub(struct timespec a, struct timespec b)
{
long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
return ret;
}
int final_ret;
void sigalarm(int signo)
@@ -88,8 +81,8 @@ void sigalarm(int signo)
clock_gettime(alarm_clock_id, &ts);
alarmcount++;
delta_ns = timespec_sub(start_time, ts);
delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
delta_ns = timespec_sub(ts, start_time);
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);
+32
View File
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef SELFTESTS_TIMERS_HELPERS_H
#define SELFTESTS_TIMERS_HELPERS_H
#include <time.h>
#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000L
#endif
/*
* timespec_to_ns - Convert timespec to nanoseconds
*/
static inline long long timespec_to_ns(const struct timespec *ts)
{
return ((long long) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
}
/*
* timespec_sub - Subtract two timespec values
*
* @a: first timespec
* @b: second timespec
*
* Returns a - b in nanoseconds.
*/
static inline long long timespec_sub(struct timespec a, struct timespec b)
{
return timespec_to_ns(&a) - timespec_to_ns(&b);
}
#endif /* SELFTESTS_TIMERS_HELPERS_H */
+1 -1
View File
@@ -138,7 +138,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));
+2 -9
View File
@@ -26,6 +26,7 @@
#include <signal.h>
#include <include/vdso/time64.h>
#include "../kselftest.h"
#include "helpers.h"
#define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
@@ -74,14 +75,6 @@ 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;
ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
return ret;
}
int nanosleep_lat_test(int clockid, long long ns)
{
struct timespec start, end, target;
@@ -146,7 +139,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;
@@ -264,16 +264,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))