diff options
author | Matthew Wilcox <willy@infradead.org> | 2018-06-18 18:39:28 -0400 |
---|---|---|
committer | Matthew Wilcox <willy@infradead.org> | 2018-08-21 23:54:21 -0400 |
commit | f272668deb9108b6118a85ffd73886b9a92c1002 (patch) | |
tree | 2e0b2713c5ef5734d15564fe18d3cba10eaa27e4 | |
parent | 5c78b0b1ebe16fbae39a1cada79ab067965828f5 (diff) |
test_ida: check_ida_destroy and check_ida_alloc
Move these tests from the userspace test-suite to the kernel test-suite.
Also convert check_ida_random to the new API.
Signed-off-by: Matthew Wilcox <willy@infradead.org>
-rw-r--r-- | lib/test_ida.c | 54 | ||||
-rw-r--r-- | tools/testing/radix-tree/idr-test.c | 70 |
2 files changed, 58 insertions, 66 deletions
diff --git a/lib/test_ida.c b/lib/test_ida.c index eaee9a28f325..2d1637d8136b 100644 --- a/lib/test_ida.c +++ b/lib/test_ida.c | |||
@@ -26,6 +26,58 @@ void ida_dump(struct ida *ida) { } | |||
26 | } while (0) | 26 | } while (0) |
27 | 27 | ||
28 | /* | 28 | /* |
29 | * Straightforward checks that allocating and freeing IDs work. | ||
30 | */ | ||
31 | static void ida_check_alloc(struct ida *ida) | ||
32 | { | ||
33 | int i, id; | ||
34 | |||
35 | for (i = 0; i < 10000; i++) | ||
36 | IDA_BUG_ON(ida, ida_alloc(ida, GFP_KERNEL) != i); | ||
37 | |||
38 | ida_free(ida, 20); | ||
39 | ida_free(ida, 21); | ||
40 | for (i = 0; i < 3; i++) { | ||
41 | id = ida_alloc(ida, GFP_KERNEL); | ||
42 | IDA_BUG_ON(ida, id < 0); | ||
43 | if (i == 2) | ||
44 | IDA_BUG_ON(ida, id != 10000); | ||
45 | } | ||
46 | |||
47 | for (i = 0; i < 5000; i++) | ||
48 | ida_free(ida, i); | ||
49 | |||
50 | IDA_BUG_ON(ida, ida_alloc_min(ida, 5000, GFP_KERNEL) != 10001); | ||
51 | ida_destroy(ida); | ||
52 | |||
53 | IDA_BUG_ON(ida, !ida_is_empty(ida)); | ||
54 | } | ||
55 | |||
56 | /* Destroy an IDA with a single entry at @base */ | ||
57 | static void ida_check_destroy_1(struct ida *ida, unsigned int base) | ||
58 | { | ||
59 | IDA_BUG_ON(ida, ida_alloc_min(ida, base, GFP_KERNEL) != base); | ||
60 | IDA_BUG_ON(ida, ida_is_empty(ida)); | ||
61 | ida_destroy(ida); | ||
62 | IDA_BUG_ON(ida, !ida_is_empty(ida)); | ||
63 | } | ||
64 | |||
65 | /* Check that ida_destroy and ida_is_empty work */ | ||
66 | static void ida_check_destroy(struct ida *ida) | ||
67 | { | ||
68 | /* Destroy an already-empty IDA */ | ||
69 | IDA_BUG_ON(ida, !ida_is_empty(ida)); | ||
70 | ida_destroy(ida); | ||
71 | IDA_BUG_ON(ida, !ida_is_empty(ida)); | ||
72 | |||
73 | ida_check_destroy_1(ida, 0); | ||
74 | ida_check_destroy_1(ida, 1); | ||
75 | ida_check_destroy_1(ida, 1023); | ||
76 | ida_check_destroy_1(ida, 1024); | ||
77 | ida_check_destroy_1(ida, 12345678); | ||
78 | } | ||
79 | |||
80 | /* | ||
29 | * Check what happens when we fill a leaf and then delete it. This may | 81 | * Check what happens when we fill a leaf and then delete it. This may |
30 | * discover mishandling of IDR_FREE. | 82 | * discover mishandling of IDR_FREE. |
31 | */ | 83 | */ |
@@ -103,6 +155,8 @@ static int ida_checks(void) | |||
103 | DEFINE_IDA(ida); | 155 | DEFINE_IDA(ida); |
104 | 156 | ||
105 | IDA_BUG_ON(&ida, !ida_is_empty(&ida)); | 157 | IDA_BUG_ON(&ida, !ida_is_empty(&ida)); |
158 | ida_check_alloc(&ida); | ||
159 | ida_check_destroy(&ida); | ||
106 | ida_check_leaf(&ida, 0); | 160 | ida_check_leaf(&ida, 0); |
107 | ida_check_leaf(&ida, 1024); | 161 | ida_check_leaf(&ida, 1024); |
108 | ida_check_leaf(&ida, 1024 * 64); | 162 | ida_check_leaf(&ida, 1024 * 64); |
diff --git a/tools/testing/radix-tree/idr-test.c b/tools/testing/radix-tree/idr-test.c index c6026cfe3145..321ba92c70d2 100644 --- a/tools/testing/radix-tree/idr-test.c +++ b/tools/testing/radix-tree/idr-test.c | |||
@@ -364,7 +364,6 @@ void ida_check_random(void) | |||
364 | { | 364 | { |
365 | DEFINE_IDA(ida); | 365 | DEFINE_IDA(ida); |
366 | DECLARE_BITMAP(bitmap, 2048); | 366 | DECLARE_BITMAP(bitmap, 2048); |
367 | int id, err; | ||
368 | unsigned int i; | 367 | unsigned int i; |
369 | time_t s = time(NULL); | 368 | time_t s = time(NULL); |
370 | 369 | ||
@@ -375,15 +374,11 @@ void ida_check_random(void) | |||
375 | int bit = i & 2047; | 374 | int bit = i & 2047; |
376 | if (test_bit(bit, bitmap)) { | 375 | if (test_bit(bit, bitmap)) { |
377 | __clear_bit(bit, bitmap); | 376 | __clear_bit(bit, bitmap); |
378 | ida_remove(&ida, bit); | 377 | ida_free(&ida, bit); |
379 | } else { | 378 | } else { |
380 | __set_bit(bit, bitmap); | 379 | __set_bit(bit, bitmap); |
381 | do { | 380 | IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL) |
382 | ida_pre_get(&ida, GFP_KERNEL); | 381 | != bit); |
383 | err = ida_get_new_above(&ida, bit, &id); | ||
384 | } while (err == -EAGAIN); | ||
385 | assert(!err); | ||
386 | assert(id == bit); | ||
387 | } | 382 | } |
388 | } | 383 | } |
389 | ida_destroy(&ida); | 384 | ida_destroy(&ida); |
@@ -411,66 +406,9 @@ void ida_simple_get_remove_test(void) | |||
411 | 406 | ||
412 | void user_ida_checks(void) | 407 | void user_ida_checks(void) |
413 | { | 408 | { |
414 | DEFINE_IDA(ida); | ||
415 | int id; | ||
416 | unsigned long i; | ||
417 | |||
418 | radix_tree_cpu_dead(1); | 409 | radix_tree_cpu_dead(1); |
419 | ida_check_nomem(); | ||
420 | |||
421 | for (i = 0; i < 10000; i++) { | ||
422 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
423 | assert(!ida_get_new(&ida, &id)); | ||
424 | assert(id == i); | ||
425 | } | ||
426 | |||
427 | ida_remove(&ida, 20); | ||
428 | ida_remove(&ida, 21); | ||
429 | for (i = 0; i < 3; i++) { | ||
430 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
431 | assert(!ida_get_new(&ida, &id)); | ||
432 | if (i == 2) | ||
433 | assert(id == 10000); | ||
434 | } | ||
435 | |||
436 | for (i = 0; i < 5000; i++) | ||
437 | ida_remove(&ida, i); | ||
438 | |||
439 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
440 | assert(!ida_get_new_above(&ida, 5000, &id)); | ||
441 | assert(id == 10001); | ||
442 | |||
443 | ida_destroy(&ida); | ||
444 | |||
445 | assert(ida_is_empty(&ida)); | ||
446 | |||
447 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
448 | assert(!ida_get_new_above(&ida, 1, &id)); | ||
449 | assert(id == 1); | ||
450 | |||
451 | ida_remove(&ida, id); | ||
452 | assert(ida_is_empty(&ida)); | ||
453 | ida_destroy(&ida); | ||
454 | assert(ida_is_empty(&ida)); | ||
455 | |||
456 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
457 | assert(!ida_get_new_above(&ida, 1, &id)); | ||
458 | ida_destroy(&ida); | ||
459 | assert(ida_is_empty(&ida)); | ||
460 | |||
461 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
462 | assert(!ida_get_new_above(&ida, 1, &id)); | ||
463 | assert(id == 1); | ||
464 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
465 | assert(!ida_get_new_above(&ida, 1025, &id)); | ||
466 | assert(id == 1025); | ||
467 | assert(ida_pre_get(&ida, GFP_KERNEL)); | ||
468 | assert(!ida_get_new_above(&ida, 10000, &id)); | ||
469 | assert(id == 10000); | ||
470 | ida_remove(&ida, 1025); | ||
471 | ida_destroy(&ida); | ||
472 | assert(ida_is_empty(&ida)); | ||
473 | 410 | ||
411 | ida_check_nomem(); | ||
474 | ida_check_conv_user(); | 412 | ida_check_conv_user(); |
475 | ida_check_random(); | 413 | ida_check_random(); |
476 | ida_simple_get_remove_test(); | 414 | ida_simple_get_remove_test(); |