blob: def6015570b22a46274cd51b6f2135c6b6157090 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Matthew Wilcox1366c372016-03-17 14:21:45 -07002#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
11struct item *
12item_tag_set(struct radix_tree_root *root, unsigned long index, int tag)
13{
14 return radix_tree_tag_set(root, index, tag);
15}
16
17struct item *
18item_tag_clear(struct radix_tree_root *root, unsigned long index, int tag)
19{
20 return radix_tree_tag_clear(root, index, tag);
21}
22
23int 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 Wilcox101d9602016-12-14 15:08:23 -080028int __item_insert(struct radix_tree_root *root, struct item *item)
Matthew Wilcox1366c372016-03-17 14:21:45 -070029{
Matthew Wilcox101d9602016-12-14 15:08:23 -080030 return __radix_tree_insert(root, item->index, item->order, item);
Matthew Wilcox1366c372016-03-17 14:21:45 -070031}
32
Matthew Wilcox18d0c572017-01-29 01:48:34 -050033struct item *item_create(unsigned long index, unsigned int order)
Matthew Wilcox1366c372016-03-17 14:21:45 -070034{
Matthew Wilcox18d0c572017-01-29 01:48:34 -050035 struct item *ret = malloc(sizeof(*ret));
36
37 ret->index = index;
38 ret->order = order;
39 return ret;
Matthew Wilcox4f3755d2016-05-20 17:02:14 -070040}
41
42int item_insert_order(struct radix_tree_root *root, unsigned long index,
43 unsigned order)
44{
Matthew Wilcox18d0c572017-01-29 01:48:34 -050045 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
52int item_insert(struct radix_tree_root *root, unsigned long index)
53{
54 return item_insert_order(root, index, 0);
Matthew Wilcox101d9602016-12-14 15:08:23 -080055}
56
57void 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 Wilcox1366c372016-03-17 14:21:45 -070064}
65
66int 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 Wilcox101d9602016-12-14 15:08:23 -080071 item_sanity(item, index);
Matthew Wilcox1366c372016-03-17 14:21:45 -070072 free(item);
73 return 1;
74 }
75 return 0;
76}
77
Ross Zwisler3e252fa2018-05-18 16:08:58 -070078static 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
85int 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 Wilcox1366c372016-03-17 14:21:45 -070097void 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 Wilcox101d9602016-12-14 15:08:23 -0800102 assert(item != NULL);
103 item_sanity(item, index);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700104}
105
106struct item *item_lookup(struct radix_tree_root *root, unsigned long index)
107{
108 return radix_tree_lookup(root, index);
109}
110
111void 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 Wilcox101d9602016-12-14 15:08:23 -0800116 assert(item == NULL);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700117}
118
119/*
120 * Scan only the passed (start, start+nr] for present items
121 */
122void 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 */
149void 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 Wilcox268f42d2016-12-14 15:08:55 -0800178/* Use the same pattern as tag_pages_for_writeback() in mm/page-writeback.c */
179int 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 Wilcox478922e2016-12-14 15:08:52 -0800212/* Use the same pattern as find_swap_entry() in mm/shmem.c */
213unsigned 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 Wilcox1366c372016-03-17 14:21:45 -0700234static int verify_node(struct radix_tree_node *slot, unsigned int tag,
Matthew Wilcox0694f0c2016-05-20 17:03:16 -0700235 int tagged)
Matthew Wilcox1366c372016-03-17 14:21:45 -0700236{
237 int anyset = 0;
238 int i;
239 int j;
240
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700241 slot = entry_to_node(slot);
Matthew Wilcox339e6352016-03-17 14:21:48 -0700242
Matthew Wilcox1366c372016-03-17 14:21:45 -0700243 /* 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 Wilcox0694f0c2016-05-20 17:03:16 -0700251 printf("tag: %u, shift %u, tagged: %d, anyset: %d\n",
252 tag, slot->shift, tagged, anyset);
Matthew Wilcox1366c372016-03-17 14:21:45 -0700253 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 Wilcox0694f0c2016-05-20 17:03:16 -0700264 if (slot->shift > 0) {
Matthew Wilcox1366c372016-03-17 14:21:45 -0700265 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
266 if (slot->slots[i])
Matthew Wilcox0694f0c2016-05-20 17:03:16 -0700267 if (verify_node(slot->slots[i], tag,
Matthew Wilcox1366c372016-03-17 14:21:45 -0700268 !!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
282void verify_tag_consistency(struct radix_tree_root *root, unsigned int tag)
283{
Matthew Wilcox0694f0c2016-05-20 17:03:16 -0700284 struct radix_tree_node *node = root->rnode;
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700285 if (!radix_tree_is_internal_node(node))
Matthew Wilcox1366c372016-03-17 14:21:45 -0700286 return;
Matthew Wilcox0694f0c2016-05-20 17:03:16 -0700287 verify_node(node, tag, !!root_tag_get(root, tag));
Matthew Wilcox1366c372016-03-17 14:21:45 -0700288}
289
290void item_kill_tree(struct radix_tree_root *root)
291{
Matthew Wilcox3ad75f82016-12-14 15:08:20 -0800292 struct radix_tree_iter iter;
293 void **slot;
Matthew Wilcox1366c372016-03-17 14:21:45 -0700294 struct item *items[32];
295 int nfound;
296
Matthew Wilcox3ad75f82016-12-14 15:08:20 -0800297 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 Wilcox1366c372016-03-17 14:21:45 -0700302 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
317void tree_verify_min_height(struct radix_tree_root *root, int maxindex)
318{
Matthew Wilcox0694f0c2016-05-20 17:03:16 -0700319 unsigned shift;
320 struct radix_tree_node *node = root->rnode;
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700321 if (!radix_tree_is_internal_node(node)) {
Matthew Wilcox0694f0c2016-05-20 17:03:16 -0700322 assert(maxindex == 0);
323 return;
324 }
325
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700326 node = entry_to_node(node);
Matthew Wilcox0694f0c2016-05-20 17:03:16 -0700327 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 Wilcox1366c372016-03-17 14:21:45 -0700334}