bug.h 6.0 KB

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