diff options
| author | Matthew Wilcox <willy@infradead.org> | 2018-07-04 10:50:12 -0400 |
|---|---|---|
| committer | Matthew Wilcox <willy@infradead.org> | 2018-10-21 10:46:32 -0400 |
| commit | 371c752dc66948714ee3b66c3306f3ff1ff71d2e (patch) | |
| tree | a9707d94787474e20b4efcd9bc81d5e6e1328a66 /lib/test_xarray.c | |
| parent | 3d5bd6e1a04ae779bc5e0de9ba2e92aa55c40fe8 (diff) | |
xarray: Track free entries in an XArray
Add the optional ability to track which entries in an XArray are free
and provide xa_alloc() to replace most of the functionality of the IDR.
Signed-off-by: Matthew Wilcox <willy@infradead.org>
Diffstat (limited to 'lib/test_xarray.c')
| -rw-r--r-- | lib/test_xarray.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/test_xarray.c b/lib/test_xarray.c index 6aafd411a5c3..a752e6a37e6f 100644 --- a/lib/test_xarray.c +++ b/lib/test_xarray.c | |||
| @@ -33,6 +33,15 @@ static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp) | |||
| 33 | return xa_store(xa, index, xa_mk_value(index & LONG_MAX), gfp); | 33 | return xa_store(xa, index, xa_mk_value(index & LONG_MAX), gfp); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp) | ||
| 37 | { | ||
| 38 | u32 id = 0; | ||
| 39 | |||
| 40 | XA_BUG_ON(xa, xa_alloc(xa, &id, UINT_MAX, xa_mk_value(index & LONG_MAX), | ||
| 41 | gfp) != 0); | ||
| 42 | XA_BUG_ON(xa, id != index); | ||
| 43 | } | ||
| 44 | |||
| 36 | static void xa_erase_index(struct xarray *xa, unsigned long index) | 45 | static void xa_erase_index(struct xarray *xa, unsigned long index) |
| 37 | { | 46 | { |
| 38 | XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_value(index & LONG_MAX)); | 47 | XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_value(index & LONG_MAX)); |
| @@ -404,6 +413,57 @@ static noinline void check_multi_store(struct xarray *xa) | |||
| 404 | #endif | 413 | #endif |
| 405 | } | 414 | } |
| 406 | 415 | ||
| 416 | static DEFINE_XARRAY_ALLOC(xa0); | ||
| 417 | |||
| 418 | static noinline void check_xa_alloc(void) | ||
| 419 | { | ||
| 420 | int i; | ||
| 421 | u32 id; | ||
| 422 | |||
| 423 | /* An empty array should assign 0 to the first alloc */ | ||
| 424 | xa_alloc_index(&xa0, 0, GFP_KERNEL); | ||
| 425 | |||
| 426 | /* Erasing it should make the array empty again */ | ||
| 427 | xa_erase_index(&xa0, 0); | ||
| 428 | XA_BUG_ON(&xa0, !xa_empty(&xa0)); | ||
| 429 | |||
| 430 | /* And it should assign 0 again */ | ||
| 431 | xa_alloc_index(&xa0, 0, GFP_KERNEL); | ||
| 432 | |||
| 433 | /* The next assigned ID should be 1 */ | ||
| 434 | xa_alloc_index(&xa0, 1, GFP_KERNEL); | ||
| 435 | xa_erase_index(&xa0, 1); | ||
| 436 | |||
| 437 | /* Storing a value should mark it used */ | ||
| 438 | xa_store_index(&xa0, 1, GFP_KERNEL); | ||
| 439 | xa_alloc_index(&xa0, 2, GFP_KERNEL); | ||
| 440 | |||
| 441 | /* If we then erase 0, it should be free */ | ||
| 442 | xa_erase_index(&xa0, 0); | ||
| 443 | xa_alloc_index(&xa0, 0, GFP_KERNEL); | ||
| 444 | |||
| 445 | xa_erase_index(&xa0, 1); | ||
| 446 | xa_erase_index(&xa0, 2); | ||
| 447 | |||
| 448 | for (i = 1; i < 5000; i++) { | ||
| 449 | xa_alloc_index(&xa0, i, GFP_KERNEL); | ||
| 450 | } | ||
| 451 | |||
| 452 | xa_destroy(&xa0); | ||
| 453 | |||
| 454 | id = 0xfffffffeU; | ||
| 455 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), | ||
| 456 | GFP_KERNEL) != 0); | ||
| 457 | XA_BUG_ON(&xa0, id != 0xfffffffeU); | ||
| 458 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), | ||
| 459 | GFP_KERNEL) != 0); | ||
| 460 | XA_BUG_ON(&xa0, id != 0xffffffffU); | ||
| 461 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), | ||
| 462 | GFP_KERNEL) != -ENOSPC); | ||
| 463 | XA_BUG_ON(&xa0, id != 0xffffffffU); | ||
| 464 | xa_destroy(&xa0); | ||
| 465 | } | ||
| 466 | |||
| 407 | static noinline void __check_store_iter(struct xarray *xa, unsigned long start, | 467 | static noinline void __check_store_iter(struct xarray *xa, unsigned long start, |
| 408 | unsigned int order, unsigned int present) | 468 | unsigned int order, unsigned int present) |
| 409 | { | 469 | { |
| @@ -849,6 +909,7 @@ static int xarray_checks(void) | |||
| 849 | check_cmpxchg(&array); | 909 | check_cmpxchg(&array); |
| 850 | check_reserve(&array); | 910 | check_reserve(&array); |
| 851 | check_multi_store(&array); | 911 | check_multi_store(&array); |
| 912 | check_xa_alloc(); | ||
| 852 | check_find(&array); | 913 | check_find(&array); |
| 853 | check_destroy(&array); | 914 | check_destroy(&array); |
| 854 | check_move(&array); | 915 | check_move(&array); |
