Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_GENERIC_BUG_H
0003 #define _ASM_GENERIC_BUG_H
0004 
0005 #include <linux/compiler.h>
0006 #include <linux/instrumentation.h>
0007 #include <linux/once_lite.h>
0008 
0009 #define CUT_HERE        "------------[ cut here ]------------\n"
0010 
0011 #ifdef CONFIG_GENERIC_BUG
0012 #define BUGFLAG_WARNING     (1 << 0)
0013 #define BUGFLAG_ONCE        (1 << 1)
0014 #define BUGFLAG_DONE        (1 << 2)
0015 #define BUGFLAG_NO_CUT_HERE (1 << 3)    /* CUT_HERE already sent */
0016 #define BUGFLAG_TAINT(taint)    ((taint) << 8)
0017 #define BUG_GET_TAINT(bug)  ((bug)->flags >> 8)
0018 #endif
0019 
0020 #ifndef __ASSEMBLY__
0021 #include <linux/panic.h>
0022 #include <linux/printk.h>
0023 
0024 struct warn_args;
0025 struct pt_regs;
0026 
0027 void __warn(const char *file, int line, void *caller, unsigned taint,
0028         struct pt_regs *regs, struct warn_args *args);
0029 
0030 #ifdef CONFIG_BUG
0031 
0032 #ifdef CONFIG_GENERIC_BUG
0033 struct bug_entry {
0034 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
0035     unsigned long   bug_addr;
0036 #else
0037     signed int  bug_addr_disp;
0038 #endif
0039 #ifdef CONFIG_DEBUG_BUGVERBOSE
0040 #ifndef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
0041     const char  *file;
0042 #else
0043     signed int  file_disp;
0044 #endif
0045     unsigned short  line;
0046 #endif
0047     unsigned short  flags;
0048 };
0049 #endif  /* CONFIG_GENERIC_BUG */
0050 
0051 /*
0052  * Don't use BUG() or BUG_ON() unless there's really no way out; one
0053  * example might be detecting data structure corruption in the middle
0054  * of an operation that can't be backed out of.  If the (sub)system
0055  * can somehow continue operating, perhaps with reduced functionality,
0056  * it's probably not BUG-worthy.
0057  *
0058  * If you're tempted to BUG(), think again:  is completely giving up
0059  * really the *only* solution?  There are usually better options, where
0060  * users don't need to reboot ASAP and can mostly shut down cleanly.
0061  */
0062 #ifndef HAVE_ARCH_BUG
0063 #define BUG() do { \
0064     printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
0065     barrier_before_unreachable(); \
0066     panic("BUG!"); \
0067 } while (0)
0068 #endif
0069 
0070 #ifndef HAVE_ARCH_BUG_ON
0071 #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
0072 #endif
0073 
0074 /*
0075  * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
0076  * significant kernel issues that need prompt attention if they should ever
0077  * appear at runtime.
0078  *
0079  * Do not use these macros when checking for invalid external inputs
0080  * (e.g. invalid system call arguments, or invalid data coming from
0081  * network/devices), and on transient conditions like ENOMEM or EAGAIN.
0082  * These macros should be used for recoverable kernel issues only.
0083  * For invalid external inputs, transient conditions, etc use
0084  * pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
0085  * Do not include "BUG"/"WARNING" in format strings manually to make these
0086  * conditions distinguishable from kernel issues.
0087  *
0088  * Use the versions with printk format strings to provide better diagnostics.
0089  */
0090 #ifndef __WARN_FLAGS
0091 extern __printf(4, 5)
0092 void warn_slowpath_fmt(const char *file, const int line, unsigned taint,
0093                const char *fmt, ...);
0094 #define __WARN()        __WARN_printf(TAINT_WARN, NULL)
0095 #define __WARN_printf(taint, arg...) do {               \
0096         instrumentation_begin();                \
0097         warn_slowpath_fmt(__FILE__, __LINE__, taint, arg);  \
0098         instrumentation_end();                  \
0099     } while (0)
0100 #else
0101 extern __printf(1, 2) void __warn_printk(const char *fmt, ...);
0102 #define __WARN()        __WARN_FLAGS(BUGFLAG_TAINT(TAINT_WARN))
0103 #define __WARN_printf(taint, arg...) do {               \
0104         instrumentation_begin();                \
0105         __warn_printk(arg);                 \
0106         __WARN_FLAGS(BUGFLAG_NO_CUT_HERE | BUGFLAG_TAINT(taint));\
0107         instrumentation_end();                  \
0108     } while (0)
0109 #define WARN_ON_ONCE(condition) ({              \
0110     int __ret_warn_on = !!(condition);          \
0111     if (unlikely(__ret_warn_on))                \
0112         __WARN_FLAGS(BUGFLAG_ONCE |         \
0113                  BUGFLAG_TAINT(TAINT_WARN));    \
0114     unlikely(__ret_warn_on);                \
0115 })
0116 #endif
0117 
0118 /* used internally by panic.c */
0119 
0120 #ifndef WARN_ON
0121 #define WARN_ON(condition) ({                       \
0122     int __ret_warn_on = !!(condition);              \
0123     if (unlikely(__ret_warn_on))                    \
0124         __WARN();                       \
0125     unlikely(__ret_warn_on);                    \
0126 })
0127 #endif
0128 
0129 #ifndef WARN
0130 #define WARN(condition, format...) ({                   \
0131     int __ret_warn_on = !!(condition);              \
0132     if (unlikely(__ret_warn_on))                    \
0133         __WARN_printf(TAINT_WARN, format);          \
0134     unlikely(__ret_warn_on);                    \
0135 })
0136 #endif
0137 
0138 #define WARN_TAINT(condition, taint, format...) ({          \
0139     int __ret_warn_on = !!(condition);              \
0140     if (unlikely(__ret_warn_on))                    \
0141         __WARN_printf(taint, format);               \
0142     unlikely(__ret_warn_on);                    \
0143 })
0144 
0145 #ifndef WARN_ON_ONCE
0146 #define WARN_ON_ONCE(condition)                 \
0147     DO_ONCE_LITE_IF(condition, WARN_ON, 1)
0148 #endif
0149 
0150 #define WARN_ONCE(condition, format...)             \
0151     DO_ONCE_LITE_IF(condition, WARN, 1, format)
0152 
0153 #define WARN_TAINT_ONCE(condition, taint, format...)        \
0154     DO_ONCE_LITE_IF(condition, WARN_TAINT, 1, taint, format)
0155 
0156 #else /* !CONFIG_BUG */
0157 #ifndef HAVE_ARCH_BUG
0158 #define BUG() do {} while (1)
0159 #endif
0160 
0161 #ifndef HAVE_ARCH_BUG_ON
0162 #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
0163 #endif
0164 
0165 #ifndef HAVE_ARCH_WARN_ON
0166 #define WARN_ON(condition) ({                       \
0167     int __ret_warn_on = !!(condition);              \
0168     unlikely(__ret_warn_on);                    \
0169 })
0170 #endif
0171 
0172 #ifndef WARN
0173 #define WARN(condition, format...) ({                   \
0174     int __ret_warn_on = !!(condition);              \
0175     no_printk(format);                      \
0176     unlikely(__ret_warn_on);                    \
0177 })
0178 #endif
0179 
0180 #define WARN_ON_ONCE(condition) WARN_ON(condition)
0181 #define WARN_ONCE(condition, format...) WARN(condition, format)
0182 #define WARN_TAINT(condition, taint, format...) WARN(condition, format)
0183 #define WARN_TAINT_ONCE(condition, taint, format...) WARN(condition, format)
0184 
0185 #endif
0186 
0187 /*
0188  * WARN_ON_SMP() is for cases that the warning is either
0189  * meaningless for !SMP or may even cause failures.
0190  * It can also be used with values that are only defined
0191  * on SMP:
0192  *
0193  * struct foo {
0194  *  [...]
0195  * #ifdef CONFIG_SMP
0196  *  int bar;
0197  * #endif
0198  * };
0199  *
0200  * void func(struct foo *zoot)
0201  * {
0202  *  WARN_ON_SMP(!zoot->bar);
0203  *
0204  * For CONFIG_SMP, WARN_ON_SMP() should act the same as WARN_ON(),
0205  * and should be a nop and return false for uniprocessor.
0206  *
0207  * if (WARN_ON_SMP(x)) returns true only when CONFIG_SMP is set
0208  * and x is true.
0209  */
0210 #ifdef CONFIG_SMP
0211 # define WARN_ON_SMP(x)         WARN_ON(x)
0212 #else
0213 /*
0214  * Use of ({0;}) because WARN_ON_SMP(x) may be used either as
0215  * a stand alone line statement or as a condition in an if ()
0216  * statement.
0217  * A simple "0" would cause gcc to give a "statement has no effect"
0218  * warning.
0219  */
0220 # define WARN_ON_SMP(x)         ({0;})
0221 #endif
0222 
0223 /*
0224  * WARN_ON_FUNCTION_MISMATCH() warns if a value doesn't match a
0225  * function address, and can be useful for catching issues with
0226  * callback functions, for example.
0227  *
0228  * With CONFIG_CFI_CLANG, the warning is disabled because the
0229  * compiler replaces function addresses taken in C code with
0230  * local jump table addresses, which breaks cross-module function
0231  * address equality.
0232  */
0233 #if defined(CONFIG_CFI_CLANG) && defined(CONFIG_MODULES)
0234 # define WARN_ON_FUNCTION_MISMATCH(x, fn) ({ 0; })
0235 #else
0236 # define WARN_ON_FUNCTION_MISMATCH(x, fn) WARN_ON_ONCE((x) != (fn))
0237 #endif
0238 
0239 #endif /* __ASSEMBLY__ */
0240 
0241 #endif