1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/types.h>
6 #include <linux/kernel.h>
7 #include <linux/bitops.h>
12 item_tag_set(struct radix_tree_root
*root
, unsigned long index
, int tag
)
14 return radix_tree_tag_set(root
, index
, tag
);
18 item_tag_clear(struct radix_tree_root
*root
, unsigned long index
, int tag
)
20 return radix_tree_tag_clear(root
, index
, tag
);
23 int item_tag_get(struct radix_tree_root
*root
, unsigned long index
, int tag
)
25 return radix_tree_tag_get(root
, index
, tag
);
28 int __item_insert(struct radix_tree_root
*root
, struct item
*item
)
30 return __radix_tree_insert(root
, item
->index
, item
->order
, item
);
33 struct item
*item_create(unsigned long index
, unsigned int order
)
35 struct item
*ret
= malloc(sizeof(*ret
));
42 int item_insert_order(struct radix_tree_root
*root
, unsigned long index
,
45 struct item
*item
= item_create(index
, order
);
46 int err
= __item_insert(root
, item
);
52 int item_insert(struct radix_tree_root
*root
, unsigned long index
)
54 return item_insert_order(root
, index
, 0);
57 void item_sanity(struct item
*item
, unsigned long index
)
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
));
66 int item_delete(struct radix_tree_root
*root
, unsigned long index
)
68 struct item
*item
= radix_tree_delete(root
, index
);
71 item_sanity(item
, index
);
78 void item_check_present(struct radix_tree_root
*root
, unsigned long index
)
82 item
= radix_tree_lookup(root
, index
);
84 item_sanity(item
, index
);
87 struct item
*item_lookup(struct radix_tree_root
*root
, unsigned long index
)
89 return radix_tree_lookup(root
, index
);
92 void item_check_absent(struct radix_tree_root
*root
, unsigned long index
)
96 item
= radix_tree_lookup(root
, index
);
101 * Scan only the passed (start, start+nr] for present items
103 void item_gang_check_present(struct radix_tree_root
*root
,
104 unsigned long start
, unsigned long nr
,
107 struct item
*items
[chunk
];
110 for (into
= 0; into
< nr
; ) {
112 int nr_to_find
= chunk
;
115 if (nr_to_find
> (nr
- into
))
116 nr_to_find
= nr
- into
;
118 nfound
= radix_tree_gang_lookup(root
, (void **)items
,
119 start
+ into
, nr_to_find
);
120 assert(nfound
== nr_to_find
);
121 for (i
= 0; i
< nfound
; i
++)
122 assert(items
[i
]->index
== start
+ into
+ i
);
128 * Scan the entire tree, only expecting present items (start, start+nr]
130 void item_full_scan(struct radix_tree_root
*root
, unsigned long start
,
131 unsigned long nr
, int chunk
)
133 struct item
*items
[chunk
];
134 unsigned long into
= 0;
135 unsigned long this_index
= start
;
139 // printf("%s(0x%08lx, 0x%08lx, %d)\n", __FUNCTION__, start, nr, chunk);
141 while ((nfound
= radix_tree_gang_lookup(root
, (void **)items
, into
,
143 // printf("At 0x%08lx, nfound=%d\n", into, nfound);
144 for (i
= 0; i
< nfound
; i
++) {
145 assert(items
[i
]->index
== this_index
);
148 // printf("Found 0x%08lx->0x%08lx\n",
149 // items[0]->index, items[nfound-1]->index);
153 assert(this_index
== start
+ nr
);
154 nfound
= radix_tree_gang_lookup(root
, (void **)items
,
159 /* Use the same pattern as tag_pages_for_writeback() in mm/page-writeback.c */
160 int tag_tagged_items(struct radix_tree_root
*root
, pthread_mutex_t
*lock
,
161 unsigned long start
, unsigned long end
, unsigned batch
,
162 unsigned iftag
, unsigned thentag
)
164 unsigned long tagged
= 0;
165 struct radix_tree_iter iter
;
172 pthread_mutex_lock(lock
);
173 radix_tree_for_each_tagged(slot
, root
, &iter
, start
, iftag
) {
174 if (iter
.index
> end
)
176 radix_tree_iter_tag_set(root
, &iter
, thentag
);
178 if ((tagged
% batch
) != 0)
180 slot
= radix_tree_iter_resume(slot
, &iter
);
182 pthread_mutex_unlock(lock
);
184 pthread_mutex_lock(lock
);
188 pthread_mutex_unlock(lock
);
193 /* Use the same pattern as find_swap_entry() in mm/shmem.c */
194 unsigned long find_item(struct radix_tree_root
*root
, void *item
)
196 struct radix_tree_iter iter
;
198 unsigned long found
= -1;
199 unsigned long checked
= 0;
201 radix_tree_for_each_slot(slot
, root
, &iter
, 0) {
207 if ((checked
% 4) != 0)
209 slot
= radix_tree_iter_resume(slot
, &iter
);
215 static int verify_node(struct radix_tree_node
*slot
, unsigned int tag
,
222 slot
= entry_to_node(slot
);
224 /* Verify consistency at this level */
225 for (i
= 0; i
< RADIX_TREE_TAG_LONGS
; i
++) {
226 if (slot
->tags
[tag
][i
]) {
231 if (tagged
!= anyset
) {
232 printf("tag: %u, shift %u, tagged: %d, anyset: %d\n",
233 tag
, slot
->shift
, tagged
, anyset
);
234 for (j
= 0; j
< RADIX_TREE_MAX_TAGS
; j
++) {
235 printf("tag %d: ", j
);
236 for (i
= 0; i
< RADIX_TREE_TAG_LONGS
; i
++)
237 printf("%016lx ", slot
->tags
[j
][i
]);
242 assert(tagged
== anyset
);
244 /* Go for next level */
245 if (slot
->shift
> 0) {
246 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++)
248 if (verify_node(slot
->slots
[i
], tag
,
249 !!test_bit(i
, slot
->tags
[tag
]))) {
250 printf("Failure at off %d\n", i
);
251 for (j
= 0; j
< RADIX_TREE_MAX_TAGS
; j
++) {
252 printf("tag %d: ", j
);
253 for (i
= 0; i
< RADIX_TREE_TAG_LONGS
; i
++)
254 printf("%016lx ", slot
->tags
[j
][i
]);
263 void verify_tag_consistency(struct radix_tree_root
*root
, unsigned int tag
)
265 struct radix_tree_node
*node
= root
->rnode
;
266 if (!radix_tree_is_internal_node(node
))
268 verify_node(node
, tag
, !!root_tag_get(root
, tag
));
271 void item_kill_tree(struct radix_tree_root
*root
)
273 struct radix_tree_iter iter
;
275 struct item
*items
[32];
278 radix_tree_for_each_slot(slot
, root
, &iter
, 0) {
279 if (radix_tree_exceptional_entry(*slot
))
280 radix_tree_delete(root
, iter
.index
);
283 while ((nfound
= radix_tree_gang_lookup(root
, (void **)items
, 0, 32))) {
286 for (i
= 0; i
< nfound
; i
++) {
289 ret
= radix_tree_delete(root
, items
[i
]->index
);
290 assert(ret
== items
[i
]);
294 assert(radix_tree_gang_lookup(root
, (void **)items
, 0, 32) == 0);
295 assert(root
->rnode
== NULL
);
298 void tree_verify_min_height(struct radix_tree_root
*root
, int maxindex
)
301 struct radix_tree_node
*node
= root
->rnode
;
302 if (!radix_tree_is_internal_node(node
)) {
303 assert(maxindex
== 0);
307 node
= entry_to_node(node
);
308 assert(maxindex
<= node_maxindex(node
));
312 assert(maxindex
> shift_maxindex(shift
- RADIX_TREE_MAP_SHIFT
));
314 assert(maxindex
> 0);