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 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 62 assert(radix_tree_tag_clear(&tree, index, 0)); 63 64 for_each_index(i, base, order) { 65 assert(!radix_tree_tag_get(&tree, i, 0)); 66 assert(!radix_tree_tag_get(&tree, i, 1)); 67 } 68 69 assert(!radix_tree_tagged(&tree, 0)); 70 assert(!radix_tree_tagged(&tree, 1)); 71 72 item_kill_tree(&tree); 73 } 74 75 static void multiorder_tag_tests(void) 76 { 77 /* test multi-order entry for indices 0-7 with no sibling pointers */ 78 __multiorder_tag_test(0, 3); 79 __multiorder_tag_test(5, 3); 80 81 /* test multi-order entry for indices 8-15 with no sibling pointers */ 82 __multiorder_tag_test(8, 3); 83 __multiorder_tag_test(15, 3); 84 85 /* 86 * Our order 5 entry covers indices 0-31 in a tree with height=2. 87 * This is broken up as follows: 88 * 0-7: canonical entry 89 * 8-15: sibling 1 90 * 16-23: sibling 2 91 * 24-31: sibling 3 92 */ 93 __multiorder_tag_test(0, 5); 94 __multiorder_tag_test(29, 5); 95 96 /* same test, but with indices 32-63 */ 97 __multiorder_tag_test(32, 5); 98 __multiorder_tag_test(44, 5); 99 100 /* 101 * Our order 8 entry covers indices 0-255 in a tree with height=3. 102 * This is broken up as follows: 103 * 0-63: canonical entry 104 * 64-127: sibling 1 105 * 128-191: sibling 2 106 * 192-255: sibling 3 107 */ 108 __multiorder_tag_test(0, 8); 109 __multiorder_tag_test(190, 8); 110 111 /* same test, but with indices 256-511 */ 112 __multiorder_tag_test(256, 8); 113 __multiorder_tag_test(300, 8); 114 115 __multiorder_tag_test(0x12345678UL, 8); 116 } 117 118 static void multiorder_check(unsigned long index, int order) 119 { 120 unsigned long i; 121 unsigned long min = index & ~((1UL << order) - 1); 122 unsigned long max = min + (1UL << order); 123 RADIX_TREE(tree, GFP_KERNEL); 124 125 printf("Multiorder index %ld, order %d\n", index, order); 126 127 assert(item_insert_order(&tree, index, order) == 0); 128 129 for (i = min; i < max; i++) { 130 struct item *item = item_lookup(&tree, i); 131 assert(item != 0); 132 assert(item->index == index); 133 } 134 for (i = 0; i < min; i++) 135 item_check_absent(&tree, i); 136 for (i = max; i < 2*max; i++) 137 item_check_absent(&tree, i); 138 for (i = min; i < max; i++) { 139 static void *entry = (void *) 140 (0xA0 | RADIX_TREE_EXCEPTIONAL_ENTRY); 141 assert(radix_tree_insert(&tree, i, entry) == -EEXIST); 142 } 143 144 assert(item_delete(&tree, index) != 0); 145 146 for (i = 0; i < 2*max; i++) 147 item_check_absent(&tree, i); 148 } 149 150 static void multiorder_shrink(unsigned long index, int order) 151 { 152 unsigned long i; 153 unsigned long max = 1 << order; 154 RADIX_TREE(tree, GFP_KERNEL); 155 struct radix_tree_node *node; 156 157 printf("Multiorder shrink index %ld, order %d\n", index, order); 158 159 assert(item_insert_order(&tree, 0, order) == 0); 160 161 node = tree.rnode; 162 163 assert(item_insert(&tree, index) == 0); 164 assert(node != tree.rnode); 165 166 assert(item_delete(&tree, index) != 0); 167 assert(node == tree.rnode); 168 169 for (i = 0; i < max; i++) { 170 struct item *item = item_lookup(&tree, i); 171 assert(item != 0); 172 assert(item->index == 0); 173 } 174 for (i = max; i < 2*max; i++) 175 item_check_absent(&tree, i); 176 177 if (!item_delete(&tree, 0)) { 178 printf("failed to delete index %ld (order %d)\n", index, order); abort(); 179 } 180 181 for (i = 0; i < 2*max; i++) 182 item_check_absent(&tree, i); 183 } 184 185 static void multiorder_insert_bug(void) 186 { 187 RADIX_TREE(tree, GFP_KERNEL); 188 189 item_insert(&tree, 0); 190 radix_tree_tag_set(&tree, 0, 0); 191 item_insert_order(&tree, 3 << 6, 6); 192 193 item_kill_tree(&tree); 194 } 195 196 void multiorder_iteration(void) 197 { 198 RADIX_TREE(tree, GFP_KERNEL); 199 struct radix_tree_iter iter; 200 void **slot; 201 int i, err; 202 203 printf("Multiorder iteration test\n"); 204 205 #define NUM_ENTRIES 11 206 int index[NUM_ENTRIES] = {0, 2, 4, 8, 16, 32, 34, 36, 64, 72, 128}; 207 int order[NUM_ENTRIES] = {1, 1, 2, 3, 4, 1, 0, 1, 3, 0, 7}; 208 209 for (i = 0; i < NUM_ENTRIES; i++) { 210 err = item_insert_order(&tree, index[i], order[i]); 211 assert(!err); 212 } 213 214 i = 0; 215 /* start from index 1 to verify we find the multi-order entry at 0 */ 216 radix_tree_for_each_slot(slot, &tree, &iter, 1) { 217 int height = order[i] / RADIX_TREE_MAP_SHIFT; 218 int shift = height * RADIX_TREE_MAP_SHIFT; 219 220 assert(iter.index == index[i]); 221 assert(iter.shift == shift); 222 i++; 223 } 224 225 /* 226 * Now iterate through the tree starting at an elevated multi-order 227 * entry, beginning at an index in the middle of the range. 228 */ 229 i = 8; 230 radix_tree_for_each_slot(slot, &tree, &iter, 70) { 231 int height = order[i] / RADIX_TREE_MAP_SHIFT; 232 int shift = height * RADIX_TREE_MAP_SHIFT; 233 234 assert(iter.index == index[i]); 235 assert(iter.shift == shift); 236 i++; 237 } 238 239 item_kill_tree(&tree); 240 } 241 242 void multiorder_tagged_iteration(void) 243 { 244 RADIX_TREE(tree, GFP_KERNEL); 245 struct radix_tree_iter iter; 246 void **slot; 247 int i; 248 249 printf("Multiorder tagged iteration test\n"); 250 251 #define MT_NUM_ENTRIES 9 252 int index[MT_NUM_ENTRIES] = {0, 2, 4, 16, 32, 40, 64, 72, 128}; 253 int order[MT_NUM_ENTRIES] = {1, 0, 2, 4, 3, 1, 3, 0, 7}; 254 255 #define TAG_ENTRIES 7 256 int tag_index[TAG_ENTRIES] = {0, 4, 16, 40, 64, 72, 128}; 257 258 for (i = 0; i < MT_NUM_ENTRIES; i++) 259 assert(!item_insert_order(&tree, index[i], order[i])); 260 261 assert(!radix_tree_tagged(&tree, 1)); 262 263 for (i = 0; i < TAG_ENTRIES; i++) 264 assert(radix_tree_tag_set(&tree, tag_index[i], 1)); 265 266 i = 0; 267 /* start from index 1 to verify we find the multi-order entry at 0 */ 268 radix_tree_for_each_tagged(slot, &tree, &iter, 1, 1) { 269 assert(iter.index == tag_index[i]); 270 i++; 271 } 272 273 /* 274 * Now iterate through the tree starting at an elevated multi-order 275 * entry, beginning at an index in the middle of the range. 276 */ 277 i = 4; 278 radix_tree_for_each_slot(slot, &tree, &iter, 70) { 279 assert(iter.index == tag_index[i]); 280 i++; 281 } 282 283 item_kill_tree(&tree); 284 } 285 286 void multiorder_checks(void) 287 { 288 int i; 289 290 for (i = 0; i < 20; i++) { 291 multiorder_check(200, i); 292 multiorder_check(0, i); 293 multiorder_check((1UL << i) + 1, i); 294 } 295 296 for (i = 0; i < 15; i++) 297 multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i); 298 299 multiorder_insert_bug(); 300 multiorder_tag_tests(); 301 multiorder_iteration(); 302 multiorder_tagged_iteration(); 303 } 304