xref: /linux-6.15/include/linux/kernel.h (revision c461aed3)
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>
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>
13e0deaff4SAndrew Morton #include <linux/typecheck.h>
14968ab183SLinus Torvalds #include <linux/printk.h>
15c7acec71SIan Abbott #include <linux/build_bug.h>
161da177e4SLinus Torvalds #include <asm/byteorder.h>
17*c461aed3SJani Nikula #include <asm/div64.h>
18607ca46eSDavid Howells #include <uapi/linux/kernel.h>
191da177e4SLinus Torvalds 
204be929beSAlexey Dobriyan #define USHRT_MAX	((u16)(~0U))
214be929beSAlexey Dobriyan #define SHRT_MAX	((s16)(USHRT_MAX>>1))
224be929beSAlexey Dobriyan #define SHRT_MIN	((s16)(-SHRT_MAX - 1))
231da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
241da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
251da177e4SLinus Torvalds #define UINT_MAX	(~0U)
261da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
271da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
281da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
29111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
30111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
31111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
32a3860c1cSXi Wang #define SIZE_MAX	(~(size_t)0)
331c4bc43dSStefan Agner #define PHYS_ADDR_MAX	(~(phys_addr_t)0)
341da177e4SLinus Torvalds 
3589a07141SAlex Elder #define U8_MAX		((u8)~0U)
3689a07141SAlex Elder #define S8_MAX		((s8)(U8_MAX>>1))
3789a07141SAlex Elder #define S8_MIN		((s8)(-S8_MAX - 1))
3889a07141SAlex Elder #define U16_MAX		((u16)~0U)
3989a07141SAlex Elder #define S16_MAX		((s16)(U16_MAX>>1))
4089a07141SAlex Elder #define S16_MIN		((s16)(-S16_MAX - 1))
4189a07141SAlex Elder #define U32_MAX		((u32)~0U)
4289a07141SAlex Elder #define S32_MAX		((s32)(U32_MAX>>1))
4389a07141SAlex Elder #define S32_MIN		((s32)(-S32_MAX - 1))
4489a07141SAlex Elder #define U64_MAX		((u64)~0ULL)
4589a07141SAlex Elder #define S64_MAX		((s64)(U64_MAX>>1))
4689a07141SAlex Elder #define S64_MIN		((s64)(-S64_MAX - 1))
4789a07141SAlex Elder 
481da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
491da177e4SLinus Torvalds 
50e8c97af0SRandy Dunlap /**
51e8c97af0SRandy Dunlap  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
52e8c97af0SRandy Dunlap  * @x: value to repeat
53e8c97af0SRandy Dunlap  *
54e8c97af0SRandy Dunlap  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
55e8c97af0SRandy Dunlap  */
5644696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
5744696908SDavid S. Miller 
583ca45a46Szijun_hu /* @a is a power of 2 value */
59a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
60ed067d4aSKrzysztof Kozlowski #define ALIGN_DOWN(x, a)	__ALIGN_KERNEL((x) - ((a) - 1), (a))
619f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
62a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
63f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
642ea58144SLinus Torvalds 
65d3849953SChristoph Hellwig /* generic data direction definitions */
66d3849953SChristoph Hellwig #define READ			0
67d3849953SChristoph Hellwig #define WRITE			1
68d3849953SChristoph Hellwig 
69e8c97af0SRandy Dunlap /**
70e8c97af0SRandy Dunlap  * ARRAY_SIZE - get the number of elements in array @arr
71e8c97af0SRandy Dunlap  * @arr: array to be sized
72e8c97af0SRandy Dunlap  */
73c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
74c5e631cfSRusty Russell 
753ed605bcSGustavo Padovan #define u64_to_user_ptr(x) (		\
763ed605bcSGustavo Padovan {					\
773ed605bcSGustavo Padovan 	typecheck(u64, x);		\
783ed605bcSGustavo Padovan 	(void __user *)(uintptr_t)x;	\
793ed605bcSGustavo Padovan }					\
803ed605bcSGustavo Padovan )
813ed605bcSGustavo Padovan 
829b3be9f9SYinghai Lu /*
839b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
849b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
859b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
869b3be9f9SYinghai Lu  * arguments just once each.
879b3be9f9SYinghai Lu  */
889b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
89cedc5b6aSKees Cook /**
90cedc5b6aSKees Cook  * round_up - round up to next specified power of 2
91cedc5b6aSKees Cook  * @x: the value to round
92cedc5b6aSKees Cook  * @y: multiple to round up to (must be a power of 2)
93cedc5b6aSKees Cook  *
94cedc5b6aSKees Cook  * Rounds @x up to next multiple of @y (which must be a power of 2).
95cedc5b6aSKees Cook  * To perform arbitrary rounding up, use roundup() below.
96cedc5b6aSKees Cook  */
979b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
98cedc5b6aSKees Cook /**
99cedc5b6aSKees Cook  * round_down - round down to next specified power of 2
100cedc5b6aSKees Cook  * @x: the value to round
101cedc5b6aSKees Cook  * @y: multiple to round down to (must be a power of 2)
102cedc5b6aSKees Cook  *
103cedc5b6aSKees Cook  * Rounds @x down to next multiple of @y (which must be a power of 2).
104cedc5b6aSKees Cook  * To perform arbitrary rounding down, use rounddown() below.
105cedc5b6aSKees Cook  */
1069b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
1079b3be9f9SYinghai Lu 
108e8c97af0SRandy Dunlap /**
109e8c97af0SRandy Dunlap  * FIELD_SIZEOF - get the size of a struct's field
110e8c97af0SRandy Dunlap  * @t: the target struct
111e8c97af0SRandy Dunlap  * @f: the target struct's field
112e8c97af0SRandy Dunlap  * Return: the size of @f in the struct definition without having a
113e8c97af0SRandy Dunlap  * declared instance of @t.
114e8c97af0SRandy Dunlap  */
1154552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
116e8c97af0SRandy Dunlap 
117b5d3755aSNicolas Dichtel #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
118604df322SMasahiro Yamada 
119604df322SMasahiro Yamada #define DIV_ROUND_DOWN_ULL(ll, d) \
120604df322SMasahiro Yamada 	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
121604df322SMasahiro Yamada 
122604df322SMasahiro Yamada #define DIV_ROUND_UP_ULL(ll, d)		DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
12336a26c69SNicholas Bellinger 
12436a26c69SNicholas Bellinger #if BITS_PER_LONG == 32
12536a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
12636a26c69SNicholas Bellinger #else
12736a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
12836a26c69SNicholas Bellinger #endif
129074e61ecSJames Morris 
130cedc5b6aSKees Cook /**
131cedc5b6aSKees Cook  * roundup - round up to the next specified multiple
132cedc5b6aSKees Cook  * @x: the value to up
133cedc5b6aSKees Cook  * @y: multiple to round up to
134cedc5b6aSKees Cook  *
135cedc5b6aSKees Cook  * Rounds @x up to next multiple of @y. If @y will always be a power
136cedc5b6aSKees Cook  * of 2, consider using the faster round_up().
137cedc5b6aSKees Cook  *
138cedc5b6aSKees Cook  * The `const' here prevents gcc-3.3 from calling __divdi3
139cedc5b6aSKees Cook  */
140b28efd54SEric Paris #define roundup(x, y) (					\
141b28efd54SEric Paris {							\
1426070bf35STetsuo Handa 	const typeof(y) __y = y;			\
143b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
144b28efd54SEric Paris }							\
145b28efd54SEric Paris )
146cedc5b6aSKees Cook /**
147cedc5b6aSKees Cook  * rounddown - round down to next specified multiple
148cedc5b6aSKees Cook  * @x: the value to round
149cedc5b6aSKees Cook  * @y: multiple to round down to
150cedc5b6aSKees Cook  *
151cedc5b6aSKees Cook  * Rounds @x down to next multiple of @y. If @y will always be a power
152cedc5b6aSKees Cook  * of 2, consider using the faster round_down().
153cedc5b6aSKees Cook  */
154686a0f3dSEric Paris #define rounddown(x, y) (				\
155686a0f3dSEric Paris {							\
156686a0f3dSEric Paris 	typeof(x) __x = (x);				\
157686a0f3dSEric Paris 	__x - (__x % (y));				\
158686a0f3dSEric Paris }							\
159686a0f3dSEric Paris )
160b6d86d3dSGuenter Roeck 
161b6d86d3dSGuenter Roeck /*
1624f5901f5SNiklas Söderlund  * Divide positive or negative dividend by positive or negative divisor
1634f5901f5SNiklas Söderlund  * and round to closest integer. Result is undefined for negative
164e8c97af0SRandy Dunlap  * divisors if the dividend variable type is unsigned and for negative
1654f5901f5SNiklas Söderlund  * dividends if the divisor variable type is unsigned.
166b6d86d3dSGuenter Roeck  */
1679fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
1689fe06081SDarrick J. Wong {							\
169b6d86d3dSGuenter Roeck 	typeof(x) __x = x;				\
170b6d86d3dSGuenter Roeck 	typeof(divisor) __d = divisor;			\
171c4e18497SGuenter Roeck 	(((typeof(x))-1) > 0 ||				\
1724f5901f5SNiklas Söderlund 	 ((typeof(divisor))-1) > 0 ||			\
1734f5901f5SNiklas Söderlund 	 (((__x) > 0) == ((__d) > 0))) ?		\
174b6d86d3dSGuenter Roeck 		(((__x) + ((__d) / 2)) / (__d)) :	\
175b6d86d3dSGuenter Roeck 		(((__x) - ((__d) / 2)) / (__d));	\
1769fe06081SDarrick J. Wong }							\
1779fe06081SDarrick J. Wong )
178f766093eSJavi Merino /*
179f766093eSJavi Merino  * Same as above but for u64 dividends. divisor must be a 32-bit
180f766093eSJavi Merino  * number.
181f766093eSJavi Merino  */
182f766093eSJavi Merino #define DIV_ROUND_CLOSEST_ULL(x, divisor)(		\
183f766093eSJavi Merino {							\
184f766093eSJavi Merino 	typeof(divisor) __d = divisor;			\
185f766093eSJavi Merino 	unsigned long long _tmp = (x) + (__d) / 2;	\
186f766093eSJavi Merino 	do_div(_tmp, __d);				\
187f766093eSJavi Merino 	_tmp;						\
188f766093eSJavi Merino }							\
189f766093eSJavi Merino )
1901da177e4SLinus Torvalds 
1919993bc63SSalman Qazi /*
1929993bc63SSalman Qazi  * Multiplies an integer by a fraction, while avoiding unnecessary
1939993bc63SSalman Qazi  * overflow or loss of precision.
1949993bc63SSalman Qazi  */
1959993bc63SSalman Qazi #define mult_frac(x, numer, denom)(			\
1969993bc63SSalman Qazi {							\
1979993bc63SSalman Qazi 	typeof(x) quot = (x) / (denom);			\
1989993bc63SSalman Qazi 	typeof(x) rem  = (x) % (denom);			\
1999993bc63SSalman Qazi 	(quot * (numer)) + ((rem * (numer)) / (denom));	\
2009993bc63SSalman Qazi }							\
2019993bc63SSalman Qazi )
2029993bc63SSalman Qazi 
2039993bc63SSalman Qazi 
204ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
205ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
206ca31e146SEduard - Gabriel Munteanu 
20790c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
2082da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
2092da96acdSJens Axboe #else
2102da96acdSJens Axboe # define sector_div(n, b)( \
2112da96acdSJens Axboe { \
2122da96acdSJens Axboe 	int _res; \
2132da96acdSJens Axboe 	_res = (n) % (b); \
2142da96acdSJens Axboe 	(n) /= (b); \
2152da96acdSJens Axboe 	_res; \
2162da96acdSJens Axboe } \
2172da96acdSJens Axboe )
2182da96acdSJens Axboe #endif
2192da96acdSJens Axboe 
220218e180eSAndrew Morton /**
221218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
222218e180eSAndrew Morton  * @n: the number we're accessing
223218e180eSAndrew Morton  *
224218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
225218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
226218e180eSAndrew Morton  * 32-bits.
227218e180eSAndrew Morton  */
228218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
229218e180eSAndrew Morton 
230204b885eSJoerg Roedel /**
231204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
232204b885eSJoerg Roedel  * @n: the number we're accessing
233204b885eSJoerg Roedel  */
234204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
235204b885eSJoerg Roedel 
2361da177e4SLinus Torvalds struct completion;
237df2e71fbS[email protected] struct pt_regs;
238df2e71fbS[email protected] struct user;
2391da177e4SLinus Torvalds 
240070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
241070cb065SUwe Kleine-König extern int _cond_resched(void);
242070cb065SUwe Kleine-König # define might_resched() _cond_resched()
243070cb065SUwe Kleine-König #else
244070cb065SUwe Kleine-König # define might_resched() do { } while (0)
245070cb065SUwe Kleine-König #endif
246070cb065SUwe Kleine-König 
247d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
248568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset);
249568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset);
250568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset);
251568f1967SPeter Zijlstra 
2521da177e4SLinus Torvalds /**
2531da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
2541da177e4SLinus Torvalds  *
2551da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
2561da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
2571da177e4SLinus Torvalds  *
2581da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
259e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
2601da177e4SLinus Torvalds  * supposed to.
2611da177e4SLinus Torvalds  */
262f8cbd99bSIngo Molnar # define might_sleep() \
263e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
264568f1967SPeter Zijlstra /**
265568f1967SPeter Zijlstra  * cant_sleep - annotation for functions that cannot sleep
266568f1967SPeter Zijlstra  *
267568f1967SPeter Zijlstra  * this macro will print a stack trace if it is executed with preemption enabled
268568f1967SPeter Zijlstra  */
269568f1967SPeter Zijlstra # define cant_sleep() \
270568f1967SPeter Zijlstra 	do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
27100845eb9SLinus Torvalds # define sched_annotate_sleep()	(current->task_state_change = 0)
272f8cbd99bSIngo Molnar #else
2733427445aSPeter Zijlstra   static inline void ___might_sleep(const char *file, int line,
2743427445aSPeter Zijlstra 				   int preempt_offset) { }
275d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
276d894837fSSimon Kagstrom 				   int preempt_offset) { }
277f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
278568f1967SPeter Zijlstra # define cant_sleep() do { } while (0)
2791029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0)
280f8cbd99bSIngo Molnar #endif
281f8cbd99bSIngo Molnar 
282368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
283f8cbd99bSIngo Molnar 
284c8299cb6SMichal Nazarewicz /**
285c8299cb6SMichal Nazarewicz  * abs - return absolute value of an argument
2868f57e4d9SMichal Nazarewicz  * @x: the value.  If it is unsigned type, it is converted to signed type first.
2878f57e4d9SMichal Nazarewicz  *     char is treated as if it was signed (regardless of whether it really is)
2888f57e4d9SMichal Nazarewicz  *     but the macro's return type is preserved as char.
289c8299cb6SMichal Nazarewicz  *
2908f57e4d9SMichal Nazarewicz  * Return: an absolute value of x.
29171a90484SAndrew Morton  */
2928f57e4d9SMichal Nazarewicz #define abs(x)	__abs_choose_expr(x, long long,				\
2938f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, long,				\
2948f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, int,				\
2958f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, short,				\
2968f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, char,				\
2978f57e4d9SMichal Nazarewicz 		__builtin_choose_expr(					\
2988f57e4d9SMichal Nazarewicz 			__builtin_types_compatible_p(typeof(x), char),	\
2998f57e4d9SMichal Nazarewicz 			(char)({ signed char __x = (x); __x<0?-__x:__x; }), \
3008f57e4d9SMichal Nazarewicz 			((void)0)))))))
3018f57e4d9SMichal Nazarewicz 
3028f57e4d9SMichal Nazarewicz #define __abs_choose_expr(x, type, other) __builtin_choose_expr(	\
3038f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
3048f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x), unsigned type),		\
3058f57e4d9SMichal Nazarewicz 	({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
3061da177e4SLinus Torvalds 
30789770b0aSDaniel Borkmann /**
30889770b0aSDaniel Borkmann  * reciprocal_scale - "scale" a value into range [0, ep_ro)
30989770b0aSDaniel Borkmann  * @val: value
31089770b0aSDaniel Borkmann  * @ep_ro: right open interval endpoint
31189770b0aSDaniel Borkmann  *
31289770b0aSDaniel Borkmann  * Perform a "reciprocal multiplication" in order to "scale" a value into
313e8c97af0SRandy Dunlap  * range [0, @ep_ro), where the upper interval endpoint is right-open.
31489770b0aSDaniel Borkmann  * This is useful, e.g. for accessing a index of an array containing
315e8c97af0SRandy Dunlap  * @ep_ro elements, for example. Think of it as sort of modulus, only that
31689770b0aSDaniel Borkmann  * the result isn't that of modulo. ;) Note that if initial input is a
31789770b0aSDaniel Borkmann  * small value, then result will return 0.
31889770b0aSDaniel Borkmann  *
319e8c97af0SRandy Dunlap  * Return: a result based on @val in interval [0, @ep_ro).
32089770b0aSDaniel Borkmann  */
32189770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
32289770b0aSDaniel Borkmann {
32389770b0aSDaniel Borkmann 	return (u32)(((u64) val * ep_ro) >> 32);
32489770b0aSDaniel Borkmann }
32589770b0aSDaniel Borkmann 
326386e7906SAxel Lin #if defined(CONFIG_MMU) && \
327386e7906SAxel Lin 	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
3289ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__)
3299ec23531SDavid Hildenbrand void __might_fault(const char *file, int line);
3303ee1afa3SNick Piggin #else
331662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { }
3323ee1afa3SNick Piggin #endif
3333ee1afa3SNick Piggin 
334e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
335c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
3369402c95fSJoe Perches __printf(1, 2)
3379af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold;
338ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg);
339dd287796SAndrew Morton extern void oops_enter(void);
340dd287796SAndrew Morton extern void oops_exit(void);
341863a6049SAnton Blanchard void print_oops_end_marker(void);
342dd287796SAndrew Morton extern int oops_may_print(void);
3439af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn;
3449af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn;
34533ee3b2eSAlexey Dobriyan 
3467a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT
3477a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err);
3487a46ec0eSKees Cook #else
3497a46ec0eSKees Cook static inline void refcount_error_report(struct pt_regs *regs, const char *err)
3507a46ec0eSKees Cook { }
3517a46ec0eSKees Cook #endif
3527a46ec0eSKees Cook 
35333ee3b2eSAlexey Dobriyan /* Internal, do not use. */
35433ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
35533ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
35633ee3b2eSAlexey Dobriyan 
35733ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
35833ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
3594c925d60SEldad Zack 
3604c925d60SEldad Zack /**
3614c925d60SEldad Zack  * kstrtoul - convert a string to an unsigned long
3624c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3634c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3644c925d60SEldad Zack  *  may also be a plus sign, but not a minus sign.
3654c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3664c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3674c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3684c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3694c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3704c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3714c925d60SEldad Zack  *
3724c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3734c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3744c925d60SEldad Zack  * be checked.
3754c925d60SEldad Zack */
37633ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
37733ee3b2eSAlexey Dobriyan {
37833ee3b2eSAlexey Dobriyan 	/*
37933ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
38033ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
38133ee3b2eSAlexey Dobriyan 	 */
38233ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
38333ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
38433ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
38533ee3b2eSAlexey Dobriyan 	else
38633ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
38733ee3b2eSAlexey Dobriyan }
38833ee3b2eSAlexey Dobriyan 
3894c925d60SEldad Zack /**
3904c925d60SEldad Zack  * kstrtol - convert a string to a long
3914c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3924c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3934c925d60SEldad Zack  *  may also be a plus sign or a minus sign.
3944c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3954c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3964c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3974c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3984c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3994c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
4004c925d60SEldad Zack  *
4014c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
4024c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
4034c925d60SEldad Zack  * be checked.
4044c925d60SEldad Zack  */
40533ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
40633ee3b2eSAlexey Dobriyan {
40733ee3b2eSAlexey Dobriyan 	/*
40833ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
40933ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
41033ee3b2eSAlexey Dobriyan 	 */
41133ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
41233ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
41333ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
41433ee3b2eSAlexey Dobriyan 	else
41533ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
41633ee3b2eSAlexey Dobriyan }
41733ee3b2eSAlexey Dobriyan 
41833ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
41933ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
42033ee3b2eSAlexey Dobriyan 
42133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
42233ee3b2eSAlexey Dobriyan {
42333ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
42433ee3b2eSAlexey Dobriyan }
42533ee3b2eSAlexey Dobriyan 
42633ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
42733ee3b2eSAlexey Dobriyan {
42833ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
42933ee3b2eSAlexey Dobriyan }
43033ee3b2eSAlexey Dobriyan 
43133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
43233ee3b2eSAlexey Dobriyan {
43333ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
43433ee3b2eSAlexey Dobriyan }
43533ee3b2eSAlexey Dobriyan 
43633ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
43733ee3b2eSAlexey Dobriyan {
43833ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
43933ee3b2eSAlexey Dobriyan }
44033ee3b2eSAlexey Dobriyan 
44133ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
44233ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
44333ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
44433ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
445ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res);
44633ee3b2eSAlexey Dobriyan 
447c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
448c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
449c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
450c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
451c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
452c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
453c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
454c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
455c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
456c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
457ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
458c196e32aSAlexey Dobriyan 
459c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
460c196e32aSAlexey Dobriyan {
461c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
462c196e32aSAlexey Dobriyan }
463c196e32aSAlexey Dobriyan 
464c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
465c196e32aSAlexey Dobriyan {
466c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
467c196e32aSAlexey Dobriyan }
468c196e32aSAlexey Dobriyan 
469c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
470c196e32aSAlexey Dobriyan {
471c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
472c196e32aSAlexey Dobriyan }
473c196e32aSAlexey Dobriyan 
474c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
475c196e32aSAlexey Dobriyan {
476c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
477c196e32aSAlexey Dobriyan }
478c196e32aSAlexey Dobriyan 
47967d0a075SJoe Perches /* Obsolete, do not use.  Use kstrto<foo> instead */
48067d0a075SJoe Perches 
4811da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
4821da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
4831da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
4841da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
48533ee3b2eSAlexey Dobriyan 
486d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size,
487d1be35cbSAndrei Vagin 		      unsigned long long num, unsigned int width);
4881ac101a5SKAMEZAWA Hiroyuki 
48967d0a075SJoe Perches /* lib/printf utilities */
49067d0a075SJoe Perches 
491b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
492b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
493b9075fa9SJoe Perches extern __printf(3, 4)
494b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
495b9075fa9SJoe Perches extern __printf(3, 0)
496b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
497b9075fa9SJoe Perches extern __printf(3, 4)
498b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
499b9075fa9SJoe Perches extern __printf(3, 0)
500b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
50148a27055SRasmus Villemoes extern __printf(2, 3) __malloc
502b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
50348a27055SRasmus Villemoes extern __printf(2, 0) __malloc
5048db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
5050a9df786SRasmus Villemoes extern __printf(2, 0)
5060a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
5071da177e4SLinus Torvalds 
5086061d949SJoe Perches extern __scanf(2, 3)
5096061d949SJoe Perches int sscanf(const char *, const char *, ...);
5106061d949SJoe Perches extern __scanf(2, 0)
5116061d949SJoe Perches int vsscanf(const char *, const char *, va_list);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
5141da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
515d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
5166ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option);
517f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val);
5181da177e4SLinus Torvalds 
5195e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
5209fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr);
521cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
5221da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
5231da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
524ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
525ab7476cfSArjan van de Ven 
5261da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
5271da177e4SLinus Torvalds 
52847a36163SCrt Mori #if BITS_PER_LONG < 64
52947a36163SCrt Mori u32 int_sqrt64(u64 x);
53047a36163SCrt Mori #else
53147a36163SCrt Mori static inline u32 int_sqrt64(u64 x)
53247a36163SCrt Mori {
53347a36163SCrt Mori 	return (u32)int_sqrt(x);
53447a36163SCrt Mori }
53547a36163SCrt Mori #endif
53647a36163SCrt Mori 
5371da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
5381da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
539aa727107SAdrian Bunk extern int panic_timeout;
54081c9d43fSFeng Tang extern unsigned long panic_print;
5411da177e4SLinus Torvalds extern int panic_on_oops;
5428da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
5435211a242SKurt Garloff extern int panic_on_io_nmi;
5449e3961a0SPrarit Bhargava extern int panic_on_warn;
545088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall;
54655af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
5475375b708SHATAYAMA Daisuke 
5485375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers;
5495375b708SHATAYAMA Daisuke 
5505800dc3cSJason Baron /*
5511717f209SHidehiro Kawai  * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
5521717f209SHidehiro Kawai  * holds a CPU number which is executing panic() currently. A value of
5531717f209SHidehiro Kawai  * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
5541717f209SHidehiro Kawai  */
5551717f209SHidehiro Kawai extern atomic_t panic_cpu;
5561717f209SHidehiro Kawai #define PANIC_CPU_INVALID	-1
5571717f209SHidehiro Kawai 
5581717f209SHidehiro Kawai /*
5595800dc3cSJason Baron  * Only to be used by arch init code. If the user over-wrote the default
5605800dc3cSJason Baron  * CONFIG_PANIC_TIMEOUT, honor it.
5615800dc3cSJason Baron  */
5625800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
5635800dc3cSJason Baron {
5645800dc3cSJason Baron 	if (panic_timeout == arch_default_timeout)
5655800dc3cSJason Baron 		panic_timeout = timeout;
5665800dc3cSJason Baron }
5671da177e4SLinus Torvalds extern const char *print_tainted(void);
568373d4d09SRusty Russell enum lockdep_ok {
569373d4d09SRusty Russell 	LOCKDEP_STILL_OK,
570373d4d09SRusty Russell 	LOCKDEP_NOW_UNRELIABLE
571373d4d09SRusty Russell };
572373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok);
57325ddbb18SAndi Kleen extern int test_taint(unsigned flag);
57425ddbb18SAndi Kleen extern unsigned long get_taint(void);
575b920de1bSDavid Howells extern int root_mountflags;
5761da177e4SLinus Torvalds 
5772ce802f6STejun Heo extern bool early_boot_irqs_disabled;
5782ce802f6STejun Heo 
57969a78ff2SThomas Gleixner /*
58069a78ff2SThomas Gleixner  * Values used for system_state. Ordering of the states must not be changed
58169a78ff2SThomas Gleixner  * as code checks for <, <=, >, >= STATE.
58269a78ff2SThomas Gleixner  */
5831da177e4SLinus Torvalds extern enum system_states {
5841da177e4SLinus Torvalds 	SYSTEM_BOOTING,
58569a78ff2SThomas Gleixner 	SYSTEM_SCHEDULING,
5861da177e4SLinus Torvalds 	SYSTEM_RUNNING,
5871da177e4SLinus Torvalds 	SYSTEM_HALT,
5881da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
5891da177e4SLinus Torvalds 	SYSTEM_RESTART,
590c1a957d1SThomas Gleixner 	SYSTEM_SUSPEND,
5911da177e4SLinus Torvalds } system_state;
5921da177e4SLinus Torvalds 
59347d4b263SKees Cook /* This cannot be an enum because some may be used in assembly source. */
59425ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
59525ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
5968c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC		2
59725ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
59825ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
59925ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
60025ddbb18SAndi Kleen #define TAINT_USER			6
60125ddbb18SAndi Kleen #define TAINT_DIE			7
60225ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
60325ddbb18SAndi Kleen #define TAINT_WARN			9
60426e9a397SLinus Torvalds #define TAINT_CRAP			10
60592946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
6062449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
60766cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE		13
60869361eefSJosh Hunt #define TAINT_SOFTLOCKUP		14
609c5f45465SSeth Jennings #define TAINT_LIVEPATCH			15
6104efb442cSBorislav Petkov #define TAINT_AUX			16
611bc4f2f54SKees Cook #define TAINT_RANDSTRUCT		17
612bc4f2f54SKees Cook #define TAINT_FLAGS_COUNT		18
6137fd8329bSPetr Mladek 
6147fd8329bSPetr Mladek struct taint_flag {
6155eb7c0d0SLarry Finger 	char c_true;	/* character printed when tainted */
6165eb7c0d0SLarry Finger 	char c_false;	/* character printed when not tainted */
6177fd8329bSPetr Mladek 	bool module;	/* also show as a per-module taint flag */
6187fd8329bSPetr Mladek };
6197fd8329bSPetr Mladek 
6207fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
6211da177e4SLinus Torvalds 
6223fc95772SHarvey Harrison extern const char hex_asc[];
6233fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
6243fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
6253fc95772SHarvey Harrison 
62655036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
6273fc95772SHarvey Harrison {
6283fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
6293fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
6303fc95772SHarvey Harrison 	return buf;
6313fc95772SHarvey Harrison }
63299eaf3c4SRandy Dunlap 
633c26d436cSAndre Naujoks extern const char hex_asc_upper[];
634c26d436cSAndre Naujoks #define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
635c26d436cSAndre Naujoks #define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]
636c26d436cSAndre Naujoks 
637c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte)
638c26d436cSAndre Naujoks {
639c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_hi(byte);
640c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_lo(byte);
641c26d436cSAndre Naujoks 	return buf;
642c26d436cSAndre Naujoks }
643c26d436cSAndre Naujoks 
64490378889SAndy Shevchenko extern int hex_to_bin(char ch);
645b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
64653d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count);
64790378889SAndy Shevchenko 
648a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac);
6494cd5773aSAndy Shevchenko 
6508a64f336SJoe Perches /*
651526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
6522002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
6532002c258SSteven Rostedt  *
6542002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
6552002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
656156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
6572002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
6582002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
6592002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
6602002c258SSteven Rostedt  * to continue tracing.
6612002c258SSteven Rostedt  *
6622002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
6632002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
6642002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
6652002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
6662002c258SSteven Rostedt  *
6672002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
668526211bcSIngo Molnar  */
669cecbca96SFrederic Weisbecker 
670cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
671cecbca96SFrederic Weisbecker 	DUMP_NONE,
672cecbca96SFrederic Weisbecker 	DUMP_ALL,
673cecbca96SFrederic Weisbecker 	DUMP_ORIG,
674cecbca96SFrederic Weisbecker };
675cecbca96SFrederic Weisbecker 
676526211bcSIngo Molnar #ifdef CONFIG_TRACING
67793d68e52SSteven Rostedt void tracing_on(void);
67893d68e52SSteven Rostedt void tracing_off(void);
67993d68e52SSteven Rostedt int tracing_is_on(void);
680ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
681ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
68293d68e52SSteven Rostedt 
683526211bcSIngo Molnar extern void tracing_start(void);
684526211bcSIngo Molnar extern void tracing_stop(void);
685526211bcSIngo Molnar 
686b9075fa9SJoe Perches static inline __printf(1, 2)
687b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
688769b0441SFrederic Weisbecker {
689769b0441SFrederic Weisbecker }
690769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
691769b0441SFrederic Weisbecker do {									\
692769b0441SFrederic Weisbecker 	if (0)								\
693769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
694769b0441SFrederic Weisbecker } while (0)
695769b0441SFrederic Weisbecker 
696526211bcSIngo Molnar /**
697526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
698526211bcSIngo Molnar  * @fmt: the printf format for printing
699526211bcSIngo Molnar  *
700e8c97af0SRandy Dunlap  * Note: __trace_printk is an internal function for trace_printk() and
701e8c97af0SRandy Dunlap  *       the @ip is passed in via the trace_printk() macro.
702526211bcSIngo Molnar  *
703526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
704526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
705526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
706526211bcSIngo Molnar  * where problems are occurring.
707526211bcSIngo Molnar  *
708526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
709526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
71009ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
711e8c97af0SRandy Dunlap  * allocated when trace_printk() is used.)
7129d3c752cSSteven Rostedt (Red Hat)  *
7138730662dSWei Wang  * A little optimization trick is done here. If there's only one
7149d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
7159d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
7169d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
7179d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
7189d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
7199d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
7209d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
7219d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
7229d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
7239d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
724526211bcSIngo Molnar  */
725769b0441SFrederic Weisbecker 
7269d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
7279d3c752cSSteven Rostedt (Red Hat) do {							\
7289d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
7299d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
7309d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
7319d3c752cSSteven Rostedt (Red Hat) 	else						\
7329d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
7339d3c752cSSteven Rostedt (Red Hat) } while (0)
7349d3c752cSSteven Rostedt (Red Hat) 
7359d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
736769b0441SFrederic Weisbecker do {									\
7373debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
73848ead020SFrederic Weisbecker 		__attribute__((section("__trace_printk_fmt"))) =	\
73948ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
74048ead020SFrederic Weisbecker 									\
74107d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
74207d777feSSteven Rostedt 									\
74307d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
74448ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
74507d777feSSteven Rostedt 	else								\
74648ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
747769b0441SFrederic Weisbecker } while (0)
748769b0441SFrederic Weisbecker 
749b9075fa9SJoe Perches extern __printf(2, 3)
750b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
75148ead020SFrederic Weisbecker 
752b9075fa9SJoe Perches extern __printf(2, 3)
753b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
754769b0441SFrederic Weisbecker 
75509ae7234SSteven Rostedt (Red Hat) /**
75609ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
75709ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
75809ae7234SSteven Rostedt (Red Hat)  *
75909ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
76009ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
76109ae7234SSteven Rostedt (Red Hat)  *
76209ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
763e8c97af0SRandy Dunlap  * paths that a developer wants the least amount of "Heisenbug" effects,
76409ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
76509ae7234SSteven Rostedt (Red Hat)  *
76609ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
76709ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
76809ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
76909ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
77009ae7234SSteven Rostedt (Red Hat)  *
77109ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
77209ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
77309ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
774e8c97af0SRandy Dunlap  * allocated when trace_puts() is used.)
77509ae7234SSteven Rostedt (Red Hat)  *
77609ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
77709ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
77809ae7234SSteven Rostedt (Red Hat)  */
77909ae7234SSteven Rostedt (Red Hat) 
78009ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
7813debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
78209ae7234SSteven Rostedt (Red Hat) 		__attribute__((section("__trace_printk_fmt"))) =	\
78309ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
78409ae7234SSteven Rostedt (Red Hat) 									\
78509ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
78609ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
78709ae7234SSteven Rostedt (Red Hat) 	else								\
78809ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
78909ae7234SSteven Rostedt (Red Hat) })
790bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str);
791bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size);
79209ae7234SSteven Rostedt (Red Hat) 
793c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
79403889384SSteven Rostedt 
79548ead020SFrederic Weisbecker /*
79648ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
79748ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
79848ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
79948ead020SFrederic Weisbecker  */
800769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
801769b0441SFrederic Weisbecker do {									\
80248ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
8033debb0a9SSteven Rostedt (Red Hat) 		static const char *trace_printk_fmt __used		\
80448ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
80548ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
8067bffc23eSIngo Molnar 									\
80748ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
80848ead020SFrederic Weisbecker 	} else								\
80948ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
810769b0441SFrederic Weisbecker } while (0)
811769b0441SFrederic Weisbecker 
8128db14860SNicolas Iooss extern __printf(2, 0) int
81348ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
81448ead020SFrederic Weisbecker 
8158db14860SNicolas Iooss extern __printf(2, 0) int
816526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
817769b0441SFrederic Weisbecker 
818cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
819526211bcSIngo Molnar #else
820526211bcSIngo Molnar static inline void tracing_start(void) { }
821526211bcSIngo Molnar static inline void tracing_stop(void) { }
822e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { }
82393d68e52SSteven Rostedt 
82493d68e52SSteven Rostedt static inline void tracing_on(void) { }
82593d68e52SSteven Rostedt static inline void tracing_off(void) { }
82693d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
827ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
828ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
82993d68e52SSteven Rostedt 
83060efc15aSMichal Hocko static inline __printf(1, 2)
83160efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
832526211bcSIngo Molnar {
833526211bcSIngo Molnar 	return 0;
834526211bcSIngo Molnar }
8358db14860SNicolas Iooss static __printf(1, 0) inline int
836526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
837526211bcSIngo Molnar {
838526211bcSIngo Molnar 	return 0;
839526211bcSIngo Molnar }
840cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
841769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
842526211bcSIngo Molnar 
843526211bcSIngo Molnar /*
8443c8ba0d6SKees Cook  * min()/max()/clamp() macros must accomplish three things:
8453c8ba0d6SKees Cook  *
8463c8ba0d6SKees Cook  * - avoid multiple evaluations of the arguments (so side-effects like
8473c8ba0d6SKees Cook  *   "x++" happen only once) when non-constant.
8483c8ba0d6SKees Cook  * - perform strict type-checking (to generate warnings instead of
8493c8ba0d6SKees Cook  *   nasty runtime surprises). See the "unnecessary" pointer comparison
8503c8ba0d6SKees Cook  *   in __typecheck().
8513c8ba0d6SKees Cook  * - retain result as a constant expressions when called with only
8523c8ba0d6SKees Cook  *   constant expressions (to avoid tripping VLA warnings in stack
8533c8ba0d6SKees Cook  *   allocation usage).
8541da177e4SLinus Torvalds  */
8553c8ba0d6SKees Cook #define __typecheck(x, y) \
8563c8ba0d6SKees Cook 		(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
8573c8ba0d6SKees Cook 
8583c8ba0d6SKees Cook /*
8593c8ba0d6SKees Cook  * This returns a constant expression while determining if an argument is
8603c8ba0d6SKees Cook  * a constant expression, most importantly without evaluating the argument.
8613c8ba0d6SKees Cook  * Glory to Martin Uecker <[email protected]>
8623c8ba0d6SKees Cook  */
8633c8ba0d6SKees Cook #define __is_constexpr(x) \
8643c8ba0d6SKees Cook 	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
8653c8ba0d6SKees Cook 
8663c8ba0d6SKees Cook #define __no_side_effects(x, y) \
8673c8ba0d6SKees Cook 		(__is_constexpr(x) && __is_constexpr(y))
8683c8ba0d6SKees Cook 
8693c8ba0d6SKees Cook #define __safe_cmp(x, y) \
8703c8ba0d6SKees Cook 		(__typecheck(x, y) && __no_side_effects(x, y))
8713c8ba0d6SKees Cook 
8723c8ba0d6SKees Cook #define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
8733c8ba0d6SKees Cook 
874e9092d0dSLinus Torvalds #define __cmp_once(x, y, unique_x, unique_y, op) ({	\
875e9092d0dSLinus Torvalds 		typeof(x) unique_x = (x);		\
876e9092d0dSLinus Torvalds 		typeof(y) unique_y = (y);		\
877e9092d0dSLinus Torvalds 		__cmp(unique_x, unique_y, op); })
8783c8ba0d6SKees Cook 
8793c8ba0d6SKees Cook #define __careful_cmp(x, y, op) \
8803c8ba0d6SKees Cook 	__builtin_choose_expr(__safe_cmp(x, y), \
881e9092d0dSLinus Torvalds 		__cmp(x, y, op), \
882e9092d0dSLinus Torvalds 		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
883e8c97af0SRandy Dunlap 
884e8c97af0SRandy Dunlap /**
885e8c97af0SRandy Dunlap  * min - return minimum of two values of the same or compatible types
886e8c97af0SRandy Dunlap  * @x: first value
887e8c97af0SRandy Dunlap  * @y: second value
888e8c97af0SRandy Dunlap  */
8893c8ba0d6SKees Cook #define min(x, y)	__careful_cmp(x, y, <)
890e8c97af0SRandy Dunlap 
891e8c97af0SRandy Dunlap /**
892e8c97af0SRandy Dunlap  * max - return maximum of two values of the same or compatible types
893e8c97af0SRandy Dunlap  * @x: first value
894e8c97af0SRandy Dunlap  * @y: second value
895e8c97af0SRandy Dunlap  */
8963c8ba0d6SKees Cook #define max(x, y)	__careful_cmp(x, y, >)
897bdf4bbaaSHarvey Harrison 
898e8c97af0SRandy Dunlap /**
899e8c97af0SRandy Dunlap  * min3 - return minimum of three values
900e8c97af0SRandy Dunlap  * @x: first value
901e8c97af0SRandy Dunlap  * @y: second value
902e8c97af0SRandy Dunlap  * @z: third value
903e8c97af0SRandy Dunlap  */
9042e1d06e1SMichal Nazarewicz #define min3(x, y, z) min((typeof(x))min(x, y), z)
905e8c97af0SRandy Dunlap 
906e8c97af0SRandy Dunlap /**
907e8c97af0SRandy Dunlap  * max3 - return maximum of three values
908e8c97af0SRandy Dunlap  * @x: first value
909e8c97af0SRandy Dunlap  * @y: second value
910e8c97af0SRandy Dunlap  * @z: third value
911e8c97af0SRandy Dunlap  */
9122e1d06e1SMichal Nazarewicz #define max3(x, y, z) max((typeof(x))max(x, y), z)
913f27c85c5SHagen Paul Pfeifer 
914bdf4bbaaSHarvey Harrison /**
915c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
916c8bf1336SMartin K. Petersen  * @x: value1
917c8bf1336SMartin K. Petersen  * @y: value2
918c8bf1336SMartin K. Petersen  */
919c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
920c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
921c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
922c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
923c8bf1336SMartin K. Petersen 
924c8bf1336SMartin K. Petersen /**
925bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
926bdf4bbaaSHarvey Harrison  * @val: current value
9272e1d06e1SMichal Nazarewicz  * @lo: lowest allowable value
9282e1d06e1SMichal Nazarewicz  * @hi: highest allowable value
929bdf4bbaaSHarvey Harrison  *
930e8c97af0SRandy Dunlap  * This macro does strict typechecking of @lo/@hi to make sure they are of the
931e8c97af0SRandy Dunlap  * same type as @val.  See the unnecessary pointer comparisons.
932bdf4bbaaSHarvey Harrison  */
9332e1d06e1SMichal Nazarewicz #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds /*
9361da177e4SLinus Torvalds  * ..and if you can't take the strict
9371da177e4SLinus Torvalds  * types, you can specify one yourself.
9381da177e4SLinus Torvalds  *
939bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
9401da177e4SLinus Torvalds  */
941e8c97af0SRandy Dunlap 
942e8c97af0SRandy Dunlap /**
943e8c97af0SRandy Dunlap  * min_t - return minimum of two values, using the specified type
944e8c97af0SRandy Dunlap  * @type: data type to use
945e8c97af0SRandy Dunlap  * @x: first value
946e8c97af0SRandy Dunlap  * @y: second value
947e8c97af0SRandy Dunlap  */
9483c8ba0d6SKees Cook #define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
9491da177e4SLinus Torvalds 
950e8c97af0SRandy Dunlap /**
951e8c97af0SRandy Dunlap  * max_t - return maximum of two values, using the specified type
952e8c97af0SRandy Dunlap  * @type: data type to use
953e8c97af0SRandy Dunlap  * @x: first value
954e8c97af0SRandy Dunlap  * @y: second value
955e8c97af0SRandy Dunlap  */
9563c8ba0d6SKees Cook #define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
957bdf4bbaaSHarvey Harrison 
958bdf4bbaaSHarvey Harrison /**
959bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
960bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
961bdf4bbaaSHarvey Harrison  * @val: current value
962c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
963c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
964bdf4bbaaSHarvey Harrison  *
965bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
966e8c97af0SRandy Dunlap  * @type to make all the comparisons.
967bdf4bbaaSHarvey Harrison  */
968c185b07fSMichal Nazarewicz #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
969bdf4bbaaSHarvey Harrison 
970bdf4bbaaSHarvey Harrison /**
971bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
972bdf4bbaaSHarvey Harrison  * @val: current value
973c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
974c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
975bdf4bbaaSHarvey Harrison  *
976bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
977e8c97af0SRandy Dunlap  * type the input argument @val is.  This is useful when @val is an unsigned
978e8c97af0SRandy Dunlap  * type and @lo and @hi are literals that will otherwise be assigned a signed
979bdf4bbaaSHarvey Harrison  * integer type.
980bdf4bbaaSHarvey Harrison  */
981c185b07fSMichal Nazarewicz #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
9821da177e4SLinus Torvalds 
98391f68b73SWu Fengguang 
984e8c97af0SRandy Dunlap /**
985e8c97af0SRandy Dunlap  * swap - swap values of @a and @b
986e8c97af0SRandy Dunlap  * @a: first value
987e8c97af0SRandy Dunlap  * @b: second value
98891f68b73SWu Fengguang  */
989ac7b9004SPeter Zijlstra #define swap(a, b) \
990ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
99191f68b73SWu Fengguang 
992cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */
993cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
994cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
995cf14f27fSAlexei Starovoitov 
996cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b
997cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b)
998cf14f27fSAlexei Starovoitov 
9991da177e4SLinus Torvalds /**
10001da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
10011da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
10021da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
10031da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
10041da177e4SLinus Torvalds  *
10051da177e4SLinus Torvalds  */
10061da177e4SLinus Torvalds #define container_of(ptr, type, member) ({				\
1007c7acec71SIan Abbott 	void *__mptr = (void *)(ptr);					\
1008c7acec71SIan Abbott 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
1009c7acec71SIan Abbott 			 !__same_type(*(ptr), void),			\
1010c7acec71SIan Abbott 			 "pointer type mismatch in container_of()");	\
1011c7acec71SIan Abbott 	((type *)(__mptr - offsetof(type, member))); })
10121da177e4SLinus Torvalds 
101305e6557bSNeilBrown /**
101405e6557bSNeilBrown  * container_of_safe - cast a member of a structure out to the containing structure
101505e6557bSNeilBrown  * @ptr:	the pointer to the member.
101605e6557bSNeilBrown  * @type:	the type of the container struct this is embedded in.
101705e6557bSNeilBrown  * @member:	the name of the member within the struct.
101805e6557bSNeilBrown  *
101905e6557bSNeilBrown  * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
102005e6557bSNeilBrown  */
102105e6557bSNeilBrown #define container_of_safe(ptr, type, member) ({				\
102205e6557bSNeilBrown 	void *__mptr = (void *)(ptr);					\
102305e6557bSNeilBrown 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
102405e6557bSNeilBrown 			 !__same_type(*(ptr), void),			\
102505e6557bSNeilBrown 			 "pointer type mismatch in container_of()");	\
1026227abcc6SDan Carpenter 	IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) :			\
102705e6557bSNeilBrown 		((type *)(__mptr - offsetof(type, member))); })
102805e6557bSNeilBrown 
1029b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
1030b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
1031b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
1032b9d4f426SArnaud Lacombe #endif
10339d00f92fSWANG Cong 
103458f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
103558f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms)						\
103658f86cc8SRusty Russell 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
103758f86cc8SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
103828b8d0c8SGobinda Charan Maji 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
103928b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
104028b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
104128b8d0c8SGobinda Charan Maji 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
104228b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
104328b8d0c8SGobinda Charan Maji 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
104437549e94SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
104558f86cc8SRusty Russell 	 (perms))
10461da177e4SLinus Torvalds #endif
1047