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 | /* |
Andy Shevchenko | 780ff33 | 2019-12-04 16:53:23 -0800 | [diff] [blame] | 3 | * Test cases for bitmap API. |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 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 | |
Timur Tabi | 4e89a78 | 2021-02-14 10:13:46 -0600 | [diff] [blame] | 19 | KSTM_MODULE_GLOBALS(); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 20 | |
| 21 | static char pbl_buffer[PAGE_SIZE] __initdata; |
| 22 | |
Andy Shevchenko | c21dd8a | 2019-12-04 16:53:20 -0800 | [diff] [blame] | 23 | static const unsigned long exp1[] __initconst = { |
| 24 | BITMAP_FROM_U64(1), |
| 25 | BITMAP_FROM_U64(2), |
| 26 | BITMAP_FROM_U64(0x0000ffff), |
| 27 | BITMAP_FROM_U64(0xffff0000), |
| 28 | BITMAP_FROM_U64(0x55555555), |
| 29 | BITMAP_FROM_U64(0xaaaaaaaa), |
| 30 | BITMAP_FROM_U64(0x11111111), |
| 31 | BITMAP_FROM_U64(0x22222222), |
| 32 | BITMAP_FROM_U64(0xffffffff), |
| 33 | BITMAP_FROM_U64(0xfffffffe), |
| 34 | BITMAP_FROM_U64(0x3333333311111111ULL), |
| 35 | BITMAP_FROM_U64(0xffffffff77777777ULL), |
| 36 | BITMAP_FROM_U64(0), |
Paul Gortmaker | 97330db | 2021-02-21 03:08:22 -0500 | [diff] [blame^] | 37 | BITMAP_FROM_U64(0x00008000), |
| 38 | BITMAP_FROM_U64(0x80000000), |
Andy Shevchenko | c21dd8a | 2019-12-04 16:53:20 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static const unsigned long exp2[] __initconst = { |
| 42 | BITMAP_FROM_U64(0x3333333311111111ULL), |
| 43 | BITMAP_FROM_U64(0xffffffff77777777ULL), |
| 44 | }; |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 45 | |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 46 | /* Fibonacci sequence */ |
| 47 | static const unsigned long exp2_to_exp3_mask[] __initconst = { |
| 48 | BITMAP_FROM_U64(0x008000020020212eULL), |
| 49 | }; |
| 50 | /* exp3_0_1 = (exp2[0] & ~exp2_to_exp3_mask) | (exp2[1] & exp2_to_exp3_mask) */ |
| 51 | static const unsigned long exp3_0_1[] __initconst = { |
| 52 | BITMAP_FROM_U64(0x33b3333311313137ULL), |
| 53 | }; |
| 54 | /* exp3_1_0 = (exp2[1] & ~exp2_to_exp3_mask) | (exp2[0] & exp2_to_exp3_mask) */ |
| 55 | static const unsigned long exp3_1_0[] __initconst = { |
| 56 | BITMAP_FROM_U64(0xff7fffff77575751ULL), |
| 57 | }; |
| 58 | |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 59 | static bool __init |
| 60 | __check_eq_uint(const char *srcfile, unsigned int line, |
| 61 | const unsigned int exp_uint, unsigned int x) |
| 62 | { |
| 63 | if (exp_uint != x) { |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 64 | pr_err("[%s:%u] expected %u, got %u\n", |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 65 | srcfile, line, exp_uint, x); |
| 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static bool __init |
| 73 | __check_eq_bitmap(const char *srcfile, unsigned int line, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 74 | const unsigned long *exp_bmap, const unsigned long *bmap, |
| 75 | unsigned int nbits) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 76 | { |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 77 | if (!bitmap_equal(exp_bmap, bmap, nbits)) { |
| 78 | pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n", |
| 79 | srcfile, line, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 80 | nbits, exp_bmap, nbits, bmap); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | static bool __init |
| 87 | __check_eq_pbl(const char *srcfile, unsigned int line, |
| 88 | const char *expected_pbl, |
| 89 | const unsigned long *bitmap, unsigned int nbits) |
| 90 | { |
| 91 | snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap); |
| 92 | if (strcmp(expected_pbl, pbl_buffer)) { |
| 93 | pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n", |
| 94 | srcfile, line, |
| 95 | expected_pbl, pbl_buffer); |
| 96 | return false; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | static bool __init |
| 102 | __check_eq_u32_array(const char *srcfile, unsigned int line, |
| 103 | const u32 *exp_arr, unsigned int exp_len, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 104 | const u32 *arr, unsigned int len) __used; |
| 105 | static bool __init |
| 106 | __check_eq_u32_array(const char *srcfile, unsigned int line, |
| 107 | const u32 *exp_arr, unsigned int exp_len, |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 108 | const u32 *arr, unsigned int len) |
| 109 | { |
| 110 | if (exp_len != len) { |
| 111 | pr_warn("[%s:%u] array length differ: expected %u, got %u\n", |
| 112 | srcfile, line, |
| 113 | exp_len, len); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | if (memcmp(exp_arr, arr, len*sizeof(*arr))) { |
| 118 | pr_warn("[%s:%u] array contents differ\n", srcfile, line); |
| 119 | print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET, |
| 120 | 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false); |
| 121 | print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET, |
| 122 | 32, 4, arr, len*sizeof(*arr), false); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 129 | static bool __init __check_eq_clump8(const char *srcfile, unsigned int line, |
| 130 | const unsigned int offset, |
| 131 | const unsigned int size, |
| 132 | const unsigned char *const clump_exp, |
| 133 | const unsigned long *const clump) |
| 134 | { |
| 135 | unsigned long exp; |
| 136 | |
| 137 | if (offset >= size) { |
| 138 | pr_warn("[%s:%u] bit offset for clump out-of-bounds: expected less than %u, got %u\n", |
| 139 | srcfile, line, size, offset); |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | exp = clump_exp[offset / 8]; |
| 144 | if (!exp) { |
| 145 | pr_warn("[%s:%u] bit offset for zero clump: expected nonzero clump, got bit offset %u with clump value 0", |
| 146 | srcfile, line, offset); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if (*clump != exp) { |
| 151 | pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX", |
| 152 | srcfile, line, exp, *clump); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 159 | #define __expect_eq(suffix, ...) \ |
| 160 | ({ \ |
| 161 | int result = 0; \ |
| 162 | total_tests++; \ |
| 163 | if (!__check_eq_ ## suffix(__FILE__, __LINE__, \ |
| 164 | ##__VA_ARGS__)) { \ |
| 165 | failed_tests++; \ |
| 166 | result = 1; \ |
| 167 | } \ |
| 168 | result; \ |
| 169 | }) |
| 170 | |
| 171 | #define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__) |
| 172 | #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__) |
| 173 | #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__) |
| 174 | #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__) |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 175 | #define expect_eq_clump8(...) __expect_eq(clump8, ##__VA_ARGS__) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 176 | |
Andy Shevchenko | ee3527bd | 2018-02-06 15:38:10 -0800 | [diff] [blame] | 177 | static void __init test_zero_clear(void) |
| 178 | { |
| 179 | DECLARE_BITMAP(bmap, 1024); |
| 180 | |
| 181 | /* Known way to set all bits */ |
| 182 | memset(bmap, 0xff, 128); |
| 183 | |
| 184 | expect_eq_pbl("0-22", bmap, 23); |
| 185 | expect_eq_pbl("0-1023", bmap, 1024); |
| 186 | |
| 187 | /* single-word bitmaps */ |
| 188 | bitmap_clear(bmap, 0, 9); |
| 189 | expect_eq_pbl("9-1023", bmap, 1024); |
| 190 | |
| 191 | bitmap_zero(bmap, 35); |
| 192 | expect_eq_pbl("64-1023", bmap, 1024); |
| 193 | |
| 194 | /* cross boundaries operations */ |
| 195 | bitmap_clear(bmap, 79, 19); |
| 196 | expect_eq_pbl("64-78,98-1023", bmap, 1024); |
| 197 | |
| 198 | bitmap_zero(bmap, 115); |
| 199 | expect_eq_pbl("128-1023", bmap, 1024); |
| 200 | |
| 201 | /* Zeroing entire area */ |
| 202 | bitmap_zero(bmap, 1024); |
| 203 | expect_eq_pbl("", bmap, 1024); |
| 204 | } |
| 205 | |
Andy Shevchenko | 978f369 | 2018-02-06 15:38:13 -0800 | [diff] [blame] | 206 | static void __init test_fill_set(void) |
| 207 | { |
| 208 | DECLARE_BITMAP(bmap, 1024); |
| 209 | |
| 210 | /* Known way to clear all bits */ |
| 211 | memset(bmap, 0x00, 128); |
| 212 | |
| 213 | expect_eq_pbl("", bmap, 23); |
| 214 | expect_eq_pbl("", bmap, 1024); |
| 215 | |
| 216 | /* single-word bitmaps */ |
| 217 | bitmap_set(bmap, 0, 9); |
| 218 | expect_eq_pbl("0-8", bmap, 1024); |
| 219 | |
| 220 | bitmap_fill(bmap, 35); |
| 221 | expect_eq_pbl("0-63", bmap, 1024); |
| 222 | |
| 223 | /* cross boundaries operations */ |
| 224 | bitmap_set(bmap, 79, 19); |
| 225 | expect_eq_pbl("0-63,79-97", bmap, 1024); |
| 226 | |
| 227 | bitmap_fill(bmap, 115); |
| 228 | expect_eq_pbl("0-127", bmap, 1024); |
| 229 | |
| 230 | /* Zeroing entire area */ |
| 231 | bitmap_fill(bmap, 1024); |
| 232 | expect_eq_pbl("0-1023", bmap, 1024); |
| 233 | } |
| 234 | |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 235 | static void __init test_copy(void) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 236 | { |
| 237 | DECLARE_BITMAP(bmap1, 1024); |
| 238 | DECLARE_BITMAP(bmap2, 1024); |
| 239 | |
| 240 | bitmap_zero(bmap1, 1024); |
| 241 | bitmap_zero(bmap2, 1024); |
| 242 | |
| 243 | /* single-word bitmaps */ |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 244 | bitmap_set(bmap1, 0, 19); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 245 | bitmap_copy(bmap2, bmap1, 23); |
| 246 | expect_eq_pbl("0-18", bmap2, 1024); |
| 247 | |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 248 | bitmap_set(bmap2, 0, 23); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 249 | bitmap_copy(bmap2, bmap1, 23); |
| 250 | expect_eq_pbl("0-18", bmap2, 1024); |
| 251 | |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 252 | /* multi-word bitmaps */ |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 253 | bitmap_set(bmap1, 0, 109); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 254 | bitmap_copy(bmap2, bmap1, 1024); |
| 255 | expect_eq_pbl("0-108", bmap2, 1024); |
| 256 | |
| 257 | bitmap_fill(bmap2, 1024); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 258 | bitmap_copy(bmap2, bmap1, 1024); |
| 259 | expect_eq_pbl("0-108", bmap2, 1024); |
| 260 | |
| 261 | /* the following tests assume a 32- or 64-bit arch (even 128b |
| 262 | * if we care) |
| 263 | */ |
| 264 | |
| 265 | bitmap_fill(bmap2, 1024); |
| 266 | bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */ |
| 267 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
| 268 | |
| 269 | bitmap_fill(bmap2, 1024); |
| 270 | bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */ |
| 271 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 274 | #define EXP2_IN_BITS (sizeof(exp2) * 8) |
| 275 | |
| 276 | static void __init test_replace(void) |
| 277 | { |
| 278 | unsigned int nbits = 64; |
Andy Shevchenko | 69334ca | 2020-01-30 22:11:01 -0800 | [diff] [blame] | 279 | unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG); |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 280 | DECLARE_BITMAP(bmap, 1024); |
| 281 | |
Andy Shevchenko | caa7f77 | 2020-04-06 20:10:28 -0700 | [diff] [blame] | 282 | BUILD_BUG_ON(EXP2_IN_BITS < nbits * 2); |
| 283 | |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 284 | bitmap_zero(bmap, 1024); |
Andy Shevchenko | 69334ca | 2020-01-30 22:11:01 -0800 | [diff] [blame] | 285 | bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits); |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 286 | expect_eq_bitmap(bmap, exp3_0_1, nbits); |
| 287 | |
| 288 | bitmap_zero(bmap, 1024); |
Andy Shevchenko | 69334ca | 2020-01-30 22:11:01 -0800 | [diff] [blame] | 289 | bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits); |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 290 | expect_eq_bitmap(bmap, exp3_1_0, nbits); |
| 291 | |
| 292 | bitmap_fill(bmap, 1024); |
Andy Shevchenko | 69334ca | 2020-01-30 22:11:01 -0800 | [diff] [blame] | 293 | bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits); |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 294 | expect_eq_bitmap(bmap, exp3_0_1, nbits); |
| 295 | |
| 296 | bitmap_fill(bmap, 1024); |
Andy Shevchenko | 69334ca | 2020-01-30 22:11:01 -0800 | [diff] [blame] | 297 | bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits); |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 298 | expect_eq_bitmap(bmap, exp3_1_0, nbits); |
| 299 | } |
| 300 | |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 301 | #define PARSE_TIME 0x1 |
| 302 | #define NO_LEN 0x2 |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 303 | |
| 304 | struct test_bitmap_parselist{ |
| 305 | const int errno; |
| 306 | const char *in; |
| 307 | const unsigned long *expected; |
| 308 | const int nbits; |
| 309 | const int flags; |
| 310 | }; |
| 311 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 312 | static const struct test_bitmap_parselist parselist_tests[] __initconst = { |
Yury Norov | 60ef690 | 2017-09-08 16:15:41 -0700 | [diff] [blame] | 313 | #define step (sizeof(u64) / sizeof(unsigned long)) |
| 314 | |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 315 | {0, "0", &exp1[0], 8, 0}, |
| 316 | {0, "1", &exp1[1 * step], 8, 0}, |
| 317 | {0, "0-15", &exp1[2 * step], 32, 0}, |
| 318 | {0, "16-31", &exp1[3 * step], 32, 0}, |
| 319 | {0, "0-31:1/2", &exp1[4 * step], 32, 0}, |
| 320 | {0, "1-31:1/2", &exp1[5 * step], 32, 0}, |
| 321 | {0, "0-31:1/4", &exp1[6 * step], 32, 0}, |
| 322 | {0, "1-31:1/4", &exp1[7 * step], 32, 0}, |
| 323 | {0, "0-31:4/4", &exp1[8 * step], 32, 0}, |
| 324 | {0, "1-31:4/4", &exp1[9 * step], 32, 0}, |
| 325 | {0, "0-31:1/4,32-63:2/4", &exp1[10 * step], 64, 0}, |
| 326 | {0, "0-31:3/4,32-63:4/4", &exp1[11 * step], 64, 0}, |
| 327 | {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] | 328 | |
| 329 | {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0}, |
| 330 | |
| 331 | {0, "0-2047:128/256", NULL, 2048, PARSE_TIME}, |
| 332 | |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 333 | {0, "", &exp1[12 * step], 8, 0}, |
| 334 | {0, "\n", &exp1[12 * step], 8, 0}, |
| 335 | {0, ",, ,, , , ,", &exp1[12 * step], 8, 0}, |
| 336 | {0, " , ,, , , ", &exp1[12 * step], 8, 0}, |
| 337 | {0, " , ,, , , \n", &exp1[12 * step], 8, 0}, |
Yury Norov | a4ab505 | 2019-05-14 15:43:21 -0700 | [diff] [blame] | 338 | |
Paul Gortmaker | 97330db | 2021-02-21 03:08:22 -0500 | [diff] [blame^] | 339 | {0, "0-0", &exp1[0], 32, 0}, |
| 340 | {0, "1-1", &exp1[1 * step], 32, 0}, |
| 341 | {0, "15-15", &exp1[13 * step], 32, 0}, |
| 342 | {0, "31-31", &exp1[14 * step], 32, 0}, |
| 343 | |
| 344 | {0, "0-0:0/1", &exp1[12 * step], 32, 0}, |
| 345 | {0, "0-0:1/1", &exp1[0], 32, 0}, |
| 346 | {0, "0-0:1/31", &exp1[0], 32, 0}, |
| 347 | {0, "0-0:31/31", &exp1[0], 32, 0}, |
| 348 | {0, "1-1:1/1", &exp1[1 * step], 32, 0}, |
| 349 | {0, "0-15:16/31", &exp1[2 * step], 32, 0}, |
| 350 | {0, "15-15:1/2", &exp1[13 * step], 32, 0}, |
| 351 | {0, "15-15:31/31", &exp1[13 * step], 32, 0}, |
| 352 | {0, "15-31:1/31", &exp1[13 * step], 32, 0}, |
| 353 | {0, "16-31:16/31", &exp1[3 * step], 32, 0}, |
| 354 | {0, "31-31:31/31", &exp1[14 * step], 32, 0}, |
| 355 | |
| 356 | {0, "0-31:1/3,1-31:1/3,2-31:1/3", &exp1[8 * step], 32, 0}, |
| 357 | {0, "1-10:8/12,8-31:24/29,0-31:0/3", &exp1[9 * step], 32, 0}, |
| 358 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 359 | {-EINVAL, "-1", NULL, 8, 0}, |
| 360 | {-EINVAL, "-0", NULL, 8, 0}, |
| 361 | {-EINVAL, "10-1", NULL, 8, 0}, |
Paul Gortmaker | 6fef590 | 2021-02-21 03:08:21 -0500 | [diff] [blame] | 362 | {-ERANGE, "8-8", NULL, 8, 0}, |
| 363 | {-ERANGE, "0-31", NULL, 8, 0}, |
Paul Gortmaker | 494215f | 2021-02-21 03:08:20 -0500 | [diff] [blame] | 364 | {-EINVAL, "0-31:", NULL, 32, 0}, |
| 365 | {-EINVAL, "0-31:0", NULL, 32, 0}, |
| 366 | {-EINVAL, "0-31:0/", NULL, 32, 0}, |
| 367 | {-EINVAL, "0-31:0/0", NULL, 32, 0}, |
| 368 | {-EINVAL, "0-31:1/0", NULL, 32, 0}, |
| 369 | {-EINVAL, "0-31:10/1", NULL, 32, 0}, |
Yury Norov | a4ab505 | 2019-05-14 15:43:21 -0700 | [diff] [blame] | 370 | {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0}, |
| 371 | |
| 372 | {-EINVAL, "a-31", NULL, 8, 0}, |
| 373 | {-EINVAL, "0-a1", NULL, 8, 0}, |
| 374 | {-EINVAL, "a-31:10/1", NULL, 8, 0}, |
| 375 | {-EINVAL, "0-31:a/1", NULL, 8, 0}, |
| 376 | {-EINVAL, "0-\n", NULL, 8, 0}, |
Andy Shevchenko | 5422404 | 2019-12-04 16:53:09 -0800 | [diff] [blame] | 377 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 380 | static void __init test_bitmap_parselist(void) |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 381 | { |
| 382 | int i; |
| 383 | int err; |
Yury Norov | 0c2111a | 2019-05-14 15:43:18 -0700 | [diff] [blame] | 384 | ktime_t time; |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 385 | DECLARE_BITMAP(bmap, 2048); |
| 386 | |
| 387 | for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) { |
| 388 | #define ptest parselist_tests[i] |
| 389 | |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 390 | time = ktime_get(); |
| 391 | err = bitmap_parselist(ptest.in, bmap, ptest.nbits); |
| 392 | time = ktime_get() - time; |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 393 | |
| 394 | if (err != ptest.errno) { |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 395 | pr_err("parselist: %d: input is %s, errno is %d, expected %d\n", |
| 396 | i, ptest.in, err, ptest.errno); |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 397 | continue; |
| 398 | } |
| 399 | |
| 400 | if (!err && ptest.expected |
| 401 | && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) { |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 402 | pr_err("parselist: %d: input is %s, result is 0x%lx, expected 0x%lx\n", |
| 403 | i, ptest.in, bmap[0], |
Yury Norov | 6ea86bd | 2019-05-14 15:43:24 -0700 | [diff] [blame] | 404 | *ptest.expected); |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 405 | continue; |
| 406 | } |
| 407 | |
| 408 | if (ptest.flags & PARSE_TIME) |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 409 | pr_err("parselist: %d: input is '%s' OK, Time: %llu\n", |
| 410 | i, ptest.in, time); |
Andy Shevchenko | 5422404 | 2019-12-04 16:53:09 -0800 | [diff] [blame] | 411 | |
| 412 | #undef ptest |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 416 | static const unsigned long parse_test[] __initconst = { |
| 417 | BITMAP_FROM_U64(0), |
| 418 | BITMAP_FROM_U64(1), |
| 419 | BITMAP_FROM_U64(0xdeadbeef), |
| 420 | BITMAP_FROM_U64(0x100000000ULL), |
| 421 | }; |
| 422 | |
| 423 | static const unsigned long parse_test2[] __initconst = { |
| 424 | BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xdeadbeef), |
| 425 | BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xbaadf00ddeadbeef), |
| 426 | BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0x0badf00ddeadbeef), |
| 427 | }; |
| 428 | |
| 429 | static const struct test_bitmap_parselist parse_tests[] __initconst = { |
Yury Norov | 809e308 | 2020-02-03 17:37:38 -0800 | [diff] [blame] | 430 | {0, "", &parse_test[0 * step], 32, 0}, |
| 431 | {0, " ", &parse_test[0 * step], 32, 0}, |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 432 | {0, "0", &parse_test[0 * step], 32, 0}, |
Yury Norov | 809e308 | 2020-02-03 17:37:38 -0800 | [diff] [blame] | 433 | {0, "0\n", &parse_test[0 * step], 32, 0}, |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 434 | {0, "1", &parse_test[1 * step], 32, 0}, |
| 435 | {0, "deadbeef", &parse_test[2 * step], 32, 0}, |
| 436 | {0, "1,0", &parse_test[3 * step], 33, 0}, |
Yury Norov | 809e308 | 2020-02-03 17:37:38 -0800 | [diff] [blame] | 437 | {0, "deadbeef,\n,0,1", &parse_test[2 * step], 96, 0}, |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 438 | |
| 439 | {0, "deadbeef,1,0", &parse_test2[0 * 2 * step], 96, 0}, |
| 440 | {0, "baadf00d,deadbeef,1,0", &parse_test2[1 * 2 * step], 128, 0}, |
| 441 | {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, 0}, |
Yury Norov | 809e308 | 2020-02-03 17:37:38 -0800 | [diff] [blame] | 442 | {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, NO_LEN}, |
| 443 | {0, " badf00d,deadbeef,1,0 ", &parse_test2[2 * 2 * step], 124, 0}, |
| 444 | {0, " , badf00d,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0}, |
| 445 | {0, " , badf00d, ,, ,,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0}, |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 446 | |
| 447 | {-EINVAL, "goodfood,deadbeef,1,0", NULL, 128, 0}, |
| 448 | {-EOVERFLOW, "3,0", NULL, 33, 0}, |
| 449 | {-EOVERFLOW, "123badf00d,deadbeef,1,0", NULL, 128, 0}, |
| 450 | {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 90, 0}, |
| 451 | {-EOVERFLOW, "fbadf00d,deadbeef,1,0", NULL, 95, 0}, |
| 452 | {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 100, 0}, |
| 453 | #undef step |
| 454 | }; |
| 455 | |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 456 | static void __init test_bitmap_parse(void) |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 457 | { |
| 458 | int i; |
| 459 | int err; |
| 460 | ktime_t time; |
| 461 | DECLARE_BITMAP(bmap, 2048); |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 462 | |
| 463 | for (i = 0; i < ARRAY_SIZE(parse_tests); i++) { |
| 464 | struct test_bitmap_parselist test = parse_tests[i]; |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 465 | size_t len = test.flags & NO_LEN ? UINT_MAX : strlen(test.in); |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 466 | |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 467 | time = ktime_get(); |
| 468 | err = bitmap_parse(test.in, len, bmap, test.nbits); |
| 469 | time = ktime_get() - time; |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 470 | |
| 471 | if (err != test.errno) { |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 472 | pr_err("parse: %d: input is %s, errno is %d, expected %d\n", |
| 473 | i, test.in, err, test.errno); |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 474 | continue; |
| 475 | } |
| 476 | |
| 477 | if (!err && test.expected |
| 478 | && !__bitmap_equal(bmap, test.expected, test.nbits)) { |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 479 | pr_err("parse: %d: input is %s, result is 0x%lx, expected 0x%lx\n", |
| 480 | i, test.in, bmap[0], |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 481 | *test.expected); |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | if (test.flags & PARSE_TIME) |
Christoph Hellwig | 81b1e24 | 2020-09-03 16:22:36 +0200 | [diff] [blame] | 486 | pr_err("parse: %d: input is '%s' OK, Time: %llu\n", |
| 487 | i, test.in, time); |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 491 | #define EXP1_IN_BITS (sizeof(exp1) * 8) |
Kees Cook | f6f66c1 | 2018-04-10 16:32:54 -0700 | [diff] [blame] | 492 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 493 | static void __init test_bitmap_arr32(void) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 494 | { |
Kees Cook | f6f66c1 | 2018-04-10 16:32:54 -0700 | [diff] [blame] | 495 | unsigned int nbits, next_bit; |
Andy Shevchenko | a4881d1 | 2019-12-04 16:53:13 -0800 | [diff] [blame] | 496 | u32 arr[EXP1_IN_BITS / 32]; |
| 497 | DECLARE_BITMAP(bmap2, EXP1_IN_BITS); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 498 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 499 | memset(arr, 0xa5, sizeof(arr)); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 500 | |
Andy Shevchenko | a4881d1 | 2019-12-04 16:53:13 -0800 | [diff] [blame] | 501 | for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) { |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 502 | bitmap_to_arr32(arr, exp1, nbits); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 503 | bitmap_from_arr32(bmap2, arr, nbits); |
Andy Shevchenko | 0ee312e | 2019-12-04 16:53:17 -0800 | [diff] [blame] | 504 | expect_eq_bitmap(bmap2, exp1, nbits); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 505 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 506 | next_bit = find_next_bit(bmap2, |
| 507 | round_up(nbits, BITS_PER_LONG), nbits); |
| 508 | if (next_bit < round_up(nbits, BITS_PER_LONG)) |
| 509 | pr_err("bitmap_copy_arr32(nbits == %d:" |
| 510 | " tail is not safely cleared: %d\n", |
| 511 | nbits, next_bit); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 512 | |
Andy Shevchenko | a4881d1 | 2019-12-04 16:53:13 -0800 | [diff] [blame] | 513 | if (nbits < EXP1_IN_BITS - 32) |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 514 | expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)], |
| 515 | 0xa5a5a5a5); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 519 | static void noinline __init test_mem_optimisations(void) |
| 520 | { |
| 521 | DECLARE_BITMAP(bmap1, 1024); |
| 522 | DECLARE_BITMAP(bmap2, 1024); |
| 523 | unsigned int start, nbits; |
| 524 | |
| 525 | for (start = 0; start < 1024; start += 8) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 526 | for (nbits = 0; nbits < 1024 - start; nbits += 8) { |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 527 | memset(bmap1, 0x5a, sizeof(bmap1)); |
| 528 | memset(bmap2, 0x5a, sizeof(bmap2)); |
| 529 | |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 530 | bitmap_set(bmap1, start, nbits); |
| 531 | __bitmap_set(bmap2, start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 532 | if (!bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 533 | printk("set not equal %d %d\n", start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 534 | failed_tests++; |
| 535 | } |
| 536 | if (!__bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 537 | printk("set not __equal %d %d\n", start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 538 | failed_tests++; |
| 539 | } |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 540 | |
| 541 | bitmap_clear(bmap1, start, nbits); |
| 542 | __bitmap_clear(bmap2, start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 543 | if (!bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 544 | printk("clear not equal %d %d\n", start, nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 545 | failed_tests++; |
| 546 | } |
| 547 | if (!__bitmap_equal(bmap1, bmap2, 1024)) { |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 548 | printk("clear not __equal %d %d\n", start, |
| 549 | nbits); |
Matthew Wilcox | 1e3054b | 2018-05-18 16:08:44 -0700 | [diff] [blame] | 550 | failed_tests++; |
| 551 | } |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 556 | static const unsigned char clump_exp[] __initconst = { |
| 557 | 0x01, /* 1 bit set */ |
| 558 | 0x02, /* non-edge 1 bit set */ |
| 559 | 0x00, /* zero bits set */ |
| 560 | 0x38, /* 3 bits set across 4-bit boundary */ |
| 561 | 0x38, /* Repeated clump */ |
| 562 | 0x0F, /* 4 bits set */ |
| 563 | 0xFF, /* all bits set */ |
| 564 | 0x05, /* non-adjacent 2 bits set */ |
| 565 | }; |
| 566 | |
| 567 | static void __init test_for_each_set_clump8(void) |
| 568 | { |
| 569 | #define CLUMP_EXP_NUMBITS 64 |
| 570 | DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS); |
| 571 | unsigned int start; |
| 572 | unsigned long clump; |
| 573 | |
| 574 | /* set bitmap to test case */ |
| 575 | bitmap_zero(bits, CLUMP_EXP_NUMBITS); |
| 576 | bitmap_set(bits, 0, 1); /* 0x01 */ |
| 577 | bitmap_set(bits, 9, 1); /* 0x02 */ |
| 578 | bitmap_set(bits, 27, 3); /* 0x28 */ |
| 579 | bitmap_set(bits, 35, 3); /* 0x28 */ |
| 580 | bitmap_set(bits, 40, 4); /* 0x0F */ |
| 581 | bitmap_set(bits, 48, 8); /* 0xFF */ |
| 582 | bitmap_set(bits, 56, 1); /* 0x05 - part 1 */ |
| 583 | bitmap_set(bits, 58, 1); /* 0x05 - part 2 */ |
| 584 | |
| 585 | for_each_set_clump8(start, clump, bits, CLUMP_EXP_NUMBITS) |
| 586 | expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump); |
| 587 | } |
| 588 | |
Stefano Brivio | bcb32a1 | 2020-08-11 18:34:32 -0700 | [diff] [blame] | 589 | struct test_bitmap_cut { |
| 590 | unsigned int first; |
| 591 | unsigned int cut; |
| 592 | unsigned int nbits; |
| 593 | unsigned long in[4]; |
| 594 | unsigned long expected[4]; |
| 595 | }; |
| 596 | |
| 597 | static struct test_bitmap_cut test_cut[] = { |
| 598 | { 0, 0, 8, { 0x0000000aUL, }, { 0x0000000aUL, }, }, |
| 599 | { 0, 0, 32, { 0xdadadeadUL, }, { 0xdadadeadUL, }, }, |
| 600 | { 0, 3, 8, { 0x000000aaUL, }, { 0x00000015UL, }, }, |
| 601 | { 3, 3, 8, { 0x000000aaUL, }, { 0x00000012UL, }, }, |
| 602 | { 0, 1, 32, { 0xa5a5a5a5UL, }, { 0x52d2d2d2UL, }, }, |
| 603 | { 0, 8, 32, { 0xdeadc0deUL, }, { 0x00deadc0UL, }, }, |
| 604 | { 1, 1, 32, { 0x5a5a5a5aUL, }, { 0x2d2d2d2cUL, }, }, |
| 605 | { 0, 15, 32, { 0xa5a5a5a5UL, }, { 0x00014b4bUL, }, }, |
| 606 | { 0, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, }, |
| 607 | { 15, 15, 32, { 0xa5a5a5a5UL, }, { 0x000125a5UL, }, }, |
| 608 | { 15, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, }, |
| 609 | { 16, 15, 32, { 0xa5a5a5a5UL, }, { 0x0001a5a5UL, }, }, |
| 610 | |
| 611 | { BITS_PER_LONG, BITS_PER_LONG, BITS_PER_LONG, |
| 612 | { 0xa5a5a5a5UL, 0xa5a5a5a5UL, }, |
| 613 | { 0xa5a5a5a5UL, 0xa5a5a5a5UL, }, |
| 614 | }, |
| 615 | { 1, BITS_PER_LONG - 1, BITS_PER_LONG, |
| 616 | { 0xa5a5a5a5UL, 0xa5a5a5a5UL, }, |
| 617 | { 0x00000001UL, 0x00000001UL, }, |
| 618 | }, |
| 619 | |
| 620 | { 0, BITS_PER_LONG * 2, BITS_PER_LONG * 2 + 1, |
| 621 | { 0xa5a5a5a5UL, 0x00000001UL, 0x00000001UL, 0x00000001UL }, |
| 622 | { 0x00000001UL, }, |
| 623 | }, |
| 624 | { 16, BITS_PER_LONG * 2 + 1, BITS_PER_LONG * 2 + 1 + 16, |
| 625 | { 0x0000ffffUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL }, |
| 626 | { 0x2d2dffffUL, }, |
| 627 | }, |
| 628 | }; |
| 629 | |
| 630 | static void __init test_bitmap_cut(void) |
| 631 | { |
| 632 | unsigned long b[5], *in = &b[1], *out = &b[0]; /* Partial overlap */ |
| 633 | int i; |
| 634 | |
| 635 | for (i = 0; i < ARRAY_SIZE(test_cut); i++) { |
| 636 | struct test_bitmap_cut *t = &test_cut[i]; |
| 637 | |
| 638 | memcpy(in, t->in, sizeof(t->in)); |
| 639 | |
| 640 | bitmap_cut(out, in, t->first, t->cut, t->nbits); |
| 641 | |
| 642 | expect_eq_bitmap(t->expected, out, t->nbits); |
| 643 | } |
| 644 | } |
| 645 | |
Tobin C. Harding | 6b1a4d5 | 2019-04-05 12:58:57 +1100 | [diff] [blame] | 646 | static void __init selftest(void) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 647 | { |
Andy Shevchenko | ee3527bd | 2018-02-06 15:38:10 -0800 | [diff] [blame] | 648 | test_zero_clear(); |
Andy Shevchenko | 978f369 | 2018-02-06 15:38:13 -0800 | [diff] [blame] | 649 | test_fill_set(); |
Andy Shevchenko | fe81814 | 2018-02-06 15:38:17 -0800 | [diff] [blame] | 650 | test_copy(); |
Andy Shevchenko | 30544ed | 2019-12-04 16:53:26 -0800 | [diff] [blame] | 651 | test_replace(); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame] | 652 | test_bitmap_arr32(); |
Yury Norov | 7eb2e94 | 2020-02-03 17:37:27 -0800 | [diff] [blame] | 653 | test_bitmap_parse(); |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 654 | test_bitmap_parselist(); |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 655 | test_mem_optimisations(); |
William Breathitt Gray | e4aa168 | 2019-12-04 16:51:01 -0800 | [diff] [blame] | 656 | test_for_each_set_clump8(); |
Stefano Brivio | bcb32a1 | 2020-08-11 18:34:32 -0700 | [diff] [blame] | 657 | test_bitmap_cut(); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 658 | } |
| 659 | |
Tobin C. Harding | 6b1a4d5 | 2019-04-05 12:58:57 +1100 | [diff] [blame] | 660 | KSTM_MODULE_LOADERS(test_bitmap); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 661 | MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>"); |
| 662 | MODULE_LICENSE("GPL"); |