bug.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_BUG_H
  3. #define _ASM_GENERIC_BUG_H
  4. #include <linux/compiler.h>
  5. #ifdef CONFIG_GENERIC_BUG
  6. #define BUGFLAG_WARNING (1 << 0)
  7. #define BUGFLAG_ONCE (1 << 1)
  8. #define BUGFLAG_DONE (1 << 2)
  9. #define BUGFLAG_TAINT(taint) ((taint) << 8)
  10. #define BUG_GET_TAINT(bug) ((bug)->flags >> 8)
  11. #endif
  12. #ifndef __ASSEMBLY__
  13. #include <linux/kernel.h>
  14. #ifdef CONFIG_BUG
  15. #ifdef CONFIG_GENERIC_BUG
  16. struct bug_entry {
  17. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  18. unsigned long bug_addr;
  19. #else
  20. signed int bug_addr_disp;
  21. #endif
  22. #ifdef CONFIG_DEBUG_BUGVERBOSE
  23. #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
  24. const char *file;
  25. #else
  26. signed int file_disp;
  27. #endif
  28. unsigned short line;
  29. #endif
  30. unsigned short flags;
  31. };
  32. #endif /* CONFIG_GENERIC_BUG */
  33. /*
  34. * Don't use BUG() or BUG_ON() unless there's really no way out; one
  35. * example might be detecting data structure corruption in the middle
  36. * of an operation that can't be backed out of. If the (sub)system
  37. * can somehow continue operating, perhaps with reduced functionality,
  38. * it's probably not BUG-worthy.
  39. *
  40. * If you're tempted to BUG(), think again: is completely giving up
  41. * really the *only* solution? There are usually better options, where
  42. * users don't need to reboot ASAP and can mostly shut down cleanly.
  43. */
  44. #ifndef HAVE_ARCH_BUG
  45. #define BUG() do { \
  46. printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
  47. barrier_before_unreachable(); \
  48. panic("BUG!"); \
  49. } while (0)
  50. #endif
  51. #ifndef HAVE_ARCH_BUG_ON
  52. #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
  53. #endif
  54. #ifdef __WARN_FLAGS
  55. #define __WARN_TAINT(taint) __WARN_FLAGS(BUGFLAG_TAINT(taint))
  56. #define __WARN_ONCE_TAINT(taint) __WARN_FLAGS(BUGFLAG_ONCE|BUGFLAG_TAINT(taint))
  57. #define WARN_ON_ONCE(condition) ({ \
  58. int __ret_warn_on = !!(condition); \
  59. if (unlikely(__ret_warn_on)) \
  60. __WARN_ONCE_TAINT(TAINT_WARN); \
  61. unlikely(__ret_warn_on); \
  62. })
  63. #endif
  64. /*
  65. * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
  66. * significant issues that need prompt attention if they should ever
  67. * appear at runtime. Use the versions with printk format strings
  68. * to provide better diagnostics.
  69. */
  70. #ifndef __WARN_TAINT
  71. extern __printf(3, 4)
  72. void warn_slowpath_fmt(const char *file, const int line,
  73. const char *fmt, ...);
  74. extern __printf(4, 5)
  75. void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
  76. const char *fmt, ...);
  77. extern void warn_slowpath_null(const char *file, const int line);
  78. #define WANT_WARN_ON_SLOWPATH
  79. #define __WARN() warn_slowpath_null(__FILE__, __LINE__)
  80. #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)
  81. #define __WARN_printf_taint(taint, arg...) \
  82. warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg)
  83. #else
  84. #define __WARN() __WARN_TAINT(TAINT_WARN)
  85. #define __WARN_printf(arg...) do { printk(arg); __WARN(); } while (0)
  86. #define __WARN_printf_taint(taint, arg...) \
  87. do { printk(arg); __WARN_TAINT(taint); } while (0)
  88. #endif
  89. /* used internally by panic.c */
  90. struct warn_args;
  91. struct pt_regs;
  92. void __warn(const char *file, int line, void *caller, unsigned taint,
  93. struct pt_regs *regs, struct warn_args *args);
  94. #ifndef WARN_ON
  95. #define WARN_ON(condition) ({ \
  96. int __ret_warn_on = !!(condition); \
  97. if (unlikely(__ret_warn_on)) \
  98. __WARN(); \
  99. unlikely(__ret_warn_on); \
  100. })
  101. #endif
  102. #ifndef WARN
  103. #define WARN(condition, format...) ({ \
  104. int __ret_warn_on = !!(condition); \
  105. if (unlikely(__ret_warn_on)) \
  106. __WARN_printf(format); \
  107. unlikely(__ret_warn_on); \
  108. })
  109. #endif
  110. #define WARN_TAINT(condition, taint, format...) ({ \
  111. int __ret_warn_on = !!(condition); \
  112. if (unlikely(__ret_warn_on)) \
  113. __WARN_printf_taint(taint, format); \
  114. unlikely(__ret_warn_on); \
  115. })
  116. #ifndef WARN_ON_ONCE
  117. #define WARN_ON_ONCE(condition) ({ \
  118. static bool __section(.data.unlikely) __warned; \
  119. int __ret_warn_once = !!(condition); \
  120. \
  121. if (unlikely(__ret_warn_once && !__warned)) { \
  122. __warned = true; \
  123. WARN_ON(1); \
  124. } \
  125. unlikely(__ret_warn_once); \
  126. })
  127. #endif
  128. #define WARN_ONCE(condition, format...) ({ \
  129. static bool __section(.data.unlikely) __warned; \
  130. int __ret_warn_once = !!(condition); \
  131. \
  132. if (unlikely(__ret_warn_once && !__warned)) { \
  133. __warned = true; \
  134. WARN(1, format); \
  135. } \
  136. unlikely(__ret_warn_once); \
  137. })
  138. #define WARN_TAINT_ONCE(condition, taint, format...) ({ \
  139. static bool __section(.data.unlikely) __warned; \
  140. int __ret_warn_once = !!(condition); \
  141. \
  142. if (unlikely(__ret_warn_once && !__warned)) { \
  143. __warned = true; \
  144. WARN_TAINT(1, taint, format); \
  145. } \
  146. unlikely(__ret_warn_once); \
  147. })
  148. #else /* !CONFIG_BUG */
  149. #ifndef HAVE_ARCH_BUG
  150. #define BUG() do {} while (1)
  151. #endif
  152. #ifndef HAVE_ARCH_BUG_ON
  153. #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
  154. #endif
  155. #ifndef HAVE_ARCH_WARN_ON
  156. #define WARN_ON(condition) ({ \
  157. int __ret_warn_on = !!(condition); \
  158. unlikely(__ret_warn_on); \
  159. })
  160. #endif
  161. #ifndef WARN
  162. #define WARN(condition, format...) ({ \
  163. int __ret_warn_on = !!(condition); \
  164. no_printk(format); \
  165. unlikely(__ret_warn_on); \
  166. })
  167. #endif
  168. #define WARN_ON_ONCE(condition) WARN_ON(condition)
  169. #define WARN_ONCE(condition, format...) WARN(condition, format)
  170. #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
  171. #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
  172. #endif
  173. /*
  174. * WARN_ON_SMP() is for cases that the warning is either
  175. * meaningless for !SMP or may even cause failures.
  176. * This is usually used for cases that we have
  177. * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
  178. * returns 0 for uniprocessor settings.
  179. * It can also be used with values that are only defined
  180. * on SMP:
  181. *
  182. * struct foo {
  183. * [...]
  184. * #ifdef CONFIG_SMP
  185. * int bar;
  186. * #endif
  187. * };
  188. *
  189. * void func(struct foo *zoot)
  190. * {
  191. * WARN_ON_SMP(!zoot->bar);
  192. *
  193. * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
  194. * and should be a nop and return false for uniprocessor.
  195. *
  196. * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
  197. * and x is true.
  198. */
  199. #ifdef CONFIG_SMP
  200. # define WARN_ON_SMP(x) WARN_ON(x)
  201. #else
  202. /*
  203. * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
  204. * a stand alone line statement or as a condition in an if ()
  205. * statement.
  206. * A simple "0" would cause gcc to give a "statement has no effect"
  207. * warning.
  208. */
  209. # define WARN_ON_SMP(x) ({0;})
  210. #endif
  211. #endif /* __ASSEMBLY__ */
  212. #endif