blob: 86daf23b3509620de06a8b0d2daee0ca58c69188 [file] [log] [blame]
Matthew Wilcox4f3755d2016-05-20 17:02:14 -07001/*
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>
6 *
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.
10 *
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
14 * more details.
15 */
16#include <linux/radix-tree.h>
17#include <linux/slab.h>
18#include <linux/errno.h>
19
20#include "test.h"
21
Ross Zwisler0fc9b8c2016-05-20 17:02:41 -070022#define for_each_index(i, base, order) \
23 for (i = base; i < base + (1 << order); i++)
24
25static void __multiorder_tag_test(int index, int order)
26{
27 RADIX_TREE(tree, GFP_KERNEL);
28 int base, err, i;
29
30 /* our canonical entry */
31 base = index & ~((1 << order) - 1);
32
33 printf("Multiorder tag test with index %d, canonical entry %d\n",
34 index, base);
35
36 err = item_insert_order(&tree, index, order);
37 assert(!err);
38
39 /*
40 * Verify we get collisions for covered indices. We try and fail to
41 * insert an exceptional entry so we don't leak memory via
42 * item_insert_order().
43 */
44 for_each_index(i, base, order) {
45 err = __radix_tree_insert(&tree, i, order,
46 (void *)(0xA0 | RADIX_TREE_EXCEPTIONAL_ENTRY));
47 assert(err == -EEXIST);
48 }
49
50 for_each_index(i, base, order) {
51 assert(!radix_tree_tag_get(&tree, i, 0));
52 assert(!radix_tree_tag_get(&tree, i, 1));
53 }
54
55 assert(radix_tree_tag_set(&tree, index, 0));
56
57 for_each_index(i, base, order) {
58 assert(radix_tree_tag_get(&tree, i, 0));
59 assert(!radix_tree_tag_get(&tree, i, 1));
60 }
61
Matthew Wilcox268f42d2016-12-14 15:08:55 -080062 assert(tag_tagged_items(&tree, NULL, 0, ~0UL, 10, 0, 1) == 1);
Ross Zwisler0fc9b8c2016-05-20 17:02:41 -070063 assert(radix_tree_tag_clear(&tree, index, 0));
64
65 for_each_index(i, base, order) {
66 assert(!radix_tree_tag_get(&tree, i, 0));
Matthew Wilcox070c5ac2016-05-20 17:02:52 -070067 assert(radix_tree_tag_get(&tree, i, 1));
Ross Zwisler0fc9b8c2016-05-20 17:02:41 -070068 }
69
Matthew Wilcox070c5ac2016-05-20 17:02:52 -070070 assert(radix_tree_tag_clear(&tree, index, 1));
71
Ross Zwisler0fc9b8c2016-05-20 17:02:41 -070072 assert(!radix_tree_tagged(&tree, 0));
73 assert(!radix_tree_tagged(&tree, 1));
74
75 item_kill_tree(&tree);
76}
77
78static void multiorder_tag_tests(void)
79{
80 /* test multi-order entry for indices 0-7 with no sibling pointers */
81 __multiorder_tag_test(0, 3);
82 __multiorder_tag_test(5, 3);
83
84 /* test multi-order entry for indices 8-15 with no sibling pointers */
85 __multiorder_tag_test(8, 3);
86 __multiorder_tag_test(15, 3);
87
88 /*
89 * Our order 5 entry covers indices 0-31 in a tree with height=2.
90 * This is broken up as follows:
91 * 0-7: canonical entry
92 * 8-15: sibling 1
93 * 16-23: sibling 2
94 * 24-31: sibling 3
95 */
96 __multiorder_tag_test(0, 5);
97 __multiorder_tag_test(29, 5);
98
99 /* same test, but with indices 32-63 */
100 __multiorder_tag_test(32, 5);
101 __multiorder_tag_test(44, 5);
102
103 /*
104 * Our order 8 entry covers indices 0-255 in a tree with height=3.
105 * This is broken up as follows:
106 * 0-63: canonical entry
107 * 64-127: sibling 1
108 * 128-191: sibling 2
109 * 192-255: sibling 3
110 */
111 __multiorder_tag_test(0, 8);
112 __multiorder_tag_test(190, 8);
113
114 /* same test, but with indices 256-511 */
115 __multiorder_tag_test(256, 8);
116 __multiorder_tag_test(300, 8);
117
118 __multiorder_tag_test(0x12345678UL, 8);
119}
120
Matthew Wilcox4f3755d2016-05-20 17:02:14 -0700121static void multiorder_check(unsigned long index, int order)
122{
123 unsigned long i;
124 unsigned long min = index & ~((1UL << order) - 1);
125 unsigned long max = min + (1UL << order);
Matthew Wilcox62fd5252016-09-22 11:53:34 -0700126 void **slot;
Matthew Wilcox101d9602016-12-14 15:08:23 -0800127 struct item *item2 = item_create(min, order);
Matthew Wilcox4f3755d2016-05-20 17:02:14 -0700128 RADIX_TREE(tree, GFP_KERNEL);
129
130 printf("Multiorder index %ld, order %d\n", index, order);
131
132 assert(item_insert_order(&tree, index, order) == 0);
133
134 for (i = min; i < max; i++) {
135 struct item *item = item_lookup(&tree, i);
136 assert(item != 0);
137 assert(item->index == index);
138 }
139 for (i = 0; i < min; i++)
140 item_check_absent(&tree, i);
141 for (i = max; i < 2*max; i++)
142 item_check_absent(&tree, i);
Matthew Wilcox62fd5252016-09-22 11:53:34 -0700143 for (i = min; i < max; i++)
144 assert(radix_tree_insert(&tree, i, item2) == -EEXIST);
145
146 slot = radix_tree_lookup_slot(&tree, index);
147 free(*slot);
Johannes Weiner6d75f362016-12-12 16:43:43 -0800148 radix_tree_replace_slot(&tree, slot, item2);
Matthew Wilcox8a14f4d2016-05-20 17:02:44 -0700149 for (i = min; i < max; i++) {
Matthew Wilcox62fd5252016-09-22 11:53:34 -0700150 struct item *item = item_lookup(&tree, i);
151 assert(item != 0);
152 assert(item->index == min);
Matthew Wilcox8a14f4d2016-05-20 17:02:44 -0700153 }
Matthew Wilcox4f3755d2016-05-20 17:02:14 -0700154
Matthew Wilcox62fd5252016-09-22 11:53:34 -0700155 assert(item_delete(&tree, min) != 0);
Matthew Wilcox4f3755d2016-05-20 17:02:14 -0700156
157 for (i = 0; i < 2*max; i++)
158 item_check_absent(&tree, i);
159}
160
Matthew Wilcoxafe0e392016-05-20 17:02:17 -0700161static void multiorder_shrink(unsigned long index, int order)
162{
163 unsigned long i;
164 unsigned long max = 1 << order;
165 RADIX_TREE(tree, GFP_KERNEL);
166 struct radix_tree_node *node;
167
168 printf("Multiorder shrink index %ld, order %d\n", index, order);
169
170 assert(item_insert_order(&tree, 0, order) == 0);
171
172 node = tree.rnode;
173
174 assert(item_insert(&tree, index) == 0);
175 assert(node != tree.rnode);
176
177 assert(item_delete(&tree, index) != 0);
178 assert(node == tree.rnode);
179
180 for (i = 0; i < max; i++) {
181 struct item *item = item_lookup(&tree, i);
182 assert(item != 0);
183 assert(item->index == 0);
184 }
185 for (i = max; i < 2*max; i++)
186 item_check_absent(&tree, i);
187
188 if (!item_delete(&tree, 0)) {
189 printf("failed to delete index %ld (order %d)\n", index, order); abort();
190 }
191
192 for (i = 0; i < 2*max; i++)
193 item_check_absent(&tree, i);
194}
195
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700196static void multiorder_insert_bug(void)
197{
198 RADIX_TREE(tree, GFP_KERNEL);
199
200 item_insert(&tree, 0);
201 radix_tree_tag_set(&tree, 0, 0);
202 item_insert_order(&tree, 3 << 6, 6);
203
204 item_kill_tree(&tree);
205}
206
Ross Zwisler643b57d2016-05-20 17:02:29 -0700207void multiorder_iteration(void)
208{
209 RADIX_TREE(tree, GFP_KERNEL);
210 struct radix_tree_iter iter;
211 void **slot;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700212 int i, j, err;
Ross Zwisler643b57d2016-05-20 17:02:29 -0700213
214 printf("Multiorder iteration test\n");
215
216#define NUM_ENTRIES 11
217 int index[NUM_ENTRIES] = {0, 2, 4, 8, 16, 32, 34, 36, 64, 72, 128};
218 int order[NUM_ENTRIES] = {1, 1, 2, 3, 4, 1, 0, 1, 3, 0, 7};
219
220 for (i = 0; i < NUM_ENTRIES; i++) {
221 err = item_insert_order(&tree, index[i], order[i]);
222 assert(!err);
223 }
224
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700225 for (j = 0; j < 256; j++) {
226 for (i = 0; i < NUM_ENTRIES; i++)
227 if (j <= (index[i] | ((1 << order[i]) - 1)))
228 break;
Ross Zwisler643b57d2016-05-20 17:02:29 -0700229
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700230 radix_tree_for_each_slot(slot, &tree, &iter, j) {
231 int height = order[i] / RADIX_TREE_MAP_SHIFT;
232 int shift = height * RADIX_TREE_MAP_SHIFT;
Matthew Wilcox148deab2016-12-14 15:08:49 -0800233 unsigned long mask = (1UL << order[i]) - 1;
234 struct item *item = *slot;
Ross Zwisler643b57d2016-05-20 17:02:29 -0700235
Matthew Wilcox148deab2016-12-14 15:08:49 -0800236 assert((iter.index | mask) == (index[i] | mask));
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700237 assert(iter.shift == shift);
Matthew Wilcox148deab2016-12-14 15:08:49 -0800238 assert(!radix_tree_is_internal_node(item));
239 assert((item->index | mask) == (index[i] | mask));
240 assert(item->order == order[i]);
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700241 i++;
242 }
Ross Zwisler643b57d2016-05-20 17:02:29 -0700243 }
244
245 item_kill_tree(&tree);
246}
247
248void multiorder_tagged_iteration(void)
249{
250 RADIX_TREE(tree, GFP_KERNEL);
251 struct radix_tree_iter iter;
252 void **slot;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700253 int i, j;
Ross Zwisler643b57d2016-05-20 17:02:29 -0700254
255 printf("Multiorder tagged iteration test\n");
256
257#define MT_NUM_ENTRIES 9
258 int index[MT_NUM_ENTRIES] = {0, 2, 4, 16, 32, 40, 64, 72, 128};
259 int order[MT_NUM_ENTRIES] = {1, 0, 2, 4, 3, 1, 3, 0, 7};
260
261#define TAG_ENTRIES 7
262 int tag_index[TAG_ENTRIES] = {0, 4, 16, 40, 64, 72, 128};
263
264 for (i = 0; i < MT_NUM_ENTRIES; i++)
265 assert(!item_insert_order(&tree, index[i], order[i]));
266
267 assert(!radix_tree_tagged(&tree, 1));
268
269 for (i = 0; i < TAG_ENTRIES; i++)
270 assert(radix_tree_tag_set(&tree, tag_index[i], 1));
271
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700272 for (j = 0; j < 256; j++) {
Matthew Wilcox148deab2016-12-14 15:08:49 -0800273 int k;
Ross Zwisler643b57d2016-05-20 17:02:29 -0700274
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700275 for (i = 0; i < TAG_ENTRIES; i++) {
276 for (k = i; index[k] < tag_index[i]; k++)
277 ;
278 if (j <= (index[k] | ((1 << order[k]) - 1)))
279 break;
280 }
281
282 radix_tree_for_each_tagged(slot, &tree, &iter, j, 1) {
Matthew Wilcox148deab2016-12-14 15:08:49 -0800283 unsigned long mask;
284 struct item *item = *slot;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700285 for (k = i; index[k] < tag_index[i]; k++)
286 ;
Matthew Wilcox148deab2016-12-14 15:08:49 -0800287 mask = (1UL << order[k]) - 1;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700288
Matthew Wilcox148deab2016-12-14 15:08:49 -0800289 assert((iter.index | mask) == (tag_index[i] | mask));
290 assert(!radix_tree_is_internal_node(item));
291 assert((item->index | mask) == (tag_index[i] | mask));
292 assert(item->order == order[k]);
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700293 i++;
294 }
Ross Zwisler643b57d2016-05-20 17:02:29 -0700295 }
296
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800297 assert(tag_tagged_items(&tree, NULL, 0, ~0UL, TAG_ENTRIES, 1, 2) ==
298 TAG_ENTRIES);
Matthew Wilcox070c5ac2016-05-20 17:02:52 -0700299
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700300 for (j = 0; j < 256; j++) {
301 int mask, k;
302
303 for (i = 0; i < TAG_ENTRIES; i++) {
304 for (k = i; index[k] < tag_index[i]; k++)
305 ;
306 if (j <= (index[k] | ((1 << order[k]) - 1)))
307 break;
308 }
309
310 radix_tree_for_each_tagged(slot, &tree, &iter, j, 2) {
Matthew Wilcox148deab2016-12-14 15:08:49 -0800311 struct item *item = *slot;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700312 for (k = i; index[k] < tag_index[i]; k++)
313 ;
314 mask = (1 << order[k]) - 1;
315
Matthew Wilcox148deab2016-12-14 15:08:49 -0800316 assert((iter.index | mask) == (tag_index[i] | mask));
317 assert(!radix_tree_is_internal_node(item));
318 assert((item->index | mask) == (tag_index[i] | mask));
319 assert(item->order == order[k]);
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700320 i++;
321 }
Matthew Wilcox070c5ac2016-05-20 17:02:52 -0700322 }
323
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800324 assert(tag_tagged_items(&tree, NULL, 1, ~0UL, MT_NUM_ENTRIES * 2, 1, 0)
325 == TAG_ENTRIES);
Matthew Wilcox070c5ac2016-05-20 17:02:52 -0700326 i = 0;
327 radix_tree_for_each_tagged(slot, &tree, &iter, 0, 0) {
328 assert(iter.index == tag_index[i]);
329 i++;
330 }
331
Ross Zwisler643b57d2016-05-20 17:02:29 -0700332 item_kill_tree(&tree);
333}
334
Matthew Wilcox4f3755d2016-05-20 17:02:14 -0700335void multiorder_checks(void)
336{
337 int i;
338
339 for (i = 0; i < 20; i++) {
340 multiorder_check(200, i);
341 multiorder_check(0, i);
342 multiorder_check((1UL << i) + 1, i);
343 }
Matthew Wilcoxafe0e392016-05-20 17:02:17 -0700344
345 for (i = 0; i < 15; i++)
346 multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i);
347
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700348 multiorder_insert_bug();
Ross Zwisler0fc9b8c2016-05-20 17:02:41 -0700349 multiorder_tag_tests();
Ross Zwisler643b57d2016-05-20 17:02:29 -0700350 multiorder_iteration();
351 multiorder_tagged_iteration();
Matthew Wilcox4f3755d2016-05-20 17:02:14 -0700352}