xref: /linux-6.15/include/linux/kernel.h (revision 4cd5773a)
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>
14e9d376f0SJason Baron #include <linux/dynamic_debug.h>
151da177e4SLinus Torvalds #include <asm/byteorder.h>
16607ca46eSDavid Howells #include <uapi/linux/kernel.h>
171da177e4SLinus Torvalds 
184be929beSAlexey Dobriyan #define USHRT_MAX	((u16)(~0U))
194be929beSAlexey Dobriyan #define SHRT_MAX	((s16)(USHRT_MAX>>1))
204be929beSAlexey Dobriyan #define SHRT_MIN	((s16)(-SHRT_MAX - 1))
211da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
221da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
231da177e4SLinus Torvalds #define UINT_MAX	(~0U)
241da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
251da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
261da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
27111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
28111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
29111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
30a3860c1cSXi Wang #define SIZE_MAX	(~(size_t)0)
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
331da177e4SLinus Torvalds 
3444696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
3544696908SDavid S. Miller 
36a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
379f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
38a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
39f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
402ea58144SLinus Torvalds 
41c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
42c5e631cfSRusty Russell 
439b3be9f9SYinghai Lu /*
449b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
459b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
469b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
479b3be9f9SYinghai Lu  * arguments just once each.
489b3be9f9SYinghai Lu  */
499b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
509b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
519b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
529b3be9f9SYinghai Lu 
534552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
54930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
5536a26c69SNicholas Bellinger #define DIV_ROUND_UP_ULL(ll,d) \
5636a26c69SNicholas Bellinger 	({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; })
5736a26c69SNicholas Bellinger 
5836a26c69SNicholas Bellinger #if BITS_PER_LONG == 32
5936a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
6036a26c69SNicholas Bellinger #else
6136a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
6236a26c69SNicholas Bellinger #endif
63074e61ecSJames Morris 
64074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
65b28efd54SEric Paris #define roundup(x, y) (					\
66b28efd54SEric Paris {							\
676070bf35STetsuo Handa 	const typeof(y) __y = y;			\
68b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
69b28efd54SEric Paris }							\
70b28efd54SEric Paris )
71686a0f3dSEric Paris #define rounddown(x, y) (				\
72686a0f3dSEric Paris {							\
73686a0f3dSEric Paris 	typeof(x) __x = (x);				\
74686a0f3dSEric Paris 	__x - (__x % (y));				\
75686a0f3dSEric Paris }							\
76686a0f3dSEric Paris )
77b6d86d3dSGuenter Roeck 
78b6d86d3dSGuenter Roeck /*
79b6d86d3dSGuenter Roeck  * Divide positive or negative dividend by positive divisor and round
80c4e18497SGuenter Roeck  * to closest integer. Result is undefined for negative divisors and
81c4e18497SGuenter Roeck  * for negative dividends if the divisor variable type is unsigned.
82b6d86d3dSGuenter Roeck  */
839fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
849fe06081SDarrick J. Wong {							\
85b6d86d3dSGuenter Roeck 	typeof(x) __x = x;				\
86b6d86d3dSGuenter Roeck 	typeof(divisor) __d = divisor;			\
87c4e18497SGuenter Roeck 	(((typeof(x))-1) > 0 ||				\
88c4e18497SGuenter Roeck 	 ((typeof(divisor))-1) > 0 || (__x) > 0) ?	\
89b6d86d3dSGuenter Roeck 		(((__x) + ((__d) / 2)) / (__d)) :	\
90b6d86d3dSGuenter Roeck 		(((__x) - ((__d) / 2)) / (__d));	\
919fe06081SDarrick J. Wong }							\
929fe06081SDarrick J. Wong )
931da177e4SLinus Torvalds 
949993bc63SSalman Qazi /*
959993bc63SSalman Qazi  * Multiplies an integer by a fraction, while avoiding unnecessary
969993bc63SSalman Qazi  * overflow or loss of precision.
979993bc63SSalman Qazi  */
989993bc63SSalman Qazi #define mult_frac(x, numer, denom)(			\
999993bc63SSalman Qazi {							\
1009993bc63SSalman Qazi 	typeof(x) quot = (x) / (denom);			\
1019993bc63SSalman Qazi 	typeof(x) rem  = (x) % (denom);			\
1029993bc63SSalman Qazi 	(quot * (numer)) + ((rem * (numer)) / (denom));	\
1039993bc63SSalman Qazi }							\
1049993bc63SSalman Qazi )
1059993bc63SSalman Qazi 
1069993bc63SSalman Qazi 
107ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
108ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
109ca31e146SEduard - Gabriel Munteanu 
11090c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
1112da96acdSJens Axboe # include <asm/div64.h>
1122da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
1132da96acdSJens Axboe #else
1142da96acdSJens Axboe # define sector_div(n, b)( \
1152da96acdSJens Axboe { \
1162da96acdSJens Axboe 	int _res; \
1172da96acdSJens Axboe 	_res = (n) % (b); \
1182da96acdSJens Axboe 	(n) /= (b); \
1192da96acdSJens Axboe 	_res; \
1202da96acdSJens Axboe } \
1212da96acdSJens Axboe )
1222da96acdSJens Axboe #endif
1232da96acdSJens Axboe 
124218e180eSAndrew Morton /**
125218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
126218e180eSAndrew Morton  * @n: the number we're accessing
127218e180eSAndrew Morton  *
128218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
129218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
130218e180eSAndrew Morton  * 32-bits.
131218e180eSAndrew Morton  */
132218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
133218e180eSAndrew Morton 
134204b885eSJoerg Roedel /**
135204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
136204b885eSJoerg Roedel  * @n: the number we're accessing
137204b885eSJoerg Roedel  */
138204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
139204b885eSJoerg Roedel 
1401da177e4SLinus Torvalds struct completion;
141df2e71fbS[email protected] struct pt_regs;
142df2e71fbS[email protected] struct user;
1431da177e4SLinus Torvalds 
144070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
145070cb065SUwe Kleine-König extern int _cond_resched(void);
146070cb065SUwe Kleine-König # define might_resched() _cond_resched()
147070cb065SUwe Kleine-König #else
148070cb065SUwe Kleine-König # define might_resched() do { } while (0)
149070cb065SUwe Kleine-König #endif
150070cb065SUwe Kleine-König 
151d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
152d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
1531da177e4SLinus Torvalds /**
1541da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1551da177e4SLinus Torvalds  *
1561da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1571da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1581da177e4SLinus Torvalds  *
1591da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
160e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1611da177e4SLinus Torvalds  * supposed to.
1621da177e4SLinus Torvalds  */
163f8cbd99bSIngo Molnar # define might_sleep() \
164e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
165f8cbd99bSIngo Molnar #else
166d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
167d894837fSSimon Kagstrom 				   int preempt_offset) { }
168f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
169f8cbd99bSIngo Molnar #endif
170f8cbd99bSIngo Molnar 
171368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
172f8cbd99bSIngo Molnar 
17371a90484SAndrew Morton /*
17471a90484SAndrew Morton  * abs() handles unsigned and signed longs, ints, shorts and chars.  For all
17571a90484SAndrew Morton  * input types abs() returns a signed long.
17671a90484SAndrew Morton  * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
17771a90484SAndrew Morton  * for those.
17871a90484SAndrew Morton  */
1791da177e4SLinus Torvalds #define abs(x) ({						\
18071a90484SAndrew Morton 		long ret;					\
18171a90484SAndrew Morton 		if (sizeof(x) == sizeof(long)) {		\
182a49c59c0SRolf Eike Beer 			long __x = (x);				\
18371a90484SAndrew Morton 			ret = (__x < 0) ? -__x : __x;		\
18471a90484SAndrew Morton 		} else {					\
18571a90484SAndrew Morton 			int __x = (x);				\
18671a90484SAndrew Morton 			ret = (__x < 0) ? -__x : __x;		\
18771a90484SAndrew Morton 		}						\
18871a90484SAndrew Morton 		ret;						\
1891da177e4SLinus Torvalds 	})
1901da177e4SLinus Torvalds 
191658716d1SBrian Behlendorf #define abs64(x) ({				\
192658716d1SBrian Behlendorf 		s64 __x = (x);			\
193658716d1SBrian Behlendorf 		(__x < 0) ? -__x : __x;		\
194658716d1SBrian Behlendorf 	})
195658716d1SBrian Behlendorf 
1963ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
1973ee1afa3SNick Piggin void might_fault(void);
1983ee1afa3SNick Piggin #else
1993ee1afa3SNick Piggin static inline void might_fault(void)
2003ee1afa3SNick Piggin {
2013ee1afa3SNick Piggin 	might_sleep();
2023ee1afa3SNick Piggin }
2033ee1afa3SNick Piggin #endif
2043ee1afa3SNick Piggin 
205e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
206c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
2079402c95fSJoe Perches __printf(1, 2)
2084da47859SJoe Perches void panic(const char *fmt, ...)
209ff2d8b19SJoe Perches 	__noreturn __cold;
210dd287796SAndrew Morton extern void oops_enter(void);
211dd287796SAndrew Morton extern void oops_exit(void);
212863a6049SAnton Blanchard void print_oops_end_marker(void);
213dd287796SAndrew Morton extern int oops_may_print(void);
2149402c95fSJoe Perches void do_exit(long error_code)
215ff2d8b19SJoe Perches 	__noreturn;
2169402c95fSJoe Perches void complete_and_exit(struct completion *, long)
217ff2d8b19SJoe Perches 	__noreturn;
21833ee3b2eSAlexey Dobriyan 
21933ee3b2eSAlexey Dobriyan /* Internal, do not use. */
22033ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
22133ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
22233ee3b2eSAlexey Dobriyan 
22333ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
22433ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
2254c925d60SEldad Zack 
2264c925d60SEldad Zack /**
2274c925d60SEldad Zack  * kstrtoul - convert a string to an unsigned long
2284c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
2294c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
2304c925d60SEldad Zack  *  may also be a plus sign, but not a minus sign.
2314c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
2324c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
2334c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
2344c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
2354c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
2364c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
2374c925d60SEldad Zack  *
2384c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
2394c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
2404c925d60SEldad Zack  * be checked.
2414c925d60SEldad Zack */
24233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
24333ee3b2eSAlexey Dobriyan {
24433ee3b2eSAlexey Dobriyan 	/*
24533ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
24633ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
24733ee3b2eSAlexey Dobriyan 	 */
24833ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
24933ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
25033ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
25133ee3b2eSAlexey Dobriyan 	else
25233ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
25333ee3b2eSAlexey Dobriyan }
25433ee3b2eSAlexey Dobriyan 
2554c925d60SEldad Zack /**
2564c925d60SEldad Zack  * kstrtol - convert a string to a long
2574c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
2584c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
2594c925d60SEldad Zack  *  may also be a plus sign or a minus sign.
2604c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
2614c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
2624c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
2634c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
2644c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
2654c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
2664c925d60SEldad Zack  *
2674c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
2684c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
2694c925d60SEldad Zack  * be checked.
2704c925d60SEldad Zack  */
27133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
27233ee3b2eSAlexey Dobriyan {
27333ee3b2eSAlexey Dobriyan 	/*
27433ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
27533ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
27633ee3b2eSAlexey Dobriyan 	 */
27733ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
27833ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
27933ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
28033ee3b2eSAlexey Dobriyan 	else
28133ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
28233ee3b2eSAlexey Dobriyan }
28333ee3b2eSAlexey Dobriyan 
28433ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
28533ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
28633ee3b2eSAlexey Dobriyan 
28733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
28833ee3b2eSAlexey Dobriyan {
28933ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
29033ee3b2eSAlexey Dobriyan }
29133ee3b2eSAlexey Dobriyan 
29233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
29333ee3b2eSAlexey Dobriyan {
29433ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
29533ee3b2eSAlexey Dobriyan }
29633ee3b2eSAlexey Dobriyan 
29733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
29833ee3b2eSAlexey Dobriyan {
29933ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
30033ee3b2eSAlexey Dobriyan }
30133ee3b2eSAlexey Dobriyan 
30233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
30333ee3b2eSAlexey Dobriyan {
30433ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
30533ee3b2eSAlexey Dobriyan }
30633ee3b2eSAlexey Dobriyan 
30733ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
30833ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
30933ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
31033ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
31133ee3b2eSAlexey Dobriyan 
312c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
313c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
314c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
315c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
316c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
317c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
318c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
319c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
320c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
321c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
322c196e32aSAlexey Dobriyan 
323c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
324c196e32aSAlexey Dobriyan {
325c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
326c196e32aSAlexey Dobriyan }
327c196e32aSAlexey Dobriyan 
328c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
329c196e32aSAlexey Dobriyan {
330c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
331c196e32aSAlexey Dobriyan }
332c196e32aSAlexey Dobriyan 
333c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
334c196e32aSAlexey Dobriyan {
335c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
336c196e32aSAlexey Dobriyan }
337c196e32aSAlexey Dobriyan 
338c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
339c196e32aSAlexey Dobriyan {
340c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
341c196e32aSAlexey Dobriyan }
342c196e32aSAlexey Dobriyan 
34367d0a075SJoe Perches /* Obsolete, do not use.  Use kstrto<foo> instead */
34467d0a075SJoe Perches 
3451da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
3461da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
3471da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
3481da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
34933ee3b2eSAlexey Dobriyan #define strict_strtoul	kstrtoul
35033ee3b2eSAlexey Dobriyan #define strict_strtol	kstrtol
35133ee3b2eSAlexey Dobriyan #define strict_strtoull	kstrtoull
35233ee3b2eSAlexey Dobriyan #define strict_strtoll	kstrtoll
35333ee3b2eSAlexey Dobriyan 
3541ac101a5SKAMEZAWA Hiroyuki extern int num_to_str(char *buf, int size, unsigned long long num);
3551ac101a5SKAMEZAWA Hiroyuki 
35667d0a075SJoe Perches /* lib/printf utilities */
35767d0a075SJoe Perches 
358b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
359b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
360b9075fa9SJoe Perches extern __printf(3, 4)
361b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
362b9075fa9SJoe Perches extern __printf(3, 0)
363b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
364b9075fa9SJoe Perches extern __printf(3, 4)
365b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
366b9075fa9SJoe Perches extern __printf(3, 0)
367b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
368b9075fa9SJoe Perches extern __printf(2, 3)
369b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
37011443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
3711da177e4SLinus Torvalds 
3726061d949SJoe Perches extern __scanf(2, 3)
3736061d949SJoe Perches int sscanf(const char *, const char *, ...);
3746061d949SJoe Perches extern __scanf(2, 0)
3756061d949SJoe Perches int vsscanf(const char *, const char *, va_list);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
3781da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
379d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
3801da177e4SLinus Torvalds 
3815e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
382cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
3831da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
3841da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
385ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
386ab7476cfSArjan van de Ven 
38704a2e6a5SEric W. Biederman struct pid;
38804a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
3931da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
394aa727107SAdrian Bunk extern int panic_timeout;
3951da177e4SLinus Torvalds extern int panic_on_oops;
3968da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
3975211a242SKurt Garloff extern int panic_on_io_nmi;
39855af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
3991da177e4SLinus Torvalds extern const char *print_tainted(void);
400373d4d09SRusty Russell enum lockdep_ok {
401373d4d09SRusty Russell 	LOCKDEP_STILL_OK,
402373d4d09SRusty Russell 	LOCKDEP_NOW_UNRELIABLE
403373d4d09SRusty Russell };
404373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok);
40525ddbb18SAndi Kleen extern int test_taint(unsigned flag);
40625ddbb18SAndi Kleen extern unsigned long get_taint(void);
407b920de1bSDavid Howells extern int root_mountflags;
4081da177e4SLinus Torvalds 
4092ce802f6STejun Heo extern bool early_boot_irqs_disabled;
4102ce802f6STejun Heo 
4111da177e4SLinus Torvalds /* Values used for system_state */
4121da177e4SLinus Torvalds extern enum system_states {
4131da177e4SLinus Torvalds 	SYSTEM_BOOTING,
4141da177e4SLinus Torvalds 	SYSTEM_RUNNING,
4151da177e4SLinus Torvalds 	SYSTEM_HALT,
4161da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
4171da177e4SLinus Torvalds 	SYSTEM_RESTART,
4181da177e4SLinus Torvalds } system_state;
4191da177e4SLinus Torvalds 
42025ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
42125ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
42225ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP		2
42325ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
42425ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
42525ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
42625ddbb18SAndi Kleen #define TAINT_USER			6
42725ddbb18SAndi Kleen #define TAINT_DIE			7
42825ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
42925ddbb18SAndi Kleen #define TAINT_WARN			9
43026e9a397SLinus Torvalds #define TAINT_CRAP			10
43192946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
4322449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
4331da177e4SLinus Torvalds 
4343fc95772SHarvey Harrison extern const char hex_asc[];
4353fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
4363fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
4373fc95772SHarvey Harrison 
43855036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
4393fc95772SHarvey Harrison {
4403fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
4413fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
4423fc95772SHarvey Harrison 	return buf;
4433fc95772SHarvey Harrison }
44499eaf3c4SRandy Dunlap 
44555036ba7SAndy Shevchenko static inline char * __deprecated pack_hex_byte(char *buf, u8 byte)
44655036ba7SAndy Shevchenko {
44755036ba7SAndy Shevchenko 	return hex_byte_pack(buf, byte);
44855036ba7SAndy Shevchenko }
44955036ba7SAndy Shevchenko 
45090378889SAndy Shevchenko extern int hex_to_bin(char ch);
451b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
45290378889SAndy Shevchenko 
453*4cd5773aSAndy Shevchenko int mac_pton(const char *s, u8 *mac);
454*4cd5773aSAndy Shevchenko 
4558a64f336SJoe Perches /*
456526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
4572002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
4582002c258SSteven Rostedt  *
4592002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
4602002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
461156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
4622002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
4632002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
4642002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
4652002c258SSteven Rostedt  * to continue tracing.
4662002c258SSteven Rostedt  *
4672002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
4682002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
4692002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
4702002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
4712002c258SSteven Rostedt  *
4722002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
473526211bcSIngo Molnar  */
4742002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER
4752002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */
4762002c258SSteven Rostedt void tracing_off_permanent(void);
4772002c258SSteven Rostedt #else
4782002c258SSteven Rostedt static inline void tracing_off_permanent(void) { }
4792002c258SSteven Rostedt #endif
480cecbca96SFrederic Weisbecker 
481cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
482cecbca96SFrederic Weisbecker 	DUMP_NONE,
483cecbca96SFrederic Weisbecker 	DUMP_ALL,
484cecbca96SFrederic Weisbecker 	DUMP_ORIG,
485cecbca96SFrederic Weisbecker };
486cecbca96SFrederic Weisbecker 
487526211bcSIngo Molnar #ifdef CONFIG_TRACING
48893d68e52SSteven Rostedt void tracing_on(void);
48993d68e52SSteven Rostedt void tracing_off(void);
49093d68e52SSteven Rostedt int tracing_is_on(void);
491ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
492ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
49393d68e52SSteven Rostedt 
494526211bcSIngo Molnar extern void tracing_start(void);
495526211bcSIngo Molnar extern void tracing_stop(void);
496526211bcSIngo Molnar extern void ftrace_off_permanent(void);
497526211bcSIngo Molnar 
498b9075fa9SJoe Perches static inline __printf(1, 2)
499b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
500769b0441SFrederic Weisbecker {
501769b0441SFrederic Weisbecker }
502769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
503769b0441SFrederic Weisbecker do {									\
504769b0441SFrederic Weisbecker 	if (0)								\
505769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
506769b0441SFrederic Weisbecker } while (0)
507769b0441SFrederic Weisbecker 
508526211bcSIngo Molnar /**
509526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
510526211bcSIngo Molnar  * @fmt: the printf format for printing
511526211bcSIngo Molnar  *
512526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
513526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
514526211bcSIngo Molnar  *
515526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
516526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
517526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
518526211bcSIngo Molnar  * where problems are occurring.
519526211bcSIngo Molnar  *
520526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
521526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
52209ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
52309ae7234SSteven Rostedt (Red Hat)  * allocated when trace_printk() is used)
5249d3c752cSSteven Rostedt (Red Hat)  *
5259d3c752cSSteven Rostedt (Red Hat)  * A little optization trick is done here. If there's only one
5269d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
5279d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
5289d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
5299d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
5309d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
5319d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
5329d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
5339d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
5349d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
5359d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
536526211bcSIngo Molnar  */
537769b0441SFrederic Weisbecker 
5389d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
5399d3c752cSSteven Rostedt (Red Hat) do {							\
5409d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
5419d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
5429d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
5439d3c752cSSteven Rostedt (Red Hat) 	else						\
5449d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
5459d3c752cSSteven Rostedt (Red Hat) } while (0)
5469d3c752cSSteven Rostedt (Red Hat) 
5479d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
548769b0441SFrederic Weisbecker do {									\
54948ead020SFrederic Weisbecker 	static const char *trace_printk_fmt				\
55048ead020SFrederic Weisbecker 		__attribute__((section("__trace_printk_fmt"))) =	\
55148ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
55248ead020SFrederic Weisbecker 									\
55307d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
55407d777feSSteven Rostedt 									\
55507d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
55648ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
55707d777feSSteven Rostedt 	else								\
55848ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
559769b0441SFrederic Weisbecker } while (0)
560769b0441SFrederic Weisbecker 
561b9075fa9SJoe Perches extern __printf(2, 3)
562b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
56348ead020SFrederic Weisbecker 
564b9075fa9SJoe Perches extern __printf(2, 3)
565b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
566769b0441SFrederic Weisbecker 
5677450231fSRandy Dunlap extern int __trace_bputs(unsigned long ip, const char *str);
5687450231fSRandy Dunlap extern int __trace_puts(unsigned long ip, const char *str, int size);
5697450231fSRandy Dunlap 
57009ae7234SSteven Rostedt (Red Hat) /**
57109ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
57209ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
57309ae7234SSteven Rostedt (Red Hat)  *
57409ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
57509ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
57609ae7234SSteven Rostedt (Red Hat)  *
57709ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
57809ae7234SSteven Rostedt (Red Hat)  * paths that a developer wants the least amount of "Heisenbug" affects,
57909ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
58009ae7234SSteven Rostedt (Red Hat)  *
58109ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
58209ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
58309ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
58409ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
58509ae7234SSteven Rostedt (Red Hat)  *
58609ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
58709ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
58809ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
58909ae7234SSteven Rostedt (Red Hat)  * allocated when trace_puts() is used)
59009ae7234SSteven Rostedt (Red Hat)  *
59109ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
59209ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
59309ae7234SSteven Rostedt (Red Hat)  */
59409ae7234SSteven Rostedt (Red Hat) 
59509ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
59609ae7234SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt				\
59709ae7234SSteven Rostedt (Red Hat) 		__attribute__((section("__trace_printk_fmt"))) =	\
59809ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
59909ae7234SSteven Rostedt (Red Hat) 									\
60009ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
60109ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
60209ae7234SSteven Rostedt (Red Hat) 	else								\
60309ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
60409ae7234SSteven Rostedt (Red Hat) })
60509ae7234SSteven Rostedt (Red Hat) 
606c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
60703889384SSteven Rostedt 
60848ead020SFrederic Weisbecker /*
60948ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
61048ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
61148ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
61248ead020SFrederic Weisbecker  */
613769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
614769b0441SFrederic Weisbecker do {									\
61548ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
616769b0441SFrederic Weisbecker 		static const char *trace_printk_fmt			\
61748ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
61848ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
6197bffc23eSIngo Molnar 									\
62048ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
62148ead020SFrederic Weisbecker 	} else								\
62248ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
623769b0441SFrederic Weisbecker } while (0)
624769b0441SFrederic Weisbecker 
625526211bcSIngo Molnar extern int
62648ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
62748ead020SFrederic Weisbecker 
62848ead020SFrederic Weisbecker extern int
629526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
630769b0441SFrederic Weisbecker 
631cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
632526211bcSIngo Molnar #else
633526211bcSIngo Molnar static inline void tracing_start(void) { }
634526211bcSIngo Molnar static inline void tracing_stop(void) { }
635526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { }
636e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { }
63793d68e52SSteven Rostedt 
63893d68e52SSteven Rostedt static inline void tracing_on(void) { }
63993d68e52SSteven Rostedt static inline void tracing_off(void) { }
64093d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
641ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
642ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
64393d68e52SSteven Rostedt 
64460efc15aSMichal Hocko static inline __printf(1, 2)
64560efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
646526211bcSIngo Molnar {
647526211bcSIngo Molnar 	return 0;
648526211bcSIngo Molnar }
649526211bcSIngo Molnar static inline int
650526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
651526211bcSIngo Molnar {
652526211bcSIngo Molnar 	return 0;
653526211bcSIngo Molnar }
654cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
655769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
656526211bcSIngo Molnar 
657526211bcSIngo Molnar /*
658bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
6591da177e4SLinus Torvalds  * strict type-checking.. See the
6601da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
6611da177e4SLinus Torvalds  */
6621da177e4SLinus Torvalds #define min(x, y) ({				\
663bdf4bbaaSHarvey Harrison 	typeof(x) _min1 = (x);			\
664bdf4bbaaSHarvey Harrison 	typeof(y) _min2 = (y);			\
665bdf4bbaaSHarvey Harrison 	(void) (&_min1 == &_min2);		\
666bdf4bbaaSHarvey Harrison 	_min1 < _min2 ? _min1 : _min2; })
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds #define max(x, y) ({				\
669bdf4bbaaSHarvey Harrison 	typeof(x) _max1 = (x);			\
670bdf4bbaaSHarvey Harrison 	typeof(y) _max2 = (y);			\
671bdf4bbaaSHarvey Harrison 	(void) (&_max1 == &_max2);		\
672bdf4bbaaSHarvey Harrison 	_max1 > _max2 ? _max1 : _max2; })
673bdf4bbaaSHarvey Harrison 
674f27c85c5SHagen Paul Pfeifer #define min3(x, y, z) ({			\
675f27c85c5SHagen Paul Pfeifer 	typeof(x) _min1 = (x);			\
676f27c85c5SHagen Paul Pfeifer 	typeof(y) _min2 = (y);			\
677f27c85c5SHagen Paul Pfeifer 	typeof(z) _min3 = (z);			\
678f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min2);		\
679f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min3);		\
680f27c85c5SHagen Paul Pfeifer 	_min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
681f27c85c5SHagen Paul Pfeifer 		(_min2 < _min3 ? _min2 : _min3); })
682f27c85c5SHagen Paul Pfeifer 
683f27c85c5SHagen Paul Pfeifer #define max3(x, y, z) ({			\
684f27c85c5SHagen Paul Pfeifer 	typeof(x) _max1 = (x);			\
685f27c85c5SHagen Paul Pfeifer 	typeof(y) _max2 = (y);			\
686f27c85c5SHagen Paul Pfeifer 	typeof(z) _max3 = (z);			\
687f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max2);		\
688f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max3);		\
689f27c85c5SHagen Paul Pfeifer 	_max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
690f27c85c5SHagen Paul Pfeifer 		(_max2 > _max3 ? _max2 : _max3); })
691f27c85c5SHagen Paul Pfeifer 
692bdf4bbaaSHarvey Harrison /**
693c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
694c8bf1336SMartin K. Petersen  * @x: value1
695c8bf1336SMartin K. Petersen  * @y: value2
696c8bf1336SMartin K. Petersen  */
697c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
698c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
699c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
700c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
701c8bf1336SMartin K. Petersen 
702c8bf1336SMartin K. Petersen /**
703bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
704bdf4bbaaSHarvey Harrison  * @val: current value
705bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
706bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
707bdf4bbaaSHarvey Harrison  *
708bdf4bbaaSHarvey Harrison  * This macro does strict typechecking of min/max to make sure they are of the
709bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
710bdf4bbaaSHarvey Harrison  */
711bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({			\
712bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
713bdf4bbaaSHarvey Harrison 	typeof(min) __min = (min);		\
714bdf4bbaaSHarvey Harrison 	typeof(max) __max = (max);		\
715bdf4bbaaSHarvey Harrison 	(void) (&__val == &__min);		\
716bdf4bbaaSHarvey Harrison 	(void) (&__val == &__max);		\
717bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
718bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds /*
7211da177e4SLinus Torvalds  * ..and if you can't take the strict
7221da177e4SLinus Torvalds  * types, you can specify one yourself.
7231da177e4SLinus Torvalds  *
724bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
7251da177e4SLinus Torvalds  */
726bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({			\
727bdf4bbaaSHarvey Harrison 	type __min1 = (x);			\
728bdf4bbaaSHarvey Harrison 	type __min2 = (y);			\
729bdf4bbaaSHarvey Harrison 	__min1 < __min2 ? __min1: __min2; })
7301da177e4SLinus Torvalds 
731bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({			\
732bdf4bbaaSHarvey Harrison 	type __max1 = (x);			\
733bdf4bbaaSHarvey Harrison 	type __max2 = (y);			\
734bdf4bbaaSHarvey Harrison 	__max1 > __max2 ? __max1: __max2; })
735bdf4bbaaSHarvey Harrison 
736bdf4bbaaSHarvey Harrison /**
737bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
738bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
739bdf4bbaaSHarvey Harrison  * @val: current value
740bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
741bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
742bdf4bbaaSHarvey Harrison  *
743bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
744bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
745bdf4bbaaSHarvey Harrison  */
746bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({		\
747bdf4bbaaSHarvey Harrison 	type __val = (val);			\
748bdf4bbaaSHarvey Harrison 	type __min = (min);			\
749bdf4bbaaSHarvey Harrison 	type __max = (max);			\
750bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
751bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
752bdf4bbaaSHarvey Harrison 
753bdf4bbaaSHarvey Harrison /**
754bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
755bdf4bbaaSHarvey Harrison  * @val: current value
756bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
757bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
758bdf4bbaaSHarvey Harrison  *
759bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
760bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
761bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
762bdf4bbaaSHarvey Harrison  * integer type.
763bdf4bbaaSHarvey Harrison  */
764bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({		\
765bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
766bdf4bbaaSHarvey Harrison 	typeof(val) __min = (min);		\
767bdf4bbaaSHarvey Harrison 	typeof(val) __max = (max);		\
768bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
769bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
7701da177e4SLinus Torvalds 
77191f68b73SWu Fengguang 
77291f68b73SWu Fengguang /*
77391f68b73SWu Fengguang  * swap - swap value of @a and @b
77491f68b73SWu Fengguang  */
775ac7b9004SPeter Zijlstra #define swap(a, b) \
776ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
77791f68b73SWu Fengguang 
7781da177e4SLinus Torvalds /**
7791da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
7801da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
7811da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
7821da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
7831da177e4SLinus Torvalds  *
7841da177e4SLinus Torvalds  */
7851da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
7861da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
7871da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
7881da177e4SLinus Torvalds 
789b9d4f426SArnaud Lacombe /* Trap pasters of __FUNCTION__ at compile-time */
790b9d4f426SArnaud Lacombe #define __FUNCTION__ (__func__)
791b9d4f426SArnaud Lacombe 
792b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
793b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
794b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
795b9d4f426SArnaud Lacombe #endif
7969d00f92fSWANG Cong 
7971da177e4SLinus Torvalds #endif
798