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