xref: /linux-6.15/include/linux/kernel.h (revision b3e90f37)
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 };
219 
220 #ifdef CONFIG_TRACING
221 void tracing_on(void);
222 void tracing_off(void);
223 int tracing_is_on(void);
224 void tracing_snapshot(void);
225 void tracing_snapshot_alloc(void);
226 
227 extern void tracing_start(void);
228 extern void tracing_stop(void);
229 
230 static inline __printf(1, 2)
231 void ____trace_printk_check_format(const char *fmt, ...)
232 {
233 }
234 #define __trace_printk_check_format(fmt, args...)			\
235 do {									\
236 	if (0)								\
237 		____trace_printk_check_format(fmt, ##args);		\
238 } while (0)
239 
240 /**
241  * trace_printk - printf formatting in the ftrace buffer
242  * @fmt: the printf format for printing
243  *
244  * Note: __trace_printk is an internal function for trace_printk() and
245  *       the @ip is passed in via the trace_printk() macro.
246  *
247  * This function allows a kernel developer to debug fast path sections
248  * that printk is not appropriate for. By scattering in various
249  * printk like tracing in the code, a developer can quickly see
250  * where problems are occurring.
251  *
252  * This is intended as a debugging tool for the developer only.
253  * Please refrain from leaving trace_printks scattered around in
254  * your code. (Extra memory is used for special buffers that are
255  * allocated when trace_printk() is used.)
256  *
257  * A little optimization trick is done here. If there's only one
258  * argument, there's no need to scan the string for printf formats.
259  * The trace_puts() will suffice. But how can we take advantage of
260  * using trace_puts() when trace_printk() has only one argument?
261  * By stringifying the args and checking the size we can tell
262  * whether or not there are args. __stringify((__VA_ARGS__)) will
263  * turn into "()\0" with a size of 3 when there are no args, anything
264  * else will be bigger. All we need to do is define a string to this,
265  * and then take its size and compare to 3. If it's bigger, use
266  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
267  * let gcc optimize the rest.
268  */
269 
270 #define trace_printk(fmt, ...)				\
271 do {							\
272 	char _______STR[] = __stringify((__VA_ARGS__));	\
273 	if (sizeof(_______STR) > 3)			\
274 		do_trace_printk(fmt, ##__VA_ARGS__);	\
275 	else						\
276 		trace_puts(fmt);			\
277 } while (0)
278 
279 #define do_trace_printk(fmt, args...)					\
280 do {									\
281 	static const char *trace_printk_fmt __used			\
282 		__section("__trace_printk_fmt") =			\
283 		__builtin_constant_p(fmt) ? fmt : NULL;			\
284 									\
285 	__trace_printk_check_format(fmt, ##args);			\
286 									\
287 	if (__builtin_constant_p(fmt))					\
288 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
289 	else								\
290 		__trace_printk(_THIS_IP_, fmt, ##args);			\
291 } while (0)
292 
293 extern __printf(2, 3)
294 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
295 
296 extern __printf(2, 3)
297 int __trace_printk(unsigned long ip, const char *fmt, ...);
298 
299 /**
300  * trace_puts - write a string into the ftrace buffer
301  * @str: the string to record
302  *
303  * Note: __trace_bputs is an internal function for trace_puts and
304  *       the @ip is passed in via the trace_puts macro.
305  *
306  * This is similar to trace_printk() but is made for those really fast
307  * paths that a developer wants the least amount of "Heisenbug" effects,
308  * where the processing of the print format is still too much.
309  *
310  * This function allows a kernel developer to debug fast path sections
311  * that printk is not appropriate for. By scattering in various
312  * printk like tracing in the code, a developer can quickly see
313  * where problems are occurring.
314  *
315  * This is intended as a debugging tool for the developer only.
316  * Please refrain from leaving trace_puts scattered around in
317  * your code. (Extra memory is used for special buffers that are
318  * allocated when trace_puts() is used.)
319  *
320  * Returns: 0 if nothing was written, positive # if string was.
321  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
322  */
323 
324 #define trace_puts(str) ({						\
325 	static const char *trace_printk_fmt __used			\
326 		__section("__trace_printk_fmt") =			\
327 		__builtin_constant_p(str) ? str : NULL;			\
328 									\
329 	if (__builtin_constant_p(str))					\
330 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
331 	else								\
332 		__trace_puts(_THIS_IP_, str, strlen(str));		\
333 })
334 extern int __trace_bputs(unsigned long ip, const char *str);
335 extern int __trace_puts(unsigned long ip, const char *str, int size);
336 
337 extern void trace_dump_stack(int skip);
338 
339 /*
340  * The double __builtin_constant_p is because gcc will give us an error
341  * if we try to allocate the static variable to fmt if it is not a
342  * constant. Even with the outer if statement.
343  */
344 #define ftrace_vprintk(fmt, vargs)					\
345 do {									\
346 	if (__builtin_constant_p(fmt)) {				\
347 		static const char *trace_printk_fmt __used		\
348 		  __section("__trace_printk_fmt") =			\
349 			__builtin_constant_p(fmt) ? fmt : NULL;		\
350 									\
351 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
352 	} else								\
353 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
354 } while (0)
355 
356 extern __printf(2, 0) int
357 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
358 
359 extern __printf(2, 0) int
360 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
361 
362 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
363 #else
364 static inline void tracing_start(void) { }
365 static inline void tracing_stop(void) { }
366 static inline void trace_dump_stack(int skip) { }
367 
368 static inline void tracing_on(void) { }
369 static inline void tracing_off(void) { }
370 static inline int tracing_is_on(void) { return 0; }
371 static inline void tracing_snapshot(void) { }
372 static inline void tracing_snapshot_alloc(void) { }
373 
374 static inline __printf(1, 2)
375 int trace_printk(const char *fmt, ...)
376 {
377 	return 0;
378 }
379 static __printf(1, 0) inline int
380 ftrace_vprintk(const char *fmt, va_list ap)
381 {
382 	return 0;
383 }
384 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
385 #endif /* CONFIG_TRACING */
386 
387 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
388 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
389 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
390 #endif
391 
392 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
393 #define VERIFY_OCTAL_PERMISSIONS(perms)						\
394 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
395 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
396 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
397 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
398 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
399 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
400 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
401 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
402 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
403 	 (perms))
404 #endif
405