xref: /linux-6.15/include/linux/kernel.h (revision b28efd54)
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>
20e9d376f0SJason Baron #include <linux/dynamic_debug.h>
211da177e4SLinus Torvalds #include <asm/byteorder.h>
221da177e4SLinus Torvalds #include <asm/bug.h>
231da177e4SLinus Torvalds 
243eb3c740SRoman Zippel extern const char linux_banner[];
253eb3c740SRoman Zippel extern const char linux_proc_banner[];
263eb3c740SRoman Zippel 
274be929beSAlexey Dobriyan #define USHRT_MAX	((u16)(~0U))
284be929beSAlexey Dobriyan #define SHRT_MAX	((s16)(USHRT_MAX>>1))
294be929beSAlexey Dobriyan #define SHRT_MIN	((s16)(-SHRT_MAX - 1))
301da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
311da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
321da177e4SLinus Torvalds #define UINT_MAX	(~0U)
331da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
341da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
351da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
36111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
37111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
38111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
411da177e4SLinus Torvalds 
42a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
439f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
44a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
45f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
462ea58144SLinus Torvalds 
47c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
48c5e631cfSRusty Russell 
499b3be9f9SYinghai Lu /*
509b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
519b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
529b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
539b3be9f9SYinghai Lu  * arguments just once each.
549b3be9f9SYinghai Lu  */
559b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
569b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
579b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
589b3be9f9SYinghai Lu 
594552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
60930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
61*b28efd54SEric Paris #define roundup(x, y) (					\
62*b28efd54SEric Paris {							\
63*b28efd54SEric Paris 	typeof(y) __y = y;				\
64*b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
65*b28efd54SEric Paris }							\
66*b28efd54SEric 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 #define	KERN_EMERG	"<0>"	/* system is unusable			*/
1141da177e4SLinus Torvalds #define	KERN_ALERT	"<1>"	/* action must be taken immediately	*/
1151da177e4SLinus Torvalds #define	KERN_CRIT	"<2>"	/* critical conditions			*/
1161da177e4SLinus Torvalds #define	KERN_ERR	"<3>"	/* error conditions			*/
1171da177e4SLinus Torvalds #define	KERN_WARNING	"<4>"	/* warning conditions			*/
1181da177e4SLinus Torvalds #define	KERN_NOTICE	"<5>"	/* normal but significant condition	*/
1191da177e4SLinus Torvalds #define	KERN_INFO	"<6>"	/* informational			*/
1201da177e4SLinus Torvalds #define	KERN_DEBUG	"<7>"	/* debug-level messages			*/
1211da177e4SLinus Torvalds 
122e28d7137SLinus Torvalds /* Use the default kernel loglevel */
123e28d7137SLinus Torvalds #define KERN_DEFAULT	"<d>"
12447492527SIngo Molnar /*
12547492527SIngo Molnar  * Annotation for a "continued" line of log printout (only done after a
12647492527SIngo Molnar  * line that had no enclosing \n). Only to be used by core/arch code
12747492527SIngo Molnar  * during early bootup (a continued line is not SMP-safe otherwise).
12847492527SIngo Molnar  */
1295fd29d6cSLinus Torvalds #define	KERN_CONT	"<c>"
13047492527SIngo Molnar 
1311da177e4SLinus Torvalds extern int console_printk[];
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #define console_loglevel (console_printk[0])
1341da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1])
1351da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2])
1361da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3])
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds struct completion;
139df2e71fbS[email protected] struct pt_regs;
140df2e71fbS[email protected] struct user;
1411da177e4SLinus Torvalds 
142070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
143070cb065SUwe Kleine-König extern int _cond_resched(void);
144070cb065SUwe Kleine-König # define might_resched() _cond_resched()
145070cb065SUwe Kleine-König #else
146070cb065SUwe Kleine-König # define might_resched() do { } while (0)
147070cb065SUwe Kleine-König #endif
148070cb065SUwe Kleine-König 
1497106a27bSRandy Dunlap #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
150d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
1511da177e4SLinus Torvalds /**
1521da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1551da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1561da177e4SLinus Torvalds  *
1571da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
158e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1591da177e4SLinus Torvalds  * supposed to.
1601da177e4SLinus Torvalds  */
161f8cbd99bSIngo Molnar # define might_sleep() \
162e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
163f8cbd99bSIngo Molnar #else
164d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
165d894837fSSimon Kagstrom 				   int preempt_offset) { }
166f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
167f8cbd99bSIngo Molnar #endif
168f8cbd99bSIngo Molnar 
169368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
170f8cbd99bSIngo Molnar 
1711da177e4SLinus Torvalds #define abs(x) ({				\
172a49c59c0SRolf Eike Beer 		long __x = (x);			\
1731da177e4SLinus Torvalds 		(__x < 0) ? -__x : __x;		\
1741da177e4SLinus Torvalds 	})
1751da177e4SLinus Torvalds 
1763ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
1773ee1afa3SNick Piggin void might_fault(void);
1783ee1afa3SNick Piggin #else
1793ee1afa3SNick Piggin static inline void might_fault(void)
1803ee1afa3SNick Piggin {
1813ee1afa3SNick Piggin 	might_sleep();
1823ee1afa3SNick Piggin }
1833ee1afa3SNick Piggin #endif
1843ee1afa3SNick Piggin 
1857db6f5fbSJoe Perches struct va_format {
1867db6f5fbSJoe Perches 	const char *fmt;
1877db6f5fbSJoe Perches 	va_list *va;
1887db6f5fbSJoe Perches };
1897db6f5fbSJoe Perches 
190e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
191c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
1921da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...)
193a586df06SAndi Kleen 	__attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
194dd287796SAndrew Morton extern void oops_enter(void);
195dd287796SAndrew Morton extern void oops_exit(void);
196863a6049SAnton Blanchard void print_oops_end_marker(void);
197dd287796SAndrew Morton extern int oops_may_print(void);
198ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code)
1991da177e4SLinus Torvalds 	ATTRIB_NORET;
2001da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long)
2011da177e4SLinus Torvalds 	ATTRIB_NORET;
2021da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
2031da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
2041da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
2051da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
20606b2a76dSYi Yang extern int strict_strtoul(const char *, unsigned int, unsigned long *);
20706b2a76dSYi Yang extern int strict_strtol(const char *, unsigned int, long *);
20806b2a76dSYi Yang extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
20906b2a76dSYi Yang extern int strict_strtoll(const char *, unsigned int, long long *);
2101da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...)
2111da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 3)));
2121da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list)
2131da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 0)));
2141da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...)
2151da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
2161da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2171da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
2181da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
2191da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
2201da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2211da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
222e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
223e905914fSJeremy Fitzhardinge 	__attribute__ ((format (printf, 2, 3)));
22411443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...)
2271da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 3)));
2281da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list)
2291da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 0)));
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
2321da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
233d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
2341da177e4SLinus Torvalds 
2355e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
2361da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
2371da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
238ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
239ab7476cfSArjan van de Ven 
24004a2e6a5SEric W. Biederman struct pid;
24104a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp);
2421da177e4SLinus Torvalds 
243a0ad05c7SThomas Renninger /*
244a0ad05c7SThomas Renninger  * FW_BUG
245a0ad05c7SThomas Renninger  * Add this to a message where you are sure the firmware is buggy or behaves
246a0ad05c7SThomas Renninger  * really stupid or out of spec. Be aware that the responsible BIOS developer
247a0ad05c7SThomas Renninger  * should be able to fix this issue or at least get a concrete idea of the
248a0ad05c7SThomas Renninger  * problem by reading your message without the need of looking at the kernel
249a0ad05c7SThomas Renninger  * code.
250a0ad05c7SThomas Renninger  *
251a0ad05c7SThomas Renninger  * Use it for definite and high priority BIOS bugs.
252a0ad05c7SThomas Renninger  *
253a0ad05c7SThomas Renninger  * FW_WARN
254a0ad05c7SThomas Renninger  * Use it for not that clear (e.g. could the kernel messed up things already?)
255a0ad05c7SThomas Renninger  * and medium priority BIOS bugs.
256a0ad05c7SThomas Renninger  *
257a0ad05c7SThomas Renninger  * FW_INFO
258a0ad05c7SThomas Renninger  * Use this one if you want to tell the user or vendor about something
259a0ad05c7SThomas Renninger  * suspicious, but generally harmless related to the firmware.
260a0ad05c7SThomas Renninger  *
261a0ad05c7SThomas Renninger  * Use it for information or very low priority BIOS bugs.
262a0ad05c7SThomas Renninger  */
263a0ad05c7SThomas Renninger #define FW_BUG		"[Firmware Bug]: "
264a0ad05c7SThomas Renninger #define FW_WARN		"[Firmware Warn]: "
265a0ad05c7SThomas Renninger #define FW_INFO		"[Firmware Info]: "
266a0ad05c7SThomas Renninger 
267c6de9f08SHuang Ying /*
268c6de9f08SHuang Ying  * HW_ERR
269c6de9f08SHuang Ying  * Add this to a message for hardware errors, so that user can report
270c6de9f08SHuang Ying  * it to hardware vendor instead of LKML or software vendor.
271c6de9f08SHuang Ying  */
272c6de9f08SHuang Ying #define HW_ERR		"[Hardware Error]: "
273c6de9f08SHuang Ying 
274d59745ceSMatt Mackall #ifdef CONFIG_PRINTK
2751da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args)
2761da177e4SLinus Torvalds 	__attribute__ ((format (printf, 1, 0)));
2771da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...)
278a586df06SAndi Kleen 	__attribute__ ((format (printf, 1, 2))) __cold;
2797ef3d2fdSJoe Perches 
2805c828713SChristian Borntraeger extern int __printk_ratelimit(const char *func);
2815c828713SChristian Borntraeger #define printk_ratelimit() __printk_ratelimit(__func__)
2827ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2837ef3d2fdSJoe Perches 				   unsigned int interval_msec);
284f036be96SIngo Molnar 
285af91322eSDave Young extern int printk_delay_msec;
286af91322eSDave Young 
287f036be96SIngo Molnar /*
288f036be96SIngo Molnar  * Print a one-time message (analogous to WARN_ONCE() et al):
289f036be96SIngo Molnar  */
290f036be96SIngo Molnar #define printk_once(x...) ({			\
2910b2749aaSJoe Perches 	static bool __print_once;		\
292f036be96SIngo Molnar 						\
2930b2749aaSJoe Perches 	if (!__print_once) {			\
2940b2749aaSJoe Perches 		__print_once = true;		\
295f036be96SIngo Molnar 		printk(x);			\
296f036be96SIngo Molnar 	}					\
297f036be96SIngo Molnar })
298f036be96SIngo Molnar 
29904d491abSNeil Horman void log_buf_kexec_setup(void);
300d59745ceSMatt Mackall #else
301d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args)
302d59745ceSMatt Mackall 	__attribute__ ((format (printf, 1, 0)));
303d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; }
304d59745ceSMatt Mackall static inline int printk(const char *s, ...)
305d59745ceSMatt Mackall 	__attribute__ ((format (printf, 1, 2)));
306a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; }
3077ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; }
3087ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
3097ef3d2fdSJoe Perches 					  unsigned int interval_msec)	\
3107ef3d2fdSJoe Perches 		{ return false; }
311f036be96SIngo Molnar 
312f036be96SIngo Molnar /* No effect, but we still get type checking even in the !PRINTK case: */
313f036be96SIngo Molnar #define printk_once(x...) printk(x)
314f036be96SIngo Molnar 
31504d491abSNeil Horman static inline void log_buf_kexec_setup(void)
31604d491abSNeil Horman {
31704d491abSNeil Horman }
318d59745ceSMatt Mackall #endif
3191da177e4SLinus Torvalds 
32012fdff3fSDavid Howells /*
32112fdff3fSDavid Howells  * Dummy printk for disabled debugging statements to use whilst maintaining
32212fdff3fSDavid Howells  * gcc's format and side-effect checking.
32312fdff3fSDavid Howells  */
32412fdff3fSDavid Howells static inline __attribute__ ((format (printf, 1, 2)))
32512fdff3fSDavid Howells int no_printk(const char *s, ...) { return 0; }
32612fdff3fSDavid Howells 
327ced9cd40SIngo Molnar extern int printk_needs_cpu(int cpu);
328ced9cd40SIngo Molnar extern void printk_tick(void);
329ced9cd40SIngo Molnar 
330e17ba73bSJiri Slaby extern void asmlinkage __attribute__((format(printf, 1, 2)))
331076f9776SIngo Molnar 	early_printk(const char *fmt, ...);
332076f9776SIngo Molnar 
3331da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds static inline void console_silent(void)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	console_loglevel = 0;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds static inline void console_verbose(void)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	if (console_loglevel)
3431da177e4SLinus Torvalds 		console_loglevel = 15;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
347e3e8a75dSKirill Korotaev extern void wake_up_klogd(void);
3481da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
349aa727107SAdrian Bunk extern int panic_timeout;
3501da177e4SLinus Torvalds extern int panic_on_oops;
3518da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
3525211a242SKurt Garloff extern int panic_on_io_nmi;
3531da177e4SLinus Torvalds extern const char *print_tainted(void);
35425ddbb18SAndi Kleen extern void add_taint(unsigned flag);
35525ddbb18SAndi Kleen extern int test_taint(unsigned flag);
35625ddbb18SAndi Kleen extern unsigned long get_taint(void);
357b920de1bSDavid Howells extern int root_mountflags;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /* Values used for system_state */
3601da177e4SLinus Torvalds extern enum system_states {
3611da177e4SLinus Torvalds 	SYSTEM_BOOTING,
3621da177e4SLinus Torvalds 	SYSTEM_RUNNING,
3631da177e4SLinus Torvalds 	SYSTEM_HALT,
3641da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
3651da177e4SLinus Torvalds 	SYSTEM_RESTART,
366729b4d4cSAlexey Starikovskiy 	SYSTEM_SUSPEND_DISK,
3671da177e4SLinus Torvalds } system_state;
3681da177e4SLinus Torvalds 
36925ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
37025ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
37125ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP		2
37225ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
37325ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
37425ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
37525ddbb18SAndi Kleen #define TAINT_USER			6
37625ddbb18SAndi Kleen #define TAINT_DIE			7
37725ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
37825ddbb18SAndi Kleen #define TAINT_WARN			9
37926e9a397SLinus Torvalds #define TAINT_CRAP			10
38092946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
3811da177e4SLinus Torvalds 
382a586df06SAndi Kleen extern void dump_stack(void) __cold;
3831da177e4SLinus Torvalds 
38499eaf3c4SRandy Dunlap enum {
38599eaf3c4SRandy Dunlap 	DUMP_PREFIX_NONE,
38699eaf3c4SRandy Dunlap 	DUMP_PREFIX_ADDRESS,
38799eaf3c4SRandy Dunlap 	DUMP_PREFIX_OFFSET
38899eaf3c4SRandy Dunlap };
389c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len,
390c7909234SRandy Dunlap 				int rowsize, int groupsize,
391c7909234SRandy Dunlap 				char *linebuf, size_t linebuflen, bool ascii);
392c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str,
393c7909234SRandy Dunlap 				int prefix_type, int rowsize, int groupsize,
3946a0ed91eSArtem Bityutskiy 				const void *buf, size_t len, bool ascii);
395c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
396eb9a9a56SAlan Stern 			const void *buf, size_t len);
3973fc95772SHarvey Harrison 
3983fc95772SHarvey Harrison extern const char hex_asc[];
3993fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
4003fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
4013fc95772SHarvey Harrison 
4023fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte)
4033fc95772SHarvey Harrison {
4043fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
4053fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
4063fc95772SHarvey Harrison 	return buf;
4073fc95772SHarvey Harrison }
40899eaf3c4SRandy Dunlap 
40990378889SAndy Shevchenko extern int hex_to_bin(char ch);
41090378889SAndy Shevchenko 
411d091c2f5SMartin Schwidefsky #ifndef pr_fmt
412d091c2f5SMartin Schwidefsky #define pr_fmt(fmt) fmt
413d091c2f5SMartin Schwidefsky #endif
414d091c2f5SMartin Schwidefsky 
415d091c2f5SMartin Schwidefsky #define pr_emerg(fmt, ...) \
416d091c2f5SMartin Schwidefsky         printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
417d091c2f5SMartin Schwidefsky #define pr_alert(fmt, ...) \
418d091c2f5SMartin Schwidefsky         printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
419d091c2f5SMartin Schwidefsky #define pr_crit(fmt, ...) \
420d091c2f5SMartin Schwidefsky         printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
421d091c2f5SMartin Schwidefsky #define pr_err(fmt, ...) \
422d091c2f5SMartin Schwidefsky         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
423d091c2f5SMartin Schwidefsky #define pr_warning(fmt, ...) \
424d091c2f5SMartin Schwidefsky         printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
425fc62f2f1SJoe Perches #define pr_warn pr_warning
426d091c2f5SMartin Schwidefsky #define pr_notice(fmt, ...) \
427d091c2f5SMartin Schwidefsky         printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
428d091c2f5SMartin Schwidefsky #define pr_info(fmt, ...) \
429d091c2f5SMartin Schwidefsky         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
430311d0761SCyrill Gorcunov #define pr_cont(fmt, ...) \
431311d0761SCyrill Gorcunov 	printk(KERN_CONT fmt, ##__VA_ARGS__)
4321f7c8234SEmil Medve 
4334ccb4579SMichael Ellerman /* pr_devel() should produce zero code unless DEBUG is defined */
4344ccb4579SMichael Ellerman #ifdef DEBUG
4354ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \
4364ccb4579SMichael Ellerman 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
4374ccb4579SMichael Ellerman #else
4384ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \
4394ccb4579SMichael Ellerman 	({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
4404ccb4579SMichael Ellerman #endif
4414ccb4579SMichael Ellerman 
4421cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */
443d0d85ff9SCornelia Huck #if defined(DEBUG)
444d0d85ff9SCornelia Huck #define pr_debug(fmt, ...) \
445d0d85ff9SCornelia Huck 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
446e9d376f0SJason Baron #elif defined(CONFIG_DYNAMIC_DEBUG)
447e6e66b02SGreg Banks /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
44800b55864SJoe Perches #define pr_debug(fmt, ...) \
44900b55864SJoe Perches 	dynamic_pr_debug(fmt, ##__VA_ARGS__)
4501da177e4SLinus Torvalds #else
451d091c2f5SMartin Schwidefsky #define pr_debug(fmt, ...) \
452d091c2f5SMartin Schwidefsky 	({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
4531da177e4SLinus Torvalds #endif
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /*
4568a64f336SJoe Perches  * ratelimited messages with local ratelimit_state,
4578a64f336SJoe Perches  * no local ratelimit_state used in the !PRINTK case
4588a64f336SJoe Perches  */
4598a64f336SJoe Perches #ifdef CONFIG_PRINTK
4608a64f336SJoe Perches #define printk_ratelimited(fmt, ...)  ({				\
461d8521fccSOGAWA Hirofumi 	static DEFINE_RATELIMIT_STATE(_rs,				\
462d8521fccSOGAWA Hirofumi 				      DEFAULT_RATELIMIT_INTERVAL,	\
463d8521fccSOGAWA Hirofumi 				      DEFAULT_RATELIMIT_BURST);		\
4648a64f336SJoe Perches 									\
465bb1dc0baSYong Zhang 	if (__ratelimit(&_rs))						\
4668a64f336SJoe Perches 		printk(fmt, ##__VA_ARGS__);				\
4678a64f336SJoe Perches })
4688a64f336SJoe Perches #else
4698a64f336SJoe Perches /* No effect, but we still get type checking even in the !PRINTK case: */
4708a64f336SJoe Perches #define printk_ratelimited printk
4718a64f336SJoe Perches #endif
4728a64f336SJoe Perches 
4738a64f336SJoe Perches #define pr_emerg_ratelimited(fmt, ...) \
4748a64f336SJoe Perches 	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
4758a64f336SJoe Perches #define pr_alert_ratelimited(fmt, ...) \
4768a64f336SJoe Perches 	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
4778a64f336SJoe Perches #define pr_crit_ratelimited(fmt, ...) \
4788a64f336SJoe Perches 	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
4798a64f336SJoe Perches #define pr_err_ratelimited(fmt, ...) \
4808a64f336SJoe Perches 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
4818a64f336SJoe Perches #define pr_warning_ratelimited(fmt, ...) \
4828a64f336SJoe Perches 	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
483fc62f2f1SJoe Perches #define pr_warn_ratelimited pr_warning_ratelimited
4848a64f336SJoe Perches #define pr_notice_ratelimited(fmt, ...) \
4858a64f336SJoe Perches 	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
4868a64f336SJoe Perches #define pr_info_ratelimited(fmt, ...) \
4878a64f336SJoe Perches 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
4888a64f336SJoe Perches /* no pr_cont_ratelimited, don't do that... */
4898a64f336SJoe Perches /* If you are writing a driver, please use dev_dbg instead */
4908a64f336SJoe Perches #if defined(DEBUG)
4918a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \
4928a64f336SJoe Perches 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
4938a64f336SJoe Perches #else
4948a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \
4958a64f336SJoe Perches 	({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \
4968a64f336SJoe Perches 				     ##__VA_ARGS__); 0; })
4978a64f336SJoe Perches #endif
4988a64f336SJoe Perches 
4998a64f336SJoe Perches /*
500526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
5012002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
5022002c258SSteven Rostedt  *
5032002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
5042002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
505156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
5062002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
5072002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
5082002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
5092002c258SSteven Rostedt  * to continue tracing.
5102002c258SSteven Rostedt  *
5112002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
5122002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
5132002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
5142002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
5152002c258SSteven Rostedt  *
5162002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
517526211bcSIngo Molnar  */
5182002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER
5192002c258SSteven Rostedt void tracing_on(void);
5202002c258SSteven Rostedt void tracing_off(void);
5212002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */
5222002c258SSteven Rostedt void tracing_off_permanent(void);
5232002c258SSteven Rostedt int tracing_is_on(void);
5242002c258SSteven Rostedt #else
5252002c258SSteven Rostedt static inline void tracing_on(void) { }
5262002c258SSteven Rostedt static inline void tracing_off(void) { }
5272002c258SSteven Rostedt static inline void tracing_off_permanent(void) { }
5282002c258SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
5292002c258SSteven Rostedt #endif
530cecbca96SFrederic Weisbecker 
531cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
532cecbca96SFrederic Weisbecker 	DUMP_NONE,
533cecbca96SFrederic Weisbecker 	DUMP_ALL,
534cecbca96SFrederic Weisbecker 	DUMP_ORIG,
535cecbca96SFrederic Weisbecker };
536cecbca96SFrederic Weisbecker 
537526211bcSIngo Molnar #ifdef CONFIG_TRACING
538526211bcSIngo Molnar extern void tracing_start(void);
539526211bcSIngo Molnar extern void tracing_stop(void);
540526211bcSIngo Molnar extern void ftrace_off_permanent(void);
541526211bcSIngo Molnar 
542769b0441SFrederic Weisbecker static inline void __attribute__ ((format (printf, 1, 2)))
543769b0441SFrederic Weisbecker ____trace_printk_check_format(const char *fmt, ...)
544769b0441SFrederic Weisbecker {
545769b0441SFrederic Weisbecker }
546769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
547769b0441SFrederic Weisbecker do {									\
548769b0441SFrederic Weisbecker 	if (0)								\
549769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
550769b0441SFrederic Weisbecker } while (0)
551769b0441SFrederic Weisbecker 
552526211bcSIngo Molnar /**
553526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
554526211bcSIngo Molnar  * @fmt: the printf format for printing
555526211bcSIngo Molnar  *
556526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
557526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
558526211bcSIngo Molnar  *
559526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
560526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
561526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
562526211bcSIngo Molnar  * where problems are occurring.
563526211bcSIngo Molnar  *
564526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
565526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
566526211bcSIngo Molnar  * your code.
567526211bcSIngo Molnar  */
568769b0441SFrederic Weisbecker 
569769b0441SFrederic Weisbecker #define trace_printk(fmt, args...)					\
570769b0441SFrederic Weisbecker do {									\
571769b0441SFrederic Weisbecker 	__trace_printk_check_format(fmt, ##args);			\
57248ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
57348ead020SFrederic Weisbecker 		static const char *trace_printk_fmt			\
57448ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
57548ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
57648ead020SFrederic Weisbecker 									\
57748ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
57848ead020SFrederic Weisbecker 	} else								\
57948ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);		\
580769b0441SFrederic Weisbecker } while (0)
581769b0441SFrederic Weisbecker 
582526211bcSIngo Molnar extern int
58348ead020SFrederic Weisbecker __trace_bprintk(unsigned long ip, const char *fmt, ...)
58448ead020SFrederic Weisbecker 	__attribute__ ((format (printf, 2, 3)));
58548ead020SFrederic Weisbecker 
58648ead020SFrederic Weisbecker extern int
587526211bcSIngo Molnar __trace_printk(unsigned long ip, const char *fmt, ...)
588526211bcSIngo Molnar 	__attribute__ ((format (printf, 2, 3)));
589769b0441SFrederic Weisbecker 
59003889384SSteven Rostedt extern void trace_dump_stack(void);
59103889384SSteven Rostedt 
59248ead020SFrederic Weisbecker /*
59348ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
59448ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
59548ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
59648ead020SFrederic Weisbecker  */
597769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
598769b0441SFrederic Weisbecker do {									\
59948ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
600769b0441SFrederic Weisbecker 		static const char *trace_printk_fmt			\
60148ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
60248ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
6037bffc23eSIngo Molnar 									\
60448ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
60548ead020SFrederic Weisbecker 	} else								\
60648ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
607769b0441SFrederic Weisbecker } while (0)
608769b0441SFrederic Weisbecker 
609526211bcSIngo Molnar extern int
61048ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
61148ead020SFrederic Weisbecker 
61248ead020SFrederic Weisbecker extern int
613526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
614769b0441SFrederic Weisbecker 
615cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
616526211bcSIngo Molnar #else
617526211bcSIngo Molnar static inline int
618526211bcSIngo Molnar trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
619526211bcSIngo Molnar 
620526211bcSIngo Molnar static inline void tracing_start(void) { }
621526211bcSIngo Molnar static inline void tracing_stop(void) { }
622526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { }
623e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { }
624526211bcSIngo Molnar static inline int
625526211bcSIngo Molnar trace_printk(const char *fmt, ...)
626526211bcSIngo Molnar {
627526211bcSIngo Molnar 	return 0;
628526211bcSIngo Molnar }
629526211bcSIngo Molnar static inline int
630526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
631526211bcSIngo Molnar {
632526211bcSIngo Molnar 	return 0;
633526211bcSIngo Molnar }
634cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
635769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
636526211bcSIngo Molnar 
637526211bcSIngo Molnar /*
638bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
6391da177e4SLinus Torvalds  * strict type-checking.. See the
6401da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
6411da177e4SLinus Torvalds  */
6421da177e4SLinus Torvalds #define min(x, y) ({				\
643bdf4bbaaSHarvey Harrison 	typeof(x) _min1 = (x);			\
644bdf4bbaaSHarvey Harrison 	typeof(y) _min2 = (y);			\
645bdf4bbaaSHarvey Harrison 	(void) (&_min1 == &_min2);		\
646bdf4bbaaSHarvey Harrison 	_min1 < _min2 ? _min1 : _min2; })
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds #define max(x, y) ({				\
649bdf4bbaaSHarvey Harrison 	typeof(x) _max1 = (x);			\
650bdf4bbaaSHarvey Harrison 	typeof(y) _max2 = (y);			\
651bdf4bbaaSHarvey Harrison 	(void) (&_max1 == &_max2);		\
652bdf4bbaaSHarvey Harrison 	_max1 > _max2 ? _max1 : _max2; })
653bdf4bbaaSHarvey Harrison 
654bdf4bbaaSHarvey Harrison /**
655bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
656bdf4bbaaSHarvey Harrison  * @val: current value
657bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
658bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
659bdf4bbaaSHarvey Harrison  *
660bdf4bbaaSHarvey Harrison  * This macro does strict typechecking of min/max to make sure they are of the
661bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
662bdf4bbaaSHarvey Harrison  */
663bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({			\
664bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
665bdf4bbaaSHarvey Harrison 	typeof(min) __min = (min);		\
666bdf4bbaaSHarvey Harrison 	typeof(max) __max = (max);		\
667bdf4bbaaSHarvey Harrison 	(void) (&__val == &__min);		\
668bdf4bbaaSHarvey Harrison 	(void) (&__val == &__max);		\
669bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
670bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds /*
6731da177e4SLinus Torvalds  * ..and if you can't take the strict
6741da177e4SLinus Torvalds  * types, you can specify one yourself.
6751da177e4SLinus Torvalds  *
676bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
6771da177e4SLinus Torvalds  */
678bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({			\
679bdf4bbaaSHarvey Harrison 	type __min1 = (x);			\
680bdf4bbaaSHarvey Harrison 	type __min2 = (y);			\
681bdf4bbaaSHarvey Harrison 	__min1 < __min2 ? __min1: __min2; })
6821da177e4SLinus Torvalds 
683bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({			\
684bdf4bbaaSHarvey Harrison 	type __max1 = (x);			\
685bdf4bbaaSHarvey Harrison 	type __max2 = (y);			\
686bdf4bbaaSHarvey Harrison 	__max1 > __max2 ? __max1: __max2; })
687bdf4bbaaSHarvey Harrison 
688bdf4bbaaSHarvey Harrison /**
689bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
690bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
691bdf4bbaaSHarvey Harrison  * @val: current value
692bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
693bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
694bdf4bbaaSHarvey Harrison  *
695bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
696bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
697bdf4bbaaSHarvey Harrison  */
698bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({		\
699bdf4bbaaSHarvey Harrison 	type __val = (val);			\
700bdf4bbaaSHarvey Harrison 	type __min = (min);			\
701bdf4bbaaSHarvey Harrison 	type __max = (max);			\
702bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
703bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
704bdf4bbaaSHarvey Harrison 
705bdf4bbaaSHarvey Harrison /**
706bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
707bdf4bbaaSHarvey Harrison  * @val: current value
708bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
709bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
710bdf4bbaaSHarvey Harrison  *
711bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
712bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
713bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
714bdf4bbaaSHarvey Harrison  * integer type.
715bdf4bbaaSHarvey Harrison  */
716bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({		\
717bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
718bdf4bbaaSHarvey Harrison 	typeof(val) __min = (min);		\
719bdf4bbaaSHarvey Harrison 	typeof(val) __max = (max);		\
720bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
721bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
7221da177e4SLinus Torvalds 
72391f68b73SWu Fengguang 
72491f68b73SWu Fengguang /*
72591f68b73SWu Fengguang  * swap - swap value of @a and @b
72691f68b73SWu Fengguang  */
727ac7b9004SPeter Zijlstra #define swap(a, b) \
728ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
72991f68b73SWu Fengguang 
7301da177e4SLinus Torvalds /**
7311da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
7321da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
7331da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
7341da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
7351da177e4SLinus Torvalds  *
7361da177e4SLinus Torvalds  */
7371da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
7381da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
7391da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
7401da177e4SLinus Torvalds 
741d4d23addSKyle McMartin struct sysinfo;
742d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info);
743d4d23addSKyle McMartin 
7441da177e4SLinus Torvalds #endif /* __KERNEL__ */
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds #define SI_LOAD_SHIFT	16
7471da177e4SLinus Torvalds struct sysinfo {
7481da177e4SLinus Torvalds 	long uptime;			/* Seconds since boot */
7491da177e4SLinus Torvalds 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
7501da177e4SLinus Torvalds 	unsigned long totalram;		/* Total usable main memory size */
7511da177e4SLinus Torvalds 	unsigned long freeram;		/* Available memory size */
7521da177e4SLinus Torvalds 	unsigned long sharedram;	/* Amount of shared memory */
7531da177e4SLinus Torvalds 	unsigned long bufferram;	/* Memory used by buffers */
7541da177e4SLinus Torvalds 	unsigned long totalswap;	/* Total swap space size */
7551da177e4SLinus Torvalds 	unsigned long freeswap;		/* swap space still available */
7561da177e4SLinus Torvalds 	unsigned short procs;		/* Number of current processes */
7571da177e4SLinus Torvalds 	unsigned short pad;		/* explicit padding for m68k */
7581da177e4SLinus Torvalds 	unsigned long totalhigh;	/* Total high memory size */
7591da177e4SLinus Torvalds 	unsigned long freehigh;		/* Available high memory size */
7601da177e4SLinus Torvalds 	unsigned int mem_unit;		/* Memory unit size in bytes */
7611da177e4SLinus Torvalds 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
7621da177e4SLinus Torvalds };
7631da177e4SLinus Torvalds 
764c0398ee6SNikita Danilov /* Force a compilation error if condition is true */
7658c87df45SJan Beulich #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
7668c87df45SJan Beulich 
7678c87df45SJan Beulich /* Force a compilation error if condition is constant and true */
7688c87df45SJan Beulich #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
7691da177e4SLinus Torvalds 
770cc8ef6ebSRoland Dreier /* Force a compilation error if a constant expression is not a power of 2 */
771cc8ef6ebSRoland Dreier #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
772cc8ef6ebSRoland Dreier 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
773cc8ef6ebSRoland Dreier 
7744552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a
7754552d5dcSJan Beulich    result (of value 0 and type size_t), so the expression can be used
7764552d5dcSJan Beulich    e.g. in a structure initializer (or where-ever else comma expressions
7774552d5dcSJan Beulich    aren't permitted). */
7788c87df45SJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
7798c87df45SJan Beulich #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
7804552d5dcSJan Beulich 
7811da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */
7821da177e4SLinus Torvalds #define __FUNCTION__ (__func__)
7831da177e4SLinus Torvalds 
78408e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */
78508e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA
78608e0f6a9SChristoph Lameter #define NUMA_BUILD 1
78708e0f6a9SChristoph Lameter #else
78808e0f6a9SChristoph Lameter #define NUMA_BUILD 0
78908e0f6a9SChristoph Lameter #endif
79008e0f6a9SChristoph Lameter 
79129e71abfSSteven Rostedt /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
79229e71abfSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
79329e71abfSSteven Rostedt # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
79429e71abfSSteven Rostedt #endif
79529e71abfSSteven Rostedt 
7961da177e4SLinus Torvalds #endif
797