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