xref: /linux/lib/test_printf.c (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2707cc728SRasmus Villemoes /*
3707cc728SRasmus Villemoes  * Test cases for printf facility.
4707cc728SRasmus Villemoes  */
5707cc728SRasmus Villemoes 
6707cc728SRasmus Villemoes #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7707cc728SRasmus Villemoes 
8707cc728SRasmus Villemoes #include <linux/init.h>
9707cc728SRasmus Villemoes #include <linux/kernel.h>
10707cc728SRasmus Villemoes #include <linux/module.h>
11707cc728SRasmus Villemoes #include <linux/printk.h>
12707cc728SRasmus Villemoes #include <linux/random.h>
134d42c447SAndy Shevchenko #include <linux/rtc.h>
14707cc728SRasmus Villemoes #include <linux/slab.h>
1539ced19bSAndy Shevchenko #include <linux/sprintf.h>
16707cc728SRasmus Villemoes #include <linux/string.h>
17707cc728SRasmus Villemoes 
18857cca4dSRasmus Villemoes #include <linux/bitmap.h>
19251c7234SRasmus Villemoes #include <linux/dcache.h>
20707cc728SRasmus Villemoes #include <linux/socket.h>
21707cc728SRasmus Villemoes #include <linux/in.h>
22707cc728SRasmus Villemoes 
23edf14cdbSVlastimil Babka #include <linux/gfp.h>
24edf14cdbSVlastimil Babka #include <linux/mm.h>
25edf14cdbSVlastimil Babka 
26f1ce39dfSSakari Ailus #include <linux/property.h>
27f1ce39dfSSakari Ailus 
286b1a4d5bSTobin C. Harding #include "../tools/testing/selftests/kselftest_module.h"
296b1a4d5bSTobin C. Harding 
30707cc728SRasmus Villemoes #define BUF_SIZE 256
31331e4debSRasmus Villemoes #define PAD_SIZE 16
32707cc728SRasmus Villemoes #define FILL_CHAR '$'
33707cc728SRasmus Villemoes 
3496dd9a2fSJustin Stitt #define NOWARN(option, comment, block) \
3596dd9a2fSJustin Stitt 	__diag_push(); \
3696dd9a2fSJustin Stitt 	__diag_ignore_all(#option, comment); \
3796dd9a2fSJustin Stitt 	block \
3896dd9a2fSJustin Stitt 	__diag_pop();
3996dd9a2fSJustin Stitt 
404e89a787STimur Tabi KSTM_MODULE_GLOBALS();
414e89a787STimur Tabi 
42707cc728SRasmus Villemoes static char *test_buffer __initdata;
43331e4debSRasmus Villemoes static char *alloced_buffer __initdata;
44707cc728SRasmus Villemoes 
45707cc728SRasmus Villemoes static int __printf(4, 0) __init
46707cc728SRasmus Villemoes do_test(int bufsize, const char *expect, int elen,
47707cc728SRasmus Villemoes 	const char *fmt, va_list ap)
48707cc728SRasmus Villemoes {
49707cc728SRasmus Villemoes 	va_list aq;
50707cc728SRasmus Villemoes 	int ret, written;
51707cc728SRasmus Villemoes 
52707cc728SRasmus Villemoes 	total_tests++;
53707cc728SRasmus Villemoes 
54331e4debSRasmus Villemoes 	memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
55707cc728SRasmus Villemoes 	va_copy(aq, ap);
56707cc728SRasmus Villemoes 	ret = vsnprintf(test_buffer, bufsize, fmt, aq);
57707cc728SRasmus Villemoes 	va_end(aq);
58707cc728SRasmus Villemoes 
59707cc728SRasmus Villemoes 	if (ret != elen) {
60707cc728SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
61707cc728SRasmus Villemoes 			bufsize, fmt, ret, elen);
62707cc728SRasmus Villemoes 		return 1;
63707cc728SRasmus Villemoes 	}
64707cc728SRasmus Villemoes 
65331e4debSRasmus Villemoes 	if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
66331e4debSRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
67331e4debSRasmus Villemoes 		return 1;
68331e4debSRasmus Villemoes 	}
69331e4debSRasmus Villemoes 
70707cc728SRasmus Villemoes 	if (!bufsize) {
71331e4debSRasmus Villemoes 		if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
72707cc728SRasmus Villemoes 			pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
73707cc728SRasmus Villemoes 				fmt);
74707cc728SRasmus Villemoes 			return 1;
75707cc728SRasmus Villemoes 		}
76707cc728SRasmus Villemoes 		return 0;
77707cc728SRasmus Villemoes 	}
78707cc728SRasmus Villemoes 
79707cc728SRasmus Villemoes 	written = min(bufsize-1, elen);
80707cc728SRasmus Villemoes 	if (test_buffer[written]) {
81707cc728SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
82707cc728SRasmus Villemoes 			bufsize, fmt);
83707cc728SRasmus Villemoes 		return 1;
84707cc728SRasmus Villemoes 	}
85707cc728SRasmus Villemoes 
869a3bfa01SRasmus Villemoes 	if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
87331e4debSRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
88331e4debSRasmus Villemoes 			bufsize, fmt);
89331e4debSRasmus Villemoes 		return 1;
90331e4debSRasmus Villemoes 	}
91331e4debSRasmus Villemoes 
929a3bfa01SRasmus Villemoes 	if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
939a3bfa01SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", bufsize, fmt);
949a3bfa01SRasmus Villemoes 		return 1;
959a3bfa01SRasmus Villemoes 	}
969a3bfa01SRasmus Villemoes 
97707cc728SRasmus Villemoes 	if (memcmp(test_buffer, expect, written)) {
98707cc728SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
99707cc728SRasmus Villemoes 			bufsize, fmt, test_buffer, written, expect);
100707cc728SRasmus Villemoes 		return 1;
101707cc728SRasmus Villemoes 	}
102707cc728SRasmus Villemoes 	return 0;
103707cc728SRasmus Villemoes }
104707cc728SRasmus Villemoes 
105707cc728SRasmus Villemoes static void __printf(3, 4) __init
106707cc728SRasmus Villemoes __test(const char *expect, int elen, const char *fmt, ...)
107707cc728SRasmus Villemoes {
108707cc728SRasmus Villemoes 	va_list ap;
109707cc728SRasmus Villemoes 	int rand;
110707cc728SRasmus Villemoes 	char *p;
111707cc728SRasmus Villemoes 
112fd0515d5SRasmus Villemoes 	if (elen >= BUF_SIZE) {
113fd0515d5SRasmus Villemoes 		pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
114fd0515d5SRasmus Villemoes 		       elen, fmt);
115fd0515d5SRasmus Villemoes 		failed_tests++;
116fd0515d5SRasmus Villemoes 		return;
117fd0515d5SRasmus Villemoes 	}
118707cc728SRasmus Villemoes 
119707cc728SRasmus Villemoes 	va_start(ap, fmt);
120707cc728SRasmus Villemoes 
121707cc728SRasmus Villemoes 	/*
122707cc728SRasmus Villemoes 	 * Every fmt+args is subjected to four tests: Three where we
123707cc728SRasmus Villemoes 	 * tell vsnprintf varying buffer sizes (plenty, not quite
124707cc728SRasmus Villemoes 	 * enough and 0), and then we also test that kvasprintf would
125707cc728SRasmus Villemoes 	 * be able to print it as expected.
126707cc728SRasmus Villemoes 	 */
127707cc728SRasmus Villemoes 	failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
128e8a533cbSJason A. Donenfeld 	rand = get_random_u32_inclusive(1, elen + 1);
129707cc728SRasmus Villemoes 	/* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
130707cc728SRasmus Villemoes 	failed_tests += do_test(rand, expect, elen, fmt, ap);
131707cc728SRasmus Villemoes 	failed_tests += do_test(0, expect, elen, fmt, ap);
132707cc728SRasmus Villemoes 
133707cc728SRasmus Villemoes 	p = kvasprintf(GFP_KERNEL, fmt, ap);
134707cc728SRasmus Villemoes 	if (p) {
135b79a7db3SRasmus Villemoes 		total_tests++;
136707cc728SRasmus Villemoes 		if (memcmp(p, expect, elen+1)) {
137707cc728SRasmus Villemoes 			pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
138707cc728SRasmus Villemoes 				fmt, p, expect);
139707cc728SRasmus Villemoes 			failed_tests++;
140707cc728SRasmus Villemoes 		}
141707cc728SRasmus Villemoes 		kfree(p);
142707cc728SRasmus Villemoes 	}
143707cc728SRasmus Villemoes 	va_end(ap);
144707cc728SRasmus Villemoes }
145707cc728SRasmus Villemoes 
146707cc728SRasmus Villemoes #define test(expect, fmt, ...)					\
147707cc728SRasmus Villemoes 	__test(expect, strlen(expect), fmt, ##__VA_ARGS__)
148707cc728SRasmus Villemoes 
149707cc728SRasmus Villemoes static void __init
150707cc728SRasmus Villemoes test_basic(void)
151707cc728SRasmus Villemoes {
152707cc728SRasmus Villemoes 	/* Work around annoying "warning: zero-length gnu_printf format string". */
153707cc728SRasmus Villemoes 	char nul = '\0';
154707cc728SRasmus Villemoes 
155707cc728SRasmus Villemoes 	test("", &nul);
156707cc728SRasmus Villemoes 	test("100%", "100%%");
157707cc728SRasmus Villemoes 	test("xxx%yyy", "xxx%cyyy", '%');
158707cc728SRasmus Villemoes 	__test("xxx\0yyy", 7, "xxx%cyyy", '\0');
159707cc728SRasmus Villemoes }
160707cc728SRasmus Villemoes 
161707cc728SRasmus Villemoes static void __init
162707cc728SRasmus Villemoes test_number(void)
163707cc728SRasmus Villemoes {
164707cc728SRasmus Villemoes 	test("0x1234abcd  ", "%#-12x", 0x1234abcd);
165707cc728SRasmus Villemoes 	test("  0x1234abcd", "%#12x", 0x1234abcd);
166707cc728SRasmus Villemoes 	test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
16796dd9a2fSJustin Stitt 	NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
1681ca8e8ebSRasmus Villemoes 		test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
1691ca8e8ebSRasmus Villemoes 		test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
1701ca8e8ebSRasmus Villemoes 		test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
17196dd9a2fSJustin Stitt 	})
1721ca8e8ebSRasmus Villemoes 	/*
1731ca8e8ebSRasmus Villemoes 	 * POSIX/C99: »The result of converting zero with an explicit
1741ca8e8ebSRasmus Villemoes 	 * precision of zero shall be no characters.« Hence the output
1751ca8e8ebSRasmus Villemoes 	 * from the below test should really be "00|0||| ". However,
1761ca8e8ebSRasmus Villemoes 	 * the kernel's printf also produces a single 0 in that
1771ca8e8ebSRasmus Villemoes 	 * case. This test case simply documents the current
1781ca8e8ebSRasmus Villemoes 	 * behaviour.
1791ca8e8ebSRasmus Villemoes 	 */
1801ca8e8ebSRasmus Villemoes 	test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
181707cc728SRasmus Villemoes }
182707cc728SRasmus Villemoes 
183707cc728SRasmus Villemoes static void __init
184707cc728SRasmus Villemoes test_string(void)
185707cc728SRasmus Villemoes {
186707cc728SRasmus Villemoes 	test("", "%s%.0s", "", "123");
187707cc728SRasmus Villemoes 	test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
188707cc728SRasmus Villemoes 	test("1  |  2|3  |  4|5  ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
189f176eb4cSRasmus Villemoes 	test("1234      ", "%-10.4s", "123456");
190f176eb4cSRasmus Villemoes 	test("      1234", "%10.4s", "123456");
191707cc728SRasmus Villemoes 	/*
192f176eb4cSRasmus Villemoes 	 * POSIX and C99 say that a negative precision (which is only
193f176eb4cSRasmus Villemoes 	 * possible to pass via a * argument) should be treated as if
194f176eb4cSRasmus Villemoes 	 * the precision wasn't present, and that if the precision is
195f176eb4cSRasmus Villemoes 	 * omitted (as in %.s), the precision should be taken to be
196f176eb4cSRasmus Villemoes 	 * 0. However, the kernel's printf behave exactly opposite,
197f176eb4cSRasmus Villemoes 	 * treating a negative precision as 0 and treating an omitted
198f176eb4cSRasmus Villemoes 	 * precision specifier as if no precision was given.
199f176eb4cSRasmus Villemoes 	 *
200f176eb4cSRasmus Villemoes 	 * These test cases document the current behaviour; should
201f176eb4cSRasmus Villemoes 	 * anyone ever feel the need to follow the standards more
202f176eb4cSRasmus Villemoes 	 * closely, this can be revisited.
203707cc728SRasmus Villemoes 	 */
204f176eb4cSRasmus Villemoes 	test("    ", "%4.*s", -5, "123456");
205f176eb4cSRasmus Villemoes 	test("123456", "%.s", "123456");
206707cc728SRasmus Villemoes 	test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
207707cc728SRasmus Villemoes 	test("a  |   |   ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
208707cc728SRasmus Villemoes }
209707cc728SRasmus Villemoes 
210ad67b74dSTobin C. Harding #define PLAIN_BUF_SIZE 64	/* leave some space so we don't oops */
211ad67b74dSTobin C. Harding 
212ad67b74dSTobin C. Harding #if BITS_PER_LONG == 64
213ad67b74dSTobin C. Harding 
214ad67b74dSTobin C. Harding #define PTR_WIDTH 16
215c604b407SAndy Shevchenko #define PTR ((void *)0xffff0123456789abUL)
216ad67b74dSTobin C. Harding #define PTR_STR "ffff0123456789ab"
217ce041c43SThierry Escande #define PTR_VAL_NO_CRNG "(____ptrval____)"
218ad67b74dSTobin C. Harding #define ZEROS "00000000"	/* hex 32 zero bits */
2197bd57fbcSIlya Dryomov #define ONES "ffffffff"		/* hex 32 one bits */
220ad67b74dSTobin C. Harding 
221ad67b74dSTobin C. Harding static int __init
222ad67b74dSTobin C. Harding plain_format(void)
223ad67b74dSTobin C. Harding {
224ad67b74dSTobin C. Harding 	char buf[PLAIN_BUF_SIZE];
225ad67b74dSTobin C. Harding 	int nchars;
226ad67b74dSTobin C. Harding 
227ad67b74dSTobin C. Harding 	nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
228ad67b74dSTobin C. Harding 
229ce041c43SThierry Escande 	if (nchars != PTR_WIDTH)
230ce041c43SThierry Escande 		return -1;
231ce041c43SThierry Escande 
232ce041c43SThierry Escande 	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
233ce041c43SThierry Escande 		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
234ce041c43SThierry Escande 			PTR_VAL_NO_CRNG);
235ce041c43SThierry Escande 		return 0;
236ce041c43SThierry Escande 	}
237ce041c43SThierry Escande 
238ce041c43SThierry Escande 	if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
239ad67b74dSTobin C. Harding 		return -1;
240ad67b74dSTobin C. Harding 
241ad67b74dSTobin C. Harding 	return 0;
242ad67b74dSTobin C. Harding }
243ad67b74dSTobin C. Harding 
244ad67b74dSTobin C. Harding #else
245ad67b74dSTobin C. Harding 
246ad67b74dSTobin C. Harding #define PTR_WIDTH 8
247ad67b74dSTobin C. Harding #define PTR ((void *)0x456789ab)
248ad67b74dSTobin C. Harding #define PTR_STR "456789ab"
249ce041c43SThierry Escande #define PTR_VAL_NO_CRNG "(ptrval)"
2503e5903ebSPetr Mladek #define ZEROS ""
2517bd57fbcSIlya Dryomov #define ONES ""
252ad67b74dSTobin C. Harding 
253ad67b74dSTobin C. Harding static int __init
254ad67b74dSTobin C. Harding plain_format(void)
255ad67b74dSTobin C. Harding {
256ad67b74dSTobin C. Harding 	/* Format is implicitly tested for 32 bit machines by plain_hash() */
257ad67b74dSTobin C. Harding 	return 0;
258ad67b74dSTobin C. Harding }
259ad67b74dSTobin C. Harding 
260ad67b74dSTobin C. Harding #endif	/* BITS_PER_LONG == 64 */
261ad67b74dSTobin C. Harding 
262ad67b74dSTobin C. Harding static int __init
2634d42c447SAndy Shevchenko plain_hash_to_buffer(const void *p, char *buf, size_t len)
264ad67b74dSTobin C. Harding {
265ad67b74dSTobin C. Harding 	int nchars;
266ad67b74dSTobin C. Harding 
2674d42c447SAndy Shevchenko 	nchars = snprintf(buf, len, "%p", p);
268ad67b74dSTobin C. Harding 
269ce041c43SThierry Escande 	if (nchars != PTR_WIDTH)
270ce041c43SThierry Escande 		return -1;
271ce041c43SThierry Escande 
272ce041c43SThierry Escande 	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
273ce041c43SThierry Escande 		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
274ce041c43SThierry Escande 			PTR_VAL_NO_CRNG);
275ce041c43SThierry Escande 		return 0;
276ce041c43SThierry Escande 	}
277ce041c43SThierry Escande 
2784d42c447SAndy Shevchenko 	return 0;
2794d42c447SAndy Shevchenko }
2804d42c447SAndy Shevchenko 
2814d42c447SAndy Shevchenko static int __init
2824d42c447SAndy Shevchenko plain_hash(void)
2834d42c447SAndy Shevchenko {
2844d42c447SAndy Shevchenko 	char buf[PLAIN_BUF_SIZE];
2854d42c447SAndy Shevchenko 	int ret;
2864d42c447SAndy Shevchenko 
2874d42c447SAndy Shevchenko 	ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
2884d42c447SAndy Shevchenko 	if (ret)
2894d42c447SAndy Shevchenko 		return ret;
2904d42c447SAndy Shevchenko 
291ce041c43SThierry Escande 	if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
292ad67b74dSTobin C. Harding 		return -1;
293ad67b74dSTobin C. Harding 
294ad67b74dSTobin C. Harding 	return 0;
295ad67b74dSTobin C. Harding }
296ad67b74dSTobin C. Harding 
297ad67b74dSTobin C. Harding /*
298ad67b74dSTobin C. Harding  * We can't use test() to test %p because we don't know what output to expect
299ad67b74dSTobin C. Harding  * after an address is hashed.
300ad67b74dSTobin C. Harding  */
301707cc728SRasmus Villemoes static void __init
302707cc728SRasmus Villemoes plain(void)
303707cc728SRasmus Villemoes {
304ad67b74dSTobin C. Harding 	int err;
305707cc728SRasmus Villemoes 
3065ead723aSTimur Tabi 	if (no_hash_pointers) {
3075ead723aSTimur Tabi 		pr_warn("skipping plain 'p' tests");
3085ead723aSTimur Tabi 		skipped_tests += 2;
3095ead723aSTimur Tabi 		return;
3105ead723aSTimur Tabi 	}
3115ead723aSTimur Tabi 
312ad67b74dSTobin C. Harding 	err = plain_hash();
313ad67b74dSTobin C. Harding 	if (err) {
314ad67b74dSTobin C. Harding 		pr_warn("plain 'p' does not appear to be hashed\n");
315ad67b74dSTobin C. Harding 		failed_tests++;
316ad67b74dSTobin C. Harding 		return;
317ad67b74dSTobin C. Harding 	}
318ad67b74dSTobin C. Harding 
319ad67b74dSTobin C. Harding 	err = plain_format();
320ad67b74dSTobin C. Harding 	if (err) {
321ad67b74dSTobin C. Harding 		pr_warn("hashing plain 'p' has unexpected format\n");
322ad67b74dSTobin C. Harding 		failed_tests++;
323ad67b74dSTobin C. Harding 	}
324707cc728SRasmus Villemoes }
325707cc728SRasmus Villemoes 
326707cc728SRasmus Villemoes static void __init
3274d42c447SAndy Shevchenko test_hashed(const char *fmt, const void *p)
3284d42c447SAndy Shevchenko {
3294d42c447SAndy Shevchenko 	char buf[PLAIN_BUF_SIZE];
3304d42c447SAndy Shevchenko 	int ret;
3314d42c447SAndy Shevchenko 
3324d42c447SAndy Shevchenko 	/*
3334d42c447SAndy Shevchenko 	 * No need to increase failed test counter since this is assumed
3344d42c447SAndy Shevchenko 	 * to be called after plain().
3354d42c447SAndy Shevchenko 	 */
3364d42c447SAndy Shevchenko 	ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
3374d42c447SAndy Shevchenko 	if (ret)
3384d42c447SAndy Shevchenko 		return;
3394d42c447SAndy Shevchenko 
3404d42c447SAndy Shevchenko 	test(buf, fmt, p);
3414d42c447SAndy Shevchenko }
3424d42c447SAndy Shevchenko 
3437bd57fbcSIlya Dryomov /*
3447bd57fbcSIlya Dryomov  * NULL pointers aren't hashed.
3457bd57fbcSIlya Dryomov  */
3464d42c447SAndy Shevchenko static void __init
3473e5903ebSPetr Mladek null_pointer(void)
3483e5903ebSPetr Mladek {
3497bd57fbcSIlya Dryomov 	test(ZEROS "00000000", "%p", NULL);
3503e5903ebSPetr Mladek 	test(ZEROS "00000000", "%px", NULL);
3513e5903ebSPetr Mladek 	test("(null)", "%pE", NULL);
3523e5903ebSPetr Mladek }
3533e5903ebSPetr Mladek 
3547bd57fbcSIlya Dryomov /*
3557bd57fbcSIlya Dryomov  * Error pointers aren't hashed.
3567bd57fbcSIlya Dryomov  */
3577bd57fbcSIlya Dryomov static void __init
3587bd57fbcSIlya Dryomov error_pointer(void)
3597bd57fbcSIlya Dryomov {
3607bd57fbcSIlya Dryomov 	test(ONES "fffffff5", "%p", ERR_PTR(-11));
3617bd57fbcSIlya Dryomov 	test(ONES "fffffff5", "%px", ERR_PTR(-11));
3627bd57fbcSIlya Dryomov 	test("(efault)", "%pE", ERR_PTR(-11));
3637bd57fbcSIlya Dryomov }
3647bd57fbcSIlya Dryomov 
3653e5903ebSPetr Mladek #define PTR_INVALID ((void *)0x000000ab)
3663e5903ebSPetr Mladek 
3673e5903ebSPetr Mladek static void __init
3683e5903ebSPetr Mladek invalid_pointer(void)
3693e5903ebSPetr Mladek {
3703e5903ebSPetr Mladek 	test_hashed("%p", PTR_INVALID);
3713e5903ebSPetr Mladek 	test(ZEROS "000000ab", "%px", PTR_INVALID);
3723e5903ebSPetr Mladek 	test("(efault)", "%pE", PTR_INVALID);
3733e5903ebSPetr Mladek }
3743e5903ebSPetr Mladek 
3753e5903ebSPetr Mladek static void __init
376707cc728SRasmus Villemoes symbol_ptr(void)
377707cc728SRasmus Villemoes {
378707cc728SRasmus Villemoes }
379707cc728SRasmus Villemoes 
380707cc728SRasmus Villemoes static void __init
381707cc728SRasmus Villemoes kernel_ptr(void)
382707cc728SRasmus Villemoes {
383ad67b74dSTobin C. Harding 	/* We can't test this without access to kptr_restrict. */
384707cc728SRasmus Villemoes }
385707cc728SRasmus Villemoes 
386707cc728SRasmus Villemoes static void __init
387707cc728SRasmus Villemoes struct_resource(void)
388707cc728SRasmus Villemoes {
389707cc728SRasmus Villemoes }
390707cc728SRasmus Villemoes 
391707cc728SRasmus Villemoes static void __init
392707cc728SRasmus Villemoes addr(void)
393707cc728SRasmus Villemoes {
394707cc728SRasmus Villemoes }
395707cc728SRasmus Villemoes 
396707cc728SRasmus Villemoes static void __init
397707cc728SRasmus Villemoes escaped_str(void)
398707cc728SRasmus Villemoes {
399707cc728SRasmus Villemoes }
400707cc728SRasmus Villemoes 
401707cc728SRasmus Villemoes static void __init
402707cc728SRasmus Villemoes hex_string(void)
403707cc728SRasmus Villemoes {
404707cc728SRasmus Villemoes 	const char buf[3] = {0xc0, 0xff, 0xee};
405707cc728SRasmus Villemoes 
406707cc728SRasmus Villemoes 	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
407707cc728SRasmus Villemoes 	     "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
408707cc728SRasmus Villemoes 	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
409707cc728SRasmus Villemoes 	     "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
410707cc728SRasmus Villemoes }
411707cc728SRasmus Villemoes 
412707cc728SRasmus Villemoes static void __init
413707cc728SRasmus Villemoes mac(void)
414707cc728SRasmus Villemoes {
415707cc728SRasmus Villemoes 	const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
416707cc728SRasmus Villemoes 
417707cc728SRasmus Villemoes 	test("2d:48:d6:fc:7a:05", "%pM", addr);
418707cc728SRasmus Villemoes 	test("05:7a:fc:d6:48:2d", "%pMR", addr);
419707cc728SRasmus Villemoes 	test("2d-48-d6-fc-7a-05", "%pMF", addr);
420707cc728SRasmus Villemoes 	test("2d48d6fc7a05", "%pm", addr);
421707cc728SRasmus Villemoes 	test("057afcd6482d", "%pmR", addr);
422707cc728SRasmus Villemoes }
423707cc728SRasmus Villemoes 
424707cc728SRasmus Villemoes static void __init
425707cc728SRasmus Villemoes ip4(void)
426707cc728SRasmus Villemoes {
427707cc728SRasmus Villemoes 	struct sockaddr_in sa;
428707cc728SRasmus Villemoes 
429707cc728SRasmus Villemoes 	sa.sin_family = AF_INET;
430707cc728SRasmus Villemoes 	sa.sin_port = cpu_to_be16(12345);
431707cc728SRasmus Villemoes 	sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
432707cc728SRasmus Villemoes 
433707cc728SRasmus Villemoes 	test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
434707cc728SRasmus Villemoes 	test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
435707cc728SRasmus Villemoes 	sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
436707cc728SRasmus Villemoes 	test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
437707cc728SRasmus Villemoes }
438707cc728SRasmus Villemoes 
439707cc728SRasmus Villemoes static void __init
440707cc728SRasmus Villemoes ip6(void)
441707cc728SRasmus Villemoes {
442707cc728SRasmus Villemoes }
443707cc728SRasmus Villemoes 
444707cc728SRasmus Villemoes static void __init
445707cc728SRasmus Villemoes ip(void)
446707cc728SRasmus Villemoes {
447707cc728SRasmus Villemoes 	ip4();
448707cc728SRasmus Villemoes 	ip6();
449707cc728SRasmus Villemoes }
450707cc728SRasmus Villemoes 
451707cc728SRasmus Villemoes static void __init
452707cc728SRasmus Villemoes uuid(void)
453707cc728SRasmus Villemoes {
454707cc728SRasmus Villemoes 	const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
455707cc728SRasmus Villemoes 			       0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
456707cc728SRasmus Villemoes 
457707cc728SRasmus Villemoes 	test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
458707cc728SRasmus Villemoes 	test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
459707cc728SRasmus Villemoes 	test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
460707cc728SRasmus Villemoes 	test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
461707cc728SRasmus Villemoes }
462707cc728SRasmus Villemoes 
463251c7234SRasmus Villemoes static struct dentry test_dentry[4] __initdata = {
464251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[0],
465251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
466251c7234SRasmus Villemoes 	  .d_iname = "foo" },
467251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[0],
468251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
469251c7234SRasmus Villemoes 	  .d_iname = "bravo" },
470251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[1],
471251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
472251c7234SRasmus Villemoes 	  .d_iname = "alfa" },
473251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[2],
474251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
475251c7234SRasmus Villemoes 	  .d_iname = "romeo" },
476251c7234SRasmus Villemoes };
477251c7234SRasmus Villemoes 
478707cc728SRasmus Villemoes static void __init
479707cc728SRasmus Villemoes dentry(void)
480707cc728SRasmus Villemoes {
481251c7234SRasmus Villemoes 	test("foo", "%pd", &test_dentry[0]);
482251c7234SRasmus Villemoes 	test("foo", "%pd2", &test_dentry[0]);
483251c7234SRasmus Villemoes 
484cf6b7921SJia He 	test("(null)", "%pd", NULL);
485cf6b7921SJia He 	test("(efault)", "%pd", PTR_INVALID);
486cf6b7921SJia He 	test("(null)", "%pD", NULL);
487cf6b7921SJia He 	test("(efault)", "%pD", PTR_INVALID);
488cf6b7921SJia He 
489251c7234SRasmus Villemoes 	test("romeo", "%pd", &test_dentry[3]);
490251c7234SRasmus Villemoes 	test("alfa/romeo", "%pd2", &test_dentry[3]);
491251c7234SRasmus Villemoes 	test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
492251c7234SRasmus Villemoes 	test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
493251c7234SRasmus Villemoes 	test("/bravo/alfa", "%pd4", &test_dentry[2]);
494251c7234SRasmus Villemoes 
495251c7234SRasmus Villemoes 	test("bravo/alfa  |bravo/alfa  ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
496251c7234SRasmus Villemoes 	test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
497707cc728SRasmus Villemoes }
498707cc728SRasmus Villemoes 
499707cc728SRasmus Villemoes static void __init
500707cc728SRasmus Villemoes struct_va_format(void)
501707cc728SRasmus Villemoes {
502707cc728SRasmus Villemoes }
503707cc728SRasmus Villemoes 
504707cc728SRasmus Villemoes static void __init
5057daac5b2SAndy Shevchenko time_and_date(void)
5064d42c447SAndy Shevchenko {
5074d42c447SAndy Shevchenko 	/* 1543210543 */
5084d42c447SAndy Shevchenko 	const struct rtc_time tm = {
5094d42c447SAndy Shevchenko 		.tm_sec = 43,
5104d42c447SAndy Shevchenko 		.tm_min = 35,
5114d42c447SAndy Shevchenko 		.tm_hour = 5,
5124d42c447SAndy Shevchenko 		.tm_mday = 26,
5134d42c447SAndy Shevchenko 		.tm_mon = 10,
5144d42c447SAndy Shevchenko 		.tm_year = 118,
5154d42c447SAndy Shevchenko 	};
5167daac5b2SAndy Shevchenko 	/* 2019-01-04T15:32:23 */
5177daac5b2SAndy Shevchenko 	time64_t t = 1546615943;
5184d42c447SAndy Shevchenko 
5197daac5b2SAndy Shevchenko 	test("(%pt?)", "%pt", &tm);
5204d42c447SAndy Shevchenko 	test("2018-11-26T05:35:43", "%ptR", &tm);
5214d42c447SAndy Shevchenko 	test("0118-10-26T05:35:43", "%ptRr", &tm);
5224d42c447SAndy Shevchenko 	test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
5234d42c447SAndy Shevchenko 	test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
5244d42c447SAndy Shevchenko 	test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
5254d42c447SAndy Shevchenko 	test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
5267daac5b2SAndy Shevchenko 
5277daac5b2SAndy Shevchenko 	test("2019-01-04T15:32:23", "%ptT", &t);
5287daac5b2SAndy Shevchenko 	test("0119-00-04T15:32:23", "%ptTr", &t);
5297daac5b2SAndy Shevchenko 	test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
5307daac5b2SAndy Shevchenko 	test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
53120bc8c1eSAndy Shevchenko 
53220bc8c1eSAndy Shevchenko 	test("2019-01-04 15:32:23", "%ptTs", &t);
53320bc8c1eSAndy Shevchenko 	test("0119-00-04 15:32:23", "%ptTsr", &t);
53420bc8c1eSAndy Shevchenko 	test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t);
53520bc8c1eSAndy Shevchenko 	test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t);
5364d42c447SAndy Shevchenko }
5374d42c447SAndy Shevchenko 
5384d42c447SAndy Shevchenko static void __init
539707cc728SRasmus Villemoes struct_clk(void)
540707cc728SRasmus Villemoes {
541707cc728SRasmus Villemoes }
542707cc728SRasmus Villemoes 
543707cc728SRasmus Villemoes static void __init
544857cca4dSRasmus Villemoes large_bitmap(void)
545857cca4dSRasmus Villemoes {
546857cca4dSRasmus Villemoes 	const int nbits = 1 << 16;
5472821fd0cSAndy Shevchenko 	unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
548857cca4dSRasmus Villemoes 	if (!bits)
549857cca4dSRasmus Villemoes 		return;
550857cca4dSRasmus Villemoes 
551857cca4dSRasmus Villemoes 	bitmap_set(bits, 1, 20);
552857cca4dSRasmus Villemoes 	bitmap_set(bits, 60000, 15);
553857cca4dSRasmus Villemoes 	test("1-20,60000-60014", "%*pbl", nbits, bits);
5542821fd0cSAndy Shevchenko 	bitmap_free(bits);
555857cca4dSRasmus Villemoes }
556857cca4dSRasmus Villemoes 
557857cca4dSRasmus Villemoes static void __init
558707cc728SRasmus Villemoes bitmap(void)
559707cc728SRasmus Villemoes {
560707cc728SRasmus Villemoes 	DECLARE_BITMAP(bits, 20);
561707cc728SRasmus Villemoes 	const int primes[] = {2,3,5,7,11,13,17,19};
562707cc728SRasmus Villemoes 	int i;
563707cc728SRasmus Villemoes 
564707cc728SRasmus Villemoes 	bitmap_zero(bits, 20);
565707cc728SRasmus Villemoes 	test("00000|00000", "%20pb|%*pb", bits, 20, bits);
566707cc728SRasmus Villemoes 	test("|", "%20pbl|%*pbl", bits, 20, bits);
567707cc728SRasmus Villemoes 
568707cc728SRasmus Villemoes 	for (i = 0; i < ARRAY_SIZE(primes); ++i)
569707cc728SRasmus Villemoes 		set_bit(primes[i], bits);
570707cc728SRasmus Villemoes 	test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
571707cc728SRasmus Villemoes 	test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
572707cc728SRasmus Villemoes 
573707cc728SRasmus Villemoes 	bitmap_fill(bits, 20);
574707cc728SRasmus Villemoes 	test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
575707cc728SRasmus Villemoes 	test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
576857cca4dSRasmus Villemoes 
577857cca4dSRasmus Villemoes 	large_bitmap();
578707cc728SRasmus Villemoes }
579707cc728SRasmus Villemoes 
580707cc728SRasmus Villemoes static void __init
581707cc728SRasmus Villemoes netdev_features(void)
582707cc728SRasmus Villemoes {
583707cc728SRasmus Villemoes }
584707cc728SRasmus Villemoes 
585c244297aSYafang Shao struct page_flags_test {
586c244297aSYafang Shao 	int width;
587c244297aSYafang Shao 	int shift;
588c244297aSYafang Shao 	int mask;
589c244297aSYafang Shao 	const char *fmt;
590c244297aSYafang Shao 	const char *name;
591c244297aSYafang Shao };
592c244297aSYafang Shao 
593c666d447SMatthew Wilcox (Oracle) static const struct page_flags_test pft[] = {
594c244297aSYafang Shao 	{SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK,
595c666d447SMatthew Wilcox (Oracle) 	 "%d", "section"},
596c244297aSYafang Shao 	{NODES_WIDTH, NODES_PGSHIFT, NODES_MASK,
597c666d447SMatthew Wilcox (Oracle) 	 "%d", "node"},
598c244297aSYafang Shao 	{ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK,
599c666d447SMatthew Wilcox (Oracle) 	 "%d", "zone"},
600c244297aSYafang Shao 	{LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK,
601c666d447SMatthew Wilcox (Oracle) 	 "%#x", "lastcpupid"},
602c244297aSYafang Shao 	{KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK,
603c666d447SMatthew Wilcox (Oracle) 	 "%#x", "kasantag"},
604c244297aSYafang Shao };
605c244297aSYafang Shao 
606c244297aSYafang Shao static void __init
607c244297aSYafang Shao page_flags_test(int section, int node, int zone, int last_cpupid,
608a25a0854SMatthew Wilcox (Oracle) 		int kasan_tag, unsigned long flags, const char *name,
609a25a0854SMatthew Wilcox (Oracle) 		char *cmp_buf)
610c244297aSYafang Shao {
611c244297aSYafang Shao 	unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag};
61223efd080SMatthew Wilcox (Oracle) 	unsigned long size;
613c244297aSYafang Shao 	bool append = false;
614c244297aSYafang Shao 	int i;
615c244297aSYafang Shao 
61623efd080SMatthew Wilcox (Oracle) 	for (i = 0; i < ARRAY_SIZE(values); i++)
61723efd080SMatthew Wilcox (Oracle) 		flags |= (values[i] & pft[i].mask) << pft[i].shift;
61823efd080SMatthew Wilcox (Oracle) 
61923efd080SMatthew Wilcox (Oracle) 	size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags);
620a25a0854SMatthew Wilcox (Oracle) 	if (flags & PAGEFLAGS_MASK) {
621507f9860SMatthew Wilcox (Oracle) 		size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
6225b358b0dSMatthew Wilcox (Oracle) 		append = true;
623c244297aSYafang Shao 	}
624c244297aSYafang Shao 
625c244297aSYafang Shao 	for (i = 0; i < ARRAY_SIZE(pft); i++) {
626c244297aSYafang Shao 		if (!pft[i].width)
627c244297aSYafang Shao 			continue;
628c244297aSYafang Shao 
629507f9860SMatthew Wilcox (Oracle) 		if (append)
630507f9860SMatthew Wilcox (Oracle) 			size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");
631c244297aSYafang Shao 
632507f9860SMatthew Wilcox (Oracle) 		size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
633507f9860SMatthew Wilcox (Oracle) 				pft[i].name);
634507f9860SMatthew Wilcox (Oracle) 		size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
635c666d447SMatthew Wilcox (Oracle) 				values[i] & pft[i].mask);
636c244297aSYafang Shao 		append = true;
637c244297aSYafang Shao 	}
638c244297aSYafang Shao 
63923efd080SMatthew Wilcox (Oracle) 	snprintf(cmp_buf + size, BUF_SIZE - size, ")");
64023efd080SMatthew Wilcox (Oracle) 
641a25a0854SMatthew Wilcox (Oracle) 	test(cmp_buf, "%pGp", &flags);
642c244297aSYafang Shao }
643c244297aSYafang Shao 
6444c85c0beSHyeonggon Yoo static void __init page_type_test(unsigned int page_type, const char *name,
6454c85c0beSHyeonggon Yoo 				  char *cmp_buf)
6464c85c0beSHyeonggon Yoo {
6474c85c0beSHyeonggon Yoo 	unsigned long size;
6484c85c0beSHyeonggon Yoo 
6494c85c0beSHyeonggon Yoo 	size = scnprintf(cmp_buf, BUF_SIZE, "%#x(", page_type);
6504c85c0beSHyeonggon Yoo 	if (page_type_has_type(page_type))
6514c85c0beSHyeonggon Yoo 		size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
6524c85c0beSHyeonggon Yoo 
6534c85c0beSHyeonggon Yoo 	snprintf(cmp_buf + size, BUF_SIZE - size, ")");
6544c85c0beSHyeonggon Yoo 	test(cmp_buf, "%pGt", &page_type);
6554c85c0beSHyeonggon Yoo }
6564c85c0beSHyeonggon Yoo 
657707cc728SRasmus Villemoes static void __init
658edf14cdbSVlastimil Babka flags(void)
659edf14cdbSVlastimil Babka {
660edf14cdbSVlastimil Babka 	unsigned long flags;
661edf14cdbSVlastimil Babka 	char *cmp_buffer;
662c244297aSYafang Shao 	gfp_t gfp;
6634c85c0beSHyeonggon Yoo 	unsigned int page_type;
664c244297aSYafang Shao 
665c244297aSYafang Shao 	cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
666c244297aSYafang Shao 	if (!cmp_buffer)
667c244297aSYafang Shao 		return;
668edf14cdbSVlastimil Babka 
669edf14cdbSVlastimil Babka 	flags = 0;
670c244297aSYafang Shao 	page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer);
671edf14cdbSVlastimil Babka 
672edf14cdbSVlastimil Babka 	flags = 1UL << NR_PAGEFLAGS;
673c244297aSYafang Shao 	page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer);
674edf14cdbSVlastimil Babka 
675edf14cdbSVlastimil Babka 	flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
676edf14cdbSVlastimil Babka 		| 1UL << PG_active | 1UL << PG_swapbacked;
677c244297aSYafang Shao 	page_flags_test(1, 1, 1, 0x1fffff, 1, flags,
678c244297aSYafang Shao 			"uptodate|dirty|lru|active|swapbacked",
679c244297aSYafang Shao 			cmp_buffer);
680edf14cdbSVlastimil Babka 
6818d0920bdSDavid Hildenbrand 	flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
6828d0920bdSDavid Hildenbrand 	test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags);
683edf14cdbSVlastimil Babka 
684edf14cdbSVlastimil Babka 	gfp = GFP_TRANSHUGE;
685edf14cdbSVlastimil Babka 	test("GFP_TRANSHUGE", "%pGg", &gfp);
686edf14cdbSVlastimil Babka 
687edf14cdbSVlastimil Babka 	gfp = GFP_ATOMIC|__GFP_DMA;
688edf14cdbSVlastimil Babka 	test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
689edf14cdbSVlastimil Babka 
6902973d822SNeilBrown 	gfp = __GFP_HIGH;
6912973d822SNeilBrown 	test("__GFP_HIGH", "%pGg", &gfp);
692edf14cdbSVlastimil Babka 
693edf14cdbSVlastimil Babka 	/* Any flags not translated by the table should remain numeric */
694edf14cdbSVlastimil Babka 	gfp = ~__GFP_BITS_MASK;
695edf14cdbSVlastimil Babka 	snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
696edf14cdbSVlastimil Babka 	test(cmp_buffer, "%pGg", &gfp);
697edf14cdbSVlastimil Babka 
6982973d822SNeilBrown 	snprintf(cmp_buffer, BUF_SIZE, "__GFP_HIGH|%#lx",
699edf14cdbSVlastimil Babka 							(unsigned long) gfp);
7002973d822SNeilBrown 	gfp |= __GFP_HIGH;
701edf14cdbSVlastimil Babka 	test(cmp_buffer, "%pGg", &gfp);
702edf14cdbSVlastimil Babka 
7034c85c0beSHyeonggon Yoo 	page_type = ~0;
7044c85c0beSHyeonggon Yoo 	page_type_test(page_type, "", cmp_buffer);
7054c85c0beSHyeonggon Yoo 
7064c85c0beSHyeonggon Yoo 	page_type = 10;
7074c85c0beSHyeonggon Yoo 	page_type_test(page_type, "", cmp_buffer);
7084c85c0beSHyeonggon Yoo 
7094c85c0beSHyeonggon Yoo 	page_type = ~PG_buddy;
7104c85c0beSHyeonggon Yoo 	page_type_test(page_type, "buddy", cmp_buffer);
7114c85c0beSHyeonggon Yoo 
7124c85c0beSHyeonggon Yoo 	page_type = ~(PG_table | PG_buddy);
7134c85c0beSHyeonggon Yoo 	page_type_test(page_type, "table|buddy", cmp_buffer);
7144c85c0beSHyeonggon Yoo 
715edf14cdbSVlastimil Babka 	kfree(cmp_buffer);
716edf14cdbSVlastimil Babka }
717edf14cdbSVlastimil Babka 
718f1ce39dfSSakari Ailus static void __init fwnode_pointer(void)
719f1ce39dfSSakari Ailus {
720fd070e8cSAndy Shevchenko 	const struct software_node first = { .name = "first" };
721fd070e8cSAndy Shevchenko 	const struct software_node second = { .name = "second", .parent = &first };
722fd070e8cSAndy Shevchenko 	const struct software_node third = { .name = "third", .parent = &second };
723fd070e8cSAndy Shevchenko 	const struct software_node *group[] = { &first, &second, &third, NULL };
724f1ce39dfSSakari Ailus 	const char * const full_name_second = "first/second";
725fd070e8cSAndy Shevchenko 	const char * const full_name_third = "first/second/third";
726f1ce39dfSSakari Ailus 	const char * const second_name = "second";
727f1ce39dfSSakari Ailus 	const char * const third_name = "third";
728f1ce39dfSSakari Ailus 	int rval;
729f1ce39dfSSakari Ailus 
730fd070e8cSAndy Shevchenko 	rval = software_node_register_node_group(group);
731f1ce39dfSSakari Ailus 	if (rval) {
732f1ce39dfSSakari Ailus 		pr_warn("cannot register softnodes; rval %d\n", rval);
733f1ce39dfSSakari Ailus 		return;
734f1ce39dfSSakari Ailus 	}
735f1ce39dfSSakari Ailus 
736fd070e8cSAndy Shevchenko 	test(full_name_second, "%pfw", software_node_fwnode(&second));
737fd070e8cSAndy Shevchenko 	test(full_name_third, "%pfw", software_node_fwnode(&third));
738fd070e8cSAndy Shevchenko 	test(full_name_third, "%pfwf", software_node_fwnode(&third));
739fd070e8cSAndy Shevchenko 	test(second_name, "%pfwP", software_node_fwnode(&second));
740fd070e8cSAndy Shevchenko 	test(third_name, "%pfwP", software_node_fwnode(&third));
741f1ce39dfSSakari Ailus 
742fd070e8cSAndy Shevchenko 	software_node_unregister_node_group(group);
743f1ce39dfSSakari Ailus }
744f1ce39dfSSakari Ailus 
745af612e43SSakari Ailus static void __init fourcc_pointer(void)
746af612e43SSakari Ailus {
747af612e43SSakari Ailus 	struct {
748af612e43SSakari Ailus 		u32 code;
749af612e43SSakari Ailus 		char *str;
750af612e43SSakari Ailus 	} const try[] = {
751af612e43SSakari Ailus 		{ 0x3231564e, "NV12 little-endian (0x3231564e)", },
752af612e43SSakari Ailus 		{ 0xb231564e, "NV12 big-endian (0xb231564e)", },
753af612e43SSakari Ailus 		{ 0x10111213, ".... little-endian (0x10111213)", },
754af612e43SSakari Ailus 		{ 0x20303159, "Y10  little-endian (0x20303159)", },
755af612e43SSakari Ailus 	};
756af612e43SSakari Ailus 	unsigned int i;
757af612e43SSakari Ailus 
758af612e43SSakari Ailus 	for (i = 0; i < ARRAY_SIZE(try); i++)
759af612e43SSakari Ailus 		test(try[i].str, "%p4cc", &try[i].code);
760af612e43SSakari Ailus }
761af612e43SSakari Ailus 
762edf14cdbSVlastimil Babka static void __init
76357f5677eSRasmus Villemoes errptr(void)
76457f5677eSRasmus Villemoes {
76557f5677eSRasmus Villemoes 	test("-1234", "%pe", ERR_PTR(-1234));
76657f5677eSRasmus Villemoes 
76757f5677eSRasmus Villemoes 	/* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
76857f5677eSRasmus Villemoes 	BUILD_BUG_ON(IS_ERR(PTR));
76957f5677eSRasmus Villemoes 	test_hashed("%pe", PTR);
77057f5677eSRasmus Villemoes 
77157f5677eSRasmus Villemoes #ifdef CONFIG_SYMBOLIC_ERRNAME
77257f5677eSRasmus Villemoes 	test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
77357f5677eSRasmus Villemoes 	test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
77457f5677eSRasmus Villemoes 	BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
77557f5677eSRasmus Villemoes 	test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
77657f5677eSRasmus Villemoes 	test("[-EIO    ]", "[%-8pe]", ERR_PTR(-EIO));
77757f5677eSRasmus Villemoes 	test("[    -EIO]", "[%8pe]", ERR_PTR(-EIO));
77857f5677eSRasmus Villemoes 	test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
77957f5677eSRasmus Villemoes #endif
78057f5677eSRasmus Villemoes }
78157f5677eSRasmus Villemoes 
78257f5677eSRasmus Villemoes static void __init
783707cc728SRasmus Villemoes test_pointer(void)
784707cc728SRasmus Villemoes {
785707cc728SRasmus Villemoes 	plain();
7863e5903ebSPetr Mladek 	null_pointer();
7877bd57fbcSIlya Dryomov 	error_pointer();
7883e5903ebSPetr Mladek 	invalid_pointer();
789707cc728SRasmus Villemoes 	symbol_ptr();
790707cc728SRasmus Villemoes 	kernel_ptr();
791707cc728SRasmus Villemoes 	struct_resource();
792707cc728SRasmus Villemoes 	addr();
793707cc728SRasmus Villemoes 	escaped_str();
794707cc728SRasmus Villemoes 	hex_string();
795707cc728SRasmus Villemoes 	mac();
796707cc728SRasmus Villemoes 	ip();
797707cc728SRasmus Villemoes 	uuid();
798707cc728SRasmus Villemoes 	dentry();
799707cc728SRasmus Villemoes 	struct_va_format();
8007daac5b2SAndy Shevchenko 	time_and_date();
801707cc728SRasmus Villemoes 	struct_clk();
802707cc728SRasmus Villemoes 	bitmap();
803707cc728SRasmus Villemoes 	netdev_features();
804edf14cdbSVlastimil Babka 	flags();
80557f5677eSRasmus Villemoes 	errptr();
806f1ce39dfSSakari Ailus 	fwnode_pointer();
807af612e43SSakari Ailus 	fourcc_pointer();
808707cc728SRasmus Villemoes }
809707cc728SRasmus Villemoes 
8106b1a4d5bSTobin C. Harding static void __init selftest(void)
811707cc728SRasmus Villemoes {
812331e4debSRasmus Villemoes 	alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
813331e4debSRasmus Villemoes 	if (!alloced_buffer)
8146b1a4d5bSTobin C. Harding 		return;
815331e4debSRasmus Villemoes 	test_buffer = alloced_buffer + PAD_SIZE;
816707cc728SRasmus Villemoes 
817707cc728SRasmus Villemoes 	test_basic();
818707cc728SRasmus Villemoes 	test_number();
819707cc728SRasmus Villemoes 	test_string();
820707cc728SRasmus Villemoes 	test_pointer();
821707cc728SRasmus Villemoes 
822331e4debSRasmus Villemoes 	kfree(alloced_buffer);
823707cc728SRasmus Villemoes }
824707cc728SRasmus Villemoes 
8256b1a4d5bSTobin C. Harding KSTM_MODULE_LOADERS(test_printf);
826707cc728SRasmus Villemoes MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
827*a930fde9SJeff Johnson MODULE_DESCRIPTION("Test cases for printf facility");
828707cc728SRasmus Villemoes MODULE_LICENSE("GPL");
829