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