1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KERNEL_H
3 #define _LINUX_KERNEL_H
6 #include <linux/align.h>
7 #include <linux/limits.h>
8 #include <linux/linkage.h>
9 #include <linux/stddef.h>
10 #include <linux/types.h>
11 #include <linux/compiler.h>
12 #include <linux/bitops.h>
13 #include <linux/kstrtox.h>
14 #include <linux/log2.h>
15 #include <linux/math.h>
16 #include <linux/minmax.h>
17 #include <linux/typecheck.h>
18 #include <linux/panic.h>
19 #include <linux/printk.h>
20 #include <linux/build_bug.h>
21 #include <linux/static_call_types.h>
22 #include <asm/byteorder.h>
24 #include <uapi/linux/kernel.h>
26 #define STACK_MAGIC 0xdeadbeef
29 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
32 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
34 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
36 /* generic data direction definitions */
41 * ARRAY_SIZE - get the number of elements in array @arr
42 * @arr: array to be sized
44 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
46 #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
48 #define u64_to_user_ptr(x) ( \
50 typecheck(u64, (x)); \
51 (void __user *)(uintptr_t)(x); \
55 #define typeof_member(T, m) typeof(((T*)0)->m)
57 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
58 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
61 * upper_32_bits - return bits 32-63 of a number
62 * @n: the number we're accessing
64 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
65 * the "right shift count >= width of type" warning when that quantity is
68 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
71 * lower_32_bits - return bits 0-31 of a number
72 * @n: the number we're accessing
74 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
79 #ifdef CONFIG_PREEMPT_VOLUNTARY
81 extern int __cond_resched(void);
82 # define might_resched() __cond_resched()
84 #elif defined(CONFIG_PREEMPT_DYNAMIC)
86 extern int __cond_resched(void);
88 DECLARE_STATIC_CALL(might_resched, __cond_resched);
90 static __always_inline void might_resched(void)
92 static_call_mod(might_resched)();
97 # define might_resched() do { } while (0)
99 #endif /* CONFIG_PREEMPT_* */
101 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
102 extern void ___might_sleep(const char *file, int line, int preempt_offset);
103 extern void __might_sleep(const char *file, int line, int preempt_offset);
104 extern void __cant_sleep(const char *file, int line, int preempt_offset);
105 extern void __cant_migrate(const char *file, int line);
108 * might_sleep - annotation for functions that can sleep
110 * this macro will print a stack trace if it is executed in an atomic
111 * context (spinlock, irq-handler, ...). Additional sections where blocking is
112 * not allowed can be annotated with non_block_start() and non_block_end()
115 * This is a useful debugging help to be able to catch problems early and not
116 * be bitten later when the calling function happens to sleep when it is not
119 # define might_sleep() \
120 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
122 * cant_sleep - annotation for functions that cannot sleep
124 * this macro will print a stack trace if it is executed with preemption enabled
126 # define cant_sleep() \
127 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
128 # define sched_annotate_sleep() (current->task_state_change = 0)
131 * cant_migrate - annotation for functions that cannot migrate
133 * Will print a stack trace if executed in code which is migratable
135 # define cant_migrate() \
137 if (IS_ENABLED(CONFIG_SMP)) \
138 __cant_migrate(__FILE__, __LINE__); \
142 * non_block_start - annotate the start of section where sleeping is prohibited
144 * This is on behalf of the oom reaper, specifically when it is calling the mmu
145 * notifiers. The problem is that if the notifier were to block on, for example,
146 * mutex_lock() and if the process which holds that mutex were to perform a
147 * sleeping memory allocation, the oom reaper is now blocked on completion of
148 * that memory allocation. Other blocking calls like wait_event() pose similar
151 # define non_block_start() (current->non_block_count++)
153 * non_block_end - annotate the end of section where sleeping is prohibited
155 * Closes a section opened by non_block_start().
157 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
159 static inline void ___might_sleep(const char *file, int line,
160 int preempt_offset) { }
161 static inline void __might_sleep(const char *file, int line,
162 int preempt_offset) { }
163 # define might_sleep() do { might_resched(); } while (0)
164 # define cant_sleep() do { } while (0)
165 # define cant_migrate() do { } while (0)
166 # define sched_annotate_sleep() do { } while (0)
167 # define non_block_start() do { } while (0)
168 # define non_block_end() do { } while (0)
171 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
173 #if defined(CONFIG_MMU) && \
174 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
175 #define might_fault() __might_fault(__FILE__, __LINE__)
176 void __might_fault(const char *file, int line);
178 static inline void might_fault(void) { }
181 void do_exit(long error_code) __noreturn;
182 void complete_and_exit(struct completion *, long) __noreturn;
184 extern int num_to_str(char *buf, int size,
185 unsigned long long num, unsigned int width);
187 /* lib/printf utilities */
189 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
190 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
191 extern __printf(3, 4)
192 int snprintf(char *buf, size_t size, const char *fmt, ...);
193 extern __printf(3, 0)
194 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
195 extern __printf(3, 4)
196 int scnprintf(char *buf, size_t size, const char *fmt, ...);
197 extern __printf(3, 0)
198 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
199 extern __printf(2, 3) __malloc
200 char *kasprintf(gfp_t gfp, const char *fmt, ...);
201 extern __printf(2, 0) __malloc
202 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
203 extern __printf(2, 0)
204 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
207 int sscanf(const char *, const char *, ...);
209 int vsscanf(const char *, const char *, va_list);
211 extern int no_hash_pointers_enable(char *str);
213 extern int get_option(char **str, int *pint);
214 extern char *get_options(const char *str, int nints, int *ints);
215 extern unsigned long long memparse(const char *ptr, char **retptr);
216 extern bool parse_option_str(const char *str, const char *option);
217 extern char *next_arg(char *args, char **param, char **val);
219 extern int core_kernel_text(unsigned long addr);
220 extern int init_kernel_text(unsigned long addr);
221 extern int core_kernel_data(unsigned long addr);
222 extern int __kernel_text_address(unsigned long addr);
223 extern int kernel_text_address(unsigned long addr);
224 extern int func_ptr_is_kernel_text(void *ptr);
226 extern void bust_spinlocks(int yes);
228 extern int root_mountflags;
230 extern bool early_boot_irqs_disabled;
233 * Values used for system_state. Ordering of the states must not be changed
234 * as code checks for <, <=, >, >= STATE.
236 extern enum system_states {
246 extern const char hex_asc[];
247 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
248 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
250 static inline char *hex_byte_pack(char *buf, u8 byte)
252 *buf++ = hex_asc_hi(byte);
253 *buf++ = hex_asc_lo(byte);
257 extern const char hex_asc_upper[];
258 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
259 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
261 static inline char *hex_byte_pack_upper(char *buf, u8 byte)
263 *buf++ = hex_asc_upper_hi(byte);
264 *buf++ = hex_asc_upper_lo(byte);
268 extern int hex_to_bin(char ch);
269 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
270 extern char *bin2hex(char *dst, const void *src, size_t count);
272 bool mac_pton(const char *s, u8 *mac);
275 * General tracing related utility functions - trace_printk(),
276 * tracing_on/tracing_off and tracing_start()/tracing_stop
278 * Use tracing_on/tracing_off when you want to quickly turn on or off
279 * tracing. It simply enables or disables the recording of the trace events.
280 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
281 * file, which gives a means for the kernel and userspace to interact.
282 * Place a tracing_off() in the kernel where you want tracing to end.
283 * From user space, examine the trace, and then echo 1 > tracing_on
284 * to continue tracing.
286 * tracing_stop/tracing_start has slightly more overhead. It is used
287 * by things like suspend to ram where disabling the recording of the
288 * trace is not enough, but tracing must actually stop because things
289 * like calling smp_processor_id() may crash the system.
291 * Most likely, you want to use tracing_on/tracing_off.
294 enum ftrace_dump_mode {
300 #ifdef CONFIG_TRACING
301 void tracing_on(void);
302 void tracing_off(void);
303 int tracing_is_on(void);
304 void tracing_snapshot(void);
305 void tracing_snapshot_alloc(void);
307 extern void tracing_start(void);
308 extern void tracing_stop(void);
310 static inline __printf(1, 2)
311 void ____trace_printk_check_format(const char *fmt, ...)
314 #define __trace_printk_check_format(fmt, args...) \
317 ____trace_printk_check_format(fmt, ##args); \
321 * trace_printk - printf formatting in the ftrace buffer
322 * @fmt: the printf format for printing
324 * Note: __trace_printk is an internal function for trace_printk() and
325 * the @ip is passed in via the trace_printk() macro.
327 * This function allows a kernel developer to debug fast path sections
328 * that printk is not appropriate for. By scattering in various
329 * printk like tracing in the code, a developer can quickly see
330 * where problems are occurring.
332 * This is intended as a debugging tool for the developer only.
333 * Please refrain from leaving trace_printks scattered around in
334 * your code. (Extra memory is used for special buffers that are
335 * allocated when trace_printk() is used.)
337 * A little optimization trick is done here. If there's only one
338 * argument, there's no need to scan the string for printf formats.
339 * The trace_puts() will suffice. But how can we take advantage of
340 * using trace_puts() when trace_printk() has only one argument?
341 * By stringifying the args and checking the size we can tell
342 * whether or not there are args. __stringify((__VA_ARGS__)) will
343 * turn into "()\0" with a size of 3 when there are no args, anything
344 * else will be bigger. All we need to do is define a string to this,
345 * and then take its size and compare to 3. If it's bigger, use
346 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
347 * let gcc optimize the rest.
350 #define trace_printk(fmt, ...) \
352 char _______STR[] = __stringify((__VA_ARGS__)); \
353 if (sizeof(_______STR) > 3) \
354 do_trace_printk(fmt, ##__VA_ARGS__); \
359 #define do_trace_printk(fmt, args...) \
361 static const char *trace_printk_fmt __used \
362 __section("__trace_printk_fmt") = \
363 __builtin_constant_p(fmt) ? fmt : NULL; \
365 __trace_printk_check_format(fmt, ##args); \
367 if (__builtin_constant_p(fmt)) \
368 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
370 __trace_printk(_THIS_IP_, fmt, ##args); \
373 extern __printf(2, 3)
374 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
376 extern __printf(2, 3)
377 int __trace_printk(unsigned long ip, const char *fmt, ...);
380 * trace_puts - write a string into the ftrace buffer
381 * @str: the string to record
383 * Note: __trace_bputs is an internal function for trace_puts and
384 * the @ip is passed in via the trace_puts macro.
386 * This is similar to trace_printk() but is made for those really fast
387 * paths that a developer wants the least amount of "Heisenbug" effects,
388 * where the processing of the print format is still too much.
390 * This function allows a kernel developer to debug fast path sections
391 * that printk is not appropriate for. By scattering in various
392 * printk like tracing in the code, a developer can quickly see
393 * where problems are occurring.
395 * This is intended as a debugging tool for the developer only.
396 * Please refrain from leaving trace_puts scattered around in
397 * your code. (Extra memory is used for special buffers that are
398 * allocated when trace_puts() is used.)
400 * Returns: 0 if nothing was written, positive # if string was.
401 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
404 #define trace_puts(str) ({ \
405 static const char *trace_printk_fmt __used \
406 __section("__trace_printk_fmt") = \
407 __builtin_constant_p(str) ? str : NULL; \
409 if (__builtin_constant_p(str)) \
410 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
412 __trace_puts(_THIS_IP_, str, strlen(str)); \
414 extern int __trace_bputs(unsigned long ip, const char *str);
415 extern int __trace_puts(unsigned long ip, const char *str, int size);
417 extern void trace_dump_stack(int skip);
420 * The double __builtin_constant_p is because gcc will give us an error
421 * if we try to allocate the static variable to fmt if it is not a
422 * constant. Even with the outer if statement.
424 #define ftrace_vprintk(fmt, vargs) \
426 if (__builtin_constant_p(fmt)) { \
427 static const char *trace_printk_fmt __used \
428 __section("__trace_printk_fmt") = \
429 __builtin_constant_p(fmt) ? fmt : NULL; \
431 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
433 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
436 extern __printf(2, 0) int
437 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
439 extern __printf(2, 0) int
440 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
442 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
444 static inline void tracing_start(void) { }
445 static inline void tracing_stop(void) { }
446 static inline void trace_dump_stack(int skip) { }
448 static inline void tracing_on(void) { }
449 static inline void tracing_off(void) { }
450 static inline int tracing_is_on(void) { return 0; }
451 static inline void tracing_snapshot(void) { }
452 static inline void tracing_snapshot_alloc(void) { }
454 static inline __printf(1, 2)
455 int trace_printk(const char *fmt, ...)
459 static __printf(1, 0) inline int
460 ftrace_vprintk(const char *fmt, va_list ap)
464 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
465 #endif /* CONFIG_TRACING */
467 /* This counts to 12. Any more, it will return 13th argument. */
468 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
469 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
471 #define __CONCAT(a, b) a ## b
472 #define CONCATENATE(a, b) __CONCAT(a, b)
475 * container_of - cast a member of a structure out to the containing structure
476 * @ptr: the pointer to the member.
477 * @type: the type of the container struct this is embedded in.
478 * @member: the name of the member within the struct.
481 #define container_of(ptr, type, member) ({ \
482 void *__mptr = (void *)(ptr); \
483 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
484 !__same_type(*(ptr), void), \
485 "pointer type mismatch in container_of()"); \
486 ((type *)(__mptr - offsetof(type, member))); })
489 * container_of_safe - cast a member of a structure out to the containing structure
490 * @ptr: the pointer to the member.
491 * @type: the type of the container struct this is embedded in.
492 * @member: the name of the member within the struct.
494 * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
496 #define container_of_safe(ptr, type, member) ({ \
497 void *__mptr = (void *)(ptr); \
498 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
499 !__same_type(*(ptr), void), \
500 "pointer type mismatch in container_of()"); \
501 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
502 ((type *)(__mptr - offsetof(type, member))); })
504 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
505 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
506 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
509 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
510 #define VERIFY_OCTAL_PERMISSIONS(perms) \
511 (BUILD_BUG_ON_ZERO((perms) < 0) + \
512 BUILD_BUG_ON_ZERO((perms) > 0777) + \
513 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
514 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
515 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
516 /* USER_WRITABLE >= GROUP_WRITABLE */ \
517 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
518 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
519 BUILD_BUG_ON_ZERO((perms) & 2) + \