bug.h 5.9 KB

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