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
);
127 RADIX_TREE(tree
, GFP_KERNEL
);
129 printf("Multiorder index %ld, order %d\n", index
, order
);
131 assert(item_insert_order(&tree
, index
, order
) == 0);
133 for (i
= min
; i
< max
; i
++) {
134 struct item
*item
= item_lookup(&tree
, i
);
136 assert(item
->index
== index
);
138 for (i
= 0; i
< min
; i
++)
139 item_check_absent(&tree
, i
);
140 for (i
= max
; i
< 2*max
; i
++)
141 item_check_absent(&tree
, i
);
142 for (i
= min
; i
< max
; i
++) {
143 static void *entry
= (void *)
144 (0xA0 | RADIX_TREE_EXCEPTIONAL_ENTRY
);
145 assert(radix_tree_insert(&tree
, i
, entry
) == -EEXIST
);
148 assert(item_delete(&tree
, index
) != 0);
150 for (i
= 0; i
< 2*max
; i
++)
151 item_check_absent(&tree
, i
);
154 static void multiorder_shrink(unsigned long index
, int order
)
157 unsigned long max
= 1 << order
;
158 RADIX_TREE(tree
, GFP_KERNEL
);
159 struct radix_tree_node
*node
;
161 printf("Multiorder shrink index %ld, order %d\n", index
, order
);
163 assert(item_insert_order(&tree
, 0, order
) == 0);
167 assert(item_insert(&tree
, index
) == 0);
168 assert(node
!= tree
.rnode
);
170 assert(item_delete(&tree
, index
) != 0);
171 assert(node
== tree
.rnode
);
173 for (i
= 0; i
< max
; i
++) {
174 struct item
*item
= item_lookup(&tree
, i
);
176 assert(item
->index
== 0);
178 for (i
= max
; i
< 2*max
; i
++)
179 item_check_absent(&tree
, i
);
181 if (!item_delete(&tree
, 0)) {
182 printf("failed to delete index %ld (order %d)\n", index
, order
); abort();
185 for (i
= 0; i
< 2*max
; i
++)
186 item_check_absent(&tree
, i
);
189 static void multiorder_insert_bug(void)
191 RADIX_TREE(tree
, GFP_KERNEL
);
193 item_insert(&tree
, 0);
194 radix_tree_tag_set(&tree
, 0, 0);
195 item_insert_order(&tree
, 3 << 6, 6);
197 item_kill_tree(&tree
);
200 void multiorder_iteration(void)
202 RADIX_TREE(tree
, GFP_KERNEL
);
203 struct radix_tree_iter iter
;
207 printf("Multiorder iteration test\n");
209 #define NUM_ENTRIES 11
210 int index
[NUM_ENTRIES
] = {0, 2, 4, 8, 16, 32, 34, 36, 64, 72, 128};
211 int order
[NUM_ENTRIES
] = {1, 1, 2, 3, 4, 1, 0, 1, 3, 0, 7};
213 for (i
= 0; i
< NUM_ENTRIES
; i
++) {
214 err
= item_insert_order(&tree
, index
[i
], order
[i
]);
218 for (j
= 0; j
< 256; j
++) {
219 for (i
= 0; i
< NUM_ENTRIES
; i
++)
220 if (j
<= (index
[i
] | ((1 << order
[i
]) - 1)))
223 radix_tree_for_each_slot(slot
, &tree
, &iter
, j
) {
224 int height
= order
[i
] / RADIX_TREE_MAP_SHIFT
;
225 int shift
= height
* RADIX_TREE_MAP_SHIFT
;
226 int mask
= (1 << order
[i
]) - 1;
228 assert(iter
.index
>= (index
[i
] &~ mask
));
229 assert(iter
.index
<= (index
[i
] | mask
));
230 assert(iter
.shift
== shift
);
235 item_kill_tree(&tree
);
238 void multiorder_tagged_iteration(void)
240 RADIX_TREE(tree
, GFP_KERNEL
);
241 struct radix_tree_iter iter
;
243 unsigned long first
= 0;
246 printf("Multiorder tagged iteration test\n");
248 #define MT_NUM_ENTRIES 9
249 int index
[MT_NUM_ENTRIES
] = {0, 2, 4, 16, 32, 40, 64, 72, 128};
250 int order
[MT_NUM_ENTRIES
] = {1, 0, 2, 4, 3, 1, 3, 0, 7};
252 #define TAG_ENTRIES 7
253 int tag_index
[TAG_ENTRIES
] = {0, 4, 16, 40, 64, 72, 128};
255 for (i
= 0; i
< MT_NUM_ENTRIES
; i
++)
256 assert(!item_insert_order(&tree
, index
[i
], order
[i
]));
258 assert(!radix_tree_tagged(&tree
, 1));
260 for (i
= 0; i
< TAG_ENTRIES
; i
++)
261 assert(radix_tree_tag_set(&tree
, tag_index
[i
], 1));
263 for (j
= 0; j
< 256; j
++) {
266 for (i
= 0; i
< TAG_ENTRIES
; i
++) {
267 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
269 if (j
<= (index
[k
] | ((1 << order
[k
]) - 1)))
273 radix_tree_for_each_tagged(slot
, &tree
, &iter
, j
, 1) {
274 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
276 mask
= (1 << order
[k
]) - 1;
278 assert(iter
.index
>= (tag_index
[i
] &~ mask
));
279 assert(iter
.index
<= (tag_index
[i
] | mask
));
284 radix_tree_range_tag_if_tagged(&tree
, &first
, ~0UL,
285 MT_NUM_ENTRIES
, 1, 2);
287 for (j
= 0; j
< 256; j
++) {
290 for (i
= 0; i
< TAG_ENTRIES
; i
++) {
291 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
293 if (j
<= (index
[k
] | ((1 << order
[k
]) - 1)))
297 radix_tree_for_each_tagged(slot
, &tree
, &iter
, j
, 2) {
298 for (k
= i
; index
[k
] < tag_index
[i
]; k
++)
300 mask
= (1 << order
[k
]) - 1;
302 assert(iter
.index
>= (tag_index
[i
] &~ mask
));
303 assert(iter
.index
<= (tag_index
[i
] | mask
));
309 radix_tree_range_tag_if_tagged(&tree
, &first
, ~0UL,
310 MT_NUM_ENTRIES
, 1, 0);
312 radix_tree_for_each_tagged(slot
, &tree
, &iter
, 0, 0) {
313 assert(iter
.index
== tag_index
[i
]);
317 item_kill_tree(&tree
);
320 void multiorder_checks(void)
324 for (i
= 0; i
< 20; i
++) {
325 multiorder_check(200, i
);
326 multiorder_check(0, i
);
327 multiorder_check((1UL << i
) + 1, i
);
330 for (i
= 0; i
< 15; i
++)
331 multiorder_shrink((1UL << (i
+ RADIX_TREE_MAP_SHIFT
)), i
);
333 multiorder_insert_bug();
334 multiorder_tag_tests();
335 multiorder_iteration();
336 multiorder_tagged_iteration();