1 // SPDX-License-Identifier: LGPL-2.1
12 #include "../kselftest.h"
15 #ifdef BUILDOPT_RSEQ_PERCPU_MM_CID
16 # define RSEQ_PERCPU RSEQ_PERCPU_MM_CID
18 int get_current_cpu_id(void)
20 return rseq_current_mm_cid();
23 bool rseq_validate_cpu_id(void)
25 return rseq_mm_cid_available();
28 # define RSEQ_PERCPU RSEQ_PERCPU_CPU_ID
30 int get_current_cpu_id(void)
32 return rseq_cpu_start();
35 bool rseq_validate_cpu_id(void)
37 return rseq_current_cpu_raw() >= 0;
41 struct percpu_lock_entry {
43 } __attribute__((aligned(128)));
46 struct percpu_lock_entry c[CPU_SETSIZE];
49 struct test_data_entry {
51 } __attribute__((aligned(128)));
53 struct spinlock_test_data {
54 struct percpu_lock lock;
55 struct test_data_entry c[CPU_SETSIZE];
59 struct percpu_list_node {
61 struct percpu_list_node *next;
64 struct percpu_list_entry {
65 struct percpu_list_node *head;
66 } __attribute__((aligned(128)));
69 struct percpu_list_entry c[CPU_SETSIZE];
72 /* A simple percpu spinlock. Returns the cpu lock was acquired on. */
73 int rseq_this_cpu_lock(struct percpu_lock *lock)
80 cpu = get_current_cpu_id();
81 ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
82 &lock->c[cpu].v, 0, 1, cpu);
83 if (rseq_likely(!ret))
85 /* Retry if comparison fails or rseq aborts. */
88 * Acquire semantic when taking lock after control dependency.
89 * Matches rseq_smp_store_release().
91 rseq_smp_acquire__after_ctrl_dep();
95 void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
97 assert(lock->c[cpu].v == 1);
99 * Release lock, with release semantic. Matches
100 * rseq_smp_acquire__after_ctrl_dep().
102 rseq_smp_store_release(&lock->c[cpu].v, 0);
105 void *test_percpu_spinlock_thread(void *arg)
107 struct spinlock_test_data *data = arg;
110 if (rseq_register_current_thread()) {
111 fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
112 errno, strerror(errno));
115 for (i = 0; i < data->reps; i++) {
116 cpu = rseq_this_cpu_lock(&data->lock);
117 data->c[cpu].count++;
118 rseq_percpu_unlock(&data->lock, cpu);
120 if (rseq_unregister_current_thread()) {
121 fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
122 errno, strerror(errno));
130 * A simple test which implements a sharded counter using a per-cpu
131 * lock. Obviously real applications might prefer to simply use a
132 * per-cpu increment; however, this is reasonable for a test and the
133 * lock can be extended to synchronize more complicated operations.
135 void test_percpu_spinlock(void)
137 const int num_threads = 200;
140 pthread_t test_threads[num_threads];
141 struct spinlock_test_data data;
143 memset(&data, 0, sizeof(data));
146 for (i = 0; i < num_threads; i++)
147 pthread_create(&test_threads[i], NULL,
148 test_percpu_spinlock_thread, &data);
150 for (i = 0; i < num_threads; i++)
151 pthread_join(test_threads[i], NULL);
154 for (i = 0; i < CPU_SETSIZE; i++)
155 sum += data.c[i].count;
157 assert(sum == (uint64_t)data.reps * num_threads);
160 void this_cpu_list_push(struct percpu_list *list,
161 struct percpu_list_node *node,
167 intptr_t *targetptr, newval, expect;
170 cpu = get_current_cpu_id();
171 /* Load list->c[cpu].head with single-copy atomicity. */
172 expect = (intptr_t)RSEQ_READ_ONCE(list->c[cpu].head);
173 newval = (intptr_t)node;
174 targetptr = (intptr_t *)&list->c[cpu].head;
175 node->next = (struct percpu_list_node *)expect;
176 ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
177 targetptr, expect, newval, cpu);
178 if (rseq_likely(!ret))
180 /* Retry if comparison fails or rseq aborts. */
187 * Unlike a traditional lock-less linked list; the availability of a
188 * rseq primitive allows us to implement pop without concerns over
191 struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list,
195 struct percpu_list_node *head;
196 intptr_t *targetptr, expectnot, *load;
200 cpu = get_current_cpu_id();
201 targetptr = (intptr_t *)&list->c[cpu].head;
202 expectnot = (intptr_t)NULL;
203 offset = offsetof(struct percpu_list_node, next);
204 load = (intptr_t *)&head;
205 ret = rseq_cmpnev_storeoffp_load(RSEQ_MO_RELAXED, RSEQ_PERCPU,
206 targetptr, expectnot,
208 if (rseq_likely(!ret)) {
215 /* Retry if rseq aborts. */
220 * __percpu_list_pop is not safe against concurrent accesses. Should
221 * only be used on lists that are not concurrently modified.
223 struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
225 struct percpu_list_node *node;
227 node = list->c[cpu].head;
230 list->c[cpu].head = node->next;
234 void *test_percpu_list_thread(void *arg)
237 struct percpu_list *list = (struct percpu_list *)arg;
239 if (rseq_register_current_thread()) {
240 fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
241 errno, strerror(errno));
245 for (i = 0; i < 100000; i++) {
246 struct percpu_list_node *node;
248 node = this_cpu_list_pop(list, NULL);
249 sched_yield(); /* encourage shuffling */
251 this_cpu_list_push(list, node, NULL);
254 if (rseq_unregister_current_thread()) {
255 fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
256 errno, strerror(errno));
263 /* Simultaneous modification to a per-cpu linked list from many threads. */
264 void test_percpu_list(void)
267 uint64_t sum = 0, expected_sum = 0;
268 struct percpu_list list;
269 pthread_t test_threads[200];
270 cpu_set_t allowed_cpus;
272 memset(&list, 0, sizeof(list));
274 /* Generate list entries for every usable cpu. */
275 sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
276 for (i = 0; i < CPU_SETSIZE; i++) {
277 if (!CPU_ISSET(i, &allowed_cpus))
279 for (j = 1; j <= 100; j++) {
280 struct percpu_list_node *node;
284 node = malloc(sizeof(*node));
287 node->next = list.c[i].head;
288 list.c[i].head = node;
292 for (i = 0; i < 200; i++)
293 pthread_create(&test_threads[i], NULL,
294 test_percpu_list_thread, &list);
296 for (i = 0; i < 200; i++)
297 pthread_join(test_threads[i], NULL);
299 for (i = 0; i < CPU_SETSIZE; i++) {
300 struct percpu_list_node *node;
302 if (!CPU_ISSET(i, &allowed_cpus))
305 while ((node = __percpu_list_pop(&list, i))) {
312 * All entries should now be accounted for (unless some external
313 * actor is interfering with our allowed affinity while this
316 assert(sum == expected_sum);
319 int main(int argc, char **argv)
321 if (rseq_register_current_thread()) {
322 fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
323 errno, strerror(errno));
326 if (!rseq_validate_cpu_id()) {
327 fprintf(stderr, "Error: cpu id getter unavailable\n");
330 printf("spinlock\n");
331 test_percpu_spinlock();
332 printf("percpu_list\n");
334 if (rseq_unregister_current_thread()) {
335 fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
336 errno, strerror(errno));