1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_BUG_H
3 #define _ASM_GENERIC_BUG_H
5 #include <linux/compiler.h>
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_TAINT(taint) ((taint) << 8)
12 #define BUG_GET_TAINT(bug) ((bug)->flags >> 8)
16 #include <linux/kernel.h>
20 #ifdef CONFIG_GENERIC_BUG
22 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
23 unsigned long bug_addr;
25 signed int bug_addr_disp;
27 #ifdef CONFIG_DEBUG_BUGVERBOSE
28 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
37 #endif /* CONFIG_GENERIC_BUG */
40 * Don't use BUG() or BUG_ON() unless there's really no way out; one
41 * example might be detecting data structure corruption in the middle
42 * of an operation that can't be backed out of. If the (sub)system
43 * can somehow continue operating, perhaps with reduced functionality,
44 * it's probably not BUG-worthy.
46 * If you're tempted to BUG(), think again: is completely giving up
47 * really the *only* solution? There are usually better options, where
48 * users don't need to reboot ASAP and can mostly shut down cleanly.
52 printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
53 barrier_before_unreachable(); \
58 #ifndef HAVE_ARCH_BUG_ON
59 #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
63 #define __WARN_TAINT(taint) __WARN_FLAGS(BUGFLAG_TAINT(taint))
64 #define __WARN_ONCE_TAINT(taint) __WARN_FLAGS(BUGFLAG_ONCE|BUGFLAG_TAINT(taint))
66 #define WARN_ON_ONCE(condition) ({ \
67 int __ret_warn_on = !!(condition); \
68 if (unlikely(__ret_warn_on)) \
69 __WARN_ONCE_TAINT(TAINT_WARN); \
70 unlikely(__ret_warn_on); \
75 * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
76 * significant issues that need prompt attention if they should ever
77 * appear at runtime. Use the versions with printk format strings
78 * to provide better diagnostics.
82 void warn_slowpath_fmt(const char *file, const int line,
83 const char *fmt, ...);
85 void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
86 const char *fmt, ...);
87 extern void warn_slowpath_null(const char *file, const int line);
88 #define WANT_WARN_ON_SLOWPATH
89 #define __WARN() warn_slowpath_null(__FILE__, __LINE__)
90 #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)
91 #define __WARN_printf_taint(taint, arg...) \
92 warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg)
94 #define __WARN() __WARN_TAINT(TAINT_WARN)
95 #define __WARN_printf(arg...) do { printk(arg); __WARN(); } while (0)
96 #define __WARN_printf_taint(taint, arg...) \
97 do { printk(arg); __WARN_TAINT(taint); } while (0)
100 /* used internally by panic.c */
104 void __warn(const char *file, int line, void *caller, unsigned taint,
105 struct pt_regs *regs, struct warn_args *args);
108 #define WARN_ON(condition) ({ \
109 int __ret_warn_on = !!(condition); \
110 if (unlikely(__ret_warn_on)) \
112 unlikely(__ret_warn_on); \
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); \
125 #define WARN_TAINT(condition, taint, format...) ({ \
126 int __ret_warn_on = !!(condition); \
127 if (unlikely(__ret_warn_on)) \
128 __WARN_printf_taint(taint, format); \
129 unlikely(__ret_warn_on); \
133 #define WARN_ON_ONCE(condition) ({ \
134 static bool __section(.data.unlikely) __warned; \
135 int __ret_warn_once = !!(condition); \
137 if (unlikely(__ret_warn_once && !__warned)) { \
141 unlikely(__ret_warn_once); \
145 #define WARN_ONCE(condition, format...) ({ \
146 static bool __section(.data.unlikely) __warned; \
147 int __ret_warn_once = !!(condition); \
149 if (unlikely(__ret_warn_once && !__warned)) { \
153 unlikely(__ret_warn_once); \
156 #define WARN_TAINT_ONCE(condition, taint, format...) ({ \
157 static bool __section(.data.unlikely) __warned; \
158 int __ret_warn_once = !!(condition); \
160 if (unlikely(__ret_warn_once && !__warned)) { \
162 WARN_TAINT(1, taint, format); \
164 unlikely(__ret_warn_once); \
167 #else /* !CONFIG_BUG */
168 #ifndef HAVE_ARCH_BUG
169 #define BUG() do {} while (1)
172 #ifndef HAVE_ARCH_BUG_ON
173 #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
176 #ifndef HAVE_ARCH_WARN_ON
177 #define WARN_ON(condition) ({ \
178 int __ret_warn_on = !!(condition); \
179 unlikely(__ret_warn_on); \
184 #define WARN(condition, format...) ({ \
185 int __ret_warn_on = !!(condition); \
187 unlikely(__ret_warn_on); \
191 #define WARN_ON_ONCE(condition) WARN_ON(condition)
192 #define WARN_ONCE(condition, format...) WARN(condition, format)
193 #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
194 #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
199 * WARN_ON_SMP() is for cases that the warning is either
200 * meaningless for !SMP or may even cause failures.
201 * This is usually used for cases that we have
202 * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
203 * returns 0 for uniprocessor settings.
204 * It can also be used with values that are only defined
214 * void func(struct foo *zoot)
216 * WARN_ON_SMP(!zoot->bar);
218 * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
219 * and should be a nop and return false for uniprocessor.
221 * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
225 # define WARN_ON_SMP(x) WARN_ON(x)
228 * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
229 * a stand alone line statement or as a condition in an if ()
231 * A simple "0" would cause gcc to give a "statement has no effect"
234 # define WARN_ON_SMP(x) ({0;})
237 #endif /* __ASSEMBLY__ */