bug.h 6.6 KB

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