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