Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Test cases for printf facility. |
| 4 | */ |
| 5 | |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
| 8 | #include <linux/bitmap.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/printk.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/string.h> |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 16 | |
Tobin C. Harding | 6b1a4d5 | 2019-04-05 12:58:57 +1100 | [diff] [blame] | 17 | #include "../tools/testing/selftests/kselftest_module.h" |
| 18 | |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 19 | static unsigned total_tests __initdata; |
| 20 | static unsigned failed_tests __initdata; |
| 21 | |
| 22 | static char pbl_buffer[PAGE_SIZE] __initdata; |
| 23 | |
Andy Shevchenko | c21dd8a | 2019-12-04 16:53:20 -0800 | [diff] [blame^] | 24 | static const unsigned long exp1[] __initconst = { |
| 25 | BITMAP_FROM_U64(1), |
| 26 | BITMAP_FROM_U64(2), |
| 27 | BITMAP_FROM_U64(0x0000ffff), |
| 28 | BITMAP_FROM_U64(0xffff0000), |
| 29 | BITMAP_FROM_U64(0x55555555), |
| 30 | BITMAP_FROM_U64(0xaaaaaaaa), |
| 31 | BITMAP_FROM_U64(0x11111111), |
| 32 | BITMAP_FROM_U64(0x22222222), |
| 33 | BITMAP_FROM_U64(0xffffffff), |
| 34 | BITMAP_FROM_U64(0xfffffffe), |
| 35 | BITMAP_FROM_U64(0x3333333311111111ULL), |
| 36 | BITMAP_FROM_U64(0xffffffff77777777ULL), |
| 37 | BITMAP_FROM_U64(0), |
| 38 | }; |
| 39 | |
| 40 | static const unsigned long exp2[] __initconst = { |
| 41 | BITMAP_FROM_U64(0x3333333311111111ULL), |
| 42 | BITMAP_FROM_U64(0xffffffff77777777ULL), |
| 43 | }; |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 44 | |
| 45 | static bool __init |
| 46 | __check_eq_uint(const char *srcfile, unsigned int line, |
| 47 | const unsigned int exp_uint, unsigned int x) |
| 48 | { |
| 49 | if (exp_uint != x) { |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 50 | pr_err("[%s:%u] expected %u, got %u\n", |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 51 | srcfile, line, exp_uint, x); |
| 52 | return false; |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static bool __init |
| 59 | __check_eq_bitmap(const char *srcfile, unsigned int line, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 60 | const unsigned long *exp_bmap, const unsigned long *bmap, |
| 61 | unsigned int nbits) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 62 | { |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 63 | if (!bitmap_equal(exp_bmap, bmap, nbits)) { |
| 64 | pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n", |
| 65 | srcfile, line, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 66 | nbits, exp_bmap, nbits, bmap); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | static bool __init |
| 73 | __check_eq_pbl(const char *srcfile, unsigned int line, |
| 74 | const char *expected_pbl, |
| 75 | const unsigned long *bitmap, unsigned int nbits) |
| 76 | { |
| 77 | snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap); |
| 78 | if (strcmp(expected_pbl, pbl_buffer)) { |
| 79 | pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n", |
| 80 | srcfile, line, |
| 81 | expected_pbl, pbl_buffer); |
| 82 | return false; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | static bool __init |
| 88 | __check_eq_u32_array(const char *srcfile, unsigned int line, |
| 89 | const u32 *exp_arr, unsigned int exp_len, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 90 | const u32 *arr, unsigned int len) __used; |
| 91 | static bool __init |
| 92 | __check_eq_u32_array(const char *srcfile, unsigned int line, |
| 93 | const u32 *exp_arr, unsigned int exp_len, |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 94 | const u32 *arr, unsigned int len) |
| 95 | { |
| 96 | if (exp_len != len) { |
| 97 | pr_warn("[%s:%u] array length differ: expected %u, got %u\n", |
| 98 | srcfile, line, |
| 99 | exp_len, len); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | if (memcmp(exp_arr, arr, len*sizeof(*arr))) { |
| 104 | pr_warn("[%s:%u] array contents differ\n", srcfile, line); |
| 105 | print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET, |
| 106 | 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false); |
| 107 | print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET, |
| 108 | 32, 4, arr, len*sizeof(*arr), false); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 115 | static bool __init __check_eq_clump8(const char *srcfile, unsigned int line, |
| 116 | const unsigned int offset, |
| 117 | const unsigned int size, |
| 118 | const unsigned char *const clump_exp, |
| 119 | const unsigned long *const clump) |
| 120 | { |
| 121 | unsigned long exp; |
| 122 | |
| 123 | if (offset >= size) { |
| 124 | pr_warn("[%s:%u] bit offset for clump out-of-bounds: expected less than %u, got %u\n", |
| 125 | srcfile, line, size, offset); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | exp = clump_exp[offset / 8]; |
| 130 | if (!exp) { |
| 131 | pr_warn("[%s:%u] bit offset for zero clump: expected nonzero clump, got bit offset %u with clump value 0", |
| 132 | srcfile, line, offset); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (*clump != exp) { |
| 137 | pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX", |
| 138 | srcfile, line, exp, *clump); |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 145 | #define __expect_eq(suffix, ...) \ |
| 146 | ({ \ |
| 147 | int result = 0; \ |
| 148 | total_tests++; \ |
| 149 | if (!__check_eq_ ## suffix(__FILE__, __LINE__, \ |
| 150 | ##__VA_ARGS__)) { \ |
| 151 | failed_tests++; \ |
| 152 | result = 1; \ |
| 153 | } \ |
| 154 | result; \ |
| 155 | }) |
| 156 | |
| 157 | #define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__) |
| 158 | #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__) |
| 159 | #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__) |
| 160 | #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__) |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 161 | #define expect_eq_clump8(...) __expect_eq(clump8, ##__VA_ARGS__) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 162 | |
Andy Shevchenko | ee3527bd | 2018-02-06 15:38:10 -0800 | [diff] [blame] | 163 | static void __init test_zero_clear(void) |
| 164 | { |
| 165 | DECLARE_BITMAP(bmap, 1024); |
| 166 | |
| 167 | /* Known way to set all bits */ |
| 168 | memset(bmap, 0xff, 128); |
| 169 | |
| 170 | expect_eq_pbl("0-22", bmap, 23); |
| 171 | expect_eq_pbl("0-1023", bmap, 1024); |
| 172 | |
| 173 | /* single-word bitmaps */ |
| 174 | bitmap_clear(bmap, 0, 9); |
| 175 | expect_eq_pbl("9-1023", bmap, 1024); |
| 176 | |
| 177 | bitmap_zero(bmap, 35); |
| 178 | expect_eq_pbl("64-1023", bmap, 1024); |
| 179 | |
| 180 | /* cross boundaries operations */ |
| 181 | bitmap_clear(bmap, 79, 19); |
| 182 | expect_eq_pbl("64-78,98-1023", bmap, 1024); |
| 183 | |
| 184 | bitmap_zero(bmap, 115); |
| 185 | expect_eq_pbl("128-1023", bmap, 1024); |
| 186 | |
| 187 | /* Zeroing entire area */ |
| 188 | bitmap_zero(bmap, 1024); |
| 189 | expect_eq_pbl("", bmap, 1024); |
| 190 | } |
| 191 | |
Andy Shevchenko | 978f369 | 2018-02-06 15:38:13 -0800 | [diff] [blame] | 192 | static void __init test_fill_set(void) |
| 193 | { |
| 194 | DECLARE_BITMAP(bmap, 1024); |
| 195 | |
| 196 | /* Known way to clear all bits */ |
| 197 | memset(bmap, 0x00, 128); |
| 198 | |
| 199 | expect_eq_pbl("", bmap, 23); |
| 200 | expect_eq_pbl("", bmap, 1024); |
| 201 | |
| 202 | /* single-word bitmaps */ |
| 203 | bitmap_set(bmap, 0, 9); |
| 204 | expect_eq_pbl("0-8", bmap, 1024); |
| 205 | |
| 206 | bitmap_fill(bmap, 35); |
| 207 | expect_eq_pbl("0-63", bmap, 1024); |
| 208 | |
| 209 | /* cross boundaries operations */ |
| 210 | bitmap_set(bmap, 79, 19); |
| 211 | expect_eq_pbl("0-63,79-97", bmap, 1024); |
| 212 | |
| 213 | bitmap_fill(bmap, 115); |
| 214 | expect_eq_pbl("0-127", bmap, 1024); |
| 215 | |
| 216 | /* Zeroing entire area */ |
| 217 | bitmap_fill(bmap, 1024); |
| 218 | expect_eq_pbl("0-1023", bmap, 1024); |
| 219 | } |
| 220 | |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 221 | static void __init test_copy(void) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 222 | { |
| 223 | DECLARE_BITMAP(bmap1, 1024); |
| 224 | DECLARE_BITMAP(bmap2, 1024); |
| 225 | |
| 226 | bitmap_zero(bmap1, 1024); |
| 227 | bitmap_zero(bmap2, 1024); |
| 228 | |
| 229 | /* single-word bitmaps */ |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 230 | bitmap_set(bmap1, 0, 19); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 231 | bitmap_copy(bmap2, bmap1, 23); |
| 232 | expect_eq_pbl("0-18", bmap2, 1024); |
| 233 | |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 234 | bitmap_set(bmap2, 0, 23); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 235 | bitmap_copy(bmap2, bmap1, 23); |
| 236 | expect_eq_pbl("0-18", bmap2, 1024); |
| 237 | |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 238 | /* multi-word bitmaps */ |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 239 | bitmap_set(bmap1, 0, 109); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 240 | bitmap_copy(bmap2, bmap1, 1024); |
| 241 | expect_eq_pbl("0-108", bmap2, 1024); |
| 242 | |
| 243 | bitmap_fill(bmap2, 1024); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 244 | bitmap_copy(bmap2, bmap1, 1024); |
| 245 | expect_eq_pbl("0-108", bmap2, 1024); |
| 246 | |
| 247 | /* the following tests assume a 32- or 64-bit arch (even 128b |
| 248 | * if we care) |
| 249 | */ |
| 250 | |
| 251 | bitmap_fill(bmap2, 1024); |
| 252 | bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */ |
| 253 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
| 254 | |
| 255 | bitmap_fill(bmap2, 1024); |
| 256 | bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */ |
| 257 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 260 | #define PARSE_TIME 0x1 |
| 261 | |
| 262 | struct test_bitmap_parselist{ |
| 263 | const int errno; |
| 264 | const char *in; |
| 265 | const unsigned long *expected; |
| 266 | const int nbits; |
| 267 | const int flags; |
| 268 | }; |
| 269 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 270 | static const struct test_bitmap_parselist parselist_tests[] __initconst = { |
Yury Norov | 60ef690 | 2017-09-08 16:15:41 -0700 | [diff] [blame] | 271 | #define step (sizeof(u64) / sizeof(unsigned long)) |
| 272 | |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 273 | {0, "0", &exp1[0], 8, 0}, |
| 274 | {0, "1", &exp1[1 * step], 8, 0}, |
| 275 | {0, "0-15", &exp1[2 * step], 32, 0}, |
| 276 | {0, "16-31", &exp1[3 * step], 32, 0}, |
| 277 | {0, "0-31:1/2", &exp1[4 * step], 32, 0}, |
| 278 | {0, "1-31:1/2", &exp1[5 * step], 32, 0}, |
| 279 | {0, "0-31:1/4", &exp1[6 * step], 32, 0}, |
| 280 | {0, "1-31:1/4", &exp1[7 * step], 32, 0}, |
| 281 | {0, "0-31:4/4", &exp1[8 * step], 32, 0}, |
| 282 | {0, "1-31:4/4", &exp1[9 * step], 32, 0}, |
| 283 | {0, "0-31:1/4,32-63:2/4", &exp1[10 * step], 64, 0}, |
| 284 | {0, "0-31:3/4,32-63:4/4", &exp1[11 * step], 64, 0}, |
| 285 | {0, " ,, 0-31:3/4 ,, 32-63:4/4 ,, ", &exp1[11 * step], 64, 0}, |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 286 | |
| 287 | {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0}, |
| 288 | |
| 289 | {0, "0-2047:128/256", NULL, 2048, PARSE_TIME}, |
| 290 | |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 291 | {0, "", &exp1[12 * step], 8, 0}, |
| 292 | {0, "\n", &exp1[12 * step], 8, 0}, |
| 293 | {0, ",, ,, , , ,", &exp1[12 * step], 8, 0}, |
| 294 | {0, " , ,, , , ", &exp1[12 * step], 8, 0}, |
| 295 | {0, " , ,, , , \n", &exp1[12 * step], 8, 0}, |
Yury Norov | a4ab505 | 2019-05-14 15:43:21 -0700 | [diff] [blame] | 296 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 297 | {-EINVAL, "-1", NULL, 8, 0}, |
| 298 | {-EINVAL, "-0", NULL, 8, 0}, |
| 299 | {-EINVAL, "10-1", NULL, 8, 0}, |
Yury Norov | 8351760 | 2018-04-05 16:18:25 -0700 | [diff] [blame] | 300 | {-EINVAL, "0-31:", NULL, 8, 0}, |
| 301 | {-EINVAL, "0-31:0", NULL, 8, 0}, |
Yury Norov | a4ab505 | 2019-05-14 15:43:21 -0700 | [diff] [blame] | 302 | {-EINVAL, "0-31:0/", NULL, 8, 0}, |
Yury Norov | 8351760 | 2018-04-05 16:18:25 -0700 | [diff] [blame] | 303 | {-EINVAL, "0-31:0/0", NULL, 8, 0}, |
| 304 | {-EINVAL, "0-31:1/0", NULL, 8, 0}, |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 305 | {-EINVAL, "0-31:10/1", NULL, 8, 0}, |
Yury Norov | a4ab505 | 2019-05-14 15:43:21 -0700 | [diff] [blame] | 306 | {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0}, |
| 307 | |
| 308 | {-EINVAL, "a-31", NULL, 8, 0}, |
| 309 | {-EINVAL, "0-a1", NULL, 8, 0}, |
| 310 | {-EINVAL, "a-31:10/1", NULL, 8, 0}, |
| 311 | {-EINVAL, "0-31:a/1", NULL, 8, 0}, |
| 312 | {-EINVAL, "0-\n", NULL, 8, 0}, |
Andy Shevchenko | 5422404 | 2019-12-04 16:53:09 -0800 | [diff] [blame] | 313 | |
| 314 | #undef step |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 315 | }; |
| 316 | |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 317 | static void __init __test_bitmap_parselist(int is_user) |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 318 | { |
| 319 | int i; |
| 320 | int err; |
Yury Norov | 0c2111a | 2019-05-14 15:43:18 -0700 | [diff] [blame] | 321 | ktime_t time; |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 322 | DECLARE_BITMAP(bmap, 2048); |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 323 | char *mode = is_user ? "_user" : ""; |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 324 | |
| 325 | for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) { |
| 326 | #define ptest parselist_tests[i] |
| 327 | |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 328 | if (is_user) { |
| 329 | mm_segment_t orig_fs = get_fs(); |
| 330 | size_t len = strlen(ptest.in); |
| 331 | |
| 332 | set_fs(KERNEL_DS); |
| 333 | time = ktime_get(); |
Andy Shevchenko | 17b6753 | 2019-12-04 16:53:06 -0800 | [diff] [blame] | 334 | err = bitmap_parselist_user((__force const char __user *)ptest.in, len, |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 335 | bmap, ptest.nbits); |
| 336 | time = ktime_get() - time; |
| 337 | set_fs(orig_fs); |
| 338 | } else { |
| 339 | time = ktime_get(); |
| 340 | err = bitmap_parselist(ptest.in, bmap, ptest.nbits); |
| 341 | time = ktime_get() - time; |
| 342 | } |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 343 | |
| 344 | if (err != ptest.errno) { |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 345 | pr_err("parselist%s: %d: input is %s, errno is %d, expected %d\n", |
| 346 | mode, i, ptest.in, err, ptest.errno); |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 347 | continue; |
| 348 | } |
| 349 | |
| 350 | if (!err && ptest.expected |
| 351 | && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) { |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 352 | pr_err("parselist%s: %d: input is %s, result is 0x%lx, expected 0x%lx\n", |
| 353 | mode, i, ptest.in, bmap[0], |
| 354 | *ptest.expected); |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 355 | continue; |
| 356 | } |
| 357 | |
| 358 | if (ptest.flags & PARSE_TIME) |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 359 | pr_err("parselist%s: %d: input is '%s' OK, Time: %llu\n", |
| 360 | mode, i, ptest.in, time); |
Andy Shevchenko | 5422404 | 2019-12-04 16:53:09 -0800 | [diff] [blame] | 361 | |
| 362 | #undef ptest |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 366 | static void __init test_bitmap_parselist(void) |
| 367 | { |
| 368 | __test_bitmap_parselist(0); |
| 369 | } |
| 370 | |
| 371 | static void __init test_bitmap_parselist_user(void) |
| 372 | { |
| 373 | __test_bitmap_parselist(1); |
| 374 | } |
| 375 | |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 376 | #define EXP1_IN_BITS (sizeof(exp1) * 8) |
Kees Cook | f6f66c1 | 2018-04-10 16:32:54 -0700 | [diff] [blame] | 377 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 378 | static void __init test_bitmap_arr32(void) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 379 | { |
Kees Cook | f6f66c1 | 2018-04-10 16:32:54 -0700 | [diff] [blame] | 380 | unsigned int nbits, next_bit; |
Andy Shevchenko | a4881d1 | 2019-12-04 16:53:13 -0800 | [diff] [blame] | 381 | u32 arr[EXP1_IN_BITS / 32]; |
| 382 | DECLARE_BITMAP(bmap2, EXP1_IN_BITS); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 383 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 384 | memset(arr, 0xa5, sizeof(arr)); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 385 | |
Andy Shevchenko | a4881d1 | 2019-12-04 16:53:13 -0800 | [diff] [blame] | 386 | for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) { |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 387 | bitmap_to_arr32(arr, exp1, nbits); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 388 | bitmap_from_arr32(bmap2, arr, nbits); |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 389 | expect_eq_bitmap(bmap2, exp1, nbits); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 390 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 391 | next_bit = find_next_bit(bmap2, |
| 392 | round_up(nbits, BITS_PER_LONG), nbits); |
| 393 | if (next_bit < round_up(nbits, BITS_PER_LONG)) |
| 394 | pr_err("bitmap_copy_arr32(nbits == %d:" |
| 395 | " tail is not safely cleared: %d\n", |
| 396 | nbits, next_bit); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 397 | |
Andy Shevchenko | a4881d1 | 2019-12-04 16:53:13 -0800 | [diff] [blame] | 398 | if (nbits < EXP1_IN_BITS - 32) |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 399 | expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)], |
| 400 | 0xa5a5a5a5); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 404 | static void noinline __init test_mem_optimisations(void) |
| 405 | { |
| 406 | DECLARE_BITMAP(bmap1, 1024); |
| 407 | DECLARE_BITMAP(bmap2, 1024); |
| 408 | unsigned int start, nbits; |
| 409 | |
| 410 | for (start = 0; start < 1024; start += 8) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 411 | for (nbits = 0; nbits < 1024 - start; nbits += 8) { |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 412 | memset(bmap1, 0x5a, sizeof(bmap1)); |
| 413 | memset(bmap2, 0x5a, sizeof(bmap2)); |
| 414 | |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 415 | bitmap_set(bmap1, start, nbits); |
| 416 | __bitmap_set(bmap2, start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 417 | if (!bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 418 | printk("set not equal %d %d\n", start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 419 | failed_tests++; |
| 420 | } |
| 421 | if (!__bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 422 | printk("set not __equal %d %d\n", start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 423 | failed_tests++; |
| 424 | } |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 425 | |
| 426 | bitmap_clear(bmap1, start, nbits); |
| 427 | __bitmap_clear(bmap2, start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 428 | if (!bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 429 | printk("clear not equal %d %d\n", start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 430 | failed_tests++; |
| 431 | } |
| 432 | if (!__bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 433 | printk("clear not __equal %d %d\n", start, |
| 434 | nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 435 | failed_tests++; |
| 436 | } |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 441 | static const unsigned char clump_exp[] __initconst = { |
| 442 | 0x01, /* 1 bit set */ |
| 443 | 0x02, /* non-edge 1 bit set */ |
| 444 | 0x00, /* zero bits set */ |
| 445 | 0x38, /* 3 bits set across 4-bit boundary */ |
| 446 | 0x38, /* Repeated clump */ |
| 447 | 0x0F, /* 4 bits set */ |
| 448 | 0xFF, /* all bits set */ |
| 449 | 0x05, /* non-adjacent 2 bits set */ |
| 450 | }; |
| 451 | |
| 452 | static void __init test_for_each_set_clump8(void) |
| 453 | { |
| 454 | #define CLUMP_EXP_NUMBITS 64 |
| 455 | DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS); |
| 456 | unsigned int start; |
| 457 | unsigned long clump; |
| 458 | |
| 459 | /* set bitmap to test case */ |
| 460 | bitmap_zero(bits, CLUMP_EXP_NUMBITS); |
| 461 | bitmap_set(bits, 0, 1); /* 0x01 */ |
| 462 | bitmap_set(bits, 9, 1); /* 0x02 */ |
| 463 | bitmap_set(bits, 27, 3); /* 0x28 */ |
| 464 | bitmap_set(bits, 35, 3); /* 0x28 */ |
| 465 | bitmap_set(bits, 40, 4); /* 0x0F */ |
| 466 | bitmap_set(bits, 48, 8); /* 0xFF */ |
| 467 | bitmap_set(bits, 56, 1); /* 0x05 - part 1 */ |
| 468 | bitmap_set(bits, 58, 1); /* 0x05 - part 2 */ |
| 469 | |
| 470 | for_each_set_clump8(start, clump, bits, CLUMP_EXP_NUMBITS) |
| 471 | expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump); |
| 472 | } |
| 473 | |
Tobin C. Harding | 6b1a4d5 | 2019-04-05 12:58:57 +1100 | [diff] [blame] | 474 | static void __init selftest(void) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 475 | { |
Andy Shevchenko | ee3527bd | 2018-02-06 15:38:10 -0800 | [diff] [blame] | 476 | test_zero_clear(); |
Andy Shevchenko | 978f369 | 2018-02-06 15:38:13 -0800 | [diff] [blame] | 477 | test_fill_set(); |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 478 | test_copy(); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 479 | test_bitmap_arr32(); |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 480 | test_bitmap_parselist(); |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 481 | test_bitmap_parselist_user(); |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 482 | test_mem_optimisations(); |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 483 | test_for_each_set_clump8(); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Tobin C. Harding | 6b1a4d5 | 2019-04-05 12:58:57 +1100 | [diff] [blame] | 486 | KSTM_MODULE_LOADERS(test_bitmap); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 487 | MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>"); |
| 488 | MODULE_LICENSE("GPL"); |