xref: /linux-6.15/include/linux/kernel.h (revision cf14f27f)
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>
17607ca46eSDavid Howells #include <uapi/linux/kernel.h>
181da177e4SLinus Torvalds 
194be929beSAlexey Dobriyan #define USHRT_MAX	((u16)(~0U))
204be929beSAlexey Dobriyan #define SHRT_MAX	((s16)(USHRT_MAX>>1))
214be929beSAlexey Dobriyan #define SHRT_MIN	((s16)(-SHRT_MAX - 1))
221da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
231da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
241da177e4SLinus Torvalds #define UINT_MAX	(~0U)
251da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
261da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
271da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
28111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
29111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
30111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
31a3860c1cSXi Wang #define SIZE_MAX	(~(size_t)0)
321da177e4SLinus Torvalds 
3389a07141SAlex Elder #define U8_MAX		((u8)~0U)
3489a07141SAlex Elder #define S8_MAX		((s8)(U8_MAX>>1))
3589a07141SAlex Elder #define S8_MIN		((s8)(-S8_MAX - 1))
3689a07141SAlex Elder #define U16_MAX		((u16)~0U)
3789a07141SAlex Elder #define S16_MAX		((s16)(U16_MAX>>1))
3889a07141SAlex Elder #define S16_MIN		((s16)(-S16_MAX - 1))
3989a07141SAlex Elder #define U32_MAX		((u32)~0U)
4089a07141SAlex Elder #define S32_MAX		((s32)(U32_MAX>>1))
4189a07141SAlex Elder #define S32_MIN		((s32)(-S32_MAX - 1))
4289a07141SAlex Elder #define U64_MAX		((u64)~0ULL)
4389a07141SAlex Elder #define S64_MAX		((s64)(U64_MAX>>1))
4489a07141SAlex Elder #define S64_MIN		((s64)(-S64_MAX - 1))
4589a07141SAlex Elder 
461da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
471da177e4SLinus Torvalds 
48e8c97af0SRandy Dunlap /**
49e8c97af0SRandy Dunlap  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
50e8c97af0SRandy Dunlap  * @x: value to repeat
51e8c97af0SRandy Dunlap  *
52e8c97af0SRandy Dunlap  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
53e8c97af0SRandy Dunlap  */
5444696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
5544696908SDavid S. Miller 
563ca45a46Szijun_hu /* @a is a power of 2 value */
57a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
58ed067d4aSKrzysztof Kozlowski #define ALIGN_DOWN(x, a)	__ALIGN_KERNEL((x) - ((a) - 1), (a))
599f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
60a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
61f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
622ea58144SLinus Torvalds 
63d3849953SChristoph Hellwig /* generic data direction definitions */
64d3849953SChristoph Hellwig #define READ			0
65d3849953SChristoph Hellwig #define WRITE			1
66d3849953SChristoph Hellwig 
67e8c97af0SRandy Dunlap /**
68e8c97af0SRandy Dunlap  * ARRAY_SIZE - get the number of elements in array @arr
69e8c97af0SRandy Dunlap  * @arr: array to be sized
70e8c97af0SRandy Dunlap  */
71c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
72c5e631cfSRusty Russell 
733ed605bcSGustavo Padovan #define u64_to_user_ptr(x) (		\
743ed605bcSGustavo Padovan {					\
753ed605bcSGustavo Padovan 	typecheck(u64, x);		\
763ed605bcSGustavo Padovan 	(void __user *)(uintptr_t)x;	\
773ed605bcSGustavo Padovan }					\
783ed605bcSGustavo Padovan )
793ed605bcSGustavo Padovan 
809b3be9f9SYinghai Lu /*
819b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
829b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
839b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
849b3be9f9SYinghai Lu  * arguments just once each.
859b3be9f9SYinghai Lu  */
869b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
879b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
889b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
899b3be9f9SYinghai Lu 
90e8c97af0SRandy Dunlap /**
91e8c97af0SRandy Dunlap  * FIELD_SIZEOF - get the size of a struct's field
92e8c97af0SRandy Dunlap  * @t: the target struct
93e8c97af0SRandy Dunlap  * @f: the target struct's field
94e8c97af0SRandy Dunlap  * Return: the size of @f in the struct definition without having a
95e8c97af0SRandy Dunlap  * declared instance of @t.
96e8c97af0SRandy Dunlap  */
974552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
98e8c97af0SRandy Dunlap 
99b5d3755aSNicolas Dichtel #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
100604df322SMasahiro Yamada 
101604df322SMasahiro Yamada #define DIV_ROUND_DOWN_ULL(ll, d) \
102604df322SMasahiro Yamada 	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
103604df322SMasahiro Yamada 
104604df322SMasahiro Yamada #define DIV_ROUND_UP_ULL(ll, d)		DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
10536a26c69SNicholas Bellinger 
10636a26c69SNicholas Bellinger #if BITS_PER_LONG == 32
10736a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
10836a26c69SNicholas Bellinger #else
10936a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
11036a26c69SNicholas Bellinger #endif
111074e61ecSJames Morris 
112074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
113b28efd54SEric Paris #define roundup(x, y) (					\
114b28efd54SEric Paris {							\
1156070bf35STetsuo Handa 	const typeof(y) __y = y;			\
116b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
117b28efd54SEric Paris }							\
118b28efd54SEric Paris )
119686a0f3dSEric Paris #define rounddown(x, y) (				\
120686a0f3dSEric Paris {							\
121686a0f3dSEric Paris 	typeof(x) __x = (x);				\
122686a0f3dSEric Paris 	__x - (__x % (y));				\
123686a0f3dSEric Paris }							\
124686a0f3dSEric Paris )
125b6d86d3dSGuenter Roeck 
126b6d86d3dSGuenter Roeck /*
1274f5901f5SNiklas Söderlund  * Divide positive or negative dividend by positive or negative divisor
1284f5901f5SNiklas Söderlund  * and round to closest integer. Result is undefined for negative
129e8c97af0SRandy Dunlap  * divisors if the dividend variable type is unsigned and for negative
1304f5901f5SNiklas Söderlund  * dividends if the divisor variable type is unsigned.
131b6d86d3dSGuenter Roeck  */
1329fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
1339fe06081SDarrick J. Wong {							\
134b6d86d3dSGuenter Roeck 	typeof(x) __x = x;				\
135b6d86d3dSGuenter Roeck 	typeof(divisor) __d = divisor;			\
136c4e18497SGuenter Roeck 	(((typeof(x))-1) > 0 ||				\
1374f5901f5SNiklas Söderlund 	 ((typeof(divisor))-1) > 0 ||			\
1384f5901f5SNiklas Söderlund 	 (((__x) > 0) == ((__d) > 0))) ?		\
139b6d86d3dSGuenter Roeck 		(((__x) + ((__d) / 2)) / (__d)) :	\
140b6d86d3dSGuenter Roeck 		(((__x) - ((__d) / 2)) / (__d));	\
1419fe06081SDarrick J. Wong }							\
1429fe06081SDarrick J. Wong )
143f766093eSJavi Merino /*
144f766093eSJavi Merino  * Same as above but for u64 dividends. divisor must be a 32-bit
145f766093eSJavi Merino  * number.
146f766093eSJavi Merino  */
147f766093eSJavi Merino #define DIV_ROUND_CLOSEST_ULL(x, divisor)(		\
148f766093eSJavi Merino {							\
149f766093eSJavi Merino 	typeof(divisor) __d = divisor;			\
150f766093eSJavi Merino 	unsigned long long _tmp = (x) + (__d) / 2;	\
151f766093eSJavi Merino 	do_div(_tmp, __d);				\
152f766093eSJavi Merino 	_tmp;						\
153f766093eSJavi Merino }							\
154f766093eSJavi Merino )
1551da177e4SLinus Torvalds 
1569993bc63SSalman Qazi /*
1579993bc63SSalman Qazi  * Multiplies an integer by a fraction, while avoiding unnecessary
1589993bc63SSalman Qazi  * overflow or loss of precision.
1599993bc63SSalman Qazi  */
1609993bc63SSalman Qazi #define mult_frac(x, numer, denom)(			\
1619993bc63SSalman Qazi {							\
1629993bc63SSalman Qazi 	typeof(x) quot = (x) / (denom);			\
1639993bc63SSalman Qazi 	typeof(x) rem  = (x) % (denom);			\
1649993bc63SSalman Qazi 	(quot * (numer)) + ((rem * (numer)) / (denom));	\
1659993bc63SSalman Qazi }							\
1669993bc63SSalman Qazi )
1679993bc63SSalman Qazi 
1689993bc63SSalman Qazi 
169ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
170ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
171ca31e146SEduard - Gabriel Munteanu 
17290c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
1732da96acdSJens Axboe # include <asm/div64.h>
1742da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
1752da96acdSJens Axboe #else
1762da96acdSJens Axboe # define sector_div(n, b)( \
1772da96acdSJens Axboe { \
1782da96acdSJens Axboe 	int _res; \
1792da96acdSJens Axboe 	_res = (n) % (b); \
1802da96acdSJens Axboe 	(n) /= (b); \
1812da96acdSJens Axboe 	_res; \
1822da96acdSJens Axboe } \
1832da96acdSJens Axboe )
1842da96acdSJens Axboe #endif
1852da96acdSJens Axboe 
186218e180eSAndrew Morton /**
187218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
188218e180eSAndrew Morton  * @n: the number we're accessing
189218e180eSAndrew Morton  *
190218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
191218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
192218e180eSAndrew Morton  * 32-bits.
193218e180eSAndrew Morton  */
194218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
195218e180eSAndrew Morton 
196204b885eSJoerg Roedel /**
197204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
198204b885eSJoerg Roedel  * @n: the number we're accessing
199204b885eSJoerg Roedel  */
200204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
201204b885eSJoerg Roedel 
2021da177e4SLinus Torvalds struct completion;
203df2e71fbS[email protected] struct pt_regs;
204df2e71fbS[email protected] struct user;
2051da177e4SLinus Torvalds 
206070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
207070cb065SUwe Kleine-König extern int _cond_resched(void);
208070cb065SUwe Kleine-König # define might_resched() _cond_resched()
209070cb065SUwe Kleine-König #else
210070cb065SUwe Kleine-König # define might_resched() do { } while (0)
211070cb065SUwe Kleine-König #endif
212070cb065SUwe Kleine-König 
213d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
2143427445aSPeter Zijlstra   void ___might_sleep(const char *file, int line, int preempt_offset);
215d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
2161da177e4SLinus Torvalds /**
2171da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
2181da177e4SLinus Torvalds  *
2191da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
2201da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
2211da177e4SLinus Torvalds  *
2221da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
223e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
2241da177e4SLinus Torvalds  * supposed to.
2251da177e4SLinus Torvalds  */
226f8cbd99bSIngo Molnar # define might_sleep() \
227e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
22800845eb9SLinus Torvalds # define sched_annotate_sleep()	(current->task_state_change = 0)
229f8cbd99bSIngo Molnar #else
2303427445aSPeter Zijlstra   static inline void ___might_sleep(const char *file, int line,
2313427445aSPeter Zijlstra 				   int preempt_offset) { }
232d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
233d894837fSSimon Kagstrom 				   int preempt_offset) { }
234f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
2351029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0)
236f8cbd99bSIngo Molnar #endif
237f8cbd99bSIngo Molnar 
238368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
239f8cbd99bSIngo Molnar 
240c8299cb6SMichal Nazarewicz /**
241c8299cb6SMichal Nazarewicz  * abs - return absolute value of an argument
2428f57e4d9SMichal Nazarewicz  * @x: the value.  If it is unsigned type, it is converted to signed type first.
2438f57e4d9SMichal Nazarewicz  *     char is treated as if it was signed (regardless of whether it really is)
2448f57e4d9SMichal Nazarewicz  *     but the macro's return type is preserved as char.
245c8299cb6SMichal Nazarewicz  *
2468f57e4d9SMichal Nazarewicz  * Return: an absolute value of x.
24771a90484SAndrew Morton  */
2488f57e4d9SMichal Nazarewicz #define abs(x)	__abs_choose_expr(x, long long,				\
2498f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, long,				\
2508f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, int,				\
2518f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, short,				\
2528f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, char,				\
2538f57e4d9SMichal Nazarewicz 		__builtin_choose_expr(					\
2548f57e4d9SMichal Nazarewicz 			__builtin_types_compatible_p(typeof(x), char),	\
2558f57e4d9SMichal Nazarewicz 			(char)({ signed char __x = (x); __x<0?-__x:__x; }), \
2568f57e4d9SMichal Nazarewicz 			((void)0)))))))
2578f57e4d9SMichal Nazarewicz 
2588f57e4d9SMichal Nazarewicz #define __abs_choose_expr(x, type, other) __builtin_choose_expr(	\
2598f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
2608f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x), unsigned type),		\
2618f57e4d9SMichal Nazarewicz 	({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
2621da177e4SLinus Torvalds 
26389770b0aSDaniel Borkmann /**
26489770b0aSDaniel Borkmann  * reciprocal_scale - "scale" a value into range [0, ep_ro)
26589770b0aSDaniel Borkmann  * @val: value
26689770b0aSDaniel Borkmann  * @ep_ro: right open interval endpoint
26789770b0aSDaniel Borkmann  *
26889770b0aSDaniel Borkmann  * Perform a "reciprocal multiplication" in order to "scale" a value into
269e8c97af0SRandy Dunlap  * range [0, @ep_ro), where the upper interval endpoint is right-open.
27089770b0aSDaniel Borkmann  * This is useful, e.g. for accessing a index of an array containing
271e8c97af0SRandy Dunlap  * @ep_ro elements, for example. Think of it as sort of modulus, only that
27289770b0aSDaniel Borkmann  * the result isn't that of modulo. ;) Note that if initial input is a
27389770b0aSDaniel Borkmann  * small value, then result will return 0.
27489770b0aSDaniel Borkmann  *
275e8c97af0SRandy Dunlap  * Return: a result based on @val in interval [0, @ep_ro).
27689770b0aSDaniel Borkmann  */
27789770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
27889770b0aSDaniel Borkmann {
27989770b0aSDaniel Borkmann 	return (u32)(((u64) val * ep_ro) >> 32);
28089770b0aSDaniel Borkmann }
28189770b0aSDaniel Borkmann 
282386e7906SAxel Lin #if defined(CONFIG_MMU) && \
283386e7906SAxel Lin 	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
2849ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__)
2859ec23531SDavid Hildenbrand void __might_fault(const char *file, int line);
2863ee1afa3SNick Piggin #else
287662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { }
2883ee1afa3SNick Piggin #endif
2893ee1afa3SNick Piggin 
290e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
291c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
2929402c95fSJoe Perches __printf(1, 2)
2939af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold;
294ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg);
295dd287796SAndrew Morton extern void oops_enter(void);
296dd287796SAndrew Morton extern void oops_exit(void);
297863a6049SAnton Blanchard void print_oops_end_marker(void);
298dd287796SAndrew Morton extern int oops_may_print(void);
2999af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn;
3009af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn;
30133ee3b2eSAlexey Dobriyan 
3027a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT
3037a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err);
3047a46ec0eSKees Cook #else
3057a46ec0eSKees Cook static inline void refcount_error_report(struct pt_regs *regs, const char *err)
3067a46ec0eSKees Cook { }
3077a46ec0eSKees Cook #endif
3087a46ec0eSKees Cook 
30933ee3b2eSAlexey Dobriyan /* Internal, do not use. */
31033ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
31133ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
31233ee3b2eSAlexey Dobriyan 
31333ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
31433ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
3154c925d60SEldad Zack 
3164c925d60SEldad Zack /**
3174c925d60SEldad Zack  * kstrtoul - convert a string to an unsigned long
3184c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3194c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3204c925d60SEldad Zack  *  may also be a plus sign, but not a minus sign.
3214c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3224c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3234c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3244c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3254c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3264c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3274c925d60SEldad Zack  *
3284c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3294c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3304c925d60SEldad Zack  * be checked.
3314c925d60SEldad Zack */
33233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
33333ee3b2eSAlexey Dobriyan {
33433ee3b2eSAlexey Dobriyan 	/*
33533ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
33633ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
33733ee3b2eSAlexey Dobriyan 	 */
33833ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
33933ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
34033ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
34133ee3b2eSAlexey Dobriyan 	else
34233ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
34333ee3b2eSAlexey Dobriyan }
34433ee3b2eSAlexey Dobriyan 
3454c925d60SEldad Zack /**
3464c925d60SEldad Zack  * kstrtol - convert a string to a long
3474c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3484c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3494c925d60SEldad Zack  *  may also be a plus sign or a minus sign.
3504c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3514c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3524c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3534c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3544c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3554c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3564c925d60SEldad Zack  *
3574c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3584c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3594c925d60SEldad Zack  * be checked.
3604c925d60SEldad Zack  */
36133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
36233ee3b2eSAlexey Dobriyan {
36333ee3b2eSAlexey Dobriyan 	/*
36433ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
36533ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
36633ee3b2eSAlexey Dobriyan 	 */
36733ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
36833ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
36933ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
37033ee3b2eSAlexey Dobriyan 	else
37133ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
37233ee3b2eSAlexey Dobriyan }
37333ee3b2eSAlexey Dobriyan 
37433ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
37533ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
37633ee3b2eSAlexey Dobriyan 
37733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
37833ee3b2eSAlexey Dobriyan {
37933ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
38033ee3b2eSAlexey Dobriyan }
38133ee3b2eSAlexey Dobriyan 
38233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
38333ee3b2eSAlexey Dobriyan {
38433ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
38533ee3b2eSAlexey Dobriyan }
38633ee3b2eSAlexey Dobriyan 
38733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
38833ee3b2eSAlexey Dobriyan {
38933ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
39033ee3b2eSAlexey Dobriyan }
39133ee3b2eSAlexey Dobriyan 
39233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
39333ee3b2eSAlexey Dobriyan {
39433ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
39533ee3b2eSAlexey Dobriyan }
39633ee3b2eSAlexey Dobriyan 
39733ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
39833ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
39933ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
40033ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
401ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res);
40233ee3b2eSAlexey Dobriyan 
403c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
404c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
405c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
406c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
407c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
408c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
409c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
410c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
411c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
412c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
413ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
414c196e32aSAlexey Dobriyan 
415c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
416c196e32aSAlexey Dobriyan {
417c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
418c196e32aSAlexey Dobriyan }
419c196e32aSAlexey Dobriyan 
420c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
421c196e32aSAlexey Dobriyan {
422c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
423c196e32aSAlexey Dobriyan }
424c196e32aSAlexey Dobriyan 
425c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
426c196e32aSAlexey Dobriyan {
427c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
428c196e32aSAlexey Dobriyan }
429c196e32aSAlexey Dobriyan 
430c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
431c196e32aSAlexey Dobriyan {
432c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
433c196e32aSAlexey Dobriyan }
434c196e32aSAlexey Dobriyan 
43567d0a075SJoe Perches /* Obsolete, do not use.  Use kstrto<foo> instead */
43667d0a075SJoe Perches 
4371da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
4381da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
4391da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
4401da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
44133ee3b2eSAlexey Dobriyan 
4421ac101a5SKAMEZAWA Hiroyuki extern int num_to_str(char *buf, int size, unsigned long long num);
4431ac101a5SKAMEZAWA Hiroyuki 
44467d0a075SJoe Perches /* lib/printf utilities */
44567d0a075SJoe Perches 
446b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
447b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
448b9075fa9SJoe Perches extern __printf(3, 4)
449b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
450b9075fa9SJoe Perches extern __printf(3, 0)
451b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
452b9075fa9SJoe Perches extern __printf(3, 4)
453b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
454b9075fa9SJoe Perches extern __printf(3, 0)
455b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
45648a27055SRasmus Villemoes extern __printf(2, 3) __malloc
457b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
45848a27055SRasmus Villemoes extern __printf(2, 0) __malloc
4598db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
4600a9df786SRasmus Villemoes extern __printf(2, 0)
4610a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
4621da177e4SLinus Torvalds 
4636061d949SJoe Perches extern __scanf(2, 3)
4646061d949SJoe Perches int sscanf(const char *, const char *, ...);
4656061d949SJoe Perches extern __scanf(2, 0)
4666061d949SJoe Perches int vsscanf(const char *, const char *, va_list);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
4691da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
470d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
4716ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option);
472f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val);
4731da177e4SLinus Torvalds 
4745e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
4759fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr);
476cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
4771da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
4781da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
479ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
480ab7476cfSArjan van de Ven 
4811da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
4841da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
485aa727107SAdrian Bunk extern int panic_timeout;
4861da177e4SLinus Torvalds extern int panic_on_oops;
4878da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
4885211a242SKurt Garloff extern int panic_on_io_nmi;
4899e3961a0SPrarit Bhargava extern int panic_on_warn;
490088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall;
49155af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
4925375b708SHATAYAMA Daisuke 
4935375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers;
4945375b708SHATAYAMA Daisuke 
4955800dc3cSJason Baron /*
4961717f209SHidehiro Kawai  * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
4971717f209SHidehiro Kawai  * holds a CPU number which is executing panic() currently. A value of
4981717f209SHidehiro Kawai  * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
4991717f209SHidehiro Kawai  */
5001717f209SHidehiro Kawai extern atomic_t panic_cpu;
5011717f209SHidehiro Kawai #define PANIC_CPU_INVALID	-1
5021717f209SHidehiro Kawai 
5031717f209SHidehiro Kawai /*
5045800dc3cSJason Baron  * Only to be used by arch init code. If the user over-wrote the default
5055800dc3cSJason Baron  * CONFIG_PANIC_TIMEOUT, honor it.
5065800dc3cSJason Baron  */
5075800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
5085800dc3cSJason Baron {
5095800dc3cSJason Baron 	if (panic_timeout == arch_default_timeout)
5105800dc3cSJason Baron 		panic_timeout = timeout;
5115800dc3cSJason Baron }
5121da177e4SLinus Torvalds extern const char *print_tainted(void);
513373d4d09SRusty Russell enum lockdep_ok {
514373d4d09SRusty Russell 	LOCKDEP_STILL_OK,
515373d4d09SRusty Russell 	LOCKDEP_NOW_UNRELIABLE
516373d4d09SRusty Russell };
517373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok);
51825ddbb18SAndi Kleen extern int test_taint(unsigned flag);
51925ddbb18SAndi Kleen extern unsigned long get_taint(void);
520b920de1bSDavid Howells extern int root_mountflags;
5211da177e4SLinus Torvalds 
5222ce802f6STejun Heo extern bool early_boot_irqs_disabled;
5232ce802f6STejun Heo 
52469a78ff2SThomas Gleixner /*
52569a78ff2SThomas Gleixner  * Values used for system_state. Ordering of the states must not be changed
52669a78ff2SThomas Gleixner  * as code checks for <, <=, >, >= STATE.
52769a78ff2SThomas Gleixner  */
5281da177e4SLinus Torvalds extern enum system_states {
5291da177e4SLinus Torvalds 	SYSTEM_BOOTING,
53069a78ff2SThomas Gleixner 	SYSTEM_SCHEDULING,
5311da177e4SLinus Torvalds 	SYSTEM_RUNNING,
5321da177e4SLinus Torvalds 	SYSTEM_HALT,
5331da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
5341da177e4SLinus Torvalds 	SYSTEM_RESTART,
5351da177e4SLinus Torvalds } system_state;
5361da177e4SLinus Torvalds 
53725ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
53825ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
5398c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC		2
54025ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
54125ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
54225ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
54325ddbb18SAndi Kleen #define TAINT_USER			6
54425ddbb18SAndi Kleen #define TAINT_DIE			7
54525ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
54625ddbb18SAndi Kleen #define TAINT_WARN			9
54726e9a397SLinus Torvalds #define TAINT_CRAP			10
54892946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
5492449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
55066cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE		13
55169361eefSJosh Hunt #define TAINT_SOFTLOCKUP		14
552c5f45465SSeth Jennings #define TAINT_LIVEPATCH			15
5534efb442cSBorislav Petkov #define TAINT_AUX			16
5544efb442cSBorislav Petkov #define TAINT_FLAGS_COUNT		17
5557fd8329bSPetr Mladek 
5567fd8329bSPetr Mladek struct taint_flag {
5575eb7c0d0SLarry Finger 	char c_true;	/* character printed when tainted */
5585eb7c0d0SLarry Finger 	char c_false;	/* character printed when not tainted */
5597fd8329bSPetr Mladek 	bool module;	/* also show as a per-module taint flag */
5607fd8329bSPetr Mladek };
5617fd8329bSPetr Mladek 
5627fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
5631da177e4SLinus Torvalds 
5643fc95772SHarvey Harrison extern const char hex_asc[];
5653fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
5663fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
5673fc95772SHarvey Harrison 
56855036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
5693fc95772SHarvey Harrison {
5703fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
5713fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
5723fc95772SHarvey Harrison 	return buf;
5733fc95772SHarvey Harrison }
57499eaf3c4SRandy Dunlap 
575c26d436cSAndre Naujoks extern const char hex_asc_upper[];
576c26d436cSAndre Naujoks #define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
577c26d436cSAndre Naujoks #define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]
578c26d436cSAndre Naujoks 
579c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte)
580c26d436cSAndre Naujoks {
581c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_hi(byte);
582c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_lo(byte);
583c26d436cSAndre Naujoks 	return buf;
584c26d436cSAndre Naujoks }
585c26d436cSAndre Naujoks 
58690378889SAndy Shevchenko extern int hex_to_bin(char ch);
587b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
58853d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count);
58990378889SAndy Shevchenko 
590a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac);
5914cd5773aSAndy Shevchenko 
5928a64f336SJoe Perches /*
593526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
5942002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
5952002c258SSteven Rostedt  *
5962002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
5972002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
598156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
5992002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
6002002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
6012002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
6022002c258SSteven Rostedt  * to continue tracing.
6032002c258SSteven Rostedt  *
6042002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
6052002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
6062002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
6072002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
6082002c258SSteven Rostedt  *
6092002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
610526211bcSIngo Molnar  */
611cecbca96SFrederic Weisbecker 
612cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
613cecbca96SFrederic Weisbecker 	DUMP_NONE,
614cecbca96SFrederic Weisbecker 	DUMP_ALL,
615cecbca96SFrederic Weisbecker 	DUMP_ORIG,
616cecbca96SFrederic Weisbecker };
617cecbca96SFrederic Weisbecker 
618526211bcSIngo Molnar #ifdef CONFIG_TRACING
61993d68e52SSteven Rostedt void tracing_on(void);
62093d68e52SSteven Rostedt void tracing_off(void);
62193d68e52SSteven Rostedt int tracing_is_on(void);
622ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
623ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
62493d68e52SSteven Rostedt 
625526211bcSIngo Molnar extern void tracing_start(void);
626526211bcSIngo Molnar extern void tracing_stop(void);
627526211bcSIngo Molnar 
628b9075fa9SJoe Perches static inline __printf(1, 2)
629b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
630769b0441SFrederic Weisbecker {
631769b0441SFrederic Weisbecker }
632769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
633769b0441SFrederic Weisbecker do {									\
634769b0441SFrederic Weisbecker 	if (0)								\
635769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
636769b0441SFrederic Weisbecker } while (0)
637769b0441SFrederic Weisbecker 
638526211bcSIngo Molnar /**
639526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
640526211bcSIngo Molnar  * @fmt: the printf format for printing
641526211bcSIngo Molnar  *
642e8c97af0SRandy Dunlap  * Note: __trace_printk is an internal function for trace_printk() and
643e8c97af0SRandy Dunlap  *       the @ip is passed in via the trace_printk() macro.
644526211bcSIngo Molnar  *
645526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
646526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
647526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
648526211bcSIngo Molnar  * where problems are occurring.
649526211bcSIngo Molnar  *
650526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
651526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
65209ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
653e8c97af0SRandy Dunlap  * allocated when trace_printk() is used.)
6549d3c752cSSteven Rostedt (Red Hat)  *
6559d3c752cSSteven Rostedt (Red Hat)  * A little optization trick is done here. If there's only one
6569d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
6579d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
6589d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
6599d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
6609d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
6619d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
6629d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
6639d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
6649d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
6659d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
666526211bcSIngo Molnar  */
667769b0441SFrederic Weisbecker 
6689d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
6699d3c752cSSteven Rostedt (Red Hat) do {							\
6709d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
6719d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
6729d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
6739d3c752cSSteven Rostedt (Red Hat) 	else						\
6749d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
6759d3c752cSSteven Rostedt (Red Hat) } while (0)
6769d3c752cSSteven Rostedt (Red Hat) 
6779d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
678769b0441SFrederic Weisbecker do {									\
6793debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
68048ead020SFrederic Weisbecker 		__attribute__((section("__trace_printk_fmt"))) =	\
68148ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
68248ead020SFrederic Weisbecker 									\
68307d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
68407d777feSSteven Rostedt 									\
68507d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
68648ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
68707d777feSSteven Rostedt 	else								\
68848ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
689769b0441SFrederic Weisbecker } while (0)
690769b0441SFrederic Weisbecker 
691b9075fa9SJoe Perches extern __printf(2, 3)
692b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
69348ead020SFrederic Weisbecker 
694b9075fa9SJoe Perches extern __printf(2, 3)
695b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
696769b0441SFrederic Weisbecker 
69709ae7234SSteven Rostedt (Red Hat) /**
69809ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
69909ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
70009ae7234SSteven Rostedt (Red Hat)  *
70109ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
70209ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
70309ae7234SSteven Rostedt (Red Hat)  *
70409ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
705e8c97af0SRandy Dunlap  * paths that a developer wants the least amount of "Heisenbug" effects,
70609ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
70709ae7234SSteven Rostedt (Red Hat)  *
70809ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
70909ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
71009ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
71109ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
71209ae7234SSteven Rostedt (Red Hat)  *
71309ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
71409ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
71509ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
716e8c97af0SRandy Dunlap  * allocated when trace_puts() is used.)
71709ae7234SSteven Rostedt (Red Hat)  *
71809ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
71909ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
72009ae7234SSteven Rostedt (Red Hat)  */
72109ae7234SSteven Rostedt (Red Hat) 
72209ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
7233debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
72409ae7234SSteven Rostedt (Red Hat) 		__attribute__((section("__trace_printk_fmt"))) =	\
72509ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
72609ae7234SSteven Rostedt (Red Hat) 									\
72709ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
72809ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
72909ae7234SSteven Rostedt (Red Hat) 	else								\
73009ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
73109ae7234SSteven Rostedt (Red Hat) })
732bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str);
733bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size);
73409ae7234SSteven Rostedt (Red Hat) 
735c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
73603889384SSteven Rostedt 
73748ead020SFrederic Weisbecker /*
73848ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
73948ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
74048ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
74148ead020SFrederic Weisbecker  */
742769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
743769b0441SFrederic Weisbecker do {									\
74448ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
7453debb0a9SSteven Rostedt (Red Hat) 		static const char *trace_printk_fmt __used		\
74648ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
74748ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
7487bffc23eSIngo Molnar 									\
74948ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
75048ead020SFrederic Weisbecker 	} else								\
75148ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
752769b0441SFrederic Weisbecker } while (0)
753769b0441SFrederic Weisbecker 
7548db14860SNicolas Iooss extern __printf(2, 0) int
75548ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
75648ead020SFrederic Weisbecker 
7578db14860SNicolas Iooss extern __printf(2, 0) int
758526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
759769b0441SFrederic Weisbecker 
760cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
761526211bcSIngo Molnar #else
762526211bcSIngo Molnar static inline void tracing_start(void) { }
763526211bcSIngo Molnar static inline void tracing_stop(void) { }
764e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { }
76593d68e52SSteven Rostedt 
76693d68e52SSteven Rostedt static inline void tracing_on(void) { }
76793d68e52SSteven Rostedt static inline void tracing_off(void) { }
76893d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
769ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
770ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
77193d68e52SSteven Rostedt 
77260efc15aSMichal Hocko static inline __printf(1, 2)
77360efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
774526211bcSIngo Molnar {
775526211bcSIngo Molnar 	return 0;
776526211bcSIngo Molnar }
7778db14860SNicolas Iooss static __printf(1, 0) inline int
778526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
779526211bcSIngo Molnar {
780526211bcSIngo Molnar 	return 0;
781526211bcSIngo Molnar }
782cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
783769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
784526211bcSIngo Molnar 
785526211bcSIngo Molnar /*
786bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
7871da177e4SLinus Torvalds  * strict type-checking.. See the
7881da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
7891da177e4SLinus Torvalds  */
790589a9785SJohannes Berg #define __min(t1, t2, min1, min2, x, y) ({		\
791589a9785SJohannes Berg 	t1 min1 = (x);					\
792589a9785SJohannes Berg 	t2 min2 = (y);					\
793589a9785SJohannes Berg 	(void) (&min1 == &min2);			\
794589a9785SJohannes Berg 	min1 < min2 ? min1 : min2; })
795e8c97af0SRandy Dunlap 
796e8c97af0SRandy Dunlap /**
797e8c97af0SRandy Dunlap  * min - return minimum of two values of the same or compatible types
798e8c97af0SRandy Dunlap  * @x: first value
799e8c97af0SRandy Dunlap  * @y: second value
800e8c97af0SRandy Dunlap  */
801589a9785SJohannes Berg #define min(x, y)					\
802589a9785SJohannes Berg 	__min(typeof(x), typeof(y),			\
803589a9785SJohannes Berg 	      __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),	\
804589a9785SJohannes Berg 	      x, y)
8051da177e4SLinus Torvalds 
806589a9785SJohannes Berg #define __max(t1, t2, max1, max2, x, y) ({		\
807589a9785SJohannes Berg 	t1 max1 = (x);					\
808589a9785SJohannes Berg 	t2 max2 = (y);					\
809589a9785SJohannes Berg 	(void) (&max1 == &max2);			\
810589a9785SJohannes Berg 	max1 > max2 ? max1 : max2; })
811e8c97af0SRandy Dunlap 
812e8c97af0SRandy Dunlap /**
813e8c97af0SRandy Dunlap  * max - return maximum of two values of the same or compatible types
814e8c97af0SRandy Dunlap  * @x: first value
815e8c97af0SRandy Dunlap  * @y: second value
816e8c97af0SRandy Dunlap  */
817589a9785SJohannes Berg #define max(x, y)					\
818589a9785SJohannes Berg 	__max(typeof(x), typeof(y),			\
819589a9785SJohannes Berg 	      __UNIQUE_ID(max1_), __UNIQUE_ID(max2_),	\
820589a9785SJohannes Berg 	      x, y)
821bdf4bbaaSHarvey Harrison 
822e8c97af0SRandy Dunlap /**
823e8c97af0SRandy Dunlap  * min3 - return minimum of three values
824e8c97af0SRandy Dunlap  * @x: first value
825e8c97af0SRandy Dunlap  * @y: second value
826e8c97af0SRandy Dunlap  * @z: third value
827e8c97af0SRandy Dunlap  */
8282e1d06e1SMichal Nazarewicz #define min3(x, y, z) min((typeof(x))min(x, y), z)
829e8c97af0SRandy Dunlap 
830e8c97af0SRandy Dunlap /**
831e8c97af0SRandy Dunlap  * max3 - return maximum of three values
832e8c97af0SRandy Dunlap  * @x: first value
833e8c97af0SRandy Dunlap  * @y: second value
834e8c97af0SRandy Dunlap  * @z: third value
835e8c97af0SRandy Dunlap  */
8362e1d06e1SMichal Nazarewicz #define max3(x, y, z) max((typeof(x))max(x, y), z)
837f27c85c5SHagen Paul Pfeifer 
838bdf4bbaaSHarvey Harrison /**
839c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
840c8bf1336SMartin K. Petersen  * @x: value1
841c8bf1336SMartin K. Petersen  * @y: value2
842c8bf1336SMartin K. Petersen  */
843c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
844c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
845c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
846c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
847c8bf1336SMartin K. Petersen 
848c8bf1336SMartin K. Petersen /**
849bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
850bdf4bbaaSHarvey Harrison  * @val: current value
8512e1d06e1SMichal Nazarewicz  * @lo: lowest allowable value
8522e1d06e1SMichal Nazarewicz  * @hi: highest allowable value
853bdf4bbaaSHarvey Harrison  *
854e8c97af0SRandy Dunlap  * This macro does strict typechecking of @lo/@hi to make sure they are of the
855e8c97af0SRandy Dunlap  * same type as @val.  See the unnecessary pointer comparisons.
856bdf4bbaaSHarvey Harrison  */
8572e1d06e1SMichal Nazarewicz #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds /*
8601da177e4SLinus Torvalds  * ..and if you can't take the strict
8611da177e4SLinus Torvalds  * types, you can specify one yourself.
8621da177e4SLinus Torvalds  *
863bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
8641da177e4SLinus Torvalds  */
865e8c97af0SRandy Dunlap 
866e8c97af0SRandy Dunlap /**
867e8c97af0SRandy Dunlap  * min_t - return minimum of two values, using the specified type
868e8c97af0SRandy Dunlap  * @type: data type to use
869e8c97af0SRandy Dunlap  * @x: first value
870e8c97af0SRandy Dunlap  * @y: second value
871e8c97af0SRandy Dunlap  */
872589a9785SJohannes Berg #define min_t(type, x, y)				\
873589a9785SJohannes Berg 	__min(type, type,				\
874589a9785SJohannes Berg 	      __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),	\
875589a9785SJohannes Berg 	      x, y)
8761da177e4SLinus Torvalds 
877e8c97af0SRandy Dunlap /**
878e8c97af0SRandy Dunlap  * max_t - return maximum of two values, using the specified type
879e8c97af0SRandy Dunlap  * @type: data type to use
880e8c97af0SRandy Dunlap  * @x: first value
881e8c97af0SRandy Dunlap  * @y: second value
882e8c97af0SRandy Dunlap  */
883589a9785SJohannes Berg #define max_t(type, x, y)				\
884589a9785SJohannes Berg 	__max(type, type,				\
885589a9785SJohannes Berg 	      __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),	\
886589a9785SJohannes Berg 	      x, y)
887bdf4bbaaSHarvey Harrison 
888bdf4bbaaSHarvey Harrison /**
889bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
890bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
891bdf4bbaaSHarvey Harrison  * @val: current value
892c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
893c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
894bdf4bbaaSHarvey Harrison  *
895bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
896e8c97af0SRandy Dunlap  * @type to make all the comparisons.
897bdf4bbaaSHarvey Harrison  */
898c185b07fSMichal Nazarewicz #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
899bdf4bbaaSHarvey Harrison 
900bdf4bbaaSHarvey Harrison /**
901bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
902bdf4bbaaSHarvey Harrison  * @val: current value
903c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
904c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
905bdf4bbaaSHarvey Harrison  *
906bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
907e8c97af0SRandy Dunlap  * type the input argument @val is.  This is useful when @val is an unsigned
908e8c97af0SRandy Dunlap  * type and @lo and @hi are literals that will otherwise be assigned a signed
909bdf4bbaaSHarvey Harrison  * integer type.
910bdf4bbaaSHarvey Harrison  */
911c185b07fSMichal Nazarewicz #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
9121da177e4SLinus Torvalds 
91391f68b73SWu Fengguang 
914e8c97af0SRandy Dunlap /**
915e8c97af0SRandy Dunlap  * swap - swap values of @a and @b
916e8c97af0SRandy Dunlap  * @a: first value
917e8c97af0SRandy Dunlap  * @b: second value
91891f68b73SWu Fengguang  */
919ac7b9004SPeter Zijlstra #define swap(a, b) \
920ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
92191f68b73SWu Fengguang 
922*cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */
923*cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
924*cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
925*cf14f27fSAlexei Starovoitov 
926*cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b
927*cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b)
928*cf14f27fSAlexei Starovoitov 
9291da177e4SLinus Torvalds /**
9301da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
9311da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
9321da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
9331da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
9341da177e4SLinus Torvalds  *
9351da177e4SLinus Torvalds  */
9361da177e4SLinus Torvalds #define container_of(ptr, type, member) ({				\
937c7acec71SIan Abbott 	void *__mptr = (void *)(ptr);					\
938c7acec71SIan Abbott 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
939c7acec71SIan Abbott 			 !__same_type(*(ptr), void),			\
940c7acec71SIan Abbott 			 "pointer type mismatch in container_of()");	\
941c7acec71SIan Abbott 	((type *)(__mptr - offsetof(type, member))); })
9421da177e4SLinus Torvalds 
943b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
944b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
945b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
946b9d4f426SArnaud Lacombe #endif
9479d00f92fSWANG Cong 
94858f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
94958f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms)						\
95058f86cc8SRusty Russell 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
95158f86cc8SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
95228b8d0c8SGobinda Charan Maji 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
95328b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
95428b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
95528b8d0c8SGobinda Charan Maji 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
95628b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
95728b8d0c8SGobinda Charan Maji 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
95837549e94SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
95958f86cc8SRusty Russell 	 (perms))
9601da177e4SLinus Torvalds #endif
961