bug.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef WARN_ON
  76. #define WARN_ON(condition) ({ \
  77. int __ret_warn_on = !!(condition); \
  78. if (unlikely(__ret_warn_on)) \
  79. __WARN(); \
  80. unlikely(__ret_warn_on); \
  81. })
  82. #endif
  83. #ifndef WARN
  84. #define WARN(condition, format...) ({ \
  85. int __ret_warn_on = !!(condition); \
  86. if (unlikely(__ret_warn_on)) \
  87. __WARN_printf(format); \
  88. unlikely(__ret_warn_on); \
  89. })
  90. #endif
  91. #define WARN_TAINT(condition, taint, format...) ({ \
  92. int __ret_warn_on = !!(condition); \
  93. if (unlikely(__ret_warn_on)) \
  94. __WARN_printf_taint(taint, format); \
  95. unlikely(__ret_warn_on); \
  96. })
  97. #define WARN_ON_ONCE(condition) ({ \
  98. static bool __section(.data.unlikely) __warned; \
  99. int __ret_warn_once = !!(condition); \
  100. \
  101. if (unlikely(__ret_warn_once)) \
  102. if (WARN_ON(!__warned)) \
  103. __warned = true; \
  104. unlikely(__ret_warn_once); \
  105. })
  106. #define WARN_ONCE(condition, format...) ({ \
  107. static bool __section(.data.unlikely) __warned; \
  108. int __ret_warn_once = !!(condition); \
  109. \
  110. if (unlikely(__ret_warn_once)) \
  111. if (WARN(!__warned, format)) \
  112. __warned = true; \
  113. unlikely(__ret_warn_once); \
  114. })
  115. #define WARN_TAINT_ONCE(condition, taint, format...) ({ \
  116. static bool __section(.data.unlikely) __warned; \
  117. int __ret_warn_once = !!(condition); \
  118. \
  119. if (unlikely(__ret_warn_once)) \
  120. if (WARN_TAINT(!__warned, taint, format)) \
  121. __warned = true; \
  122. unlikely(__ret_warn_once); \
  123. })
  124. #else /* !CONFIG_BUG */
  125. #ifndef HAVE_ARCH_BUG
  126. #define BUG() do {} while (1)
  127. #endif
  128. #ifndef HAVE_ARCH_BUG_ON
  129. #define BUG_ON(condition) do { if (condition) ; } while (0)
  130. #endif
  131. #ifndef HAVE_ARCH_WARN_ON
  132. #define WARN_ON(condition) ({ \
  133. int __ret_warn_on = !!(condition); \
  134. unlikely(__ret_warn_on); \
  135. })
  136. #endif
  137. #ifndef WARN
  138. #define WARN(condition, format...) ({ \
  139. int __ret_warn_on = !!(condition); \
  140. no_printk(format); \
  141. unlikely(__ret_warn_on); \
  142. })
  143. #endif
  144. #define WARN_ON_ONCE(condition) WARN_ON(condition)
  145. #define WARN_ONCE(condition, format...) WARN(condition, format)
  146. #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
  147. #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
  148. #endif
  149. /*
  150. * WARN_ON_SMP() is for cases that the warning is either
  151. * meaningless for !SMP or may even cause failures.
  152. * This is usually used for cases that we have
  153. * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
  154. * returns 0 for uniprocessor settings.
  155. * It can also be used with values that are only defined
  156. * on SMP:
  157. *
  158. * struct foo {
  159. * [...]
  160. * #ifdef CONFIG_SMP
  161. * int bar;
  162. * #endif
  163. * };
  164. *
  165. * void func(struct foo *zoot)
  166. * {
  167. * WARN_ON_SMP(!zoot->bar);
  168. *
  169. * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
  170. * and should be a nop and return false for uniprocessor.
  171. *
  172. * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
  173. * and x is true.
  174. */
  175. #ifdef CONFIG_SMP
  176. # define WARN_ON_SMP(x) WARN_ON(x)
  177. #else
  178. /*
  179. * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
  180. * a stand alone line statement or as a condition in an if ()
  181. * statement.
  182. * A simple "0" would cause gcc to give a "statement has no effect"
  183. * warning.
  184. */
  185. # define WARN_ON_SMP(x) ({0;})
  186. #endif
  187. #endif /* __ASSEMBLY__ */
  188. #endif