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