1 // SPDX-License-Identifier: GPL-2.0-only
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>
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
;
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);
29 for (order
= max_order
; order
>= 0; order
--) {
30 xas_set_order(&xas
, index
, order
);
32 if (xas_find_conflict(&xas
))
34 xas_store(&xas
, item
);
35 xas_set_mark(&xas
, TAG
);
39 if (xas_nomem(&xas
, GFP_KERNEL
))
45 /* relentlessly fill the array with tagged entries */
46 static void *add_entries_fn(void *arg
)
48 rcu_register_thread();
50 while (!test_complete
) {
53 for (pgoff
= 0; pgoff
< MAX_IDX
; pgoff
++) {
54 my_item_insert(&array
, pgoff
);
58 rcu_unregister_thread();
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);
72 rcu_register_thread();
74 while (!test_complete
) {
77 xas_for_each_marked(&xas
, entry
, ULONG_MAX
, TAG
) {
78 if (xas_retry(&xas
, entry
))
81 if (rand_r(&seeds
[0]) % 50 == 0) {
91 rcu_unregister_thread();
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);
105 rcu_register_thread();
107 while (!test_complete
) {
110 xas_for_each(&xas
, entry
, ULONG_MAX
) {
111 if (xas_retry(&xas
, entry
))
114 if (rand_r(&seeds
[1]) % 50 == 0) {
124 rcu_unregister_thread();
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
) {
141 pgoff
= rand_r(&seeds
[2]) % MAX_IDX
;
143 item
= xa_erase(&array
, pgoff
);
145 item_free(item
, pgoff
);
148 rcu_unregister_thread();
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();
164 /* This is a unit test for a bug found by the syzkaller tester */
165 void iteration_test(unsigned order
, unsigned test_duration
)
169 printv(1, "Running %siteration tests for %d seconds\n",
170 order
> 0 ? "multiorder " : "", test_duration
);
173 test_complete
= false;
175 for (i
= 0; i
< 3; i
++)
178 if (pthread_create(&threads
[0], NULL
, tagged_iteration_fn
, NULL
)) {
179 perror("create tagged iteration thread");
182 if (pthread_create(&threads
[1], NULL
, untagged_iteration_fn
, NULL
)) {
183 perror("create untagged iteration thread");
186 if (pthread_create(&threads
[2], NULL
, add_entries_fn
, NULL
)) {
187 perror("create add entry thread");
190 if (pthread_create(&threads
[3], NULL
, remove_entries_fn
, NULL
)) {
191 perror("create remove entry thread");
194 if (pthread_create(&threads
[4], NULL
, tag_entries_fn
, NULL
)) {
195 perror("create tag entry thread");
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");
209 item_kill_tree(&array
);