aboutsummaryrefslogtreecommitdiffstats
path: root/lib/test_xarray.c
diff options
context:
space:
mode:
authorMatthew Wilcox <willy@infradead.org>2017-11-14 08:30:11 -0500
committerMatthew Wilcox <willy@infradead.org>2018-10-21 10:45:58 -0400
commitb803b42823d0d9e8b6deccf01ffc2aba5d0738df (patch)
treea3ea387087a9f64258e0bc9300f92ee63d1313fa /lib/test_xarray.c
parent41aec91f55985e7f14ee75fe2f6e7bcfff0d0fae (diff)
xarray: Add XArray iterators
The xa_for_each iterator allows the user to efficiently walk a range of the array, executing the loop body once for each entry in that range that matches the filter. This commit also includes xa_find() and xa_find_after() which are helper functions for xa_for_each() but may also be useful in their own right. In the xas family of functions, we have xas_for_each(), xas_find(), xas_next_entry(), xas_for_each_tagged(), xas_find_tagged(), xas_next_tagged() and xas_pause(). Signed-off-by: Matthew Wilcox <willy@infradead.org>
Diffstat (limited to 'lib/test_xarray.c')
-rw-r--r--lib/test_xarray.c183
1 files changed, 183 insertions, 0 deletions
diff --git a/lib/test_xarray.c b/lib/test_xarray.c
index fb472258b639..e3c2d4d00b15 100644
--- a/lib/test_xarray.c
+++ b/lib/test_xarray.c
@@ -75,6 +75,48 @@ static noinline void check_xa_err(struct xarray *xa)
75// XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL); 75// XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL);
76} 76}
77 77
78static noinline void check_xas_retry(struct xarray *xa)
79{
80 XA_STATE(xas, xa, 0);
81 void *entry;
82
83 xa_store_index(xa, 0, GFP_KERNEL);
84 xa_store_index(xa, 1, GFP_KERNEL);
85
86 rcu_read_lock();
87 XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_value(0));
88 xa_erase_index(xa, 1);
89 XA_BUG_ON(xa, !xa_is_retry(xas_reload(&xas)));
90 XA_BUG_ON(xa, xas_retry(&xas, NULL));
91 XA_BUG_ON(xa, xas_retry(&xas, xa_mk_value(0)));
92 xas_reset(&xas);
93 XA_BUG_ON(xa, xas.xa_node != XAS_RESTART);
94 XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0));
95 XA_BUG_ON(xa, xas.xa_node != NULL);
96
97 XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
98 XA_BUG_ON(xa, !xa_is_internal(xas_reload(&xas)));
99 xas.xa_node = XAS_RESTART;
100 XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0));
101 rcu_read_unlock();
102
103 /* Make sure we can iterate through retry entries */
104 xas_lock(&xas);
105 xas_set(&xas, 0);
106 xas_store(&xas, XA_RETRY_ENTRY);
107 xas_set(&xas, 1);
108 xas_store(&xas, XA_RETRY_ENTRY);
109
110 xas_set(&xas, 0);
111 xas_for_each(&xas, entry, ULONG_MAX) {
112 xas_store(&xas, xa_mk_value(xas.xa_index));
113 }
114 xas_unlock(&xas);
115
116 xa_erase_index(xa, 0);
117 xa_erase_index(xa, 1);
118}
119
78static noinline void check_xa_load(struct xarray *xa) 120static noinline void check_xa_load(struct xarray *xa)
79{ 121{
80 unsigned long i, j; 122 unsigned long i, j;
@@ -217,6 +259,44 @@ static noinline void check_cmpxchg(struct xarray *xa)
217 XA_BUG_ON(xa, !xa_empty(xa)); 259 XA_BUG_ON(xa, !xa_empty(xa));
218} 260}
219 261
262static noinline void check_xas_erase(struct xarray *xa)
263{
264 XA_STATE(xas, xa, 0);
265 void *entry;
266 unsigned long i, j;
267
268 for (i = 0; i < 200; i++) {
269 for (j = i; j < 2 * i + 17; j++) {
270 xas_set(&xas, j);
271 do {
272 xas_lock(&xas);
273 xas_store(&xas, xa_mk_value(j));
274 xas_unlock(&xas);
275 } while (xas_nomem(&xas, GFP_KERNEL));
276 }
277
278 xas_set(&xas, ULONG_MAX);
279 do {
280 xas_lock(&xas);
281 xas_store(&xas, xa_mk_value(0));
282 xas_unlock(&xas);
283 } while (xas_nomem(&xas, GFP_KERNEL));
284
285 xas_lock(&xas);
286 xas_store(&xas, NULL);
287
288 xas_set(&xas, 0);
289 j = i;
290 xas_for_each(&xas, entry, ULONG_MAX) {
291 XA_BUG_ON(xa, entry != xa_mk_value(j));
292 xas_store(&xas, NULL);
293 j++;
294 }
295 xas_unlock(&xas);
296 XA_BUG_ON(xa, !xa_empty(xa));
297 }
298}
299
220static noinline void check_multi_store(struct xarray *xa) 300static noinline void check_multi_store(struct xarray *xa)
221{ 301{
222#ifdef CONFIG_XARRAY_MULTI 302#ifdef CONFIG_XARRAY_MULTI
@@ -285,16 +365,119 @@ static noinline void check_multi_store(struct xarray *xa)
285#endif 365#endif
286} 366}
287 367
368static noinline void check_multi_find(struct xarray *xa)
369{
370#ifdef CONFIG_XARRAY_MULTI
371 unsigned long index;
372
373 xa_store_order(xa, 12, 2, xa_mk_value(12), GFP_KERNEL);
374 XA_BUG_ON(xa, xa_store_index(xa, 16, GFP_KERNEL) != NULL);
375
376 index = 0;
377 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
378 xa_mk_value(12));
379 XA_BUG_ON(xa, index != 12);
380 index = 13;
381 XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
382 xa_mk_value(12));
383 XA_BUG_ON(xa, (index < 12) || (index >= 16));
384 XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
385 xa_mk_value(16));
386 XA_BUG_ON(xa, index != 16);
387
388 xa_erase_index(xa, 12);
389 xa_erase_index(xa, 16);
390 XA_BUG_ON(xa, !xa_empty(xa));
391#endif
392}
393
394static noinline void check_multi_find_2(struct xarray *xa)
395{
396 unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1;
397 unsigned int i, j;
398 void *entry;
399
400 for (i = 0; i < max_order; i++) {
401 unsigned long index = 1UL << i;
402 for (j = 0; j < index; j++) {
403 XA_STATE(xas, xa, j + index);
404 xa_store_index(xa, index - 1, GFP_KERNEL);
405 xa_store_order(xa, index, i, xa_mk_value(index),
406 GFP_KERNEL);
407 rcu_read_lock();
408 xas_for_each(&xas, entry, ULONG_MAX) {
409 xa_erase_index(xa, index);
410 }
411 rcu_read_unlock();
412 xa_erase_index(xa, index - 1);
413 XA_BUG_ON(xa, !xa_empty(xa));
414 }
415 }
416}
417
418static noinline void check_find(struct xarray *xa)
419{
420 unsigned long i, j, k;
421
422 XA_BUG_ON(xa, !xa_empty(xa));
423
424 /*
425 * Check xa_find with all pairs between 0 and 99 inclusive,
426 * starting at every index between 0 and 99
427 */
428 for (i = 0; i < 100; i++) {
429 XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
430 xa_set_mark(xa, i, XA_MARK_0);
431 for (j = 0; j < i; j++) {
432 XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) !=
433 NULL);
434 xa_set_mark(xa, j, XA_MARK_0);
435 for (k = 0; k < 100; k++) {
436 unsigned long index = k;
437 void *entry = xa_find(xa, &index, ULONG_MAX,
438 XA_PRESENT);
439 if (k <= j)
440 XA_BUG_ON(xa, index != j);
441 else if (k <= i)
442 XA_BUG_ON(xa, index != i);
443 else
444 XA_BUG_ON(xa, entry != NULL);
445
446 index = k;
447 entry = xa_find(xa, &index, ULONG_MAX,
448 XA_MARK_0);
449 if (k <= j)
450 XA_BUG_ON(xa, index != j);
451 else if (k <= i)
452 XA_BUG_ON(xa, index != i);
453 else
454 XA_BUG_ON(xa, entry != NULL);
455 }
456 xa_erase_index(xa, j);
457 XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0));
458 XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
459 }
460 xa_erase_index(xa, i);
461 XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0));
462 }
463 XA_BUG_ON(xa, !xa_empty(xa));
464 check_multi_find(xa);
465 check_multi_find_2(xa);
466}
467
288static DEFINE_XARRAY(array); 468static DEFINE_XARRAY(array);
289 469
290static int xarray_checks(void) 470static int xarray_checks(void)
291{ 471{
292 check_xa_err(&array); 472 check_xa_err(&array);
473 check_xas_retry(&array);
293 check_xa_load(&array); 474 check_xa_load(&array);
294 check_xa_mark(&array); 475 check_xa_mark(&array);
295 check_xa_shrink(&array); 476 check_xa_shrink(&array);
477 check_xas_erase(&array);
296 check_cmpxchg(&array); 478 check_cmpxchg(&array);
297 check_multi_store(&array); 479 check_multi_store(&array);
480 check_find(&array);
298 481
299 printk("XArray: %u of %u tests passed\n", tests_passed, tests_run); 482 printk("XArray: %u of %u tests passed\n", tests_passed, tests_run);
300 return (tests_run == tests_passed) ? 0 : -EINVAL; 483 return (tests_run == tests_passed) ? 0 : -EINVAL;