blob: 384b5c835ced334a19d2552c914cc65be19d69a3 [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
Kees Cook2a8358d2017-11-17 15:27:21 -08007#define CUT_HERE "------------[ cut here ]------------\n"
8
Paul Mundt09682c12012-06-25 17:15:31 +09009#ifdef CONFIG_GENERIC_BUG
10#define BUGFLAG_WARNING (1 << 0)
Peter Zijlstra19d43622017-02-25 08:56:53 +010011#define BUGFLAG_ONCE (1 << 1)
12#define BUGFLAG_DONE (1 << 2)
Kees Cooka44f71a2019-09-25 16:48:11 -070013#define BUGFLAG_NO_CUT_HERE (1 << 3) /* CUT_HERE already sent */
Peter Zijlstraf26dee12017-04-10 10:49:39 +020014#define BUGFLAG_TAINT(taint) ((taint) << 8)
Paul Mundt09682c12012-06-25 17:15:31 +090015#define BUG_GET_TAINT(bug) ((bug)->flags >> 8)
16#endif
17
18#ifndef __ASSEMBLY__
19#include <linux/kernel.h>
20
Ingo Molnarffb61c62018-12-19 11:20:50 +010021#ifdef CONFIG_BUG
22
Nadav Amitf81f8ad2018-10-03 14:30:56 -070023#ifdef CONFIG_GENERIC_BUG
Ingo Molnarffb61c62018-12-19 11:20:50 +010024struct bug_entry {
Jan Beulichb93a5312008-12-16 11:40:27 +000025#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080026 unsigned long bug_addr;
Jan Beulichb93a5312008-12-16 11:40:27 +000027#else
28 signed int bug_addr_disp;
29#endif
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080030#ifdef CONFIG_DEBUG_BUGVERBOSE
Jan Beulichb93a5312008-12-16 11:40:27 +000031#ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080032 const char *file;
Jan Beulichb93a5312008-12-16 11:40:27 +000033#else
34 signed int file_disp;
35#endif
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080036 unsigned short line;
37#endif
38 unsigned short flags;
Nadav Amitf81f8ad2018-10-03 14:30:56 -070039};
Ingo Molnarffb61c62018-12-19 11:20:50 +010040#endif /* CONFIG_GENERIC_BUG */
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080041
David Brownellaf9379c2009-01-06 14:41:01 -080042/*
43 * Don't use BUG() or BUG_ON() unless there's really no way out; one
44 * example might be detecting data structure corruption in the middle
45 * of an operation that can't be backed out of. If the (sub)system
46 * can somehow continue operating, perhaps with reduced functionality,
47 * it's probably not BUG-worthy.
48 *
49 * If you're tempted to BUG(), think again: is completely giving up
50 * really the *only* solution? There are usually better options, where
51 * users don't need to reboot ASAP and can mostly shut down cleanly.
52 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifndef HAVE_ARCH_BUG
54#define BUG() do { \
Harvey Harrisond5c003b2008-10-15 22:01:24 -070055 printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
Arnd Bergmann173a3ef2018-02-21 14:45:54 -080056 barrier_before_unreachable(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 panic("BUG!"); \
58} while (0)
59#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifndef HAVE_ARCH_BUG_ON
Josh Tripletta3f76072014-04-07 15:39:11 -070062#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
David Brownellaf9379c2009-01-06 14:41:01 -080065/*
66 * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
Dmitry Vyukov96c6a322018-08-21 21:55:24 -070067 * significant kernel issues that need prompt attention if they should ever
68 * appear at runtime.
69 *
70 * Do not use these macros when checking for invalid external inputs
71 * (e.g. invalid system call arguments, or invalid data coming from
72 * network/devices), and on transient conditions like ENOMEM or EAGAIN.
73 * These macros should be used for recoverable kernel issues only.
74 * For invalid external inputs, transient conditions, etc use
75 * pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
76 * Do not include "BUG"/"WARNING" in format strings manually to make these
77 * conditions distinguishable from kernel issues.
78 *
79 * Use the versions with printk format strings to provide better diagnostics.
David Brownellaf9379c2009-01-06 14:41:01 -080080 */
Kees Cookd4bce142019-09-25 16:48:04 -070081#ifndef __WARN_FLAGS
Joe Perchesb9075fa2011-10-31 17:11:33 -070082extern __printf(4, 5)
Kees Cookee871132019-09-25 16:47:52 -070083void warn_slowpath_fmt(const char *file, const int line, unsigned taint,
84 const char *fmt, ...);
Kees Cookf2f84b02019-09-25 16:47:58 -070085#define __WARN() __WARN_printf(TAINT_WARN, NULL)
Kees Cook89348fc2019-09-25 16:47:55 -070086#define __WARN_printf(taint, arg...) \
Kees Cookee871132019-09-25 16:47:52 -070087 warn_slowpath_fmt(__FILE__, __LINE__, taint, arg)
Arjan van de Vena8f18b92008-07-25 01:45:53 -070088#else
Kees Cooka7bed27a2017-11-17 15:27:24 -080089extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
Kees Cooka44f71a2019-09-25 16:48:11 -070090#define __WARN() __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN))
Kees Cookd4bce142019-09-25 16:48:04 -070091#define __WARN_printf(taint, arg...) do { \
92 __warn_printk(arg); \
Kees Cooka44f71a2019-09-25 16:48:11 -070093 __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
Kees Cookd4bce142019-09-25 16:48:04 -070094 } while (0)
Kees Cook2da1ead2019-09-25 16:48:08 -070095#define WARN_ON_ONCE(condition) ({ \
96 int __ret_warn_on = !!(condition); \
97 if (unlikely(__ret_warn_on)) \
98 __WARN_FLAGS(BUGFLAG_ONCE | \
99 BUGFLAG_TAINT(TAINT_WARN)); \
100 unlikely(__ret_warn_on); \
101})
Olof Johansson3a6a62f92008-01-30 13:32:50 +0100102#endif
103
Josh Poimboeuf2553b672016-03-17 14:23:04 -0700104/* used internally by panic.c */
105struct warn_args;
Ian Abbott0b396922017-07-10 15:50:55 -0700106struct pt_regs;
Josh Poimboeuf2553b672016-03-17 14:23:04 -0700107
108void __warn(const char *file, int line, void *caller, unsigned taint,
109 struct pt_regs *regs, struct warn_args *args);
110
Olof Johansson3a6a62f92008-01-30 13:32:50 +0100111#ifndef WARN_ON
Herbert Xu684f9782006-09-29 01:59:06 -0700112#define WARN_ON(condition) ({ \
Linus Torvalds8d4fbcf2007-07-31 21:12:07 -0700113 int __ret_warn_on = !!(condition); \
Olof Johansson3a6a62f92008-01-30 13:32:50 +0100114 if (unlikely(__ret_warn_on)) \
115 __WARN(); \
Herbert Xu684f9782006-09-29 01:59:06 -0700116 unlikely(__ret_warn_on); \
117})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#endif
119
Arjan van de Vena8f18b92008-07-25 01:45:53 -0700120#ifndef WARN
Peter Zijlstra19d43622017-02-25 08:56:53 +0100121#define WARN(condition, format...) ({ \
Arjan van de Vena8f18b92008-07-25 01:45:53 -0700122 int __ret_warn_on = !!(condition); \
123 if (unlikely(__ret_warn_on)) \
Kees Cook89348fc2019-09-25 16:47:55 -0700124 __WARN_printf(TAINT_WARN, format); \
Arjan van de Vena8f18b92008-07-25 01:45:53 -0700125 unlikely(__ret_warn_on); \
126})
127#endif
128
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100129#define WARN_TAINT(condition, taint, format...) ({ \
130 int __ret_warn_on = !!(condition); \
131 if (unlikely(__ret_warn_on)) \
Kees Cook89348fc2019-09-25 16:47:55 -0700132 __WARN_printf(taint, format); \
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100133 unlikely(__ret_warn_on); \
134})
135
Peter Zijlstra19d43622017-02-25 08:56:53 +0100136#ifndef WARN_ON_ONCE
Andrew Mortond69a8922006-10-06 00:43:49 -0700137#define WARN_ON_ONCE(condition) ({ \
Andi Kleenb1fca272017-11-17 15:27:03 -0800138 static bool __section(.data.once) __warned; \
Linus Torvalds8d4fbcf2007-07-31 21:12:07 -0700139 int __ret_warn_once = !!(condition); \
Andrew Mortond69a8922006-10-06 00:43:49 -0700140 \
Steven Rostedtdfbf2892016-03-17 14:21:12 -0700141 if (unlikely(__ret_warn_once && !__warned)) { \
142 __warned = true; \
143 WARN_ON(1); \
144 } \
Andrew Mortond69a8922006-10-06 00:43:49 -0700145 unlikely(__ret_warn_once); \
Ingo Molnar74bb6a02006-06-25 05:48:09 -0700146})
Peter Zijlstra19d43622017-02-25 08:56:53 +0100147#endif
Ingo Molnar74bb6a02006-06-25 05:48:09 -0700148
Arjan van de Ven45e9c0d2008-09-15 16:43:18 -0700149#define WARN_ONCE(condition, format...) ({ \
Andi Kleenb1fca272017-11-17 15:27:03 -0800150 static bool __section(.data.once) __warned; \
Arjan van de Ven45e9c0d2008-09-15 16:43:18 -0700151 int __ret_warn_once = !!(condition); \
152 \
Steven Rostedtdfbf2892016-03-17 14:21:12 -0700153 if (unlikely(__ret_warn_once && !__warned)) { \
154 __warned = true; \
155 WARN(1, format); \
156 } \
Arjan van de Ven45e9c0d2008-09-15 16:43:18 -0700157 unlikely(__ret_warn_once); \
158})
159
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100160#define WARN_TAINT_ONCE(condition, taint, format...) ({ \
Andi Kleenb1fca272017-11-17 15:27:03 -0800161 static bool __section(.data.once) __warned; \
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100162 int __ret_warn_once = !!(condition); \
163 \
Steven Rostedtdfbf2892016-03-17 14:21:12 -0700164 if (unlikely(__ret_warn_once && !__warned)) { \
165 __warned = true; \
166 WARN_TAINT(1, taint, format); \
167 } \
Ben Hutchingsb2be0522010-04-03 19:34:56 +0100168 unlikely(__ret_warn_once); \
169})
170
Josh Triplettb607e702014-04-07 15:39:10 -0700171#else /* !CONFIG_BUG */
172#ifndef HAVE_ARCH_BUG
Josh Tripletta4b5d582014-04-07 15:39:13 -0700173#define BUG() do {} while (1)
Josh Triplettb607e702014-04-07 15:39:10 -0700174#endif
175
176#ifndef HAVE_ARCH_BUG_ON
Denis Efremov9b876472019-08-29 00:09:34 +0300177#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
Josh Triplettb607e702014-04-07 15:39:10 -0700178#endif
179
180#ifndef HAVE_ARCH_WARN_ON
181#define WARN_ON(condition) ({ \
182 int __ret_warn_on = !!(condition); \
183 unlikely(__ret_warn_on); \
184})
185#endif
186
187#ifndef WARN
188#define WARN(condition, format...) ({ \
189 int __ret_warn_on = !!(condition); \
Josh Triplett4e50ebde2014-04-07 15:39:12 -0700190 no_printk(format); \
Josh Triplettb607e702014-04-07 15:39:10 -0700191 unlikely(__ret_warn_on); \
192})
193#endif
194
195#define WARN_ON_ONCE(condition) WARN_ON(condition)
196#define WARN_ONCE(condition, format...) WARN(condition, format)
197#define WARN_TAINT(condition, taint, format...) WARN(condition, format)
198#define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
199
200#endif
201
Steven Rostedt2092e6b2011-03-17 15:21:06 -0400202/*
203 * WARN_ON_SMP() is for cases that the warning is either
204 * meaningless for !SMP or may even cause failures.
Steven Rostedt2092e6b2011-03-17 15:21:06 -0400205 * It can also be used with values that are only defined
206 * on SMP:
207 *
208 * struct foo {
209 * [...]
210 * #ifdef CONFIG_SMP
211 * int bar;
212 * #endif
213 * };
214 *
215 * void func(struct foo *zoot)
216 * {
217 * WARN_ON_SMP(!zoot->bar);
218 *
219 * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
220 * and should be a nop and return false for uniprocessor.
221 *
222 * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
223 * and x is true.
224 */
Ingo Molnar8eb94f82006-06-27 02:54:50 -0700225#ifdef CONFIG_SMP
226# define WARN_ON_SMP(x) WARN_ON(x)
227#else
Steven Rostedtccd0d442011-03-25 16:21:06 -0400228/*
229 * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
230 * a stand alone line statement or as a condition in an if ()
231 * statement.
232 * A simple "0" would cause gcc to give a "statement has no effect"
233 * warning.
234 */
Steven Rostedt2092e6b2011-03-17 15:21:06 -0400235# define WARN_ON_SMP(x) ({0;})
Ingo Molnar8eb94f82006-06-27 02:54:50 -0700236#endif
237
Paul Mundt2603efa2012-06-18 13:54:17 +0900238#endif /* __ASSEMBLY__ */
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#endif