1 // SPDX-License-Identifier: MIT
2 // SPDX-FileCopyrightText: 2020-2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #include <linux/version.h>
8 #include <linux/membarrier.h>
18 #include <sys/types.h>
23 #include <rseq/mempool.h>
25 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,10,0)
27 MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ
= (1 << 7),
28 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ
= (1 << 8),
32 MEMBARRIER_CMD_FLAG_CPU
= (1 << 0),
37 static int loop_cnt
[NR_INJECT
+ 1];
39 static int loop_cnt_1
asm("asm_loop_cnt_1") __attribute__((used
));
40 static int loop_cnt_2
asm("asm_loop_cnt_2") __attribute__((used
));
41 static int loop_cnt_3
asm("asm_loop_cnt_3") __attribute__((used
));
42 static int loop_cnt_4
asm("asm_loop_cnt_4") __attribute__((used
));
43 static int loop_cnt_5
asm("asm_loop_cnt_5") __attribute__((used
));
44 static int loop_cnt_6
asm("asm_loop_cnt_6") __attribute__((used
));
46 static int opt_modulo
, verbose
;
48 static int opt_yield
, opt_signal
, opt_sleep
,
49 opt_disable_rseq
, opt_threads
= 200,
50 opt_disable_mod
= 0, opt_test
= 's';
52 static long long opt_reps
= 5000;
54 static __thread
__attribute__((tls_model("initial-exec")))
55 unsigned int signals_delivered
;
57 static inline pid_t
rseq_gettid(void)
59 return syscall(__NR_gettid
);
64 static __thread
__attribute__((tls_model("initial-exec"), unused
))
65 int yield_mod_cnt
, nr_abort
;
67 #define printf_verbose(fmt, ...) \
70 printf(fmt, ## __VA_ARGS__); \
75 #define INJECT_ASM_REG "eax"
77 #define RSEQ_INJECT_CLOBBER \
81 * Use ip-relative addressing to get the loop counter.
83 #define __RSEQ_INJECT_ASM(n, ref_ip, ref_label) \
84 "movl " __rseq_str(ref_ip) ", %%" INJECT_ASM_REG "\n\t" \
85 "leal ( asm_loop_cnt_" #n " - " __rseq_str(ref_label) "b)(%%" INJECT_ASM_REG "), %%" INJECT_ASM_REG "\n\t" \
86 "movl (%%" INJECT_ASM_REG "), %%" INJECT_ASM_REG "\n\t" \
87 "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
90 "dec %%" INJECT_ASM_REG "\n\t" \
94 #define RSEQ_INJECT_ASM(n) \
95 __RSEQ_INJECT_ASM(n, %[ref_ip], RSEQ_ASM_REF_LABEL)
97 #elif defined(__x86_64__)
99 #define INJECT_ASM_REG_P "rax"
100 #define INJECT_ASM_REG "eax"
102 #define RSEQ_INJECT_CLOBBER \
106 #define RSEQ_INJECT_ASM(n) \
107 "lea asm_loop_cnt_" #n "(%%rip), %%" INJECT_ASM_REG_P "\n\t" \
108 "mov (%%" INJECT_ASM_REG_P "), %%" INJECT_ASM_REG "\n\t" \
109 "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
112 "dec %%" INJECT_ASM_REG "\n\t" \
116 #elif defined(__s390__)
118 #define RSEQ_INJECT_INPUT \
119 , [loop_cnt_1]"m"(loop_cnt[1]) \
120 , [loop_cnt_2]"m"(loop_cnt[2]) \
121 , [loop_cnt_3]"m"(loop_cnt[3]) \
122 , [loop_cnt_4]"m"(loop_cnt[4]) \
123 , [loop_cnt_5]"m"(loop_cnt[5]) \
124 , [loop_cnt_6]"m"(loop_cnt[6])
126 #define INJECT_ASM_REG "r12"
128 #define RSEQ_INJECT_CLOBBER \
131 #define RSEQ_INJECT_ASM(n) \
132 "l %%" INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
133 "ltr %%" INJECT_ASM_REG ", %%" INJECT_ASM_REG "\n\t" \
136 "ahi %%" INJECT_ASM_REG ", -1\n\t" \
140 #elif defined(__ARMEL__)
142 #define RSEQ_INJECT_INPUT \
143 , [loop_cnt_1]"m"(loop_cnt[1]) \
144 , [loop_cnt_2]"m"(loop_cnt[2]) \
145 , [loop_cnt_3]"m"(loop_cnt[3]) \
146 , [loop_cnt_4]"m"(loop_cnt[4]) \
147 , [loop_cnt_5]"m"(loop_cnt[5]) \
148 , [loop_cnt_6]"m"(loop_cnt[6])
150 #define INJECT_ASM_REG "r4"
152 #define RSEQ_INJECT_CLOBBER \
155 #define RSEQ_INJECT_ASM(n) \
156 "ldr " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
157 "cmp " INJECT_ASM_REG ", #0\n\t" \
160 "subs " INJECT_ASM_REG ", #1\n\t" \
164 #elif defined(__AARCH64EL__)
166 #define RSEQ_INJECT_INPUT \
167 , [loop_cnt_1] "Qo" (loop_cnt[1]) \
168 , [loop_cnt_2] "Qo" (loop_cnt[2]) \
169 , [loop_cnt_3] "Qo" (loop_cnt[3]) \
170 , [loop_cnt_4] "Qo" (loop_cnt[4]) \
171 , [loop_cnt_5] "Qo" (loop_cnt[5]) \
172 , [loop_cnt_6] "Qo" (loop_cnt[6])
174 #define INJECT_ASM_REG RSEQ_ASM_TMP_REG32
176 #define RSEQ_INJECT_ASM(n) \
177 " ldr " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n" \
178 " cbz " INJECT_ASM_REG ", 333f\n" \
180 " sub " INJECT_ASM_REG ", " INJECT_ASM_REG ", #1\n" \
181 " cbnz " INJECT_ASM_REG ", 222b\n" \
184 #elif defined(__PPC__)
186 #define RSEQ_INJECT_INPUT \
187 , [loop_cnt_1]"m"(loop_cnt[1]) \
188 , [loop_cnt_2]"m"(loop_cnt[2]) \
189 , [loop_cnt_3]"m"(loop_cnt[3]) \
190 , [loop_cnt_4]"m"(loop_cnt[4]) \
191 , [loop_cnt_5]"m"(loop_cnt[5]) \
192 , [loop_cnt_6]"m"(loop_cnt[6])
194 #define INJECT_ASM_REG "r18"
196 #define RSEQ_INJECT_CLOBBER \
199 #define RSEQ_INJECT_ASM(n) \
200 "lwz %%" INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
201 "cmpwi %%" INJECT_ASM_REG ", 0\n\t" \
204 "subic. %%" INJECT_ASM_REG ", %%" INJECT_ASM_REG ", 1\n\t" \
208 #elif defined(__mips__)
210 #define RSEQ_INJECT_INPUT \
211 , [loop_cnt_1]"m"(loop_cnt[1]) \
212 , [loop_cnt_2]"m"(loop_cnt[2]) \
213 , [loop_cnt_3]"m"(loop_cnt[3]) \
214 , [loop_cnt_4]"m"(loop_cnt[4]) \
215 , [loop_cnt_5]"m"(loop_cnt[5]) \
216 , [loop_cnt_6]"m"(loop_cnt[6])
218 #define INJECT_ASM_REG "$5"
220 #define RSEQ_INJECT_CLOBBER \
223 #define RSEQ_INJECT_ASM(n) \
224 "lw " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
225 "beqz " INJECT_ASM_REG ", 333f\n\t" \
227 "addiu " INJECT_ASM_REG ", -1\n\t" \
228 "bnez " INJECT_ASM_REG ", 222b\n\t" \
231 #elif defined(__riscv)
233 #define RSEQ_INJECT_INPUT \
234 , [loop_cnt_1]"m"(loop_cnt[1]) \
235 , [loop_cnt_2]"m"(loop_cnt[2]) \
236 , [loop_cnt_3]"m"(loop_cnt[3]) \
237 , [loop_cnt_4]"m"(loop_cnt[4]) \
238 , [loop_cnt_5]"m"(loop_cnt[5]) \
239 , [loop_cnt_6]"m"(loop_cnt[6])
241 #define INJECT_ASM_REG "t1"
243 #define RSEQ_INJECT_CLOBBER \
246 #define RSEQ_INJECT_ASM(n) \
247 "lw " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
248 "beqz " INJECT_ASM_REG ", 333f\n\t" \
250 "addi " INJECT_ASM_REG "," INJECT_ASM_REG ", -1\n\t" \
251 "bnez " INJECT_ASM_REG ", 222b\n\t" \
255 #error unsupported target
258 #define RSEQ_INJECT_FAILED \
261 #define RSEQ_INJECT_C(n) \
263 int loc_i, loc_nr_loops = loop_cnt[n]; \
265 for (loc_i = 0; loc_i < loc_nr_loops; loc_i++) { \
268 if (loc_nr_loops == -1 && opt_modulo) { \
269 if (yield_mod_cnt == opt_modulo - 1) { \
271 poll(NULL, 0, opt_sleep); \
285 #define printf_verbose(fmt, ...)
287 #endif /* BENCHMARK */
289 #include <rseq/rseq.h>
291 static enum rseq_mo opt_mo
= RSEQ_MO_RELAXED
;
293 static int sys_membarrier(int cmd
, int flags
, int cpu_id
)
295 return syscall(__NR_membarrier
, cmd
, flags
, cpu_id
);
298 #ifdef rseq_arch_has_load_add_load_load_add_store
299 #define TEST_MEMBARRIER
302 #ifdef BUILDOPT_RSEQ_PERCPU_MM_CID
303 # define RSEQ_PERCPU RSEQ_PERCPU_MM_CID
305 int get_current_cpu_id(void)
307 return rseq_current_mm_cid();
310 bool rseq_validate_cpu_id(void)
312 return rseq_mm_cid_available();
315 bool rseq_use_cpu_index(void)
317 return false; /* Use mm_cid */
319 # ifdef TEST_MEMBARRIER
321 * Membarrier does not currently support targeting a mm_cid, so
322 * issue the barrier on all cpus.
325 int rseq_membarrier_expedited(__attribute__ ((unused
)) int cpu
)
327 return sys_membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ
,
330 # endif /* TEST_MEMBARRIER */
332 # define RSEQ_PERCPU RSEQ_PERCPU_CPU_ID
334 int get_current_cpu_id(void)
336 return rseq_cpu_start();
339 bool rseq_validate_cpu_id(void)
341 return rseq_current_cpu_raw() >= 0;
344 bool rseq_use_cpu_index(void)
346 return true; /* Use cpu_id as index. */
348 # ifdef TEST_MEMBARRIER
350 int rseq_membarrier_expedited(int cpu
)
352 return sys_membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ
,
353 MEMBARRIER_CMD_FLAG_CPU
, cpu
);
355 # endif /* TEST_MEMBARRIER */
362 struct spinlock_test_data
{
363 struct percpu_lock lock
;
367 struct spinlock_thread_test_data
{
368 struct spinlock_test_data __rseq_percpu
*data
;
373 struct inc_test_data
{
377 struct inc_thread_test_data
{
378 struct inc_test_data __rseq_percpu
*data
;
383 struct percpu_list_node
{
385 struct percpu_list_node
*next
;
389 struct percpu_list_node
*head
;
392 #define BUFFER_ITEM_PER_CPU 100
394 struct percpu_buffer_node
{
398 struct percpu_buffer
{
401 struct percpu_buffer_node
**array
;
404 #define MEMCPY_BUFFER_ITEM_PER_CPU 100
406 struct percpu_memcpy_buffer_node
{
411 struct percpu_memcpy_buffer
{
414 struct percpu_memcpy_buffer_node
*array
;
417 /* A simple percpu spinlock. Grabs lock on current cpu. */
418 static int rseq_this_cpu_lock(struct percpu_lock __rseq_percpu
*lock
)
425 cpu
= get_current_cpu_id();
427 fprintf(stderr
, "pid: %d: tid: %d, cpu: %d: cid: %d\n",
428 getpid(), (int) rseq_gettid(), rseq_current_cpu_raw(), cpu
);
431 ret
= rseq_load_cbne_store__ptr(RSEQ_MO_RELAXED
, RSEQ_PERCPU
,
432 &rseq_percpu_ptr(lock
, cpu
)->v
,
434 if (rseq_likely(!ret
))
436 /* Retry if comparison fails or rseq aborts. */
439 * Acquire semantic when taking lock after control dependency.
440 * Matches rseq_smp_store_release().
442 rseq_smp_acquire__after_ctrl_dep();
446 static void rseq_percpu_unlock(struct percpu_lock __rseq_percpu
*lock
, int cpu
)
448 assert(rseq_percpu_ptr(lock
, cpu
)->v
== 1);
450 * Release lock, with release semantic. Matches
451 * rseq_smp_acquire__after_ctrl_dep().
453 rseq_smp_store_release(&rseq_percpu_ptr(lock
, cpu
)->v
, 0);
456 static void *test_percpu_spinlock_thread(void *arg
)
458 struct spinlock_thread_test_data
*thread_data
= (struct spinlock_thread_test_data
*) arg
;
459 struct spinlock_test_data __rseq_percpu
*data
= thread_data
->data
;
462 if (!opt_disable_rseq
&& thread_data
->reg
&&
463 rseq_register_current_thread())
465 reps
= thread_data
->reps
;
466 for (i
= 0; i
< reps
; i
++) {
467 int cpu
= rseq_this_cpu_lock(&data
->lock
);
468 rseq_percpu_ptr(data
, cpu
)->count
++;
469 rseq_percpu_unlock(&data
->lock
, cpu
);
471 if (i
!= 0 && !(i
% (reps
/ 10)))
472 printf_verbose("tid %d: count %lld\n",
473 (int) rseq_gettid(), i
);
476 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
477 (int) rseq_gettid(), nr_abort
, signals_delivered
);
478 if (!opt_disable_rseq
&& thread_data
->reg
&&
479 rseq_unregister_current_thread())
485 * A simple test which implements a sharded counter using a per-cpu
486 * lock. Obviously real applications might prefer to simply use a
487 * per-cpu increment; however, this is reasonable for a test and the
488 * lock can be extended to synchronize more complicated operations.
490 static void test_percpu_spinlock(void)
492 const int num_threads
= opt_threads
;
495 pthread_t test_threads
[num_threads
];
496 struct spinlock_test_data __rseq_percpu
*data
;
497 struct spinlock_thread_test_data thread_data
[num_threads
];
498 struct rseq_mempool
*mempool
;
499 struct rseq_mempool_attr
*attr
;
501 attr
= rseq_mempool_attr_create();
503 perror("rseq_mempool_attr_create");
506 ret
= rseq_mempool_attr_set_percpu(attr
, RSEQ_MEMPOOL_STRIDE
, CPU_SETSIZE
);
508 perror("rseq_mempool_attr_set_percpu");
511 mempool
= rseq_mempool_create("spinlock_test_data",
512 sizeof(struct spinlock_test_data
), attr
);
514 perror("rseq_mempool_create");
517 rseq_mempool_attr_destroy(attr
);
518 data
= (struct spinlock_test_data __rseq_percpu
*)rseq_mempool_percpu_zmalloc(mempool
);
520 perror("rseq_mempool_percpu_zmalloc");
524 for (i
= 0; i
< num_threads
; i
++) {
525 thread_data
[i
].reps
= opt_reps
;
526 if (opt_disable_mod
<= 0 || (i
% opt_disable_mod
))
527 thread_data
[i
].reg
= 1;
529 thread_data
[i
].reg
= 0;
530 thread_data
[i
].data
= data
;
531 ret
= pthread_create(&test_threads
[i
], NULL
,
532 test_percpu_spinlock_thread
,
536 perror("pthread_create");
541 for (i
= 0; i
< num_threads
; i
++) {
542 ret
= pthread_join(test_threads
[i
], NULL
);
545 perror("pthread_join");
551 for (i
= 0; i
< CPU_SETSIZE
; i
++)
552 sum
+= rseq_percpu_ptr(data
, i
)->count
;
554 assert(sum
== (uint64_t)opt_reps
* num_threads
);
555 rseq_mempool_percpu_free(data
);
556 ret
= rseq_mempool_destroy(mempool
);
558 perror("rseq_mempool_destroy");
563 static void *test_percpu_inc_thread(void *arg
)
565 struct inc_thread_test_data
*thread_data
= (struct inc_thread_test_data
*) arg
;
566 struct inc_test_data __rseq_percpu
*data
= thread_data
->data
;
569 if (!opt_disable_rseq
&& thread_data
->reg
&&
570 rseq_register_current_thread())
572 reps
= thread_data
->reps
;
573 for (i
= 0; i
< reps
; i
++) {
579 cpu
= get_current_cpu_id();
580 ret
= rseq_load_add_store__ptr(RSEQ_MO_RELAXED
, RSEQ_PERCPU
,
581 &rseq_percpu_ptr(data
, cpu
)->count
, 1, cpu
);
582 } while (rseq_unlikely(ret
));
584 if (i
!= 0 && !(i
% (reps
/ 10)))
585 printf_verbose("tid %d: count %lld\n",
586 (int) rseq_gettid(), i
);
589 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
590 (int) rseq_gettid(), nr_abort
, signals_delivered
);
591 if (!opt_disable_rseq
&& thread_data
->reg
&&
592 rseq_unregister_current_thread())
597 static void test_percpu_inc(void)
599 const int num_threads
= opt_threads
;
602 pthread_t test_threads
[num_threads
];
603 struct inc_test_data __rseq_percpu
*data
;
604 struct inc_thread_test_data thread_data
[num_threads
];
605 struct rseq_mempool
*mempool
;
606 struct rseq_mempool_attr
*attr
;
608 attr
= rseq_mempool_attr_create();
610 perror("rseq_mempool_attr_create");
613 ret
= rseq_mempool_attr_set_percpu(attr
, RSEQ_MEMPOOL_STRIDE
, CPU_SETSIZE
);
615 perror("rseq_mempool_attr_set_percpu");
618 mempool
= rseq_mempool_create("inc_test_data",
619 sizeof(struct inc_test_data
), attr
);
621 perror("rseq_mempool_create");
624 rseq_mempool_attr_destroy(attr
);
625 data
= (struct inc_test_data __rseq_percpu
*)rseq_mempool_percpu_zmalloc(mempool
);
627 perror("rseq_mempool_percpu_zmalloc");
631 for (i
= 0; i
< num_threads
; i
++) {
632 thread_data
[i
].reps
= opt_reps
;
633 if (opt_disable_mod
<= 0 || (i
% opt_disable_mod
))
634 thread_data
[i
].reg
= 1;
636 thread_data
[i
].reg
= 0;
637 thread_data
[i
].data
= data
;
638 ret
= pthread_create(&test_threads
[i
], NULL
,
639 test_percpu_inc_thread
,
643 perror("pthread_create");
648 for (i
= 0; i
< num_threads
; i
++) {
649 ret
= pthread_join(test_threads
[i
], NULL
);
652 perror("pthread_join");
658 for (i
= 0; i
< CPU_SETSIZE
; i
++)
659 sum
+= rseq_percpu_ptr(data
, i
)->count
;
661 assert(sum
== (uint64_t)opt_reps
* num_threads
);
662 rseq_mempool_percpu_free(data
);
663 ret
= rseq_mempool_destroy(mempool
);
665 perror("rseq_mempool_destroy");
670 static void this_cpu_list_push(struct percpu_list __rseq_percpu
*list
,
671 struct percpu_list_node
*node
,
677 intptr_t *targetptr
, newval
, expect
;
678 struct percpu_list
*cpulist
;
681 cpu
= get_current_cpu_id();
682 cpulist
= rseq_percpu_ptr(list
, cpu
);
683 /* Load list->c[cpu].head with single-copy atomicity. */
684 expect
= (intptr_t)RSEQ_READ_ONCE(cpulist
->head
);
685 newval
= (intptr_t)node
;
686 targetptr
= (intptr_t *)&cpulist
->head
;
687 node
->next
= (struct percpu_list_node
*)expect
;
688 ret
= rseq_load_cbne_store__ptr(RSEQ_MO_RELAXED
, RSEQ_PERCPU
,
689 targetptr
, expect
, newval
, cpu
);
690 if (rseq_likely(!ret
))
692 /* Retry if comparison fails or rseq aborts. */
699 * Unlike a traditional lock-less linked list; the availability of a
700 * rseq primitive allows us to implement pop without concerns over
703 static struct percpu_list_node
*this_cpu_list_pop(struct percpu_list __rseq_percpu
*list
,
706 struct percpu_list_node
*node
= NULL
;
710 struct percpu_list_node
*head
;
711 intptr_t *targetptr
, expectnot
, *load
;
712 struct percpu_list
*cpulist
;
716 cpu
= get_current_cpu_id();
717 cpulist
= rseq_percpu_ptr(list
, cpu
);
718 targetptr
= (intptr_t *)&cpulist
->head
;
719 expectnot
= (intptr_t)NULL
;
720 offset
= offsetof(struct percpu_list_node
, next
);
721 load
= (intptr_t *)&head
;
722 ret
= rseq_load_cbeq_store_add_load_store__ptr(RSEQ_MO_RELAXED
, RSEQ_PERCPU
,
723 targetptr
, expectnot
,
725 if (rseq_likely(!ret
)) {
731 /* Retry if rseq aborts. */
739 * __percpu_list_pop is not safe against concurrent accesses. Should
740 * only be used on lists that are not concurrently modified.
742 static struct percpu_list_node
*__percpu_list_pop(struct percpu_list __rseq_percpu
*list
, int cpu
)
744 struct percpu_list
*cpulist
= rseq_percpu_ptr(list
, cpu
);
745 struct percpu_list_node
*node
;
747 node
= cpulist
->head
;
750 cpulist
->head
= node
->next
;
754 static void *test_percpu_list_thread(void *arg
)
757 struct percpu_list __rseq_percpu
*list
= (struct percpu_list __rseq_percpu
*)arg
;
759 if (!opt_disable_rseq
&& rseq_register_current_thread())
763 for (i
= 0; i
< reps
; i
++) {
764 struct percpu_list_node
*node
;
766 node
= this_cpu_list_pop(list
, NULL
);
768 sched_yield(); /* encourage shuffling */
770 this_cpu_list_push(list
, node
, NULL
);
773 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
774 (int) rseq_gettid(), nr_abort
, signals_delivered
);
775 if (!opt_disable_rseq
&& rseq_unregister_current_thread())
781 /* Simultaneous modification to a per-cpu linked list from many threads. */
782 static void test_percpu_list(void)
784 const int num_threads
= opt_threads
;
786 uint64_t sum
= 0, expected_sum
= 0;
787 struct percpu_list __rseq_percpu
*list
;
788 pthread_t test_threads
[num_threads
];
789 cpu_set_t allowed_cpus
;
790 struct rseq_mempool
*mempool
;
791 struct rseq_mempool_attr
*attr
;
793 attr
= rseq_mempool_attr_create();
795 perror("rseq_mempool_attr_create");
798 ret
= rseq_mempool_attr_set_percpu(attr
, RSEQ_MEMPOOL_STRIDE
, CPU_SETSIZE
);
800 perror("rseq_mempool_attr_set_percpu");
803 mempool
= rseq_mempool_create("percpu_list",
804 sizeof(struct percpu_list
), attr
);
806 perror("rseq_mempool_create");
809 rseq_mempool_attr_destroy(attr
);
810 list
= (struct percpu_list __rseq_percpu
*)rseq_mempool_percpu_zmalloc(mempool
);
812 perror("rseq_mempool_percpu_zmalloc");
816 /* Generate list entries for every usable cpu. */
817 sched_getaffinity(0, sizeof(allowed_cpus
), &allowed_cpus
);
818 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
819 if (rseq_use_cpu_index() && !CPU_ISSET(i
, &allowed_cpus
))
821 for (j
= 1; j
<= 100; j
++) {
822 struct percpu_list
*cpulist
= rseq_percpu_ptr(list
, i
);
823 struct percpu_list_node
*node
;
827 node
= (struct percpu_list_node
*) malloc(sizeof(*node
));
830 node
->next
= cpulist
->head
;
831 cpulist
->head
= node
;
835 for (i
= 0; i
< num_threads
; i
++) {
836 ret
= pthread_create(&test_threads
[i
], NULL
,
837 test_percpu_list_thread
, list
);
840 perror("pthread_create");
845 for (i
= 0; i
< num_threads
; i
++) {
846 ret
= pthread_join(test_threads
[i
], NULL
);
849 perror("pthread_join");
854 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
855 struct percpu_list_node
*node
;
857 if (rseq_use_cpu_index() && !CPU_ISSET(i
, &allowed_cpus
))
860 while ((node
= __percpu_list_pop(list
, i
))) {
867 * All entries should now be accounted for (unless some external
868 * actor is interfering with our allowed affinity while this
871 assert(sum
== expected_sum
);
872 rseq_mempool_percpu_free(list
);
873 ret
= rseq_mempool_destroy(mempool
);
875 perror("rseq_mempool_destroy");
880 static bool this_cpu_buffer_push(struct percpu_buffer __rseq_percpu
*buffer
,
881 struct percpu_buffer_node
*node
,
888 struct percpu_buffer
*cpubuffer
;
889 intptr_t *targetptr_spec
, newval_spec
;
890 intptr_t *targetptr_final
, newval_final
;
894 cpu
= get_current_cpu_id();
895 cpubuffer
= rseq_percpu_ptr(buffer
, cpu
);
896 offset
= RSEQ_READ_ONCE(cpubuffer
->offset
);
897 if (offset
== cpubuffer
->buflen
)
899 newval_spec
= (intptr_t)node
;
900 targetptr_spec
= (intptr_t *)&cpubuffer
->array
[offset
];
901 newval_final
= offset
+ 1;
902 targetptr_final
= &cpubuffer
->offset
;
903 ret
= rseq_load_cbne_store_store__ptr(opt_mo
, RSEQ_PERCPU
,
904 targetptr_final
, offset
, targetptr_spec
,
905 newval_spec
, newval_final
, cpu
);
906 if (rseq_likely(!ret
)) {
910 /* Retry if comparison fails or rseq aborts. */
917 static struct percpu_buffer_node
*this_cpu_buffer_pop(struct percpu_buffer __rseq_percpu
*buffer
,
920 struct percpu_buffer_node
*head
;
924 struct percpu_buffer
*cpubuffer
;
925 intptr_t *targetptr
, newval
;
929 cpu
= get_current_cpu_id();
930 cpubuffer
= rseq_percpu_ptr(buffer
, cpu
);
931 /* Load offset with single-copy atomicity. */
932 offset
= RSEQ_READ_ONCE(cpubuffer
->offset
);
937 head
= RSEQ_READ_ONCE(cpubuffer
->array
[offset
- 1]);
939 targetptr
= (intptr_t *)&cpubuffer
->offset
;
940 ret
= rseq_load_cbne_load_cbne_store__ptr(RSEQ_MO_RELAXED
, RSEQ_PERCPU
,
942 (intptr_t *)&cpubuffer
->array
[offset
- 1],
943 (intptr_t)head
, newval
, cpu
);
944 if (rseq_likely(!ret
))
946 /* Retry if comparison fails or rseq aborts. */
954 * __percpu_buffer_pop is not safe against concurrent accesses. Should
955 * only be used on buffers that are not concurrently modified.
957 static struct percpu_buffer_node
*__percpu_buffer_pop(struct percpu_buffer __rseq_percpu
*buffer
,
960 struct percpu_buffer
*cpubuffer
;
961 struct percpu_buffer_node
*head
;
964 cpubuffer
= rseq_percpu_ptr(buffer
, cpu
);
965 offset
= cpubuffer
->offset
;
968 head
= cpubuffer
->array
[offset
- 1];
969 cpubuffer
->offset
= offset
- 1;
973 static void *test_percpu_buffer_thread(void *arg
)
976 struct percpu_buffer __rseq_percpu
*buffer
= (struct percpu_buffer __rseq_percpu
*)arg
;
978 if (!opt_disable_rseq
&& rseq_register_current_thread())
982 for (i
= 0; i
< reps
; i
++) {
983 struct percpu_buffer_node
*node
;
985 node
= this_cpu_buffer_pop(buffer
, NULL
);
987 sched_yield(); /* encourage shuffling */
989 if (!this_cpu_buffer_push(buffer
, node
, NULL
)) {
990 /* Should increase buffer size. */
996 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
997 (int) rseq_gettid(), nr_abort
, signals_delivered
);
998 if (!opt_disable_rseq
&& rseq_unregister_current_thread())
1004 /* Simultaneous modification to a per-cpu buffer from many threads. */
1005 static void test_percpu_buffer(void)
1007 const int num_threads
= opt_threads
;
1009 uint64_t sum
= 0, expected_sum
= 0;
1010 struct percpu_buffer __rseq_percpu
*buffer
;
1011 pthread_t test_threads
[num_threads
];
1012 cpu_set_t allowed_cpus
;
1013 struct rseq_mempool
*mempool
;
1014 struct rseq_mempool_attr
*attr
;
1016 attr
= rseq_mempool_attr_create();
1018 perror("rseq_mempool_attr_create");
1021 ret
= rseq_mempool_attr_set_percpu(attr
, RSEQ_MEMPOOL_STRIDE
, CPU_SETSIZE
);
1023 perror("rseq_mempool_attr_set_percpu");
1026 mempool
= rseq_mempool_create("percpu_buffer",
1027 sizeof(struct percpu_buffer
), attr
);
1029 perror("rseq_mempool_create");
1032 rseq_mempool_attr_destroy(attr
);
1033 buffer
= (struct percpu_buffer __rseq_percpu
*)rseq_mempool_percpu_zmalloc(mempool
);
1035 perror("rseq_mempool_percpu_zmalloc");
1039 /* Generate list entries for every usable cpu. */
1040 sched_getaffinity(0, sizeof(allowed_cpus
), &allowed_cpus
);
1041 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
1042 struct percpu_buffer
*cpubuffer
;
1044 if (rseq_use_cpu_index() && !CPU_ISSET(i
, &allowed_cpus
))
1046 cpubuffer
= rseq_percpu_ptr(buffer
, i
);
1047 /* Worse-case is every item in same CPU. */
1049 (struct percpu_buffer_node
**)
1050 malloc(sizeof(*cpubuffer
->array
) * CPU_SETSIZE
*
1051 BUFFER_ITEM_PER_CPU
);
1052 assert(cpubuffer
->array
);
1053 cpubuffer
->buflen
= CPU_SETSIZE
* BUFFER_ITEM_PER_CPU
;
1054 for (j
= 1; j
<= BUFFER_ITEM_PER_CPU
; j
++) {
1055 struct percpu_buffer_node
*node
;
1060 * We could theoretically put the word-sized
1061 * "data" directly in the buffer. However, we
1062 * want to model objects that would not fit
1063 * within a single word, so allocate an object
1066 node
= (struct percpu_buffer_node
*) malloc(sizeof(*node
));
1069 cpubuffer
->array
[j
- 1] = node
;
1070 cpubuffer
->offset
++;
1074 for (i
= 0; i
< num_threads
; i
++) {
1075 ret
= pthread_create(&test_threads
[i
], NULL
,
1076 test_percpu_buffer_thread
, buffer
);
1079 perror("pthread_create");
1084 for (i
= 0; i
< num_threads
; i
++) {
1085 ret
= pthread_join(test_threads
[i
], NULL
);
1088 perror("pthread_join");
1093 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
1094 struct percpu_buffer
*cpubuffer
;
1095 struct percpu_buffer_node
*node
;
1097 if (rseq_use_cpu_index() && !CPU_ISSET(i
, &allowed_cpus
))
1100 cpubuffer
= rseq_percpu_ptr(buffer
, i
);
1101 while ((node
= __percpu_buffer_pop(buffer
, i
))) {
1105 free(cpubuffer
->array
);
1109 * All entries should now be accounted for (unless some external
1110 * actor is interfering with our allowed affinity while this
1113 assert(sum
== expected_sum
);
1114 rseq_mempool_percpu_free(buffer
);
1115 ret
= rseq_mempool_destroy(mempool
);
1117 perror("rseq_mempool_destroy");
1122 static bool this_cpu_memcpy_buffer_push(struct percpu_memcpy_buffer __rseq_percpu
*buffer
,
1123 struct percpu_memcpy_buffer_node item
,
1126 bool result
= false;
1130 struct percpu_memcpy_buffer
*cpubuffer
;
1131 intptr_t *targetptr_final
, newval_final
, offset
;
1132 char *destptr
, *srcptr
;
1136 cpu
= get_current_cpu_id();
1137 cpubuffer
= rseq_percpu_ptr(buffer
, cpu
);
1138 /* Load offset with single-copy atomicity. */
1139 offset
= RSEQ_READ_ONCE(cpubuffer
->offset
);
1140 if (offset
== cpubuffer
->buflen
)
1142 destptr
= (char *)&cpubuffer
->array
[offset
];
1143 srcptr
= (char *)&item
;
1144 /* copylen must be <= 4kB. */
1145 copylen
= sizeof(item
);
1146 newval_final
= offset
+ 1;
1147 targetptr_final
= &cpubuffer
->offset
;
1148 ret
= rseq_load_cbne_memcpy_store__ptr(
1149 opt_mo
, RSEQ_PERCPU
,
1150 targetptr_final
, offset
,
1151 destptr
, srcptr
, copylen
,
1153 if (rseq_likely(!ret
)) {
1157 /* Retry if comparison fails or rseq aborts. */
1164 static bool this_cpu_memcpy_buffer_pop(struct percpu_memcpy_buffer __rseq_percpu
*buffer
,
1165 struct percpu_memcpy_buffer_node
*item
,
1168 bool result
= false;
1172 struct percpu_memcpy_buffer
*cpubuffer
;
1173 intptr_t *targetptr_final
, newval_final
, offset
;
1174 char *destptr
, *srcptr
;
1178 cpu
= get_current_cpu_id();
1179 cpubuffer
= rseq_percpu_ptr(buffer
, cpu
);
1180 /* Load offset with single-copy atomicity. */
1181 offset
= RSEQ_READ_ONCE(cpubuffer
->offset
);
1184 destptr
= (char *)item
;
1185 srcptr
= (char *)&cpubuffer
->array
[offset
- 1];
1186 /* copylen must be <= 4kB. */
1187 copylen
= sizeof(*item
);
1188 newval_final
= offset
- 1;
1189 targetptr_final
= &cpubuffer
->offset
;
1190 ret
= rseq_load_cbne_memcpy_store__ptr(RSEQ_MO_RELAXED
, RSEQ_PERCPU
,
1191 targetptr_final
, offset
, destptr
, srcptr
, copylen
,
1193 if (rseq_likely(!ret
)) {
1197 /* Retry if comparison fails or rseq aborts. */
1205 * __percpu_memcpy_buffer_pop is not safe against concurrent accesses. Should
1206 * only be used on buffers that are not concurrently modified.
1208 static bool __percpu_memcpy_buffer_pop(struct percpu_memcpy_buffer __rseq_percpu
*buffer
,
1209 struct percpu_memcpy_buffer_node
*item
,
1212 struct percpu_memcpy_buffer
*cpubuffer
;
1215 cpubuffer
= rseq_percpu_ptr(buffer
, cpu
);
1216 offset
= cpubuffer
->offset
;
1219 memcpy(item
, &cpubuffer
->array
[offset
- 1], sizeof(*item
));
1220 cpubuffer
->offset
= offset
- 1;
1224 static void *test_percpu_memcpy_buffer_thread(void *arg
)
1227 struct percpu_memcpy_buffer __rseq_percpu
*buffer
= (struct percpu_memcpy_buffer __rseq_percpu
*)arg
;
1229 if (!opt_disable_rseq
&& rseq_register_current_thread())
1233 for (i
= 0; i
< reps
; i
++) {
1234 struct percpu_memcpy_buffer_node item
;
1237 result
= this_cpu_memcpy_buffer_pop(buffer
, &item
, NULL
);
1239 sched_yield(); /* encourage shuffling */
1241 if (!this_cpu_memcpy_buffer_push(buffer
, item
, NULL
)) {
1242 /* Should increase buffer size. */
1248 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
1249 (int) rseq_gettid(), nr_abort
, signals_delivered
);
1250 if (!opt_disable_rseq
&& rseq_unregister_current_thread())
1256 /* Simultaneous modification to a per-cpu buffer from many threads. */
1257 static void test_percpu_memcpy_buffer(void)
1259 const int num_threads
= opt_threads
;
1261 uint64_t sum
= 0, expected_sum
= 0;
1262 struct percpu_memcpy_buffer
*buffer
;
1263 pthread_t test_threads
[num_threads
];
1264 cpu_set_t allowed_cpus
;
1265 struct rseq_mempool
*mempool
;
1266 struct rseq_mempool_attr
*attr
;
1268 attr
= rseq_mempool_attr_create();
1270 perror("rseq_mempool_attr_create");
1273 ret
= rseq_mempool_attr_set_percpu(attr
, RSEQ_MEMPOOL_STRIDE
, CPU_SETSIZE
);
1275 perror("rseq_mempool_attr_set_percpu");
1278 mempool
= rseq_mempool_create("percpu_memcpy_buffer",
1279 sizeof(struct percpu_memcpy_buffer
), attr
);
1281 perror("rseq_mempool_create");
1284 rseq_mempool_attr_destroy(attr
);
1285 buffer
= (struct percpu_memcpy_buffer __rseq_percpu
*)rseq_mempool_percpu_zmalloc(mempool
);
1287 perror("rseq_mempool_percpu_zmalloc");
1291 /* Generate list entries for every usable cpu. */
1292 sched_getaffinity(0, sizeof(allowed_cpus
), &allowed_cpus
);
1293 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
1294 struct percpu_memcpy_buffer
*cpubuffer
;
1296 if (rseq_use_cpu_index() && !CPU_ISSET(i
, &allowed_cpus
))
1298 cpubuffer
= rseq_percpu_ptr(buffer
, i
);
1299 /* Worse-case is every item in same CPU. */
1301 (struct percpu_memcpy_buffer_node
*)
1302 malloc(sizeof(*cpubuffer
->array
) * CPU_SETSIZE
*
1303 MEMCPY_BUFFER_ITEM_PER_CPU
);
1304 assert(cpubuffer
->array
);
1305 cpubuffer
->buflen
= CPU_SETSIZE
* MEMCPY_BUFFER_ITEM_PER_CPU
;
1306 for (j
= 1; j
<= MEMCPY_BUFFER_ITEM_PER_CPU
; j
++) {
1307 expected_sum
+= 2 * j
+ 1;
1310 * We could theoretically put the word-sized
1311 * "data" directly in the buffer. However, we
1312 * want to model objects that would not fit
1313 * within a single word, so allocate an object
1316 cpubuffer
->array
[j
- 1].data1
= j
;
1317 cpubuffer
->array
[j
- 1].data2
= j
+ 1;
1318 cpubuffer
->offset
++;
1322 for (i
= 0; i
< num_threads
; i
++) {
1323 ret
= pthread_create(&test_threads
[i
], NULL
,
1324 test_percpu_memcpy_buffer_thread
,
1328 perror("pthread_create");
1333 for (i
= 0; i
< num_threads
; i
++) {
1334 ret
= pthread_join(test_threads
[i
], NULL
);
1337 perror("pthread_join");
1342 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
1343 struct percpu_memcpy_buffer_node item
;
1344 struct percpu_memcpy_buffer
*cpubuffer
;
1346 if (rseq_use_cpu_index() && !CPU_ISSET(i
, &allowed_cpus
))
1349 cpubuffer
= rseq_percpu_ptr(buffer
, i
);
1350 while (__percpu_memcpy_buffer_pop(buffer
, &item
, i
)) {
1354 free(cpubuffer
->array
);
1358 * All entries should now be accounted for (unless some external
1359 * actor is interfering with our allowed affinity while this
1362 assert(sum
== expected_sum
);
1363 rseq_mempool_percpu_free(buffer
);
1364 ret
= rseq_mempool_destroy(mempool
);
1366 perror("rseq_mempool_destroy");
1371 static void test_signal_interrupt_handler(__attribute__ ((unused
)) int signo
)
1373 signals_delivered
++;
1376 static int set_signal_handler(void)
1379 struct sigaction sa
;
1382 ret
= sigemptyset(&sigset
);
1384 perror("sigemptyset");
1388 sa
.sa_handler
= test_signal_interrupt_handler
;
1389 sa
.sa_mask
= sigset
;
1391 ret
= sigaction(SIGUSR1
, &sa
, NULL
);
1393 perror("sigaction");
1397 printf_verbose("Signal handler set for SIGUSR1\n");
1403 bool membarrier_private_expedited_rseq_available(void)
1405 int status
= sys_membarrier(MEMBARRIER_CMD_QUERY
, 0, 0);
1408 perror("membarrier");
1411 if (!(status
& MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ
))
1416 /* Test MEMBARRIER_CMD_PRIVATE_RESTART_RSEQ_ON_CPU membarrier command. */
1417 #ifdef TEST_MEMBARRIER
1418 struct test_membarrier_thread_args
{
1419 struct rseq_mempool
*mempool
;
1420 struct percpu_list __rseq_percpu
*percpu_list_ptr
;
1424 /* Worker threads modify data in their "active" percpu lists. */
1426 void *test_membarrier_worker_thread(void *arg
)
1428 struct test_membarrier_thread_args
*args
=
1429 (struct test_membarrier_thread_args
*)arg
;
1430 const long long iters
= opt_reps
;
1433 if (rseq_register_current_thread()) {
1434 fprintf(stderr
, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
1435 errno
, strerror(errno
));
1439 /* Wait for initialization. */
1440 while (!rseq_smp_load_acquire(&args
->percpu_list_ptr
)) { }
1442 for (i
= 0; i
< iters
; ++i
) {
1446 int cpu
= get_current_cpu_id();
1448 ret
= rseq_load_add_load_load_add_store__ptr(RSEQ_MO_RELAXED
, RSEQ_PERCPU
,
1449 (intptr_t *) &args
->percpu_list_ptr
,
1450 (RSEQ_MEMPOOL_STRIDE
* cpu
) + offsetof(struct percpu_list
, head
),
1452 } while (rseq_unlikely(ret
));
1455 if (rseq_unregister_current_thread()) {
1456 fprintf(stderr
, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
1457 errno
, strerror(errno
));
1464 struct percpu_list __rseq_percpu
*test_membarrier_alloc_percpu_list(struct rseq_mempool
*mempool
)
1466 struct percpu_list __rseq_percpu
*list
;
1469 list
= (struct percpu_list __rseq_percpu
*)rseq_mempool_percpu_zmalloc(mempool
);
1471 perror("rseq_mempool_percpu_zmalloc");
1474 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
1475 struct percpu_list
*cpulist
= rseq_percpu_ptr(list
, i
);
1476 struct percpu_list_node
*node
;
1478 node
= (struct percpu_list_node
*) malloc(sizeof(*node
));
1482 cpulist
->head
= node
;
1488 void test_membarrier_free_percpu_list(struct percpu_list __rseq_percpu
*list
)
1492 for (i
= 0; i
< CPU_SETSIZE
; i
++)
1493 free(rseq_percpu_ptr(list
, i
)->head
);
1494 rseq_mempool_percpu_free(list
);
1498 long long test_membarrier_count_percpu_list(struct percpu_list __rseq_percpu
*list
)
1500 long long total_count
= 0;
1503 for (i
= 0; i
< CPU_SETSIZE
; i
++)
1504 total_count
+= rseq_percpu_ptr(list
, i
)->head
->data
;
1509 * The manager thread swaps per-cpu lists that worker threads see,
1510 * and validates that there are no unexpected modifications.
1513 void *test_membarrier_manager_thread(void *arg
)
1515 struct test_membarrier_thread_args
*args
=
1516 (struct test_membarrier_thread_args
*)arg
;
1517 struct percpu_list __rseq_percpu
*list_a
, __rseq_percpu
*list_b
;
1518 intptr_t expect_a
= 0, expect_b
= 0;
1519 int cpu_a
= 0, cpu_b
= 0;
1520 struct rseq_mempool
*mempool
;
1522 long long total_count
= 0;
1523 struct rseq_mempool_attr
*attr
;
1525 attr
= rseq_mempool_attr_create();
1527 perror("rseq_mempool_attr_create");
1530 ret
= rseq_mempool_attr_set_percpu(attr
, RSEQ_MEMPOOL_STRIDE
, CPU_SETSIZE
);
1532 perror("rseq_mempool_attr_set_percpu");
1535 mempool
= rseq_mempool_create("percpu_list",
1536 sizeof(struct percpu_list
), attr
);
1538 perror("rseq_mempool_create");
1541 rseq_mempool_attr_destroy(attr
);
1542 args
->mempool
= mempool
;
1544 if (rseq_register_current_thread()) {
1545 fprintf(stderr
, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
1546 errno
, strerror(errno
));
1551 list_a
= test_membarrier_alloc_percpu_list(mempool
);
1553 list_b
= test_membarrier_alloc_percpu_list(mempool
);
1556 /* Initialize lists before publishing them. */
1559 RSEQ_WRITE_ONCE(args
->percpu_list_ptr
, list_a
);
1561 while (!RSEQ_READ_ONCE(args
->stop
)) {
1562 /* list_a is "active". */
1563 cpu_a
= rand() % CPU_SETSIZE
;
1565 * As list_b is "inactive", we should never see changes
1568 if (expect_b
!= RSEQ_READ_ONCE(rseq_percpu_ptr(list_b
, cpu_b
)->head
->data
)) {
1569 fprintf(stderr
, "Membarrier test failed\n");
1573 /* Make list_b "active". */
1574 RSEQ_WRITE_ONCE(args
->percpu_list_ptr
, list_b
);
1575 if (rseq_membarrier_expedited(cpu_a
) &&
1576 errno
!= ENXIO
/* missing CPU */) {
1577 perror("sys_membarrier");
1581 * Cpu A should now only modify list_b, so the values
1582 * in list_a should be stable.
1584 expect_a
= RSEQ_READ_ONCE(rseq_percpu_ptr(list_a
, cpu_a
)->head
->data
);
1586 cpu_b
= rand() % CPU_SETSIZE
;
1588 * As list_a is "inactive", we should never see changes
1591 if (expect_a
!= RSEQ_READ_ONCE(rseq_percpu_ptr(list_a
, cpu_a
)->head
->data
)) {
1592 fprintf(stderr
, "Membarrier test failed\n");
1596 /* Make list_a "active". */
1597 RSEQ_WRITE_ONCE(args
->percpu_list_ptr
, list_a
);
1598 if (rseq_membarrier_expedited(cpu_b
) &&
1599 errno
!= ENXIO
/* missing CPU */) {
1600 perror("sys_membarrier");
1603 /* Remember a value from list_b. */
1604 expect_b
= RSEQ_READ_ONCE(rseq_percpu_ptr(list_b
, cpu_b
)->head
->data
);
1607 total_count
+= test_membarrier_count_percpu_list(list_a
);
1608 total_count
+= test_membarrier_count_percpu_list(list_b
);
1610 /* Validate that we observe the right number of increments. */
1611 if (total_count
!= opt_threads
* opt_reps
) {
1612 fprintf(stderr
, "Error: Observed %lld increments, expected %lld\n",
1613 total_count
, opt_threads
* opt_reps
);
1616 test_membarrier_free_percpu_list(list_a
);
1617 test_membarrier_free_percpu_list(list_b
);
1619 if (rseq_unregister_current_thread()) {
1620 fprintf(stderr
, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
1621 errno
, strerror(errno
));
1624 ret
= rseq_mempool_destroy(mempool
);
1626 perror("rseq_mempool_destroy");
1634 void test_membarrier(void)
1636 const int num_threads
= opt_threads
;
1637 struct test_membarrier_thread_args thread_args
;
1638 pthread_t worker_threads
[num_threads
];
1639 pthread_t manager_thread
;
1642 if (!membarrier_private_expedited_rseq_available()) {
1643 fprintf(stderr
, "Membarrier private expedited rseq not available. "
1644 "Skipping membarrier test.\n");
1647 if (sys_membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ
, 0, 0)) {
1648 perror("sys_membarrier");
1652 thread_args
.percpu_list_ptr
= NULL
;
1653 thread_args
.stop
= 0;
1654 ret
= pthread_create(&manager_thread
, NULL
,
1655 test_membarrier_manager_thread
, &thread_args
);
1658 perror("pthread_create");
1662 for (i
= 0; i
< num_threads
; i
++) {
1663 ret
= pthread_create(&worker_threads
[i
], NULL
,
1664 test_membarrier_worker_thread
, &thread_args
);
1667 perror("pthread_create");
1673 for (i
= 0; i
< num_threads
; i
++) {
1674 ret
= pthread_join(worker_threads
[i
], NULL
);
1677 perror("pthread_join");
1682 RSEQ_WRITE_ONCE(thread_args
.stop
, 1);
1683 ret
= pthread_join(manager_thread
, NULL
);
1686 perror("pthread_join");
1690 #else /* TEST_MEMBARRIER */
1692 void test_membarrier(void)
1694 if (!membarrier_private_expedited_rseq_available()) {
1695 fprintf(stderr
, "Membarrier private expedited rseq not available. "
1696 "Skipping membarrier test.\n");
1699 fprintf(stderr
, "rseq_load_add_load_load_add_store__ptr is not implemented on this architecture. "
1700 "Skipping membarrier test.\n");
1704 static void show_usage(char **argv
)
1706 printf("Usage : %s <OPTIONS>\n",
1708 printf("OPTIONS:\n");
1709 printf(" [-1 loops] Number of loops for delay injection 1\n");
1710 printf(" [-2 loops] Number of loops for delay injection 2\n");
1711 printf(" [-3 loops] Number of loops for delay injection 3\n");
1712 printf(" [-4 loops] Number of loops for delay injection 4\n");
1713 printf(" [-5 loops] Number of loops for delay injection 5\n");
1714 printf(" [-6 loops] Number of loops for delay injection 6\n");
1715 printf(" [-7 loops] Number of loops for delay injection 7 (-1 to enable -m)\n");
1716 printf(" [-8 loops] Number of loops for delay injection 8 (-1 to enable -m)\n");
1717 printf(" [-9 loops] Number of loops for delay injection 9 (-1 to enable -m)\n");
1718 printf(" [-m N] Yield/sleep/kill every modulo N (default 0: disabled) (>= 0)\n");
1719 printf(" [-y] Yield\n");
1720 printf(" [-k] Kill thread with signal\n");
1721 printf(" [-s S] S: =0: disabled (default), >0: sleep time (ms)\n");
1722 printf(" [-t N] Number of threads (default 200)\n");
1723 printf(" [-r N] Number of repetitions per thread (default 5000)\n");
1724 printf(" [-d] Disable rseq system call (no initialization)\n");
1725 printf(" [-D M] Disable rseq for each M threads\n");
1726 printf(" [-T test] Choose test: (s)pinlock, (l)ist, (b)uffer, (m)emcpy, (i)ncrement, membarrie(r)\n");
1727 printf(" [-M] Push into buffer and memcpy buffer with memory barriers.\n");
1728 printf(" [-c] Check if the rseq syscall is available.\n");
1729 printf(" [-v] Verbose output.\n");
1730 printf(" [-h] Show this help.\n");
1734 int main(int argc
, char **argv
)
1738 for (i
= 1; i
< argc
; i
++) {
1739 if (argv
[i
][0] != '-')
1741 switch (argv
[i
][1]) {
1755 loop_cnt
[argv
[i
][1] - '0'] = atol(argv
[i
+ 1]);
1763 opt_modulo
= atol(argv
[i
+ 1]);
1764 if (opt_modulo
< 0) {
1775 opt_sleep
= atol(argv
[i
+ 1]);
1776 if (opt_sleep
< 0) {
1789 opt_disable_rseq
= 1;
1796 opt_disable_mod
= atol(argv
[i
+ 1]);
1797 if (opt_disable_mod
< 0) {
1808 opt_threads
= atol(argv
[i
+ 1]);
1809 if (opt_threads
< 0) {
1820 opt_reps
= atoll(argv
[i
+ 1]);
1835 opt_test
= *argv
[i
+ 1];
1854 opt_mo
= RSEQ_MO_RELEASE
;
1857 if (rseq_available(RSEQ_AVAILABLE_QUERY_KERNEL
)) {
1858 printf_verbose("The rseq syscall is available.\n");
1861 printf_verbose("The rseq syscall is unavailable.\n");
1870 loop_cnt_1
= loop_cnt
[1];
1871 loop_cnt_2
= loop_cnt
[2];
1872 loop_cnt_3
= loop_cnt
[3];
1873 loop_cnt_4
= loop_cnt
[4];
1874 loop_cnt_5
= loop_cnt
[5];
1875 loop_cnt_6
= loop_cnt
[6];
1877 if (set_signal_handler())
1880 if (!opt_disable_rseq
&& rseq_register_current_thread())
1882 if (!opt_disable_rseq
&& !rseq_validate_cpu_id()) {
1883 printf_verbose("The rseq cpu id getter is unavailable\n");
1888 printf_verbose("spinlock\n");
1889 test_percpu_spinlock();
1892 printf_verbose("linked list\n");
1896 printf_verbose("buffer\n");
1897 test_percpu_buffer();
1900 printf_verbose("memcpy buffer\n");
1901 test_percpu_memcpy_buffer();
1904 printf_verbose("counter increment\n");
1908 printf_verbose("membarrier\n");
1912 if (!opt_disable_rseq
&& rseq_unregister_current_thread())