]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - lib/vsprintf.c
nvme: expose cntrltype and dctype through sysfs
[mirror_ubuntu-jammy-kernel.git] / lib / vsprintf.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/lib/vsprintf.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 /*
10 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 */
12
13 /*
14 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
15 * - changed to provide snprintf and vsnprintf functions
16 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
17 * - scnprintf and vscnprintf
18 */
19
20 #include <linux/stdarg.h>
21 #include <linux/build_bug.h>
22 #include <linux/clk.h>
23 #include <linux/clk-provider.h>
24 #include <linux/errname.h>
25 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/ctype.h>
29 #include <linux/kernel.h>
30 #include <linux/kallsyms.h>
31 #include <linux/math64.h>
32 #include <linux/uaccess.h>
33 #include <linux/ioport.h>
34 #include <linux/dcache.h>
35 #include <linux/cred.h>
36 #include <linux/rtc.h>
37 #include <linux/time.h>
38 #include <linux/uuid.h>
39 #include <linux/of.h>
40 #include <net/addrconf.h>
41 #include <linux/siphash.h>
42 #include <linux/compiler.h>
43 #include <linux/property.h>
44 #ifdef CONFIG_BLOCK
45 #include <linux/blkdev.h>
46 #endif
47
48 #include "../mm/internal.h" /* For the trace_print_flags arrays */
49
50 #include <asm/page.h> /* for PAGE_SIZE */
51 #include <asm/byteorder.h> /* cpu_to_le16 */
52 #include <asm/unaligned.h>
53
54 #include <linux/string_helpers.h>
55 #include "kstrtox.h"
56
57 /* Disable pointer hashing if requested */
58 bool no_hash_pointers __ro_after_init;
59 EXPORT_SYMBOL_GPL(no_hash_pointers);
60
61 static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base)
62 {
63 const char *cp;
64 unsigned long long result = 0ULL;
65 size_t prefix_chars;
66 unsigned int rv;
67
68 cp = _parse_integer_fixup_radix(startp, &base);
69 prefix_chars = cp - startp;
70 if (prefix_chars < max_chars) {
71 rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
72 /* FIXME */
73 cp += (rv & ~KSTRTOX_OVERFLOW);
74 } else {
75 /* Field too short for prefix + digit, skip over without converting */
76 cp = startp + max_chars;
77 }
78
79 if (endp)
80 *endp = (char *)cp;
81
82 return result;
83 }
84
85 /**
86 * simple_strtoull - convert a string to an unsigned long long
87 * @cp: The start of the string
88 * @endp: A pointer to the end of the parsed string will be placed here
89 * @base: The number base to use
90 *
91 * This function has caveats. Please use kstrtoull instead.
92 */
93 noinline
94 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
95 {
96 return simple_strntoull(cp, INT_MAX, endp, base);
97 }
98 EXPORT_SYMBOL(simple_strtoull);
99
100 /**
101 * simple_strtoul - convert a string to an unsigned long
102 * @cp: The start of the string
103 * @endp: A pointer to the end of the parsed string will be placed here
104 * @base: The number base to use
105 *
106 * This function has caveats. Please use kstrtoul instead.
107 */
108 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
109 {
110 return simple_strtoull(cp, endp, base);
111 }
112 EXPORT_SYMBOL(simple_strtoul);
113
114 /**
115 * simple_strtol - convert a string to a signed long
116 * @cp: The start of the string
117 * @endp: A pointer to the end of the parsed string will be placed here
118 * @base: The number base to use
119 *
120 * This function has caveats. Please use kstrtol instead.
121 */
122 long simple_strtol(const char *cp, char **endp, unsigned int base)
123 {
124 if (*cp == '-')
125 return -simple_strtoul(cp + 1, endp, base);
126
127 return simple_strtoul(cp, endp, base);
128 }
129 EXPORT_SYMBOL(simple_strtol);
130
131 static long long simple_strntoll(const char *cp, size_t max_chars, char **endp,
132 unsigned int base)
133 {
134 /*
135 * simple_strntoull() safely handles receiving max_chars==0 in the
136 * case cp[0] == '-' && max_chars == 1.
137 * If max_chars == 0 we can drop through and pass it to simple_strntoull()
138 * and the content of *cp is irrelevant.
139 */
140 if (*cp == '-' && max_chars > 0)
141 return -simple_strntoull(cp + 1, max_chars - 1, endp, base);
142
143 return simple_strntoull(cp, max_chars, endp, base);
144 }
145
146 /**
147 * simple_strtoll - convert a string to a signed long long
148 * @cp: The start of the string
149 * @endp: A pointer to the end of the parsed string will be placed here
150 * @base: The number base to use
151 *
152 * This function has caveats. Please use kstrtoll instead.
153 */
154 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
155 {
156 return simple_strntoll(cp, INT_MAX, endp, base);
157 }
158 EXPORT_SYMBOL(simple_strtoll);
159
160 static noinline_for_stack
161 int skip_atoi(const char **s)
162 {
163 int i = 0;
164
165 do {
166 i = i*10 + *((*s)++) - '0';
167 } while (isdigit(**s));
168
169 return i;
170 }
171
172 /*
173 * Decimal conversion is by far the most typical, and is used for
174 * /proc and /sys data. This directly impacts e.g. top performance
175 * with many processes running. We optimize it for speed by emitting
176 * two characters at a time, using a 200 byte lookup table. This
177 * roughly halves the number of multiplications compared to computing
178 * the digits one at a time. Implementation strongly inspired by the
179 * previous version, which in turn used ideas described at
180 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
181 * from the author, Douglas W. Jones).
182 *
183 * It turns out there is precisely one 26 bit fixed-point
184 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
185 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
186 * range happens to be somewhat larger (x <= 1073741898), but that's
187 * irrelevant for our purpose.
188 *
189 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
190 * need a 32x32->64 bit multiply, so we simply use the same constant.
191 *
192 * For dividing a number in the range [100, 10^4-1] by 100, there are
193 * several options. The simplest is (x * 0x147b) >> 19, which is valid
194 * for all x <= 43698.
195 */
196
197 static const u16 decpair[100] = {
198 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
199 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
200 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
201 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
202 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
203 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
204 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
205 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
206 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
207 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
208 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
209 #undef _
210 };
211
212 /*
213 * This will print a single '0' even if r == 0, since we would
214 * immediately jump to out_r where two 0s would be written but only
215 * one of them accounted for in buf. This is needed by ip4_string
216 * below. All other callers pass a non-zero value of r.
217 */
218 static noinline_for_stack
219 char *put_dec_trunc8(char *buf, unsigned r)
220 {
221 unsigned q;
222
223 /* 1 <= r < 10^8 */
224 if (r < 100)
225 goto out_r;
226
227 /* 100 <= r < 10^8 */
228 q = (r * (u64)0x28f5c29) >> 32;
229 *((u16 *)buf) = decpair[r - 100*q];
230 buf += 2;
231
232 /* 1 <= q < 10^6 */
233 if (q < 100)
234 goto out_q;
235
236 /* 100 <= q < 10^6 */
237 r = (q * (u64)0x28f5c29) >> 32;
238 *((u16 *)buf) = decpair[q - 100*r];
239 buf += 2;
240
241 /* 1 <= r < 10^4 */
242 if (r < 100)
243 goto out_r;
244
245 /* 100 <= r < 10^4 */
246 q = (r * 0x147b) >> 19;
247 *((u16 *)buf) = decpair[r - 100*q];
248 buf += 2;
249 out_q:
250 /* 1 <= q < 100 */
251 r = q;
252 out_r:
253 /* 1 <= r < 100 */
254 *((u16 *)buf) = decpair[r];
255 buf += r < 10 ? 1 : 2;
256 return buf;
257 }
258
259 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
260 static noinline_for_stack
261 char *put_dec_full8(char *buf, unsigned r)
262 {
263 unsigned q;
264
265 /* 0 <= r < 10^8 */
266 q = (r * (u64)0x28f5c29) >> 32;
267 *((u16 *)buf) = decpair[r - 100*q];
268 buf += 2;
269
270 /* 0 <= q < 10^6 */
271 r = (q * (u64)0x28f5c29) >> 32;
272 *((u16 *)buf) = decpair[q - 100*r];
273 buf += 2;
274
275 /* 0 <= r < 10^4 */
276 q = (r * 0x147b) >> 19;
277 *((u16 *)buf) = decpair[r - 100*q];
278 buf += 2;
279
280 /* 0 <= q < 100 */
281 *((u16 *)buf) = decpair[q];
282 buf += 2;
283 return buf;
284 }
285
286 static noinline_for_stack
287 char *put_dec(char *buf, unsigned long long n)
288 {
289 if (n >= 100*1000*1000)
290 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
291 /* 1 <= n <= 1.6e11 */
292 if (n >= 100*1000*1000)
293 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
294 /* 1 <= n < 1e8 */
295 return put_dec_trunc8(buf, n);
296 }
297
298 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
299
300 static void
301 put_dec_full4(char *buf, unsigned r)
302 {
303 unsigned q;
304
305 /* 0 <= r < 10^4 */
306 q = (r * 0x147b) >> 19;
307 *((u16 *)buf) = decpair[r - 100*q];
308 buf += 2;
309 /* 0 <= q < 100 */
310 *((u16 *)buf) = decpair[q];
311 }
312
313 /*
314 * Call put_dec_full4 on x % 10000, return x / 10000.
315 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
316 * holds for all x < 1,128,869,999. The largest value this
317 * helper will ever be asked to convert is 1,125,520,955.
318 * (second call in the put_dec code, assuming n is all-ones).
319 */
320 static noinline_for_stack
321 unsigned put_dec_helper4(char *buf, unsigned x)
322 {
323 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
324
325 put_dec_full4(buf, x - q * 10000);
326 return q;
327 }
328
329 /* Based on code by Douglas W. Jones found at
330 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
331 * (with permission from the author).
332 * Performs no 64-bit division and hence should be fast on 32-bit machines.
333 */
334 static
335 char *put_dec(char *buf, unsigned long long n)
336 {
337 uint32_t d3, d2, d1, q, h;
338
339 if (n < 100*1000*1000)
340 return put_dec_trunc8(buf, n);
341
342 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
343 h = (n >> 32);
344 d2 = (h ) & 0xffff;
345 d3 = (h >> 16); /* implicit "& 0xffff" */
346
347 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
348 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
349 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
350 q = put_dec_helper4(buf, q);
351
352 q += 7671 * d3 + 9496 * d2 + 6 * d1;
353 q = put_dec_helper4(buf+4, q);
354
355 q += 4749 * d3 + 42 * d2;
356 q = put_dec_helper4(buf+8, q);
357
358 q += 281 * d3;
359 buf += 12;
360 if (q)
361 buf = put_dec_trunc8(buf, q);
362 else while (buf[-1] == '0')
363 --buf;
364
365 return buf;
366 }
367
368 #endif
369
370 /*
371 * Convert passed number to decimal string.
372 * Returns the length of string. On buffer overflow, returns 0.
373 *
374 * If speed is not important, use snprintf(). It's easy to read the code.
375 */
376 int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
377 {
378 /* put_dec requires 2-byte alignment of the buffer. */
379 char tmp[sizeof(num) * 3] __aligned(2);
380 int idx, len;
381
382 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
383 if (num <= 9) {
384 tmp[0] = '0' + num;
385 len = 1;
386 } else {
387 len = put_dec(tmp, num) - tmp;
388 }
389
390 if (len > size || width > size)
391 return 0;
392
393 if (width > len) {
394 width = width - len;
395 for (idx = 0; idx < width; idx++)
396 buf[idx] = ' ';
397 } else {
398 width = 0;
399 }
400
401 for (idx = 0; idx < len; ++idx)
402 buf[idx + width] = tmp[len - idx - 1];
403
404 return len + width;
405 }
406
407 #define SIGN 1 /* unsigned/signed, must be 1 */
408 #define LEFT 2 /* left justified */
409 #define PLUS 4 /* show plus */
410 #define SPACE 8 /* space if plus */
411 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
412 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
413 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
414
415 static_assert(ZEROPAD == ('0' - ' '));
416 static_assert(SMALL == ' ');
417
418 enum format_type {
419 FORMAT_TYPE_NONE, /* Just a string part */
420 FORMAT_TYPE_WIDTH,
421 FORMAT_TYPE_PRECISION,
422 FORMAT_TYPE_CHAR,
423 FORMAT_TYPE_STR,
424 FORMAT_TYPE_PTR,
425 FORMAT_TYPE_PERCENT_CHAR,
426 FORMAT_TYPE_INVALID,
427 FORMAT_TYPE_LONG_LONG,
428 FORMAT_TYPE_ULONG,
429 FORMAT_TYPE_LONG,
430 FORMAT_TYPE_UBYTE,
431 FORMAT_TYPE_BYTE,
432 FORMAT_TYPE_USHORT,
433 FORMAT_TYPE_SHORT,
434 FORMAT_TYPE_UINT,
435 FORMAT_TYPE_INT,
436 FORMAT_TYPE_SIZE_T,
437 FORMAT_TYPE_PTRDIFF
438 };
439
440 struct printf_spec {
441 unsigned int type:8; /* format_type enum */
442 signed int field_width:24; /* width of output field */
443 unsigned int flags:8; /* flags to number() */
444 unsigned int base:8; /* number base, 8, 10 or 16 only */
445 signed int precision:16; /* # of digits/chars */
446 } __packed;
447 static_assert(sizeof(struct printf_spec) == 8);
448
449 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
450 #define PRECISION_MAX ((1 << 15) - 1)
451
452 static noinline_for_stack
453 char *number(char *buf, char *end, unsigned long long num,
454 struct printf_spec spec)
455 {
456 /* put_dec requires 2-byte alignment of the buffer. */
457 char tmp[3 * sizeof(num)] __aligned(2);
458 char sign;
459 char locase;
460 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
461 int i;
462 bool is_zero = num == 0LL;
463 int field_width = spec.field_width;
464 int precision = spec.precision;
465
466 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
467 * produces same digits or (maybe lowercased) letters */
468 locase = (spec.flags & SMALL);
469 if (spec.flags & LEFT)
470 spec.flags &= ~ZEROPAD;
471 sign = 0;
472 if (spec.flags & SIGN) {
473 if ((signed long long)num < 0) {
474 sign = '-';
475 num = -(signed long long)num;
476 field_width--;
477 } else if (spec.flags & PLUS) {
478 sign = '+';
479 field_width--;
480 } else if (spec.flags & SPACE) {
481 sign = ' ';
482 field_width--;
483 }
484 }
485 if (need_pfx) {
486 if (spec.base == 16)
487 field_width -= 2;
488 else if (!is_zero)
489 field_width--;
490 }
491
492 /* generate full string in tmp[], in reverse order */
493 i = 0;
494 if (num < spec.base)
495 tmp[i++] = hex_asc_upper[num] | locase;
496 else if (spec.base != 10) { /* 8 or 16 */
497 int mask = spec.base - 1;
498 int shift = 3;
499
500 if (spec.base == 16)
501 shift = 4;
502 do {
503 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
504 num >>= shift;
505 } while (num);
506 } else { /* base 10 */
507 i = put_dec(tmp, num) - tmp;
508 }
509
510 /* printing 100 using %2d gives "100", not "00" */
511 if (i > precision)
512 precision = i;
513 /* leading space padding */
514 field_width -= precision;
515 if (!(spec.flags & (ZEROPAD | LEFT))) {
516 while (--field_width >= 0) {
517 if (buf < end)
518 *buf = ' ';
519 ++buf;
520 }
521 }
522 /* sign */
523 if (sign) {
524 if (buf < end)
525 *buf = sign;
526 ++buf;
527 }
528 /* "0x" / "0" prefix */
529 if (need_pfx) {
530 if (spec.base == 16 || !is_zero) {
531 if (buf < end)
532 *buf = '0';
533 ++buf;
534 }
535 if (spec.base == 16) {
536 if (buf < end)
537 *buf = ('X' | locase);
538 ++buf;
539 }
540 }
541 /* zero or space padding */
542 if (!(spec.flags & LEFT)) {
543 char c = ' ' + (spec.flags & ZEROPAD);
544
545 while (--field_width >= 0) {
546 if (buf < end)
547 *buf = c;
548 ++buf;
549 }
550 }
551 /* hmm even more zero padding? */
552 while (i <= --precision) {
553 if (buf < end)
554 *buf = '0';
555 ++buf;
556 }
557 /* actual digits of result */
558 while (--i >= 0) {
559 if (buf < end)
560 *buf = tmp[i];
561 ++buf;
562 }
563 /* trailing space padding */
564 while (--field_width >= 0) {
565 if (buf < end)
566 *buf = ' ';
567 ++buf;
568 }
569
570 return buf;
571 }
572
573 static noinline_for_stack
574 char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
575 {
576 struct printf_spec spec;
577
578 spec.type = FORMAT_TYPE_PTR;
579 spec.field_width = 2 + 2 * size; /* 0x + hex */
580 spec.flags = SPECIAL | SMALL | ZEROPAD;
581 spec.base = 16;
582 spec.precision = -1;
583
584 return number(buf, end, num, spec);
585 }
586
587 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
588 {
589 size_t size;
590 if (buf >= end) /* nowhere to put anything */
591 return;
592 size = end - buf;
593 if (size <= spaces) {
594 memset(buf, ' ', size);
595 return;
596 }
597 if (len) {
598 if (len > size - spaces)
599 len = size - spaces;
600 memmove(buf + spaces, buf, len);
601 }
602 memset(buf, ' ', spaces);
603 }
604
605 /*
606 * Handle field width padding for a string.
607 * @buf: current buffer position
608 * @n: length of string
609 * @end: end of output buffer
610 * @spec: for field width and flags
611 * Returns: new buffer position after padding.
612 */
613 static noinline_for_stack
614 char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
615 {
616 unsigned spaces;
617
618 if (likely(n >= spec.field_width))
619 return buf;
620 /* we want to pad the sucker */
621 spaces = spec.field_width - n;
622 if (!(spec.flags & LEFT)) {
623 move_right(buf - n, end, n, spaces);
624 return buf + spaces;
625 }
626 while (spaces--) {
627 if (buf < end)
628 *buf = ' ';
629 ++buf;
630 }
631 return buf;
632 }
633
634 /* Handle string from a well known address. */
635 static char *string_nocheck(char *buf, char *end, const char *s,
636 struct printf_spec spec)
637 {
638 int len = 0;
639 int lim = spec.precision;
640
641 while (lim--) {
642 char c = *s++;
643 if (!c)
644 break;
645 if (buf < end)
646 *buf = c;
647 ++buf;
648 ++len;
649 }
650 return widen_string(buf, len, end, spec);
651 }
652
653 static char *err_ptr(char *buf, char *end, void *ptr,
654 struct printf_spec spec)
655 {
656 int err = PTR_ERR(ptr);
657 const char *sym = errname(err);
658
659 if (sym)
660 return string_nocheck(buf, end, sym, spec);
661
662 /*
663 * Somebody passed ERR_PTR(-1234) or some other non-existing
664 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
665 * printing it as its decimal representation.
666 */
667 spec.flags |= SIGN;
668 spec.base = 10;
669 return number(buf, end, err, spec);
670 }
671
672 /* Be careful: error messages must fit into the given buffer. */
673 static char *error_string(char *buf, char *end, const char *s,
674 struct printf_spec spec)
675 {
676 /*
677 * Hard limit to avoid a completely insane messages. It actually
678 * works pretty well because most error messages are in
679 * the many pointer format modifiers.
680 */
681 if (spec.precision == -1)
682 spec.precision = 2 * sizeof(void *);
683
684 return string_nocheck(buf, end, s, spec);
685 }
686
687 /*
688 * Do not call any complex external code here. Nested printk()/vsprintf()
689 * might cause infinite loops. Failures might break printk() and would
690 * be hard to debug.
691 */
692 static const char *check_pointer_msg(const void *ptr)
693 {
694 if (!ptr)
695 return "(null)";
696
697 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
698 return "(efault)";
699
700 return NULL;
701 }
702
703 static int check_pointer(char **buf, char *end, const void *ptr,
704 struct printf_spec spec)
705 {
706 const char *err_msg;
707
708 err_msg = check_pointer_msg(ptr);
709 if (err_msg) {
710 *buf = error_string(*buf, end, err_msg, spec);
711 return -EFAULT;
712 }
713
714 return 0;
715 }
716
717 static noinline_for_stack
718 char *string(char *buf, char *end, const char *s,
719 struct printf_spec spec)
720 {
721 if (check_pointer(&buf, end, s, spec))
722 return buf;
723
724 return string_nocheck(buf, end, s, spec);
725 }
726
727 static char *pointer_string(char *buf, char *end,
728 const void *ptr,
729 struct printf_spec spec)
730 {
731 spec.base = 16;
732 spec.flags |= SMALL;
733 if (spec.field_width == -1) {
734 spec.field_width = 2 * sizeof(ptr);
735 spec.flags |= ZEROPAD;
736 }
737
738 return number(buf, end, (unsigned long int)ptr, spec);
739 }
740
741 /* Make pointers available for printing early in the boot sequence. */
742 static int debug_boot_weak_hash __ro_after_init;
743
744 static int __init debug_boot_weak_hash_enable(char *str)
745 {
746 debug_boot_weak_hash = 1;
747 pr_info("debug_boot_weak_hash enabled\n");
748 return 0;
749 }
750 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
751
752 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
753 static siphash_key_t ptr_key __read_mostly;
754
755 static void enable_ptr_key_workfn(struct work_struct *work)
756 {
757 get_random_bytes(&ptr_key, sizeof(ptr_key));
758 /* Needs to run from preemptible context */
759 static_branch_disable(&not_filled_random_ptr_key);
760 }
761
762 static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
763
764 static void fill_random_ptr_key(struct random_ready_callback *unused)
765 {
766 /* This may be in an interrupt handler. */
767 queue_work(system_unbound_wq, &enable_ptr_key_work);
768 }
769
770 static struct random_ready_callback random_ready = {
771 .func = fill_random_ptr_key
772 };
773
774 static int __init initialize_ptr_random(void)
775 {
776 int key_size = sizeof(ptr_key);
777 int ret;
778
779 /* Use hw RNG if available. */
780 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
781 static_branch_disable(&not_filled_random_ptr_key);
782 return 0;
783 }
784
785 ret = add_random_ready_callback(&random_ready);
786 if (!ret) {
787 return 0;
788 } else if (ret == -EALREADY) {
789 /* This is in preemptible context */
790 enable_ptr_key_workfn(&enable_ptr_key_work);
791 return 0;
792 }
793
794 return ret;
795 }
796 early_initcall(initialize_ptr_random);
797
798 /* Maps a pointer to a 32 bit unique identifier. */
799 static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
800 {
801 unsigned long hashval;
802
803 if (static_branch_unlikely(&not_filled_random_ptr_key))
804 return -EAGAIN;
805
806 #ifdef CONFIG_64BIT
807 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
808 /*
809 * Mask off the first 32 bits, this makes explicit that we have
810 * modified the address (and 32 bits is plenty for a unique ID).
811 */
812 hashval = hashval & 0xffffffff;
813 #else
814 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
815 #endif
816 *hashval_out = hashval;
817 return 0;
818 }
819
820 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
821 {
822 return __ptr_to_hashval(ptr, hashval_out);
823 }
824
825 static char *ptr_to_id(char *buf, char *end, const void *ptr,
826 struct printf_spec spec)
827 {
828 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
829 unsigned long hashval;
830 int ret;
831
832 /*
833 * Print the real pointer value for NULL and error pointers,
834 * as they are not actual addresses.
835 */
836 if (IS_ERR_OR_NULL(ptr))
837 return pointer_string(buf, end, ptr, spec);
838
839 /* When debugging early boot use non-cryptographically secure hash. */
840 if (unlikely(debug_boot_weak_hash)) {
841 hashval = hash_long((unsigned long)ptr, 32);
842 return pointer_string(buf, end, (const void *)hashval, spec);
843 }
844
845 ret = __ptr_to_hashval(ptr, &hashval);
846 if (ret) {
847 spec.field_width = 2 * sizeof(ptr);
848 /* string length must be less than default_width */
849 return error_string(buf, end, str, spec);
850 }
851
852 return pointer_string(buf, end, (const void *)hashval, spec);
853 }
854
855 static char *default_pointer(char *buf, char *end, const void *ptr,
856 struct printf_spec spec)
857 {
858 /*
859 * default is to _not_ leak addresses, so hash before printing,
860 * unless no_hash_pointers is specified on the command line.
861 */
862 if (unlikely(no_hash_pointers))
863 return pointer_string(buf, end, ptr, spec);
864
865 return ptr_to_id(buf, end, ptr, spec);
866 }
867
868 int kptr_restrict __read_mostly;
869
870 static noinline_for_stack
871 char *restricted_pointer(char *buf, char *end, const void *ptr,
872 struct printf_spec spec)
873 {
874 switch (kptr_restrict) {
875 case 0:
876 /* Handle as %p, hash and do _not_ leak addresses. */
877 return default_pointer(buf, end, ptr, spec);
878 case 1: {
879 const struct cred *cred;
880
881 /*
882 * kptr_restrict==1 cannot be used in IRQ context
883 * because its test for CAP_SYSLOG would be meaningless.
884 */
885 if (in_irq() || in_serving_softirq() || in_nmi()) {
886 if (spec.field_width == -1)
887 spec.field_width = 2 * sizeof(ptr);
888 return error_string(buf, end, "pK-error", spec);
889 }
890
891 /*
892 * Only print the real pointer value if the current
893 * process has CAP_SYSLOG and is running with the
894 * same credentials it started with. This is because
895 * access to files is checked at open() time, but %pK
896 * checks permission at read() time. We don't want to
897 * leak pointer values if a binary opens a file using
898 * %pK and then elevates privileges before reading it.
899 */
900 cred = current_cred();
901 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
902 !uid_eq(cred->euid, cred->uid) ||
903 !gid_eq(cred->egid, cred->gid))
904 ptr = NULL;
905 break;
906 }
907 case 2:
908 default:
909 /* Always print 0's for %pK */
910 ptr = NULL;
911 break;
912 }
913
914 return pointer_string(buf, end, ptr, spec);
915 }
916
917 static noinline_for_stack
918 char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
919 const char *fmt)
920 {
921 const char *array[4], *s;
922 const struct dentry *p;
923 int depth;
924 int i, n;
925
926 switch (fmt[1]) {
927 case '2': case '3': case '4':
928 depth = fmt[1] - '0';
929 break;
930 default:
931 depth = 1;
932 }
933
934 rcu_read_lock();
935 for (i = 0; i < depth; i++, d = p) {
936 if (check_pointer(&buf, end, d, spec)) {
937 rcu_read_unlock();
938 return buf;
939 }
940
941 p = READ_ONCE(d->d_parent);
942 array[i] = READ_ONCE(d->d_name.name);
943 if (p == d) {
944 if (i)
945 array[i] = "";
946 i++;
947 break;
948 }
949 }
950 s = array[--i];
951 for (n = 0; n != spec.precision; n++, buf++) {
952 char c = *s++;
953 if (!c) {
954 if (!i)
955 break;
956 c = '/';
957 s = array[--i];
958 }
959 if (buf < end)
960 *buf = c;
961 }
962 rcu_read_unlock();
963 return widen_string(buf, n, end, spec);
964 }
965
966 static noinline_for_stack
967 char *file_dentry_name(char *buf, char *end, const struct file *f,
968 struct printf_spec spec, const char *fmt)
969 {
970 if (check_pointer(&buf, end, f, spec))
971 return buf;
972
973 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
974 }
975 #ifdef CONFIG_BLOCK
976 static noinline_for_stack
977 char *bdev_name(char *buf, char *end, struct block_device *bdev,
978 struct printf_spec spec, const char *fmt)
979 {
980 struct gendisk *hd;
981
982 if (check_pointer(&buf, end, bdev, spec))
983 return buf;
984
985 hd = bdev->bd_disk;
986 buf = string(buf, end, hd->disk_name, spec);
987 if (bdev->bd_partno) {
988 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
989 if (buf < end)
990 *buf = 'p';
991 buf++;
992 }
993 buf = number(buf, end, bdev->bd_partno, spec);
994 }
995 return buf;
996 }
997 #endif
998
999 static noinline_for_stack
1000 char *symbol_string(char *buf, char *end, void *ptr,
1001 struct printf_spec spec, const char *fmt)
1002 {
1003 unsigned long value;
1004 #ifdef CONFIG_KALLSYMS
1005 char sym[KSYM_SYMBOL_LEN];
1006 #endif
1007
1008 if (fmt[1] == 'R')
1009 ptr = __builtin_extract_return_addr(ptr);
1010 value = (unsigned long)ptr;
1011
1012 #ifdef CONFIG_KALLSYMS
1013 if (*fmt == 'B' && fmt[1] == 'b')
1014 sprint_backtrace_build_id(sym, value);
1015 else if (*fmt == 'B')
1016 sprint_backtrace(sym, value);
1017 else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b')))
1018 sprint_symbol_build_id(sym, value);
1019 else if (*fmt != 's')
1020 sprint_symbol(sym, value);
1021 else
1022 sprint_symbol_no_offset(sym, value);
1023
1024 return string_nocheck(buf, end, sym, spec);
1025 #else
1026 return special_hex_number(buf, end, value, sizeof(void *));
1027 #endif
1028 }
1029
1030 static const struct printf_spec default_str_spec = {
1031 .field_width = -1,
1032 .precision = -1,
1033 };
1034
1035 static const struct printf_spec default_flag_spec = {
1036 .base = 16,
1037 .precision = -1,
1038 .flags = SPECIAL | SMALL,
1039 };
1040
1041 static const struct printf_spec default_dec_spec = {
1042 .base = 10,
1043 .precision = -1,
1044 };
1045
1046 static const struct printf_spec default_dec02_spec = {
1047 .base = 10,
1048 .field_width = 2,
1049 .precision = -1,
1050 .flags = ZEROPAD,
1051 };
1052
1053 static const struct printf_spec default_dec04_spec = {
1054 .base = 10,
1055 .field_width = 4,
1056 .precision = -1,
1057 .flags = ZEROPAD,
1058 };
1059
1060 static noinline_for_stack
1061 char *resource_string(char *buf, char *end, struct resource *res,
1062 struct printf_spec spec, const char *fmt)
1063 {
1064 #ifndef IO_RSRC_PRINTK_SIZE
1065 #define IO_RSRC_PRINTK_SIZE 6
1066 #endif
1067
1068 #ifndef MEM_RSRC_PRINTK_SIZE
1069 #define MEM_RSRC_PRINTK_SIZE 10
1070 #endif
1071 static const struct printf_spec io_spec = {
1072 .base = 16,
1073 .field_width = IO_RSRC_PRINTK_SIZE,
1074 .precision = -1,
1075 .flags = SPECIAL | SMALL | ZEROPAD,
1076 };
1077 static const struct printf_spec mem_spec = {
1078 .base = 16,
1079 .field_width = MEM_RSRC_PRINTK_SIZE,
1080 .precision = -1,
1081 .flags = SPECIAL | SMALL | ZEROPAD,
1082 };
1083 static const struct printf_spec bus_spec = {
1084 .base = 16,
1085 .field_width = 2,
1086 .precision = -1,
1087 .flags = SMALL | ZEROPAD,
1088 };
1089 static const struct printf_spec str_spec = {
1090 .field_width = -1,
1091 .precision = 10,
1092 .flags = LEFT,
1093 };
1094
1095 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1096 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1097 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1098 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
1099 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
1100 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1101 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1102 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1103
1104 char *p = sym, *pend = sym + sizeof(sym);
1105 int decode = (fmt[0] == 'R') ? 1 : 0;
1106 const struct printf_spec *specp;
1107
1108 if (check_pointer(&buf, end, res, spec))
1109 return buf;
1110
1111 *p++ = '[';
1112 if (res->flags & IORESOURCE_IO) {
1113 p = string_nocheck(p, pend, "io ", str_spec);
1114 specp = &io_spec;
1115 } else if (res->flags & IORESOURCE_MEM) {
1116 p = string_nocheck(p, pend, "mem ", str_spec);
1117 specp = &mem_spec;
1118 } else if (res->flags & IORESOURCE_IRQ) {
1119 p = string_nocheck(p, pend, "irq ", str_spec);
1120 specp = &default_dec_spec;
1121 } else if (res->flags & IORESOURCE_DMA) {
1122 p = string_nocheck(p, pend, "dma ", str_spec);
1123 specp = &default_dec_spec;
1124 } else if (res->flags & IORESOURCE_BUS) {
1125 p = string_nocheck(p, pend, "bus ", str_spec);
1126 specp = &bus_spec;
1127 } else {
1128 p = string_nocheck(p, pend, "??? ", str_spec);
1129 specp = &mem_spec;
1130 decode = 0;
1131 }
1132 if (decode && res->flags & IORESOURCE_UNSET) {
1133 p = string_nocheck(p, pend, "size ", str_spec);
1134 p = number(p, pend, resource_size(res), *specp);
1135 } else {
1136 p = number(p, pend, res->start, *specp);
1137 if (res->start != res->end) {
1138 *p++ = '-';
1139 p = number(p, pend, res->end, *specp);
1140 }
1141 }
1142 if (decode) {
1143 if (res->flags & IORESOURCE_MEM_64)
1144 p = string_nocheck(p, pend, " 64bit", str_spec);
1145 if (res->flags & IORESOURCE_PREFETCH)
1146 p = string_nocheck(p, pend, " pref", str_spec);
1147 if (res->flags & IORESOURCE_WINDOW)
1148 p = string_nocheck(p, pend, " window", str_spec);
1149 if (res->flags & IORESOURCE_DISABLED)
1150 p = string_nocheck(p, pend, " disabled", str_spec);
1151 } else {
1152 p = string_nocheck(p, pend, " flags ", str_spec);
1153 p = number(p, pend, res->flags, default_flag_spec);
1154 }
1155 *p++ = ']';
1156 *p = '\0';
1157
1158 return string_nocheck(buf, end, sym, spec);
1159 }
1160
1161 static noinline_for_stack
1162 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1163 const char *fmt)
1164 {
1165 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
1166 negative value, fallback to the default */
1167 char separator;
1168
1169 if (spec.field_width == 0)
1170 /* nothing to print */
1171 return buf;
1172
1173 if (check_pointer(&buf, end, addr, spec))
1174 return buf;
1175
1176 switch (fmt[1]) {
1177 case 'C':
1178 separator = ':';
1179 break;
1180 case 'D':
1181 separator = '-';
1182 break;
1183 case 'N':
1184 separator = 0;
1185 break;
1186 default:
1187 separator = ' ';
1188 break;
1189 }
1190
1191 if (spec.field_width > 0)
1192 len = min_t(int, spec.field_width, 64);
1193
1194 for (i = 0; i < len; ++i) {
1195 if (buf < end)
1196 *buf = hex_asc_hi(addr[i]);
1197 ++buf;
1198 if (buf < end)
1199 *buf = hex_asc_lo(addr[i]);
1200 ++buf;
1201
1202 if (separator && i != len - 1) {
1203 if (buf < end)
1204 *buf = separator;
1205 ++buf;
1206 }
1207 }
1208
1209 return buf;
1210 }
1211
1212 static noinline_for_stack
1213 char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1214 struct printf_spec spec, const char *fmt)
1215 {
1216 const int CHUNKSZ = 32;
1217 int nr_bits = max_t(int, spec.field_width, 0);
1218 int i, chunksz;
1219 bool first = true;
1220
1221 if (check_pointer(&buf, end, bitmap, spec))
1222 return buf;
1223
1224 /* reused to print numbers */
1225 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1226
1227 chunksz = nr_bits & (CHUNKSZ - 1);
1228 if (chunksz == 0)
1229 chunksz = CHUNKSZ;
1230
1231 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1232 for (; i >= 0; i -= CHUNKSZ) {
1233 u32 chunkmask, val;
1234 int word, bit;
1235
1236 chunkmask = ((1ULL << chunksz) - 1);
1237 word = i / BITS_PER_LONG;
1238 bit = i % BITS_PER_LONG;
1239 val = (bitmap[word] >> bit) & chunkmask;
1240
1241 if (!first) {
1242 if (buf < end)
1243 *buf = ',';
1244 buf++;
1245 }
1246 first = false;
1247
1248 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1249 buf = number(buf, end, val, spec);
1250
1251 chunksz = CHUNKSZ;
1252 }
1253 return buf;
1254 }
1255
1256 static noinline_for_stack
1257 char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1258 struct printf_spec spec, const char *fmt)
1259 {
1260 int nr_bits = max_t(int, spec.field_width, 0);
1261 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1262 int cur, rbot, rtop;
1263 bool first = true;
1264
1265 if (check_pointer(&buf, end, bitmap, spec))
1266 return buf;
1267
1268 rbot = cur = find_first_bit(bitmap, nr_bits);
1269 while (cur < nr_bits) {
1270 rtop = cur;
1271 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1272 if (cur < nr_bits && cur <= rtop + 1)
1273 continue;
1274
1275 if (!first) {
1276 if (buf < end)
1277 *buf = ',';
1278 buf++;
1279 }
1280 first = false;
1281
1282 buf = number(buf, end, rbot, default_dec_spec);
1283 if (rbot < rtop) {
1284 if (buf < end)
1285 *buf = '-';
1286 buf++;
1287
1288 buf = number(buf, end, rtop, default_dec_spec);
1289 }
1290
1291 rbot = cur;
1292 }
1293 return buf;
1294 }
1295
1296 static noinline_for_stack
1297 char *mac_address_string(char *buf, char *end, u8 *addr,
1298 struct printf_spec spec, const char *fmt)
1299 {
1300 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1301 char *p = mac_addr;
1302 int i;
1303 char separator;
1304 bool reversed = false;
1305
1306 if (check_pointer(&buf, end, addr, spec))
1307 return buf;
1308
1309 switch (fmt[1]) {
1310 case 'F':
1311 separator = '-';
1312 break;
1313
1314 case 'R':
1315 reversed = true;
1316 fallthrough;
1317
1318 default:
1319 separator = ':';
1320 break;
1321 }
1322
1323 for (i = 0; i < 6; i++) {
1324 if (reversed)
1325 p = hex_byte_pack(p, addr[5 - i]);
1326 else
1327 p = hex_byte_pack(p, addr[i]);
1328
1329 if (fmt[0] == 'M' && i != 5)
1330 *p++ = separator;
1331 }
1332 *p = '\0';
1333
1334 return string_nocheck(buf, end, mac_addr, spec);
1335 }
1336
1337 static noinline_for_stack
1338 char *ip4_string(char *p, const u8 *addr, const char *fmt)
1339 {
1340 int i;
1341 bool leading_zeros = (fmt[0] == 'i');
1342 int index;
1343 int step;
1344
1345 switch (fmt[2]) {
1346 case 'h':
1347 #ifdef __BIG_ENDIAN
1348 index = 0;
1349 step = 1;
1350 #else
1351 index = 3;
1352 step = -1;
1353 #endif
1354 break;
1355 case 'l':
1356 index = 3;
1357 step = -1;
1358 break;
1359 case 'n':
1360 case 'b':
1361 default:
1362 index = 0;
1363 step = 1;
1364 break;
1365 }
1366 for (i = 0; i < 4; i++) {
1367 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
1368 int digits = put_dec_trunc8(temp, addr[index]) - temp;
1369 if (leading_zeros) {
1370 if (digits < 3)
1371 *p++ = '0';
1372 if (digits < 2)
1373 *p++ = '0';
1374 }
1375 /* reverse the digits in the quad */
1376 while (digits--)
1377 *p++ = temp[digits];
1378 if (i < 3)
1379 *p++ = '.';
1380 index += step;
1381 }
1382 *p = '\0';
1383
1384 return p;
1385 }
1386
1387 static noinline_for_stack
1388 char *ip6_compressed_string(char *p, const char *addr)
1389 {
1390 int i, j, range;
1391 unsigned char zerolength[8];
1392 int longest = 1;
1393 int colonpos = -1;
1394 u16 word;
1395 u8 hi, lo;
1396 bool needcolon = false;
1397 bool useIPv4;
1398 struct in6_addr in6;
1399
1400 memcpy(&in6, addr, sizeof(struct in6_addr));
1401
1402 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1403
1404 memset(zerolength, 0, sizeof(zerolength));
1405
1406 if (useIPv4)
1407 range = 6;
1408 else
1409 range = 8;
1410
1411 /* find position of longest 0 run */
1412 for (i = 0; i < range; i++) {
1413 for (j = i; j < range; j++) {
1414 if (in6.s6_addr16[j] != 0)
1415 break;
1416 zerolength[i]++;
1417 }
1418 }
1419 for (i = 0; i < range; i++) {
1420 if (zerolength[i] > longest) {
1421 longest = zerolength[i];
1422 colonpos = i;
1423 }
1424 }
1425 if (longest == 1) /* don't compress a single 0 */
1426 colonpos = -1;
1427
1428 /* emit address */
1429 for (i = 0; i < range; i++) {
1430 if (i == colonpos) {
1431 if (needcolon || i == 0)
1432 *p++ = ':';
1433 *p++ = ':';
1434 needcolon = false;
1435 i += longest - 1;
1436 continue;
1437 }
1438 if (needcolon) {
1439 *p++ = ':';
1440 needcolon = false;
1441 }
1442 /* hex u16 without leading 0s */
1443 word = ntohs(in6.s6_addr16[i]);
1444 hi = word >> 8;
1445 lo = word & 0xff;
1446 if (hi) {
1447 if (hi > 0x0f)
1448 p = hex_byte_pack(p, hi);
1449 else
1450 *p++ = hex_asc_lo(hi);
1451 p = hex_byte_pack(p, lo);
1452 }
1453 else if (lo > 0x0f)
1454 p = hex_byte_pack(p, lo);
1455 else
1456 *p++ = hex_asc_lo(lo);
1457 needcolon = true;
1458 }
1459
1460 if (useIPv4) {
1461 if (needcolon)
1462 *p++ = ':';
1463 p = ip4_string(p, &in6.s6_addr[12], "I4");
1464 }
1465 *p = '\0';
1466
1467 return p;
1468 }
1469
1470 static noinline_for_stack
1471 char *ip6_string(char *p, const char *addr, const char *fmt)
1472 {
1473 int i;
1474
1475 for (i = 0; i < 8; i++) {
1476 p = hex_byte_pack(p, *addr++);
1477 p = hex_byte_pack(p, *addr++);
1478 if (fmt[0] == 'I' && i != 7)
1479 *p++ = ':';
1480 }
1481 *p = '\0';
1482
1483 return p;
1484 }
1485
1486 static noinline_for_stack
1487 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1488 struct printf_spec spec, const char *fmt)
1489 {
1490 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1491
1492 if (fmt[0] == 'I' && fmt[2] == 'c')
1493 ip6_compressed_string(ip6_addr, addr);
1494 else
1495 ip6_string(ip6_addr, addr, fmt);
1496
1497 return string_nocheck(buf, end, ip6_addr, spec);
1498 }
1499
1500 static noinline_for_stack
1501 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1502 struct printf_spec spec, const char *fmt)
1503 {
1504 char ip4_addr[sizeof("255.255.255.255")];
1505
1506 ip4_string(ip4_addr, addr, fmt);
1507
1508 return string_nocheck(buf, end, ip4_addr, spec);
1509 }
1510
1511 static noinline_for_stack
1512 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1513 struct printf_spec spec, const char *fmt)
1514 {
1515 bool have_p = false, have_s = false, have_f = false, have_c = false;
1516 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1517 sizeof(":12345") + sizeof("/123456789") +
1518 sizeof("%1234567890")];
1519 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1520 const u8 *addr = (const u8 *) &sa->sin6_addr;
1521 char fmt6[2] = { fmt[0], '6' };
1522 u8 off = 0;
1523
1524 fmt++;
1525 while (isalpha(*++fmt)) {
1526 switch (*fmt) {
1527 case 'p':
1528 have_p = true;
1529 break;
1530 case 'f':
1531 have_f = true;
1532 break;
1533 case 's':
1534 have_s = true;
1535 break;
1536 case 'c':
1537 have_c = true;
1538 break;
1539 }
1540 }
1541
1542 if (have_p || have_s || have_f) {
1543 *p = '[';
1544 off = 1;
1545 }
1546
1547 if (fmt6[0] == 'I' && have_c)
1548 p = ip6_compressed_string(ip6_addr + off, addr);
1549 else
1550 p = ip6_string(ip6_addr + off, addr, fmt6);
1551
1552 if (have_p || have_s || have_f)
1553 *p++ = ']';
1554
1555 if (have_p) {
1556 *p++ = ':';
1557 p = number(p, pend, ntohs(sa->sin6_port), spec);
1558 }
1559 if (have_f) {
1560 *p++ = '/';
1561 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1562 IPV6_FLOWINFO_MASK), spec);
1563 }
1564 if (have_s) {
1565 *p++ = '%';
1566 p = number(p, pend, sa->sin6_scope_id, spec);
1567 }
1568 *p = '\0';
1569
1570 return string_nocheck(buf, end, ip6_addr, spec);
1571 }
1572
1573 static noinline_for_stack
1574 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1575 struct printf_spec spec, const char *fmt)
1576 {
1577 bool have_p = false;
1578 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1579 char *pend = ip4_addr + sizeof(ip4_addr);
1580 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1581 char fmt4[3] = { fmt[0], '4', 0 };
1582
1583 fmt++;
1584 while (isalpha(*++fmt)) {
1585 switch (*fmt) {
1586 case 'p':
1587 have_p = true;
1588 break;
1589 case 'h':
1590 case 'l':
1591 case 'n':
1592 case 'b':
1593 fmt4[2] = *fmt;
1594 break;
1595 }
1596 }
1597
1598 p = ip4_string(ip4_addr, addr, fmt4);
1599 if (have_p) {
1600 *p++ = ':';
1601 p = number(p, pend, ntohs(sa->sin_port), spec);
1602 }
1603 *p = '\0';
1604
1605 return string_nocheck(buf, end, ip4_addr, spec);
1606 }
1607
1608 static noinline_for_stack
1609 char *ip_addr_string(char *buf, char *end, const void *ptr,
1610 struct printf_spec spec, const char *fmt)
1611 {
1612 char *err_fmt_msg;
1613
1614 if (check_pointer(&buf, end, ptr, spec))
1615 return buf;
1616
1617 switch (fmt[1]) {
1618 case '6':
1619 return ip6_addr_string(buf, end, ptr, spec, fmt);
1620 case '4':
1621 return ip4_addr_string(buf, end, ptr, spec, fmt);
1622 case 'S': {
1623 const union {
1624 struct sockaddr raw;
1625 struct sockaddr_in v4;
1626 struct sockaddr_in6 v6;
1627 } *sa = ptr;
1628
1629 switch (sa->raw.sa_family) {
1630 case AF_INET:
1631 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1632 case AF_INET6:
1633 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1634 default:
1635 return error_string(buf, end, "(einval)", spec);
1636 }}
1637 }
1638
1639 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1640 return error_string(buf, end, err_fmt_msg, spec);
1641 }
1642
1643 static noinline_for_stack
1644 char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1645 const char *fmt)
1646 {
1647 bool found = true;
1648 int count = 1;
1649 unsigned int flags = 0;
1650 int len;
1651
1652 if (spec.field_width == 0)
1653 return buf; /* nothing to print */
1654
1655 if (check_pointer(&buf, end, addr, spec))
1656 return buf;
1657
1658 do {
1659 switch (fmt[count++]) {
1660 case 'a':
1661 flags |= ESCAPE_ANY;
1662 break;
1663 case 'c':
1664 flags |= ESCAPE_SPECIAL;
1665 break;
1666 case 'h':
1667 flags |= ESCAPE_HEX;
1668 break;
1669 case 'n':
1670 flags |= ESCAPE_NULL;
1671 break;
1672 case 'o':
1673 flags |= ESCAPE_OCTAL;
1674 break;
1675 case 'p':
1676 flags |= ESCAPE_NP;
1677 break;
1678 case 's':
1679 flags |= ESCAPE_SPACE;
1680 break;
1681 default:
1682 found = false;
1683 break;
1684 }
1685 } while (found);
1686
1687 if (!flags)
1688 flags = ESCAPE_ANY_NP;
1689
1690 len = spec.field_width < 0 ? 1 : spec.field_width;
1691
1692 /*
1693 * string_escape_mem() writes as many characters as it can to
1694 * the given buffer, and returns the total size of the output
1695 * had the buffer been big enough.
1696 */
1697 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1698
1699 return buf;
1700 }
1701
1702 static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1703 struct printf_spec spec, const char *fmt)
1704 {
1705 va_list va;
1706
1707 if (check_pointer(&buf, end, va_fmt, spec))
1708 return buf;
1709
1710 va_copy(va, *va_fmt->va);
1711 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1712 va_end(va);
1713
1714 return buf;
1715 }
1716
1717 static noinline_for_stack
1718 char *uuid_string(char *buf, char *end, const u8 *addr,
1719 struct printf_spec spec, const char *fmt)
1720 {
1721 char uuid[UUID_STRING_LEN + 1];
1722 char *p = uuid;
1723 int i;
1724 const u8 *index = uuid_index;
1725 bool uc = false;
1726
1727 if (check_pointer(&buf, end, addr, spec))
1728 return buf;
1729
1730 switch (*(++fmt)) {
1731 case 'L':
1732 uc = true;
1733 fallthrough;
1734 case 'l':
1735 index = guid_index;
1736 break;
1737 case 'B':
1738 uc = true;
1739 break;
1740 }
1741
1742 for (i = 0; i < 16; i++) {
1743 if (uc)
1744 p = hex_byte_pack_upper(p, addr[index[i]]);
1745 else
1746 p = hex_byte_pack(p, addr[index[i]]);
1747 switch (i) {
1748 case 3:
1749 case 5:
1750 case 7:
1751 case 9:
1752 *p++ = '-';
1753 break;
1754 }
1755 }
1756
1757 *p = 0;
1758
1759 return string_nocheck(buf, end, uuid, spec);
1760 }
1761
1762 static noinline_for_stack
1763 char *netdev_bits(char *buf, char *end, const void *addr,
1764 struct printf_spec spec, const char *fmt)
1765 {
1766 unsigned long long num;
1767 int size;
1768
1769 if (check_pointer(&buf, end, addr, spec))
1770 return buf;
1771
1772 switch (fmt[1]) {
1773 case 'F':
1774 num = *(const netdev_features_t *)addr;
1775 size = sizeof(netdev_features_t);
1776 break;
1777 default:
1778 return error_string(buf, end, "(%pN?)", spec);
1779 }
1780
1781 return special_hex_number(buf, end, num, size);
1782 }
1783
1784 static noinline_for_stack
1785 char *fourcc_string(char *buf, char *end, const u32 *fourcc,
1786 struct printf_spec spec, const char *fmt)
1787 {
1788 char output[sizeof("0123 little-endian (0x01234567)")];
1789 char *p = output;
1790 unsigned int i;
1791 u32 orig, val;
1792
1793 if (fmt[1] != 'c' || fmt[2] != 'c')
1794 return error_string(buf, end, "(%p4?)", spec);
1795
1796 if (check_pointer(&buf, end, fourcc, spec))
1797 return buf;
1798
1799 orig = get_unaligned(fourcc);
1800 val = orig & ~BIT(31);
1801
1802 for (i = 0; i < sizeof(u32); i++) {
1803 unsigned char c = val >> (i * 8);
1804
1805 /* Print non-control ASCII characters as-is, dot otherwise */
1806 *p++ = isascii(c) && isprint(c) ? c : '.';
1807 }
1808
1809 strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
1810 p += strlen(p);
1811
1812 *p++ = ' ';
1813 *p++ = '(';
1814 p = special_hex_number(p, output + sizeof(output) - 2, orig, sizeof(u32));
1815 *p++ = ')';
1816 *p = '\0';
1817
1818 return string(buf, end, output, spec);
1819 }
1820
1821 static noinline_for_stack
1822 char *address_val(char *buf, char *end, const void *addr,
1823 struct printf_spec spec, const char *fmt)
1824 {
1825 unsigned long long num;
1826 int size;
1827
1828 if (check_pointer(&buf, end, addr, spec))
1829 return buf;
1830
1831 switch (fmt[1]) {
1832 case 'd':
1833 num = *(const dma_addr_t *)addr;
1834 size = sizeof(dma_addr_t);
1835 break;
1836 case 'p':
1837 default:
1838 num = *(const phys_addr_t *)addr;
1839 size = sizeof(phys_addr_t);
1840 break;
1841 }
1842
1843 return special_hex_number(buf, end, num, size);
1844 }
1845
1846 static noinline_for_stack
1847 char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1848 {
1849 int year = tm->tm_year + (r ? 0 : 1900);
1850 int mon = tm->tm_mon + (r ? 0 : 1);
1851
1852 buf = number(buf, end, year, default_dec04_spec);
1853 if (buf < end)
1854 *buf = '-';
1855 buf++;
1856
1857 buf = number(buf, end, mon, default_dec02_spec);
1858 if (buf < end)
1859 *buf = '-';
1860 buf++;
1861
1862 return number(buf, end, tm->tm_mday, default_dec02_spec);
1863 }
1864
1865 static noinline_for_stack
1866 char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1867 {
1868 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1869 if (buf < end)
1870 *buf = ':';
1871 buf++;
1872
1873 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1874 if (buf < end)
1875 *buf = ':';
1876 buf++;
1877
1878 return number(buf, end, tm->tm_sec, default_dec02_spec);
1879 }
1880
1881 static noinline_for_stack
1882 char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1883 struct printf_spec spec, const char *fmt)
1884 {
1885 bool have_t = true, have_d = true;
1886 bool raw = false, iso8601_separator = true;
1887 bool found = true;
1888 int count = 2;
1889
1890 if (check_pointer(&buf, end, tm, spec))
1891 return buf;
1892
1893 switch (fmt[count]) {
1894 case 'd':
1895 have_t = false;
1896 count++;
1897 break;
1898 case 't':
1899 have_d = false;
1900 count++;
1901 break;
1902 }
1903
1904 do {
1905 switch (fmt[count++]) {
1906 case 'r':
1907 raw = true;
1908 break;
1909 case 's':
1910 iso8601_separator = false;
1911 break;
1912 default:
1913 found = false;
1914 break;
1915 }
1916 } while (found);
1917
1918 if (have_d)
1919 buf = date_str(buf, end, tm, raw);
1920 if (have_d && have_t) {
1921 if (buf < end)
1922 *buf = iso8601_separator ? 'T' : ' ';
1923 buf++;
1924 }
1925 if (have_t)
1926 buf = time_str(buf, end, tm, raw);
1927
1928 return buf;
1929 }
1930
1931 static noinline_for_stack
1932 char *time64_str(char *buf, char *end, const time64_t time,
1933 struct printf_spec spec, const char *fmt)
1934 {
1935 struct rtc_time rtc_time;
1936 struct tm tm;
1937
1938 time64_to_tm(time, 0, &tm);
1939
1940 rtc_time.tm_sec = tm.tm_sec;
1941 rtc_time.tm_min = tm.tm_min;
1942 rtc_time.tm_hour = tm.tm_hour;
1943 rtc_time.tm_mday = tm.tm_mday;
1944 rtc_time.tm_mon = tm.tm_mon;
1945 rtc_time.tm_year = tm.tm_year;
1946 rtc_time.tm_wday = tm.tm_wday;
1947 rtc_time.tm_yday = tm.tm_yday;
1948
1949 rtc_time.tm_isdst = 0;
1950
1951 return rtc_str(buf, end, &rtc_time, spec, fmt);
1952 }
1953
1954 static noinline_for_stack
1955 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1956 const char *fmt)
1957 {
1958 switch (fmt[1]) {
1959 case 'R':
1960 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
1961 case 'T':
1962 return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
1963 default:
1964 return error_string(buf, end, "(%pt?)", spec);
1965 }
1966 }
1967
1968 static noinline_for_stack
1969 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1970 const char *fmt)
1971 {
1972 if (!IS_ENABLED(CONFIG_HAVE_CLK))
1973 return error_string(buf, end, "(%pC?)", spec);
1974
1975 if (check_pointer(&buf, end, clk, spec))
1976 return buf;
1977
1978 switch (fmt[1]) {
1979 case 'n':
1980 default:
1981 #ifdef CONFIG_COMMON_CLK
1982 return string(buf, end, __clk_get_name(clk), spec);
1983 #else
1984 return ptr_to_id(buf, end, clk, spec);
1985 #endif
1986 }
1987 }
1988
1989 static
1990 char *format_flags(char *buf, char *end, unsigned long flags,
1991 const struct trace_print_flags *names)
1992 {
1993 unsigned long mask;
1994
1995 for ( ; flags && names->name; names++) {
1996 mask = names->mask;
1997 if ((flags & mask) != mask)
1998 continue;
1999
2000 buf = string(buf, end, names->name, default_str_spec);
2001
2002 flags &= ~mask;
2003 if (flags) {
2004 if (buf < end)
2005 *buf = '|';
2006 buf++;
2007 }
2008 }
2009
2010 if (flags)
2011 buf = number(buf, end, flags, default_flag_spec);
2012
2013 return buf;
2014 }
2015
2016 struct page_flags_fields {
2017 int width;
2018 int shift;
2019 int mask;
2020 const struct printf_spec *spec;
2021 const char *name;
2022 };
2023
2024 static const struct page_flags_fields pff[] = {
2025 {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK,
2026 &default_dec_spec, "section"},
2027 {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK,
2028 &default_dec_spec, "node"},
2029 {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK,
2030 &default_dec_spec, "zone"},
2031 {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK,
2032 &default_flag_spec, "lastcpupid"},
2033 {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK,
2034 &default_flag_spec, "kasantag"},
2035 };
2036
2037 static
2038 char *format_page_flags(char *buf, char *end, unsigned long flags)
2039 {
2040 unsigned long main_flags = flags & PAGEFLAGS_MASK;
2041 bool append = false;
2042 int i;
2043
2044 /* Page flags from the main area. */
2045 if (main_flags) {
2046 buf = format_flags(buf, end, main_flags, pageflag_names);
2047 append = true;
2048 }
2049
2050 /* Page flags from the fields area */
2051 for (i = 0; i < ARRAY_SIZE(pff); i++) {
2052 /* Skip undefined fields. */
2053 if (!pff[i].width)
2054 continue;
2055
2056 /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */
2057 if (append) {
2058 if (buf < end)
2059 *buf = '|';
2060 buf++;
2061 }
2062
2063 buf = string(buf, end, pff[i].name, default_str_spec);
2064 if (buf < end)
2065 *buf = '=';
2066 buf++;
2067 buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask,
2068 *pff[i].spec);
2069
2070 append = true;
2071 }
2072
2073 return buf;
2074 }
2075
2076 static noinline_for_stack
2077 char *flags_string(char *buf, char *end, void *flags_ptr,
2078 struct printf_spec spec, const char *fmt)
2079 {
2080 unsigned long flags;
2081 const struct trace_print_flags *names;
2082
2083 if (check_pointer(&buf, end, flags_ptr, spec))
2084 return buf;
2085
2086 switch (fmt[1]) {
2087 case 'p':
2088 return format_page_flags(buf, end, *(unsigned long *)flags_ptr);
2089 case 'v':
2090 flags = *(unsigned long *)flags_ptr;
2091 names = vmaflag_names;
2092 break;
2093 case 'g':
2094 flags = (__force unsigned long)(*(gfp_t *)flags_ptr);
2095 names = gfpflag_names;
2096 break;
2097 default:
2098 return error_string(buf, end, "(%pG?)", spec);
2099 }
2100
2101 return format_flags(buf, end, flags, names);
2102 }
2103
2104 static noinline_for_stack
2105 char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
2106 char *end)
2107 {
2108 int depth;
2109
2110 /* Loop starting from the root node to the current node. */
2111 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
2112 struct fwnode_handle *__fwnode =
2113 fwnode_get_nth_parent(fwnode, depth);
2114
2115 buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
2116 default_str_spec);
2117 buf = string(buf, end, fwnode_get_name(__fwnode),
2118 default_str_spec);
2119
2120 fwnode_handle_put(__fwnode);
2121 }
2122
2123 return buf;
2124 }
2125
2126 static noinline_for_stack
2127 char *device_node_string(char *buf, char *end, struct device_node *dn,
2128 struct printf_spec spec, const char *fmt)
2129 {
2130 char tbuf[sizeof("xxxx") + 1];
2131 const char *p;
2132 int ret;
2133 char *buf_start = buf;
2134 struct property *prop;
2135 bool has_mult, pass;
2136
2137 struct printf_spec str_spec = spec;
2138 str_spec.field_width = -1;
2139
2140 if (fmt[0] != 'F')
2141 return error_string(buf, end, "(%pO?)", spec);
2142
2143 if (!IS_ENABLED(CONFIG_OF))
2144 return error_string(buf, end, "(%pOF?)", spec);
2145
2146 if (check_pointer(&buf, end, dn, spec))
2147 return buf;
2148
2149 /* simple case without anything any more format specifiers */
2150 fmt++;
2151 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
2152 fmt = "f";
2153
2154 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
2155 int precision;
2156 if (pass) {
2157 if (buf < end)
2158 *buf = ':';
2159 buf++;
2160 }
2161
2162 switch (*fmt) {
2163 case 'f': /* full_name */
2164 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
2165 end);
2166 break;
2167 case 'n': /* name */
2168 p = fwnode_get_name(of_fwnode_handle(dn));
2169 precision = str_spec.precision;
2170 str_spec.precision = strchrnul(p, '@') - p;
2171 buf = string(buf, end, p, str_spec);
2172 str_spec.precision = precision;
2173 break;
2174 case 'p': /* phandle */
2175 buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec);
2176 break;
2177 case 'P': /* path-spec */
2178 p = fwnode_get_name(of_fwnode_handle(dn));
2179 if (!p[1])
2180 p = "/";
2181 buf = string(buf, end, p, str_spec);
2182 break;
2183 case 'F': /* flags */
2184 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2185 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2186 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2187 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2188 tbuf[4] = 0;
2189 buf = string_nocheck(buf, end, tbuf, str_spec);
2190 break;
2191 case 'c': /* major compatible string */
2192 ret = of_property_read_string(dn, "compatible", &p);
2193 if (!ret)
2194 buf = string(buf, end, p, str_spec);
2195 break;
2196 case 'C': /* full compatible string */
2197 has_mult = false;
2198 of_property_for_each_string(dn, "compatible", prop, p) {
2199 if (has_mult)
2200 buf = string_nocheck(buf, end, ",", str_spec);
2201 buf = string_nocheck(buf, end, "\"", str_spec);
2202 buf = string(buf, end, p, str_spec);
2203 buf = string_nocheck(buf, end, "\"", str_spec);
2204
2205 has_mult = true;
2206 }
2207 break;
2208 default:
2209 break;
2210 }
2211 }
2212
2213 return widen_string(buf, buf - buf_start, end, spec);
2214 }
2215
2216 static noinline_for_stack
2217 char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2218 struct printf_spec spec, const char *fmt)
2219 {
2220 struct printf_spec str_spec = spec;
2221 char *buf_start = buf;
2222
2223 str_spec.field_width = -1;
2224
2225 if (*fmt != 'w')
2226 return error_string(buf, end, "(%pf?)", spec);
2227
2228 if (check_pointer(&buf, end, fwnode, spec))
2229 return buf;
2230
2231 fmt++;
2232
2233 switch (*fmt) {
2234 case 'P': /* name */
2235 buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
2236 break;
2237 case 'f': /* full_name */
2238 default:
2239 buf = fwnode_full_name_string(fwnode, buf, end);
2240 break;
2241 }
2242
2243 return widen_string(buf, buf - buf_start, end, spec);
2244 }
2245
2246 int __init no_hash_pointers_enable(char *str)
2247 {
2248 if (no_hash_pointers)
2249 return 0;
2250
2251 no_hash_pointers = true;
2252
2253 pr_warn("**********************************************************\n");
2254 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2255 pr_warn("** **\n");
2256 pr_warn("** This system shows unhashed kernel memory addresses **\n");
2257 pr_warn("** via the console, logs, and other interfaces. This **\n");
2258 pr_warn("** might reduce the security of your system. **\n");
2259 pr_warn("** **\n");
2260 pr_warn("** If you see this message and you are not debugging **\n");
2261 pr_warn("** the kernel, report this immediately to your system **\n");
2262 pr_warn("** administrator! **\n");
2263 pr_warn("** **\n");
2264 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2265 pr_warn("**********************************************************\n");
2266
2267 return 0;
2268 }
2269 early_param("no_hash_pointers", no_hash_pointers_enable);
2270
2271 /*
2272 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2273 * by an extra set of alphanumeric characters that are extended format
2274 * specifiers.
2275 *
2276 * Please update scripts/checkpatch.pl when adding/removing conversion
2277 * characters. (Search for "check for vsprintf extension").
2278 *
2279 * Right now we handle:
2280 *
2281 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2282 * - 's' For symbolic direct pointers (or function descriptors) without offset
2283 * - '[Ss]R' as above with __builtin_extract_return_addr() translation
2284 * - 'S[R]b' as above with module build ID (for use in backtraces)
2285 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2286 * %ps and %pS. Be careful when re-using these specifiers.
2287 * - 'B' For backtraced symbolic direct pointers with offset
2288 * - 'Bb' as above with module build ID (for use in backtraces)
2289 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2290 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2291 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2292 * width which must be explicitly specified either as part of the
2293 * format string '%32b[l]' or through '%*b[l]', [l] selects
2294 * range-list format instead of hex format
2295 * - 'M' For a 6-byte MAC address, it prints the address in the
2296 * usual colon-separated hex notation
2297 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2298 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2299 * with a dash-separated hex notation
2300 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
2301 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2302 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2303 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
2304 * [S][pfs]
2305 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2306 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2307 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2308 * IPv6 omits the colons (01020304...0f)
2309 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
2310 * [S][pfs]
2311 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2312 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2313 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2314 * - 'I[6S]c' for IPv6 addresses printed as specified by
2315 * https://tools.ietf.org/html/rfc5952
2316 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2317 * of the following flags (see string_escape_mem() for the
2318 * details):
2319 * a - ESCAPE_ANY
2320 * c - ESCAPE_SPECIAL
2321 * h - ESCAPE_HEX
2322 * n - ESCAPE_NULL
2323 * o - ESCAPE_OCTAL
2324 * p - ESCAPE_NP
2325 * s - ESCAPE_SPACE
2326 * By default ESCAPE_ANY_NP is used.
2327 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2328 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2329 * Options for %pU are:
2330 * b big endian lower case hex (default)
2331 * B big endian UPPER case hex
2332 * l little endian lower case hex
2333 * L little endian UPPER case hex
2334 * big endian output byte order is:
2335 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2336 * little endian output byte order is:
2337 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
2338 * - 'V' For a struct va_format which contains a format string * and va_list *,
2339 * call vsnprintf(->format, *->va_list).
2340 * Implements a "recursive vsnprintf".
2341 * Do not use this feature without some mechanism to verify the
2342 * correctness of the format string and va_list arguments.
2343 * - 'K' For a kernel pointer that should be hidden from unprivileged users.
2344 * Use only for procfs, sysfs and similar files, not printk(); please
2345 * read the documentation (path below) first.
2346 * - 'NF' For a netdev_features_t
2347 * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value.
2348 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2349 * a certain separator (' ' by default):
2350 * C colon
2351 * D dash
2352 * N no separator
2353 * The maximum supported length is 64 bytes of the input. Consider
2354 * to use print_hex_dump() for the larger input.
2355 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2356 * (default assumed to be phys_addr_t, passed by reference)
2357 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2358 * - 'D[234]' Same as 'd' but for a struct file
2359 * - 'g' For block_device name (gendisk + partition number)
2360 * - 't[RT][dt][r][s]' For time and date as represented by:
2361 * R struct rtc_time
2362 * T time64_t
2363 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2364 * (legacy clock framework) of the clock
2365 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2366 * (legacy clock framework) of the clock
2367 * - 'G' For flags to be printed as a collection of symbolic strings that would
2368 * construct the specific value. Supported flags given by option:
2369 * p page flags (see struct page) given as pointer to unsigned long
2370 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2371 * v vma flags (VM_*) given as pointer to unsigned long
2372 * - 'OF[fnpPcCF]' For a device tree object
2373 * Without any optional arguments prints the full_name
2374 * f device node full_name
2375 * n device node name
2376 * p device node phandle
2377 * P device node path spec (name + @unit)
2378 * F device node flags
2379 * c major compatible string
2380 * C full compatible string
2381 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2382 * Without an option prints the full name of the node
2383 * f full name
2384 * P node name, including a possible unit address
2385 * - 'x' For printing the address unmodified. Equivalent to "%lx".
2386 * Please read the documentation (path below) before using!
2387 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2388 * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2389 * or user (u) memory to probe, and:
2390 * s a string, equivalent to "%s" on direct vsnprintf() use
2391 *
2392 * ** When making changes please also update:
2393 * Documentation/core-api/printk-formats.rst
2394 *
2395 * Note: The default behaviour (unadorned %p) is to hash the address,
2396 * rendering it useful as a unique identifier.
2397 */
2398 static noinline_for_stack
2399 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2400 struct printf_spec spec)
2401 {
2402 switch (*fmt) {
2403 case 'S':
2404 case 's':
2405 ptr = dereference_symbol_descriptor(ptr);
2406 fallthrough;
2407 case 'B':
2408 return symbol_string(buf, end, ptr, spec, fmt);
2409 case 'R':
2410 case 'r':
2411 return resource_string(buf, end, ptr, spec, fmt);
2412 case 'h':
2413 return hex_string(buf, end, ptr, spec, fmt);
2414 case 'b':
2415 switch (fmt[1]) {
2416 case 'l':
2417 return bitmap_list_string(buf, end, ptr, spec, fmt);
2418 default:
2419 return bitmap_string(buf, end, ptr, spec, fmt);
2420 }
2421 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2422 case 'm': /* Contiguous: 000102030405 */
2423 /* [mM]F (FDDI) */
2424 /* [mM]R (Reverse order; Bluetooth) */
2425 return mac_address_string(buf, end, ptr, spec, fmt);
2426 case 'I': /* Formatted IP supported
2427 * 4: 1.2.3.4
2428 * 6: 0001:0203:...:0708
2429 * 6c: 1::708 or 1::1.2.3.4
2430 */
2431 case 'i': /* Contiguous:
2432 * 4: 001.002.003.004
2433 * 6: 000102...0f
2434 */
2435 return ip_addr_string(buf, end, ptr, spec, fmt);
2436 case 'E':
2437 return escaped_string(buf, end, ptr, spec, fmt);
2438 case 'U':
2439 return uuid_string(buf, end, ptr, spec, fmt);
2440 case 'V':
2441 return va_format(buf, end, ptr, spec, fmt);
2442 case 'K':
2443 return restricted_pointer(buf, end, ptr, spec);
2444 case 'N':
2445 return netdev_bits(buf, end, ptr, spec, fmt);
2446 case '4':
2447 return fourcc_string(buf, end, ptr, spec, fmt);
2448 case 'a':
2449 return address_val(buf, end, ptr, spec, fmt);
2450 case 'd':
2451 return dentry_name(buf, end, ptr, spec, fmt);
2452 case 't':
2453 return time_and_date(buf, end, ptr, spec, fmt);
2454 case 'C':
2455 return clock(buf, end, ptr, spec, fmt);
2456 case 'D':
2457 return file_dentry_name(buf, end, ptr, spec, fmt);
2458 #ifdef CONFIG_BLOCK
2459 case 'g':
2460 return bdev_name(buf, end, ptr, spec, fmt);
2461 #endif
2462
2463 case 'G':
2464 return flags_string(buf, end, ptr, spec, fmt);
2465 case 'O':
2466 return device_node_string(buf, end, ptr, spec, fmt + 1);
2467 case 'f':
2468 return fwnode_string(buf, end, ptr, spec, fmt + 1);
2469 case 'x':
2470 return pointer_string(buf, end, ptr, spec);
2471 case 'e':
2472 /* %pe with a non-ERR_PTR gets treated as plain %p */
2473 if (!IS_ERR(ptr))
2474 return default_pointer(buf, end, ptr, spec);
2475 return err_ptr(buf, end, ptr, spec);
2476 case 'u':
2477 case 'k':
2478 switch (fmt[1]) {
2479 case 's':
2480 return string(buf, end, ptr, spec);
2481 default:
2482 return error_string(buf, end, "(einval)", spec);
2483 }
2484 default:
2485 return default_pointer(buf, end, ptr, spec);
2486 }
2487 }
2488
2489 /*
2490 * Helper function to decode printf style format.
2491 * Each call decode a token from the format and return the
2492 * number of characters read (or likely the delta where it wants
2493 * to go on the next call).
2494 * The decoded token is returned through the parameters
2495 *
2496 * 'h', 'l', or 'L' for integer fields
2497 * 'z' support added 23/7/1999 S.H.
2498 * 'z' changed to 'Z' --davidm 1/25/99
2499 * 'Z' changed to 'z' --adobriyan 2017-01-25
2500 * 't' added for ptrdiff_t
2501 *
2502 * @fmt: the format string
2503 * @type of the token returned
2504 * @flags: various flags such as +, -, # tokens..
2505 * @field_width: overwritten width
2506 * @base: base of the number (octal, hex, ...)
2507 * @precision: precision of a number
2508 * @qualifier: qualifier of a number (long, size_t, ...)
2509 */
2510 static noinline_for_stack
2511 int format_decode(const char *fmt, struct printf_spec *spec)
2512 {
2513 const char *start = fmt;
2514 char qualifier;
2515
2516 /* we finished early by reading the field width */
2517 if (spec->type == FORMAT_TYPE_WIDTH) {
2518 if (spec->field_width < 0) {
2519 spec->field_width = -spec->field_width;
2520 spec->flags |= LEFT;
2521 }
2522 spec->type = FORMAT_TYPE_NONE;
2523 goto precision;
2524 }
2525
2526 /* we finished early by reading the precision */
2527 if (spec->type == FORMAT_TYPE_PRECISION) {
2528 if (spec->precision < 0)
2529 spec->precision = 0;
2530
2531 spec->type = FORMAT_TYPE_NONE;
2532 goto qualifier;
2533 }
2534
2535 /* By default */
2536 spec->type = FORMAT_TYPE_NONE;
2537
2538 for (; *fmt ; ++fmt) {
2539 if (*fmt == '%')
2540 break;
2541 }
2542
2543 /* Return the current non-format string */
2544 if (fmt != start || !*fmt)
2545 return fmt - start;
2546
2547 /* Process flags */
2548 spec->flags = 0;
2549
2550 while (1) { /* this also skips first '%' */
2551 bool found = true;
2552
2553 ++fmt;
2554
2555 switch (*fmt) {
2556 case '-': spec->flags |= LEFT; break;
2557 case '+': spec->flags |= PLUS; break;
2558 case ' ': spec->flags |= SPACE; break;
2559 case '#': spec->flags |= SPECIAL; break;
2560 case '0': spec->flags |= ZEROPAD; break;
2561 default: found = false;
2562 }
2563
2564 if (!found)
2565 break;
2566 }
2567
2568 /* get field width */
2569 spec->field_width = -1;
2570
2571 if (isdigit(*fmt))
2572 spec->field_width = skip_atoi(&fmt);
2573 else if (*fmt == '*') {
2574 /* it's the next argument */
2575 spec->type = FORMAT_TYPE_WIDTH;
2576 return ++fmt - start;
2577 }
2578
2579 precision:
2580 /* get the precision */
2581 spec->precision = -1;
2582 if (*fmt == '.') {
2583 ++fmt;
2584 if (isdigit(*fmt)) {
2585 spec->precision = skip_atoi(&fmt);
2586 if (spec->precision < 0)
2587 spec->precision = 0;
2588 } else if (*fmt == '*') {
2589 /* it's the next argument */
2590 spec->type = FORMAT_TYPE_PRECISION;
2591 return ++fmt - start;
2592 }
2593 }
2594
2595 qualifier:
2596 /* get the conversion qualifier */
2597 qualifier = 0;
2598 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2599 *fmt == 'z' || *fmt == 't') {
2600 qualifier = *fmt++;
2601 if (unlikely(qualifier == *fmt)) {
2602 if (qualifier == 'l') {
2603 qualifier = 'L';
2604 ++fmt;
2605 } else if (qualifier == 'h') {
2606 qualifier = 'H';
2607 ++fmt;
2608 }
2609 }
2610 }
2611
2612 /* default base */
2613 spec->base = 10;
2614 switch (*fmt) {
2615 case 'c':
2616 spec->type = FORMAT_TYPE_CHAR;
2617 return ++fmt - start;
2618
2619 case 's':
2620 spec->type = FORMAT_TYPE_STR;
2621 return ++fmt - start;
2622
2623 case 'p':
2624 spec->type = FORMAT_TYPE_PTR;
2625 return ++fmt - start;
2626
2627 case '%':
2628 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2629 return ++fmt - start;
2630
2631 /* integer number formats - set up the flags and "break" */
2632 case 'o':
2633 spec->base = 8;
2634 break;
2635
2636 case 'x':
2637 spec->flags |= SMALL;
2638 fallthrough;
2639
2640 case 'X':
2641 spec->base = 16;
2642 break;
2643
2644 case 'd':
2645 case 'i':
2646 spec->flags |= SIGN;
2647 break;
2648 case 'u':
2649 break;
2650
2651 case 'n':
2652 /*
2653 * Since %n poses a greater security risk than
2654 * utility, treat it as any other invalid or
2655 * unsupported format specifier.
2656 */
2657 fallthrough;
2658
2659 default:
2660 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2661 spec->type = FORMAT_TYPE_INVALID;
2662 return fmt - start;
2663 }
2664
2665 if (qualifier == 'L')
2666 spec->type = FORMAT_TYPE_LONG_LONG;
2667 else if (qualifier == 'l') {
2668 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2669 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2670 } else if (qualifier == 'z') {
2671 spec->type = FORMAT_TYPE_SIZE_T;
2672 } else if (qualifier == 't') {
2673 spec->type = FORMAT_TYPE_PTRDIFF;
2674 } else if (qualifier == 'H') {
2675 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2676 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2677 } else if (qualifier == 'h') {
2678 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2679 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2680 } else {
2681 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2682 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2683 }
2684
2685 return ++fmt - start;
2686 }
2687
2688 static void
2689 set_field_width(struct printf_spec *spec, int width)
2690 {
2691 spec->field_width = width;
2692 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2693 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2694 }
2695 }
2696
2697 static void
2698 set_precision(struct printf_spec *spec, int prec)
2699 {
2700 spec->precision = prec;
2701 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2702 spec->precision = clamp(prec, 0, PRECISION_MAX);
2703 }
2704 }
2705
2706 /**
2707 * vsnprintf - Format a string and place it in a buffer
2708 * @buf: The buffer to place the result into
2709 * @size: The size of the buffer, including the trailing null space
2710 * @fmt: The format string to use
2711 * @args: Arguments for the format string
2712 *
2713 * This function generally follows C99 vsnprintf, but has some
2714 * extensions and a few limitations:
2715 *
2716 * - ``%n`` is unsupported
2717 * - ``%p*`` is handled by pointer()
2718 *
2719 * See pointer() or Documentation/core-api/printk-formats.rst for more
2720 * extensive description.
2721 *
2722 * **Please update the documentation in both places when making changes**
2723 *
2724 * The return value is the number of characters which would
2725 * be generated for the given input, excluding the trailing
2726 * '\0', as per ISO C99. If you want to have the exact
2727 * number of characters written into @buf as return value
2728 * (not including the trailing '\0'), use vscnprintf(). If the
2729 * return is greater than or equal to @size, the resulting
2730 * string is truncated.
2731 *
2732 * If you're not already dealing with a va_list consider using snprintf().
2733 */
2734 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2735 {
2736 unsigned long long num;
2737 char *str, *end;
2738 struct printf_spec spec = {0};
2739
2740 /* Reject out-of-range values early. Large positive sizes are
2741 used for unknown buffer sizes. */
2742 if (WARN_ON_ONCE(size > INT_MAX))
2743 return 0;
2744
2745 str = buf;
2746 end = buf + size;
2747
2748 /* Make sure end is always >= buf */
2749 if (end < buf) {
2750 end = ((void *)-1);
2751 size = end - buf;
2752 }
2753
2754 while (*fmt) {
2755 const char *old_fmt = fmt;
2756 int read = format_decode(fmt, &spec);
2757
2758 fmt += read;
2759
2760 switch (spec.type) {
2761 case FORMAT_TYPE_NONE: {
2762 int copy = read;
2763 if (str < end) {
2764 if (copy > end - str)
2765 copy = end - str;
2766 memcpy(str, old_fmt, copy);
2767 }
2768 str += read;
2769 break;
2770 }
2771
2772 case FORMAT_TYPE_WIDTH:
2773 set_field_width(&spec, va_arg(args, int));
2774 break;
2775
2776 case FORMAT_TYPE_PRECISION:
2777 set_precision(&spec, va_arg(args, int));
2778 break;
2779
2780 case FORMAT_TYPE_CHAR: {
2781 char c;
2782
2783 if (!(spec.flags & LEFT)) {
2784 while (--spec.field_width > 0) {
2785 if (str < end)
2786 *str = ' ';
2787 ++str;
2788
2789 }
2790 }
2791 c = (unsigned char) va_arg(args, int);
2792 if (str < end)
2793 *str = c;
2794 ++str;
2795 while (--spec.field_width > 0) {
2796 if (str < end)
2797 *str = ' ';
2798 ++str;
2799 }
2800 break;
2801 }
2802
2803 case FORMAT_TYPE_STR:
2804 str = string(str, end, va_arg(args, char *), spec);
2805 break;
2806
2807 case FORMAT_TYPE_PTR:
2808 str = pointer(fmt, str, end, va_arg(args, void *),
2809 spec);
2810 while (isalnum(*fmt))
2811 fmt++;
2812 break;
2813
2814 case FORMAT_TYPE_PERCENT_CHAR:
2815 if (str < end)
2816 *str = '%';
2817 ++str;
2818 break;
2819
2820 case FORMAT_TYPE_INVALID:
2821 /*
2822 * Presumably the arguments passed gcc's type
2823 * checking, but there is no safe or sane way
2824 * for us to continue parsing the format and
2825 * fetching from the va_list; the remaining
2826 * specifiers and arguments would be out of
2827 * sync.
2828 */
2829 goto out;
2830
2831 default:
2832 switch (spec.type) {
2833 case FORMAT_TYPE_LONG_LONG:
2834 num = va_arg(args, long long);
2835 break;
2836 case FORMAT_TYPE_ULONG:
2837 num = va_arg(args, unsigned long);
2838 break;
2839 case FORMAT_TYPE_LONG:
2840 num = va_arg(args, long);
2841 break;
2842 case FORMAT_TYPE_SIZE_T:
2843 if (spec.flags & SIGN)
2844 num = va_arg(args, ssize_t);
2845 else
2846 num = va_arg(args, size_t);
2847 break;
2848 case FORMAT_TYPE_PTRDIFF:
2849 num = va_arg(args, ptrdiff_t);
2850 break;
2851 case FORMAT_TYPE_UBYTE:
2852 num = (unsigned char) va_arg(args, int);
2853 break;
2854 case FORMAT_TYPE_BYTE:
2855 num = (signed char) va_arg(args, int);
2856 break;
2857 case FORMAT_TYPE_USHORT:
2858 num = (unsigned short) va_arg(args, int);
2859 break;
2860 case FORMAT_TYPE_SHORT:
2861 num = (short) va_arg(args, int);
2862 break;
2863 case FORMAT_TYPE_INT:
2864 num = (int) va_arg(args, int);
2865 break;
2866 default:
2867 num = va_arg(args, unsigned int);
2868 }
2869
2870 str = number(str, end, num, spec);
2871 }
2872 }
2873
2874 out:
2875 if (size > 0) {
2876 if (str < end)
2877 *str = '\0';
2878 else
2879 end[-1] = '\0';
2880 }
2881
2882 /* the trailing null byte doesn't count towards the total */
2883 return str-buf;
2884
2885 }
2886 EXPORT_SYMBOL(vsnprintf);
2887
2888 /**
2889 * vscnprintf - Format a string and place it in a buffer
2890 * @buf: The buffer to place the result into
2891 * @size: The size of the buffer, including the trailing null space
2892 * @fmt: The format string to use
2893 * @args: Arguments for the format string
2894 *
2895 * The return value is the number of characters which have been written into
2896 * the @buf not including the trailing '\0'. If @size is == 0 the function
2897 * returns 0.
2898 *
2899 * If you're not already dealing with a va_list consider using scnprintf().
2900 *
2901 * See the vsnprintf() documentation for format string extensions over C99.
2902 */
2903 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2904 {
2905 int i;
2906
2907 i = vsnprintf(buf, size, fmt, args);
2908
2909 if (likely(i < size))
2910 return i;
2911 if (size != 0)
2912 return size - 1;
2913 return 0;
2914 }
2915 EXPORT_SYMBOL(vscnprintf);
2916
2917 /**
2918 * snprintf - Format a string and place it in a buffer
2919 * @buf: The buffer to place the result into
2920 * @size: The size of the buffer, including the trailing null space
2921 * @fmt: The format string to use
2922 * @...: Arguments for the format string
2923 *
2924 * The return value is the number of characters which would be
2925 * generated for the given input, excluding the trailing null,
2926 * as per ISO C99. If the return is greater than or equal to
2927 * @size, the resulting string is truncated.
2928 *
2929 * See the vsnprintf() documentation for format string extensions over C99.
2930 */
2931 int snprintf(char *buf, size_t size, const char *fmt, ...)
2932 {
2933 va_list args;
2934 int i;
2935
2936 va_start(args, fmt);
2937 i = vsnprintf(buf, size, fmt, args);
2938 va_end(args);
2939
2940 return i;
2941 }
2942 EXPORT_SYMBOL(snprintf);
2943
2944 /**
2945 * scnprintf - Format a string and place it in a buffer
2946 * @buf: The buffer to place the result into
2947 * @size: The size of the buffer, including the trailing null space
2948 * @fmt: The format string to use
2949 * @...: Arguments for the format string
2950 *
2951 * The return value is the number of characters written into @buf not including
2952 * the trailing '\0'. If @size is == 0 the function returns 0.
2953 */
2954
2955 int scnprintf(char *buf, size_t size, const char *fmt, ...)
2956 {
2957 va_list args;
2958 int i;
2959
2960 va_start(args, fmt);
2961 i = vscnprintf(buf, size, fmt, args);
2962 va_end(args);
2963
2964 return i;
2965 }
2966 EXPORT_SYMBOL(scnprintf);
2967
2968 /**
2969 * vsprintf - Format a string and place it in a buffer
2970 * @buf: The buffer to place the result into
2971 * @fmt: The format string to use
2972 * @args: Arguments for the format string
2973 *
2974 * The function returns the number of characters written
2975 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2976 * buffer overflows.
2977 *
2978 * If you're not already dealing with a va_list consider using sprintf().
2979 *
2980 * See the vsnprintf() documentation for format string extensions over C99.
2981 */
2982 int vsprintf(char *buf, const char *fmt, va_list args)
2983 {
2984 return vsnprintf(buf, INT_MAX, fmt, args);
2985 }
2986 EXPORT_SYMBOL(vsprintf);
2987
2988 /**
2989 * sprintf - Format a string and place it in a buffer
2990 * @buf: The buffer to place the result into
2991 * @fmt: The format string to use
2992 * @...: Arguments for the format string
2993 *
2994 * The function returns the number of characters written
2995 * into @buf. Use snprintf() or scnprintf() in order to avoid
2996 * buffer overflows.
2997 *
2998 * See the vsnprintf() documentation for format string extensions over C99.
2999 */
3000 int sprintf(char *buf, const char *fmt, ...)
3001 {
3002 va_list args;
3003 int i;
3004
3005 va_start(args, fmt);
3006 i = vsnprintf(buf, INT_MAX, fmt, args);
3007 va_end(args);
3008
3009 return i;
3010 }
3011 EXPORT_SYMBOL(sprintf);
3012
3013 #ifdef CONFIG_BINARY_PRINTF
3014 /*
3015 * bprintf service:
3016 * vbin_printf() - VA arguments to binary data
3017 * bstr_printf() - Binary data to text string
3018 */
3019
3020 /**
3021 * vbin_printf - Parse a format string and place args' binary value in a buffer
3022 * @bin_buf: The buffer to place args' binary value
3023 * @size: The size of the buffer(by words(32bits), not characters)
3024 * @fmt: The format string to use
3025 * @args: Arguments for the format string
3026 *
3027 * The format follows C99 vsnprintf, except %n is ignored, and its argument
3028 * is skipped.
3029 *
3030 * The return value is the number of words(32bits) which would be generated for
3031 * the given input.
3032 *
3033 * NOTE:
3034 * If the return value is greater than @size, the resulting bin_buf is NOT
3035 * valid for bstr_printf().
3036 */
3037 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
3038 {
3039 struct printf_spec spec = {0};
3040 char *str, *end;
3041 int width;
3042
3043 str = (char *)bin_buf;
3044 end = (char *)(bin_buf + size);
3045
3046 #define save_arg(type) \
3047 ({ \
3048 unsigned long long value; \
3049 if (sizeof(type) == 8) { \
3050 unsigned long long val8; \
3051 str = PTR_ALIGN(str, sizeof(u32)); \
3052 val8 = va_arg(args, unsigned long long); \
3053 if (str + sizeof(type) <= end) { \
3054 *(u32 *)str = *(u32 *)&val8; \
3055 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
3056 } \
3057 value = val8; \
3058 } else { \
3059 unsigned int val4; \
3060 str = PTR_ALIGN(str, sizeof(type)); \
3061 val4 = va_arg(args, int); \
3062 if (str + sizeof(type) <= end) \
3063 *(typeof(type) *)str = (type)(long)val4; \
3064 value = (unsigned long long)val4; \
3065 } \
3066 str += sizeof(type); \
3067 value; \
3068 })
3069
3070 while (*fmt) {
3071 int read = format_decode(fmt, &spec);
3072
3073 fmt += read;
3074
3075 switch (spec.type) {
3076 case FORMAT_TYPE_NONE:
3077 case FORMAT_TYPE_PERCENT_CHAR:
3078 break;
3079 case FORMAT_TYPE_INVALID:
3080 goto out;
3081
3082 case FORMAT_TYPE_WIDTH:
3083 case FORMAT_TYPE_PRECISION:
3084 width = (int)save_arg(int);
3085 /* Pointers may require the width */
3086 if (*fmt == 'p')
3087 set_field_width(&spec, width);
3088 break;
3089
3090 case FORMAT_TYPE_CHAR:
3091 save_arg(char);
3092 break;
3093
3094 case FORMAT_TYPE_STR: {
3095 const char *save_str = va_arg(args, char *);
3096 const char *err_msg;
3097 size_t len;
3098
3099 err_msg = check_pointer_msg(save_str);
3100 if (err_msg)
3101 save_str = err_msg;
3102
3103 len = strlen(save_str) + 1;
3104 if (str + len < end)
3105 memcpy(str, save_str, len);
3106 str += len;
3107 break;
3108 }
3109
3110 case FORMAT_TYPE_PTR:
3111 /* Dereferenced pointers must be done now */
3112 switch (*fmt) {
3113 /* Dereference of functions is still OK */
3114 case 'S':
3115 case 's':
3116 case 'x':
3117 case 'K':
3118 case 'e':
3119 save_arg(void *);
3120 break;
3121 default:
3122 if (!isalnum(*fmt)) {
3123 save_arg(void *);
3124 break;
3125 }
3126 str = pointer(fmt, str, end, va_arg(args, void *),
3127 spec);
3128 if (str + 1 < end)
3129 *str++ = '\0';
3130 else
3131 end[-1] = '\0'; /* Must be nul terminated */
3132 }
3133 /* skip all alphanumeric pointer suffixes */
3134 while (isalnum(*fmt))
3135 fmt++;
3136 break;
3137
3138 default:
3139 switch (spec.type) {
3140
3141 case FORMAT_TYPE_LONG_LONG:
3142 save_arg(long long);
3143 break;
3144 case FORMAT_TYPE_ULONG:
3145 case FORMAT_TYPE_LONG:
3146 save_arg(unsigned long);
3147 break;
3148 case FORMAT_TYPE_SIZE_T:
3149 save_arg(size_t);
3150 break;
3151 case FORMAT_TYPE_PTRDIFF:
3152 save_arg(ptrdiff_t);
3153 break;
3154 case FORMAT_TYPE_UBYTE:
3155 case FORMAT_TYPE_BYTE:
3156 save_arg(char);
3157 break;
3158 case FORMAT_TYPE_USHORT:
3159 case FORMAT_TYPE_SHORT:
3160 save_arg(short);
3161 break;
3162 default:
3163 save_arg(int);
3164 }
3165 }
3166 }
3167
3168 out:
3169 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
3170 #undef save_arg
3171 }
3172 EXPORT_SYMBOL_GPL(vbin_printf);
3173
3174 /**
3175 * bstr_printf - Format a string from binary arguments and place it in a buffer
3176 * @buf: The buffer to place the result into
3177 * @size: The size of the buffer, including the trailing null space
3178 * @fmt: The format string to use
3179 * @bin_buf: Binary arguments for the format string
3180 *
3181 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
3182 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
3183 * a binary buffer that generated by vbin_printf.
3184 *
3185 * The format follows C99 vsnprintf, but has some extensions:
3186 * see vsnprintf comment for details.
3187 *
3188 * The return value is the number of characters which would
3189 * be generated for the given input, excluding the trailing
3190 * '\0', as per ISO C99. If you want to have the exact
3191 * number of characters written into @buf as return value
3192 * (not including the trailing '\0'), use vscnprintf(). If the
3193 * return is greater than or equal to @size, the resulting
3194 * string is truncated.
3195 */
3196 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
3197 {
3198 struct printf_spec spec = {0};
3199 char *str, *end;
3200 const char *args = (const char *)bin_buf;
3201
3202 if (WARN_ON_ONCE(size > INT_MAX))
3203 return 0;
3204
3205 str = buf;
3206 end = buf + size;
3207
3208 #define get_arg(type) \
3209 ({ \
3210 typeof(type) value; \
3211 if (sizeof(type) == 8) { \
3212 args = PTR_ALIGN(args, sizeof(u32)); \
3213 *(u32 *)&value = *(u32 *)args; \
3214 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
3215 } else { \
3216 args = PTR_ALIGN(args, sizeof(type)); \
3217 value = *(typeof(type) *)args; \
3218 } \
3219 args += sizeof(type); \
3220 value; \
3221 })
3222
3223 /* Make sure end is always >= buf */
3224 if (end < buf) {
3225 end = ((void *)-1);
3226 size = end - buf;
3227 }
3228
3229 while (*fmt) {
3230 const char *old_fmt = fmt;
3231 int read = format_decode(fmt, &spec);
3232
3233 fmt += read;
3234
3235 switch (spec.type) {
3236 case FORMAT_TYPE_NONE: {
3237 int copy = read;
3238 if (str < end) {
3239 if (copy > end - str)
3240 copy = end - str;
3241 memcpy(str, old_fmt, copy);
3242 }
3243 str += read;
3244 break;
3245 }
3246
3247 case FORMAT_TYPE_WIDTH:
3248 set_field_width(&spec, get_arg(int));
3249 break;
3250
3251 case FORMAT_TYPE_PRECISION:
3252 set_precision(&spec, get_arg(int));
3253 break;
3254
3255 case FORMAT_TYPE_CHAR: {
3256 char c;
3257
3258 if (!(spec.flags & LEFT)) {
3259 while (--spec.field_width > 0) {
3260 if (str < end)
3261 *str = ' ';
3262 ++str;
3263 }
3264 }
3265 c = (unsigned char) get_arg(char);
3266 if (str < end)
3267 *str = c;
3268 ++str;
3269 while (--spec.field_width > 0) {
3270 if (str < end)
3271 *str = ' ';
3272 ++str;
3273 }
3274 break;
3275 }
3276
3277 case FORMAT_TYPE_STR: {
3278 const char *str_arg = args;
3279 args += strlen(str_arg) + 1;
3280 str = string(str, end, (char *)str_arg, spec);
3281 break;
3282 }
3283
3284 case FORMAT_TYPE_PTR: {
3285 bool process = false;
3286 int copy, len;
3287 /* Non function dereferences were already done */
3288 switch (*fmt) {
3289 case 'S':
3290 case 's':
3291 case 'x':
3292 case 'K':
3293 case 'e':
3294 process = true;
3295 break;
3296 default:
3297 if (!isalnum(*fmt)) {
3298 process = true;
3299 break;
3300 }
3301 /* Pointer dereference was already processed */
3302 if (str < end) {
3303 len = copy = strlen(args);
3304 if (copy > end - str)
3305 copy = end - str;
3306 memcpy(str, args, copy);
3307 str += len;
3308 args += len + 1;
3309 }
3310 }
3311 if (process)
3312 str = pointer(fmt, str, end, get_arg(void *), spec);
3313
3314 while (isalnum(*fmt))
3315 fmt++;
3316 break;
3317 }
3318
3319 case FORMAT_TYPE_PERCENT_CHAR:
3320 if (str < end)
3321 *str = '%';
3322 ++str;
3323 break;
3324
3325 case FORMAT_TYPE_INVALID:
3326 goto out;
3327
3328 default: {
3329 unsigned long long num;
3330
3331 switch (spec.type) {
3332
3333 case FORMAT_TYPE_LONG_LONG:
3334 num = get_arg(long long);
3335 break;
3336 case FORMAT_TYPE_ULONG:
3337 case FORMAT_TYPE_LONG:
3338 num = get_arg(unsigned long);
3339 break;
3340 case FORMAT_TYPE_SIZE_T:
3341 num = get_arg(size_t);
3342 break;
3343 case FORMAT_TYPE_PTRDIFF:
3344 num = get_arg(ptrdiff_t);
3345 break;
3346 case FORMAT_TYPE_UBYTE:
3347 num = get_arg(unsigned char);
3348 break;
3349 case FORMAT_TYPE_BYTE:
3350 num = get_arg(signed char);
3351 break;
3352 case FORMAT_TYPE_USHORT:
3353 num = get_arg(unsigned short);
3354 break;
3355 case FORMAT_TYPE_SHORT:
3356 num = get_arg(short);
3357 break;
3358 case FORMAT_TYPE_UINT:
3359 num = get_arg(unsigned int);
3360 break;
3361 default:
3362 num = get_arg(int);
3363 }
3364
3365 str = number(str, end, num, spec);
3366 } /* default: */
3367 } /* switch(spec.type) */
3368 } /* while(*fmt) */
3369
3370 out:
3371 if (size > 0) {
3372 if (str < end)
3373 *str = '\0';
3374 else
3375 end[-1] = '\0';
3376 }
3377
3378 #undef get_arg
3379
3380 /* the trailing null byte doesn't count towards the total */
3381 return str - buf;
3382 }
3383 EXPORT_SYMBOL_GPL(bstr_printf);
3384
3385 /**
3386 * bprintf - Parse a format string and place args' binary value in a buffer
3387 * @bin_buf: The buffer to place args' binary value
3388 * @size: The size of the buffer(by words(32bits), not characters)
3389 * @fmt: The format string to use
3390 * @...: Arguments for the format string
3391 *
3392 * The function returns the number of words(u32) written
3393 * into @bin_buf.
3394 */
3395 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3396 {
3397 va_list args;
3398 int ret;
3399
3400 va_start(args, fmt);
3401 ret = vbin_printf(bin_buf, size, fmt, args);
3402 va_end(args);
3403
3404 return ret;
3405 }
3406 EXPORT_SYMBOL_GPL(bprintf);
3407
3408 #endif /* CONFIG_BINARY_PRINTF */
3409
3410 /**
3411 * vsscanf - Unformat a buffer into a list of arguments
3412 * @buf: input buffer
3413 * @fmt: format of buffer
3414 * @args: arguments
3415 */
3416 int vsscanf(const char *buf, const char *fmt, va_list args)
3417 {
3418 const char *str = buf;
3419 char *next;
3420 char digit;
3421 int num = 0;
3422 u8 qualifier;
3423 unsigned int base;
3424 union {
3425 long long s;
3426 unsigned long long u;
3427 } val;
3428 s16 field_width;
3429 bool is_sign;
3430
3431 while (*fmt) {
3432 /* skip any white space in format */
3433 /* white space in format matches any amount of
3434 * white space, including none, in the input.
3435 */
3436 if (isspace(*fmt)) {
3437 fmt = skip_spaces(++fmt);
3438 str = skip_spaces(str);
3439 }
3440
3441 /* anything that is not a conversion must match exactly */
3442 if (*fmt != '%' && *fmt) {
3443 if (*fmt++ != *str++)
3444 break;
3445 continue;
3446 }
3447
3448 if (!*fmt)
3449 break;
3450 ++fmt;
3451
3452 /* skip this conversion.
3453 * advance both strings to next white space
3454 */
3455 if (*fmt == '*') {
3456 if (!*str)
3457 break;
3458 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3459 /* '%*[' not yet supported, invalid format */
3460 if (*fmt == '[')
3461 return num;
3462 fmt++;
3463 }
3464 while (!isspace(*str) && *str)
3465 str++;
3466 continue;
3467 }
3468
3469 /* get field width */
3470 field_width = -1;
3471 if (isdigit(*fmt)) {
3472 field_width = skip_atoi(&fmt);
3473 if (field_width <= 0)
3474 break;
3475 }
3476
3477 /* get conversion qualifier */
3478 qualifier = -1;
3479 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
3480 *fmt == 'z') {
3481 qualifier = *fmt++;
3482 if (unlikely(qualifier == *fmt)) {
3483 if (qualifier == 'h') {
3484 qualifier = 'H';
3485 fmt++;
3486 } else if (qualifier == 'l') {
3487 qualifier = 'L';
3488 fmt++;
3489 }
3490 }
3491 }
3492
3493 if (!*fmt)
3494 break;
3495
3496 if (*fmt == 'n') {
3497 /* return number of characters read so far */
3498 *va_arg(args, int *) = str - buf;
3499 ++fmt;
3500 continue;
3501 }
3502
3503 if (!*str)
3504 break;
3505
3506 base = 10;
3507 is_sign = false;
3508
3509 switch (*fmt++) {
3510 case 'c':
3511 {
3512 char *s = (char *)va_arg(args, char*);
3513 if (field_width == -1)
3514 field_width = 1;
3515 do {
3516 *s++ = *str++;
3517 } while (--field_width > 0 && *str);
3518 num++;
3519 }
3520 continue;
3521 case 's':
3522 {
3523 char *s = (char *)va_arg(args, char *);
3524 if (field_width == -1)
3525 field_width = SHRT_MAX;
3526 /* first, skip leading white space in buffer */
3527 str = skip_spaces(str);
3528
3529 /* now copy until next white space */
3530 while (*str && !isspace(*str) && field_width--)
3531 *s++ = *str++;
3532 *s = '\0';
3533 num++;
3534 }
3535 continue;
3536 /*
3537 * Warning: This implementation of the '[' conversion specifier
3538 * deviates from its glibc counterpart in the following ways:
3539 * (1) It does NOT support ranges i.e. '-' is NOT a special
3540 * character
3541 * (2) It cannot match the closing bracket ']' itself
3542 * (3) A field width is required
3543 * (4) '%*[' (discard matching input) is currently not supported
3544 *
3545 * Example usage:
3546 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3547 * buf1, buf2, buf3);
3548 * if (ret < 3)
3549 * // etc..
3550 */
3551 case '[':
3552 {
3553 char *s = (char *)va_arg(args, char *);
3554 DECLARE_BITMAP(set, 256) = {0};
3555 unsigned int len = 0;
3556 bool negate = (*fmt == '^');
3557
3558 /* field width is required */
3559 if (field_width == -1)
3560 return num;
3561
3562 if (negate)
3563 ++fmt;
3564
3565 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3566 set_bit((u8)*fmt, set);
3567
3568 /* no ']' or no character set found */
3569 if (!*fmt || !len)
3570 return num;
3571 ++fmt;
3572
3573 if (negate) {
3574 bitmap_complement(set, set, 256);
3575 /* exclude null '\0' byte */
3576 clear_bit(0, set);
3577 }
3578
3579 /* match must be non-empty */
3580 if (!test_bit((u8)*str, set))
3581 return num;
3582
3583 while (test_bit((u8)*str, set) && field_width--)
3584 *s++ = *str++;
3585 *s = '\0';
3586 ++num;
3587 }
3588 continue;
3589 case 'o':
3590 base = 8;
3591 break;
3592 case 'x':
3593 case 'X':
3594 base = 16;
3595 break;
3596 case 'i':
3597 base = 0;
3598 fallthrough;
3599 case 'd':
3600 is_sign = true;
3601 fallthrough;
3602 case 'u':
3603 break;
3604 case '%':
3605 /* looking for '%' in str */
3606 if (*str++ != '%')
3607 return num;
3608 continue;
3609 default:
3610 /* invalid format; stop here */
3611 return num;
3612 }
3613
3614 /* have some sort of integer conversion.
3615 * first, skip white space in buffer.
3616 */
3617 str = skip_spaces(str);
3618
3619 digit = *str;
3620 if (is_sign && digit == '-') {
3621 if (field_width == 1)
3622 break;
3623
3624 digit = *(str + 1);
3625 }
3626
3627 if (!digit
3628 || (base == 16 && !isxdigit(digit))
3629 || (base == 10 && !isdigit(digit))
3630 || (base == 8 && (!isdigit(digit) || digit > '7'))
3631 || (base == 0 && !isdigit(digit)))
3632 break;
3633
3634 if (is_sign)
3635 val.s = simple_strntoll(str,
3636 field_width >= 0 ? field_width : INT_MAX,
3637 &next, base);
3638 else
3639 val.u = simple_strntoull(str,
3640 field_width >= 0 ? field_width : INT_MAX,
3641 &next, base);
3642
3643 switch (qualifier) {
3644 case 'H': /* that's 'hh' in format */
3645 if (is_sign)
3646 *va_arg(args, signed char *) = val.s;
3647 else
3648 *va_arg(args, unsigned char *) = val.u;
3649 break;
3650 case 'h':
3651 if (is_sign)
3652 *va_arg(args, short *) = val.s;
3653 else
3654 *va_arg(args, unsigned short *) = val.u;
3655 break;
3656 case 'l':
3657 if (is_sign)
3658 *va_arg(args, long *) = val.s;
3659 else
3660 *va_arg(args, unsigned long *) = val.u;
3661 break;
3662 case 'L':
3663 if (is_sign)
3664 *va_arg(args, long long *) = val.s;
3665 else
3666 *va_arg(args, unsigned long long *) = val.u;
3667 break;
3668 case 'z':
3669 *va_arg(args, size_t *) = val.u;
3670 break;
3671 default:
3672 if (is_sign)
3673 *va_arg(args, int *) = val.s;
3674 else
3675 *va_arg(args, unsigned int *) = val.u;
3676 break;
3677 }
3678 num++;
3679
3680 if (!next)
3681 break;
3682 str = next;
3683 }
3684
3685 return num;
3686 }
3687 EXPORT_SYMBOL(vsscanf);
3688
3689 /**
3690 * sscanf - Unformat a buffer into a list of arguments
3691 * @buf: input buffer
3692 * @fmt: formatting of buffer
3693 * @...: resulting arguments
3694 */
3695 int sscanf(const char *buf, const char *fmt, ...)
3696 {
3697 va_list args;
3698 int i;
3699
3700 va_start(args, fmt);
3701 i = vsscanf(buf, fmt, args);
3702 va_end(args);
3703
3704 return i;
3705 }
3706 EXPORT_SYMBOL(sscanf);