1 // SPDX-License-Identifier: LGPL-2.1-only
14 #include <rseq/rseq.h>
20 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
22 struct percpu_lock_entry
{
24 } __attribute__((aligned(128)));
27 struct percpu_lock_entry c
[CPU_SETSIZE
];
30 struct test_data_entry
{
32 } __attribute__((aligned(128)));
34 struct spinlock_test_data
{
35 struct percpu_lock lock
;
36 struct test_data_entry c
[CPU_SETSIZE
];
40 struct percpu_list_node
{
42 struct percpu_list_node
*next
;
45 struct percpu_list_entry
{
46 struct percpu_list_node
*head
;
47 } __attribute__((aligned(128)));
50 struct percpu_list_entry c
[CPU_SETSIZE
];
53 /* A simple percpu spinlock. Returns the cpu lock was acquired on. */
54 int rseq_this_cpu_lock(struct percpu_lock
*lock
)
61 cpu
= rseq_cpu_start();
62 ret
= rseq_cmpeqv_storev(&lock
->c
[cpu
].v
,
64 if (rseq_likely(!ret
))
66 /* Retry if comparison fails or rseq aborts. */
69 * Acquire semantic when taking lock after control dependency.
70 * Matches rseq_smp_store_release().
72 rseq_smp_acquire__after_ctrl_dep();
76 void rseq_percpu_unlock(struct percpu_lock
*lock
, int cpu
)
78 assert(lock
->c
[cpu
].v
== 1);
80 * Release lock, with release semantic. Matches
81 * rseq_smp_acquire__after_ctrl_dep().
83 rseq_smp_store_release(&lock
->c
[cpu
].v
, 0);
86 void *test_percpu_spinlock_thread(void *arg
)
88 struct spinlock_test_data
*data
= (struct spinlock_test_data
*) arg
;
91 if (rseq_register_current_thread()) {
92 fprintf(stderr
, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
93 errno
, strerror(errno
));
96 for (i
= 0; i
< data
->reps
; i
++) {
97 cpu
= rseq_this_cpu_lock(&data
->lock
);
99 rseq_percpu_unlock(&data
->lock
, cpu
);
101 if (rseq_unregister_current_thread()) {
102 fprintf(stderr
, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
103 errno
, strerror(errno
));
111 * A simple test which implements a sharded counter using a per-cpu
112 * lock. Obviously real applications might prefer to simply use a
113 * per-cpu increment; however, this is reasonable for a test and the
114 * lock can be extended to synchronize more complicated operations.
116 void test_percpu_spinlock(void)
118 const int num_threads
= 200;
121 pthread_t test_threads
[num_threads
];
122 struct spinlock_test_data data
;
126 memset(&data
, 0, sizeof(data
));
129 for (i
= 0; i
< num_threads
; i
++)
130 pthread_create(&test_threads
[i
], NULL
,
131 test_percpu_spinlock_thread
, &data
);
133 for (i
= 0; i
< num_threads
; i
++)
134 pthread_join(test_threads
[i
], NULL
);
137 for (i
= 0; i
< CPU_SETSIZE
; i
++)
138 sum
+= data
.c
[i
].count
;
140 ok(sum
== (uint64_t)data
.reps
* num_threads
, "sum");
143 void this_cpu_list_push(struct percpu_list
*list
,
144 struct percpu_list_node
*node
,
150 intptr_t *targetptr
, newval
, expect
;
153 cpu
= rseq_cpu_start();
154 /* Load list->c[cpu].head with single-copy atomicity. */
155 expect
= (intptr_t)RSEQ_READ_ONCE(list
->c
[cpu
].head
);
156 newval
= (intptr_t)node
;
157 targetptr
= (intptr_t *)&list
->c
[cpu
].head
;
158 node
->next
= (struct percpu_list_node
*)expect
;
159 ret
= rseq_cmpeqv_storev(targetptr
, expect
, newval
, cpu
);
160 if (rseq_likely(!ret
))
162 /* Retry if comparison fails or rseq aborts. */
169 * Unlike a traditional lock-less linked list; the availability of a
170 * rseq primitive allows us to implement pop without concerns over
173 struct percpu_list_node
*this_cpu_list_pop(struct percpu_list
*list
,
177 struct percpu_list_node
*head
;
178 intptr_t *targetptr
, expectnot
, *load
;
182 cpu
= rseq_cpu_start();
183 targetptr
= (intptr_t *)&list
->c
[cpu
].head
;
184 expectnot
= (intptr_t)NULL
;
185 offset
= offsetof(struct percpu_list_node
, next
);
186 load
= (intptr_t *)&head
;
187 ret
= rseq_cmpnev_storeoffp_load(targetptr
, expectnot
,
189 if (rseq_likely(!ret
)) {
196 /* Retry if rseq aborts. */
201 * __percpu_list_pop is not safe against concurrent accesses. Should
202 * only be used on lists that are not concurrently modified.
204 struct percpu_list_node
*__percpu_list_pop(struct percpu_list
*list
, int cpu
)
206 struct percpu_list_node
*node
;
208 node
= list
->c
[cpu
].head
;
211 list
->c
[cpu
].head
= node
->next
;
215 void *test_percpu_list_thread(void *arg
)
218 struct percpu_list
*list
= (struct percpu_list
*)arg
;
220 if (rseq_register_current_thread()) {
221 fprintf(stderr
, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
222 errno
, strerror(errno
));
226 for (i
= 0; i
< 100000; i
++) {
227 struct percpu_list_node
*node
;
229 node
= this_cpu_list_pop(list
, NULL
);
230 sched_yield(); /* encourage shuffling */
232 this_cpu_list_push(list
, node
, NULL
);
235 if (rseq_unregister_current_thread()) {
236 fprintf(stderr
, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
237 errno
, strerror(errno
));
244 /* Simultaneous modification to a per-cpu linked list from many threads. */
245 void test_percpu_list(void)
248 uint64_t sum
= 0, expected_sum
= 0;
249 struct percpu_list list
;
250 pthread_t test_threads
[200];
251 cpu_set_t allowed_cpus
;
255 memset(&list
, 0, sizeof(list
));
257 /* Generate list entries for every usable cpu. */
258 sched_getaffinity(0, sizeof(allowed_cpus
), &allowed_cpus
);
259 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
260 if (!CPU_ISSET(i
, &allowed_cpus
))
262 for (j
= 1; j
<= 100; j
++) {
263 struct percpu_list_node
*node
;
267 node
= (struct percpu_list_node
*) malloc(sizeof(*node
));
270 node
->next
= list
.c
[i
].head
;
271 list
.c
[i
].head
= node
;
275 for (i
= 0; i
< 200; i
++)
276 pthread_create(&test_threads
[i
], NULL
,
277 test_percpu_list_thread
, &list
);
279 for (i
= 0; i
< 200; i
++)
280 pthread_join(test_threads
[i
], NULL
);
282 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
283 struct percpu_list_node
*node
;
285 if (!CPU_ISSET(i
, &allowed_cpus
))
288 while ((node
= __percpu_list_pop(&list
, i
))) {
295 * All entries should now be accounted for (unless some external
296 * actor is interfering with our allowed affinity while this
299 ok(sum
== expected_sum
, "sum");
304 plan_tests(NR_TESTS
);
306 if (!rseq_available()) {
307 skip(NR_TESTS
, "The rseq syscall is unavailable");
311 if (rseq_register_current_thread()) {
312 fail("rseq_register_current_thread(...) failed(%d): %s\n",
313 errno
, strerror(errno
));
316 pass("Registered current thread with rseq");
319 test_percpu_spinlock();
322 if (rseq_unregister_current_thread()) {
323 fail("rseq_unregister_current_thread(...) failed(%d): %s\n",
324 errno
, strerror(errno
));
327 pass("Unregistered current thread with rseq");