bug.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. panic("BUG!"); \
  43. } while (0)
  44. #endif
  45. #ifndef HAVE_ARCH_BUG_ON
  46. #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while(0)
  47. #endif
  48. /*
  49. * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
  50. * significant issues that need prompt attention if they should ever
  51. * appear at runtime. Use the versions with printk format strings
  52. * to provide better diagnostics.
  53. */
  54. #ifndef __WARN_TAINT
  55. #ifndef __ASSEMBLY__
  56. extern __printf(3, 4)
  57. void warn_slowpath_fmt(const char *file, const int line,
  58. const char *fmt, ...);
  59. extern __printf(4, 5)
  60. void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
  61. const char *fmt, ...);
  62. extern void warn_slowpath_null(const char *file, const int line);
  63. #define WANT_WARN_ON_SLOWPATH
  64. #endif
  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. #else /* !CONFIG_BUG */
  98. #ifndef HAVE_ARCH_BUG
  99. #define BUG() do {} while(0)
  100. #endif
  101. #ifndef HAVE_ARCH_BUG_ON
  102. #define BUG_ON(condition) do { if (condition) ; } while(0)
  103. #endif
  104. #ifndef HAVE_ARCH_WARN_ON
  105. #define WARN_ON(condition) ({ \
  106. int __ret_warn_on = !!(condition); \
  107. unlikely(__ret_warn_on); \
  108. })
  109. #endif
  110. #ifndef WARN
  111. #define WARN(condition, format...) ({ \
  112. int __ret_warn_on = !!(condition); \
  113. unlikely(__ret_warn_on); \
  114. })
  115. #endif
  116. #define WARN_TAINT(condition, taint, format...) WARN_ON(condition)
  117. #endif
  118. #define WARN_ON_ONCE(condition) ({ \
  119. static bool __section(.data.unlikely) __warned; \
  120. int __ret_warn_once = !!(condition); \
  121. \
  122. if (unlikely(__ret_warn_once)) \
  123. if (WARN_ON(!__warned)) \
  124. __warned = true; \
  125. unlikely(__ret_warn_once); \
  126. })
  127. #define WARN_ONCE(condition, format...) ({ \
  128. static bool __section(.data.unlikely) __warned; \
  129. int __ret_warn_once = !!(condition); \
  130. \
  131. if (unlikely(__ret_warn_once)) \
  132. if (WARN(!__warned, format)) \
  133. __warned = true; \
  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)) \
  141. if (WARN_TAINT(!__warned, taint, format)) \
  142. __warned = true; \
  143. unlikely(__ret_warn_once); \
  144. })
  145. /*
  146. * WARN_ON_SMP() is for cases that the warning is either
  147. * meaningless for !SMP or may even cause failures.
  148. * This is usually used for cases that we have
  149. * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
  150. * returns 0 for uniprocessor settings.
  151. * It can also be used with values that are only defined
  152. * on SMP:
  153. *
  154. * struct foo {
  155. * [...]
  156. * #ifdef CONFIG_SMP
  157. * int bar;
  158. * #endif
  159. * };
  160. *
  161. * void func(struct foo *zoot)
  162. * {
  163. * WARN_ON_SMP(!zoot->bar);
  164. *
  165. * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
  166. * and should be a nop and return false for uniprocessor.
  167. *
  168. * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
  169. * and x is true.
  170. */
  171. #ifdef CONFIG_SMP
  172. # define WARN_ON_SMP(x) WARN_ON(x)
  173. #else
  174. /*
  175. * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
  176. * a stand alone line statement or as a condition in an if ()
  177. * statement.
  178. * A simple "0" would cause gcc to give a "statement has no effect"
  179. * warning.
  180. */
  181. # define WARN_ON_SMP(x) ({0;})
  182. #endif
  183. #endif