blob: 71ec3afe1681c2c3b1250d49b9212d2241ef70d1 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
David Decotigny5fd003f2016-02-19 09:24:00 -05002/*
Andy Shevchenko780ff332019-12-04 16:53:23 -08003 * Test cases for bitmap API.
David Decotigny5fd003f2016-02-19 09:24:00 -05004 */
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 Norov6ea86bd2019-05-14 15:43:24 -070015#include <linux/uaccess.h>
David Decotigny5fd003f2016-02-19 09:24:00 -050016
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +110017#include "../tools/testing/selftests/kselftest_module.h"
18
David Decotigny5fd003f2016-02-19 09:24:00 -050019static unsigned total_tests __initdata;
20static unsigned failed_tests __initdata;
21
22static char pbl_buffer[PAGE_SIZE] __initdata;
23
Andy Shevchenkoc21dd8a2019-12-04 16:53:20 -080024static 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
40static const unsigned long exp2[] __initconst = {
41 BITMAP_FROM_U64(0x3333333311111111ULL),
42 BITMAP_FROM_U64(0xffffffff77777777ULL),
43};
David Decotigny5fd003f2016-02-19 09:24:00 -050044
Andy Shevchenko30544ed2019-12-04 16:53:26 -080045/* Fibonacci sequence */
46static const unsigned long exp2_to_exp3_mask[] __initconst = {
47 BITMAP_FROM_U64(0x008000020020212eULL),
48};
49/* exp3_0_1 = (exp2[0] & ~exp2_to_exp3_mask) | (exp2[1] & exp2_to_exp3_mask) */
50static const unsigned long exp3_0_1[] __initconst = {
51 BITMAP_FROM_U64(0x33b3333311313137ULL),
52};
53/* exp3_1_0 = (exp2[1] & ~exp2_to_exp3_mask) | (exp2[0] & exp2_to_exp3_mask) */
54static const unsigned long exp3_1_0[] __initconst = {
55 BITMAP_FROM_U64(0xff7fffff77575751ULL),
56};
57
David Decotigny5fd003f2016-02-19 09:24:00 -050058static bool __init
59__check_eq_uint(const char *srcfile, unsigned int line,
60 const unsigned int exp_uint, unsigned int x)
61{
62 if (exp_uint != x) {
Yury Norov3aa56882018-02-06 15:38:06 -080063 pr_err("[%s:%u] expected %u, got %u\n",
David Decotigny5fd003f2016-02-19 09:24:00 -050064 srcfile, line, exp_uint, x);
65 return false;
66 }
67 return true;
68}
69
70
71static bool __init
72__check_eq_bitmap(const char *srcfile, unsigned int line,
Yury Norov3aa56882018-02-06 15:38:06 -080073 const unsigned long *exp_bmap, const unsigned long *bmap,
74 unsigned int nbits)
David Decotigny5fd003f2016-02-19 09:24:00 -050075{
David Decotigny5fd003f2016-02-19 09:24:00 -050076 if (!bitmap_equal(exp_bmap, bmap, nbits)) {
77 pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
78 srcfile, line,
Yury Norov3aa56882018-02-06 15:38:06 -080079 nbits, exp_bmap, nbits, bmap);
David Decotigny5fd003f2016-02-19 09:24:00 -050080 return false;
81 }
82 return true;
83}
84
85static bool __init
86__check_eq_pbl(const char *srcfile, unsigned int line,
87 const char *expected_pbl,
88 const unsigned long *bitmap, unsigned int nbits)
89{
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",
93 srcfile, line,
94 expected_pbl, pbl_buffer);
95 return false;
96 }
97 return true;
98}
99
100static bool __init
101__check_eq_u32_array(const char *srcfile, unsigned int line,
102 const u32 *exp_arr, unsigned int exp_len,
Yury Norov3aa56882018-02-06 15:38:06 -0800103 const u32 *arr, unsigned int len) __used;
104static bool __init
105__check_eq_u32_array(const char *srcfile, unsigned int line,
106 const u32 *exp_arr, unsigned int exp_len,
David Decotigny5fd003f2016-02-19 09:24:00 -0500107 const u32 *arr, unsigned int len)
108{
109 if (exp_len != len) {
110 pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
111 srcfile, line,
112 exp_len, len);
113 return false;
114 }
115
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);
122 return false;
123 }
124
125 return true;
126}
127
William Breathitt Graye4aa1682019-12-04 16:51:01 -0800128static 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)
133{
134 unsigned long exp;
135
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);
139 return false;
140 }
141
142 exp = clump_exp[offset / 8];
143 if (!exp) {
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);
146 return false;
147 }
148
149 if (*clump != exp) {
150 pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX",
151 srcfile, line, exp, *clump);
152 return false;
153 }
154
155 return true;
156}
157
David Decotigny5fd003f2016-02-19 09:24:00 -0500158#define __expect_eq(suffix, ...) \
159 ({ \
160 int result = 0; \
161 total_tests++; \
162 if (!__check_eq_ ## suffix(__FILE__, __LINE__, \
163 ##__VA_ARGS__)) { \
164 failed_tests++; \
165 result = 1; \
166 } \
167 result; \
168 })
169
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__)
William Breathitt Graye4aa1682019-12-04 16:51:01 -0800174#define expect_eq_clump8(...) __expect_eq(clump8, ##__VA_ARGS__)
David Decotigny5fd003f2016-02-19 09:24:00 -0500175
Andy Shevchenkoee3527bd2018-02-06 15:38:10 -0800176static void __init test_zero_clear(void)
177{
178 DECLARE_BITMAP(bmap, 1024);
179
180 /* Known way to set all bits */
181 memset(bmap, 0xff, 128);
182
183 expect_eq_pbl("0-22", bmap, 23);
184 expect_eq_pbl("0-1023", bmap, 1024);
185
186 /* single-word bitmaps */
187 bitmap_clear(bmap, 0, 9);
188 expect_eq_pbl("9-1023", bmap, 1024);
189
190 bitmap_zero(bmap, 35);
191 expect_eq_pbl("64-1023", bmap, 1024);
192
193 /* cross boundaries operations */
194 bitmap_clear(bmap, 79, 19);
195 expect_eq_pbl("64-78,98-1023", bmap, 1024);
196
197 bitmap_zero(bmap, 115);
198 expect_eq_pbl("128-1023", bmap, 1024);
199
200 /* Zeroing entire area */
201 bitmap_zero(bmap, 1024);
202 expect_eq_pbl("", bmap, 1024);
203}
204
Andy Shevchenko978f3692018-02-06 15:38:13 -0800205static void __init test_fill_set(void)
206{
207 DECLARE_BITMAP(bmap, 1024);
208
209 /* Known way to clear all bits */
210 memset(bmap, 0x00, 128);
211
212 expect_eq_pbl("", bmap, 23);
213 expect_eq_pbl("", bmap, 1024);
214
215 /* single-word bitmaps */
216 bitmap_set(bmap, 0, 9);
217 expect_eq_pbl("0-8", bmap, 1024);
218
219 bitmap_fill(bmap, 35);
220 expect_eq_pbl("0-63", bmap, 1024);
221
222 /* cross boundaries operations */
223 bitmap_set(bmap, 79, 19);
224 expect_eq_pbl("0-63,79-97", bmap, 1024);
225
226 bitmap_fill(bmap, 115);
227 expect_eq_pbl("0-127", bmap, 1024);
228
229 /* Zeroing entire area */
230 bitmap_fill(bmap, 1024);
231 expect_eq_pbl("0-1023", bmap, 1024);
232}
233
Andy Shevchenkofe818142018-02-06 15:38:17 -0800234static void __init test_copy(void)
David Decotigny5fd003f2016-02-19 09:24:00 -0500235{
236 DECLARE_BITMAP(bmap1, 1024);
237 DECLARE_BITMAP(bmap2, 1024);
238
239 bitmap_zero(bmap1, 1024);
240 bitmap_zero(bmap2, 1024);
241
242 /* single-word bitmaps */
Andy Shevchenkofe818142018-02-06 15:38:17 -0800243 bitmap_set(bmap1, 0, 19);
David Decotigny5fd003f2016-02-19 09:24:00 -0500244 bitmap_copy(bmap2, bmap1, 23);
245 expect_eq_pbl("0-18", bmap2, 1024);
246
Andy Shevchenkofe818142018-02-06 15:38:17 -0800247 bitmap_set(bmap2, 0, 23);
David Decotigny5fd003f2016-02-19 09:24:00 -0500248 bitmap_copy(bmap2, bmap1, 23);
249 expect_eq_pbl("0-18", bmap2, 1024);
250
David Decotigny5fd003f2016-02-19 09:24:00 -0500251 /* multi-word bitmaps */
Andy Shevchenkofe818142018-02-06 15:38:17 -0800252 bitmap_set(bmap1, 0, 109);
David Decotigny5fd003f2016-02-19 09:24:00 -0500253 bitmap_copy(bmap2, bmap1, 1024);
254 expect_eq_pbl("0-108", bmap2, 1024);
255
256 bitmap_fill(bmap2, 1024);
David Decotigny5fd003f2016-02-19 09:24:00 -0500257 bitmap_copy(bmap2, bmap1, 1024);
258 expect_eq_pbl("0-108", bmap2, 1024);
259
260 /* the following tests assume a 32- or 64-bit arch (even 128b
261 * if we care)
262 */
263
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);
267
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);
David Decotigny5fd003f2016-02-19 09:24:00 -0500271}
272
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800273#define EXP2_IN_BITS (sizeof(exp2) * 8)
274
275static void __init test_replace(void)
276{
277 unsigned int nbits = 64;
Andy Shevchenko69334ca2020-01-30 22:11:01 -0800278 unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG);
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800279 DECLARE_BITMAP(bmap, 1024);
280
281 bitmap_zero(bmap, 1024);
Andy Shevchenko69334ca2020-01-30 22:11:01 -0800282 bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800283 expect_eq_bitmap(bmap, exp3_0_1, nbits);
284
285 bitmap_zero(bmap, 1024);
Andy Shevchenko69334ca2020-01-30 22:11:01 -0800286 bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800287 expect_eq_bitmap(bmap, exp3_1_0, nbits);
288
289 bitmap_fill(bmap, 1024);
Andy Shevchenko69334ca2020-01-30 22:11:01 -0800290 bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800291 expect_eq_bitmap(bmap, exp3_0_1, nbits);
292
293 bitmap_fill(bmap, 1024);
Andy Shevchenko69334ca2020-01-30 22:11:01 -0800294 bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800295 expect_eq_bitmap(bmap, exp3_1_0, nbits);
296}
297
Yury Norov6df0d462017-09-08 16:15:38 -0700298#define PARSE_TIME 0x1
299
300struct test_bitmap_parselist{
301 const int errno;
302 const char *in;
303 const unsigned long *expected;
304 const int nbits;
305 const int flags;
306};
307
Yury Norov6df0d462017-09-08 16:15:38 -0700308static const struct test_bitmap_parselist parselist_tests[] __initconst = {
Yury Norov60ef6902017-09-08 16:15:41 -0700309#define step (sizeof(u64) / sizeof(unsigned long))
310
Andy Shevchenko0ee312e2019-12-04 16:53:17 -0800311 {0, "0", &exp1[0], 8, 0},
312 {0, "1", &exp1[1 * step], 8, 0},
313 {0, "0-15", &exp1[2 * step], 32, 0},
314 {0, "16-31", &exp1[3 * step], 32, 0},
315 {0, "0-31:1/2", &exp1[4 * step], 32, 0},
316 {0, "1-31:1/2", &exp1[5 * step], 32, 0},
317 {0, "0-31:1/4", &exp1[6 * step], 32, 0},
318 {0, "1-31:1/4", &exp1[7 * step], 32, 0},
319 {0, "0-31:4/4", &exp1[8 * step], 32, 0},
320 {0, "1-31:4/4", &exp1[9 * step], 32, 0},
321 {0, "0-31:1/4,32-63:2/4", &exp1[10 * step], 64, 0},
322 {0, "0-31:3/4,32-63:4/4", &exp1[11 * step], 64, 0},
323 {0, " ,, 0-31:3/4 ,, 32-63:4/4 ,, ", &exp1[11 * step], 64, 0},
Yury Norov6df0d462017-09-08 16:15:38 -0700324
325 {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0},
326
327 {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
328
Andy Shevchenko0ee312e2019-12-04 16:53:17 -0800329 {0, "", &exp1[12 * step], 8, 0},
330 {0, "\n", &exp1[12 * step], 8, 0},
331 {0, ",, ,, , , ,", &exp1[12 * step], 8, 0},
332 {0, " , ,, , , ", &exp1[12 * step], 8, 0},
333 {0, " , ,, , , \n", &exp1[12 * step], 8, 0},
Yury Norova4ab5052019-05-14 15:43:21 -0700334
Yury Norov6df0d462017-09-08 16:15:38 -0700335 {-EINVAL, "-1", NULL, 8, 0},
336 {-EINVAL, "-0", NULL, 8, 0},
337 {-EINVAL, "10-1", NULL, 8, 0},
Yury Norov83517602018-04-05 16:18:25 -0700338 {-EINVAL, "0-31:", NULL, 8, 0},
339 {-EINVAL, "0-31:0", NULL, 8, 0},
Yury Norova4ab5052019-05-14 15:43:21 -0700340 {-EINVAL, "0-31:0/", NULL, 8, 0},
Yury Norov83517602018-04-05 16:18:25 -0700341 {-EINVAL, "0-31:0/0", NULL, 8, 0},
342 {-EINVAL, "0-31:1/0", NULL, 8, 0},
Yury Norov6df0d462017-09-08 16:15:38 -0700343 {-EINVAL, "0-31:10/1", NULL, 8, 0},
Yury Norova4ab5052019-05-14 15:43:21 -0700344 {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0},
345
346 {-EINVAL, "a-31", NULL, 8, 0},
347 {-EINVAL, "0-a1", NULL, 8, 0},
348 {-EINVAL, "a-31:10/1", NULL, 8, 0},
349 {-EINVAL, "0-31:a/1", NULL, 8, 0},
350 {-EINVAL, "0-\n", NULL, 8, 0},
Andy Shevchenko54224042019-12-04 16:53:09 -0800351
352#undef step
Yury Norov6df0d462017-09-08 16:15:38 -0700353};
354
Yury Norov6ea86bd2019-05-14 15:43:24 -0700355static void __init __test_bitmap_parselist(int is_user)
Yury Norov6df0d462017-09-08 16:15:38 -0700356{
357 int i;
358 int err;
Yury Norov0c2111a2019-05-14 15:43:18 -0700359 ktime_t time;
Yury Norov6df0d462017-09-08 16:15:38 -0700360 DECLARE_BITMAP(bmap, 2048);
Yury Norov6ea86bd2019-05-14 15:43:24 -0700361 char *mode = is_user ? "_user" : "";
Yury Norov6df0d462017-09-08 16:15:38 -0700362
363 for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
364#define ptest parselist_tests[i]
365
Yury Norov6ea86bd2019-05-14 15:43:24 -0700366 if (is_user) {
367 mm_segment_t orig_fs = get_fs();
368 size_t len = strlen(ptest.in);
369
370 set_fs(KERNEL_DS);
371 time = ktime_get();
Andy Shevchenko17b67532019-12-04 16:53:06 -0800372 err = bitmap_parselist_user((__force const char __user *)ptest.in, len,
Yury Norov6ea86bd2019-05-14 15:43:24 -0700373 bmap, ptest.nbits);
374 time = ktime_get() - time;
375 set_fs(orig_fs);
376 } else {
377 time = ktime_get();
378 err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
379 time = ktime_get() - time;
380 }
Yury Norov6df0d462017-09-08 16:15:38 -0700381
382 if (err != ptest.errno) {
Yury Norov6ea86bd2019-05-14 15:43:24 -0700383 pr_err("parselist%s: %d: input is %s, errno is %d, expected %d\n",
384 mode, i, ptest.in, err, ptest.errno);
Yury Norov6df0d462017-09-08 16:15:38 -0700385 continue;
386 }
387
388 if (!err && ptest.expected
389 && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
Yury Norov6ea86bd2019-05-14 15:43:24 -0700390 pr_err("parselist%s: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
391 mode, i, ptest.in, bmap[0],
392 *ptest.expected);
Yury Norov6df0d462017-09-08 16:15:38 -0700393 continue;
394 }
395
396 if (ptest.flags & PARSE_TIME)
Yury Norov6ea86bd2019-05-14 15:43:24 -0700397 pr_err("parselist%s: %d: input is '%s' OK, Time: %llu\n",
398 mode, i, ptest.in, time);
Andy Shevchenko54224042019-12-04 16:53:09 -0800399
400#undef ptest
Yury Norov6df0d462017-09-08 16:15:38 -0700401 }
402}
403
Yury Norov6ea86bd2019-05-14 15:43:24 -0700404static void __init test_bitmap_parselist(void)
405{
406 __test_bitmap_parselist(0);
407}
408
409static void __init test_bitmap_parselist_user(void)
410{
411 __test_bitmap_parselist(1);
412}
413
Andy Shevchenko0ee312e2019-12-04 16:53:17 -0800414#define EXP1_IN_BITS (sizeof(exp1) * 8)
Kees Cookf6f66c12018-04-10 16:32:54 -0700415
Yury Norov3aa56882018-02-06 15:38:06 -0800416static void __init test_bitmap_arr32(void)
David Decotigny5fd003f2016-02-19 09:24:00 -0500417{
Kees Cookf6f66c12018-04-10 16:32:54 -0700418 unsigned int nbits, next_bit;
Andy Shevchenkoa4881d12019-12-04 16:53:13 -0800419 u32 arr[EXP1_IN_BITS / 32];
420 DECLARE_BITMAP(bmap2, EXP1_IN_BITS);
David Decotigny5fd003f2016-02-19 09:24:00 -0500421
Yury Norov3aa56882018-02-06 15:38:06 -0800422 memset(arr, 0xa5, sizeof(arr));
David Decotigny5fd003f2016-02-19 09:24:00 -0500423
Andy Shevchenkoa4881d12019-12-04 16:53:13 -0800424 for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) {
Andy Shevchenko0ee312e2019-12-04 16:53:17 -0800425 bitmap_to_arr32(arr, exp1, nbits);
Yury Norov3aa56882018-02-06 15:38:06 -0800426 bitmap_from_arr32(bmap2, arr, nbits);
Andy Shevchenko0ee312e2019-12-04 16:53:17 -0800427 expect_eq_bitmap(bmap2, exp1, nbits);
David Decotigny5fd003f2016-02-19 09:24:00 -0500428
Yury Norov3aa56882018-02-06 15:38:06 -0800429 next_bit = find_next_bit(bmap2,
430 round_up(nbits, BITS_PER_LONG), nbits);
431 if (next_bit < round_up(nbits, BITS_PER_LONG))
432 pr_err("bitmap_copy_arr32(nbits == %d:"
433 " tail is not safely cleared: %d\n",
434 nbits, next_bit);
David Decotigny5fd003f2016-02-19 09:24:00 -0500435
Andy Shevchenkoa4881d12019-12-04 16:53:13 -0800436 if (nbits < EXP1_IN_BITS - 32)
Yury Norov3aa56882018-02-06 15:38:06 -0800437 expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
438 0xa5a5a5a5);
David Decotigny5fd003f2016-02-19 09:24:00 -0500439 }
440}
441
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700442static void noinline __init test_mem_optimisations(void)
443{
444 DECLARE_BITMAP(bmap1, 1024);
445 DECLARE_BITMAP(bmap2, 1024);
446 unsigned int start, nbits;
447
448 for (start = 0; start < 1024; start += 8) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700449 for (nbits = 0; nbits < 1024 - start; nbits += 8) {
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700450 memset(bmap1, 0x5a, sizeof(bmap1));
451 memset(bmap2, 0x5a, sizeof(bmap2));
452
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700453 bitmap_set(bmap1, start, nbits);
454 __bitmap_set(bmap2, start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700455 if (!bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700456 printk("set not equal %d %d\n", start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700457 failed_tests++;
458 }
459 if (!__bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700460 printk("set not __equal %d %d\n", start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700461 failed_tests++;
462 }
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700463
464 bitmap_clear(bmap1, start, nbits);
465 __bitmap_clear(bmap2, start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700466 if (!bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700467 printk("clear not equal %d %d\n", start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700468 failed_tests++;
469 }
470 if (!__bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700471 printk("clear not __equal %d %d\n", start,
472 nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700473 failed_tests++;
474 }
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700475 }
476 }
477}
478
William Breathitt Graye4aa1682019-12-04 16:51:01 -0800479static const unsigned char clump_exp[] __initconst = {
480 0x01, /* 1 bit set */
481 0x02, /* non-edge 1 bit set */
482 0x00, /* zero bits set */
483 0x38, /* 3 bits set across 4-bit boundary */
484 0x38, /* Repeated clump */
485 0x0F, /* 4 bits set */
486 0xFF, /* all bits set */
487 0x05, /* non-adjacent 2 bits set */
488};
489
490static void __init test_for_each_set_clump8(void)
491{
492#define CLUMP_EXP_NUMBITS 64
493 DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS);
494 unsigned int start;
495 unsigned long clump;
496
497 /* set bitmap to test case */
498 bitmap_zero(bits, CLUMP_EXP_NUMBITS);
499 bitmap_set(bits, 0, 1); /* 0x01 */
500 bitmap_set(bits, 9, 1); /* 0x02 */
501 bitmap_set(bits, 27, 3); /* 0x28 */
502 bitmap_set(bits, 35, 3); /* 0x28 */
503 bitmap_set(bits, 40, 4); /* 0x0F */
504 bitmap_set(bits, 48, 8); /* 0xFF */
505 bitmap_set(bits, 56, 1); /* 0x05 - part 1 */
506 bitmap_set(bits, 58, 1); /* 0x05 - part 2 */
507
508 for_each_set_clump8(start, clump, bits, CLUMP_EXP_NUMBITS)
509 expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump);
510}
511
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100512static void __init selftest(void)
David Decotigny5fd003f2016-02-19 09:24:00 -0500513{
Andy Shevchenkoee3527bd2018-02-06 15:38:10 -0800514 test_zero_clear();
Andy Shevchenko978f3692018-02-06 15:38:13 -0800515 test_fill_set();
Andy Shevchenkofe818142018-02-06 15:38:17 -0800516 test_copy();
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800517 test_replace();
Yury Norov3aa56882018-02-06 15:38:06 -0800518 test_bitmap_arr32();
Yury Norov6df0d462017-09-08 16:15:38 -0700519 test_bitmap_parselist();
Yury Norov6ea86bd2019-05-14 15:43:24 -0700520 test_bitmap_parselist_user();
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700521 test_mem_optimisations();
William Breathitt Graye4aa1682019-12-04 16:51:01 -0800522 test_for_each_set_clump8();
David Decotigny5fd003f2016-02-19 09:24:00 -0500523}
524
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100525KSTM_MODULE_LOADERS(test_bitmap);
David Decotigny5fd003f2016-02-19 09:24:00 -0500526MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
527MODULE_LICENSE("GPL");