1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2025 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
*/
#define _GNU_SOURCE
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/prctl.h>
#include <sys/prctl.h>
#include "logging.h"
#define MAX_THREADS 64
static pthread_barrier_t barrier_main;
static pthread_mutex_t global_lock;
static pthread_t threads[MAX_THREADS];
static int counter;
#ifndef PR_FUTEX_HASH
#define PR_FUTEX_HASH 78
# define PR_FUTEX_HASH_SET_SLOTS 1
# define FH_FLAG_IMMUTABLE (1ULL << 0)
# define PR_FUTEX_HASH_GET_SLOTS 2
# define PR_FUTEX_HASH_GET_IMMUTABLE 3
#endif
static int futex_hash_slots_set(unsigned int slots, int flags)
{
return prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_SET_SLOTS, slots, flags);
}
static int futex_hash_slots_get(void)
{
return prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_GET_SLOTS);
}
static int futex_hash_immutable_get(void)
{
return prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_GET_IMMUTABLE);
}
static void futex_hash_slots_set_verify(int slots)
{
int ret;
ret = futex_hash_slots_set(slots, 0);
if (ret != 0) {
ksft_test_result_fail("Failed to set slots to %d: %m\n", slots);
ksft_finished();
}
ret = futex_hash_slots_get();
if (ret != slots) {
ksft_test_result_fail("Set %d slots but PR_FUTEX_HASH_GET_SLOTS returns: %d, %m\n",
slots, ret);
ksft_finished();
}
ksft_test_result_pass("SET and GET slots %d passed\n", slots);
}
static void futex_hash_slots_set_must_fail(int slots, int flags)
{
int ret;
ret = futex_hash_slots_set(slots, flags);
ksft_test_result(ret < 0, "futex_hash_slots_set(%d, %d)\n",
slots, flags);
}
static void *thread_return_fn(void *arg)
{
return NULL;
}
static void *thread_lock_fn(void *arg)
{
pthread_barrier_wait(&barrier_main);
pthread_mutex_lock(&global_lock);
counter++;
usleep(20);
pthread_mutex_unlock(&global_lock);
return NULL;
}
static void create_max_threads(void *(*thread_fn)(void *))
{
int i, ret;
for (i = 0; i < MAX_THREADS; i++) {
ret = pthread_create(&threads[i], NULL, thread_fn, NULL);
if (ret)
ksft_exit_fail_msg("pthread_create failed: %m\n");
}
}
static void join_max_threads(void)
{
int i, ret;
for (i = 0; i < MAX_THREADS; i++) {
ret = pthread_join(threads[i], NULL);
if (ret)
ksft_exit_fail_msg("pthread_join failed for thread %d\n", i);
}
}
static void usage(char *prog)
{
printf("Usage: %s\n", prog);
printf(" -c Use color\n");
printf(" -g Test global hash instead intead local immutable \n");
printf(" -h Display this help message\n");
printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
VQUIET, VCRITICAL, VINFO);
}
static const char *test_msg_auto_create = "Automatic hash bucket init on thread creation.\n";
static const char *test_msg_auto_inc = "Automatic increase with more than 16 CPUs\n";
int main(int argc, char *argv[])
{
int futex_slots1, futex_slotsn, online_cpus;
pthread_mutexattr_t mutex_attr_pi;
int use_global_hash = 0;
int ret;
char c;
while ((c = getopt(argc, argv, "cghv:")) != -1) {
switch (c) {
case 'c':
log_color(1);
break;
case 'g':
use_global_hash = 1;
break;
case 'h':
usage(basename(argv[0]));
exit(0);
break;
case 'v':
log_verbosity(atoi(optarg));
break;
default:
usage(basename(argv[0]));
exit(1);
}
}
ksft_print_header();
ksft_set_plan(22);
ret = pthread_mutexattr_init(&mutex_attr_pi);
ret |= pthread_mutexattr_setprotocol(&mutex_attr_pi, PTHREAD_PRIO_INHERIT);
ret |= pthread_mutex_init(&global_lock, &mutex_attr_pi);
if (ret != 0) {
ksft_exit_fail_msg("Failed to initialize pthread mutex.\n");
}
/* First thread, expect to be 0, not yet initialized */
ret = futex_hash_slots_get();
if (ret != 0)
ksft_exit_fail_msg("futex_hash_slots_get() failed: %d, %m\n", ret);
ret = futex_hash_immutable_get();
if (ret != 0)
ksft_exit_fail_msg("futex_hash_immutable_get() failed: %d, %m\n", ret);
ksft_test_result_pass("Basic get slots and immutable status.\n");
ret = pthread_create(&threads[0], NULL, thread_return_fn, NULL);
if (ret != 0)
ksft_exit_fail_msg("pthread_create() failed: %d, %m\n", ret);
ret = pthread_join(threads[0], NULL);
if (ret != 0)
ksft_exit_fail_msg("pthread_join() failed: %d, %m\n", ret);
/* First thread, has to initialiaze private hash */
futex_slots1 = futex_hash_slots_get();
if (futex_slots1 <= 0) {
ksft_print_msg("Current hash buckets: %d\n", futex_slots1);
ksft_exit_fail_msg(test_msg_auto_create);
}
ksft_test_result_pass(test_msg_auto_create);
online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
ret = pthread_barrier_init(&barrier_main, NULL, MAX_THREADS + 1);
if (ret != 0)
ksft_exit_fail_msg("pthread_barrier_init failed: %m.\n");
ret = pthread_mutex_lock(&global_lock);
if (ret != 0)
ksft_exit_fail_msg("pthread_mutex_lock failed: %m.\n");
counter = 0;
create_max_threads(thread_lock_fn);
pthread_barrier_wait(&barrier_main);
/*
* The current default size of hash buckets is 16. The auto increase
* works only if more than 16 CPUs are available.
*/
ksft_print_msg("Online CPUs: %d\n", online_cpus);
if (online_cpus > 16) {
futex_slotsn = futex_hash_slots_get();
if (futex_slotsn < 0 || futex_slots1 == futex_slotsn) {
ksft_print_msg("Expected increase of hash buckets but got: %d -> %d\n",
futex_slots1, futex_slotsn);
ksft_exit_fail_msg(test_msg_auto_inc);
}
ksft_test_result_pass(test_msg_auto_inc);
} else {
ksft_test_result_skip(test_msg_auto_inc);
}
ret = pthread_mutex_unlock(&global_lock);
/* Once the user changes it, it has to be what is set */
futex_hash_slots_set_verify(2);
futex_hash_slots_set_verify(4);
futex_hash_slots_set_verify(8);
futex_hash_slots_set_verify(32);
futex_hash_slots_set_verify(16);
ret = futex_hash_slots_set(15, 0);
ksft_test_result(ret < 0, "Use 15 slots\n");
futex_hash_slots_set_verify(2);
join_max_threads();
ksft_test_result(counter == MAX_THREADS, "Created of waited for %d of %d threads\n",
counter, MAX_THREADS);
counter = 0;
/* Once the user set something, auto reisze must be disabled */
ret = pthread_barrier_init(&barrier_main, NULL, MAX_THREADS);
create_max_threads(thread_lock_fn);
join_max_threads();
ret = futex_hash_slots_get();
ksft_test_result(ret == 2, "No more auto-resize after manaul setting, got %d\n",
ret);
futex_hash_slots_set_must_fail(1 << 29, 0);
/*
* Once the private hash has been made immutable or global hash has been requested,
* then this requested can not be undone.
*/
if (use_global_hash) {
ret = futex_hash_slots_set(0, 0);
ksft_test_result(ret == 0, "Global hash request\n");
} else {
ret = futex_hash_slots_set(4, FH_FLAG_IMMUTABLE);
ksft_test_result(ret == 0, "Immutable resize to 4\n");
}
if (ret != 0)
goto out;
futex_hash_slots_set_must_fail(4, 0);
futex_hash_slots_set_must_fail(4, FH_FLAG_IMMUTABLE);
futex_hash_slots_set_must_fail(8, 0);
futex_hash_slots_set_must_fail(8, FH_FLAG_IMMUTABLE);
futex_hash_slots_set_must_fail(0, FH_FLAG_IMMUTABLE);
futex_hash_slots_set_must_fail(6, FH_FLAG_IMMUTABLE);
ret = pthread_barrier_init(&barrier_main, NULL, MAX_THREADS);
if (ret != 0) {
ksft_exit_fail_msg("pthread_barrier_init failed: %m\n");
return 1;
}
create_max_threads(thread_lock_fn);
join_max_threads();
ret = futex_hash_slots_get();
if (use_global_hash) {
ksft_test_result(ret == 0, "Continue to use global hash\n");
} else {
ksft_test_result(ret == 4, "Continue to use the 4 hash buckets\n");
}
ret = futex_hash_immutable_get();
ksft_test_result(ret == 1, "Hash reports to be immutable\n");
out:
ksft_finished();
return 0;
}
|