1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef _ASM_GENERIC_BUG_H 31da177e4SLinus Torvalds #define _ASM_GENERIC_BUG_H 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds #include <linux/compiler.h> 61da177e4SLinus Torvalds 72a8358d8SKees Cook #define CUT_HERE "------------[ cut here ]------------\n" 82a8358d8SKees Cook 909682c1dSPaul Mundt #ifdef CONFIG_GENERIC_BUG 1009682c1dSPaul Mundt #define BUGFLAG_WARNING (1 << 0) 1119d43626SPeter Zijlstra #define BUGFLAG_ONCE (1 << 1) 1219d43626SPeter Zijlstra #define BUGFLAG_DONE (1 << 2) 13f26dee15SPeter Zijlstra #define BUGFLAG_TAINT(taint) ((taint) << 8) 1409682c1dSPaul Mundt #define BUG_GET_TAINT(bug) ((bug)->flags >> 8) 1509682c1dSPaul Mundt #endif 1609682c1dSPaul Mundt 1709682c1dSPaul Mundt #ifndef __ASSEMBLY__ 1809682c1dSPaul Mundt #include <linux/kernel.h> 1909682c1dSPaul Mundt 20ffb61c63SIngo Molnar #ifdef CONFIG_BUG 21ffb61c63SIngo Molnar 22f81f8ad5SNadav Amit #ifdef CONFIG_GENERIC_BUG 23ffb61c63SIngo Molnar struct bug_entry { 24b93a531eSJan Beulich #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS 257664c5a1SJeremy Fitzhardinge unsigned long bug_addr; 26b93a531eSJan Beulich #else 27b93a531eSJan Beulich signed int bug_addr_disp; 28b93a531eSJan Beulich #endif 297664c5a1SJeremy Fitzhardinge #ifdef CONFIG_DEBUG_BUGVERBOSE 30b93a531eSJan Beulich #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS 317664c5a1SJeremy Fitzhardinge const char *file; 32b93a531eSJan Beulich #else 33b93a531eSJan Beulich signed int file_disp; 34b93a531eSJan Beulich #endif 357664c5a1SJeremy Fitzhardinge unsigned short line; 367664c5a1SJeremy Fitzhardinge #endif 377664c5a1SJeremy Fitzhardinge unsigned short flags; 38f81f8ad5SNadav Amit }; 39ffb61c63SIngo Molnar #endif /* CONFIG_GENERIC_BUG */ 407664c5a1SJeremy Fitzhardinge 41af9379c7SDavid Brownell /* 42af9379c7SDavid Brownell * Don't use BUG() or BUG_ON() unless there's really no way out; one 43af9379c7SDavid Brownell * example might be detecting data structure corruption in the middle 44af9379c7SDavid Brownell * of an operation that can't be backed out of. If the (sub)system 45af9379c7SDavid Brownell * can somehow continue operating, perhaps with reduced functionality, 46af9379c7SDavid Brownell * it's probably not BUG-worthy. 47af9379c7SDavid Brownell * 48af9379c7SDavid Brownell * If you're tempted to BUG(), think again: is completely giving up 49af9379c7SDavid Brownell * really the *only* solution? There are usually better options, where 50af9379c7SDavid Brownell * users don't need to reboot ASAP and can mostly shut down cleanly. 51af9379c7SDavid Brownell */ 521da177e4SLinus Torvalds #ifndef HAVE_ARCH_BUG 531da177e4SLinus Torvalds #define BUG() do { \ 54d5c003b4SHarvey Harrison printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ 55173a3efdSArnd Bergmann barrier_before_unreachable(); \ 561da177e4SLinus Torvalds panic("BUG!"); \ 571da177e4SLinus Torvalds } while (0) 581da177e4SLinus Torvalds #endif 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #ifndef HAVE_ARCH_BUG_ON 612a41de48SAlexey Dobriyan #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0) 621da177e4SLinus Torvalds #endif 631da177e4SLinus Torvalds 6419d43626SPeter Zijlstra #ifdef __WARN_FLAGS 6519d43626SPeter Zijlstra #define __WARN_TAINT(taint) __WARN_FLAGS(BUGFLAG_TAINT(taint)) 6619d43626SPeter Zijlstra #define __WARN_ONCE_TAINT(taint) __WARN_FLAGS(BUGFLAG_ONCE|BUGFLAG_TAINT(taint)) 6719d43626SPeter Zijlstra 6819d43626SPeter Zijlstra #define WARN_ON_ONCE(condition) ({ \ 6919d43626SPeter Zijlstra int __ret_warn_on = !!(condition); \ 7019d43626SPeter Zijlstra if (unlikely(__ret_warn_on)) \ 7119d43626SPeter Zijlstra __WARN_ONCE_TAINT(TAINT_WARN); \ 7219d43626SPeter Zijlstra unlikely(__ret_warn_on); \ 7319d43626SPeter Zijlstra }) 7419d43626SPeter Zijlstra #endif 7519d43626SPeter Zijlstra 76af9379c7SDavid Brownell /* 77af9379c7SDavid Brownell * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report 7896c6a32cSDmitry Vyukov * significant kernel issues that need prompt attention if they should ever 7996c6a32cSDmitry Vyukov * appear at runtime. 8096c6a32cSDmitry Vyukov * 8196c6a32cSDmitry Vyukov * Do not use these macros when checking for invalid external inputs 8296c6a32cSDmitry Vyukov * (e.g. invalid system call arguments, or invalid data coming from 8396c6a32cSDmitry Vyukov * network/devices), and on transient conditions like ENOMEM or EAGAIN. 8496c6a32cSDmitry Vyukov * These macros should be used for recoverable kernel issues only. 8596c6a32cSDmitry Vyukov * For invalid external inputs, transient conditions, etc use 8696c6a32cSDmitry Vyukov * pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary. 8796c6a32cSDmitry Vyukov * Do not include "BUG"/"WARNING" in format strings manually to make these 8896c6a32cSDmitry Vyukov * conditions distinguishable from kernel issues. 8996c6a32cSDmitry Vyukov * 9096c6a32cSDmitry Vyukov * Use the versions with printk format strings to provide better diagnostics. 91af9379c7SDavid Brownell */ 92b2be0527SBen Hutchings #ifndef __WARN_TAINT 93b9075fa9SJoe Perches extern __printf(4, 5) 94ee871133SKees Cook void warn_slowpath_fmt(const char *file, const int line, unsigned taint, 95b9075fa9SJoe Perches const char *fmt, ...); 9679b4cc5eSArjan van de Ven #define WANT_WARN_ON_SLOWPATH 97f2f84b05SKees Cook #define __WARN() __WARN_printf(TAINT_WARN, NULL) 9889348fc3SKees Cook #define __WARN_printf(taint, arg...) \ 99ee871133SKees Cook warn_slowpath_fmt(__FILE__, __LINE__, taint, arg) 100a8f18b90SArjan van de Ven #else 101a7bed27aSKees Cook extern __printf(1, 2) void __warn_printk(const char *fmt, ...); 1026b15f678SDrew Davenport #define __WARN() do { \ 1036b15f678SDrew Davenport printk(KERN_WARNING CUT_HERE); __WARN_TAINT(TAINT_WARN); \ 1046b15f678SDrew Davenport } while (0) 10589348fc3SKees Cook #define __WARN_printf(taint, arg...) \ 106a7bed27aSKees Cook do { __warn_printk(arg); __WARN_TAINT(taint); } while (0) 1073a6a62f9SOlof Johansson #endif 1083a6a62f9SOlof Johansson 1092553b67aSJosh Poimboeuf /* used internally by panic.c */ 1102553b67aSJosh Poimboeuf struct warn_args; 1110b396923SIan Abbott struct pt_regs; 1122553b67aSJosh Poimboeuf 1132553b67aSJosh Poimboeuf void __warn(const char *file, int line, void *caller, unsigned taint, 1142553b67aSJosh Poimboeuf struct pt_regs *regs, struct warn_args *args); 1152553b67aSJosh Poimboeuf 1163a6a62f9SOlof Johansson #ifndef WARN_ON 1173a6a62f9SOlof Johansson #define WARN_ON(condition) ({ \ 1183a6a62f9SOlof Johansson int __ret_warn_on = !!(condition); \ 1193a6a62f9SOlof Johansson if (unlikely(__ret_warn_on)) \ 1203a6a62f9SOlof Johansson __WARN(); \ 121684f9783SHerbert Xu unlikely(__ret_warn_on); \ 122684f9783SHerbert Xu }) 1231da177e4SLinus Torvalds #endif 1241da177e4SLinus Torvalds 125a8f18b90SArjan van de Ven #ifndef WARN 126a8f18b90SArjan van de Ven #define WARN(condition, format...) ({ \ 127a8f18b90SArjan van de Ven int __ret_warn_on = !!(condition); \ 128a8f18b90SArjan van de Ven if (unlikely(__ret_warn_on)) \ 12989348fc3SKees Cook __WARN_printf(TAINT_WARN, format); \ 130a8f18b90SArjan van de Ven unlikely(__ret_warn_on); \ 131a8f18b90SArjan van de Ven }) 132a8f18b90SArjan van de Ven #endif 133a8f18b90SArjan van de Ven 134b2be0527SBen Hutchings #define WARN_TAINT(condition, taint, format...) ({ \ 135b2be0527SBen Hutchings int __ret_warn_on = !!(condition); \ 136b2be0527SBen Hutchings if (unlikely(__ret_warn_on)) \ 13789348fc3SKees Cook __WARN_printf(taint, format); \ 138b2be0527SBen Hutchings unlikely(__ret_warn_on); \ 139b2be0527SBen Hutchings }) 140b2be0527SBen Hutchings 14119d43626SPeter Zijlstra #ifndef WARN_ON_ONCE 142684f9783SHerbert Xu #define WARN_ON_ONCE(condition) ({ \ 143b1fca27dSAndi Kleen static bool __section(.data.once) __warned; \ 1448d4fbcfbSLinus Torvalds int __ret_warn_once = !!(condition); \ 14574bb6a09SIngo Molnar \ 146dfbf2897SSteven Rostedt if (unlikely(__ret_warn_once && !__warned)) { \ 14742f247c8SCesar Eduardo Barros __warned = true; \ 148dfbf2897SSteven Rostedt WARN_ON(1); \ 149dfbf2897SSteven Rostedt } \ 150684f9783SHerbert Xu unlikely(__ret_warn_once); \ 15174bb6a09SIngo Molnar }) 15219d43626SPeter Zijlstra #endif 15374bb6a09SIngo Molnar 15445e9c0deSArjan van de Ven #define WARN_ONCE(condition, format...) ({ \ 155b1fca27dSAndi Kleen static bool __section(.data.once) __warned; \ 15645e9c0deSArjan van de Ven int __ret_warn_once = !!(condition); \ 15745e9c0deSArjan van de Ven \ 158dfbf2897SSteven Rostedt if (unlikely(__ret_warn_once && !__warned)) { \ 15942f247c8SCesar Eduardo Barros __warned = true; \ 160dfbf2897SSteven Rostedt WARN(1, format); \ 161dfbf2897SSteven Rostedt } \ 16245e9c0deSArjan van de Ven unlikely(__ret_warn_once); \ 16345e9c0deSArjan van de Ven }) 16445e9c0deSArjan van de Ven 165b2be0527SBen Hutchings #define WARN_TAINT_ONCE(condition, taint, format...) ({ \ 166b1fca27dSAndi Kleen static bool __section(.data.once) __warned; \ 167b2be0527SBen Hutchings int __ret_warn_once = !!(condition); \ 168b2be0527SBen Hutchings \ 169dfbf2897SSteven Rostedt if (unlikely(__ret_warn_once && !__warned)) { \ 170b2be0527SBen Hutchings __warned = true; \ 171dfbf2897SSteven Rostedt WARN_TAINT(1, taint, format); \ 172dfbf2897SSteven Rostedt } \ 173b2be0527SBen Hutchings unlikely(__ret_warn_once); \ 174b2be0527SBen Hutchings }) 175b2be0527SBen Hutchings 176b607e70eSJosh Triplett #else /* !CONFIG_BUG */ 177b607e70eSJosh Triplett #ifndef HAVE_ARCH_BUG 178a4b5d580SJosh Triplett #define BUG() do {} while (1) 179b607e70eSJosh Triplett #endif 180b607e70eSJosh Triplett 181b607e70eSJosh Triplett #ifndef HAVE_ARCH_BUG_ON 1829b87647cSDenis Efremov #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0) 183b607e70eSJosh Triplett #endif 184b607e70eSJosh Triplett 185b607e70eSJosh Triplett #ifndef HAVE_ARCH_WARN_ON 186b607e70eSJosh Triplett #define WARN_ON(condition) ({ \ 187b607e70eSJosh Triplett int __ret_warn_on = !!(condition); \ 188b607e70eSJosh Triplett unlikely(__ret_warn_on); \ 189b607e70eSJosh Triplett }) 190b607e70eSJosh Triplett #endif 191b607e70eSJosh Triplett 192b607e70eSJosh Triplett #ifndef WARN 193b607e70eSJosh Triplett #define WARN(condition, format...) ({ \ 194b607e70eSJosh Triplett int __ret_warn_on = !!(condition); \ 1954e50ebdeSJosh Triplett no_printk(format); \ 196b607e70eSJosh Triplett unlikely(__ret_warn_on); \ 197b607e70eSJosh Triplett }) 198b607e70eSJosh Triplett #endif 199b607e70eSJosh Triplett 200b607e70eSJosh Triplett #define WARN_ON_ONCE(condition) WARN_ON(condition) 201b607e70eSJosh Triplett #define WARN_ONCE(condition, format...) WARN(condition, format) 202b607e70eSJosh Triplett #define WARN_TAINT(condition, taint, format...) WARN(condition, format) 203b607e70eSJosh Triplett #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format) 204b607e70eSJosh Triplett 205b607e70eSJosh Triplett #endif 206b607e70eSJosh Triplett 2072092e6beSSteven Rostedt /* 2082092e6beSSteven Rostedt * WARN_ON_SMP() is for cases that the warning is either 2092092e6beSSteven Rostedt * meaningless for !SMP or may even cause failures. 2102092e6beSSteven Rostedt * It can also be used with values that are only defined 2112092e6beSSteven Rostedt * on SMP: 2122092e6beSSteven Rostedt * 2132092e6beSSteven Rostedt * struct foo { 2142092e6beSSteven Rostedt * [...] 2152092e6beSSteven Rostedt * #ifdef CONFIG_SMP 2162092e6beSSteven Rostedt * int bar; 2172092e6beSSteven Rostedt * #endif 2182092e6beSSteven Rostedt * }; 2192092e6beSSteven Rostedt * 2202092e6beSSteven Rostedt * void func(struct foo *zoot) 2212092e6beSSteven Rostedt * { 2222092e6beSSteven Rostedt * WARN_ON_SMP(!zoot->bar); 2232092e6beSSteven Rostedt * 2242092e6beSSteven Rostedt * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(), 2252092e6beSSteven Rostedt * and should be a nop and return false for uniprocessor. 2262092e6beSSteven Rostedt * 2272092e6beSSteven Rostedt * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set 2282092e6beSSteven Rostedt * and x is true. 2292092e6beSSteven Rostedt */ 2308eb94f80SIngo Molnar #ifdef CONFIG_SMP 2318eb94f80SIngo Molnar # define WARN_ON_SMP(x) WARN_ON(x) 2328eb94f80SIngo Molnar #else 233ccd0d44fSSteven Rostedt /* 234ccd0d44fSSteven Rostedt * Use of ({0;}) because WARN_ON_SMP(x) may be used either as 235ccd0d44fSSteven Rostedt * a stand alone line statement or as a condition in an if () 236ccd0d44fSSteven Rostedt * statement. 237ccd0d44fSSteven Rostedt * A simple "0" would cause gcc to give a "statement has no effect" 238ccd0d44fSSteven Rostedt * warning. 239ccd0d44fSSteven Rostedt */ 2402092e6beSSteven Rostedt # define WARN_ON_SMP(x) ({0;}) 2418eb94f80SIngo Molnar #endif 2428eb94f80SIngo Molnar 2432603efa3SPaul Mundt #endif /* __ASSEMBLY__ */ 2442603efa3SPaul Mundt 2451da177e4SLinus Torvalds #endif 246