1 // SPDX-License-Identifier: GPL-2.0+
3 * test_xarray.c: Test the XArray API
4 * Copyright (c) 2017-2018 Microsoft Corporation
5 * Author: Matthew Wilcox <willy@infradead.org>
8 #include <linux/xarray.h>
9 #include <linux/module.h>
11 static unsigned int tests_run;
12 static unsigned int tests_passed;
16 void xa_dump(const struct xarray *xa) { }
19 #define XA_BUG_ON(xa, x) do { \
22 printk("BUG at %s:%d\n", __func__, __LINE__); \
31 static void *xa_mk_index(unsigned long index)
33 return xa_mk_value(index & LONG_MAX);
36 static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp)
38 return xa_store(xa, index, xa_mk_index(index), gfp);
41 static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp)
45 XA_BUG_ON(xa, xa_alloc(xa, &id, UINT_MAX, xa_mk_index(index),
47 XA_BUG_ON(xa, id != index);
50 static void xa_erase_index(struct xarray *xa, unsigned long index)
52 XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_index(index));
53 XA_BUG_ON(xa, xa_load(xa, index) != NULL);
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.
61 static void *xa_store_order(struct xarray *xa, unsigned long index,
62 unsigned order, void *entry, gfp_t gfp)
64 XA_STATE_ORDER(xas, xa, index, order);
69 curr = xas_store(&xas, entry);
71 } while (xas_nomem(&xas, gfp));
76 static noinline void check_xa_err(struct xarray *xa)
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);
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);
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);
92 static noinline void check_xas_retry(struct xarray *xa)
97 xa_store_index(xa, 0, GFP_KERNEL);
98 xa_store_index(xa, 1, GFP_KERNEL);
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)));
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);
111 XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
112 XA_BUG_ON(xa, !xa_is_internal(xas_reload(&xas)));
113 xas.xa_node = XAS_RESTART;
114 XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0));
117 /* Make sure we can iterate through retry entries */
120 xas_store(&xas, XA_RETRY_ENTRY);
122 xas_store(&xas, XA_RETRY_ENTRY);
125 xas_for_each(&xas, entry, ULONG_MAX) {
126 xas_store(&xas, xa_mk_index(xas.xa_index));
130 xa_erase_index(xa, 0);
131 xa_erase_index(xa, 1);
134 static noinline void check_xa_load(struct xarray *xa)
138 for (i = 0; i < 1024; i++) {
139 for (j = 0; j < 1024; j++) {
140 void *entry = xa_load(xa, j);
142 XA_BUG_ON(xa, xa_to_value(entry) != j);
144 XA_BUG_ON(xa, entry);
146 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
149 for (i = 0; i < 1024; i++) {
150 for (j = 0; j < 1024; j++) {
151 void *entry = xa_load(xa, j);
153 XA_BUG_ON(xa, xa_to_value(entry) != j);
155 XA_BUG_ON(xa, entry);
157 xa_erase_index(xa, i);
159 XA_BUG_ON(xa, !xa_empty(xa));
162 static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index)
165 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1;
167 /* NULL elements have no marks set */
168 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
169 xa_set_mark(xa, index, XA_MARK_0);
170 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
172 /* Storing a pointer will not make a mark appear */
173 XA_BUG_ON(xa, xa_store_index(xa, index, GFP_KERNEL) != NULL);
174 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
175 xa_set_mark(xa, index, XA_MARK_0);
176 XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0));
178 /* Setting one mark will not set another mark */
179 XA_BUG_ON(xa, xa_get_mark(xa, index + 1, XA_MARK_0));
180 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_1));
182 /* Storing NULL clears marks, and they can't be set again */
183 xa_erase_index(xa, index);
184 XA_BUG_ON(xa, !xa_empty(xa));
185 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
186 xa_set_mark(xa, index, XA_MARK_0);
187 XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
190 * Storing a multi-index entry over entries with marks gives the
191 * entire entry the union of the marks
193 BUG_ON((index % 4) != 0);
194 for (order = 2; order < max_order; order++) {
195 unsigned long base = round_down(index, 1UL << order);
196 unsigned long next = base + (1UL << order);
199 XA_BUG_ON(xa, xa_store_index(xa, index + 1, GFP_KERNEL));
200 xa_set_mark(xa, index + 1, XA_MARK_0);
201 XA_BUG_ON(xa, xa_store_index(xa, index + 2, GFP_KERNEL));
202 xa_set_mark(xa, index + 2, XA_MARK_2);
203 XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL));
204 xa_store_order(xa, index, order, xa_mk_index(index),
206 for (i = base; i < next; i++) {
207 XA_STATE(xas, xa, i);
208 unsigned int seen = 0;
211 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
212 XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_1));
213 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_2));
215 /* We should see two elements in the array */
217 xas_for_each(&xas, entry, ULONG_MAX)
220 XA_BUG_ON(xa, seen != 2);
222 /* One of which is marked */
226 xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
229 XA_BUG_ON(xa, seen != 1);
231 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_0));
232 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_1));
233 XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_2));
234 xa_erase_index(xa, index);
235 xa_erase_index(xa, next);
236 XA_BUG_ON(xa, !xa_empty(xa));
238 XA_BUG_ON(xa, !xa_empty(xa));
241 static noinline void check_xa_mark_2(struct xarray *xa)
243 XA_STATE(xas, xa, 0);
245 unsigned int count = 0;
248 xa_store_index(xa, 0, GFP_KERNEL);
249 xa_set_mark(xa, 0, XA_MARK_0);
252 xas_init_marks(&xas);
254 XA_BUG_ON(xa, !xa_get_mark(xa, 0, XA_MARK_0) == 0);
256 for (index = 3500; index < 4500; index++) {
257 xa_store_index(xa, index, GFP_KERNEL);
258 xa_set_mark(xa, index, XA_MARK_0);
263 xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
266 XA_BUG_ON(xa, count != 1000);
269 xas_for_each(&xas, entry, ULONG_MAX) {
270 xas_init_marks(&xas);
271 XA_BUG_ON(xa, !xa_get_mark(xa, xas.xa_index, XA_MARK_0));
272 XA_BUG_ON(xa, !xas_get_mark(&xas, XA_MARK_0));
279 static noinline void check_xa_mark(struct xarray *xa)
283 for (index = 0; index < 16384; index += 4)
284 check_xa_mark_1(xa, index);
289 static noinline void check_xa_shrink(struct xarray *xa)
291 XA_STATE(xas, xa, 1);
292 struct xa_node *node;
294 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1;
296 XA_BUG_ON(xa, !xa_empty(xa));
297 XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL);
298 XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
301 * Check that erasing the entry at 1 shrinks the tree and properly
302 * marks the node as being deleted.
305 XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1));
307 XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0));
308 XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
309 XA_BUG_ON(xa, xa_load(xa, 1) != NULL);
310 XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS);
311 XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY);
312 XA_BUG_ON(xa, xas_load(&xas) != NULL);
314 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
315 xa_erase_index(xa, 0);
316 XA_BUG_ON(xa, !xa_empty(xa));
318 for (order = 0; order < max_order; order++) {
319 unsigned long max = (1UL << order) - 1;
320 xa_store_order(xa, 0, order, xa_mk_value(0), GFP_KERNEL);
321 XA_BUG_ON(xa, xa_load(xa, max) != xa_mk_value(0));
322 XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
326 XA_BUG_ON(xa, xa_store_index(xa, ULONG_MAX, GFP_KERNEL) !=
329 XA_BUG_ON(xa, xa_head(xa) == node);
331 XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL);
332 xa_erase_index(xa, ULONG_MAX);
333 XA_BUG_ON(xa, xa->xa_head != node);
334 xa_erase_index(xa, 0);
338 static noinline void check_cmpxchg(struct xarray *xa)
340 void *FIVE = xa_mk_value(5);
341 void *SIX = xa_mk_value(6);
342 void *LOTS = xa_mk_value(12345678);
344 XA_BUG_ON(xa, !xa_empty(xa));
345 XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_KERNEL) != NULL);
346 XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EEXIST);
347 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, SIX, FIVE, GFP_KERNEL) != LOTS);
348 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, LOTS, FIVE, GFP_KERNEL) != LOTS);
349 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, FIVE, LOTS, GFP_KERNEL) != FIVE);
350 XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != NULL);
351 XA_BUG_ON(xa, xa_cmpxchg(xa, 5, NULL, FIVE, GFP_KERNEL) != NULL);
352 xa_erase_index(xa, 12345678);
353 xa_erase_index(xa, 5);
354 XA_BUG_ON(xa, !xa_empty(xa));
357 static noinline void check_reserve(struct xarray *xa)
362 /* An array with a reserved entry is not empty */
363 XA_BUG_ON(xa, !xa_empty(xa));
364 xa_reserve(xa, 12345678, GFP_KERNEL);
365 XA_BUG_ON(xa, xa_empty(xa));
366 XA_BUG_ON(xa, xa_load(xa, 12345678));
367 xa_release(xa, 12345678);
368 XA_BUG_ON(xa, !xa_empty(xa));
370 /* Releasing a used entry does nothing */
371 xa_reserve(xa, 12345678, GFP_KERNEL);
372 XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_NOWAIT) != NULL);
373 xa_release(xa, 12345678);
374 xa_erase_index(xa, 12345678);
375 XA_BUG_ON(xa, !xa_empty(xa));
377 /* cmpxchg sees a reserved entry as NULL */
378 xa_reserve(xa, 12345678, GFP_KERNEL);
379 XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, NULL, xa_mk_value(12345678),
380 GFP_NOWAIT) != NULL);
381 xa_release(xa, 12345678);
382 xa_erase_index(xa, 12345678);
383 XA_BUG_ON(xa, !xa_empty(xa));
385 /* But xa_insert does not */
386 xa_reserve(xa, 12345678, GFP_KERNEL);
387 XA_BUG_ON(xa, xa_insert(xa, 12345678, xa_mk_value(12345678), 0) !=
389 XA_BUG_ON(xa, xa_empty(xa));
390 XA_BUG_ON(xa, xa_erase(xa, 12345678) != NULL);
391 XA_BUG_ON(xa, !xa_empty(xa));
393 /* Can iterate through a reserved entry */
394 xa_store_index(xa, 5, GFP_KERNEL);
395 xa_reserve(xa, 6, GFP_KERNEL);
396 xa_store_index(xa, 7, GFP_KERNEL);
398 xa_for_each(xa, index, entry) {
399 XA_BUG_ON(xa, index != 5 && index != 7);
404 static noinline void check_xas_erase(struct xarray *xa)
406 XA_STATE(xas, xa, 0);
410 for (i = 0; i < 200; i++) {
411 for (j = i; j < 2 * i + 17; j++) {
415 xas_store(&xas, xa_mk_index(j));
417 } while (xas_nomem(&xas, GFP_KERNEL));
420 xas_set(&xas, ULONG_MAX);
423 xas_store(&xas, xa_mk_value(0));
425 } while (xas_nomem(&xas, GFP_KERNEL));
428 xas_store(&xas, NULL);
432 xas_for_each(&xas, entry, ULONG_MAX) {
433 XA_BUG_ON(xa, entry != xa_mk_index(j));
434 xas_store(&xas, NULL);
438 XA_BUG_ON(xa, !xa_empty(xa));
442 #ifdef CONFIG_XARRAY_MULTI
443 static noinline void check_multi_store_1(struct xarray *xa, unsigned long index,
446 XA_STATE(xas, xa, index);
447 unsigned long min = index & ~((1UL << order) - 1);
448 unsigned long max = min + (1UL << order);
450 xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
451 XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(index));
452 XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(index));
453 XA_BUG_ON(xa, xa_load(xa, max) != NULL);
454 XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
457 XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(min)) != xa_mk_index(index));
459 XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_index(min));
460 XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_index(min));
461 XA_BUG_ON(xa, xa_load(xa, max) != NULL);
462 XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL);
464 xa_erase_index(xa, min);
465 XA_BUG_ON(xa, !xa_empty(xa));
468 static noinline void check_multi_store_2(struct xarray *xa, unsigned long index,
471 XA_STATE(xas, xa, index);
472 xa_store_order(xa, index, order, xa_mk_value(0), GFP_KERNEL);
475 XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(1)) != xa_mk_value(0));
476 XA_BUG_ON(xa, xas.xa_index != index);
477 XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
479 XA_BUG_ON(xa, !xa_empty(xa));
482 static noinline void check_multi_store_3(struct xarray *xa, unsigned long index,
485 XA_STATE(xas, xa, 0);
489 xa_store_order(xa, index, order, xa_mk_index(index), GFP_KERNEL);
492 xas_for_each(&xas, entry, ULONG_MAX) {
493 XA_BUG_ON(xa, entry != xa_mk_index(index));
496 XA_BUG_ON(xa, n != 1);
497 xas_set(&xas, index + 1);
498 xas_for_each(&xas, entry, ULONG_MAX) {
499 XA_BUG_ON(xa, entry != xa_mk_index(index));
502 XA_BUG_ON(xa, n != 2);
509 static noinline void check_multi_store(struct xarray *xa)
511 #ifdef CONFIG_XARRAY_MULTI
512 unsigned long i, j, k;
513 unsigned int max_order = (sizeof(long) == 4) ? 30 : 60;
515 /* Loading from any position returns the same value */
516 xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL);
517 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
518 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
519 XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
521 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2);
522 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
525 /* Storing adjacent to the value does not alter the value */
526 xa_store(xa, 3, xa, GFP_KERNEL);
527 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
528 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
529 XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
531 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3);
532 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
535 /* Overwriting multiple indexes works */
536 xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL);
537 XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1));
538 XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1));
539 XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1));
540 XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1));
541 XA_BUG_ON(xa, xa_load(xa, 4) != NULL);
543 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4);
544 XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4);
547 /* We can erase multiple values with a single store */
548 xa_store_order(xa, 0, BITS_PER_LONG - 1, NULL, GFP_KERNEL);
549 XA_BUG_ON(xa, !xa_empty(xa));
551 /* Even when the first slot is empty but the others aren't */
552 xa_store_index(xa, 1, GFP_KERNEL);
553 xa_store_index(xa, 2, GFP_KERNEL);
554 xa_store_order(xa, 0, 2, NULL, GFP_KERNEL);
555 XA_BUG_ON(xa, !xa_empty(xa));
557 for (i = 0; i < max_order; i++) {
558 for (j = 0; j < max_order; j++) {
559 xa_store_order(xa, 0, i, xa_mk_index(i), GFP_KERNEL);
560 xa_store_order(xa, 0, j, xa_mk_index(j), GFP_KERNEL);
562 for (k = 0; k < max_order; k++) {
563 void *entry = xa_load(xa, (1UL << k) - 1);
564 if ((i < k) && (j < k))
565 XA_BUG_ON(xa, entry != NULL);
567 XA_BUG_ON(xa, entry != xa_mk_index(j));
571 XA_BUG_ON(xa, !xa_empty(xa));
575 for (i = 0; i < 20; i++) {
576 check_multi_store_1(xa, 200, i);
577 check_multi_store_1(xa, 0, i);
578 check_multi_store_1(xa, (1UL << i) + 1, i);
580 check_multi_store_2(xa, 4095, 9);
582 for (i = 1; i < 20; i++) {
583 check_multi_store_3(xa, 0, i);
584 check_multi_store_3(xa, 1UL << i, i);
589 static DEFINE_XARRAY_ALLOC(xa0);
591 static noinline void check_xa_alloc(void)
596 /* An empty array should assign 0 to the first alloc */
597 xa_alloc_index(&xa0, 0, GFP_KERNEL);
599 /* Erasing it should make the array empty again */
600 xa_erase_index(&xa0, 0);
601 XA_BUG_ON(&xa0, !xa_empty(&xa0));
603 /* And it should assign 0 again */
604 xa_alloc_index(&xa0, 0, GFP_KERNEL);
606 /* The next assigned ID should be 1 */
607 xa_alloc_index(&xa0, 1, GFP_KERNEL);
608 xa_erase_index(&xa0, 1);
610 /* Storing a value should mark it used */
611 xa_store_index(&xa0, 1, GFP_KERNEL);
612 xa_alloc_index(&xa0, 2, GFP_KERNEL);
614 /* If we then erase 0, it should be free */
615 xa_erase_index(&xa0, 0);
616 xa_alloc_index(&xa0, 0, GFP_KERNEL);
618 xa_erase_index(&xa0, 1);
619 xa_erase_index(&xa0, 2);
621 for (i = 1; i < 5000; i++) {
622 xa_alloc_index(&xa0, i, GFP_KERNEL);
628 XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_index(id),
630 XA_BUG_ON(&xa0, id != 0xfffffffeU);
631 XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_index(id),
633 XA_BUG_ON(&xa0, id != 0xffffffffU);
634 XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_index(id),
635 GFP_KERNEL) != -ENOSPC);
636 XA_BUG_ON(&xa0, id != 0xffffffffU);
640 XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, 5, xa_mk_index(id),
641 GFP_KERNEL) != -ENOSPC);
642 XA_BUG_ON(&xa0, xa_store_index(&xa0, 3, GFP_KERNEL) != 0);
643 XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, 5, xa_mk_index(id),
644 GFP_KERNEL) != -ENOSPC);
645 xa_erase_index(&xa0, 3);
646 XA_BUG_ON(&xa0, !xa_empty(&xa0));
649 static noinline void __check_store_iter(struct xarray *xa, unsigned long start,
650 unsigned int order, unsigned int present)
652 XA_STATE_ORDER(xas, xa, start, order);
654 unsigned int count = 0;
658 xas_for_each_conflict(&xas, entry) {
659 XA_BUG_ON(xa, !xa_is_value(entry));
660 XA_BUG_ON(xa, entry < xa_mk_index(start));
661 XA_BUG_ON(xa, entry > xa_mk_index(start + (1UL << order) - 1));
664 xas_store(&xas, xa_mk_index(start));
666 if (xas_nomem(&xas, GFP_KERNEL)) {
670 XA_BUG_ON(xa, xas_error(&xas));
671 XA_BUG_ON(xa, count != present);
672 XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_index(start));
673 XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) !=
675 xa_erase_index(xa, start);
678 static noinline void check_store_iter(struct xarray *xa)
681 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1;
683 for (i = 0; i < max_order; i++) {
684 unsigned int min = 1 << i;
685 unsigned int max = (2 << i) - 1;
686 __check_store_iter(xa, 0, i, 0);
687 XA_BUG_ON(xa, !xa_empty(xa));
688 __check_store_iter(xa, min, i, 0);
689 XA_BUG_ON(xa, !xa_empty(xa));
691 xa_store_index(xa, min, GFP_KERNEL);
692 __check_store_iter(xa, min, i, 1);
693 XA_BUG_ON(xa, !xa_empty(xa));
694 xa_store_index(xa, max, GFP_KERNEL);
695 __check_store_iter(xa, min, i, 1);
696 XA_BUG_ON(xa, !xa_empty(xa));
698 for (j = 0; j < min; j++)
699 xa_store_index(xa, j, GFP_KERNEL);
700 __check_store_iter(xa, 0, i, min);
701 XA_BUG_ON(xa, !xa_empty(xa));
702 for (j = 0; j < min; j++)
703 xa_store_index(xa, min + j, GFP_KERNEL);
704 __check_store_iter(xa, min, i, min);
705 XA_BUG_ON(xa, !xa_empty(xa));
707 #ifdef CONFIG_XARRAY_MULTI
708 xa_store_index(xa, 63, GFP_KERNEL);
709 xa_store_index(xa, 65, GFP_KERNEL);
710 __check_store_iter(xa, 64, 2, 1);
711 xa_erase_index(xa, 63);
713 XA_BUG_ON(xa, !xa_empty(xa));
716 static noinline void check_multi_find(struct xarray *xa)
718 #ifdef CONFIG_XARRAY_MULTI
721 xa_store_order(xa, 12, 2, xa_mk_value(12), GFP_KERNEL);
722 XA_BUG_ON(xa, xa_store_index(xa, 16, GFP_KERNEL) != NULL);
725 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
727 XA_BUG_ON(xa, index != 12);
729 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
731 XA_BUG_ON(xa, (index < 12) || (index >= 16));
732 XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
734 XA_BUG_ON(xa, index != 16);
736 xa_erase_index(xa, 12);
737 xa_erase_index(xa, 16);
738 XA_BUG_ON(xa, !xa_empty(xa));
742 static noinline void check_multi_find_2(struct xarray *xa)
744 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1;
748 for (i = 0; i < max_order; i++) {
749 unsigned long index = 1UL << i;
750 for (j = 0; j < index; j++) {
751 XA_STATE(xas, xa, j + index);
752 xa_store_index(xa, index - 1, GFP_KERNEL);
753 xa_store_order(xa, index, i, xa_mk_index(index),
756 xas_for_each(&xas, entry, ULONG_MAX) {
757 xa_erase_index(xa, index);
760 xa_erase_index(xa, index - 1);
761 XA_BUG_ON(xa, !xa_empty(xa));
766 static noinline void check_find_1(struct xarray *xa)
768 unsigned long i, j, k;
770 XA_BUG_ON(xa, !xa_empty(xa));
773 * Check xa_find with all pairs between 0 and 99 inclusive,
774 * starting at every index between 0 and 99
776 for (i = 0; i < 100; i++) {
777 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
778 xa_set_mark(xa, i, XA_MARK_0);
779 for (j = 0; j < i; j++) {
780 XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) !=
782 xa_set_mark(xa, j, XA_MARK_0);
783 for (k = 0; k < 100; k++) {
784 unsigned long index = k;
785 void *entry = xa_find(xa, &index, ULONG_MAX,
788 XA_BUG_ON(xa, index != j);
790 XA_BUG_ON(xa, index != i);
792 XA_BUG_ON(xa, entry != NULL);
795 entry = xa_find(xa, &index, ULONG_MAX,
798 XA_BUG_ON(xa, index != j);
800 XA_BUG_ON(xa, index != i);
802 XA_BUG_ON(xa, entry != NULL);
804 xa_erase_index(xa, j);
805 XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0));
806 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
808 xa_erase_index(xa, i);
809 XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0));
811 XA_BUG_ON(xa, !xa_empty(xa));
814 static noinline void check_find_2(struct xarray *xa)
817 unsigned long i, j, index;
819 xa_for_each(xa, index, entry) {
823 for (i = 0; i < 1024; i++) {
824 xa_store_index(xa, index, GFP_KERNEL);
826 xa_for_each(xa, index, entry) {
827 XA_BUG_ON(xa, xa_mk_index(index) != entry);
828 XA_BUG_ON(xa, index != j++);
835 static noinline void check_find_3(struct xarray *xa)
837 XA_STATE(xas, xa, 0);
838 unsigned long i, j, k;
841 for (i = 0; i < 100; i++) {
842 for (j = 0; j < 100; j++) {
844 for (k = 0; k < 100; k++) {
846 xas_for_each_marked(&xas, entry, k, XA_MARK_0)
850 xas.xa_node != XAS_RESTART);
854 xa_store_index(xa, i, GFP_KERNEL);
855 xa_set_mark(xa, i, XA_MARK_0);
860 static noinline void check_find(struct xarray *xa)
865 check_multi_find(xa);
866 check_multi_find_2(xa);
869 /* See find_swap_entry() in mm/shmem.c */
870 static noinline unsigned long xa_find_entry(struct xarray *xa, void *item)
872 XA_STATE(xas, xa, 0);
873 unsigned int checked = 0;
877 xas_for_each(&xas, entry, ULONG_MAX) {
878 if (xas_retry(&xas, entry))
883 if ((checked % 4) != 0)
889 return entry ? xas.xa_index : -1;
892 static noinline void check_find_entry(struct xarray *xa)
894 #ifdef CONFIG_XARRAY_MULTI
896 unsigned long offset, index;
898 for (order = 0; order < 20; order++) {
899 for (offset = 0; offset < (1UL << (order + 3));
900 offset += (1UL << order)) {
901 for (index = 0; index < (1UL << (order + 5));
902 index += (1UL << order)) {
903 xa_store_order(xa, index, order,
904 xa_mk_index(index), GFP_KERNEL);
905 XA_BUG_ON(xa, xa_load(xa, index) !=
907 XA_BUG_ON(xa, xa_find_entry(xa,
908 xa_mk_index(index)) != index);
910 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
916 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
917 xa_store_index(xa, ULONG_MAX, GFP_KERNEL);
918 XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1);
919 XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_index(ULONG_MAX)) != -1);
920 xa_erase_index(xa, ULONG_MAX);
921 XA_BUG_ON(xa, !xa_empty(xa));
924 static noinline void check_move_small(struct xarray *xa, unsigned long idx)
926 XA_STATE(xas, xa, 0);
929 xa_store_index(xa, 0, GFP_KERNEL);
930 xa_store_index(xa, idx, GFP_KERNEL);
933 for (i = 0; i < idx * 4; i++) {
934 void *entry = xas_next(&xas);
936 XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
937 XA_BUG_ON(xa, xas.xa_index != i);
938 if (i == 0 || i == idx)
939 XA_BUG_ON(xa, entry != xa_mk_index(i));
941 XA_BUG_ON(xa, entry != NULL);
944 XA_BUG_ON(xa, xas.xa_index != i);
947 void *entry = xas_prev(&xas);
950 XA_BUG_ON(xa, xas.xa_node == XAS_RESTART);
951 XA_BUG_ON(xa, xas.xa_index != i);
952 if (i == 0 || i == idx)
953 XA_BUG_ON(xa, entry != xa_mk_index(i));
955 XA_BUG_ON(xa, entry != NULL);
958 xas_set(&xas, ULONG_MAX);
959 XA_BUG_ON(xa, xas_next(&xas) != NULL);
960 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
961 XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0));
962 XA_BUG_ON(xa, xas.xa_index != 0);
963 XA_BUG_ON(xa, xas_prev(&xas) != NULL);
964 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
967 xa_erase_index(xa, 0);
968 xa_erase_index(xa, idx);
969 XA_BUG_ON(xa, !xa_empty(xa));
972 static noinline void check_move(struct xarray *xa)
974 XA_STATE(xas, xa, (1 << 16) - 1);
977 for (i = 0; i < (1 << 16); i++)
978 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
982 void *entry = xas_prev(&xas);
984 XA_BUG_ON(xa, entry != xa_mk_index(i));
985 XA_BUG_ON(xa, i != xas.xa_index);
988 XA_BUG_ON(xa, xas_prev(&xas) != NULL);
989 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
992 void *entry = xas_next(&xas);
993 XA_BUG_ON(xa, entry != xa_mk_index(i));
994 XA_BUG_ON(xa, i != xas.xa_index);
996 } while (i < (1 << 16));
999 for (i = (1 << 8); i < (1 << 15); i++)
1000 xa_erase_index(xa, i);
1006 void *entry = xas_prev(&xas);
1008 if ((i < (1 << 8)) || (i >= (1 << 15)))
1009 XA_BUG_ON(xa, entry != xa_mk_index(i));
1011 XA_BUG_ON(xa, entry != NULL);
1012 XA_BUG_ON(xa, i != xas.xa_index);
1015 XA_BUG_ON(xa, xas_prev(&xas) != NULL);
1016 XA_BUG_ON(xa, xas.xa_index != ULONG_MAX);
1019 void *entry = xas_next(&xas);
1020 if ((i < (1 << 8)) || (i >= (1 << 15)))
1021 XA_BUG_ON(xa, entry != xa_mk_index(i));
1023 XA_BUG_ON(xa, entry != NULL);
1024 XA_BUG_ON(xa, i != xas.xa_index);
1026 } while (i < (1 << 16));
1031 for (i = 0; i < 16; i++)
1032 check_move_small(xa, 1UL << i);
1034 for (i = 2; i < 16; i++)
1035 check_move_small(xa, (1UL << i) - 1);
1038 static noinline void xa_store_many_order(struct xarray *xa,
1039 unsigned long index, unsigned order)
1041 XA_STATE_ORDER(xas, xa, index, order);
1046 XA_BUG_ON(xa, xas_find_conflict(&xas));
1047 xas_create_range(&xas);
1048 if (xas_error(&xas))
1050 for (i = 0; i < (1U << order); i++) {
1051 XA_BUG_ON(xa, xas_store(&xas, xa_mk_index(index + i)));
1056 } while (xas_nomem(&xas, GFP_KERNEL));
1058 XA_BUG_ON(xa, xas_error(&xas));
1061 static noinline void check_create_range_1(struct xarray *xa,
1062 unsigned long index, unsigned order)
1066 xa_store_many_order(xa, index, order);
1067 for (i = index; i < index + (1UL << order); i++)
1068 xa_erase_index(xa, i);
1069 XA_BUG_ON(xa, !xa_empty(xa));
1072 static noinline void check_create_range_2(struct xarray *xa, unsigned order)
1075 unsigned long nr = 1UL << order;
1077 for (i = 0; i < nr * nr; i += nr)
1078 xa_store_many_order(xa, i, order);
1079 for (i = 0; i < nr * nr; i++)
1080 xa_erase_index(xa, i);
1081 XA_BUG_ON(xa, !xa_empty(xa));
1084 static noinline void check_create_range_3(void)
1086 XA_STATE(xas, NULL, 0);
1087 xas_set_err(&xas, -EEXIST);
1088 xas_create_range(&xas);
1089 XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST);
1092 static noinline void check_create_range_4(struct xarray *xa,
1093 unsigned long index, unsigned order)
1095 XA_STATE_ORDER(xas, xa, index, order);
1096 unsigned long base = xas.xa_index;
1097 unsigned long i = 0;
1099 xa_store_index(xa, index, GFP_KERNEL);
1102 xas_create_range(&xas);
1103 if (xas_error(&xas))
1105 for (i = 0; i < (1UL << order); i++) {
1106 void *old = xas_store(&xas, xa_mk_index(base + i));
1107 if (xas.xa_index == index)
1108 XA_BUG_ON(xa, old != xa_mk_index(base + i));
1110 XA_BUG_ON(xa, old != NULL);
1115 } while (xas_nomem(&xas, GFP_KERNEL));
1117 XA_BUG_ON(xa, xas_error(&xas));
1119 for (i = base; i < base + (1UL << order); i++)
1120 xa_erase_index(xa, i);
1121 XA_BUG_ON(xa, !xa_empty(xa));
1124 static noinline void check_create_range(struct xarray *xa)
1127 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1;
1129 for (order = 0; order < max_order; order++) {
1130 check_create_range_1(xa, 0, order);
1131 check_create_range_1(xa, 1U << order, order);
1132 check_create_range_1(xa, 2U << order, order);
1133 check_create_range_1(xa, 3U << order, order);
1134 check_create_range_1(xa, 1U << 24, order);
1136 check_create_range_2(xa, order);
1138 check_create_range_4(xa, 0, order);
1139 check_create_range_4(xa, 1U << order, order);
1140 check_create_range_4(xa, 2U << order, order);
1141 check_create_range_4(xa, 3U << order, order);
1142 check_create_range_4(xa, 1U << 24, order);
1144 check_create_range_4(xa, 1, order);
1145 check_create_range_4(xa, (1U << order) + 1, order);
1146 check_create_range_4(xa, (2U << order) + 1, order);
1147 check_create_range_4(xa, (2U << order) - 1, order);
1148 check_create_range_4(xa, (3U << order) + 1, order);
1149 check_create_range_4(xa, (3U << order) - 1, order);
1150 check_create_range_4(xa, (1U << 24) + 1, order);
1153 check_create_range_3();
1156 static noinline void __check_store_range(struct xarray *xa, unsigned long first,
1159 #ifdef CONFIG_XARRAY_MULTI
1160 xa_store_range(xa, first, last, xa_mk_index(first), GFP_KERNEL);
1162 XA_BUG_ON(xa, xa_load(xa, first) != xa_mk_index(first));
1163 XA_BUG_ON(xa, xa_load(xa, last) != xa_mk_index(first));
1164 XA_BUG_ON(xa, xa_load(xa, first - 1) != NULL);
1165 XA_BUG_ON(xa, xa_load(xa, last + 1) != NULL);
1167 xa_store_range(xa, first, last, NULL, GFP_KERNEL);
1170 XA_BUG_ON(xa, !xa_empty(xa));
1173 static noinline void check_store_range(struct xarray *xa)
1177 for (i = 0; i < 128; i++) {
1178 for (j = i; j < 128; j++) {
1179 __check_store_range(xa, i, j);
1180 __check_store_range(xa, 128 + i, 128 + j);
1181 __check_store_range(xa, 4095 + i, 4095 + j);
1182 __check_store_range(xa, 4096 + i, 4096 + j);
1183 __check_store_range(xa, 123456 + i, 123456 + j);
1184 __check_store_range(xa, (1 << 24) + i, (1 << 24) + j);
1189 static void check_align_1(struct xarray *xa, char *name)
1193 unsigned long index;
1196 for (i = 0; i < 8; i++) {
1198 XA_BUG_ON(xa, xa_alloc(xa, &id, UINT_MAX, name + i, GFP_KERNEL)
1200 XA_BUG_ON(xa, id != i);
1202 xa_for_each(xa, index, entry)
1203 XA_BUG_ON(xa, xa_is_err(entry));
1207 static noinline void check_align(struct xarray *xa)
1209 char name[] = "Motorola 68000";
1211 check_align_1(xa, name);
1212 check_align_1(xa, name + 1);
1213 check_align_1(xa, name + 2);
1214 check_align_1(xa, name + 3);
1215 // check_align_2(xa, name);
1218 static LIST_HEAD(shadow_nodes);
1220 static void test_update_node(struct xa_node *node)
1222 if (node->count && node->count == node->nr_values) {
1223 if (list_empty(&node->private_list))
1224 list_add(&shadow_nodes, &node->private_list);
1226 if (!list_empty(&node->private_list))
1227 list_del_init(&node->private_list);
1231 static noinline void shadow_remove(struct xarray *xa)
1233 struct xa_node *node;
1236 while ((node = list_first_entry_or_null(&shadow_nodes,
1237 struct xa_node, private_list))) {
1238 XA_STATE(xas, node->array, 0);
1239 XA_BUG_ON(xa, node->array != xa);
1240 list_del_init(&node->private_list);
1241 xas.xa_node = xa_parent_locked(node->array, node);
1242 xas.xa_offset = node->offset;
1243 xas.xa_shift = node->shift + XA_CHUNK_SHIFT;
1244 xas_set_update(&xas, test_update_node);
1245 xas_store(&xas, NULL);
1250 static noinline void check_workingset(struct xarray *xa, unsigned long index)
1252 XA_STATE(xas, xa, index);
1253 xas_set_update(&xas, test_update_node);
1257 xas_store(&xas, xa_mk_value(0));
1259 xas_store(&xas, xa_mk_value(1));
1261 } while (xas_nomem(&xas, GFP_KERNEL));
1263 XA_BUG_ON(xa, list_empty(&shadow_nodes));
1267 xas_store(&xas, &xas);
1268 XA_BUG_ON(xa, !list_empty(&shadow_nodes));
1270 xas_store(&xas, xa_mk_value(2));
1272 XA_BUG_ON(xa, list_empty(&shadow_nodes));
1275 XA_BUG_ON(xa, !list_empty(&shadow_nodes));
1276 XA_BUG_ON(xa, !xa_empty(xa));
1280 * Check that the pointer / value / sibling entries are accounted the
1281 * way we expect them to be.
1283 static noinline void check_account(struct xarray *xa)
1285 #ifdef CONFIG_XARRAY_MULTI
1288 for (order = 1; order < 12; order++) {
1289 XA_STATE(xas, xa, 1 << order);
1291 xa_store_order(xa, 0, order, xa, GFP_KERNEL);
1294 XA_BUG_ON(xa, xas.xa_node->count == 0);
1295 XA_BUG_ON(xa, xas.xa_node->count > (1 << order));
1296 XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
1299 xa_store_order(xa, 1 << order, order, xa_mk_index(1UL << order),
1301 XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2);
1303 xa_erase(xa, 1 << order);
1304 XA_BUG_ON(xa, xas.xa_node->nr_values != 0);
1307 XA_BUG_ON(xa, !xa_empty(xa));
1312 static noinline void check_destroy(struct xarray *xa)
1314 unsigned long index;
1316 XA_BUG_ON(xa, !xa_empty(xa));
1318 /* Destroying an empty array is a no-op */
1320 XA_BUG_ON(xa, !xa_empty(xa));
1322 /* Destroying an array with a single entry */
1323 for (index = 0; index < 1000; index++) {
1324 xa_store_index(xa, index, GFP_KERNEL);
1325 XA_BUG_ON(xa, xa_empty(xa));
1327 XA_BUG_ON(xa, !xa_empty(xa));
1330 /* Destroying an array with a single entry at ULONG_MAX */
1331 xa_store(xa, ULONG_MAX, xa, GFP_KERNEL);
1332 XA_BUG_ON(xa, xa_empty(xa));
1334 XA_BUG_ON(xa, !xa_empty(xa));
1336 #ifdef CONFIG_XARRAY_MULTI
1337 /* Destroying an array with a multi-index entry */
1338 xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL);
1339 XA_BUG_ON(xa, xa_empty(xa));
1341 XA_BUG_ON(xa, !xa_empty(xa));
1345 static DEFINE_XARRAY(array);
1347 static int xarray_checks(void)
1349 check_xa_err(&array);
1350 check_xas_retry(&array);
1351 check_xa_load(&array);
1352 check_xa_mark(&array);
1353 check_xa_shrink(&array);
1354 check_xas_erase(&array);
1355 check_cmpxchg(&array);
1356 check_reserve(&array);
1357 check_multi_store(&array);
1360 check_find_entry(&array);
1361 check_account(&array);
1362 check_destroy(&array);
1364 check_create_range(&array);
1365 check_store_range(&array);
1366 check_store_iter(&array);
1369 check_workingset(&array, 0);
1370 check_workingset(&array, 64);
1371 check_workingset(&array, 4096);
1373 printk("XArray: %u of %u tests passed\n", tests_passed, tests_run);
1374 return (tests_run == tests_passed) ? 0 : -EINVAL;
1377 static void xarray_exit(void)
1381 module_init(xarray_checks);
1382 module_exit(xarray_exit);
1383 MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
1384 MODULE_LICENSE("GPL");