Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Zwisler | 0fc9b8c | 2016-05-20 17:02:41 -0700 | [diff] [blame] | 22 | #define for_each_index(i, base, order) \ |
| 23 | for (i = base; i < base + (1 << order); i++) |
| 24 | |
| 25 | static 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 Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame^] | 62 | assert(tag_tagged_items(&tree, NULL, 0, ~0UL, 10, 0, 1) == 1); |
Ross Zwisler | 0fc9b8c | 2016-05-20 17:02:41 -0700 | [diff] [blame] | 63 | 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 Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 67 | assert(radix_tree_tag_get(&tree, i, 1)); |
Ross Zwisler | 0fc9b8c | 2016-05-20 17:02:41 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 70 | assert(radix_tree_tag_clear(&tree, index, 1)); |
| 71 | |
Ross Zwisler | 0fc9b8c | 2016-05-20 17:02:41 -0700 | [diff] [blame] | 72 | assert(!radix_tree_tagged(&tree, 0)); |
| 73 | assert(!radix_tree_tagged(&tree, 1)); |
| 74 | |
| 75 | item_kill_tree(&tree); |
| 76 | } |
| 77 | |
| 78 | static 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 Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 121 | static 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 Wilcox | 62fd525 | 2016-09-22 11:53:34 -0700 | [diff] [blame] | 126 | void **slot; |
Matthew Wilcox | 101d960 | 2016-12-14 15:08:23 -0800 | [diff] [blame] | 127 | struct item *item2 = item_create(min, order); |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 128 | 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 Wilcox | 62fd525 | 2016-09-22 11:53:34 -0700 | [diff] [blame] | 143 | 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 Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 148 | radix_tree_replace_slot(&tree, slot, item2); |
Matthew Wilcox | 8a14f4d | 2016-05-20 17:02:44 -0700 | [diff] [blame] | 149 | for (i = min; i < max; i++) { |
Matthew Wilcox | 62fd525 | 2016-09-22 11:53:34 -0700 | [diff] [blame] | 150 | struct item *item = item_lookup(&tree, i); |
| 151 | assert(item != 0); |
| 152 | assert(item->index == min); |
Matthew Wilcox | 8a14f4d | 2016-05-20 17:02:44 -0700 | [diff] [blame] | 153 | } |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 154 | |
Matthew Wilcox | 62fd525 | 2016-09-22 11:53:34 -0700 | [diff] [blame] | 155 | assert(item_delete(&tree, min) != 0); |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 156 | |
| 157 | for (i = 0; i < 2*max; i++) |
| 158 | item_check_absent(&tree, i); |
| 159 | } |
| 160 | |
Matthew Wilcox | afe0e39 | 2016-05-20 17:02:17 -0700 | [diff] [blame] | 161 | static 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 Wilcox | 7b60e9a | 2016-05-20 17:02:23 -0700 | [diff] [blame] | 196 | static 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 Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 207 | void multiorder_iteration(void) |
| 208 | { |
| 209 | RADIX_TREE(tree, GFP_KERNEL); |
| 210 | struct radix_tree_iter iter; |
| 211 | void **slot; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 212 | int i, j, err; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 213 | |
| 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 Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 225 | 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 Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 229 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 230 | 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 Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 233 | unsigned long mask = (1UL << order[i]) - 1; |
| 234 | struct item *item = *slot; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 235 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 236 | assert((iter.index | mask) == (index[i] | mask)); |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 237 | assert(iter.shift == shift); |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 238 | assert(!radix_tree_is_internal_node(item)); |
| 239 | assert((item->index | mask) == (index[i] | mask)); |
| 240 | assert(item->order == order[i]); |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 241 | i++; |
| 242 | } |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | item_kill_tree(&tree); |
| 246 | } |
| 247 | |
| 248 | void multiorder_tagged_iteration(void) |
| 249 | { |
| 250 | RADIX_TREE(tree, GFP_KERNEL); |
| 251 | struct radix_tree_iter iter; |
| 252 | void **slot; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 253 | int i, j; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 254 | |
| 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 Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 272 | for (j = 0; j < 256; j++) { |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 273 | int k; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 274 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 275 | 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 Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 283 | unsigned long mask; |
| 284 | struct item *item = *slot; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 285 | for (k = i; index[k] < tag_index[i]; k++) |
| 286 | ; |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 287 | mask = (1UL << order[k]) - 1; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 288 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 289 | 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 Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 293 | i++; |
| 294 | } |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame^] | 297 | assert(tag_tagged_items(&tree, NULL, 0, ~0UL, TAG_ENTRIES, 1, 2) == |
| 298 | TAG_ENTRIES); |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 299 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 300 | 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 Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 311 | struct item *item = *slot; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 312 | for (k = i; index[k] < tag_index[i]; k++) |
| 313 | ; |
| 314 | mask = (1 << order[k]) - 1; |
| 315 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 316 | 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 Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 320 | i++; |
| 321 | } |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame^] | 324 | assert(tag_tagged_items(&tree, NULL, 1, ~0UL, MT_NUM_ENTRIES * 2, 1, 0) |
| 325 | == TAG_ENTRIES); |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 326 | 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 Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 332 | item_kill_tree(&tree); |
| 333 | } |
| 334 | |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 335 | void 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 Wilcox | afe0e39 | 2016-05-20 17:02:17 -0700 | [diff] [blame] | 344 | |
| 345 | for (i = 0; i < 15; i++) |
| 346 | multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i); |
| 347 | |
Matthew Wilcox | 7b60e9a | 2016-05-20 17:02:23 -0700 | [diff] [blame] | 348 | multiorder_insert_bug(); |
Ross Zwisler | 0fc9b8c | 2016-05-20 17:02:41 -0700 | [diff] [blame] | 349 | multiorder_tag_tests(); |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 350 | multiorder_iteration(); |
| 351 | multiorder_tagged_iteration(); |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 352 | } |