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 * Values used for system_state. Ordering of the states must not be changed
169 * as code checks for <, <=, >, >= STATE.
170 */
171 extern enum system_states {
172 SYSTEM_BOOTING,
173 SYSTEM_SCHEDULING,
174 SYSTEM_FREEING_INITMEM,
175 SYSTEM_RUNNING,
176 SYSTEM_HALT,
177 SYSTEM_POWER_OFF,
178 SYSTEM_RESTART,
179 SYSTEM_SUSPEND,
180 } system_state;
181
182 /*
183 * General tracing related utility functions - trace_printk(),
184 * tracing_on/tracing_off and tracing_start()/tracing_stop
185 *
186 * Use tracing_on/tracing_off when you want to quickly turn on or off
187 * tracing. It simply enables or disables the recording of the trace events.
188 * This also corresponds to the user space /sys/kernel/tracing/tracing_on
189 * file, which gives a means for the kernel and userspace to interact.
190 * Place a tracing_off() in the kernel where you want tracing to end.
191 * From user space, examine the trace, and then echo 1 > tracing_on
192 * to continue tracing.
193 *
194 * tracing_stop/tracing_start has slightly more overhead. It is used
195 * by things like suspend to ram where disabling the recording of the
196 * trace is not enough, but tracing must actually stop because things
197 * like calling smp_processor_id() may crash the system.
198 *
199 * Most likely, you want to use tracing_on/tracing_off.
200 */
201
202 enum ftrace_dump_mode {
203 DUMP_NONE,
204 DUMP_ALL,
205 DUMP_ORIG,
206 DUMP_PARAM,
207 };
208
209 #ifdef CONFIG_TRACING
210 void tracing_on(void);
211 void tracing_off(void);
212 int tracing_is_on(void);
213 void tracing_snapshot(void);
214 void tracing_snapshot_alloc(void);
215
216 extern void tracing_start(void);
217 extern void tracing_stop(void);
218
219 static inline __printf(1, 2)
____trace_printk_check_format(const char * fmt,...)220 void ____trace_printk_check_format(const char *fmt, ...)
221 {
222 }
223 #define __trace_printk_check_format(fmt, args...) \
224 do { \
225 if (0) \
226 ____trace_printk_check_format(fmt, ##args); \
227 } while (0)
228
229 /**
230 * trace_printk - printf formatting in the ftrace buffer
231 * @fmt: the printf format for printing
232 *
233 * Note: __trace_printk is an internal function for trace_printk() and
234 * the @ip is passed in via the trace_printk() macro.
235 *
236 * This function allows a kernel developer to debug fast path sections
237 * that printk is not appropriate for. By scattering in various
238 * printk like tracing in the code, a developer can quickly see
239 * where problems are occurring.
240 *
241 * This is intended as a debugging tool for the developer only.
242 * Please refrain from leaving trace_printks scattered around in
243 * your code. (Extra memory is used for special buffers that are
244 * allocated when trace_printk() is used.)
245 *
246 * A little optimization trick is done here. If there's only one
247 * argument, there's no need to scan the string for printf formats.
248 * The trace_puts() will suffice. But how can we take advantage of
249 * using trace_puts() when trace_printk() has only one argument?
250 * By stringifying the args and checking the size we can tell
251 * whether or not there are args. __stringify((__VA_ARGS__)) will
252 * turn into "()\0" with a size of 3 when there are no args, anything
253 * else will be bigger. All we need to do is define a string to this,
254 * and then take its size and compare to 3. If it's bigger, use
255 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
256 * let gcc optimize the rest.
257 */
258
259 #define trace_printk(fmt, ...) \
260 do { \
261 char _______STR[] = __stringify((__VA_ARGS__)); \
262 if (sizeof(_______STR) > 3) \
263 do_trace_printk(fmt, ##__VA_ARGS__); \
264 else \
265 trace_puts(fmt); \
266 } while (0)
267
268 #define do_trace_printk(fmt, args...) \
269 do { \
270 static const char *trace_printk_fmt __used \
271 __section("__trace_printk_fmt") = \
272 __builtin_constant_p(fmt) ? fmt : NULL; \
273 \
274 __trace_printk_check_format(fmt, ##args); \
275 \
276 if (__builtin_constant_p(fmt)) \
277 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
278 else \
279 __trace_printk(_THIS_IP_, fmt, ##args); \
280 } while (0)
281
282 extern __printf(2, 3)
283 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
284
285 extern __printf(2, 3)
286 int __trace_printk(unsigned long ip, const char *fmt, ...);
287
288 /**
289 * trace_puts - write a string into the ftrace buffer
290 * @str: the string to record
291 *
292 * Note: __trace_bputs is an internal function for trace_puts and
293 * the @ip is passed in via the trace_puts macro.
294 *
295 * This is similar to trace_printk() but is made for those really fast
296 * paths that a developer wants the least amount of "Heisenbug" effects,
297 * where the processing of the print format is still too much.
298 *
299 * This function allows a kernel developer to debug fast path sections
300 * that printk is not appropriate for. By scattering in various
301 * printk like tracing in the code, a developer can quickly see
302 * where problems are occurring.
303 *
304 * This is intended as a debugging tool for the developer only.
305 * Please refrain from leaving trace_puts scattered around in
306 * your code. (Extra memory is used for special buffers that are
307 * allocated when trace_puts() is used.)
308 *
309 * Returns: 0 if nothing was written, positive # if string was.
310 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
311 */
312
313 #define trace_puts(str) ({ \
314 static const char *trace_printk_fmt __used \
315 __section("__trace_printk_fmt") = \
316 __builtin_constant_p(str) ? str : NULL; \
317 \
318 if (__builtin_constant_p(str)) \
319 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
320 else \
321 __trace_puts(_THIS_IP_, str, strlen(str)); \
322 })
323 extern int __trace_bputs(unsigned long ip, const char *str);
324 extern int __trace_puts(unsigned long ip, const char *str, int size);
325
326 extern void trace_dump_stack(int skip);
327
328 /*
329 * The double __builtin_constant_p is because gcc will give us an error
330 * if we try to allocate the static variable to fmt if it is not a
331 * constant. Even with the outer if statement.
332 */
333 #define ftrace_vprintk(fmt, vargs) \
334 do { \
335 if (__builtin_constant_p(fmt)) { \
336 static const char *trace_printk_fmt __used \
337 __section("__trace_printk_fmt") = \
338 __builtin_constant_p(fmt) ? fmt : NULL; \
339 \
340 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
341 } else \
342 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
343 } while (0)
344
345 extern __printf(2, 0) int
346 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
347
348 extern __printf(2, 0) int
349 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
350
351 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
352 #else
tracing_start(void)353 static inline void tracing_start(void) { }
tracing_stop(void)354 static inline void tracing_stop(void) { }
trace_dump_stack(int skip)355 static inline void trace_dump_stack(int skip) { }
356
tracing_on(void)357 static inline void tracing_on(void) { }
tracing_off(void)358 static inline void tracing_off(void) { }
tracing_is_on(void)359 static inline int tracing_is_on(void) { return 0; }
tracing_snapshot(void)360 static inline void tracing_snapshot(void) { }
tracing_snapshot_alloc(void)361 static inline void tracing_snapshot_alloc(void) { }
362
363 static inline __printf(1, 2)
trace_printk(const char * fmt,...)364 int trace_printk(const char *fmt, ...)
365 {
366 return 0;
367 }
368 static __printf(1, 0) inline int
ftrace_vprintk(const char * fmt,va_list ap)369 ftrace_vprintk(const char *fmt, va_list ap)
370 {
371 return 0;
372 }
ftrace_dump(enum ftrace_dump_mode oops_dump_mode)373 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
374 #endif /* CONFIG_TRACING */
375
376 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
377 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
378 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
379 #endif
380
381 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
382 #define VERIFY_OCTAL_PERMISSIONS(perms) \
383 (BUILD_BUG_ON_ZERO((perms) < 0) + \
384 BUILD_BUG_ON_ZERO((perms) > 0777) + \
385 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
386 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
387 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
388 /* USER_WRITABLE >= GROUP_WRITABLE */ \
389 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
390 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
391 BUILD_BUG_ON_ZERO((perms) & 2) + \
392 (perms))
393 #endif
394