1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/slab.h>
10 #include <linux/radix-tree.h>
13 #include "regression.h"
15 void __gang_check(unsigned long middle, long down, long up, int chunk, int hop)
18 RADIX_TREE(tree, GFP_KERNEL);
22 for (idx = -down; idx < up; idx++)
23 item_insert(&tree, middle + idx);
25 item_check_absent(&tree, middle - down - 1);
26 for (idx = -down; idx < up; idx++)
27 item_check_present(&tree, middle + idx);
28 item_check_absent(&tree, middle + up);
30 item_gang_check_present(&tree, middle - down,
31 up + down, chunk, hop);
32 item_full_scan(&tree, middle - down, down + up, chunk);
33 item_kill_tree(&tree);
38 __gang_check(1 << 30, 128, 128, 35, 2);
39 __gang_check(1 << 31, 128, 128, 32, 32);
40 __gang_check(1 << 31, 128, 128, 32, 100);
41 __gang_check(1 << 31, 128, 128, 17, 7);
42 __gang_check(0xffff0000, 0, 65536, 17, 7);
43 __gang_check(0xfffffffe, 1, 1, 17, 7);
46 void __big_gang_check(void)
53 unsigned long old_start;
55 // printf("0x%08lx\n", start);
56 __gang_check(start, rand() % 113 + 1, rand() % 71,
57 rand() % 157, rand() % 91 + 1);
59 start += rand() % 1000000;
61 if (start < old_start)
66 void big_gang_check(bool long_run)
70 for (i = 0; i < (long_run ? 1000 : 3); i++) {
77 void add_and_check(void)
79 RADIX_TREE(tree, GFP_KERNEL);
81 item_insert(&tree, 44);
82 item_check_present(&tree, 44);
83 item_check_absent(&tree, 43);
84 item_kill_tree(&tree);
87 void dynamic_height_check(void)
90 RADIX_TREE(tree, GFP_KERNEL);
91 tree_verify_min_height(&tree, 0);
93 item_insert(&tree, 42);
94 tree_verify_min_height(&tree, 42);
96 item_insert(&tree, 1000000);
97 tree_verify_min_height(&tree, 1000000);
99 assert(item_delete(&tree, 1000000));
100 tree_verify_min_height(&tree, 42);
102 assert(item_delete(&tree, 42));
103 tree_verify_min_height(&tree, 0);
105 for (i = 0; i < 1000; i++) {
106 item_insert(&tree, i);
107 tree_verify_min_height(&tree, i);
112 assert(item_delete(&tree, i));
114 tree_verify_min_height(&tree, 0);
118 tree_verify_min_height(&tree, i);
121 item_kill_tree(&tree);
124 void check_copied_tags(struct radix_tree_root *tree, unsigned long start, unsigned long end, unsigned long *idx, int count, int fromtag, int totag)
128 for (i = 0; i < count; i++) {
129 /* if (i % 1000 == 0)
131 if (idx[i] < start || idx[i] > end) {
132 if (item_tag_get(tree, idx[i], totag)) {
133 printv(2, "%lu-%lu: %lu, tags %d-%d\n", start,
134 end, idx[i], item_tag_get(tree, idx[i],
136 item_tag_get(tree, idx[i], totag));
138 assert(!item_tag_get(tree, idx[i], totag));
141 if (item_tag_get(tree, idx[i], fromtag) ^
142 item_tag_get(tree, idx[i], totag)) {
143 printv(2, "%lu-%lu: %lu, tags %d-%d\n", start, end,
144 idx[i], item_tag_get(tree, idx[i], fromtag),
145 item_tag_get(tree, idx[i], totag));
147 assert(!(item_tag_get(tree, idx[i], fromtag) ^
148 item_tag_get(tree, idx[i], totag)));
154 void copy_tag_check(void)
156 RADIX_TREE(tree, GFP_KERNEL);
157 unsigned long idx[ITEMS];
158 unsigned long start, end, count = 0, tagged, cur, tmp;
161 // printf("generating radix tree indices...\n");
164 if (start > end && (rand() % 10)) {
169 /* Specifically create items around the start and the end of the range
170 * with high probability to check for off by one errors */
173 item_insert(&tree, start);
177 item_tag_set(&tree, start, 0);
181 item_insert(&tree, start-1);
183 item_tag_set(&tree, start-1, 0);
186 item_insert(&tree, end);
190 item_tag_set(&tree, end, 0);
194 item_insert(&tree, end+1);
196 item_tag_set(&tree, end+1, 0);
199 for (i = 0; i < ITEMS; i++) {
202 } while (item_lookup(&tree, idx[i]));
204 item_insert(&tree, idx[i]);
206 item_tag_set(&tree, idx[i], 0);
207 if (idx[i] >= start && idx[i] <= end)
210 /* if (i % 1000 == 0)
214 // printf("\ncopying tags...\n");
215 tagged = tag_tagged_items(&tree, NULL, start, end, ITEMS, 0, 1);
217 // printf("checking copied tags\n");
218 assert(tagged == count);
219 check_copied_tags(&tree, start, end, idx, ITEMS, 0, 1);
221 /* Copy tags in several rounds */
222 // printf("\ncopying tags...\n");
223 tmp = rand() % (count / 10 + 2);
224 tagged = tag_tagged_items(&tree, NULL, start, end, tmp, 0, 2);
225 assert(tagged == count);
227 // printf("%lu %lu %lu\n", tagged, tmp, count);
228 // printf("checking copied tags\n");
229 check_copied_tags(&tree, start, end, idx, ITEMS, 0, 2);
230 verify_tag_consistency(&tree, 0);
231 verify_tag_consistency(&tree, 1);
232 verify_tag_consistency(&tree, 2);
234 item_kill_tree(&tree);
237 static void __locate_check(struct radix_tree_root *tree, unsigned long index,
241 unsigned long index2;
243 item_insert_order(tree, index, order);
244 item = item_lookup(tree, index);
245 index2 = find_item(tree, item);
246 if (index != index2) {
247 printv(2, "index %ld order %d inserted; found %ld\n",
248 index, order, index2);
253 static void __order_0_locate_check(void)
255 RADIX_TREE(tree, GFP_KERNEL);
258 for (i = 0; i < 50; i++)
259 __locate_check(&tree, rand() % INT_MAX, 0);
261 item_kill_tree(&tree);
264 static void locate_check(void)
266 RADIX_TREE(tree, GFP_KERNEL);
268 unsigned long offset, index;
270 __order_0_locate_check();
272 for (order = 0; order < 20; order++) {
273 for (offset = 0; offset < (1 << (order + 3));
274 offset += (1UL << order)) {
275 for (index = 0; index < (1UL << (order + 5));
276 index += (1UL << order)) {
277 __locate_check(&tree, index + offset, order);
279 if (find_item(&tree, &tree) != -1)
282 item_kill_tree(&tree);
286 if (find_item(&tree, &tree) != -1)
288 __locate_check(&tree, -1, 0);
289 if (find_item(&tree, &tree) != -1)
291 item_kill_tree(&tree);
294 static void single_thread_tests(bool long_run)
298 printv(1, "starting single_thread_tests: %d allocated, preempt %d\n",
299 nr_allocated, preempt_count);
302 printv(2, "after multiorder_check: %d allocated, preempt %d\n",
303 nr_allocated, preempt_count);
306 printv(2, "after locate_check: %d allocated, preempt %d\n",
307 nr_allocated, preempt_count);
310 printv(2, "after tag_check: %d allocated, preempt %d\n",
311 nr_allocated, preempt_count);
314 printv(2, "after gang_check: %d allocated, preempt %d\n",
315 nr_allocated, preempt_count);
318 printv(2, "after add_and_check: %d allocated, preempt %d\n",
319 nr_allocated, preempt_count);
320 dynamic_height_check();
322 printv(2, "after dynamic_height_check: %d allocated, preempt %d\n",
323 nr_allocated, preempt_count);
327 printv(2, "after idr_checks: %d allocated, preempt %d\n",
328 nr_allocated, preempt_count);
329 big_gang_check(long_run);
331 printv(2, "after big_gang_check: %d allocated, preempt %d\n",
332 nr_allocated, preempt_count);
333 for (i = 0; i < (long_run ? 2000 : 3); i++) {
339 printv(2, "after copy_tag_check: %d allocated, preempt %d\n",
340 nr_allocated, preempt_count);
343 int main(int argc, char **argv)
345 bool long_run = false;
347 unsigned int seed = time(NULL);
349 while ((opt = getopt(argc, argv, "ls:v")) != -1) {
353 seed = strtoul(optarg, NULL, 0);
358 printf("random seed %u\n", seed);
361 printf("running tests\n");
363 rcu_register_thread();
369 iteration_test(0, 10 + 90 * long_run);
370 iteration_test(7, 10 + 90 * long_run);
371 single_thread_tests(long_run);
374 /* Free any remaining preallocated nodes */
375 radix_tree_cpu_dead(0);
380 printv(2, "after rcu_barrier: %d allocated, preempt %d\n",
381 nr_allocated, preempt_count);
382 rcu_unregister_thread();
384 printf("tests completed\n");