bug.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef _ASM_GENERIC_BUG_H
  2. #define _ASM_GENERIC_BUG_H
  3. #include <linux/compiler.h>
  4. #ifdef CONFIG_GENERIC_BUG
  5. #define BUGFLAG_WARNING (1 << 0)
  6. #define BUGFLAG_ONCE (1 << 1)
  7. #define BUGFLAG_DONE (1 << 2)
  8. #define BUGFLAG_TAINT(taint) ((taint) << 8)
  9. #define BUG_GET_TAINT(bug) ((bug)->flags >> 8)
  10. #endif
  11. #ifndef __ASSEMBLY__
  12. #include <linux/kernel.h>
  13. #ifdef CONFIG_BUG
  14. #ifdef CONFIG_GENERIC_BUG
  15. struct bug_entry {
  16. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  17. unsigned long bug_addr;
  18. #else
  19. signed int bug_addr_disp;
  20. #endif
  21. #ifdef CONFIG_DEBUG_BUGVERBOSE
  22. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  23. const char *file;
  24. #else
  25. signed int file_disp;
  26. #endif
  27. unsigned short line;
  28. #endif
  29. unsigned short flags;
  30. };
  31. #endif /* CONFIG_GENERIC_BUG */
  32. /*
  33. * Don't use BUG() or BUG_ON() unless there's really no way out; one
  34. * example might be detecting data structure corruption in the middle
  35. * of an operation that can't be backed out of. If the (sub)system
  36. * can somehow continue operating, perhaps with reduced functionality,
  37. * it's probably not BUG-worthy.
  38. *
  39. * If you're tempted to BUG(), think again: is completely giving up
  40. * really the *only* solution? There are usually better options, where
  41. * users don't need to reboot ASAP and can mostly shut down cleanly.
  42. */
  43. #ifndef HAVE_ARCH_BUG
  44. #define BUG() do { \
  45. printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
  46. panic("BUG!"); \
  47. } while (0)
  48. #endif
  49. #ifndef HAVE_ARCH_BUG_ON
  50. #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
  51. #endif
  52. #ifdef __WARN_FLAGS
  53. #define __WARN_TAINT(taint) __WARN_FLAGS(BUGFLAG_TAINT(taint))
  54. #define __WARN_ONCE_TAINT(taint) __WARN_FLAGS(BUGFLAG_ONCE|BUGFLAG_TAINT(taint))
  55. #define WARN_ON_ONCE(condition) ({ \
  56. int __ret_warn_on = !!(condition); \
  57. if (unlikely(__ret_warn_on)) \
  58. __WARN_ONCE_TAINT(TAINT_WARN); \
  59. unlikely(__ret_warn_on); \
  60. })
  61. #endif
  62. /*
  63. * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
  64. * significant issues that need prompt attention if they should ever
  65. * appear at runtime. Use the versions with printk format strings
  66. * to provide better diagnostics.
  67. */
  68. #ifndef __WARN_TAINT
  69. extern __printf(3, 4)
  70. void warn_slowpath_fmt(const char *file, const int line,
  71. const char *fmt, ...);
  72. extern __printf(4, 5)
  73. void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
  74. const char *fmt, ...);
  75. extern void warn_slowpath_null(const char *file, const int line);
  76. #define WANT_WARN_ON_SLOWPATH
  77. #define __WARN() warn_slowpath_null(__FILE__, __LINE__)
  78. #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)
  79. #define __WARN_printf_taint(taint, arg...) \
  80. warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg)
  81. #else
  82. #define __WARN() __WARN_TAINT(TAINT_WARN)
  83. #define __WARN_printf(arg...) do { printk(arg); __WARN(); } while (0)
  84. #define __WARN_printf_taint(taint, arg...) \
  85. do { printk(arg); __WARN_TAINT(taint); } while (0)
  86. #endif
  87. /* used internally by panic.c */
  88. struct warn_args;
  89. struct pt_regs;
  90. void __warn(const char *file, int line, void *caller, unsigned taint,
  91. struct pt_regs *regs, struct warn_args *args);
  92. #ifndef WARN_ON
  93. #define WARN_ON(condition) ({ \
  94. int __ret_warn_on = !!(condition); \
  95. if (unlikely(__ret_warn_on)) \
  96. __WARN(); \
  97. unlikely(__ret_warn_on); \
  98. })
  99. #endif
  100. #ifndef WARN
  101. #define WARN(condition, format...) ({ \
  102. int __ret_warn_on = !!(condition); \
  103. if (unlikely(__ret_warn_on)) \
  104. __WARN_printf(format); \
  105. unlikely(__ret_warn_on); \
  106. })
  107. #endif
  108. #define WARN_TAINT(condition, taint, format...) ({ \
  109. int __ret_warn_on = !!(condition); \
  110. if (unlikely(__ret_warn_on)) \
  111. __WARN_printf_taint(taint, format); \
  112. unlikely(__ret_warn_on); \
  113. })
  114. #ifndef WARN_ON_ONCE
  115. #define WARN_ON_ONCE(condition) ({ \
  116. static bool __section(.data.unlikely) __warned; \
  117. int __ret_warn_once = !!(condition); \
  118. \
  119. if (unlikely(__ret_warn_once && !__warned)) { \
  120. __warned = true; \
  121. WARN_ON(1); \
  122. } \
  123. unlikely(__ret_warn_once); \
  124. })
  125. #endif
  126. #define WARN_ONCE(condition, format...) ({ \
  127. static bool __section(.data.unlikely) __warned; \
  128. int __ret_warn_once = !!(condition); \
  129. \
  130. if (unlikely(__ret_warn_once && !__warned)) { \
  131. __warned = true; \
  132. WARN(1, format); \
  133. } \
  134. unlikely(__ret_warn_once); \
  135. })
  136. #define WARN_TAINT_ONCE(condition, taint, format...) ({ \
  137. static bool __section(.data.unlikely) __warned; \
  138. int __ret_warn_once = !!(condition); \
  139. \
  140. if (unlikely(__ret_warn_once && !__warned)) { \
  141. __warned = true; \
  142. WARN_TAINT(1, taint, format); \
  143. } \
  144. unlikely(__ret_warn_once); \
  145. })
  146. #else /* !CONFIG_BUG */
  147. #ifndef HAVE_ARCH_BUG
  148. #define BUG() do {} while (1)
  149. #endif
  150. #ifndef HAVE_ARCH_BUG_ON
  151. #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
  152. #endif
  153. #ifndef HAVE_ARCH_WARN_ON
  154. #define WARN_ON(condition) ({ \
  155. int __ret_warn_on = !!(condition); \
  156. unlikely(__ret_warn_on); \
  157. })
  158. #endif
  159. #ifndef WARN
  160. #define WARN(condition, format...) ({ \
  161. int __ret_warn_on = !!(condition); \
  162. no_printk(format); \
  163. unlikely(__ret_warn_on); \
  164. })
  165. #endif
  166. #define WARN_ON_ONCE(condition) WARN_ON(condition)
  167. #define WARN_ONCE(condition, format...) WARN(condition, format)
  168. #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
  169. #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
  170. #endif
  171. /*
  172. * WARN_ON_SMP() is for cases that the warning is either
  173. * meaningless for !SMP or may even cause failures.
  174. * This is usually used for cases that we have
  175. * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
  176. * returns 0 for uniprocessor settings.
  177. * It can also be used with values that are only defined
  178. * on SMP:
  179. *
  180. * struct foo {
  181. * [...]
  182. * #ifdef CONFIG_SMP
  183. * int bar;
  184. * #endif
  185. * };
  186. *
  187. * void func(struct foo *zoot)
  188. * {
  189. * WARN_ON_SMP(!zoot->bar);
  190. *
  191. * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
  192. * and should be a nop and return false for uniprocessor.
  193. *
  194. * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
  195. * and x is true.
  196. */
  197. #ifdef CONFIG_SMP
  198. # define WARN_ON_SMP(x) WARN_ON(x)
  199. #else
  200. /*
  201. * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
  202. * a stand alone line statement or as a condition in an if ()
  203. * statement.
  204. * A simple "0" would cause gcc to give a "statement has no effect"
  205. * warning.
  206. */
  207. # define WARN_ON_SMP(x) ({0;})
  208. #endif
  209. #endif /* __ASSEMBLY__ */
  210. #endif