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