blob: 6640a82ad44b7df6f41c8f5ecb8e1c11a3b8f79d [file] [log] [blame]
David Decotigny5fd003f2016-02-19 09:24:00 -05001/*
2 * Test cases for printf facility.
3 */
4
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
7#include <linux/bitmap.h>
8#include <linux/init.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/printk.h>
12#include <linux/slab.h>
13#include <linux/string.h>
14
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +110015#include "../tools/testing/selftests/kselftest_module.h"
16
David Decotigny5fd003f2016-02-19 09:24:00 -050017static unsigned total_tests __initdata;
18static unsigned failed_tests __initdata;
19
20static char pbl_buffer[PAGE_SIZE] __initdata;
21
22
23static bool __init
24__check_eq_uint(const char *srcfile, unsigned int line,
25 const unsigned int exp_uint, unsigned int x)
26{
27 if (exp_uint != x) {
Yury Norov3aa56882018-02-06 15:38:06 -080028 pr_err("[%s:%u] expected %u, got %u\n",
David Decotigny5fd003f2016-02-19 09:24:00 -050029 srcfile, line, exp_uint, x);
30 return false;
31 }
32 return true;
33}
34
35
36static bool __init
37__check_eq_bitmap(const char *srcfile, unsigned int line,
Yury Norov3aa56882018-02-06 15:38:06 -080038 const unsigned long *exp_bmap, const unsigned long *bmap,
39 unsigned int nbits)
David Decotigny5fd003f2016-02-19 09:24:00 -050040{
David Decotigny5fd003f2016-02-19 09:24:00 -050041 if (!bitmap_equal(exp_bmap, bmap, nbits)) {
42 pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
43 srcfile, line,
Yury Norov3aa56882018-02-06 15:38:06 -080044 nbits, exp_bmap, nbits, bmap);
David Decotigny5fd003f2016-02-19 09:24:00 -050045 return false;
46 }
47 return true;
48}
49
50static bool __init
51__check_eq_pbl(const char *srcfile, unsigned int line,
52 const char *expected_pbl,
53 const unsigned long *bitmap, unsigned int nbits)
54{
55 snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
56 if (strcmp(expected_pbl, pbl_buffer)) {
57 pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
58 srcfile, line,
59 expected_pbl, pbl_buffer);
60 return false;
61 }
62 return true;
63}
64
65static bool __init
66__check_eq_u32_array(const char *srcfile, unsigned int line,
67 const u32 *exp_arr, unsigned int exp_len,
Yury Norov3aa56882018-02-06 15:38:06 -080068 const u32 *arr, unsigned int len) __used;
69static bool __init
70__check_eq_u32_array(const char *srcfile, unsigned int line,
71 const u32 *exp_arr, unsigned int exp_len,
David Decotigny5fd003f2016-02-19 09:24:00 -050072 const u32 *arr, unsigned int len)
73{
74 if (exp_len != len) {
75 pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
76 srcfile, line,
77 exp_len, len);
78 return false;
79 }
80
81 if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
82 pr_warn("[%s:%u] array contents differ\n", srcfile, line);
83 print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET,
84 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
85 print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET,
86 32, 4, arr, len*sizeof(*arr), false);
87 return false;
88 }
89
90 return true;
91}
92
93#define __expect_eq(suffix, ...) \
94 ({ \
95 int result = 0; \
96 total_tests++; \
97 if (!__check_eq_ ## suffix(__FILE__, __LINE__, \
98 ##__VA_ARGS__)) { \
99 failed_tests++; \
100 result = 1; \
101 } \
102 result; \
103 })
104
105#define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__)
106#define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__)
107#define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__)
108#define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__)
109
Andy Shevchenkoee3527bd2018-02-06 15:38:10 -0800110static void __init test_zero_clear(void)
111{
112 DECLARE_BITMAP(bmap, 1024);
113
114 /* Known way to set all bits */
115 memset(bmap, 0xff, 128);
116
117 expect_eq_pbl("0-22", bmap, 23);
118 expect_eq_pbl("0-1023", bmap, 1024);
119
120 /* single-word bitmaps */
121 bitmap_clear(bmap, 0, 9);
122 expect_eq_pbl("9-1023", bmap, 1024);
123
124 bitmap_zero(bmap, 35);
125 expect_eq_pbl("64-1023", bmap, 1024);
126
127 /* cross boundaries operations */
128 bitmap_clear(bmap, 79, 19);
129 expect_eq_pbl("64-78,98-1023", bmap, 1024);
130
131 bitmap_zero(bmap, 115);
132 expect_eq_pbl("128-1023", bmap, 1024);
133
134 /* Zeroing entire area */
135 bitmap_zero(bmap, 1024);
136 expect_eq_pbl("", bmap, 1024);
137}
138
Andy Shevchenko978f3692018-02-06 15:38:13 -0800139static void __init test_fill_set(void)
140{
141 DECLARE_BITMAP(bmap, 1024);
142
143 /* Known way to clear all bits */
144 memset(bmap, 0x00, 128);
145
146 expect_eq_pbl("", bmap, 23);
147 expect_eq_pbl("", bmap, 1024);
148
149 /* single-word bitmaps */
150 bitmap_set(bmap, 0, 9);
151 expect_eq_pbl("0-8", bmap, 1024);
152
153 bitmap_fill(bmap, 35);
154 expect_eq_pbl("0-63", bmap, 1024);
155
156 /* cross boundaries operations */
157 bitmap_set(bmap, 79, 19);
158 expect_eq_pbl("0-63,79-97", bmap, 1024);
159
160 bitmap_fill(bmap, 115);
161 expect_eq_pbl("0-127", bmap, 1024);
162
163 /* Zeroing entire area */
164 bitmap_fill(bmap, 1024);
165 expect_eq_pbl("0-1023", bmap, 1024);
166}
167
Andy Shevchenkofe818142018-02-06 15:38:17 -0800168static void __init test_copy(void)
David Decotigny5fd003f2016-02-19 09:24:00 -0500169{
170 DECLARE_BITMAP(bmap1, 1024);
171 DECLARE_BITMAP(bmap2, 1024);
172
173 bitmap_zero(bmap1, 1024);
174 bitmap_zero(bmap2, 1024);
175
176 /* single-word bitmaps */
Andy Shevchenkofe818142018-02-06 15:38:17 -0800177 bitmap_set(bmap1, 0, 19);
David Decotigny5fd003f2016-02-19 09:24:00 -0500178 bitmap_copy(bmap2, bmap1, 23);
179 expect_eq_pbl("0-18", bmap2, 1024);
180
Andy Shevchenkofe818142018-02-06 15:38:17 -0800181 bitmap_set(bmap2, 0, 23);
David Decotigny5fd003f2016-02-19 09:24:00 -0500182 bitmap_copy(bmap2, bmap1, 23);
183 expect_eq_pbl("0-18", bmap2, 1024);
184
David Decotigny5fd003f2016-02-19 09:24:00 -0500185 /* multi-word bitmaps */
Andy Shevchenkofe818142018-02-06 15:38:17 -0800186 bitmap_set(bmap1, 0, 109);
David Decotigny5fd003f2016-02-19 09:24:00 -0500187 bitmap_copy(bmap2, bmap1, 1024);
188 expect_eq_pbl("0-108", bmap2, 1024);
189
190 bitmap_fill(bmap2, 1024);
David Decotigny5fd003f2016-02-19 09:24:00 -0500191 bitmap_copy(bmap2, bmap1, 1024);
192 expect_eq_pbl("0-108", bmap2, 1024);
193
194 /* the following tests assume a 32- or 64-bit arch (even 128b
195 * if we care)
196 */
197
198 bitmap_fill(bmap2, 1024);
199 bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */
200 expect_eq_pbl("0-108,128-1023", bmap2, 1024);
201
202 bitmap_fill(bmap2, 1024);
203 bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */
204 expect_eq_pbl("0-108,128-1023", bmap2, 1024);
David Decotigny5fd003f2016-02-19 09:24:00 -0500205}
206
Yury Norov6df0d462017-09-08 16:15:38 -0700207#define PARSE_TIME 0x1
208
209struct test_bitmap_parselist{
210 const int errno;
211 const char *in;
212 const unsigned long *expected;
213 const int nbits;
214 const int flags;
215};
216
Yury Norov60ef6902017-09-08 16:15:41 -0700217static const unsigned long exp[] __initconst = {
218 BITMAP_FROM_U64(1),
219 BITMAP_FROM_U64(2),
220 BITMAP_FROM_U64(0x0000ffff),
221 BITMAP_FROM_U64(0xffff0000),
222 BITMAP_FROM_U64(0x55555555),
223 BITMAP_FROM_U64(0xaaaaaaaa),
224 BITMAP_FROM_U64(0x11111111),
225 BITMAP_FROM_U64(0x22222222),
226 BITMAP_FROM_U64(0xffffffff),
227 BITMAP_FROM_U64(0xfffffffe),
Geert Uytterhoeven8185f5702017-09-13 16:28:20 -0700228 BITMAP_FROM_U64(0x3333333311111111ULL),
Yury Norova4ab5052019-05-14 15:43:21 -0700229 BITMAP_FROM_U64(0xffffffff77777777ULL),
230 BITMAP_FROM_U64(0),
Yury Norov60ef6902017-09-08 16:15:41 -0700231};
232
233static const unsigned long exp2[] __initconst = {
Geert Uytterhoeven8185f5702017-09-13 16:28:20 -0700234 BITMAP_FROM_U64(0x3333333311111111ULL),
235 BITMAP_FROM_U64(0xffffffff77777777ULL)
Yury Norov60ef6902017-09-08 16:15:41 -0700236};
Yury Norov6df0d462017-09-08 16:15:38 -0700237
238static const struct test_bitmap_parselist parselist_tests[] __initconst = {
Yury Norov60ef6902017-09-08 16:15:41 -0700239#define step (sizeof(u64) / sizeof(unsigned long))
240
Yury Norov6df0d462017-09-08 16:15:38 -0700241 {0, "0", &exp[0], 8, 0},
Yury Norov60ef6902017-09-08 16:15:41 -0700242 {0, "1", &exp[1 * step], 8, 0},
243 {0, "0-15", &exp[2 * step], 32, 0},
244 {0, "16-31", &exp[3 * step], 32, 0},
245 {0, "0-31:1/2", &exp[4 * step], 32, 0},
246 {0, "1-31:1/2", &exp[5 * step], 32, 0},
247 {0, "0-31:1/4", &exp[6 * step], 32, 0},
248 {0, "1-31:1/4", &exp[7 * step], 32, 0},
249 {0, "0-31:4/4", &exp[8 * step], 32, 0},
250 {0, "1-31:4/4", &exp[9 * step], 32, 0},
251 {0, "0-31:1/4,32-63:2/4", &exp[10 * step], 64, 0},
252 {0, "0-31:3/4,32-63:4/4", &exp[11 * step], 64, 0},
Yury Norova4ab5052019-05-14 15:43:21 -0700253 {0, " ,, 0-31:3/4 ,, 32-63:4/4 ,, ", &exp[11 * step], 64, 0},
Yury Norov6df0d462017-09-08 16:15:38 -0700254
255 {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0},
256
257 {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
258
Yury Norova4ab5052019-05-14 15:43:21 -0700259 {0, "", &exp[12 * step], 8, 0},
260 {0, "\n", &exp[12 * step], 8, 0},
261 {0, ",, ,, , , ,", &exp[12 * step], 8, 0},
262 {0, " , ,, , , ", &exp[12 * step], 8, 0},
263 {0, " , ,, , , \n", &exp[12 * step], 8, 0},
264
Yury Norov6df0d462017-09-08 16:15:38 -0700265 {-EINVAL, "-1", NULL, 8, 0},
266 {-EINVAL, "-0", NULL, 8, 0},
267 {-EINVAL, "10-1", NULL, 8, 0},
Yury Norov83517602018-04-05 16:18:25 -0700268 {-EINVAL, "0-31:", NULL, 8, 0},
269 {-EINVAL, "0-31:0", NULL, 8, 0},
Yury Norova4ab5052019-05-14 15:43:21 -0700270 {-EINVAL, "0-31:0/", NULL, 8, 0},
Yury Norov83517602018-04-05 16:18:25 -0700271 {-EINVAL, "0-31:0/0", NULL, 8, 0},
272 {-EINVAL, "0-31:1/0", NULL, 8, 0},
Yury Norov6df0d462017-09-08 16:15:38 -0700273 {-EINVAL, "0-31:10/1", NULL, 8, 0},
Yury Norova4ab5052019-05-14 15:43:21 -0700274 {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0},
275
276 {-EINVAL, "a-31", NULL, 8, 0},
277 {-EINVAL, "0-a1", NULL, 8, 0},
278 {-EINVAL, "a-31:10/1", NULL, 8, 0},
279 {-EINVAL, "0-31:a/1", NULL, 8, 0},
280 {-EINVAL, "0-\n", NULL, 8, 0},
Yury Norov6df0d462017-09-08 16:15:38 -0700281};
282
283static void __init test_bitmap_parselist(void)
284{
285 int i;
286 int err;
Yury Norov0c2111a2019-05-14 15:43:18 -0700287 ktime_t time;
Yury Norov6df0d462017-09-08 16:15:38 -0700288 DECLARE_BITMAP(bmap, 2048);
289
290 for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
291#define ptest parselist_tests[i]
292
Yury Norov0c2111a2019-05-14 15:43:18 -0700293 time = ktime_get();
Yury Norov6df0d462017-09-08 16:15:38 -0700294 err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
Yury Norov0c2111a2019-05-14 15:43:18 -0700295 time = ktime_get() - time;
Yury Norov6df0d462017-09-08 16:15:38 -0700296
297 if (err != ptest.errno) {
298 pr_err("test %d: input is %s, errno is %d, expected %d\n",
299 i, ptest.in, err, ptest.errno);
300 continue;
301 }
302
303 if (!err && ptest.expected
304 && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
305 pr_err("test %d: input is %s, result is 0x%lx, expected 0x%lx\n",
306 i, ptest.in, bmap[0], *ptest.expected);
307 continue;
308 }
309
310 if (ptest.flags & PARSE_TIME)
311 pr_err("test %d: input is '%s' OK, Time: %llu\n",
Yury Norov0c2111a2019-05-14 15:43:18 -0700312 i, ptest.in, time);
Yury Norov6df0d462017-09-08 16:15:38 -0700313 }
314}
315
Kees Cookf6f66c12018-04-10 16:32:54 -0700316#define EXP_BYTES (sizeof(exp) * 8)
317
Yury Norov3aa56882018-02-06 15:38:06 -0800318static void __init test_bitmap_arr32(void)
David Decotigny5fd003f2016-02-19 09:24:00 -0500319{
Kees Cookf6f66c12018-04-10 16:32:54 -0700320 unsigned int nbits, next_bit;
Yury Norov3aa56882018-02-06 15:38:06 -0800321 u32 arr[sizeof(exp) / 4];
Kees Cookf6f66c12018-04-10 16:32:54 -0700322 DECLARE_BITMAP(bmap2, EXP_BYTES);
David Decotigny5fd003f2016-02-19 09:24:00 -0500323
Yury Norov3aa56882018-02-06 15:38:06 -0800324 memset(arr, 0xa5, sizeof(arr));
David Decotigny5fd003f2016-02-19 09:24:00 -0500325
Kees Cookf6f66c12018-04-10 16:32:54 -0700326 for (nbits = 0; nbits < EXP_BYTES; ++nbits) {
Yury Norov3aa56882018-02-06 15:38:06 -0800327 bitmap_to_arr32(arr, exp, nbits);
328 bitmap_from_arr32(bmap2, arr, nbits);
329 expect_eq_bitmap(bmap2, exp, nbits);
David Decotigny5fd003f2016-02-19 09:24:00 -0500330
Yury Norov3aa56882018-02-06 15:38:06 -0800331 next_bit = find_next_bit(bmap2,
332 round_up(nbits, BITS_PER_LONG), nbits);
333 if (next_bit < round_up(nbits, BITS_PER_LONG))
334 pr_err("bitmap_copy_arr32(nbits == %d:"
335 " tail is not safely cleared: %d\n",
336 nbits, next_bit);
David Decotigny5fd003f2016-02-19 09:24:00 -0500337
Kees Cookf6f66c12018-04-10 16:32:54 -0700338 if (nbits < EXP_BYTES - 32)
Yury Norov3aa56882018-02-06 15:38:06 -0800339 expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
340 0xa5a5a5a5);
David Decotigny5fd003f2016-02-19 09:24:00 -0500341 }
342}
343
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700344static void noinline __init test_mem_optimisations(void)
345{
346 DECLARE_BITMAP(bmap1, 1024);
347 DECLARE_BITMAP(bmap2, 1024);
348 unsigned int start, nbits;
349
350 for (start = 0; start < 1024; start += 8) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700351 for (nbits = 0; nbits < 1024 - start; nbits += 8) {
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700352 memset(bmap1, 0x5a, sizeof(bmap1));
353 memset(bmap2, 0x5a, sizeof(bmap2));
354
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700355 bitmap_set(bmap1, start, nbits);
356 __bitmap_set(bmap2, start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700357 if (!bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700358 printk("set not equal %d %d\n", start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700359 failed_tests++;
360 }
361 if (!__bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700362 printk("set not __equal %d %d\n", start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700363 failed_tests++;
364 }
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700365
366 bitmap_clear(bmap1, start, nbits);
367 __bitmap_clear(bmap2, start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700368 if (!bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700369 printk("clear not equal %d %d\n", start, nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700370 failed_tests++;
371 }
372 if (!__bitmap_equal(bmap1, bmap2, 1024)) {
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700373 printk("clear not __equal %d %d\n", start,
374 nbits);
Matthew Wilcox1e3054b2018-05-18 16:08:44 -0700375 failed_tests++;
376 }
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700377 }
378 }
379}
380
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100381static void __init selftest(void)
David Decotigny5fd003f2016-02-19 09:24:00 -0500382{
Andy Shevchenkoee3527bd2018-02-06 15:38:10 -0800383 test_zero_clear();
Andy Shevchenko978f3692018-02-06 15:38:13 -0800384 test_fill_set();
Andy Shevchenkofe818142018-02-06 15:38:17 -0800385 test_copy();
Yury Norov3aa56882018-02-06 15:38:06 -0800386 test_bitmap_arr32();
Yury Norov6df0d462017-09-08 16:15:38 -0700387 test_bitmap_parselist();
Matthew Wilcox3cc78122017-07-10 15:51:26 -0700388 test_mem_optimisations();
David Decotigny5fd003f2016-02-19 09:24:00 -0500389}
390
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100391KSTM_MODULE_LOADERS(test_bitmap);
David Decotigny5fd003f2016-02-19 09:24:00 -0500392MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
393MODULE_LICENSE("GPL");