Skip to content

Commit f272668

Browse files
author
Matthew Wilcox
committed
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>
1 parent 5c78b0b commit f272668

File tree

2 files changed

+58
-66
lines changed

2 files changed

+58
-66
lines changed

lib/test_ida.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,58 @@ void ida_dump(struct ida *ida) { }
2525
} \
2626
} while (0)
2727

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+
2880
/*
2981
* Check what happens when we fill a leaf and then delete it. This may
3082
* discover mishandling of IDR_FREE.
@@ -103,6 +155,8 @@ static int ida_checks(void)
103155
DEFINE_IDA(ida);
104156

105157
IDA_BUG_ON(&ida, !ida_is_empty(&ida));
158+
ida_check_alloc(&ida);
159+
ida_check_destroy(&ida);
106160
ida_check_leaf(&ida, 0);
107161
ida_check_leaf(&ida, 1024);
108162
ida_check_leaf(&ida, 1024 * 64);

tools/testing/radix-tree/idr-test.c

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ void ida_check_random(void)
364364
{
365365
DEFINE_IDA(ida);
366366
DECLARE_BITMAP(bitmap, 2048);
367-
int id, err;
368367
unsigned int i;
369368
time_t s = time(NULL);
370369

@@ -375,15 +374,11 @@ void ida_check_random(void)
375374
int bit = i & 2047;
376375
if (test_bit(bit, bitmap)) {
377376
__clear_bit(bit, bitmap);
378-
ida_remove(&ida, bit);
377+
ida_free(&ida, bit);
379378
} else {
380379
__set_bit(bit, bitmap);
381-
do {
382-
ida_pre_get(&ida, GFP_KERNEL);
383-
err = ida_get_new_above(&ida, bit, &id);
384-
} while (err == -EAGAIN);
385-
assert(!err);
386-
assert(id == bit);
380+
IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL)
381+
!= bit);
387382
}
388383
}
389384
ida_destroy(&ida);
@@ -411,66 +406,9 @@ void ida_simple_get_remove_test(void)
411406

412407
void user_ida_checks(void)
413408
{
414-
DEFINE_IDA(ida);
415-
int id;
416-
unsigned long i;
417-
418409
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));
473410

411+
ida_check_nomem();
474412
ida_check_conv_user();
475413
ida_check_random();
476414
ida_simple_get_remove_test();

0 commit comments

Comments
 (0)