blob: af2cc94a61bf9e1e7f4e29d6bd1fd1ad997bbf5e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _ASM_GENERIC_BUG_H
3#define _ASM_GENERIC_BUG_H
4
5#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Paul Mundt09682c12012-06-25 17:15:31 +09007#ifdef CONFIG_GENERIC_BUG
8#define BUGFLAG_WARNING (1 << 0)
Peter Zijlstra19d43622017-02-25 08:56:53 +01009#define BUGFLAG_ONCE (1 << 1)
10#define BUGFLAG_DONE (1 << 2)
Peter Zijlstraf26dee12017-04-10 10:49:39 +020011#define BUGFLAG_TAINT(taint) ((taint) << 8)
Paul Mundt09682c12012-06-25 17:15:31 +090012#define BUG_GET_TAINT(bug) ((bug)->flags >> 8)
13#endif
14
15#ifndef __ASSEMBLY__
16#include <linux/kernel.h>
17
Matt Mackallc8538a72005-05-01 08:59:01 -070018#ifdef CONFIG_BUG
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080019
20#ifdef CONFIG_GENERIC_BUG
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080021struct bug_entry {
Jan Beulichb93a5312008-12-16 11:40:27 +000022#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080023 unsigned long bug_addr;
Jan Beulichb93a5312008-12-16 11:40:27 +000024#else
25 signed int bug_addr_disp;
26#endif
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080027#ifdef CONFIG_DEBUG_BUGVERBOSE
Jan Beulichb93a5312008-12-16 11:40:27 +000028#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080029 const char *file;
Jan Beulichb93a5312008-12-16 11:40:27 +000030#else
31 signed int file_disp;
32#endif
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080033 unsigned short line;
34#endif
35 unsigned short flags;
36};
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080037#endif /* CONFIG_GENERIC_BUG */
38
David Brownellaf9379c2009-01-06 14:41:01 -080039/*
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.
45 *
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.
49 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifndef HAVE_ARCH_BUG
51#define BUG() do { \
Harvey Harrisond5c003b2008-10-15 22:01:24 -070052 printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 panic("BUG!"); \
54} while (0)
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef HAVE_ARCH_BUG_ON
Josh Tripletta3f76072014-04-07 15:39:11 -070058#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Peter Zijlstra19d43622017-02-25 08:56:53 +010061#ifdef __WARN_FLAGS
62#define __WARN_TAINT(taint) __WARN_FLAGS(BUGFLAG_TAINT(taint))
63#define __WARN_ONCE_TAINT(taint) __WARN_FLAGS(BUGFLAG_ONCE|BUGFLAG_TAINT(taint))
64
65#define WARN_ON_ONCE(condition) ({ \
66 int __ret_warn_on = !!(condition); \
67 if (unlikely(__ret_warn_on)) \
68 __WARN_ONCE_TAINT(TAINT_WARN); \
69 unlikely(__ret_warn_on); \
70})
71#endif
72
David Brownellaf9379c2009-01-06 14:41:01 -080073/*
74 * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
75 * significant issues that need prompt attention if they should ever
76 * appear at runtime. Use the versions with printk format strings
77 * to provide better diagnostics.
78 */
Ben Hutchingsb2be0522010-04-03 19:34:56 +010079#ifndef __WARN_TAINT
Joe Perchesb9075fa2011-10-31 17:11:33 -070080extern __printf(3, 4)
81void warn_slowpath_fmt(const char *file, const int line,
82 const char *fmt, ...);
83extern __printf(4, 5)
84void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint,
85 const char *fmt, ...);
Andi Kleen57adc4d2009-05-06 16:02:53 -070086extern void warn_slowpath_null(const char *file, const int line);
Arjan van de Ven79b4cc52008-01-30 13:32:50 +010087#define WANT_WARN_ON_SLOWPATH
Andi Kleen57adc4d2009-05-06 16:02:53 -070088#define __WARN() warn_slowpath_null(__FILE__, __LINE__)
89#define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)
Ben Hutchingsb2be0522010-04-03 19:34:56 +010090#define __WARN_printf_taint(taint, arg...) \
91 warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg)
Arjan van de Vena8f18b92008-07-25 01:45:53 -070092#else
Ben Hutchingsb2be0522010-04-03 19:34:56 +010093#define __WARN() __WARN_TAINT(TAINT_WARN)
Ingo Molnarec5679e2008-11-28 17:56:14 +010094#define __WARN_printf(arg...) do { printk(arg); __WARN(); } while (0)
Ben Hutchingsb2be0522010-04-03 19:34:56 +010095#define __WARN_printf_taint(taint, arg...) \
96 do { printk(arg); __WARN_TAINT(taint); } while (0)
Olof Johansson3a6a62f92008-01-30 13:32:50 +010097#endif
98
Josh Poimboeuf2553b672016-03-17 14:23:04 -070099/* used internally by panic.c */
100struct warn_args;
Ian Abbott0b396922017-07-10 15:50:55 -0700101struct pt_regs;
Josh Poimboeuf2553b672016-03-17 14:23:04 -0700102
103void __warn(const char *file, int line, void *caller, unsigned taint,
104 struct pt_regs *regs, struct warn_args *args);
105
Olof Johansson3a6a62f92008-01-30 13:32:50 +0100106#ifndef WARN_ON
Herbert Xu684f9782006-09-29 01:59:06 -0700107#define WARN_ON(condition) ({ \
Linus Torvalds8d4fbcf2007-07-31 21:12:07 -0700108 int __ret_warn_on = !!(condition); \
Olof Johansson3a6a62f92008-01-30 13:32:50 +0100109 if (unlikely(__ret_warn_on)) \
110 __WARN(); \
Herbert Xu684f9782006-09-29 01:59:06 -0700111 unlikely(__ret_warn_on); \
112})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
114
Arjan van de Vena8f18b92008-07-25 01:45:53 -0700115#ifndef WARN
Peter Zijlstra19d43622017-02-25 08:56:53 +0100116#define WARN(condition, format...) ({ \
Arjan van de Vena8f18b92008-07-25 01:45:53 -0700117 int __ret_warn_on = !!(condition); \
118 if (unlikely(__ret_warn_on)) \
119 __WARN_printf(format); \
120 unlikely(__ret_warn_on); \
121})
122#endif
123
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100124#define WARN_TAINT(condition, taint, format...) ({ \
125 int __ret_warn_on = !!(condition); \
126 if (unlikely(__ret_warn_on)) \
127 __WARN_printf_taint(taint, format); \
128 unlikely(__ret_warn_on); \
129})
130
Peter Zijlstra19d43622017-02-25 08:56:53 +0100131#ifndef WARN_ON_ONCE
Andrew Mortond69a8922006-10-06 00:43:49 -0700132#define WARN_ON_ONCE(condition) ({ \
Jan Beulich7ccaba52012-03-23 15:01:52 -0700133 static bool __section(.data.unlikely) __warned; \
Linus Torvalds8d4fbcf2007-07-31 21:12:07 -0700134 int __ret_warn_once = !!(condition); \
Andrew Mortond69a8922006-10-06 00:43:49 -0700135 \
Steven Rostedtdfbf2892016-03-17 14:21:12 -0700136 if (unlikely(__ret_warn_once && !__warned)) { \
137 __warned = true; \
138 WARN_ON(1); \
139 } \
Andrew Mortond69a8922006-10-06 00:43:49 -0700140 unlikely(__ret_warn_once); \
Ingo Molnar74bb6a02006-06-25 05:48:09 -0700141})
Peter Zijlstra19d43622017-02-25 08:56:53 +0100142#endif
Ingo Molnar74bb6a02006-06-25 05:48:09 -0700143
Arjan van de Ven45e9c0d2008-09-15 16:43:18 -0700144#define WARN_ONCE(condition, format...) ({ \
Jan Beulich7ccaba52012-03-23 15:01:52 -0700145 static bool __section(.data.unlikely) __warned; \
Arjan van de Ven45e9c0d2008-09-15 16:43:18 -0700146 int __ret_warn_once = !!(condition); \
147 \
Steven Rostedtdfbf2892016-03-17 14:21:12 -0700148 if (unlikely(__ret_warn_once && !__warned)) { \
149 __warned = true; \
150 WARN(1, format); \
151 } \
Arjan van de Ven45e9c0d2008-09-15 16:43:18 -0700152 unlikely(__ret_warn_once); \
153})
154
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100155#define WARN_TAINT_ONCE(condition, taint, format...) ({ \
Jan Beulich7ccaba52012-03-23 15:01:52 -0700156 static bool __section(.data.unlikely) __warned; \
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100157 int __ret_warn_once = !!(condition); \
158 \
Steven Rostedtdfbf2892016-03-17 14:21:12 -0700159 if (unlikely(__ret_warn_once && !__warned)) { \
160 __warned = true; \
161 WARN_TAINT(1, taint, format); \
162 } \
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100163 unlikely(__ret_warn_once); \
164})
165
Josh Triplettb607e702014-04-07 15:39:10 -0700166#else /* !CONFIG_BUG */
167#ifndef HAVE_ARCH_BUG
Josh Tripletta4b5d582014-04-07 15:39:13 -0700168#define BUG() do {} while (1)
Josh Triplettb607e702014-04-07 15:39:10 -0700169#endif
170
171#ifndef HAVE_ARCH_BUG_ON
Arnd Bergmann3c047052015-11-21 00:27:26 +0100172#define BUG_ON(condition) do { if (condition) BUG(); } while (0)
Josh Triplettb607e702014-04-07 15:39:10 -0700173#endif
174
175#ifndef HAVE_ARCH_WARN_ON
176#define WARN_ON(condition) ({ \
177 int __ret_warn_on = !!(condition); \
178 unlikely(__ret_warn_on); \
179})
180#endif
181
182#ifndef WARN
183#define WARN(condition, format...) ({ \
184 int __ret_warn_on = !!(condition); \
Josh Triplett4e50ebde2014-04-07 15:39:12 -0700185 no_printk(format); \
Josh Triplettb607e702014-04-07 15:39:10 -0700186 unlikely(__ret_warn_on); \
187})
188#endif
189
190#define WARN_ON_ONCE(condition) WARN_ON(condition)
191#define WARN_ONCE(condition, format...) WARN(condition, format)
192#define WARN_TAINT(condition, taint, format...) WARN(condition, format)
193#define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
194
195#endif
196
Steven Rostedt2092e6b2011-03-17 15:21:06 -0400197/*
198 * WARN_ON_SMP() is for cases that the warning is either
199 * meaningless for !SMP or may even cause failures.
200 * This is usually used for cases that we have
201 * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked()
202 * returns 0 for uniprocessor settings.
203 * It can also be used with values that are only defined
204 * on SMP:
205 *
206 * struct foo {
207 * [...]
208 * #ifdef CONFIG_SMP
209 * int bar;
210 * #endif
211 * };
212 *
213 * void func(struct foo *zoot)
214 * {
215 * WARN_ON_SMP(!zoot->bar);
216 *
217 * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
218 * and should be a nop and return false for uniprocessor.
219 *
220 * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
221 * and x is true.
222 */
Ingo Molnar8eb94f82006-06-27 02:54:50 -0700223#ifdef CONFIG_SMP
224# define WARN_ON_SMP(x) WARN_ON(x)
225#else
Steven Rostedtccd0d442011-03-25 16:21:06 -0400226/*
227 * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
228 * a stand alone line statement or as a condition in an if ()
229 * statement.
230 * A simple "0" would cause gcc to give a "statement has no effect"
231 * warning.
232 */
Steven Rostedt2092e6b2011-03-17 15:21:06 -0400233# define WARN_ON_SMP(x) ({0;})
Ingo Molnar8eb94f82006-06-27 02:54:50 -0700234#endif
235
Paul Mundt2603efa2012-06-18 13:54:17 +0900236#endif /* __ASSEMBLY__ */
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif