2 * iteration_check.c: test races having to do with xarray iteration
3 * Copyright (c) 2016 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 #define NEW_TAG XA_MARK_1
23 static pthread_t threads[NUM_THREADS];
24 static unsigned int seeds[3];
25 static DEFINE_XARRAY(array);
26 static bool test_complete;
29 void my_item_insert(struct xarray *xa, unsigned long index)
31 XA_STATE(xas, xa, index);
32 struct item *item = item_create(index, 0);
37 for (order = max_order; order >= 0; order--) {
38 xas_set_order(&xas, index, order);
40 if (xas_find_conflict(&xas))
42 xas_store(&xas, item);
43 xas_set_mark(&xas, TAG);
47 if (xas_nomem(&xas, GFP_KERNEL))
53 /* relentlessly fill the array with tagged entries */
54 static void *add_entries_fn(void *arg)
56 rcu_register_thread();
58 while (!test_complete) {
61 for (pgoff = 0; pgoff < MAX_IDX; pgoff++) {
62 my_item_insert(&array, pgoff);
66 rcu_unregister_thread();
72 * Iterate over tagged entries, retrying when we find ourselves in a deleted
73 * node and randomly pausing the iteration.
75 static void *tagged_iteration_fn(void *arg)
77 XA_STATE(xas, &array, 0);
80 rcu_register_thread();
82 while (!test_complete) {
85 xas_for_each_marked(&xas, entry, ULONG_MAX, TAG) {
86 if (xas_retry(&xas, entry))
89 if (rand_r(&seeds[0]) % 50 == 0) {
99 rcu_unregister_thread();
105 * Iterate over the entries, retrying when we find ourselves in a deleted
106 * node and randomly pausing the iteration.
108 static void *untagged_iteration_fn(void *arg)
110 XA_STATE(xas, &array, 0);
113 rcu_register_thread();
115 while (!test_complete) {
118 xas_for_each(&xas, entry, ULONG_MAX) {
119 if (xas_retry(&xas, entry))
122 if (rand_r(&seeds[1]) % 50 == 0) {
132 rcu_unregister_thread();
138 * Randomly remove entries to help induce retries in the
139 * two iteration functions.
141 static void *remove_entries_fn(void *arg)
143 rcu_register_thread();
145 while (!test_complete) {
149 pgoff = rand_r(&seeds[2]) % MAX_IDX;
151 item = xa_erase(&array, pgoff);
153 item_free(item, pgoff);
156 rcu_unregister_thread();
161 static void *tag_entries_fn(void *arg)
163 rcu_register_thread();
165 while (!test_complete) {
166 tag_tagged_items(&array, 0, MAX_IDX, 10, TAG, NEW_TAG);
168 rcu_unregister_thread();
172 /* This is a unit test for a bug found by the syzkaller tester */
173 void iteration_test(unsigned order, unsigned test_duration)
177 printv(1, "Running %siteration tests for %d seconds\n",
178 order > 0 ? "multiorder " : "", test_duration);
181 test_complete = false;
183 for (i = 0; i < 3; i++)
186 if (pthread_create(&threads[0], NULL, tagged_iteration_fn, NULL)) {
187 perror("create tagged iteration thread");
190 if (pthread_create(&threads[1], NULL, untagged_iteration_fn, NULL)) {
191 perror("create untagged iteration thread");
194 if (pthread_create(&threads[2], NULL, add_entries_fn, NULL)) {
195 perror("create add entry thread");
198 if (pthread_create(&threads[3], NULL, remove_entries_fn, NULL)) {
199 perror("create remove entry thread");
202 if (pthread_create(&threads[4], NULL, tag_entries_fn, NULL)) {
203 perror("create tag entry thread");
207 sleep(test_duration);
208 test_complete = true;
210 for (i = 0; i < NUM_THREADS; i++) {
211 if (pthread_join(threads[i], NULL)) {
212 perror("pthread_join");
217 item_kill_tree(&array);