Introduce common generic header file
[librseq.git] / tests / basic_percpu_ops_test.c
index e514ce6a6cae36280242013a6a8cbba5d18e0afb..ff260091f52e4c170d59eb3a0e15d0c193ecf265 100644 (file)
@@ -1,4 +1,5 @@
-// SPDX-License-Identifier: LGPL-2.1-only
+// SPDX-License-Identifier: MIT
+// SPDX-FileCopyrightText: 2018-2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
@@ -6,6 +7,7 @@
 #include <pthread.h>
 #include <sched.h>
 #include <stdint.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #define ARRAY_SIZE(arr)        (sizeof(arr) / sizeof((arr)[0]))
 
+#ifdef BUILDOPT_RSEQ_PERCPU_MM_CID
+# define RSEQ_PERCPU   RSEQ_PERCPU_MM_CID
+static
+int get_current_cpu_id(void)
+{
+       return rseq_current_mm_cid();
+}
+static
+bool rseq_validate_cpu_id(void)
+{
+       return rseq_mm_cid_available();
+}
+static
+bool rseq_use_cpu_index(void)
+{
+       return false;   /* Use mm_cid */
+}
+#else
+# define RSEQ_PERCPU   RSEQ_PERCPU_CPU_ID
+static
+int get_current_cpu_id(void)
+{
+       return rseq_cpu_start();
+}
+static
+bool rseq_validate_cpu_id(void)
+{
+       return rseq_current_cpu_raw() >= 0;
+}
+static
+bool rseq_use_cpu_index(void)
+{
+       return true;    /* Use cpu_id as index. */
+}
+#endif
+
 struct percpu_lock_entry {
        intptr_t v;
 } __attribute__((aligned(128)));
@@ -51,16 +89,16 @@ struct percpu_list {
 };
 
 /* A simple percpu spinlock.  Returns the cpu lock was acquired on. */
