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