xref: /linux-6.15/include/linux/kernel.h (revision 5eb7c0d0)
11da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H
21da177e4SLinus Torvalds #define _LINUX_KERNEL_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <stdarg.h>
61da177e4SLinus Torvalds #include <linux/linkage.h>
71da177e4SLinus Torvalds #include <linux/stddef.h>
81da177e4SLinus Torvalds #include <linux/types.h>
91da177e4SLinus Torvalds #include <linux/compiler.h>
101da177e4SLinus Torvalds #include <linux/bitops.h>
11f0d1b0b3SDavid Howells #include <linux/log2.h>
12e0deaff4SAndrew Morton #include <linux/typecheck.h>
13968ab183SLinus Torvalds #include <linux/printk.h>
141da177e4SLinus Torvalds #include <asm/byteorder.h>
15607ca46eSDavid Howells #include <uapi/linux/kernel.h>
161da177e4SLinus Torvalds 
174be929beSAlexey Dobriyan #define USHRT_MAX	((u16)(~0U))
184be929beSAlexey Dobriyan #define SHRT_MAX	((s16)(USHRT_MAX>>1))
194be929beSAlexey Dobriyan #define SHRT_MIN	((s16)(-SHRT_MAX - 1))
201da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
211da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
221da177e4SLinus Torvalds #define UINT_MAX	(~0U)
231da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
241da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
251da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
26111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
27111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
28111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
29a3860c1cSXi Wang #define SIZE_MAX	(~(size_t)0)
301da177e4SLinus Torvalds 
3189a07141SAlex Elder #define U8_MAX		((u8)~0U)
3289a07141SAlex Elder #define S8_MAX		((s8)(U8_MAX>>1))
3389a07141SAlex Elder #define S8_MIN		((s8)(-S8_MAX - 1))
3489a07141SAlex Elder #define U16_MAX		((u16)~0U)
3589a07141SAlex Elder #define S16_MAX		((s16)(U16_MAX>>1))
3689a07141SAlex Elder #define S16_MIN		((s16)(-S16_MAX - 1))
3789a07141SAlex Elder #define U32_MAX		((u32)~0U)
3889a07141SAlex Elder #define S32_MAX		((s32)(U32_MAX>>1))
3989a07141SAlex Elder #define S32_MIN		((s32)(-S32_MAX - 1))
4089a07141SAlex Elder #define U64_MAX		((u64)~0ULL)
4189a07141SAlex Elder #define S64_MAX		((s64)(U64_MAX>>1))
4289a07141SAlex Elder #define S64_MIN		((s64)(-S64_MAX - 1))
4389a07141SAlex Elder 
441da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
451da177e4SLinus Torvalds 
4644696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
4744696908SDavid S. Miller 
483ca45a46Szijun_hu /* @a is a power of 2 value */
49a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
509f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
51a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
52f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
532ea58144SLinus Torvalds 
54d3849953SChristoph Hellwig /* generic data direction definitions */
55d3849953SChristoph Hellwig #define READ			0
56d3849953SChristoph Hellwig #define WRITE			1
57d3849953SChristoph Hellwig 
58c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
59c5e631cfSRusty Russell 
603ed605bcSGustavo Padovan #define u64_to_user_ptr(x) (		\
613ed605bcSGustavo Padovan {					\
623ed605bcSGustavo Padovan 	typecheck(u64, x);		\
633ed605bcSGustavo Padovan 	(void __user *)(uintptr_t)x;	\
643ed605bcSGustavo Padovan }					\
653ed605bcSGustavo Padovan )
663ed605bcSGustavo Padovan 
679b3be9f9SYinghai Lu /*
689b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
699b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
709b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
719b3be9f9SYinghai Lu  * arguments just once each.
729b3be9f9SYinghai Lu  */
739b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
749b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
759b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
769b3be9f9SYinghai Lu 
774552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
78b5d3755aSNicolas Dichtel #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
7936a26c69SNicholas Bellinger #define DIV_ROUND_UP_ULL(ll,d) \
8036a26c69SNicholas Bellinger 	({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; })
8136a26c69SNicholas Bellinger 
8236a26c69SNicholas Bellinger #if BITS_PER_LONG == 32
8336a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
8436a26c69SNicholas Bellinger #else
8536a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
8636a26c69SNicholas Bellinger #endif
87074e61ecSJames Morris 
88074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
89b28efd54SEric Paris #define roundup(x, y) (					\
90b28efd54SEric Paris {							\
916070bf35STetsuo Handa 	const typeof(y) __y = y;			\
92b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
93b28efd54SEric Paris }							\
94b28efd54SEric Paris )
95686a0f3dSEric Paris #define rounddown(x, y) (				\
96686a0f3dSEric Paris {							\
97686a0f3dSEric Paris 	typeof(x) __x = (x);				\
98686a0f3dSEric Paris 	__x - (__x % (y));				\
99686a0f3dSEric Paris }							\
100686a0f3dSEric Paris )
101b6d86d3dSGuenter Roeck 
102b6d86d3dSGuenter Roeck /*
103b6d86d3dSGuenter Roeck  * Divide positive or negative dividend by positive divisor and round
104c4e18497SGuenter Roeck  * to closest integer. Result is undefined for negative divisors and
105c4e18497SGuenter Roeck  * for negative dividends if the divisor variable type is unsigned.
106b6d86d3dSGuenter Roeck  */
1079fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
1089fe06081SDarrick J. Wong {							\
109b6d86d3dSGuenter Roeck 	typeof(x) __x = x;				\
110b6d86d3dSGuenter Roeck 	typeof(divisor) __d = divisor;			\
111c4e18497SGuenter Roeck 	(((typeof(x))-1) > 0 ||				\
112c4e18497SGuenter Roeck 	 ((typeof(divisor))-1) > 0 || (__x) > 0) ?	\
113b6d86d3dSGuenter Roeck 		(((__x) + ((__d) / 2)) / (__d)) :	\
114b6d86d3dSGuenter Roeck 		(((__x) - ((__d) / 2)) / (__d));	\
1159fe06081SDarrick J. Wong }							\
1169fe06081SDarrick J. Wong )
117f766093eSJavi Merino /*
118f766093eSJavi Merino  * Same as above but for u64 dividends. divisor must be a 32-bit
119f766093eSJavi Merino  * number.
120f766093eSJavi Merino  */
121f766093eSJavi Merino #define DIV_ROUND_CLOSEST_ULL(x, divisor)(		\
122f766093eSJavi Merino {							\
123f766093eSJavi Merino 	typeof(divisor) __d = divisor;			\
124f766093eSJavi Merino 	unsigned long long _tmp = (x) + (__d) / 2;	\
125f766093eSJavi Merino 	do_div(_tmp, __d);				\
126f766093eSJavi Merino 	_tmp;						\
127f766093eSJavi Merino }							\
128f766093eSJavi Merino )
1291da177e4SLinus Torvalds 
1309993bc63SSalman Qazi /*
1319993bc63SSalman Qazi  * Multiplies an integer by a fraction, while avoiding unnecessary
1329993bc63SSalman Qazi  * overflow or loss of precision.
1339993bc63SSalman Qazi  */
1349993bc63SSalman Qazi #define mult_frac(x, numer, denom)(			\
1359993bc63SSalman Qazi {							\
1369993bc63SSalman Qazi 	typeof(x) quot = (x) / (denom);			\
1379993bc63SSalman Qazi 	typeof(x) rem  = (x) % (denom);			\
1389993bc63SSalman Qazi 	(quot * (numer)) + ((rem * (numer)) / (denom));	\
1399993bc63SSalman Qazi }							\
1409993bc63SSalman Qazi )
1419993bc63SSalman Qazi 
1429993bc63SSalman Qazi 
143ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
144ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
145ca31e146SEduard - Gabriel Munteanu 
14690c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
1472da96acdSJens Axboe # include <asm/div64.h>
1482da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
1492da96acdSJens Axboe #else
1502da96acdSJens Axboe # define sector_div(n, b)( \
1512da96acdSJens Axboe { \
1522da96acdSJens Axboe 	int _res; \
1532da96acdSJens Axboe 	_res = (n) % (b); \
1542da96acdSJens Axboe 	(n) /= (b); \
1552da96acdSJens Axboe 	_res; \
1562da96acdSJens Axboe } \
1572da96acdSJens Axboe )
1582da96acdSJens Axboe #endif
1592da96acdSJens Axboe 
160218e180eSAndrew Morton /**
161218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
162218e180eSAndrew Morton  * @n: the number we're accessing
163218e180eSAndrew Morton  *
164218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
165218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
166218e180eSAndrew Morton  * 32-bits.
167218e180eSAndrew Morton  */
168218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
169218e180eSAndrew Morton 
170204b885eSJoerg Roedel /**
171204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
172204b885eSJoerg Roedel  * @n: the number we're accessing
173204b885eSJoerg Roedel  */
174204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
175204b885eSJoerg Roedel 
1761da177e4SLinus Torvalds struct completion;
177df2e71fbS[email protected] struct pt_regs;
178df2e71fbS[email protected] struct user;
1791da177e4SLinus Torvalds 
180070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
181070cb065SUwe Kleine-König extern int _cond_resched(void);
182070cb065SUwe Kleine-König # define might_resched() _cond_resched()
183070cb065SUwe Kleine-König #else
184070cb065SUwe Kleine-König # define might_resched() do { } while (0)
185070cb065SUwe Kleine-König #endif
186070cb065SUwe Kleine-König 
187d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1883427445aSPeter Zijlstra   void ___might_sleep(const char *file, int line, int preempt_offset);
189d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
1901da177e4SLinus Torvalds /**
1911da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1921da177e4SLinus Torvalds  *
1931da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1941da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
197e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1981da177e4SLinus Torvalds  * supposed to.
1991da177e4SLinus Torvalds  */
200f8cbd99bSIngo Molnar # define might_sleep() \
201e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
20200845eb9SLinus Torvalds # define sched_annotate_sleep()	(current->task_state_change = 0)
203f8cbd99bSIngo Molnar #else
2043427445aSPeter Zijlstra   static inline void ___might_sleep(const char *file, int line,
2053427445aSPeter Zijlstra 				   int preempt_offset) { }
206d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
207d894837fSSimon Kagstrom 				   int preempt_offset) { }
208f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
2091029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0)
210f8cbd99bSIngo Molnar #endif
211f8cbd99bSIngo Molnar 
212368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
213f8cbd99bSIngo Molnar 
214c8299cb6SMichal Nazarewicz /**
215c8299cb6SMichal Nazarewicz  * abs - return absolute value of an argument
2168f57e4d9SMichal Nazarewicz  * @x: the value.  If it is unsigned type, it is converted to signed type first.
2178f57e4d9SMichal Nazarewicz  *     char is treated as if it was signed (regardless of whether it really is)
2188f57e4d9SMichal Nazarewicz  *     but the macro's return type is preserved as char.
219c8299cb6SMichal Nazarewicz  *
2208f57e4d9SMichal Nazarewicz  * Return: an absolute value of x.
22171a90484SAndrew Morton  */
2228f57e4d9SMichal Nazarewicz #define abs(x)	__abs_choose_expr(x, long long,				\
2238f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, long,				\
2248f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, int,				\
2258f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, short,				\
2268f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, char,				\
2278f57e4d9SMichal Nazarewicz 		__builtin_choose_expr(					\
2288f57e4d9SMichal Nazarewicz 			__builtin_types_compatible_p(typeof(x), char),	\
2298f57e4d9SMichal Nazarewicz 			(char)({ signed char __x = (x); __x<0?-__x:__x; }), \
2308f57e4d9SMichal Nazarewicz 			((void)0)))))))
2318f57e4d9SMichal Nazarewicz 
2328f57e4d9SMichal Nazarewicz #define __abs_choose_expr(x, type, other) __builtin_choose_expr(	\
2338f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
2348f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x), unsigned type),		\
2358f57e4d9SMichal Nazarewicz 	({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
2361da177e4SLinus Torvalds 
23789770b0aSDaniel Borkmann /**
23889770b0aSDaniel Borkmann  * reciprocal_scale - "scale" a value into range [0, ep_ro)
23989770b0aSDaniel Borkmann  * @val: value
24089770b0aSDaniel Borkmann  * @ep_ro: right open interval endpoint
24189770b0aSDaniel Borkmann  *
24289770b0aSDaniel Borkmann  * Perform a "reciprocal multiplication" in order to "scale" a value into
24389770b0aSDaniel Borkmann  * range [0, ep_ro), where the upper interval endpoint is right-open.
24489770b0aSDaniel Borkmann  * This is useful, e.g. for accessing a index of an array containing
24589770b0aSDaniel Borkmann  * ep_ro elements, for example. Think of it as sort of modulus, only that
24689770b0aSDaniel Borkmann  * the result isn't that of modulo. ;) Note that if initial input is a
24789770b0aSDaniel Borkmann  * small value, then result will return 0.
24889770b0aSDaniel Borkmann  *
24989770b0aSDaniel Borkmann  * Return: a result based on val in interval [0, ep_ro).
25089770b0aSDaniel Borkmann  */
25189770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
25289770b0aSDaniel Borkmann {
25389770b0aSDaniel Borkmann 	return (u32)(((u64) val * ep_ro) >> 32);
25489770b0aSDaniel Borkmann }
25589770b0aSDaniel Borkmann 
256386e7906SAxel Lin #if defined(CONFIG_MMU) && \
257386e7906SAxel Lin 	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
2589ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__)
2599ec23531SDavid Hildenbrand void __might_fault(const char *file, int line);
2603ee1afa3SNick Piggin #else
261662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { }
2623ee1afa3SNick Piggin #endif
2633ee1afa3SNick Piggin 
264e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
265c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
2669402c95fSJoe Perches __printf(1, 2)
2679af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold;
268ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg);
269dd287796SAndrew Morton extern void oops_enter(void);
270dd287796SAndrew Morton extern void oops_exit(void);
271863a6049SAnton Blanchard void print_oops_end_marker(void);
272dd287796SAndrew Morton extern int oops_may_print(void);
2739af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn;
2749af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn;
27533ee3b2eSAlexey Dobriyan 
27633ee3b2eSAlexey Dobriyan /* Internal, do not use. */
27733ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
27833ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
27933ee3b2eSAlexey Dobriyan 
28033ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
28133ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
2824c925d60SEldad Zack 
2834c925d60SEldad Zack /**
2844c925d60SEldad Zack  * kstrtoul - convert a string to an unsigned long
2854c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
2864c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
2874c925d60SEldad Zack  *  may also be a plus sign, but not a minus sign.
2884c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
2894c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
2904c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
2914c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
2924c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
2934c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
2944c925d60SEldad Zack  *
2954c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
2964c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
2974c925d60SEldad Zack  * be checked.
2984c925d60SEldad Zack */
29933ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
30033ee3b2eSAlexey Dobriyan {
30133ee3b2eSAlexey Dobriyan 	/*
30233ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
30333ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
30433ee3b2eSAlexey Dobriyan 	 */
30533ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
30633ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
30733ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
30833ee3b2eSAlexey Dobriyan 	else
30933ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
31033ee3b2eSAlexey Dobriyan }
31133ee3b2eSAlexey Dobriyan 
3124c925d60SEldad Zack /**
3134c925d60SEldad Zack  * kstrtol - convert a string to a long
3144c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3154c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3164c925d60SEldad Zack  *  may also be a plus sign or a minus sign.
3174c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3184c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3194c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3204c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3214c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3224c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3234c925d60SEldad Zack  *
3244c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3254c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3264c925d60SEldad Zack  * be checked.
3274c925d60SEldad Zack  */
32833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
32933ee3b2eSAlexey Dobriyan {
33033ee3b2eSAlexey Dobriyan 	/*
33133ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
33233ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
33333ee3b2eSAlexey Dobriyan 	 */
33433ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
33533ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
33633ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
33733ee3b2eSAlexey Dobriyan 	else
33833ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
33933ee3b2eSAlexey Dobriyan }
34033ee3b2eSAlexey Dobriyan 
34133ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
34233ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
34333ee3b2eSAlexey Dobriyan 
34433ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
34533ee3b2eSAlexey Dobriyan {
34633ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
34733ee3b2eSAlexey Dobriyan }
34833ee3b2eSAlexey Dobriyan 
34933ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
35033ee3b2eSAlexey Dobriyan {
35133ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
35233ee3b2eSAlexey Dobriyan }
35333ee3b2eSAlexey Dobriyan 
35433ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
35533ee3b2eSAlexey Dobriyan {
35633ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
35733ee3b2eSAlexey Dobriyan }
35833ee3b2eSAlexey Dobriyan 
35933ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
36033ee3b2eSAlexey Dobriyan {
36133ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
36233ee3b2eSAlexey Dobriyan }
36333ee3b2eSAlexey Dobriyan 
36433ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
36533ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
36633ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
36733ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
368ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res);
36933ee3b2eSAlexey Dobriyan 
370c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
371c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
372c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
373c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
374c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
375c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
376c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
377c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
378c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
379c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
380ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
381c196e32aSAlexey Dobriyan 
382c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
383c196e32aSAlexey Dobriyan {
384c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
385c196e32aSAlexey Dobriyan }
386c196e32aSAlexey Dobriyan 
387c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
388c196e32aSAlexey Dobriyan {
389c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
390c196e32aSAlexey Dobriyan }
391c196e32aSAlexey Dobriyan 
392c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
393c196e32aSAlexey Dobriyan {
394c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
395c196e32aSAlexey Dobriyan }
396c196e32aSAlexey Dobriyan 
397c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
398c196e32aSAlexey Dobriyan {
399c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
400c196e32aSAlexey Dobriyan }
401c196e32aSAlexey Dobriyan 
40267d0a075SJoe Perches /* Obsolete, do not use.  Use kstrto<foo> instead */
40367d0a075SJoe Perches 
4041da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
4051da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
4061da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
4071da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
40833ee3b2eSAlexey Dobriyan 
4091ac101a5SKAMEZAWA Hiroyuki extern int num_to_str(char *buf, int size, unsigned long long num);
4101ac101a5SKAMEZAWA Hiroyuki 
41167d0a075SJoe Perches /* lib/printf utilities */
41267d0a075SJoe Perches 
413b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
414b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
415b9075fa9SJoe Perches extern __printf(3, 4)
416b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
417b9075fa9SJoe Perches extern __printf(3, 0)
418b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
419b9075fa9SJoe Perches extern __printf(3, 4)
420b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
421b9075fa9SJoe Perches extern __printf(3, 0)
422b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
42348a27055SRasmus Villemoes extern __printf(2, 3) __malloc
424b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
42548a27055SRasmus Villemoes extern __printf(2, 0) __malloc
4268db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
4270a9df786SRasmus Villemoes extern __printf(2, 0)
4280a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
4291da177e4SLinus Torvalds 
4306061d949SJoe Perches extern __scanf(2, 3)
4316061d949SJoe Perches int sscanf(const char *, const char *, ...);
4326061d949SJoe Perches extern __scanf(2, 0)
4336061d949SJoe Perches int vsscanf(const char *, const char *, va_list);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
4361da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
437d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
4386ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option);
4391da177e4SLinus Torvalds 
4405e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
441cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
4421da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
4431da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
444ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
445ab7476cfSArjan van de Ven 
4461da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
4491da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
450aa727107SAdrian Bunk extern int panic_timeout;
4511da177e4SLinus Torvalds extern int panic_on_oops;
4528da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
4535211a242SKurt Garloff extern int panic_on_io_nmi;
4549e3961a0SPrarit Bhargava extern int panic_on_warn;
455088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall;
45655af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
4575375b708SHATAYAMA Daisuke 
4585375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers;
4595375b708SHATAYAMA Daisuke 
4605800dc3cSJason Baron /*
4611717f209SHidehiro Kawai  * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
4621717f209SHidehiro Kawai  * holds a CPU number which is executing panic() currently. A value of
4631717f209SHidehiro Kawai  * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
4641717f209SHidehiro Kawai  */
4651717f209SHidehiro Kawai extern atomic_t panic_cpu;
4661717f209SHidehiro Kawai #define PANIC_CPU_INVALID	-1
4671717f209SHidehiro Kawai 
4681717f209SHidehiro Kawai /*
4695800dc3cSJason Baron  * Only to be used by arch init code. If the user over-wrote the default
4705800dc3cSJason Baron  * CONFIG_PANIC_TIMEOUT, honor it.
4715800dc3cSJason Baron  */
4725800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
4735800dc3cSJason Baron {
4745800dc3cSJason Baron 	if (panic_timeout == arch_default_timeout)
4755800dc3cSJason Baron 		panic_timeout = timeout;
4765800dc3cSJason Baron }
4771da177e4SLinus Torvalds extern const char *print_tainted(void);
478373d4d09SRusty Russell enum lockdep_ok {
479373d4d09SRusty Russell 	LOCKDEP_STILL_OK,
480373d4d09SRusty Russell 	LOCKDEP_NOW_UNRELIABLE
481373d4d09SRusty Russell };
482373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok);
48325ddbb18SAndi Kleen extern int test_taint(unsigned flag);
48425ddbb18SAndi Kleen extern unsigned long get_taint(void);
485b920de1bSDavid Howells extern int root_mountflags;
4861da177e4SLinus Torvalds 
4872ce802f6STejun Heo extern bool early_boot_irqs_disabled;
4882ce802f6STejun Heo 
4891da177e4SLinus Torvalds /* Values used for system_state */
4901da177e4SLinus Torvalds extern enum system_states {
4911da177e4SLinus Torvalds 	SYSTEM_BOOTING,
4921da177e4SLinus Torvalds 	SYSTEM_RUNNING,
4931da177e4SLinus Torvalds 	SYSTEM_HALT,
4941da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
4951da177e4SLinus Torvalds 	SYSTEM_RESTART,
4961da177e4SLinus Torvalds } system_state;
4971da177e4SLinus Torvalds 
49825ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
49925ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
5008c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC		2
50125ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
50225ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
50325ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
50425ddbb18SAndi Kleen #define TAINT_USER			6
50525ddbb18SAndi Kleen #define TAINT_DIE			7
50625ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
50725ddbb18SAndi Kleen #define TAINT_WARN			9
50826e9a397SLinus Torvalds #define TAINT_CRAP			10
50992946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
5102449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
51166cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE		13
51269361eefSJosh Hunt #define TAINT_SOFTLOCKUP		14
513c5f45465SSeth Jennings #define TAINT_LIVEPATCH			15
5147fd8329bSPetr Mladek #define TAINT_FLAGS_COUNT		16
5157fd8329bSPetr Mladek 
5167fd8329bSPetr Mladek struct taint_flag {
517*5eb7c0d0SLarry Finger 	char c_true;	/* character printed when tainted */
518*5eb7c0d0SLarry Finger 	char c_false;	/* character printed when not tainted */
5197fd8329bSPetr Mladek 	bool module;	/* also show as a per-module taint flag */
5207fd8329bSPetr Mladek };
5217fd8329bSPetr Mladek 
5227fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
5231da177e4SLinus Torvalds 
5243fc95772SHarvey Harrison extern const char hex_asc[];
5253fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
5263fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
5273fc95772SHarvey Harrison 
52855036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
5293fc95772SHarvey Harrison {
5303fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
5313fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
5323fc95772SHarvey Harrison 	return buf;
5333fc95772SHarvey Harrison }
53499eaf3c4SRandy Dunlap 
535c26d436cSAndre Naujoks extern const char hex_asc_upper[];
536c26d436cSAndre Naujoks #define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
537c26d436cSAndre Naujoks #define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]
538c26d436cSAndre Naujoks 
539c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte)
540c26d436cSAndre Naujoks {
541c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_hi(byte);
542c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_lo(byte);
543c26d436cSAndre Naujoks 	return buf;
544c26d436cSAndre Naujoks }
545c26d436cSAndre Naujoks 
54690378889SAndy Shevchenko extern int hex_to_bin(char ch);
547b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
54853d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count);
54990378889SAndy Shevchenko 
550a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac);
5514cd5773aSAndy Shevchenko 
5528a64f336SJoe Perches /*
553526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
5542002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
5552002c258SSteven Rostedt  *
5562002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
5572002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
558156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
5592002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
5602002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
5612002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
5622002c258SSteven Rostedt  * to continue tracing.
5632002c258SSteven Rostedt  *
5642002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
5652002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
5662002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
5672002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
5682002c258SSteven Rostedt  *
5692002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
570526211bcSIngo Molnar  */
571cecbca96SFrederic Weisbecker 
572cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
573cecbca96SFrederic Weisbecker 	DUMP_NONE,
574cecbca96SFrederic Weisbecker 	DUMP_ALL,
575cecbca96SFrederic Weisbecker 	DUMP_ORIG,
576cecbca96SFrederic Weisbecker };
577cecbca96SFrederic Weisbecker 
578526211bcSIngo Molnar #ifdef CONFIG_TRACING
57993d68e52SSteven Rostedt void tracing_on(void);
58093d68e52SSteven Rostedt void tracing_off(void);
58193d68e52SSteven Rostedt int tracing_is_on(void);
582ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
583ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
58493d68e52SSteven Rostedt 
585526211bcSIngo Molnar extern void tracing_start(void);
586526211bcSIngo Molnar extern void tracing_stop(void);
587526211bcSIngo Molnar 
588b9075fa9SJoe Perches static inline __printf(1, 2)
589b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
590769b0441SFrederic Weisbecker {
591769b0441SFrederic Weisbecker }
592769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
593769b0441SFrederic Weisbecker do {									\
594769b0441SFrederic Weisbecker 	if (0)								\
595769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
596769b0441SFrederic Weisbecker } while (0)
597769b0441SFrederic Weisbecker 
598526211bcSIngo Molnar /**
599526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
600526211bcSIngo Molnar  * @fmt: the printf format for printing
601526211bcSIngo Molnar  *
602526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
603526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
604526211bcSIngo Molnar  *
605526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
606526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
607526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
608526211bcSIngo Molnar  * where problems are occurring.
609526211bcSIngo Molnar  *
610526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
611526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
61209ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
61309ae7234SSteven Rostedt (Red Hat)  * allocated when trace_printk() is used)
6149d3c752cSSteven Rostedt (Red Hat)  *
6159d3c752cSSteven Rostedt (Red Hat)  * A little optization trick is done here. If there's only one
6169d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
6179d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
6189d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
6199d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
6209d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
6219d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
6229d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
6239d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
6249d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
6259d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
626526211bcSIngo Molnar  */
627769b0441SFrederic Weisbecker 
6289d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
6299d3c752cSSteven Rostedt (Red Hat) do {							\
6309d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
6319d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
6329d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
6339d3c752cSSteven Rostedt (Red Hat) 	else						\
6349d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
6359d3c752cSSteven Rostedt (Red Hat) } while (0)
6369d3c752cSSteven Rostedt (Red Hat) 
6379d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
638769b0441SFrederic Weisbecker do {									\
6393debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
64048ead020SFrederic Weisbecker 		__attribute__((section("__trace_printk_fmt"))) =	\
64148ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
64248ead020SFrederic Weisbecker 									\
64307d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
64407d777feSSteven Rostedt 									\
64507d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
64648ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
64707d777feSSteven Rostedt 	else								\
64848ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
649769b0441SFrederic Weisbecker } while (0)
650769b0441SFrederic Weisbecker 
651b9075fa9SJoe Perches extern __printf(2, 3)
652b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
65348ead020SFrederic Weisbecker 
654b9075fa9SJoe Perches extern __printf(2, 3)
655b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
656769b0441SFrederic Weisbecker 
65709ae7234SSteven Rostedt (Red Hat) /**
65809ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
65909ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
66009ae7234SSteven Rostedt (Red Hat)  *
66109ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
66209ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
66309ae7234SSteven Rostedt (Red Hat)  *
66409ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
66509ae7234SSteven Rostedt (Red Hat)  * paths that a developer wants the least amount of "Heisenbug" affects,
66609ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
66709ae7234SSteven Rostedt (Red Hat)  *
66809ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
66909ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
67009ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
67109ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
67209ae7234SSteven Rostedt (Red Hat)  *
67309ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
67409ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
67509ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
67609ae7234SSteven Rostedt (Red Hat)  * allocated when trace_puts() is used)
67709ae7234SSteven Rostedt (Red Hat)  *
67809ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
67909ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
68009ae7234SSteven Rostedt (Red Hat)  */
68109ae7234SSteven Rostedt (Red Hat) 
68209ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
6833debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
68409ae7234SSteven Rostedt (Red Hat) 		__attribute__((section("__trace_printk_fmt"))) =	\
68509ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
68609ae7234SSteven Rostedt (Red Hat) 									\
68709ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
68809ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
68909ae7234SSteven Rostedt (Red Hat) 	else								\
69009ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
69109ae7234SSteven Rostedt (Red Hat) })
692bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str);
693bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size);
69409ae7234SSteven Rostedt (Red Hat) 
695c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
69603889384SSteven Rostedt 
69748ead020SFrederic Weisbecker /*
69848ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
69948ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
70048ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
70148ead020SFrederic Weisbecker  */
702769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
703769b0441SFrederic Weisbecker do {									\
70448ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
7053debb0a9SSteven Rostedt (Red Hat) 		static const char *trace_printk_fmt __used		\
70648ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
70748ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
7087bffc23eSIngo Molnar 									\
70948ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
71048ead020SFrederic Weisbecker 	} else								\
71148ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
712769b0441SFrederic Weisbecker } while (0)
713769b0441SFrederic Weisbecker 
7148db14860SNicolas Iooss extern __printf(2, 0) int
71548ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
71648ead020SFrederic Weisbecker 
7178db14860SNicolas Iooss extern __printf(2, 0) int
718526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
719769b0441SFrederic Weisbecker 
720cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
721526211bcSIngo Molnar #else
722526211bcSIngo Molnar static inline void tracing_start(void) { }
723526211bcSIngo Molnar static inline void tracing_stop(void) { }
724e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { }
72593d68e52SSteven Rostedt 
72693d68e52SSteven Rostedt static inline void tracing_on(void) { }
72793d68e52SSteven Rostedt static inline void tracing_off(void) { }
72893d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
729ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
730ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
73193d68e52SSteven Rostedt 
73260efc15aSMichal Hocko static inline __printf(1, 2)
73360efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
734526211bcSIngo Molnar {
735526211bcSIngo Molnar 	return 0;
736526211bcSIngo Molnar }
7378db14860SNicolas Iooss static __printf(1, 0) inline int
738526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
739526211bcSIngo Molnar {
740526211bcSIngo Molnar 	return 0;
741526211bcSIngo Molnar }
742cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
743769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
744526211bcSIngo Molnar 
745526211bcSIngo Molnar /*
746bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
7471da177e4SLinus Torvalds  * strict type-checking.. See the
7481da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
7491da177e4SLinus Torvalds  */
750589a9785SJohannes Berg #define __min(t1, t2, min1, min2, x, y) ({		\
751589a9785SJohannes Berg 	t1 min1 = (x);					\
752589a9785SJohannes Berg 	t2 min2 = (y);					\
753589a9785SJohannes Berg 	(void) (&min1 == &min2);			\
754589a9785SJohannes Berg 	min1 < min2 ? min1 : min2; })
755589a9785SJohannes Berg #define min(x, y)					\
756589a9785SJohannes Berg 	__min(typeof(x), typeof(y),			\
757589a9785SJohannes Berg 	      __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),	\
758589a9785SJohannes Berg 	      x, y)
7591da177e4SLinus Torvalds 
760589a9785SJohannes Berg #define __max(t1, t2, max1, max2, x, y) ({		\
761589a9785SJohannes Berg 	t1 max1 = (x);					\
762589a9785SJohannes Berg 	t2 max2 = (y);					\
763589a9785SJohannes Berg 	(void) (&max1 == &max2);			\
764589a9785SJohannes Berg 	max1 > max2 ? max1 : max2; })
765589a9785SJohannes Berg #define max(x, y)					\
766589a9785SJohannes Berg 	__max(typeof(x), typeof(y),			\
767589a9785SJohannes Berg 	      __UNIQUE_ID(max1_), __UNIQUE_ID(max2_),	\
768589a9785SJohannes Berg 	      x, y)
769bdf4bbaaSHarvey Harrison 
7702e1d06e1SMichal Nazarewicz #define min3(x, y, z) min((typeof(x))min(x, y), z)
7712e1d06e1SMichal Nazarewicz #define max3(x, y, z) max((typeof(x))max(x, y), z)
772f27c85c5SHagen Paul Pfeifer 
773bdf4bbaaSHarvey Harrison /**
774c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
775c8bf1336SMartin K. Petersen  * @x: value1
776c8bf1336SMartin K. Petersen  * @y: value2
777c8bf1336SMartin K. Petersen  */
778c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
779c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
780c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
781c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
782c8bf1336SMartin K. Petersen 
783c8bf1336SMartin K. Petersen /**
784bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
785bdf4bbaaSHarvey Harrison  * @val: current value
7862e1d06e1SMichal Nazarewicz  * @lo: lowest allowable value
7872e1d06e1SMichal Nazarewicz  * @hi: highest allowable value
788bdf4bbaaSHarvey Harrison  *
789c185b07fSMichal Nazarewicz  * This macro does strict typechecking of lo/hi to make sure they are of the
790bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
791bdf4bbaaSHarvey Harrison  */
7922e1d06e1SMichal Nazarewicz #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /*
7951da177e4SLinus Torvalds  * ..and if you can't take the strict
7961da177e4SLinus Torvalds  * types, you can specify one yourself.
7971da177e4SLinus Torvalds  *
798bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
7991da177e4SLinus Torvalds  */
800589a9785SJohannes Berg #define min_t(type, x, y)				\
801589a9785SJohannes Berg 	__min(type, type,				\
802589a9785SJohannes Berg 	      __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),	\
803589a9785SJohannes Berg 	      x, y)
8041da177e4SLinus Torvalds 
805589a9785SJohannes Berg #define max_t(type, x, y)				\
806589a9785SJohannes Berg 	__max(type, type,				\
807589a9785SJohannes Berg 	      __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),	\
808589a9785SJohannes Berg 	      x, y)
809bdf4bbaaSHarvey Harrison 
810bdf4bbaaSHarvey Harrison /**
811bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
812bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
813bdf4bbaaSHarvey Harrison  * @val: current value
814c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
815c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
816bdf4bbaaSHarvey Harrison  *
817bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
818bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
819bdf4bbaaSHarvey Harrison  */
820c185b07fSMichal Nazarewicz #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
821bdf4bbaaSHarvey Harrison 
822bdf4bbaaSHarvey Harrison /**
823bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
824bdf4bbaaSHarvey Harrison  * @val: current value
825c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
826c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
827bdf4bbaaSHarvey Harrison  *
828bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
829bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
830bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
831bdf4bbaaSHarvey Harrison  * integer type.
832bdf4bbaaSHarvey Harrison  */
833c185b07fSMichal Nazarewicz #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
8341da177e4SLinus Torvalds 
83591f68b73SWu Fengguang 
83691f68b73SWu Fengguang /*
83791f68b73SWu Fengguang  * swap - swap value of @a and @b
83891f68b73SWu Fengguang  */
839ac7b9004SPeter Zijlstra #define swap(a, b) \
840ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
84191f68b73SWu Fengguang 
8421da177e4SLinus Torvalds /**
8431da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
8441da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
8451da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
8461da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
8471da177e4SLinus Torvalds  *
8481da177e4SLinus Torvalds  */
8491da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
8501da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
8511da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
8521da177e4SLinus Torvalds 
853b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
854b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
855b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
856b9d4f426SArnaud Lacombe #endif
8579d00f92fSWANG Cong 
85858f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
85958f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms)						\
86058f86cc8SRusty Russell 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
86158f86cc8SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
86228b8d0c8SGobinda Charan Maji 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
86328b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
86428b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
86528b8d0c8SGobinda Charan Maji 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
86628b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
86728b8d0c8SGobinda Charan Maji 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
86837549e94SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
86958f86cc8SRusty Russell 	 (perms))
8701da177e4SLinus Torvalds #endif
871