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.
Showing the changes to lib/test_xarray.c (1 of 4 files). Show all
Unified · Split
| @@ -33,6 +33,15 @@ static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp) · 32 unmodified lines | ||
| 33 | 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 | 45 | static void xa_erase_index(struct xarray *xa, unsigned long index) |
| 37 | 46 | { |
| 38 | 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) · 365 unmodified lines | ||
| 404 | 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 | 467 | static noinline void __check_store_iter(struct xarray *xa, unsigned long start, |
| 408 | 468 | unsigned int order, unsigned int present) |
| 409 | 469 | { |
| @@ -849,6 +909,7 @@ static int xarray_checks(void) · 439 unmodified lines | ||
| 849 | 909 | check_cmpxchg(&array); |
| 850 | 910 | check_reserve(&array); |
| 851 | 911 | check_multi_store(&array); |
| 912 | + check_xa_alloc(); | |
| 852 | 913 | check_find(&array); |
| 853 | 914 | check_destroy(&array); |
| 854 | 915 | check_move(&array); |