[PATCH v2 1/5] nptl: use futex_time64 for lll_futex_syscall() if possible
Arnd Bergmann
arnd@kernel.org
Mon Nov 16 12:08:53 GMT 2026
From: Arnd Bergmann <arnd@arndb.de>
When __ASSUME_TIME64_SYSCALLS is set, __NR_futex_time64 is always
available, so use that in place of __NR_futex for 32-bit architectures
regardless of __TIMESIZE. The lll_futex_* operations never pass a
timeout, so both variants behave the same.
This covers all lll_futex_syscall() users, plus lll_futex_wait_cancel()
(used by the old sem_wait@GLIBC_2.0) and the FUTEX_TRYLOCK_PI call in
pthread_mutex_trylock(), which now goes through lll_futex_syscall()
instead of calling futex directly. The __NR_futex_time64 fallback
definition moves from futex-internal.h to lowlevellock-futex.h.
Remove the slightly ambiguous lll_futex_timed_wait() macro that
could take either a timespec or __timespec64 argument depending
on the architecture. This was harmless since the only caller
always passes a NULL timeout. All users that do care about the
timeout were already calling helpers from futex-internal.c
which handles this correctly.
This is required for running on __TIMESIZE==32 architectures on
kernels built with CONFIG_COMPAT_32BIT_TIME=n.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
nptl/pthread_mutex_trylock.c | 9 ++++-----
sysdeps/nptl/futex-internal.h | 8 ++------
sysdeps/nptl/lowlevellock-futex.h | 22 +++++++++++++++-------
3 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/nptl/pthread_mutex_trylock.c b/nptl/pthread_mutex_trylock.c
index 1a61e141517e..d9beb06e73d5 100644
--- a/nptl/pthread_mutex_trylock.c
+++ b/nptl/pthread_mutex_trylock.c
@@ -285,12 +285,11 @@ ___pthread_mutex_trylock (pthread_mutex_t *mutex)
int private = (robust
? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
: PTHREAD_MUTEX_PSHARED (mutex));
- int e = INTERNAL_SYSCALL_CALL (futex, &mutex->__data.__lock,
- __lll_private_flag (FUTEX_TRYLOCK_PI,
- private), 0, 0);
+ int e = lll_futex_syscall (4, &mutex->__data.__lock,
+ __lll_private_flag (FUTEX_TRYLOCK_PI,
+ private), 0, 0);
- if (INTERNAL_SYSCALL_ERROR_P (e)
- && INTERNAL_SYSCALL_ERRNO (e) == EWOULDBLOCK)
+ if (e == -EWOULDBLOCK)
{
/* The kernel has not yet finished the mutex owner death.
We do not need to ensure ordering wrt another memory
diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h
index 502f89476a24..f4c917980eca 100644
--- a/sysdeps/nptl/futex-internal.h
+++ b/sysdeps/nptl/futex-internal.h
@@ -69,10 +69,6 @@
# error FUTEX_PRIVATE must be equal to 0
#endif
-#ifndef __NR_futex_time64
-# define __NR_futex_time64 __NR_futex
-#endif
-
/* Calls __libc_fatal with an error message. Convenience function for
concrete implementations of the futex interface. */
static __always_inline __attribute__ ((__noreturn__)) void
@@ -123,7 +119,7 @@ futex_fatal_error (void)
static __always_inline int
futex_wait (unsigned int *futex_word, unsigned int expected, int private)
{
- int err = lll_futex_timed_wait (futex_word, expected, NULL, private);
+ int err = lll_futex_wait (futex_word, expected, private);
switch (err)
{
case 0:
@@ -242,7 +238,7 @@ int __futex_lock_pi64 (int *futex_word, clockid_t clockid,
static __always_inline int
futex_unlock_pi (unsigned int *futex_word, int private)
{
- int err = lll_futex_timed_unlock_pi (futex_word, private);
+ int err = lll_futex_unlock_pi (futex_word, private);
switch (err)
{
case 0:
diff --git a/sysdeps/nptl/lowlevellock-futex.h b/sysdeps/nptl/lowlevellock-futex.h
index 640861350101..f5d1162edb4b 100644
--- a/sysdeps/nptl/lowlevellock-futex.h
+++ b/sysdeps/nptl/lowlevellock-futex.h
@@ -53,9 +53,20 @@
# define __lll_private_flag(fl, private) \
(((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
+# ifndef __NR_futex_time64
+# define __NR_futex_time64 __NR_futex
+# endif
+
+/* The operations below never pass a timeout, so both futex variants work. */
+# ifdef __ASSUME_TIME64_SYSCALLS
+# define __NR_lll_futex __NR_futex_time64
+# else
+# define __NR_lll_futex __NR_futex
+# endif
+
# define lll_futex_syscall(nargs, futexp, op, ...) \
({ \
- long int __ret = INTERNAL_SYSCALL (futex, nargs, futexp, op, \
+ long int __ret = INTERNAL_SYSCALL (lll_futex, nargs, futexp, op, \
__VA_ARGS__); \
(__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret)) \
? -INTERNAL_SYSCALL_ERRNO (__ret) : 0); \
@@ -70,12 +81,9 @@
/* Wait while *FUTEXP == VAL for an lll_futex_wake call on FUTEXP. */
# define lll_futex_wait(futexp, val, private) \
- lll_futex_timed_wait (futexp, val, NULL, private)
-
-# define lll_futex_timed_wait(futexp, val, timeout, private) \
lll_futex_syscall (4, futexp, \
__lll_private_flag (FUTEX_WAIT, private), \
- val, timeout)
+ val, NULL)
/* Verify whether the supplied clockid is supported by
lll_futex_clock_wait_bitset. */
@@ -104,7 +112,7 @@
FUTEX_OP_CLEAR_WAKE_IF_GT_ONE)
-#define lll_futex_timed_unlock_pi(futexp, private) \
+#define lll_futex_unlock_pi(futexp, private) \
lll_futex_syscall (4, futexp, \
__lll_private_flag (FUTEX_UNLOCK_PI, private), \
0, 0)
@@ -122,7 +130,7 @@
# define lll_futex_wait_cancel(futexp, val, private) \
({ \
int __op = __lll_private_flag (FUTEX_WAIT, private); \
- INTERNAL_SYSCALL_CANCEL (futex, futexp, __op, val, NULL); \
+ INTERNAL_SYSCALL_CANCEL (lll_futex, futexp, __op, val, NULL); \
})
#endif /* !__ASSEMBLER__ */
--
2.53.0
More information about the Libc-alpha
mailing list