]>
Commit | Line | Data |
---|---|---|
8ab8ba38 MW |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * test_ida.c: Test the IDA API | |
4 | * Copyright (c) 2016-2018 Microsoft Corporation | |
5 | * Copyright (c) 2018 Oracle Corporation | |
6 | * Author: Matthew Wilcox <willy@infradead.org> | |
7 | */ | |
8 | ||
9 | #include <linux/idr.h> | |
10 | #include <linux/module.h> | |
11 | ||
12 | static unsigned int tests_run; | |
13 | static unsigned int tests_passed; | |
14 | ||
15 | #ifdef __KERNEL__ | |
16 | void ida_dump(struct ida *ida) { } | |
17 | #endif | |
18 | #define IDA_BUG_ON(ida, x) do { \ | |
19 | tests_run++; \ | |
20 | if (x) { \ | |
21 | ida_dump(ida); \ | |
22 | dump_stack(); \ | |
23 | } else { \ | |
24 | tests_passed++; \ | |
25 | } \ | |
26 | } while (0) | |
27 | ||
0a385639 MW |
28 | /* |
29 | * Check what happens when we fill a leaf and then delete it. This may | |
30 | * discover mishandling of IDR_FREE. | |
31 | */ | |
32 | static void ida_check_leaf(struct ida *ida, unsigned int base) | |
33 | { | |
34 | unsigned long i; | |
35 | ||
36 | for (i = 0; i < IDA_BITMAP_BITS; i++) { | |
37 | IDA_BUG_ON(ida, ida_alloc_min(ida, base, GFP_KERNEL) != | |
38 | base + i); | |
39 | } | |
40 | ||
41 | ida_destroy(ida); | |
42 | IDA_BUG_ON(ida, !ida_is_empty(ida)); | |
43 | ||
44 | IDA_BUG_ON(ida, ida_alloc(ida, GFP_KERNEL) != 0); | |
45 | IDA_BUG_ON(ida, ida_is_empty(ida)); | |
46 | ida_free(ida, 0); | |
47 | IDA_BUG_ON(ida, !ida_is_empty(ida)); | |
48 | } | |
49 | ||
8ab8ba38 MW |
50 | static int ida_checks(void) |
51 | { | |
52 | DEFINE_IDA(ida); | |
53 | ||
54 | IDA_BUG_ON(&ida, !ida_is_empty(&ida)); | |
0a385639 MW |
55 | ida_check_leaf(&ida, 0); |
56 | ida_check_leaf(&ida, 1024); | |
57 | ida_check_leaf(&ida, 1024 * 64); | |
8ab8ba38 MW |
58 | |
59 | printk("IDA: %u of %u tests passed\n", tests_passed, tests_run); | |
60 | return (tests_run != tests_passed) ? 0 : -EINVAL; | |
61 | } | |
62 | ||
63 | static void ida_exit(void) | |
64 | { | |
65 | } | |
66 | ||
67 | module_init(ida_checks); | |
68 | module_exit(ida_exit); | |
69 | MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>"); | |
70 | MODULE_LICENSE("GPL"); |