-int rseq_this_cpu_lock(struct percpu_lock *lock)
+static int rseq_this_cpu_lock(struct percpu_lock *lock)
 {
        int cpu;
 
        for (;;) {
                int ret;
 
-               cpu = rseq_cpu_start();
-               ret = rseq_cmpeqv_storev(&lock->c[cpu].v,
-                                        0, 1, cpu);
+               cpu = get_current_cpu_id();
+               ret = rseq_load_cbne_store__ptr(RSEQ_MO_RELAXED, RSEQ_PERCPU,
+                                        &lock->c[cpu].v, 0, 1, cpu);
                if (rseq_likely(!ret))
                        break;
                /* Retry if comparison fails or rseq aborts. */
@@ -73,7 +111,7 @@ int rseq_this_cpu_lock(struct percpu_lock *lock)
        return cpu;
 }
 
-void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
+static void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
 {
        assert(lock->c[cpu].v == 1);
        /*
@@ -83,9 +121,9 @@ void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
        rseq_smp_store_release(&lock->c[cpu].v, 0);
 }
 
-void *test_percpu_spinlock_thread(void *arg)
+static void *test_percpu_spinlock_thread(void *arg)
 {
-       struct spinlock_test_data *data = arg;
+       struct spinlock_test_data *data = (struct spinlock_test_data *) arg;
        int i, cpu;
 
        if (rseq_register_current_thread()) {
@@ -113,11 +151,11 @@ void *test_percpu_spinlock_thread(void *arg)
  * per-cpu increment; however, this is reasonable for a test and the
  * lock can be extended to synchronize more complicated operations.
  */
-void test_percpu_spinlock(void)
+static void test_percpu_spinlock(void)
 {
        const int num_threads = 200;
        int i;
-       uint64_t sum;
+       uint64_t sum, expected_sum;
        pthread_t test_threads[num_threads];
        struct spinlock_test_data data;
 
@@ -137,10 +175,12 @@ void test_percpu_spinlock(void)
        for (i = 0; i < CPU_SETSIZE; i++)
                sum += data.c[i].count;
 
-       ok(sum == (uint64_t)data.reps * num_threads, "sum");
+       expected_sum = (uint64_t)data.reps * num_threads;
+
+       ok(sum == expected_sum, "spinlock - sum (%" PRIu64 " == %" PRIu64 ")", sum, expected_sum);
 }
 
-void this_cpu_list_push(struct percpu_list *list,
+static void this_cpu_list_push(struct percpu_list *list,
                        struct percpu_list_node *node,
                        int *_cpu)
 {
@@ -150,13 +190,14 @@ void this_cpu_list_push(struct percpu_list *list,
                intptr_t *targetptr, newval, expect;
                int ret;
 
-               cpu = rseq_cpu_start();
+               cpu = get_current_cpu_id();
                /* Load list->c[cpu].head with single-copy atomicity. */
                expect = (intptr_t)RSEQ_READ_ONCE(list->c[cpu].head);
                newval = (intptr_t)node;
                targetptr = (intptr_t *)&list->c[cpu].head;
                node->next = (struct percpu_list_node *)expect;
-               ret = rseq_cmpeqv_storev(targetptr, expect, newval, cpu);
+               ret = rseq_load_cbne_store__ptr(RSEQ_MO_RELAXED, RSEQ_PERCPU,
+                                        targetptr, expect, newval, cpu);
                if (rseq_likely(!ret))
                        break;
                /* Retry if comparison fails or rseq aborts. */
@@ -170,21 +211,22 @@ void this_cpu_list_push(struct percpu_list *list,
  * rseq primitive allows us to implement pop without concerns over
  * ABA-type races.
  */
-struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list,
+static struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list,
                                           int *_cpu)
 {
        for (;;) {
                struct percpu_list_node *head;
                intptr_t *targetptr, expectnot, *load;
-               off_t offset;
+               long offset;
                int ret, cpu;
 
-               cpu = rseq_cpu_start();
+               cpu = get_current_cpu_id();
                targetptr = (intptr_t *)&list->c[cpu].head;
                expectnot = (intptr_t)NULL;
                offset = offsetof(struct percpu_list_node, next);
                load = (intptr_t *)&head;
-               ret = rseq_cmpnev_storeoffp_load(targetptr, expectnot,
+               ret = rseq_load_cbeq_store_add_load_store__ptr(RSEQ_MO_RELAXED, RSEQ_PERCPU,
+                                                targetptr, expectnot,
                                                 offset, load, cpu);
                if (rseq_likely(!ret)) {
                        if (_cpu)
@@ -201,7 +243,7 @@ struct percpu_list_node *this_cpu_list_pop(struct percpu_list *list,
  * __percpu_list_pop is not safe against concurrent accesses. Should
  * only be used on lists that are not concurrently modified.
  */
-struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
+static struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
 {
        struct percpu_list_node *node;
 
@@ -212,7 +254,7 @@ struct percpu_list_node *__percpu_list_pop(struct percpu_list *list, int cpu)
        return node;
 }
 
-void *test_percpu_list_thread(void *arg)
+static void *test_percpu_list_thread(void *arg)
 {
        int i;
        struct percpu_list *list = (struct percpu_list *)arg;
@@ -242,7 +284,7 @@ void *test_percpu_list_thread(void *arg)
 }
 
 /* Simultaneous modification to a per-cpu linked list from many threads.  */
-void test_percpu_list(void)
+static void test_percpu_list(void)
 {
        int i, j;
        uint64_t sum = 0, expected_sum = 0;
@@ -257,14 +299,14 @@ void test_percpu_list(void)
        /* Generate list entries for every usable cpu. */
        sched_getaffinity(0, sizeof(allowed_cpus), &allowed_cpus);
        for (i = 0; i < CPU_SETSIZE; i++) {
-               if (!CPU_ISSET(i, &allowed_cpus))
+               if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
                        continue;
                for (j = 1; j <= 100; j++) {
                        struct percpu_list_node *node;
 
                        expected_sum += j;
 
-                       node = malloc(sizeof(*node));
+                       node = (struct percpu_list_node *) malloc(sizeof(*node));
                        assert(node);
                        node->data = j;
                        node->next = list.c[i].head;
@@ -282,7 +324,7 @@ void test_percpu_list(void)
        for (i = 0; i < CPU_SETSIZE; i++) {
                struct percpu_list_node *node;
 
-               if (!CPU_ISSET(i, &allowed_cpus))
+               if (rseq_use_cpu_index() && !CPU_ISSET(i, &allowed_cpus))
                        continue;
 
                while ((node = __percpu_list_pop(&list, i))) {
@@ -296,14 +338,14 @@ void test_percpu_list(void)
         * actor is interfering with our allowed affinity while this
         * test is running).
         */
-       ok(sum == expected_sum, "sum");
+       ok(sum == expected_sum, "percpu_list - sum (%" PRIu64 " == %" PRIu64 ")", sum, expected_sum);
 }
 
 int main(void)
 {
        plan_tests(NR_TESTS);
 
-       if (!rseq_available()) {
+       if (!rseq_available(RSEQ_AVAILABLE_QUERY_KERNEL)) {
                skip(NR_TESTS, "The rseq syscall is unavailable");
                goto end;
        }
@@ -315,7 +357,10 @@ int main(void)
        } else {
                pass("Registered current thread with rseq");
        }
-
+       if (!rseq_validate_cpu_id()) {
+               skip(NR_TESTS - 1, "Error: cpu id getter unavailable");
+               goto end;
+       }
        test_percpu_spinlock();
        test_percpu_list();
 
@@ -326,7 +371,6 @@ int main(void)
        } else {
                pass("Unregistered current thread with rseq");
        }
-
 end:
        exit(exit_status());
 }
This page took 0.026083 seconds and 4 git commands to generate.