xref: /linux-6.15/include/linux/kernel.h (revision 074e61ec)
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*074e61ecSJames Morris 
62*074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
63b28efd54SEric Paris #define roundup(x, y) (					\
64b28efd54SEric Paris {							\
656070bf35STetsuo Handa 	const typeof(y) __y = y;			\
66b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
67b28efd54SEric Paris }							\
68b28efd54SEric Paris )
69686a0f3dSEric Paris #define rounddown(x, y) (				\
70686a0f3dSEric Paris {							\
71686a0f3dSEric Paris 	typeof(x) __x = (x);				\
72686a0f3dSEric Paris 	__x - (__x % (y));				\
73686a0f3dSEric Paris }							\
74686a0f3dSEric Paris )
759fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
769fe06081SDarrick J. Wong {							\
779fe06081SDarrick J. Wong 	typeof(divisor) __divisor = divisor;		\
789fe06081SDarrick J. Wong 	(((x) + ((__divisor) / 2)) / (__divisor));	\
799fe06081SDarrick J. Wong }							\
809fe06081SDarrick J. Wong )
811da177e4SLinus Torvalds 
82ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
83ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
84ca31e146SEduard - Gabriel Munteanu 
8590c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
862da96acdSJens Axboe # include <asm/div64.h>
872da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
882da96acdSJens Axboe #else
892da96acdSJens Axboe # define sector_div(n, b)( \
902da96acdSJens Axboe { \
912da96acdSJens Axboe 	int _res; \
922da96acdSJens Axboe 	_res = (n) % (b); \
932da96acdSJens Axboe 	(n) /= (b); \
942da96acdSJens Axboe 	_res; \
952da96acdSJens Axboe } \
962da96acdSJens Axboe )
972da96acdSJens Axboe #endif
982da96acdSJens Axboe 
99218e180eSAndrew Morton /**
100218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
101218e180eSAndrew Morton  * @n: the number we're accessing
102218e180eSAndrew Morton  *
103218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
104218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
105218e180eSAndrew Morton  * 32-bits.
106218e180eSAndrew Morton  */
107218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
108218e180eSAndrew Morton 
109204b885eSJoerg Roedel /**
110204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
111204b885eSJoerg Roedel  * @n: the number we're accessing
112204b885eSJoerg Roedel  */
113204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
114204b885eSJoerg Roedel 
1151da177e4SLinus Torvalds #define	KERN_EMERG	"<0>"	/* system is unusable			*/
1161da177e4SLinus Torvalds #define	KERN_ALERT	"<1>"	/* action must be taken immediately	*/
1171da177e4SLinus Torvalds #define	KERN_CRIT	"<2>"	/* critical conditions			*/
1181da177e4SLinus Torvalds #define	KERN_ERR	"<3>"	/* error conditions			*/
1191da177e4SLinus Torvalds #define	KERN_WARNING	"<4>"	/* warning conditions			*/
1201da177e4SLinus Torvalds #define	KERN_NOTICE	"<5>"	/* normal but significant condition	*/
1211da177e4SLinus Torvalds #define	KERN_INFO	"<6>"	/* informational			*/
1221da177e4SLinus Torvalds #define	KERN_DEBUG	"<7>"	/* debug-level messages			*/
1231da177e4SLinus Torvalds 
124e28d7137SLinus Torvalds /* Use the default kernel loglevel */
125e28d7137SLinus Torvalds #define KERN_DEFAULT	"<d>"
12647492527SIngo Molnar /*
12747492527SIngo Molnar  * Annotation for a "continued" line of log printout (only done after a
12847492527SIngo Molnar  * line that had no enclosing \n). Only to be used by core/arch code
12947492527SIngo Molnar  * during early bootup (a continued line is not SMP-safe otherwise).
13047492527SIngo Molnar  */
1315fd29d6cSLinus Torvalds #define	KERN_CONT	"<c>"
13247492527SIngo Molnar 
1331da177e4SLinus Torvalds extern int console_printk[];
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #define console_loglevel (console_printk[0])
1361da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1])
1371da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2])
1381da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3])
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds struct completion;
141df2e71fbS[email protected] struct pt_regs;
142df2e71fbS[email protected] struct user;
1431da177e4SLinus Torvalds 
144070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
145070cb065SUwe Kleine-König extern int _cond_resched(void);
146070cb065SUwe Kleine-König # define might_resched() _cond_resched()
147070cb065SUwe Kleine-König #else
148070cb065SUwe Kleine-König # define might_resched() do { } while (0)
149070cb065SUwe Kleine-König #endif
150070cb065SUwe Kleine-König 
1517106a27bSRandy Dunlap #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
152d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
1531da177e4SLinus Torvalds /**
1541da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1551da177e4SLinus Torvalds  *
1561da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1571da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1581da177e4SLinus Torvalds  *
1591da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
160e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1611da177e4SLinus Torvalds  * supposed to.
1621da177e4SLinus Torvalds  */
163f8cbd99bSIngo Molnar # define might_sleep() \
164e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
165f8cbd99bSIngo Molnar #else
166d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
167d894837fSSimon Kagstrom 				   int preempt_offset) { }
168f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
169f8cbd99bSIngo Molnar #endif
170f8cbd99bSIngo Molnar 
171368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
172f8cbd99bSIngo Molnar 
1731da177e4SLinus Torvalds #define abs(x) ({				\
174a49c59c0SRolf Eike Beer 		long __x = (x);			\
1751da177e4SLinus Torvalds 		(__x < 0) ? -__x : __x;		\
1761da177e4SLinus Torvalds 	})
1771da177e4SLinus Torvalds 
178658716d1SBrian Behlendorf #define abs64(x) ({				\
179658716d1SBrian Behlendorf 		s64 __x = (x);			\
180658716d1SBrian Behlendorf 		(__x < 0) ? -__x : __x;		\
181658716d1SBrian Behlendorf 	})
182658716d1SBrian Behlendorf 
1833ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
1843ee1afa3SNick Piggin void might_fault(void);
1853ee1afa3SNick Piggin #else
1863ee1afa3SNick Piggin static inline void might_fault(void)
1873ee1afa3SNick Piggin {
1883ee1afa3SNick Piggin 	might_sleep();
1893ee1afa3SNick Piggin }
1903ee1afa3SNick Piggin #endif
1913ee1afa3SNick Piggin 
1927db6f5fbSJoe Perches struct va_format {
1937db6f5fbSJoe Perches 	const char *fmt;
1947db6f5fbSJoe Perches 	va_list *va;
1957db6f5fbSJoe Perches };
1967db6f5fbSJoe Perches 
197e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
198c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
1991da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...)
200a586df06SAndi Kleen 	__attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
201dd287796SAndrew Morton extern void oops_enter(void);
202dd287796SAndrew Morton extern void oops_exit(void);
203863a6049SAnton Blanchard void print_oops_end_marker(void);
204dd287796SAndrew Morton extern int oops_may_print(void);
205ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code)
2061da177e4SLinus Torvalds 	ATTRIB_NORET;
2071da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long)
2081da177e4SLinus Torvalds 	ATTRIB_NORET;
2091da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
2101da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
2111da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
2121da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
213a55621f1SAndrew Morton extern int __must_check strict_strtoul(const char *, unsigned int, unsigned long *);
214a55621f1SAndrew Morton extern int __must_check strict_strtol(const char *, unsigned int, long *);
215a55621f1SAndrew Morton extern int __must_check strict_strtoull(const char *, unsigned int, unsigned long long *);
216a55621f1SAndrew Morton extern int __must_check strict_strtoll(const char *, unsigned int, long long *);
2171da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...)
2181da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 3)));
2191da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list)
2201da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 0)));
2211da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...)
2221da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
2231da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2241da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
2251da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
2261da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
2271da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2281da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
229e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
230e905914fSJeremy Fitzhardinge 	__attribute__ ((format (printf, 2, 3)));
23111443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...)
2341da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 3)));
2351da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list)
2361da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 0)));
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
2391da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
240d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
2411da177e4SLinus Torvalds 
2425e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
2431da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
2441da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
245ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
246ab7476cfSArjan van de Ven 
24704a2e6a5SEric W. Biederman struct pid;
24804a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp);
2491da177e4SLinus Torvalds 
250a0ad05c7SThomas Renninger /*
251a0ad05c7SThomas Renninger  * FW_BUG
252a0ad05c7SThomas Renninger  * Add this to a message where you are sure the firmware is buggy or behaves
253a0ad05c7SThomas Renninger  * really stupid or out of spec. Be aware that the responsible BIOS developer
254a0ad05c7SThomas Renninger  * should be able to fix this issue or at least get a concrete idea of the
255a0ad05c7SThomas Renninger  * problem by reading your message without the need of looking at the kernel
256a0ad05c7SThomas Renninger  * code.
257a0ad05c7SThomas Renninger  *
258a0ad05c7SThomas Renninger  * Use it for definite and high priority BIOS bugs.
259a0ad05c7SThomas Renninger  *
260a0ad05c7SThomas Renninger  * FW_WARN
261a0ad05c7SThomas Renninger  * Use it for not that clear (e.g. could the kernel messed up things already?)
262a0ad05c7SThomas Renninger  * and medium priority BIOS bugs.
263a0ad05c7SThomas Renninger  *
264a0ad05c7SThomas Renninger  * FW_INFO
265a0ad05c7SThomas Renninger  * Use this one if you want to tell the user or vendor about something
266a0ad05c7SThomas Renninger  * suspicious, but generally harmless related to the firmware.
267a0ad05c7SThomas Renninger  *
268a0ad05c7SThomas Renninger  * Use it for information or very low priority BIOS bugs.
269a0ad05c7SThomas Renninger  */
270a0ad05c7SThomas Renninger #define FW_BUG		"[Firmware Bug]: "
271a0ad05c7SThomas Renninger #define FW_WARN		"[Firmware Warn]: "
272a0ad05c7SThomas Renninger #define FW_INFO		"[Firmware Info]: "
273a0ad05c7SThomas Renninger 
274c6de9f08SHuang Ying /*
275c6de9f08SHuang Ying  * HW_ERR
276c6de9f08SHuang Ying  * Add this to a message for hardware errors, so that user can report
277c6de9f08SHuang Ying  * it to hardware vendor instead of LKML or software vendor.
278c6de9f08SHuang Ying  */
279c6de9f08SHuang Ying #define HW_ERR		"[Hardware Error]: "
280c6de9f08SHuang Ying 
281d59745ceSMatt Mackall #ifdef CONFIG_PRINTK
2821da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args)
2831da177e4SLinus Torvalds 	__attribute__ ((format (printf, 1, 0)));
2841da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...)
285a586df06SAndi Kleen 	__attribute__ ((format (printf, 1, 2))) __cold;
2867ef3d2fdSJoe Perches 
28777006a0aSAndrew Morton /*
28877006a0aSAndrew Morton  * Please don't use printk_ratelimit(), because it shares ratelimiting state
28977006a0aSAndrew Morton  * with all other unrelated printk_ratelimit() callsites.  Instead use
29077006a0aSAndrew Morton  * printk_ratelimited() or plain old __ratelimit().
29177006a0aSAndrew Morton  */
2925c828713SChristian Borntraeger extern int __printk_ratelimit(const char *func);
2935c828713SChristian Borntraeger #define printk_ratelimit() __printk_ratelimit(__func__)
2947ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2957ef3d2fdSJoe Perches 				   unsigned int interval_msec);
296f036be96SIngo Molnar 
297af91322eSDave Young extern int printk_delay_msec;
298af91322eSDave Young 
299f036be96SIngo Molnar /*
300f036be96SIngo Molnar  * Print a one-time message (analogous to WARN_ONCE() et al):
301f036be96SIngo Molnar  */
302f036be96SIngo Molnar #define printk_once(x...) ({			\
3030b2749aaSJoe Perches 	static bool __print_once;		\
304f036be96SIngo Molnar 						\
3050b2749aaSJoe Perches 	if (!__print_once) {			\
3060b2749aaSJoe Perches 		__print_once = true;		\
307f036be96SIngo Molnar 		printk(x);			\
308f036be96SIngo Molnar 	}					\
309f036be96SIngo Molnar })
310f036be96SIngo Molnar 
31104d491abSNeil Horman void log_buf_kexec_setup(void);
312d59745ceSMatt Mackall #else
313d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args)
314d59745ceSMatt Mackall 	__attribute__ ((format (printf, 1, 0)));
315d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; }
316d59745ceSMatt Mackall static inline int printk(const char *s, ...)
317d59745ceSMatt Mackall 	__attribute__ ((format (printf, 1, 2)));
318a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; }
3197ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; }
3207ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
3217ef3d2fdSJoe Perches 					  unsigned int interval_msec)	\
3227ef3d2fdSJoe Perches 		{ return false; }
323f036be96SIngo Molnar 
324f036be96SIngo Molnar /* No effect, but we still get type checking even in the !PRINTK case: */
325f036be96SIngo Molnar #define printk_once(x...) printk(x)
326f036be96SIngo Molnar 
32704d491abSNeil Horman static inline void log_buf_kexec_setup(void)
32804d491abSNeil Horman {
32904d491abSNeil Horman }
330d59745ceSMatt Mackall #endif
3311da177e4SLinus Torvalds 
33212fdff3fSDavid Howells /*
33312fdff3fSDavid Howells  * Dummy printk for disabled debugging statements to use whilst maintaining
33412fdff3fSDavid Howells  * gcc's format and side-effect checking.
33512fdff3fSDavid Howells  */
33612fdff3fSDavid Howells static inline __attribute__ ((format (printf, 1, 2)))
33712fdff3fSDavid Howells int no_printk(const char *s, ...) { return 0; }
33812fdff3fSDavid Howells 
339ced9cd40SIngo Molnar extern int printk_needs_cpu(int cpu);
340ced9cd40SIngo Molnar extern void printk_tick(void);
341ced9cd40SIngo Molnar 
342e17ba73bSJiri Slaby extern void asmlinkage __attribute__((format(printf, 1, 2)))
343076f9776SIngo Molnar 	early_printk(const char *fmt, ...);
344076f9776SIngo Molnar 
3451da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds static inline void console_silent(void)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	console_loglevel = 0;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static inline void console_verbose(void)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	if (console_loglevel)
3551da177e4SLinus Torvalds 		console_loglevel = 15;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
359e3e8a75dSKirill Korotaev extern void wake_up_klogd(void);
3601da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
361aa727107SAdrian Bunk extern int panic_timeout;
3621da177e4SLinus Torvalds extern int panic_on_oops;
3638da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
3645211a242SKurt Garloff extern int panic_on_io_nmi;
3651da177e4SLinus Torvalds extern const char *print_tainted(void);
36625ddbb18SAndi Kleen extern void add_taint(unsigned flag);
36725ddbb18SAndi Kleen extern int test_taint(unsigned flag);
36825ddbb18SAndi Kleen extern unsigned long get_taint(void);
369b920de1bSDavid Howells extern int root_mountflags;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /* Values used for system_state */
3721da177e4SLinus Torvalds extern enum system_states {
3731da177e4SLinus Torvalds 	SYSTEM_BOOTING,
3741da177e4SLinus Torvalds 	SYSTEM_RUNNING,
3751da177e4SLinus Torvalds 	SYSTEM_HALT,
3761da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
3771da177e4SLinus Torvalds 	SYSTEM_RESTART,
378729b4d4cSAlexey Starikovskiy 	SYSTEM_SUSPEND_DISK,
3791da177e4SLinus Torvalds } system_state;
3801da177e4SLinus Torvalds 
38125ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
38225ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
38325ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP		2
38425ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
38525ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
38625ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
38725ddbb18SAndi Kleen #define TAINT_USER			6
38825ddbb18SAndi Kleen #define TAINT_DIE			7
38925ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
39025ddbb18SAndi Kleen #define TAINT_WARN			9
39126e9a397SLinus Torvalds #define TAINT_CRAP			10
39292946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
3931da177e4SLinus Torvalds 
394a586df06SAndi Kleen extern void dump_stack(void) __cold;
3951da177e4SLinus Torvalds 
39699eaf3c4SRandy Dunlap enum {
39799eaf3c4SRandy Dunlap 	DUMP_PREFIX_NONE,
39899eaf3c4SRandy Dunlap 	DUMP_PREFIX_ADDRESS,
39999eaf3c4SRandy Dunlap 	DUMP_PREFIX_OFFSET
40099eaf3c4SRandy Dunlap };
401c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len,
402c7909234SRandy Dunlap 				int rowsize, int groupsize,
403c7909234SRandy Dunlap 				char *linebuf, size_t linebuflen, bool ascii);
404c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str,
405c7909234SRandy Dunlap 				int prefix_type, int rowsize, int groupsize,
4066a0ed91eSArtem Bityutskiy 				const void *buf, size_t len, bool ascii);
407c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
408eb9a9a56SAlan Stern 			const void *buf, size_t len);
4093fc95772SHarvey Harrison 
4103fc95772SHarvey Harrison extern const char hex_asc[];
4113fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
4123fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
4133fc95772SHarvey Harrison 
4143fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte)
4153fc95772SHarvey Harrison {
4163fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
4173fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
4183fc95772SHarvey Harrison 	return buf;
4193fc95772SHarvey Harrison }
42099eaf3c4SRandy Dunlap 
42190378889SAndy Shevchenko extern int hex_to_bin(char ch);
42290378889SAndy Shevchenko 
423d091c2f5SMartin Schwidefsky #ifndef pr_fmt
424d091c2f5SMartin Schwidefsky #define pr_fmt(fmt) fmt
425d091c2f5SMartin Schwidefsky #endif
426d091c2f5SMartin Schwidefsky 
427d091c2f5SMartin Schwidefsky #define pr_emerg(fmt, ...) \
428d091c2f5SMartin Schwidefsky         printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
429d091c2f5SMartin Schwidefsky #define pr_alert(fmt, ...) \
430d091c2f5SMartin Schwidefsky         printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
431d091c2f5SMartin Schwidefsky #define pr_crit(fmt, ...) \
432d091c2f5SMartin Schwidefsky         printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
433d091c2f5SMartin Schwidefsky #define pr_err(fmt, ...) \
434d091c2f5SMartin Schwidefsky         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
435d091c2f5SMartin Schwidefsky #define pr_warning(fmt, ...) \
436d091c2f5SMartin Schwidefsky         printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
437fc62f2f1SJoe Perches #define pr_warn pr_warning
438d091c2f5SMartin Schwidefsky #define pr_notice(fmt, ...) \
439d091c2f5SMartin Schwidefsky         printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
440d091c2f5SMartin Schwidefsky #define pr_info(fmt, ...) \
441d091c2f5SMartin Schwidefsky         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
442311d0761SCyrill Gorcunov #define pr_cont(fmt, ...) \
443311d0761SCyrill Gorcunov 	printk(KERN_CONT fmt, ##__VA_ARGS__)
4441f7c8234SEmil Medve 
4454ccb4579SMichael Ellerman /* pr_devel() should produce zero code unless DEBUG is defined */
4464ccb4579SMichael Ellerman #ifdef DEBUG
4474ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \
4484ccb4579SMichael Ellerman 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
4494ccb4579SMichael Ellerman #else
4504ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \
4514ccb4579SMichael Ellerman 	({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
4524ccb4579SMichael Ellerman #endif
4534ccb4579SMichael Ellerman 
4541cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */
455d0d85ff9SCornelia Huck #if defined(DEBUG)
456d0d85ff9SCornelia Huck #define pr_debug(fmt, ...) \
457d0d85ff9SCornelia Huck 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
458e9d376f0SJason Baron #elif defined(CONFIG_DYNAMIC_DEBUG)
459e6e66b02SGreg Banks /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
46000b55864SJoe Perches #define pr_debug(fmt, ...) \
46100b55864SJoe Perches 	dynamic_pr_debug(fmt, ##__VA_ARGS__)
4621da177e4SLinus Torvalds #else
463d091c2f5SMartin Schwidefsky #define pr_debug(fmt, ...) \
464d091c2f5SMartin Schwidefsky 	({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
4651da177e4SLinus Torvalds #endif
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /*
4688a64f336SJoe Perches  * ratelimited messages with local ratelimit_state,
4698a64f336SJoe Perches  * no local ratelimit_state used in the !PRINTK case
4708a64f336SJoe Perches  */
4718a64f336SJoe Perches #ifdef CONFIG_PRINTK
4728a64f336SJoe Perches #define printk_ratelimited(fmt, ...)  ({				\
473d8521fccSOGAWA Hirofumi 	static DEFINE_RATELIMIT_STATE(_rs,				\
474d8521fccSOGAWA Hirofumi 				      DEFAULT_RATELIMIT_INTERVAL,	\
475d8521fccSOGAWA Hirofumi 				      DEFAULT_RATELIMIT_BURST);		\
4768a64f336SJoe Perches 									\
477bb1dc0baSYong Zhang 	if (__ratelimit(&_rs))						\
4788a64f336SJoe Perches 		printk(fmt, ##__VA_ARGS__);				\
4798a64f336SJoe Perches })
4808a64f336SJoe Perches #else
4818a64f336SJoe Perches /* No effect, but we still get type checking even in the !PRINTK case: */
4828a64f336SJoe Perches #define printk_ratelimited printk
4838a64f336SJoe Perches #endif
4848a64f336SJoe Perches 
4858a64f336SJoe Perches #define pr_emerg_ratelimited(fmt, ...) \
4868a64f336SJoe Perches 	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
4878a64f336SJoe Perches #define pr_alert_ratelimited(fmt, ...) \
4888a64f336SJoe Perches 	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
4898a64f336SJoe Perches #define pr_crit_ratelimited(fmt, ...) \
4908a64f336SJoe Perches 	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
4918a64f336SJoe Perches #define pr_err_ratelimited(fmt, ...) \
4928a64f336SJoe Perches 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
4938a64f336SJoe Perches #define pr_warning_ratelimited(fmt, ...) \
4948a64f336SJoe Perches 	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
495fc62f2f1SJoe Perches #define pr_warn_ratelimited pr_warning_ratelimited
4968a64f336SJoe Perches #define pr_notice_ratelimited(fmt, ...) \
4978a64f336SJoe Perches 	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
4988a64f336SJoe Perches #define pr_info_ratelimited(fmt, ...) \
4998a64f336SJoe Perches 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
5008a64f336SJoe Perches /* no pr_cont_ratelimited, don't do that... */
5018a64f336SJoe Perches /* If you are writing a driver, please use dev_dbg instead */
5028a64f336SJoe Perches #if defined(DEBUG)
5038a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \
5048a64f336SJoe Perches 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
5058a64f336SJoe Perches #else
5068a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \
5078a64f336SJoe Perches 	({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \
5088a64f336SJoe Perches 				     ##__VA_ARGS__); 0; })
5098a64f336SJoe Perches #endif
5108a64f336SJoe Perches 
5118a64f336SJoe Perches /*
512526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
5132002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
5142002c258SSteven Rostedt  *
5152002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
5162002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
517156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
5182002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
5192002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
5202002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
5212002c258SSteven Rostedt  * to continue tracing.
5222002c258SSteven Rostedt  *
5232002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
5242002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
5252002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
5262002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
5272002c258SSteven Rostedt  *
5282002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
529526211bcSIngo Molnar  */
5302002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER
5312002c258SSteven Rostedt void tracing_on(void);
5322002c258SSteven Rostedt void tracing_off(void);
5332002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */
5342002c258SSteven Rostedt void tracing_off_permanent(void);
5352002c258SSteven Rostedt int tracing_is_on(void);
5362002c258SSteven Rostedt #else
5372002c258SSteven Rostedt static inline void tracing_on(void) { }
5382002c258SSteven Rostedt static inline void tracing_off(void) { }
5392002c258SSteven Rostedt static inline void tracing_off_permanent(void) { }
5402002c258SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
5412002c258SSteven Rostedt #endif
542cecbca96SFrederic Weisbecker 
543cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
544cecbca96SFrederic Weisbecker 	DUMP_NONE,
545cecbca96SFrederic Weisbecker 	DUMP_ALL,
546cecbca96SFrederic Weisbecker 	DUMP_ORIG,
547cecbca96SFrederic Weisbecker };
548cecbca96SFrederic Weisbecker 
549526211bcSIngo Molnar #ifdef CONFIG_TRACING
550526211bcSIngo Molnar extern void tracing_start(void);
551526211bcSIngo Molnar extern void tracing_stop(void);
552526211bcSIngo Molnar extern void ftrace_off_permanent(void);
553526211bcSIngo Molnar 
554769b0441SFrederic Weisbecker static inline void __attribute__ ((format (printf, 1, 2)))
555769b0441SFrederic Weisbecker ____trace_printk_check_format(const char *fmt, ...)
556769b0441SFrederic Weisbecker {
557769b0441SFrederic Weisbecker }
558769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
559769b0441SFrederic Weisbecker do {									\
560769b0441SFrederic Weisbecker 	if (0)								\
561769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
562769b0441SFrederic Weisbecker } while (0)
563769b0441SFrederic Weisbecker 
564526211bcSIngo Molnar /**
565526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
566526211bcSIngo Molnar  * @fmt: the printf format for printing
567526211bcSIngo Molnar  *
568526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
569526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
570526211bcSIngo Molnar  *
571526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
572526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
573526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
574526211bcSIngo Molnar  * where problems are occurring.
575526211bcSIngo Molnar  *
576526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
577526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
578526211bcSIngo Molnar  * your code.
579526211bcSIngo Molnar  */
580769b0441SFrederic Weisbecker 
581769b0441SFrederic Weisbecker #define trace_printk(fmt, args...)					\
582769b0441SFrederic Weisbecker do {									\
583769b0441SFrederic Weisbecker 	__trace_printk_check_format(fmt, ##args);			\
58448ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
58548ead020SFrederic Weisbecker 		static const char *trace_printk_fmt			\
58648ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
58748ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
58848ead020SFrederic Weisbecker 									\
58948ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
59048ead020SFrederic Weisbecker 	} else								\
59148ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);		\
592769b0441SFrederic Weisbecker } while (0)
593769b0441SFrederic Weisbecker 
594526211bcSIngo Molnar extern int
59548ead020SFrederic Weisbecker __trace_bprintk(unsigned long ip, const char *fmt, ...)
59648ead020SFrederic Weisbecker 	__attribute__ ((format (printf, 2, 3)));
59748ead020SFrederic Weisbecker 
59848ead020SFrederic Weisbecker extern int
599526211bcSIngo Molnar __trace_printk(unsigned long ip, const char *fmt, ...)
600526211bcSIngo Molnar 	__attribute__ ((format (printf, 2, 3)));
601769b0441SFrederic Weisbecker 
60203889384SSteven Rostedt extern void trace_dump_stack(void);
60303889384SSteven Rostedt 
60448ead020SFrederic Weisbecker /*
60548ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
60648ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
60748ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
60848ead020SFrederic Weisbecker  */
609769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
610769b0441SFrederic Weisbecker do {									\
61148ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
612769b0441SFrederic Weisbecker 		static const char *trace_printk_fmt			\
61348ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
61448ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
6157bffc23eSIngo Molnar 									\
61648ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
61748ead020SFrederic Weisbecker 	} else								\
61848ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
619769b0441SFrederic Weisbecker } while (0)
620769b0441SFrederic Weisbecker 
621526211bcSIngo Molnar extern int
62248ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
62348ead020SFrederic Weisbecker 
62448ead020SFrederic Weisbecker extern int
625526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
626769b0441SFrederic Weisbecker 
627cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
628526211bcSIngo Molnar #else
629526211bcSIngo Molnar static inline int
630526211bcSIngo Molnar trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
631526211bcSIngo Molnar 
632526211bcSIngo Molnar static inline void tracing_start(void) { }
633526211bcSIngo Molnar static inline void tracing_stop(void) { }
634526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { }
635e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { }
636526211bcSIngo Molnar static inline int
637526211bcSIngo Molnar trace_printk(const char *fmt, ...)
638526211bcSIngo Molnar {
639526211bcSIngo Molnar 	return 0;
640526211bcSIngo Molnar }
641526211bcSIngo Molnar static inline int
642526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
643526211bcSIngo Molnar {
644526211bcSIngo Molnar 	return 0;
645526211bcSIngo Molnar }
646cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
647769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
648526211bcSIngo Molnar 
649526211bcSIngo Molnar /*
650bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
6511da177e4SLinus Torvalds  * strict type-checking.. See the
6521da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
6531da177e4SLinus Torvalds  */
6541da177e4SLinus Torvalds #define min(x, y) ({				\
655bdf4bbaaSHarvey Harrison 	typeof(x) _min1 = (x);			\
656bdf4bbaaSHarvey Harrison 	typeof(y) _min2 = (y);			\
657bdf4bbaaSHarvey Harrison 	(void) (&_min1 == &_min2);		\
658bdf4bbaaSHarvey Harrison 	_min1 < _min2 ? _min1 : _min2; })
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds #define max(x, y) ({				\
661bdf4bbaaSHarvey Harrison 	typeof(x) _max1 = (x);			\
662bdf4bbaaSHarvey Harrison 	typeof(y) _max2 = (y);			\
663bdf4bbaaSHarvey Harrison 	(void) (&_max1 == &_max2);		\
664bdf4bbaaSHarvey Harrison 	_max1 > _max2 ? _max1 : _max2; })
665bdf4bbaaSHarvey Harrison 
666f27c85c5SHagen Paul Pfeifer #define min3(x, y, z) ({			\
667f27c85c5SHagen Paul Pfeifer 	typeof(x) _min1 = (x);			\
668f27c85c5SHagen Paul Pfeifer 	typeof(y) _min2 = (y);			\
669f27c85c5SHagen Paul Pfeifer 	typeof(z) _min3 = (z);			\
670f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min2);		\
671f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min3);		\
672f27c85c5SHagen Paul Pfeifer 	_min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
673f27c85c5SHagen Paul Pfeifer 		(_min2 < _min3 ? _min2 : _min3); })
674f27c85c5SHagen Paul Pfeifer 
675f27c85c5SHagen Paul Pfeifer #define max3(x, y, z) ({			\
676f27c85c5SHagen Paul Pfeifer 	typeof(x) _max1 = (x);			\
677f27c85c5SHagen Paul Pfeifer 	typeof(y) _max2 = (y);			\
678f27c85c5SHagen Paul Pfeifer 	typeof(z) _max3 = (z);			\
679f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max2);		\
680f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max3);		\
681f27c85c5SHagen Paul Pfeifer 	_max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
682f27c85c5SHagen Paul Pfeifer 		(_max2 > _max3 ? _max2 : _max3); })
683f27c85c5SHagen Paul Pfeifer 
684bdf4bbaaSHarvey Harrison /**
685c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
686c8bf1336SMartin K. Petersen  * @x: value1
687c8bf1336SMartin K. Petersen  * @y: value2
688c8bf1336SMartin K. Petersen  */
689c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
690c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
691c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
692c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
693c8bf1336SMartin K. Petersen 
694c8bf1336SMartin K. Petersen /**
695bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
696bdf4bbaaSHarvey Harrison  * @val: current value
697bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
698bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
699bdf4bbaaSHarvey Harrison  *
700bdf4bbaaSHarvey Harrison  * This macro does strict typechecking of min/max to make sure they are of the
701bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
702bdf4bbaaSHarvey Harrison  */
703bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({			\
704bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
705bdf4bbaaSHarvey Harrison 	typeof(min) __min = (min);		\
706bdf4bbaaSHarvey Harrison 	typeof(max) __max = (max);		\
707bdf4bbaaSHarvey Harrison 	(void) (&__val == &__min);		\
708bdf4bbaaSHarvey Harrison 	(void) (&__val == &__max);		\
709bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
710bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /*
7131da177e4SLinus Torvalds  * ..and if you can't take the strict
7141da177e4SLinus Torvalds  * types, you can specify one yourself.
7151da177e4SLinus Torvalds  *
716bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
7171da177e4SLinus Torvalds  */
718bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({			\
719bdf4bbaaSHarvey Harrison 	type __min1 = (x);			\
720bdf4bbaaSHarvey Harrison 	type __min2 = (y);			\
721bdf4bbaaSHarvey Harrison 	__min1 < __min2 ? __min1: __min2; })
7221da177e4SLinus Torvalds 
723bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({			\
724bdf4bbaaSHarvey Harrison 	type __max1 = (x);			\
725bdf4bbaaSHarvey Harrison 	type __max2 = (y);			\
726bdf4bbaaSHarvey Harrison 	__max1 > __max2 ? __max1: __max2; })
727bdf4bbaaSHarvey Harrison 
728bdf4bbaaSHarvey Harrison /**
729bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
730bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
731bdf4bbaaSHarvey Harrison  * @val: current value
732bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
733bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
734bdf4bbaaSHarvey Harrison  *
735bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
736bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
737bdf4bbaaSHarvey Harrison  */
738bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({		\
739bdf4bbaaSHarvey Harrison 	type __val = (val);			\
740bdf4bbaaSHarvey Harrison 	type __min = (min);			\
741bdf4bbaaSHarvey Harrison 	type __max = (max);			\
742bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
743bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
744bdf4bbaaSHarvey Harrison 
745bdf4bbaaSHarvey Harrison /**
746bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
747bdf4bbaaSHarvey Harrison  * @val: current value
748bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
749bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
750bdf4bbaaSHarvey Harrison  *
751bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
752bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
753bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
754bdf4bbaaSHarvey Harrison  * integer type.
755bdf4bbaaSHarvey Harrison  */
756bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({		\
757bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
758bdf4bbaaSHarvey Harrison 	typeof(val) __min = (min);		\
759bdf4bbaaSHarvey Harrison 	typeof(val) __max = (max);		\
760bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
761bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
7621da177e4SLinus Torvalds 
76391f68b73SWu Fengguang 
76491f68b73SWu Fengguang /*
76591f68b73SWu Fengguang  * swap - swap value of @a and @b
76691f68b73SWu Fengguang  */
767ac7b9004SPeter Zijlstra #define swap(a, b) \
768ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
76991f68b73SWu Fengguang 
7701da177e4SLinus Torvalds /**
7711da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
7721da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
7731da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
7741da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
7751da177e4SLinus Torvalds  *
7761da177e4SLinus Torvalds  */
7771da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
7781da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
7791da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
7801da177e4SLinus Torvalds 
781d4d23addSKyle McMartin struct sysinfo;
782d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info);
783d4d23addSKyle McMartin 
7841da177e4SLinus Torvalds #endif /* __KERNEL__ */
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds #define SI_LOAD_SHIFT	16
7871da177e4SLinus Torvalds struct sysinfo {
7881da177e4SLinus Torvalds 	long uptime;			/* Seconds since boot */
7891da177e4SLinus Torvalds 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
7901da177e4SLinus Torvalds 	unsigned long totalram;		/* Total usable main memory size */
7911da177e4SLinus Torvalds 	unsigned long freeram;		/* Available memory size */
7921da177e4SLinus Torvalds 	unsigned long sharedram;	/* Amount of shared memory */
7931da177e4SLinus Torvalds 	unsigned long bufferram;	/* Memory used by buffers */
7941da177e4SLinus Torvalds 	unsigned long totalswap;	/* Total swap space size */
7951da177e4SLinus Torvalds 	unsigned long freeswap;		/* swap space still available */
7961da177e4SLinus Torvalds 	unsigned short procs;		/* Number of current processes */
7971da177e4SLinus Torvalds 	unsigned short pad;		/* explicit padding for m68k */
7981da177e4SLinus Torvalds 	unsigned long totalhigh;	/* Total high memory size */
7991da177e4SLinus Torvalds 	unsigned long freehigh;		/* Available high memory size */
8001da177e4SLinus Torvalds 	unsigned int mem_unit;		/* Memory unit size in bytes */
8011da177e4SLinus Torvalds 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
8021da177e4SLinus Torvalds };
8031da177e4SLinus Torvalds 
804c0398ee6SNikita Danilov /* Force a compilation error if condition is true */
8058c87df45SJan Beulich #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
8068c87df45SJan Beulich 
8078c87df45SJan Beulich /* Force a compilation error if condition is constant and true */
8088c87df45SJan Beulich #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
8091da177e4SLinus Torvalds 
810cc8ef6ebSRoland Dreier /* Force a compilation error if a constant expression is not a power of 2 */
811cc8ef6ebSRoland Dreier #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
812cc8ef6ebSRoland Dreier 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
813cc8ef6ebSRoland Dreier 
8144552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a
8154552d5dcSJan Beulich    result (of value 0 and type size_t), so the expression can be used
8164552d5dcSJan Beulich    e.g. in a structure initializer (or where-ever else comma expressions
8174552d5dcSJan Beulich    aren't permitted). */
8188c87df45SJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
8198c87df45SJan Beulich #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
8204552d5dcSJan Beulich 
8211da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */
8221da177e4SLinus Torvalds #define __FUNCTION__ (__func__)
8231da177e4SLinus Torvalds 
82408e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */
82508e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA
82608e0f6a9SChristoph Lameter #define NUMA_BUILD 1
82708e0f6a9SChristoph Lameter #else
82808e0f6a9SChristoph Lameter #define NUMA_BUILD 0
82908e0f6a9SChristoph Lameter #endif
83008e0f6a9SChristoph Lameter 
83129e71abfSSteven Rostedt /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
83229e71abfSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
83329e71abfSSteven Rostedt # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
83429e71abfSSteven Rostedt #endif
83529e71abfSSteven Rostedt 
8361da177e4SLinus Torvalds #endif
837