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 struct item
*item_create(unsigned long index
, unsigned int order
)
30 struct item
*ret
= malloc(sizeof(*ret
));
37 int item_insert(struct radix_tree_root
*root
, unsigned long index
)
39 struct item
*item
= item_create(index
, 0);
40 int err
= radix_tree_insert(root
, item
->index
, item
);
46 void item_sanity(struct item
*item
, unsigned long index
)
49 assert(!radix_tree_is_internal_node(item
));
50 assert(item
->order
< BITS_PER_LONG
);
51 mask
= (1UL << item
->order
) - 1;
52 assert((item
->index
| mask
) == (index
| mask
));
55 void item_free(struct item
*item
, unsigned long index
)
57 item_sanity(item
, index
);
61 int item_delete(struct radix_tree_root
*root
, unsigned long index
)
63 struct item
*item
= radix_tree_delete(root
, index
);
68 item_free(item
, index
);
72 static void item_free_rcu(struct rcu_head
*head
)
74 struct item
*item
= container_of(head
, struct item
, rcu_head
);
79 int item_delete_rcu(struct xarray
*xa
, unsigned long index
)
81 struct item
*item
= xa_erase(xa
, index
);
84 item_sanity(item
, index
);
85 call_rcu(&item
->rcu_head
, item_free_rcu
);
91 void item_check_present(struct radix_tree_root
*root
, unsigned long index
)
95 item
= radix_tree_lookup(root
, index
);
97 item_sanity(item
, index
);
100 struct item
*item_lookup(struct radix_tree_root
*root
, unsigned long index
)
102 return radix_tree_lookup(root
, index
);
105 void item_check_absent(struct radix_tree_root
*root
, unsigned long index
)
109 item
= radix_tree_lookup(root
, index
);
110 assert(item
== NULL
);
114 * Scan only the passed (start, start+nr] for present items
116 void item_gang_check_present(struct radix_tree_root
*root
,
117 unsigned long start
, unsigned long nr
,
120 struct item
*items
[chunk
];
123 for (into
= 0; into
< nr
; ) {
125 int nr_to_find
= chunk
;
128 if (nr_to_find
> (nr
- into
))
129 nr_to_find
= nr
- into
;
131 nfound
= radix_tree_gang_lookup(root
, (void **)items
,
132 start
+ into
, nr_to_find
);
133 assert(nfound
== nr_to_find
);
134 for (i
= 0; i
< nfound
; i
++)
135 assert(items
[i
]->index
== start
+ into
+ i
);
141 * Scan the entire tree, only expecting present items (start, start+nr]
143 void item_full_scan(struct radix_tree_root
*root
, unsigned long start
,
144 unsigned long nr
, int chunk
)
146 struct item
*items
[chunk
];
147 unsigned long into
= 0;
148 unsigned long this_index
= start
;
152 // printf("%s(0x%08lx, 0x%08lx, %d)\n", __FUNCTION__, start, nr, chunk);
154 while ((nfound
= radix_tree_gang_lookup(root
, (void **)items
, into
,
156 // printf("At 0x%08lx, nfound=%d\n", into, nfound);
157 for (i
= 0; i
< nfound
; i
++) {
158 assert(items
[i
]->index
== this_index
);
161 // printf("Found 0x%08lx->0x%08lx\n",
162 // items[0]->index, items[nfound-1]->index);
166 assert(this_index
== start
+ nr
);
167 nfound
= radix_tree_gang_lookup(root
, (void **)items
,
172 /* Use the same pattern as tag_pages_for_writeback() in mm/page-writeback.c */
173 int tag_tagged_items(struct xarray
*xa
, unsigned long start
, unsigned long end
,
174 unsigned batch
, xa_mark_t iftag
, xa_mark_t thentag
)
176 XA_STATE(xas
, xa
, start
);
177 unsigned int tagged
= 0;
184 xas_for_each_marked(&xas
, item
, end
, iftag
) {
185 xas_set_mark(&xas
, thentag
);
186 if (++tagged
% batch
)
190 xas_unlock_irq(&xas
);
194 xas_unlock_irq(&xas
);
199 static int verify_node(struct radix_tree_node
*slot
, unsigned int tag
,
206 slot
= entry_to_node(slot
);
208 /* Verify consistency at this level */
209 for (i
= 0; i
< RADIX_TREE_TAG_LONGS
; i
++) {
210 if (slot
->tags
[tag
][i
]) {
215 if (tagged
!= anyset
) {
216 printf("tag: %u, shift %u, tagged: %d, anyset: %d\n",
217 tag
, slot
->shift
, tagged
, anyset
);
218 for (j
= 0; j
< RADIX_TREE_MAX_TAGS
; j
++) {
219 printf("tag %d: ", j
);
220 for (i
= 0; i
< RADIX_TREE_TAG_LONGS
; i
++)
221 printf("%016lx ", slot
->tags
[j
][i
]);
226 assert(tagged
== anyset
);
228 /* Go for next level */
229 if (slot
->shift
> 0) {
230 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++)
232 if (verify_node(slot
->slots
[i
], tag
,
233 !!test_bit(i
, slot
->tags
[tag
]))) {
234 printf("Failure at off %d\n", i
);
235 for (j
= 0; j
< RADIX_TREE_MAX_TAGS
; j
++) {
236 printf("tag %d: ", j
);
237 for (i
= 0; i
< RADIX_TREE_TAG_LONGS
; i
++)
238 printf("%016lx ", slot
->tags
[j
][i
]);
247 void verify_tag_consistency(struct radix_tree_root
*root
, unsigned int tag
)
249 struct radix_tree_node
*node
= root
->xa_head
;
250 if (!radix_tree_is_internal_node(node
))
252 verify_node(node
, tag
, !!root_tag_get(root
, tag
));
255 void item_kill_tree(struct xarray
*xa
)
257 XA_STATE(xas
, xa
, 0);
260 xas_for_each(&xas
, entry
, ULONG_MAX
) {
261 if (!xa_is_value(entry
)) {
262 item_free(entry
, xas
.xa_index
);
264 xas_store(&xas
, NULL
);
267 assert(xa_empty(xa
));
270 void tree_verify_min_height(struct radix_tree_root
*root
, int maxindex
)
273 struct radix_tree_node
*node
= root
->xa_head
;
274 if (!radix_tree_is_internal_node(node
)) {
275 assert(maxindex
== 0);
279 node
= entry_to_node(node
);
280 assert(maxindex
<= node_maxindex(node
));
284 assert(maxindex
> shift_maxindex(shift
- RADIX_TREE_MAP_SHIFT
));
286 assert(maxindex
> 0);