bug.h 7.0 KB

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