2 * multiorder.c: Multi-order radix tree entry testing
3 * Copyright (c) 2016 Intel Corporation
4 * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
5 * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/radix-tree.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
22 #define for_each_index(i, base, order) \
23 for (i = base; i < base + (1 << order); i++)
25 static void __multiorder_tag_test(int index
, int order
)
27 RADIX_TREE(tree
, GFP_KERNEL
);
29 unsigned long first
= 0;
31 /* our canonical entry */
32 base
= index
& ~((1 << order
) - 1);
34 printf("Multiorder tag test with index %d, canonical entry %d\n",
37 err
= item_insert_order(&tree
, index
, order
);
41 * Verify we get collisions for covered indices. We try and fail to
42 * insert an exceptional entry so we don't leak memory via
43 * item_insert_order().
45 for_each_index(i
, base
, order
) {
46 err
= __radix_tree_insert(&tree
, i
, order
,
47 (void *)(0xA0 | RADIX_TREE_EXCEPTIONAL_ENTRY
));
48 assert(err
== -EEXIST
);
51 for_each_index(i
, base
, order
) {
52 assert(!radix_tree_tag_get(&tree
, i
, 0));
53 assert(!radix_tree_tag_get(&tree
, i
, 1));
56 assert(radix_tree_tag_set(&tree
, index
, 0));
58 for_each_index(i
, base
, order
) {
59 assert(radix_tree_tag_get(&tree
, i
, 0));
60 assert(!radix_tree_tag_get(&tree
, i
, 1));
63 assert(radix_tree_range_tag_if_tagged(&tree
, &first
, ~0UL, 10, 0, 1) == 1);
64 assert(radix_tree_tag_clear(&tree
, index
, 0));
66 for_each_index(i
, base
, order
) {
67 assert(!radix_tree_tag_get(&tree
, i
, 0));
68 assert(radix_tree_tag_get(&tree
, i
, 1));
71 assert(radix_tree_tag_clear(&tree
, index
, 1));
73 assert(!radix_tree_tagged(&tree
, 0));
74 assert(!radix_tree_tagged(&tree
, 1));
76 item_kill_tree(&tree
);
79 static void multiorder_tag_tests(void)
81 /* test multi-order entry for indices 0-7 with no sibling pointers */
82 __multiorder_tag_test(0, 3);
83 __multiorder_tag_test(5, 3);
85 /* test multi-order entry for indices 8-15 with no sibling pointers */
86 __multiorder_tag_test(8, 3);
87 __multiorder_tag_test(15, 3);
90 * Our order 5 entry covers indices 0-31 in a tree with height=2.
91 * This is broken up as follows:
92 * 0-7: canonical entry
97 __multiorder_tag_test(0, 5);
98 __multiorder_tag_test(29, 5);
100 /* same test, but with indices 32-63 */
101 __multiorder_tag_test(32, 5);
102 __multiorder_tag_test(44, 5);
105 * Our order 8 entry covers indices 0-255 in a tree with height=3.
106 * This is broken up as follows:
107 * 0-63: canonical entry
112 __multiorder_tag_test(0, 8);
113 __multiorder_tag_test(190, 8);
115 /* same test, but with indices 256-511 */
116 __multiorder_tag_test(256, 8);
117 __multiorder_tag_test(300, 8);
119 __multiorder_tag_test(0x12345678UL
, 8);
122 static void multiorder_check(unsigned long index
, int order
)
125 unsigned long min
= index
& ~((1UL << order
) - 1);
126 unsigned long max
= min
+ (1UL << order
);
128 struct item
*item2
= item_create(min
);
129 RADIX_TREE(tree
, GFP_KERNEL
);
131 printf("Multiorder index %ld, order %d\n", index
, order
);
133 assert(item_insert_order(&tree
, index
, order
) == 0);
135 for (i
= min
; i
< max
; i
++) {
136 struct item
*item
= item_lookup(&tree
, i
);
138 assert(item
->index
== index
);
140 for (i
= 0; i
< min
; i
++)
141 item_check_absent(&tree
, i
);
142 for (i
= max
; i
< 2*max
; i
++)
143 item_check_absent(&tree
, i
);
144 for (i
= min
; i
< max
; i
++)
145 assert(radix_tree_insert(&tree
, i
, item2
) == -EEXIST
);
147 slot
= radix_tree_lookup_slot(&tree
, index
);
149 radix_tree_replace_slot(slot
, item2
);
150 for (i
= min
; i
< max
; i
++) {
151 struct item
*item
= item_lookup(&tree
, i
);
153 assert(item
->index
== min
);
156 assert(item_delete(&tree
, min
) != 0);
158 for (i
= 0; i
< 2*max
; i
++)
159 item_check_absent(&tree
, i
);
162 static void multiorder_shrink(unsigned long index
, int order
)
165 unsigned long max
= 1 << order
;
166 RADIX_TREE(tree
, GFP_KERNEL
);
167 struct radix_tree_node
*node
;
169 printf("Multiorder shrink index %ld, order %d\n", index
, order
);
171 assert(item_insert_order(&tree
, 0, order
) == 0);
175 assert(item_insert(&tree
, index
) == 0);
176 assert(node
!= tree
.rnode
);
178 assert(item_delete(&tree
, index
) != 0);
179 assert(node
== tree
.rnode
);
181 for (i
= 0; i
< max
; i
++) {
182 struct item
*item
= item_lookup(&tree
, i
);
184 assert(item
->index
== 0);
186 for (i
= max
; i
< 2*max
; i
++)
187 item_check_absent(&tree
, i
);
189 if (!item_delete(&tree
, 0)) {
190 printf("failed to delete index %ld (order %d)\n", index
, order
); abort();
193 for (i
= 0; i
< 2*max
; i
++)
194 item_check_absent(&tree
, i
);
197 static void multiorder_insert_bug(void)
199 RADIX_TREE(tree
, GFP_KERNEL
);
201 item_insert(&tree
, 0);
202 radix_tree_tag_set(&tree
, 0, 0);
203 item_insert_order(&tree
, 3 << 6, 6);
205 item_kill_tree(&tree
);
208 void multiorder_iteration(void)
210 RADIX_TREE(tree
, GFP_KERNEL
);
211 struct radix_tree_iter iter
;
215 printf("Multiorder iteration test\n");
217 #define NUM_ENTRIES 11
218 int index
[NUM_ENTRIES
] = {0, 2, 4, 8, 16, 32, 34, 36, 64, 72, 128};
219 int order
[NUM_ENTRIES
] = {1, 1, 2, 3, 4, 1, 0, 1, 3, 0, 7};
221 for (i
= 0; i
< NUM_ENTRIES
; i
++) {
222 err
= item_insert_order(&tree
, index
[i
], order
[i
]);
226 for (j
= 0; j
< 256; j
++) {
227 for (i
= 0; i
< NUM_ENTRIES
; i
++)
228 if (j
<= (index
[i
] | ((1 << order
[i
]) - 1)))
231 radix_tree_for_each_slot(slot
, &tree
, &iter
, j
) {
232 int height
= order
[i
] / RADIX_TREE_MAP_SHIFT
;
233 int shift
= height
* RADIX_TREE_MAP_SHIFT
;
234 int mask
= (1 << order
[i
]) - 1;
236 assert(iter
.index
>= (index
[i
] &~ mask
));
237 assert(iter
.index
<= (index
[i
] | mask
));
238 assert(iter
.shift
== shift
);
243 item_kill_tree(&tree
);
246 void multiorder_tagged_iteration(void)
248 RADIX_TREE(tree
, GFP_KERNEL
);
249 struct radix_tree_iter iter
;
251 unsigned long first
= 0;
254 printf("Multiorder tagged iteration test\n");
256 #define MT_NUM_ENTRIES 9
257 int index
[MT_NUM_ENTRIES
] = {0, 2, 4, 16, 32, 40, 64, 72, 128};
258 int order
[MT_NUM_ENTRIES
] = {1, 0, 2, 4, 3, 1, 3, 0, 7};
260 #define TAG_ENTRIES 7
261 int tag_index
[TAG_ENTRIES
] = {0, 4, 16, 40, 64, 72, 128};
263 for (i
= 0; i
< MT_NUM_ENTRIES
; i
++)
264 assert(!item_insert_order(&tree
, index
[i
], order
[i
]));
266 assert(!radix_tree_tagged(&tree
, 1));
268 for (i
= 0; i
< TAG_ENTRIES
; i
++)
269 assert(radix_tree_tag_set(&tree
, tag_index
[i
], 1));
271 for (j
= 0; j
< 256; j
++) {
274 for (i
= 0; i
< TAG_ENTRIES
; i
++) {
275 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
277 if (j
<= (index
[k
] | ((1 << order
[k
]) - 1)))
281 radix_tree_for_each_tagged(slot
, &tree
, &iter
, j
, 1) {
282 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
284 mask
= (1 << order
[k
]) - 1;
286 assert(iter
.index
>= (tag_index
[i
] &~ mask
));
287 assert(iter
.index
<= (tag_index
[i
] | mask
));
292 radix_tree_range_tag_if_tagged(&tree
, &first
, ~0UL,
293 MT_NUM_ENTRIES
, 1, 2);
295 for (j
= 0; j
< 256; j
++) {
298 for (i
= 0; i
< TAG_ENTRIES
; i
++) {
299 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
301 if (j
<= (index
[k
] | ((1 << order
[k
]) - 1)))
305 radix_tree_for_each_tagged(slot
, &tree
, &iter
, j
, 2) {
306 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
308 mask
= (1 << order
[k
]) - 1;
310 assert(iter
.index
>= (tag_index
[i
] &~ mask
));
311 assert(iter
.index
<= (tag_index
[i
] | mask
));
317 radix_tree_range_tag_if_tagged(&tree
, &first
, ~0UL,
318 MT_NUM_ENTRIES
, 1, 0);
320 radix_tree_for_each_tagged(slot
, &tree
, &iter
, 0, 0) {
321 assert(iter
.index
== tag_index
[i
]);
325 item_kill_tree(&tree
);
328 void multiorder_checks(void)
332 for (i
= 0; i
< 20; i
++) {
333 multiorder_check(200, i
);
334 multiorder_check(0, i
);
335 multiorder_check((1UL << i
) + 1, i
);
338 for (i
= 0; i
< 15; i
++)
339 multiorder_shrink((1UL << (i
+ RADIX_TREE_MAP_SHIFT
)), i
);
341 multiorder_insert_bug();
342 multiorder_tag_tests();
343 multiorder_iteration();
344 multiorder_tagged_iteration();