bug.h 5.5 KB

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