kernel: add roundup() code comment from akpm
[linux-flexiantxendom0.git] / include / linux / kernel.h
1 #ifndef _LINUX_KERNEL_H
2 #define _LINUX_KERNEL_H
3
4 /*
5  * 'kernel.h' contains some often-used function prototypes etc
6  */
7 #define __ALIGN_KERNEL(x, a)            __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
8 #define __ALIGN_KERNEL_MASK(x, mask)    (((x) + (mask)) & ~(mask))
9
10 #ifdef __KERNEL__
11
12 #include <stdarg.h>
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>
22 #include <asm/bug.h>
23
24 extern const char linux_banner[];
25 extern const char linux_proc_banner[];
26
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)
39
40 #define STACK_MAGIC     0xdeadbeef
41
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)
46
47 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
48
49 /*
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.
54  */
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))
58
59 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
60 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
61
62 /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
63 #define roundup(x, y) (                                 \
64 {                                                       \
65         const typeof(y) __y = y;                        \
66         (((x) + (__y - 1)) / __y) * __y;                \
67 }                                                       \
68 )
69 #define rounddown(x, y) (                               \
70 {                                                       \
71         typeof(x) __x = (x);                            \
72         __x - (__x % (y));                              \
73 }                                                       \
74 )
75 #define DIV_ROUND_CLOSEST(x, divisor)(                  \
76 {                                                       \
77         typeof(divisor) __divisor = divisor;            \
78         (((x) + ((__divisor) / 2)) / (__divisor));      \
79 }                                                       \
80 )
81
82 #define _RET_IP_                (unsigned long)__builtin_return_address(0)
83 #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
84
85 #ifdef CONFIG_LBDAF
86 # include <asm/div64.h>
87 # define sector_div(a, b) do_div(a, b)
88 #else
89 # define sector_div(n, b)( \
90 { \
91         int _res; \
92         _res = (n) % (b); \
93         (n) /= (b); \
94         _res; \
95 } \
96 )
97 #endif
98
99 /**
100  * upper_32_bits - return bits 32-63 of a number
101  * @n: the number we're accessing
102  *
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
105  * 32-bits.
106  */
107 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
108
109 /**
110  * lower_32_bits - return bits 0-31 of a number
111  * @n: the number we're accessing
112  */
113 #define lower_32_bits(n) ((u32)(n))
114
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                 */
123
124 /* Use the default kernel loglevel */
125 #define KERN_DEFAULT    "<d>"
126 /*
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).
130  */
131 #define KERN_CONT       "<c>"
132
133 extern int console_printk[];
134
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])
139
140 struct completion;
141 struct pt_regs;
142 struct user;
143
144 #ifdef CONFIG_PREEMPT_VOLUNTARY
145 extern int _cond_resched(void);
146 # define might_resched() _cond_resched()
147 #else
148 # define might_resched() do { } while (0)
149 #endif
150
151 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
152   void __might_sleep(const char *file, int line, int preempt_offset);
153 /**
154  * might_sleep - annotation for functions that can sleep
155  *
156  * this macro will print a stack trace if it is executed in an atomic
157  * context (spinlock, irq-handler, ...).
158  *
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
161  * supposed to.
162  */
163 # define might_sleep() \
164         do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
165 #else
166   static inline void __might_sleep(const char *file, int line,
167                                    int preempt_offset) { }
168 # define might_sleep() do { might_resched(); } while (0)
169 #endif
170
171 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
172
173 #define abs(x) ({                               \
174                 long __x = (x);                 \
175                 (__x < 0) ? -__x : __x;         \
176         })
177
178 #define abs64(x) ({                             \
179                 s64 __x = (x);                  \
180                 (__x < 0) ? -__x : __x;         \
181         })
182
183 #ifdef CONFIG_PROVE_LOCKING
184 void might_fault(void);
185 #else
186 static inline void might_fault(void)
187 {
188         might_sleep();
189 }
190 #endif
191
192 struct va_format {
193         const char *fmt;
194         va_list *va;
195 };
196
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)
206         ATTRIB_NORET;
207 NORET_TYPE void complete_and_exit(struct completion *, long)
208         ATTRIB_NORET;
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);
232
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)));
237
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);
241
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);
246
247 struct pid;
248 extern struct pid *session_of_pgrp(struct pid *pgrp);
249
250 /*
251  * FW_BUG
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
256  * code.
257  * 
258  * Use it for definite and high priority BIOS bugs.
259  *
260  * FW_WARN
261  * Use it for not that clear (e.g. could the kernel messed up things already?)
262  * and medium priority BIOS bugs.
263  *
264  * FW_INFO
265  * Use this one if you want to tell the user or vendor about something
266  * suspicious, but generally harmless related to the firmware.
267  *
268  * Use it for information or very low priority BIOS bugs.
269  */
270 #define FW_BUG          "[Firmware Bug]: "
271 #define FW_WARN         "[Firmware Warn]: "
272 #define FW_INFO         "[Firmware Info]: "
273
274 /*
275  * HW_ERR
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.
278  */
279 #define HW_ERR          "[Hardware Error]: "
280
281 #ifdef CONFIG_PRINTK
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;
286
287 /*
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().
291  */
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);
296
297 extern int printk_delay_msec;
298
299 /*
300  * Print a one-time message (analogous to WARN_ONCE() et al):
301  */
302 #define printk_once(x...) ({                    \
303         static bool __print_once;               \
304                                                 \
305         if (!__print_once) {                    \
306                 __print_once = true;            \
307                 printk(x);                      \
308         }                                       \
309 })
310
311 void log_buf_kexec_setup(void);
312 #else
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)   \
322                 { return false; }
323
324 /* No effect, but we still get type checking even in the !PRINTK case: */
325 #define printk_once(x...) printk(x)
326
327 static inline void log_buf_kexec_setup(void)
328 {
329 }
330 #endif
331
332 /*
333  * Dummy printk for disabled debugging statements to use whilst maintaining
334  * gcc's format and side-effect checking.
335  */
336 static inline __attribute__ ((format (printf, 1, 2)))
337 int no_printk(const char *s, ...) { return 0; }
338
339 extern int printk_needs_cpu(int cpu);
340 extern void printk_tick(void);
341
342 extern void asmlinkage __attribute__((format(printf, 1, 2)))
343         early_printk(const char *fmt, ...);
344
345 unsigned long int_sqrt(unsigned long);
346
347 static inline void console_silent(void)
348 {
349         console_loglevel = 0;
350 }
351
352 static inline void console_verbose(void)
353 {
354         if (console_loglevel)
355                 console_loglevel = 15;
356 }
357
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;
370
371 /* Values used for system_state */
372 extern enum system_states {
373         SYSTEM_BOOTING,
374         SYSTEM_RUNNING,
375         SYSTEM_HALT,
376         SYSTEM_POWER_OFF,
377         SYSTEM_RESTART,
378         SYSTEM_SUSPEND_DISK,
379 } system_state;
380
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
387 #define TAINT_USER                      6
388 #define TAINT_DIE                       7
389 #define TAINT_OVERRIDDEN_ACPI_TABLE     8
390 #define TAINT_WARN                      9
391 #define TAINT_CRAP                      10
392 #define TAINT_FIRMWARE_WORKAROUND       11
393
394 extern void dump_stack(void) __cold;
395
396 enum {
397         DUMP_PREFIX_NONE,
398         DUMP_PREFIX_ADDRESS,
399         DUMP_PREFIX_OFFSET
400 };
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);
409
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]
413
414 static inline char *pack_hex_byte(char *buf, u8 byte)
415 {
416         *buf++ = hex_asc_hi(byte);
417         *buf++ = hex_asc_lo(byte);
418         return buf;
419 }
420
421 extern int hex_to_bin(char ch);
422
423 #ifndef pr_fmt
424 #define pr_fmt(fmt) fmt
425 #endif
426
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__)
444
445 /* pr_devel() should produce zero code unless DEBUG is defined */
446 #ifdef DEBUG
447 #define pr_devel(fmt, ...) \
448         printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
449 #else
450 #define pr_devel(fmt, ...) \
451         ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
452 #endif
453
454 /* If you are writing a driver, please use dev_dbg instead */
455 #if defined(DEBUG)
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__)
462 #else
463 #define pr_debug(fmt, ...) \
464         ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
465 #endif
466
467 /*
468  * ratelimited messages with local ratelimit_state,
469  * no local ratelimit_state used in the !PRINTK case
470  */
471 #ifdef CONFIG_PRINTK
472 #define printk_ratelimited(fmt, ...)  ({                                \
473         static DEFINE_RATELIMIT_STATE(_rs,                              \
474                                       DEFAULT_RATELIMIT_INTERVAL,       \
475                                       DEFAULT_RATELIMIT_BURST);         \
476                                                                         \
477         if (__ratelimit(&_rs))                                          \
478                 printk(fmt, ##__VA_ARGS__);                             \
479 })
480 #else
481 /* No effect, but we still get type checking even in the !PRINTK case: */
482 #define printk_ratelimited printk
483 #endif
484
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 */
502 #if defined(DEBUG)
503 #define pr_debug_ratelimited(fmt, ...) \
504         printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
505 #else
506 #define pr_debug_ratelimited(fmt, ...) \
507         ({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \
508                                      ##__VA_ARGS__); 0; })
509 #endif
510
511 /*
512  * General tracing related utility functions - trace_printk(),
513  * tracing_on/tracing_off and tracing_start()/tracing_stop
514  *
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.
522  *
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.
527  *
528  * Most likely, you want to use tracing_on/tracing_off.
529  */
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);
536 #else
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; }
541 #endif
542
543 enum ftrace_dump_mode {
544         DUMP_NONE,
545         DUMP_ALL,
546         DUMP_ORIG,
547 };
548
549 #ifdef CONFIG_TRACING
550 extern void tracing_start(void);
551 extern void tracing_stop(void);
552 extern void ftrace_off_permanent(void);
553
554 static inline void __attribute__ ((format (printf, 1, 2)))
555 ____trace_printk_check_format(const char *fmt, ...)
556 {
557 }
558 #define __trace_printk_check_format(fmt, args...)                       \
559 do {                                                                    \
560         if (0)                                                          \
561                 ____trace_printk_check_format(fmt, ##args);             \
562 } while (0)
563
564 /**
565  * trace_printk - printf formatting in the ftrace buffer
566  * @fmt: the printf format for printing
567  *
568  * Note: __trace_printk is an internal function for trace_printk and
569  *       the @ip is passed in via the trace_printk macro.
570  *
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.
575  *
576  * This is intended as a debugging tool for the developer only.
577  * Please refrain from leaving trace_printks scattered around in
578  * your code.
579  */
580
581 #define trace_printk(fmt, args...)                                      \
582 do {                                                                    \
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;         \
588                                                                         \
589                 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);   \
590         } else                                                          \
591                 __trace_printk(_THIS_IP_, fmt, ##args);         \
592 } while (0)
593
594 extern int
595 __trace_bprintk(unsigned long ip, const char *fmt, ...)
596         __attribute__ ((format (printf, 2, 3)));
597
598 extern int
599 __trace_printk(unsigned long ip, const char *fmt, ...)
600         __attribute__ ((format (printf, 2, 3)));
601
602 extern void trace_dump_stack(void);
603
604 /*
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.
608  */
609 #define ftrace_vprintk(fmt, vargs)                                      \
610 do {                                                                    \
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;         \
615                                                                         \
616                 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);  \
617         } else                                                          \
618                 __ftrace_vprintk(_THIS_IP_, fmt, vargs);                \
619 } while (0)
620
621 extern int
622 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
623
624 extern int
625 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
626
627 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
628 #else
629 static inline int
630 trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
631
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) { }
636 static inline int
637 trace_printk(const char *fmt, ...)
638 {
639         return 0;
640 }
641 static inline int
642 ftrace_vprintk(const char *fmt, va_list ap)
643 {
644         return 0;
645 }
646 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
647 #endif /* CONFIG_TRACING */
648
649 /*
650  * min()/max()/clamp() macros that also do
651  * strict type-checking.. See the
652  * "unnecessary" pointer comparison.
653  */
654 #define min(x, y) ({                            \
655         typeof(x) _min1 = (x);                  \
656         typeof(y) _min2 = (y);                  \
657         (void) (&_min1 == &_min2);              \
658         _min1 < _min2 ? _min1 : _min2; })
659
660 #define max(x, y) ({                            \
661         typeof(x) _max1 = (x);                  \
662         typeof(y) _max2 = (y);                  \
663         (void) (&_max1 == &_max2);              \
664         _max1 > _max2 ? _max1 : _max2; })
665
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); })
674
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); })
683
684 /**
685  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
686  * @x: value1
687  * @y: value2
688  */
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)); })
693
694 /**
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
699  *
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.
702  */
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; })
711
712 /*
713  * ..and if you can't take the strict
714  * types, you can specify one yourself.
715  *
716  * Or not use min/max/clamp at all, of course.
717  */
718 #define min_t(type, x, y) ({                    \
719         type __min1 = (x);                      \
720         type __min2 = (y);                      \
721         __min1 < __min2 ? __min1: __min2; })
722
723 #define max_t(type, x, y) ({                    \
724         type __max1 = (x);                      \
725         type __max2 = (y);                      \
726         __max1 > __max2 ? __max1: __max2; })
727
728 /**
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
734  *
735  * This macro does no typechecking and uses temporary variables of type
736  * 'type' to make all the comparisons.
737  */
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; })
744
745 /**
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
750  *
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
754  * integer type.
755  */
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; })
762
763
764 /*
765  * swap - swap value of @a and @b
766  */
767 #define swap(a, b) \
768         do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
769
770 /**
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.
775  *
776  */
777 #define container_of(ptr, type, member) ({                      \
778         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
779         (type *)( (char *)__mptr - offsetof(type,member) );})
780
781 struct sysinfo;
782 extern int do_sysinfo(struct sysinfo *info);
783
784 #endif /* __KERNEL__ */
785
786 #define SI_LOAD_SHIFT   16
787 struct sysinfo {
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.. */
802 };
803
804 /* Force a compilation error if condition is true */
805 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
806
807 /* Force a compilation error if condition is constant and true */
808 #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
809
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))
813
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); }))
820
821 /* Trap pasters of __FUNCTION__ at compile-time */
822 #define __FUNCTION__ (__func__)
823
824 /* This helps us to avoid #ifdef CONFIG_NUMA */
825 #ifdef CONFIG_NUMA
826 #define NUMA_BUILD 1
827 #else
828 #define NUMA_BUILD 0
829 #endif
830
831 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
832 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
833 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
834 #endif
835
836 #endif