Skip to content

Commit c574fb2

Browse files
committed
Merge tag 'locking-futex-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull futex updates from Thomas Gleixner: "A set of updates for futexes and related selftests: - Plug the ptrace_may_access() race against a concurrent exec() which allows to pass the check before the target's process transition in exec() by taking a read lock on signal->ext_update_lock. - A large set of cleanups and enhancement to the futex selftests. The bulk of changes is the conversion to the kselftest harness" * tag 'locking-futex-2025-09-29' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (25 commits) selftest/futex: Fix spelling mistake "boundarie" -> "boundary" selftests/futex: Remove logging.h file selftests/futex: Drop logging.h include from futex_numa selftests/futex: Refactor futex_numa_mpol with kselftest_harness.h selftests/futex: Refactor futex_priv_hash with kselftest_harness.h selftests/futex: Refactor futex_waitv with kselftest_harness.h selftests/futex: Refactor futex_requeue with kselftest_harness.h selftests/futex: Refactor futex_wait with kselftest_harness.h selftests/futex: Refactor futex_wait_private_mapped_file with kselftest_harness.h selftests/futex: Refactor futex_wait_unitialized_heap with kselftest_harness.h selftests/futex: Refactor futex_wait_wouldblock with kselftest_harness.h selftests/futex: Refactor futex_wait_timeout with kselftest_harness.h selftests/futex: Refactor futex_requeue_pi_signal_restart with kselftest_harness.h selftests/futex: Refactor futex_requeue_pi_mismatched_ops with kselftest_harness.h selftests/futex: Refactor futex_requeue_pi with kselftest_harness.h selftests: kselftest: Create ksft_print_dbg_msg() futex: Don't leak robust_list pointer on exec race selftest/futex: Compile also with libnuma < 2.0.16 selftest/futex: Reintroduce "Memory out of range" numa_mpol's subtest selftest/futex: Make the error check more precise for futex_numa_mpol ...
2 parents d8de368 + 4386f71 commit c574fb2

20 files changed

+575
-1090
lines changed

kernel/futex/syscalls.c

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,56 @@ SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3939
return 0;
4040
}
4141

42+
static inline void __user *futex_task_robust_list(struct task_struct *p, bool compat)
43+
{
44+
#ifdef CONFIG_COMPAT
45+
if (compat)
46+
return p->compat_robust_list;
47+
#endif
48+
return p->robust_list;
49+
}
50+
51+
static void __user *futex_get_robust_list_common(int pid, bool compat)
52+
{
53+
struct task_struct *p = current;
54+
void __user *head;
55+
int ret;
56+
57+
scoped_guard(rcu) {
58+
if (pid) {
59+
p = find_task_by_vpid(pid);
60+
if (!p)
61+
return (void __user *)ERR_PTR(-ESRCH);
62+
}
63+
get_task_struct(p);
64+
}
65+
66+
/*
67+
* Hold exec_update_lock to serialize with concurrent exec()
68+
* so ptrace_may_access() is checked against stable credentials
69+
*/
70+
ret = down_read_killable(&p->signal->exec_update_lock);
71+
if (ret)
72+
goto err_put;
73+
74+
ret = -EPERM;
75+
if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
76+
goto err_unlock;
77+
78+
head = futex_task_robust_list(p, compat);
79+
80+
up_read(&p->signal->exec_update_lock);
81+
put_task_struct(p);
82+
83+
return head;
84+
85+
err_unlock:
86+
up_read(&p->signal->exec_update_lock);
87+
err_put:
88+
put_task_struct(p);
89+
return (void __user *)ERR_PTR(ret);
90+
}
91+
4292
/**
4393
* sys_get_robust_list() - Get the robust-futex list head of a task
4494
* @pid: pid of the process [zero for current task]
@@ -49,36 +99,14 @@ SYSCALL_DEFINE3(get_robust_list, int, pid,
4999
struct robust_list_head __user * __user *, head_ptr,
50100
size_t __user *, len_ptr)
51101
{
52-
struct robust_list_head __user *head;
53-
unsigned long ret;
54-
struct task_struct *p;
55-
56-
rcu_read_lock();
57-
58-
ret = -ESRCH;
59-
if (!pid)
60-
p = current;
61-
else {
62-
p = find_task_by_vpid(pid);
63-
if (!p)
64-
goto err_unlock;
65-
}
66-
67-
ret = -EPERM;
68-
if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
69-
goto err_unlock;
102+
struct robust_list_head __user *head = futex_get_robust_list_common(pid, false);
70103

71-
head = p->robust_list;
72-
rcu_read_unlock();
104+
if (IS_ERR(head))
105+
return PTR_ERR(head);
73106

74107
if (put_user(sizeof(*head), len_ptr))
75108
return -EFAULT;
76109
return put_user(head, head_ptr);
77-
78-
err_unlock:
79-
rcu_read_unlock();
80-
81-
return ret;
82110
}
83111

84112
long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
@@ -455,36 +483,14 @@ COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
455483
compat_uptr_t __user *, head_ptr,
456484
compat_size_t __user *, len_ptr)
457485
{
458-
struct compat_robust_list_head __user *head;
459-
unsigned long ret;
460-
struct task_struct *p;
461-
462-
rcu_read_lock();
463-
464-
ret = -ESRCH;
465-
if (!pid)
466-
p = current;
467-
else {
468-
p = find_task_by_vpid(pid);
469-
if (!p)
470-
goto err_unlock;
471-
}
472-
473-
ret = -EPERM;
474-
if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
475-
goto err_unlock;
486+
struct compat_robust_list_head __user *head = futex_get_robust_list_common(pid, true);
476487

477-
head = p->compat_robust_list;
478-
rcu_read_unlock();
488+
if (IS_ERR(head))
489+
return PTR_ERR(head);
479490

480491
if (put_user(sizeof(*head), len_ptr))
481492
return -EFAULT;
482493
return put_user(ptr_to_compat(head), head_ptr);
483-
484-
err_unlock:
485-
rcu_read_unlock();
486-
487-
return ret;
488494
}
489495
#endif /* CONFIG_COMPAT */
490496

