kernel.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. #ifndef _LINUX_KERNEL_H
  2. #define _LINUX_KERNEL_H
  3. /*
  4. * 'kernel.h' contains some often-used function prototypes etc
  5. */
  6. #ifdef __KERNEL__
  7. #include <stdarg.h>
  8. #include <linux/linkage.h>
  9. #include <linux/stddef.h>
  10. #include <linux/types.h>
  11. #include <linux/compiler.h>
  12. #include <linux/bitops.h>
  13. #include <linux/log2.h>
  14. #include <linux/typecheck.h>
  15. #include <linux/ratelimit.h>
  16. #include <linux/dynamic_debug.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/bug.h>
  19. extern const char linux_banner[];
  20. extern const char linux_proc_banner[];
  21. #define USHORT_MAX ((u16)(~0U))
  22. #define SHORT_MAX ((s16)(USHORT_MAX>>1))
  23. #define SHORT_MIN (-SHORT_MAX - 1)
  24. #define INT_MAX ((int)(~0U>>1))
  25. #define INT_MIN (-INT_MAX - 1)
  26. #define UINT_MAX (~0U)
  27. #define LONG_MAX ((long)(~0UL>>1))
  28. #define LONG_MIN (-LONG_MAX - 1)
  29. #define ULONG_MAX (~0UL)
  30. #define LLONG_MAX ((long long)(~0ULL>>1))
  31. #define LLONG_MIN (-LLONG_MAX - 1)
  32. #define ULLONG_MAX (~0ULL)
  33. #define STACK_MAGIC 0xdeadbeef
  34. #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
  35. #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  36. #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
  37. #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
  38. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  39. #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
  40. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  41. #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
  42. #define DIV_ROUND_CLOSEST(x, divisor)( \
  43. { \
  44. typeof(divisor) __divisor = divisor; \
  45. (((x) + ((__divisor) / 2)) / (__divisor)); \
  46. } \
  47. )
  48. #define _RET_IP_ (unsigned long)__builtin_return_address(0)
  49. #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
  50. #ifdef CONFIG_LBD
  51. # include <asm/div64.h>
  52. # define sector_div(a, b) do_div(a, b)
  53. #else
  54. # define sector_div(n, b)( \
  55. { \
  56. int _res; \
  57. _res = (n) % (b); \
  58. (n) /= (b); \
  59. _res; \
  60. } \
  61. )
  62. #endif
  63. /**
  64. * upper_32_bits - return bits 32-63 of a number
  65. * @n: the number we're accessing
  66. *
  67. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  68. * the "right shift count >= width of type" warning when that quantity is
  69. * 32-bits.
  70. */
  71. #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
  72. /**
  73. * lower_32_bits - return bits 0-31 of a number
  74. * @n: the number we're accessing
  75. */
  76. #define lower_32_bits(n) ((u32)(n))
  77. #define KERN_EMERG "<0>" /* system is unusable */
  78. #define KERN_ALERT "<1>" /* action must be taken immediately */
  79. #define KERN_CRIT "<2>" /* critical conditions */
  80. #define KERN_ERR "<3>" /* error conditions */
  81. #define KERN_WARNING "<4>" /* warning conditions */
  82. #define KERN_NOTICE "<5>" /* normal but significant condition */
  83. #define KERN_INFO "<6>" /* informational */
  84. #define KERN_DEBUG "<7>" /* debug-level messages */
  85. /* Use the default kernel loglevel */
  86. #define KERN_DEFAULT "<d>"
  87. /*
  88. * Annotation for a "continued" line of log printout (only done after a
  89. * line that had no enclosing \n). Only to be used by core/arch code
  90. * during early bootup (a continued line is not SMP-safe otherwise).
  91. */
  92. #define KERN_CONT "<c>"
  93. extern int console_printk[];
  94. #define console_loglevel (console_printk[0])
  95. #define default_message_loglevel (console_printk[1])
  96. #define minimum_console_loglevel (console_printk[2])
  97. #define default_console_loglevel (console_printk[3])
  98. struct completion;
  99. struct pt_regs;
  100. struct user;
  101. #ifdef CONFIG_PREEMPT_VOLUNTARY
  102. extern int _cond_resched(void);
  103. # define might_resched() _cond_resched()
  104. #else
  105. # define might_resched() do { } while (0)
  106. #endif
  107. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  108. void __might_sleep(char *file, int line);
  109. /**
  110. * might_sleep - annotation for functions that can sleep
  111. *
  112. * this macro will print a stack trace if it is executed in an atomic
  113. * context (spinlock, irq-handler, ...).
  114. *
  115. * This is a useful debugging help to be able to catch problems early and not
  116. * be bitten later when the calling function happens to sleep when it is not
  117. * supposed to.
  118. */
  119. # define might_sleep() \
  120. do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
  121. #else
  122. # define might_sleep() do { might_resched(); } while (0)
  123. #endif
  124. #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
  125. #define abs(x) ({ \
  126. int __x = (x); \
  127. (__x < 0) ? -__x : __x; \
  128. })
  129. #ifdef CONFIG_PROVE_LOCKING
  130. void might_fault(void);
  131. #else
  132. static inline void might_fault(void)
  133. {
  134. might_sleep();
  135. }
  136. #endif
  137. extern struct atomic_notifier_head panic_notifier_list;
  138. extern long (*panic_blink)(long time);
  139. NORET_TYPE void panic(const char * fmt, ...)
  140. __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
  141. extern void oops_enter(void);
  142. extern void oops_exit(void);
  143. extern int oops_may_print(void);
  144. NORET_TYPE void do_exit(long error_code)
  145. ATTRIB_NORET;
  146. NORET_TYPE void complete_and_exit(struct completion *, long)
  147. ATTRIB_NORET;
  148. extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  149. extern long simple_strtol(const char *,char **,unsigned int);
  150. extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
  151. extern long long simple_strtoll(const char *,char **,unsigned int);
  152. extern int strict_strtoul(const char *, unsigned int, unsigned long *);
  153. extern int strict_strtol(const char *, unsigned int, long *);
  154. extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
  155. extern int strict_strtoll(const char *, unsigned int, long long *);
  156. extern int sprintf(char * buf, const char * fmt, ...)
  157. __attribute__ ((format (printf, 2, 3)));
  158. extern int vsprintf(char *buf, const char *, va_list)
  159. __attribute__ ((format (printf, 2, 0)));
  160. extern int snprintf(char * buf, size_t size, const char * fmt, ...)
  161. __attribute__ ((format (printf, 3, 4)));
  162. extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  163. __attribute__ ((format (printf, 3, 0)));
  164. extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
  165. __attribute__ ((format (printf, 3, 4)));
  166. extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  167. __attribute__ ((format (printf, 3, 0)));
  168. extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
  169. __attribute__ ((format (printf, 2, 3)));
  170. extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
  171. extern int sscanf(const char *, const char *, ...)
  172. __attribute__ ((format (scanf, 2, 3)));
  173. extern int vsscanf(const char *, const char *, va_list)
  174. __attribute__ ((format (scanf, 2, 0)));
  175. extern int get_option(char **str, int *pint);
  176. extern char *get_options(const char *str, int nints, int *ints);
  177. extern unsigned long long memparse(const char *ptr, char **retptr);
  178. extern int core_kernel_text(unsigned long addr);
  179. extern int __kernel_text_address(unsigned long addr);
  180. extern int kernel_text_address(unsigned long addr);
  181. extern int func_ptr_is_kernel_text(void *ptr);
  182. struct pid;
  183. extern struct pid *session_of_pgrp(struct pid *pgrp);
  184. /*
  185. * FW_BUG
  186. * Add this to a message where you are sure the firmware is buggy or behaves
  187. * really stupid or out of spec. Be aware that the responsible BIOS developer
  188. * should be able to fix this issue or at least get a concrete idea of the
  189. * problem by reading your message without the need of looking at the kernel
  190. * code.
  191. *
  192. * Use it for definite and high priority BIOS bugs.
  193. *
  194. * FW_WARN
  195. * Use it for not that clear (e.g. could the kernel messed up things already?)
  196. * and medium priority BIOS bugs.
  197. *
  198. * FW_INFO
  199. * Use this one if you want to tell the user or vendor about something
  200. * suspicious, but generally harmless related to the firmware.
  201. *
  202. * Use it for information or very low priority BIOS bugs.
  203. */
  204. #define FW_BUG "[Firmware Bug]: "
  205. #define FW_WARN "[Firmware Warn]: "
  206. #define FW_INFO "[Firmware Info]: "
  207. #ifdef CONFIG_PRINTK
  208. asmlinkage int vprintk(const char *fmt, va_list args)
  209. __attribute__ ((format (printf, 1, 0)));
  210. asmlinkage int printk(const char * fmt, ...)
  211. __attribute__ ((format (printf, 1, 2))) __cold;
  212. extern struct ratelimit_state printk_ratelimit_state;
  213. extern int printk_ratelimit(void);
  214. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  215. unsigned int interval_msec);
  216. /*
  217. * Print a one-time message (analogous to WARN_ONCE() et al):
  218. */
  219. #define printk_once(x...) ({ \
  220. static int __print_once = 1; \
  221. \
  222. if (__print_once) { \
  223. __print_once = 0; \
  224. printk(x); \
  225. } \
  226. })
  227. void log_buf_kexec_setup(void);
  228. #else
  229. static inline int vprintk(const char *s, va_list args)
  230. __attribute__ ((format (printf, 1, 0)));
  231. static inline int vprintk(const char *s, va_list args) { return 0; }
  232. static inline int printk(const char *s, ...)
  233. __attribute__ ((format (printf, 1, 2)));
  234. static inline int __cold printk(const char *s, ...) { return 0; }
  235. static inline int printk_ratelimit(void) { return 0; }
  236. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
  237. unsigned int interval_msec) \
  238. { return false; }
  239. /* No effect, but we still get type checking even in the !PRINTK case: */
  240. #define printk_once(x...) printk(x)
  241. static inline void log_buf_kexec_setup(void)
  242. {
  243. }
  244. #endif
  245. extern int printk_needs_cpu(int cpu);
  246. extern void printk_tick(void);
  247. extern void asmlinkage __attribute__((format(printf, 1, 2)))
  248. early_printk(const char *fmt, ...);
  249. unsigned long int_sqrt(unsigned long);
  250. static inline void console_silent(void)
  251. {
  252. console_loglevel = 0;
  253. }
  254. static inline void console_verbose(void)
  255. {
  256. if (console_loglevel)
  257. console_loglevel = 15;
  258. }
  259. extern void bust_spinlocks(int yes);
  260. extern void wake_up_klogd(void);
  261. extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
  262. extern int panic_timeout;
  263. extern int panic_on_oops;
  264. extern int panic_on_unrecovered_nmi;
  265. extern const char *print_tainted(void);
  266. extern void add_taint(unsigned flag);
  267. extern int test_taint(unsigned flag);
  268. extern unsigned long get_taint(void);
  269. extern int root_mountflags;
  270. /* Values used for system_state */
  271. extern enum system_states {
  272. SYSTEM_BOOTING,
  273. SYSTEM_RUNNING,
  274. SYSTEM_HALT,
  275. SYSTEM_POWER_OFF,
  276. SYSTEM_RESTART,
  277. SYSTEM_SUSPEND_DISK,
  278. } system_state;
  279. #define TAINT_PROPRIETARY_MODULE 0
  280. #define TAINT_FORCED_MODULE 1
  281. #define TAINT_UNSAFE_SMP 2
  282. #define TAINT_FORCED_RMMOD 3
  283. #define TAINT_MACHINE_CHECK 4
  284. #define TAINT_BAD_PAGE 5
  285. #define TAINT_USER 6
  286. #define TAINT_DIE 7
  287. #define TAINT_OVERRIDDEN_ACPI_TABLE 8
  288. #define TAINT_WARN 9
  289. #define TAINT_CRAP 10
  290. extern void dump_stack(void) __cold;
  291. enum {
  292. DUMP_PREFIX_NONE,
  293. DUMP_PREFIX_ADDRESS,
  294. DUMP_PREFIX_OFFSET
  295. };
  296. extern void hex_dump_to_buffer(const void *buf, size_t len,
  297. int rowsize, int groupsize,
  298. char *linebuf, size_t linebuflen, bool ascii);
  299. extern void print_hex_dump(const char *level, const char *prefix_str,
  300. int prefix_type, int rowsize, int groupsize,
  301. const void *buf, size_t len, bool ascii);
  302. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  303. const void *buf, size_t len);
  304. extern const char hex_asc[];
  305. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  306. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  307. static inline char *pack_hex_byte(char *buf, u8 byte)
  308. {
  309. *buf++ = hex_asc_hi(byte);
  310. *buf++ = hex_asc_lo(byte);
  311. return buf;
  312. }
  313. #ifndef pr_fmt
  314. #define pr_fmt(fmt) fmt
  315. #endif
  316. #define pr_emerg(fmt, ...) \
  317. printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  318. #define pr_alert(fmt, ...) \
  319. printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  320. #define pr_crit(fmt, ...) \
  321. printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  322. #define pr_err(fmt, ...) \
  323. printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  324. #define pr_warning(fmt, ...) \
  325. printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  326. #define pr_notice(fmt, ...) \
  327. printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  328. #define pr_info(fmt, ...) \
  329. printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  330. #define pr_cont(fmt, ...) \
  331. printk(KERN_CONT fmt, ##__VA_ARGS__)
  332. /* pr_devel() should produce zero code unless DEBUG is defined */
  333. #ifdef DEBUG
  334. #define pr_devel(fmt, ...) \
  335. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  336. #else
  337. #define pr_devel(fmt, ...) \
  338. ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
  339. #endif
  340. /* If you are writing a driver, please use dev_dbg instead */
  341. #if defined(DEBUG)
  342. #define pr_debug(fmt, ...) \
  343. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  344. #elif defined(CONFIG_DYNAMIC_DEBUG)
  345. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  346. #define pr_debug(fmt, ...) do { \
  347. dynamic_pr_debug(fmt, ##__VA_ARGS__); \
  348. } while (0)
  349. #else
  350. #define pr_debug(fmt, ...) \
  351. ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
  352. #endif
  353. /*
  354. * General tracing related utility functions - trace_printk(),
  355. * tracing_on/tracing_off and tracing_start()/tracing_stop
  356. *
  357. * Use tracing_on/tracing_off when you want to quickly turn on or off
  358. * tracing. It simply enables or disables the recording of the trace events.
  359. * This also corresponds to the user space debugfs/tracing/tracing_on
  360. * file, which gives a means for the kernel and userspace to interact.
  361. * Place a tracing_off() in the kernel where you want tracing to end.
  362. * From user space, examine the trace, and then echo 1 > tracing_on
  363. * to continue tracing.
  364. *
  365. * tracing_stop/tracing_start has slightly more overhead. It is used
  366. * by things like suspend to ram where disabling the recording of the
  367. * trace is not enough, but tracing must actually stop because things
  368. * like calling smp_processor_id() may crash the system.
  369. *
  370. * Most likely, you want to use tracing_on/tracing_off.
  371. */
  372. #ifdef CONFIG_RING_BUFFER
  373. void tracing_on(void);
  374. void tracing_off(void);
  375. /* trace_off_permanent stops recording with no way to bring it back */
  376. void tracing_off_permanent(void);
  377. int tracing_is_on(void);
  378. #else
  379. static inline void tracing_on(void) { }
  380. static inline void tracing_off(void) { }
  381. static inline void tracing_off_permanent(void) { }
  382. static inline int tracing_is_on(void) { return 0; }
  383. #endif
  384. #ifdef CONFIG_TRACING
  385. extern void tracing_start(void);
  386. extern void tracing_stop(void);
  387. extern void ftrace_off_permanent(void);
  388. extern void
  389. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
  390. static inline void __attribute__ ((format (printf, 1, 2)))
  391. ____trace_printk_check_format(const char *fmt, ...)
  392. {
  393. }
  394. #define __trace_printk_check_format(fmt, args...) \
  395. do { \
  396. if (0) \
  397. ____trace_printk_check_format(fmt, ##args); \
  398. } while (0)
  399. /**
  400. * trace_printk - printf formatting in the ftrace buffer
  401. * @fmt: the printf format for printing
  402. *
  403. * Note: __trace_printk is an internal function for trace_printk and
  404. * the @ip is passed in via the trace_printk macro.
  405. *
  406. * This function allows a kernel developer to debug fast path sections
  407. * that printk is not appropriate for. By scattering in various
  408. * printk like tracing in the code, a developer can quickly see
  409. * where problems are occurring.
  410. *
  411. * This is intended as a debugging tool for the developer only.
  412. * Please refrain from leaving trace_printks scattered around in
  413. * your code.
  414. */
  415. #define trace_printk(fmt, args...) \
  416. do { \
  417. __trace_printk_check_format(fmt, ##args); \
  418. if (__builtin_constant_p(fmt)) { \
  419. static const char *trace_printk_fmt \
  420. __attribute__((section("__trace_printk_fmt"))) = \
  421. __builtin_constant_p(fmt) ? fmt : NULL; \
  422. \
  423. __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
  424. } else \
  425. __trace_printk(_THIS_IP_, fmt, ##args); \
  426. } while (0)
  427. extern int
  428. __trace_bprintk(unsigned long ip, const char *fmt, ...)
  429. __attribute__ ((format (printf, 2, 3)));
  430. extern int
  431. __trace_printk(unsigned long ip, const char *fmt, ...)
  432. __attribute__ ((format (printf, 2, 3)));
  433. /*
  434. * The double __builtin_constant_p is because gcc will give us an error
  435. * if we try to allocate the static variable to fmt if it is not a
  436. * constant. Even with the outer if statement.
  437. */
  438. #define ftrace_vprintk(fmt, vargs) \
  439. do { \
  440. if (__builtin_constant_p(fmt)) { \
  441. static const char *trace_printk_fmt \
  442. __attribute__((section("__trace_printk_fmt"))) = \
  443. __builtin_constant_p(fmt) ? fmt : NULL; \
  444. \
  445. __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
  446. } else \
  447. __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
  448. } while (0)
  449. extern int
  450. __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
  451. extern int
  452. __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
  453. extern void ftrace_dump(void);
  454. #else
  455. static inline void
  456. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
  457. static inline int
  458. trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  459. static inline void tracing_start(void) { }
  460. static inline void tracing_stop(void) { }
  461. static inline void ftrace_off_permanent(void) { }
  462. static inline int
  463. trace_printk(const char *fmt, ...)
  464. {
  465. return 0;
  466. }
  467. static inline int
  468. ftrace_vprintk(const char *fmt, va_list ap)
  469. {
  470. return 0;
  471. }
  472. static inline void ftrace_dump(void) { }
  473. #endif /* CONFIG_TRACING */
  474. /*
  475. * Display an IP address in readable format.
  476. */
  477. #define NIPQUAD(addr) \
  478. ((unsigned char *)&addr)[0], \
  479. ((unsigned char *)&addr)[1], \
  480. ((unsigned char *)&addr)[2], \
  481. ((unsigned char *)&addr)[3]
  482. #define NIPQUAD_FMT "%u.%u.%u.%u"
  483. /*
  484. * min()/max()/clamp() macros that also do
  485. * strict type-checking.. See the
  486. * "unnecessary" pointer comparison.
  487. */
  488. #define min(x, y) ({ \
  489. typeof(x) _min1 = (x); \
  490. typeof(y) _min2 = (y); \
  491. (void) (&_min1 == &_min2); \
  492. _min1 < _min2 ? _min1 : _min2; })
  493. #define max(x, y) ({ \
  494. typeof(x) _max1 = (x); \
  495. typeof(y) _max2 = (y); \
  496. (void) (&_max1 == &_max2); \
  497. _max1 > _max2 ? _max1 : _max2; })
  498. /**
  499. * clamp - return a value clamped to a given range with strict typechecking
  500. * @val: current value
  501. * @min: minimum allowable value
  502. * @max: maximum allowable value
  503. *
  504. * This macro does strict typechecking of min/max to make sure they are of the
  505. * same type as val. See the unnecessary pointer comparisons.
  506. */
  507. #define clamp(val, min, max) ({ \
  508. typeof(val) __val = (val); \
  509. typeof(min) __min = (min); \
  510. typeof(max) __max = (max); \
  511. (void) (&__val == &__min); \
  512. (void) (&__val == &__max); \
  513. __val = __val < __min ? __min: __val; \
  514. __val > __max ? __max: __val; })
  515. /*
  516. * ..and if you can't take the strict
  517. * types, you can specify one yourself.
  518. *
  519. * Or not use min/max/clamp at all, of course.
  520. */
  521. #define min_t(type, x, y) ({ \
  522. type __min1 = (x); \
  523. type __min2 = (y); \
  524. __min1 < __min2 ? __min1: __min2; })
  525. #define max_t(type, x, y) ({ \
  526. type __max1 = (x); \
  527. type __max2 = (y); \
  528. __max1 > __max2 ? __max1: __max2; })
  529. /**
  530. * clamp_t - return a value clamped to a given range using a given type
  531. * @type: the type of variable to use
  532. * @val: current value
  533. * @min: minimum allowable value
  534. * @max: maximum allowable value
  535. *
  536. * This macro does no typechecking and uses temporary variables of type
  537. * 'type' to make all the comparisons.
  538. */
  539. #define clamp_t(type, val, min, max) ({ \
  540. type __val = (val); \
  541. type __min = (min); \
  542. type __max = (max); \
  543. __val = __val < __min ? __min: __val; \
  544. __val > __max ? __max: __val; })
  545. /**
  546. * clamp_val - return a value clamped to a given range using val's type
  547. * @val: current value
  548. * @min: minimum allowable value
  549. * @max: maximum allowable value
  550. *
  551. * This macro does no typechecking and uses temporary variables of whatever
  552. * type the input argument 'val' is. This is useful when val is an unsigned
  553. * type and min and max are literals that will otherwise be assigned a signed
  554. * integer type.
  555. */
  556. #define clamp_val(val, min, max) ({ \
  557. typeof(val) __val = (val); \
  558. typeof(val) __min = (min); \
  559. typeof(val) __max = (max); \
  560. __val = __val < __min ? __min: __val; \
  561. __val > __max ? __max: __val; })
  562. /*
  563. * swap - swap value of @a and @b
  564. */
  565. #define swap(a, b) \
  566. do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
  567. /**
  568. * container_of - cast a member of a structure out to the containing structure
  569. * @ptr: the pointer to the member.
  570. * @type: the type of the container struct this is embedded in.
  571. * @member: the name of the member within the struct.
  572. *
  573. */
  574. #define container_of(ptr, type, member) ({ \
  575. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  576. (type *)( (char *)__mptr - offsetof(type,member) );})
  577. struct sysinfo;
  578. extern int do_sysinfo(struct sysinfo *info);
  579. #endif /* __KERNEL__ */
  580. #define SI_LOAD_SHIFT 16
  581. struct sysinfo {
  582. long uptime; /* Seconds since boot */
  583. unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  584. unsigned long totalram; /* Total usable main memory size */
  585. unsigned long freeram; /* Available memory size */
  586. unsigned long sharedram; /* Amount of shared memory */
  587. unsigned long bufferram; /* Memory used by buffers */
  588. unsigned long totalswap; /* Total swap space size */
  589. unsigned long freeswap; /* swap space still available */
  590. unsigned short procs; /* Number of current processes */
  591. unsigned short pad; /* explicit padding for m68k */
  592. unsigned long totalhigh; /* Total high memory size */
  593. unsigned long freehigh; /* Available high memory size */
  594. unsigned int mem_unit; /* Memory unit size in bytes */
  595. char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
  596. };
  597. /* Force a compilation error if condition is true */
  598. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  599. /* Force a compilation error if condition is true, but also produce a
  600. result (of value 0 and type size_t), so the expression can be used
  601. e.g. in a structure initializer (or where-ever else comma expressions
  602. aren't permitted). */
  603. #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
  604. /* Trap pasters of __FUNCTION__ at compile-time */
  605. #define __FUNCTION__ (__func__)
  606. /* This helps us to avoid #ifdef CONFIG_NUMA */
  607. #ifdef CONFIG_NUMA
  608. #define NUMA_BUILD 1
  609. #else
  610. #define NUMA_BUILD 0
  611. #endif
  612. /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
  613. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  614. # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
  615. #endif
  616. #endif