^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/radix-tree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "regression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static pthread_barrier_t worker_barrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static int obj0, obj1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static RADIX_TREE(mt_tree, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static void *reader_fn(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) rcu_register_thread();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) pthread_barrier_wait(&worker_barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) for (i = 0; i < 1000000; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) entry = radix_tree_lookup(&mt_tree, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (entry != &obj0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) printf("iteration %d bad entry = %p\n", i, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) abort();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) rcu_unregister_thread();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void *writer_fn(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) rcu_register_thread();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pthread_barrier_wait(&worker_barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) for (i = 0; i < 1000000; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) radix_tree_insert(&mt_tree, 1, &obj1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) radix_tree_delete(&mt_tree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) rcu_unregister_thread();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void regression4_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) pthread_t reader, writer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) printv(1, "regression test 4 starting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) radix_tree_insert(&mt_tree, 0, &obj0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pthread_barrier_init(&worker_barrier, NULL, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (pthread_create(&reader, NULL, reader_fn, NULL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pthread_create(&writer, NULL, writer_fn, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) perror("pthread_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (pthread_join(reader, NULL) || pthread_join(writer, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) perror("pthread_join");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) printv(1, "regression test 4 passed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }