1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * NOTE: 4 * 5 * This header has combined a lot of unrelated to each other stuff. 6 * The process of splitting its content is in progress while keeping 7 * backward compatibility. That's why it's highly recommended NOT to 8 * include this header inside another header file, especially under 9 * generic or architectural include/ directory. 10 */ 11 #ifndef _LINUX_KERNEL_H 12 #define _LINUX_KERNEL_H 13 14 #include <linux/stdarg.h> 15 #include <linux/align.h> 16 #include <linux/limits.h> 17 #include <linux/linkage.h> 18 #include <linux/stddef.h> 19 #include <linux/types.h> 20 #include <linux/compiler.h> 21 #include <linux/container_of.h> 22 #include <linux/bitops.h> 23 #include <linux/kstrtox.h> 24 #include <linux/log2.h> 25 #include <linux/math.h> 26 #include <linux/minmax.h> 27 #include <linux/typecheck.h> 28 #include <linux/panic.h> 29 #include <linux/printk.h> 30 #include <linux/build_bug.h> 31 #include <linux/static_call_types.h> 32 #include <linux/instruction_pointer.h> 33 #include <asm/byteorder.h> 34 35 #include <uapi/linux/kernel.h> 36 37 #define STACK_MAGIC 0xdeadbeef 38 39 /** 40 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 41 * @x: value to repeat 42 * 43 * NOTE: @x is not checked for > 0xff; larger values produce odd results. 44 */ 45 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 46 47 /* generic data direction definitions */ 48 #define READ 0 49 #define WRITE 1 50 51 /** 52 * ARRAY_SIZE - get the number of elements in array @arr 53 * @arr: array to be sized 54 */ 55 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 56 57 #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) 58 59 #define u64_to_user_ptr(x) ( \ 60 { \ 61 typecheck(u64, (x)); \ 62 (void __user *)(uintptr_t)(x); \ 63 } \ 64 ) 65 66 /** 67 * lower_48_bits() - return bits 0-47 of a number 68 * @n: the number we're accessing 69 */ 70 static inline u64 lower_48_bits(u64 n) 71 { 72 return n & ((1ull << 48) - 1); 73 } 74 75 /** 76 * upper_32_bits - return bits 32-63 of a number 77 * @n: the number we're accessing 78 * 79 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 80 * the "right shift count >= width of type" warning when that quantity is 81 * 32-bits. 82 */ 83 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 84 85 /** 86 * lower_32_bits - return bits 0-31 of a number 87 * @n: the number we're accessing 88 */ 89 #define lower_32_bits(n) ((u32)((n) & 0xffffffff)) 90 91 /** 92 * upper_16_bits - return bits 16-31 of a number 93 * @n: the number we're accessing 94 */ 95 #define upper_16_bits(n) ((u16)((n) >> 16)) 96 97 /** 98 * lower_16_bits - return bits 0-15 of a number 99 * @n: the number we're accessing 100 */ 101 #define lower_16_bits(n) ((u16)((n) & 0xffff)) 102 103 struct completion; 104 struct user; 105 106 #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD 107 108 extern int __cond_resched(void); 109 # define might_resched() __cond_resched() 110 111 #elif defined(CONFIG_PREEMPT_DYNAMIC) 112 113 extern int __cond_resched(void); 114 115 DECLARE_STATIC_CALL(might_resched, __cond_resched); 116 117 static __always_inline void might_resched(void) 118 { 119 static_call_mod(might_resched)(); 120 } 121 122 #else 123 124 # define might_resched() do { } while (0) 125 126 #endif /* CONFIG_PREEMPT_* */ 127 128 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 129 extern void __might_resched(const char *file, int line, unsigned int offsets); 130 extern void __might_sleep(const char *file, int line); 131 extern void __cant_sleep(const char *file, int line, int preempt_offset); 132 extern void __cant_migrate(const char *file, int line); 133 134 /** 135 * might_sleep - annotation for functions that can sleep 136 * 137 * this macro will print a stack trace if it is executed in an atomic 138 * context (spinlock, irq-handler, ...). Additional sections where blocking is 139 * not allowed can be annotated with non_block_start() and non_block_end() 140 * pairs. 141 * 142 * This is a useful debugging help to be able to catch problems early and not 143 * be bitten later when the calling function happens to sleep when it is not 144 * supposed to. 145 */ 146 # define might_sleep() \ 147 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) 148 /** 149 * cant_sleep - annotation for functions that cannot sleep 150 * 151 * this macro will print a stack trace if it is executed with preemption enabled 152 */ 153 # define cant_sleep() \ 154 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 155 # define sched_annotate_sleep() (current->task_state_change = 0) 156 157 /** 158 * cant_migrate - annotation for functions that cannot migrate 159 * 160 * Will print a stack trace if executed in code which is migratable 161 */ 162 # define cant_migrate() \ 163 do { \ 164 if (IS_ENABLED(CONFIG_SMP)) \ 165 __cant_migrate(__FILE__, __LINE__); \ 166 } while (0) 167 168 /** 169 * non_block_start - annotate the start of section where sleeping is prohibited 170 * 171 * This is on behalf of the oom reaper, specifically when it is calling the mmu 172 * notifiers. The problem is that if the notifier were to block on, for example, 173 * mutex_lock() and if the process which holds that mutex were to perform a 174 * sleeping memory allocation, the oom reaper is now blocked on completion of 175 * that memory allocation. Other blocking calls like wait_event() pose similar 176 * issues. 177 */ 178 # define non_block_start() (current->non_block_count++) 179 /** 180 * non_block_end - annotate the end of section where sleeping is prohibited 181 * 182 * Closes a section opened by non_block_start(). 183 */ 184 # define non_block_end() WARN_ON(current->non_block_count-- == 0) 185 #else 186 static inline void __might_resched(const char *file, int line, 187 unsigned int offsets) { } 188 static inline void __might_sleep(const char *file, int line) { } 189 # define might_sleep() do { might_resched(); } while (0) 190 # define cant_sleep() do { } while (0) 191 # define cant_migrate() do { } while (0) 192 # define sched_annotate_sleep() do { } while (0) 193 # define non_block_start() do { } while (0) 194 # define non_block_end() do { } while (0) 195 #endif 196 197 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 198 199 #if defined(CONFIG_MMU) && \ 200 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 201 #define might_fault() __might_fault(__FILE__, __LINE__) 202 void __might_fault(const char *file, int line); 203 #else 204 static inline void might_fault(void) { } 205 #endif 206 207 void do_exit(long error_code) __noreturn; 208 209 extern int num_to_str(char *buf, int size, 210 unsigned long long num, unsigned int width); 211 212 /* lib/printf utilities */ 213 214 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 215 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 216 extern __printf(3, 4) 217 int snprintf(char *buf, size_t size, const char *fmt, ...); 218 extern __printf(3, 0) 219 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 220 extern __printf(3, 4) 221 int scnprintf(char *buf, size_t size, const char *fmt, ...); 222 extern __printf(3, 0) 223 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 224 extern __printf(2, 3) __malloc 225 char *kasprintf(gfp_t gfp, const char *fmt, ...); 226 extern __printf(2, 0) __malloc 227 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 228 extern __printf(2, 0) 229 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 230 231 extern __scanf(2, 3) 232 int sscanf(const char *, const char *, ...); 233 extern __scanf(2, 0) 234 int vsscanf(const char *, const char *, va_list); 235 236 extern int no_hash_pointers_enable(char *str); 237 238 extern int get_option(char **str, int *pint); 239 extern char *get_options(const char *str, int nints, int *ints); 240 extern unsigned long long memparse(const char *ptr, char **retptr); 241 extern bool parse_option_str(const char *str, const char *option); 242 extern char *next_arg(char *args, char **param, char **val); 243 244 extern int core_kernel_text(unsigned long addr); 245 extern int __kernel_text_address(unsigned long addr); 246 extern int kernel_text_address(unsigned long addr); 247 extern int func_ptr_is_kernel_text(void *ptr); 248 249 extern void bust_spinlocks(int yes); 250 251 extern int root_mountflags; 252 253 extern bool early_boot_irqs_disabled; 254 255 /* 256 * Values used for system_state. Ordering of the states must not be changed 257 * as code checks for <, <=, >, >= STATE. 258 */ 259 extern enum system_states { 260 SYSTEM_BOOTING, 261 SYSTEM_SCHEDULING, 262 SYSTEM_FREEING_INITMEM, 263 SYSTEM_RUNNING, 264 SYSTEM_HALT, 265 SYSTEM_POWER_OFF, 266 SYSTEM_RESTART, 267 SYSTEM_SUSPEND, 268 } system_state; 269 270 extern const char hex_asc[]; 271 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 272 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 273 274 static inline char *hex_byte_pack(char *buf, u8 byte) 275 { 276 *buf++ = hex_asc_hi(byte); 277 *buf++ = hex_asc_lo(byte); 278 return buf; 279 } 280 281 extern const char hex_asc_upper[]; 282 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 283 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 284 285 static inline char *hex_byte_pack_upper(char *buf, u8 byte) 286 { 287 *buf++ = hex_asc_upper_hi(byte); 288 *buf++ = hex_asc_upper_lo(byte); 289 return buf; 290 } 291 292 extern int hex_to_bin(char ch); 293 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 294 extern char *bin2hex(char *dst, const void *src, size_t count); 295 296 bool mac_pton(const char *s, u8 *mac); 297 298 /* 299 * General tracing related utility functions - trace_printk(), 300 * tracing_on/tracing_off and tracing_start()/tracing_stop 301 * 302 * Use tracing_on/tracing_off when you want to quickly turn on or off 303 * tracing. It simply enables or disables the recording of the trace events. 304 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 305 * file, which gives a means for the kernel and userspace to interact. 306 * Place a tracing_off() in the kernel where you want tracing to end. 307 * From user space, examine the trace, and then echo 1 > tracing_on 308 * to continue tracing. 309 * 310 * tracing_stop/tracing_start has slightly more overhead. It is used 311 * by things like suspend to ram where disabling the recording of the 312 * trace is not enough, but tracing must actually stop because things 313 * like calling smp_processor_id() may crash the system. 314 * 315 * Most likely, you want to use tracing_on/tracing_off. 316 */ 317 318 enum ftrace_dump_mode { 319 DUMP_NONE, 320 DUMP_ALL, 321 DUMP_ORIG, 322 }; 323 324 #ifdef CONFIG_TRACING 325 void tracing_on(void); 326 void tracing_off(void); 327 int tracing_is_on(void); 328 void tracing_snapshot(void); 329 void tracing_snapshot_alloc(void); 330 331 extern void tracing_start(void); 332 extern void tracing_stop(void); 333 334 static inline __printf(1, 2) 335 void ____trace_printk_check_format(const char *fmt, ...) 336 { 337 } 338 #define __trace_printk_check_format(fmt, args...) \ 339 do { \ 340 if (0) \ 341 ____trace_printk_check_format(fmt, ##args); \ 342 } while (0) 343 344 /** 345 * trace_printk - printf formatting in the ftrace buffer 346 * @fmt: the printf format for printing 347 * 348 * Note: __trace_printk is an internal function for trace_printk() and 349 * the @ip is passed in via the trace_printk() macro. 350 * 351 * This function allows a kernel developer to debug fast path sections 352 * that printk is not appropriate for. By scattering in various 353 * printk like tracing in the code, a developer can quickly see 354 * where problems are occurring. 355 * 356 * This is intended as a debugging tool for the developer only. 357 * Please refrain from leaving trace_printks scattered around in 358 * your code. (Extra memory is used for special buffers that are 359 * allocated when trace_printk() is used.) 360 * 361 * A little optimization trick is done here. If there's only one 362 * argument, there's no need to scan the string for printf formats. 363 * The trace_puts() will suffice. But how can we take advantage of 364 * using trace_puts() when trace_printk() has only one argument? 365 * By stringifying the args and checking the size we can tell 366 * whether or not there are args. __stringify((__VA_ARGS__)) will 367 * turn into "()\0" with a size of 3 when there are no args, anything 368 * else will be bigger. All we need to do is define a string to this, 369 * and then take its size and compare to 3. If it's bigger, use 370 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 371 * let gcc optimize the rest. 372 */ 373 374 #define trace_printk(fmt, ...) \ 375 do { \ 376 char _______STR[] = __stringify((__VA_ARGS__)); \ 377 if (sizeof(_______STR) > 3) \ 378 do_trace_printk(fmt, ##__VA_ARGS__); \ 379 else \ 380 trace_puts(fmt); \ 381 } while (0) 382 383 #define do_trace_printk(fmt, args...) \ 384 do { \ 385 static const char *trace_printk_fmt __used \ 386 __section("__trace_printk_fmt") = \ 387 __builtin_constant_p(fmt) ? fmt : NULL; \ 388 \ 389 __trace_printk_check_format(fmt, ##args); \ 390 \ 391 if (__builtin_constant_p(fmt)) \ 392 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 393 else \ 394 __trace_printk(_THIS_IP_, fmt, ##args); \ 395 } while (0) 396 397 extern __printf(2, 3) 398 int __trace_bprintk(unsigned long ip, const char *fmt, ...); 399 400 extern __printf(2, 3) 401 int __trace_printk(unsigned long ip, const char *fmt, ...); 402 403 /** 404 * trace_puts - write a string into the ftrace buffer 405 * @str: the string to record 406 * 407 * Note: __trace_bputs is an internal function for trace_puts and 408 * the @ip is passed in via the trace_puts macro. 409 * 410 * This is similar to trace_printk() but is made for those really fast 411 * paths that a developer wants the least amount of "Heisenbug" effects, 412 * where the processing of the print format is still too much. 413 * 414 * This function allows a kernel developer to debug fast path sections 415 * that printk is not appropriate for. By scattering in various 416 * printk like tracing in the code, a developer can quickly see 417 * where problems are occurring. 418 * 419 * This is intended as a debugging tool for the developer only. 420 * Please refrain from leaving trace_puts scattered around in 421 * your code. (Extra memory is used for special buffers that are 422 * allocated when trace_puts() is used.) 423 * 424 * Returns: 0 if nothing was written, positive # if string was. 425 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 426 */ 427 428 #define trace_puts(str) ({ \ 429 static const char *trace_printk_fmt __used \ 430 __section("__trace_printk_fmt") = \ 431 __builtin_constant_p(str) ? str : NULL; \ 432 \ 433 if (__builtin_constant_p(str)) \ 434 __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 435 else \ 436 __trace_puts(_THIS_IP_, str, strlen(str)); \ 437 }) 438 extern int __trace_bputs(unsigned long ip, const char *str); 439 extern int __trace_puts(unsigned long ip, const char *str, int size); 440 441 extern void trace_dump_stack(int skip); 442 443 /* 444 * The double __builtin_constant_p is because gcc will give us an error 445 * if we try to allocate the static variable to fmt if it is not a 446 * constant. Even with the outer if statement. 447 */ 448 #define ftrace_vprintk(fmt, vargs) \ 449 do { \ 450 if (__builtin_constant_p(fmt)) { \ 451 static const char *trace_printk_fmt __used \ 452 __section("__trace_printk_fmt") = \ 453 __builtin_constant_p(fmt) ? fmt : NULL; \ 454 \ 455 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 456 } else \ 457 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 458 } while (0) 459 460 extern __printf(2, 0) int 461 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 462 463 extern __printf(2, 0) int 464 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 465 466 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 467 #else 468 static inline void tracing_start(void) { } 469 static inline void tracing_stop(void) { } 470 static inline void trace_dump_stack(int skip) { } 471 472 static inline void tracing_on(void) { } 473 static inline void tracing_off(void) { } 474 static inline int tracing_is_on(void) { return 0; } 475 static inline void tracing_snapshot(void) { } 476 static inline void tracing_snapshot_alloc(void) { } 477 478 static inline __printf(1, 2) 479 int trace_printk(const char *fmt, ...) 480 { 481 return 0; 482 } 483 static __printf(1, 0) inline int 484 ftrace_vprintk(const char *fmt, va_list ap) 485 { 486 return 0; 487 } 488 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 489 #endif /* CONFIG_TRACING */ 490 491 /* This counts to 12. Any more, it will return 13th argument. */ 492 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n 493 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 494 495 #define __CONCAT(a, b) a ## b 496 #define CONCATENATE(a, b) __CONCAT(a, b) 497 498 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 499 #ifdef CONFIG_FTRACE_MCOUNT_RECORD 500 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 501 #endif 502 503 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 504 #define VERIFY_OCTAL_PERMISSIONS(perms) \ 505 (BUILD_BUG_ON_ZERO((perms) < 0) + \ 506 BUILD_BUG_ON_ZERO((perms) > 0777) + \ 507 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 508 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 509 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 510 /* USER_WRITABLE >= GROUP_WRITABLE */ \ 511 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 512 /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 513 BUILD_BUG_ON_ZERO((perms) & 2) + \ 514 (perms)) 515 #endif 516