4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
20 #include <linux/build_bug.h>
21 #include <linux/clk.h>
22 #include <linux/clk-provider.h>
23 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/ctype.h>
27 #include <linux/kernel.h>
28 #include <linux/kallsyms.h>
29 #include <linux/math64.h>
30 #include <linux/uaccess.h>
31 #include <linux/ioport.h>
32 #include <linux/dcache.h>
33 #include <linux/cred.h>
34 #include <linux/rtc.h>
35 #include <linux/uuid.h>
37 #include <net/addrconf.h>
38 #include <linux/siphash.h>
39 #include <linux/compiler.h>
41 #include <linux/blkdev.h>
44 #include "../mm/internal.h" /* For the trace_print_flags arrays */
46 #include <asm/page.h> /* for PAGE_SIZE */
47 #include <asm/byteorder.h> /* cpu_to_le16 */
49 #include <linux/string_helpers.h>
53 * simple_strtoull - convert a string to an unsigned long long
54 * @cp: The start of the string
55 * @endp: A pointer to the end of the parsed string will be placed here
56 * @base: The number base to use
58 * This function is obsolete. Please use kstrtoull instead.
60 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
62 unsigned long long result;
65 cp = _parse_integer_fixup_radix(cp, &base);
66 rv = _parse_integer(cp, base, &result);
68 cp += (rv & ~KSTRTOX_OVERFLOW);
75 EXPORT_SYMBOL(simple_strtoull);
78 * simple_strtoul - convert a string to an unsigned long
79 * @cp: The start of the string
80 * @endp: A pointer to the end of the parsed string will be placed here
81 * @base: The number base to use
83 * This function is obsolete. Please use kstrtoul instead.
85 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
87 return simple_strtoull(cp, endp, base);
89 EXPORT_SYMBOL(simple_strtoul);
92 * simple_strtol - convert a string to a signed long
93 * @cp: The start of the string
94 * @endp: A pointer to the end of the parsed string will be placed here
95 * @base: The number base to use
97 * This function is obsolete. Please use kstrtol instead.
99 long simple_strtol(const char *cp, char **endp, unsigned int base)
102 return -simple_strtoul(cp + 1, endp, base);
104 return simple_strtoul(cp, endp, base);
106 EXPORT_SYMBOL(simple_strtol);
109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use
114 * This function is obsolete. Please use kstrtoll instead.
116 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
119 return -simple_strtoull(cp + 1, endp, base);
121 return simple_strtoull(cp, endp, base);
123 EXPORT_SYMBOL(simple_strtoll);
125 static noinline_for_stack
126 int skip_atoi(const char **s)
131 i = i*10 + *((*s)++) - '0';
132 } while (isdigit(**s));
138 * Decimal conversion is by far the most typical, and is used for
139 * /proc and /sys data. This directly impacts e.g. top performance
140 * with many processes running. We optimize it for speed by emitting
141 * two characters at a time, using a 200 byte lookup table. This
142 * roughly halves the number of multiplications compared to computing
143 * the digits one at a time. Implementation strongly inspired by the
144 * previous version, which in turn used ideas described at
145 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
146 * from the author, Douglas W. Jones).
148 * It turns out there is precisely one 26 bit fixed-point
149 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
150 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
151 * range happens to be somewhat larger (x <= 1073741898), but that's
152 * irrelevant for our purpose.
154 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
155 * need a 32x32->64 bit multiply, so we simply use the same constant.
157 * For dividing a number in the range [100, 10^4-1] by 100, there are
158 * several options. The simplest is (x * 0x147b) >> 19, which is valid
159 * for all x <= 43698.
162 static const u16 decpair[100] = {
163 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
164 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
165 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
166 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
167 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
168 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
169 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
170 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
171 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
172 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
173 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
178 * This will print a single '0' even if r == 0, since we would
179 * immediately jump to out_r where two 0s would be written but only
180 * one of them accounted for in buf. This is needed by ip4_string
181 * below. All other callers pass a non-zero value of r.
183 static noinline_for_stack
184 char *put_dec_trunc8(char *buf, unsigned r)
192 /* 100 <= r < 10^8 */
193 q = (r * (u64)0x28f5c29) >> 32;
194 *((u16 *)buf) = decpair[r - 100*q];
201 /* 100 <= q < 10^6 */
202 r = (q * (u64)0x28f5c29) >> 32;
203 *((u16 *)buf) = decpair[q - 100*r];
210 /* 100 <= r < 10^4 */
211 q = (r * 0x147b) >> 19;
212 *((u16 *)buf) = decpair[r - 100*q];
219 *((u16 *)buf) = decpair[r];
220 buf += r < 10 ? 1 : 2;
224 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
225 static noinline_for_stack
226 char *put_dec_full8(char *buf, unsigned r)
231 q = (r * (u64)0x28f5c29) >> 32;
232 *((u16 *)buf) = decpair[r - 100*q];
236 r = (q * (u64)0x28f5c29) >> 32;
237 *((u16 *)buf) = decpair[q - 100*r];
241 q = (r * 0x147b) >> 19;
242 *((u16 *)buf) = decpair[r - 100*q];
246 *((u16 *)buf) = decpair[q];
251 static noinline_for_stack
252 char *put_dec(char *buf, unsigned long long n)
254 if (n >= 100*1000*1000)
255 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
256 /* 1 <= n <= 1.6e11 */
257 if (n >= 100*1000*1000)
258 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
260 return put_dec_trunc8(buf, n);
263 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
266 put_dec_full4(char *buf, unsigned r)
271 q = (r * 0x147b) >> 19;
272 *((u16 *)buf) = decpair[r - 100*q];
275 *((u16 *)buf) = decpair[q];
279 * Call put_dec_full4 on x % 10000, return x / 10000.
280 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
281 * holds for all x < 1,128,869,999. The largest value this
282 * helper will ever be asked to convert is 1,125,520,955.
283 * (second call in the put_dec code, assuming n is all-ones).
285 static noinline_for_stack
286 unsigned put_dec_helper4(char *buf, unsigned x)
288 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
290 put_dec_full4(buf, x - q * 10000);
294 /* Based on code by Douglas W. Jones found at
295 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
296 * (with permission from the author).
297 * Performs no 64-bit division and hence should be fast on 32-bit machines.
300 char *put_dec(char *buf, unsigned long long n)
302 uint32_t d3, d2, d1, q, h;
304 if (n < 100*1000*1000)
305 return put_dec_trunc8(buf, n);
307 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
310 d3 = (h >> 16); /* implicit "& 0xffff" */
312 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
313 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
314 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
315 q = put_dec_helper4(buf, q);
317 q += 7671 * d3 + 9496 * d2 + 6 * d1;
318 q = put_dec_helper4(buf+4, q);
320 q += 4749 * d3 + 42 * d2;
321 q = put_dec_helper4(buf+8, q);
326 buf = put_dec_trunc8(buf, q);
327 else while (buf[-1] == '0')
336 * Convert passed number to decimal string.
337 * Returns the length of string. On buffer overflow, returns 0.
339 * If speed is not important, use snprintf(). It's easy to read the code.
341 int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
343 /* put_dec requires 2-byte alignment of the buffer. */
344 char tmp[sizeof(num) * 3] __aligned(2);
347 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
352 len = put_dec(tmp, num) - tmp;
355 if (len > size || width > size)
360 for (idx = 0; idx < width; idx++)
366 for (idx = 0; idx < len; ++idx)
367 buf[idx + width] = tmp[len - idx - 1];
372 #define SIGN 1 /* unsigned/signed, must be 1 */
373 #define LEFT 2 /* left justified */
374 #define PLUS 4 /* show plus */
375 #define SPACE 8 /* space if plus */
376 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
377 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
378 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
381 FORMAT_TYPE_NONE, /* Just a string part */
383 FORMAT_TYPE_PRECISION,
387 FORMAT_TYPE_PERCENT_CHAR,
389 FORMAT_TYPE_LONG_LONG,
403 unsigned int type:8; /* format_type enum */
404 signed int field_width:24; /* width of output field */
405 unsigned int flags:8; /* flags to number() */
406 unsigned int base:8; /* number base, 8, 10 or 16 only */
407 signed int precision:16; /* # of digits/chars */
409 static_assert(sizeof(struct printf_spec) == 8);
411 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
412 #define PRECISION_MAX ((1 << 15) - 1)
414 static noinline_for_stack
415 char *number(char *buf, char *end, unsigned long long num,
416 struct printf_spec spec)
418 /* put_dec requires 2-byte alignment of the buffer. */
419 char tmp[3 * sizeof(num)] __aligned(2);
422 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
424 bool is_zero = num == 0LL;
425 int field_width = spec.field_width;
426 int precision = spec.precision;
428 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
429 * produces same digits or (maybe lowercased) letters */
430 locase = (spec.flags & SMALL);
431 if (spec.flags & LEFT)
432 spec.flags &= ~ZEROPAD;
434 if (spec.flags & SIGN) {
435 if ((signed long long)num < 0) {
437 num = -(signed long long)num;
439 } else if (spec.flags & PLUS) {
442 } else if (spec.flags & SPACE) {
454 /* generate full string in tmp[], in reverse order */
457 tmp[i++] = hex_asc_upper[num] | locase;
458 else if (spec.base != 10) { /* 8 or 16 */
459 int mask = spec.base - 1;
465 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
468 } else { /* base 10 */
469 i = put_dec(tmp, num) - tmp;
472 /* printing 100 using %2d gives "100", not "00" */
475 /* leading space padding */
476 field_width -= precision;
477 if (!(spec.flags & (ZEROPAD | LEFT))) {
478 while (--field_width >= 0) {
490 /* "0x" / "0" prefix */
492 if (spec.base == 16 || !is_zero) {
497 if (spec.base == 16) {
499 *buf = ('X' | locase);
503 /* zero or space padding */
504 if (!(spec.flags & LEFT)) {
505 char c = ' ' + (spec.flags & ZEROPAD);
506 BUILD_BUG_ON(' ' + ZEROPAD != '0');
507 while (--field_width >= 0) {
513 /* hmm even more zero padding? */
514 while (i <= --precision) {
519 /* actual digits of result */
525 /* trailing space padding */
526 while (--field_width >= 0) {
535 static noinline_for_stack
536 char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
538 struct printf_spec spec;
540 spec.type = FORMAT_TYPE_PTR;
541 spec.field_width = 2 + 2 * size; /* 0x + hex */
542 spec.flags = SPECIAL | SMALL | ZEROPAD;
546 return number(buf, end, num, spec);
549 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
552 if (buf >= end) /* nowhere to put anything */
555 if (size <= spaces) {
556 memset(buf, ' ', size);
560 if (len > size - spaces)
562 memmove(buf + spaces, buf, len);
564 memset(buf, ' ', spaces);
568 * Handle field width padding for a string.
569 * @buf: current buffer position
570 * @n: length of string
571 * @end: end of output buffer
572 * @spec: for field width and flags
573 * Returns: new buffer position after padding.
575 static noinline_for_stack
576 char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
580 if (likely(n >= spec.field_width))
582 /* we want to pad the sucker */
583 spaces = spec.field_width - n;
584 if (!(spec.flags & LEFT)) {
585 move_right(buf - n, end, n, spaces);
596 static noinline_for_stack
597 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
600 size_t lim = spec.precision;
602 if ((unsigned long)s < PAGE_SIZE)
614 return widen_string(buf, len, end, spec);
617 static noinline_for_stack
618 char *pointer_string(char *buf, char *end, const void *ptr,
619 struct printf_spec spec)
623 if (spec.field_width == -1) {
624 spec.field_width = 2 * sizeof(ptr);
625 spec.flags |= ZEROPAD;
628 return number(buf, end, (unsigned long int)ptr, spec);
631 /* Make pointers available for printing early in the boot sequence. */
632 static int debug_boot_weak_hash __ro_after_init;
634 static int __init debug_boot_weak_hash_enable(char *str)
636 debug_boot_weak_hash = 1;
637 pr_info("debug_boot_weak_hash enabled\n");
640 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
642 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
643 static siphash_key_t ptr_key __read_mostly;
645 static void enable_ptr_key_workfn(struct work_struct *work)
647 get_random_bytes(&ptr_key, sizeof(ptr_key));
648 /* Needs to run from preemptible context */
649 static_branch_disable(¬_filled_random_ptr_key);
652 static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
654 static void fill_random_ptr_key(struct random_ready_callback *unused)
656 /* This may be in an interrupt handler. */
657 queue_work(system_unbound_wq, &enable_ptr_key_work);
660 static struct random_ready_callback random_ready = {
661 .func = fill_random_ptr_key
664 static int __init initialize_ptr_random(void)
666 int key_size = sizeof(ptr_key);
669 /* Use hw RNG if available. */
670 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
671 static_branch_disable(¬_filled_random_ptr_key);
675 ret = add_random_ready_callback(&random_ready);
678 } else if (ret == -EALREADY) {
679 /* This is in preemptible context */
680 enable_ptr_key_workfn(&enable_ptr_key_work);
686 early_initcall(initialize_ptr_random);
688 /* Maps a pointer to a 32 bit unique identifier. */
689 static char *ptr_to_id(char *buf, char *end, const void *ptr,
690 struct printf_spec spec)
692 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
693 unsigned long hashval;
695 /* When debugging early boot use non-cryptographically secure hash. */
696 if (unlikely(debug_boot_weak_hash)) {
697 hashval = hash_long((unsigned long)ptr, 32);
698 return pointer_string(buf, end, (const void *)hashval, spec);
701 if (static_branch_unlikely(¬_filled_random_ptr_key)) {
702 spec.field_width = 2 * sizeof(ptr);
703 /* string length must be less than default_width */
704 return string(buf, end, str, spec);
708 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
710 * Mask off the first 32 bits, this makes explicit that we have
711 * modified the address (and 32 bits is plenty for a unique ID).
713 hashval = hashval & 0xffffffff;
715 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
717 return pointer_string(buf, end, (const void *)hashval, spec);
720 static noinline_for_stack
721 char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
724 const char *array[4], *s;
725 const struct dentry *p;
730 case '2': case '3': case '4':
731 depth = fmt[1] - '0';
738 for (i = 0; i < depth; i++, d = p) {
739 p = READ_ONCE(d->d_parent);
740 array[i] = READ_ONCE(d->d_name.name);
749 for (n = 0; n != spec.precision; n++, buf++) {
761 return widen_string(buf, n, end, spec);
765 static noinline_for_stack
766 char *bdev_name(char *buf, char *end, struct block_device *bdev,
767 struct printf_spec spec, const char *fmt)
769 struct gendisk *hd = bdev->bd_disk;
771 buf = string(buf, end, hd->disk_name, spec);
772 if (bdev->bd_part->partno) {
773 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
778 buf = number(buf, end, bdev->bd_part->partno, spec);
784 static noinline_for_stack
785 char *symbol_string(char *buf, char *end, void *ptr,
786 struct printf_spec spec, const char *fmt)
789 #ifdef CONFIG_KALLSYMS
790 char sym[KSYM_SYMBOL_LEN];
794 ptr = __builtin_extract_return_addr(ptr);
795 value = (unsigned long)ptr;
797 #ifdef CONFIG_KALLSYMS
799 sprint_backtrace(sym, value);
800 else if (*fmt != 'f' && *fmt != 's')
801 sprint_symbol(sym, value);
803 sprint_symbol_no_offset(sym, value);
805 return string(buf, end, sym, spec);
807 return special_hex_number(buf, end, value, sizeof(void *));
811 static const struct printf_spec default_str_spec = {
816 static const struct printf_spec default_flag_spec = {
819 .flags = SPECIAL | SMALL,
822 static const struct printf_spec default_dec_spec = {
827 static const struct printf_spec default_dec02_spec = {
834 static const struct printf_spec default_dec04_spec = {
841 static noinline_for_stack
842 char *resource_string(char *buf, char *end, struct resource *res,
843 struct printf_spec spec, const char *fmt)
845 #ifndef IO_RSRC_PRINTK_SIZE
846 #define IO_RSRC_PRINTK_SIZE 6
849 #ifndef MEM_RSRC_PRINTK_SIZE
850 #define MEM_RSRC_PRINTK_SIZE 10
852 static const struct printf_spec io_spec = {
854 .field_width = IO_RSRC_PRINTK_SIZE,
856 .flags = SPECIAL | SMALL | ZEROPAD,
858 static const struct printf_spec mem_spec = {
860 .field_width = MEM_RSRC_PRINTK_SIZE,
862 .flags = SPECIAL | SMALL | ZEROPAD,
864 static const struct printf_spec bus_spec = {
868 .flags = SMALL | ZEROPAD,
870 static const struct printf_spec str_spec = {
876 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
877 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
878 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
879 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
880 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
881 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
882 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
883 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
885 char *p = sym, *pend = sym + sizeof(sym);
886 int decode = (fmt[0] == 'R') ? 1 : 0;
887 const struct printf_spec *specp;
890 if (res->flags & IORESOURCE_IO) {
891 p = string(p, pend, "io ", str_spec);
893 } else if (res->flags & IORESOURCE_MEM) {
894 p = string(p, pend, "mem ", str_spec);
896 } else if (res->flags & IORESOURCE_IRQ) {
897 p = string(p, pend, "irq ", str_spec);
898 specp = &default_dec_spec;
899 } else if (res->flags & IORESOURCE_DMA) {
900 p = string(p, pend, "dma ", str_spec);
901 specp = &default_dec_spec;
902 } else if (res->flags & IORESOURCE_BUS) {
903 p = string(p, pend, "bus ", str_spec);
906 p = string(p, pend, "??? ", str_spec);
910 if (decode && res->flags & IORESOURCE_UNSET) {
911 p = string(p, pend, "size ", str_spec);
912 p = number(p, pend, resource_size(res), *specp);
914 p = number(p, pend, res->start, *specp);
915 if (res->start != res->end) {
917 p = number(p, pend, res->end, *specp);
921 if (res->flags & IORESOURCE_MEM_64)
922 p = string(p, pend, " 64bit", str_spec);
923 if (res->flags & IORESOURCE_PREFETCH)
924 p = string(p, pend, " pref", str_spec);
925 if (res->flags & IORESOURCE_WINDOW)
926 p = string(p, pend, " window", str_spec);
927 if (res->flags & IORESOURCE_DISABLED)
928 p = string(p, pend, " disabled", str_spec);
930 p = string(p, pend, " flags ", str_spec);
931 p = number(p, pend, res->flags, default_flag_spec);
936 return string(buf, end, sym, spec);
939 static noinline_for_stack
940 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
943 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
944 negative value, fallback to the default */
947 if (spec.field_width == 0)
948 /* nothing to print */
951 if (ZERO_OR_NULL_PTR(addr))
953 return string(buf, end, NULL, spec);
970 if (spec.field_width > 0)
971 len = min_t(int, spec.field_width, 64);
973 for (i = 0; i < len; ++i) {
975 *buf = hex_asc_hi(addr[i]);
978 *buf = hex_asc_lo(addr[i]);
981 if (separator && i != len - 1) {
991 static noinline_for_stack
992 char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
993 struct printf_spec spec, const char *fmt)
995 const int CHUNKSZ = 32;
996 int nr_bits = max_t(int, spec.field_width, 0);
1000 /* reused to print numbers */
1001 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1003 chunksz = nr_bits & (CHUNKSZ - 1);
1007 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1008 for (; i >= 0; i -= CHUNKSZ) {
1012 chunkmask = ((1ULL << chunksz) - 1);
1013 word = i / BITS_PER_LONG;
1014 bit = i % BITS_PER_LONG;
1015 val = (bitmap[word] >> bit) & chunkmask;
1024 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1025 buf = number(buf, end, val, spec);
1032 static noinline_for_stack
1033 char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1034 struct printf_spec spec, const char *fmt)
1036 int nr_bits = max_t(int, spec.field_width, 0);
1037 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1038 int cur, rbot, rtop;
1041 rbot = cur = find_first_bit(bitmap, nr_bits);
1042 while (cur < nr_bits) {
1044 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1045 if (cur < nr_bits && cur <= rtop + 1)
1055 buf = number(buf, end, rbot, default_dec_spec);
1061 buf = number(buf, end, rtop, default_dec_spec);
1069 static noinline_for_stack
1070 char *mac_address_string(char *buf, char *end, u8 *addr,
1071 struct printf_spec spec, const char *fmt)
1073 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1077 bool reversed = false;
1093 for (i = 0; i < 6; i++) {
1095 p = hex_byte_pack(p, addr[5 - i]);
1097 p = hex_byte_pack(p, addr[i]);
1099 if (fmt[0] == 'M' && i != 5)
1104 return string(buf, end, mac_addr, spec);
1107 static noinline_for_stack
1108 char *ip4_string(char *p, const u8 *addr, const char *fmt)
1111 bool leading_zeros = (fmt[0] == 'i');
1136 for (i = 0; i < 4; i++) {
1137 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
1138 int digits = put_dec_trunc8(temp, addr[index]) - temp;
1139 if (leading_zeros) {
1145 /* reverse the digits in the quad */
1147 *p++ = temp[digits];
1157 static noinline_for_stack
1158 char *ip6_compressed_string(char *p, const char *addr)
1161 unsigned char zerolength[8];
1166 bool needcolon = false;
1168 struct in6_addr in6;
1170 memcpy(&in6, addr, sizeof(struct in6_addr));
1172 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1174 memset(zerolength, 0, sizeof(zerolength));
1181 /* find position of longest 0 run */
1182 for (i = 0; i < range; i++) {
1183 for (j = i; j < range; j++) {
1184 if (in6.s6_addr16[j] != 0)
1189 for (i = 0; i < range; i++) {
1190 if (zerolength[i] > longest) {
1191 longest = zerolength[i];
1195 if (longest == 1) /* don't compress a single 0 */
1199 for (i = 0; i < range; i++) {
1200 if (i == colonpos) {
1201 if (needcolon || i == 0)
1212 /* hex u16 without leading 0s */
1213 word = ntohs(in6.s6_addr16[i]);
1218 p = hex_byte_pack(p, hi);
1220 *p++ = hex_asc_lo(hi);
1221 p = hex_byte_pack(p, lo);
1224 p = hex_byte_pack(p, lo);
1226 *p++ = hex_asc_lo(lo);
1233 p = ip4_string(p, &in6.s6_addr[12], "I4");
1240 static noinline_for_stack
1241 char *ip6_string(char *p, const char *addr, const char *fmt)
1245 for (i = 0; i < 8; i++) {
1246 p = hex_byte_pack(p, *addr++);
1247 p = hex_byte_pack(p, *addr++);
1248 if (fmt[0] == 'I' && i != 7)
1256 static noinline_for_stack
1257 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1258 struct printf_spec spec, const char *fmt)
1260 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1262 if (fmt[0] == 'I' && fmt[2] == 'c')
1263 ip6_compressed_string(ip6_addr, addr);
1265 ip6_string(ip6_addr, addr, fmt);
1267 return string(buf, end, ip6_addr, spec);
1270 static noinline_for_stack
1271 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1272 struct printf_spec spec, const char *fmt)
1274 char ip4_addr[sizeof("255.255.255.255")];
1276 ip4_string(ip4_addr, addr, fmt);
1278 return string(buf, end, ip4_addr, spec);
1281 static noinline_for_stack
1282 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1283 struct printf_spec spec, const char *fmt)
1285 bool have_p = false, have_s = false, have_f = false, have_c = false;
1286 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1287 sizeof(":12345") + sizeof("/123456789") +
1288 sizeof("%1234567890")];
1289 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1290 const u8 *addr = (const u8 *) &sa->sin6_addr;
1291 char fmt6[2] = { fmt[0], '6' };
1295 while (isalpha(*++fmt)) {
1312 if (have_p || have_s || have_f) {
1317 if (fmt6[0] == 'I' && have_c)
1318 p = ip6_compressed_string(ip6_addr + off, addr);
1320 p = ip6_string(ip6_addr + off, addr, fmt6);
1322 if (have_p || have_s || have_f)
1327 p = number(p, pend, ntohs(sa->sin6_port), spec);
1331 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1332 IPV6_FLOWINFO_MASK), spec);
1336 p = number(p, pend, sa->sin6_scope_id, spec);
1340 return string(buf, end, ip6_addr, spec);
1343 static noinline_for_stack
1344 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1345 struct printf_spec spec, const char *fmt)
1347 bool have_p = false;
1348 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1349 char *pend = ip4_addr + sizeof(ip4_addr);
1350 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1351 char fmt4[3] = { fmt[0], '4', 0 };
1354 while (isalpha(*++fmt)) {
1368 p = ip4_string(ip4_addr, addr, fmt4);
1371 p = number(p, pend, ntohs(sa->sin_port), spec);
1375 return string(buf, end, ip4_addr, spec);
1378 static noinline_for_stack
1379 char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1384 unsigned int flags = 0;
1387 if (spec.field_width == 0)
1388 return buf; /* nothing to print */
1390 if (ZERO_OR_NULL_PTR(addr))
1391 return string(buf, end, NULL, spec); /* NULL pointer */
1395 switch (fmt[count++]) {
1397 flags |= ESCAPE_ANY;
1400 flags |= ESCAPE_SPECIAL;
1403 flags |= ESCAPE_HEX;
1406 flags |= ESCAPE_NULL;
1409 flags |= ESCAPE_OCTAL;
1415 flags |= ESCAPE_SPACE;
1424 flags = ESCAPE_ANY_NP;
1426 len = spec.field_width < 0 ? 1 : spec.field_width;
1429 * string_escape_mem() writes as many characters as it can to
1430 * the given buffer, and returns the total size of the output
1431 * had the buffer been big enough.
1433 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1438 static noinline_for_stack
1439 char *uuid_string(char *buf, char *end, const u8 *addr,
1440 struct printf_spec spec, const char *fmt)
1442 char uuid[UUID_STRING_LEN + 1];
1445 const u8 *index = uuid_index;
1450 uc = true; /* fall-through */
1459 for (i = 0; i < 16; i++) {
1461 p = hex_byte_pack_upper(p, addr[index[i]]);
1463 p = hex_byte_pack(p, addr[index[i]]);
1476 return string(buf, end, uuid, spec);
1479 int kptr_restrict __read_mostly;
1481 static noinline_for_stack
1482 char *restricted_pointer(char *buf, char *end, const void *ptr,
1483 struct printf_spec spec)
1485 switch (kptr_restrict) {
1487 /* Always print %pK values */
1490 const struct cred *cred;
1493 * kptr_restrict==1 cannot be used in IRQ context
1494 * because its test for CAP_SYSLOG would be meaningless.
1496 if (in_irq() || in_serving_softirq() || in_nmi()) {
1497 if (spec.field_width == -1)
1498 spec.field_width = 2 * sizeof(ptr);
1499 return string(buf, end, "pK-error", spec);
1503 * Only print the real pointer value if the current
1504 * process has CAP_SYSLOG and is running with the
1505 * same credentials it started with. This is because
1506 * access to files is checked at open() time, but %pK
1507 * checks permission at read() time. We don't want to
1508 * leak pointer values if a binary opens a file using
1509 * %pK and then elevates privileges before reading it.
1511 cred = current_cred();
1512 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1513 !uid_eq(cred->euid, cred->uid) ||
1514 !gid_eq(cred->egid, cred->gid))
1520 /* Always print 0's for %pK */
1525 return pointer_string(buf, end, ptr, spec);
1528 static noinline_for_stack
1529 char *netdev_bits(char *buf, char *end, const void *addr,
1530 struct printf_spec spec, const char *fmt)
1532 unsigned long long num;
1537 num = *(const netdev_features_t *)addr;
1538 size = sizeof(netdev_features_t);
1541 return ptr_to_id(buf, end, addr, spec);
1544 return special_hex_number(buf, end, num, size);
1547 static noinline_for_stack
1548 char *address_val(char *buf, char *end, const void *addr, const char *fmt)
1550 unsigned long long num;
1555 num = *(const dma_addr_t *)addr;
1556 size = sizeof(dma_addr_t);
1560 num = *(const phys_addr_t *)addr;
1561 size = sizeof(phys_addr_t);
1565 return special_hex_number(buf, end, num, size);
1568 static noinline_for_stack
1569 char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1571 int year = tm->tm_year + (r ? 0 : 1900);
1572 int mon = tm->tm_mon + (r ? 0 : 1);
1574 buf = number(buf, end, year, default_dec04_spec);
1579 buf = number(buf, end, mon, default_dec02_spec);
1584 return number(buf, end, tm->tm_mday, default_dec02_spec);
1587 static noinline_for_stack
1588 char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1590 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1595 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1600 return number(buf, end, tm->tm_sec, default_dec02_spec);
1603 static noinline_for_stack
1604 char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt)
1606 bool have_t = true, have_d = true;
1610 switch (fmt[count]) {
1621 raw = fmt[count] == 'r';
1624 buf = date_str(buf, end, tm, raw);
1625 if (have_d && have_t) {
1626 /* Respect ISO 8601 */
1632 buf = time_str(buf, end, tm, raw);
1637 static noinline_for_stack
1638 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1643 return rtc_str(buf, end, (const struct rtc_time *)ptr, fmt);
1645 return ptr_to_id(buf, end, ptr, spec);
1649 static noinline_for_stack
1650 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1653 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1654 return string(buf, end, NULL, spec);
1659 #ifdef CONFIG_COMMON_CLK
1660 return string(buf, end, __clk_get_name(clk), spec);
1662 return ptr_to_id(buf, end, clk, spec);
1668 char *format_flags(char *buf, char *end, unsigned long flags,
1669 const struct trace_print_flags *names)
1673 for ( ; flags && names->name; names++) {
1675 if ((flags & mask) != mask)
1678 buf = string(buf, end, names->name, default_str_spec);
1689 buf = number(buf, end, flags, default_flag_spec);
1694 static noinline_for_stack
1695 char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1697 unsigned long flags;
1698 const struct trace_print_flags *names;
1702 flags = *(unsigned long *)flags_ptr;
1703 /* Remove zone id */
1704 flags &= (1UL << NR_PAGEFLAGS) - 1;
1705 names = pageflag_names;
1708 flags = *(unsigned long *)flags_ptr;
1709 names = vmaflag_names;
1712 flags = *(gfp_t *)flags_ptr;
1713 names = gfpflag_names;
1716 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1720 return format_flags(buf, end, flags, names);
1723 static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1725 for ( ; np && depth; depth--)
1728 return kbasename(np->full_name);
1731 static noinline_for_stack
1732 char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1735 const struct device_node *parent = np->parent;
1737 /* special case for root node */
1739 return string(buf, end, "/", default_str_spec);
1741 for (depth = 0; parent->parent; depth++)
1742 parent = parent->parent;
1744 for ( ; depth >= 0; depth--) {
1745 buf = string(buf, end, "/", default_str_spec);
1746 buf = string(buf, end, device_node_name_for_depth(np, depth),
1752 static noinline_for_stack
1753 char *device_node_string(char *buf, char *end, struct device_node *dn,
1754 struct printf_spec spec, const char *fmt)
1756 char tbuf[sizeof("xxxx") + 1];
1759 char *buf_start = buf;
1760 struct property *prop;
1761 bool has_mult, pass;
1762 static const struct printf_spec num_spec = {
1769 struct printf_spec str_spec = spec;
1770 str_spec.field_width = -1;
1772 if (!IS_ENABLED(CONFIG_OF))
1773 return string(buf, end, "(!OF)", spec);
1775 if ((unsigned long)dn < PAGE_SIZE)
1776 return string(buf, end, "(null)", spec);
1778 /* simple case without anything any more format specifiers */
1780 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1783 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1792 case 'f': /* full_name */
1793 buf = device_node_gen_full_name(dn, buf, end);
1795 case 'n': /* name */
1796 p = kbasename(of_node_full_name(dn));
1797 precision = str_spec.precision;
1798 str_spec.precision = strchrnul(p, '@') - p;
1799 buf = string(buf, end, p, str_spec);
1800 str_spec.precision = precision;
1802 case 'p': /* phandle */
1803 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1805 case 'P': /* path-spec */
1806 p = kbasename(of_node_full_name(dn));
1809 buf = string(buf, end, p, str_spec);
1811 case 'F': /* flags */
1812 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1813 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1814 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1815 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1817 buf = string(buf, end, tbuf, str_spec);
1819 case 'c': /* major compatible string */
1820 ret = of_property_read_string(dn, "compatible", &p);
1822 buf = string(buf, end, p, str_spec);
1824 case 'C': /* full compatible string */
1826 of_property_for_each_string(dn, "compatible", prop, p) {
1828 buf = string(buf, end, ",", str_spec);
1829 buf = string(buf, end, "\"", str_spec);
1830 buf = string(buf, end, p, str_spec);
1831 buf = string(buf, end, "\"", str_spec);
1841 return widen_string(buf, buf - buf_start, end, spec);
1845 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1846 * by an extra set of alphanumeric characters that are extended format
1849 * Please update scripts/checkpatch.pl when adding/removing conversion
1850 * characters. (Search for "check for vsprintf extension").
1852 * Right now we handle:
1854 * - 'S' For symbolic direct pointers (or function descriptors) with offset
1855 * - 's' For symbolic direct pointers (or function descriptors) without offset
1858 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1859 * - 'B' For backtraced symbolic direct pointers with offset
1860 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1861 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1862 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1863 * width which must be explicitly specified either as part of the
1864 * format string '%32b[l]' or through '%*b[l]', [l] selects
1865 * range-list format instead of hex format
1866 * - 'M' For a 6-byte MAC address, it prints the address in the
1867 * usual colon-separated hex notation
1868 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1869 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1870 * with a dash-separated hex notation
1871 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1872 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1873 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1874 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1876 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1877 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1878 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1879 * IPv6 omits the colons (01020304...0f)
1880 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1882 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1883 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1884 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1885 * - 'I[6S]c' for IPv6 addresses printed as specified by
1886 * http://tools.ietf.org/html/rfc5952
1887 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1888 * of the following flags (see string_escape_mem() for the
1891 * c - ESCAPE_SPECIAL
1897 * By default ESCAPE_ANY_NP is used.
1898 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1899 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1900 * Options for %pU are:
1901 * b big endian lower case hex (default)
1902 * B big endian UPPER case hex
1903 * l little endian lower case hex
1904 * L little endian UPPER case hex
1905 * big endian output byte order is:
1906 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1907 * little endian output byte order is:
1908 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1909 * - 'V' For a struct va_format which contains a format string * and va_list *,
1910 * call vsnprintf(->format, *->va_list).
1911 * Implements a "recursive vsnprintf".
1912 * Do not use this feature without some mechanism to verify the
1913 * correctness of the format string and va_list arguments.
1914 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1915 * - 'NF' For a netdev_features_t
1916 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1917 * a certain separator (' ' by default):
1921 * The maximum supported length is 64 bytes of the input. Consider
1922 * to use print_hex_dump() for the larger input.
1923 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1924 * (default assumed to be phys_addr_t, passed by reference)
1925 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1926 * - 'D[234]' Same as 'd' but for a struct file
1927 * - 'g' For block_device name (gendisk + partition number)
1928 * - 't[R][dt][r]' For time and date as represented:
1930 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1931 * (legacy clock framework) of the clock
1932 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1933 * (legacy clock framework) of the clock
1934 * - 'Cr' For a clock, it prints the current rate of the clock
1935 * - 'G' For flags to be printed as a collection of symbolic strings that would
1936 * construct the specific value. Supported flags given by option:
1937 * p page flags (see struct page) given as pointer to unsigned long
1938 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1939 * v vma flags (VM_*) given as pointer to unsigned long
1940 * - 'OF[fnpPcCF]' For a device tree object
1941 * Without any optional arguments prints the full_name
1942 * f device node full_name
1943 * n device node name
1944 * p device node phandle
1945 * P device node path spec (name + @unit)
1946 * F device node flags
1947 * c major compatible string
1948 * C full compatible string
1949 * - 'x' For printing the address. Equivalent to "%lx".
1951 * ** When making changes please also update:
1952 * Documentation/core-api/printk-formats.rst
1954 * Note: The default behaviour (unadorned %p) is to hash the address,
1955 * rendering it useful as a unique identifier.
1957 static noinline_for_stack
1958 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1959 struct printf_spec spec)
1961 const int default_width = 2 * sizeof(void *);
1963 if (!ptr && *fmt != 'K' && *fmt != 'x') {
1965 * Print (null) with the same width as a pointer so it makes
1966 * tabular output look nice.
1968 if (spec.field_width == -1)
1969 spec.field_width = default_width;
1970 return string(buf, end, "(null)", spec);
1978 ptr = dereference_symbol_descriptor(ptr);
1981 return symbol_string(buf, end, ptr, spec, fmt);
1984 return resource_string(buf, end, ptr, spec, fmt);
1986 return hex_string(buf, end, ptr, spec, fmt);
1990 return bitmap_list_string(buf, end, ptr, spec, fmt);
1992 return bitmap_string(buf, end, ptr, spec, fmt);
1994 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1995 case 'm': /* Contiguous: 000102030405 */
1997 /* [mM]R (Reverse order; Bluetooth) */
1998 return mac_address_string(buf, end, ptr, spec, fmt);
1999 case 'I': /* Formatted IP supported
2001 * 6: 0001:0203:...:0708
2002 * 6c: 1::708 or 1::1.2.3.4
2004 case 'i': /* Contiguous:
2005 * 4: 001.002.003.004
2010 return ip6_addr_string(buf, end, ptr, spec, fmt);
2012 return ip4_addr_string(buf, end, ptr, spec, fmt);
2015 struct sockaddr raw;
2016 struct sockaddr_in v4;
2017 struct sockaddr_in6 v6;
2020 switch (sa->raw.sa_family) {
2022 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
2024 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
2026 return string(buf, end, "(invalid address)", spec);
2031 return escaped_string(buf, end, ptr, spec, fmt);
2033 return uuid_string(buf, end, ptr, spec, fmt);
2038 va_copy(va, *((struct va_format *)ptr)->va);
2039 buf += vsnprintf(buf, end > buf ? end - buf : 0,
2040 ((struct va_format *)ptr)->fmt, va);
2047 return restricted_pointer(buf, end, ptr, spec);
2049 return netdev_bits(buf, end, ptr, spec, fmt);
2051 return address_val(buf, end, ptr, fmt);
2053 return dentry_name(buf, end, ptr, spec, fmt);
2055 return time_and_date(buf, end, ptr, spec, fmt);
2057 return clock(buf, end, ptr, spec, fmt);
2059 return dentry_name(buf, end,
2060 ((const struct file *)ptr)->f_path.dentry,
2064 return bdev_name(buf, end, ptr, spec, fmt);
2068 return flags_string(buf, end, ptr, fmt);
2072 return device_node_string(buf, end, ptr, spec, fmt + 1);
2076 return pointer_string(buf, end, ptr, spec);
2079 /* default is to _not_ leak addresses, hash before printing */
2080 return ptr_to_id(buf, end, ptr, spec);
2084 * Helper function to decode printf style format.
2085 * Each call decode a token from the format and return the
2086 * number of characters read (or likely the delta where it wants
2087 * to go on the next call).
2088 * The decoded token is returned through the parameters
2090 * 'h', 'l', or 'L' for integer fields
2091 * 'z' support added 23/7/1999 S.H.
2092 * 'z' changed to 'Z' --davidm 1/25/99
2093 * 'Z' changed to 'z' --adobriyan 2017-01-25
2094 * 't' added for ptrdiff_t
2096 * @fmt: the format string
2097 * @type of the token returned
2098 * @flags: various flags such as +, -, # tokens..
2099 * @field_width: overwritten width
2100 * @base: base of the number (octal, hex, ...)
2101 * @precision: precision of a number
2102 * @qualifier: qualifier of a number (long, size_t, ...)
2104 static noinline_for_stack
2105 int format_decode(const char *fmt, struct printf_spec *spec)
2107 const char *start = fmt;
2110 /* we finished early by reading the field width */
2111 if (spec->type == FORMAT_TYPE_WIDTH) {
2112 if (spec->field_width < 0) {
2113 spec->field_width = -spec->field_width;
2114 spec->flags |= LEFT;
2116 spec->type = FORMAT_TYPE_NONE;
2120 /* we finished early by reading the precision */
2121 if (spec->type == FORMAT_TYPE_PRECISION) {
2122 if (spec->precision < 0)
2123 spec->precision = 0;
2125 spec->type = FORMAT_TYPE_NONE;
2130 spec->type = FORMAT_TYPE_NONE;
2132 for (; *fmt ; ++fmt) {
2137 /* Return the current non-format string */
2138 if (fmt != start || !*fmt)
2144 while (1) { /* this also skips first '%' */
2150 case '-': spec->flags |= LEFT; break;
2151 case '+': spec->flags |= PLUS; break;
2152 case ' ': spec->flags |= SPACE; break;
2153 case '#': spec->flags |= SPECIAL; break;
2154 case '0': spec->flags |= ZEROPAD; break;
2155 default: found = false;
2162 /* get field width */
2163 spec->field_width = -1;
2166 spec->field_width = skip_atoi(&fmt);
2167 else if (*fmt == '*') {
2168 /* it's the next argument */
2169 spec->type = FORMAT_TYPE_WIDTH;
2170 return ++fmt - start;
2174 /* get the precision */
2175 spec->precision = -1;
2178 if (isdigit(*fmt)) {
2179 spec->precision = skip_atoi(&fmt);
2180 if (spec->precision < 0)
2181 spec->precision = 0;
2182 } else if (*fmt == '*') {
2183 /* it's the next argument */
2184 spec->type = FORMAT_TYPE_PRECISION;
2185 return ++fmt - start;
2190 /* get the conversion qualifier */
2192 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2193 *fmt == 'z' || *fmt == 't') {
2195 if (unlikely(qualifier == *fmt)) {
2196 if (qualifier == 'l') {
2199 } else if (qualifier == 'h') {
2210 spec->type = FORMAT_TYPE_CHAR;
2211 return ++fmt - start;
2214 spec->type = FORMAT_TYPE_STR;
2215 return ++fmt - start;
2218 spec->type = FORMAT_TYPE_PTR;
2219 return ++fmt - start;
2222 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2223 return ++fmt - start;
2225 /* integer number formats - set up the flags and "break" */
2231 spec->flags |= SMALL;
2240 spec->flags |= SIGN;
2246 * Since %n poses a greater security risk than
2247 * utility, treat it as any other invalid or
2248 * unsupported format specifier.
2253 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2254 spec->type = FORMAT_TYPE_INVALID;
2258 if (qualifier == 'L')
2259 spec->type = FORMAT_TYPE_LONG_LONG;
2260 else if (qualifier == 'l') {
2261 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2262 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2263 } else if (qualifier == 'z') {
2264 spec->type = FORMAT_TYPE_SIZE_T;
2265 } else if (qualifier == 't') {
2266 spec->type = FORMAT_TYPE_PTRDIFF;
2267 } else if (qualifier == 'H') {
2268 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2269 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2270 } else if (qualifier == 'h') {
2271 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2272 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2274 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2275 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2278 return ++fmt - start;
2282 set_field_width(struct printf_spec *spec, int width)
2284 spec->field_width = width;
2285 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2286 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2291 set_precision(struct printf_spec *spec, int prec)
2293 spec->precision = prec;
2294 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2295 spec->precision = clamp(prec, 0, PRECISION_MAX);
2300 * vsnprintf - Format a string and place it in a buffer
2301 * @buf: The buffer to place the result into
2302 * @size: The size of the buffer, including the trailing null space
2303 * @fmt: The format string to use
2304 * @args: Arguments for the format string
2306 * This function generally follows C99 vsnprintf, but has some
2307 * extensions and a few limitations:
2309 * - ``%n`` is unsupported
2310 * - ``%p*`` is handled by pointer()
2312 * See pointer() or Documentation/core-api/printk-formats.rst for more
2313 * extensive description.
2315 * **Please update the documentation in both places when making changes**
2317 * The return value is the number of characters which would
2318 * be generated for the given input, excluding the trailing
2319 * '\0', as per ISO C99. If you want to have the exact
2320 * number of characters written into @buf as return value
2321 * (not including the trailing '\0'), use vscnprintf(). If the
2322 * return is greater than or equal to @size, the resulting
2323 * string is truncated.
2325 * If you're not already dealing with a va_list consider using snprintf().
2327 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2329 unsigned long long num;
2331 struct printf_spec spec = {0};
2333 /* Reject out-of-range values early. Large positive sizes are
2334 used for unknown buffer sizes. */
2335 if (WARN_ON_ONCE(size > INT_MAX))
2341 /* Make sure end is always >= buf */
2348 const char *old_fmt = fmt;
2349 int read = format_decode(fmt, &spec);
2353 switch (spec.type) {
2354 case FORMAT_TYPE_NONE: {
2357 if (copy > end - str)
2359 memcpy(str, old_fmt, copy);
2365 case FORMAT_TYPE_WIDTH:
2366 set_field_width(&spec, va_arg(args, int));
2369 case FORMAT_TYPE_PRECISION:
2370 set_precision(&spec, va_arg(args, int));
2373 case FORMAT_TYPE_CHAR: {
2376 if (!(spec.flags & LEFT)) {
2377 while (--spec.field_width > 0) {
2384 c = (unsigned char) va_arg(args, int);
2388 while (--spec.field_width > 0) {
2396 case FORMAT_TYPE_STR:
2397 str = string(str, end, va_arg(args, char *), spec);
2400 case FORMAT_TYPE_PTR:
2401 str = pointer(fmt, str, end, va_arg(args, void *),
2403 while (isalnum(*fmt))
2407 case FORMAT_TYPE_PERCENT_CHAR:
2413 case FORMAT_TYPE_INVALID:
2415 * Presumably the arguments passed gcc's type
2416 * checking, but there is no safe or sane way
2417 * for us to continue parsing the format and
2418 * fetching from the va_list; the remaining
2419 * specifiers and arguments would be out of
2425 switch (spec.type) {
2426 case FORMAT_TYPE_LONG_LONG:
2427 num = va_arg(args, long long);
2429 case FORMAT_TYPE_ULONG:
2430 num = va_arg(args, unsigned long);
2432 case FORMAT_TYPE_LONG:
2433 num = va_arg(args, long);
2435 case FORMAT_TYPE_SIZE_T:
2436 if (spec.flags & SIGN)
2437 num = va_arg(args, ssize_t);
2439 num = va_arg(args, size_t);
2441 case FORMAT_TYPE_PTRDIFF:
2442 num = va_arg(args, ptrdiff_t);
2444 case FORMAT_TYPE_UBYTE:
2445 num = (unsigned char) va_arg(args, int);
2447 case FORMAT_TYPE_BYTE:
2448 num = (signed char) va_arg(args, int);
2450 case FORMAT_TYPE_USHORT:
2451 num = (unsigned short) va_arg(args, int);
2453 case FORMAT_TYPE_SHORT:
2454 num = (short) va_arg(args, int);
2456 case FORMAT_TYPE_INT:
2457 num = (int) va_arg(args, int);
2460 num = va_arg(args, unsigned int);
2463 str = number(str, end, num, spec);
2475 /* the trailing null byte doesn't count towards the total */
2479 EXPORT_SYMBOL(vsnprintf);
2482 * vscnprintf - Format a string and place it in a buffer
2483 * @buf: The buffer to place the result into
2484 * @size: The size of the buffer, including the trailing null space
2485 * @fmt: The format string to use
2486 * @args: Arguments for the format string
2488 * The return value is the number of characters which have been written into
2489 * the @buf not including the trailing '\0'. If @size is == 0 the function
2492 * If you're not already dealing with a va_list consider using scnprintf().
2494 * See the vsnprintf() documentation for format string extensions over C99.
2496 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2500 i = vsnprintf(buf, size, fmt, args);
2502 if (likely(i < size))
2508 EXPORT_SYMBOL(vscnprintf);
2511 * snprintf - Format a string and place it in a buffer
2512 * @buf: The buffer to place the result into
2513 * @size: The size of the buffer, including the trailing null space
2514 * @fmt: The format string to use
2515 * @...: Arguments for the format string
2517 * The return value is the number of characters which would be
2518 * generated for the given input, excluding the trailing null,
2519 * as per ISO C99. If the return is greater than or equal to
2520 * @size, the resulting string is truncated.
2522 * See the vsnprintf() documentation for format string extensions over C99.
2524 int snprintf(char *buf, size_t size, const char *fmt, ...)
2529 va_start(args, fmt);
2530 i = vsnprintf(buf, size, fmt, args);
2535 EXPORT_SYMBOL(snprintf);
2538 * scnprintf - Format a string and place it in a buffer
2539 * @buf: The buffer to place the result into
2540 * @size: The size of the buffer, including the trailing null space
2541 * @fmt: The format string to use
2542 * @...: Arguments for the format string
2544 * The return value is the number of characters written into @buf not including
2545 * the trailing '\0'. If @size is == 0 the function returns 0.
2548 int scnprintf(char *buf, size_t size, const char *fmt, ...)
2553 va_start(args, fmt);
2554 i = vscnprintf(buf, size, fmt, args);
2559 EXPORT_SYMBOL(scnprintf);
2562 * vsprintf - Format a string and place it in a buffer
2563 * @buf: The buffer to place the result into
2564 * @fmt: The format string to use
2565 * @args: Arguments for the format string
2567 * The function returns the number of characters written
2568 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2571 * If you're not already dealing with a va_list consider using sprintf().
2573 * See the vsnprintf() documentation for format string extensions over C99.
2575 int vsprintf(char *buf, const char *fmt, va_list args)
2577 return vsnprintf(buf, INT_MAX, fmt, args);
2579 EXPORT_SYMBOL(vsprintf);
2582 * sprintf - Format a string and place it in a buffer
2583 * @buf: The buffer to place the result into
2584 * @fmt: The format string to use
2585 * @...: Arguments for the format string
2587 * The function returns the number of characters written
2588 * into @buf. Use snprintf() or scnprintf() in order to avoid
2591 * See the vsnprintf() documentation for format string extensions over C99.
2593 int sprintf(char *buf, const char *fmt, ...)
2598 va_start(args, fmt);
2599 i = vsnprintf(buf, INT_MAX, fmt, args);
2604 EXPORT_SYMBOL(sprintf);
2606 #ifdef CONFIG_BINARY_PRINTF
2609 * vbin_printf() - VA arguments to binary data
2610 * bstr_printf() - Binary data to text string
2614 * vbin_printf - Parse a format string and place args' binary value in a buffer
2615 * @bin_buf: The buffer to place args' binary value
2616 * @size: The size of the buffer(by words(32bits), not characters)
2617 * @fmt: The format string to use
2618 * @args: Arguments for the format string
2620 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2623 * The return value is the number of words(32bits) which would be generated for
2627 * If the return value is greater than @size, the resulting bin_buf is NOT
2628 * valid for bstr_printf().
2630 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2632 struct printf_spec spec = {0};
2636 str = (char *)bin_buf;
2637 end = (char *)(bin_buf + size);
2639 #define save_arg(type) \
2641 unsigned long long value; \
2642 if (sizeof(type) == 8) { \
2643 unsigned long long val8; \
2644 str = PTR_ALIGN(str, sizeof(u32)); \
2645 val8 = va_arg(args, unsigned long long); \
2646 if (str + sizeof(type) <= end) { \
2647 *(u32 *)str = *(u32 *)&val8; \
2648 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2652 unsigned int val4; \
2653 str = PTR_ALIGN(str, sizeof(type)); \
2654 val4 = va_arg(args, int); \
2655 if (str + sizeof(type) <= end) \
2656 *(typeof(type) *)str = (type)(long)val4; \
2657 value = (unsigned long long)val4; \
2659 str += sizeof(type); \
2664 int read = format_decode(fmt, &spec);
2668 switch (spec.type) {
2669 case FORMAT_TYPE_NONE:
2670 case FORMAT_TYPE_PERCENT_CHAR:
2672 case FORMAT_TYPE_INVALID:
2675 case FORMAT_TYPE_WIDTH:
2676 case FORMAT_TYPE_PRECISION:
2677 width = (int)save_arg(int);
2678 /* Pointers may require the width */
2680 set_field_width(&spec, width);
2683 case FORMAT_TYPE_CHAR:
2687 case FORMAT_TYPE_STR: {
2688 const char *save_str = va_arg(args, char *);
2691 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2692 || (unsigned long)save_str < PAGE_SIZE)
2693 save_str = "(null)";
2694 len = strlen(save_str) + 1;
2695 if (str + len < end)
2696 memcpy(str, save_str, len);
2701 case FORMAT_TYPE_PTR:
2702 /* Dereferenced pointers must be done now */
2704 /* Dereference of functions is still OK */
2714 if (!isalnum(*fmt)) {
2718 str = pointer(fmt, str, end, va_arg(args, void *),
2723 end[-1] = '\0'; /* Must be nul terminated */
2725 /* skip all alphanumeric pointer suffixes */
2726 while (isalnum(*fmt))
2731 switch (spec.type) {
2733 case FORMAT_TYPE_LONG_LONG:
2734 save_arg(long long);
2736 case FORMAT_TYPE_ULONG:
2737 case FORMAT_TYPE_LONG:
2738 save_arg(unsigned long);
2740 case FORMAT_TYPE_SIZE_T:
2743 case FORMAT_TYPE_PTRDIFF:
2744 save_arg(ptrdiff_t);
2746 case FORMAT_TYPE_UBYTE:
2747 case FORMAT_TYPE_BYTE:
2750 case FORMAT_TYPE_USHORT:
2751 case FORMAT_TYPE_SHORT:
2761 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2764 EXPORT_SYMBOL_GPL(vbin_printf);
2767 * bstr_printf - Format a string from binary arguments and place it in a buffer
2768 * @buf: The buffer to place the result into
2769 * @size: The size of the buffer, including the trailing null space
2770 * @fmt: The format string to use
2771 * @bin_buf: Binary arguments for the format string
2773 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2774 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2775 * a binary buffer that generated by vbin_printf.
2777 * The format follows C99 vsnprintf, but has some extensions:
2778 * see vsnprintf comment for details.
2780 * The return value is the number of characters which would
2781 * be generated for the given input, excluding the trailing
2782 * '\0', as per ISO C99. If you want to have the exact
2783 * number of characters written into @buf as return value
2784 * (not including the trailing '\0'), use vscnprintf(). If the
2785 * return is greater than or equal to @size, the resulting
2786 * string is truncated.
2788 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2790 struct printf_spec spec = {0};
2792 const char *args = (const char *)bin_buf;
2794 if (WARN_ON_ONCE(size > INT_MAX))
2800 #define get_arg(type) \
2802 typeof(type) value; \
2803 if (sizeof(type) == 8) { \
2804 args = PTR_ALIGN(args, sizeof(u32)); \
2805 *(u32 *)&value = *(u32 *)args; \
2806 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2808 args = PTR_ALIGN(args, sizeof(type)); \
2809 value = *(typeof(type) *)args; \
2811 args += sizeof(type); \
2815 /* Make sure end is always >= buf */
2822 const char *old_fmt = fmt;
2823 int read = format_decode(fmt, &spec);
2827 switch (spec.type) {
2828 case FORMAT_TYPE_NONE: {
2831 if (copy > end - str)
2833 memcpy(str, old_fmt, copy);
2839 case FORMAT_TYPE_WIDTH:
2840 set_field_width(&spec, get_arg(int));
2843 case FORMAT_TYPE_PRECISION:
2844 set_precision(&spec, get_arg(int));
2847 case FORMAT_TYPE_CHAR: {
2850 if (!(spec.flags & LEFT)) {
2851 while (--spec.field_width > 0) {
2857 c = (unsigned char) get_arg(char);
2861 while (--spec.field_width > 0) {
2869 case FORMAT_TYPE_STR: {
2870 const char *str_arg = args;
2871 args += strlen(str_arg) + 1;
2872 str = string(str, end, (char *)str_arg, spec);
2876 case FORMAT_TYPE_PTR: {
2877 bool process = false;
2879 /* Non function dereferences were already done */
2890 if (!isalnum(*fmt)) {
2894 /* Pointer dereference was already processed */
2896 len = copy = strlen(args);
2897 if (copy > end - str)
2899 memcpy(str, args, copy);
2905 str = pointer(fmt, str, end, get_arg(void *), spec);
2907 while (isalnum(*fmt))
2912 case FORMAT_TYPE_PERCENT_CHAR:
2918 case FORMAT_TYPE_INVALID:
2922 unsigned long long num;
2924 switch (spec.type) {
2926 case FORMAT_TYPE_LONG_LONG:
2927 num = get_arg(long long);
2929 case FORMAT_TYPE_ULONG:
2930 case FORMAT_TYPE_LONG:
2931 num = get_arg(unsigned long);
2933 case FORMAT_TYPE_SIZE_T:
2934 num = get_arg(size_t);
2936 case FORMAT_TYPE_PTRDIFF:
2937 num = get_arg(ptrdiff_t);
2939 case FORMAT_TYPE_UBYTE:
2940 num = get_arg(unsigned char);
2942 case FORMAT_TYPE_BYTE:
2943 num = get_arg(signed char);
2945 case FORMAT_TYPE_USHORT:
2946 num = get_arg(unsigned short);
2948 case FORMAT_TYPE_SHORT:
2949 num = get_arg(short);
2951 case FORMAT_TYPE_UINT:
2952 num = get_arg(unsigned int);
2958 str = number(str, end, num, spec);
2960 } /* switch(spec.type) */
2973 /* the trailing null byte doesn't count towards the total */
2976 EXPORT_SYMBOL_GPL(bstr_printf);
2979 * bprintf - Parse a format string and place args' binary value in a buffer
2980 * @bin_buf: The buffer to place args' binary value
2981 * @size: The size of the buffer(by words(32bits), not characters)
2982 * @fmt: The format string to use
2983 * @...: Arguments for the format string
2985 * The function returns the number of words(u32) written
2988 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2993 va_start(args, fmt);
2994 ret = vbin_printf(bin_buf, size, fmt, args);
2999 EXPORT_SYMBOL_GPL(bprintf);
3001 #endif /* CONFIG_BINARY_PRINTF */
3004 * vsscanf - Unformat a buffer into a list of arguments
3005 * @buf: input buffer
3006 * @fmt: format of buffer
3009 int vsscanf(const char *buf, const char *fmt, va_list args)
3011 const char *str = buf;
3019 unsigned long long u;
3025 /* skip any white space in format */
3026 /* white space in format matchs any amount of
3027 * white space, including none, in the input.
3029 if (isspace(*fmt)) {
3030 fmt = skip_spaces(++fmt);
3031 str = skip_spaces(str);
3034 /* anything that is not a conversion must match exactly */
3035 if (*fmt != '%' && *fmt) {
3036 if (*fmt++ != *str++)
3045 /* skip this conversion.
3046 * advance both strings to next white space
3051 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3052 /* '%*[' not yet supported, invalid format */
3057 while (!isspace(*str) && *str)
3062 /* get field width */
3064 if (isdigit(*fmt)) {
3065 field_width = skip_atoi(&fmt);
3066 if (field_width <= 0)
3070 /* get conversion qualifier */
3072 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
3075 if (unlikely(qualifier == *fmt)) {
3076 if (qualifier == 'h') {
3079 } else if (qualifier == 'l') {
3090 /* return number of characters read so far */
3091 *va_arg(args, int *) = str - buf;
3105 char *s = (char *)va_arg(args, char*);
3106 if (field_width == -1)
3110 } while (--field_width > 0 && *str);
3116 char *s = (char *)va_arg(args, char *);
3117 if (field_width == -1)
3118 field_width = SHRT_MAX;
3119 /* first, skip leading white space in buffer */
3120 str = skip_spaces(str);
3122 /* now copy until next white space */
3123 while (*str && !isspace(*str) && field_width--)
3130 * Warning: This implementation of the '[' conversion specifier
3131 * deviates from its glibc counterpart in the following ways:
3132 * (1) It does NOT support ranges i.e. '-' is NOT a special
3134 * (2) It cannot match the closing bracket ']' itself
3135 * (3) A field width is required
3136 * (4) '%*[' (discard matching input) is currently not supported
3139 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3140 * buf1, buf2, buf3);
3146 char *s = (char *)va_arg(args, char *);
3147 DECLARE_BITMAP(set, 256) = {0};
3148 unsigned int len = 0;
3149 bool negate = (*fmt == '^');
3151 /* field width is required */
3152 if (field_width == -1)
3158 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3159 set_bit((u8)*fmt, set);
3161 /* no ']' or no character set found */
3167 bitmap_complement(set, set, 256);
3168 /* exclude null '\0' byte */
3172 /* match must be non-empty */
3173 if (!test_bit((u8)*str, set))
3176 while (test_bit((u8)*str, set) && field_width--)
3198 /* looking for '%' in str */
3203 /* invalid format; stop here */
3207 /* have some sort of integer conversion.
3208 * first, skip white space in buffer.
3210 str = skip_spaces(str);
3213 if (is_sign && digit == '-')
3217 || (base == 16 && !isxdigit(digit))
3218 || (base == 10 && !isdigit(digit))
3219 || (base == 8 && (!isdigit(digit) || digit > '7'))
3220 || (base == 0 && !isdigit(digit)))
3224 val.s = qualifier != 'L' ?
3225 simple_strtol(str, &next, base) :
3226 simple_strtoll(str, &next, base);
3228 val.u = qualifier != 'L' ?
3229 simple_strtoul(str, &next, base) :
3230 simple_strtoull(str, &next, base);
3232 if (field_width > 0 && next - str > field_width) {
3234 _parse_integer_fixup_radix(str, &base);
3235 while (next - str > field_width) {
3237 val.s = div_s64(val.s, base);
3239 val.u = div_u64(val.u, base);
3244 switch (qualifier) {
3245 case 'H': /* that's 'hh' in format */
3247 *va_arg(args, signed char *) = val.s;
3249 *va_arg(args, unsigned char *) = val.u;
3253 *va_arg(args, short *) = val.s;
3255 *va_arg(args, unsigned short *) = val.u;
3259 *va_arg(args, long *) = val.s;
3261 *va_arg(args, unsigned long *) = val.u;
3265 *va_arg(args, long long *) = val.s;
3267 *va_arg(args, unsigned long long *) = val.u;
3270 *va_arg(args, size_t *) = val.u;
3274 *va_arg(args, int *) = val.s;
3276 *va_arg(args, unsigned int *) = val.u;
3288 EXPORT_SYMBOL(vsscanf);
3291 * sscanf - Unformat a buffer into a list of arguments
3292 * @buf: input buffer
3293 * @fmt: formatting of buffer
3294 * @...: resulting arguments
3296 int sscanf(const char *buf, const char *fmt, ...)
3301 va_start(args, fmt);
3302 i = vsscanf(buf, fmt, args);
3307 EXPORT_SYMBOL(sscanf);