blob: 2d9f520d2f275ca95562a01371a0bd33c9278505 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Rasmus Villemoes707cc722015-11-06 16:30:29 -08002/*
3 * Test cases for printf facility.
4 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/init.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/printk.h>
12#include <linux/random.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020013#include <linux/rtc.h>
Rasmus Villemoes707cc722015-11-06 16:30:29 -080014#include <linux/slab.h>
15#include <linux/string.h>
16
Rasmus Villemoes857cca42016-01-15 16:59:06 -080017#include <linux/bitmap.h>
Rasmus Villemoes251c7232016-01-15 16:59:09 -080018#include <linux/dcache.h>
Rasmus Villemoes707cc722015-11-06 16:30:29 -080019#include <linux/socket.h>
20#include <linux/in.h>
21
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070022#include <linux/gfp.h>
23#include <linux/mm.h>
24
Sakari Ailusf1ce39df2019-10-03 15:32:19 +030025#include <linux/property.h>
26
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +110027#include "../tools/testing/selftests/kselftest_module.h"
28
Rasmus Villemoes707cc722015-11-06 16:30:29 -080029#define BUF_SIZE 256
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080030#define PAD_SIZE 16
Rasmus Villemoes707cc722015-11-06 16:30:29 -080031#define FILL_CHAR '$'
32
Rasmus Villemoes707cc722015-11-06 16:30:29 -080033static unsigned total_tests __initdata;
34static unsigned failed_tests __initdata;
35static char *test_buffer __initdata;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080036static char *alloced_buffer __initdata;
Rasmus Villemoes707cc722015-11-06 16:30:29 -080037
38static int __printf(4, 0) __init
39do_test(int bufsize, const char *expect, int elen,
40 const char *fmt, va_list ap)
41{
42 va_list aq;
43 int ret, written;
44
45 total_tests++;
46
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080047 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
Rasmus Villemoes707cc722015-11-06 16:30:29 -080048 va_copy(aq, ap);
49 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
50 va_end(aq);
51
52 if (ret != elen) {
53 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
54 bufsize, fmt, ret, elen);
55 return 1;
56 }
57
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080058 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
59 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
60 return 1;
61 }
62
Rasmus Villemoes707cc722015-11-06 16:30:29 -080063 if (!bufsize) {
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080064 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
Rasmus Villemoes707cc722015-11-06 16:30:29 -080065 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
66 fmt);
67 return 1;
68 }
69 return 0;
70 }
71
72 written = min(bufsize-1, elen);
73 if (test_buffer[written]) {
74 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
75 bufsize, fmt);
76 return 1;
77 }
78
Rasmus Villemoes331e4de2016-01-15 16:58:53 -080079 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
80 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
81 bufsize, fmt);
82 return 1;
83 }
84
Rasmus Villemoes707cc722015-11-06 16:30:29 -080085 if (memcmp(test_buffer, expect, written)) {
86 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
87 bufsize, fmt, test_buffer, written, expect);
88 return 1;
89 }
90 return 0;
91}
92
93static void __printf(3, 4) __init
94__test(const char *expect, int elen, const char *fmt, ...)
95{
96 va_list ap;
97 int rand;
98 char *p;
99
Rasmus Villemoesfd0515d2016-01-15 16:58:50 -0800100 if (elen >= BUF_SIZE) {
101 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
102 elen, fmt);
103 failed_tests++;
104 return;
105 }
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800106
107 va_start(ap, fmt);
108
109 /*
110 * Every fmt+args is subjected to four tests: Three where we
111 * tell vsnprintf varying buffer sizes (plenty, not quite
112 * enough and 0), and then we also test that kvasprintf would
113 * be able to print it as expected.
114 */
115 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
116 rand = 1 + prandom_u32_max(elen+1);
117 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
118 failed_tests += do_test(rand, expect, elen, fmt, ap);
119 failed_tests += do_test(0, expect, elen, fmt, ap);
120
121 p = kvasprintf(GFP_KERNEL, fmt, ap);
122 if (p) {
Rasmus Villemoesb79a7db2016-01-15 16:59:02 -0800123 total_tests++;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800124 if (memcmp(p, expect, elen+1)) {
125 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
126 fmt, p, expect);
127 failed_tests++;
128 }
129 kfree(p);
130 }
131 va_end(ap);
132}
133
134#define test(expect, fmt, ...) \
135 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
136
137static void __init
138test_basic(void)
139{
140 /* Work around annoying "warning: zero-length gnu_printf format string". */
141 char nul = '\0';
142
143 test("", &nul);
144 test("100%", "100%%");
145 test("xxx%yyy", "xxx%cyyy", '%');
146 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
147}
148
149static void __init
150test_number(void)
151{
152 test("0x1234abcd ", "%#-12x", 0x1234abcd);
153 test(" 0x1234abcd", "%#12x", 0x1234abcd);
154 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
Rasmus Villemoes1ca8e8e2016-01-15 16:58:59 -0800155 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
156 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
157 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
158 /*
159 * POSIX/C99: »The result of converting zero with an explicit
160 * precision of zero shall be no characters.« Hence the output
161 * from the below test should really be "00|0||| ". However,
162 * the kernel's printf also produces a single 0 in that
163 * case. This test case simply documents the current
164 * behaviour.
165 */
166 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
167#ifndef __CHAR_UNSIGNED__
168 {
169 /*
170 * Passing a 'char' to a %02x specifier doesn't do
171 * what was presumably the intention when char is
172 * signed and the value is negative. One must either &
173 * with 0xff or cast to u8.
174 */
175 char val = -16;
176 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
177 }
178#endif
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800179}
180
181static void __init
182test_string(void)
183{
184 test("", "%s%.0s", "", "123");
185 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
186 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800187 test("1234 ", "%-10.4s", "123456");
188 test(" 1234", "%10.4s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800189 /*
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800190 * POSIX and C99 say that a negative precision (which is only
191 * possible to pass via a * argument) should be treated as if
192 * the precision wasn't present, and that if the precision is
193 * omitted (as in %.s), the precision should be taken to be
194 * 0. However, the kernel's printf behave exactly opposite,
195 * treating a negative precision as 0 and treating an omitted
196 * precision specifier as if no precision was given.
197 *
198 * These test cases document the current behaviour; should
199 * anyone ever feel the need to follow the standards more
200 * closely, this can be revisited.
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800201 */
Rasmus Villemoesf176eb42016-01-15 16:58:56 -0800202 test(" ", "%4.*s", -5, "123456");
203 test("123456", "%.s", "123456");
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800204 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
205 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
206}
207
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100208#define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
209
210#if BITS_PER_LONG == 64
211
212#define PTR_WIDTH 16
Andy Shevchenkoc604b402018-02-16 23:07:03 +0200213#define PTR ((void *)0xffff0123456789abUL)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100214#define PTR_STR "ffff0123456789ab"
Thierry Escandece041c42018-06-13 19:18:40 +0200215#define PTR_VAL_NO_CRNG "(____ptrval____)"
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100216#define ZEROS "00000000" /* hex 32 zero bits */
217
218static int __init
219plain_format(void)
220{
221 char buf[PLAIN_BUF_SIZE];
222 int nchars;
223
224 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
225
Thierry Escandece041c42018-06-13 19:18:40 +0200226 if (nchars != PTR_WIDTH)
227 return -1;
228
229 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
230 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
231 PTR_VAL_NO_CRNG);
232 return 0;
233 }
234
235 if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100236 return -1;
237
238 return 0;
239}
240
241#else
242
243#define PTR_WIDTH 8
244#define PTR ((void *)0x456789ab)
245#define PTR_STR "456789ab"
Thierry Escandece041c42018-06-13 19:18:40 +0200246#define PTR_VAL_NO_CRNG "(ptrval)"
Petr Mladek3e5903e2019-04-17 13:53:48 +0200247#define ZEROS ""
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100248
249static int __init
250plain_format(void)
251{
252 /* Format is implicitly tested for 32 bit machines by plain_hash() */
253 return 0;
254}
255
256#endif /* BITS_PER_LONG == 64 */
257
258static int __init
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200259plain_hash_to_buffer(const void *p, char *buf, size_t len)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100260{
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100261 int nchars;
262
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200263 nchars = snprintf(buf, len, "%p", p);
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100264
Thierry Escandece041c42018-06-13 19:18:40 +0200265 if (nchars != PTR_WIDTH)
266 return -1;
267
268 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
269 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
270 PTR_VAL_NO_CRNG);
271 return 0;
272 }
273
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200274 return 0;
275}
276
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200277static int __init
278plain_hash(void)
279{
280 char buf[PLAIN_BUF_SIZE];
281 int ret;
282
283 ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
284 if (ret)
285 return ret;
286
Thierry Escandece041c42018-06-13 19:18:40 +0200287 if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100288 return -1;
289
290 return 0;
291}
292
293/*
294 * We can't use test() to test %p because we don't know what output to expect
295 * after an address is hashed.
296 */
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800297static void __init
298plain(void)
299{
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100300 int err;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800301
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100302 err = plain_hash();
303 if (err) {
304 pr_warn("plain 'p' does not appear to be hashed\n");
305 failed_tests++;
306 return;
307 }
308
309 err = plain_format();
310 if (err) {
311 pr_warn("hashing plain 'p' has unexpected format\n");
312 failed_tests++;
313 }
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800314}
315
316static void __init
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200317test_hashed(const char *fmt, const void *p)
318{
319 char buf[PLAIN_BUF_SIZE];
320 int ret;
321
322 /*
323 * No need to increase failed test counter since this is assumed
324 * to be called after plain().
325 */
326 ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
327 if (ret)
328 return;
329
330 test(buf, fmt, p);
331}
332
333static void __init
Petr Mladek3e5903e2019-04-17 13:53:48 +0200334null_pointer(void)
335{
336 test_hashed("%p", NULL);
337 test(ZEROS "00000000", "%px", NULL);
338 test("(null)", "%pE", NULL);
339}
340
341#define PTR_INVALID ((void *)0x000000ab)
342
343static void __init
344invalid_pointer(void)
345{
346 test_hashed("%p", PTR_INVALID);
347 test(ZEROS "000000ab", "%px", PTR_INVALID);
348 test("(efault)", "%pE", PTR_INVALID);
349}
350
351static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800352symbol_ptr(void)
353{
354}
355
356static void __init
357kernel_ptr(void)
358{
Tobin C. Hardingad67b742017-11-01 15:32:23 +1100359 /* We can't test this without access to kptr_restrict. */
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800360}
361
362static void __init
363struct_resource(void)
364{
365}
366
367static void __init
368addr(void)
369{
370}
371
372static void __init
373escaped_str(void)
374{
375}
376
377static void __init
378hex_string(void)
379{
380 const char buf[3] = {0xc0, 0xff, 0xee};
381
382 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
383 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
384 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
385 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
386}
387
388static void __init
389mac(void)
390{
391 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
392
393 test("2d:48:d6:fc:7a:05", "%pM", addr);
394 test("05:7a:fc:d6:48:2d", "%pMR", addr);
395 test("2d-48-d6-fc-7a-05", "%pMF", addr);
396 test("2d48d6fc7a05", "%pm", addr);
397 test("057afcd6482d", "%pmR", addr);
398}
399
400static void __init
401ip4(void)
402{
403 struct sockaddr_in sa;
404
405 sa.sin_family = AF_INET;
406 sa.sin_port = cpu_to_be16(12345);
407 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
408
409 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
410 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
411 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
412 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
413}
414
415static void __init
416ip6(void)
417{
418}
419
420static void __init
421ip(void)
422{
423 ip4();
424 ip6();
425}
426
427static void __init
428uuid(void)
429{
430 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
431 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
432
433 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
434 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
435 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
436 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
437}
438
Rasmus Villemoes251c7232016-01-15 16:59:09 -0800439static struct dentry test_dentry[4] __initdata = {
440 { .d_parent = &test_dentry[0],
441 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
442 .d_iname = "foo" },
443 { .d_parent = &test_dentry[0],
444 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
445 .d_iname = "bravo" },
446 { .d_parent = &test_dentry[1],
447 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
448 .d_iname = "alfa" },
449 { .d_parent = &test_dentry[2],
450 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
451 .d_iname = "romeo" },
452};
453
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800454static void __init
455dentry(void)
456{
Rasmus Villemoes251c7232016-01-15 16:59:09 -0800457 test("foo", "%pd", &test_dentry[0]);
458 test("foo", "%pd2", &test_dentry[0]);
459
Jia Hecf6b7922019-08-09 09:24:57 +0800460 test("(null)", "%pd", NULL);
461 test("(efault)", "%pd", PTR_INVALID);
Jia Hecf6b7922019-08-09 09:24:57 +0800462 test("(null)", "%pD", NULL);
463 test("(efault)", "%pD", PTR_INVALID);
464
Rasmus Villemoes251c7232016-01-15 16:59:09 -0800465 test("romeo", "%pd", &test_dentry[3]);
466 test("alfa/romeo", "%pd2", &test_dentry[3]);
467 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
468 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
469 test("/bravo/alfa", "%pd4", &test_dentry[2]);
470
471 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
472 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800473}
474
475static void __init
476struct_va_format(void)
477{
478}
479
480static void __init
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200481struct_rtc_time(void)
482{
483 /* 1543210543 */
484 const struct rtc_time tm = {
485 .tm_sec = 43,
486 .tm_min = 35,
487 .tm_hour = 5,
488 .tm_mday = 26,
489 .tm_mon = 10,
490 .tm_year = 118,
491 };
492
Petr Mladek0b74d4d2019-04-17 13:53:47 +0200493 test("(%ptR?)", "%pt", &tm);
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200494 test("2018-11-26T05:35:43", "%ptR", &tm);
495 test("0118-10-26T05:35:43", "%ptRr", &tm);
496 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
497 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
498 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
499 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
500}
501
502static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800503struct_clk(void)
504{
505}
506
507static void __init
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800508large_bitmap(void)
509{
510 const int nbits = 1 << 16;
Andy Shevchenko2821fd02019-03-04 12:00:09 +0200511 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800512 if (!bits)
513 return;
514
515 bitmap_set(bits, 1, 20);
516 bitmap_set(bits, 60000, 15);
517 test("1-20,60000-60014", "%*pbl", nbits, bits);
Andy Shevchenko2821fd02019-03-04 12:00:09 +0200518 bitmap_free(bits);
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800519}
520
521static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800522bitmap(void)
523{
524 DECLARE_BITMAP(bits, 20);
525 const int primes[] = {2,3,5,7,11,13,17,19};
526 int i;
527
528 bitmap_zero(bits, 20);
529 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
530 test("|", "%20pbl|%*pbl", bits, 20, bits);
531
532 for (i = 0; i < ARRAY_SIZE(primes); ++i)
533 set_bit(primes[i], bits);
534 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
535 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
536
537 bitmap_fill(bits, 20);
538 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
539 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
Rasmus Villemoes857cca42016-01-15 16:59:06 -0800540
541 large_bitmap();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800542}
543
544static void __init
545netdev_features(void)
546{
547}
548
549static void __init
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -0700550flags(void)
551{
552 unsigned long flags;
553 gfp_t gfp;
554 char *cmp_buffer;
555
556 flags = 0;
557 test("", "%pGp", &flags);
558
559 /* Page flags should filter the zone id */
560 flags = 1UL << NR_PAGEFLAGS;
561 test("", "%pGp", &flags);
562
563 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
564 | 1UL << PG_active | 1UL << PG_swapbacked;
565 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
566
567
568 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
569 | VM_DENYWRITE;
570 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
571
572 gfp = GFP_TRANSHUGE;
573 test("GFP_TRANSHUGE", "%pGg", &gfp);
574
575 gfp = GFP_ATOMIC|__GFP_DMA;
576 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
577
578 gfp = __GFP_ATOMIC;
579 test("__GFP_ATOMIC", "%pGg", &gfp);
580
581 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
582 if (!cmp_buffer)
583 return;
584
585 /* Any flags not translated by the table should remain numeric */
586 gfp = ~__GFP_BITS_MASK;
587 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
588 test(cmp_buffer, "%pGg", &gfp);
589
590 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
591 (unsigned long) gfp);
592 gfp |= __GFP_ATOMIC;
593 test(cmp_buffer, "%pGg", &gfp);
594
595 kfree(cmp_buffer);
596}
597
Sakari Ailusf1ce39df2019-10-03 15:32:19 +0300598static void __init fwnode_pointer(void)
599{
600 const struct software_node softnodes[] = {
601 { .name = "first", },
602 { .name = "second", .parent = &softnodes[0], },
603 { .name = "third", .parent = &softnodes[1], },
604 { NULL /* Guardian */ }
605 };
606 const char * const full_name = "first/second/third";
607 const char * const full_name_second = "first/second";
608 const char * const second_name = "second";
609 const char * const third_name = "third";
610 int rval;
611
612 rval = software_node_register_nodes(softnodes);
613 if (rval) {
614 pr_warn("cannot register softnodes; rval %d\n", rval);
615 return;
616 }
617
618 test(full_name_second, "%pfw", software_node_fwnode(&softnodes[1]));
619 test(full_name, "%pfw", software_node_fwnode(&softnodes[2]));
620 test(full_name, "%pfwf", software_node_fwnode(&softnodes[2]));
621 test(second_name, "%pfwP", software_node_fwnode(&softnodes[1]));
622 test(third_name, "%pfwP", software_node_fwnode(&softnodes[2]));
623
624 software_node_unregister_nodes(softnodes);
625}
626
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -0700627static void __init
Rasmus Villemoes57f56772019-10-15 21:07:05 +0200628errptr(void)
629{
630 test("-1234", "%pe", ERR_PTR(-1234));
631
632 /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
633 BUILD_BUG_ON(IS_ERR(PTR));
634 test_hashed("%pe", PTR);
635
636#ifdef CONFIG_SYMBOLIC_ERRNAME
637 test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
638 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
639 BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
640 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
641 test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO));
642 test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO));
643 test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
644#endif
645}
646
647static void __init
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800648test_pointer(void)
649{
650 plain();
Petr Mladek3e5903e2019-04-17 13:53:48 +0200651 null_pointer();
652 invalid_pointer();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800653 symbol_ptr();
654 kernel_ptr();
655 struct_resource();
656 addr();
657 escaped_str();
658 hex_string();
659 mac();
660 ip();
661 uuid();
662 dentry();
663 struct_va_format();
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200664 struct_rtc_time();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800665 struct_clk();
666 bitmap();
667 netdev_features();
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -0700668 flags();
Rasmus Villemoes57f56772019-10-15 21:07:05 +0200669 errptr();
Sakari Ailusf1ce39df2019-10-03 15:32:19 +0300670 fwnode_pointer();
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800671}
672
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100673static void __init selftest(void)
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800674{
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800675 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
676 if (!alloced_buffer)
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100677 return;
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800678 test_buffer = alloced_buffer + PAD_SIZE;
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800679
680 test_basic();
681 test_number();
682 test_string();
683 test_pointer();
684
Rasmus Villemoes331e4de2016-01-15 16:58:53 -0800685 kfree(alloced_buffer);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800686}
687
Tobin C. Harding6b1a4d52019-04-05 12:58:57 +1100688KSTM_MODULE_LOADERS(test_printf);
Rasmus Villemoes707cc722015-11-06 16:30:29 -0800689MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
690MODULE_LICENSE("GPL");