bug.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 kernel issues that need prompt attention if they should ever
  68. * appear at runtime.
  69. *
  70. * Do not use these macros when checking for invalid external inputs
  71. * (e.g. invalid system call arguments, or invalid data coming from
  72. * network/devices), and on transient conditions like ENOMEM or EAGAIN.
  73. * These macros should be used for recoverable kernel issues only.
  74. * For invalid external inputs, transient conditions, etc use
  75. * pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
  76. * Do not include "BUG"/"WARNING" in format strings manually to make these
  77. * conditions distinguishable from kernel issues.
  78. *
  79. * Use the versions with printk format strings to provide better diagnostics.
  80. */
  81. #ifndef __WARN_TAINT
  82. extern __printf(3, 4)
  83. void warn_slowpath_fmt(const char *file, const int line,
  84. const char *fmt, ...);
  85. extern __printf(4, 5)
  86. void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
  87. const char *fmt, ...);
  88. extern void warn_slowpath_null(const char *file, const int line);
  89. #define WANT_WARN_ON_SLOWPATH
  90. #define __WARN() warn_slowpath_null(__FILE__, __LINE__)
  91. #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)
  92. #define __WARN_printf_taint(taint, arg...) \
  93. warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg)
  94. #else
  95. extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
  96. #define __WARN() do { \
  97. printk(KERN_WARNING CUT_HERE); __WARN_TAINT(TAINT_WARN); \
  98. } while (0)
  99. #define __WARN_printf(arg...) __WARN_printf_taint(TAINT_WARN, arg)
  100. #define __WARN_printf_taint(taint, arg...) \
  101. do { __warn_printk(arg); __WARN_TAINT(taint); } while (0)
  102. #endif
  103. /* used internally by panic.c */
  104. struct warn_args;
  105. struct pt_regs;
  106. void __warn(const char *file, int line, void *caller, unsigned taint,
  107. struct pt_regs *regs, struct warn_args *args);
  108. #ifndef WARN_ON
  109. #define WARN_ON(condition) ({ \
  110. int __ret_warn_on = !!(condition); \
  111. if (unlikely(__ret_warn_on)) \
  112. __WARN(); \
  113. unlikely(__ret_warn_on); \
  114. })
  115. #endif
  116. #ifndef WARN
  117. #define WARN(condition, format...) ({ \
  118. int __ret_warn_on = !!(condition); \
  119. if (unlikely(__ret_warn_on)) \
  120. __WARN_printf(format); \
  121. unlikely(__ret_warn_on); \
  122. })
  123. #endif
  124. #define WARN_TAINT(condition, taint, format...) ({ \
  125. int __ret_warn_on = !!(condition); \
  126. if (unlikely(__ret_warn_on)) \
  127. __WARN_printf_taint(taint, format); \
  128. unlikely(__ret_warn_on); \
  129. })
  130. #ifndef WARN_ON_ONCE
  131. #define WARN_ON_ONCE(condition) ({ \
  132. static bool __section(.data.once) __warned; \
  133. int __ret_warn_once = !!(condition); \
  134. \
  135. if (unlikely(__ret_warn_once && !__warned)) { \
  136. __warned = true; \
  137. WARN_ON(1); \
  138. } \
  139. unlikely(__ret_warn_once); \
  140. })
  141. #endif
  142. #define WARN_ONCE(condition, format...) ({ \
  143. static bool __section(.data.once) __warned; \
  144. int __ret_warn_once = !!(condition); \
  145. \
  146. if (unlikely(__ret_warn_once && !__warned)) { \
  147. __warned = true; \
  148. WARN(1, format); \
  149. } \
  150. unlikely(__ret_warn_once); \
  151. })
  152. #define WARN_TAINT_ONCE(condition, taint, format...) ({ \
  153. static bool __section(.data.once) __warned; \
  154. int __ret_warn_once = !!(condition); \
  155. \
  156. if (unlikely(__ret_warn_once && !__warned)) { \
  157. __warned = true; \
  158. WARN_TAINT(1, taint, format); \
  159. } \
  160. unlikely(__ret_warn_once); \
  161. })
  162. #else /* !CONFIG_BUG */
  163. #ifndef HAVE_ARCH_BUG
  164. #define BUG() do {} while (1)
  165. #endif
  166. #ifndef HAVE_ARCH_BUG_ON
  167. #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
  168. #endif
  169. #ifndef HAVE_ARCH_WARN_ON
  170. #define WARN_ON(condition) ({ \
  171. int __ret_warn_on = !!(condition); \
  172. unlikely(__ret_warn_on); \
  173. })
  174. #endif
  175. #ifndef WARN
  176. #define WARN(condition, format...) ({ \
  177. int __ret_warn_on = !!(condition); \
  178. no_printk(format); \
  179. unlikely(__ret_warn_on); \
  180. })
  181. #endif
  182. #define WARN_ON_ONCE(condition) WARN_ON(condition)
  183. #define WARN_ONCE(condition, format...) WARN(condition, format)
  184. #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
  185. #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
  186. #endif
  187. /*
  188. * WARN_ON_SMP() is for cases that the warning is either
  189. * meaningless for !SMP or may even cause failures.
  190. * This is usually used for cases that we have
  191. * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
  192. * returns 0 for uniprocessor settings.
  193. * It can also be used with values that are only defined
  194. * on SMP:
  195. *
  196. * struct foo {
  197. * [...]
  198. * #ifdef CONFIG_SMP
  199. * int bar;
  200. * #endif
  201. * };
  202. *
  203. * void func(struct foo *zoot)
  204. * {
  205. * WARN_ON_SMP(!zoot->bar);
  206. *
  207. * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
  208. * and should be a nop and return false for uniprocessor.
  209. *
  210. * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
  211. * and x is true.
  212. */
  213. #ifdef CONFIG_SMP
  214. # define WARN_ON_SMP(x) WARN_ON(x)
  215. #else
  216. /*
  217. * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
  218. * a stand alone line statement or as a condition in an if ()
  219. * statement.
  220. * A simple "0" would cause gcc to give a "statement has no effect"
  221. * warning.
  222. */
  223. # define WARN_ON_SMP(x) ({0;})
  224. #endif
  225. #endif /* __ASSEMBLY__ */
  226. #endif