xref: /linux-6.15/include/linux/kernel.h (revision 9993bc63)
11da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H
21da177e4SLinus Torvalds #define _LINUX_KERNEL_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds /*
51da177e4SLinus Torvalds  * 'kernel.h' contains some often-used function prototypes etc
61da177e4SLinus Torvalds  */
7a79ff731SAlexey Dobriyan #define __ALIGN_KERNEL(x, a)		__ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
8a79ff731SAlexey Dobriyan #define __ALIGN_KERNEL_MASK(x, mask)	(((x) + (mask)) & ~(mask))
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #ifdef __KERNEL__
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <stdarg.h>
131da177e4SLinus Torvalds #include <linux/linkage.h>
141da177e4SLinus Torvalds #include <linux/stddef.h>
151da177e4SLinus Torvalds #include <linux/types.h>
161da177e4SLinus Torvalds #include <linux/compiler.h>
171da177e4SLinus Torvalds #include <linux/bitops.h>
18f0d1b0b3SDavid Howells #include <linux/log2.h>
19e0deaff4SAndrew Morton #include <linux/typecheck.h>
20968ab183SLinus Torvalds #include <linux/printk.h>
21e9d376f0SJason Baron #include <linux/dynamic_debug.h>
221da177e4SLinus Torvalds #include <asm/byteorder.h>
231da177e4SLinus Torvalds #include <asm/bug.h>
241da177e4SLinus Torvalds 
254be929beSAlexey Dobriyan #define USHRT_MAX	((u16)(~0U))
264be929beSAlexey Dobriyan #define SHRT_MAX	((s16)(USHRT_MAX>>1))
274be929beSAlexey Dobriyan #define SHRT_MIN	((s16)(-SHRT_MAX - 1))
281da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
291da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
301da177e4SLinus Torvalds #define UINT_MAX	(~0U)
311da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
321da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
331da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
34111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
35111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
36111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
391da177e4SLinus Torvalds 
40a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
419f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
42a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
43f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
442ea58144SLinus Torvalds 
45c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
46c5e631cfSRusty Russell 
479b3be9f9SYinghai Lu /*
489b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
499b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
509b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
519b3be9f9SYinghai Lu  * arguments just once each.
529b3be9f9SYinghai Lu  */
539b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
549b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
559b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
569b3be9f9SYinghai Lu 
574552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
58930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
5936a26c69SNicholas Bellinger #define DIV_ROUND_UP_ULL(ll,d) \
6036a26c69SNicholas Bellinger 	({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; })
6136a26c69SNicholas Bellinger 
6236a26c69SNicholas Bellinger #if BITS_PER_LONG == 32
6336a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
6436a26c69SNicholas Bellinger #else
6536a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
6636a26c69SNicholas Bellinger #endif
67074e61ecSJames Morris 
68074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
69b28efd54SEric Paris #define roundup(x, y) (					\
70b28efd54SEric Paris {							\
716070bf35STetsuo Handa 	const typeof(y) __y = y;			\
72b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
73b28efd54SEric Paris }							\
74b28efd54SEric Paris )
75686a0f3dSEric Paris #define rounddown(x, y) (				\
76686a0f3dSEric Paris {							\
77686a0f3dSEric Paris 	typeof(x) __x = (x);				\
78686a0f3dSEric Paris 	__x - (__x % (y));				\
79686a0f3dSEric Paris }							\
80686a0f3dSEric Paris )
819fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
829fe06081SDarrick J. Wong {							\
839fe06081SDarrick J. Wong 	typeof(divisor) __divisor = divisor;		\
849fe06081SDarrick J. Wong 	(((x) + ((__divisor) / 2)) / (__divisor));	\
859fe06081SDarrick J. Wong }							\
869fe06081SDarrick J. Wong )
871da177e4SLinus Torvalds 
88*9993bc63SSalman Qazi /*
89*9993bc63SSalman Qazi  * Multiplies an integer by a fraction, while avoiding unnecessary
90*9993bc63SSalman Qazi  * overflow or loss of precision.
91*9993bc63SSalman Qazi  */
92*9993bc63SSalman Qazi #define mult_frac(x, numer, denom)(			\
93*9993bc63SSalman Qazi {							\
94*9993bc63SSalman Qazi 	typeof(x) quot = (x) / (denom);			\
95*9993bc63SSalman Qazi 	typeof(x) rem  = (x) % (denom);			\
96*9993bc63SSalman Qazi 	(quot * (numer)) + ((rem * (numer)) / (denom));	\
97*9993bc63SSalman Qazi }							\
98*9993bc63SSalman Qazi )
99*9993bc63SSalman Qazi 
100*9993bc63SSalman Qazi 
101ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
102ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
103ca31e146SEduard - Gabriel Munteanu 
10490c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
1052da96acdSJens Axboe # include <asm/div64.h>
1062da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
1072da96acdSJens Axboe #else
1082da96acdSJens Axboe # define sector_div(n, b)( \
1092da96acdSJens Axboe { \
1102da96acdSJens Axboe 	int _res; \
1112da96acdSJens Axboe 	_res = (n) % (b); \
1122da96acdSJens Axboe 	(n) /= (b); \
1132da96acdSJens Axboe 	_res; \
1142da96acdSJens Axboe } \
1152da96acdSJens Axboe )
1162da96acdSJens Axboe #endif
1172da96acdSJens Axboe 
118218e180eSAndrew Morton /**
119218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
120218e180eSAndrew Morton  * @n: the number we're accessing
121218e180eSAndrew Morton  *
122218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
123218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
124218e180eSAndrew Morton  * 32-bits.
125218e180eSAndrew Morton  */
126218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
127218e180eSAndrew Morton 
128204b885eSJoerg Roedel /**
129204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
130204b885eSJoerg Roedel  * @n: the number we're accessing
131204b885eSJoerg Roedel  */
132204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
133204b885eSJoerg Roedel 
1341da177e4SLinus Torvalds struct completion;
135df2e71fbS[email protected] struct pt_regs;
136df2e71fbS[email protected] struct user;
1371da177e4SLinus Torvalds 
138070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
139070cb065SUwe Kleine-König extern int _cond_resched(void);
140070cb065SUwe Kleine-König # define might_resched() _cond_resched()
141070cb065SUwe Kleine-König #else
142070cb065SUwe Kleine-König # define might_resched() do { } while (0)
143070cb065SUwe Kleine-König #endif
144070cb065SUwe Kleine-König 
145d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
146d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
1471da177e4SLinus Torvalds /**
1481da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1491da177e4SLinus Torvalds  *
1501da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1511da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1521da177e4SLinus Torvalds  *
1531da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
154e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1551da177e4SLinus Torvalds  * supposed to.
1561da177e4SLinus Torvalds  */
157f8cbd99bSIngo Molnar # define might_sleep() \
158e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
159f8cbd99bSIngo Molnar #else
160d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
161d894837fSSimon Kagstrom 				   int preempt_offset) { }
162f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
163f8cbd99bSIngo Molnar #endif
164f8cbd99bSIngo Molnar 
165368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
166f8cbd99bSIngo Molnar 
16771a90484SAndrew Morton /*
16871a90484SAndrew Morton  * abs() handles unsigned and signed longs, ints, shorts and chars.  For all
16971a90484SAndrew Morton  * input types abs() returns a signed long.
17071a90484SAndrew Morton  * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
17171a90484SAndrew Morton  * for those.
17271a90484SAndrew Morton  */
1731da177e4SLinus Torvalds #define abs(x) ({						\
17471a90484SAndrew Morton 		long ret;					\
17571a90484SAndrew Morton 		if (sizeof(x) == sizeof(long)) {		\
176a49c59c0SRolf Eike Beer 			long __x = (x);				\
17771a90484SAndrew Morton 			ret = (__x < 0) ? -__x : __x;		\
17871a90484SAndrew Morton 		} else {					\
17971a90484SAndrew Morton 			int __x = (x);				\
18071a90484SAndrew Morton 			ret = (__x < 0) ? -__x : __x;		\
18171a90484SAndrew Morton 		}						\
18271a90484SAndrew Morton 		ret;						\
1831da177e4SLinus Torvalds 	})
1841da177e4SLinus Torvalds 
185658716d1SBrian Behlendorf #define abs64(x) ({				\
186658716d1SBrian Behlendorf 		s64 __x = (x);			\
187658716d1SBrian Behlendorf 		(__x < 0) ? -__x : __x;		\
188658716d1SBrian Behlendorf 	})
189658716d1SBrian Behlendorf 
1903ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
1913ee1afa3SNick Piggin void might_fault(void);
1923ee1afa3SNick Piggin #else
1933ee1afa3SNick Piggin static inline void might_fault(void)
1943ee1afa3SNick Piggin {
1953ee1afa3SNick Piggin 	might_sleep();
1963ee1afa3SNick Piggin }
1973ee1afa3SNick Piggin #endif
1983ee1afa3SNick Piggin 
199e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
200c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
2019402c95fSJoe Perches __printf(1, 2)
2024da47859SJoe Perches void panic(const char *fmt, ...)
203ff2d8b19SJoe Perches 	__noreturn __cold;
204dd287796SAndrew Morton extern void oops_enter(void);
205dd287796SAndrew Morton extern void oops_exit(void);
206863a6049SAnton Blanchard void print_oops_end_marker(void);
207dd287796SAndrew Morton extern int oops_may_print(void);
2089402c95fSJoe Perches void do_exit(long error_code)
209ff2d8b19SJoe Perches 	__noreturn;
2109402c95fSJoe Perches void complete_and_exit(struct completion *, long)
211ff2d8b19SJoe Perches 	__noreturn;
21233ee3b2eSAlexey Dobriyan 
21333ee3b2eSAlexey Dobriyan /* Internal, do not use. */
21433ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
21533ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
21633ee3b2eSAlexey Dobriyan 
21733ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
21833ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
21933ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
22033ee3b2eSAlexey Dobriyan {
22133ee3b2eSAlexey Dobriyan 	/*
22233ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
22333ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
22433ee3b2eSAlexey Dobriyan 	 */
22533ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
22633ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
22733ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
22833ee3b2eSAlexey Dobriyan 	else
22933ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
23033ee3b2eSAlexey Dobriyan }
23133ee3b2eSAlexey Dobriyan 
23233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
23333ee3b2eSAlexey Dobriyan {
23433ee3b2eSAlexey Dobriyan 	/*
23533ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
23633ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
23733ee3b2eSAlexey Dobriyan 	 */
23833ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
23933ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
24033ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
24133ee3b2eSAlexey Dobriyan 	else
24233ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
24333ee3b2eSAlexey Dobriyan }
24433ee3b2eSAlexey Dobriyan 
24533ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
24633ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
24733ee3b2eSAlexey Dobriyan 
24833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
24933ee3b2eSAlexey Dobriyan {
25033ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
25133ee3b2eSAlexey Dobriyan }
25233ee3b2eSAlexey Dobriyan 
25333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
25433ee3b2eSAlexey Dobriyan {
25533ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
25633ee3b2eSAlexey Dobriyan }
25733ee3b2eSAlexey Dobriyan 
25833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
25933ee3b2eSAlexey Dobriyan {
26033ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
26133ee3b2eSAlexey Dobriyan }
26233ee3b2eSAlexey Dobriyan 
26333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
26433ee3b2eSAlexey Dobriyan {
26533ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
26633ee3b2eSAlexey Dobriyan }
26733ee3b2eSAlexey Dobriyan 
26833ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
26933ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
27033ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
27133ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
27233ee3b2eSAlexey Dobriyan 
273c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
274c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
275c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
276c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
277c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
278c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
279c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
280c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
281c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
282c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
283c196e32aSAlexey Dobriyan 
284c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
285c196e32aSAlexey Dobriyan {
286c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
287c196e32aSAlexey Dobriyan }
288c196e32aSAlexey Dobriyan 
289c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
290c196e32aSAlexey Dobriyan {
291c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
292c196e32aSAlexey Dobriyan }
293c196e32aSAlexey Dobriyan 
294c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
295c196e32aSAlexey Dobriyan {
296c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
297c196e32aSAlexey Dobriyan }
298c196e32aSAlexey Dobriyan 
299c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
300c196e32aSAlexey Dobriyan {
301c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
302c196e32aSAlexey Dobriyan }
303c196e32aSAlexey Dobriyan 
30467d0a075SJoe Perches /* Obsolete, do not use.  Use kstrto<foo> instead */
30567d0a075SJoe Perches 
3061da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
3071da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
3081da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
3091da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
31033ee3b2eSAlexey Dobriyan #define strict_strtoul	kstrtoul
31133ee3b2eSAlexey Dobriyan #define strict_strtol	kstrtol
31233ee3b2eSAlexey Dobriyan #define strict_strtoull	kstrtoull
31333ee3b2eSAlexey Dobriyan #define strict_strtoll	kstrtoll
31433ee3b2eSAlexey Dobriyan 
31567d0a075SJoe Perches /* lib/printf utilities */
31667d0a075SJoe Perches 
317b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
318b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
319b9075fa9SJoe Perches extern __printf(3, 4)
320b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
321b9075fa9SJoe Perches extern __printf(3, 0)
322b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
323b9075fa9SJoe Perches extern __printf(3, 4)
324b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
325b9075fa9SJoe Perches extern __printf(3, 0)
326b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
327b9075fa9SJoe Perches extern __printf(2, 3)
328b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
32911443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...)
3321da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 3)));
3331da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list)
3341da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 0)));
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
3371da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
338d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
3391da177e4SLinus Torvalds 
3405e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
341cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
3421da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
3431da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
344ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
345ab7476cfSArjan van de Ven 
34604a2e6a5SEric W. Biederman struct pid;
34704a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
352e3e8a75dSKirill Korotaev extern void wake_up_klogd(void);
3531da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
354aa727107SAdrian Bunk extern int panic_timeout;
3551da177e4SLinus Torvalds extern int panic_on_oops;
3568da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
3575211a242SKurt Garloff extern int panic_on_io_nmi;
35855af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
3591da177e4SLinus Torvalds extern const char *print_tainted(void);
36025ddbb18SAndi Kleen extern void add_taint(unsigned flag);
36125ddbb18SAndi Kleen extern int test_taint(unsigned flag);
36225ddbb18SAndi Kleen extern unsigned long get_taint(void);
363b920de1bSDavid Howells extern int root_mountflags;
3641da177e4SLinus Torvalds 
3652ce802f6STejun Heo extern bool early_boot_irqs_disabled;
3662ce802f6STejun Heo 
3671da177e4SLinus Torvalds /* Values used for system_state */
3681da177e4SLinus Torvalds extern enum system_states {
3691da177e4SLinus Torvalds 	SYSTEM_BOOTING,
3701da177e4SLinus Torvalds 	SYSTEM_RUNNING,
3711da177e4SLinus Torvalds 	SYSTEM_HALT,
3721da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
3731da177e4SLinus Torvalds 	SYSTEM_RESTART,
374729b4d4cSAlexey Starikovskiy 	SYSTEM_SUSPEND_DISK,
3751da177e4SLinus Torvalds } system_state;
3761da177e4SLinus Torvalds 
37725ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
37825ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
37925ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP		2
38025ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
38125ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
38225ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
38325ddbb18SAndi Kleen #define TAINT_USER			6
38425ddbb18SAndi Kleen #define TAINT_DIE			7
38525ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
38625ddbb18SAndi Kleen #define TAINT_WARN			9
38726e9a397SLinus Torvalds #define TAINT_CRAP			10
38892946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
3892449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
3901da177e4SLinus Torvalds 
3913fc95772SHarvey Harrison extern const char hex_asc[];
3923fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
3933fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
3943fc95772SHarvey Harrison 
39555036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
3963fc95772SHarvey Harrison {
3973fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
3983fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
3993fc95772SHarvey Harrison 	return buf;
4003fc95772SHarvey Harrison }
40199eaf3c4SRandy Dunlap 
40255036ba7SAndy Shevchenko static inline char * __deprecated pack_hex_byte(char *buf, u8 byte)
40355036ba7SAndy Shevchenko {
40455036ba7SAndy Shevchenko 	return hex_byte_pack(buf, byte);
40555036ba7SAndy Shevchenko }
40655036ba7SAndy Shevchenko 
40790378889SAndy Shevchenko extern int hex_to_bin(char ch);
408b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
40990378889SAndy Shevchenko 
4108a64f336SJoe Perches /*
411526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
4122002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
4132002c258SSteven Rostedt  *
4142002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
4152002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
416156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
4172002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
4182002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
4192002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
4202002c258SSteven Rostedt  * to continue tracing.
4212002c258SSteven Rostedt  *
4222002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
4232002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
4242002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
4252002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
4262002c258SSteven Rostedt  *
4272002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
428526211bcSIngo Molnar  */
4292002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER
4302002c258SSteven Rostedt void tracing_on(void);
4312002c258SSteven Rostedt void tracing_off(void);
4322002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */
4332002c258SSteven Rostedt void tracing_off_permanent(void);
4342002c258SSteven Rostedt int tracing_is_on(void);
4352002c258SSteven Rostedt #else
4362002c258SSteven Rostedt static inline void tracing_on(void) { }
4372002c258SSteven Rostedt static inline void tracing_off(void) { }
4382002c258SSteven Rostedt static inline void tracing_off_permanent(void) { }
4392002c258SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
4402002c258SSteven Rostedt #endif
441cecbca96SFrederic Weisbecker 
442cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
443cecbca96SFrederic Weisbecker 	DUMP_NONE,
444cecbca96SFrederic Weisbecker 	DUMP_ALL,
445cecbca96SFrederic Weisbecker 	DUMP_ORIG,
446cecbca96SFrederic Weisbecker };
447cecbca96SFrederic Weisbecker 
448526211bcSIngo Molnar #ifdef CONFIG_TRACING
449526211bcSIngo Molnar extern void tracing_start(void);
450526211bcSIngo Molnar extern void tracing_stop(void);
451526211bcSIngo Molnar extern void ftrace_off_permanent(void);
452526211bcSIngo Molnar 
453b9075fa9SJoe Perches static inline __printf(1, 2)
454b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
455769b0441SFrederic Weisbecker {
456769b0441SFrederic Weisbecker }
457769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
458769b0441SFrederic Weisbecker do {									\
459769b0441SFrederic Weisbecker 	if (0)								\
460769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
461769b0441SFrederic Weisbecker } while (0)
462769b0441SFrederic Weisbecker 
463526211bcSIngo Molnar /**
464526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
465526211bcSIngo Molnar  * @fmt: the printf format for printing
466526211bcSIngo Molnar  *
467526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
468526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
469526211bcSIngo Molnar  *
470526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
471526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
472526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
473526211bcSIngo Molnar  * where problems are occurring.
474526211bcSIngo Molnar  *
475526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
476526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
477526211bcSIngo Molnar  * your code.
478526211bcSIngo Molnar  */
479769b0441SFrederic Weisbecker 
480769b0441SFrederic Weisbecker #define trace_printk(fmt, args...)					\
481769b0441SFrederic Weisbecker do {									\
482769b0441SFrederic Weisbecker 	__trace_printk_check_format(fmt, ##args);			\
48348ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
48448ead020SFrederic Weisbecker 		static const char *trace_printk_fmt			\
48548ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
48648ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
48748ead020SFrederic Weisbecker 									\
48848ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
48948ead020SFrederic Weisbecker 	} else								\
49048ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);		\
491769b0441SFrederic Weisbecker } while (0)
492769b0441SFrederic Weisbecker 
493b9075fa9SJoe Perches extern __printf(2, 3)
494b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
49548ead020SFrederic Weisbecker 
496b9075fa9SJoe Perches extern __printf(2, 3)
497b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
498769b0441SFrederic Weisbecker 
49903889384SSteven Rostedt extern void trace_dump_stack(void);
50003889384SSteven Rostedt 
50148ead020SFrederic Weisbecker /*
50248ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
50348ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
50448ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
50548ead020SFrederic Weisbecker  */
506769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
507769b0441SFrederic Weisbecker do {									\
50848ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
509769b0441SFrederic Weisbecker 		static const char *trace_printk_fmt			\
51048ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
51148ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
5127bffc23eSIngo Molnar 									\
51348ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
51448ead020SFrederic Weisbecker 	} else								\
51548ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
516769b0441SFrederic Weisbecker } while (0)
517769b0441SFrederic Weisbecker 
518526211bcSIngo Molnar extern int
51948ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
52048ead020SFrederic Weisbecker 
52148ead020SFrederic Weisbecker extern int
522526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
523769b0441SFrederic Weisbecker 
524cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
525526211bcSIngo Molnar #else
526b9075fa9SJoe Perches static inline __printf(1, 2)
527b9075fa9SJoe Perches int trace_printk(const char *fmt, ...);
528526211bcSIngo Molnar 
529526211bcSIngo Molnar static inline void tracing_start(void) { }
530526211bcSIngo Molnar static inline void tracing_stop(void) { }
531526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { }
532e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { }
533526211bcSIngo Molnar static inline int
534526211bcSIngo Molnar trace_printk(const char *fmt, ...)
535526211bcSIngo Molnar {
536526211bcSIngo Molnar 	return 0;
537526211bcSIngo Molnar }
538526211bcSIngo Molnar static inline int
539526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
540526211bcSIngo Molnar {
541526211bcSIngo Molnar 	return 0;
542526211bcSIngo Molnar }
543cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
544769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
545526211bcSIngo Molnar 
546526211bcSIngo Molnar /*
547bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
5481da177e4SLinus Torvalds  * strict type-checking.. See the
5491da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
5501da177e4SLinus Torvalds  */
5511da177e4SLinus Torvalds #define min(x, y) ({				\
552bdf4bbaaSHarvey Harrison 	typeof(x) _min1 = (x);			\
553bdf4bbaaSHarvey Harrison 	typeof(y) _min2 = (y);			\
554bdf4bbaaSHarvey Harrison 	(void) (&_min1 == &_min2);		\
555bdf4bbaaSHarvey Harrison 	_min1 < _min2 ? _min1 : _min2; })
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds #define max(x, y) ({				\
558bdf4bbaaSHarvey Harrison 	typeof(x) _max1 = (x);			\
559bdf4bbaaSHarvey Harrison 	typeof(y) _max2 = (y);			\
560bdf4bbaaSHarvey Harrison 	(void) (&_max1 == &_max2);		\
561bdf4bbaaSHarvey Harrison 	_max1 > _max2 ? _max1 : _max2; })
562bdf4bbaaSHarvey Harrison 
563f27c85c5SHagen Paul Pfeifer #define min3(x, y, z) ({			\
564f27c85c5SHagen Paul Pfeifer 	typeof(x) _min1 = (x);			\
565f27c85c5SHagen Paul Pfeifer 	typeof(y) _min2 = (y);			\
566f27c85c5SHagen Paul Pfeifer 	typeof(z) _min3 = (z);			\
567f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min2);		\
568f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min3);		\
569f27c85c5SHagen Paul Pfeifer 	_min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
570f27c85c5SHagen Paul Pfeifer 		(_min2 < _min3 ? _min2 : _min3); })
571f27c85c5SHagen Paul Pfeifer 
572f27c85c5SHagen Paul Pfeifer #define max3(x, y, z) ({			\
573f27c85c5SHagen Paul Pfeifer 	typeof(x) _max1 = (x);			\
574f27c85c5SHagen Paul Pfeifer 	typeof(y) _max2 = (y);			\
575f27c85c5SHagen Paul Pfeifer 	typeof(z) _max3 = (z);			\
576f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max2);		\
577f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max3);		\
578f27c85c5SHagen Paul Pfeifer 	_max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
579f27c85c5SHagen Paul Pfeifer 		(_max2 > _max3 ? _max2 : _max3); })
580f27c85c5SHagen Paul Pfeifer 
581bdf4bbaaSHarvey Harrison /**
582c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
583c8bf1336SMartin K. Petersen  * @x: value1
584c8bf1336SMartin K. Petersen  * @y: value2
585c8bf1336SMartin K. Petersen  */
586c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
587c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
588c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
589c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
590c8bf1336SMartin K. Petersen 
591c8bf1336SMartin K. Petersen /**
592bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
593bdf4bbaaSHarvey Harrison  * @val: current value
594bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
595bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
596bdf4bbaaSHarvey Harrison  *
597bdf4bbaaSHarvey Harrison  * This macro does strict typechecking of min/max to make sure they are of the
598bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
599bdf4bbaaSHarvey Harrison  */
600bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({			\
601bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
602bdf4bbaaSHarvey Harrison 	typeof(min) __min = (min);		\
603bdf4bbaaSHarvey Harrison 	typeof(max) __max = (max);		\
604bdf4bbaaSHarvey Harrison 	(void) (&__val == &__min);		\
605bdf4bbaaSHarvey Harrison 	(void) (&__val == &__max);		\
606bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
607bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds /*
6101da177e4SLinus Torvalds  * ..and if you can't take the strict
6111da177e4SLinus Torvalds  * types, you can specify one yourself.
6121da177e4SLinus Torvalds  *
613bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
6141da177e4SLinus Torvalds  */
615bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({			\
616bdf4bbaaSHarvey Harrison 	type __min1 = (x);			\
617bdf4bbaaSHarvey Harrison 	type __min2 = (y);			\
618bdf4bbaaSHarvey Harrison 	__min1 < __min2 ? __min1: __min2; })
6191da177e4SLinus Torvalds 
620bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({			\
621bdf4bbaaSHarvey Harrison 	type __max1 = (x);			\
622bdf4bbaaSHarvey Harrison 	type __max2 = (y);			\
623bdf4bbaaSHarvey Harrison 	__max1 > __max2 ? __max1: __max2; })
624bdf4bbaaSHarvey Harrison 
625bdf4bbaaSHarvey Harrison /**
626bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
627bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
628bdf4bbaaSHarvey Harrison  * @val: current value
629bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
630bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
631bdf4bbaaSHarvey Harrison  *
632bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
633bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
634bdf4bbaaSHarvey Harrison  */
635bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({		\
636bdf4bbaaSHarvey Harrison 	type __val = (val);			\
637bdf4bbaaSHarvey Harrison 	type __min = (min);			\
638bdf4bbaaSHarvey Harrison 	type __max = (max);			\
639bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
640bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
641bdf4bbaaSHarvey Harrison 
642bdf4bbaaSHarvey Harrison /**
643bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
644bdf4bbaaSHarvey Harrison  * @val: current value
645bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
646bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
647bdf4bbaaSHarvey Harrison  *
648bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
649bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
650bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
651bdf4bbaaSHarvey Harrison  * integer type.
652bdf4bbaaSHarvey Harrison  */
653bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({		\
654bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
655bdf4bbaaSHarvey Harrison 	typeof(val) __min = (min);		\
656bdf4bbaaSHarvey Harrison 	typeof(val) __max = (max);		\
657bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
658bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
6591da177e4SLinus Torvalds 
66091f68b73SWu Fengguang 
66191f68b73SWu Fengguang /*
66291f68b73SWu Fengguang  * swap - swap value of @a and @b
66391f68b73SWu Fengguang  */
664ac7b9004SPeter Zijlstra #define swap(a, b) \
665ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
66691f68b73SWu Fengguang 
6671da177e4SLinus Torvalds /**
6681da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
6691da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
6701da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
6711da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
6721da177e4SLinus Torvalds  *
6731da177e4SLinus Torvalds  */
6741da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
6751da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
6761da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
6771da177e4SLinus Torvalds 
678903c0c7cSKOSAKI Motohiro #ifdef __CHECKER__
679903c0c7cSKOSAKI Motohiro #define BUILD_BUG_ON_NOT_POWER_OF_2(n)
680ca39599cSDr. David Alan Gilbert #define BUILD_BUG_ON_ZERO(e) (0)
681ca39599cSDr. David Alan Gilbert #define BUILD_BUG_ON_NULL(e) ((void*)0)
682903c0c7cSKOSAKI Motohiro #define BUILD_BUG_ON(condition)
6831399ff86SDavid Daney #define BUILD_BUG() (0)
684903c0c7cSKOSAKI Motohiro #else /* __CHECKER__ */
685903c0c7cSKOSAKI Motohiro 
686cc8ef6ebSRoland Dreier /* Force a compilation error if a constant expression is not a power of 2 */
687cc8ef6ebSRoland Dreier #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
688cc8ef6ebSRoland Dreier 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
689cc8ef6ebSRoland Dreier 
6904552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a
6914552d5dcSJan Beulich    result (of value 0 and type size_t), so the expression can be used
6924552d5dcSJan Beulich    e.g. in a structure initializer (or where-ever else comma expressions
6934552d5dcSJan Beulich    aren't permitted). */
6948c87df45SJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
6958c87df45SJan Beulich #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
6964552d5dcSJan Beulich 
6977ef88ad5SRusty Russell /**
6987ef88ad5SRusty Russell  * BUILD_BUG_ON - break compile if a condition is true.
699ffbbf2daSRandy Dunlap  * @condition: the condition which the compiler should know is false.
7007ef88ad5SRusty Russell  *
7017ef88ad5SRusty Russell  * If you have some code which relies on certain constants being equal, or
7027ef88ad5SRusty Russell  * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
7037ef88ad5SRusty Russell  * detect if someone changes it.
7047ef88ad5SRusty Russell  *
7057ef88ad5SRusty Russell  * The implementation uses gcc's reluctance to create a negative array, but
7067ef88ad5SRusty Russell  * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
7077ef88ad5SRusty Russell  * to inline functions).  So as a fallback we use the optimizer; if it can't
7087ef88ad5SRusty Russell  * prove the condition is false, it will cause a link error on the undefined
7097ef88ad5SRusty Russell  * "__build_bug_on_failed".  This error message can be harder to track down
7107ef88ad5SRusty Russell  * though, hence the two different methods.
7117ef88ad5SRusty Russell  */
7127ef88ad5SRusty Russell #ifndef __OPTIMIZE__
7137ef88ad5SRusty Russell #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
7147ef88ad5SRusty Russell #else
7157ef88ad5SRusty Russell extern int __build_bug_on_failed;
7167ef88ad5SRusty Russell #define BUILD_BUG_ON(condition)					\
7177ef88ad5SRusty Russell 	do {							\
7187ef88ad5SRusty Russell 		((void)sizeof(char[1 - 2*!!(condition)]));	\
7197ef88ad5SRusty Russell 		if (condition) __build_bug_on_failed = 1;	\
7207ef88ad5SRusty Russell 	} while(0)
7217ef88ad5SRusty Russell #endif
7221399ff86SDavid Daney 
7231399ff86SDavid Daney /**
7241399ff86SDavid Daney  * BUILD_BUG - break compile if used.
7251399ff86SDavid Daney  *
7261399ff86SDavid Daney  * If you have some code that you expect the compiler to eliminate at
7271399ff86SDavid Daney  * build time, you should use BUILD_BUG to detect if it is
7281399ff86SDavid Daney  * unexpectedly used.
7291399ff86SDavid Daney  */
7301399ff86SDavid Daney #define BUILD_BUG()						\
7311399ff86SDavid Daney 	do {							\
7321399ff86SDavid Daney 		extern void __build_bug_failed(void)		\
7331399ff86SDavid Daney 			__linktime_error("BUILD_BUG failed");	\
7341399ff86SDavid Daney 		__build_bug_failed();				\
7351399ff86SDavid Daney 	} while (0)
7361399ff86SDavid Daney 
737903c0c7cSKOSAKI Motohiro #endif	/* __CHECKER__ */
7387ef88ad5SRusty Russell 
739b9d4f426SArnaud Lacombe /* Trap pasters of __FUNCTION__ at compile-time */
740b9d4f426SArnaud Lacombe #define __FUNCTION__ (__func__)
741b9d4f426SArnaud Lacombe 
742b9d4f426SArnaud Lacombe /* This helps us to avoid #ifdef CONFIG_NUMA */
743b9d4f426SArnaud Lacombe #ifdef CONFIG_NUMA
744b9d4f426SArnaud Lacombe #define NUMA_BUILD 1
745b9d4f426SArnaud Lacombe #else
746b9d4f426SArnaud Lacombe #define NUMA_BUILD 0
747b9d4f426SArnaud Lacombe #endif
748b9d4f426SArnaud Lacombe 
749b9d4f426SArnaud Lacombe /* This helps us avoid #ifdef CONFIG_COMPACTION */
750b9d4f426SArnaud Lacombe #ifdef CONFIG_COMPACTION
751b9d4f426SArnaud Lacombe #define COMPACTION_BUILD 1
752b9d4f426SArnaud Lacombe #else
753b9d4f426SArnaud Lacombe #define COMPACTION_BUILD 0
754b9d4f426SArnaud Lacombe #endif
755b9d4f426SArnaud Lacombe 
756b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
757b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
758b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
759b9d4f426SArnaud Lacombe #endif
7609d00f92fSWANG Cong 
7619d00f92fSWANG Cong struct sysinfo;
7629d00f92fSWANG Cong extern int do_sysinfo(struct sysinfo *info);
7639d00f92fSWANG Cong 
7649d00f92fSWANG Cong #endif /* __KERNEL__ */
7659d00f92fSWANG Cong 
7669d00f92fSWANG Cong #define SI_LOAD_SHIFT	16
7679d00f92fSWANG Cong struct sysinfo {
7689d00f92fSWANG Cong 	long uptime;			/* Seconds since boot */
7699d00f92fSWANG Cong 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
7709d00f92fSWANG Cong 	unsigned long totalram;		/* Total usable main memory size */
7719d00f92fSWANG Cong 	unsigned long freeram;		/* Available memory size */
7729d00f92fSWANG Cong 	unsigned long sharedram;	/* Amount of shared memory */
7739d00f92fSWANG Cong 	unsigned long bufferram;	/* Memory used by buffers */
7749d00f92fSWANG Cong 	unsigned long totalswap;	/* Total swap space size */
7759d00f92fSWANG Cong 	unsigned long freeswap;		/* swap space still available */
7769d00f92fSWANG Cong 	unsigned short procs;		/* Number of current processes */
7779d00f92fSWANG Cong 	unsigned short pad;		/* explicit padding for m68k */
7789d00f92fSWANG Cong 	unsigned long totalhigh;	/* Total high memory size */
7799d00f92fSWANG Cong 	unsigned long freehigh;		/* Available high memory size */
7809d00f92fSWANG Cong 	unsigned int mem_unit;		/* Memory unit size in bytes */
7819d00f92fSWANG Cong 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
7829d00f92fSWANG Cong };
7839d00f92fSWANG Cong 
7841da177e4SLinus Torvalds #endif
785