xref: /linux-6.15/include/linux/kernel.h (revision 8f9fab48)
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 
61da177e4SLinus Torvalds #include <stdarg.h>
754d50897SMasahiro Yamada #include <linux/limits.h>
81da177e4SLinus Torvalds #include <linux/linkage.h>
91da177e4SLinus Torvalds #include <linux/stddef.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/compiler.h>
121da177e4SLinus Torvalds #include <linux/bitops.h>
13f0d1b0b3SDavid Howells #include <linux/log2.h>
14e0deaff4SAndrew Morton #include <linux/typecheck.h>
15968ab183SLinus Torvalds #include <linux/printk.h>
16c7acec71SIan Abbott #include <linux/build_bug.h>
171da177e4SLinus Torvalds #include <asm/byteorder.h>
18c461aed3SJani Nikula #include <asm/div64.h>
19607ca46eSDavid Howells #include <uapi/linux/kernel.h>
2072deb455SChristoph Hellwig #include <asm/div64.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
231da177e4SLinus Torvalds 
24e8c97af0SRandy Dunlap /**
25e8c97af0SRandy Dunlap  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
26e8c97af0SRandy Dunlap  * @x: value to repeat
27e8c97af0SRandy Dunlap  *
28e8c97af0SRandy Dunlap  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
29e8c97af0SRandy Dunlap  */
3044696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
3144696908SDavid S. Miller 
323ca45a46Szijun_hu /* @a is a power of 2 value */
33a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
34ed067d4aSKrzysztof Kozlowski #define ALIGN_DOWN(x, a)	__ALIGN_KERNEL((x) - ((a) - 1), (a))
359f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
36a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
37f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
382ea58144SLinus Torvalds 
39d3849953SChristoph Hellwig /* generic data direction definitions */
40d3849953SChristoph Hellwig #define READ			0
41d3849953SChristoph Hellwig #define WRITE			1
42d3849953SChristoph Hellwig 
43e8c97af0SRandy Dunlap /**
44e8c97af0SRandy Dunlap  * ARRAY_SIZE - get the number of elements in array @arr
45e8c97af0SRandy Dunlap  * @arr: array to be sized
46e8c97af0SRandy Dunlap  */
47c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
48c5e631cfSRusty Russell 
493ed605bcSGustavo Padovan #define u64_to_user_ptr(x) (		\
503ed605bcSGustavo Padovan {					\
51a0fe2c64SJann Horn 	typecheck(u64, (x));		\
52a0fe2c64SJann Horn 	(void __user *)(uintptr_t)(x);	\
533ed605bcSGustavo Padovan }					\
543ed605bcSGustavo Padovan )
553ed605bcSGustavo Padovan 
569b3be9f9SYinghai Lu /*
579b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
589b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
599b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
609b3be9f9SYinghai Lu  * arguments just once each.
619b3be9f9SYinghai Lu  */
629b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
63cedc5b6aSKees Cook /**
64cedc5b6aSKees Cook  * round_up - round up to next specified power of 2
65cedc5b6aSKees Cook  * @x: the value to round
66cedc5b6aSKees Cook  * @y: multiple to round up to (must be a power of 2)
67cedc5b6aSKees Cook  *
68cedc5b6aSKees Cook  * Rounds @x up to next multiple of @y (which must be a power of 2).
69cedc5b6aSKees Cook  * To perform arbitrary rounding up, use roundup() below.
70cedc5b6aSKees Cook  */
719b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
72cedc5b6aSKees Cook /**
73cedc5b6aSKees Cook  * round_down - round down to next specified power of 2
74cedc5b6aSKees Cook  * @x: the value to round
75cedc5b6aSKees Cook  * @y: multiple to round down to (must be a power of 2)
76cedc5b6aSKees Cook  *
77cedc5b6aSKees Cook  * Rounds @x down to next multiple of @y (which must be a power of 2).
78cedc5b6aSKees Cook  * To perform arbitrary rounding down, use rounddown() below.
79cedc5b6aSKees Cook  */
809b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
819b3be9f9SYinghai Lu 
82e8c97af0SRandy Dunlap /**
83e8c97af0SRandy Dunlap  * FIELD_SIZEOF - get the size of a struct's field
84e8c97af0SRandy Dunlap  * @t: the target struct
85e8c97af0SRandy Dunlap  * @f: the target struct's field
86e8c97af0SRandy Dunlap  * Return: the size of @f in the struct definition without having a
87e8c97af0SRandy Dunlap  * declared instance of @t.
88e8c97af0SRandy Dunlap  */
894552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
90e8c97af0SRandy Dunlap 
91b5d3755aSNicolas Dichtel #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
92604df322SMasahiro Yamada 
93604df322SMasahiro Yamada #define DIV_ROUND_DOWN_ULL(ll, d) \
94604df322SMasahiro Yamada 	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
95604df322SMasahiro Yamada 
96*8f9fab48SVinod Koul #define DIV_ROUND_UP_ULL(ll, d) \
97*8f9fab48SVinod Koul 	DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
9836a26c69SNicholas Bellinger 
9936a26c69SNicholas Bellinger #if BITS_PER_LONG == 32
10036a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
10136a26c69SNicholas Bellinger #else
10236a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
10336a26c69SNicholas Bellinger #endif
104074e61ecSJames Morris 
105cedc5b6aSKees Cook /**
106cedc5b6aSKees Cook  * roundup - round up to the next specified multiple
107cedc5b6aSKees Cook  * @x: the value to up
108cedc5b6aSKees Cook  * @y: multiple to round up to
109cedc5b6aSKees Cook  *
110cedc5b6aSKees Cook  * Rounds @x up to next multiple of @y. If @y will always be a power
111cedc5b6aSKees Cook  * of 2, consider using the faster round_up().
112cedc5b6aSKees Cook  */
113b28efd54SEric Paris #define roundup(x, y) (					\
114b28efd54SEric Paris {							\
115b95c4d18SRandy Dunlap 	typeof(y) __y = y;				\
116b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
117b28efd54SEric Paris }							\
118b28efd54SEric Paris )
119cedc5b6aSKees Cook /**
120cedc5b6aSKees Cook  * rounddown - round down to next specified multiple
121cedc5b6aSKees Cook  * @x: the value to round
122cedc5b6aSKees Cook  * @y: multiple to round down to
123cedc5b6aSKees Cook  *
124cedc5b6aSKees Cook  * Rounds @x down to next multiple of @y. If @y will always be a power
125cedc5b6aSKees Cook  * of 2, consider using the faster round_down().
126cedc5b6aSKees Cook  */
127686a0f3dSEric Paris #define rounddown(x, y) (				\
128686a0f3dSEric Paris {							\
129686a0f3dSEric Paris 	typeof(x) __x = (x);				\
130686a0f3dSEric Paris 	__x - (__x % (y));				\
131686a0f3dSEric Paris }							\
132686a0f3dSEric Paris )
133b6d86d3dSGuenter Roeck 
134b6d86d3dSGuenter Roeck /*
1354f5901f5SNiklas Söderlund  * Divide positive or negative dividend by positive or negative divisor
1364f5901f5SNiklas Söderlund  * and round to closest integer. Result is undefined for negative
137e8c97af0SRandy Dunlap  * divisors if the dividend variable type is unsigned and for negative
1384f5901f5SNiklas Söderlund  * dividends if the divisor variable type is unsigned.
139b6d86d3dSGuenter Roeck  */
1409fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
1419fe06081SDarrick J. Wong {							\
142b6d86d3dSGuenter Roeck 	typeof(x) __x = x;				\
143b6d86d3dSGuenter Roeck 	typeof(divisor) __d = divisor;			\
144c4e18497SGuenter Roeck 	(((typeof(x))-1) > 0 ||				\
1454f5901f5SNiklas Söderlund 	 ((typeof(divisor))-1) > 0 ||			\
1464f5901f5SNiklas Söderlund 	 (((__x) > 0) == ((__d) > 0))) ?		\
147b6d86d3dSGuenter Roeck 		(((__x) + ((__d) / 2)) / (__d)) :	\
148b6d86d3dSGuenter Roeck 		(((__x) - ((__d) / 2)) / (__d));	\
1499fe06081SDarrick J. Wong }							\
1509fe06081SDarrick J. Wong )
151f766093eSJavi Merino /*
152f766093eSJavi Merino  * Same as above but for u64 dividends. divisor must be a 32-bit
153f766093eSJavi Merino  * number.
154f766093eSJavi Merino  */
155f766093eSJavi Merino #define DIV_ROUND_CLOSEST_ULL(x, divisor)(		\
156f766093eSJavi Merino {							\
157f766093eSJavi Merino 	typeof(divisor) __d = divisor;			\
158f766093eSJavi Merino 	unsigned long long _tmp = (x) + (__d) / 2;	\
159f766093eSJavi Merino 	do_div(_tmp, __d);				\
160f766093eSJavi Merino 	_tmp;						\
161f766093eSJavi Merino }							\
162f766093eSJavi Merino )
1631da177e4SLinus Torvalds 
1649993bc63SSalman Qazi /*
1659993bc63SSalman Qazi  * Multiplies an integer by a fraction, while avoiding unnecessary
1669993bc63SSalman Qazi  * overflow or loss of precision.
1679993bc63SSalman Qazi  */
1689993bc63SSalman Qazi #define mult_frac(x, numer, denom)(			\
1699993bc63SSalman Qazi {							\
1709993bc63SSalman Qazi 	typeof(x) quot = (x) / (denom);			\
1719993bc63SSalman Qazi 	typeof(x) rem  = (x) % (denom);			\
1729993bc63SSalman Qazi 	(quot * (numer)) + ((rem * (numer)) / (denom));	\
1739993bc63SSalman Qazi }							\
1749993bc63SSalman Qazi )
1759993bc63SSalman Qazi 
1769993bc63SSalman Qazi 
177ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
178ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
179ca31e146SEduard - Gabriel Munteanu 
1802da96acdSJens Axboe #define sector_div(a, b) do_div(a, b)
1812da96acdSJens Axboe 
182218e180eSAndrew Morton /**
183218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
184218e180eSAndrew Morton  * @n: the number we're accessing
185218e180eSAndrew Morton  *
186218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
187218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
188218e180eSAndrew Morton  * 32-bits.
189218e180eSAndrew Morton  */
190218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
191218e180eSAndrew Morton 
192204b885eSJoerg Roedel /**
193204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
194204b885eSJoerg Roedel  * @n: the number we're accessing
195204b885eSJoerg Roedel  */
196204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
197204b885eSJoerg Roedel 
1981da177e4SLinus Torvalds struct completion;
199df2e71fbS[email protected] struct pt_regs;
200df2e71fbS[email protected] struct user;
2011da177e4SLinus Torvalds 
202070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
203070cb065SUwe Kleine-König extern int _cond_resched(void);
204070cb065SUwe Kleine-König # define might_resched() _cond_resched()
205070cb065SUwe Kleine-König #else
206070cb065SUwe Kleine-König # define might_resched() do { } while (0)
207070cb065SUwe Kleine-König #endif
208070cb065SUwe Kleine-König 
209d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
210568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset);
211568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset);
212568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset);
213568f1967SPeter Zijlstra 
2141da177e4SLinus Torvalds /**
2151da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
2161da177e4SLinus Torvalds  *
2171da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
2181da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
2191da177e4SLinus Torvalds  *
2201da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
221e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
2221da177e4SLinus Torvalds  * supposed to.
2231da177e4SLinus Torvalds  */
224f8cbd99bSIngo Molnar # define might_sleep() \
225e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
226568f1967SPeter Zijlstra /**
227568f1967SPeter Zijlstra  * cant_sleep - annotation for functions that cannot sleep
228568f1967SPeter Zijlstra  *
229568f1967SPeter Zijlstra  * this macro will print a stack trace if it is executed with preemption enabled
230568f1967SPeter Zijlstra  */
231568f1967SPeter Zijlstra # define cant_sleep() \
232568f1967SPeter Zijlstra 	do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
23300845eb9SLinus Torvalds # define sched_annotate_sleep()	(current->task_state_change = 0)
234f8cbd99bSIngo Molnar #else
2353427445aSPeter Zijlstra   static inline void ___might_sleep(const char *file, int line,
2363427445aSPeter Zijlstra 				   int preempt_offset) { }
237d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
238d894837fSSimon Kagstrom 				   int preempt_offset) { }
239f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
240568f1967SPeter Zijlstra # define cant_sleep() do { } while (0)
2411029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0)
242f8cbd99bSIngo Molnar #endif
243f8cbd99bSIngo Molnar 
244368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
245f8cbd99bSIngo Molnar 
246c8299cb6SMichal Nazarewicz /**
247c8299cb6SMichal Nazarewicz  * abs - return absolute value of an argument
2488f57e4d9SMichal Nazarewicz  * @x: the value.  If it is unsigned type, it is converted to signed type first.
2498f57e4d9SMichal Nazarewicz  *     char is treated as if it was signed (regardless of whether it really is)
2508f57e4d9SMichal Nazarewicz  *     but the macro's return type is preserved as char.
251c8299cb6SMichal Nazarewicz  *
2528f57e4d9SMichal Nazarewicz  * Return: an absolute value of x.
25371a90484SAndrew Morton  */
2548f57e4d9SMichal Nazarewicz #define abs(x)	__abs_choose_expr(x, long long,				\
2558f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, long,				\
2568f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, int,				\
2578f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, short,				\
2588f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, char,				\
2598f57e4d9SMichal Nazarewicz 		__builtin_choose_expr(					\
2608f57e4d9SMichal Nazarewicz 			__builtin_types_compatible_p(typeof(x), char),	\
2618f57e4d9SMichal Nazarewicz 			(char)({ signed char __x = (x); __x<0?-__x:__x; }), \
2628f57e4d9SMichal Nazarewicz 			((void)0)))))))
2638f57e4d9SMichal Nazarewicz 
2648f57e4d9SMichal Nazarewicz #define __abs_choose_expr(x, type, other) __builtin_choose_expr(	\
2658f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
2668f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x), unsigned type),		\
2678f57e4d9SMichal Nazarewicz 	({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
2681da177e4SLinus Torvalds 
26989770b0aSDaniel Borkmann /**
27089770b0aSDaniel Borkmann  * reciprocal_scale - "scale" a value into range [0, ep_ro)
27189770b0aSDaniel Borkmann  * @val: value
27289770b0aSDaniel Borkmann  * @ep_ro: right open interval endpoint
27389770b0aSDaniel Borkmann  *
27489770b0aSDaniel Borkmann  * Perform a "reciprocal multiplication" in order to "scale" a value into
275e8c97af0SRandy Dunlap  * range [0, @ep_ro), where the upper interval endpoint is right-open.
27689770b0aSDaniel Borkmann  * This is useful, e.g. for accessing a index of an array containing
277e8c97af0SRandy Dunlap  * @ep_ro elements, for example. Think of it as sort of modulus, only that
27889770b0aSDaniel Borkmann  * the result isn't that of modulo. ;) Note that if initial input is a
27989770b0aSDaniel Borkmann  * small value, then result will return 0.
28089770b0aSDaniel Borkmann  *
281e8c97af0SRandy Dunlap  * Return: a result based on @val in interval [0, @ep_ro).
28289770b0aSDaniel Borkmann  */
28389770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
28489770b0aSDaniel Borkmann {
28589770b0aSDaniel Borkmann 	return (u32)(((u64) val * ep_ro) >> 32);
28689770b0aSDaniel Borkmann }
28789770b0aSDaniel Borkmann 
288386e7906SAxel Lin #if defined(CONFIG_MMU) && \
289386e7906SAxel Lin 	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
2909ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__)
2919ec23531SDavid Hildenbrand void __might_fault(const char *file, int line);
2923ee1afa3SNick Piggin #else
293662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { }
2943ee1afa3SNick Piggin #endif
2953ee1afa3SNick Piggin 
296e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
297c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
2989402c95fSJoe Perches __printf(1, 2)
2999af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold;
300ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg);
301dd287796SAndrew Morton extern void oops_enter(void);
302dd287796SAndrew Morton extern void oops_exit(void);
303863a6049SAnton Blanchard void print_oops_end_marker(void);
304dd287796SAndrew Morton extern int oops_may_print(void);
3059af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn;
3069af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn;
30733ee3b2eSAlexey Dobriyan 
3087a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT
3097a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err);
3107a46ec0eSKees Cook #else
3117a46ec0eSKees Cook static inline void refcount_error_report(struct pt_regs *regs, const char *err)
3127a46ec0eSKees Cook { }
3137a46ec0eSKees Cook #endif
3147a46ec0eSKees Cook 
31533ee3b2eSAlexey Dobriyan /* Internal, do not use. */
31633ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
31733ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
31833ee3b2eSAlexey Dobriyan 
31933ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
32033ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
3214c925d60SEldad Zack 
3224c925d60SEldad Zack /**
3234c925d60SEldad Zack  * kstrtoul - convert a string to an unsigned long
3244c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3254c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3264c925d60SEldad Zack  *  may also be a plus sign, but not a minus sign.
3274c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3284c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3294c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3304c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3314c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3324c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3334c925d60SEldad Zack  *
3344c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3354c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3364c925d60SEldad Zack  * be checked.
3374c925d60SEldad Zack */
33833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
33933ee3b2eSAlexey Dobriyan {
34033ee3b2eSAlexey Dobriyan 	/*
34133ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
34233ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
34333ee3b2eSAlexey Dobriyan 	 */
34433ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
34533ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
34633ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
34733ee3b2eSAlexey Dobriyan 	else
34833ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
34933ee3b2eSAlexey Dobriyan }
35033ee3b2eSAlexey Dobriyan 
3514c925d60SEldad Zack /**
3524c925d60SEldad Zack  * kstrtol - convert a string to a long
3534c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3544c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3554c925d60SEldad Zack  *  may also be a plus sign or a minus sign.
3564c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3574c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3584c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3594c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3604c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3614c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3624c925d60SEldad Zack  *
3634c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3644c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3654c925d60SEldad Zack  * be checked.
3664c925d60SEldad Zack  */
36733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
36833ee3b2eSAlexey Dobriyan {
36933ee3b2eSAlexey Dobriyan 	/*
37033ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
37133ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
37233ee3b2eSAlexey Dobriyan 	 */
37333ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
37433ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
37533ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
37633ee3b2eSAlexey Dobriyan 	else
37733ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
37833ee3b2eSAlexey Dobriyan }
37933ee3b2eSAlexey Dobriyan 
38033ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
38133ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
38233ee3b2eSAlexey Dobriyan 
38333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
38433ee3b2eSAlexey Dobriyan {
38533ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
38633ee3b2eSAlexey Dobriyan }
38733ee3b2eSAlexey Dobriyan 
38833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
38933ee3b2eSAlexey Dobriyan {
39033ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
39133ee3b2eSAlexey Dobriyan }
39233ee3b2eSAlexey Dobriyan 
39333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
39433ee3b2eSAlexey Dobriyan {
39533ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
39633ee3b2eSAlexey Dobriyan }
39733ee3b2eSAlexey Dobriyan 
39833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
39933ee3b2eSAlexey Dobriyan {
40033ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
40133ee3b2eSAlexey Dobriyan }
40233ee3b2eSAlexey Dobriyan 
40333ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
40433ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
40533ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
40633ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
407ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res);
40833ee3b2eSAlexey Dobriyan 
409c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
410c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
411c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
412c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
413c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
414c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
415c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
416c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
417c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
418c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
419ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
420c196e32aSAlexey Dobriyan 
421c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
422c196e32aSAlexey Dobriyan {
423c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
424c196e32aSAlexey Dobriyan }
425c196e32aSAlexey Dobriyan 
426c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
427c196e32aSAlexey Dobriyan {
428c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
429c196e32aSAlexey Dobriyan }
430c196e32aSAlexey Dobriyan 
431c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
432c196e32aSAlexey Dobriyan {
433c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
434c196e32aSAlexey Dobriyan }
435c196e32aSAlexey Dobriyan 
436c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
437c196e32aSAlexey Dobriyan {
438c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
439c196e32aSAlexey Dobriyan }
440c196e32aSAlexey Dobriyan 
44167d0a075SJoe Perches /* Obsolete, do not use.  Use kstrto<foo> instead */
44267d0a075SJoe Perches 
4431da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
4441da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
4451da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
4461da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
44733ee3b2eSAlexey Dobriyan 
448d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size,
449d1be35cbSAndrei Vagin 		      unsigned long long num, unsigned int width);
4501ac101a5SKAMEZAWA Hiroyuki 
45167d0a075SJoe Perches /* lib/printf utilities */
45267d0a075SJoe Perches 
453b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
454b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
455b9075fa9SJoe Perches extern __printf(3, 4)
456b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
457b9075fa9SJoe Perches extern __printf(3, 0)
458b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
459b9075fa9SJoe Perches extern __printf(3, 4)
460b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
461b9075fa9SJoe Perches extern __printf(3, 0)
462b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
46348a27055SRasmus Villemoes extern __printf(2, 3) __malloc
464b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
46548a27055SRasmus Villemoes extern __printf(2, 0) __malloc
4668db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
4670a9df786SRasmus Villemoes extern __printf(2, 0)
4680a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
4691da177e4SLinus Torvalds 
4706061d949SJoe Perches extern __scanf(2, 3)
4716061d949SJoe Perches int sscanf(const char *, const char *, ...);
4726061d949SJoe Perches extern __scanf(2, 0)
4736061d949SJoe Perches int vsscanf(const char *, const char *, va_list);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
4761da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
477d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
4786ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option);
479f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val);
4801da177e4SLinus Torvalds 
4815e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
4829fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr);
483cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
4841da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
4851da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
486ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
487ab7476cfSArjan van de Ven 
4889f615894SAndy Shevchenko u64 int_pow(u64 base, unsigned int exp);
4891da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
4901da177e4SLinus Torvalds 
49147a36163SCrt Mori #if BITS_PER_LONG < 64
49247a36163SCrt Mori u32 int_sqrt64(u64 x);
49347a36163SCrt Mori #else
49447a36163SCrt Mori static inline u32 int_sqrt64(u64 x)
49547a36163SCrt Mori {
49647a36163SCrt Mori 	return (u32)int_sqrt(x);
49747a36163SCrt Mori }
49847a36163SCrt Mori #endif
49947a36163SCrt Mori 
5001da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
5011da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
502aa727107SAdrian Bunk extern int panic_timeout;
50381c9d43fSFeng Tang extern unsigned long panic_print;
5041da177e4SLinus Torvalds extern int panic_on_oops;
5058da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
5065211a242SKurt Garloff extern int panic_on_io_nmi;
5079e3961a0SPrarit Bhargava extern int panic_on_warn;
508088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall;
50955af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
5105375b708SHATAYAMA Daisuke 
5115375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers;
5125375b708SHATAYAMA Daisuke 
5135800dc3cSJason Baron /*
5141717f209SHidehiro Kawai  * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
5151717f209SHidehiro Kawai  * holds a CPU number which is executing panic() currently. A value of
5161717f209SHidehiro Kawai  * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
5171717f209SHidehiro Kawai  */
5181717f209SHidehiro Kawai extern atomic_t panic_cpu;
5191717f209SHidehiro Kawai #define PANIC_CPU_INVALID	-1
5201717f209SHidehiro Kawai 
5211717f209SHidehiro Kawai /*
5225800dc3cSJason Baron  * Only to be used by arch init code. If the user over-wrote the default
5235800dc3cSJason Baron  * CONFIG_PANIC_TIMEOUT, honor it.
5245800dc3cSJason Baron  */
5255800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
5265800dc3cSJason Baron {
5275800dc3cSJason Baron 	if (panic_timeout == arch_default_timeout)
5285800dc3cSJason Baron 		panic_timeout = timeout;
5295800dc3cSJason Baron }
5301da177e4SLinus Torvalds extern const char *print_tainted(void);
531373d4d09SRusty Russell enum lockdep_ok {
532373d4d09SRusty Russell 	LOCKDEP_STILL_OK,
533373d4d09SRusty Russell 	LOCKDEP_NOW_UNRELIABLE
534373d4d09SRusty Russell };
535373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok);
53625ddbb18SAndi Kleen extern int test_taint(unsigned flag);
53725ddbb18SAndi Kleen extern unsigned long get_taint(void);
538b920de1bSDavid Howells extern int root_mountflags;
5391da177e4SLinus Torvalds 
5402ce802f6STejun Heo extern bool early_boot_irqs_disabled;
5412ce802f6STejun Heo 
54269a78ff2SThomas Gleixner /*
54369a78ff2SThomas Gleixner  * Values used for system_state. Ordering of the states must not be changed
54469a78ff2SThomas Gleixner  * as code checks for <, <=, >, >= STATE.
54569a78ff2SThomas Gleixner  */
5461da177e4SLinus Torvalds extern enum system_states {
5471da177e4SLinus Torvalds 	SYSTEM_BOOTING,
54869a78ff2SThomas Gleixner 	SYSTEM_SCHEDULING,
5491da177e4SLinus Torvalds 	SYSTEM_RUNNING,
5501da177e4SLinus Torvalds 	SYSTEM_HALT,
5511da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
5521da177e4SLinus Torvalds 	SYSTEM_RESTART,
553c1a957d1SThomas Gleixner 	SYSTEM_SUSPEND,
5541da177e4SLinus Torvalds } system_state;
5551da177e4SLinus Torvalds 
55647d4b263SKees Cook /* This cannot be an enum because some may be used in assembly source. */
55725ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
55825ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
5598c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC		2
56025ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
56125ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
56225ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
56325ddbb18SAndi Kleen #define TAINT_USER			6
56425ddbb18SAndi Kleen #define TAINT_DIE			7
56525ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
56625ddbb18SAndi Kleen #define TAINT_WARN			9
56726e9a397SLinus Torvalds #define TAINT_CRAP			10
56892946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
5692449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
57066cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE		13
57169361eefSJosh Hunt #define TAINT_SOFTLOCKUP		14
572c5f45465SSeth Jennings #define TAINT_LIVEPATCH			15
5734efb442cSBorislav Petkov #define TAINT_AUX			16
574bc4f2f54SKees Cook #define TAINT_RANDSTRUCT		17
575bc4f2f54SKees Cook #define TAINT_FLAGS_COUNT		18
5767fd8329bSPetr Mladek 
5777fd8329bSPetr Mladek struct taint_flag {
5785eb7c0d0SLarry Finger 	char c_true;	/* character printed when tainted */
5795eb7c0d0SLarry Finger 	char c_false;	/* character printed when not tainted */
5807fd8329bSPetr Mladek 	bool module;	/* also show as a per-module taint flag */
5817fd8329bSPetr Mladek };
5827fd8329bSPetr Mladek 
5837fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
5841da177e4SLinus Torvalds 
5853fc95772SHarvey Harrison extern const char hex_asc[];
5863fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
5873fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
5883fc95772SHarvey Harrison 
58955036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
5903fc95772SHarvey Harrison {
5913fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
5923fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
5933fc95772SHarvey Harrison 	return buf;
5943fc95772SHarvey Harrison }
59599eaf3c4SRandy Dunlap 
596c26d436cSAndre Naujoks extern const char hex_asc_upper[];
597c26d436cSAndre Naujoks #define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
598c26d436cSAndre Naujoks #define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]
599c26d436cSAndre Naujoks 
600c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte)
601c26d436cSAndre Naujoks {
602c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_hi(byte);
603c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_lo(byte);
604c26d436cSAndre Naujoks 	return buf;
605c26d436cSAndre Naujoks }
606c26d436cSAndre Naujoks 
60790378889SAndy Shevchenko extern int hex_to_bin(char ch);
608b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
60953d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count);
61090378889SAndy Shevchenko 
611a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac);
6124cd5773aSAndy Shevchenko 
6138a64f336SJoe Perches /*
614526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
6152002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
6162002c258SSteven Rostedt  *
6172002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
6182002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
619156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
6202002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
6212002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
6222002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
6232002c258SSteven Rostedt  * to continue tracing.
6242002c258SSteven Rostedt  *
6252002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
6262002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
6272002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
6282002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
6292002c258SSteven Rostedt  *
6302002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
631526211bcSIngo Molnar  */
632cecbca96SFrederic Weisbecker 
633cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
634cecbca96SFrederic Weisbecker 	DUMP_NONE,
635cecbca96SFrederic Weisbecker 	DUMP_ALL,
636cecbca96SFrederic Weisbecker 	DUMP_ORIG,
637cecbca96SFrederic Weisbecker };
638cecbca96SFrederic Weisbecker 
639526211bcSIngo Molnar #ifdef CONFIG_TRACING
64093d68e52SSteven Rostedt void tracing_on(void);
64193d68e52SSteven Rostedt void tracing_off(void);
64293d68e52SSteven Rostedt int tracing_is_on(void);
643ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
644ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
64593d68e52SSteven Rostedt 
646526211bcSIngo Molnar extern void tracing_start(void);
647526211bcSIngo Molnar extern void tracing_stop(void);
648526211bcSIngo Molnar 
649b9075fa9SJoe Perches static inline __printf(1, 2)
650b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
651769b0441SFrederic Weisbecker {
652769b0441SFrederic Weisbecker }
653769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
654769b0441SFrederic Weisbecker do {									\
655769b0441SFrederic Weisbecker 	if (0)								\
656769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
657769b0441SFrederic Weisbecker } while (0)
658769b0441SFrederic Weisbecker 
659526211bcSIngo Molnar /**
660526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
661526211bcSIngo Molnar  * @fmt: the printf format for printing
662526211bcSIngo Molnar  *
663e8c97af0SRandy Dunlap  * Note: __trace_printk is an internal function for trace_printk() and
664e8c97af0SRandy Dunlap  *       the @ip is passed in via the trace_printk() macro.
665526211bcSIngo Molnar  *
666526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
667526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
668526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
669526211bcSIngo Molnar  * where problems are occurring.
670526211bcSIngo Molnar  *
671526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
672526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
67309ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
674e8c97af0SRandy Dunlap  * allocated when trace_printk() is used.)
6759d3c752cSSteven Rostedt (Red Hat)  *
6768730662dSWei Wang  * A little optimization trick is done here. If there's only one
6779d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
6789d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
6799d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
6809d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
6819d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
6829d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
6839d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
6849d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
6859d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
6869d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
687526211bcSIngo Molnar  */
688769b0441SFrederic Weisbecker 
6899d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
6909d3c752cSSteven Rostedt (Red Hat) do {							\
6919d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
6929d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
6939d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
6949d3c752cSSteven Rostedt (Red Hat) 	else						\
6959d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
6969d3c752cSSteven Rostedt (Red Hat) } while (0)
6979d3c752cSSteven Rostedt (Red Hat) 
6989d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
699769b0441SFrederic Weisbecker do {									\
7003debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
70148ead020SFrederic Weisbecker 		__attribute__((section("__trace_printk_fmt"))) =	\
70248ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
70348ead020SFrederic Weisbecker 									\
70407d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
70507d777feSSteven Rostedt 									\
70607d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
70748ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
70807d777feSSteven Rostedt 	else								\
70948ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
710769b0441SFrederic Weisbecker } while (0)
711769b0441SFrederic Weisbecker 
712b9075fa9SJoe Perches extern __printf(2, 3)
713b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
71448ead020SFrederic Weisbecker 
715b9075fa9SJoe Perches extern __printf(2, 3)
716b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
717769b0441SFrederic Weisbecker 
71809ae7234SSteven Rostedt (Red Hat) /**
71909ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
72009ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
72109ae7234SSteven Rostedt (Red Hat)  *
72209ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
72309ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
72409ae7234SSteven Rostedt (Red Hat)  *
72509ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
726e8c97af0SRandy Dunlap  * paths that a developer wants the least amount of "Heisenbug" effects,
72709ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
72809ae7234SSteven Rostedt (Red Hat)  *
72909ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
73009ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
73109ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
73209ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
73309ae7234SSteven Rostedt (Red Hat)  *
73409ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
73509ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
73609ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
737e8c97af0SRandy Dunlap  * allocated when trace_puts() is used.)
73809ae7234SSteven Rostedt (Red Hat)  *
73909ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
74009ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
74109ae7234SSteven Rostedt (Red Hat)  */
74209ae7234SSteven Rostedt (Red Hat) 
74309ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
7443debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
74509ae7234SSteven Rostedt (Red Hat) 		__attribute__((section("__trace_printk_fmt"))) =	\
74609ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
74709ae7234SSteven Rostedt (Red Hat) 									\
74809ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
74909ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
75009ae7234SSteven Rostedt (Red Hat) 	else								\
75109ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
75209ae7234SSteven Rostedt (Red Hat) })
753bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str);
754bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size);
75509ae7234SSteven Rostedt (Red Hat) 
756c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
75703889384SSteven Rostedt 
75848ead020SFrederic Weisbecker /*
75948ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
76048ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
76148ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
76248ead020SFrederic Weisbecker  */
763769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
764769b0441SFrederic Weisbecker do {									\
76548ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
7663debb0a9SSteven Rostedt (Red Hat) 		static const char *trace_printk_fmt __used		\
76748ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
76848ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
7697bffc23eSIngo Molnar 									\
77048ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
77148ead020SFrederic Weisbecker 	} else								\
77248ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
773769b0441SFrederic Weisbecker } while (0)
774769b0441SFrederic Weisbecker 
7758db14860SNicolas Iooss extern __printf(2, 0) int
77648ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
77748ead020SFrederic Weisbecker 
7788db14860SNicolas Iooss extern __printf(2, 0) int
779526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
780769b0441SFrederic Weisbecker 
781cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
782526211bcSIngo Molnar #else
783526211bcSIngo Molnar static inline void tracing_start(void) { }
784526211bcSIngo Molnar static inline void tracing_stop(void) { }
785e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { }
78693d68e52SSteven Rostedt 
78793d68e52SSteven Rostedt static inline void tracing_on(void) { }
78893d68e52SSteven Rostedt static inline void tracing_off(void) { }
78993d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
790ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
791ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
79293d68e52SSteven Rostedt 
79360efc15aSMichal Hocko static inline __printf(1, 2)
79460efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
795526211bcSIngo Molnar {
796526211bcSIngo Molnar 	return 0;
797526211bcSIngo Molnar }
7988db14860SNicolas Iooss static __printf(1, 0) inline int
799526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
800526211bcSIngo Molnar {
801526211bcSIngo Molnar 	return 0;
802526211bcSIngo Molnar }
803cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
804769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
805526211bcSIngo Molnar 
806526211bcSIngo Molnar /*
8073c8ba0d6SKees Cook  * min()/max()/clamp() macros must accomplish three things:
8083c8ba0d6SKees Cook  *
8093c8ba0d6SKees Cook  * - avoid multiple evaluations of the arguments (so side-effects like
8103c8ba0d6SKees Cook  *   "x++" happen only once) when non-constant.
8113c8ba0d6SKees Cook  * - perform strict type-checking (to generate warnings instead of
8123c8ba0d6SKees Cook  *   nasty runtime surprises). See the "unnecessary" pointer comparison
8133c8ba0d6SKees Cook  *   in __typecheck().
8143c8ba0d6SKees Cook  * - retain result as a constant expressions when called with only
8153c8ba0d6SKees Cook  *   constant expressions (to avoid tripping VLA warnings in stack
8163c8ba0d6SKees Cook  *   allocation usage).
8171da177e4SLinus Torvalds  */
8183c8ba0d6SKees Cook #define __typecheck(x, y) \
8193c8ba0d6SKees Cook 		(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
8203c8ba0d6SKees Cook 
8213c8ba0d6SKees Cook /*
8223c8ba0d6SKees Cook  * This returns a constant expression while determining if an argument is
8233c8ba0d6SKees Cook  * a constant expression, most importantly without evaluating the argument.
8243c8ba0d6SKees Cook  * Glory to Martin Uecker <[email protected]>
8253c8ba0d6SKees Cook  */
8263c8ba0d6SKees Cook #define __is_constexpr(x) \
8273c8ba0d6SKees Cook 	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
8283c8ba0d6SKees Cook 
8293c8ba0d6SKees Cook #define __no_side_effects(x, y) \
8303c8ba0d6SKees Cook 		(__is_constexpr(x) && __is_constexpr(y))
8313c8ba0d6SKees Cook 
8323c8ba0d6SKees Cook #define __safe_cmp(x, y) \
8333c8ba0d6SKees Cook 		(__typecheck(x, y) && __no_side_effects(x, y))
8343c8ba0d6SKees Cook 
8353c8ba0d6SKees Cook #define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
8363c8ba0d6SKees Cook 
837e9092d0dSLinus Torvalds #define __cmp_once(x, y, unique_x, unique_y, op) ({	\
838e9092d0dSLinus Torvalds 		typeof(x) unique_x = (x);		\
839e9092d0dSLinus Torvalds 		typeof(y) unique_y = (y);		\
840e9092d0dSLinus Torvalds 		__cmp(unique_x, unique_y, op); })
8413c8ba0d6SKees Cook 
8423c8ba0d6SKees Cook #define __careful_cmp(x, y, op) \
8433c8ba0d6SKees Cook 	__builtin_choose_expr(__safe_cmp(x, y), \
844e9092d0dSLinus Torvalds 		__cmp(x, y, op), \
845e9092d0dSLinus Torvalds 		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
846e8c97af0SRandy Dunlap 
847e8c97af0SRandy Dunlap /**
848e8c97af0SRandy Dunlap  * min - return minimum of two values of the same or compatible types
849e8c97af0SRandy Dunlap  * @x: first value
850e8c97af0SRandy Dunlap  * @y: second value
851e8c97af0SRandy Dunlap  */
8523c8ba0d6SKees Cook #define min(x, y)	__careful_cmp(x, y, <)
853e8c97af0SRandy Dunlap 
854e8c97af0SRandy Dunlap /**
855e8c97af0SRandy Dunlap  * max - return maximum of two values of the same or compatible types
856e8c97af0SRandy Dunlap  * @x: first value
857e8c97af0SRandy Dunlap  * @y: second value
858e8c97af0SRandy Dunlap  */
8593c8ba0d6SKees Cook #define max(x, y)	__careful_cmp(x, y, >)
860bdf4bbaaSHarvey Harrison 
861e8c97af0SRandy Dunlap /**
862e8c97af0SRandy Dunlap  * min3 - return minimum of three values
863e8c97af0SRandy Dunlap  * @x: first value
864e8c97af0SRandy Dunlap  * @y: second value
865e8c97af0SRandy Dunlap  * @z: third value
866e8c97af0SRandy Dunlap  */
8672e1d06e1SMichal Nazarewicz #define min3(x, y, z) min((typeof(x))min(x, y), z)
868e8c97af0SRandy Dunlap 
869e8c97af0SRandy Dunlap /**
870e8c97af0SRandy Dunlap  * max3 - return maximum of three values
871e8c97af0SRandy Dunlap  * @x: first value
872e8c97af0SRandy Dunlap  * @y: second value
873e8c97af0SRandy Dunlap  * @z: third value
874e8c97af0SRandy Dunlap  */
8752e1d06e1SMichal Nazarewicz #define max3(x, y, z) max((typeof(x))max(x, y), z)
876f27c85c5SHagen Paul Pfeifer 
877bdf4bbaaSHarvey Harrison /**
878c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
879c8bf1336SMartin K. Petersen  * @x: value1
880c8bf1336SMartin K. Petersen  * @y: value2
881c8bf1336SMartin K. Petersen  */
882c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
883c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
884c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
885c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
886c8bf1336SMartin K. Petersen 
887c8bf1336SMartin K. Petersen /**
888bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
889bdf4bbaaSHarvey Harrison  * @val: current value
8902e1d06e1SMichal Nazarewicz  * @lo: lowest allowable value
8912e1d06e1SMichal Nazarewicz  * @hi: highest allowable value
892bdf4bbaaSHarvey Harrison  *
893e8c97af0SRandy Dunlap  * This macro does strict typechecking of @lo/@hi to make sure they are of the
894e8c97af0SRandy Dunlap  * same type as @val.  See the unnecessary pointer comparisons.
895bdf4bbaaSHarvey Harrison  */
8962e1d06e1SMichal Nazarewicz #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds /*
8991da177e4SLinus Torvalds  * ..and if you can't take the strict
9001da177e4SLinus Torvalds  * types, you can specify one yourself.
9011da177e4SLinus Torvalds  *
902bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
9031da177e4SLinus Torvalds  */
904e8c97af0SRandy Dunlap 
905e8c97af0SRandy Dunlap /**
906e8c97af0SRandy Dunlap  * min_t - return minimum of two values, using the specified type
907e8c97af0SRandy Dunlap  * @type: data type to use
908e8c97af0SRandy Dunlap  * @x: first value
909e8c97af0SRandy Dunlap  * @y: second value
910e8c97af0SRandy Dunlap  */
9113c8ba0d6SKees Cook #define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
9121da177e4SLinus Torvalds 
913e8c97af0SRandy Dunlap /**
914e8c97af0SRandy Dunlap  * max_t - return maximum of two values, using the specified type
915e8c97af0SRandy Dunlap  * @type: data type to use
916e8c97af0SRandy Dunlap  * @x: first value
917e8c97af0SRandy Dunlap  * @y: second value
918e8c97af0SRandy Dunlap  */
9193c8ba0d6SKees Cook #define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
920bdf4bbaaSHarvey Harrison 
921bdf4bbaaSHarvey Harrison /**
922bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
923bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
924bdf4bbaaSHarvey Harrison  * @val: current value
925c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
926c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
927bdf4bbaaSHarvey Harrison  *
928bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
929e8c97af0SRandy Dunlap  * @type to make all the comparisons.
930bdf4bbaaSHarvey Harrison  */
931c185b07fSMichal Nazarewicz #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
932bdf4bbaaSHarvey Harrison 
933bdf4bbaaSHarvey Harrison /**
934bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
935bdf4bbaaSHarvey Harrison  * @val: current value
936c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
937c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
938bdf4bbaaSHarvey Harrison  *
939bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
940e8c97af0SRandy Dunlap  * type the input argument @val is.  This is useful when @val is an unsigned
941e8c97af0SRandy Dunlap  * type and @lo and @hi are literals that will otherwise be assigned a signed
942bdf4bbaaSHarvey Harrison  * integer type.
943bdf4bbaaSHarvey Harrison  */
944c185b07fSMichal Nazarewicz #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
9451da177e4SLinus Torvalds 
94691f68b73SWu Fengguang 
947e8c97af0SRandy Dunlap /**
948e8c97af0SRandy Dunlap  * swap - swap values of @a and @b
949e8c97af0SRandy Dunlap  * @a: first value
950e8c97af0SRandy Dunlap  * @b: second value
95191f68b73SWu Fengguang  */
952ac7b9004SPeter Zijlstra #define swap(a, b) \
953ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
95491f68b73SWu Fengguang 
955cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */
956cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
957cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
958cf14f27fSAlexei Starovoitov 
959cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b
960cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b)
961cf14f27fSAlexei Starovoitov 
9621da177e4SLinus Torvalds /**
9631da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
9641da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
9651da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
9661da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
9671da177e4SLinus Torvalds  *
9681da177e4SLinus Torvalds  */
9691da177e4SLinus Torvalds #define container_of(ptr, type, member) ({				\
970c7acec71SIan Abbott 	void *__mptr = (void *)(ptr);					\
971c7acec71SIan Abbott 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
972c7acec71SIan Abbott 			 !__same_type(*(ptr), void),			\
973c7acec71SIan Abbott 			 "pointer type mismatch in container_of()");	\
974c7acec71SIan Abbott 	((type *)(__mptr - offsetof(type, member))); })
9751da177e4SLinus Torvalds 
97605e6557bSNeilBrown /**
97705e6557bSNeilBrown  * container_of_safe - cast a member of a structure out to the containing structure
97805e6557bSNeilBrown  * @ptr:	the pointer to the member.
97905e6557bSNeilBrown  * @type:	the type of the container struct this is embedded in.
98005e6557bSNeilBrown  * @member:	the name of the member within the struct.
98105e6557bSNeilBrown  *
98205e6557bSNeilBrown  * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
98305e6557bSNeilBrown  */
98405e6557bSNeilBrown #define container_of_safe(ptr, type, member) ({				\
98505e6557bSNeilBrown 	void *__mptr = (void *)(ptr);					\
98605e6557bSNeilBrown 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
98705e6557bSNeilBrown 			 !__same_type(*(ptr), void),			\
98805e6557bSNeilBrown 			 "pointer type mismatch in container_of()");	\
989227abcc6SDan Carpenter 	IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) :			\
99005e6557bSNeilBrown 		((type *)(__mptr - offsetof(type, member))); })
99105e6557bSNeilBrown 
992b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
993b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
994b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
995b9d4f426SArnaud Lacombe #endif
9969d00f92fSWANG Cong 
99758f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
99858f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms)						\
99958f86cc8SRusty Russell 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
100058f86cc8SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
100128b8d0c8SGobinda Charan Maji 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
100228b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
100328b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
100428b8d0c8SGobinda Charan Maji 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
100528b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
100628b8d0c8SGobinda Charan Maji 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
100737549e94SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
100858f86cc8SRusty Russell 	 (perms))
10091da177e4SLinus Torvalds #endif
1010