io_uring: split out cmd api into a separate header
[linux-block.git] / include / linux / kernel.h
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 <asm/byteorder.h>
37
38 #include <uapi/linux/kernel.h>
39
40 #define STACK_MAGIC     0xdeadbeef
41
42 /**
43  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
44  * @x: value to repeat
45  *
46  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
47  */
48 #define REPEAT_BYTE(x)  ((~0ul / 0xff) * (x))
49
50 /* generic data direction definitions */
51 #define READ                    0
52 #define WRITE                   1
53
54 #define PTR_IF(cond, ptr)       ((cond) ? (ptr) : NULL)
55
56 #define u64_to_user_ptr(x) (            \
57 {                                       \
58         typecheck(u64, (x));            \
59         (void __user *)(uintptr_t)(x);  \
60 }                                       \
61 )
62
63 /**
64  * upper_32_bits - return bits 32-63 of a number
65  * @n: the number we're accessing
66  *
67  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
68  * the "right shift count >= width of type" warning when that quantity is
69  * 32-bits.
70  */
71 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
72
73 /**
74  * lower_32_bits - return bits 0-31 of a number
75  * @n: the number we're accessing
76  */
77 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
78
79 /**
80  * upper_16_bits - return bits 16-31 of a number
81  * @n: the number we're accessing
82  */
83 #define upper_16_bits(n) ((u16)((n) >> 16))
84
85 /**
86  * lower_16_bits - return bits 0-15 of a number
87  * @n: the number we're accessing
88  */
89 #define lower_16_bits(n) ((u16)((n) & 0xffff))
90
91 struct completion;
92 struct user;
93
94 #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
95
96 extern int __cond_resched(void);
97 # define might_resched() __cond_resched()
98
99 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
100
101 extern int __cond_resched(void);
102
103 DECLARE_STATIC_CALL(might_resched, __cond_resched);
104
105 static __always_inline void might_resched(void)
106 {
107         static_call_mod(might_resched)();
108 }
109
110 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
111
112 extern int dynamic_might_resched(void);
113 # define might_resched() dynamic_might_resched()
114
115 #else
116
117 # define might_resched() do { } while (0)
118
119 #endif /* CONFIG_PREEMPT_* */
120
121 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
122 extern void __might_resched(const char *file, int line, unsigned int offsets);
123 extern void __might_sleep(const char *file, int line);
124 extern void __cant_sleep(const char *file, int line, int preempt_offset);
125 extern void __cant_migrate(const char *file, int line);
126
127 /**
128  * might_sleep - annotation for functions that can sleep
129  *
130  * this macro will print a stack trace if it is executed in an atomic
131  * context (spinlock, irq-handler, ...). Additional sections where blocking is
132  * not allowed can be annotated with non_block_start() and non_block_end()
133  * pairs.
134  *
135  * This is a useful debugging help to be able to catch problems early and not
136  * be bitten later when the calling function happens to sleep when it is not
137  * supposed to.
138  */
139 # define might_sleep() \
140         do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
141 /**
142  * cant_sleep - annotation for functions that cannot sleep
143  *
144  * this macro will print a stack trace if it is executed with preemption enabled
145  */
146 # define cant_sleep() \
147         do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
148 # define sched_annotate_sleep() (current->task_state_change = 0)
149
150 /**
151  * cant_migrate - annotation for functions that cannot migrate
152  *
153  * Will print a stack trace if executed in code which is migratable
154  */
155 # define cant_migrate()                                                 \
156         do {                                                            \
157                 if (IS_ENABLED(CONFIG_SMP))                             \
158                         __cant_migrate(__FILE__, __LINE__);             \
159         } while (0)
160
161 /**
162  * non_block_start - annotate the start of section where sleeping is prohibited
163  *
164  * This is on behalf of the oom reaper, specifically when it is calling the mmu
165  * notifiers. The problem is that if the notifier were to block on, for example,
166  * mutex_lock() and if the process which holds that mutex were to perform a
167  * sleeping memory allocation, the oom reaper is now blocked on completion of
168  * that memory allocation. Other blocking calls like wait_event() pose similar
169  * issues.
170  */
171 # define non_block_start() (current->non_block_count++)
172 /**
173  * non_block_end - annotate the end of section where sleeping is prohibited
174  *
175  * Closes a section opened by non_block_start().
176  */
177 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
178 #else
179   static inline void __might_resched(const char *file, int line,
180                                      unsigned int offsets) { }
181 static inline void __might_sleep(const char *file, int line) { }
182 # define might_sleep() do { might_resched(); } while (0)
183 # define cant_sleep() do { } while (0)
184 # define cant_migrate()         do { } while (0)
185 # define sched_annotate_sleep() do { } while (0)
186 # define non_block_start() do { } while (0)
187 # define non_block_end() do { } while (0)
188 #endif
189
190 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
191
192 #if defined(CONFIG_MMU) && \
193         (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
194 #define might_fault() __might_fault(__FILE__, __LINE__)
195 void __might_fault(const char *file, int line);
196 #else
197 static inline void might_fault(void) { }
198 #endif
199
200 void do_exit(long error_code) __noreturn;
201
202 extern int get_option(char **str, int *pint);
203 extern char *get_options(const char *str, int nints, int *ints);
204 extern unsigned long long memparse(const char *ptr, char **retptr);
205 extern bool parse_option_str(const char *str, const char *option);
206 extern char *next_arg(char *args, char **param, char **val);
207
208 extern int core_kernel_text(unsigned long addr);
209 extern int __kernel_text_address(unsigned long addr);
210 extern int kernel_text_address(unsigned long addr);
211 extern int func_ptr_is_kernel_text(void *ptr);
212
213 extern void bust_spinlocks(int yes);
214
215 extern int root_mountflags;
216
217 extern bool early_boot_irqs_disabled;
218
219 /*
220  * Values used for system_state. Ordering of the states must not be changed
221  * as code checks for <, <=, >, >= STATE.
222  */
223 extern enum system_states {
224         SYSTEM_BOOTING,
225         SYSTEM_SCHEDULING,
226         SYSTEM_FREEING_INITMEM,
227         SYSTEM_RUNNING,
228         SYSTEM_HALT,
229         SYSTEM_POWER_OFF,
230         SYSTEM_RESTART,
231         SYSTEM_SUSPEND,
232 } system_state;
233
234 /*
235  * General tracing related utility functions - trace_printk(),
236  * tracing_on/tracing_off and tracing_start()/tracing_stop
237  *
238  * Use tracing_on/tracing_off when you want to quickly turn on or off
239  * tracing. It simply enables or disables the recording of the trace events.
240  * This also corresponds to the user space /sys/kernel/tracing/tracing_on
241  * file, which gives a means for the kernel and userspace to interact.
242  * Place a tracing_off() in the kernel where you want tracing to end.
243  * From user space, examine the trace, and then echo 1 > tracing_on
244  * to continue tracing.
245  *
246  * tracing_stop/tracing_start has slightly more overhead. It is used
247  * by things like suspend to ram where disabling the recording of the
248  * trace is not enough, but tracing must actually stop because things
249  * like calling smp_processor_id() may crash the system.
250  *
251  * Most likely, you want to use tracing_on/tracing_off.
252  */
253
254 enum ftrace_dump_mode {
255         DUMP_NONE,
256         DUMP_ALL,
257         DUMP_ORIG,
258 };
259
260 #ifdef CONFIG_TRACING
261 void tracing_on(void);
262 void tracing_off(void);
263 int tracing_is_on(void);
264 void tracing_snapshot(void);
265 void tracing_snapshot_alloc(void);
266
267 extern void tracing_start(void);
268 extern void tracing_stop(void);
269
270 static inline __printf(1, 2)
271 void ____trace_printk_check_format(const char *fmt, ...)
272 {
273 }
274 #define __trace_printk_check_format(fmt, args...)                       \
275 do {                                                                    \
276         if (0)                                                          \
277                 ____trace_printk_check_format(fmt, ##args);             \
278 } while (0)
279
280 /**
281  * trace_printk - printf formatting in the ftrace buffer
282  * @fmt: the printf format for printing
283  *
284  * Note: __trace_printk is an internal function for trace_printk() and
285  *       the @ip is passed in via the trace_printk() macro.
286  *
287  * This function allows a kernel developer to debug fast path sections
288  * that printk is not appropriate for. By scattering in various
289  * printk like tracing in the code, a developer can quickly see
290  * where problems are occurring.
291  *
292  * This is intended as a debugging tool for the developer only.
293  * Please refrain from leaving trace_printks scattered around in
294  * your code. (Extra memory is used for special buffers that are
295  * allocated when trace_printk() is used.)
296  *
297  * A little optimization trick is done here. If there's only one
298  * argument, there's no need to scan the string for printf formats.
299  * The trace_puts() will suffice. But how can we take advantage of
300  * using trace_puts() when trace_printk() has only one argument?
301  * By stringifying the args and checking the size we can tell
302  * whether or not there are args. __stringify((__VA_ARGS__)) will
303  * turn into "()\0" with a size of 3 when there are no args, anything
304  * else will be bigger. All we need to do is define a string to this,
305  * and then take its size and compare to 3. If it's bigger, use
306  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
307  * let gcc optimize the rest.
308  */
309
310 #define trace_printk(fmt, ...)                          \
311 do {                                                    \
312         char _______STR[] = __stringify((__VA_ARGS__)); \
313         if (sizeof(_______STR) > 3)                     \
314                 do_trace_printk(fmt, ##__VA_ARGS__);    \
315         else                                            \
316                 trace_puts(fmt);                        \
317 } while (0)
318
319 #define do_trace_printk(fmt, args...)                                   \
320 do {                                                                    \
321         static const char *trace_printk_fmt __used                      \
322                 __section("__trace_printk_fmt") =                       \
323                 __builtin_constant_p(fmt) ? fmt : NULL;                 \
324                                                                         \
325         __trace_printk_check_format(fmt, ##args);                       \
326                                                                         \
327         if (__builtin_constant_p(fmt))                                  \
328                 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);   \
329         else                                                            \
330                 __trace_printk(_THIS_IP_, fmt, ##args);                 \
331 } while (0)
332
333 extern __printf(2, 3)
334 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
335
336 extern __printf(2, 3)
337 int __trace_printk(unsigned long ip, const char *fmt, ...);
338
339 /**
340  * trace_puts - write a string into the ftrace buffer
341  * @str: the string to record
342  *
343  * Note: __trace_bputs is an internal function for trace_puts and
344  *       the @ip is passed in via the trace_puts macro.
345  *
346  * This is similar to trace_printk() but is made for those really fast
347  * paths that a developer wants the least amount of "Heisenbug" effects,
348  * where the processing of the print format is still too much.
349  *
350  * This function allows a kernel developer to debug fast path sections
351  * that printk is not appropriate for. By scattering in various
352  * printk like tracing in the code, a developer can quickly see
353  * where problems are occurring.
354  *
355  * This is intended as a debugging tool for the developer only.
356  * Please refrain from leaving trace_puts scattered around in
357  * your code. (Extra memory is used for special buffers that are
358  * allocated when trace_puts() is used.)
359  *
360  * Returns: 0 if nothing was written, positive # if string was.
361  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
362  */
363
364 #define trace_puts(str) ({                                              \
365         static const char *trace_printk_fmt __used                      \
366                 __section("__trace_printk_fmt") =                       \
367                 __builtin_constant_p(str) ? str : NULL;                 \
368                                                                         \
369         if (__builtin_constant_p(str))                                  \
370                 __trace_bputs(_THIS_IP_, trace_printk_fmt);             \
371         else                                                            \
372                 __trace_puts(_THIS_IP_, str, strlen(str));              \
373 })
374 extern int __trace_bputs(unsigned long ip, const char *str);
375 extern int __trace_puts(unsigned long ip, const char *str, int size);
376
377 extern void trace_dump_stack(int skip);
378
379 /*
380  * The double __builtin_constant_p is because gcc will give us an error
381  * if we try to allocate the static variable to fmt if it is not a
382  * constant. Even with the outer if statement.
383  */
384 #define ftrace_vprintk(fmt, vargs)                                      \
385 do {                                                                    \
386         if (__builtin_constant_p(fmt)) {                                \
387                 static const char *trace_printk_fmt __used              \
388                   __section("__trace_printk_fmt") =                     \
389                         __builtin_constant_p(fmt) ? fmt : NULL;         \
390                                                                         \
391                 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);  \
392         } else                                                          \
393                 __ftrace_vprintk(_THIS_IP_, fmt, vargs);                \
394 } while (0)
395
396 extern __printf(2, 0) int
397 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
398
399 extern __printf(2, 0) int
400 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
401
402 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
403 #else
404 static inline void tracing_start(void) { }
405 static inline void tracing_stop(void) { }
406 static inline void trace_dump_stack(int skip) { }
407
408 static inline void tracing_on(void) { }
409 static inline void tracing_off(void) { }
410 static inline int tracing_is_on(void) { return 0; }
411 static inline void tracing_snapshot(void) { }
412 static inline void tracing_snapshot_alloc(void) { }
413
414 static inline __printf(1, 2)
415 int trace_printk(const char *fmt, ...)
416 {
417         return 0;
418 }
419 static __printf(1, 0) inline int
420 ftrace_vprintk(const char *fmt, va_list ap)
421 {
422         return 0;
423 }
424 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
425 #endif /* CONFIG_TRACING */
426
427 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
428 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
429 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
430 #endif
431
432 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
433 #define VERIFY_OCTAL_PERMISSIONS(perms)                                         \
434         (BUILD_BUG_ON_ZERO((perms) < 0) +                                       \
435          BUILD_BUG_ON_ZERO((perms) > 0777) +                                    \
436          /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */                \
437          BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +       \
438          BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +              \
439          /* USER_WRITABLE >= GROUP_WRITABLE */                                  \
440          BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +       \
441          /* OTHER_WRITABLE?  Generally considered a bad idea. */                \
442          BUILD_BUG_ON_ZERO((perms) & 2) +                                       \
443          (perms))
444 #endif