xref: /linux-6.15/include/linux/kernel.h (revision cc8ef6eb)
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  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #ifdef __KERNEL__
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <stdarg.h>
111da177e4SLinus Torvalds #include <linux/linkage.h>
121da177e4SLinus Torvalds #include <linux/stddef.h>
131da177e4SLinus Torvalds #include <linux/types.h>
141da177e4SLinus Torvalds #include <linux/compiler.h>
151da177e4SLinus Torvalds #include <linux/bitops.h>
16f0d1b0b3SDavid Howells #include <linux/log2.h>
17e0deaff4SAndrew Morton #include <linux/typecheck.h>
18e9d376f0SJason Baron #include <linux/dynamic_debug.h>
191da177e4SLinus Torvalds #include <asm/byteorder.h>
201da177e4SLinus Torvalds #include <asm/bug.h>
211da177e4SLinus Torvalds 
223eb3c740SRoman Zippel extern const char linux_banner[];
233eb3c740SRoman Zippel extern const char linux_proc_banner[];
243eb3c740SRoman Zippel 
2544f564a4SZhang, Yanmin #define USHORT_MAX	((u16)(~0U))
2644f564a4SZhang, Yanmin #define SHORT_MAX	((s16)(USHORT_MAX>>1))
2744f564a4SZhang, Yanmin #define SHORT_MIN	(-SHORT_MAX - 1)
281da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
291da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
301da177e4SLinus Torvalds #define UINT_MAX	(~0U)
311da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
321da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
331da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
34111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
35111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
36111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
391da177e4SLinus Torvalds 
402ea58144SLinus Torvalds #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
412ea58144SLinus Torvalds #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
42a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
43f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
442ea58144SLinus Torvalds 
45c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
46c5e631cfSRusty Russell 
474552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
48930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
49b4cac1a0SDavid Howells #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
509fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
519fe06081SDarrick J. Wong {							\
529fe06081SDarrick J. Wong 	typeof(divisor) __divisor = divisor;		\
539fe06081SDarrick J. Wong 	(((x) + ((__divisor) / 2)) / (__divisor));	\
549fe06081SDarrick J. Wong }							\
559fe06081SDarrick J. Wong )
561da177e4SLinus Torvalds 
57ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
58ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
59ca31e146SEduard - Gabriel Munteanu 
6090c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
612da96acdSJens Axboe # include <asm/div64.h>
622da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
632da96acdSJens Axboe #else
642da96acdSJens Axboe # define sector_div(n, b)( \
652da96acdSJens Axboe { \
662da96acdSJens Axboe 	int _res; \
672da96acdSJens Axboe 	_res = (n) % (b); \
682da96acdSJens Axboe 	(n) /= (b); \
692da96acdSJens Axboe 	_res; \
702da96acdSJens Axboe } \
712da96acdSJens Axboe )
722da96acdSJens Axboe #endif
732da96acdSJens Axboe 
74218e180eSAndrew Morton /**
75218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
76218e180eSAndrew Morton  * @n: the number we're accessing
77218e180eSAndrew Morton  *
78218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
79218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
80218e180eSAndrew Morton  * 32-bits.
81218e180eSAndrew Morton  */
82218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
83218e180eSAndrew Morton 
84204b885eSJoerg Roedel /**
85204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
86204b885eSJoerg Roedel  * @n: the number we're accessing
87204b885eSJoerg Roedel  */
88204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
89204b885eSJoerg Roedel 
901da177e4SLinus Torvalds #define	KERN_EMERG	"<0>"	/* system is unusable			*/
911da177e4SLinus Torvalds #define	KERN_ALERT	"<1>"	/* action must be taken immediately	*/
921da177e4SLinus Torvalds #define	KERN_CRIT	"<2>"	/* critical conditions			*/
931da177e4SLinus Torvalds #define	KERN_ERR	"<3>"	/* error conditions			*/
941da177e4SLinus Torvalds #define	KERN_WARNING	"<4>"	/* warning conditions			*/
951da177e4SLinus Torvalds #define	KERN_NOTICE	"<5>"	/* normal but significant condition	*/
961da177e4SLinus Torvalds #define	KERN_INFO	"<6>"	/* informational			*/
971da177e4SLinus Torvalds #define	KERN_DEBUG	"<7>"	/* debug-level messages			*/
981da177e4SLinus Torvalds 
99e28d7137SLinus Torvalds /* Use the default kernel loglevel */
100e28d7137SLinus Torvalds #define KERN_DEFAULT	"<d>"
10147492527SIngo Molnar /*
10247492527SIngo Molnar  * Annotation for a "continued" line of log printout (only done after a
10347492527SIngo Molnar  * line that had no enclosing \n). Only to be used by core/arch code
10447492527SIngo Molnar  * during early bootup (a continued line is not SMP-safe otherwise).
10547492527SIngo Molnar  */
1065fd29d6cSLinus Torvalds #define	KERN_CONT	"<c>"
10747492527SIngo Molnar 
1081da177e4SLinus Torvalds extern int console_printk[];
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds #define console_loglevel (console_printk[0])
1111da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1])
1121da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2])
1131da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3])
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds struct completion;
116df2e71fbS[email protected] struct pt_regs;
117df2e71fbS[email protected] struct user;
1181da177e4SLinus Torvalds 
119070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
120070cb065SUwe Kleine-König extern int _cond_resched(void);
121070cb065SUwe Kleine-König # define might_resched() _cond_resched()
122070cb065SUwe Kleine-König #else
123070cb065SUwe Kleine-König # define might_resched() do { } while (0)
124070cb065SUwe Kleine-König #endif
125070cb065SUwe Kleine-König 
1267106a27bSRandy Dunlap #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
127e4aafea2SFrederic Weisbecker   void __might_sleep(char *file, int line, int preempt_offset);
1281da177e4SLinus Torvalds /**
1291da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1301da177e4SLinus Torvalds  *
1311da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1321da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1331da177e4SLinus Torvalds  *
1341da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
135e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1361da177e4SLinus Torvalds  * supposed to.
1371da177e4SLinus Torvalds  */
138f8cbd99bSIngo Molnar # define might_sleep() \
139e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
140f8cbd99bSIngo Molnar #else
141e4aafea2SFrederic Weisbecker   static inline void __might_sleep(char *file, int line, int preempt_offset) { }
142f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
143f8cbd99bSIngo Molnar #endif
144f8cbd99bSIngo Molnar 
145368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
146f8cbd99bSIngo Molnar 
1471da177e4SLinus Torvalds #define abs(x) ({				\
148a49c59c0SRolf Eike Beer 		long __x = (x);			\
1491da177e4SLinus Torvalds 		(__x < 0) ? -__x : __x;		\
1501da177e4SLinus Torvalds 	})
1511da177e4SLinus Torvalds 
1523ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
1533ee1afa3SNick Piggin void might_fault(void);
1543ee1afa3SNick Piggin #else
1553ee1afa3SNick Piggin static inline void might_fault(void)
1563ee1afa3SNick Piggin {
1573ee1afa3SNick Piggin 	might_sleep();
1583ee1afa3SNick Piggin }
1593ee1afa3SNick Piggin #endif
1603ee1afa3SNick Piggin 
161e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
1621da177e4SLinus Torvalds extern long (*panic_blink)(long time);
1631da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...)
164a586df06SAndi Kleen 	__attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
165dd287796SAndrew Morton extern void oops_enter(void);
166dd287796SAndrew Morton extern void oops_exit(void);
167dd287796SAndrew Morton extern int oops_may_print(void);
168ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code)
1691da177e4SLinus Torvalds 	ATTRIB_NORET;
1701da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long)
1711da177e4SLinus Torvalds 	ATTRIB_NORET;
1721da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
1731da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
1741da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
1751da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
17606b2a76dSYi Yang extern int strict_strtoul(const char *, unsigned int, unsigned long *);
17706b2a76dSYi Yang extern int strict_strtol(const char *, unsigned int, long *);
17806b2a76dSYi Yang extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
17906b2a76dSYi Yang extern int strict_strtoll(const char *, unsigned int, long long *);
1801da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...)
1811da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 3)));
1821da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list)
1831da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 0)));
1841da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...)
1851da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
1861da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1871da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
1881da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
1891da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
1901da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1911da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
192e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
193e905914fSJeremy Fitzhardinge 	__attribute__ ((format (printf, 2, 3)));
19411443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...)
1971da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 3)));
1981da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list)
1991da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 0)));
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
2021da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
203d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
2041da177e4SLinus Torvalds 
2055e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
2061da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
2071da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
208ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
209ab7476cfSArjan van de Ven 
21004a2e6a5SEric W. Biederman struct pid;
21104a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp);
2121da177e4SLinus Torvalds 
213a0ad05c7SThomas Renninger /*
214a0ad05c7SThomas Renninger  * FW_BUG
215a0ad05c7SThomas Renninger  * Add this to a message where you are sure the firmware is buggy or behaves
216a0ad05c7SThomas Renninger  * really stupid or out of spec. Be aware that the responsible BIOS developer
217a0ad05c7SThomas Renninger  * should be able to fix this issue or at least get a concrete idea of the
218a0ad05c7SThomas Renninger  * problem by reading your message without the need of looking at the kernel
219a0ad05c7SThomas Renninger  * code.
220a0ad05c7SThomas Renninger  *
221a0ad05c7SThomas Renninger  * Use it for definite and high priority BIOS bugs.
222a0ad05c7SThomas Renninger  *
223a0ad05c7SThomas Renninger  * FW_WARN
224a0ad05c7SThomas Renninger  * Use it for not that clear (e.g. could the kernel messed up things already?)
225a0ad05c7SThomas Renninger  * and medium priority BIOS bugs.
226a0ad05c7SThomas Renninger  *
227a0ad05c7SThomas Renninger  * FW_INFO
228a0ad05c7SThomas Renninger  * Use this one if you want to tell the user or vendor about something
229a0ad05c7SThomas Renninger  * suspicious, but generally harmless related to the firmware.
230a0ad05c7SThomas Renninger  *
231a0ad05c7SThomas Renninger  * Use it for information or very low priority BIOS bugs.
232a0ad05c7SThomas Renninger  */
233a0ad05c7SThomas Renninger #define FW_BUG		"[Firmware Bug]: "
234a0ad05c7SThomas Renninger #define FW_WARN		"[Firmware Warn]: "
235a0ad05c7SThomas Renninger #define FW_INFO		"[Firmware Info]: "
236a0ad05c7SThomas Renninger 
237d59745ceSMatt Mackall #ifdef CONFIG_PRINTK
2381da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args)
2391da177e4SLinus Torvalds 	__attribute__ ((format (printf, 1, 0)));
2401da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...)
241a586df06SAndi Kleen 	__attribute__ ((format (printf, 1, 2))) __cold;
2427ef3d2fdSJoe Perches 
2435c828713SChristian Borntraeger extern int __printk_ratelimit(const char *func);
2445c828713SChristian Borntraeger #define printk_ratelimit() __printk_ratelimit(__func__)
2457ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2467ef3d2fdSJoe Perches 				   unsigned int interval_msec);
247f036be96SIngo Molnar 
248af91322eSDave Young extern int printk_delay_msec;
249af91322eSDave Young 
250f036be96SIngo Molnar /*
251f036be96SIngo Molnar  * Print a one-time message (analogous to WARN_ONCE() et al):
252f036be96SIngo Molnar  */
253f036be96SIngo Molnar #define printk_once(x...) ({			\
2540b2749aaSJoe Perches 	static bool __print_once;		\
255f036be96SIngo Molnar 						\
2560b2749aaSJoe Perches 	if (!__print_once) {			\
2570b2749aaSJoe Perches 		__print_once = true;		\
258f036be96SIngo Molnar 		printk(x);			\
259f036be96SIngo Molnar 	}					\
260f036be96SIngo Molnar })
261f036be96SIngo Molnar 
26204d491abSNeil Horman void log_buf_kexec_setup(void);
263d59745ceSMatt Mackall #else
264d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args)
265d59745ceSMatt Mackall 	__attribute__ ((format (printf, 1, 0)));
266d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; }
267d59745ceSMatt Mackall static inline int printk(const char *s, ...)
268d59745ceSMatt Mackall 	__attribute__ ((format (printf, 1, 2)));
269a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; }
2707ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; }
2717ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
2727ef3d2fdSJoe Perches 					  unsigned int interval_msec)	\
2737ef3d2fdSJoe Perches 		{ return false; }
274f036be96SIngo Molnar 
275f036be96SIngo Molnar /* No effect, but we still get type checking even in the !PRINTK case: */
276f036be96SIngo Molnar #define printk_once(x...) printk(x)
277f036be96SIngo Molnar 
27804d491abSNeil Horman static inline void log_buf_kexec_setup(void)
27904d491abSNeil Horman {
28004d491abSNeil Horman }
281d59745ceSMatt Mackall #endif
2821da177e4SLinus Torvalds 
283ced9cd40SIngo Molnar extern int printk_needs_cpu(int cpu);
284ced9cd40SIngo Molnar extern void printk_tick(void);
285ced9cd40SIngo Molnar 
286e17ba73bSJiri Slaby extern void asmlinkage __attribute__((format(printf, 1, 2)))
287076f9776SIngo Molnar 	early_printk(const char *fmt, ...);
288076f9776SIngo Molnar 
2891da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static inline void console_silent(void)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	console_loglevel = 0;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static inline void console_verbose(void)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	if (console_loglevel)
2991da177e4SLinus Torvalds 		console_loglevel = 15;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
303e3e8a75dSKirill Korotaev extern void wake_up_klogd(void);
3041da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
305aa727107SAdrian Bunk extern int panic_timeout;
3061da177e4SLinus Torvalds extern int panic_on_oops;
3078da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
3085211a242SKurt Garloff extern int panic_on_io_nmi;
3091da177e4SLinus Torvalds extern const char *print_tainted(void);
31025ddbb18SAndi Kleen extern void add_taint(unsigned flag);
31125ddbb18SAndi Kleen extern int test_taint(unsigned flag);
31225ddbb18SAndi Kleen extern unsigned long get_taint(void);
313b920de1bSDavid Howells extern int root_mountflags;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds /* Values used for system_state */
3161da177e4SLinus Torvalds extern enum system_states {
3171da177e4SLinus Torvalds 	SYSTEM_BOOTING,
3181da177e4SLinus Torvalds 	SYSTEM_RUNNING,
3191da177e4SLinus Torvalds 	SYSTEM_HALT,
3201da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
3211da177e4SLinus Torvalds 	SYSTEM_RESTART,
322729b4d4cSAlexey Starikovskiy 	SYSTEM_SUSPEND_DISK,
3231da177e4SLinus Torvalds } system_state;
3241da177e4SLinus Torvalds 
32525ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
32625ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
32725ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP		2
32825ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
32925ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
33025ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
33125ddbb18SAndi Kleen #define TAINT_USER			6
33225ddbb18SAndi Kleen #define TAINT_DIE			7
33325ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
33425ddbb18SAndi Kleen #define TAINT_WARN			9
33526e9a397SLinus Torvalds #define TAINT_CRAP			10
3361da177e4SLinus Torvalds 
337a586df06SAndi Kleen extern void dump_stack(void) __cold;
3381da177e4SLinus Torvalds 
33999eaf3c4SRandy Dunlap enum {
34099eaf3c4SRandy Dunlap 	DUMP_PREFIX_NONE,
34199eaf3c4SRandy Dunlap 	DUMP_PREFIX_ADDRESS,
34299eaf3c4SRandy Dunlap 	DUMP_PREFIX_OFFSET
34399eaf3c4SRandy Dunlap };
344c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len,
345c7909234SRandy Dunlap 				int rowsize, int groupsize,
346c7909234SRandy Dunlap 				char *linebuf, size_t linebuflen, bool ascii);
347c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str,
348c7909234SRandy Dunlap 				int prefix_type, int rowsize, int groupsize,
3496a0ed91eSArtem Bityutskiy 				const void *buf, size_t len, bool ascii);
350c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
351eb9a9a56SAlan Stern 			const void *buf, size_t len);
3523fc95772SHarvey Harrison 
3533fc95772SHarvey Harrison extern const char hex_asc[];
3543fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
3553fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
3563fc95772SHarvey Harrison 
3573fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte)
3583fc95772SHarvey Harrison {
3593fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
3603fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
3613fc95772SHarvey Harrison 	return buf;
3623fc95772SHarvey Harrison }
36399eaf3c4SRandy Dunlap 
364d091c2f5SMartin Schwidefsky #ifndef pr_fmt
365d091c2f5SMartin Schwidefsky #define pr_fmt(fmt) fmt
366d091c2f5SMartin Schwidefsky #endif
367d091c2f5SMartin Schwidefsky 
368d091c2f5SMartin Schwidefsky #define pr_emerg(fmt, ...) \
369d091c2f5SMartin Schwidefsky         printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
370d091c2f5SMartin Schwidefsky #define pr_alert(fmt, ...) \
371d091c2f5SMartin Schwidefsky         printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
372d091c2f5SMartin Schwidefsky #define pr_crit(fmt, ...) \
373d091c2f5SMartin Schwidefsky         printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
374d091c2f5SMartin Schwidefsky #define pr_err(fmt, ...) \
375d091c2f5SMartin Schwidefsky         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
376d091c2f5SMartin Schwidefsky #define pr_warning(fmt, ...) \
377d091c2f5SMartin Schwidefsky         printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
378d091c2f5SMartin Schwidefsky #define pr_notice(fmt, ...) \
379d091c2f5SMartin Schwidefsky         printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
380d091c2f5SMartin Schwidefsky #define pr_info(fmt, ...) \
381d091c2f5SMartin Schwidefsky         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
382311d0761SCyrill Gorcunov #define pr_cont(fmt, ...) \
383311d0761SCyrill Gorcunov 	printk(KERN_CONT fmt, ##__VA_ARGS__)
3841f7c8234SEmil Medve 
3854ccb4579SMichael Ellerman /* pr_devel() should produce zero code unless DEBUG is defined */
3864ccb4579SMichael Ellerman #ifdef DEBUG
3874ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \
3884ccb4579SMichael Ellerman 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
3894ccb4579SMichael Ellerman #else
3904ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \
3914ccb4579SMichael Ellerman 	({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
3924ccb4579SMichael Ellerman #endif
3934ccb4579SMichael Ellerman 
3941cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */
395d0d85ff9SCornelia Huck #if defined(DEBUG)
396d0d85ff9SCornelia Huck #define pr_debug(fmt, ...) \
397d0d85ff9SCornelia Huck 	printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
398e9d376f0SJason Baron #elif defined(CONFIG_DYNAMIC_DEBUG)
399e6e66b02SGreg Banks /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
40000b55864SJoe Perches #define pr_debug(fmt, ...) \
40100b55864SJoe Perches 	dynamic_pr_debug(fmt, ##__VA_ARGS__)
4021da177e4SLinus Torvalds #else
403d091c2f5SMartin Schwidefsky #define pr_debug(fmt, ...) \
404d091c2f5SMartin Schwidefsky 	({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
4051da177e4SLinus Torvalds #endif
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /*
4088a64f336SJoe Perches  * ratelimited messages with local ratelimit_state,
4098a64f336SJoe Perches  * no local ratelimit_state used in the !PRINTK case
4108a64f336SJoe Perches  */
4118a64f336SJoe Perches #ifdef CONFIG_PRINTK
4128a64f336SJoe Perches #define printk_ratelimited(fmt, ...)  ({		\
4138a64f336SJoe Perches 	static struct ratelimit_state _rs = {		\
4148a64f336SJoe Perches 		.interval = DEFAULT_RATELIMIT_INTERVAL, \
4158a64f336SJoe Perches 		.burst = DEFAULT_RATELIMIT_BURST,       \
4168a64f336SJoe Perches 	};                                              \
4178a64f336SJoe Perches 							\
4188a64f336SJoe Perches 	if (!__ratelimit(&_rs))                         \
4198a64f336SJoe Perches 		printk(fmt, ##__VA_ARGS__);		\
4208a64f336SJoe Perches })
4218a64f336SJoe Perches #else
4228a64f336SJoe Perches /* No effect, but we still get type checking even in the !PRINTK case: */
4238a64f336SJoe Perches #define printk_ratelimited printk
4248a64f336SJoe Perches #endif
4258a64f336SJoe Perches 
4268a64f336SJoe Perches #define pr_emerg_ratelimited(fmt, ...) \
4278a64f336SJoe Perches 	printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
4288a64f336SJoe Perches #define pr_alert_ratelimited(fmt, ...) \
4298a64f336SJoe Perches 	printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
4308a64f336SJoe Perches #define pr_crit_ratelimited(fmt, ...) \
4318a64f336SJoe Perches 	printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
4328a64f336SJoe Perches #define pr_err_ratelimited(fmt, ...) \
4338a64f336SJoe Perches 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
4348a64f336SJoe Perches #define pr_warning_ratelimited(fmt, ...) \
4358a64f336SJoe Perches 	printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
4368a64f336SJoe Perches #define pr_notice_ratelimited(fmt, ...) \
4378a64f336SJoe Perches 	printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
4388a64f336SJoe Perches #define pr_info_ratelimited(fmt, ...) \
4398a64f336SJoe Perches 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
4408a64f336SJoe Perches /* no pr_cont_ratelimited, don't do that... */
4418a64f336SJoe Perches /* If you are writing a driver, please use dev_dbg instead */
4428a64f336SJoe Perches #if defined(DEBUG)
4438a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \
4448a64f336SJoe Perches 	printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
4458a64f336SJoe Perches #else
4468a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \
4478a64f336SJoe Perches 	({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \
4488a64f336SJoe Perches 				     ##__VA_ARGS__); 0; })
4498a64f336SJoe Perches #endif
4508a64f336SJoe Perches 
4518a64f336SJoe Perches /*
452526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
4532002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
4542002c258SSteven Rostedt  *
4552002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
4562002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
457156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
4582002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
4592002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
4602002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
4612002c258SSteven Rostedt  * to continue tracing.
4622002c258SSteven Rostedt  *
4632002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
4642002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
4652002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
4662002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
4672002c258SSteven Rostedt  *
4682002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
469526211bcSIngo Molnar  */
4702002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER
4712002c258SSteven Rostedt void tracing_on(void);
4722002c258SSteven Rostedt void tracing_off(void);
4732002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */
4742002c258SSteven Rostedt void tracing_off_permanent(void);
4752002c258SSteven Rostedt int tracing_is_on(void);
4762002c258SSteven Rostedt #else
4772002c258SSteven Rostedt static inline void tracing_on(void) { }
4782002c258SSteven Rostedt static inline void tracing_off(void) { }
4792002c258SSteven Rostedt static inline void tracing_off_permanent(void) { }
4802002c258SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
4812002c258SSteven Rostedt #endif
482526211bcSIngo Molnar #ifdef CONFIG_TRACING
483526211bcSIngo Molnar extern void tracing_start(void);
484526211bcSIngo Molnar extern void tracing_stop(void);
485526211bcSIngo Molnar extern void ftrace_off_permanent(void);
486526211bcSIngo Molnar 
487526211bcSIngo Molnar extern void
488526211bcSIngo Molnar ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
489526211bcSIngo Molnar 
490769b0441SFrederic Weisbecker static inline void __attribute__ ((format (printf, 1, 2)))
491769b0441SFrederic Weisbecker ____trace_printk_check_format(const char *fmt, ...)
492769b0441SFrederic Weisbecker {
493769b0441SFrederic Weisbecker }
494769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
495769b0441SFrederic Weisbecker do {									\
496769b0441SFrederic Weisbecker 	if (0)								\
497769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
498769b0441SFrederic Weisbecker } while (0)
499769b0441SFrederic Weisbecker 
500526211bcSIngo Molnar /**
501526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
502526211bcSIngo Molnar  * @fmt: the printf format for printing
503526211bcSIngo Molnar  *
504526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
505526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
506526211bcSIngo Molnar  *
507526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
508526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
509526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
510526211bcSIngo Molnar  * where problems are occurring.
511526211bcSIngo Molnar  *
512526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
513526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
514526211bcSIngo Molnar  * your code.
515526211bcSIngo Molnar  */
516769b0441SFrederic Weisbecker 
517769b0441SFrederic Weisbecker #define trace_printk(fmt, args...)					\
518769b0441SFrederic Weisbecker do {									\
519769b0441SFrederic Weisbecker 	__trace_printk_check_format(fmt, ##args);			\
52048ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
52148ead020SFrederic Weisbecker 		static const char *trace_printk_fmt			\
52248ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
52348ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
52448ead020SFrederic Weisbecker 									\
52548ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
52648ead020SFrederic Weisbecker 	} else								\
52748ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);		\
528769b0441SFrederic Weisbecker } while (0)
529769b0441SFrederic Weisbecker 
530526211bcSIngo Molnar extern int
53148ead020SFrederic Weisbecker __trace_bprintk(unsigned long ip, const char *fmt, ...)
53248ead020SFrederic Weisbecker 	__attribute__ ((format (printf, 2, 3)));
53348ead020SFrederic Weisbecker 
53448ead020SFrederic Weisbecker extern int
535526211bcSIngo Molnar __trace_printk(unsigned long ip, const char *fmt, ...)
536526211bcSIngo Molnar 	__attribute__ ((format (printf, 2, 3)));
537769b0441SFrederic Weisbecker 
53803889384SSteven Rostedt extern void trace_dump_stack(void);
53903889384SSteven Rostedt 
54048ead020SFrederic Weisbecker /*
54148ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
54248ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
54348ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
54448ead020SFrederic Weisbecker  */
545769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
546769b0441SFrederic Weisbecker do {									\
54748ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
548769b0441SFrederic Weisbecker 		static const char *trace_printk_fmt			\
54948ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
55048ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
5517bffc23eSIngo Molnar 									\
55248ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
55348ead020SFrederic Weisbecker 	} else								\
55448ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
555769b0441SFrederic Weisbecker } while (0)
556769b0441SFrederic Weisbecker 
557526211bcSIngo Molnar extern int
55848ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
55948ead020SFrederic Weisbecker 
56048ead020SFrederic Weisbecker extern int
561526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
562769b0441SFrederic Weisbecker 
563526211bcSIngo Molnar extern void ftrace_dump(void);
564526211bcSIngo Molnar #else
565526211bcSIngo Molnar static inline void
566526211bcSIngo Molnar ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
567526211bcSIngo Molnar static inline int
568526211bcSIngo Molnar trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
569526211bcSIngo Molnar 
570526211bcSIngo Molnar static inline void tracing_start(void) { }
571526211bcSIngo Molnar static inline void tracing_stop(void) { }
572526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { }
573e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { }
574526211bcSIngo Molnar static inline int
575526211bcSIngo Molnar trace_printk(const char *fmt, ...)
576526211bcSIngo Molnar {
577526211bcSIngo Molnar 	return 0;
578526211bcSIngo Molnar }
579526211bcSIngo Molnar static inline int
580526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
581526211bcSIngo Molnar {
582526211bcSIngo Molnar 	return 0;
583526211bcSIngo Molnar }
584526211bcSIngo Molnar static inline void ftrace_dump(void) { }
585769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
586526211bcSIngo Molnar 
587526211bcSIngo Molnar /*
5881da177e4SLinus Torvalds  *      Display an IP address in readable format.
5891da177e4SLinus Torvalds  */
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds #define NIPQUAD(addr) \
5921da177e4SLinus Torvalds 	((unsigned char *)&addr)[0], \
5931da177e4SLinus Torvalds 	((unsigned char *)&addr)[1], \
5941da177e4SLinus Torvalds 	((unsigned char *)&addr)[2], \
5951da177e4SLinus Torvalds 	((unsigned char *)&addr)[3]
59646b86a2dSJoe Perches #define NIPQUAD_FMT "%u.%u.%u.%u"
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds /*
599bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
6001da177e4SLinus Torvalds  * strict type-checking.. See the
6011da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
6021da177e4SLinus Torvalds  */
6031da177e4SLinus Torvalds #define min(x, y) ({				\
604bdf4bbaaSHarvey Harrison 	typeof(x) _min1 = (x);			\
605bdf4bbaaSHarvey Harrison 	typeof(y) _min2 = (y);			\
606bdf4bbaaSHarvey Harrison 	(void) (&_min1 == &_min2);		\
607bdf4bbaaSHarvey Harrison 	_min1 < _min2 ? _min1 : _min2; })
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds #define max(x, y) ({				\
610bdf4bbaaSHarvey Harrison 	typeof(x) _max1 = (x);			\
611bdf4bbaaSHarvey Harrison 	typeof(y) _max2 = (y);			\
612bdf4bbaaSHarvey Harrison 	(void) (&_max1 == &_max2);		\
613bdf4bbaaSHarvey Harrison 	_max1 > _max2 ? _max1 : _max2; })
614bdf4bbaaSHarvey Harrison 
615bdf4bbaaSHarvey Harrison /**
616bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
617bdf4bbaaSHarvey Harrison  * @val: current value
618bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
619bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
620bdf4bbaaSHarvey Harrison  *
621bdf4bbaaSHarvey Harrison  * This macro does strict typechecking of min/max to make sure they are of the
622bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
623bdf4bbaaSHarvey Harrison  */
624bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({			\
625bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
626bdf4bbaaSHarvey Harrison 	typeof(min) __min = (min);		\
627bdf4bbaaSHarvey Harrison 	typeof(max) __max = (max);		\
628bdf4bbaaSHarvey Harrison 	(void) (&__val == &__min);		\
629bdf4bbaaSHarvey Harrison 	(void) (&__val == &__max);		\
630bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
631bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /*
6341da177e4SLinus Torvalds  * ..and if you can't take the strict
6351da177e4SLinus Torvalds  * types, you can specify one yourself.
6361da177e4SLinus Torvalds  *
637bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
6381da177e4SLinus Torvalds  */
639bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({			\
640bdf4bbaaSHarvey Harrison 	type __min1 = (x);			\
641bdf4bbaaSHarvey Harrison 	type __min2 = (y);			\
642bdf4bbaaSHarvey Harrison 	__min1 < __min2 ? __min1: __min2; })
6431da177e4SLinus Torvalds 
644bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({			\
645bdf4bbaaSHarvey Harrison 	type __max1 = (x);			\
646bdf4bbaaSHarvey Harrison 	type __max2 = (y);			\
647bdf4bbaaSHarvey Harrison 	__max1 > __max2 ? __max1: __max2; })
648bdf4bbaaSHarvey Harrison 
649bdf4bbaaSHarvey Harrison /**
650bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
651bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
652bdf4bbaaSHarvey Harrison  * @val: current value
653bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
654bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
655bdf4bbaaSHarvey Harrison  *
656bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
657bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
658bdf4bbaaSHarvey Harrison  */
659bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({		\
660bdf4bbaaSHarvey Harrison 	type __val = (val);			\
661bdf4bbaaSHarvey Harrison 	type __min = (min);			\
662bdf4bbaaSHarvey Harrison 	type __max = (max);			\
663bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
664bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
665bdf4bbaaSHarvey Harrison 
666bdf4bbaaSHarvey Harrison /**
667bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
668bdf4bbaaSHarvey Harrison  * @val: current value
669bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
670bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
671bdf4bbaaSHarvey Harrison  *
672bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
673bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
674bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
675bdf4bbaaSHarvey Harrison  * integer type.
676bdf4bbaaSHarvey Harrison  */
677bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({		\
678bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
679bdf4bbaaSHarvey Harrison 	typeof(val) __min = (min);		\
680bdf4bbaaSHarvey Harrison 	typeof(val) __max = (max);		\
681bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
682bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
6831da177e4SLinus Torvalds 
68491f68b73SWu Fengguang 
68591f68b73SWu Fengguang /*
68691f68b73SWu Fengguang  * swap - swap value of @a and @b
68791f68b73SWu Fengguang  */
688ac7b9004SPeter Zijlstra #define swap(a, b) \
689ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
69091f68b73SWu Fengguang 
6911da177e4SLinus Torvalds /**
6921da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
6931da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
6941da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
6951da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
6961da177e4SLinus Torvalds  *
6971da177e4SLinus Torvalds  */
6981da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
6991da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
7001da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
7011da177e4SLinus Torvalds 
702d4d23addSKyle McMartin struct sysinfo;
703d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info);
704d4d23addSKyle McMartin 
7051da177e4SLinus Torvalds #endif /* __KERNEL__ */
7061da177e4SLinus Torvalds 
707c01226c3SArnd Bergmann #ifndef __EXPORTED_HEADERS__
708c01226c3SArnd Bergmann #ifndef __KERNEL__
709c01226c3SArnd Bergmann #warning Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders
710c01226c3SArnd Bergmann #endif /* __KERNEL__ */
711c01226c3SArnd Bergmann #endif /* __EXPORTED_HEADERS__ */
712c01226c3SArnd Bergmann 
7131da177e4SLinus Torvalds #define SI_LOAD_SHIFT	16
7141da177e4SLinus Torvalds struct sysinfo {
7151da177e4SLinus Torvalds 	long uptime;			/* Seconds since boot */
7161da177e4SLinus Torvalds 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
7171da177e4SLinus Torvalds 	unsigned long totalram;		/* Total usable main memory size */
7181da177e4SLinus Torvalds 	unsigned long freeram;		/* Available memory size */
7191da177e4SLinus Torvalds 	unsigned long sharedram;	/* Amount of shared memory */
7201da177e4SLinus Torvalds 	unsigned long bufferram;	/* Memory used by buffers */
7211da177e4SLinus Torvalds 	unsigned long totalswap;	/* Total swap space size */
7221da177e4SLinus Torvalds 	unsigned long freeswap;		/* swap space still available */
7231da177e4SLinus Torvalds 	unsigned short procs;		/* Number of current processes */
7241da177e4SLinus Torvalds 	unsigned short pad;		/* explicit padding for m68k */
7251da177e4SLinus Torvalds 	unsigned long totalhigh;	/* Total high memory size */
7261da177e4SLinus Torvalds 	unsigned long freehigh;		/* Available high memory size */
7271da177e4SLinus Torvalds 	unsigned int mem_unit;		/* Memory unit size in bytes */
7281da177e4SLinus Torvalds 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
7291da177e4SLinus Torvalds };
7301da177e4SLinus Torvalds 
731c0398ee6SNikita Danilov /* Force a compilation error if condition is true */
7328c87df45SJan Beulich #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
7338c87df45SJan Beulich 
7348c87df45SJan Beulich /* Force a compilation error if condition is constant and true */
7358c87df45SJan Beulich #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
7361da177e4SLinus Torvalds 
737*cc8ef6ebSRoland Dreier /* Force a compilation error if a constant expression is not a power of 2 */
738*cc8ef6ebSRoland Dreier #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
739*cc8ef6ebSRoland Dreier 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
740*cc8ef6ebSRoland Dreier 
7414552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a
7424552d5dcSJan Beulich    result (of value 0 and type size_t), so the expression can be used
7434552d5dcSJan Beulich    e.g. in a structure initializer (or where-ever else comma expressions
7444552d5dcSJan Beulich    aren't permitted). */
7458c87df45SJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
7468c87df45SJan Beulich #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
7474552d5dcSJan Beulich 
7481da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */
7491da177e4SLinus Torvalds #define __FUNCTION__ (__func__)
7501da177e4SLinus Torvalds 
75108e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */
75208e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA
75308e0f6a9SChristoph Lameter #define NUMA_BUILD 1
75408e0f6a9SChristoph Lameter #else
75508e0f6a9SChristoph Lameter #define NUMA_BUILD 0
75608e0f6a9SChristoph Lameter #endif
75708e0f6a9SChristoph Lameter 
75829e71abfSSteven Rostedt /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
75929e71abfSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
76029e71abfSSteven Rostedt # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
76129e71abfSSteven Rostedt #endif
76229e71abfSSteven Rostedt 
7631da177e4SLinus Torvalds #endif
764