bug.h 6.8 KB

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