xref: /linux-6.15/include/linux/kernel.h (revision cdbe61bf)
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))
59074e61ecSJames Morris 
60074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
61b28efd54SEric Paris #define roundup(x, y) (					\
62b28efd54SEric Paris {							\
636070bf35STetsuo Handa 	const typeof(y) __y = y;			\
64b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
65b28efd54SEric Paris }							\
66b28efd54SEric Paris )
67686a0f3dSEric Paris #define rounddown(x, y) (				\
68686a0f3dSEric Paris {							\
69686a0f3dSEric Paris 	typeof(x) __x = (x);				\
70686a0f3dSEric Paris 	__x - (__x % (y));				\
71686a0f3dSEric Paris }							\
72686a0f3dSEric Paris )
739fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
749fe06081SDarrick J. Wong {							\
759fe06081SDarrick J. Wong 	typeof(divisor) __divisor = divisor;		\
769fe06081SDarrick J. Wong 	(((x) + ((__divisor) / 2)) / (__divisor));	\
779fe06081SDarrick J. Wong }							\
789fe06081SDarrick J. Wong )
791da177e4SLinus Torvalds 
80ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
81ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
82ca31e146SEduard - Gabriel Munteanu 
8390c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
842da96acdSJens Axboe # include <asm/div64.h>
852da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
862da96acdSJens Axboe #else
872da96acdSJens Axboe # define sector_div(n, b)( \
882da96acdSJens Axboe { \
892da96acdSJens Axboe 	int _res; \
902da96acdSJens Axboe 	_res = (n) % (b); \
912da96acdSJens Axboe 	(n) /= (b); \
922da96acdSJens Axboe 	_res; \
932da96acdSJens Axboe } \
942da96acdSJens Axboe )
952da96acdSJens Axboe #endif
962da96acdSJens Axboe 
97218e180eSAndrew Morton /**
98218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
99218e180eSAndrew Morton  * @n: the number we're accessing
100218e180eSAndrew Morton  *
101218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
102218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
103218e180eSAndrew Morton  * 32-bits.
104218e180eSAndrew Morton  */
105218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
106218e180eSAndrew Morton 
107204b885eSJoerg Roedel /**
108204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
109204b885eSJoerg Roedel  * @n: the number we're accessing
110204b885eSJoerg Roedel  */
111204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
112204b885eSJoerg Roedel 
1131da177e4SLinus Torvalds struct completion;
114df2e71fbS[email protected] struct pt_regs;
115df2e71fbS[email protected] struct user;
1161da177e4SLinus Torvalds 
117070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
118070cb065SUwe Kleine-König extern int _cond_resched(void);
119070cb065SUwe Kleine-König # define might_resched() _cond_resched()
120070cb065SUwe Kleine-König #else
121070cb065SUwe Kleine-König # define might_resched() do { } while (0)
122070cb065SUwe Kleine-König #endif
123070cb065SUwe Kleine-König 
1247106a27bSRandy Dunlap #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
125d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
1261da177e4SLinus Torvalds /**
1271da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1281da177e4SLinus Torvalds  *
1291da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1301da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1311da177e4SLinus Torvalds  *
1321da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
133e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1341da177e4SLinus Torvalds  * supposed to.
1351da177e4SLinus Torvalds  */
136f8cbd99bSIngo Molnar # define might_sleep() \
137e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
138f8cbd99bSIngo Molnar #else
139d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
140d894837fSSimon Kagstrom 				   int preempt_offset) { }
141f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
142f8cbd99bSIngo Molnar #endif
143f8cbd99bSIngo Molnar 
144368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
145f8cbd99bSIngo Molnar 
14671a90484SAndrew Morton /*
14771a90484SAndrew Morton  * abs() handles unsigned and signed longs, ints, shorts and chars.  For all
14871a90484SAndrew Morton  * input types abs() returns a signed long.
14971a90484SAndrew Morton  * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
15071a90484SAndrew Morton  * for those.
15171a90484SAndrew Morton  */
1521da177e4SLinus Torvalds #define abs(x) ({						\
15371a90484SAndrew Morton 		long ret;					\
15471a90484SAndrew Morton 		if (sizeof(x) == sizeof(long)) {		\
155a49c59c0SRolf Eike Beer 			long __x = (x);				\
15671a90484SAndrew Morton 			ret = (__x < 0) ? -__x : __x;		\
15771a90484SAndrew Morton 		} else {					\
15871a90484SAndrew Morton 			int __x = (x);				\
15971a90484SAndrew Morton 			ret = (__x < 0) ? -__x : __x;		\
16071a90484SAndrew Morton 		}						\
16171a90484SAndrew Morton 		ret;						\
1621da177e4SLinus Torvalds 	})
1631da177e4SLinus Torvalds 
164658716d1SBrian Behlendorf #define abs64(x) ({				\
165658716d1SBrian Behlendorf 		s64 __x = (x);			\
166658716d1SBrian Behlendorf 		(__x < 0) ? -__x : __x;		\
167658716d1SBrian Behlendorf 	})
168658716d1SBrian Behlendorf 
1693ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
1703ee1afa3SNick Piggin void might_fault(void);
1713ee1afa3SNick Piggin #else
1723ee1afa3SNick Piggin static inline void might_fault(void)
1733ee1afa3SNick Piggin {
1743ee1afa3SNick Piggin 	might_sleep();
1753ee1afa3SNick Piggin }
1763ee1afa3SNick Piggin #endif
1773ee1afa3SNick Piggin 
178e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
179c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
1801da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...)
181a586df06SAndi Kleen 	__attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
182dd287796SAndrew Morton extern void oops_enter(void);
183dd287796SAndrew Morton extern void oops_exit(void);
184863a6049SAnton Blanchard void print_oops_end_marker(void);
185dd287796SAndrew Morton extern int oops_may_print(void);
186ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code)
1871da177e4SLinus Torvalds 	ATTRIB_NORET;
1881da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long)
1891da177e4SLinus Torvalds 	ATTRIB_NORET;
19033ee3b2eSAlexey Dobriyan 
19133ee3b2eSAlexey Dobriyan /* Internal, do not use. */
19233ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
19333ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
19433ee3b2eSAlexey Dobriyan 
19533ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
19633ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
19733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
19833ee3b2eSAlexey Dobriyan {
19933ee3b2eSAlexey Dobriyan 	/*
20033ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
20133ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
20233ee3b2eSAlexey Dobriyan 	 */
20333ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
20433ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
20533ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
20633ee3b2eSAlexey Dobriyan 	else
20733ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
20833ee3b2eSAlexey Dobriyan }
20933ee3b2eSAlexey Dobriyan 
21033ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
21133ee3b2eSAlexey Dobriyan {
21233ee3b2eSAlexey Dobriyan 	/*
21333ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
21433ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
21533ee3b2eSAlexey Dobriyan 	 */
21633ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
21733ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
21833ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
21933ee3b2eSAlexey Dobriyan 	else
22033ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
22133ee3b2eSAlexey Dobriyan }
22233ee3b2eSAlexey Dobriyan 
22333ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
22433ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
22533ee3b2eSAlexey Dobriyan 
22633ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
22733ee3b2eSAlexey Dobriyan {
22833ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
22933ee3b2eSAlexey Dobriyan }
23033ee3b2eSAlexey Dobriyan 
23133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
23233ee3b2eSAlexey Dobriyan {
23333ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
23433ee3b2eSAlexey Dobriyan }
23533ee3b2eSAlexey Dobriyan 
23633ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
23733ee3b2eSAlexey Dobriyan {
23833ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
23933ee3b2eSAlexey Dobriyan }
24033ee3b2eSAlexey Dobriyan 
24133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
24233ee3b2eSAlexey Dobriyan {
24333ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
24433ee3b2eSAlexey Dobriyan }
24533ee3b2eSAlexey Dobriyan 
24633ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
24733ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
24833ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
24933ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
25033ee3b2eSAlexey Dobriyan 
2511da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
2521da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
2531da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
2541da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
25533ee3b2eSAlexey Dobriyan #define strict_strtoul	kstrtoul
25633ee3b2eSAlexey Dobriyan #define strict_strtol	kstrtol
25733ee3b2eSAlexey Dobriyan #define strict_strtoull	kstrtoull
25833ee3b2eSAlexey Dobriyan #define strict_strtoll	kstrtoll
25933ee3b2eSAlexey Dobriyan 
2601da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...)
2611da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 3)));
2621da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list)
2631da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 0)));
2641da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...)
2651da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
2661da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2671da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
2681da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
2691da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
2701da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2711da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
272e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
273e905914fSJeremy Fitzhardinge 	__attribute__ ((format (printf, 2, 3)));
27411443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...)
2771da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 3)));
2781da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list)
2791da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 0)));
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
2821da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
283d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
2841da177e4SLinus Torvalds 
2855e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
286*cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
2871da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
2881da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
289ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
290ab7476cfSArjan van de Ven 
29104a2e6a5SEric W. Biederman struct pid;
29204a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
297e3e8a75dSKirill Korotaev extern void wake_up_klogd(void);
2981da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
299aa727107SAdrian Bunk extern int panic_timeout;
3001da177e4SLinus Torvalds extern int panic_on_oops;
3018da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
3025211a242SKurt Garloff extern int panic_on_io_nmi;
3031da177e4SLinus Torvalds extern const char *print_tainted(void);
30425ddbb18SAndi Kleen extern void add_taint(unsigned flag);
30525ddbb18SAndi Kleen extern int test_taint(unsigned flag);
30625ddbb18SAndi Kleen extern unsigned long get_taint(void);
307b920de1bSDavid Howells extern int root_mountflags;
3081da177e4SLinus Torvalds 
3092ce802f6STejun Heo extern bool early_boot_irqs_disabled;
3102ce802f6STejun Heo 
3111da177e4SLinus Torvalds /* Values used for system_state */
3121da177e4SLinus Torvalds extern enum system_states {
3131da177e4SLinus Torvalds 	SYSTEM_BOOTING,
3141da177e4SLinus Torvalds 	SYSTEM_RUNNING,
3151da177e4SLinus Torvalds 	SYSTEM_HALT,
3161da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
3171da177e4SLinus Torvalds 	SYSTEM_RESTART,
318729b4d4cSAlexey Starikovskiy 	SYSTEM_SUSPEND_DISK,
3191da177e4SLinus Torvalds } system_state;
3201da177e4SLinus Torvalds 
32125ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
32225ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
32325ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP		2
32425ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
32525ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
32625ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
32725ddbb18SAndi Kleen #define TAINT_USER			6
32825ddbb18SAndi Kleen #define TAINT_DIE			7
32925ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
33025ddbb18SAndi Kleen #define TAINT_WARN			9
33126e9a397SLinus Torvalds #define TAINT_CRAP			10
33292946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
3331da177e4SLinus Torvalds 
3343fc95772SHarvey Harrison extern const char hex_asc[];
3353fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
3363fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
3373fc95772SHarvey Harrison 
3383fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte)
3393fc95772SHarvey Harrison {
3403fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
3413fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
3423fc95772SHarvey Harrison 	return buf;
3433fc95772SHarvey Harrison }
34499eaf3c4SRandy Dunlap 
34590378889SAndy Shevchenko extern int hex_to_bin(char ch);
346dc88e460SMimi Zohar extern void hex2bin(u8 *dst, const char *src, size_t count);
34790378889SAndy Shevchenko 
3488a64f336SJoe Perches /*
349526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
3502002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
3512002c258SSteven Rostedt  *
3522002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
3532002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
354156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
3552002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
3562002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
3572002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
3582002c258SSteven Rostedt  * to continue tracing.
3592002c258SSteven Rostedt  *
3602002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
3612002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
3622002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
3632002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
3642002c258SSteven Rostedt  *
3652002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
366526211bcSIngo Molnar  */
3672002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER
3682002c258SSteven Rostedt void tracing_on(void);
3692002c258SSteven Rostedt void tracing_off(void);
3702002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */
3712002c258SSteven Rostedt void tracing_off_permanent(void);
3722002c258SSteven Rostedt int tracing_is_on(void);
3732002c258SSteven Rostedt #else
3742002c258SSteven Rostedt static inline void tracing_on(void) { }
3752002c258SSteven Rostedt static inline void tracing_off(void) { }
3762002c258SSteven Rostedt static inline void tracing_off_permanent(void) { }
3772002c258SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
3782002c258SSteven Rostedt #endif
379cecbca96SFrederic Weisbecker 
380cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
381cecbca96SFrederic Weisbecker 	DUMP_NONE,
382cecbca96SFrederic Weisbecker 	DUMP_ALL,
383cecbca96SFrederic Weisbecker 	DUMP_ORIG,
384cecbca96SFrederic Weisbecker };
385cecbca96SFrederic Weisbecker 
386526211bcSIngo Molnar #ifdef CONFIG_TRACING
387526211bcSIngo Molnar extern void tracing_start(void);
388526211bcSIngo Molnar extern void tracing_stop(void);
389526211bcSIngo Molnar extern void ftrace_off_permanent(void);
390526211bcSIngo Molnar 
391769b0441SFrederic Weisbecker static inline void __attribute__ ((format (printf, 1, 2)))
392769b0441SFrederic Weisbecker ____trace_printk_check_format(const char *fmt, ...)
393769b0441SFrederic Weisbecker {
394769b0441SFrederic Weisbecker }
395769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
396769b0441SFrederic Weisbecker do {									\
397769b0441SFrederic Weisbecker 	if (0)								\
398769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
399769b0441SFrederic Weisbecker } while (0)
400769b0441SFrederic Weisbecker 
401526211bcSIngo Molnar /**
402526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
403526211bcSIngo Molnar  * @fmt: the printf format for printing
404526211bcSIngo Molnar  *
405526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
406526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
407526211bcSIngo Molnar  *
408526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
409526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
410526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
411526211bcSIngo Molnar  * where problems are occurring.
412526211bcSIngo Molnar  *
413526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
414526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
415526211bcSIngo Molnar  * your code.
416526211bcSIngo Molnar  */
417769b0441SFrederic Weisbecker 
418769b0441SFrederic Weisbecker #define trace_printk(fmt, args...)					\
419769b0441SFrederic Weisbecker do {									\
420769b0441SFrederic Weisbecker 	__trace_printk_check_format(fmt, ##args);			\
42148ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
42248ead020SFrederic Weisbecker 		static const char *trace_printk_fmt			\
42348ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
42448ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
42548ead020SFrederic Weisbecker 									\
42648ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
42748ead020SFrederic Weisbecker 	} else								\
42848ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);		\
429769b0441SFrederic Weisbecker } while (0)
430769b0441SFrederic Weisbecker 
431526211bcSIngo Molnar extern int
43248ead020SFrederic Weisbecker __trace_bprintk(unsigned long ip, const char *fmt, ...)
43348ead020SFrederic Weisbecker 	__attribute__ ((format (printf, 2, 3)));
43448ead020SFrederic Weisbecker 
43548ead020SFrederic Weisbecker extern int
436526211bcSIngo Molnar __trace_printk(unsigned long ip, const char *fmt, ...)
437526211bcSIngo Molnar 	__attribute__ ((format (printf, 2, 3)));
438769b0441SFrederic Weisbecker 
43903889384SSteven Rostedt extern void trace_dump_stack(void);
44003889384SSteven Rostedt 
44148ead020SFrederic Weisbecker /*
44248ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
44348ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
44448ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
44548ead020SFrederic Weisbecker  */
446769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
447769b0441SFrederic Weisbecker do {									\
44848ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
449769b0441SFrederic Weisbecker 		static const char *trace_printk_fmt			\
45048ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
45148ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
4527bffc23eSIngo Molnar 									\
45348ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
45448ead020SFrederic Weisbecker 	} else								\
45548ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
456769b0441SFrederic Weisbecker } while (0)
457769b0441SFrederic Weisbecker 
458526211bcSIngo Molnar extern int
45948ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
46048ead020SFrederic Weisbecker 
46148ead020SFrederic Weisbecker extern int
462526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
463769b0441SFrederic Weisbecker 
464cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
465526211bcSIngo Molnar #else
466526211bcSIngo Molnar static inline int
467526211bcSIngo Molnar trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
468526211bcSIngo Molnar 
469526211bcSIngo Molnar static inline void tracing_start(void) { }
470526211bcSIngo Molnar static inline void tracing_stop(void) { }
471526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { }
472e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { }
473526211bcSIngo Molnar static inline int
474526211bcSIngo Molnar trace_printk(const char *fmt, ...)
475526211bcSIngo Molnar {
476526211bcSIngo Molnar 	return 0;
477526211bcSIngo Molnar }
478526211bcSIngo Molnar static inline int
479526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
480526211bcSIngo Molnar {
481526211bcSIngo Molnar 	return 0;
482526211bcSIngo Molnar }
483cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
484769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
485526211bcSIngo Molnar 
486526211bcSIngo Molnar /*
487bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
4881da177e4SLinus Torvalds  * strict type-checking.. See the
4891da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
4901da177e4SLinus Torvalds  */
4911da177e4SLinus Torvalds #define min(x, y) ({				\
492bdf4bbaaSHarvey Harrison 	typeof(x) _min1 = (x);			\
493bdf4bbaaSHarvey Harrison 	typeof(y) _min2 = (y);			\
494bdf4bbaaSHarvey Harrison 	(void) (&_min1 == &_min2);		\
495bdf4bbaaSHarvey Harrison 	_min1 < _min2 ? _min1 : _min2; })
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds #define max(x, y) ({				\
498bdf4bbaaSHarvey Harrison 	typeof(x) _max1 = (x);			\
499bdf4bbaaSHarvey Harrison 	typeof(y) _max2 = (y);			\
500bdf4bbaaSHarvey Harrison 	(void) (&_max1 == &_max2);		\
501bdf4bbaaSHarvey Harrison 	_max1 > _max2 ? _max1 : _max2; })
502bdf4bbaaSHarvey Harrison 
503f27c85c5SHagen Paul Pfeifer #define min3(x, y, z) ({			\
504f27c85c5SHagen Paul Pfeifer 	typeof(x) _min1 = (x);			\
505f27c85c5SHagen Paul Pfeifer 	typeof(y) _min2 = (y);			\
506f27c85c5SHagen Paul Pfeifer 	typeof(z) _min3 = (z);			\
507f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min2);		\
508f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min3);		\
509f27c85c5SHagen Paul Pfeifer 	_min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
510f27c85c5SHagen Paul Pfeifer 		(_min2 < _min3 ? _min2 : _min3); })
511f27c85c5SHagen Paul Pfeifer 
512f27c85c5SHagen Paul Pfeifer #define max3(x, y, z) ({			\
513f27c85c5SHagen Paul Pfeifer 	typeof(x) _max1 = (x);			\
514f27c85c5SHagen Paul Pfeifer 	typeof(y) _max2 = (y);			\
515f27c85c5SHagen Paul Pfeifer 	typeof(z) _max3 = (z);			\
516f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max2);		\
517f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max3);		\
518f27c85c5SHagen Paul Pfeifer 	_max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
519f27c85c5SHagen Paul Pfeifer 		(_max2 > _max3 ? _max2 : _max3); })
520f27c85c5SHagen Paul Pfeifer 
521bdf4bbaaSHarvey Harrison /**
522c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
523c8bf1336SMartin K. Petersen  * @x: value1
524c8bf1336SMartin K. Petersen  * @y: value2
525c8bf1336SMartin K. Petersen  */
526c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
527c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
528c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
529c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
530c8bf1336SMartin K. Petersen 
531c8bf1336SMartin K. Petersen /**
532bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
533bdf4bbaaSHarvey Harrison  * @val: current value
534bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
535bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
536bdf4bbaaSHarvey Harrison  *
537bdf4bbaaSHarvey Harrison  * This macro does strict typechecking of min/max to make sure they are of the
538bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
539bdf4bbaaSHarvey Harrison  */
540bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({			\
541bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
542bdf4bbaaSHarvey Harrison 	typeof(min) __min = (min);		\
543bdf4bbaaSHarvey Harrison 	typeof(max) __max = (max);		\
544bdf4bbaaSHarvey Harrison 	(void) (&__val == &__min);		\
545bdf4bbaaSHarvey Harrison 	(void) (&__val == &__max);		\
546bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
547bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  * ..and if you can't take the strict
5511da177e4SLinus Torvalds  * types, you can specify one yourself.
5521da177e4SLinus Torvalds  *
553bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
5541da177e4SLinus Torvalds  */
555bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({			\
556bdf4bbaaSHarvey Harrison 	type __min1 = (x);			\
557bdf4bbaaSHarvey Harrison 	type __min2 = (y);			\
558bdf4bbaaSHarvey Harrison 	__min1 < __min2 ? __min1: __min2; })
5591da177e4SLinus Torvalds 
560bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({			\
561bdf4bbaaSHarvey Harrison 	type __max1 = (x);			\
562bdf4bbaaSHarvey Harrison 	type __max2 = (y);			\
563bdf4bbaaSHarvey Harrison 	__max1 > __max2 ? __max1: __max2; })
564bdf4bbaaSHarvey Harrison 
565bdf4bbaaSHarvey Harrison /**
566bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
567bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
568bdf4bbaaSHarvey Harrison  * @val: current value
569bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
570bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
571bdf4bbaaSHarvey Harrison  *
572bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
573bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
574bdf4bbaaSHarvey Harrison  */
575bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({		\
576bdf4bbaaSHarvey Harrison 	type __val = (val);			\
577bdf4bbaaSHarvey Harrison 	type __min = (min);			\
578bdf4bbaaSHarvey Harrison 	type __max = (max);			\
579bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
580bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
581bdf4bbaaSHarvey Harrison 
582bdf4bbaaSHarvey Harrison /**
583bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
584bdf4bbaaSHarvey Harrison  * @val: current value
585bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
586bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
587bdf4bbaaSHarvey Harrison  *
588bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
589bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
590bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
591bdf4bbaaSHarvey Harrison  * integer type.
592bdf4bbaaSHarvey Harrison  */
593bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({		\
594bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
595bdf4bbaaSHarvey Harrison 	typeof(val) __min = (min);		\
596bdf4bbaaSHarvey Harrison 	typeof(val) __max = (max);		\
597bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
598bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
5991da177e4SLinus Torvalds 
60091f68b73SWu Fengguang 
60191f68b73SWu Fengguang /*
60291f68b73SWu Fengguang  * swap - swap value of @a and @b
60391f68b73SWu Fengguang  */
604ac7b9004SPeter Zijlstra #define swap(a, b) \
605ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
60691f68b73SWu Fengguang 
6071da177e4SLinus Torvalds /**
6081da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
6091da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
6101da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
6111da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
6121da177e4SLinus Torvalds  *
6131da177e4SLinus Torvalds  */
6141da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
6151da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
6161da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
6171da177e4SLinus Torvalds 
618d4d23addSKyle McMartin struct sysinfo;
619d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info);
620d4d23addSKyle McMartin 
6211da177e4SLinus Torvalds #endif /* __KERNEL__ */
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds #define SI_LOAD_SHIFT	16
6241da177e4SLinus Torvalds struct sysinfo {
6251da177e4SLinus Torvalds 	long uptime;			/* Seconds since boot */
6261da177e4SLinus Torvalds 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
6271da177e4SLinus Torvalds 	unsigned long totalram;		/* Total usable main memory size */
6281da177e4SLinus Torvalds 	unsigned long freeram;		/* Available memory size */
6291da177e4SLinus Torvalds 	unsigned long sharedram;	/* Amount of shared memory */
6301da177e4SLinus Torvalds 	unsigned long bufferram;	/* Memory used by buffers */
6311da177e4SLinus Torvalds 	unsigned long totalswap;	/* Total swap space size */
6321da177e4SLinus Torvalds 	unsigned long freeswap;		/* swap space still available */
6331da177e4SLinus Torvalds 	unsigned short procs;		/* Number of current processes */
6341da177e4SLinus Torvalds 	unsigned short pad;		/* explicit padding for m68k */
6351da177e4SLinus Torvalds 	unsigned long totalhigh;	/* Total high memory size */
6361da177e4SLinus Torvalds 	unsigned long freehigh;		/* Available high memory size */
6371da177e4SLinus Torvalds 	unsigned int mem_unit;		/* Memory unit size in bytes */
6381da177e4SLinus Torvalds 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
6391da177e4SLinus Torvalds };
6401da177e4SLinus Torvalds 
641cc8ef6ebSRoland Dreier /* Force a compilation error if a constant expression is not a power of 2 */
642cc8ef6ebSRoland Dreier #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
643cc8ef6ebSRoland Dreier 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
644cc8ef6ebSRoland Dreier 
6454552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a
6464552d5dcSJan Beulich    result (of value 0 and type size_t), so the expression can be used
6474552d5dcSJan Beulich    e.g. in a structure initializer (or where-ever else comma expressions
6484552d5dcSJan Beulich    aren't permitted). */
6498c87df45SJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
6508c87df45SJan Beulich #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
6514552d5dcSJan Beulich 
6527ef88ad5SRusty Russell /**
6537ef88ad5SRusty Russell  * BUILD_BUG_ON - break compile if a condition is true.
654ffbbf2daSRandy Dunlap  * @condition: the condition which the compiler should know is false.
6557ef88ad5SRusty Russell  *
6567ef88ad5SRusty Russell  * If you have some code which relies on certain constants being equal, or
6577ef88ad5SRusty Russell  * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
6587ef88ad5SRusty Russell  * detect if someone changes it.
6597ef88ad5SRusty Russell  *
6607ef88ad5SRusty Russell  * The implementation uses gcc's reluctance to create a negative array, but
6617ef88ad5SRusty Russell  * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
6627ef88ad5SRusty Russell  * to inline functions).  So as a fallback we use the optimizer; if it can't
6637ef88ad5SRusty Russell  * prove the condition is false, it will cause a link error on the undefined
6647ef88ad5SRusty Russell  * "__build_bug_on_failed".  This error message can be harder to track down
6657ef88ad5SRusty Russell  * though, hence the two different methods.
6667ef88ad5SRusty Russell  */
6677ef88ad5SRusty Russell #ifndef __OPTIMIZE__
6687ef88ad5SRusty Russell #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
6697ef88ad5SRusty Russell #else
6707ef88ad5SRusty Russell extern int __build_bug_on_failed;
6717ef88ad5SRusty Russell #define BUILD_BUG_ON(condition)					\
6727ef88ad5SRusty Russell 	do {							\
6737ef88ad5SRusty Russell 		((void)sizeof(char[1 - 2*!!(condition)]));	\
6747ef88ad5SRusty Russell 		if (condition) __build_bug_on_failed = 1;	\
6757ef88ad5SRusty Russell 	} while(0)
6767ef88ad5SRusty Russell #endif
6777ef88ad5SRusty Russell 
6781da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */
6791da177e4SLinus Torvalds #define __FUNCTION__ (__func__)
6801da177e4SLinus Torvalds 
68108e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */
68208e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA
68308e0f6a9SChristoph Lameter #define NUMA_BUILD 1
68408e0f6a9SChristoph Lameter #else
68508e0f6a9SChristoph Lameter #define NUMA_BUILD 0
68608e0f6a9SChristoph Lameter #endif
68708e0f6a9SChristoph Lameter 
6883e7d3449SMel Gorman /* This helps us avoid #ifdef CONFIG_COMPACTION */
6893e7d3449SMel Gorman #ifdef CONFIG_COMPACTION
6903e7d3449SMel Gorman #define COMPACTION_BUILD 1
6913e7d3449SMel Gorman #else
6923e7d3449SMel Gorman #define COMPACTION_BUILD 0
6933e7d3449SMel Gorman #endif
6943e7d3449SMel Gorman 
69529e71abfSSteven Rostedt /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
69629e71abfSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
69729e71abfSSteven Rostedt # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
69829e71abfSSteven Rostedt #endif
69929e71abfSSteven Rostedt 
7001da177e4SLinus Torvalds #endif
701