1 #ifndef _LINUX_KERNEL_H
2 #define _LINUX_KERNEL_H
5 * 'kernel.h' contains some often-used function prototypes etc
7 #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
8 #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
13 #include <linux/linkage.h>
14 #include <linux/stddef.h>
15 #include <linux/types.h>
16 #include <linux/compiler.h>
17 #include <linux/bitops.h>
18 #include <linux/log2.h>
19 #include <linux/typecheck.h>
20 #include <linux/dynamic_debug.h>
21 #include <asm/byteorder.h>
24 extern const char linux_banner[];
25 extern const char linux_proc_banner[];
27 #define USHRT_MAX ((u16)(~0U))
28 #define SHRT_MAX ((s16)(USHRT_MAX>>1))
29 #define SHRT_MIN ((s16)(-SHRT_MAX - 1))
30 #define INT_MAX ((int)(~0U>>1))
31 #define INT_MIN (-INT_MAX - 1)
32 #define UINT_MAX (~0U)
33 #define LONG_MAX ((long)(~0UL>>1))
34 #define LONG_MIN (-LONG_MAX - 1)
35 #define ULONG_MAX (~0UL)
36 #define LLONG_MAX ((long long)(~0ULL>>1))
37 #define LLONG_MIN (-LLONG_MAX - 1)
38 #define ULLONG_MAX (~0ULL)
40 #define STACK_MAGIC 0xdeadbeef
42 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
43 #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
44 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
45 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
47 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
50 * This looks more complex than it should be. But we need to
51 * get the type for the ~ right in round_down (it needs to be
52 * as wide as the result!), and we want to evaluate the macro
53 * arguments just once each.
55 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
56 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
57 #define round_down(x, y) ((x) & ~__round_mask(x, y))
59 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
60 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
62 /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
63 #define roundup(x, y) ( \
65 const typeof(y) __y = y; \
66 (((x) + (__y - 1)) / __y) * __y; \
69 #define rounddown(x, y) ( \
71 typeof(x) __x = (x); \
75 #define DIV_ROUND_CLOSEST(x, divisor)( \
77 typeof(divisor) __divisor = divisor; \
78 (((x) + ((__divisor) / 2)) / (__divisor)); \
82 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
83 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
86 # include <asm/div64.h>
87 # define sector_div(a, b) do_div(a, b)
89 # define sector_div(n, b)( \
100 * upper_32_bits - return bits 32-63 of a number
101 * @n: the number we're accessing
103 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
104 * the "right shift count >= width of type" warning when that quantity is
107 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
110 * lower_32_bits - return bits 0-31 of a number
111 * @n: the number we're accessing
113 #define lower_32_bits(n) ((u32)(n))
115 #define KERN_EMERG "<0>" /* system is unusable */
116 #define KERN_ALERT "<1>" /* action must be taken immediately */
117 #define KERN_CRIT "<2>" /* critical conditions */
118 #define KERN_ERR "<3>" /* error conditions */
119 #define KERN_WARNING "<4>" /* warning conditions */
120 #define KERN_NOTICE "<5>" /* normal but significant condition */
121 #define KERN_INFO "<6>" /* informational */
122 #define KERN_DEBUG "<7>" /* debug-level messages */
124 /* Use the default kernel loglevel */
125 #define KERN_DEFAULT "<d>"
127 * Annotation for a "continued" line of log printout (only done after a
128 * line that had no enclosing \n). Only to be used by core/arch code
129 * during early bootup (a continued line is not SMP-safe otherwise).
131 #define KERN_CONT "<c>"
133 extern int console_printk[];
135 #define console_loglevel (console_printk[0])
136 #define default_message_loglevel (console_printk[1])
137 #define minimum_console_loglevel (console_printk[2])
138 #define default_console_loglevel (console_printk[3])
144 #ifdef CONFIG_PREEMPT_VOLUNTARY
145 extern int _cond_resched(void);
146 # define might_resched() _cond_resched()
148 # define might_resched() do { } while (0)
151 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
152 void __might_sleep(const char *file, int line, int preempt_offset);
154 * might_sleep - annotation for functions that can sleep
156 * this macro will print a stack trace if it is executed in an atomic
157 * context (spinlock, irq-handler, ...).
159 * This is a useful debugging help to be able to catch problems early and not
160 * be bitten later when the calling function happens to sleep when it is not
163 # define might_sleep() \
164 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
166 static inline void __might_sleep(const char *file, int line,
167 int preempt_offset) { }
168 # define might_sleep() do { might_resched(); } while (0)
171 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
175 (__x < 0) ? -__x : __x; \
178 #define abs64(x) ({ \
180 (__x < 0) ? -__x : __x; \
183 #ifdef CONFIG_PROVE_LOCKING
184 void might_fault(void);
186 static inline void might_fault(void)
197 extern struct atomic_notifier_head panic_notifier_list;
198 extern long (*panic_blink)(int state);
199 NORET_TYPE void panic(const char * fmt, ...)
200 __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
201 extern void oops_enter(void);
202 extern void oops_exit(void);
203 void print_oops_end_marker(void);
204 extern int oops_may_print(void);
205 NORET_TYPE void do_exit(long error_code)
207 NORET_TYPE void complete_and_exit(struct completion *, long)
209 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
210 extern long simple_strtol(const char *,char **,unsigned int);
211 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
212 extern long long simple_strtoll(const char *,char **,unsigned int);
213 extern int __must_check strict_strtoul(const char *, unsigned int, unsigned long *);
214 extern int __must_check strict_strtol(const char *, unsigned int, long *);
215 extern int __must_check strict_strtoull(const char *, unsigned int, unsigned long long *);
216 extern int __must_check strict_strtoll(const char *, unsigned int, long long *);
217 extern int sprintf(char * buf, const char * fmt, ...)
218 __attribute__ ((format (printf, 2, 3)));
219 extern int vsprintf(char *buf, const char *, va_list)
220 __attribute__ ((format (printf, 2, 0)));
221 extern int snprintf(char * buf, size_t size, const char * fmt, ...)
222 __attribute__ ((format (printf, 3, 4)));
223 extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
224 __attribute__ ((format (printf, 3, 0)));
225 extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
226 __attribute__ ((format (printf, 3, 4)));
227 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
228 __attribute__ ((format (printf, 3, 0)));
229 extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
230 __attribute__ ((format (printf, 2, 3)));
231 extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
233 extern int sscanf(const char *, const char *, ...)
234 __attribute__ ((format (scanf, 2, 3)));
235 extern int vsscanf(const char *, const char *, va_list)
236 __attribute__ ((format (scanf, 2, 0)));
238 extern int get_option(char **str, int *pint);
239 extern char *get_options(const char *str, int nints, int *ints);
240 extern unsigned long long memparse(const char *ptr, char **retptr);
242 extern int core_kernel_text(unsigned long addr);
243 extern int __kernel_text_address(unsigned long addr);
244 extern int kernel_text_address(unsigned long addr);
245 extern int func_ptr_is_kernel_text(void *ptr);
248 extern struct pid *session_of_pgrp(struct pid *pgrp);
252 * Add this to a message where you are sure the firmware is buggy or behaves
253 * really stupid or out of spec. Be aware that the responsible BIOS developer
254 * should be able to fix this issue or at least get a concrete idea of the
255 * problem by reading your message without the need of looking at the kernel
258 * Use it for definite and high priority BIOS bugs.
261 * Use it for not that clear (e.g. could the kernel messed up things already?)
262 * and medium priority BIOS bugs.
265 * Use this one if you want to tell the user or vendor about something
266 * suspicious, but generally harmless related to the firmware.
268 * Use it for information or very low priority BIOS bugs.
270 #define FW_BUG "[Firmware Bug]: "
271 #define FW_WARN "[Firmware Warn]: "
272 #define FW_INFO "[Firmware Info]: "
276 * Add this to a message for hardware errors, so that user can report
277 * it to hardware vendor instead of LKML or software vendor.
279 #define HW_ERR "[Hardware Error]: "
282 asmlinkage int vprintk(const char *fmt, va_list args)
283 __attribute__ ((format (printf, 1, 0)));
284 asmlinkage int printk(const char * fmt, ...)
285 __attribute__ ((format (printf, 1, 2))) __cold;
288 * Please don't use printk_ratelimit(), because it shares ratelimiting state
289 * with all other unrelated printk_ratelimit() callsites. Instead use
290 * printk_ratelimited() or plain old __ratelimit().
292 extern int __printk_ratelimit(const char *func);
293 #define printk_ratelimit() __printk_ratelimit(__func__)
294 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
295 unsigned int interval_msec);
297 extern int printk_delay_msec;
300 * Print a one-time message (analogous to WARN_ONCE() et al):
302 #define printk_once(x...) ({ \
303 static bool __print_once; \
305 if (!__print_once) { \
306 __print_once = true; \
311 void log_buf_kexec_setup(void);
313 static inline int vprintk(const char *s, va_list args)
314 __attribute__ ((format (printf, 1, 0)));
315 static inline int vprintk(const char *s, va_list args) { return 0; }
316 static inline int printk(const char *s, ...)
317 __attribute__ ((format (printf, 1, 2)));
318 static inline int __cold printk(const char *s, ...) { return 0; }
319 static inline int printk_ratelimit(void) { return 0; }
320 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
321 unsigned int interval_msec) \
324 /* No effect, but we still get type checking even in the !PRINTK case: */
325 #define printk_once(x...) printk(x)
327 static inline void log_buf_kexec_setup(void)
333 * Dummy printk for disabled debugging statements to use whilst maintaining
334 * gcc's format and side-effect checking.
336 static inline __attribute__ ((format (printf, 1, 2)))
337 int no_printk(const char *s, ...) { return 0; }
339 extern int printk_needs_cpu(int cpu);
340 extern void printk_tick(void);
342 extern void asmlinkage __attribute__((format(printf, 1, 2)))
343 early_printk(const char *fmt, ...);
345 unsigned long int_sqrt(unsigned long);
347 static inline void console_silent(void)
349 console_loglevel = 0;
352 static inline void console_verbose(void)
354 if (console_loglevel)
355 console_loglevel = 15;
358 extern void bust_spinlocks(int yes);
359 extern void wake_up_klogd(void);
360 extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
361 extern int panic_timeout;
362 extern int panic_on_oops;
363 extern int panic_on_unrecovered_nmi;
364 extern int panic_on_io_nmi;
365 extern const char *print_tainted(void);
366 extern void add_taint(unsigned flag);
367 extern int test_taint(unsigned flag);
368 extern unsigned long get_taint(void);
369 extern int root_mountflags;
371 /* Values used for system_state */
372 extern enum system_states {
381 #define TAINT_PROPRIETARY_MODULE 0
382 #define TAINT_FORCED_MODULE 1
383 #define TAINT_UNSAFE_SMP 2
384 #define TAINT_FORCED_RMMOD 3
385 #define TAINT_MACHINE_CHECK 4
386 #define TAINT_BAD_PAGE 5
389 #define TAINT_OVERRIDDEN_ACPI_TABLE 8
391 #define TAINT_CRAP 10
392 #define TAINT_FIRMWARE_WORKAROUND 11
394 extern void dump_stack(void) __cold;
401 extern void hex_dump_to_buffer(const void *buf, size_t len,
402 int rowsize, int groupsize,
403 char *linebuf, size_t linebuflen, bool ascii);
404 extern void print_hex_dump(const char *level, const char *prefix_str,
405 int prefix_type, int rowsize, int groupsize,
406 const void *buf, size_t len, bool ascii);
407 extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
408 const void *buf, size_t len);
410 extern const char hex_asc[];
411 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
412 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
414 static inline char *pack_hex_byte(char *buf, u8 byte)
416 *buf++ = hex_asc_hi(byte);
417 *buf++ = hex_asc_lo(byte);
421 extern int hex_to_bin(char ch);
424 #define pr_fmt(fmt) fmt
427 #define pr_emerg(fmt, ...) \
428 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
429 #define pr_alert(fmt, ...) \
430 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
431 #define pr_crit(fmt, ...) \
432 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
433 #define pr_err(fmt, ...) \
434 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
435 #define pr_warning(fmt, ...) \
436 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
437 #define pr_warn pr_warning
438 #define pr_notice(fmt, ...) \
439 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
440 #define pr_info(fmt, ...) \
441 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
442 #define pr_cont(fmt, ...) \
443 printk(KERN_CONT fmt, ##__VA_ARGS__)
445 /* pr_devel() should produce zero code unless DEBUG is defined */
447 #define pr_devel(fmt, ...) \
448 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
450 #define pr_devel(fmt, ...) \
451 ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
454 /* If you are writing a driver, please use dev_dbg instead */
456 #define pr_debug(fmt, ...) \
457 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
458 #elif defined(CONFIG_DYNAMIC_DEBUG)
459 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
460 #define pr_debug(fmt, ...) \
461 dynamic_pr_debug(fmt, ##__VA_ARGS__)
463 #define pr_debug(fmt, ...) \
464 ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
468 * ratelimited messages with local ratelimit_state,
469 * no local ratelimit_state used in the !PRINTK case
472 #define printk_ratelimited(fmt, ...) ({ \
473 static DEFINE_RATELIMIT_STATE(_rs, \
474 DEFAULT_RATELIMIT_INTERVAL, \
475 DEFAULT_RATELIMIT_BURST); \
477 if (__ratelimit(&_rs)) \
478 printk(fmt, ##__VA_ARGS__); \
481 /* No effect, but we still get type checking even in the !PRINTK case: */
482 #define printk_ratelimited printk
485 #define pr_emerg_ratelimited(fmt, ...) \
486 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
487 #define pr_alert_ratelimited(fmt, ...) \
488 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
489 #define pr_crit_ratelimited(fmt, ...) \
490 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
491 #define pr_err_ratelimited(fmt, ...) \
492 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
493 #define pr_warning_ratelimited(fmt, ...) \
494 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
495 #define pr_warn_ratelimited pr_warning_ratelimited
496 #define pr_notice_ratelimited(fmt, ...) \
497 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
498 #define pr_info_ratelimited(fmt, ...) \
499 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
500 /* no pr_cont_ratelimited, don't do that... */
501 /* If you are writing a driver, please use dev_dbg instead */
503 #define pr_debug_ratelimited(fmt, ...) \
504 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
506 #define pr_debug_ratelimited(fmt, ...) \
507 ({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \
508 ##__VA_ARGS__); 0; })
512 * General tracing related utility functions - trace_printk(),
513 * tracing_on/tracing_off and tracing_start()/tracing_stop
515 * Use tracing_on/tracing_off when you want to quickly turn on or off
516 * tracing. It simply enables or disables the recording of the trace events.
517 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
518 * file, which gives a means for the kernel and userspace to interact.
519 * Place a tracing_off() in the kernel where you want tracing to end.
520 * From user space, examine the trace, and then echo 1 > tracing_on
521 * to continue tracing.
523 * tracing_stop/tracing_start has slightly more overhead. It is used
524 * by things like suspend to ram where disabling the recording of the
525 * trace is not enough, but tracing must actually stop because things
526 * like calling smp_processor_id() may crash the system.
528 * Most likely, you want to use tracing_on/tracing_off.
530 #ifdef CONFIG_RING_BUFFER
531 void tracing_on(void);
532 void tracing_off(void);
533 /* trace_off_permanent stops recording with no way to bring it back */
534 void tracing_off_permanent(void);
535 int tracing_is_on(void);
537 static inline void tracing_on(void) { }
538 static inline void tracing_off(void) { }
539 static inline void tracing_off_permanent(void) { }
540 static inline int tracing_is_on(void) { return 0; }
543 enum ftrace_dump_mode {
549 #ifdef CONFIG_TRACING
550 extern void tracing_start(void);
551 extern void tracing_stop(void);
552 extern void ftrace_off_permanent(void);
554 static inline void __attribute__ ((format (printf, 1, 2)))
555 ____trace_printk_check_format(const char *fmt, ...)
558 #define __trace_printk_check_format(fmt, args...) \
561 ____trace_printk_check_format(fmt, ##args); \
565 * trace_printk - printf formatting in the ftrace buffer
566 * @fmt: the printf format for printing
568 * Note: __trace_printk is an internal function for trace_printk and
569 * the @ip is passed in via the trace_printk macro.
571 * This function allows a kernel developer to debug fast path sections
572 * that printk is not appropriate for. By scattering in various
573 * printk like tracing in the code, a developer can quickly see
574 * where problems are occurring.
576 * This is intended as a debugging tool for the developer only.
577 * Please refrain from leaving trace_printks scattered around in
581 #define trace_printk(fmt, args...) \
583 __trace_printk_check_format(fmt, ##args); \
584 if (__builtin_constant_p(fmt)) { \
585 static const char *trace_printk_fmt \
586 __attribute__((section("__trace_printk_fmt"))) = \
587 __builtin_constant_p(fmt) ? fmt : NULL; \
589 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
591 __trace_printk(_THIS_IP_, fmt, ##args); \
595 __trace_bprintk(unsigned long ip, const char *fmt, ...)
596 __attribute__ ((format (printf, 2, 3)));
599 __trace_printk(unsigned long ip, const char *fmt, ...)
600 __attribute__ ((format (printf, 2, 3)));
602 extern void trace_dump_stack(void);
605 * The double __builtin_constant_p is because gcc will give us an error
606 * if we try to allocate the static variable to fmt if it is not a
607 * constant. Even with the outer if statement.
609 #define ftrace_vprintk(fmt, vargs) \
611 if (__builtin_constant_p(fmt)) { \
612 static const char *trace_printk_fmt \
613 __attribute__((section("__trace_printk_fmt"))) = \
614 __builtin_constant_p(fmt) ? fmt : NULL; \
616 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
618 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
622 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
625 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
627 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
630 trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
632 static inline void tracing_start(void) { }
633 static inline void tracing_stop(void) { }
634 static inline void ftrace_off_permanent(void) { }
635 static inline void trace_dump_stack(void) { }
637 trace_printk(const char *fmt, ...)
642 ftrace_vprintk(const char *fmt, va_list ap)
646 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
647 #endif /* CONFIG_TRACING */
650 * min()/max()/clamp() macros that also do
651 * strict type-checking.. See the
652 * "unnecessary" pointer comparison.
654 #define min(x, y) ({ \
655 typeof(x) _min1 = (x); \
656 typeof(y) _min2 = (y); \
657 (void) (&_min1 == &_min2); \
658 _min1 < _min2 ? _min1 : _min2; })
660 #define max(x, y) ({ \
661 typeof(x) _max1 = (x); \
662 typeof(y) _max2 = (y); \
663 (void) (&_max1 == &_max2); \
664 _max1 > _max2 ? _max1 : _max2; })
666 #define min3(x, y, z) ({ \
667 typeof(x) _min1 = (x); \
668 typeof(y) _min2 = (y); \
669 typeof(z) _min3 = (z); \
670 (void) (&_min1 == &_min2); \
671 (void) (&_min1 == &_min3); \
672 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
673 (_min2 < _min3 ? _min2 : _min3); })
675 #define max3(x, y, z) ({ \
676 typeof(x) _max1 = (x); \
677 typeof(y) _max2 = (y); \
678 typeof(z) _max3 = (z); \
679 (void) (&_max1 == &_max2); \
680 (void) (&_max1 == &_max3); \
681 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
682 (_max2 > _max3 ? _max2 : _max3); })
685 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
689 #define min_not_zero(x, y) ({ \
690 typeof(x) __x = (x); \
691 typeof(y) __y = (y); \
692 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
695 * clamp - return a value clamped to a given range with strict typechecking
696 * @val: current value
697 * @min: minimum allowable value
698 * @max: maximum allowable value
700 * This macro does strict typechecking of min/max to make sure they are of the
701 * same type as val. See the unnecessary pointer comparisons.
703 #define clamp(val, min, max) ({ \
704 typeof(val) __val = (val); \
705 typeof(min) __min = (min); \
706 typeof(max) __max = (max); \
707 (void) (&__val == &__min); \
708 (void) (&__val == &__max); \
709 __val = __val < __min ? __min: __val; \
710 __val > __max ? __max: __val; })
713 * ..and if you can't take the strict
714 * types, you can specify one yourself.
716 * Or not use min/max/clamp at all, of course.
718 #define min_t(type, x, y) ({ \
721 __min1 < __min2 ? __min1: __min2; })
723 #define max_t(type, x, y) ({ \
726 __max1 > __max2 ? __max1: __max2; })
729 * clamp_t - return a value clamped to a given range using a given type
730 * @type: the type of variable to use
731 * @val: current value
732 * @min: minimum allowable value
733 * @max: maximum allowable value
735 * This macro does no typechecking and uses temporary variables of type
736 * 'type' to make all the comparisons.
738 #define clamp_t(type, val, min, max) ({ \
739 type __val = (val); \
740 type __min = (min); \
741 type __max = (max); \
742 __val = __val < __min ? __min: __val; \
743 __val > __max ? __max: __val; })
746 * clamp_val - return a value clamped to a given range using val's type
747 * @val: current value
748 * @min: minimum allowable value
749 * @max: maximum allowable value
751 * This macro does no typechecking and uses temporary variables of whatever
752 * type the input argument 'val' is. This is useful when val is an unsigned
753 * type and min and max are literals that will otherwise be assigned a signed
756 #define clamp_val(val, min, max) ({ \
757 typeof(val) __val = (val); \
758 typeof(val) __min = (min); \
759 typeof(val) __max = (max); \
760 __val = __val < __min ? __min: __val; \
761 __val > __max ? __max: __val; })
765 * swap - swap value of @a and @b
768 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
771 * container_of - cast a member of a structure out to the containing structure
772 * @ptr: the pointer to the member.
773 * @type: the type of the container struct this is embedded in.
774 * @member: the name of the member within the struct.
777 #define container_of(ptr, type, member) ({ \
778 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
779 (type *)( (char *)__mptr - offsetof(type,member) );})
782 extern int do_sysinfo(struct sysinfo *info);
784 #endif /* __KERNEL__ */
786 #define SI_LOAD_SHIFT 16
788 long uptime; /* Seconds since boot */
789 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
790 unsigned long totalram; /* Total usable main memory size */
791 unsigned long freeram; /* Available memory size */
792 unsigned long sharedram; /* Amount of shared memory */
793 unsigned long bufferram; /* Memory used by buffers */
794 unsigned long totalswap; /* Total swap space size */
795 unsigned long freeswap; /* swap space still available */
796 unsigned short procs; /* Number of current processes */
797 unsigned short pad; /* explicit padding for m68k */
798 unsigned long totalhigh; /* Total high memory size */
799 unsigned long freehigh; /* Available high memory size */
800 unsigned int mem_unit; /* Memory unit size in bytes */
801 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
804 /* Force a compilation error if condition is true */
805 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
807 /* Force a compilation error if condition is constant and true */
808 #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
810 /* Force a compilation error if a constant expression is not a power of 2 */
811 #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
812 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
814 /* Force a compilation error if condition is true, but also produce a
815 result (of value 0 and type size_t), so the expression can be used
816 e.g. in a structure initializer (or where-ever else comma expressions
817 aren't permitted). */
818 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
819 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
821 /* Trap pasters of __FUNCTION__ at compile-time */
822 #define __FUNCTION__ (__func__)
824 /* This helps us to avoid #ifdef CONFIG_NUMA */
831 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
832 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
833 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD