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