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