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