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 7 #ifdef CONFIG_GENERIC_BUG 8 #define BUGFLAG_WARNING (1 << 0) 9 #define BUGFLAG_ONCE (1 << 1) 10 #define BUGFLAG_DONE (1 << 2) 11 #define BUGFLAG_TAINT(taint) ((taint) << 8) 12 #define BUG_GET_TAINT(bug) ((bug)->flags >> 8) 13 #endif 14 15 #ifndef __ASSEMBLY__ 16 #include <linux/kernel.h> 17 18 #ifdef CONFIG_BUG 19 20 #ifdef CONFIG_GENERIC_BUG 21 struct bug_entry { 22 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS 23 unsigned long bug_addr; 24 #else 25 signed int bug_addr_disp; 26 #endif 27 #ifdef CONFIG_DEBUG_BUGVERBOSE 28 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS 29 const char *file; 30 #else 31 signed int file_disp; 32 #endif 33 unsigned short line; 34 #endif 35 unsigned short flags; 36 }; 37 #endif /* CONFIG_GENERIC_BUG */ 38 39 /* 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 */ 50 #ifndef HAVE_ARCH_BUG 51 #define BUG() do { \ 52 printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ 53 barrier_before_unreachable(); \ 54 panic("BUG!"); \ 55 } while (0) 56 #endif 57 58 #ifndef HAVE_ARCH_BUG_ON 59 #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0) 60 #endif 61 62 #ifdef __WARN_FLAGS 63 #define __WARN_TAINT(taint) __WARN_FLAGS(BUGFLAG_TAINT(taint)) 64 #define __WARN_ONCE_TAINT(taint) __WARN_FLAGS(BUGFLAG_ONCE|BUGFLAG_TAINT(taint)) 65 66 #define WARN_ON_ONCE(condition) ({ \ 67 int __ret_warn_on = !!(condition); \ 68 if (unlikely(__ret_warn_on)) \ 69 __WARN_ONCE_TAINT(TAINT_WARN); \ 70 unlikely(__ret_warn_on); \ 71 }) 72 #endif 73 74 /* 75 * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report 76 * significant issues that need prompt attention if they should ever 77 * appear at runtime. Use the versions with printk format strings 78 * to provide better diagnostics. 79 */ 80 #ifndef __WARN_TAINT 81 extern __printf(3, 4) 82 void warn_slowpath_fmt(const char *file, const int line, 83 const char *fmt, ...); 84 extern __printf(4, 5) 85 void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint, 86 const char *fmt, ...); 87 extern void warn_slowpath_null(const char *file, const int line); 88 #define WANT_WARN_ON_SLOWPATH 89 #define __WARN() warn_slowpath_null(__FILE__, __LINE__) 90 #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg) 91 #define __WARN_printf_taint(taint, arg...) \ 92 warn_slowpath_fmt_taint(__FILE__, __LINE__, taint, arg) 93 #else 94 #define __WARN() __WARN_TAINT(TAINT_WARN) 95 #define __WARN_printf(arg...) do { printk(arg); __WARN(); } while (0) 96 #define __WARN_printf_taint(taint, arg...) \ 97 do { printk(arg); __WARN_TAINT(taint); } while (0) 98 #endif 99 100 /* used internally by panic.c */ 101 struct warn_args; 102 struct pt_regs; 103 104 void __warn(const char *file, int line, void *caller, unsigned taint, 105 struct pt_regs *regs, struct warn_args *args); 106 107 #ifndef WARN_ON 108 #define WARN_ON(condition) ({ \ 109 int __ret_warn_on = !!(condition); \ 110 if (unlikely(__ret_warn_on)) \ 111 __WARN(); \ 112 unlikely(__ret_warn_on); \ 113 }) 114 #endif 115 116 #ifndef WARN 117 #define WARN(condition, format...) ({ \ 118 int __ret_warn_on = !!(condition); \ 119 if (unlikely(__ret_warn_on)) \ 120 __WARN_printf(format); \ 121 unlikely(__ret_warn_on); \ 122 }) 123 #endif 124 125 #define WARN_TAINT(condition, taint, format...) ({ \ 126 int __ret_warn_on = !!(condition); \ 127 if (unlikely(__ret_warn_on)) \ 128 __WARN_printf_taint(taint, format); \ 129 unlikely(__ret_warn_on); \ 130 }) 131 132 #ifndef WARN_ON_ONCE 133 #define WARN_ON_ONCE(condition) ({ \ 134 static bool __section(.data.unlikely) __warned; \ 135 int __ret_warn_once = !!(condition); \ 136 \ 137 if (unlikely(__ret_warn_once && !__warned)) { \ 138 __warned = true; \ 139 WARN_ON(1); \ 140 } \ 141 unlikely(__ret_warn_once); \ 142 }) 143 #endif 144 145 #define WARN_ONCE(condition, format...) ({ \ 146 static bool __section(.data.unlikely) __warned; \ 147 int __ret_warn_once = !!(condition); \ 148 \ 149 if (unlikely(__ret_warn_once && !__warned)) { \ 150 __warned = true; \ 151 WARN(1, format); \ 152 } \ 153 unlikely(__ret_warn_once); \ 154 }) 155 156 #define WARN_TAINT_ONCE(condition, taint, format...) ({ \ 157 static bool __section(.data.unlikely) __warned; \ 158 int __ret_warn_once = !!(condition); \ 159 \ 160 if (unlikely(__ret_warn_once && !__warned)) { \ 161 __warned = true; \ 162 WARN_TAINT(1, taint, format); \ 163 } \ 164 unlikely(__ret_warn_once); \ 165 }) 166 167 #else /* !CONFIG_BUG */ 168 #ifndef HAVE_ARCH_BUG 169 #define BUG() do {} while (1) 170 #endif 171 172 #ifndef HAVE_ARCH_BUG_ON 173 #define BUG_ON(condition) do { if (condition) BUG(); } while (0) 174 #endif 175 176 #ifndef HAVE_ARCH_WARN_ON 177 #define WARN_ON(condition) ({ \ 178 int __ret_warn_on = !!(condition); \ 179 unlikely(__ret_warn_on); \ 180 }) 181 #endif 182 183 #ifndef WARN 184 #define WARN(condition, format...) ({ \ 185 int __ret_warn_on = !!(condition); \ 186 no_printk(format); \ 187 unlikely(__ret_warn_on); \ 188 }) 189 #endif 190 191 #define WARN_ON_ONCE(condition) WARN_ON(condition) 192 #define WARN_ONCE(condition, format...) WARN(condition, format) 193 #define WARN_TAINT(condition, taint, format...) WARN(condition, format) 194 #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format) 195 196 #endif 197 198 /* 199 * WARN_ON_SMP() is for cases that the warning is either 200 * meaningless for !SMP or may even cause failures. 201 * This is usually used for cases that we have 202 * WARN_ON(!spin_is_locked(&lock)) checks, as spin_is_locked() 203 * returns 0 for uniprocessor settings. 204 * It can also be used with values that are only defined 205 * on SMP: 206 * 207 * struct foo { 208 * [...] 209 * #ifdef CONFIG_SMP 210 * int bar; 211 * #endif 212 * }; 213 * 214 * void func(struct foo *zoot) 215 * { 216 * WARN_ON_SMP(!zoot->bar); 217 * 218 * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(), 219 * and should be a nop and return false for uniprocessor. 220 * 221 * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set 222 * and x is true. 223 */ 224 #ifdef CONFIG_SMP 225 # define WARN_ON_SMP(x) WARN_ON(x) 226 #else 227 /* 228 * Use of ({0;}) because WARN_ON_SMP(x) may be used either as 229 * a stand alone line statement or as a condition in an if () 230 * statement. 231 * A simple "0" would cause gcc to give a "statement has no effect" 232 * warning. 233 */ 234 # define WARN_ON_SMP(x) ({0;}) 235 #endif 236 237 #endif /* __ASSEMBLY__ */ 238 239 #endif 240