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/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/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/trace_printk.h>
35 #include <linux/util_macros.h>
36 #include <linux/wordpart.h>
37
38 #include <asm/byteorder.h>
39
40 #include <uapi/linux/kernel.h>
41
42 struct completion;
43 struct user;
44
45 #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
46 extern int __cond_resched(void);
47 # define might_resched() __cond_resched()
48 #else
49 # define might_resched() do { } while (0)
50 #endif /* CONFIG_PREEMPT_* */
51
52 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
53 extern void __might_resched(const char *file, int line, unsigned int offsets);
54 extern void __might_sleep(const char *file, int line);
55 extern void __cant_sleep(const char *file, int line);
56 extern void __cant_migrate(const char *file, int line);
57
58 /**
59 * might_sleep - annotation for functions that can sleep
60 *
61 * this macro will print a stack trace if it is executed in an atomic
62 * context (spinlock, irq-handler, ...). Additional sections where blocking is
63 * not allowed can be annotated with non_block_start() and non_block_end()
64 * pairs.
65 *
66 * This is a useful debugging help to be able to catch problems early and not
67 * be bitten later when the calling function happens to sleep when it is not
68 * supposed to.
69 */
70 # define might_sleep() \
71 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
72 /**
73 * cant_sleep - annotation for functions that cannot sleep
74 *
75 * this macro will print a stack trace if it is executed with preemption enabled
76 */
77 # define cant_sleep() \
78 do { __cant_sleep(__FILE__, __LINE__); } while (0)
79 # define sched_annotate_sleep() (current->task_state_change = 0)
80
81 /**
82 * cant_migrate - annotation for functions that cannot migrate
83 *
84 * Will print a stack trace if executed in code which is migratable
85 */
86 # define cant_migrate() \
87 do { \
88 if (IS_ENABLED(CONFIG_SMP)) \
89 __cant_migrate(__FILE__, __LINE__); \
90 } while (0)
91
92 /**
93 * non_block_start - annotate the start of section where sleeping is prohibited
94 *
95 * This is on behalf of the oom reaper, specifically when it is calling the mmu
96 * notifiers. The problem is that if the notifier were to block on, for example,
97 * mutex_lock() and if the process which holds that mutex were to perform a
98 * sleeping memory allocation, the oom reaper is now blocked on completion of
99 * that memory allocation. Other blocking calls like wait_event() pose similar
100 * issues.
101 */
102 # define non_block_start() (current->non_block_count++)
103 /**
104 * non_block_end - annotate the end of section where sleeping is prohibited
105 *
106 * Closes a section opened by non_block_start().
107 */
108 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
109 #else
__might_resched(const char * file,int line,unsigned int offsets)110 static inline void __might_resched(const char *file, int line,
111 unsigned int offsets) { }
__might_sleep(const char * file,int line)112 static inline void __might_sleep(const char *file, int line) { }
113 # define might_sleep() do { might_resched(); } while (0)
114 # define cant_sleep() do { } while (0)
115 # define cant_migrate() do { } while (0)
116 # define sched_annotate_sleep() do { } while (0)
117 # define non_block_start() do { } while (0)
118 # define non_block_end() do { } while (0)
119 #endif
120
121 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
122
123 #if defined(CONFIG_MMU) && \
124 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
125 #define might_fault() __might_fault(__FILE__, __LINE__)
126 void __might_fault(const char *file, int line);
127 #else
might_fault(void)128 static inline void might_fault(void) { }
129 #endif
130
131 void do_exit(long error_code) __noreturn;
132
133 extern int core_kernel_text(unsigned long addr);
134 extern int __kernel_text_address(unsigned long addr);
135 extern int kernel_text_address(unsigned long addr);
136 extern int func_ptr_is_kernel_text(void *ptr);
137
138 extern void bust_spinlocks(int yes);
139
140 extern int root_mountflags;
141
142 extern bool early_boot_irqs_disabled;
143
144 /**
145 * enum system_states - Values used for system_state.
146 *
147 * @SYSTEM_BOOTING: %0, no init needed
148 * @SYSTEM_SCHEDULING: system is ready for scheduling; OK to use RCU
149 * @SYSTEM_FREEING_INITMEM: system is freeing all of initmem; almost running
150 * @SYSTEM_RUNNING: system is up and running
151 * @SYSTEM_HALT: system entered clean system halt state
152 * @SYSTEM_POWER_OFF: system entered shutdown/clean power off state
153 * @SYSTEM_RESTART: system entered emergency power off or normal restart
154 * @SYSTEM_SUSPEND: system entered suspend or hibernate state
155 *
156 * Note:
157 * Ordering of the states must not be changed
158 * as code checks for <, <=, >, >= STATE.
159 */
160 enum system_states {
161 SYSTEM_BOOTING,
162 SYSTEM_SCHEDULING,
163 SYSTEM_FREEING_INITMEM,
164 SYSTEM_RUNNING,
165 SYSTEM_HALT,
166 SYSTEM_POWER_OFF,
167 SYSTEM_RESTART,
168 SYSTEM_SUSPEND,
169 };
170 extern enum system_states system_state;
171
172 /* Rebuild everything on CONFIG_DYNAMIC_FTRACE */
173 #ifdef CONFIG_DYNAMIC_FTRACE
174 # define REBUILD_DUE_TO_DYNAMIC_FTRACE
175 #endif
176
177 #endif
178