xref: /openbmc/linux/include/asm-generic/bug.h (revision a4b5d580)
11da177e4SLinus Torvalds #ifndef _ASM_GENERIC_BUG_H
21da177e4SLinus Torvalds #define _ASM_GENERIC_BUG_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds #include <linux/compiler.h>
51da177e4SLinus Torvalds 
609682c1dSPaul Mundt #ifdef CONFIG_GENERIC_BUG
709682c1dSPaul Mundt #define BUGFLAG_WARNING		(1 << 0)
809682c1dSPaul Mundt #define BUGFLAG_TAINT(taint)	(BUGFLAG_WARNING | ((taint) << 8))
909682c1dSPaul Mundt #define BUG_GET_TAINT(bug)	((bug)->flags >> 8)
1009682c1dSPaul Mundt #endif
1109682c1dSPaul Mundt 
1209682c1dSPaul Mundt #ifndef __ASSEMBLY__
1309682c1dSPaul Mundt #include <linux/kernel.h>
1409682c1dSPaul Mundt 
15c8538a7aSMatt Mackall #ifdef CONFIG_BUG
167664c5a1SJeremy Fitzhardinge 
177664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
187664c5a1SJeremy Fitzhardinge struct bug_entry {
19b93a531eSJan Beulich #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
207664c5a1SJeremy Fitzhardinge 	unsigned long	bug_addr;
21b93a531eSJan Beulich #else
22b93a531eSJan Beulich 	signed int	bug_addr_disp;
23b93a531eSJan Beulich #endif
247664c5a1SJeremy Fitzhardinge #ifdef CONFIG_DEBUG_BUGVERBOSE
25b93a531eSJan Beulich #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
267664c5a1SJeremy Fitzhardinge 	const char	*file;
27b93a531eSJan Beulich #else
28b93a531eSJan Beulich 	signed int	file_disp;
29b93a531eSJan Beulich #endif
307664c5a1SJeremy Fitzhardinge 	unsigned short	line;
317664c5a1SJeremy Fitzhardinge #endif
327664c5a1SJeremy Fitzhardinge 	unsigned short	flags;
337664c5a1SJeremy Fitzhardinge };
347664c5a1SJeremy Fitzhardinge #endif	/* CONFIG_GENERIC_BUG */
357664c5a1SJeremy Fitzhardinge 
36af9379c7SDavid Brownell /*
37af9379c7SDavid Brownell  * Don't use BUG() or BUG_ON() unless there's really no way out; one
38af9379c7SDavid Brownell  * example might be detecting data structure corruption in the middle
39af9379c7SDavid Brownell  * of an operation that can't be backed out of.  If the (sub)system
40af9379c7SDavid Brownell  * can somehow continue operating, perhaps with reduced functionality,
41af9379c7SDavid Brownell  * it's probably not BUG-worthy.
42af9379c7SDavid Brownell  *
43af9379c7SDavid Brownell  * If you're tempted to BUG(), think again:  is completely giving up
44af9379c7SDavid Brownell  * really the *only* solution?  There are usually better options, where
45af9379c7SDavid Brownell  * users don't need to reboot ASAP and can mostly shut down cleanly.
46af9379c7SDavid Brownell  */
471da177e4SLinus Torvalds #ifndef HAVE_ARCH_BUG
481da177e4SLinus Torvalds #define BUG() do { \
49d5c003b4SHarvey Harrison 	printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
501da177e4SLinus Torvalds 	panic("BUG!"); \
511da177e4SLinus Torvalds } while (0)
521da177e4SLinus Torvalds #endif
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #ifndef HAVE_ARCH_BUG_ON
552a41de48SAlexey Dobriyan #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
561da177e4SLinus Torvalds #endif
571da177e4SLinus Torvalds 
58af9379c7SDavid Brownell /*
59af9379c7SDavid Brownell  * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
60af9379c7SDavid Brownell  * significant issues that need prompt attention if they should ever
61af9379c7SDavid Brownell  * appear at runtime.  Use the versions with printk format strings
62af9379c7SDavid Brownell  * to provide better diagnostics.
63af9379c7SDavid Brownell  */
64b2be0527SBen Hutchings #ifndef __WARN_TAINT
65b9075fa9SJoe Perches extern __printf(3, 4)
66b9075fa9SJoe Perches void warn_slowpath_fmt(const char *file, const int line,
67b9075fa9SJoe Perches 		       const char *fmt, ...);
68b9075fa9SJoe Perches extern __printf(4, 5)
69b9075fa9SJoe Perches void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
70b9075fa9SJoe Perches 			     const char *fmt, ...);
7157adc4d2SAndi Kleen extern void warn_slowpath_null(const char *file, const int line);
7279b4cc5eSArjan van de Ven #define WANT_WARN_ON_SLOWPATH
7357adc4d2SAndi Kleen #define __WARN()		warn_slowpath_null(__FILE__, __LINE__)
7457adc4d2SAndi Kleen #define __WARN_printf(arg...)	warn_slowpath_fmt(__FILE__, __LINE__, arg)
75b2be0527SBen Hutchings #define __WARN_printf_taint(taint, arg...)				\
76b2be0527SBen Hutchings 	warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg)
77a8f18b90SArjan van de Ven #else
78b2be0527SBen Hutchings #define __WARN()		__WARN_TAINT(TAINT_WARN)
79f6f286f3SArjan van de Ven #define __WARN_printf(arg...)	do { printk(arg); __WARN(); } while (0)
80b2be0527SBen Hutchings #define __WARN_printf_taint(taint, arg...)				\
81b2be0527SBen Hutchings 	do { printk(arg); __WARN_TAINT(taint); } while (0)
823a6a62f9SOlof Johansson #endif
833a6a62f9SOlof Johansson 
843a6a62f9SOlof Johansson #ifndef WARN_ON
853a6a62f9SOlof Johansson #define WARN_ON(condition) ({						\
863a6a62f9SOlof Johansson 	int __ret_warn_on = !!(condition);				\
873a6a62f9SOlof Johansson 	if (unlikely(__ret_warn_on))					\
883a6a62f9SOlof Johansson 		__WARN();						\
89684f9783SHerbert Xu 	unlikely(__ret_warn_on);					\
90684f9783SHerbert Xu })
911da177e4SLinus Torvalds #endif
921da177e4SLinus Torvalds 
93a8f18b90SArjan van de Ven #ifndef WARN
94a8f18b90SArjan van de Ven #define WARN(condition, format...) ({						\
95a8f18b90SArjan van de Ven 	int __ret_warn_on = !!(condition);				\
96a8f18b90SArjan van de Ven 	if (unlikely(__ret_warn_on))					\
97a8f18b90SArjan van de Ven 		__WARN_printf(format);					\
98a8f18b90SArjan van de Ven 	unlikely(__ret_warn_on);					\
99a8f18b90SArjan van de Ven })
100a8f18b90SArjan van de Ven #endif
101a8f18b90SArjan van de Ven 
102b2be0527SBen Hutchings #define WARN_TAINT(condition, taint, format...) ({			\
103b2be0527SBen Hutchings 	int __ret_warn_on = !!(condition);				\
104b2be0527SBen Hutchings 	if (unlikely(__ret_warn_on))					\
105b2be0527SBen Hutchings 		__WARN_printf_taint(taint, format);			\
106b2be0527SBen Hutchings 	unlikely(__ret_warn_on);					\
107b2be0527SBen Hutchings })
108b2be0527SBen Hutchings 
109684f9783SHerbert Xu #define WARN_ON_ONCE(condition)	({				\
1107ccaba53SJan Beulich 	static bool __section(.data.unlikely) __warned;		\
1118d4fbcfbSLinus Torvalds 	int __ret_warn_once = !!(condition);			\
11274bb6a09SIngo Molnar 								\
113d69a8922SAndrew Morton 	if (unlikely(__ret_warn_once))				\
114d69a8922SAndrew Morton 		if (WARN_ON(!__warned)) 			\
11542f247c8SCesar Eduardo Barros 			__warned = true;			\
116684f9783SHerbert Xu 	unlikely(__ret_warn_once);				\
11774bb6a09SIngo Molnar })
11874bb6a09SIngo Molnar 
11945e9c0deSArjan van de Ven #define WARN_ONCE(condition, format...)	({			\
1207ccaba53SJan Beulich 	static bool __section(.data.unlikely) __warned;		\
12145e9c0deSArjan van de Ven 	int __ret_warn_once = !!(condition);			\
12245e9c0deSArjan van de Ven 								\
12345e9c0deSArjan van de Ven 	if (unlikely(__ret_warn_once))				\
12445e9c0deSArjan van de Ven 		if (WARN(!__warned, format)) 			\
12542f247c8SCesar Eduardo Barros 			__warned = true;			\
12645e9c0deSArjan van de Ven 	unlikely(__ret_warn_once);				\
12745e9c0deSArjan van de Ven })
12845e9c0deSArjan van de Ven 
129b2be0527SBen Hutchings #define WARN_TAINT_ONCE(condition, taint, format...)	({	\
1307ccaba53SJan Beulich 	static bool __section(.data.unlikely) __warned;		\
131b2be0527SBen Hutchings 	int __ret_warn_once = !!(condition);			\
132b2be0527SBen Hutchings 								\
133b2be0527SBen Hutchings 	if (unlikely(__ret_warn_once))				\
134b2be0527SBen Hutchings 		if (WARN_TAINT(!__warned, taint, format))	\
135b2be0527SBen Hutchings 			__warned = true;			\
136b2be0527SBen Hutchings 	unlikely(__ret_warn_once);				\
137b2be0527SBen Hutchings })
138b2be0527SBen Hutchings 
139b607e70eSJosh Triplett #else /* !CONFIG_BUG */
140b607e70eSJosh Triplett #ifndef HAVE_ARCH_BUG
141a4b5d580SJosh Triplett #define BUG() do {} while (1)
142b607e70eSJosh Triplett #endif
143b607e70eSJosh Triplett 
144b607e70eSJosh Triplett #ifndef HAVE_ARCH_BUG_ON
145b607e70eSJosh Triplett #define BUG_ON(condition) do { if (condition) ; } while (0)
146b607e70eSJosh Triplett #endif
147b607e70eSJosh Triplett 
148b607e70eSJosh Triplett #ifndef HAVE_ARCH_WARN_ON
149b607e70eSJosh Triplett #define WARN_ON(condition) ({						\
150b607e70eSJosh Triplett 	int __ret_warn_on = !!(condition);				\
151b607e70eSJosh Triplett 	unlikely(__ret_warn_on);					\
152b607e70eSJosh Triplett })
153b607e70eSJosh Triplett #endif
154b607e70eSJosh Triplett 
155b607e70eSJosh Triplett #ifndef WARN
156b607e70eSJosh Triplett #define WARN(condition, format...) ({					\
157b607e70eSJosh Triplett 	int __ret_warn_on = !!(condition);				\
1584e50ebdeSJosh Triplett 	no_printk(format);						\
159b607e70eSJosh Triplett 	unlikely(__ret_warn_on);					\
160b607e70eSJosh Triplett })
161b607e70eSJosh Triplett #endif
162b607e70eSJosh Triplett 
163b607e70eSJosh Triplett #define WARN_ON_ONCE(condition) WARN_ON(condition)
164b607e70eSJosh Triplett #define WARN_ONCE(condition, format...) WARN(condition, format)
165b607e70eSJosh Triplett #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
166b607e70eSJosh Triplett #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
167b607e70eSJosh Triplett 
168b607e70eSJosh Triplett #endif
169b607e70eSJosh Triplett 
1702092e6beSSteven Rostedt /*
1712092e6beSSteven Rostedt  * WARN_ON_SMP() is for cases that the warning is either
1722092e6beSSteven Rostedt  * meaningless for !SMP or may even cause failures.
1732092e6beSSteven Rostedt  * This is usually used for cases that we have
1742092e6beSSteven Rostedt  * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
1752092e6beSSteven Rostedt  * returns 0 for uniprocessor settings.
1762092e6beSSteven Rostedt  * It can also be used with values that are only defined
1772092e6beSSteven Rostedt  * on SMP:
1782092e6beSSteven Rostedt  *
1792092e6beSSteven Rostedt  * struct foo {
1802092e6beSSteven Rostedt  *  [...]
1812092e6beSSteven Rostedt  * #ifdef CONFIG_SMP
1822092e6beSSteven Rostedt  *	int bar;
1832092e6beSSteven Rostedt  * #endif
1842092e6beSSteven Rostedt  * };
1852092e6beSSteven Rostedt  *
1862092e6beSSteven Rostedt  * void func(struct foo *zoot)
1872092e6beSSteven Rostedt  * {
1882092e6beSSteven Rostedt  *	WARN_ON_SMP(!zoot->bar);
1892092e6beSSteven Rostedt  *
1902092e6beSSteven Rostedt  * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
1912092e6beSSteven Rostedt  * and should be a nop and return false for uniprocessor.
1922092e6beSSteven Rostedt  *
1932092e6beSSteven Rostedt  * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
1942092e6beSSteven Rostedt  * and x is true.
1952092e6beSSteven Rostedt  */
1968eb94f80SIngo Molnar #ifdef CONFIG_SMP
1978eb94f80SIngo Molnar # define WARN_ON_SMP(x)			WARN_ON(x)
1988eb94f80SIngo Molnar #else
199ccd0d44fSSteven Rostedt /*
200ccd0d44fSSteven Rostedt  * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
201ccd0d44fSSteven Rostedt  * a stand alone line statement or as a condition in an if ()
202ccd0d44fSSteven Rostedt  * statement.
203ccd0d44fSSteven Rostedt  * A simple "0" would cause gcc to give a "statement has no effect"
204ccd0d44fSSteven Rostedt  * warning.
205ccd0d44fSSteven Rostedt  */
2062092e6beSSteven Rostedt # define WARN_ON_SMP(x)			({0;})
2078eb94f80SIngo Molnar #endif
2088eb94f80SIngo Molnar 
2092603efa3SPaul Mundt #endif /* __ASSEMBLY__ */
2102603efa3SPaul Mundt 
2111da177e4SLinus Torvalds #endif
212