tools/testing/selftests/futex/functional/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
PKG_CONFIG ?= pkg-config
3+
LIBNUMA_TEST = $(shell sh -c "$(PKG_CONFIG) numa --atleast-version 2.0.16 > /dev/null 2>&1 && echo SUFFICIENT || echo NO")
4+
25
INCLUDES := -I../include -I../../ $(KHDR_INCLUDES)
3-
CFLAGS := $(CFLAGS) -g -O2 -Wall -pthread $(INCLUDES) $(KHDR_INCLUDES)
6+
CFLAGS := $(CFLAGS) -g -O2 -Wall -pthread -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 $(INCLUDES) $(KHDR_INCLUDES) -DLIBNUMA_VER_$(LIBNUMA_TEST)=1
47
LDLIBS := -lpthread -lrt -lnuma
58

69
LOCAL_HDRS := \
710
../include/futextest.h \
8-
../include/atomic.h \
9-
../include/logging.h
11+
../include/atomic.h
1012
TEST_GEN_PROGS := \
1113
futex_wait_timeout \
1214
futex_wait_wouldblock \

tools/testing/selftests/futex/functional/futex_numa.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
#include <sys/mman.h>
66
#include <fcntl.h>
77
#include <stdbool.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
810
#include <time.h>
911
#include <assert.h>
10-
#include "logging.h"
1112
#include "futextest.h"
1213
#include "futex2test.h"
1314

tools/testing/selftests/futex/functional/futex_numa_mpol.c

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#include <linux/futex.h>
1717
#include <sys/mman.h>
1818

19-
#include "logging.h"
2019
#include "futextest.h"
2120
#include "futex2test.h"
21+
#include "../../kselftest_harness.h"
2222

2323
#define MAX_THREADS 64
2424

@@ -77,7 +77,7 @@ static void join_max_threads(void)
7777
}
7878
}
7979

