Matthew Wilcox | 8ab8ba3 | 2018-06-18 16:59:29 -0400 | [diff] [blame] | 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 | |
Matthew Wilcox | 0a38563 | 2018-06-18 17:23:37 -0400 | [diff] [blame] | 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 | |
Matthew Wilcox | 161b47e | 2018-06-18 17:25:20 -0400 | [diff] [blame] | 50 | /* |
| 51 | * Check allocations up to and slightly above the maximum allowed (2^31-1) ID. |
| 52 | * Allocating up to 2^31-1 should succeed, and then allocating the next one |
| 53 | * should fail. |
| 54 | */ |
| 55 | static void ida_check_max(struct ida *ida) |
| 56 | { |
| 57 | unsigned long i, j; |
| 58 | |
| 59 | for (j = 1; j < 65537; j *= 2) { |
| 60 | unsigned long base = (1UL << 31) - j; |
| 61 | for (i = 0; i < j; i++) { |
| 62 | IDA_BUG_ON(ida, ida_alloc_min(ida, base, GFP_KERNEL) != |
| 63 | base + i); |
| 64 | } |
| 65 | IDA_BUG_ON(ida, ida_alloc_min(ida, base, GFP_KERNEL) != |
| 66 | -ENOSPC); |
| 67 | ida_destroy(ida); |
| 68 | IDA_BUG_ON(ida, !ida_is_empty(ida)); |
| 69 | } |
| 70 | } |
| 71 | |
Matthew Wilcox | 5c78b0b | 2018-06-18 18:10:32 -0400 | [diff] [blame^] | 72 | /* |
| 73 | * Check handling of conversions between exceptional entries and full bitmaps. |
| 74 | */ |
| 75 | static void ida_check_conv(struct ida *ida) |
| 76 | { |
| 77 | unsigned long i; |
| 78 | |
| 79 | for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) { |
| 80 | IDA_BUG_ON(ida, ida_alloc_min(ida, i + 1, GFP_KERNEL) != i + 1); |
| 81 | IDA_BUG_ON(ida, ida_alloc_min(ida, i + BITS_PER_LONG, |
| 82 | GFP_KERNEL) != i + BITS_PER_LONG); |
| 83 | ida_free(ida, i + 1); |
| 84 | ida_free(ida, i + BITS_PER_LONG); |
| 85 | IDA_BUG_ON(ida, !ida_is_empty(ida)); |
| 86 | } |
| 87 | |
| 88 | for (i = 0; i < IDA_BITMAP_BITS * 2; i++) |
| 89 | IDA_BUG_ON(ida, ida_alloc(ida, GFP_KERNEL) != i); |
| 90 | for (i = IDA_BITMAP_BITS * 2; i > 0; i--) |
| 91 | ida_free(ida, i - 1); |
| 92 | IDA_BUG_ON(ida, !ida_is_empty(ida)); |
| 93 | |
| 94 | for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) |
| 95 | IDA_BUG_ON(ida, ida_alloc(ida, GFP_KERNEL) != i); |
| 96 | for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) |
| 97 | ida_free(ida, i - 1); |
| 98 | IDA_BUG_ON(ida, !ida_is_empty(ida)); |
| 99 | } |
| 100 | |
Matthew Wilcox | 8ab8ba3 | 2018-06-18 16:59:29 -0400 | [diff] [blame] | 101 | static int ida_checks(void) |
| 102 | { |
| 103 | DEFINE_IDA(ida); |
| 104 | |
| 105 | IDA_BUG_ON(&ida, !ida_is_empty(&ida)); |
Matthew Wilcox | 0a38563 | 2018-06-18 17:23:37 -0400 | [diff] [blame] | 106 | ida_check_leaf(&ida, 0); |
| 107 | ida_check_leaf(&ida, 1024); |
| 108 | ida_check_leaf(&ida, 1024 * 64); |
Matthew Wilcox | 161b47e | 2018-06-18 17:25:20 -0400 | [diff] [blame] | 109 | ida_check_max(&ida); |
Matthew Wilcox | 5c78b0b | 2018-06-18 18:10:32 -0400 | [diff] [blame^] | 110 | ida_check_conv(&ida); |
Matthew Wilcox | 8ab8ba3 | 2018-06-18 16:59:29 -0400 | [diff] [blame] | 111 | |
| 112 | printk("IDA: %u of %u tests passed\n", tests_passed, tests_run); |
| 113 | return (tests_run != tests_passed) ? 0 : -EINVAL; |
| 114 | } |
| 115 | |
| 116 | static void ida_exit(void) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | module_init(ida_checks); |
| 121 | module_exit(ida_exit); |
| 122 | MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>"); |
| 123 | MODULE_LICENSE("GPL"); |