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