80-
static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flags)
80+
static void __test_futex(void *futex_ptr, int err_value, unsigned int futex_flags)
8181
{
8282
int to_wake, ret, i, need_exit = 0;
8383

@@ -88,11 +88,17 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
8888

8989
do {
9090
ret = futex2_wake(futex_ptr, to_wake, futex_flags);
91-
if (must_fail) {
92-
if (ret < 0)
93-
break;
94-
ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
95-
to_wake, futex_flags);
91+
92+
if (err_value) {
93+
if (ret >= 0)
94+
ksft_exit_fail_msg("futex2_wake(%d, 0x%x) should fail, but didn't\n",
95+
to_wake, futex_flags);
96+
97+
if (errno != err_value)
98+
ksft_exit_fail_msg("futex2_wake(%d, 0x%x) expected error was %d, but returned %d (%s)\n",
99+
to_wake, futex_flags, err_value, errno, strerror(errno));
100+
101+
break;
96102
}
97103
if (ret < 0) {
98104
ksft_exit_fail_msg("Failed futex2_wake(%d, 0x%x): %m\n",
@@ -106,12 +112,12 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
106112
join_max_threads();
107113

108114
for (i = 0; i < MAX_THREADS; i++) {
109-
if (must_fail && thread_args[i].result != -1) {
115+
if (err_value && thread_args[i].result != -1) {
110116
ksft_print_msg("Thread %d should fail but succeeded (%d)\n",
111117
i, thread_args[i].result);
112118
need_exit = 1;
113119
}
114-
if (!must_fail && thread_args[i].result != 0) {
120+
if (!err_value && thread_args[i].result != 0) {
115121
ksft_print_msg("Thread %d failed (%d)\n", i, thread_args[i].result);
116122
need_exit = 1;
117123
}
@@ -120,58 +126,30 @@ static void __test_futex(void *futex_ptr, int must_fail, unsigned int futex_flag
120126
ksft_exit_fail_msg("Aborting due to earlier errors.\n");
121127
}
122128

123-
static void test_futex(void *futex_ptr, int must_fail)
129+
static void test_futex(void *futex_ptr, int err_value)
124130
{
125-
__test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
131+
__test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA);
126132
}
127133

128-
static void test_futex_mpol(void *futex_ptr, int must_fail)
134+
static void test_futex_mpol(void *futex_ptr, int err_value)
129135
{
130-
__test_futex(futex_ptr, must_fail, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL);
136+
__test_futex(futex_ptr, err_value, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL);
131137
}
132138

133-
static void usage(char *prog)
134-
{
135-
printf("Usage: %s\n", prog);
136-
printf(" -c Use color\n");
137-
printf(" -h Display this help message\n");
138-
printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
139-
VQUIET, VCRITICAL, VINFO);
140-
}
141-
142-
int main(int argc, char *argv[])
139+
TEST(futex_numa_mpol)
143140
{
144141
struct futex32_numa *futex_numa;
145-
int mem_size, i;
146142
void *futex_ptr;
147-
int c;
148-
149-
while ((c = getopt(argc, argv, "chv:")) != -1) {
150-
switch (c) {
151-
case 'c':
152-
log_color(1);
153-
break;
154-
case 'h':
155-
usage(basename(argv[0]));
156-
exit(0);
157-
break;
158-
case 'v':
159-
log_verbosity(atoi(optarg));
160-
break;
161-
default:
162-
usage(basename(argv[0]));
163-
exit(1);
164-
}
165-
}
166-
167-
ksft_print_header();
168-
ksft_set_plan(1);
143+
int mem_size;
169144

170145
mem_size = sysconf(_SC_PAGE_SIZE);
171-
futex_ptr = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
146+
futex_ptr = mmap(NULL, mem_size * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
172147
if (futex_ptr == MAP_FAILED)
173148
ksft_exit_fail_msg("mmap() for %d bytes failed\n", mem_size);
174149

150+
/* Create an invalid memory region for the "Memory out of range" test */
151+
mprotect(futex_ptr + mem_size, mem_size, PROT_NONE);
152+
175153
futex_numa = futex_ptr;
176154

177155
ksft_print_msg("Regular test\n");
@@ -182,27 +160,31 @@ int main(int argc, char *argv[])
182160
if (futex_numa->numa == FUTEX_NO_NODE)
183161
ksft_exit_fail_msg("NUMA node is left uninitialized\n");
184162

185-
ksft_print_msg("Memory too small\n");
186-
test_futex(futex_ptr + mem_size - 4, 1);
163+
/* FUTEX2_NUMA futex must be 8-byte aligned */
164+
ksft_print_msg("Mis-aligned futex\n");
165+
test_futex(futex_ptr + mem_size - 4, EINVAL);
187166

188167
ksft_print_msg("Memory out of range\n");
189-
test_futex(futex_ptr + mem_size, 1);
168+
test_futex(futex_ptr + mem_size, EFAULT);
190169

191170
futex_numa->numa = FUTEX_NO_NODE;
192171
mprotect(futex_ptr, mem_size, PROT_READ);
193172
ksft_print_msg("Memory, RO\n");
194-
test_futex(futex_ptr, 1);
173+
test_futex(futex_ptr, EFAULT);
195174

196175
mprotect(futex_ptr, mem_size, PROT_NONE);
197176
ksft_print_msg("Memory, no access\n");
198-
test_futex(futex_ptr, 1);
177+
test_futex(futex_ptr, EFAULT);
199178

200179
mprotect(futex_ptr, mem_size, PROT_READ | PROT_WRITE);
201180
ksft_print_msg("Memory back to RW\n");
202181
test_futex(futex_ptr, 0);
203182

183+
ksft_test_result_pass("futex2 memory boundary tests passed\n");
184+
204185
/* MPOL test. Does not work as expected */
205-
for (i = 0; i < 4; i++) {
186+
#ifdef LIBNUMA_VER_SUFFICIENT
187+
for (int i = 0; i < 4; i++) {
206188
unsigned long nodemask;
207189
int ret;
208190

@@ -221,15 +203,17 @@ int main(int argc, char *argv[])
221203
ret = futex2_wake(futex_ptr, 0, FUTEX2_SIZE_U32 | FUTEX_PRIVATE_FLAG | FUTEX2_NUMA | FUTEX2_MPOL);
222204
if (ret < 0)
223205
ksft_test_result_fail("Failed to wake 0 with MPOL: %m\n");
224-
if (0)
225-
test_futex_mpol(futex_numa, 0);
226206
if (futex_numa->numa != i) {
227207
ksft_exit_fail_msg("Returned NUMA node is %d expected %d\n",
228208
futex_numa->numa, i);
229209
}
230210
}
231211
}
232-
ksft_test_result_pass("NUMA MPOL tests passed\n");
233-
ksft_finished();
234-
return 0;
212+
ksft_test_result_pass("futex2 MPOL hints test passed\n");
213+
#else
214+
ksft_test_result_skip("futex2 MPOL hints test requires libnuma 2.0.16+\n");
215+
#endif
216+
munmap(futex_ptr, mem_size * 2);
235217
}
218+
219+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)