[PATCH 07/12] tools/nolibc: prefer explicit 64-bit time-related system calls

Thomas Weißschuh posted 12 patches 3 months, 1 week ago
[PATCH 07/12] tools/nolibc: prefer explicit 64-bit time-related system calls
Posted by Thomas Weißschuh 3 months, 1 week ago
Make sure to always use the 64-bit safe system calls
in preparation for 64-bit time_t on 32-bit architectures.

Also prevent issues on kernels which disable CONFIG_COMPAT_32BIT_TIME
and therefore don't provide the 32-bit system calls anymore.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/include/nolibc/poll.h | 12 ++++++------
 tools/include/nolibc/sys.h  | 10 +++++-----
 tools/include/nolibc/time.h | 36 ++++++++++++++++++------------------
 3 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/tools/include/nolibc/poll.h b/tools/include/nolibc/poll.h
index 0d053f93ea99..5ff911b73b34 100644
--- a/tools/include/nolibc/poll.h
+++ b/tools/include/nolibc/poll.h
@@ -23,22 +23,22 @@
 static __attribute__((unused))
 int sys_poll(struct pollfd *fds, int nfds, int timeout)
 {
-#if defined(__NR_ppoll)
-	struct timespec t;
+#if defined(__NR_ppoll_time64)
+	struct __kernel_timespec t;
 
 	if (timeout >= 0) {
 		t.tv_sec  = timeout / 1000;
 		t.tv_nsec = (timeout % 1000) * 1000000;
 	}
-	return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
-#elif defined(__NR_ppoll_time64)
-	struct __kernel_timespec t;
+	return my_syscall5(__NR_ppoll_time64, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
+#elif defined(__NR_ppoll)
+	struct timespec t;
 
 	if (timeout >= 0) {
 		t.tv_sec  = timeout / 1000;
 		t.tv_nsec = (timeout % 1000) * 1000000;
 	}
-	return my_syscall5(__NR_ppoll_time64, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
+	return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
 #else
 	return my_syscall3(__NR_poll, fds, nfds, timeout);
 #endif
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index e91b7d947161..10c517a38f86 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -772,22 +772,22 @@ int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeva
 	return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
 #elif defined(__NR_select)
 	return my_syscall5(__NR_select, nfds, rfds, wfds, efds, timeout);
-#elif defined(__NR_pselect6)
-	struct timespec t;
+#elif defined(__NR_pselect6_time64)
+	struct __kernel_timespec t;
 
 	if (timeout) {
 		t.tv_sec  = timeout->tv_sec;
 		t.tv_nsec = timeout->tv_usec * 1000;
 	}
-	return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
+	return my_syscall6(__NR_pselect6_time64, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
 #else
-	struct __kernel_timespec t;
+	struct timespec t;
 
 	if (timeout) {
 		t.tv_sec  = timeout->tv_sec;
 		t.tv_nsec = timeout->tv_usec * 1000;
 	}
-	return my_syscall6(__NR_pselect6_time64, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
+	return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
 #endif
 }
 
diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h
index 48e78f8becf9..45df9b09d7b6 100644
--- a/tools/include/nolibc/time.h
+++ b/tools/include/nolibc/time.h
@@ -43,9 +43,7 @@ void __nolibc_timespec_kernel_to_user(const struct __kernel_timespec *kts, struc
 static __attribute__((unused))
 int sys_clock_getres(clockid_t clockid, struct timespec *res)
 {
-#if defined(__NR_clock_getres)
-	return my_syscall2(__NR_clock_getres, clockid, res);
-#else
+#if defined(__NR_clock_getres_time64)
 	struct __kernel_timespec kres;
 	int ret;
 
@@ -53,6 +51,8 @@ int sys_clock_getres(clockid_t clockid, struct timespec *res)
 	if (res)
 		__nolibc_timespec_kernel_to_user(&kres, res);
 	return ret;
+#else
+	return my_syscall2(__NR_clock_getres, clockid, res);
 #endif
 }
 
@@ -65,9 +65,7 @@ int clock_getres(clockid_t clockid, struct timespec *res)
 static __attribute__((unused))
 int sys_clock_gettime(clockid_t clockid, struct timespec *tp)
 {
-#if defined(__NR_clock_gettime)
-	return my_syscall2(__NR_clock_gettime, clockid, tp);
-#else
+#if defined(__NR_clock_gettime64)
 	struct __kernel_timespec ktp;
 	int ret;
 
@@ -75,6 +73,8 @@ int sys_clock_gettime(clockid_t clockid, struct timespec *tp)
 	if (tp)
 		__nolibc_timespec_kernel_to_user(&ktp, tp);
 	return ret;
+#else
+	return my_syscall2(__NR_clock_gettime, clockid, tp);
 #endif
 }
 
@@ -87,13 +87,13 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
 static __attribute__((unused))
 int sys_clock_settime(clockid_t clockid, struct timespec *tp)
 {
-#if defined(__NR_clock_settime)
-	return my_syscall2(__NR_clock_settime, clockid, tp);
-#else
+#if defined(__NR_clock_settime64)
 	struct __kernel_timespec ktp;
 
 	__nolibc_timespec_user_to_kernel(tp, &ktp);
 	return my_syscall2(__NR_clock_settime64, clockid, &ktp);
+#else
+	return my_syscall2(__NR_clock_settime, clockid, tp);
 #endif
 }
 
@@ -107,9 +107,7 @@ static __attribute__((unused))
 int sys_clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp,
 			struct timespec *rmtp)
 {
-#if defined(__NR_clock_nanosleep)
-	return my_syscall4(__NR_clock_nanosleep, clockid, flags, rqtp, rmtp);
-#else
+#if defined(__NR_clock_nanosleep_time64)
 	struct __kernel_timespec krqtp, krmtp;
 	int ret;
 
@@ -118,6 +116,8 @@ int sys_clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqt
 	if (rmtp)
 		__nolibc_timespec_kernel_to_user(&krmtp, rmtp);
 	return ret;
+#else
+	return my_syscall4(__NR_clock_nanosleep, clockid, flags, rqtp, rmtp);
 #endif
 }
 
@@ -189,9 +189,7 @@ int timer_delete(timer_t timerid)
 static __attribute__((unused))
 int sys_timer_gettime(timer_t timerid, struct itimerspec *curr_value)
 {
-#if defined(__NR_timer_gettime)
-	return my_syscall2(__NR_timer_gettime, timerid, curr_value);
-#else
+#if defined(__NR_timer_gettime64)
 	struct __kernel_itimerspec kcurr_value;
 	int ret;
 
@@ -199,6 +197,8 @@ int sys_timer_gettime(timer_t timerid, struct itimerspec *curr_value)
 	__nolibc_timespec_kernel_to_user(&kcurr_value.it_interval, &curr_value->it_interval);
 	__nolibc_timespec_kernel_to_user(&kcurr_value.it_value, &curr_value->it_value);
 	return ret;
+#else
+	return my_syscall2(__NR_timer_gettime, timerid, curr_value);
 #endif
 }
 
@@ -212,9 +212,7 @@ static __attribute__((unused))
 int sys_timer_settime(timer_t timerid, int flags,
 		      const struct itimerspec *new_value, struct itimerspec *old_value)
 {
-#if defined(__NR_timer_settime)
-	return my_syscall4(__NR_timer_settime, timerid, flags, new_value, old_value);
-#else
+#if defined(__NR_timer_settime64)
 	struct __kernel_itimerspec knew_value, kold_value;
 	int ret;
 
@@ -226,6 +224,8 @@ int sys_timer_settime(timer_t timerid, int flags,
 		__nolibc_timespec_kernel_to_user(&kold_value.it_value, &old_value->it_value);
 	}
 	return ret;
+#else
+	return my_syscall4(__NR_timer_settime, timerid, flags, new_value, old_value);
 #endif
 }
 

-- 
2.51.1.dirty

Re: [PATCH 07/12] tools/nolibc: prefer explicit 64-bit time-related system calls
Posted by Arnd Bergmann 3 months, 1 week ago
On Wed, Oct 29, 2025, at 17:02, Thomas Weißschuh wrote:
> Make sure to always use the 64-bit safe system calls
> in preparation for 64-bit time_t on 32-bit architectures.
>
> Also prevent issues on kernels which disable CONFIG_COMPAT_32BIT_TIME
> and therefore don't provide the 32-bit system calls anymore.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Thanks for working on this!

> -#if defined(__NR_ppoll)
> -	struct timespec t;
> +#if defined(__NR_ppoll_time64)
> +	struct __kernel_timespec t;
> 
>  	if (timeout >= 0) {
>  		t.tv_sec  = timeout / 1000;
>  		t.tv_nsec = (timeout % 1000) * 1000000;
>  	}
> -	return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, 
> NULL, 0);
> +	return my_syscall5(__NR_ppoll_time64, fds, nfds, (timeout >= 0) ? &t 

This looks good to me.

> -#elif defined(__NR_ppoll_time64)
> -	struct __kernel_timespec t;
> : NULL, NULL, 0);
> +#elif defined(__NR_ppoll)
> +	struct timespec t;
> 
>  	if (timeout >= 0) {
>  		t.tv_sec  = timeout / 1000;
>  		t.tv_nsec = (timeout % 1000) * 1000000;
>  	}

This is not wrong, but for consistency, I would use
__kernel_old_timespec with the old syscall macros, rather
than the nolibc-defined type.

A different approach would be to rely on timespec/timeval/time_t
to always use the 64-bit types and then just pick the time64
macros on 32-bit vs the old macros on 64-bit builds.

> -	return my_syscall5(__NR_ppoll_time64, fds, nfds, (timeout >= 0) ? &t 
> : NULL, NULL, 0);
> +	return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, 
> NULL, 0);
>  #else
>  	return my_syscall3(__NR_poll, fds, nfds, timeout);
>  #endif

I would think that we can remove the final #else clause here
and just use the __NR_ppoll case as #else. It would also make
sense to change the first #if to check for a 32-bit ABI.

> diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> index e91b7d947161..10c517a38f86 100644
> --- a/tools/include/nolibc/sys.h
> +++ b/tools/include/nolibc/sys.h
> @@ -772,22 +772,22 @@ int sys_select(int nfds, fd_set *rfds, fd_set 
> *wfds, fd_set *efds, struct timeva
>  	return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
>  #elif defined(__NR_select)
>  	return my_syscall5(__NR_select, nfds, rfds, wfds, efds, timeout);
> -#elif defined(__NR_pselect6)
> -	struct timespec t;
> +#elif defined(__NR_pselect6_time64)
> +	struct __kernel_timespec t;

These probably need to be flipped around, so that
__NR_pselect6_time64/__NR_pselect6 comes first because the other
ones use the wrong type on 32-bit targets.

Probably also do the same thing here with the #ifdef checking
the architecture instead of the syscall macro.

       Arnd