net: dev: Makes sure netif_rx() can be invoked in any context.
[linux-block.git] / include / linux / kernel.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
40cbf09f
AS
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 */
1da177e4
LT
11#ifndef _LINUX_KERNEL_H
12#define _LINUX_KERNEL_H
13
c0891ac1 14#include <linux/stdarg.h>
08c5188e 15#include <linux/align.h>
54d50897 16#include <linux/limits.h>
1da177e4
LT
17#include <linux/linkage.h>
18#include <linux/stddef.h>
19#include <linux/types.h>
20#include <linux/compiler.h>
d2a8ebbf 21#include <linux/container_of.h>
1da177e4 22#include <linux/bitops.h>
4c527293 23#include <linux/kstrtox.h>
f0d1b0b3 24#include <linux/log2.h>
aa6159ab 25#include <linux/math.h>
b296a6d5 26#include <linux/minmax.h>
e0deaff4 27#include <linux/typecheck.h>
f39650de 28#include <linux/panic.h>
968ab183 29#include <linux/printk.h>
c7acec71 30#include <linux/build_bug.h>
b965f1dd 31#include <linux/static_call_types.h>
e52340de 32#include <linux/instruction_pointer.h>
1da177e4 33#include <asm/byteorder.h>
aa6159ab 34
607ca46e 35#include <uapi/linux/kernel.h>
1da177e4 36
1da177e4
LT
37#define STACK_MAGIC 0xdeadbeef
38
e8c97af0
RD
39/**
40 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
41 * @x: value to repeat
42 *
43 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
44 */
44696908
DM
45#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
46
d3849953
CH
47/* generic data direction definitions */
48#define READ 0
49#define WRITE 1
50
e8c97af0
RD
51/**
52 * ARRAY_SIZE - get the number of elements in array @arr
53 * @arr: array to be sized
54 */
c5e631cf
RR
55#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
56
0ab1438b
MY
57#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
58
3ed605bc
GP
59#define u64_to_user_ptr(x) ( \
60{ \
a0fe2c64
JH
61 typecheck(u64, (x)); \
62 (void __user *)(uintptr_t)(x); \
3ed605bc
GP
63} \
64)
65
218e180e
AM
66/**
67 * upper_32_bits - return bits 32-63 of a number
68 * @n: the number we're accessing
69 *
70 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
71 * the "right shift count >= width of type" warning when that quantity is
72 * 32-bits.
73 */
74#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
75
204b885e
JR
76/**
77 * lower_32_bits - return bits 0-31 of a number
78 * @n: the number we're accessing
79 */
ef91bb19 80#define lower_32_bits(n) ((u32)((n) & 0xffffffff))
204b885e 81
03cb4473
JK
82/**
83 * upper_16_bits - return bits 16-31 of a number
84 * @n: the number we're accessing
85 */
86#define upper_16_bits(n) ((u16)((n) >> 16))
87
88/**
89 * lower_16_bits - return bits 0-15 of a number
90 * @n: the number we're accessing
91 */
92#define lower_16_bits(n) ((u16)((n) & 0xffff))
93
1da177e4 94struct completion;
df2e71fb 95struct user;
1da177e4 96
a8b76910 97#ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
b965f1dd
PZI
98
99extern int __cond_resched(void);
100# define might_resched() __cond_resched()
101
102#elif defined(CONFIG_PREEMPT_DYNAMIC)
103
104extern int __cond_resched(void);
105
106DECLARE_STATIC_CALL(might_resched, __cond_resched);
107
108static __always_inline void might_resched(void)
109{
ef72661e 110 static_call_mod(might_resched)();
b965f1dd
PZI
111}
112
070cb065 113#else
b965f1dd 114
070cb065 115# define might_resched() do { } while (0)
b965f1dd
PZI
116
117#endif /* CONFIG_PREEMPT_* */
070cb065 118
d902db1e 119#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
50e081b9 120extern void __might_resched(const char *file, int line, unsigned int offsets);
42a38756 121extern void __might_sleep(const char *file, int line);
568f1967 122extern void __cant_sleep(const char *file, int line, int preempt_offset);
74d862b6 123extern void __cant_migrate(const char *file, int line);
568f1967 124
1da177e4
LT
125/**
126 * might_sleep - annotation for functions that can sleep
127 *
128 * this macro will print a stack trace if it is executed in an atomic
312364f3
DV
129 * context (spinlock, irq-handler, ...). Additional sections where blocking is
130 * not allowed can be annotated with non_block_start() and non_block_end()
131 * pairs.
1da177e4
LT
132 *
133 * This is a useful debugging help to be able to catch problems early and not
e20ec991 134 * be bitten later when the calling function happens to sleep when it is not
1da177e4
LT
135 * supposed to.
136 */
f8cbd99b 137# define might_sleep() \
42a38756 138 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
568f1967
PZ
139/**
140 * cant_sleep - annotation for functions that cannot sleep
141 *
142 * this macro will print a stack trace if it is executed with preemption enabled
143 */
144# define cant_sleep() \
145 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
00845eb9 146# define sched_annotate_sleep() (current->task_state_change = 0)
74d862b6
TG
147
148/**
149 * cant_migrate - annotation for functions that cannot migrate
150 *
151 * Will print a stack trace if executed in code which is migratable
152 */
153# define cant_migrate() \
154 do { \
155 if (IS_ENABLED(CONFIG_SMP)) \
156 __cant_migrate(__FILE__, __LINE__); \
157 } while (0)
158
312364f3
DV
159/**
160 * non_block_start - annotate the start of section where sleeping is prohibited
161 *
162 * This is on behalf of the oom reaper, specifically when it is calling the mmu
163 * notifiers. The problem is that if the notifier were to block on, for example,
164 * mutex_lock() and if the process which holds that mutex were to perform a
165 * sleeping memory allocation, the oom reaper is now blocked on completion of
166 * that memory allocation. Other blocking calls like wait_event() pose similar
167 * issues.
168 */
169# define non_block_start() (current->non_block_count++)
170/**
171 * non_block_end - annotate the end of section where sleeping is prohibited
172 *
173 * Closes a section opened by non_block_start().
174 */
175# define non_block_end() WARN_ON(current->non_block_count-- == 0)
1da177e4 176#else
874f670e 177 static inline void __might_resched(const char *file, int line,
50e081b9 178 unsigned int offsets) { }
42a38756 179static inline void __might_sleep(const char *file, int line) { }
f8cbd99b 180# define might_sleep() do { might_resched(); } while (0)
568f1967 181# define cant_sleep() do { } while (0)
74d862b6 182# define cant_migrate() do { } while (0)
1029a2b5 183# define sched_annotate_sleep() do { } while (0)
312364f3
DV
184# define non_block_start() do { } while (0)
185# define non_block_end() do { } while (0)
1da177e4
LT
186#endif
187
368a5fa1 188#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
f8cbd99b 189
386e7906
AL
190#if defined(CONFIG_MMU) && \
191 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
9ec23531
DH
192#define might_fault() __might_fault(__FILE__, __LINE__)
193void __might_fault(const char *file, int line);
3ee1afa3 194#else
662bbcb2 195static inline void might_fault(void) { }
3ee1afa3
NP
196#endif
197
9af6528e 198void do_exit(long error_code) __noreturn;
33ee3b2e 199
d1be35cb
AV
200extern int num_to_str(char *buf, int size,
201 unsigned long long num, unsigned int width);
1ac101a5 202
67d0a075
JP
203/* lib/printf utilities */
204
b9075fa9
JP
205extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
206extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
207extern __printf(3, 4)
208int snprintf(char *buf, size_t size, const char *fmt, ...);
209extern __printf(3, 0)
210int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
211extern __printf(3, 4)
212int scnprintf(char *buf, size_t size, const char *fmt, ...);
213extern __printf(3, 0)
214int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
48a27055 215extern __printf(2, 3) __malloc
b9075fa9 216char *kasprintf(gfp_t gfp, const char *fmt, ...);
48a27055 217extern __printf(2, 0) __malloc
8db14860 218char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
0a9df786
RV
219extern __printf(2, 0)
220const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
1da177e4 221
6061d949
JP
222extern __scanf(2, 3)
223int sscanf(const char *, const char *, ...);
224extern __scanf(2, 0)
225int vsscanf(const char *, const char *, va_list);
1da177e4 226
79270291
SB
227extern int no_hash_pointers_enable(char *str);
228
1da177e4
LT
229extern int get_option(char **str, int *pint);
230extern char *get_options(const char *str, int nints, int *ints);
d974ae37 231extern unsigned long long memparse(const char *ptr, char **retptr);
6ccc72b8 232extern bool parse_option_str(const char *str, const char *option);
f51b17c8 233extern char *next_arg(char *args, char **param, char **val);
1da177e4 234
5e376613 235extern int core_kernel_text(unsigned long addr);
1da177e4
LT
236extern int __kernel_text_address(unsigned long addr);
237extern int kernel_text_address(unsigned long addr);
ab7476cf 238extern int func_ptr_is_kernel_text(void *ptr);
ab7476cf 239
1da177e4 240extern void bust_spinlocks(int yes);
5375b708 241
b920de1b 242extern int root_mountflags;
1da177e4 243
2ce802f6
TH
244extern bool early_boot_irqs_disabled;
245
69a78ff2
TG
246/*
247 * Values used for system_state. Ordering of the states must not be changed
248 * as code checks for <, <=, >, >= STATE.
249 */
1da177e4
LT
250extern enum system_states {
251 SYSTEM_BOOTING,
69a78ff2 252 SYSTEM_SCHEDULING,
d2635f20 253 SYSTEM_FREEING_INITMEM,
1da177e4
LT
254 SYSTEM_RUNNING,
255 SYSTEM_HALT,
256 SYSTEM_POWER_OFF,
257 SYSTEM_RESTART,
c1a957d1 258 SYSTEM_SUSPEND,
1da177e4
LT
259} system_state;
260
3fc95772
HH
261extern const char hex_asc[];
262#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
263#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
264
55036ba7 265static inline char *hex_byte_pack(char *buf, u8 byte)
3fc95772
HH
266{
267 *buf++ = hex_asc_hi(byte);
268 *buf++ = hex_asc_lo(byte);
269 return buf;
270}
99eaf3c4 271
c26d436c
AN
272extern const char hex_asc_upper[];
273#define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
274#define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
275
276static inline char *hex_byte_pack_upper(char *buf, u8 byte)
277{
278 *buf++ = hex_asc_upper_hi(byte);
279 *buf++ = hex_asc_upper_lo(byte);
280 return buf;
281}
282
90378889 283extern int hex_to_bin(char ch);
b7804983 284extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
53d91c5c 285extern char *bin2hex(char *dst, const void *src, size_t count);
90378889 286
a69f5edb 287bool mac_pton(const char *s, u8 *mac);
4cd5773a 288
526211bc
IM
289/*
290 * General tracing related utility functions - trace_printk(),
2002c258
SR
291 * tracing_on/tracing_off and tracing_start()/tracing_stop
292 *
293 * Use tracing_on/tracing_off when you want to quickly turn on or off
294 * tracing. It simply enables or disables the recording of the trace events.
156f5a78 295 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
2002c258
SR
296 * file, which gives a means for the kernel and userspace to interact.
297 * Place a tracing_off() in the kernel where you want tracing to end.
298 * From user space, examine the trace, and then echo 1 > tracing_on
299 * to continue tracing.
300 *
301 * tracing_stop/tracing_start has slightly more overhead. It is used
302 * by things like suspend to ram where disabling the recording of the
303 * trace is not enough, but tracing must actually stop because things
304 * like calling smp_processor_id() may crash the system.
305 *
306 * Most likely, you want to use tracing_on/tracing_off.
526211bc 307 */
cecbca96
FW
308
309enum ftrace_dump_mode {
310 DUMP_NONE,
311 DUMP_ALL,
312 DUMP_ORIG,
313};
314
526211bc 315#ifdef CONFIG_TRACING
93d68e52
SR
316void tracing_on(void);
317void tracing_off(void);
318int tracing_is_on(void);
ad909e21
SRRH
319void tracing_snapshot(void);
320void tracing_snapshot_alloc(void);
93d68e52 321
526211bc
IM
322extern void tracing_start(void);
323extern void tracing_stop(void);
526211bc 324
b9075fa9
JP
325static inline __printf(1, 2)
326void ____trace_printk_check_format(const char *fmt, ...)
769b0441
FW
327{
328}
329#define __trace_printk_check_format(fmt, args...) \
330do { \
331 if (0) \
332 ____trace_printk_check_format(fmt, ##args); \
333} while (0)
334
526211bc
IM
335/**
336 * trace_printk - printf formatting in the ftrace buffer
337 * @fmt: the printf format for printing
338 *
e8c97af0
RD
339 * Note: __trace_printk is an internal function for trace_printk() and
340 * the @ip is passed in via the trace_printk() macro.
526211bc
IM
341 *
342 * This function allows a kernel developer to debug fast path sections
343 * that printk is not appropriate for. By scattering in various
344 * printk like tracing in the code, a developer can quickly see
345 * where problems are occurring.
346 *
347 * This is intended as a debugging tool for the developer only.
348 * Please refrain from leaving trace_printks scattered around in
09ae7234 349 * your code. (Extra memory is used for special buffers that are
e8c97af0 350 * allocated when trace_printk() is used.)
9d3c752c 351 *
8730662d 352 * A little optimization trick is done here. If there's only one
9d3c752c
SRRH
353 * argument, there's no need to scan the string for printf formats.
354 * The trace_puts() will suffice. But how can we take advantage of
355 * using trace_puts() when trace_printk() has only one argument?
356 * By stringifying the args and checking the size we can tell
357 * whether or not there are args. __stringify((__VA_ARGS__)) will
358 * turn into "()\0" with a size of 3 when there are no args, anything
359 * else will be bigger. All we need to do is define a string to this,
360 * and then take its size and compare to 3. If it's bigger, use
361 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
362 * let gcc optimize the rest.
526211bc 363 */
769b0441 364
9d3c752c
SRRH
365#define trace_printk(fmt, ...) \
366do { \
367 char _______STR[] = __stringify((__VA_ARGS__)); \
368 if (sizeof(_______STR) > 3) \
369 do_trace_printk(fmt, ##__VA_ARGS__); \
370 else \
371 trace_puts(fmt); \
372} while (0)
373
374#define do_trace_printk(fmt, args...) \
769b0441 375do { \
3debb0a9 376 static const char *trace_printk_fmt __used \
33def849 377 __section("__trace_printk_fmt") = \
07d777fe
SR
378 __builtin_constant_p(fmt) ? fmt : NULL; \
379 \
769b0441 380 __trace_printk_check_format(fmt, ##args); \
48ead020 381 \
07d777fe 382 if (__builtin_constant_p(fmt)) \
48ead020 383 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
07d777fe
SR
384 else \
385 __trace_printk(_THIS_IP_, fmt, ##args); \
769b0441
FW
386} while (0)
387
b9075fa9
JP
388extern __printf(2, 3)
389int __trace_bprintk(unsigned long ip, const char *fmt, ...);
48ead020 390
b9075fa9
JP
391extern __printf(2, 3)
392int __trace_printk(unsigned long ip, const char *fmt, ...);
769b0441 393
09ae7234
SRRH
394/**
395 * trace_puts - write a string into the ftrace buffer
396 * @str: the string to record
397 *
398 * Note: __trace_bputs is an internal function for trace_puts and
399 * the @ip is passed in via the trace_puts macro.
400 *
401 * This is similar to trace_printk() but is made for those really fast
e8c97af0 402 * paths that a developer wants the least amount of "Heisenbug" effects,
09ae7234
SRRH
403 * where the processing of the print format is still too much.
404 *
405 * This function allows a kernel developer to debug fast path sections
406 * that printk is not appropriate for. By scattering in various
407 * printk like tracing in the code, a developer can quickly see
408 * where problems are occurring.
409 *
410 * This is intended as a debugging tool for the developer only.
411 * Please refrain from leaving trace_puts scattered around in
412 * your code. (Extra memory is used for special buffers that are
e8c97af0 413 * allocated when trace_puts() is used.)
09ae7234
SRRH
414 *
415 * Returns: 0 if nothing was written, positive # if string was.
416 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
417 */
418
09ae7234 419#define trace_puts(str) ({ \
3debb0a9 420 static const char *trace_printk_fmt __used \
33def849 421 __section("__trace_printk_fmt") = \
09ae7234
SRRH
422 __builtin_constant_p(str) ? str : NULL; \
423 \
424 if (__builtin_constant_p(str)) \
425 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
426 else \
427 __trace_puts(_THIS_IP_, str, strlen(str)); \
428})
bcf312cf
SR
429extern int __trace_bputs(unsigned long ip, const char *str);
430extern int __trace_puts(unsigned long ip, const char *str, int size);
09ae7234 431
c142be8e 432extern void trace_dump_stack(int skip);
03889384 433
48ead020
FW
434/*
435 * The double __builtin_constant_p is because gcc will give us an error
436 * if we try to allocate the static variable to fmt if it is not a
437 * constant. Even with the outer if statement.
438 */
769b0441
FW
439#define ftrace_vprintk(fmt, vargs) \
440do { \
48ead020 441 if (__builtin_constant_p(fmt)) { \
3debb0a9 442 static const char *trace_printk_fmt __used \
33def849 443 __section("__trace_printk_fmt") = \
48ead020 444 __builtin_constant_p(fmt) ? fmt : NULL; \
7bffc23e 445 \
48ead020
FW
446 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
447 } else \
448 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
769b0441
FW
449} while (0)
450
8db14860 451extern __printf(2, 0) int
48ead020
FW
452__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
453
8db14860 454extern __printf(2, 0) int
526211bc 455__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
769b0441 456
cecbca96 457extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
526211bc 458#else
526211bc
IM
459static inline void tracing_start(void) { }
460static inline void tracing_stop(void) { }
e67bc51e 461static inline void trace_dump_stack(int skip) { }
93d68e52
SR
462
463static inline void tracing_on(void) { }
464static inline void tracing_off(void) { }
465static inline int tracing_is_on(void) { return 0; }
ad909e21
SRRH
466static inline void tracing_snapshot(void) { }
467static inline void tracing_snapshot_alloc(void) { }
93d68e52 468
60efc15a
MH
469static inline __printf(1, 2)
470int trace_printk(const char *fmt, ...)
526211bc
IM
471{
472 return 0;
473}
8db14860 474static __printf(1, 0) inline int
526211bc
IM
475ftrace_vprintk(const char *fmt, va_list ap)
476{
477 return 0;
478}
cecbca96 479static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
769b0441 480#endif /* CONFIG_TRACING */
526211bc 481
cf14f27f
AS
482/* This counts to 12. Any more, it will return 13th argument. */
483#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
484#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
485
486#define __CONCAT(a, b) a ## b
487#define CONCATENATE(a, b) __CONCAT(a, b)
488
b9d4f426
AL
489/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
490#ifdef CONFIG_FTRACE_MCOUNT_RECORD
491# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
492#endif
9d00f92f 493
58f86cc8 494/* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
28b8d0c8
GCM
495#define VERIFY_OCTAL_PERMISSIONS(perms) \
496 (BUILD_BUG_ON_ZERO((perms) < 0) + \
497 BUILD_BUG_ON_ZERO((perms) > 0777) + \
498 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
499 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
500 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
501 /* USER_WRITABLE >= GROUP_WRITABLE */ \
502 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
503 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
504 BUILD_BUG_ON_ZERO((perms) & 2) + \
58f86cc8 505 (perms))
1da177e4 506#endif