xref: /linux-6.15/include/linux/kernel.h (revision dc88e460)
11da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H
21da177e4SLinus Torvalds #define _LINUX_KERNEL_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds /*
51da177e4SLinus Torvalds  * 'kernel.h' contains some often-used function prototypes etc
61da177e4SLinus Torvalds  */
7a79ff731SAlexey Dobriyan #define __ALIGN_KERNEL(x, a)		__ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
8a79ff731SAlexey Dobriyan #define __ALIGN_KERNEL_MASK(x, mask)	(((x) + (mask)) & ~(mask))
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #ifdef __KERNEL__
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <stdarg.h>
131da177e4SLinus Torvalds #include <linux/linkage.h>
141da177e4SLinus Torvalds #include <linux/stddef.h>
151da177e4SLinus Torvalds #include <linux/types.h>
161da177e4SLinus Torvalds #include <linux/compiler.h>
171da177e4SLinus Torvalds #include <linux/bitops.h>
18f0d1b0b3SDavid Howells #include <linux/log2.h>
19e0deaff4SAndrew Morton #include <linux/typecheck.h>
20968ab183SLinus Torvalds #include <linux/printk.h>
21e9d376f0SJason Baron #include <linux/dynamic_debug.h>
221da177e4SLinus Torvalds #include <asm/byteorder.h>
231da177e4SLinus Torvalds #include <asm/bug.h>
241da177e4SLinus Torvalds 
254be929beSAlexey Dobriyan #define USHRT_MAX	((u16)(~0U))
264be929beSAlexey Dobriyan #define SHRT_MAX	((s16)(USHRT_MAX>>1))
274be929beSAlexey Dobriyan #define SHRT_MIN	((s16)(-SHRT_MAX - 1))
281da177e4SLinus Torvalds #define INT_MAX		((int)(~0U>>1))
291da177e4SLinus Torvalds #define INT_MIN		(-INT_MAX - 1)
301da177e4SLinus Torvalds #define UINT_MAX	(~0U)
311da177e4SLinus Torvalds #define LONG_MAX	((long)(~0UL>>1))
321da177e4SLinus Torvalds #define LONG_MIN	(-LONG_MAX - 1)
331da177e4SLinus Torvalds #define ULONG_MAX	(~0UL)
34111ebb6eSOGAWA Hirofumi #define LLONG_MAX	((long long)(~0ULL>>1))
35111ebb6eSOGAWA Hirofumi #define LLONG_MIN	(-LLONG_MAX - 1)
36111ebb6eSOGAWA Hirofumi #define ULLONG_MAX	(~0ULL)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
391da177e4SLinus Torvalds 
40a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
419f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
42a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
43f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
442ea58144SLinus Torvalds 
45c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
46c5e631cfSRusty Russell 
479b3be9f9SYinghai Lu /*
489b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
499b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
509b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
519b3be9f9SYinghai Lu  * arguments just once each.
529b3be9f9SYinghai Lu  */
539b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
549b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
559b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
569b3be9f9SYinghai Lu 
574552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
58930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
59074e61ecSJames Morris 
60074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
61b28efd54SEric Paris #define roundup(x, y) (					\
62b28efd54SEric Paris {							\
636070bf35STetsuo Handa 	const typeof(y) __y = y;			\
64b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
65b28efd54SEric Paris }							\
66b28efd54SEric Paris )
67686a0f3dSEric Paris #define rounddown(x, y) (				\
68686a0f3dSEric Paris {							\
69686a0f3dSEric Paris 	typeof(x) __x = (x);				\
70686a0f3dSEric Paris 	__x - (__x % (y));				\
71686a0f3dSEric Paris }							\
72686a0f3dSEric Paris )
739fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
749fe06081SDarrick J. Wong {							\
759fe06081SDarrick J. Wong 	typeof(divisor) __divisor = divisor;		\
769fe06081SDarrick J. Wong 	(((x) + ((__divisor) / 2)) / (__divisor));	\
779fe06081SDarrick J. Wong }							\
789fe06081SDarrick J. Wong )
791da177e4SLinus Torvalds 
80ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
81ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
82ca31e146SEduard - Gabriel Munteanu 
8390c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
842da96acdSJens Axboe # include <asm/div64.h>
852da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
862da96acdSJens Axboe #else
872da96acdSJens Axboe # define sector_div(n, b)( \
882da96acdSJens Axboe { \
892da96acdSJens Axboe 	int _res; \
902da96acdSJens Axboe 	_res = (n) % (b); \
912da96acdSJens Axboe 	(n) /= (b); \
922da96acdSJens Axboe 	_res; \
932da96acdSJens Axboe } \
942da96acdSJens Axboe )
952da96acdSJens Axboe #endif
962da96acdSJens Axboe 
97218e180eSAndrew Morton /**
98218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
99218e180eSAndrew Morton  * @n: the number we're accessing
100218e180eSAndrew Morton  *
101218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
102218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
103218e180eSAndrew Morton  * 32-bits.
104218e180eSAndrew Morton  */
105218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
106218e180eSAndrew Morton 
107204b885eSJoerg Roedel /**
108204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
109204b885eSJoerg Roedel  * @n: the number we're accessing
110204b885eSJoerg Roedel  */
111204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
112204b885eSJoerg Roedel 
1131da177e4SLinus Torvalds struct completion;
114df2e71fbS[email protected] struct pt_regs;
115df2e71fbS[email protected] struct user;
1161da177e4SLinus Torvalds 
117070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
118070cb065SUwe Kleine-König extern int _cond_resched(void);
119070cb065SUwe Kleine-König # define might_resched() _cond_resched()
120070cb065SUwe Kleine-König #else
121070cb065SUwe Kleine-König # define might_resched() do { } while (0)
122070cb065SUwe Kleine-König #endif
123070cb065SUwe Kleine-König 
1247106a27bSRandy Dunlap #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
125d894837fSSimon Kagstrom   void __might_sleep(const char *file, int line, int preempt_offset);
1261da177e4SLinus Torvalds /**
1271da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1281da177e4SLinus Torvalds  *
1291da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
1301da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
1311da177e4SLinus Torvalds  *
1321da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
133e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1341da177e4SLinus Torvalds  * supposed to.
1351da177e4SLinus Torvalds  */
136f8cbd99bSIngo Molnar # define might_sleep() \
137e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
138f8cbd99bSIngo Molnar #else
139d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
140d894837fSSimon Kagstrom 				   int preempt_offset) { }
141f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
142f8cbd99bSIngo Molnar #endif
143f8cbd99bSIngo Molnar 
144368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
145f8cbd99bSIngo Molnar 
1461da177e4SLinus Torvalds #define abs(x) ({				\
147a49c59c0SRolf Eike Beer 		long __x = (x);			\
1481da177e4SLinus Torvalds 		(__x < 0) ? -__x : __x;		\
1491da177e4SLinus Torvalds 	})
1501da177e4SLinus Torvalds 
151658716d1SBrian Behlendorf #define abs64(x) ({				\
152658716d1SBrian Behlendorf 		s64 __x = (x);			\
153658716d1SBrian Behlendorf 		(__x < 0) ? -__x : __x;		\
154658716d1SBrian Behlendorf 	})
155658716d1SBrian Behlendorf 
1563ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING
1573ee1afa3SNick Piggin void might_fault(void);
1583ee1afa3SNick Piggin #else
1593ee1afa3SNick Piggin static inline void might_fault(void)
1603ee1afa3SNick Piggin {
1613ee1afa3SNick Piggin 	might_sleep();
1623ee1afa3SNick Piggin }
1633ee1afa3SNick Piggin #endif
1643ee1afa3SNick Piggin 
165e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
166c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
1671da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...)
168a586df06SAndi Kleen 	__attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
169dd287796SAndrew Morton extern void oops_enter(void);
170dd287796SAndrew Morton extern void oops_exit(void);
171863a6049SAnton Blanchard void print_oops_end_marker(void);
172dd287796SAndrew Morton extern int oops_may_print(void);
173ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code)
1741da177e4SLinus Torvalds 	ATTRIB_NORET;
1751da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long)
1761da177e4SLinus Torvalds 	ATTRIB_NORET;
1771da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
1781da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
1791da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
1801da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
181a55621f1SAndrew Morton extern int __must_check strict_strtoul(const char *, unsigned int, unsigned long *);
182a55621f1SAndrew Morton extern int __must_check strict_strtol(const char *, unsigned int, long *);
183a55621f1SAndrew Morton extern int __must_check strict_strtoull(const char *, unsigned int, unsigned long long *);
184a55621f1SAndrew Morton extern int __must_check strict_strtoll(const char *, unsigned int, long long *);
1851da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...)
1861da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 3)));
1871da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list)
1881da177e4SLinus Torvalds 	__attribute__ ((format (printf, 2, 0)));
1891da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...)
1901da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
1911da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1921da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
1931da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
1941da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 4)));
1951da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1961da177e4SLinus Torvalds 	__attribute__ ((format (printf, 3, 0)));
197e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
198e905914fSJeremy Fitzhardinge 	__attribute__ ((format (printf, 2, 3)));
19911443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...)
2021da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 3)));
2031da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list)
2041da177e4SLinus Torvalds 	__attribute__ ((format (scanf, 2, 0)));
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
2071da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
208d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
2091da177e4SLinus Torvalds 
2105e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
2111da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
2121da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
213ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
214ab7476cfSArjan van de Ven 
21504a2e6a5SEric W. Biederman struct pid;
21604a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
221e3e8a75dSKirill Korotaev extern void wake_up_klogd(void);
2221da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
223aa727107SAdrian Bunk extern int panic_timeout;
2241da177e4SLinus Torvalds extern int panic_on_oops;
2258da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
2265211a242SKurt Garloff extern int panic_on_io_nmi;
2271da177e4SLinus Torvalds extern const char *print_tainted(void);
22825ddbb18SAndi Kleen extern void add_taint(unsigned flag);
22925ddbb18SAndi Kleen extern int test_taint(unsigned flag);
23025ddbb18SAndi Kleen extern unsigned long get_taint(void);
231b920de1bSDavid Howells extern int root_mountflags;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /* Values used for system_state */
2341da177e4SLinus Torvalds extern enum system_states {
2351da177e4SLinus Torvalds 	SYSTEM_BOOTING,
2361da177e4SLinus Torvalds 	SYSTEM_RUNNING,
2371da177e4SLinus Torvalds 	SYSTEM_HALT,
2381da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
2391da177e4SLinus Torvalds 	SYSTEM_RESTART,
240729b4d4cSAlexey Starikovskiy 	SYSTEM_SUSPEND_DISK,
2411da177e4SLinus Torvalds } system_state;
2421da177e4SLinus Torvalds 
24325ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
24425ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
24525ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP		2
24625ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
24725ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
24825ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
24925ddbb18SAndi Kleen #define TAINT_USER			6
25025ddbb18SAndi Kleen #define TAINT_DIE			7
25125ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
25225ddbb18SAndi Kleen #define TAINT_WARN			9
25326e9a397SLinus Torvalds #define TAINT_CRAP			10
25492946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
2551da177e4SLinus Torvalds 
2563fc95772SHarvey Harrison extern const char hex_asc[];
2573fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
2583fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
2593fc95772SHarvey Harrison 
2603fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte)
2613fc95772SHarvey Harrison {
2623fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
2633fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
2643fc95772SHarvey Harrison 	return buf;
2653fc95772SHarvey Harrison }
26699eaf3c4SRandy Dunlap 
26790378889SAndy Shevchenko extern int hex_to_bin(char ch);
268*dc88e460SMimi Zohar extern void hex2bin(u8 *dst, const char *src, size_t count);
26990378889SAndy Shevchenko 
2708a64f336SJoe Perches /*
271526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
2722002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
2732002c258SSteven Rostedt  *
2742002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
2752002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
276156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
2772002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
2782002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
2792002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
2802002c258SSteven Rostedt  * to continue tracing.
2812002c258SSteven Rostedt  *
2822002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
2832002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
2842002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
2852002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
2862002c258SSteven Rostedt  *
2872002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
288526211bcSIngo Molnar  */
2892002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER
2902002c258SSteven Rostedt void tracing_on(void);
2912002c258SSteven Rostedt void tracing_off(void);
2922002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */
2932002c258SSteven Rostedt void tracing_off_permanent(void);
2942002c258SSteven Rostedt int tracing_is_on(void);
2952002c258SSteven Rostedt #else
2962002c258SSteven Rostedt static inline void tracing_on(void) { }
2972002c258SSteven Rostedt static inline void tracing_off(void) { }
2982002c258SSteven Rostedt static inline void tracing_off_permanent(void) { }
2992002c258SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
3002002c258SSteven Rostedt #endif
301cecbca96SFrederic Weisbecker 
302cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
303cecbca96SFrederic Weisbecker 	DUMP_NONE,
304cecbca96SFrederic Weisbecker 	DUMP_ALL,
305cecbca96SFrederic Weisbecker 	DUMP_ORIG,
306cecbca96SFrederic Weisbecker };
307cecbca96SFrederic Weisbecker 
308526211bcSIngo Molnar #ifdef CONFIG_TRACING
309526211bcSIngo Molnar extern void tracing_start(void);
310526211bcSIngo Molnar extern void tracing_stop(void);
311526211bcSIngo Molnar extern void ftrace_off_permanent(void);
312526211bcSIngo Molnar 
313769b0441SFrederic Weisbecker static inline void __attribute__ ((format (printf, 1, 2)))
314769b0441SFrederic Weisbecker ____trace_printk_check_format(const char *fmt, ...)
315769b0441SFrederic Weisbecker {
316769b0441SFrederic Weisbecker }
317769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
318769b0441SFrederic Weisbecker do {									\
319769b0441SFrederic Weisbecker 	if (0)								\
320769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
321769b0441SFrederic Weisbecker } while (0)
322769b0441SFrederic Weisbecker 
323526211bcSIngo Molnar /**
324526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
325526211bcSIngo Molnar  * @fmt: the printf format for printing
326526211bcSIngo Molnar  *
327526211bcSIngo Molnar  * Note: __trace_printk is an internal function for trace_printk and
328526211bcSIngo Molnar  *       the @ip is passed in via the trace_printk macro.
329526211bcSIngo Molnar  *
330526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
331526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
332526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
333526211bcSIngo Molnar  * where problems are occurring.
334526211bcSIngo Molnar  *
335526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
336526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
337526211bcSIngo Molnar  * your code.
338526211bcSIngo Molnar  */
339769b0441SFrederic Weisbecker 
340769b0441SFrederic Weisbecker #define trace_printk(fmt, args...)					\
341769b0441SFrederic Weisbecker do {									\
342769b0441SFrederic Weisbecker 	__trace_printk_check_format(fmt, ##args);			\
34348ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
34448ead020SFrederic Weisbecker 		static const char *trace_printk_fmt			\
34548ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
34648ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
34748ead020SFrederic Weisbecker 									\
34848ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
34948ead020SFrederic Weisbecker 	} else								\
35048ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);		\
351769b0441SFrederic Weisbecker } while (0)
352769b0441SFrederic Weisbecker 
353526211bcSIngo Molnar extern int
35448ead020SFrederic Weisbecker __trace_bprintk(unsigned long ip, const char *fmt, ...)
35548ead020SFrederic Weisbecker 	__attribute__ ((format (printf, 2, 3)));
35648ead020SFrederic Weisbecker 
35748ead020SFrederic Weisbecker extern int
358526211bcSIngo Molnar __trace_printk(unsigned long ip, const char *fmt, ...)
359526211bcSIngo Molnar 	__attribute__ ((format (printf, 2, 3)));
360769b0441SFrederic Weisbecker 
36103889384SSteven Rostedt extern void trace_dump_stack(void);
36203889384SSteven Rostedt 
36348ead020SFrederic Weisbecker /*
36448ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
36548ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
36648ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
36748ead020SFrederic Weisbecker  */
368769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
369769b0441SFrederic Weisbecker do {									\
37048ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
371769b0441SFrederic Weisbecker 		static const char *trace_printk_fmt			\
37248ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
37348ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
3747bffc23eSIngo Molnar 									\
37548ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
37648ead020SFrederic Weisbecker 	} else								\
37748ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
378769b0441SFrederic Weisbecker } while (0)
379769b0441SFrederic Weisbecker 
380526211bcSIngo Molnar extern int
38148ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
38248ead020SFrederic Weisbecker 
38348ead020SFrederic Weisbecker extern int
384526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
385769b0441SFrederic Weisbecker 
386cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
387526211bcSIngo Molnar #else
388526211bcSIngo Molnar static inline int
389526211bcSIngo Molnar trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
390526211bcSIngo Molnar 
391526211bcSIngo Molnar static inline void tracing_start(void) { }
392526211bcSIngo Molnar static inline void tracing_stop(void) { }
393526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { }
394e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { }
395526211bcSIngo Molnar static inline int
396526211bcSIngo Molnar trace_printk(const char *fmt, ...)
397526211bcSIngo Molnar {
398526211bcSIngo Molnar 	return 0;
399526211bcSIngo Molnar }
400526211bcSIngo Molnar static inline int
401526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
402526211bcSIngo Molnar {
403526211bcSIngo Molnar 	return 0;
404526211bcSIngo Molnar }
405cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
406769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
407526211bcSIngo Molnar 
408526211bcSIngo Molnar /*
409bdf4bbaaSHarvey Harrison  * min()/max()/clamp() macros that also do
4101da177e4SLinus Torvalds  * strict type-checking.. See the
4111da177e4SLinus Torvalds  * "unnecessary" pointer comparison.
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds #define min(x, y) ({				\
414bdf4bbaaSHarvey Harrison 	typeof(x) _min1 = (x);			\
415bdf4bbaaSHarvey Harrison 	typeof(y) _min2 = (y);			\
416bdf4bbaaSHarvey Harrison 	(void) (&_min1 == &_min2);		\
417bdf4bbaaSHarvey Harrison 	_min1 < _min2 ? _min1 : _min2; })
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds #define max(x, y) ({				\
420bdf4bbaaSHarvey Harrison 	typeof(x) _max1 = (x);			\
421bdf4bbaaSHarvey Harrison 	typeof(y) _max2 = (y);			\
422bdf4bbaaSHarvey Harrison 	(void) (&_max1 == &_max2);		\
423bdf4bbaaSHarvey Harrison 	_max1 > _max2 ? _max1 : _max2; })
424bdf4bbaaSHarvey Harrison 
425f27c85c5SHagen Paul Pfeifer #define min3(x, y, z) ({			\
426f27c85c5SHagen Paul Pfeifer 	typeof(x) _min1 = (x);			\
427f27c85c5SHagen Paul Pfeifer 	typeof(y) _min2 = (y);			\
428f27c85c5SHagen Paul Pfeifer 	typeof(z) _min3 = (z);			\
429f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min2);		\
430f27c85c5SHagen Paul Pfeifer 	(void) (&_min1 == &_min3);		\
431f27c85c5SHagen Paul Pfeifer 	_min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
432f27c85c5SHagen Paul Pfeifer 		(_min2 < _min3 ? _min2 : _min3); })
433f27c85c5SHagen Paul Pfeifer 
434f27c85c5SHagen Paul Pfeifer #define max3(x, y, z) ({			\
435f27c85c5SHagen Paul Pfeifer 	typeof(x) _max1 = (x);			\
436f27c85c5SHagen Paul Pfeifer 	typeof(y) _max2 = (y);			\
437f27c85c5SHagen Paul Pfeifer 	typeof(z) _max3 = (z);			\
438f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max2);		\
439f27c85c5SHagen Paul Pfeifer 	(void) (&_max1 == &_max3);		\
440f27c85c5SHagen Paul Pfeifer 	_max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
441f27c85c5SHagen Paul Pfeifer 		(_max2 > _max3 ? _max2 : _max3); })
442f27c85c5SHagen Paul Pfeifer 
443bdf4bbaaSHarvey Harrison /**
444c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
445c8bf1336SMartin K. Petersen  * @x: value1
446c8bf1336SMartin K. Petersen  * @y: value2
447c8bf1336SMartin K. Petersen  */
448c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
449c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
450c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
451c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
452c8bf1336SMartin K. Petersen 
453c8bf1336SMartin K. Petersen /**
454bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
455bdf4bbaaSHarvey Harrison  * @val: current value
456bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
457bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
458bdf4bbaaSHarvey Harrison  *
459bdf4bbaaSHarvey Harrison  * This macro does strict typechecking of min/max to make sure they are of the
460bdf4bbaaSHarvey Harrison  * same type as val.  See the unnecessary pointer comparisons.
461bdf4bbaaSHarvey Harrison  */
462bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({			\
463bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
464bdf4bbaaSHarvey Harrison 	typeof(min) __min = (min);		\
465bdf4bbaaSHarvey Harrison 	typeof(max) __max = (max);		\
466bdf4bbaaSHarvey Harrison 	(void) (&__val == &__min);		\
467bdf4bbaaSHarvey Harrison 	(void) (&__val == &__max);		\
468bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
469bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds /*
4721da177e4SLinus Torvalds  * ..and if you can't take the strict
4731da177e4SLinus Torvalds  * types, you can specify one yourself.
4741da177e4SLinus Torvalds  *
475bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
4761da177e4SLinus Torvalds  */
477bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({			\
478bdf4bbaaSHarvey Harrison 	type __min1 = (x);			\
479bdf4bbaaSHarvey Harrison 	type __min2 = (y);			\
480bdf4bbaaSHarvey Harrison 	__min1 < __min2 ? __min1: __min2; })
4811da177e4SLinus Torvalds 
482bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({			\
483bdf4bbaaSHarvey Harrison 	type __max1 = (x);			\
484bdf4bbaaSHarvey Harrison 	type __max2 = (y);			\
485bdf4bbaaSHarvey Harrison 	__max1 > __max2 ? __max1: __max2; })
486bdf4bbaaSHarvey Harrison 
487bdf4bbaaSHarvey Harrison /**
488bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
489bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
490bdf4bbaaSHarvey Harrison  * @val: current value
491bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
492bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
493bdf4bbaaSHarvey Harrison  *
494bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
495bdf4bbaaSHarvey Harrison  * 'type' to make all the comparisons.
496bdf4bbaaSHarvey Harrison  */
497bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({		\
498bdf4bbaaSHarvey Harrison 	type __val = (val);			\
499bdf4bbaaSHarvey Harrison 	type __min = (min);			\
500bdf4bbaaSHarvey Harrison 	type __max = (max);			\
501bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
502bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
503bdf4bbaaSHarvey Harrison 
504bdf4bbaaSHarvey Harrison /**
505bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
506bdf4bbaaSHarvey Harrison  * @val: current value
507bdf4bbaaSHarvey Harrison  * @min: minimum allowable value
508bdf4bbaaSHarvey Harrison  * @max: maximum allowable value
509bdf4bbaaSHarvey Harrison  *
510bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
511bdf4bbaaSHarvey Harrison  * type the input argument 'val' is.  This is useful when val is an unsigned
512bdf4bbaaSHarvey Harrison  * type and min and max are literals that will otherwise be assigned a signed
513bdf4bbaaSHarvey Harrison  * integer type.
514bdf4bbaaSHarvey Harrison  */
515bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({		\
516bdf4bbaaSHarvey Harrison 	typeof(val) __val = (val);		\
517bdf4bbaaSHarvey Harrison 	typeof(val) __min = (min);		\
518bdf4bbaaSHarvey Harrison 	typeof(val) __max = (max);		\
519bdf4bbaaSHarvey Harrison 	__val = __val < __min ? __min: __val;	\
520bdf4bbaaSHarvey Harrison 	__val > __max ? __max: __val; })
5211da177e4SLinus Torvalds 
52291f68b73SWu Fengguang 
52391f68b73SWu Fengguang /*
52491f68b73SWu Fengguang  * swap - swap value of @a and @b
52591f68b73SWu Fengguang  */
526ac7b9004SPeter Zijlstra #define swap(a, b) \
527ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
52891f68b73SWu Fengguang 
5291da177e4SLinus Torvalds /**
5301da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
5311da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
5321da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
5331da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
5341da177e4SLinus Torvalds  *
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds #define container_of(ptr, type, member) ({			\
5371da177e4SLinus Torvalds 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
5381da177e4SLinus Torvalds 	(type *)( (char *)__mptr - offsetof(type,member) );})
5391da177e4SLinus Torvalds 
540d4d23addSKyle McMartin struct sysinfo;
541d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info);
542d4d23addSKyle McMartin 
5431da177e4SLinus Torvalds #endif /* __KERNEL__ */
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds #define SI_LOAD_SHIFT	16
5461da177e4SLinus Torvalds struct sysinfo {
5471da177e4SLinus Torvalds 	long uptime;			/* Seconds since boot */
5481da177e4SLinus Torvalds 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
5491da177e4SLinus Torvalds 	unsigned long totalram;		/* Total usable main memory size */
5501da177e4SLinus Torvalds 	unsigned long freeram;		/* Available memory size */
5511da177e4SLinus Torvalds 	unsigned long sharedram;	/* Amount of shared memory */
5521da177e4SLinus Torvalds 	unsigned long bufferram;	/* Memory used by buffers */
5531da177e4SLinus Torvalds 	unsigned long totalswap;	/* Total swap space size */
5541da177e4SLinus Torvalds 	unsigned long freeswap;		/* swap space still available */
5551da177e4SLinus Torvalds 	unsigned short procs;		/* Number of current processes */
5561da177e4SLinus Torvalds 	unsigned short pad;		/* explicit padding for m68k */
5571da177e4SLinus Torvalds 	unsigned long totalhigh;	/* Total high memory size */
5581da177e4SLinus Torvalds 	unsigned long freehigh;		/* Available high memory size */
5591da177e4SLinus Torvalds 	unsigned int mem_unit;		/* Memory unit size in bytes */
5601da177e4SLinus Torvalds 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
5611da177e4SLinus Torvalds };
5621da177e4SLinus Torvalds 
563c0398ee6SNikita Danilov /* Force a compilation error if condition is true */
5648c87df45SJan Beulich #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
5658c87df45SJan Beulich 
5668c87df45SJan Beulich /* Force a compilation error if condition is constant and true */
5678c87df45SJan Beulich #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
5681da177e4SLinus Torvalds 
569cc8ef6ebSRoland Dreier /* Force a compilation error if a constant expression is not a power of 2 */
570cc8ef6ebSRoland Dreier #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
571cc8ef6ebSRoland Dreier 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
572cc8ef6ebSRoland Dreier 
5734552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a
5744552d5dcSJan Beulich    result (of value 0 and type size_t), so the expression can be used
5754552d5dcSJan Beulich    e.g. in a structure initializer (or where-ever else comma expressions
5764552d5dcSJan Beulich    aren't permitted). */
5778c87df45SJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
5788c87df45SJan Beulich #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
5794552d5dcSJan Beulich 
5801da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */
5811da177e4SLinus Torvalds #define __FUNCTION__ (__func__)
5821da177e4SLinus Torvalds 
58308e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */
58408e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA
58508e0f6a9SChristoph Lameter #define NUMA_BUILD 1
58608e0f6a9SChristoph Lameter #else
58708e0f6a9SChristoph Lameter #define NUMA_BUILD 0
58808e0f6a9SChristoph Lameter #endif
58908e0f6a9SChristoph Lameter 
59029e71abfSSteven Rostedt /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
59129e71abfSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
59229e71abfSSteven Rostedt # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
59329e71abfSSteven Rostedt #endif
59429e71abfSSteven Rostedt 
5951da177e4SLinus Torvalds #endif
596