1 // SPDX-License-Identifier: LGPL-2.1
4 #include <linux/membarrier.h>
15 #include <sys/types.h>
21 static inline pid_t rseq_gettid(void)
23 return syscall(__NR_gettid);
27 static int loop_cnt[NR_INJECT + 1];
29 static int loop_cnt_1 asm("asm_loop_cnt_1") __attribute__((used));
30 static int loop_cnt_2 asm("asm_loop_cnt_2") __attribute__((used));
31 static int loop_cnt_3 asm("asm_loop_cnt_3") __attribute__((used));
32 static int loop_cnt_4 asm("asm_loop_cnt_4") __attribute__((used));
33 static int loop_cnt_5 asm("asm_loop_cnt_5") __attribute__((used));
34 static int loop_cnt_6 asm("asm_loop_cnt_6") __attribute__((used));
36 static int opt_modulo, verbose;
38 static int opt_yield, opt_signal, opt_sleep,
39 opt_disable_rseq, opt_threads = 200,
40 opt_disable_mod = 0, opt_test = 's';
42 static long long opt_reps = 5000;
44 static __thread __attribute__((tls_model("initial-exec")))
45 unsigned int signals_delivered;
49 static __thread __attribute__((tls_model("initial-exec"), unused))
50 unsigned int yield_mod_cnt, nr_abort;
52 #define printf_verbose(fmt, ...) \
55 printf(fmt, ## __VA_ARGS__); \
60 #define INJECT_ASM_REG "eax"
62 #define RSEQ_INJECT_CLOBBER \
65 #define RSEQ_INJECT_ASM(n) \
66 "mov asm_loop_cnt_" #n ", %%" INJECT_ASM_REG "\n\t" \
67 "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
70 "dec %%" INJECT_ASM_REG "\n\t" \
74 #elif defined(__x86_64__)
76 #define INJECT_ASM_REG_P "rax"
77 #define INJECT_ASM_REG "eax"
79 #define RSEQ_INJECT_CLOBBER \
83 #define RSEQ_INJECT_ASM(n) \
84 "lea asm_loop_cnt_" #n "(%%rip), %%" INJECT_ASM_REG_P "\n\t" \
85 "mov (%%" INJECT_ASM_REG_P "), %%" INJECT_ASM_REG "\n\t" \
86 "test %%" INJECT_ASM_REG ",%%" INJECT_ASM_REG "\n\t" \
89 "dec %%" INJECT_ASM_REG "\n\t" \
93 #elif defined(__s390__)
95 #define RSEQ_INJECT_INPUT \
96 , [loop_cnt_1]"m"(loop_cnt[1]) \
97 , [loop_cnt_2]"m"(loop_cnt[2]) \
98 , [loop_cnt_3]"m"(loop_cnt[3]) \
99 , [loop_cnt_4]"m"(loop_cnt[4]) \
100 , [loop_cnt_5]"m"(loop_cnt[5]) \
101 , [loop_cnt_6]"m"(loop_cnt[6])
103 #define INJECT_ASM_REG "r12"
105 #define RSEQ_INJECT_CLOBBER \
108 #define RSEQ_INJECT_ASM(n) \
109 "l %%" INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
110 "ltr %%" INJECT_ASM_REG ", %%" INJECT_ASM_REG "\n\t" \
113 "ahi %%" INJECT_ASM_REG ", -1\n\t" \
117 #elif defined(__ARMEL__)
119 #define RSEQ_INJECT_INPUT \
120 , [loop_cnt_1]"m"(loop_cnt[1]) \
121 , [loop_cnt_2]"m"(loop_cnt[2]) \
122 , [loop_cnt_3]"m"(loop_cnt[3]) \
123 , [loop_cnt_4]"m"(loop_cnt[4]) \
124 , [loop_cnt_5]"m"(loop_cnt[5]) \
125 , [loop_cnt_6]"m"(loop_cnt[6])
127 #define INJECT_ASM_REG "r4"
129 #define RSEQ_INJECT_CLOBBER \
132 #define RSEQ_INJECT_ASM(n) \
133 "ldr " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
134 "cmp " INJECT_ASM_REG ", #0\n\t" \
137 "subs " INJECT_ASM_REG ", #1\n\t" \
141 #elif defined(__AARCH64EL__)
143 #define RSEQ_INJECT_INPUT \
144 , [loop_cnt_1] "Qo" (loop_cnt[1]) \
145 , [loop_cnt_2] "Qo" (loop_cnt[2]) \
146 , [loop_cnt_3] "Qo" (loop_cnt[3]) \
147 , [loop_cnt_4] "Qo" (loop_cnt[4]) \
148 , [loop_cnt_5] "Qo" (loop_cnt[5]) \
149 , [loop_cnt_6] "Qo" (loop_cnt[6])
151 #define INJECT_ASM_REG RSEQ_ASM_TMP_REG32
153 #define RSEQ_INJECT_ASM(n) \
154 " ldr " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n" \
155 " cbz " INJECT_ASM_REG ", 333f\n" \
157 " sub " INJECT_ASM_REG ", " INJECT_ASM_REG ", #1\n" \
158 " cbnz " INJECT_ASM_REG ", 222b\n" \
161 #elif defined(__PPC__)
163 #define RSEQ_INJECT_INPUT \
164 , [loop_cnt_1]"m"(loop_cnt[1]) \
165 , [loop_cnt_2]"m"(loop_cnt[2]) \
166 , [loop_cnt_3]"m"(loop_cnt[3]) \
167 , [loop_cnt_4]"m"(loop_cnt[4]) \
168 , [loop_cnt_5]"m"(loop_cnt[5]) \
169 , [loop_cnt_6]"m"(loop_cnt[6])
171 #define INJECT_ASM_REG "r18"
173 #define RSEQ_INJECT_CLOBBER \
176 #define RSEQ_INJECT_ASM(n) \
177 "lwz %%" INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
178 "cmpwi %%" INJECT_ASM_REG ", 0\n\t" \
181 "subic. %%" INJECT_ASM_REG ", %%" INJECT_ASM_REG ", 1\n\t" \
185 #elif defined(__mips__)
187 #define RSEQ_INJECT_INPUT \
188 , [loop_cnt_1]"m"(loop_cnt[1]) \
189 , [loop_cnt_2]"m"(loop_cnt[2]) \
190 , [loop_cnt_3]"m"(loop_cnt[3]) \
191 , [loop_cnt_4]"m"(loop_cnt[4]) \
192 , [loop_cnt_5]"m"(loop_cnt[5]) \
193 , [loop_cnt_6]"m"(loop_cnt[6])
195 #define INJECT_ASM_REG "$5"
197 #define RSEQ_INJECT_CLOBBER \
200 #define RSEQ_INJECT_ASM(n) \
201 "lw " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
202 "beqz " INJECT_ASM_REG ", 333f\n\t" \
204 "addiu " INJECT_ASM_REG ", -1\n\t" \
205 "bnez " INJECT_ASM_REG ", 222b\n\t" \
207 #elif defined(__riscv)
209 #define RSEQ_INJECT_INPUT \
210 , [loop_cnt_1]"m"(loop_cnt[1]) \
211 , [loop_cnt_2]"m"(loop_cnt[2]) \
212 , [loop_cnt_3]"m"(loop_cnt[3]) \
213 , [loop_cnt_4]"m"(loop_cnt[4]) \
214 , [loop_cnt_5]"m"(loop_cnt[5]) \
215 , [loop_cnt_6]"m"(loop_cnt[6])
217 #define INJECT_ASM_REG "t1"
219 #define RSEQ_INJECT_CLOBBER \
222 #define RSEQ_INJECT_ASM(n) \
223 "lw " INJECT_ASM_REG ", %[loop_cnt_" #n "]\n\t" \
224 "beqz " INJECT_ASM_REG ", 333f\n\t" \
226 "addi " INJECT_ASM_REG "," INJECT_ASM_REG ", -1\n\t" \
227 "bnez " INJECT_ASM_REG ", 222b\n\t" \
232 #error unsupported target
235 #define RSEQ_INJECT_FAILED \
238 #define RSEQ_INJECT_C(n) \
240 int loc_i, loc_nr_loops = loop_cnt[n]; \
242 for (loc_i = 0; loc_i < loc_nr_loops; loc_i++) { \
245 if (loc_nr_loops == -1 && opt_modulo) { \
246 if (yield_mod_cnt == opt_modulo - 1) { \
248 poll(NULL, 0, opt_sleep); \
262 #define printf_verbose(fmt, ...)
264 #endif /* BENCHMARK */
268 static enum rseq_mo opt_mo = RSEQ_MO_RELAXED;
270 #ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
271 #define TEST_MEMBARRIER
273 static int sys_membarrier(int cmd, int flags, int cpu_id)
275 return syscall(__NR_membarrier, cmd, flags, cpu_id);
279 #ifdef BUILDOPT_RSEQ_PERCPU_MM_CID
280 # define RSEQ_PERCPU RSEQ_PERCPU_MM_CID
282 int get_current_cpu_id(void)
284 return rseq_current_mm_cid();
287 bool rseq_validate_cpu_id(void)
289 return rseq_mm_cid_available();
291 # ifdef TEST_MEMBARRIER
293 * Membarrier does not currently support targeting a mm_cid, so
294 * issue the barrier on all cpus.
297 int rseq_membarrier_expedited(int cpu)
299 return sys_membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ,
302 # endif /* TEST_MEMBARRIER */
304 # define RSEQ_PERCPU RSEQ_PERCPU_CPU_ID
306 int get_current_cpu_id(void)
308 return rseq_cpu_start();
311 bool rseq_validate_cpu_id(void)
313 return rseq_current_cpu_raw() >= 0;
315 # ifdef TEST_MEMBARRIER
317 int rseq_membarrier_expedited(int cpu)
319 return sys_membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ,
320 MEMBARRIER_CMD_FLAG_CPU, cpu);
322 # endif /* TEST_MEMBARRIER */
325 struct percpu_lock_entry {
327 } __attribute__((aligned(128)));
330 struct percpu_lock_entry c[CPU_SETSIZE];
333 struct test_data_entry {
335 } __attribute__((aligned(128)));
337 struct spinlock_test_data {
338 struct percpu_lock lock;
339 struct test_data_entry c[CPU_SETSIZE];
342 struct spinlock_thread_test_data {
343 struct spinlock_test_data *data;
348 struct inc_test_data {
349 struct test_data_entry c[CPU_SETSIZE];
352 struct inc_thread_test_data {
353 struct inc_test_data *data;
358 struct percpu_list_node {
360 struct percpu_list_node *next;
363 struct percpu_list_entry {
364 struct percpu_list_node *head;
365 } __attribute__((aligned(128)));
368 struct percpu_list_entry c[CPU_SETSIZE];
371 #define BUFFER_ITEM_PER_CPU 100
373 struct percpu_buffer_node {
377 struct percpu_buffer_entry {
380 struct percpu_buffer_node **array;
381 } __attribute__((aligned(128)));
383 struct percpu_buffer {
384 struct percpu_buffer_entry c[CPU_SETSIZE];
387 #define MEMCPY_BUFFER_ITEM_PER_CPU 100
389 struct percpu_memcpy_buffer_node {
394 struct percpu_memcpy_buffer_entry {
397 struct percpu_memcpy_buffer_node *array;
398 } __attribute__((aligned(128)));
400 struct percpu_memcpy_buffer {
401 struct percpu_memcpy_buffer_entry c[CPU_SETSIZE];
404 /* A simple percpu spinlock. Grabs lock on current cpu. */
405 static int rseq_this_cpu_lock(struct percpu_lock *lock)
412 cpu = get_current_cpu_id();
414 fprintf(stderr, "pid: %d: tid: %d, cpu: %d: cid: %d\n",
415 getpid(), (int) rseq_gettid(), rseq_current_cpu_raw(), cpu);
418 ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
421 if (rseq_likely(!ret))
423 /* Retry if comparison fails or rseq aborts. */
426 * Acquire semantic when taking lock after control dependency.
427 * Matches rseq_smp_store_release().
429 rseq_smp_acquire__after_ctrl_dep();
433 static void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
435 assert(lock->c[cpu].v == 1);
437 * Release lock, with release semantic. Matches
438 * rseq_smp_acquire__after_ctrl_dep().
440 rseq_smp_store_release(&lock->c[cpu].v, 0);
443 void *test_percpu_spinlock_thread(void *arg)
445 struct spinlock_thread_test_data *thread_data = arg;
446 struct spinlock_test_data *data = thread_data->data;
449 if (!opt_disable_rseq && thread_data->reg &&
450 rseq_register_current_thread())
452 reps = thread_data->reps;
453 for (i = 0; i < reps; i++) {
454 int cpu = rseq_this_cpu_lock(&data->lock);
455 data->c[cpu].count++;
456 rseq_percpu_unlock(&data->lock, cpu);
458 if (i != 0 && !(i % (reps / 10)))
459 printf_verbose("tid %d: count %lld\n",
460 (int) rseq_gettid(), i);
463 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
464 (int) rseq_gettid(), nr_abort, signals_delivered);
465 if (!opt_disable_rseq && thread_data->reg &&
466 rseq_unregister_current_thread())
472 * A simple test which implements a sharded counter using a per-cpu
473 * lock. Obviously real applications might prefer to simply use a
474 * per-cpu increment; however, this is reasonable for a test and the
475 * lock can be extended to synchronize more complicated operations.
477 void test_percpu_spinlock(void)
479 const int num_threads = opt_threads;
482 pthread_t test_threads[num_threads];
483 struct spinlock_test_data data;
484 struct spinlock_thread_test_data thread_data[num_threads];
486 memset(&data, 0, sizeof(data));
487 for (i = 0; i < num_threads; i++) {
488 thread_data[i].reps = opt_reps;
489 if (opt_disable_mod <= 0 || (i % opt_disable_mod))
490 thread_data[i].reg = 1;
492 thread_data[i].reg = 0;
493 thread_data[i].data = &data;
494 ret = pthread_create(&test_threads[i], NULL,
495 test_percpu_spinlock_thread,
499 perror("pthread_create");
504 for (i = 0; i < num_threads; i++) {
505 ret = pthread_join(test_threads[i], NULL);
508 perror("pthread_join");
514 for (i = 0; i < CPU_SETSIZE; i++)
515 sum += data.c[i].count;
517 assert(sum == (uint64_t)opt_reps * num_threads);
520 void *test_percpu_inc_thread(void *arg)
522 struct inc_thread_test_data *thread_data = arg;
523 struct inc_test_data *data = thread_data->data;
526 if (!opt_disable_rseq && thread_data->reg &&
527 rseq_register_current_thread())
529 reps = thread_data->reps;
530 for (i = 0; i < reps; i++) {
536 cpu = get_current_cpu_id();
537 ret = rseq_addv(RSEQ_MO_RELAXED, RSEQ_PERCPU,
538 &data->c[cpu].count, 1, cpu);
539 } while (rseq_unlikely(ret));
541 if (i != 0 && !(i % (reps / 10)))
542 printf_verbose("tid %d: count %lld\n",
543 (int) rseq_gettid(), i);
546 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
547 (int) rseq_gettid(), nr_abort, signals_delivered);
548 if (!opt_disable_rseq && thread_data->reg &&
549 rseq_unregister_current_thread())
554 void test_percpu_inc(void)
556 const int num_threads = opt_threads;
559 pthread_t test_threads[num_threads];
560 struct inc_test_data data;
561 struct inc_thread_test_data thread_data[num_threads];
563 memset(&data, 0, sizeof(data));
564 for (i = 0; i < num_threads; i++) {
565 thread_data[i].reps = opt_reps;
566 if (opt_disable_mod <= 0 || (i % opt_disable_mod))
567 thread_data[i].reg = 1;
569 thread_data[i].reg = 0;
570 thread_data[i].data = &data;
571 ret = pthread_create(&test_threads[i], NULL,
572 test_percpu_inc_thread,
576 perror("pthread_create");
581 for (i = 0; i < num_threads; i++) {
582 ret = pthread_join(test_threads[i], NULL);
585 perror("pthread_join");
591 for (i = 0; i < CPU_SETSIZE; i++)
592 sum += data.c[i].count;
594 assert(sum == (uint64_t)opt_reps * num_threads);
597 void this_cpu_list_push(struct percpu_list *list,
598 struct percpu_list_node *node,
604 intptr_t *targetptr, newval, expect;
607 cpu = get_current_cpu_id();
608 /* Load list->c[cpu].head with single-copy atomicity. */
609 expect = (intptr_t)RSEQ_READ_ONCE(list->c[cpu].head);
610 newval = (intptr_t)node;
611 targetptr = (intptr_t *)&list->c[cpu].head;
612 node->next = (struct percpu_list_node *)expect;
613 ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
614 targetptr, expect, newval, cpu);
615 if (rseq_likely(!ret))
617 /* Retry if comparison fails or rseq aborts. */
624 * Unlike a traditional lock-less linked list; the availability of a
625 * rseq primitive allows us to implement pop without concerns over
628 struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list,
631 struct percpu_list_node *node = NULL;
635 struct percpu_list_node *head;
636 intptr_t *targetptr, expectnot, *load;
640 cpu = get_current_cpu_id();
641 targetptr = (intptr_t *)&list->c[cpu].head;
642 expectnot = (intptr_t)NULL;
643 offset = offsetof(struct percpu_list_node, next);
644 load = (intptr_t *)&head;
645 ret = rseq_cmpnev_storeoffp_load(RSEQ_MO_RELAXED, RSEQ_PERCPU,
646 targetptr, expectnot,
648 if (rseq_likely(!ret)) {
654 /* Retry if rseq aborts. */
662 * __percpu_list_pop is not safe against concurrent accesses. Should
663 * only be used on lists that are not concurrently modified.
665 struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
667 struct percpu_list_node *node;
669 node = list->c[cpu].head;
672 list->c[cpu].head = node->next;
676 void *test_percpu_list_thread(void *arg)
679 struct percpu_list *list = (struct percpu_list *)arg;
681 if (!opt_disable_rseq && rseq_register_current_thread())
685 for (i = 0; i < reps; i++) {
686 struct percpu_list_node *node;
688 node = this_cpu_list_pop(list, NULL);
690 sched_yield(); /* encourage shuffling */
692 this_cpu_list_push(list, node, NULL);
695 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
696 (int) rseq_gettid(), nr_abort, signals_delivered);
697 if (!opt_disable_rseq && rseq_unregister_current_thread())
703 /* Simultaneous modification to a per-cpu linked list from many threads. */
704 void test_percpu_list(void)
706 const int num_threads = opt_threads;
708 uint64_t sum = 0, expected_sum = 0;
709 struct percpu_list list;
710 pthread_t test_threads[num_threads];
711 cpu_set_t allowed_cpus;
713 memset(&list, 0, sizeof(list));
715 /* Generate list entries for every usable cpu. */
716 sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
717 for (i = 0; i < CPU_SETSIZE; i++) {
718 if (!CPU_ISSET(i, &allowed_cpus))
720 for (j = 1; j <= 100; j++) {
721 struct percpu_list_node *node;
725 node = malloc(sizeof(*node));
728 node->next = list.c[i].head;
729 list.c[i].head = node;
733 for (i = 0; i < num_threads; i++) {
734 ret = pthread_create(&test_threads[i], NULL,
735 test_percpu_list_thread, &list);
738 perror("pthread_create");
743 for (i = 0; i < num_threads; i++) {
744 ret = pthread_join(test_threads[i], NULL);
747 perror("pthread_join");
752 for (i = 0; i < CPU_SETSIZE; i++) {
753 struct percpu_list_node *node;
755 if (!CPU_ISSET(i, &allowed_cpus))
758 while ((node = __percpu_list_pop(&list, i))) {
765 * All entries should now be accounted for (unless some external
766 * actor is interfering with our allowed affinity while this
769 assert(sum == expected_sum);
772 bool this_cpu_buffer_push(struct percpu_buffer *buffer,
773 struct percpu_buffer_node *node,
780 intptr_t *targetptr_spec, newval_spec;
781 intptr_t *targetptr_final, newval_final;
785 cpu = get_current_cpu_id();
786 offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
787 if (offset == buffer->c[cpu].buflen)
789 newval_spec = (intptr_t)node;
790 targetptr_spec = (intptr_t *)&buffer->c[cpu].array[offset];
791 newval_final = offset + 1;
792 targetptr_final = &buffer->c[cpu].offset;
793 ret = rseq_cmpeqv_trystorev_storev(opt_mo, RSEQ_PERCPU,
794 targetptr_final, offset, targetptr_spec,
795 newval_spec, newval_final, cpu);
796 if (rseq_likely(!ret)) {
800 /* Retry if comparison fails or rseq aborts. */
807 struct percpu_buffer_node *this_cpu_buffer_pop(struct percpu_buffer *buffer,
810 struct percpu_buffer_node *head;
814 intptr_t *targetptr, newval;
818 cpu = get_current_cpu_id();
819 /* Load offset with single-copy atomicity. */
820 offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
825 head = RSEQ_READ_ONCE(buffer->c[cpu].array[offset - 1]);
827 targetptr = (intptr_t *)&buffer->c[cpu].offset;
828 ret = rseq_cmpeqv_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
830 (intptr_t *)&buffer->c[cpu].array[offset - 1],
831 (intptr_t)head, newval, cpu);
832 if (rseq_likely(!ret))
834 /* Retry if comparison fails or rseq aborts. */
842 * __percpu_buffer_pop is not safe against concurrent accesses. Should
843 * only be used on buffers that are not concurrently modified.
845 struct percpu_buffer_node *__percpu_buffer_pop(struct percpu_buffer *buffer,
848 struct percpu_buffer_node *head;
851 offset = buffer->c[cpu].offset;
854 head = buffer->c[cpu].array[offset - 1];
855 buffer->c[cpu].offset = offset - 1;
859 void *test_percpu_buffer_thread(void *arg)
862 struct percpu_buffer *buffer = (struct percpu_buffer *)arg;
864 if (!opt_disable_rseq && rseq_register_current_thread())
868 for (i = 0; i < reps; i++) {
869 struct percpu_buffer_node *node;
871 node = this_cpu_buffer_pop(buffer, NULL);
873 sched_yield(); /* encourage shuffling */
875 if (!this_cpu_buffer_push(buffer, node, NULL)) {
876 /* Should increase buffer size. */
882 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
883 (int) rseq_gettid(), nr_abort, signals_delivered);
884 if (!opt_disable_rseq && rseq_unregister_current_thread())
890 /* Simultaneous modification to a per-cpu buffer from many threads. */
891 void test_percpu_buffer(void)
893 const int num_threads = opt_threads;
895 uint64_t sum = 0, expected_sum = 0;
896 struct percpu_buffer buffer;
897 pthread_t test_threads[num_threads];
898 cpu_set_t allowed_cpus;
900 memset(&buffer, 0, sizeof(buffer));
902 /* Generate list entries for every usable cpu. */
903 sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
904 for (i = 0; i < CPU_SETSIZE; i++) {
905 if (!CPU_ISSET(i, &allowed_cpus))
907 /* Worse-case is every item in same CPU. */
909 malloc(sizeof(*buffer.c[i].array) * CPU_SETSIZE *
910 BUFFER_ITEM_PER_CPU);
911 assert(buffer.c[i].array);
912 buffer.c[i].buflen = CPU_SETSIZE * BUFFER_ITEM_PER_CPU;
913 for (j = 1; j <= BUFFER_ITEM_PER_CPU; j++) {
914 struct percpu_buffer_node *node;
919 * We could theoretically put the word-sized
920 * "data" directly in the buffer. However, we
921 * want to model objects that would not fit
922 * within a single word, so allocate an object
925 node = malloc(sizeof(*node));
928 buffer.c[i].array[j - 1] = node;
929 buffer.c[i].offset++;
933 for (i = 0; i < num_threads; i++) {
934 ret = pthread_create(&test_threads[i], NULL,
935 test_percpu_buffer_thread, &buffer);
938 perror("pthread_create");
943 for (i = 0; i < num_threads; i++) {
944 ret = pthread_join(test_threads[i], NULL);
947 perror("pthread_join");
952 for (i = 0; i < CPU_SETSIZE; i++) {
953 struct percpu_buffer_node *node;
955 if (!CPU_ISSET(i, &allowed_cpus))
958 while ((node = __percpu_buffer_pop(&buffer, i))) {
962 free(buffer.c[i].array);
966 * All entries should now be accounted for (unless some external
967 * actor is interfering with our allowed affinity while this
970 assert(sum == expected_sum);
973 bool this_cpu_memcpy_buffer_push(struct percpu_memcpy_buffer *buffer,
974 struct percpu_memcpy_buffer_node item,
981 intptr_t *targetptr_final, newval_final, offset;
982 char *destptr, *srcptr;
986 cpu = get_current_cpu_id();
987 /* Load offset with single-copy atomicity. */
988 offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
989 if (offset == buffer->c[cpu].buflen)
991 destptr = (char *)&buffer->c[cpu].array[offset];
992 srcptr = (char *)&item;
993 /* copylen must be <= 4kB. */
994 copylen = sizeof(item);
995 newval_final = offset + 1;
996 targetptr_final = &buffer->c[cpu].offset;
997 ret = rseq_cmpeqv_trymemcpy_storev(
999 targetptr_final, offset,
1000 destptr, srcptr, copylen,
1002 if (rseq_likely(!ret)) {
1006 /* Retry if comparison fails or rseq aborts. */
1013 bool this_cpu_memcpy_buffer_pop(struct percpu_memcpy_buffer *buffer,
1014 struct percpu_memcpy_buffer_node *item,
1017 bool result = false;
1021 intptr_t *targetptr_final, newval_final, offset;
1022 char *destptr, *srcptr;
1026 cpu = get_current_cpu_id();
1027 /* Load offset with single-copy atomicity. */
1028 offset = RSEQ_READ_ONCE(buffer->c[cpu].offset);
1031 destptr = (char *)item;
1032 srcptr = (char *)&buffer->c[cpu].array[offset - 1];
1033 /* copylen must be <= 4kB. */
1034 copylen = sizeof(*item);
1035 newval_final = offset - 1;
1036 targetptr_final = &buffer->c[cpu].offset;
1037 ret = rseq_cmpeqv_trymemcpy_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
1038 targetptr_final, offset, destptr, srcptr, copylen,
1040 if (rseq_likely(!ret)) {
1044 /* Retry if comparison fails or rseq aborts. */
1052 * __percpu_memcpy_buffer_pop is not safe against concurrent accesses. Should
1053 * only be used on buffers that are not concurrently modified.
1055 bool __percpu_memcpy_buffer_pop(struct percpu_memcpy_buffer *buffer,
1056 struct percpu_memcpy_buffer_node *item,
1061 offset = buffer->c[cpu].offset;
1064 memcpy(item, &buffer->c[cpu].array[offset - 1], sizeof(*item));
1065 buffer->c[cpu].offset = offset - 1;
1069 void *test_percpu_memcpy_buffer_thread(void *arg)
1072 struct percpu_memcpy_buffer *buffer = (struct percpu_memcpy_buffer *)arg;
1074 if (!opt_disable_rseq && rseq_register_current_thread())
1078 for (i = 0; i < reps; i++) {
1079 struct percpu_memcpy_buffer_node item;
1082 result = this_cpu_memcpy_buffer_pop(buffer, &item, NULL);
1084 sched_yield(); /* encourage shuffling */
1086 if (!this_cpu_memcpy_buffer_push(buffer, item, NULL)) {
1087 /* Should increase buffer size. */
1093 printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
1094 (int) rseq_gettid(), nr_abort, signals_delivered);
1095 if (!opt_disable_rseq && rseq_unregister_current_thread())
1101 /* Simultaneous modification to a per-cpu buffer from many threads. */
1102 void test_percpu_memcpy_buffer(void)
1104 const int num_threads = opt_threads;
1106 uint64_t sum = 0, expected_sum = 0;
1107 struct percpu_memcpy_buffer buffer;
1108 pthread_t test_threads[num_threads];
1109 cpu_set_t allowed_cpus;
1111 memset(&buffer, 0, sizeof(buffer));
1113 /* Generate list entries for every usable cpu. */
1114 sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
1115 for (i = 0; i < CPU_SETSIZE; i++) {
1116 if (!CPU_ISSET(i, &allowed_cpus))
1118 /* Worse-case is every item in same CPU. */
1120 malloc(sizeof(*buffer.c[i].array) * CPU_SETSIZE *
1121 MEMCPY_BUFFER_ITEM_PER_CPU);
1122 assert(buffer.c[i].array);
1123 buffer.c[i].buflen = CPU_SETSIZE * MEMCPY_BUFFER_ITEM_PER_CPU;
1124 for (j = 1; j <= MEMCPY_BUFFER_ITEM_PER_CPU; j++) {
1125 expected_sum += 2 * j + 1;
1128 * We could theoretically put the word-sized
1129 * "data" directly in the buffer. However, we
1130 * want to model objects that would not fit
1131 * within a single word, so allocate an object
1134 buffer.c[i].array[j - 1].data1 = j;
1135 buffer.c[i].array[j - 1].data2 = j + 1;
1136 buffer.c[i].offset++;
1140 for (i = 0; i < num_threads; i++) {
1141 ret = pthread_create(&test_threads[i], NULL,
1142 test_percpu_memcpy_buffer_thread,
1146 perror("pthread_create");
1151 for (i = 0; i < num_threads; i++) {
1152 ret = pthread_join(test_threads[i], NULL);
1155 perror("pthread_join");
1160 for (i = 0; i < CPU_SETSIZE; i++) {
1161 struct percpu_memcpy_buffer_node item;
1163 if (!CPU_ISSET(i, &allowed_cpus))
1166 while (__percpu_memcpy_buffer_pop(&buffer, &item, i)) {
1170 free(buffer.c[i].array);
1174 * All entries should now be accounted for (unless some external
1175 * actor is interfering with our allowed affinity while this
1178 assert(sum == expected_sum);
1181 static void test_signal_interrupt_handler(int signo)
1183 signals_delivered++;
1186 static int set_signal_handler(void)
1189 struct sigaction sa;
1192 ret = sigemptyset(&sigset);
1194 perror("sigemptyset");
1198 sa.sa_handler = test_signal_interrupt_handler;
1199 sa.sa_mask = sigset;
1201 ret = sigaction(SIGUSR1, &sa, NULL);
1203 perror("sigaction");
1207 printf_verbose("Signal handler set for SIGUSR1\n");
1212 /* Test MEMBARRIER_CMD_PRIVATE_RESTART_RSEQ_ON_CPU membarrier command. */
1213 #ifdef TEST_MEMBARRIER
1214 struct test_membarrier_thread_args {
1216 intptr_t percpu_list_ptr;
1219 /* Worker threads modify data in their "active" percpu lists. */
1220 void *test_membarrier_worker_thread(void *arg)
1222 struct test_membarrier_thread_args *args =
1223 (struct test_membarrier_thread_args *)arg;
1224 const int iters = opt_reps;
1227 if (rseq_register_current_thread()) {
1228 fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
1229 errno, strerror(errno));
1233 /* Wait for initialization. */
1234 while (!atomic_load(&args->percpu_list_ptr)) {}
1236 for (i = 0; i < iters; ++i) {
1240 int cpu = get_current_cpu_id();
1242 ret = rseq_offset_deref_addv(RSEQ_MO_RELAXED, RSEQ_PERCPU,
1243 &args->percpu_list_ptr,
1244 sizeof(struct percpu_list_entry) * cpu, 1, cpu);
1245 } while (rseq_unlikely(ret));
1248 if (rseq_unregister_current_thread()) {
1249 fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
1250 errno, strerror(errno));
1256 void test_membarrier_init_percpu_list(struct percpu_list *list)
1260 memset(list, 0, sizeof(*list));
1261 for (i = 0; i < CPU_SETSIZE; i++) {
1262 struct percpu_list_node *node;
1264 node = malloc(sizeof(*node));
1268 list->c[i].head = node;
1272 void test_membarrier_free_percpu_list(struct percpu_list *list)
1276 for (i = 0; i < CPU_SETSIZE; i++)
1277 free(list->c[i].head);
1281 * The manager thread swaps per-cpu lists that worker threads see,
1282 * and validates that there are no unexpected modifications.
1284 void *test_membarrier_manager_thread(void *arg)
1286 struct test_membarrier_thread_args *args =
1287 (struct test_membarrier_thread_args *)arg;
1288 struct percpu_list list_a, list_b;
1289 intptr_t expect_a = 0, expect_b = 0;
1290 int cpu_a = 0, cpu_b = 0;
1292 if (rseq_register_current_thread()) {
1293 fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
1294 errno, strerror(errno));
1299 test_membarrier_init_percpu_list(&list_a);
1300 test_membarrier_init_percpu_list(&list_b);
1302 atomic_store(&args->percpu_list_ptr, (intptr_t)&list_a);
1304 while (!atomic_load(&args->stop)) {
1305 /* list_a is "active". */
1306 cpu_a = rand() % CPU_SETSIZE;
1308 * As list_b is "inactive", we should never see changes
1311 if (expect_b != atomic_load(&list_b.c[cpu_b].head->data)) {
1312 fprintf(stderr, "Membarrier test failed\n");
1316 /* Make list_b "active". */
1317 atomic_store(&args->percpu_list_ptr, (intptr_t)&list_b);
1318 if (rseq_membarrier_expedited(cpu_a) &&
1319 errno != ENXIO /* missing CPU */) {
1320 perror("sys_membarrier");
1324 * Cpu A should now only modify list_b, so the values
1325 * in list_a should be stable.
1327 expect_a = atomic_load(&list_a.c[cpu_a].head->data);
1329 cpu_b = rand() % CPU_SETSIZE;
1331 * As list_a is "inactive", we should never see changes
1334 if (expect_a != atomic_load(&list_a.c[cpu_a].head->data)) {
1335 fprintf(stderr, "Membarrier test failed\n");
1339 /* Make list_a "active". */
1340 atomic_store(&args->percpu_list_ptr, (intptr_t)&list_a);
1341 if (rseq_membarrier_expedited(cpu_b) &&
1342 errno != ENXIO /* missing CPU*/) {
1343 perror("sys_membarrier");
1346 /* Remember a value from list_b. */
1347 expect_b = atomic_load(&list_b.c[cpu_b].head->data);
1350 test_membarrier_free_percpu_list(&list_a);
1351 test_membarrier_free_percpu_list(&list_b);
1353 if (rseq_unregister_current_thread()) {
1354 fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
1355 errno, strerror(errno));
1361 void test_membarrier(void)
1363 const int num_threads = opt_threads;
1364 struct test_membarrier_thread_args thread_args;
1365 pthread_t worker_threads[num_threads];
1366 pthread_t manager_thread;
1369 if (sys_membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ, 0, 0)) {
1370 perror("sys_membarrier");
1374 thread_args.stop = 0;
1375 thread_args.percpu_list_ptr = 0;
1376 ret = pthread_create(&manager_thread, NULL,
1377 test_membarrier_manager_thread, &thread_args);
1380 perror("pthread_create");
1384 for (i = 0; i < num_threads; i++) {
1385 ret = pthread_create(&worker_threads[i], NULL,
1386 test_membarrier_worker_thread, &thread_args);
1389 perror("pthread_create");
1395 for (i = 0; i < num_threads; i++) {
1396 ret = pthread_join(worker_threads[i], NULL);
1399 perror("pthread_join");
1404 atomic_store(&thread_args.stop, 1);
1405 ret = pthread_join(manager_thread, NULL);
1408 perror("pthread_join");
1412 #else /* TEST_MEMBARRIER */
1413 void test_membarrier(void)
1415 fprintf(stderr, "rseq_offset_deref_addv is not implemented on this architecture. "
1416 "Skipping membarrier test.\n");
1420 static void show_usage(int argc, char **argv)
1422 printf("Usage : %s <OPTIONS>\n",
1424 printf("OPTIONS:\n");
1425 printf(" [-1 loops] Number of loops for delay injection 1\n");
1426 printf(" [-2 loops] Number of loops for delay injection 2\n");
1427 printf(" [-3 loops] Number of loops for delay injection 3\n");
1428 printf(" [-4 loops] Number of loops for delay injection 4\n");
1429 printf(" [-5 loops] Number of loops for delay injection 5\n");
1430 printf(" [-6 loops] Number of loops for delay injection 6\n");
1431 printf(" [-7 loops] Number of loops for delay injection 7 (-1 to enable -m)\n");
1432 printf(" [-8 loops] Number of loops for delay injection 8 (-1 to enable -m)\n");
1433 printf(" [-9 loops] Number of loops for delay injection 9 (-1 to enable -m)\n");
1434 printf(" [-m N] Yield/sleep/kill every modulo N (default 0: disabled) (>= 0)\n");
1435 printf(" [-y] Yield\n");
1436 printf(" [-k] Kill thread with signal\n");
1437 printf(" [-s S] S: =0: disabled (default), >0: sleep time (ms)\n");
1438 printf(" [-t N] Number of threads (default 200)\n");
1439 printf(" [-r N] Number of repetitions per thread (default 5000)\n");
1440 printf(" [-d] Disable rseq system call (no initialization)\n");
1441 printf(" [-D M] Disable rseq for each M threads\n");
1442 printf(" [-T test] Choose test: (s)pinlock, (l)ist, (b)uffer, (m)emcpy, (i)ncrement, membarrie(r)\n");
1443 printf(" [-M] Push into buffer and memcpy buffer with memory barriers.\n");
1444 printf(" [-v] Verbose output.\n");
1445 printf(" [-h] Show this help.\n");
1449 int main(int argc, char **argv)
1453 for (i = 1; i < argc; i++) {
1454 if (argv[i][0] != '-')
1456 switch (argv[i][1]) {
1467 show_usage(argc, argv);
1470 loop_cnt[argv[i][1] - '0'] = atol(argv[i + 1]);
1475 show_usage(argc, argv);
1478 opt_modulo = atol(argv[i + 1]);
1479 if (opt_modulo < 0) {
1480 show_usage(argc, argv);
1487 show_usage(argc, argv);
1490 opt_sleep = atol(argv[i + 1]);
1491 if (opt_sleep < 0) {
1492 show_usage(argc, argv);
1504 opt_disable_rseq = 1;
1508 show_usage(argc, argv);
1511 opt_disable_mod = atol(argv[i + 1]);
1512 if (opt_disable_mod < 0) {
1513 show_usage(argc, argv);
1520 show_usage(argc, argv);
1523 opt_threads = atol(argv[i + 1]);
1524 if (opt_threads < 0) {
1525 show_usage(argc, argv);
1532 show_usage(argc, argv);
1535 opt_reps = atoll(argv[i + 1]);
1537 show_usage(argc, argv);
1543 show_usage(argc, argv);
1547 show_usage(argc, argv);
1550 opt_test = *argv[i + 1];
1560 show_usage(argc, argv);
1569 opt_mo = RSEQ_MO_RELEASE;
1572 show_usage(argc, argv);
1577 loop_cnt_1 = loop_cnt[1];
1578 loop_cnt_2 = loop_cnt[2];
1579 loop_cnt_3 = loop_cnt[3];
1580 loop_cnt_4 = loop_cnt[4];
1581 loop_cnt_5 = loop_cnt[5];
1582 loop_cnt_6 = loop_cnt[6];
1584 if (set_signal_handler())
1587 if (!opt_disable_rseq && rseq_register_current_thread())
1589 if (!opt_disable_rseq && !rseq_validate_cpu_id()) {
1590 fprintf(stderr, "Error: cpu id getter unavailable\n");
1595 printf_verbose("spinlock\n");
1596 test_percpu_spinlock();
1599 printf_verbose("linked list\n");
1603 printf_verbose("buffer\n");
1604 test_percpu_buffer();
1607 printf_verbose("memcpy buffer\n");
1608 test_percpu_memcpy_buffer();
1611 printf_verbose("counter increment\n");
1615 printf_verbose("membarrier\n");
1619 if (!opt_disable_rseq && rseq_unregister_current_thread())