1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test cases for bitmap API.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
15 #include <linux/uaccess.h>
17 #include "../tools/testing/selftests/kselftest_module.h"
19 static unsigned total_tests __initdata;
20 static unsigned failed_tests __initdata;
22 static char pbl_buffer[PAGE_SIZE] __initdata;
24 static const unsigned long exp1[] __initconst = {
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),
40 static const unsigned long exp2[] __initconst = {
41 BITMAP_FROM_U64(0x3333333311111111ULL),
42 BITMAP_FROM_U64(0xffffffff77777777ULL),
45 /* Fibonacci sequence */
46 static const unsigned long exp2_to_exp3_mask[] __initconst = {
47 BITMAP_FROM_U64(0x008000020020212eULL),
49 /* exp3_0_1 = (exp2[0] & ~exp2_to_exp3_mask) | (exp2[1] & exp2_to_exp3_mask) */
50 static const unsigned long exp3_0_1[] __initconst = {
51 BITMAP_FROM_U64(0x33b3333311313137ULL),
53 /* exp3_1_0 = (exp2[1] & ~exp2_to_exp3_mask) | (exp2[0] & exp2_to_exp3_mask) */
54 static const unsigned long exp3_1_0[] __initconst = {
55 BITMAP_FROM_U64(0xff7fffff77575751ULL),
59 __check_eq_uint(const char *srcfile, unsigned int line,
60 const unsigned int exp_uint, unsigned int x)
63 pr_err("[%s:%u] expected %u, got %u\n",
64 srcfile, line, exp_uint, x);
72 __check_eq_bitmap(const char *srcfile, unsigned int line,
73 const unsigned long *exp_bmap, const unsigned long *bmap,
76 if (!bitmap_equal(exp_bmap, bmap, nbits)) {
77 pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
79 nbits, exp_bmap, nbits, bmap);
86 __check_eq_pbl(const char *srcfile, unsigned int line,
87 const char *expected_pbl,
88 const unsigned long *bitmap, unsigned int nbits)
90 snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
91 if (strcmp(expected_pbl, pbl_buffer)) {
92 pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
94 expected_pbl, pbl_buffer);
101 __check_eq_u32_array(const char *srcfile, unsigned int line,
102 const u32 *exp_arr, unsigned int exp_len,
103 const u32 *arr, unsigned int len) __used;
105 __check_eq_u32_array(const char *srcfile, unsigned int line,
106 const u32 *exp_arr, unsigned int exp_len,
107 const u32 *arr, unsigned int len)
109 if (exp_len != len) {
110 pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
116 if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
117 pr_warn("[%s:%u] array contents differ\n", srcfile, line);
118 print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET,
119 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
120 print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET,
121 32, 4, arr, len*sizeof(*arr), false);
128 static bool __init __check_eq_clump8(const char *srcfile, unsigned int line,
129 const unsigned int offset,
130 const unsigned int size,
131 const unsigned char *const clump_exp,
132 const unsigned long *const clump)
136 if (offset >= size) {
137 pr_warn("[%s:%u] bit offset for clump out-of-bounds: expected less than %u, got %u\n",
138 srcfile, line, size, offset);
142 exp = clump_exp[offset / 8];
144 pr_warn("[%s:%u] bit offset for zero clump: expected nonzero clump, got bit offset %u with clump value 0",
145 srcfile, line, offset);
150 pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX",
151 srcfile, line, exp, *clump);
158 #define __expect_eq(suffix, ...) \
162 if (!__check_eq_ ## suffix(__FILE__, __LINE__, \
170 #define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__)
171 #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__)
172 #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__)
173 #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__)
174 #define expect_eq_clump8(...) __expect_eq(clump8, ##__VA_ARGS__)
176 static void __init test_zero_clear(void)
178 DECLARE_BITMAP(bmap, 1024);
180 /* Known way to set all bits */
181 memset(bmap, 0xff, 128);
183 expect_eq_pbl("0-22", bmap, 23);
184 expect_eq_pbl("0-1023", bmap, 1024);
186 /* single-word bitmaps */
187 bitmap_clear(bmap, 0, 9);
188 expect_eq_pbl("9-1023", bmap, 1024);
190 bitmap_zero(bmap, 35);
191 expect_eq_pbl("64-1023", bmap, 1024);
193 /* cross boundaries operations */
194 bitmap_clear(bmap, 79, 19);
195 expect_eq_pbl("64-78,98-1023", bmap, 1024);
197 bitmap_zero(bmap, 115);
198 expect_eq_pbl("128-1023", bmap, 1024);
200 /* Zeroing entire area */
201 bitmap_zero(bmap, 1024);
202 expect_eq_pbl("", bmap, 1024);
205 static void __init test_fill_set(void)
207 DECLARE_BITMAP(bmap, 1024);
209 /* Known way to clear all bits */
210 memset(bmap, 0x00, 128);
212 expect_eq_pbl("", bmap, 23);
213 expect_eq_pbl("", bmap, 1024);
215 /* single-word bitmaps */
216 bitmap_set(bmap, 0, 9);
217 expect_eq_pbl("0-8", bmap, 1024);
219 bitmap_fill(bmap, 35);
220 expect_eq_pbl("0-63", bmap, 1024);
222 /* cross boundaries operations */
223 bitmap_set(bmap, 79, 19);
224 expect_eq_pbl("0-63,79-97", bmap, 1024);
226 bitmap_fill(bmap, 115);
227 expect_eq_pbl("0-127", bmap, 1024);
229 /* Zeroing entire area */
230 bitmap_fill(bmap, 1024);
231 expect_eq_pbl("0-1023", bmap, 1024);
234 static void __init test_copy(void)
236 DECLARE_BITMAP(bmap1, 1024);
237 DECLARE_BITMAP(bmap2, 1024);
239 bitmap_zero(bmap1, 1024);
240 bitmap_zero(bmap2, 1024);
242 /* single-word bitmaps */
243 bitmap_set(bmap1, 0, 19);
244 bitmap_copy(bmap2, bmap1, 23);
245 expect_eq_pbl("0-18", bmap2, 1024);
247 bitmap_set(bmap2, 0, 23);
248 bitmap_copy(bmap2, bmap1, 23);
249 expect_eq_pbl("0-18", bmap2, 1024);
251 /* multi-word bitmaps */
252 bitmap_set(bmap1, 0, 109);
253 bitmap_copy(bmap2, bmap1, 1024);
254 expect_eq_pbl("0-108", bmap2, 1024);
256 bitmap_fill(bmap2, 1024);
257 bitmap_copy(bmap2, bmap1, 1024);
258 expect_eq_pbl("0-108", bmap2, 1024);
260 /* the following tests assume a 32- or 64-bit arch (even 128b
264 bitmap_fill(bmap2, 1024);
265 bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */
266 expect_eq_pbl("0-108,128-1023", bmap2, 1024);
268 bitmap_fill(bmap2, 1024);
269 bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */
270 expect_eq_pbl("0-108,128-1023", bmap2, 1024);
273 #define EXP2_IN_BITS (sizeof(exp2) * 8)
275 static void __init test_replace(void)
277 unsigned int nbits = 64;
278 unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG);
279 DECLARE_BITMAP(bmap, 1024);
281 BUILD_BUG_ON(EXP2_IN_BITS < nbits * 2);
283 bitmap_zero(bmap, 1024);
284 bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
285 expect_eq_bitmap(bmap, exp3_0_1, nbits);
287 bitmap_zero(bmap, 1024);
288 bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
289 expect_eq_bitmap(bmap, exp3_1_0, nbits);
291 bitmap_fill(bmap, 1024);
292 bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
293 expect_eq_bitmap(bmap, exp3_0_1, nbits);
295 bitmap_fill(bmap, 1024);
296 bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
297 expect_eq_bitmap(bmap, exp3_1_0, nbits);
300 #define PARSE_TIME 0x1
303 struct test_bitmap_parselist{
306 const unsigned long *expected;
311 static const struct test_bitmap_parselist parselist_tests[] __initconst = {
312 #define step (sizeof(u64) / sizeof(unsigned long))
314 {0, "0", &exp1[0], 8, 0},
315 {0, "1", &exp1[1 * step], 8, 0},
316 {0, "0-15", &exp1[2 * step], 32, 0},
317 {0, "16-31", &exp1[3 * step], 32, 0},
318 {0, "0-31:1/2", &exp1[4 * step], 32, 0},
319 {0, "1-31:1/2", &exp1[5 * step], 32, 0},
320 {0, "0-31:1/4", &exp1[6 * step], 32, 0},
321 {0, "1-31:1/4", &exp1[7 * step], 32, 0},
322 {0, "0-31:4/4", &exp1[8 * step], 32, 0},
323 {0, "1-31:4/4", &exp1[9 * step], 32, 0},
324 {0, "0-31:1/4,32-63:2/4", &exp1[10 * step], 64, 0},
325 {0, "0-31:3/4,32-63:4/4", &exp1[11 * step], 64, 0},
326 {0, " ,, 0-31:3/4 ,, 32-63:4/4 ,, ", &exp1[11 * step], 64, 0},
328 {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0},
330 {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
332 {0, "", &exp1[12 * step], 8, 0},
333 {0, "\n", &exp1[12 * step], 8, 0},
334 {0, ",, ,, , , ,", &exp1[12 * step], 8, 0},
335 {0, " , ,, , , ", &exp1[12 * step], 8, 0},
336 {0, " , ,, , , \n", &exp1[12 * step], 8, 0},
338 {-EINVAL, "-1", NULL, 8, 0},
339 {-EINVAL, "-0", NULL, 8, 0},
340 {-EINVAL, "10-1", NULL, 8, 0},
341 {-EINVAL, "0-31:", NULL, 8, 0},
342 {-EINVAL, "0-31:0", NULL, 8, 0},
343 {-EINVAL, "0-31:0/", NULL, 8, 0},
344 {-EINVAL, "0-31:0/0", NULL, 8, 0},
345 {-EINVAL, "0-31:1/0", NULL, 8, 0},
346 {-EINVAL, "0-31:10/1", NULL, 8, 0},
347 {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0},
349 {-EINVAL, "a-31", NULL, 8, 0},
350 {-EINVAL, "0-a1", NULL, 8, 0},
351 {-EINVAL, "a-31:10/1", NULL, 8, 0},
352 {-EINVAL, "0-31:a/1", NULL, 8, 0},
353 {-EINVAL, "0-\n", NULL, 8, 0},
357 static void __init test_bitmap_parselist(void)
362 DECLARE_BITMAP(bmap, 2048);
364 for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
365 #define ptest parselist_tests[i]
368 err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
369 time = ktime_get() - time;
371 if (err != ptest.errno) {
372 pr_err("parselist: %d: input is %s, errno is %d, expected %d\n",
373 i, ptest.in, err, ptest.errno);
377 if (!err && ptest.expected
378 && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
379 pr_err("parselist: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
380 i, ptest.in, bmap[0],
385 if (ptest.flags & PARSE_TIME)
386 pr_err("parselist: %d: input is '%s' OK, Time: %llu\n",
393 static const unsigned long parse_test[] __initconst = {
396 BITMAP_FROM_U64(0xdeadbeef),
397 BITMAP_FROM_U64(0x100000000ULL),
400 static const unsigned long parse_test2[] __initconst = {
401 BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xdeadbeef),
402 BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xbaadf00ddeadbeef),
403 BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0x0badf00ddeadbeef),
406 static const struct test_bitmap_parselist parse_tests[] __initconst = {
407 {0, "", &parse_test[0 * step], 32, 0},
408 {0, " ", &parse_test[0 * step], 32, 0},
409 {0, "0", &parse_test[0 * step], 32, 0},
410 {0, "0\n", &parse_test[0 * step], 32, 0},
411 {0, "1", &parse_test[1 * step], 32, 0},
412 {0, "deadbeef", &parse_test[2 * step], 32, 0},
413 {0, "1,0", &parse_test[3 * step], 33, 0},
414 {0, "deadbeef,\n,0,1", &parse_test[2 * step], 96, 0},
416 {0, "deadbeef,1,0", &parse_test2[0 * 2 * step], 96, 0},
417 {0, "baadf00d,deadbeef,1,0", &parse_test2[1 * 2 * step], 128, 0},
418 {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, 0},
419 {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, NO_LEN},
420 {0, " badf00d,deadbeef,1,0 ", &parse_test2[2 * 2 * step], 124, 0},
421 {0, " , badf00d,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0},
422 {0, " , badf00d, ,, ,,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0},
424 {-EINVAL, "goodfood,deadbeef,1,0", NULL, 128, 0},
425 {-EOVERFLOW, "3,0", NULL, 33, 0},
426 {-EOVERFLOW, "123badf00d,deadbeef,1,0", NULL, 128, 0},
427 {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 90, 0},
428 {-EOVERFLOW, "fbadf00d,deadbeef,1,0", NULL, 95, 0},
429 {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 100, 0},
433 static void __init test_bitmap_parse(void)
438 DECLARE_BITMAP(bmap, 2048);
440 for (i = 0; i < ARRAY_SIZE(parse_tests); i++) {
441 struct test_bitmap_parselist test = parse_tests[i];
442 size_t len = test.flags & NO_LEN ? UINT_MAX : strlen(test.in);
445 err = bitmap_parse(test.in, len, bmap, test.nbits);
446 time = ktime_get() - time;
448 if (err != test.errno) {
449 pr_err("parse: %d: input is %s, errno is %d, expected %d\n",
450 i, test.in, err, test.errno);
454 if (!err && test.expected
455 && !__bitmap_equal(bmap, test.expected, test.nbits)) {
456 pr_err("parse: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
462 if (test.flags & PARSE_TIME)
463 pr_err("parse: %d: input is '%s' OK, Time: %llu\n",
468 #define EXP1_IN_BITS (sizeof(exp1) * 8)
470 static void __init test_bitmap_arr32(void)
472 unsigned int nbits, next_bit;
473 u32 arr[EXP1_IN_BITS / 32];
474 DECLARE_BITMAP(bmap2, EXP1_IN_BITS);
476 memset(arr, 0xa5, sizeof(arr));
478 for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) {
479 bitmap_to_arr32(arr, exp1, nbits);
480 bitmap_from_arr32(bmap2, arr, nbits);
481 expect_eq_bitmap(bmap2, exp1, nbits);
483 next_bit = find_next_bit(bmap2,
484 round_up(nbits, BITS_PER_LONG), nbits);
485 if (next_bit < round_up(nbits, BITS_PER_LONG))
486 pr_err("bitmap_copy_arr32(nbits == %d:"
487 " tail is not safely cleared: %d\n",
490 if (nbits < EXP1_IN_BITS - 32)
491 expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
496 static void noinline __init test_mem_optimisations(void)
498 DECLARE_BITMAP(bmap1, 1024);
499 DECLARE_BITMAP(bmap2, 1024);
500 unsigned int start, nbits;
502 for (start = 0; start < 1024; start += 8) {
503 for (nbits = 0; nbits < 1024 - start; nbits += 8) {
504 memset(bmap1, 0x5a, sizeof(bmap1));
505 memset(bmap2, 0x5a, sizeof(bmap2));
507 bitmap_set(bmap1, start, nbits);
508 __bitmap_set(bmap2, start, nbits);
509 if (!bitmap_equal(bmap1, bmap2, 1024)) {
510 printk("set not equal %d %d\n", start, nbits);
513 if (!__bitmap_equal(bmap1, bmap2, 1024)) {
514 printk("set not __equal %d %d\n", start, nbits);
518 bitmap_clear(bmap1, start, nbits);
519 __bitmap_clear(bmap2, start, nbits);
520 if (!bitmap_equal(bmap1, bmap2, 1024)) {
521 printk("clear not equal %d %d\n", start, nbits);
524 if (!__bitmap_equal(bmap1, bmap2, 1024)) {
525 printk("clear not __equal %d %d\n", start,
533 static const unsigned char clump_exp[] __initconst = {
534 0x01, /* 1 bit set */
535 0x02, /* non-edge 1 bit set */
536 0x00, /* zero bits set */
537 0x38, /* 3 bits set across 4-bit boundary */
538 0x38, /* Repeated clump */
539 0x0F, /* 4 bits set */
540 0xFF, /* all bits set */
541 0x05, /* non-adjacent 2 bits set */
544 static void __init test_for_each_set_clump8(void)
546 #define CLUMP_EXP_NUMBITS 64
547 DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS);
551 /* set bitmap to test case */
552 bitmap_zero(bits, CLUMP_EXP_NUMBITS);
553 bitmap_set(bits, 0, 1); /* 0x01 */
554 bitmap_set(bits, 9, 1); /* 0x02 */
555 bitmap_set(bits, 27, 3); /* 0x28 */
556 bitmap_set(bits, 35, 3); /* 0x28 */
557 bitmap_set(bits, 40, 4); /* 0x0F */
558 bitmap_set(bits, 48, 8); /* 0xFF */
559 bitmap_set(bits, 56, 1); /* 0x05 - part 1 */
560 bitmap_set(bits, 58, 1); /* 0x05 - part 2 */
562 for_each_set_clump8(start, clump, bits, CLUMP_EXP_NUMBITS)
563 expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump);
566 struct test_bitmap_cut {
571 unsigned long expected[4];
574 static struct test_bitmap_cut test_cut[] = {
575 { 0, 0, 8, { 0x0000000aUL, }, { 0x0000000aUL, }, },
576 { 0, 0, 32, { 0xdadadeadUL, }, { 0xdadadeadUL, }, },
577 { 0, 3, 8, { 0x000000aaUL, }, { 0x00000015UL, }, },
578 { 3, 3, 8, { 0x000000aaUL, }, { 0x00000012UL, }, },
579 { 0, 1, 32, { 0xa5a5a5a5UL, }, { 0x52d2d2d2UL, }, },
580 { 0, 8, 32, { 0xdeadc0deUL, }, { 0x00deadc0UL, }, },
581 { 1, 1, 32, { 0x5a5a5a5aUL, }, { 0x2d2d2d2cUL, }, },
582 { 0, 15, 32, { 0xa5a5a5a5UL, }, { 0x00014b4bUL, }, },
583 { 0, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, },
584 { 15, 15, 32, { 0xa5a5a5a5UL, }, { 0x000125a5UL, }, },
585 { 15, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, },
586 { 16, 15, 32, { 0xa5a5a5a5UL, }, { 0x0001a5a5UL, }, },
588 { BITS_PER_LONG, BITS_PER_LONG, BITS_PER_LONG,
589 { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
590 { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
592 { 1, BITS_PER_LONG - 1, BITS_PER_LONG,
593 { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
594 { 0x00000001UL, 0x00000001UL, },
597 { 0, BITS_PER_LONG * 2, BITS_PER_LONG * 2 + 1,
598 { 0xa5a5a5a5UL, 0x00000001UL, 0x00000001UL, 0x00000001UL },
601 { 16, BITS_PER_LONG * 2 + 1, BITS_PER_LONG * 2 + 1 + 16,
602 { 0x0000ffffUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL },
607 static void __init test_bitmap_cut(void)
609 unsigned long b[5], *in = &b[1], *out = &b[0]; /* Partial overlap */
612 for (i = 0; i < ARRAY_SIZE(test_cut); i++) {
613 struct test_bitmap_cut *t = &test_cut[i];
615 memcpy(in, t->in, sizeof(t->in));
617 bitmap_cut(out, in, t->first, t->cut, t->nbits);
619 expect_eq_bitmap(t->expected, out, t->nbits);
623 static void __init selftest(void)
631 test_bitmap_parselist();
632 test_mem_optimisations();
633 test_for_each_set_clump8();
637 KSTM_MODULE_LOADERS(test_bitmap);
638 MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
639 MODULE_LICENSE("GPL");