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