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