xref: /linux-6.15/include/linux/kernel.h (revision aa6159ab)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H
31da177e4SLinus Torvalds #define _LINUX_KERNEL_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <stdarg.h>
654d50897SMasahiro Yamada #include <linux/limits.h>
71da177e4SLinus Torvalds #include <linux/linkage.h>
81da177e4SLinus Torvalds #include <linux/stddef.h>
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/compiler.h>
111da177e4SLinus Torvalds #include <linux/bitops.h>
12f0d1b0b3SDavid Howells #include <linux/log2.h>
13*aa6159abSAndy Shevchenko #include <linux/math.h>
14b296a6d5SAndy Shevchenko #include <linux/minmax.h>
15e0deaff4SAndrew Morton #include <linux/typecheck.h>
16968ab183SLinus Torvalds #include <linux/printk.h>
17c7acec71SIan Abbott #include <linux/build_bug.h>
18*aa6159abSAndy Shevchenko 
191da177e4SLinus Torvalds #include <asm/byteorder.h>
20*aa6159abSAndy Shevchenko 
21607ca46eSDavid Howells #include <uapi/linux/kernel.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
241da177e4SLinus Torvalds 
25e8c97af0SRandy Dunlap /**
26e8c97af0SRandy Dunlap  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
27e8c97af0SRandy Dunlap  * @x: value to repeat
28e8c97af0SRandy Dunlap  *
29e8c97af0SRandy Dunlap  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
30e8c97af0SRandy Dunlap  */
3144696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
3244696908SDavid S. Miller 
333ca45a46Szijun_hu /* @a is a power of 2 value */
34a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
35ed067d4aSKrzysztof Kozlowski #define ALIGN_DOWN(x, a)	__ALIGN_KERNEL((x) - ((a) - 1), (a))
369f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
37a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
38229f5879SKishon Vijay Abraham I #define PTR_ALIGN_DOWN(p, a)	((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
39f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
402ea58144SLinus Torvalds 
41d3849953SChristoph Hellwig /* generic data direction definitions */
42d3849953SChristoph Hellwig #define READ			0
43d3849953SChristoph Hellwig #define WRITE			1
44d3849953SChristoph Hellwig 
45e8c97af0SRandy Dunlap /**
46e8c97af0SRandy Dunlap  * ARRAY_SIZE - get the number of elements in array @arr
47e8c97af0SRandy Dunlap  * @arr: array to be sized
48e8c97af0SRandy Dunlap  */
49c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
50c5e631cfSRusty Russell 
513ed605bcSGustavo Padovan #define u64_to_user_ptr(x) (		\
523ed605bcSGustavo Padovan {					\
53a0fe2c64SJann Horn 	typecheck(u64, (x));		\
54a0fe2c64SJann Horn 	(void __user *)(uintptr_t)(x);	\
553ed605bcSGustavo Padovan }					\
563ed605bcSGustavo Padovan )
573ed605bcSGustavo Padovan 
58ce251e0eSAlexey Dobriyan #define typeof_member(T, m)	typeof(((T*)0)->m)
59ce251e0eSAlexey Dobriyan 
60ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
61ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
62ca31e146SEduard - Gabriel Munteanu 
63218e180eSAndrew Morton /**
64218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
65218e180eSAndrew Morton  * @n: the number we're accessing
66218e180eSAndrew Morton  *
67218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
68218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
69218e180eSAndrew Morton  * 32-bits.
70218e180eSAndrew Morton  */
71218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
72218e180eSAndrew Morton 
73204b885eSJoerg Roedel /**
74204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
75204b885eSJoerg Roedel  * @n: the number we're accessing
76204b885eSJoerg Roedel  */
77ef91bb19SHerbert Xu #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
78204b885eSJoerg Roedel 
791da177e4SLinus Torvalds struct completion;
80df2e71fbS[email protected] struct pt_regs;
81df2e71fbS[email protected] struct user;
821da177e4SLinus Torvalds 
83070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
84070cb065SUwe Kleine-König extern int _cond_resched(void);
85070cb065SUwe Kleine-König # define might_resched() _cond_resched()
86070cb065SUwe Kleine-König #else
87070cb065SUwe Kleine-König # define might_resched() do { } while (0)
88070cb065SUwe Kleine-König #endif
89070cb065SUwe Kleine-König 
90d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
91568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset);
92568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset);
93568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset);
9474d862b6SThomas Gleixner extern void __cant_migrate(const char *file, int line);
95568f1967SPeter Zijlstra 
961da177e4SLinus Torvalds /**
971da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
100312364f3SDaniel Vetter  * context (spinlock, irq-handler, ...). Additional sections where blocking is
101312364f3SDaniel Vetter  * not allowed can be annotated with non_block_start() and non_block_end()
102312364f3SDaniel Vetter  * pairs.
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
105e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1061da177e4SLinus Torvalds  * supposed to.
1071da177e4SLinus Torvalds  */
108f8cbd99bSIngo Molnar # define might_sleep() \
109e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
110568f1967SPeter Zijlstra /**
111568f1967SPeter Zijlstra  * cant_sleep - annotation for functions that cannot sleep
112568f1967SPeter Zijlstra  *
113568f1967SPeter Zijlstra  * this macro will print a stack trace if it is executed with preemption enabled
114568f1967SPeter Zijlstra  */
115568f1967SPeter Zijlstra # define cant_sleep() \
116568f1967SPeter Zijlstra 	do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
11700845eb9SLinus Torvalds # define sched_annotate_sleep()	(current->task_state_change = 0)
11874d862b6SThomas Gleixner 
11974d862b6SThomas Gleixner /**
12074d862b6SThomas Gleixner  * cant_migrate - annotation for functions that cannot migrate
12174d862b6SThomas Gleixner  *
12274d862b6SThomas Gleixner  * Will print a stack trace if executed in code which is migratable
12374d862b6SThomas Gleixner  */
12474d862b6SThomas Gleixner # define cant_migrate()							\
12574d862b6SThomas Gleixner 	do {								\
12674d862b6SThomas Gleixner 		if (IS_ENABLED(CONFIG_SMP))				\
12774d862b6SThomas Gleixner 			__cant_migrate(__FILE__, __LINE__);		\
12874d862b6SThomas Gleixner 	} while (0)
12974d862b6SThomas Gleixner 
130312364f3SDaniel Vetter /**
131312364f3SDaniel Vetter  * non_block_start - annotate the start of section where sleeping is prohibited
132312364f3SDaniel Vetter  *
133312364f3SDaniel Vetter  * This is on behalf of the oom reaper, specifically when it is calling the mmu
134312364f3SDaniel Vetter  * notifiers. The problem is that if the notifier were to block on, for example,
135312364f3SDaniel Vetter  * mutex_lock() and if the process which holds that mutex were to perform a
136312364f3SDaniel Vetter  * sleeping memory allocation, the oom reaper is now blocked on completion of
137312364f3SDaniel Vetter  * that memory allocation. Other blocking calls like wait_event() pose similar
138312364f3SDaniel Vetter  * issues.
139312364f3SDaniel Vetter  */
140312364f3SDaniel Vetter # define non_block_start() (current->non_block_count++)
141312364f3SDaniel Vetter /**
142312364f3SDaniel Vetter  * non_block_end - annotate the end of section where sleeping is prohibited
143312364f3SDaniel Vetter  *
144312364f3SDaniel Vetter  * Closes a section opened by non_block_start().
145312364f3SDaniel Vetter  */
146312364f3SDaniel Vetter # define non_block_end() WARN_ON(current->non_block_count-- == 0)
147f8cbd99bSIngo Molnar #else
1483427445aSPeter Zijlstra   static inline void ___might_sleep(const char *file, int line,
1493427445aSPeter Zijlstra 				   int preempt_offset) { }
150d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
151d894837fSSimon Kagstrom 				   int preempt_offset) { }
152f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
153568f1967SPeter Zijlstra # define cant_sleep() do { } while (0)
15474d862b6SThomas Gleixner # define cant_migrate()		do { } while (0)
1551029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0)
156312364f3SDaniel Vetter # define non_block_start() do { } while (0)
157312364f3SDaniel Vetter # define non_block_end() do { } while (0)
158f8cbd99bSIngo Molnar #endif
159f8cbd99bSIngo Molnar 
160368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
161f8cbd99bSIngo Molnar 
162386e7906SAxel Lin #if defined(CONFIG_MMU) && \
163386e7906SAxel Lin 	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
1649ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__)
1659ec23531SDavid Hildenbrand void __might_fault(const char *file, int line);
1663ee1afa3SNick Piggin #else
167662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { }
1683ee1afa3SNick Piggin #endif
1693ee1afa3SNick Piggin 
170e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
171c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
1729402c95fSJoe Perches __printf(1, 2)
1739af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold;
174ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg);
175dd287796SAndrew Morton extern void oops_enter(void);
176dd287796SAndrew Morton extern void oops_exit(void);
17779076e12STiezhu Yang extern bool oops_may_print(void);
1789af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn;
1799af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn;
18033ee3b2eSAlexey Dobriyan 
18133ee3b2eSAlexey Dobriyan /* Internal, do not use. */
18233ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
18333ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
18433ee3b2eSAlexey Dobriyan 
18533ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
18633ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
1874c925d60SEldad Zack 
1884c925d60SEldad Zack /**
1894c925d60SEldad Zack  * kstrtoul - convert a string to an unsigned long
1904c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
1914c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
1924c925d60SEldad Zack  *  may also be a plus sign, but not a minus sign.
1934c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
1944c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
1954c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
1964c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
1974c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
1984c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
1994c925d60SEldad Zack  *
2004c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
201ef0f2685SKars Mulder  * Preferred over simple_strtoul(). Return code must be checked.
2024c925d60SEldad Zack */
20333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
20433ee3b2eSAlexey Dobriyan {
20533ee3b2eSAlexey Dobriyan 	/*
20633ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
20733ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
20833ee3b2eSAlexey Dobriyan 	 */
20933ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
21033ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
21133ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
21233ee3b2eSAlexey Dobriyan 	else
21333ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
21433ee3b2eSAlexey Dobriyan }
21533ee3b2eSAlexey Dobriyan 
2164c925d60SEldad Zack /**
2174c925d60SEldad Zack  * kstrtol - convert a string to a long
2184c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
2194c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
2204c925d60SEldad Zack  *  may also be a plus sign or a minus sign.
2214c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
2224c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
2234c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
2244c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
2254c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
2264c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
2274c925d60SEldad Zack  *
2284c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
229ef0f2685SKars Mulder  * Preferred over simple_strtol(). Return code must be checked.
2304c925d60SEldad Zack  */
23133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
23233ee3b2eSAlexey Dobriyan {
23333ee3b2eSAlexey Dobriyan 	/*
23433ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
23533ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
23633ee3b2eSAlexey Dobriyan 	 */
23733ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
23833ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
23933ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
24033ee3b2eSAlexey Dobriyan 	else
24133ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
24233ee3b2eSAlexey Dobriyan }
24333ee3b2eSAlexey Dobriyan 
24433ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
24533ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
24633ee3b2eSAlexey Dobriyan 
24733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
24833ee3b2eSAlexey Dobriyan {
24933ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
25033ee3b2eSAlexey Dobriyan }
25133ee3b2eSAlexey Dobriyan 
25233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
25333ee3b2eSAlexey Dobriyan {
25433ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
25533ee3b2eSAlexey Dobriyan }
25633ee3b2eSAlexey Dobriyan 
25733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
25833ee3b2eSAlexey Dobriyan {
25933ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
26033ee3b2eSAlexey Dobriyan }
26133ee3b2eSAlexey Dobriyan 
26233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
26333ee3b2eSAlexey Dobriyan {
26433ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
26533ee3b2eSAlexey Dobriyan }
26633ee3b2eSAlexey Dobriyan 
26733ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
26833ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
26933ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
27033ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
271ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res);
27233ee3b2eSAlexey Dobriyan 
273c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
274c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
275c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
276c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
277c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
278c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
279c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
280c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
281c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
282c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
283ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
284c196e32aSAlexey Dobriyan 
285c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
286c196e32aSAlexey Dobriyan {
287c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
288c196e32aSAlexey Dobriyan }
289c196e32aSAlexey Dobriyan 
290c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
291c196e32aSAlexey Dobriyan {
292c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
293c196e32aSAlexey Dobriyan }
294c196e32aSAlexey Dobriyan 
295c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
296c196e32aSAlexey Dobriyan {
297c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
298c196e32aSAlexey Dobriyan }
299c196e32aSAlexey Dobriyan 
300c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
301c196e32aSAlexey Dobriyan {
302c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
303c196e32aSAlexey Dobriyan }
304c196e32aSAlexey Dobriyan 
305885e68e8SAndy Shevchenko /*
306885e68e8SAndy Shevchenko  * Use kstrto<foo> instead.
307885e68e8SAndy Shevchenko  *
308885e68e8SAndy Shevchenko  * NOTE: simple_strto<foo> does not check for the range overflow and,
309885e68e8SAndy Shevchenko  *	 depending on the input, may give interesting results.
310885e68e8SAndy Shevchenko  *
311885e68e8SAndy Shevchenko  * Use these functions if and only if you cannot use kstrto<foo>, because
312885e68e8SAndy Shevchenko  * the conversion ends on the first non-digit character, which may be far
313885e68e8SAndy Shevchenko  * beyond the supported range. It might be useful to parse the strings like
314885e68e8SAndy Shevchenko  * 10x50 or 12:21 without altering original string or temporary buffer in use.
315885e68e8SAndy Shevchenko  * Keep in mind above caveat.
316885e68e8SAndy Shevchenko  */
31767d0a075SJoe Perches 
3181da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
3191da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
3201da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
3211da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
32233ee3b2eSAlexey Dobriyan 
323d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size,
324d1be35cbSAndrei Vagin 		      unsigned long long num, unsigned int width);
3251ac101a5SKAMEZAWA Hiroyuki 
32667d0a075SJoe Perches /* lib/printf utilities */
32767d0a075SJoe Perches 
328b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
329b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
330b9075fa9SJoe Perches extern __printf(3, 4)
331b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
332b9075fa9SJoe Perches extern __printf(3, 0)
333b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
334b9075fa9SJoe Perches extern __printf(3, 4)
335b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
336b9075fa9SJoe Perches extern __printf(3, 0)
337b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
33848a27055SRasmus Villemoes extern __printf(2, 3) __malloc
339b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
34048a27055SRasmus Villemoes extern __printf(2, 0) __malloc
3418db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
3420a9df786SRasmus Villemoes extern __printf(2, 0)
3430a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
3441da177e4SLinus Torvalds 
3456061d949SJoe Perches extern __scanf(2, 3)
3466061d949SJoe Perches int sscanf(const char *, const char *, ...);
3476061d949SJoe Perches extern __scanf(2, 0)
3486061d949SJoe Perches int vsscanf(const char *, const char *, va_list);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
3511da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
352d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
3536ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option);
354f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val);
3551da177e4SLinus Torvalds 
3565e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
3579fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr);
358cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
3591da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
3601da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
361ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
362ab7476cfSArjan van de Ven 
36360c958d8SGuilherme G. Piccoli #ifdef CONFIG_SMP
36460c958d8SGuilherme G. Piccoli extern unsigned int sysctl_oops_all_cpu_backtrace;
36560c958d8SGuilherme G. Piccoli #else
36660c958d8SGuilherme G. Piccoli #define sysctl_oops_all_cpu_backtrace 0
36760c958d8SGuilherme G. Piccoli #endif /* CONFIG_SMP */
36860c958d8SGuilherme G. Piccoli 
3691da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
370aa727107SAdrian Bunk extern int panic_timeout;
37181c9d43fSFeng Tang extern unsigned long panic_print;
3721da177e4SLinus Torvalds extern int panic_on_oops;
3738da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
3745211a242SKurt Garloff extern int panic_on_io_nmi;
3759e3961a0SPrarit Bhargava extern int panic_on_warn;
376db38d5c1SRafael Aquini extern unsigned long panic_on_taint;
377db38d5c1SRafael Aquini extern bool panic_on_taint_nousertaint;
378088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall;
379dfe56404Schao extern int sysctl_max_rcu_stall_to_panic;
38055af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
3815375b708SHATAYAMA Daisuke 
3825375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers;
3835375b708SHATAYAMA Daisuke 
3845800dc3cSJason Baron /*
3851717f209SHidehiro Kawai  * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
3861717f209SHidehiro Kawai  * holds a CPU number which is executing panic() currently. A value of
3871717f209SHidehiro Kawai  * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
3881717f209SHidehiro Kawai  */
3891717f209SHidehiro Kawai extern atomic_t panic_cpu;
3901717f209SHidehiro Kawai #define PANIC_CPU_INVALID	-1
3911717f209SHidehiro Kawai 
3921717f209SHidehiro Kawai /*
3935800dc3cSJason Baron  * Only to be used by arch init code. If the user over-wrote the default
3945800dc3cSJason Baron  * CONFIG_PANIC_TIMEOUT, honor it.
3955800dc3cSJason Baron  */
3965800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
3975800dc3cSJason Baron {
3985800dc3cSJason Baron 	if (panic_timeout == arch_default_timeout)
3995800dc3cSJason Baron 		panic_timeout = timeout;
4005800dc3cSJason Baron }
4011da177e4SLinus Torvalds extern const char *print_tainted(void);
402373d4d09SRusty Russell enum lockdep_ok {
403373d4d09SRusty Russell 	LOCKDEP_STILL_OK,
404373d4d09SRusty Russell 	LOCKDEP_NOW_UNRELIABLE
405373d4d09SRusty Russell };
406373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok);
40725ddbb18SAndi Kleen extern int test_taint(unsigned flag);
40825ddbb18SAndi Kleen extern unsigned long get_taint(void);
409b920de1bSDavid Howells extern int root_mountflags;
4101da177e4SLinus Torvalds 
4112ce802f6STejun Heo extern bool early_boot_irqs_disabled;
4122ce802f6STejun Heo 
41369a78ff2SThomas Gleixner /*
41469a78ff2SThomas Gleixner  * Values used for system_state. Ordering of the states must not be changed
41569a78ff2SThomas Gleixner  * as code checks for <, <=, >, >= STATE.
41669a78ff2SThomas Gleixner  */
4171da177e4SLinus Torvalds extern enum system_states {
4181da177e4SLinus Torvalds 	SYSTEM_BOOTING,
41969a78ff2SThomas Gleixner 	SYSTEM_SCHEDULING,
4201da177e4SLinus Torvalds 	SYSTEM_RUNNING,
4211da177e4SLinus Torvalds 	SYSTEM_HALT,
4221da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
4231da177e4SLinus Torvalds 	SYSTEM_RESTART,
424c1a957d1SThomas Gleixner 	SYSTEM_SUSPEND,
4251da177e4SLinus Torvalds } system_state;
4261da177e4SLinus Torvalds 
42747d4b263SKees Cook /* This cannot be an enum because some may be used in assembly source. */
42825ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
42925ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
4308c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC		2
43125ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
43225ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
43325ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
43425ddbb18SAndi Kleen #define TAINT_USER			6
43525ddbb18SAndi Kleen #define TAINT_DIE			7
43625ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
43725ddbb18SAndi Kleen #define TAINT_WARN			9
43826e9a397SLinus Torvalds #define TAINT_CRAP			10
43992946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
4402449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
44166cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE		13
44269361eefSJosh Hunt #define TAINT_SOFTLOCKUP		14
443c5f45465SSeth Jennings #define TAINT_LIVEPATCH			15
4444efb442cSBorislav Petkov #define TAINT_AUX			16
445bc4f2f54SKees Cook #define TAINT_RANDSTRUCT		17
446bc4f2f54SKees Cook #define TAINT_FLAGS_COUNT		18
447db38d5c1SRafael Aquini #define TAINT_FLAGS_MAX			((1UL << TAINT_FLAGS_COUNT) - 1)
4487fd8329bSPetr Mladek 
4497fd8329bSPetr Mladek struct taint_flag {
4505eb7c0d0SLarry Finger 	char c_true;	/* character printed when tainted */
4515eb7c0d0SLarry Finger 	char c_false;	/* character printed when not tainted */
4527fd8329bSPetr Mladek 	bool module;	/* also show as a per-module taint flag */
4537fd8329bSPetr Mladek };
4547fd8329bSPetr Mladek 
4557fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
4561da177e4SLinus Torvalds 
4573fc95772SHarvey Harrison extern const char hex_asc[];
4583fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
4593fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
4603fc95772SHarvey Harrison 
46155036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
4623fc95772SHarvey Harrison {
4633fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
4643fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
4653fc95772SHarvey Harrison 	return buf;
4663fc95772SHarvey Harrison }
46799eaf3c4SRandy Dunlap 
468c26d436cSAndre Naujoks extern const char hex_asc_upper[];
469c26d436cSAndre Naujoks #define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
470c26d436cSAndre Naujoks #define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]
471c26d436cSAndre Naujoks 
472c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte)
473c26d436cSAndre Naujoks {
474c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_hi(byte);
475c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_lo(byte);
476c26d436cSAndre Naujoks 	return buf;
477c26d436cSAndre Naujoks }
478c26d436cSAndre Naujoks 
47990378889SAndy Shevchenko extern int hex_to_bin(char ch);
480b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
48153d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count);
48290378889SAndy Shevchenko 
483a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac);
4844cd5773aSAndy Shevchenko 
4858a64f336SJoe Perches /*
486526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
4872002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
4882002c258SSteven Rostedt  *
4892002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
4902002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
491156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
4922002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
4932002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
4942002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
4952002c258SSteven Rostedt  * to continue tracing.
4962002c258SSteven Rostedt  *
4972002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
4982002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
4992002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
5002002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
5012002c258SSteven Rostedt  *
5022002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
503526211bcSIngo Molnar  */
504cecbca96SFrederic Weisbecker 
505cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
506cecbca96SFrederic Weisbecker 	DUMP_NONE,
507cecbca96SFrederic Weisbecker 	DUMP_ALL,
508cecbca96SFrederic Weisbecker 	DUMP_ORIG,
509cecbca96SFrederic Weisbecker };
510cecbca96SFrederic Weisbecker 
511526211bcSIngo Molnar #ifdef CONFIG_TRACING
51293d68e52SSteven Rostedt void tracing_on(void);
51393d68e52SSteven Rostedt void tracing_off(void);
51493d68e52SSteven Rostedt int tracing_is_on(void);
515ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
516ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
51793d68e52SSteven Rostedt 
518526211bcSIngo Molnar extern void tracing_start(void);
519526211bcSIngo Molnar extern void tracing_stop(void);
520526211bcSIngo Molnar 
521b9075fa9SJoe Perches static inline __printf(1, 2)
522b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
523769b0441SFrederic Weisbecker {
524769b0441SFrederic Weisbecker }
525769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
526769b0441SFrederic Weisbecker do {									\
527769b0441SFrederic Weisbecker 	if (0)								\
528769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
529769b0441SFrederic Weisbecker } while (0)
530769b0441SFrederic Weisbecker 
531526211bcSIngo Molnar /**
532526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
533526211bcSIngo Molnar  * @fmt: the printf format for printing
534526211bcSIngo Molnar  *
535e8c97af0SRandy Dunlap  * Note: __trace_printk is an internal function for trace_printk() and
536e8c97af0SRandy Dunlap  *       the @ip is passed in via the trace_printk() macro.
537526211bcSIngo Molnar  *
538526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
539526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
540526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
541526211bcSIngo Molnar  * where problems are occurring.
542526211bcSIngo Molnar  *
543526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
544526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
54509ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
546e8c97af0SRandy Dunlap  * allocated when trace_printk() is used.)
5479d3c752cSSteven Rostedt (Red Hat)  *
5488730662dSWei Wang  * A little optimization trick is done here. If there's only one
5499d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
5509d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
5519d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
5529d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
5539d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
5549d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
5559d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
5569d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
5579d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
5589d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
559526211bcSIngo Molnar  */
560769b0441SFrederic Weisbecker 
5619d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
5629d3c752cSSteven Rostedt (Red Hat) do {							\
5639d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
5649d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
5659d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
5669d3c752cSSteven Rostedt (Red Hat) 	else						\
5679d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
5689d3c752cSSteven Rostedt (Red Hat) } while (0)
5699d3c752cSSteven Rostedt (Red Hat) 
5709d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
571769b0441SFrederic Weisbecker do {									\
5723debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
57333def849SJoe Perches 		__section("__trace_printk_fmt") =			\
57448ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
57548ead020SFrederic Weisbecker 									\
57607d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
57707d777feSSteven Rostedt 									\
57807d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
57948ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
58007d777feSSteven Rostedt 	else								\
58148ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
582769b0441SFrederic Weisbecker } while (0)
583769b0441SFrederic Weisbecker 
584b9075fa9SJoe Perches extern __printf(2, 3)
585b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
58648ead020SFrederic Weisbecker 
587b9075fa9SJoe Perches extern __printf(2, 3)
588b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
589769b0441SFrederic Weisbecker 
59009ae7234SSteven Rostedt (Red Hat) /**
59109ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
59209ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
59309ae7234SSteven Rostedt (Red Hat)  *
59409ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
59509ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
59609ae7234SSteven Rostedt (Red Hat)  *
59709ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
598e8c97af0SRandy Dunlap  * paths that a developer wants the least amount of "Heisenbug" effects,
59909ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
60009ae7234SSteven Rostedt (Red Hat)  *
60109ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
60209ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
60309ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
60409ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
60509ae7234SSteven Rostedt (Red Hat)  *
60609ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
60709ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
60809ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
609e8c97af0SRandy Dunlap  * allocated when trace_puts() is used.)
61009ae7234SSteven Rostedt (Red Hat)  *
61109ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
61209ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
61309ae7234SSteven Rostedt (Red Hat)  */
61409ae7234SSteven Rostedt (Red Hat) 
61509ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
6163debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
61733def849SJoe Perches 		__section("__trace_printk_fmt") =			\
61809ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
61909ae7234SSteven Rostedt (Red Hat) 									\
62009ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
62109ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
62209ae7234SSteven Rostedt (Red Hat) 	else								\
62309ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
62409ae7234SSteven Rostedt (Red Hat) })
625bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str);
626bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size);
62709ae7234SSteven Rostedt (Red Hat) 
628c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
62903889384SSteven Rostedt 
63048ead020SFrederic Weisbecker /*
63148ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
63248ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
63348ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
63448ead020SFrederic Weisbecker  */
635769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
636769b0441SFrederic Weisbecker do {									\
63748ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
6383debb0a9SSteven Rostedt (Red Hat) 		static const char *trace_printk_fmt __used		\
63933def849SJoe Perches 		  __section("__trace_printk_fmt") =			\
64048ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
6417bffc23eSIngo Molnar 									\
64248ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
64348ead020SFrederic Weisbecker 	} else								\
64448ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
645769b0441SFrederic Weisbecker } while (0)
646769b0441SFrederic Weisbecker 
6478db14860SNicolas Iooss extern __printf(2, 0) int
64848ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
64948ead020SFrederic Weisbecker 
6508db14860SNicolas Iooss extern __printf(2, 0) int
651526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
652769b0441SFrederic Weisbecker 
653cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
654526211bcSIngo Molnar #else
655526211bcSIngo Molnar static inline void tracing_start(void) { }
656526211bcSIngo Molnar static inline void tracing_stop(void) { }
657e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { }
65893d68e52SSteven Rostedt 
65993d68e52SSteven Rostedt static inline void tracing_on(void) { }
66093d68e52SSteven Rostedt static inline void tracing_off(void) { }
66193d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
662ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
663ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
66493d68e52SSteven Rostedt 
66560efc15aSMichal Hocko static inline __printf(1, 2)
66660efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
667526211bcSIngo Molnar {
668526211bcSIngo Molnar 	return 0;
669526211bcSIngo Molnar }
6708db14860SNicolas Iooss static __printf(1, 0) inline int
671526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
672526211bcSIngo Molnar {
673526211bcSIngo Molnar 	return 0;
674526211bcSIngo Molnar }
675cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
676769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
677526211bcSIngo Molnar 
678cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */
679cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
680cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
681cf14f27fSAlexei Starovoitov 
682cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b
683cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b)
684cf14f27fSAlexei Starovoitov 
6851da177e4SLinus Torvalds /**
6861da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
6871da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
6881da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
6891da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
6901da177e4SLinus Torvalds  *
6911da177e4SLinus Torvalds  */
6921da177e4SLinus Torvalds #define container_of(ptr, type, member) ({				\
693c7acec71SIan Abbott 	void *__mptr = (void *)(ptr);					\
694c7acec71SIan Abbott 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
695c7acec71SIan Abbott 			 !__same_type(*(ptr), void),			\
696c7acec71SIan Abbott 			 "pointer type mismatch in container_of()");	\
697c7acec71SIan Abbott 	((type *)(__mptr - offsetof(type, member))); })
6981da177e4SLinus Torvalds 
69905e6557bSNeilBrown /**
70005e6557bSNeilBrown  * container_of_safe - cast a member of a structure out to the containing structure
70105e6557bSNeilBrown  * @ptr:	the pointer to the member.
70205e6557bSNeilBrown  * @type:	the type of the container struct this is embedded in.
70305e6557bSNeilBrown  * @member:	the name of the member within the struct.
70405e6557bSNeilBrown  *
70505e6557bSNeilBrown  * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
70605e6557bSNeilBrown  */
70705e6557bSNeilBrown #define container_of_safe(ptr, type, member) ({				\
70805e6557bSNeilBrown 	void *__mptr = (void *)(ptr);					\
70905e6557bSNeilBrown 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
71005e6557bSNeilBrown 			 !__same_type(*(ptr), void),			\
71105e6557bSNeilBrown 			 "pointer type mismatch in container_of()");	\
712227abcc6SDan Carpenter 	IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) :			\
71305e6557bSNeilBrown 		((type *)(__mptr - offsetof(type, member))); })
71405e6557bSNeilBrown 
715b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
716b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
717b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
718b9d4f426SArnaud Lacombe #endif
7199d00f92fSWANG Cong 
72058f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
72158f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms)						\
72258f86cc8SRusty Russell 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
72358f86cc8SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
72428b8d0c8SGobinda Charan Maji 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
72528b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
72628b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
72728b8d0c8SGobinda Charan Maji 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
72828b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
72928b8d0c8SGobinda Charan Maji 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
73037549e94SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
73158f86cc8SRusty Russell 	 (perms))
7321da177e4SLinus Torvalds #endif
733