bug.h 5.9 KB

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