1 /* SPDX-License-Identifier: GPL-2.0 */
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.
11 #ifndef _LINUX_KERNEL_H
12 #define _LINUX_KERNEL_H
14 #include <linux/stdarg.h>
15 #include <linux/align.h>
16 #include <linux/array_size.h>
17 #include <linux/limits.h>
18 #include <linux/linkage.h>
19 #include <linux/stddef.h>
20 #include <linux/types.h>
21 #include <linux/compiler.h>
22 #include <linux/container_of.h>
23 #include <linux/bitops.h>
24 #include <linux/hex.h>
25 #include <linux/kstrtox.h>
26 #include <linux/log2.h>
27 #include <linux/math.h>
28 #include <linux/minmax.h>
29 #include <linux/typecheck.h>
30 #include <linux/panic.h>
31 #include <linux/printk.h>
32 #include <linux/build_bug.h>
33 #include <linux/sprintf.h>
34 #include <linux/static_call_types.h>
35 #include <linux/instruction_pointer.h>
36 #include <asm/byteorder.h>
38 #include <uapi/linux/kernel.h>
40 #define STACK_MAGIC 0xdeadbeef
43 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
46 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
48 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
50 /* generic data direction definitions */
54 #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
56 #define u64_to_user_ptr(x) ( \
58 typecheck(u64, (x)); \
59 (void __user *)(uintptr_t)(x); \
64 * upper_32_bits - return bits 32-63 of a number
65 * @n: the number we're accessing
67 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
68 * the "right shift count >= width of type" warning when that quantity is
71 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
74 * lower_32_bits - return bits 0-31 of a number
75 * @n: the number we're accessing
77 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
80 * upper_16_bits - return bits 16-31 of a number
81 * @n: the number we're accessing
83 #define upper_16_bits(n) ((u16)((n) >> 16))
86 * lower_16_bits - return bits 0-15 of a number
87 * @n: the number we're accessing
89 #define lower_16_bits(n) ((u16)((n) & 0xffff))
94 #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
96 extern int __cond_resched(void);
97 # define might_resched() __cond_resched()
99 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
101 extern int __cond_resched(void);
103 DECLARE_STATIC_CALL(might_resched, __cond_resched);
105 static __always_inline void might_resched(void)
107 static_call_mod(might_resched)();
110 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
112 extern int dynamic_might_resched(void);
113 # define might_resched() dynamic_might_resched()
117 # define might_resched() do { } while (0)
119 #endif /* CONFIG_PREEMPT_* */
121 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
122 extern void __might_resched(const char *file, int line, unsigned int offsets);
123 extern void __might_sleep(const char *file, int line);
124 extern void __cant_sleep(const char *file, int line, int preempt_offset);
125 extern void __cant_migrate(const char *file, int line);
128 * might_sleep - annotation for functions that can sleep
130 * this macro will print a stack trace if it is executed in an atomic
131 * context (spinlock, irq-handler, ...). Additional sections where blocking is
132 * not allowed can be annotated with non_block_start() and non_block_end()
135 * This is a useful debugging help to be able to catch problems early and not
136 * be bitten later when the calling function happens to sleep when it is not
139 # define might_sleep() \
140 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
142 * cant_sleep - annotation for functions that cannot sleep
144 * this macro will print a stack trace if it is executed with preemption enabled
146 # define cant_sleep() \
147 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
148 # define sched_annotate_sleep() (current->task_state_change = 0)
151 * cant_migrate - annotation for functions that cannot migrate
153 * Will print a stack trace if executed in code which is migratable
155 # define cant_migrate() \
157 if (IS_ENABLED(CONFIG_SMP)) \
158 __cant_migrate(__FILE__, __LINE__); \
162 * non_block_start - annotate the start of section where sleeping is prohibited
164 * This is on behalf of the oom reaper, specifically when it is calling the mmu
165 * notifiers. The problem is that if the notifier were to block on, for example,
166 * mutex_lock() and if the process which holds that mutex were to perform a
167 * sleeping memory allocation, the oom reaper is now blocked on completion of
168 * that memory allocation. Other blocking calls like wait_event() pose similar
171 # define non_block_start() (current->non_block_count++)
173 * non_block_end - annotate the end of section where sleeping is prohibited
175 * Closes a section opened by non_block_start().
177 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
179 static inline void __might_resched(const char *file, int line,
180 unsigned int offsets) { }
181 static inline void __might_sleep(const char *file, int line) { }
182 # define might_sleep() do { might_resched(); } while (0)
183 # define cant_sleep() do { } while (0)
184 # define cant_migrate() do { } while (0)
185 # define sched_annotate_sleep() do { } while (0)
186 # define non_block_start() do { } while (0)
187 # define non_block_end() do { } while (0)
190 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
192 #if defined(CONFIG_MMU) && \
193 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
194 #define might_fault() __might_fault(__FILE__, __LINE__)
195 void __might_fault(const char *file, int line);
197 static inline void might_fault(void) { }
200 void do_exit(long error_code) __noreturn;
202 extern int get_option(char **str, int *pint);
203 extern char *get_options(const char *str, int nints, int *ints);
204 extern unsigned long long memparse(const char *ptr, char **retptr);
205 extern bool parse_option_str(const char *str, const char *option);
206 extern char *next_arg(char *args, char **param, char **val);
208 extern int core_kernel_text(unsigned long addr);
209 extern int __kernel_text_address(unsigned long addr);
210 extern int kernel_text_address(unsigned long addr);
211 extern int func_ptr_is_kernel_text(void *ptr);
213 extern void bust_spinlocks(int yes);
215 extern int root_mountflags;
217 extern bool early_boot_irqs_disabled;
220 * Values used for system_state. Ordering of the states must not be changed
221 * as code checks for <, <=, >, >= STATE.
223 extern enum system_states {
226 SYSTEM_FREEING_INITMEM,
235 * General tracing related utility functions - trace_printk(),
236 * tracing_on/tracing_off and tracing_start()/tracing_stop
238 * Use tracing_on/tracing_off when you want to quickly turn on or off
239 * tracing. It simply enables or disables the recording of the trace events.
240 * This also corresponds to the user space /sys/kernel/tracing/tracing_on
241 * file, which gives a means for the kernel and userspace to interact.
242 * Place a tracing_off() in the kernel where you want tracing to end.
243 * From user space, examine the trace, and then echo 1 > tracing_on
244 * to continue tracing.
246 * tracing_stop/tracing_start has slightly more overhead. It is used
247 * by things like suspend to ram where disabling the recording of the
248 * trace is not enough, but tracing must actually stop because things
249 * like calling smp_processor_id() may crash the system.
251 * Most likely, you want to use tracing_on/tracing_off.
254 enum ftrace_dump_mode {
260 #ifdef CONFIG_TRACING
261 void tracing_on(void);
262 void tracing_off(void);
263 int tracing_is_on(void);
264 void tracing_snapshot(void);
265 void tracing_snapshot_alloc(void);
267 extern void tracing_start(void);
268 extern void tracing_stop(void);
270 static inline __printf(1, 2)
271 void ____trace_printk_check_format(const char *fmt, ...)
274 #define __trace_printk_check_format(fmt, args...) \
277 ____trace_printk_check_format(fmt, ##args); \
281 * trace_printk - printf formatting in the ftrace buffer
282 * @fmt: the printf format for printing
284 * Note: __trace_printk is an internal function for trace_printk() and
285 * the @ip is passed in via the trace_printk() macro.
287 * This function allows a kernel developer to debug fast path sections
288 * that printk is not appropriate for. By scattering in various
289 * printk like tracing in the code, a developer can quickly see
290 * where problems are occurring.
292 * This is intended as a debugging tool for the developer only.
293 * Please refrain from leaving trace_printks scattered around in
294 * your code. (Extra memory is used for special buffers that are
295 * allocated when trace_printk() is used.)
297 * A little optimization trick is done here. If there's only one
298 * argument, there's no need to scan the string for printf formats.
299 * The trace_puts() will suffice. But how can we take advantage of
300 * using trace_puts() when trace_printk() has only one argument?
301 * By stringifying the args and checking the size we can tell
302 * whether or not there are args. __stringify((__VA_ARGS__)) will
303 * turn into "()\0" with a size of 3 when there are no args, anything
304 * else will be bigger. All we need to do is define a string to this,
305 * and then take its size and compare to 3. If it's bigger, use
306 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
307 * let gcc optimize the rest.
310 #define trace_printk(fmt, ...) \
312 char _______STR[] = __stringify((__VA_ARGS__)); \
313 if (sizeof(_______STR) > 3) \
314 do_trace_printk(fmt, ##__VA_ARGS__); \
319 #define do_trace_printk(fmt, args...) \
321 static const char *trace_printk_fmt __used \
322 __section("__trace_printk_fmt") = \
323 __builtin_constant_p(fmt) ? fmt : NULL; \
325 __trace_printk_check_format(fmt, ##args); \
327 if (__builtin_constant_p(fmt)) \
328 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
330 __trace_printk(_THIS_IP_, fmt, ##args); \
333 extern __printf(2, 3)
334 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
336 extern __printf(2, 3)
337 int __trace_printk(unsigned long ip, const char *fmt, ...);
340 * trace_puts - write a string into the ftrace buffer
341 * @str: the string to record
343 * Note: __trace_bputs is an internal function for trace_puts and
344 * the @ip is passed in via the trace_puts macro.
346 * This is similar to trace_printk() but is made for those really fast
347 * paths that a developer wants the least amount of "Heisenbug" effects,
348 * where the processing of the print format is still too much.
350 * This function allows a kernel developer to debug fast path sections
351 * that printk is not appropriate for. By scattering in various
352 * printk like tracing in the code, a developer can quickly see
353 * where problems are occurring.
355 * This is intended as a debugging tool for the developer only.
356 * Please refrain from leaving trace_puts scattered around in
357 * your code. (Extra memory is used for special buffers that are
358 * allocated when trace_puts() is used.)
360 * Returns: 0 if nothing was written, positive # if string was.
361 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
364 #define trace_puts(str) ({ \
365 static const char *trace_printk_fmt __used \
366 __section("__trace_printk_fmt") = \
367 __builtin_constant_p(str) ? str : NULL; \
369 if (__builtin_constant_p(str)) \
370 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
372 __trace_puts(_THIS_IP_, str, strlen(str)); \
374 extern int __trace_bputs(unsigned long ip, const char *str);
375 extern int __trace_puts(unsigned long ip, const char *str, int size);
377 extern void trace_dump_stack(int skip);
380 * The double __builtin_constant_p is because gcc will give us an error
381 * if we try to allocate the static variable to fmt if it is not a
382 * constant. Even with the outer if statement.
384 #define ftrace_vprintk(fmt, vargs) \
386 if (__builtin_constant_p(fmt)) { \
387 static const char *trace_printk_fmt __used \
388 __section("__trace_printk_fmt") = \
389 __builtin_constant_p(fmt) ? fmt : NULL; \
391 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
393 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
396 extern __printf(2, 0) int
397 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
399 extern __printf(2, 0) int
400 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
402 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
404 static inline void tracing_start(void) { }
405 static inline void tracing_stop(void) { }
406 static inline void trace_dump_stack(int skip) { }
408 static inline void tracing_on(void) { }
409 static inline void tracing_off(void) { }
410 static inline int tracing_is_on(void) { return 0; }
411 static inline void tracing_snapshot(void) { }
412 static inline void tracing_snapshot_alloc(void) { }
414 static inline __printf(1, 2)
415 int trace_printk(const char *fmt, ...)
419 static __printf(1, 0) inline int
420 ftrace_vprintk(const char *fmt, va_list ap)
424 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
425 #endif /* CONFIG_TRACING */
427 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
428 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
429 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
432 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
433 #define VERIFY_OCTAL_PERMISSIONS(perms) \
434 (BUILD_BUG_ON_ZERO((perms) < 0) + \
435 BUILD_BUG_ON_ZERO((perms) > 0777) + \
436 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
437 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
438 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
439 /* USER_WRITABLE >= GROUP_WRITABLE */ \
440 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
441 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
442 BUILD_BUG_ON_ZERO((perms) & 2) + \