test_printf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test cases for printf facility.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include <linux/random.h>
  11. #include <linux/rtc.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/dcache.h>
  16. #include <linux/socket.h>
  17. #include <linux/in.h>
  18. #include <linux/gfp.h>
  19. #include <linux/mm.h>
  20. #include <linux/property.h>
  21. #include "../tools/testing/selftests/kselftest_module.h"
  22. #define BUF_SIZE 256
  23. #define PAD_SIZE 16
  24. #define FILL_CHAR '$'
  25. #define NOWARN(option, comment, block) \
  26. __diag_push(); \
  27. __diag_ignore_all(#option, comment); \
  28. block \
  29. __diag_pop();
  30. KSTM_MODULE_GLOBALS();
  31. static char *test_buffer __initdata;
  32. static char *alloced_buffer __initdata;
  33. extern bool no_hash_pointers;
  34. static int __printf(4, 0) __init
  35. do_test(int bufsize, const char *expect, int elen,
  36. const char *fmt, va_list ap)
  37. {
  38. va_list aq;
  39. int ret, written;
  40. total_tests++;
  41. memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
  42. va_copy(aq, ap);
  43. ret = vsnprintf(test_buffer, bufsize, fmt, aq);
  44. va_end(aq);
  45. if (ret != elen) {
  46. pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
  47. bufsize, fmt, ret, elen);
  48. return 1;
  49. }
  50. if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
  51. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
  52. return 1;
  53. }
  54. if (!bufsize) {
  55. if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
  56. pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
  57. fmt);
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. written = min(bufsize-1, elen);
  63. if (test_buffer[written]) {
  64. pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
  65. bufsize, fmt);
  66. return 1;
  67. }
  68. if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
  69. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
  70. bufsize, fmt);
  71. return 1;
  72. }
  73. if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
  74. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", bufsize, fmt);
  75. return 1;
  76. }
  77. if (memcmp(test_buffer, expect, written)) {
  78. pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
  79. bufsize, fmt, test_buffer, written, expect);
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static void __printf(3, 4) __init
  85. __test(const char *expect, int elen, const char *fmt, ...)
  86. {
  87. va_list ap;
  88. int rand;
  89. char *p;
  90. if (elen >= BUF_SIZE) {
  91. pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
  92. elen, fmt);
  93. failed_tests++;
  94. return;
  95. }
  96. va_start(ap, fmt);
  97. /*
  98. * Every fmt+args is subjected to four tests: Three where we
  99. * tell vsnprintf varying buffer sizes (plenty, not quite
  100. * enough and 0), and then we also test that kvasprintf would
  101. * be able to print it as expected.
  102. */
  103. failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
  104. rand = 1 + prandom_u32_max(elen+1);
  105. /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
  106. failed_tests += do_test(rand, expect, elen, fmt, ap);
  107. failed_tests += do_test(0, expect, elen, fmt, ap);
  108. p = kvasprintf(GFP_KERNEL, fmt, ap);
  109. if (p) {
  110. total_tests++;
  111. if (memcmp(p, expect, elen+1)) {
  112. pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
  113. fmt, p, expect);
  114. failed_tests++;
  115. }
  116. kfree(p);
  117. }
  118. va_end(ap);
  119. }
  120. #define test(expect, fmt, ...) \
  121. __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
  122. static void __init
  123. test_basic(void)
  124. {
  125. /* Work around annoying "warning: zero-length gnu_printf format string". */
  126. char nul = '\0';
  127. test("", &nul);
  128. test("100%", "100%%");
  129. test("xxx%yyy", "xxx%cyyy", '%');
  130. __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
  131. }
  132. static void __init
  133. test_number(void)
  134. {
  135. test("0x1234abcd ", "%#-12x", 0x1234abcd);
  136. test(" 0x1234abcd", "%#12x", 0x1234abcd);
  137. test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
  138. NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
  139. test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
  140. test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
  141. test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
  142. })
  143. /*
  144. * POSIX/C99: »The result of converting zero with an explicit
  145. * precision of zero shall be no characters.« Hence the output
  146. * from the below test should really be "00|0||| ". However,
  147. * the kernel's printf also produces a single 0 in that
  148. * case. This test case simply documents the current
  149. * behaviour.
  150. */
  151. test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
  152. #ifndef __CHAR_UNSIGNED__
  153. {
  154. /*
  155. * Passing a 'char' to a %02x specifier doesn't do
  156. * what was presumably the intention when char is
  157. * signed and the value is negative. One must either &
  158. * with 0xff or cast to u8.
  159. */
  160. char val = -16;
  161. test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
  162. }
  163. #endif
  164. }
  165. static void __init
  166. test_string(void)
  167. {
  168. test("", "%s%.0s", "", "123");
  169. test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
  170. test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
  171. test("1234 ", "%-10.4s", "123456");
  172. test(" 1234", "%10.4s", "123456");
  173. /*
  174. * POSIX and C99 say that a negative precision (which is only
  175. * possible to pass via a * argument) should be treated as if
  176. * the precision wasn't present, and that if the precision is
  177. * omitted (as in %.s), the precision should be taken to be
  178. * 0. However, the kernel's printf behave exactly opposite,
  179. * treating a negative precision as 0 and treating an omitted
  180. * precision specifier as if no precision was given.
  181. *
  182. * These test cases document the current behaviour; should
  183. * anyone ever feel the need to follow the standards more
  184. * closely, this can be revisited.
  185. */
  186. test(" ", "%4.*s", -5, "123456");
  187. test("123456", "%.s", "123456");
  188. test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
  189. test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
  190. }
  191. #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
  192. #if BITS_PER_LONG == 64
  193. #define PTR_WIDTH 16
  194. #define PTR ((void *)0xffff0123456789abUL)
  195. #define PTR_STR "ffff0123456789ab"
  196. #define PTR_VAL_NO_CRNG "(____ptrval____)"
  197. #define ZEROS "00000000" /* hex 32 zero bits */
  198. #define ONES "ffffffff" /* hex 32 one bits */
  199. static int __init
  200. plain_format(void)
  201. {
  202. char buf[PLAIN_BUF_SIZE];
  203. int nchars;
  204. nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
  205. if (nchars != PTR_WIDTH)
  206. return -1;
  207. if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
  208. pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
  209. PTR_VAL_NO_CRNG);
  210. return 0;
  211. }
  212. if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
  213. return -1;
  214. return 0;
  215. }
  216. #else
  217. #define PTR_WIDTH 8
  218. #define PTR ((void *)0x456789ab)
  219. #define PTR_STR "456789ab"
  220. #define PTR_VAL_NO_CRNG "(ptrval)"
  221. #define ZEROS ""
  222. #define ONES ""
  223. static int __init
  224. plain_format(void)
  225. {
  226. /* Format is implicitly tested for 32 bit machines by plain_hash() */
  227. return 0;
  228. }
  229. #endif /* BITS_PER_LONG == 64 */
  230. static int __init
  231. plain_hash_to_buffer(const void *p, char *buf, size_t len)
  232. {
  233. int nchars;
  234. nchars = snprintf(buf, len, "%p", p);
  235. if (nchars != PTR_WIDTH)
  236. return -1;
  237. if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
  238. pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
  239. PTR_VAL_NO_CRNG);
  240. return 0;
  241. }
  242. return 0;
  243. }
  244. static int __init
  245. plain_hash(void)
  246. {
  247. char buf[PLAIN_BUF_SIZE];
  248. int ret;
  249. ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
  250. if (ret)
  251. return ret;
  252. if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
  253. return -1;
  254. return 0;
  255. }
  256. /*
  257. * We can't use test() to test %p because we don't know what output to expect
  258. * after an address is hashed.
  259. */
  260. static void __init
  261. plain(void)
  262. {
  263. int err;
  264. if (no_hash_pointers) {
  265. pr_warn("skipping plain 'p' tests");
  266. skipped_tests += 2;
  267. return;
  268. }
  269. err = plain_hash();
  270. if (err) {
  271. pr_warn("plain 'p' does not appear to be hashed\n");
  272. failed_tests++;
  273. return;
  274. }
  275. err = plain_format();
  276. if (err) {
  277. pr_warn("hashing plain 'p' has unexpected format\n");
  278. failed_tests++;
  279. }
  280. }
  281. static void __init
  282. test_hashed(const char *fmt, const void *p)
  283. {
  284. char buf[PLAIN_BUF_SIZE];
  285. int ret;
  286. /*
  287. * No need to increase failed test counter since this is assumed
  288. * to be called after plain().
  289. */
  290. ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
  291. if (ret)
  292. return;
  293. test(buf, fmt, p);
  294. }
  295. /*
  296. * NULL pointers aren't hashed.
  297. */
  298. static void __init
  299. null_pointer(void)
  300. {
  301. test(ZEROS "00000000", "%p", NULL);
  302. test(ZEROS "00000000", "%px", NULL);
  303. test("(null)", "%pE", NULL);
  304. }
  305. /*
  306. * Error pointers aren't hashed.
  307. */
  308. static void __init
  309. error_pointer(void)
  310. {
  311. test(ONES "fffffff5", "%p", ERR_PTR(-11));
  312. test(ONES "fffffff5", "%px", ERR_PTR(-11));
  313. test("(efault)", "%pE", ERR_PTR(-11));
  314. }
  315. #define PTR_INVALID ((void *)0x000000ab)
  316. static void __init
  317. invalid_pointer(void)
  318. {
  319. test_hashed("%p", PTR_INVALID);
  320. test(ZEROS "000000ab", "%px", PTR_INVALID);
  321. test("(efault)", "%pE", PTR_INVALID);
  322. }
  323. static void __init
  324. symbol_ptr(void)
  325. {
  326. }
  327. static void __init
  328. kernel_ptr(void)
  329. {
  330. /* We can't test this without access to kptr_restrict. */
  331. }
  332. static void __init
  333. struct_resource(void)
  334. {
  335. }
  336. static void __init
  337. addr(void)
  338. {
  339. }
  340. static void __init
  341. escaped_str(void)
  342. {
  343. }
  344. static void __init
  345. hex_string(void)
  346. {
  347. const char buf[3] = {0xc0, 0xff, 0xee};
  348. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  349. "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
  350. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  351. "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
  352. }
  353. static void __init
  354. mac(void)
  355. {
  356. const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
  357. test("2d:48:d6:fc:7a:05", "%pM", addr);
  358. test("05:7a:fc:d6:48:2d", "%pMR", addr);
  359. test("2d-48-d6-fc-7a-05", "%pMF", addr);
  360. test("2d48d6fc7a05", "%pm", addr);
  361. test("057afcd6482d", "%pmR", addr);
  362. }
  363. static void __init
  364. ip4(void)
  365. {
  366. struct sockaddr_in sa;
  367. sa.sin_family = AF_INET;
  368. sa.sin_port = cpu_to_be16(12345);
  369. sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
  370. test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
  371. test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
  372. sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
  373. test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
  374. }
  375. static void __init
  376. ip6(void)
  377. {
  378. }
  379. static void __init
  380. ip(void)
  381. {
  382. ip4();
  383. ip6();
  384. }
  385. static void __init
  386. uuid(void)
  387. {
  388. const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
  389. 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  390. test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
  391. test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
  392. test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
  393. test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
  394. }
  395. static struct dentry test_dentry[4] __initdata = {
  396. { .d_parent = &test_dentry[0],
  397. .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
  398. .d_iname = "foo" },
  399. { .d_parent = &test_dentry[0],
  400. .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
  401. .d_iname = "bravo" },
  402. { .d_parent = &test_dentry[1],
  403. .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
  404. .d_iname = "alfa" },
  405. { .d_parent = &test_dentry[2],
  406. .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
  407. .d_iname = "romeo" },
  408. };
  409. static void __init
  410. dentry(void)
  411. {
  412. test("foo", "%pd", &test_dentry[0]);
  413. test("foo", "%pd2", &test_dentry[0]);
  414. test("(null)", "%pd", NULL);
  415. test("(efault)", "%pd", PTR_INVALID);
  416. test("(null)", "%pD", NULL);
  417. test("(efault)", "%pD", PTR_INVALID);
  418. test("romeo", "%pd", &test_dentry[3]);
  419. test("alfa/romeo", "%pd2", &test_dentry[3]);
  420. test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
  421. test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
  422. test("/bravo/alfa", "%pd4", &test_dentry[2]);
  423. test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
  424. test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
  425. }
  426. static void __init
  427. struct_va_format(void)
  428. {
  429. }
  430. static void __init
  431. time_and_date(void)
  432. {
  433. /* 1543210543 */
  434. const struct rtc_time tm = {
  435. .tm_sec = 43,
  436. .tm_min = 35,
  437. .tm_hour = 5,
  438. .tm_mday = 26,
  439. .tm_mon = 10,
  440. .tm_year = 118,
  441. };
  442. /* 2019-01-04T15:32:23 */
  443. time64_t t = 1546615943;
  444. test("(%pt?)", "%pt", &tm);
  445. test("2018-11-26T05:35:43", "%ptR", &tm);
  446. test("0118-10-26T05:35:43", "%ptRr", &tm);
  447. test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
  448. test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
  449. test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
  450. test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
  451. test("2019-01-04T15:32:23", "%ptT", &t);
  452. test("0119-00-04T15:32:23", "%ptTr", &t);
  453. test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
  454. test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
  455. test("2019-01-04 15:32:23", "%ptTs", &t);
  456. test("0119-00-04 15:32:23", "%ptTsr", &t);
  457. test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t);
  458. test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t);
  459. }
  460. static void __init
  461. struct_clk(void)
  462. {
  463. }
  464. static void __init
  465. large_bitmap(void)
  466. {
  467. const int nbits = 1 << 16;
  468. unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
  469. if (!bits)
  470. return;
  471. bitmap_set(bits, 1, 20);
  472. bitmap_set(bits, 60000, 15);
  473. test("1-20,60000-60014", "%*pbl", nbits, bits);
  474. bitmap_free(bits);
  475. }
  476. static void __init
  477. bitmap(void)
  478. {
  479. DECLARE_BITMAP(bits, 20);
  480. const int primes[] = {2,3,5,7,11,13,17,19};
  481. int i;
  482. bitmap_zero(bits, 20);
  483. test("00000|00000", "%20pb|%*pb", bits, 20, bits);
  484. test("|", "%20pbl|%*pbl", bits, 20, bits);
  485. for (i = 0; i < ARRAY_SIZE(primes); ++i)
  486. set_bit(primes[i], bits);
  487. test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
  488. test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
  489. bitmap_fill(bits, 20);
  490. test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
  491. test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
  492. large_bitmap();
  493. }
  494. static void __init
  495. netdev_features(void)
  496. {
  497. }
  498. struct page_flags_test {
  499. int width;
  500. int shift;
  501. int mask;
  502. const char *fmt;
  503. const char *name;
  504. };
  505. static const struct page_flags_test pft[] = {
  506. {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK,
  507. "%d", "section"},
  508. {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK,
  509. "%d", "node"},
  510. {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK,
  511. "%d", "zone"},
  512. {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK,
  513. "%#x", "lastcpupid"},
  514. {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK,
  515. "%#x", "kasantag"},
  516. };
  517. static void __init
  518. page_flags_test(int section, int node, int zone, int last_cpupid,
  519. int kasan_tag, unsigned long flags, const char *name,
  520. char *cmp_buf)
  521. {
  522. unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag};
  523. unsigned long size;
  524. bool append = false;
  525. int i;
  526. for (i = 0; i < ARRAY_SIZE(values); i++)
  527. flags |= (values[i] & pft[i].mask) << pft[i].shift;
  528. size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags);
  529. if (flags & PAGEFLAGS_MASK) {
  530. size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
  531. append = true;
  532. }
  533. for (i = 0; i < ARRAY_SIZE(pft); i++) {
  534. if (!pft[i].width)
  535. continue;
  536. if (append)
  537. size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");
  538. size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
  539. pft[i].name);
  540. size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
  541. values[i] & pft[i].mask);
  542. append = true;
  543. }
  544. snprintf(cmp_buf + size, BUF_SIZE - size, ")");
  545. test(cmp_buf, "%pGp", &flags);
  546. }
  547. static void __init
  548. flags(void)
  549. {
  550. unsigned long flags;
  551. char *cmp_buffer;
  552. gfp_t gfp;
  553. cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
  554. if (!cmp_buffer)
  555. return;
  556. flags = 0;
  557. page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer);
  558. flags = 1UL << NR_PAGEFLAGS;
  559. page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer);
  560. flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
  561. | 1UL << PG_active | 1UL << PG_swapbacked;
  562. page_flags_test(1, 1, 1, 0x1fffff, 1, flags,
  563. "uptodate|dirty|lru|active|swapbacked",
  564. cmp_buffer);
  565. flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  566. test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags);
  567. gfp = GFP_TRANSHUGE;
  568. test("GFP_TRANSHUGE", "%pGg", &gfp);
  569. gfp = GFP_ATOMIC|__GFP_DMA;
  570. test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
  571. gfp = __GFP_ATOMIC;
  572. test("__GFP_ATOMIC", "%pGg", &gfp);
  573. /* Any flags not translated by the table should remain numeric */
  574. gfp = ~__GFP_BITS_MASK;
  575. snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
  576. test(cmp_buffer, "%pGg", &gfp);
  577. snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
  578. (unsigned long) gfp);
  579. gfp |= __GFP_ATOMIC;
  580. test(cmp_buffer, "%pGg", &gfp);
  581. kfree(cmp_buffer);
  582. }
  583. static void __init fwnode_pointer(void)
  584. {
  585. const struct software_node softnodes[] = {
  586. { .name = "first", },
  587. { .name = "second", .parent = &softnodes[0], },
  588. { .name = "third", .parent = &softnodes[1], },
  589. { NULL /* Guardian */ }
  590. };
  591. const char * const full_name = "first/second/third";
  592. const char * const full_name_second = "first/second";
  593. const char * const second_name = "second";
  594. const char * const third_name = "third";
  595. int rval;
  596. rval = software_node_register_nodes(softnodes);
  597. if (rval) {
  598. pr_warn("cannot register softnodes; rval %d\n", rval);
  599. return;
  600. }
  601. test(full_name_second, "%pfw", software_node_fwnode(&softnodes[1]));
  602. test(full_name, "%pfw", software_node_fwnode(&softnodes[2]));
  603. test(full_name, "%pfwf", software_node_fwnode(&softnodes[2]));
  604. test(second_name, "%pfwP", software_node_fwnode(&softnodes[1]));
  605. test(third_name, "%pfwP", software_node_fwnode(&softnodes[2]));
  606. software_node_unregister_nodes(softnodes);
  607. }
  608. static void __init fourcc_pointer(void)
  609. {
  610. struct {
  611. u32 code;
  612. char *str;
  613. } const try[] = {
  614. { 0x3231564e, "NV12 little-endian (0x3231564e)", },
  615. { 0xb231564e, "NV12 big-endian (0xb231564e)", },
  616. { 0x10111213, ".... little-endian (0x10111213)", },
  617. { 0x20303159, "Y10 little-endian (0x20303159)", },
  618. };
  619. unsigned int i;
  620. for (i = 0; i < ARRAY_SIZE(try); i++)
  621. test(try[i].str, "%p4cc", &try[i].code);
  622. }
  623. static void __init
  624. errptr(void)
  625. {
  626. test("-1234", "%pe", ERR_PTR(-1234));
  627. /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
  628. BUILD_BUG_ON(IS_ERR(PTR));
  629. test_hashed("%pe", PTR);
  630. #ifdef CONFIG_SYMBOLIC_ERRNAME
  631. test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK));
  632. test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN));
  633. BUILD_BUG_ON(EAGAIN != EWOULDBLOCK);
  634. test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK));
  635. test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO));
  636. test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO));
  637. test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER));
  638. #endif
  639. }
  640. static void __init
  641. test_pointer(void)
  642. {
  643. plain();
  644. null_pointer();
  645. error_pointer();
  646. invalid_pointer();
  647. symbol_ptr();
  648. kernel_ptr();
  649. struct_resource();
  650. addr();
  651. escaped_str();
  652. hex_string();
  653. mac();
  654. ip();
  655. uuid();
  656. dentry();
  657. struct_va_format();
  658. time_and_date();
  659. struct_clk();
  660. bitmap();
  661. netdev_features();
  662. flags();
  663. errptr();
  664. fwnode_pointer();
  665. fourcc_pointer();
  666. }
  667. static void __init selftest(void)
  668. {
  669. alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
  670. if (!alloced_buffer)
  671. return;
  672. test_buffer = alloced_buffer + PAD_SIZE;
  673. test_basic();
  674. test_number();
  675. test_string();
  676. test_pointer();
  677. kfree(alloced_buffer);
  678. }
  679. KSTM_MODULE_LOADERS(test_printf);
  680. MODULE_AUTHOR("Rasmus Villemoes <[email protected]>");
  681. MODULE_LICENSE("GPL");