Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 2 | #include <stdlib.h> |
| 3 | #include <assert.h> |
| 4 | #include <stdio.h> |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/bitops.h> |
| 8 | |
| 9 | #include "test.h" |
| 10 | |
| 11 | struct item * |
| 12 | item_tag_set(struct radix_tree_root *root, unsigned long index, int tag) |
| 13 | { |
| 14 | return radix_tree_tag_set(root, index, tag); |
| 15 | } |
| 16 | |
| 17 | struct item * |
| 18 | item_tag_clear(struct radix_tree_root *root, unsigned long index, int tag) |
| 19 | { |
| 20 | return radix_tree_tag_clear(root, index, tag); |
| 21 | } |
| 22 | |
| 23 | int item_tag_get(struct radix_tree_root *root, unsigned long index, int tag) |
| 24 | { |
| 25 | return radix_tree_tag_get(root, index, tag); |
| 26 | } |
| 27 | |
Matthew Wilcox | 101d960 | 2016-12-14 15:08:23 -0800 | [diff] [blame] | 28 | int __item_insert(struct radix_tree_root *root, struct item *item) |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 29 | { |
Matthew Wilcox | 101d960 | 2016-12-14 15:08:23 -0800 | [diff] [blame] | 30 | return __radix_tree_insert(root, item->index, item->order, item); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Matthew Wilcox | 18d0c57 | 2017-01-29 01:48:34 -0500 | [diff] [blame] | 33 | struct item *item_create(unsigned long index, unsigned int order) |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 34 | { |
Matthew Wilcox | 18d0c57 | 2017-01-29 01:48:34 -0500 | [diff] [blame] | 35 | struct item *ret = malloc(sizeof(*ret)); |
| 36 | |
| 37 | ret->index = index; |
| 38 | ret->order = order; |
| 39 | return ret; |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | int item_insert_order(struct radix_tree_root *root, unsigned long index, |
| 43 | unsigned order) |
| 44 | { |
Matthew Wilcox | 18d0c57 | 2017-01-29 01:48:34 -0500 | [diff] [blame] | 45 | struct item *item = item_create(index, order); |
| 46 | int err = __item_insert(root, item); |
| 47 | if (err) |
| 48 | free(item); |
| 49 | return err; |
| 50 | } |
| 51 | |
| 52 | int item_insert(struct radix_tree_root *root, unsigned long index) |
| 53 | { |
| 54 | return item_insert_order(root, index, 0); |
Matthew Wilcox | 101d960 | 2016-12-14 15:08:23 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void item_sanity(struct item *item, unsigned long index) |
| 58 | { |
| 59 | unsigned long mask; |
| 60 | assert(!radix_tree_is_internal_node(item)); |
| 61 | assert(item->order < BITS_PER_LONG); |
| 62 | mask = (1UL << item->order) - 1; |
| 63 | assert((item->index | mask) == (index | mask)); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int item_delete(struct radix_tree_root *root, unsigned long index) |
| 67 | { |
| 68 | struct item *item = radix_tree_delete(root, index); |
| 69 | |
| 70 | if (item) { |
Matthew Wilcox | 101d960 | 2016-12-14 15:08:23 -0800 | [diff] [blame] | 71 | item_sanity(item, index); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 72 | free(item); |
| 73 | return 1; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
Ross Zwisler | 3e252fa | 2018-05-18 16:08:58 -0700 | [diff] [blame] | 78 | static void item_free_rcu(struct rcu_head *head) |
| 79 | { |
| 80 | struct item *item = container_of(head, struct item, rcu_head); |
| 81 | |
| 82 | free(item); |
| 83 | } |
| 84 | |
| 85 | int item_delete_rcu(struct radix_tree_root *root, unsigned long index) |
| 86 | { |
| 87 | struct item *item = radix_tree_delete(root, index); |
| 88 | |
| 89 | if (item) { |
| 90 | item_sanity(item, index); |
| 91 | call_rcu(&item->rcu_head, item_free_rcu); |
| 92 | return 1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 97 | void item_check_present(struct radix_tree_root *root, unsigned long index) |
| 98 | { |
| 99 | struct item *item; |
| 100 | |
| 101 | item = radix_tree_lookup(root, index); |
Matthew Wilcox | 101d960 | 2016-12-14 15:08:23 -0800 | [diff] [blame] | 102 | assert(item != NULL); |
| 103 | item_sanity(item, index); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | struct item *item_lookup(struct radix_tree_root *root, unsigned long index) |
| 107 | { |
| 108 | return radix_tree_lookup(root, index); |
| 109 | } |
| 110 | |
| 111 | void item_check_absent(struct radix_tree_root *root, unsigned long index) |
| 112 | { |
| 113 | struct item *item; |
| 114 | |
| 115 | item = radix_tree_lookup(root, index); |
Matthew Wilcox | 101d960 | 2016-12-14 15:08:23 -0800 | [diff] [blame] | 116 | assert(item == NULL); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Scan only the passed (start, start+nr] for present items |
| 121 | */ |
| 122 | void item_gang_check_present(struct radix_tree_root *root, |
| 123 | unsigned long start, unsigned long nr, |
| 124 | int chunk, int hop) |
| 125 | { |
| 126 | struct item *items[chunk]; |
| 127 | unsigned long into; |
| 128 | |
| 129 | for (into = 0; into < nr; ) { |
| 130 | int nfound; |
| 131 | int nr_to_find = chunk; |
| 132 | int i; |
| 133 | |
| 134 | if (nr_to_find > (nr - into)) |
| 135 | nr_to_find = nr - into; |
| 136 | |
| 137 | nfound = radix_tree_gang_lookup(root, (void **)items, |
| 138 | start + into, nr_to_find); |
| 139 | assert(nfound == nr_to_find); |
| 140 | for (i = 0; i < nfound; i++) |
| 141 | assert(items[i]->index == start + into + i); |
| 142 | into += hop; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Scan the entire tree, only expecting present items (start, start+nr] |
| 148 | */ |
| 149 | void item_full_scan(struct radix_tree_root *root, unsigned long start, |
| 150 | unsigned long nr, int chunk) |
| 151 | { |
| 152 | struct item *items[chunk]; |
| 153 | unsigned long into = 0; |
| 154 | unsigned long this_index = start; |
| 155 | int nfound; |
| 156 | int i; |
| 157 | |
| 158 | // printf("%s(0x%08lx, 0x%08lx, %d)\n", __FUNCTION__, start, nr, chunk); |
| 159 | |
| 160 | while ((nfound = radix_tree_gang_lookup(root, (void **)items, into, |
| 161 | chunk))) { |
| 162 | // printf("At 0x%08lx, nfound=%d\n", into, nfound); |
| 163 | for (i = 0; i < nfound; i++) { |
| 164 | assert(items[i]->index == this_index); |
| 165 | this_index++; |
| 166 | } |
| 167 | // printf("Found 0x%08lx->0x%08lx\n", |
| 168 | // items[0]->index, items[nfound-1]->index); |
| 169 | into = this_index; |
| 170 | } |
| 171 | if (chunk) |
| 172 | assert(this_index == start + nr); |
| 173 | nfound = radix_tree_gang_lookup(root, (void **)items, |
| 174 | this_index, chunk); |
| 175 | assert(nfound == 0); |
| 176 | } |
| 177 | |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 178 | /* Use the same pattern as tag_pages_for_writeback() in mm/page-writeback.c */ |
| 179 | int tag_tagged_items(struct radix_tree_root *root, pthread_mutex_t *lock, |
| 180 | unsigned long start, unsigned long end, unsigned batch, |
| 181 | unsigned iftag, unsigned thentag) |
| 182 | { |
| 183 | unsigned long tagged = 0; |
| 184 | struct radix_tree_iter iter; |
| 185 | void **slot; |
| 186 | |
| 187 | if (batch == 0) |
| 188 | batch = 1; |
| 189 | |
| 190 | if (lock) |
| 191 | pthread_mutex_lock(lock); |
| 192 | radix_tree_for_each_tagged(slot, root, &iter, start, iftag) { |
| 193 | if (iter.index > end) |
| 194 | break; |
| 195 | radix_tree_iter_tag_set(root, &iter, thentag); |
| 196 | tagged++; |
| 197 | if ((tagged % batch) != 0) |
| 198 | continue; |
| 199 | slot = radix_tree_iter_resume(slot, &iter); |
| 200 | if (lock) { |
| 201 | pthread_mutex_unlock(lock); |
| 202 | rcu_barrier(); |
| 203 | pthread_mutex_lock(lock); |
| 204 | } |
| 205 | } |
| 206 | if (lock) |
| 207 | pthread_mutex_unlock(lock); |
| 208 | |
| 209 | return tagged; |
| 210 | } |
| 211 | |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 212 | /* Use the same pattern as find_swap_entry() in mm/shmem.c */ |
| 213 | unsigned long find_item(struct radix_tree_root *root, void *item) |
| 214 | { |
| 215 | struct radix_tree_iter iter; |
| 216 | void **slot; |
| 217 | unsigned long found = -1; |
| 218 | unsigned long checked = 0; |
| 219 | |
| 220 | radix_tree_for_each_slot(slot, root, &iter, 0) { |
| 221 | if (*slot == item) { |
| 222 | found = iter.index; |
| 223 | break; |
| 224 | } |
| 225 | checked++; |
| 226 | if ((checked % 4) != 0) |
| 227 | continue; |
| 228 | slot = radix_tree_iter_resume(slot, &iter); |
| 229 | } |
| 230 | |
| 231 | return found; |
| 232 | } |
| 233 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 234 | static int verify_node(struct radix_tree_node *slot, unsigned int tag, |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 235 | int tagged) |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 236 | { |
| 237 | int anyset = 0; |
| 238 | int i; |
| 239 | int j; |
| 240 | |
Matthew Wilcox | 4dd6c09 | 2016-05-20 17:03:27 -0700 | [diff] [blame] | 241 | slot = entry_to_node(slot); |
Matthew Wilcox | 339e635 | 2016-03-17 14:21:48 -0700 | [diff] [blame] | 242 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 243 | /* Verify consistency at this level */ |
| 244 | for (i = 0; i < RADIX_TREE_TAG_LONGS; i++) { |
| 245 | if (slot->tags[tag][i]) { |
| 246 | anyset = 1; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | if (tagged != anyset) { |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 251 | printf("tag: %u, shift %u, tagged: %d, anyset: %d\n", |
| 252 | tag, slot->shift, tagged, anyset); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 253 | for (j = 0; j < RADIX_TREE_MAX_TAGS; j++) { |
| 254 | printf("tag %d: ", j); |
| 255 | for (i = 0; i < RADIX_TREE_TAG_LONGS; i++) |
| 256 | printf("%016lx ", slot->tags[j][i]); |
| 257 | printf("\n"); |
| 258 | } |
| 259 | return 1; |
| 260 | } |
| 261 | assert(tagged == anyset); |
| 262 | |
| 263 | /* Go for next level */ |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 264 | if (slot->shift > 0) { |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 265 | for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) |
| 266 | if (slot->slots[i]) |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 267 | if (verify_node(slot->slots[i], tag, |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 268 | !!test_bit(i, slot->tags[tag]))) { |
| 269 | printf("Failure at off %d\n", i); |
| 270 | for (j = 0; j < RADIX_TREE_MAX_TAGS; j++) { |
| 271 | printf("tag %d: ", j); |
| 272 | for (i = 0; i < RADIX_TREE_TAG_LONGS; i++) |
| 273 | printf("%016lx ", slot->tags[j][i]); |
| 274 | printf("\n"); |
| 275 | } |
| 276 | return 1; |
| 277 | } |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | void verify_tag_consistency(struct radix_tree_root *root, unsigned int tag) |
| 283 | { |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 284 | struct radix_tree_node *node = root->rnode; |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 285 | if (!radix_tree_is_internal_node(node)) |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 286 | return; |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 287 | verify_node(node, tag, !!root_tag_get(root, tag)); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void item_kill_tree(struct radix_tree_root *root) |
| 291 | { |
Matthew Wilcox | 3ad75f8 | 2016-12-14 15:08:20 -0800 | [diff] [blame] | 292 | struct radix_tree_iter iter; |
| 293 | void **slot; |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 294 | struct item *items[32]; |
| 295 | int nfound; |
| 296 | |
Matthew Wilcox | 3ad75f8 | 2016-12-14 15:08:20 -0800 | [diff] [blame] | 297 | radix_tree_for_each_slot(slot, root, &iter, 0) { |
| 298 | if (radix_tree_exceptional_entry(*slot)) |
| 299 | radix_tree_delete(root, iter.index); |
| 300 | } |
| 301 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 302 | while ((nfound = radix_tree_gang_lookup(root, (void **)items, 0, 32))) { |
| 303 | int i; |
| 304 | |
| 305 | for (i = 0; i < nfound; i++) { |
| 306 | void *ret; |
| 307 | |
| 308 | ret = radix_tree_delete(root, items[i]->index); |
| 309 | assert(ret == items[i]); |
| 310 | free(items[i]); |
| 311 | } |
| 312 | } |
| 313 | assert(radix_tree_gang_lookup(root, (void **)items, 0, 32) == 0); |
| 314 | assert(root->rnode == NULL); |
| 315 | } |
| 316 | |
| 317 | void tree_verify_min_height(struct radix_tree_root *root, int maxindex) |
| 318 | { |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 319 | unsigned shift; |
| 320 | struct radix_tree_node *node = root->rnode; |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 321 | if (!radix_tree_is_internal_node(node)) { |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 322 | assert(maxindex == 0); |
| 323 | return; |
| 324 | } |
| 325 | |
Matthew Wilcox | 4dd6c09 | 2016-05-20 17:03:27 -0700 | [diff] [blame] | 326 | node = entry_to_node(node); |
Matthew Wilcox | 0694f0c | 2016-05-20 17:03:16 -0700 | [diff] [blame] | 327 | assert(maxindex <= node_maxindex(node)); |
| 328 | |
| 329 | shift = node->shift; |
| 330 | if (shift > 0) |
| 331 | assert(maxindex > shift_maxindex(shift - RADIX_TREE_MAP_SHIFT)); |
| 332 | else |
| 333 | assert(maxindex > 0); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 334 | } |