bug.h 5.4 KB

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