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