WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / radix-tree / iteration_check.c
blobe9908bcb06dd403ce2551e9a6a2fc6c8282bbf80
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * iteration_check.c: test races having to do with xarray iteration
4 * Copyright (c) 2016 Intel Corporation
5 * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
6 */
7 #include <pthread.h>
8 #include "test.h"
10 #define NUM_THREADS 5
11 #define MAX_IDX 100
12 #define TAG XA_MARK_0
13 #define NEW_TAG XA_MARK_1
15 static pthread_t threads[NUM_THREADS];
16 static unsigned int seeds[3];
17 static DEFINE_XARRAY(array);
18 static bool test_complete;
19 static int max_order;
21 void my_item_insert(struct xarray *xa, unsigned long index)
23 XA_STATE(xas, xa, index);
24 struct item *item = item_create(index, 0);
25 int order;
27 retry:
28 xas_lock(&xas);
29 for (order = max_order; order >= 0; order--) {
30 xas_set_order(&xas, index, order);
31 item->order = order;
32 if (xas_find_conflict(&xas))
33 continue;
34 xas_store(&xas, item);
35 xas_set_mark(&xas, TAG);
36 break;
38 xas_unlock(&xas);
39 if (xas_nomem(&xas, GFP_KERNEL))
40 goto retry;
41 if (order < 0)
42 free(item);
45 /* relentlessly fill the array with tagged entries */
46 static void *add_entries_fn(void *arg)
48 rcu_register_thread();
50 while (!test_complete) {
51 unsigned long pgoff;
53 for (pgoff = 0; pgoff < MAX_IDX; pgoff++) {
54 my_item_insert(&array, pgoff);
58 rcu_unregister_thread();
60 return NULL;
64 * Iterate over tagged entries, retrying when we find ourselves in a deleted
65 * node and randomly pausing the iteration.
67 static void *tagged_iteration_fn(void *arg)
69 XA_STATE(xas, &array, 0);
70 void *entry;
72 rcu_register_thread();
74 while (!test_complete) {
75 xas_set(&xas, 0);
76 rcu_read_lock();
77 xas_for_each_marked(&xas, entry, ULONG_MAX, TAG) {
78 if (xas_retry(&xas, entry))
79 continue;
81 if (rand_r(&seeds[0]) % 50 == 0) {
82 xas_pause(&xas);
83 rcu_read_unlock();
84 rcu_barrier();
85 rcu_read_lock();
88 rcu_read_unlock();
91 rcu_unregister_thread();
93 return NULL;
97 * Iterate over the entries, retrying when we find ourselves in a deleted
98 * node and randomly pausing the iteration.
100 static void *untagged_iteration_fn(void *arg)
102 XA_STATE(xas, &array, 0);
103 void *entry;
105 rcu_register_thread();
107 while (!test_complete) {
108 xas_set(&xas, 0);
109 rcu_read_lock();
110 xas_for_each(&xas, entry, ULONG_MAX) {
111 if (xas_retry(&xas, entry))
112 continue;
114 if (rand_r(&seeds[1]) % 50 == 0) {
115 xas_pause(&xas);
116 rcu_read_unlock();
117 rcu_barrier();
118 rcu_read_lock();
121 rcu_read_unlock();
124 rcu_unregister_thread();
126 return NULL;
130 * Randomly remove entries to help induce retries in the
131 * two iteration functions.
133 static void *remove_entries_fn(void *arg)
135 rcu_register_thread();
137 while (!test_complete) {
138 int pgoff;
139 struct item *item;
141 pgoff = rand_r(&seeds[2]) % MAX_IDX;
143 item = xa_erase(&array, pgoff);
144 if (item)
145 item_free(item, pgoff);
148 rcu_unregister_thread();
150 return NULL;
153 static void *tag_entries_fn(void *arg)
155 rcu_register_thread();
157 while (!test_complete) {
158 tag_tagged_items(&array, 0, MAX_IDX, 10, TAG, NEW_TAG);
160 rcu_unregister_thread();
161 return NULL;
164 /* This is a unit test for a bug found by the syzkaller tester */
165 void iteration_test(unsigned order, unsigned test_duration)
167 int i;
169 printv(1, "Running %siteration tests for %d seconds\n",
170 order > 0 ? "multiorder " : "", test_duration);
172 max_order = order;
173 test_complete = false;
175 for (i = 0; i < 3; i++)
176 seeds[i] = rand();
178 if (pthread_create(&threads[0], NULL, tagged_iteration_fn, NULL)) {
179 perror("create tagged iteration thread");
180 exit(1);
182 if (pthread_create(&threads[1], NULL, untagged_iteration_fn, NULL)) {
183 perror("create untagged iteration thread");
184 exit(1);
186 if (pthread_create(&threads[2], NULL, add_entries_fn, NULL)) {
187 perror("create add entry thread");
188 exit(1);
190 if (pthread_create(&threads[3], NULL, remove_entries_fn, NULL)) {
191 perror("create remove entry thread");
192 exit(1);
194 if (pthread_create(&threads[4], NULL, tag_entries_fn, NULL)) {
195 perror("create tag entry thread");
196 exit(1);
199 sleep(test_duration);
200 test_complete = true;
202 for (i = 0; i < NUM_THREADS; i++) {
203 if (pthread_join(threads[i], NULL)) {
204 perror("pthread_join");
205 exit(1);
209 item_kill_tree(&array);