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