Merge branch 'for-5.14' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
[linux-2.6-microblaze.git] / include / linux / kernel.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KERNEL_H
3 #define _LINUX_KERNEL_H
4
5 #include <stdarg.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/log2.h>
14 #include <linux/math.h>
15 #include <linux/minmax.h>
16 #include <linux/typecheck.h>
17 #include <linux/printk.h>
18 #include <linux/build_bug.h>
19 #include <linux/static_call_types.h>
20 #include <asm/byteorder.h>
21
22 #include <uapi/linux/kernel.h>
23
24 #define STACK_MAGIC     0xdeadbeef
25
26 /**
27  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
28  * @x: value to repeat
29  *
30  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
31  */
32 #define REPEAT_BYTE(x)  ((~0ul / 0xff) * (x))
33
34 /* generic data direction definitions */
35 #define READ                    0
36 #define WRITE                   1
37
38 /**
39  * ARRAY_SIZE - get the number of elements in array @arr
40  * @arr: array to be sized
41  */
42 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
43
44 #define PTR_IF(cond, ptr)       ((cond) ? (ptr) : NULL)
45
46 #define u64_to_user_ptr(x) (            \
47 {                                       \
48         typecheck(u64, (x));            \
49         (void __user *)(uintptr_t)(x);  \
50 }                                       \
51 )
52
53 #define typeof_member(T, m)     typeof(((T*)0)->m)
54
55 #define _RET_IP_                (unsigned long)__builtin_return_address(0)
56 #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
57
58 /**
59  * upper_32_bits - return bits 32-63 of a number
60  * @n: the number we're accessing
61  *
62  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
63  * the "right shift count >= width of type" warning when that quantity is
64  * 32-bits.
65  */
66 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
67
68 /**
69  * lower_32_bits - return bits 0-31 of a number
70  * @n: the number we're accessing
71  */
72 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
73
74 /**
75  * upper_16_bits - return bits 16-31 of a number
76  * @n: the number we're accessing
77  */
78 #define upper_16_bits(n) ((u16)((n) >> 16))
79
80 /**
81  * lower_16_bits - return bits 0-15 of a number
82  * @n: the number we're accessing
83  */
84 #define lower_16_bits(n) ((u16)((n) & 0xffff))
85
86 struct completion;
87 struct pt_regs;
88 struct user;
89
90 #ifdef CONFIG_PREEMPT_VOLUNTARY
91
92 extern int __cond_resched(void);
93 # define might_resched() __cond_resched()
94
95 #elif defined(CONFIG_PREEMPT_DYNAMIC)
96
97 extern int __cond_resched(void);
98
99 DECLARE_STATIC_CALL(might_resched, __cond_resched);
100
101 static __always_inline void might_resched(void)
102 {
103         static_call_mod(might_resched)();
104 }
105
106 #else
107
108 # define might_resched() do { } while (0)
109
110 #endif /* CONFIG_PREEMPT_* */
111
112 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
113 extern void ___might_sleep(const char *file, int line, int preempt_offset);
114 extern void __might_sleep(const char *file, int line, int preempt_offset);
115 extern void __cant_sleep(const char *file, int line, int preempt_offset);
116 extern void __cant_migrate(const char *file, int line);
117
118 /**
119  * might_sleep - annotation for functions that can sleep
120  *
121  * this macro will print a stack trace if it is executed in an atomic
122  * context (spinlock, irq-handler, ...). Additional sections where blocking is
123  * not allowed can be annotated with non_block_start() and non_block_end()
124  * pairs.
125  *
126  * This is a useful debugging help to be able to catch problems early and not
127  * be bitten later when the calling function happens to sleep when it is not
128  * supposed to.
129  */
130 # define might_sleep() \
131         do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
132 /**
133  * cant_sleep - annotation for functions that cannot sleep
134  *
135  * this macro will print a stack trace if it is executed with preemption enabled
136  */
137 # define cant_sleep() \
138         do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
139 # define sched_annotate_sleep() (current->task_state_change = 0)
140
141 /**
142  * cant_migrate - annotation for functions that cannot migrate
143  *
144  * Will print a stack trace if executed in code which is migratable
145  */
146 # define cant_migrate()                                                 \
147         do {                                                            \
148                 if (IS_ENABLED(CONFIG_SMP))                             \
149                         __cant_migrate(__FILE__, __LINE__);             \
150         } while (0)
151
152 /**
153  * non_block_start - annotate the start of section where sleeping is prohibited
154  *
155  * This is on behalf of the oom reaper, specifically when it is calling the mmu
156  * notifiers. The problem is that if the notifier were to block on, for example,
157  * mutex_lock() and if the process which holds that mutex were to perform a
158  * sleeping memory allocation, the oom reaper is now blocked on completion of
159  * that memory allocation. Other blocking calls like wait_event() pose similar
160  * issues.
161  */
162 # define non_block_start() (current->non_block_count++)
163 /**
164  * non_block_end - annotate the end of section where sleeping is prohibited
165  *
166  * Closes a section opened by non_block_start().
167  */
168 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
169 #else
170   static inline void ___might_sleep(const char *file, int line,
171                                    int preempt_offset) { }
172   static inline void __might_sleep(const char *file, int line,
173                                    int preempt_offset) { }
174 # define might_sleep() do { might_resched(); } while (0)
175 # define cant_sleep() do { } while (0)
176 # define cant_migrate()         do { } while (0)
177 # define sched_annotate_sleep() do { } while (0)
178 # define non_block_start() do { } while (0)
179 # define non_block_end() do { } while (0)
180 #endif
181
182 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
183
184 #if defined(CONFIG_MMU) && \
185         (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
186 #define might_fault() __might_fault(__FILE__, __LINE__)
187 void __might_fault(const char *file, int line);
188 #else
189 static inline void might_fault(void) { }
190 #endif
191
192 extern struct atomic_notifier_head panic_notifier_list;
193 extern long (*panic_blink)(int state);
194 __printf(1, 2)
195 void panic(const char *fmt, ...) __noreturn __cold;
196 void nmi_panic(struct pt_regs *regs, const char *msg);
197 extern void oops_enter(void);
198 extern void oops_exit(void);
199 extern bool oops_may_print(void);
200 void do_exit(long error_code) __noreturn;
201 void complete_and_exit(struct completion *, long) __noreturn;
202
203 /* Internal, do not use. */
204 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
205 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
206
207 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
208 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
209
210 /**
211  * kstrtoul - convert a string to an unsigned long
212  * @s: The start of the string. The string must be null-terminated, and may also
213  *  include a single newline before its terminating null. The first character
214  *  may also be a plus sign, but not a minus sign.
215  * @base: The number base to use. The maximum supported base is 16. If base is
216  *  given as 0, then the base of the string is automatically detected with the
217  *  conventional semantics - If it begins with 0x the number will be parsed as a
218  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
219  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
220  * @res: Where to write the result of the conversion on success.
221  *
222  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
223  * Preferred over simple_strtoul(). Return code must be checked.
224 */
225 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
226 {
227         /*
228          * We want to shortcut function call, but
229          * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
230          */
231         if (sizeof(unsigned long) == sizeof(unsigned long long) &&
232             __alignof__(unsigned long) == __alignof__(unsigned long long))
233                 return kstrtoull(s, base, (unsigned long long *)res);
234         else
235                 return _kstrtoul(s, base, res);
236 }
237
238 /**
239  * kstrtol - convert a string to a long
240  * @s: The start of the string. The string must be null-terminated, and may also
241  *  include a single newline before its terminating null. The first character
242  *  may also be a plus sign or a minus sign.
243  * @base: The number base to use. The maximum supported base is 16. If base is
244  *  given as 0, then the base of the string is automatically detected with the
245  *  conventional semantics - If it begins with 0x the number will be parsed as a
246  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
247  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
248  * @res: Where to write the result of the conversion on success.
249  *
250  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
251  * Preferred over simple_strtol(). Return code must be checked.
252  */
253 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
254 {
255         /*
256          * We want to shortcut function call, but
257          * __builtin_types_compatible_p(long, long long) = 0.
258          */
259         if (sizeof(long) == sizeof(long long) &&
260             __alignof__(long) == __alignof__(long long))
261                 return kstrtoll(s, base, (long long *)res);
262         else
263                 return _kstrtol(s, base, res);
264 }
265
266 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
267 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
268
269 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
270 {
271         return kstrtoull(s, base, res);
272 }
273
274 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
275 {
276         return kstrtoll(s, base, res);
277 }
278
279 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
280 {
281         return kstrtouint(s, base, res);
282 }
283
284 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
285 {
286         return kstrtoint(s, base, res);
287 }
288
289 int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
290 int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
291 int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
292 int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
293 int __must_check kstrtobool(const char *s, bool *res);
294
295 int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
296 int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
297 int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
298 int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
299 int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
300 int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
301 int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
302 int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
303 int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
304 int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
305 int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
306
307 static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
308 {
309         return kstrtoull_from_user(s, count, base, res);
310 }
311
312 static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
313 {
314         return kstrtoll_from_user(s, count, base, res);
315 }
316
317 static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
318 {
319         return kstrtouint_from_user(s, count, base, res);
320 }
321
322 static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
323 {
324         return kstrtoint_from_user(s, count, base, res);
325 }
326
327 /*
328  * Use kstrto<foo> instead.
329  *
330  * NOTE: simple_strto<foo> does not check for the range overflow and,
331  *       depending on the input, may give interesting results.
332  *
333  * Use these functions if and only if you cannot use kstrto<foo>, because
334  * the conversion ends on the first non-digit character, which may be far
335  * beyond the supported range. It might be useful to parse the strings like
336  * 10x50 or 12:21 without altering original string or temporary buffer in use.
337  * Keep in mind above caveat.
338  */
339
340 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
341 extern long simple_strtol(const char *,char **,unsigned int);
342 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
343 extern long long simple_strtoll(const char *,char **,unsigned int);
344
345 extern int num_to_str(char *buf, int size,
346                       unsigned long long num, unsigned int width);
347
348 /* lib/printf utilities */
349
350 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
351 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
352 extern __printf(3, 4)
353 int snprintf(char *buf, size_t size, const char *fmt, ...);
354 extern __printf(3, 0)
355 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
356 extern __printf(3, 4)
357 int scnprintf(char *buf, size_t size, const char *fmt, ...);
358 extern __printf(3, 0)
359 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
360 extern __printf(2, 3) __malloc
361 char *kasprintf(gfp_t gfp, const char *fmt, ...);
362 extern __printf(2, 0) __malloc
363 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
364 extern __printf(2, 0)
365 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
366
367 extern __scanf(2, 3)
368 int sscanf(const char *, const char *, ...);
369 extern __scanf(2, 0)
370 int vsscanf(const char *, const char *, va_list);
371
372 extern int no_hash_pointers_enable(char *str);
373
374 extern int get_option(char **str, int *pint);
375 extern char *get_options(const char *str, int nints, int *ints);
376 extern unsigned long long memparse(const char *ptr, char **retptr);
377 extern bool parse_option_str(const char *str, const char *option);
378 extern char *next_arg(char *args, char **param, char **val);
379
380 extern int core_kernel_text(unsigned long addr);
381 extern int init_kernel_text(unsigned long addr);
382 extern int core_kernel_data(unsigned long addr);
383 extern int __kernel_text_address(unsigned long addr);
384 extern int kernel_text_address(unsigned long addr);
385 extern int func_ptr_is_kernel_text(void *ptr);
386
387 #ifdef CONFIG_SMP
388 extern unsigned int sysctl_oops_all_cpu_backtrace;
389 #else
390 #define sysctl_oops_all_cpu_backtrace 0
391 #endif /* CONFIG_SMP */
392
393 extern void bust_spinlocks(int yes);
394 extern int panic_timeout;
395 extern unsigned long panic_print;
396 extern int panic_on_oops;
397 extern int panic_on_unrecovered_nmi;
398 extern int panic_on_io_nmi;
399 extern int panic_on_warn;
400 extern unsigned long panic_on_taint;
401 extern bool panic_on_taint_nousertaint;
402 extern int sysctl_panic_on_rcu_stall;
403 extern int sysctl_max_rcu_stall_to_panic;
404 extern int sysctl_panic_on_stackoverflow;
405
406 extern bool crash_kexec_post_notifiers;
407
408 /*
409  * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
410  * holds a CPU number which is executing panic() currently. A value of
411  * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
412  */
413 extern atomic_t panic_cpu;
414 #define PANIC_CPU_INVALID       -1
415
416 /*
417  * Only to be used by arch init code. If the user over-wrote the default
418  * CONFIG_PANIC_TIMEOUT, honor it.
419  */
420 static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
421 {
422         if (panic_timeout == arch_default_timeout)
423                 panic_timeout = timeout;
424 }
425 extern const char *print_tainted(void);
426 enum lockdep_ok {
427         LOCKDEP_STILL_OK,
428         LOCKDEP_NOW_UNRELIABLE
429 };
430 extern void add_taint(unsigned flag, enum lockdep_ok);
431 extern int test_taint(unsigned flag);
432 extern unsigned long get_taint(void);
433 extern int root_mountflags;
434
435 extern bool early_boot_irqs_disabled;
436
437 /*
438  * Values used for system_state. Ordering of the states must not be changed
439  * as code checks for <, <=, >, >= STATE.
440  */
441 extern enum system_states {
442         SYSTEM_BOOTING,
443         SYSTEM_SCHEDULING,
444         SYSTEM_RUNNING,
445         SYSTEM_HALT,
446         SYSTEM_POWER_OFF,
447         SYSTEM_RESTART,
448         SYSTEM_SUSPEND,
449 } system_state;
450
451 /* This cannot be an enum because some may be used in assembly source. */
452 #define TAINT_PROPRIETARY_MODULE        0
453 #define TAINT_FORCED_MODULE             1
454 #define TAINT_CPU_OUT_OF_SPEC           2
455 #define TAINT_FORCED_RMMOD              3
456 #define TAINT_MACHINE_CHECK             4
457 #define TAINT_BAD_PAGE                  5
458 #define TAINT_USER                      6
459 #define TAINT_DIE                       7
460 #define TAINT_OVERRIDDEN_ACPI_TABLE     8
461 #define TAINT_WARN                      9
462 #define TAINT_CRAP                      10
463 #define TAINT_FIRMWARE_WORKAROUND       11
464 #define TAINT_OOT_MODULE                12
465 #define TAINT_UNSIGNED_MODULE           13
466 #define TAINT_SOFTLOCKUP                14
467 #define TAINT_LIVEPATCH                 15
468 #define TAINT_AUX                       16
469 #define TAINT_RANDSTRUCT                17
470 #define TAINT_FLAGS_COUNT               18
471 #define TAINT_FLAGS_MAX                 ((1UL << TAINT_FLAGS_COUNT) - 1)
472
473 struct taint_flag {
474         char c_true;    /* character printed when tainted */
475         char c_false;   /* character printed when not tainted */
476         bool module;    /* also show as a per-module taint flag */
477 };
478
479 extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
480
481 extern const char hex_asc[];
482 #define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
483 #define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
484
485 static inline char *hex_byte_pack(char *buf, u8 byte)
486 {
487         *buf++ = hex_asc_hi(byte);
488         *buf++ = hex_asc_lo(byte);
489         return buf;
490 }
491
492 extern const char hex_asc_upper[];
493 #define hex_asc_upper_lo(x)     hex_asc_upper[((x) & 0x0f)]
494 #define hex_asc_upper_hi(x)     hex_asc_upper[((x) & 0xf0) >> 4]
495
496 static inline char *hex_byte_pack_upper(char *buf, u8 byte)
497 {
498         *buf++ = hex_asc_upper_hi(byte);
499         *buf++ = hex_asc_upper_lo(byte);
500         return buf;
501 }
502
503 extern int hex_to_bin(char ch);
504 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
505 extern char *bin2hex(char *dst, const void *src, size_t count);
506
507 bool mac_pton(const char *s, u8 *mac);
508
509 /*
510  * General tracing related utility functions - trace_printk(),
511  * tracing_on/tracing_off and tracing_start()/tracing_stop
512  *
513  * Use tracing_on/tracing_off when you want to quickly turn on or off
514  * tracing. It simply enables or disables the recording of the trace events.
515  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
516  * file, which gives a means for the kernel and userspace to interact.
517  * Place a tracing_off() in the kernel where you want tracing to end.
518  * From user space, examine the trace, and then echo 1 > tracing_on
519  * to continue tracing.
520  *
521  * tracing_stop/tracing_start has slightly more overhead. It is used
522  * by things like suspend to ram where disabling the recording of the
523  * trace is not enough, but tracing must actually stop because things
524  * like calling smp_processor_id() may crash the system.
525  *
526  * Most likely, you want to use tracing_on/tracing_off.
527  */
528
529 enum ftrace_dump_mode {
530         DUMP_NONE,
531         DUMP_ALL,
532         DUMP_ORIG,
533 };
534
535 #ifdef CONFIG_TRACING
536 void tracing_on(void);
537 void tracing_off(void);
538 int tracing_is_on(void);
539 void tracing_snapshot(void);
540 void tracing_snapshot_alloc(void);
541
542 extern void tracing_start(void);
543 extern void tracing_stop(void);
544
545 static inline __printf(1, 2)
546 void ____trace_printk_check_format(const char *fmt, ...)
547 {
548 }
549 #define __trace_printk_check_format(fmt, args...)                       \
550 do {                                                                    \
551         if (0)                                                          \
552                 ____trace_printk_check_format(fmt, ##args);             \
553 } while (0)
554
555 /**
556  * trace_printk - printf formatting in the ftrace buffer
557  * @fmt: the printf format for printing
558  *
559  * Note: __trace_printk is an internal function for trace_printk() and
560  *       the @ip is passed in via the trace_printk() macro.
561  *
562  * This function allows a kernel developer to debug fast path sections
563  * that printk is not appropriate for. By scattering in various
564  * printk like tracing in the code, a developer can quickly see
565  * where problems are occurring.
566  *
567  * This is intended as a debugging tool for the developer only.
568  * Please refrain from leaving trace_printks scattered around in
569  * your code. (Extra memory is used for special buffers that are
570  * allocated when trace_printk() is used.)
571  *
572  * A little optimization trick is done here. If there's only one
573  * argument, there's no need to scan the string for printf formats.
574  * The trace_puts() will suffice. But how can we take advantage of
575  * using trace_puts() when trace_printk() has only one argument?
576  * By stringifying the args and checking the size we can tell
577  * whether or not there are args. __stringify((__VA_ARGS__)) will
578  * turn into "()\0" with a size of 3 when there are no args, anything
579  * else will be bigger. All we need to do is define a string to this,
580  * and then take its size and compare to 3. If it's bigger, use
581  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
582  * let gcc optimize the rest.
583  */
584
585 #define trace_printk(fmt, ...)                          \
586 do {                                                    \
587         char _______STR[] = __stringify((__VA_ARGS__)); \
588         if (sizeof(_______STR) > 3)                     \
589                 do_trace_printk(fmt, ##__VA_ARGS__);    \
590         else                                            \
591                 trace_puts(fmt);                        \
592 } while (0)
593
594 #define do_trace_printk(fmt, args...)                                   \
595 do {                                                                    \
596         static const char *trace_printk_fmt __used                      \
597                 __section("__trace_printk_fmt") =                       \
598                 __builtin_constant_p(fmt) ? fmt : NULL;                 \
599                                                                         \
600         __trace_printk_check_format(fmt, ##args);                       \
601                                                                         \
602         if (__builtin_constant_p(fmt))                                  \
603                 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);   \
604         else                                                            \
605                 __trace_printk(_THIS_IP_, fmt, ##args);                 \
606 } while (0)
607
608 extern __printf(2, 3)
609 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
610
611 extern __printf(2, 3)
612 int __trace_printk(unsigned long ip, const char *fmt, ...);
613
614 /**
615  * trace_puts - write a string into the ftrace buffer
616  * @str: the string to record
617  *
618  * Note: __trace_bputs is an internal function for trace_puts and
619  *       the @ip is passed in via the trace_puts macro.
620  *
621  * This is similar to trace_printk() but is made for those really fast
622  * paths that a developer wants the least amount of "Heisenbug" effects,
623  * where the processing of the print format is still too much.
624  *
625  * This function allows a kernel developer to debug fast path sections
626  * that printk is not appropriate for. By scattering in various
627  * printk like tracing in the code, a developer can quickly see
628  * where problems are occurring.
629  *
630  * This is intended as a debugging tool for the developer only.
631  * Please refrain from leaving trace_puts scattered around in
632  * your code. (Extra memory is used for special buffers that are
633  * allocated when trace_puts() is used.)
634  *
635  * Returns: 0 if nothing was written, positive # if string was.
636  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
637  */
638
639 #define trace_puts(str) ({                                              \
640         static const char *trace_printk_fmt __used                      \
641                 __section("__trace_printk_fmt") =                       \
642                 __builtin_constant_p(str) ? str : NULL;                 \
643                                                                         \
644         if (__builtin_constant_p(str))                                  \
645                 __trace_bputs(_THIS_IP_, trace_printk_fmt);             \
646         else                                                            \
647                 __trace_puts(_THIS_IP_, str, strlen(str));              \
648 })
649 extern int __trace_bputs(unsigned long ip, const char *str);
650 extern int __trace_puts(unsigned long ip, const char *str, int size);
651
652 extern void trace_dump_stack(int skip);
653
654 /*
655  * The double __builtin_constant_p is because gcc will give us an error
656  * if we try to allocate the static variable to fmt if it is not a
657  * constant. Even with the outer if statement.
658  */
659 #define ftrace_vprintk(fmt, vargs)                                      \
660 do {                                                                    \
661         if (__builtin_constant_p(fmt)) {                                \
662                 static const char *trace_printk_fmt __used              \
663                   __section("__trace_printk_fmt") =                     \
664                         __builtin_constant_p(fmt) ? fmt : NULL;         \
665                                                                         \
666                 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);  \
667         } else                                                          \
668                 __ftrace_vprintk(_THIS_IP_, fmt, vargs);                \
669 } while (0)
670
671 extern __printf(2, 0) int
672 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
673
674 extern __printf(2, 0) int
675 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
676
677 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
678 #else
679 static inline void tracing_start(void) { }
680 static inline void tracing_stop(void) { }
681 static inline void trace_dump_stack(int skip) { }
682
683 static inline void tracing_on(void) { }
684 static inline void tracing_off(void) { }
685 static inline int tracing_is_on(void) { return 0; }
686 static inline void tracing_snapshot(void) { }
687 static inline void tracing_snapshot_alloc(void) { }
688
689 static inline __printf(1, 2)
690 int trace_printk(const char *fmt, ...)
691 {
692         return 0;
693 }
694 static __printf(1, 0) inline int
695 ftrace_vprintk(const char *fmt, va_list ap)
696 {
697         return 0;
698 }
699 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
700 #endif /* CONFIG_TRACING */
701
702 /* This counts to 12. Any more, it will return 13th argument. */
703 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
704 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
705
706 #define __CONCAT(a, b) a ## b
707 #define CONCATENATE(a, b) __CONCAT(a, b)
708
709 /**
710  * container_of - cast a member of a structure out to the containing structure
711  * @ptr:        the pointer to the member.
712  * @type:       the type of the container struct this is embedded in.
713  * @member:     the name of the member within the struct.
714  *
715  */
716 #define container_of(ptr, type, member) ({                              \
717         void *__mptr = (void *)(ptr);                                   \
718         BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&   \
719                          !__same_type(*(ptr), void),                    \
720                          "pointer type mismatch in container_of()");    \
721         ((type *)(__mptr - offsetof(type, member))); })
722
723 /**
724  * container_of_safe - cast a member of a structure out to the containing structure
725  * @ptr:        the pointer to the member.
726  * @type:       the type of the container struct this is embedded in.
727  * @member:     the name of the member within the struct.
728  *
729  * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
730  */
731 #define container_of_safe(ptr, type, member) ({                         \
732         void *__mptr = (void *)(ptr);                                   \
733         BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&   \
734                          !__same_type(*(ptr), void),                    \
735                          "pointer type mismatch in container_of()");    \
736         IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) :                     \
737                 ((type *)(__mptr - offsetof(type, member))); })
738
739 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
740 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
741 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
742 #endif
743
744 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
745 #define VERIFY_OCTAL_PERMISSIONS(perms)                                         \
746         (BUILD_BUG_ON_ZERO((perms) < 0) +                                       \
747          BUILD_BUG_ON_ZERO((perms) > 0777) +                                    \
748          /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */                \
749          BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +       \
750          BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +              \
751          /* USER_WRITABLE >= GROUP_WRITABLE */                                  \
752          BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +       \
753          /* OTHER_WRITABLE?  Generally considered a bad idea. */                \
754          BUILD_BUG_ON_ZERO((perms) & 2) +                                       \
755          (perms))
756 #endif