]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - lib/test_xarray.c
XArray: Redesign xa_alloc API
[mirror_ubuntu-jammy-kernel.git] / lib / test_xarray.c
CommitLineData
ad3d6c72
MW
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * test_xarray.c: Test the XArray API
4 * Copyright (c) 2017-2018 Microsoft Corporation
5 * Author: Matthew Wilcox <willy@infradead.org>
6 */
7
8#include <linux/xarray.h>
9#include <linux/module.h>
10
11static unsigned int tests_run;
12static unsigned int tests_passed;
13
14#ifndef XA_DEBUG
15# ifdef __KERNEL__
16void xa_dump(const struct xarray *xa) { }
17# endif
18#undef XA_BUG_ON
19#define XA_BUG_ON(xa, x) do { \
20 tests_run++; \
21 if (x) { \
22 printk("BUG at %s:%d\n", __func__, __LINE__); \
23 xa_dump(xa); \
24 dump_stack(); \
25 } else { \
26 tests_passed++; \
27 } \
28} while (0)
29#endif
30
b7677a13
MW
31static void *xa_mk_index(unsigned long index)
32{
33 return xa_mk_value(index & LONG_MAX);
34}
35
ad3d6c72
MW
36static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp)
37{
b7677a13 38 return xa_store(xa, index, xa_mk_index(index), gfp);
ad3d6c72
MW
39}
40
371c752d
MW
41static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp)
42{
a3e4d3f9 43 u32 id;
371c752d 44
a3e4d3f9 45 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(index), xa_limit_32b,
371c752d
MW
46 gfp) != 0);
47 XA_BUG_ON(xa, id != index);
48}
49
ad3d6c72
MW
50static void xa_erase_index(struct xarray *xa, unsigned long index)
51{
b7677a13 52 XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_index(index));
58d6ea30
MW
53 XA_BUG_ON(xa, xa_load(xa, index) != NULL);
54}
55
56/*
57 * If anyone needs this, please move it to xarray.c. We have no current
58 * users outside the test suite because all current multislot users want
59 * to use the advanced API.
60 */
61static void *xa_store_order(struct xarray *xa, unsigned long index,
62 unsigned order, void *entry, gfp_t gfp)
63{
64 XA_STATE_ORDER(xas, xa, index, order);
65 void *curr;
66
67 do {
68 xas_lock(&xas);
69 curr = xas_store(&xas, entry);
70 xas_unlock(&xas);
71 } while (xas_nomem(&xas, gfp));
72
73 return curr;
74}
75
76static noinline void check_xa_err(struct xarray *xa)
77{
78 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 0, GFP_NOWAIT)) != 0);
79 XA_BUG_ON(xa, xa_err(xa_erase(xa, 0)) != 0);
80#ifndef __KERNEL__
81 /* The kernel does not fail GFP_NOWAIT allocations */
82 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM);
83 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM);
84#endif
85 XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_KERNEL)) != 0);
86 XA_BUG_ON(xa, xa_err(xa_store(xa, 1, xa_mk_value(0), GFP_KERNEL)) != 0);
87 XA_BUG_ON(xa, xa_err(xa_erase(xa, 1)) != 0);
88// kills the test-suite :-(
89// XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL);
ad3d6c72
MW
90}
91
b803b428
MW
92static noinline void check_xas_retry(struct xarray *xa)
93{
94 XA_STATE(xas, xa, 0);
95 void *entry;
96
97 xa_store_index(xa, 0, GFP_KERNEL);
98 xa_store_index(xa, 1, GFP_KERNEL);
99
100 rcu_read_lock();
101 XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_value(0));
102 xa_erase_index(xa, 1);
103 XA_BUG_ON(xa, !xa_is_retry(xas_reload(&xas)));
104 XA_BUG_ON(xa, xas_retry(&xas, NULL));
105 XA_BUG_ON(xa, xas_retry(&xas, xa_mk_value(0)));
106 xas_reset(&xas);
107 XA_BUG_ON(xa, xas.xa_node != XAS_RESTART);
108 XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0));
109 XA_BUG_ON(xa, xas.xa_node != NULL);
bd54211b 110 rcu_read_unlock();
b803b428
MW
111
112 XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
bd54211b
MW
113
114 rcu_read_lock();
b803b428
MW
115 XA_BUG_ON(xa, !xa_is_internal(xas_reload(&xas)));
116 xas.xa_node = XAS_RESTART;
117 XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0));
118 rcu_read_unlock();
119
120 /* Make sure we can iterate through retry entries */
121 xas_lock(&xas);
122 xas_set(&xas, 0);
123 xas_store(&xas, XA_RETRY_ENTRY);
124 xas_set(&xas, 1);
125 xas_store(&xas, XA_RETRY_ENTRY);
126
127 xas_set(&xas, 0);
128 xas_for_each(&xas, entry, ULONG_MAX) {
b7677a13 129 xas_store(&xas, xa_mk_index(xas.xa_index));
b803b428
MW
130 }
131 xas_unlock(&xas);
132
133 xa_erase_index(xa, 0);
134 xa_erase_index(xa, 1);
135}
136
ad3d6c72
MW
137static noinline void check_xa_load(struct xarray *xa)
138{
139 unsigned long i, j;
140
141 for (i = 0; i < 1024; i++) {
142 for (j = 0; j < 1024; j++) {
143 void *entry = xa_load(xa, j);
144 if (j < i)
145 XA_BUG_ON(xa, xa_to_value(entry) != j);
146 else
147 XA_BUG_ON(xa, entry);
148 }
149 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
150 }
151
152 for (i = 0; i < 1024; i++) {
153 for (j = 0; j < 1024; j++) {
154 void *entry = xa_load(xa, j);
155 if (j >= i)
156 XA_BUG_ON(xa, xa_to_value(entry) != j);
157 else
158 XA_BUG_ON(xa, entry);
159 }
160 xa_erase_index(xa, i);
161 }
162 XA_BUG_ON(xa, !xa_empty(xa));
163}
164
9b89a035
MW
165static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index)
166{
58d6ea30
MW
167 unsigned int order;
168 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1;
169
9b89a035
MW
170 /* NULL elements have no marks set */
171 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
172 xa_set_mark(xa, index, XA_MARK_0);
173 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
174
175 /* Storing a pointer will not make a mark appear */
176 XA_BUG_ON(xa, xa_store_index(xa, index, GFP_KERNEL) != NULL);
177 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
178 xa_set_mark(xa, index, XA_MARK_0);
179 XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0));
180
181 /* Setting one mark will not set another mark */
182 XA_BUG_ON(xa, xa_get_mark(xa, index + 1, XA_MARK_0));
183 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_1));
184
185 /* Storing NULL clears marks, and they can't be set again */
186 xa_erase_index(xa, index);
187 XA_BUG_ON(xa, !xa_empty(xa));
188 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
189 xa_set_mark(xa, index, XA_MARK_0);
190 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
58d6ea30
MW
191
192 /*
193 * Storing a multi-index entry over entries with marks gives the
194 * entire entry the union of the marks
195 */
196 BUG_ON((index % 4) != 0);
197 for (order = 2; order < max_order; order++) {
198 unsigned long base = round_down(index, 1UL << order);
199 unsigned long next = base + (1UL << order);
200 unsigned long i;
201
202 XA_BUG_ON(xa, xa_store_index(xa, index + 1, GFP_KERNEL));
203 xa_set_mark(xa, index + 1, XA_MARK_0);
204 XA_BUG_ON(xa, xa_store_index(xa, index + 2, GFP_KERNEL));
d69d287a 205 xa_set_mark(xa, index + 2, XA_MARK_2);
58d6ea30 206 XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL));
b7677a13 207 xa_store_order(xa, index, order, xa_mk_index(index),
58d6ea30
MW
208 GFP_KERNEL);
209 for (i = base; i < next; i++) {
93eb07f7
MW
210 XA_STATE(xas, xa, i);
211 unsigned int seen = 0;
212 void *entry;
213
58d6ea30 214 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
d69d287a
MW
215 XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_1));
216 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_2));
93eb07f7
MW
217
218 /* We should see two elements in the array */
fffc9a26 219 rcu_read_lock();
93eb07f7
MW
220 xas_for_each(&xas, entry, ULONG_MAX)
221 seen++;
fffc9a26 222 rcu_read_unlock();
93eb07f7
MW
223 XA_BUG_ON(xa, seen != 2);
224
225 /* One of which is marked */
226 xas_set(&xas, 0);
227 seen = 0;
fffc9a26 228 rcu_read_lock();
93eb07f7
MW
229 xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
230 seen++;
fffc9a26 231 rcu_read_unlock();
93eb07f7 232 XA_BUG_ON(xa, seen != 1);
58d6ea30
MW
233 }
234 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_0));
235 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_1));
236 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_2));
237 xa_erase_index(xa, index);
238 xa_erase_index(xa, next);
239 XA_BUG_ON(xa, !xa_empty(xa));
240 }
241 XA_BUG_ON(xa, !xa_empty(xa));
9b89a035
MW
242}
243
adb9d9c4
MW
244static noinline void check_xa_mark_2(struct xarray *xa)
245{
246 XA_STATE(xas, xa, 0);
247 unsigned long index;
248 unsigned int count = 0;
249 void *entry;
250
251 xa_store_index(xa, 0, GFP_KERNEL);
252 xa_set_mark(xa, 0, XA_MARK_0);
253 xas_lock(&xas);
254 xas_load(&xas);
255 xas_init_marks(&xas);
256 xas_unlock(&xas);
257 XA_BUG_ON(xa, !xa_get_mark(xa, 0, XA_MARK_0) == 0);
258
259 for (index = 3500; index < 4500; index++) {
260 xa_store_index(xa, index, GFP_KERNEL);
261 xa_set_mark(xa, index, XA_MARK_0);
262 }
263
264 xas_reset(&xas);
265 rcu_read_lock();
266 xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
267 count++;
268 rcu_read_unlock();
269 XA_BUG_ON(xa, count != 1000);
270
271 xas_lock(&xas);
272 xas_for_each(&xas, entry, ULONG_MAX) {
273 xas_init_marks(&xas);
274 XA_BUG_ON(xa, !xa_get_mark(xa, xas.xa_index, XA_MARK_0));
275 XA_BUG_ON(xa, !xas_get_mark(&xas, XA_MARK_0));
276 }
277 xas_unlock(&xas);
278
279 xa_destroy(xa);
280}
281
9b89a035
MW
282static noinline void check_xa_mark(struct xarray *xa)
283{
284 unsigned long index;
285
286 for (index = 0; index < 16384; index += 4)
287 check_xa_mark_1(xa, index);
adb9d9c4
MW
288
289 check_xa_mark_2(xa);
9b89a035
MW
290}
291
58d6ea30
MW
292static noinline void check_xa_shrink(struct xarray *xa)
293{
294 XA_STATE(xas, xa, 1);
295 struct xa_node *node;
93eb07f7
MW
296 unsigned int order;
297 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1;
58d6ea30
MW
298
299 XA_BUG_ON(xa, !xa_empty(xa));
300 XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL);
301 XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
302
303 /*
304 * Check that erasing the entry at 1 shrinks the tree and properly
305 * marks the node as being deleted.
306 */
307 xas_lock(&xas);
308 XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1));
309 node = xas.xa_node;
310 XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0));
311 XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
312 XA_BUG_ON(xa, xa_load(xa, 1) != NULL);
313 XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS);
314 XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY);
315 XA_BUG_ON(xa, xas_load(&xas) != NULL);
316 xas_unlock(&xas);
317 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
318 xa_erase_index(xa, 0);
319 XA_BUG_ON(xa, !xa_empty(xa));
93eb07f7
MW
320
321 for (order = 0; order < max_order; order++) {
322 unsigned long max = (1UL << order) - 1;
323 xa_store_order(xa, 0, order, xa_mk_value(0), GFP_KERNEL);
324 XA_BUG_ON(xa, xa_load(xa, max) != xa_mk_value(0));
325 XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
326 rcu_read_lock();
327 node = xa_head(xa);
328 rcu_read_unlock();
329 XA_BUG_ON(xa, xa_store_index(xa, ULONG_MAX, GFP_KERNEL) !=
330 NULL);
331 rcu_read_lock();
332 XA_BUG_ON(xa, xa_head(xa) == node);
333 rcu_read_unlock();
334 XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
335 xa_erase_index(xa, ULONG_MAX);
336 XA_BUG_ON(xa, xa->xa_head != node);
337 xa_erase_index(xa, 0);
338 }
58d6ea30
MW
339}
340
41aec91f
MW
341static noinline void check_cmpxchg(struct xarray *xa)
342{
343 void *FIVE = xa_mk_value(5);
344 void *SIX = xa_mk_value(6);
345 void *LOTS = xa_mk_value(12345678);
346
347 XA_BUG_ON(xa, !xa_empty(xa));
348 XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_KERNEL) != NULL);
fd9dc93e 349 XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EBUSY);
41aec91f
MW
350 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, SIX, FIVE, GFP_KERNEL) != LOTS);
351 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, LOTS, FIVE, GFP_KERNEL) != LOTS);
352 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, FIVE, LOTS, GFP_KERNEL) != FIVE);
353 XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != NULL);
354 XA_BUG_ON(xa, xa_cmpxchg(xa, 5, NULL, FIVE, GFP_KERNEL) != NULL);
355 xa_erase_index(xa, 12345678);
356 xa_erase_index(xa, 5);
357 XA_BUG_ON(xa, !xa_empty(xa));
358}
359
9f14d4f1
MW
360static noinline void check_reserve(struct xarray *xa)
361{
362 void *entry;
4a31896c 363 unsigned long index;
9f14d4f1
MW
364
365 /* An array with a reserved entry is not empty */
366 XA_BUG_ON(xa, !xa_empty(xa));
367 xa_reserve(xa, 12345678, GFP_KERNEL);
368 XA_BUG_ON(xa, xa_empty(xa));
369 XA_BUG_ON(xa, xa_load(xa, 12345678));
370 xa_release(xa, 12345678);
371 XA_BUG_ON(xa, !xa_empty(xa));
372
373 /* Releasing a used entry does nothing */
374 xa_reserve(xa, 12345678, GFP_KERNEL);
375 XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_NOWAIT) != NULL);
376 xa_release(xa, 12345678);
377 xa_erase_index(xa, 12345678);
378 XA_BUG_ON(xa, !xa_empty(xa));
379
380 /* cmpxchg sees a reserved entry as NULL */
381 xa_reserve(xa, 12345678, GFP_KERNEL);
382 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, NULL, xa_mk_value(12345678),
383 GFP_NOWAIT) != NULL);
384 xa_release(xa, 12345678);
385 xa_erase_index(xa, 12345678);
386 XA_BUG_ON(xa, !xa_empty(xa));
387
b0606fed 388 /* But xa_insert does not */
4c0608f4 389 xa_reserve(xa, 12345678, GFP_KERNEL);
b0606fed 390 XA_BUG_ON(xa, xa_insert(xa, 12345678, xa_mk_value(12345678), 0) !=
fd9dc93e 391 -EBUSY);
b0606fed
MW
392 XA_BUG_ON(xa, xa_empty(xa));
393 XA_BUG_ON(xa, xa_erase(xa, 12345678) != NULL);
4c0608f4
MW
394 XA_BUG_ON(xa, !xa_empty(xa));
395
9f14d4f1
MW
396 /* Can iterate through a reserved entry */
397 xa_store_index(xa, 5, GFP_KERNEL);
398 xa_reserve(xa, 6, GFP_KERNEL);
399 xa_store_index(xa, 7, GFP_KERNEL);
400
4a31896c 401 xa_for_each(xa, index, entry) {
9f14d4f1
MW
402 XA_BUG_ON(xa, index != 5 && index != 7);
403 }
404 xa_destroy(xa);
405}
406
b803b428
MW
407static noinline void check_xas_erase(struct xarray *xa)
408{
409 XA_STATE(xas, xa, 0);
410 void *entry;
411 unsigned long i, j;
412
413 for (i = 0; i < 200; i++) {
414 for (j = i; j < 2 * i + 17; j++) {
415 xas_set(&xas, j);
416 do {
417 xas_lock(&xas);
b7677a13 418 xas_store(&xas, xa_mk_index(j));
b803b428
MW
419 xas_unlock(&xas);
420 } while (xas_nomem(&xas, GFP_KERNEL));
421 }
422
423 xas_set(&xas, ULONG_MAX);
424 do {
425 xas_lock(&xas);
426 xas_store(&xas, xa_mk_value(0));
427 xas_unlock(&xas);
428 } while (xas_nomem(&xas, GFP_KERNEL));
429
430 xas_lock(&xas);
431 xas_store(&xas, NULL);
432
433 xas_set(&xas, 0);
434 j = i;
435 xas_for_each(&xas, entry, ULONG_MAX) {
b7677a13 436 XA_BUG_ON(xa, entry != xa_mk_index(j));
b803b428
MW
437 xas_store(&xas, NULL);
438 j++;
439 }
440 xas_unlock(&xas);
441 XA_BUG_ON(xa, !xa_empty(xa));
442 }
443}
444
4f06d630
MW
445#ifdef CONFIG_XARRAY_MULTI
446static noinline void check_multi_store_1(struct xarray *xa, unsigned long index,
447 unsigned int order)
448{
449 XA_STATE(xas, xa, index);
450 unsigned long min = index & ~((1UL << order) - 1);
451 unsigned long max = min + (1UL << order);
452
b7677a13
MW
453 xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
454 XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(index));
455 XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(index));
4f06d630
MW
456 XA_BUG_ON(xa, xa_load(xa, max) != NULL);
457 XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
458
fffc9a26 459 xas_lock(&xas);
b7677a13 460 XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(min)) != xa_mk_index(index));
fffc9a26 461 xas_unlock(&xas);
b7677a13
MW
462 XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(min));
463 XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(min));
4f06d630
MW
464 XA_BUG_ON(xa, xa_load(xa, max) != NULL);
465 XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
466
467 xa_erase_index(xa, min);
468 XA_BUG_ON(xa, !xa_empty(xa));
469}
470
471static noinline void check_multi_store_2(struct xarray *xa, unsigned long index,
472 unsigned int order)
473{
474 XA_STATE(xas, xa, index);
475 xa_store_order(xa, index, order, xa_mk_value(0), GFP_KERNEL);
476
fffc9a26 477 xas_lock(&xas);
4f06d630
MW
478 XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(1)) != xa_mk_value(0));
479 XA_BUG_ON(xa, xas.xa_index != index);
480 XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
fffc9a26 481 xas_unlock(&xas);
4f06d630
MW
482 XA_BUG_ON(xa, !xa_empty(xa));
483}
4f145cd6
MW
484
485static noinline void check_multi_store_3(struct xarray *xa, unsigned long index,
486 unsigned int order)
487{
488 XA_STATE(xas, xa, 0);
489 void *entry;
490 int n = 0;
491
492 xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
493
494 xas_lock(&xas);
495 xas_for_each(&xas, entry, ULONG_MAX) {
496 XA_BUG_ON(xa, entry != xa_mk_index(index));
497 n++;
498 }
499 XA_BUG_ON(xa, n != 1);
500 xas_set(&xas, index + 1);
501 xas_for_each(&xas, entry, ULONG_MAX) {
502 XA_BUG_ON(xa, entry != xa_mk_index(index));
503 n++;
504 }
505 XA_BUG_ON(xa, n != 2);
506 xas_unlock(&xas);
507
508 xa_destroy(xa);
509}
4f06d630
MW
510#endif
511
58d6ea30
MW
512static noinline void check_multi_store(struct xarray *xa)
513{
514#ifdef CONFIG_XARRAY_MULTI
515 unsigned long i, j, k;
516 unsigned int max_order = (sizeof(long) == 4) ? 30 : 60;
517
518 /* Loading from any position returns the same value */
519 xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL);
520 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
521 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
522 XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
523 rcu_read_lock();
524 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2);
525 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
526 rcu_read_unlock();
527
528 /* Storing adjacent to the value does not alter the value */
529 xa_store(xa, 3, xa, GFP_KERNEL);
530 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
531 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
532 XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
533 rcu_read_lock();
534 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3);
535 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
536 rcu_read_unlock();
537
538 /* Overwriting multiple indexes works */
539 xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL);
540 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1));
541 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1));
542 XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1));
543 XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1));
544 XA_BUG_ON(xa, xa_load(xa, 4) != NULL);
545 rcu_read_lock();
546 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4);
547 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4);
548 rcu_read_unlock();
549
550 /* We can erase multiple values with a single store */
5404a7f1 551 xa_store_order(xa, 0, BITS_PER_LONG - 1, NULL, GFP_KERNEL);
58d6ea30
MW
552 XA_BUG_ON(xa, !xa_empty(xa));
553
554 /* Even when the first slot is empty but the others aren't */
555 xa_store_index(xa, 1, GFP_KERNEL);
556 xa_store_index(xa, 2, GFP_KERNEL);
557 xa_store_order(xa, 0, 2, NULL, GFP_KERNEL);
558 XA_BUG_ON(xa, !xa_empty(xa));
559
560 for (i = 0; i < max_order; i++) {
561 for (j = 0; j < max_order; j++) {
b7677a13
MW
562 xa_store_order(xa, 0, i, xa_mk_index(i), GFP_KERNEL);
563 xa_store_order(xa, 0, j, xa_mk_index(j), GFP_KERNEL);
58d6ea30
MW
564
565 for (k = 0; k < max_order; k++) {
566 void *entry = xa_load(xa, (1UL << k) - 1);
567 if ((i < k) && (j < k))
568 XA_BUG_ON(xa, entry != NULL);
569 else
b7677a13 570 XA_BUG_ON(xa, entry != xa_mk_index(j));
58d6ea30
MW
571 }
572
573 xa_erase(xa, 0);
574 XA_BUG_ON(xa, !xa_empty(xa));
575 }
576 }
4f06d630
MW
577
578 for (i = 0; i < 20; i++) {
579 check_multi_store_1(xa, 200, i);
580 check_multi_store_1(xa, 0, i);
581 check_multi_store_1(xa, (1UL << i) + 1, i);
582 }
583 check_multi_store_2(xa, 4095, 9);
4f145cd6
MW
584
585 for (i = 1; i < 20; i++) {
586 check_multi_store_3(xa, 0, i);
587 check_multi_store_3(xa, 1UL << i, i);
588 }
58d6ea30
MW
589#endif
590}
591
3ccaf57a 592static noinline void check_xa_alloc_1(struct xarray *xa, unsigned int base)
371c752d
MW
593{
594 int i;
595 u32 id;
596
3ccaf57a
MW
597 XA_BUG_ON(xa, !xa_empty(xa));
598 /* An empty array should assign %base to the first alloc */
599 xa_alloc_index(xa, base, GFP_KERNEL);
371c752d
MW
600
601 /* Erasing it should make the array empty again */
3ccaf57a
MW
602 xa_erase_index(xa, base);
603 XA_BUG_ON(xa, !xa_empty(xa));
604
605 /* And it should assign %base again */
606 xa_alloc_index(xa, base, GFP_KERNEL);
607
608 /* Allocating and then erasing a lot should not lose base */
609 for (i = base + 1; i < 2 * XA_CHUNK_SIZE; i++)
610 xa_alloc_index(xa, i, GFP_KERNEL);
611 for (i = base; i < 2 * XA_CHUNK_SIZE; i++)
612 xa_erase_index(xa, i);
613 xa_alloc_index(xa, base, GFP_KERNEL);
614
615 /* Destroying the array should do the same as erasing */
616 xa_destroy(xa);
371c752d 617
3ccaf57a
MW
618 /* And it should assign %base again */
619 xa_alloc_index(xa, base, GFP_KERNEL);
371c752d 620
3ccaf57a
MW
621 /* The next assigned ID should be base+1 */
622 xa_alloc_index(xa, base + 1, GFP_KERNEL);
623 xa_erase_index(xa, base + 1);
371c752d
MW
624
625 /* Storing a value should mark it used */
3ccaf57a
MW
626 xa_store_index(xa, base + 1, GFP_KERNEL);
627 xa_alloc_index(xa, base + 2, GFP_KERNEL);
371c752d 628
3ccaf57a
MW
629 /* If we then erase base, it should be free */
630 xa_erase_index(xa, base);
631 xa_alloc_index(xa, base, GFP_KERNEL);
371c752d 632
3ccaf57a
MW
633 xa_erase_index(xa, base + 1);
634 xa_erase_index(xa, base + 2);
371c752d
MW
635
636 for (i = 1; i < 5000; i++) {
3ccaf57a 637 xa_alloc_index(xa, base + i, GFP_KERNEL);
371c752d
MW
638 }
639
3ccaf57a 640 xa_destroy(xa);
371c752d 641
3ccaf57a 642 /* Check that we fail properly at the limit of allocation */
a3e4d3f9
MW
643 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX - 1),
644 XA_LIMIT(UINT_MAX - 1, UINT_MAX),
371c752d 645 GFP_KERNEL) != 0);
3ccaf57a 646 XA_BUG_ON(xa, id != 0xfffffffeU);
a3e4d3f9
MW
647 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(UINT_MAX),
648 XA_LIMIT(UINT_MAX - 1, UINT_MAX),
371c752d 649 GFP_KERNEL) != 0);
3ccaf57a 650 XA_BUG_ON(xa, id != 0xffffffffU);
a3e4d3f9
MW
651 id = 3;
652 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(0),
653 XA_LIMIT(UINT_MAX - 1, UINT_MAX),
654 GFP_KERNEL) != -EBUSY);
655 XA_BUG_ON(xa, id != 3);
3ccaf57a 656 xa_destroy(xa);
48483614 657
a3e4d3f9
MW
658 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5),
659 GFP_KERNEL) != -EBUSY);
3ccaf57a 660 XA_BUG_ON(xa, xa_store_index(xa, 3, GFP_KERNEL) != 0);
a3e4d3f9
MW
661 XA_BUG_ON(xa, xa_alloc(xa, &id, xa_mk_index(10), XA_LIMIT(10, 5),
662 GFP_KERNEL) != -EBUSY);
3ccaf57a
MW
663 xa_erase_index(xa, 3);
664 XA_BUG_ON(xa, !xa_empty(xa));
665}
666
a3e4d3f9
MW
667static noinline void check_xa_alloc_2(struct xarray *xa, unsigned int base)
668{
669 unsigned int i, id;
670 unsigned long index;
671 void *entry;
672
673 /* Allocate and free a NULL and check xa_empty() behaves */
674 XA_BUG_ON(xa, !xa_empty(xa));
675 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
676 XA_BUG_ON(xa, id != base);
677 XA_BUG_ON(xa, xa_empty(xa));
678 XA_BUG_ON(xa, xa_erase(xa, id) != NULL);
679 XA_BUG_ON(xa, !xa_empty(xa));
680
681 /* Ditto, but check destroy instead of erase */
682 XA_BUG_ON(xa, !xa_empty(xa));
683 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
684 XA_BUG_ON(xa, id != base);
685 XA_BUG_ON(xa, xa_empty(xa));
686 xa_destroy(xa);
687 XA_BUG_ON(xa, !xa_empty(xa));
688
689 for (i = base; i < base + 10; i++) {
690 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b,
691 GFP_KERNEL) != 0);
692 XA_BUG_ON(xa, id != i);
693 }
694
695 XA_BUG_ON(xa, xa_store(xa, 3, xa_mk_index(3), GFP_KERNEL) != NULL);
696 XA_BUG_ON(xa, xa_store(xa, 4, xa_mk_index(4), GFP_KERNEL) != NULL);
697 XA_BUG_ON(xa, xa_store(xa, 4, NULL, GFP_KERNEL) != xa_mk_index(4));
698 XA_BUG_ON(xa, xa_erase(xa, 5) != NULL);
699 XA_BUG_ON(xa, xa_alloc(xa, &id, NULL, xa_limit_32b, GFP_KERNEL) != 0);
700 XA_BUG_ON(xa, id != 5);
701
702 xa_for_each(xa, index, entry) {
703 xa_erase_index(xa, index);
704 }
705
706 for (i = base; i < base + 9; i++) {
707 XA_BUG_ON(xa, xa_erase(xa, i) != NULL);
708 XA_BUG_ON(xa, xa_empty(xa));
709 }
710 XA_BUG_ON(xa, xa_erase(xa, 8) != NULL);
711 XA_BUG_ON(xa, xa_empty(xa));
712 XA_BUG_ON(xa, xa_erase(xa, base + 9) != NULL);
713 XA_BUG_ON(xa, !xa_empty(xa));
714
715 xa_destroy(xa);
716}
717
3ccaf57a
MW
718static DEFINE_XARRAY_ALLOC(xa0);
719static DEFINE_XARRAY_ALLOC1(xa1);
720
721static noinline void check_xa_alloc(void)
722{
723 check_xa_alloc_1(&xa0, 0);
724 check_xa_alloc_1(&xa1, 1);
a3e4d3f9
MW
725 check_xa_alloc_2(&xa0, 0);
726 check_xa_alloc_2(&xa1, 1);
371c752d
MW
727}
728
4e99d4e9
MW
729static noinline void __check_store_iter(struct xarray *xa, unsigned long start,
730 unsigned int order, unsigned int present)
731{
732 XA_STATE_ORDER(xas, xa, start, order);
733 void *entry;
734 unsigned int count = 0;
735
736retry:
737 xas_lock(&xas);
738 xas_for_each_conflict(&xas, entry) {
739 XA_BUG_ON(xa, !xa_is_value(entry));
b7677a13
MW
740 XA_BUG_ON(xa, entry < xa_mk_index(start));
741 XA_BUG_ON(xa, entry > xa_mk_index(start + (1UL << order) - 1));
4e99d4e9
MW
742 count++;
743 }
b7677a13 744 xas_store(&xas, xa_mk_index(start));
4e99d4e9
MW
745 xas_unlock(&xas);
746 if (xas_nomem(&xas, GFP_KERNEL)) {
747 count = 0;
748 goto retry;
749 }
750 XA_BUG_ON(xa, xas_error(&xas));
751 XA_BUG_ON(xa, count != present);
b7677a13 752 XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_index(start));
4e99d4e9 753 XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) !=
b7677a13 754 xa_mk_index(start));
4e99d4e9
MW
755 xa_erase_index(xa, start);
756}
757
758static noinline void check_store_iter(struct xarray *xa)
759{
760 unsigned int i, j;
761 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
762
763 for (i = 0; i < max_order; i++) {
764 unsigned int min = 1 << i;
765 unsigned int max = (2 << i) - 1;
766 __check_store_iter(xa, 0, i, 0);
767 XA_BUG_ON(xa, !xa_empty(xa));
768 __check_store_iter(xa, min, i, 0);
769 XA_BUG_ON(xa, !xa_empty(xa));
770
771 xa_store_index(xa, min, GFP_KERNEL);
772 __check_store_iter(xa, min, i, 1);
773 XA_BUG_ON(xa, !xa_empty(xa));
774 xa_store_index(xa, max, GFP_KERNEL);
775 __check_store_iter(xa, min, i, 1);
776 XA_BUG_ON(xa, !xa_empty(xa));
777
778 for (j = 0; j < min; j++)
779 xa_store_index(xa, j, GFP_KERNEL);
780 __check_store_iter(xa, 0, i, min);
781 XA_BUG_ON(xa, !xa_empty(xa));
782 for (j = 0; j < min; j++)
783 xa_store_index(xa, min + j, GFP_KERNEL);
784 __check_store_iter(xa, min, i, min);
785 XA_BUG_ON(xa, !xa_empty(xa));
786 }
787#ifdef CONFIG_XARRAY_MULTI
788 xa_store_index(xa, 63, GFP_KERNEL);
789 xa_store_index(xa, 65, GFP_KERNEL);
790 __check_store_iter(xa, 64, 2, 1);
791 xa_erase_index(xa, 63);
792#endif
793 XA_BUG_ON(xa, !xa_empty(xa));
794}
795
b803b428
MW
796static noinline void check_multi_find(struct xarray *xa)
797{
798#ifdef CONFIG_XARRAY_MULTI
799 unsigned long index;
800
801 xa_store_order(xa, 12, 2, xa_mk_value(12), GFP_KERNEL);
802 XA_BUG_ON(xa, xa_store_index(xa, 16, GFP_KERNEL) != NULL);
803
804 index = 0;
805 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
806 xa_mk_value(12));
807 XA_BUG_ON(xa, index != 12);
808 index = 13;
809 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
810 xa_mk_value(12));
811 XA_BUG_ON(xa, (index < 12) || (index >= 16));
812 XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
813 xa_mk_value(16));
814 XA_BUG_ON(xa, index != 16);
815
816 xa_erase_index(xa, 12);
817 xa_erase_index(xa, 16);
818 XA_BUG_ON(xa, !xa_empty(xa));
819#endif
820}
821
822static noinline void check_multi_find_2(struct xarray *xa)
823{
824 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1;
825 unsigned int i, j;
826 void *entry;
827
828 for (i = 0; i < max_order; i++) {
829 unsigned long index = 1UL << i;
830 for (j = 0; j < index; j++) {
831 XA_STATE(xas, xa, j + index);
832 xa_store_index(xa, index - 1, GFP_KERNEL);
b7677a13 833 xa_store_order(xa, index, i, xa_mk_index(index),
b803b428
MW
834 GFP_KERNEL);
835 rcu_read_lock();
836 xas_for_each(&xas, entry, ULONG_MAX) {
837 xa_erase_index(xa, index);
838 }
839 rcu_read_unlock();
840 xa_erase_index(xa, index - 1);
841 XA_BUG_ON(xa, !xa_empty(xa));
842 }
843 }
844}
845
8229706e 846static noinline void check_find_1(struct xarray *xa)
b803b428
MW
847{
848 unsigned long i, j, k;
849
850 XA_BUG_ON(xa, !xa_empty(xa));
851
852 /*
853 * Check xa_find with all pairs between 0 and 99 inclusive,
854 * starting at every index between 0 and 99
855 */
856 for (i = 0; i < 100; i++) {
857 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
858 xa_set_mark(xa, i, XA_MARK_0);
859 for (j = 0; j < i; j++) {
860 XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) !=
861 NULL);
862 xa_set_mark(xa, j, XA_MARK_0);
863 for (k = 0; k < 100; k++) {
864 unsigned long index = k;
865 void *entry = xa_find(xa, &index, ULONG_MAX,
866 XA_PRESENT);
867 if (k <= j)
868 XA_BUG_ON(xa, index != j);
869 else if (k <= i)
870 XA_BUG_ON(xa, index != i);
871 else
872 XA_BUG_ON(xa, entry != NULL);
873
874 index = k;
875 entry = xa_find(xa, &index, ULONG_MAX,
876 XA_MARK_0);
877 if (k <= j)
878 XA_BUG_ON(xa, index != j);
879 else if (k <= i)
880 XA_BUG_ON(xa, index != i);
881 else
882 XA_BUG_ON(xa, entry != NULL);
883 }
884 xa_erase_index(xa, j);
885 XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0));
886 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
887 }
888 xa_erase_index(xa, i);
889 XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0));
890 }
891 XA_BUG_ON(xa, !xa_empty(xa));
8229706e
MW
892}
893
894static noinline void check_find_2(struct xarray *xa)
895{
896 void *entry;
4a31896c 897 unsigned long i, j, index;
8229706e 898
4a31896c 899 xa_for_each(xa, index, entry) {
8229706e
MW
900 XA_BUG_ON(xa, true);
901 }
902
903 for (i = 0; i < 1024; i++) {
904 xa_store_index(xa, index, GFP_KERNEL);
905 j = 0;
4a31896c 906 xa_for_each(xa, index, entry) {
b7677a13 907 XA_BUG_ON(xa, xa_mk_index(index) != entry);
8229706e
MW
908 XA_BUG_ON(xa, index != j++);
909 }
910 }
911
912 xa_destroy(xa);
913}
914
48483614
MW
915static noinline void check_find_3(struct xarray *xa)
916{
917 XA_STATE(xas, xa, 0);
918 unsigned long i, j, k;
919 void *entry;
920
921 for (i = 0; i < 100; i++) {
922 for (j = 0; j < 100; j++) {
490fd30f 923 rcu_read_lock();
48483614
MW
924 for (k = 0; k < 100; k++) {
925 xas_set(&xas, j);
926 xas_for_each_marked(&xas, entry, k, XA_MARK_0)
927 ;
928 if (j > k)
929 XA_BUG_ON(xa,
930 xas.xa_node != XAS_RESTART);
931 }
490fd30f 932 rcu_read_unlock();
48483614
MW
933 }
934 xa_store_index(xa, i, GFP_KERNEL);
935 xa_set_mark(xa, i, XA_MARK_0);
936 }
937 xa_destroy(xa);
938}
939
8229706e
MW
940static noinline void check_find(struct xarray *xa)
941{
942 check_find_1(xa);
943 check_find_2(xa);
48483614 944 check_find_3(xa);
b803b428
MW
945 check_multi_find(xa);
946 check_multi_find_2(xa);
947}
948
e21a2955
MW
949/* See find_swap_entry() in mm/shmem.c */
950static noinline unsigned long xa_find_entry(struct xarray *xa, void *item)
951{
952 XA_STATE(xas, xa, 0);
953 unsigned int checked = 0;
954 void *entry;
955
956 rcu_read_lock();
957 xas_for_each(&xas, entry, ULONG_MAX) {
958 if (xas_retry(&xas, entry))
959 continue;
960 if (entry == item)
961 break;
962 checked++;
963 if ((checked % 4) != 0)
964 continue;
965 xas_pause(&xas);
966 }
967 rcu_read_unlock();
968
969 return entry ? xas.xa_index : -1;
970}
971
972static noinline void check_find_entry(struct xarray *xa)
973{
974#ifdef CONFIG_XARRAY_MULTI
975 unsigned int order;
976 unsigned long offset, index;
977
978 for (order = 0; order < 20; order++) {
979 for (offset = 0; offset < (1UL << (order + 3));
980 offset += (1UL << order)) {
981 for (index = 0; index < (1UL << (order + 5));
982 index += (1UL << order)) {
983 xa_store_order(xa, index, order,
b7677a13 984 xa_mk_index(index), GFP_KERNEL);
e21a2955 985 XA_BUG_ON(xa, xa_load(xa, index) !=
b7677a13 986 xa_mk_index(index));
e21a2955 987 XA_BUG_ON(xa, xa_find_entry(xa,
b7677a13 988 xa_mk_index(index)) != index);
e21a2955
MW
989 }
990 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
991 xa_destroy(xa);
992 }
993 }
994#endif
995
996 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
997 xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
998 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
b7677a13 999 XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_index(ULONG_MAX)) != -1);
e21a2955
MW
1000 xa_erase_index(xa, ULONG_MAX);
1001 XA_BUG_ON(xa, !xa_empty(xa));
1002}
1003
64d3e9a9
MW
1004static noinline void check_move_small(struct xarray *xa, unsigned long idx)
1005{
1006 XA_STATE(xas, xa, 0);
1007 unsigned long i;
1008
1009 xa_store_index(xa, 0, GFP_KERNEL);
1010 xa_store_index(xa, idx, GFP_KERNEL);
1011
1012 rcu_read_lock();
1013 for (i = 0; i < idx * 4; i++) {
1014 void *entry = xas_next(&xas);
1015 if (i <= idx)
1016 XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
1017 XA_BUG_ON(xa, xas.xa_index != i);
1018 if (i == 0 || i == idx)
b7677a13 1019 XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9
MW
1020 else
1021 XA_BUG_ON(xa, entry != NULL);
1022 }
1023 xas_next(&xas);
1024 XA_BUG_ON(xa, xas.xa_index != i);
1025
1026 do {
1027 void *entry = xas_prev(&xas);
1028 i--;
1029 if (i <= idx)
1030 XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
1031 XA_BUG_ON(xa, xas.xa_index != i);
1032 if (i == 0 || i == idx)
b7677a13 1033 XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9
MW
1034 else
1035 XA_BUG_ON(xa, entry != NULL);
1036 } while (i > 0);
1037
1038 xas_set(&xas, ULONG_MAX);
1039 XA_BUG_ON(xa, xas_next(&xas) != NULL);
1040 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
1041 XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0));
1042 XA_BUG_ON(xa, xas.xa_index != 0);
1043 XA_BUG_ON(xa, xas_prev(&xas) != NULL);
1044 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
1045 rcu_read_unlock();
1046
1047 xa_erase_index(xa, 0);
1048 xa_erase_index(xa, idx);
1049 XA_BUG_ON(xa, !xa_empty(xa));
1050}
1051
1052static noinline void check_move(struct xarray *xa)
1053{
1054 XA_STATE(xas, xa, (1 << 16) - 1);
1055 unsigned long i;
1056
1057 for (i = 0; i < (1 << 16); i++)
1058 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
1059
1060 rcu_read_lock();
1061 do {
1062 void *entry = xas_prev(&xas);
1063 i--;
b7677a13 1064 XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9
MW
1065 XA_BUG_ON(xa, i != xas.xa_index);
1066 } while (i != 0);
1067
1068 XA_BUG_ON(xa, xas_prev(&xas) != NULL);
1069 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
1070
1071 do {
1072 void *entry = xas_next(&xas);
b7677a13 1073 XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9
MW
1074 XA_BUG_ON(xa, i != xas.xa_index);
1075 i++;
1076 } while (i < (1 << 16));
1077 rcu_read_unlock();
1078
1079 for (i = (1 << 8); i < (1 << 15); i++)
1080 xa_erase_index(xa, i);
1081
1082 i = xas.xa_index;
1083
1084 rcu_read_lock();
1085 do {
1086 void *entry = xas_prev(&xas);
1087 i--;
1088 if ((i < (1 << 8)) || (i >= (1 << 15)))
b7677a13 1089 XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9
MW
1090 else
1091 XA_BUG_ON(xa, entry != NULL);
1092 XA_BUG_ON(xa, i != xas.xa_index);
1093 } while (i != 0);
1094
1095 XA_BUG_ON(xa, xas_prev(&xas) != NULL);
1096 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
1097
1098 do {
1099 void *entry = xas_next(&xas);
1100 if ((i < (1 << 8)) || (i >= (1 << 15)))
b7677a13 1101 XA_BUG_ON(xa, entry != xa_mk_index(i));
64d3e9a9
MW
1102 else
1103 XA_BUG_ON(xa, entry != NULL);
1104 XA_BUG_ON(xa, i != xas.xa_index);
1105 i++;
1106 } while (i < (1 << 16));
1107 rcu_read_unlock();
1108
1109 xa_destroy(xa);
1110
1111 for (i = 0; i < 16; i++)
1112 check_move_small(xa, 1UL << i);
1113
1114 for (i = 2; i < 16; i++)
1115 check_move_small(xa, (1UL << i) - 1);
1116}
1117
2264f513
MW
1118static noinline void xa_store_many_order(struct xarray *xa,
1119 unsigned long index, unsigned order)
1120{
1121 XA_STATE_ORDER(xas, xa, index, order);
1122 unsigned int i = 0;
1123
1124 do {
1125 xas_lock(&xas);
1126 XA_BUG_ON(xa, xas_find_conflict(&xas));
1127 xas_create_range(&xas);
1128 if (xas_error(&xas))
1129 goto unlock;
1130 for (i = 0; i < (1U << order); i++) {
b7677a13 1131 XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(index + i)));
2264f513
MW
1132 xas_next(&xas);
1133 }
1134unlock:
1135 xas_unlock(&xas);
1136 } while (xas_nomem(&xas, GFP_KERNEL));
1137
1138 XA_BUG_ON(xa, xas_error(&xas));
1139}
1140
1141static noinline void check_create_range_1(struct xarray *xa,
1142 unsigned long index, unsigned order)
1143{
1144 unsigned long i;
1145
1146 xa_store_many_order(xa, index, order);
1147 for (i = index; i < index + (1UL << order); i++)
1148 xa_erase_index(xa, i);
1149 XA_BUG_ON(xa, !xa_empty(xa));
1150}
1151
1152static noinline void check_create_range_2(struct xarray *xa, unsigned order)
1153{
1154 unsigned long i;
1155 unsigned long nr = 1UL << order;
1156
1157 for (i = 0; i < nr * nr; i += nr)
1158 xa_store_many_order(xa, i, order);
1159 for (i = 0; i < nr * nr; i++)
1160 xa_erase_index(xa, i);
1161 XA_BUG_ON(xa, !xa_empty(xa));
1162}
1163
1164static noinline void check_create_range_3(void)
1165{
1166 XA_STATE(xas, NULL, 0);
1167 xas_set_err(&xas, -EEXIST);
1168 xas_create_range(&xas);
1169 XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST);
1170}
1171
1172static noinline void check_create_range_4(struct xarray *xa,
1173 unsigned long index, unsigned order)
1174{
1175 XA_STATE_ORDER(xas, xa, index, order);
1176 unsigned long base = xas.xa_index;
1177 unsigned long i = 0;
1178
1179 xa_store_index(xa, index, GFP_KERNEL);
1180 do {
1181 xas_lock(&xas);
1182 xas_create_range(&xas);
1183 if (xas_error(&xas))
1184 goto unlock;
1185 for (i = 0; i < (1UL << order); i++) {
b7677a13 1186 void *old = xas_store(&xas, xa_mk_index(base + i));
2264f513 1187 if (xas.xa_index == index)
b7677a13 1188 XA_BUG_ON(xa, old != xa_mk_index(base + i));
2264f513
MW
1189 else
1190 XA_BUG_ON(xa, old != NULL);
1191 xas_next(&xas);
1192 }
1193unlock:
1194 xas_unlock(&xas);
1195 } while (xas_nomem(&xas, GFP_KERNEL));
1196
1197 XA_BUG_ON(xa, xas_error(&xas));
1198
1199 for (i = base; i < base + (1UL << order); i++)
1200 xa_erase_index(xa, i);
1201 XA_BUG_ON(xa, !xa_empty(xa));
1202}
1203
1204static noinline void check_create_range(struct xarray *xa)
1205{
1206 unsigned int order;
1207 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1;
1208
1209 for (order = 0; order < max_order; order++) {
1210 check_create_range_1(xa, 0, order);
1211 check_create_range_1(xa, 1U << order, order);
1212 check_create_range_1(xa, 2U << order, order);
1213 check_create_range_1(xa, 3U << order, order);
1214 check_create_range_1(xa, 1U << 24, order);
1215 if (order < 10)
1216 check_create_range_2(xa, order);
1217
1218 check_create_range_4(xa, 0, order);
1219 check_create_range_4(xa, 1U << order, order);
1220 check_create_range_4(xa, 2U << order, order);
1221 check_create_range_4(xa, 3U << order, order);
1222 check_create_range_4(xa, 1U << 24, order);
1223
1224 check_create_range_4(xa, 1, order);
1225 check_create_range_4(xa, (1U << order) + 1, order);
1226 check_create_range_4(xa, (2U << order) + 1, order);
1227 check_create_range_4(xa, (2U << order) - 1, order);
1228 check_create_range_4(xa, (3U << order) + 1, order);
1229 check_create_range_4(xa, (3U << order) - 1, order);
1230 check_create_range_4(xa, (1U << 24) + 1, order);
1231 }
1232
1233 check_create_range_3();
1234}
1235
0e9446c3
MW
1236static noinline void __check_store_range(struct xarray *xa, unsigned long first,
1237 unsigned long last)
1238{
1239#ifdef CONFIG_XARRAY_MULTI
b7677a13 1240 xa_store_range(xa, first, last, xa_mk_index(first), GFP_KERNEL);
0e9446c3 1241
b7677a13
MW
1242 XA_BUG_ON(xa, xa_load(xa, first) != xa_mk_index(first));
1243 XA_BUG_ON(xa, xa_load(xa, last) != xa_mk_index(first));
0e9446c3
MW
1244 XA_BUG_ON(xa, xa_load(xa, first - 1) != NULL);
1245 XA_BUG_ON(xa, xa_load(xa, last + 1) != NULL);
1246
1247 xa_store_range(xa, first, last, NULL, GFP_KERNEL);
1248#endif
1249
1250 XA_BUG_ON(xa, !xa_empty(xa));
1251}
1252
1253static noinline void check_store_range(struct xarray *xa)
1254{
1255 unsigned long i, j;
1256
1257 for (i = 0; i < 128; i++) {
1258 for (j = i; j < 128; j++) {
1259 __check_store_range(xa, i, j);
1260 __check_store_range(xa, 128 + i, 128 + j);
1261 __check_store_range(xa, 4095 + i, 4095 + j);
1262 __check_store_range(xa, 4096 + i, 4096 + j);
1263 __check_store_range(xa, 123456 + i, 123456 + j);
5404a7f1 1264 __check_store_range(xa, (1 << 24) + i, (1 << 24) + j);
0e9446c3
MW
1265 }
1266 }
1267}
1268
76b4e529
MW
1269static void check_align_1(struct xarray *xa, char *name)
1270{
1271 int i;
1272 unsigned int id;
1273 unsigned long index;
1274 void *entry;
1275
1276 for (i = 0; i < 8; i++) {
a3e4d3f9
MW
1277 XA_BUG_ON(xa, xa_alloc(xa, &id, name + i, xa_limit_32b,
1278 GFP_KERNEL) != 0);
76b4e529
MW
1279 XA_BUG_ON(xa, id != i);
1280 }
1281 xa_for_each(xa, index, entry)
1282 XA_BUG_ON(xa, xa_is_err(entry));
1283 xa_destroy(xa);
1284}
1285
1286static noinline void check_align(struct xarray *xa)
1287{
1288 char name[] = "Motorola 68000";
1289
1290 check_align_1(xa, name);
1291 check_align_1(xa, name + 1);
1292 check_align_1(xa, name + 2);
1293 check_align_1(xa, name + 3);
1294// check_align_2(xa, name);
1295}
1296
a97e7904
MW
1297static LIST_HEAD(shadow_nodes);
1298
1299static void test_update_node(struct xa_node *node)
1300{
1301 if (node->count && node->count == node->nr_values) {
1302 if (list_empty(&node->private_list))
1303 list_add(&shadow_nodes, &node->private_list);
1304 } else {
1305 if (!list_empty(&node->private_list))
1306 list_del_init(&node->private_list);
1307 }
1308}
1309
1310static noinline void shadow_remove(struct xarray *xa)
1311{
1312 struct xa_node *node;
1313
1314 xa_lock(xa);
1315 while ((node = list_first_entry_or_null(&shadow_nodes,
1316 struct xa_node, private_list))) {
1317 XA_STATE(xas, node->array, 0);
1318 XA_BUG_ON(xa, node->array != xa);
1319 list_del_init(&node->private_list);
1320 xas.xa_node = xa_parent_locked(node->array, node);
1321 xas.xa_offset = node->offset;
1322 xas.xa_shift = node->shift + XA_CHUNK_SHIFT;
1323 xas_set_update(&xas, test_update_node);
1324 xas_store(&xas, NULL);
1325 }
1326 xa_unlock(xa);
1327}
1328
1329static noinline void check_workingset(struct xarray *xa, unsigned long index)
1330{
1331 XA_STATE(xas, xa, index);
1332 xas_set_update(&xas, test_update_node);
1333
1334 do {
1335 xas_lock(&xas);
1336 xas_store(&xas, xa_mk_value(0));
1337 xas_next(&xas);
1338 xas_store(&xas, xa_mk_value(1));
1339 xas_unlock(&xas);
1340 } while (xas_nomem(&xas, GFP_KERNEL));
1341
1342 XA_BUG_ON(xa, list_empty(&shadow_nodes));
1343
1344 xas_lock(&xas);
1345 xas_next(&xas);
1346 xas_store(&xas, &xas);
1347 XA_BUG_ON(xa, !list_empty(&shadow_nodes));
1348
1349 xas_store(&xas, xa_mk_value(2));
1350 xas_unlock(&xas);
1351 XA_BUG_ON(xa, list_empty(&shadow_nodes));
1352
1353 shadow_remove(xa);
1354 XA_BUG_ON(xa, !list_empty(&shadow_nodes));
1355 XA_BUG_ON(xa, !xa_empty(xa));
1356}
1357
d6427f81
MW
1358/*
1359 * Check that the pointer / value / sibling entries are accounted the
1360 * way we expect them to be.
1361 */
1362static noinline void check_account(struct xarray *xa)
1363{
1364#ifdef CONFIG_XARRAY_MULTI
1365 unsigned int order;
1366
1367 for (order = 1; order < 12; order++) {
1368 XA_STATE(xas, xa, 1 << order);
1369
1370 xa_store_order(xa, 0, order, xa, GFP_KERNEL);
fffc9a26 1371 rcu_read_lock();
d6427f81
MW
1372 xas_load(&xas);
1373 XA_BUG_ON(xa, xas.xa_node->count == 0);
1374 XA_BUG_ON(xa, xas.xa_node->count > (1 << order));
1375 XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
fffc9a26 1376 rcu_read_unlock();
d6427f81 1377
b7677a13 1378 xa_store_order(xa, 1 << order, order, xa_mk_index(1UL << order),
d6427f81
MW
1379 GFP_KERNEL);
1380 XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2);
1381
1382 xa_erase(xa, 1 << order);
1383 XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
1384
1385 xa_erase(xa, 0);
1386 XA_BUG_ON(xa, !xa_empty(xa));
1387 }
1388#endif
1389}
1390
687149fc
MW
1391static noinline void check_destroy(struct xarray *xa)
1392{
1393 unsigned long index;
1394
1395 XA_BUG_ON(xa, !xa_empty(xa));
1396
1397 /* Destroying an empty array is a no-op */
1398 xa_destroy(xa);
1399 XA_BUG_ON(xa, !xa_empty(xa));
1400
1401 /* Destroying an array with a single entry */
1402 for (index = 0; index < 1000; index++) {
1403 xa_store_index(xa, index, GFP_KERNEL);
1404 XA_BUG_ON(xa, xa_empty(xa));
1405 xa_destroy(xa);
1406 XA_BUG_ON(xa, !xa_empty(xa));
1407 }
1408
1409 /* Destroying an array with a single entry at ULONG_MAX */
1410 xa_store(xa, ULONG_MAX, xa, GFP_KERNEL);
1411 XA_BUG_ON(xa, xa_empty(xa));
1412 xa_destroy(xa);
1413 XA_BUG_ON(xa, !xa_empty(xa));
1414
1415#ifdef CONFIG_XARRAY_MULTI
1416 /* Destroying an array with a multi-index entry */
1417 xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL);
1418 XA_BUG_ON(xa, xa_empty(xa));
1419 xa_destroy(xa);
1420 XA_BUG_ON(xa, !xa_empty(xa));
1421#endif
1422}
1423
58d6ea30 1424static DEFINE_XARRAY(array);
ad3d6c72
MW
1425
1426static int xarray_checks(void)
1427{
58d6ea30 1428 check_xa_err(&array);
b803b428 1429 check_xas_retry(&array);
ad3d6c72 1430 check_xa_load(&array);
9b89a035 1431 check_xa_mark(&array);
58d6ea30 1432 check_xa_shrink(&array);
b803b428 1433 check_xas_erase(&array);
41aec91f 1434 check_cmpxchg(&array);
9f14d4f1 1435 check_reserve(&array);
58d6ea30 1436 check_multi_store(&array);
371c752d 1437 check_xa_alloc();
b803b428 1438 check_find(&array);
e21a2955 1439 check_find_entry(&array);
d6427f81 1440 check_account(&array);
687149fc 1441 check_destroy(&array);
64d3e9a9 1442 check_move(&array);
2264f513 1443 check_create_range(&array);
0e9446c3 1444 check_store_range(&array);
4e99d4e9 1445 check_store_iter(&array);
76b4e529 1446 check_align(&xa0);
ad3d6c72 1447
a97e7904
MW
1448 check_workingset(&array, 0);
1449 check_workingset(&array, 64);
1450 check_workingset(&array, 4096);
1451
ad3d6c72
MW
1452 printk("XArray: %u of %u tests passed\n", tests_passed, tests_run);
1453 return (tests_run == tests_passed) ? 0 : -EINVAL;
1454}
1455
1456static void xarray_exit(void)
1457{
1458}
1459
1460module_init(xarray_checks);
1461module_exit(xarray_exit);
1462MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
1463MODULE_LICENSE("GPL");