xref: /openbmc/linux/include/asm-generic/bug.h (revision d9cdb431)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _ASM_GENERIC_BUG_H
31da177e4SLinus Torvalds #define _ASM_GENERIC_BUG_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/compiler.h>
6d19e789fSIngo Molnar #include <linux/instrumentation.h>
7a358f406STanner Love #include <linux/once_lite.h>
81da177e4SLinus Torvalds 
92a8358d8SKees Cook #define CUT_HERE		"------------[ cut here ]------------\n"
102a8358d8SKees Cook 
1109682c1dSPaul Mundt #ifdef CONFIG_GENERIC_BUG
1209682c1dSPaul Mundt #define BUGFLAG_WARNING		(1 << 0)
1319d43626SPeter Zijlstra #define BUGFLAG_ONCE		(1 << 1)
1419d43626SPeter Zijlstra #define BUGFLAG_DONE		(1 << 2)
15a44f71a9SKees Cook #define BUGFLAG_NO_CUT_HERE	(1 << 3)	/* CUT_HERE already sent */
16f26dee15SPeter Zijlstra #define BUGFLAG_TAINT(taint)	((taint) << 8)
1709682c1dSPaul Mundt #define BUG_GET_TAINT(bug)	((bug)->flags >> 8)
1809682c1dSPaul Mundt #endif
1909682c1dSPaul Mundt 
2009682c1dSPaul Mundt #ifndef __ASSEMBLY__
21f39650deSAndy Shevchenko #include <linux/panic.h>
22f39650deSAndy Shevchenko #include <linux/printk.h>
2309682c1dSPaul Mundt 
241fa568e2SShida Zhang struct warn_args;
251fa568e2SShida Zhang struct pt_regs;
261fa568e2SShida Zhang 
271fa568e2SShida Zhang void __warn(const char *file, int line, void *caller, unsigned taint,
281fa568e2SShida Zhang 	    struct pt_regs *regs, struct warn_args *args);
291fa568e2SShida Zhang 
30ffb61c63SIngo Molnar #ifdef CONFIG_BUG
31ffb61c63SIngo Molnar 
32f81f8ad5SNadav Amit #ifdef CONFIG_GENERIC_BUG
33ffb61c63SIngo Molnar struct bug_entry {
34b93a531eSJan Beulich #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
357664c5a1SJeremy Fitzhardinge 	unsigned long	bug_addr;
36b93a531eSJan Beulich #else
37b93a531eSJan Beulich 	signed int	bug_addr_disp;
38b93a531eSJan Beulich #endif
397664c5a1SJeremy Fitzhardinge #ifdef CONFIG_DEBUG_BUGVERBOSE
40b93a531eSJan Beulich #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
417664c5a1SJeremy Fitzhardinge 	const char	*file;
42b93a531eSJan Beulich #else
43b93a531eSJan Beulich 	signed int	file_disp;
44b93a531eSJan Beulich #endif
457664c5a1SJeremy Fitzhardinge 	unsigned short	line;
467664c5a1SJeremy Fitzhardinge #endif
477664c5a1SJeremy Fitzhardinge 	unsigned short	flags;
48f81f8ad5SNadav Amit };
49ffb61c63SIngo Molnar #endif	/* CONFIG_GENERIC_BUG */
507664c5a1SJeremy Fitzhardinge 
51af9379c7SDavid Brownell /*
52af9379c7SDavid Brownell  * Don't use BUG() or BUG_ON() unless there's really no way out; one
53af9379c7SDavid Brownell  * example might be detecting data structure corruption in the middle
54af9379c7SDavid Brownell  * of an operation that can't be backed out of.  If the (sub)system
55af9379c7SDavid Brownell  * can somehow continue operating, perhaps with reduced functionality,
56af9379c7SDavid Brownell  * it's probably not BUG-worthy.
57af9379c7SDavid Brownell  *
58af9379c7SDavid Brownell  * If you're tempted to BUG(), think again:  is completely giving up
59af9379c7SDavid Brownell  * really the *only* solution?  There are usually better options, where
60af9379c7SDavid Brownell  * users don't need to reboot ASAP and can mostly shut down cleanly.
61af9379c7SDavid Brownell  */
621da177e4SLinus Torvalds #ifndef HAVE_ARCH_BUG
631da177e4SLinus Torvalds #define BUG() do { \
64d5c003b4SHarvey Harrison 	printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
65173a3efdSArnd Bergmann 	barrier_before_unreachable(); \
661da177e4SLinus Torvalds 	panic("BUG!"); \
671da177e4SLinus Torvalds } while (0)
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #ifndef HAVE_ARCH_BUG_ON
712a41de48SAlexey Dobriyan #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
74af9379c7SDavid Brownell /*
75af9379c7SDavid Brownell  * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
7696c6a32cSDmitry Vyukov  * significant kernel issues that need prompt attention if they should ever
7796c6a32cSDmitry Vyukov  * appear at runtime.
7896c6a32cSDmitry Vyukov  *
7996c6a32cSDmitry Vyukov  * Do not use these macros when checking for invalid external inputs
8096c6a32cSDmitry Vyukov  * (e.g. invalid system call arguments, or invalid data coming from
8196c6a32cSDmitry Vyukov  * network/devices), and on transient conditions like ENOMEM or EAGAIN.
8296c6a32cSDmitry Vyukov  * These macros should be used for recoverable kernel issues only.
8396c6a32cSDmitry Vyukov  * For invalid external inputs, transient conditions, etc use
8496c6a32cSDmitry Vyukov  * pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
8596c6a32cSDmitry Vyukov  * Do not include "BUG"/"WARNING" in format strings manually to make these
8696c6a32cSDmitry Vyukov  * conditions distinguishable from kernel issues.
8796c6a32cSDmitry Vyukov  *
8896c6a32cSDmitry Vyukov  * Use the versions with printk format strings to provide better diagnostics.
89af9379c7SDavid Brownell  */
90b9075fa9SJoe Perches extern __printf(4, 5)
91ee871133SKees Cook void warn_slowpath_fmt(const char *file, const int line, unsigned taint,
92b9075fa9SJoe Perches 		       const char *fmt, ...);
93*d9cdb431SArnd Bergmann extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
94*d9cdb431SArnd Bergmann 
95*d9cdb431SArnd Bergmann #ifndef __WARN_FLAGS
96f2f84b05SKees Cook #define __WARN()		__WARN_printf(TAINT_WARN, NULL)
975916d5f9SThomas Gleixner #define __WARN_printf(taint, arg...) do {				\
985916d5f9SThomas Gleixner 		instrumentation_begin();				\
995916d5f9SThomas Gleixner 		warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);	\
1005916d5f9SThomas Gleixner 		instrumentation_end();					\
1015916d5f9SThomas Gleixner 	} while (0)
102a8f18b90SArjan van de Ven #else
103a44f71a9SKees Cook #define __WARN()		__WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN))
104d4bce140SKees Cook #define __WARN_printf(taint, arg...) do {				\
1055916d5f9SThomas Gleixner 		instrumentation_begin();				\
106d4bce140SKees Cook 		__warn_printk(arg);					\
107a44f71a9SKees Cook 		__WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
1085916d5f9SThomas Gleixner 		instrumentation_end();					\
109d4bce140SKees Cook 	} while (0)
1102da1ead4SKees Cook #define WARN_ON_ONCE(condition) ({				\
1112da1ead4SKees Cook 	int __ret_warn_on = !!(condition);			\
1122da1ead4SKees Cook 	if (unlikely(__ret_warn_on))				\
1132da1ead4SKees Cook 		__WARN_FLAGS(BUGFLAG_ONCE |			\
1142da1ead4SKees Cook 			     BUGFLAG_TAINT(TAINT_WARN));	\
1152da1ead4SKees Cook 	unlikely(__ret_warn_on);				\
1162da1ead4SKees Cook })
1173a6a62f9SOlof Johansson #endif
1183a6a62f9SOlof Johansson 
1192553b67aSJosh Poimboeuf /* used internally by panic.c */
1202553b67aSJosh Poimboeuf 
1213a6a62f9SOlof Johansson #ifndef WARN_ON
1223a6a62f9SOlof Johansson #define WARN_ON(condition) ({						\
1233a6a62f9SOlof Johansson 	int __ret_warn_on = !!(condition);				\
1243a6a62f9SOlof Johansson 	if (unlikely(__ret_warn_on))					\
1253a6a62f9SOlof Johansson 		__WARN();						\
126684f9783SHerbert Xu 	unlikely(__ret_warn_on);					\
127684f9783SHerbert Xu })
1281da177e4SLinus Torvalds #endif
1291da177e4SLinus Torvalds 
130a8f18b90SArjan van de Ven #ifndef WARN
131a8f18b90SArjan van de Ven #define WARN(condition, format...) ({					\
132a8f18b90SArjan van de Ven 	int __ret_warn_on = !!(condition);				\
133a8f18b90SArjan van de Ven 	if (unlikely(__ret_warn_on))					\
13489348fc3SKees Cook 		__WARN_printf(TAINT_WARN, format);			\
135a8f18b90SArjan van de Ven 	unlikely(__ret_warn_on);					\
136a8f18b90SArjan van de Ven })
137a8f18b90SArjan van de Ven #endif
138a8f18b90SArjan van de Ven 
139b2be0527SBen Hutchings #define WARN_TAINT(condition, taint, format...) ({			\
140b2be0527SBen Hutchings 	int __ret_warn_on = !!(condition);				\
141b2be0527SBen Hutchings 	if (unlikely(__ret_warn_on))					\
14289348fc3SKees Cook 		__WARN_printf(taint, format);				\
143b2be0527SBen Hutchings 	unlikely(__ret_warn_on);					\
144b2be0527SBen Hutchings })
145b2be0527SBen Hutchings 
14619d43626SPeter Zijlstra #ifndef WARN_ON_ONCE
147a358f406STanner Love #define WARN_ON_ONCE(condition)					\
148a358f406STanner Love 	DO_ONCE_LITE_IF(condition, WARN_ON, 1)
14919d43626SPeter Zijlstra #endif
15074bb6a09SIngo Molnar 
151a358f406STanner Love #define WARN_ONCE(condition, format...)				\
152a358f406STanner Love 	DO_ONCE_LITE_IF(condition, WARN, 1, format)
15345e9c0deSArjan van de Ven 
154a358f406STanner Love #define WARN_TAINT_ONCE(condition, taint, format...)		\
155a358f406STanner Love 	DO_ONCE_LITE_IF(condition, WARN_TAINT, 1, taint, format)
156b2be0527SBen Hutchings 
157b607e70eSJosh Triplett #else /* !CONFIG_BUG */
158b607e70eSJosh Triplett #ifndef HAVE_ARCH_BUG
159a4b5d580SJosh Triplett #define BUG() do {} while (1)
160b607e70eSJosh Triplett #endif
161b607e70eSJosh Triplett 
162b607e70eSJosh Triplett #ifndef HAVE_ARCH_BUG_ON
1639b87647cSDenis Efremov #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
164b607e70eSJosh Triplett #endif
165b607e70eSJosh Triplett 
166b607e70eSJosh Triplett #ifndef HAVE_ARCH_WARN_ON
167b607e70eSJosh Triplett #define WARN_ON(condition) ({						\
168b607e70eSJosh Triplett 	int __ret_warn_on = !!(condition);				\
169b607e70eSJosh Triplett 	unlikely(__ret_warn_on);					\
170b607e70eSJosh Triplett })
171b607e70eSJosh Triplett #endif
172b607e70eSJosh Triplett 
173b607e70eSJosh Triplett #ifndef WARN
174b607e70eSJosh Triplett #define WARN(condition, format...) ({					\
175b607e70eSJosh Triplett 	int __ret_warn_on = !!(condition);				\
1764e50ebdeSJosh Triplett 	no_printk(format);						\
177b607e70eSJosh Triplett 	unlikely(__ret_warn_on);					\
178b607e70eSJosh Triplett })
179b607e70eSJosh Triplett #endif
180b607e70eSJosh Triplett 
181b607e70eSJosh Triplett #define WARN_ON_ONCE(condition) WARN_ON(condition)
182b607e70eSJosh Triplett #define WARN_ONCE(condition, format...) WARN(condition, format)
183b607e70eSJosh Triplett #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
184b607e70eSJosh Triplett #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
185b607e70eSJosh Triplett 
186b607e70eSJosh Triplett #endif
187b607e70eSJosh Triplett 
1882092e6beSSteven Rostedt /*
1892092e6beSSteven Rostedt  * WARN_ON_SMP() is for cases that the warning is either
1902092e6beSSteven Rostedt  * meaningless for !SMP or may even cause failures.
1912092e6beSSteven Rostedt  * It can also be used with values that are only defined
1922092e6beSSteven Rostedt  * on SMP:
1932092e6beSSteven Rostedt  *
1942092e6beSSteven Rostedt  * struct foo {
1952092e6beSSteven Rostedt  *  [...]
1962092e6beSSteven Rostedt  * #ifdef CONFIG_SMP
1972092e6beSSteven Rostedt  *	int bar;
1982092e6beSSteven Rostedt  * #endif
1992092e6beSSteven Rostedt  * };
2002092e6beSSteven Rostedt  *
2012092e6beSSteven Rostedt  * void func(struct foo *zoot)
2022092e6beSSteven Rostedt  * {
2032092e6beSSteven Rostedt  *	WARN_ON_SMP(!zoot->bar);
2042092e6beSSteven Rostedt  *
2052092e6beSSteven Rostedt  * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
2062092e6beSSteven Rostedt  * and should be a nop and return false for uniprocessor.
2072092e6beSSteven Rostedt  *
2082092e6beSSteven Rostedt  * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
2092092e6beSSteven Rostedt  * and x is true.
2102092e6beSSteven Rostedt  */
2118eb94f80SIngo Molnar #ifdef CONFIG_SMP
2128eb94f80SIngo Molnar # define WARN_ON_SMP(x)			WARN_ON(x)
2138eb94f80SIngo Molnar #else
214ccd0d44fSSteven Rostedt /*
215ccd0d44fSSteven Rostedt  * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
216ccd0d44fSSteven Rostedt  * a stand alone line statement or as a condition in an if ()
217ccd0d44fSSteven Rostedt  * statement.
218ccd0d44fSSteven Rostedt  * A simple "0" would cause gcc to give a "statement has no effect"
219ccd0d44fSSteven Rostedt  * warning.
220ccd0d44fSSteven Rostedt  */
2212092e6beSSteven Rostedt # define WARN_ON_SMP(x)			({0;})
2228eb94f80SIngo Molnar #endif
2238eb94f80SIngo Molnar 
2242603efa3SPaul Mundt #endif /* __ASSEMBLY__ */
2252603efa3SPaul Mundt 
2261da177e4SLinus Torvalds #endif
227