xref: /linux/lib/test_printf.c (revision e8a533cbeb79809206f8724e89961e0079508c3c)
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>
15707cc728SRasmus Villemoes #include <linux/string.h>
16707cc728SRasmus Villemoes 
17857cca4dSRasmus Villemoes #include <linux/bitmap.h>
18251c7234SRasmus Villemoes #include <linux/dcache.h>
19707cc728SRasmus Villemoes #include <linux/socket.h>
20707cc728SRasmus Villemoes #include <linux/in.h>
21707cc728SRasmus Villemoes 
22edf14cdbSVlastimil Babka #include <linux/gfp.h>
23edf14cdbSVlastimil Babka #include <linux/mm.h>
24edf14cdbSVlastimil Babka 
25f1ce39dfSSakari Ailus #include <linux/property.h>
26f1ce39dfSSakari Ailus 
276b1a4d5bSTobin C. Harding #include "../tools/testing/selftests/kselftest_module.h"
286b1a4d5bSTobin C. Harding 
29707cc728SRasmus Villemoes #define BUF_SIZE 256
30331e4debSRasmus Villemoes #define PAD_SIZE 16
31707cc728SRasmus Villemoes #define FILL_CHAR '$'
32707cc728SRasmus Villemoes 
3396dd9a2fSJustin Stitt #define NOWARN(option, comment, block) \
3496dd9a2fSJustin Stitt 	__diag_push(); \
3596dd9a2fSJustin Stitt 	__diag_ignore_all(#option, comment); \
3696dd9a2fSJustin Stitt 	block \
3796dd9a2fSJustin Stitt 	__diag_pop();
3896dd9a2fSJustin Stitt 
394e89a787STimur Tabi KSTM_MODULE_GLOBALS();
404e89a787STimur Tabi 
41707cc728SRasmus Villemoes static char *test_buffer __initdata;
42331e4debSRasmus Villemoes static char *alloced_buffer __initdata;
43707cc728SRasmus Villemoes 
445ead723aSTimur Tabi extern bool no_hash_pointers;
455ead723aSTimur Tabi 
46707cc728SRasmus Villemoes static int __printf(4, 0) __init
47707cc728SRasmus Villemoes do_test(int bufsize, const char *expect, int elen,
48707cc728SRasmus Villemoes 	const char *fmt, va_list ap)
49707cc728SRasmus Villemoes {
50707cc728SRasmus Villemoes 	va_list aq;
51707cc728SRasmus Villemoes 	int ret, written;
52707cc728SRasmus Villemoes 
53707cc728SRasmus Villemoes 	total_tests++;
54707cc728SRasmus Villemoes 
55331e4debSRasmus Villemoes 	memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
56707cc728SRasmus Villemoes 	va_copy(aq, ap);
57707cc728SRasmus Villemoes 	ret = vsnprintf(test_buffer, bufsize, fmt, aq);
58707cc728SRasmus Villemoes 	va_end(aq);
59707cc728SRasmus Villemoes 
60707cc728SRasmus Villemoes 	if (ret != elen) {
61707cc728SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
62707cc728SRasmus Villemoes 			bufsize, fmt, ret, elen);
63707cc728SRasmus Villemoes 		return 1;
64707cc728SRasmus Villemoes 	}
65707cc728SRasmus Villemoes 
66331e4debSRasmus Villemoes 	if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
67331e4debSRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
68331e4debSRasmus Villemoes 		return 1;
69331e4debSRasmus Villemoes 	}
70331e4debSRasmus Villemoes 
71707cc728SRasmus Villemoes 	if (!bufsize) {
72331e4debSRasmus Villemoes 		if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
73707cc728SRasmus Villemoes 			pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
74707cc728SRasmus Villemoes 				fmt);
75707cc728SRasmus Villemoes 			return 1;
76707cc728SRasmus Villemoes 		}
77707cc728SRasmus Villemoes 		return 0;
78707cc728SRasmus Villemoes 	}
79707cc728SRasmus Villemoes 
80707cc728SRasmus Villemoes 	written = min(bufsize-1, elen);
81707cc728SRasmus Villemoes 	if (test_buffer[written]) {
82707cc728SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
83707cc728SRasmus Villemoes 			bufsize, fmt);
84707cc728SRasmus Villemoes 		return 1;
85707cc728SRasmus Villemoes 	}
86707cc728SRasmus Villemoes 
879a3bfa01SRasmus Villemoes 	if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
88331e4debSRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
89331e4debSRasmus Villemoes 			bufsize, fmt);
90331e4debSRasmus Villemoes 		return 1;
91331e4debSRasmus Villemoes 	}
92331e4debSRasmus Villemoes 
939a3bfa01SRasmus Villemoes 	if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
949a3bfa01SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", bufsize, fmt);
959a3bfa01SRasmus Villemoes 		return 1;
969a3bfa01SRasmus Villemoes 	}
979a3bfa01SRasmus Villemoes 
98707cc728SRasmus Villemoes 	if (memcmp(test_buffer, expect, written)) {
99707cc728SRasmus Villemoes 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
100707cc728SRasmus Villemoes 			bufsize, fmt, test_buffer, written, expect);
101707cc728SRasmus Villemoes 		return 1;
102707cc728SRasmus Villemoes 	}
103707cc728SRasmus Villemoes 	return 0;
104707cc728SRasmus Villemoes }
105707cc728SRasmus Villemoes 
106707cc728SRasmus Villemoes static void __printf(3, 4) __init
107707cc728SRasmus Villemoes __test(const char *expect, int elen, const char *fmt, ...)
108707cc728SRasmus Villemoes {
109707cc728SRasmus Villemoes 	va_list ap;
110707cc728SRasmus Villemoes 	int rand;
111707cc728SRasmus Villemoes 	char *p;
112707cc728SRasmus Villemoes 
113fd0515d5SRasmus Villemoes 	if (elen >= BUF_SIZE) {
114fd0515d5SRasmus Villemoes 		pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
115fd0515d5SRasmus Villemoes 		       elen, fmt);
116fd0515d5SRasmus Villemoes 		failed_tests++;
117fd0515d5SRasmus Villemoes 		return;
118fd0515d5SRasmus Villemoes 	}
119707cc728SRasmus Villemoes 
120707cc728SRasmus Villemoes 	va_start(ap, fmt);
121707cc728SRasmus Villemoes 
122707cc728SRasmus Villemoes 	/*
123707cc728SRasmus Villemoes 	 * Every fmt+args is subjected to four tests: Three where we
124707cc728SRasmus Villemoes 	 * tell vsnprintf varying buffer sizes (plenty, not quite
125707cc728SRasmus Villemoes 	 * enough and 0), and then we also test that kvasprintf would
126707cc728SRasmus Villemoes 	 * be able to print it as expected.
127707cc728SRasmus Villemoes 	 */
128707cc728SRasmus Villemoes 	failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
129*e8a533cbSJason A. Donenfeld 	rand = get_random_u32_inclusive(1, elen + 1);
130707cc728SRasmus Villemoes 	/* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
131707cc728SRasmus Villemoes 	failed_tests += do_test(rand, expect, elen, fmt, ap);
132707cc728SRasmus Villemoes 	failed_tests += do_test(0, expect, elen, fmt, ap);
133707cc728SRasmus Villemoes 
134707cc728SRasmus Villemoes 	p = kvasprintf(GFP_KERNEL, fmt, ap);
135707cc728SRasmus Villemoes 	if (p) {
136b79a7db3SRasmus Villemoes 		total_tests++;
137707cc728SRasmus Villemoes 		if (memcmp(p, expect, elen+1)) {
138707cc728SRasmus Villemoes 			pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
139707cc728SRasmus Villemoes 				fmt, p, expect);
140707cc728SRasmus Villemoes 			failed_tests++;
141707cc728SRasmus Villemoes 		}
142707cc728SRasmus Villemoes 		kfree(p);
143707cc728SRasmus Villemoes 	}
144707cc728SRasmus Villemoes 	va_end(ap);
145707cc728SRasmus Villemoes }
146707cc728SRasmus Villemoes 
147707cc728SRasmus Villemoes #define test(expect, fmt, ...)					\
148707cc728SRasmus Villemoes 	__test(expect, strlen(expect), fmt, ##__VA_ARGS__)
149707cc728SRasmus Villemoes 
150707cc728SRasmus Villemoes static void __init
151707cc728SRasmus Villemoes test_basic(void)
152707cc728SRasmus Villemoes {
153707cc728SRasmus Villemoes 	/* Work around annoying "warning: zero-length gnu_printf format string". */
154707cc728SRasmus Villemoes 	char nul = '\0';
155707cc728SRasmus Villemoes 
156707cc728SRasmus Villemoes 	test("", &nul);
157707cc728SRasmus Villemoes 	test("100%", "100%%");
158707cc728SRasmus Villemoes 	test("xxx%yyy", "xxx%cyyy", '%');
159707cc728SRasmus Villemoes 	__test("xxx\0yyy", 7, "xxx%cyyy", '\0');
160707cc728SRasmus Villemoes }
161707cc728SRasmus Villemoes 
162707cc728SRasmus Villemoes static void __init
163707cc728SRasmus Villemoes test_number(void)
164707cc728SRasmus Villemoes {
165707cc728SRasmus Villemoes 	test("0x1234abcd  ", "%#-12x", 0x1234abcd);
166707cc728SRasmus Villemoes 	test("  0x1234abcd", "%#12x", 0x1234abcd);
167707cc728SRasmus Villemoes 	test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
16896dd9a2fSJustin Stitt 	NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
1691ca8e8ebSRasmus Villemoes 		test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
1701ca8e8ebSRasmus Villemoes 		test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
1711ca8e8ebSRasmus Villemoes 		test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
17296dd9a2fSJustin Stitt 	})
1731ca8e8ebSRasmus Villemoes 	/*
1741ca8e8ebSRasmus Villemoes 	 * POSIX/C99: »The result of converting zero with an explicit
1751ca8e8ebSRasmus Villemoes 	 * precision of zero shall be no characters.« Hence the output
1761ca8e8ebSRasmus Villemoes 	 * from the below test should really be "00|0||| ". However,
1771ca8e8ebSRasmus Villemoes 	 * the kernel's printf also produces a single 0 in that
1781ca8e8ebSRasmus Villemoes 	 * case. This test case simply documents the current
1791ca8e8ebSRasmus Villemoes 	 * behaviour.
1801ca8e8ebSRasmus Villemoes 	 */
1811ca8e8ebSRasmus Villemoes 	test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
1821ca8e8ebSRasmus Villemoes #ifndef __CHAR_UNSIGNED__
1831ca8e8ebSRasmus Villemoes 	{
1841ca8e8ebSRasmus Villemoes 		/*
1851ca8e8ebSRasmus Villemoes 		 * Passing a 'char' to a %02x specifier doesn't do
1861ca8e8ebSRasmus Villemoes 		 * what was presumably the intention when char is
1871ca8e8ebSRasmus Villemoes 		 * signed and the value is negative. One must either &
1881ca8e8ebSRasmus Villemoes 		 * with 0xff or cast to u8.
1891ca8e8ebSRasmus Villemoes 		 */
1901ca8e8ebSRasmus Villemoes 		char val = -16;
1911ca8e8ebSRasmus Villemoes 		test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
1921ca8e8ebSRasmus Villemoes 	}
1931ca8e8ebSRasmus Villemoes #endif
194707cc728SRasmus Villemoes }
195707cc728SRasmus Villemoes 
196707cc728SRasmus Villemoes static void __init
197707cc728SRasmus Villemoes test_string(void)
198707cc728SRasmus Villemoes {
199707cc728SRasmus Villemoes 	test("", "%s%.0s", "", "123");
200707cc728SRasmus Villemoes 	test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
201707cc728SRasmus Villemoes 	test("1  |  2|3  |  4|5  ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
202f176eb4cSRasmus Villemoes 	test("1234      ", "%-10.4s", "123456");
203f176eb4cSRasmus Villemoes 	test("      1234", "%10.4s", "123456");
204707cc728SRasmus Villemoes 	/*
205f176eb4cSRasmus Villemoes 	 * POSIX and C99 say that a negative precision (which is only
206f176eb4cSRasmus Villemoes 	 * possible to pass via a * argument) should be treated as if
207f176eb4cSRasmus Villemoes 	 * the precision wasn't present, and that if the precision is
208f176eb4cSRasmus Villemoes 	 * omitted (as in %.s), the precision should be taken to be
209f176eb4cSRasmus Villemoes 	 * 0. However, the kernel's printf behave exactly opposite,
210f176eb4cSRasmus Villemoes 	 * treating a negative precision as 0 and treating an omitted
211f176eb4cSRasmus Villemoes 	 * precision specifier as if no precision was given.
212f176eb4cSRasmus Villemoes 	 *
213f176eb4cSRasmus Villemoes 	 * These test cases document the current behaviour; should
214f176eb4cSRasmus Villemoes 	 * anyone ever feel the need to follow the standards more
215f176eb4cSRasmus Villemoes 	 * closely, this can be revisited.
216707cc728SRasmus Villemoes 	 */
217f176eb4cSRasmus Villemoes 	test("    ", "%4.*s", -5, "123456");
218f176eb4cSRasmus Villemoes 	test("123456", "%.s", "123456");
219707cc728SRasmus Villemoes 	test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
220707cc728SRasmus Villemoes 	test("a  |   |   ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
221707cc728SRasmus Villemoes }
222707cc728SRasmus Villemoes 
223ad67b74dSTobin C. Harding #define PLAIN_BUF_SIZE 64	/* leave some space so we don't oops */
224ad67b74dSTobin C. Harding 
225ad67b74dSTobin C. Harding #if BITS_PER_LONG == 64
226ad67b74dSTobin C. Harding 
227ad67b74dSTobin C. Harding #define PTR_WIDTH 16
228c604b407SAndy Shevchenko #define PTR ((void *)0xffff0123456789abUL)
229ad67b74dSTobin C. Harding #define PTR_STR "ffff0123456789ab"
230ce041c43SThierry Escande #define PTR_VAL_NO_CRNG "(____ptrval____)"
231ad67b74dSTobin C. Harding #define ZEROS "00000000"	/* hex 32 zero bits */
2327bd57fbcSIlya Dryomov #define ONES "ffffffff"		/* hex 32 one bits */
233ad67b74dSTobin C. Harding 
234ad67b74dSTobin C. Harding static int __init
235ad67b74dSTobin C. Harding plain_format(void)
236ad67b74dSTobin C. Harding {
237ad67b74dSTobin C. Harding 	char buf[PLAIN_BUF_SIZE];
238ad67b74dSTobin C. Harding 	int nchars;
239ad67b74dSTobin C. Harding 
240ad67b74dSTobin C. Harding 	nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
241ad67b74dSTobin C. Harding 
242ce041c43SThierry Escande 	if (nchars != PTR_WIDTH)
243ce041c43SThierry Escande 		return -1;
244ce041c43SThierry Escande 
245ce041c43SThierry Escande 	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
246ce041c43SThierry Escande 		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
247ce041c43SThierry Escande 			PTR_VAL_NO_CRNG);
248ce041c43SThierry Escande 		return 0;
249ce041c43SThierry Escande 	}
250ce041c43SThierry Escande 
251ce041c43SThierry Escande 	if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
252ad67b74dSTobin C. Harding 		return -1;
253ad67b74dSTobin C. Harding 
254ad67b74dSTobin C. Harding 	return 0;
255ad67b74dSTobin C. Harding }
256ad67b74dSTobin C. Harding 
257ad67b74dSTobin C. Harding #else
258ad67b74dSTobin C. Harding 
259ad67b74dSTobin C. Harding #define PTR_WIDTH 8
260ad67b74dSTobin C. Harding #define PTR ((void *)0x456789ab)
261ad67b74dSTobin C. Harding #define PTR_STR "456789ab"
262ce041c43SThierry Escande #define PTR_VAL_NO_CRNG "(ptrval)"
2633e5903ebSPetr Mladek #define ZEROS ""
2647bd57fbcSIlya Dryomov #define ONES ""
265ad67b74dSTobin C. Harding 
266ad67b74dSTobin C. Harding static int __init
267ad67b74dSTobin C. Harding plain_format(void)
268ad67b74dSTobin C. Harding {
269ad67b74dSTobin C. Harding 	/* Format is implicitly tested for 32 bit machines by plain_hash() */
270ad67b74dSTobin C. Harding 	return 0;
271ad67b74dSTobin C. Harding }
272ad67b74dSTobin C. Harding 
273ad67b74dSTobin C. Harding #endif	/* BITS_PER_LONG == 64 */
274ad67b74dSTobin C. Harding 
275ad67b74dSTobin C. Harding static int __init
2764d42c447SAndy Shevchenko plain_hash_to_buffer(const void *p, char *buf, size_t len)
277ad67b74dSTobin C. Harding {
278ad67b74dSTobin C. Harding 	int nchars;
279ad67b74dSTobin C. Harding 
2804d42c447SAndy Shevchenko 	nchars = snprintf(buf, len, "%p", p);
281ad67b74dSTobin C. Harding 
282ce041c43SThierry Escande 	if (nchars != PTR_WIDTH)
283ce041c43SThierry Escande 		return -1;
284ce041c43SThierry Escande 
285ce041c43SThierry Escande 	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
286ce041c43SThierry Escande 		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
287ce041c43SThierry Escande 			PTR_VAL_NO_CRNG);
288ce041c43SThierry Escande 		return 0;
289ce041c43SThierry Escande 	}
290ce041c43SThierry Escande 
2914d42c447SAndy Shevchenko 	return 0;
2924d42c447SAndy Shevchenko }
2934d42c447SAndy Shevchenko 
2944d42c447SAndy Shevchenko static int __init
2954d42c447SAndy Shevchenko plain_hash(void)
2964d42c447SAndy Shevchenko {
2974d42c447SAndy Shevchenko 	char buf[PLAIN_BUF_SIZE];
2984d42c447SAndy Shevchenko 	int ret;
2994d42c447SAndy Shevchenko 
3004d42c447SAndy Shevchenko 	ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
3014d42c447SAndy Shevchenko 	if (ret)
3024d42c447SAndy Shevchenko 		return ret;
3034d42c447SAndy Shevchenko 
304ce041c43SThierry Escande 	if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
305ad67b74dSTobin C. Harding 		return -1;
306ad67b74dSTobin C. Harding 
307ad67b74dSTobin C. Harding 	return 0;
308ad67b74dSTobin C. Harding }
309ad67b74dSTobin C. Harding 
310ad67b74dSTobin C. Harding /*
311ad67b74dSTobin C. Harding  * We can't use test() to test %p because we don't know what output to expect
312ad67b74dSTobin C. Harding  * after an address is hashed.
313ad67b74dSTobin C. Harding  */
314707cc728SRasmus Villemoes static void __init
315707cc728SRasmus Villemoes plain(void)
316707cc728SRasmus Villemoes {
317ad67b74dSTobin C. Harding 	int err;
318707cc728SRasmus Villemoes 
3195ead723aSTimur Tabi 	if (no_hash_pointers) {
3205ead723aSTimur Tabi 		pr_warn("skipping plain 'p' tests");
3215ead723aSTimur Tabi 		skipped_tests += 2;
3225ead723aSTimur Tabi 		return;
3235ead723aSTimur Tabi 	}
3245ead723aSTimur Tabi 
325ad67b74dSTobin C. Harding 	err = plain_hash();
326ad67b74dSTobin C. Harding 	if (err) {
327ad67b74dSTobin C. Harding 		pr_warn("plain 'p' does not appear to be hashed\n");
328ad67b74dSTobin C. Harding 		failed_tests++;
329ad67b74dSTobin C. Harding 		return;
330ad67b74dSTobin C. Harding 	}
331ad67b74dSTobin C. Harding 
332ad67b74dSTobin C. Harding 	err = plain_format();
333ad67b74dSTobin C. Harding 	if (err) {
334ad67b74dSTobin C. Harding 		pr_warn("hashing plain 'p' has unexpected format\n");
335ad67b74dSTobin C. Harding 		failed_tests++;
336ad67b74dSTobin C. Harding 	}
337707cc728SRasmus Villemoes }
338707cc728SRasmus Villemoes 
339707cc728SRasmus Villemoes static void __init
3404d42c447SAndy Shevchenko test_hashed(const char *fmt, const void *p)
3414d42c447SAndy Shevchenko {
3424d42c447SAndy Shevchenko 	char buf[PLAIN_BUF_SIZE];
3434d42c447SAndy Shevchenko 	int ret;
3444d42c447SAndy Shevchenko 
3454d42c447SAndy Shevchenko 	/*
3464d42c447SAndy Shevchenko 	 * No need to increase failed test counter since this is assumed
3474d42c447SAndy Shevchenko 	 * to be called after plain().
3484d42c447SAndy Shevchenko 	 */
3494d42c447SAndy Shevchenko 	ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
3504d42c447SAndy Shevchenko 	if (ret)
3514d42c447SAndy Shevchenko 		return;
3524d42c447SAndy Shevchenko 
3534d42c447SAndy Shevchenko 	test(buf, fmt, p);
3544d42c447SAndy Shevchenko }
3554d42c447SAndy Shevchenko 
3567bd57fbcSIlya Dryomov /*
3577bd57fbcSIlya Dryomov  * NULL pointers aren't hashed.
3587bd57fbcSIlya Dryomov  */
3594d42c447SAndy Shevchenko static void __init
3603e5903ebSPetr Mladek null_pointer(void)
3613e5903ebSPetr Mladek {
3627bd57fbcSIlya Dryomov 	test(ZEROS "00000000", "%p", NULL);
3633e5903ebSPetr Mladek 	test(ZEROS "00000000", "%px", NULL);
3643e5903ebSPetr Mladek 	test("(null)", "%pE", NULL);
3653e5903ebSPetr Mladek }
3663e5903ebSPetr Mladek 
3677bd57fbcSIlya Dryomov /*
3687bd57fbcSIlya Dryomov  * Error pointers aren't hashed.
3697bd57fbcSIlya Dryomov  */
3707bd57fbcSIlya Dryomov static void __init
3717bd57fbcSIlya Dryomov error_pointer(void)
3727bd57fbcSIlya Dryomov {
3737bd57fbcSIlya Dryomov 	test(ONES "fffffff5", "%p", ERR_PTR(-11));
3747bd57fbcSIlya Dryomov 	test(ONES "fffffff5", "%px", ERR_PTR(-11));
3757bd57fbcSIlya Dryomov 	test("(efault)", "%pE", ERR_PTR(-11));
3767bd57fbcSIlya Dryomov }
3777bd57fbcSIlya Dryomov 
3783e5903ebSPetr Mladek #define PTR_INVALID ((void *)0x000000ab)
3793e5903ebSPetr Mladek 
3803e5903ebSPetr Mladek static void __init
3813e5903ebSPetr Mladek invalid_pointer(void)
3823e5903ebSPetr Mladek {
3833e5903ebSPetr Mladek 	test_hashed("%p", PTR_INVALID);
3843e5903ebSPetr Mladek 	test(ZEROS "000000ab", "%px", PTR_INVALID);
3853e5903ebSPetr Mladek 	test("(efault)", "%pE", PTR_INVALID);
3863e5903ebSPetr Mladek }
3873e5903ebSPetr Mladek 
3883e5903ebSPetr Mladek static void __init
389707cc728SRasmus Villemoes symbol_ptr(void)
390707cc728SRasmus Villemoes {
391707cc728SRasmus Villemoes }
392707cc728SRasmus Villemoes 
393707cc728SRasmus Villemoes static void __init
394707cc728SRasmus Villemoes kernel_ptr(void)
395707cc728SRasmus Villemoes {
396ad67b74dSTobin C. Harding 	/* We can't test this without access to kptr_restrict. */
397707cc728SRasmus Villemoes }
398707cc728SRasmus Villemoes 
399707cc728SRasmus Villemoes static void __init
400707cc728SRasmus Villemoes struct_resource(void)
401707cc728SRasmus Villemoes {
402707cc728SRasmus Villemoes }
403707cc728SRasmus Villemoes 
404707cc728SRasmus Villemoes static void __init
405707cc728SRasmus Villemoes addr(void)
406707cc728SRasmus Villemoes {
407707cc728SRasmus Villemoes }
408707cc728SRasmus Villemoes 
409707cc728SRasmus Villemoes static void __init
410707cc728SRasmus Villemoes escaped_str(void)
411707cc728SRasmus Villemoes {
412707cc728SRasmus Villemoes }
413707cc728SRasmus Villemoes 
414707cc728SRasmus Villemoes static void __init
415707cc728SRasmus Villemoes hex_string(void)
416707cc728SRasmus Villemoes {
417707cc728SRasmus Villemoes 	const char buf[3] = {0xc0, 0xff, 0xee};
418707cc728SRasmus Villemoes 
419707cc728SRasmus Villemoes 	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
420707cc728SRasmus Villemoes 	     "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
421707cc728SRasmus Villemoes 	test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
422707cc728SRasmus Villemoes 	     "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
423707cc728SRasmus Villemoes }
424707cc728SRasmus Villemoes 
425707cc728SRasmus Villemoes static void __init
426707cc728SRasmus Villemoes mac(void)
427707cc728SRasmus Villemoes {
428707cc728SRasmus Villemoes 	const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
429707cc728SRasmus Villemoes 
430707cc728SRasmus Villemoes 	test("2d:48:d6:fc:7a:05", "%pM", addr);
431707cc728SRasmus Villemoes 	test("05:7a:fc:d6:48:2d", "%pMR", addr);
432707cc728SRasmus Villemoes 	test("2d-48-d6-fc-7a-05", "%pMF", addr);
433707cc728SRasmus Villemoes 	test("2d48d6fc7a05", "%pm", addr);
434707cc728SRasmus Villemoes 	test("057afcd6482d", "%pmR", addr);
435707cc728SRasmus Villemoes }
436707cc728SRasmus Villemoes 
437707cc728SRasmus Villemoes static void __init
438707cc728SRasmus Villemoes ip4(void)
439707cc728SRasmus Villemoes {
440707cc728SRasmus Villemoes 	struct sockaddr_in sa;
441707cc728SRasmus Villemoes 
442707cc728SRasmus Villemoes 	sa.sin_family = AF_INET;
443707cc728SRasmus Villemoes 	sa.sin_port = cpu_to_be16(12345);
444707cc728SRasmus Villemoes 	sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
445707cc728SRasmus Villemoes 
446707cc728SRasmus Villemoes 	test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
447707cc728SRasmus Villemoes 	test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
448707cc728SRasmus Villemoes 	sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
449707cc728SRasmus Villemoes 	test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
450707cc728SRasmus Villemoes }
451707cc728SRasmus Villemoes 
452707cc728SRasmus Villemoes static void __init
453707cc728SRasmus Villemoes ip6(void)
454707cc728SRasmus Villemoes {
455707cc728SRasmus Villemoes }
456707cc728SRasmus Villemoes 
457707cc728SRasmus Villemoes static void __init
458707cc728SRasmus Villemoes ip(void)
459707cc728SRasmus Villemoes {
460707cc728SRasmus Villemoes 	ip4();
461707cc728SRasmus Villemoes 	ip6();
462707cc728SRasmus Villemoes }
463707cc728SRasmus Villemoes 
464707cc728SRasmus Villemoes static void __init
465707cc728SRasmus Villemoes uuid(void)
466707cc728SRasmus Villemoes {
467707cc728SRasmus Villemoes 	const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
468707cc728SRasmus Villemoes 			       0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
469707cc728SRasmus Villemoes 
470707cc728SRasmus Villemoes 	test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
471707cc728SRasmus Villemoes 	test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
472707cc728SRasmus Villemoes 	test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
473707cc728SRasmus Villemoes 	test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
474707cc728SRasmus Villemoes }
475707cc728SRasmus Villemoes 
476251c7234SRasmus Villemoes static struct dentry test_dentry[4] __initdata = {
477251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[0],
478251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
479251c7234SRasmus Villemoes 	  .d_iname = "foo" },
480251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[0],
481251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
482251c7234SRasmus Villemoes 	  .d_iname = "bravo" },
483251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[1],
484251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
485251c7234SRasmus Villemoes 	  .d_iname = "alfa" },
486251c7234SRasmus Villemoes 	{ .d_parent = &test_dentry[2],
487251c7234SRasmus Villemoes 	  .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
488251c7234SRasmus Villemoes 	  .d_iname = "romeo" },
489251c7234SRasmus Villemoes };
490251c7234SRasmus Villemoes 
491707cc728SRasmus Villemoes static void __init
492707cc728SRasmus Villemoes dentry(void)
493707cc728SRasmus Villemoes {
494251c7234SRasmus Villemoes 	test("foo", "%pd", &test_dentry[0]);
495251c7234SRasmus Villemoes 	test("foo", "%pd2", &test_dentry[0]);
496251c7234SRasmus Villemoes 
497cf6b7921SJia He 	test("(null)", "%pd", NULL);
498cf6b7921SJia He 	test("(efault)", "%pd", PTR_INVALID);
499cf6b7921SJia He 	test("(null)", "%pD", NULL);
500cf6b7921SJia He 	test("(efault)", "%pD", PTR_INVALID);
501cf6b7921SJia He 
502251c7234SRasmus Villemoes 	test("romeo", "%pd", &test_dentry[3]);
503251c7234SRasmus Villemoes 	test("alfa/romeo", "%pd2", &test_dentry[3]);
504251c7234SRasmus Villemoes 	test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
505251c7234SRasmus Villemoes 	test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
506251c7234SRasmus Villemoes 	test("/bravo/alfa", "%pd4", &test_dentry[2]);
507251c7234SRasmus Villemoes 
508251c7234SRasmus Villemoes 	test("bravo/alfa  |bravo/alfa  ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
509251c7234SRasmus Villemoes 	test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
510707cc728SRasmus Villemoes }
511707cc728SRasmus Villemoes 
512707cc728SRasmus Villemoes static void __init
513707cc728SRasmus Villemoes struct_va_format(void)
514707cc728SRasmus Villemoes {
515707cc728SRasmus Villemoes }
516707cc728SRasmus Villemoes 
517707cc728SRasmus Villemoes static void __init
5187daac5b2SAndy Shevchenko time_and_date(void)
5194d42c447SAndy Shevchenko {
5204d42c447SAndy Shevchenko 	/* 1543210543 */
5214d42c447SAndy Shevchenko 	const struct rtc_time tm = {
5224d42c447SAndy Shevchenko 		.tm_sec = 43,
5234d42c447SAndy Shevchenko 		.tm_min = 35,
5244d42c447SAndy Shevchenko 		.tm_hour = 5,
5254d42c447SAndy Shevchenko 		.tm_mday = 26,
5264d42c447SAndy Shevchenko 		.tm_mon = 10,
5274d42c447SAndy Shevchenko 		.tm_year = 118,
5284d42c447SAndy Shevchenko 	};
5297daac5b2SAndy Shevchenko 	/* 2019-01-04T15:32:23 */
5307daac5b2SAndy Shevchenko 	time64_t t = 1546615943;
5314d42c447SAndy Shevchenko 
5327daac5b2SAndy Shevchenko 	test("(%pt?)", "%pt", &tm);
5334d42c447SAndy Shevchenko 	test("2018-11-26T05:35:43", "%ptR", &tm);
5344d42c447SAndy Shevchenko 	test("0118-10-26T05:35:43", "%ptRr", &tm);
5354d42c447SAndy Shevchenko 	test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
5364d42c447SAndy Shevchenko 	test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
5374d42c447SAndy Shevchenko 	test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
5384d42c447SAndy Shevchenko 	test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
5397daac5b2SAndy Shevchenko 
5407daac5b2SAndy Shevchenko 	test("2019-01-04T15:32:23", "%ptT", &t);
5417daac5b2SAndy Shevchenko 	test("0119-00-04T15:32:23", "%ptTr", &t);
5427daac5b2SAndy Shevchenko 	test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
5437daac5b2SAndy Shevchenko 	test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
54420bc8c1eSAndy Shevchenko 
54520bc8c1eSAndy Shevchenko 	test("2019-01-04 15:32:23", "%ptTs", &t);
54620bc8c1eSAndy Shevchenko 	test("0119-00-04 15:32:23", "%ptTsr", &t);
54720bc8c1eSAndy Shevchenko 	test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t);
54820bc8c1eSAndy Shevchenko 	test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t);
5494d42c447SAndy Shevchenko }
5504d42c447SAndy Shevchenko 
5514d42c447SAndy Shevchenko static void __init
552707cc728SRasmus Villemoes struct_clk(void)
553707cc728SRasmus Villemoes {
554707cc728SRasmus Villemoes }
555707cc728SRasmus Villemoes 
556707cc728SRasmus Villemoes static void __init
557857cca4dSRasmus Villemoes large_bitmap(void)
558857cca4dSRasmus Villemoes {
559857cca4dSRasmus Villemoes 	const int nbits = 1 << 16;
5602821fd0cSAndy Shevchenko 	unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
561857cca4dSRasmus Villemoes 	if (!bits)
562857cca4dSRasmus Villemoes 		return;
563857cca4dSRasmus Villemoes 
564857cca4dSRasmus Villemoes 	bitmap_set(bits, 1, 20);
565857cca4dSRasmus Villemoes 	bitmap_set(bits, 60000, 15);
566857cca4dSRasmus Villemoes 	test("1-20,60000-60014", "%*pbl", nbits, bits);
5672821fd0cSAndy Shevchenko 	bitmap_free(bits);
568857cca4dSRasmus Villemoes }
569857cca4dSRasmus Villemoes 
570857cca4dSRasmus Villemoes static void __init
571707cc728SRasmus Villemoes bitmap(void)
572707cc728SRasmus Villemoes {
573707cc728SRasmus Villemoes 	DECLARE_BITMAP(bits, 20);
574707cc728SRasmus Villemoes 	const int primes[] = {2,3,5,7,11,13,17,19};
575707cc728SRasmus Villemoes 	int i;
576707cc728SRasmus Villemoes 
577707cc728SRasmus Villemoes 	bitmap_zero(bits, 20);
578707cc728SRasmus Villemoes 	test("00000|00000", "%20pb|%*pb", bits, 20, bits);
579707cc728SRasmus Villemoes 	test("|", "%20pbl|%*pbl", bits, 20, bits);
580707cc728SRasmus Villemoes 
581707cc728SRasmus Villemoes 	for (i = 0; i < ARRAY_SIZE(primes); ++i)
582707cc728SRasmus Villemoes 		set_bit(primes[i], bits);
583707cc728SRasmus Villemoes 	test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
584707cc728SRasmus Villemoes 	test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
585707cc728SRasmus Villemoes 
586707cc728SRasmus Villemoes 	bitmap_fill(bits, 20);
587707cc728SRasmus Villemoes 	test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
588707cc728SRasmus Villemoes 	test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
589857cca4dSRasmus Villemoes 
590857cca4dSRasmus Villemoes 	large_bitmap();
591707cc728SRasmus Villemoes }
592707cc728SRasmus Villemoes 
593707cc728SRasmus Villemoes static void __init
594707cc728SRasmus Villemoes netdev_features(void)
595707cc728SRasmus Villemoes {
596707cc728SRasmus Villemoes }
597707cc728SRasmus Villemoes 
598c244297aSYafang Shao struct page_flags_test {
599c244297aSYafang Shao 	int width;
600c244297aSYafang Shao 	int shift;
601c244297aSYafang Shao 	int mask;
602c244297aSYafang Shao 	const char *fmt;
603c244297aSYafang Shao 	const char *name;
604c244297aSYafang Shao };
605c244297aSYafang Shao 
606c666d447SMatthew Wilcox (Oracle) static const struct page_flags_test pft[] = {
607c244297aSYafang Shao 	{SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK,
608c666d447SMatthew Wilcox (Oracle) 	 "%d", "section"},
609c244297aSYafang Shao 	{NODES_WIDTH, NODES_PGSHIFT, NODES_MASK,
610c666d447SMatthew Wilcox (Oracle) 	 "%d", "node"},
611c244297aSYafang Shao 	{ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK,
612c666d447SMatthew Wilcox (Oracle) 	 "%d", "zone"},
613c244297aSYafang Shao 	{LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK,
614c666d447SMatthew Wilcox (Oracle) 	 "%#x", "lastcpupid"},
615c244297aSYafang Shao 	{KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK,
616c666d447SMatthew Wilcox (Oracle) 	 "%#x", "kasantag"},
617c244297aSYafang Shao };
618c244297aSYafang Shao 
619c244297aSYafang Shao static void __init
620c244297aSYafang Shao page_flags_test(int section, int node, int zone, int last_cpupid,
621a25a0854SMatthew Wilcox (Oracle) 		int kasan_tag, unsigned long flags, const char *name,
622a25a0854SMatthew Wilcox (Oracle) 		char *cmp_buf)
623c244297aSYafang Shao {
624c244297aSYafang Shao 	unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag};
62523efd080SMatthew Wilcox (Oracle) 	unsigned long size;
626c244297aSYafang Shao 	bool append = false;
627c244297aSYafang Shao 	int i;
628c244297aSYafang Shao 
62923efd080SMatthew Wilcox (Oracle) 	for (i = 0; i < ARRAY_SIZE(values); i++)
63023efd080SMatthew Wilcox (Oracle) 		flags |= (values[i] & pft[i].mask) << pft[i].shift;
63123efd080SMatthew Wilcox (Oracle) 
63223efd080SMatthew Wilcox (Oracle) 	size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags);
633a25a0854SMatthew Wilcox (Oracle) 	if (flags & PAGEFLAGS_MASK) {
634507f9860SMatthew Wilcox (Oracle) 		size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
6355b358b0dSMatthew Wilcox (Oracle) 		append = true;
636c244297aSYafang Shao 	}
637c244297aSYafang Shao 
638c244297aSYafang Shao 	for (i = 0; i < ARRAY_SIZE(pft); i++) {
639c244297aSYafang Shao 		if (!pft[i].width)
640c244297aSYafang Shao 			continue;
641c244297aSYafang Shao 
642507f9860SMatthew Wilcox (Oracle) 		if (append)
643507f9860SMatthew Wilcox (Oracle) 			size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");
644c244297aSYafang Shao 
645507f9860SMatthew Wilcox (Oracle) 		size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
646507f9860SMatthew Wilcox (Oracle) 				pft[i].name);
647507f9860SMatthew Wilcox (Oracle) 		size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
648c666d447SMatthew Wilcox (Oracle) 				values[i] & pft[i].mask);
649c244297aSYafang Shao 		append = true;
650c244297aSYafang Shao 	}
651c244297aSYafang Shao 
65223efd080SMatthew Wilcox (Oracle) 	snprintf(cmp_buf + size, BUF_SIZE - size, ")");
65323efd080SMatthew Wilcox (Oracle) 
654a25a0854SMatthew Wilcox (Oracle) 	test(cmp_buf, "%pGp", &flags);
655c244297aSYafang Shao }
656c244297aSYafang Shao 
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;
663c244297aSYafang Shao 
664c244297aSYafang Shao 	cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
665c244297aSYafang Shao 	if (!cmp_buffer)
666c244297aSYafang Shao 		return;
667edf14cdbSVlastimil Babka 
668edf14cdbSVlastimil Babka 	flags = 0;
669c244297aSYafang Shao 	page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer);
670edf14cdbSVlastimil Babka 
671edf14cdbSVlastimil Babka 	flags = 1UL << NR_PAGEFLAGS;
672c244297aSYafang Shao 	page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer);
673edf14cdbSVlastimil Babka 
674edf14cdbSVlastimil Babka 	flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
675edf14cdbSVlastimil Babka 		| 1UL << PG_active | 1UL << PG_swapbacked;
676c244297aSYafang Shao 	page_flags_test(1, 1, 1, 0x1fffff, 1, flags,
677c244297aSYafang Shao 			"uptodate|dirty|lru|active|swapbacked",
678c244297aSYafang Shao 			cmp_buffer);
679edf14cdbSVlastimil Babka 
6808d0920bdSDavid Hildenbrand 	flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
6818d0920bdSDavid Hildenbrand 	test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags);
682edf14cdbSVlastimil Babka 
683edf14cdbSVlastimil Babka 	gfp = GFP_TRANSHUGE;
684edf14cdbSVlastimil Babka 	test("GFP_TRANSHUGE", "%pGg", &gfp);
685edf14cdbSVlastimil Babka 
686edf14cdbSVlastimil Babka 	gfp = GFP_ATOMIC|__GFP_DMA;
687edf14cdbSVlastimil Babka 	test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
688edf14cdbSVlastimil Babka 
689edf14cdbSVlastimil Babka 	gfp = __GFP_ATOMIC;
690edf14cdbSVlastimil Babka 	test("__GFP_ATOMIC", "%pGg", &gfp);
691edf14cdbSVlastimil Babka 
692edf14cdbSVlastimil Babka 	/* Any flags not translated by the table should remain numeric */
693edf14cdbSVlastimil Babka 	gfp = ~__GFP_BITS_MASK;
694edf14cdbSVlastimil Babka 	snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
695edf14cdbSVlastimil Babka 	test(cmp_buffer, "%pGg", &gfp);
696edf14cdbSVlastimil Babka 
697edf14cdbSVlastimil Babka 	snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
698edf14cdbSVlastimil Babka 							(unsigned long) gfp);
699edf14cdbSVlastimil Babka 	gfp |= __GFP_ATOMIC;
700edf14cdbSVlastimil Babka 	test(cmp_buffer, "%pGg", &gfp);
701edf14cdbSVlastimil Babka 
702edf14cdbSVlastimil Babka 	kfree(cmp_buffer);
703edf14cdbSVlastimil Babka }
704edf14cdbSVlastimil Babka 
705f1ce39dfSSakari Ailus static void __init fwnode_pointer(void)
706f1ce39dfSSakari Ailus {
707f1ce39dfSSakari Ailus 	const struct software_node softnodes[] = {
708f1ce39dfSSakari Ailus 		{ .name = "first", },
709f1ce39dfSSakari Ailus 		{ .name = "second", .parent = &softnodes[0], },
710f1ce39dfSSakari Ailus 		{ .name = "third", .parent = &softnodes[1], },
711f1ce39dfSSakari Ailus 		{ NULL /* Guardian */ }
712f1ce39dfSSakari Ailus 	};
713f1ce39dfSSakari Ailus 	const char * const full_name = "first/second/third";
714f1ce39dfSSakari Ailus 	const char * const full_name_second = "first/second";
715f1ce39dfSSakari Ailus 	const char * const second_name = "second";
716f1ce39dfSSakari Ailus 	const char * const third_name = "third";
717f1ce39dfSSakari Ailus 	int rval;
718f1ce39dfSSakari Ailus 
719f1ce39dfSSakari Ailus 	rval = software_node_register_nodes(softnodes);
720f1ce39dfSSakari Ailus 	if (rval) {
721f1ce39dfSSakari Ailus 		pr_warn("cannot register softnodes; rval %d\n", rval);
722f1ce39dfSSakari Ailus 		return;
723f1ce39dfSSakari Ailus 	}
724f1ce39dfSSakari Ailus 
725f1ce39dfSSakari Ailus 	test(full_name_second, "%pfw", software_node_fwnode(&softnodes[1]));
726f1ce39dfSSakari Ailus 	test(full_name, "%pfw", software_node_fwnode(&softnodes[2]));
727f1ce39dfSSakari Ailus 	test(full_name, "%pfwf", software_node_fwnode(&softnodes[2]));
728f1ce39dfSSakari Ailus 	test(second_name, "%pfwP", software_node_fwnode(&softnodes[1]));
729f1ce39dfSSakari Ailus 	test(third_name, "%pfwP", software_node_fwnode(&softnodes[2]));
730f1ce39dfSSakari Ailus 
731f0328be5SDaniel Scally 	software_node_unregister_nodes(softnodes);
732f1ce39dfSSakari Ailus }
733f1ce39dfSSakari Ailus 
734af612e43SSakari Ailus static void __init fourcc_pointer(void)
735af612e43SSakari Ailus {
736af612e43SSakari Ailus 	struct {
737af612e43SSakari Ailus 		u32 code;
738af612e43SSakari Ailus 		char *str;
739af612e43SSakari Ailus 	} const try[] = {
740af612e43SSakari Ailus 		{ 0x3231564e, "NV12 little-endian (0x3231564e)", },
741af612e43SSakari Ailus 		{ 0xb231564e, "NV12 big-endian (0xb231564e)", },
742af612e43SSakari Ailus 		{ 0x10111213, ".... little-endian (0x10111213)", },
743af612e43SSakari Ailus 		{ 0x20303159, "Y10  little-endian (0x20303159)", },
744af612e43SSakari Ailus 	};
745af612e43SSakari Ailus 	unsigned int i;
746af612e43SSakari Ailus 
747af612e43SSakari Ailus 	for (i = 0; i < ARRAY_SIZE(try); i++)
748af612e43SSakari Ailus 		test(try[i].str, "%p4cc", &try[i].code);
749af612e43SSakari Ailus }
750af612e43SSakari Ailus 
751edf14cdbSVlastimil Babka static void __init
75257f5677eSRasmus Villemoes errptr(void)
75357f5677eSRasmus Villemoes {
75457f5677eSRasmus Villemoes 	test("-1234", "%pe", ERR_PTR(-1234));
75557f5677eSRasmus Villemoes 
75657f5677eSRasmus Villemoes 	/* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
75757f5677eSRasmus Villemoes 	BUILD_BUG_ON(IS_ERR(PTR));
75857f5677eSRasmus Villemoes 	test_hashed("%pe", PTR);
75957f5677eSRasmus Villemoes 
76057f5677eSRasmus Villemoes #ifdef CONFIG_SYMBOLIC_ERRNAME
76157f5677eSRasmus Villemoes 	test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
76257f5677eSRasmus Villemoes 	test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
76357f5677eSRasmus Villemoes 	BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
76457f5677eSRasmus Villemoes 	test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
76557f5677eSRasmus Villemoes 	test("[-EIO    ]", "[%-8pe]", ERR_PTR(-EIO));
76657f5677eSRasmus Villemoes 	test("[    -EIO]", "[%8pe]", ERR_PTR(-EIO));
76757f5677eSRasmus Villemoes 	test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
76857f5677eSRasmus Villemoes #endif
76957f5677eSRasmus Villemoes }
77057f5677eSRasmus Villemoes 
77157f5677eSRasmus Villemoes static void __init
772707cc728SRasmus Villemoes test_pointer(void)
773707cc728SRasmus Villemoes {
774707cc728SRasmus Villemoes 	plain();
7753e5903ebSPetr Mladek 	null_pointer();
7767bd57fbcSIlya Dryomov 	error_pointer();
7773e5903ebSPetr Mladek 	invalid_pointer();
778707cc728SRasmus Villemoes 	symbol_ptr();
779707cc728SRasmus Villemoes 	kernel_ptr();
780707cc728SRasmus Villemoes 	struct_resource();
781707cc728SRasmus Villemoes 	addr();
782707cc728SRasmus Villemoes 	escaped_str();
783707cc728SRasmus Villemoes 	hex_string();
784707cc728SRasmus Villemoes 	mac();
785707cc728SRasmus Villemoes 	ip();
786707cc728SRasmus Villemoes 	uuid();
787707cc728SRasmus Villemoes 	dentry();
788707cc728SRasmus Villemoes 	struct_va_format();
7897daac5b2SAndy Shevchenko 	time_and_date();
790707cc728SRasmus Villemoes 	struct_clk();
791707cc728SRasmus Villemoes 	bitmap();
792707cc728SRasmus Villemoes 	netdev_features();
793edf14cdbSVlastimil Babka 	flags();
79457f5677eSRasmus Villemoes 	errptr();
795f1ce39dfSSakari Ailus 	fwnode_pointer();
796af612e43SSakari Ailus 	fourcc_pointer();
797707cc728SRasmus Villemoes }
798707cc728SRasmus Villemoes 
7996b1a4d5bSTobin C. Harding static void __init selftest(void)
800707cc728SRasmus Villemoes {
801331e4debSRasmus Villemoes 	alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
802331e4debSRasmus Villemoes 	if (!alloced_buffer)
8036b1a4d5bSTobin C. Harding 		return;
804331e4debSRasmus Villemoes 	test_buffer = alloced_buffer + PAD_SIZE;
805707cc728SRasmus Villemoes 
806707cc728SRasmus Villemoes 	test_basic();
807707cc728SRasmus Villemoes 	test_number();
808707cc728SRasmus Villemoes 	test_string();
809707cc728SRasmus Villemoes 	test_pointer();
810707cc728SRasmus Villemoes 
811331e4debSRasmus Villemoes 	kfree(alloced_buffer);
812707cc728SRasmus Villemoes }
813707cc728SRasmus Villemoes 
8146b1a4d5bSTobin C. Harding KSTM_MODULE_LOADERS(test_printf);
815707cc728SRasmus Villemoes MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
816707cc728SRasmus Villemoes MODULE_LICENSE("GPL");
817