bug.h 6.2 KB

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