bug.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_BUG_H
  3. #define _LINUX_BUG_H
  4. #include <asm/bug.h>
  5. #include <linux/compiler.h>
  6. #include <linux/build_bug.h>
  7. enum bug_trap_type {
  8. BUG_TRAP_TYPE_NONE = 0,
  9. BUG_TRAP_TYPE_WARN = 1,
  10. BUG_TRAP_TYPE_BUG = 2,
  11. };
  12. struct pt_regs;
  13. #ifdef __CHECKER__
  14. #define MAYBE_BUILD_BUG_ON(cond) (0)
  15. #else /* __CHECKER__ */
  16. #define MAYBE_BUILD_BUG_ON(cond) \
  17. do { \
  18. if (__builtin_constant_p((cond))) \
  19. BUILD_BUG_ON(cond); \
  20. else \
  21. BUG_ON(cond); \
  22. } while (0)
  23. #endif /* __CHECKER__ */
  24. #ifdef CONFIG_GENERIC_BUG
  25. #include <asm-generic/bug.h>
  26. static inline int is_warning_bug(const struct bug_entry *bug)
  27. {
  28. return bug->flags & BUGFLAG_WARNING;
  29. }
  30. struct bug_entry *find_bug(unsigned long bugaddr);
  31. enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
  32. /* These are defined by the architecture */
  33. int is_valid_bugaddr(unsigned long addr);
  34. void generic_bug_clear_once(void);
  35. #else /* !CONFIG_GENERIC_BUG */
  36. static inline void *find_bug(unsigned long bugaddr)
  37. {
  38. return NULL;
  39. }
  40. static inline enum bug_trap_type report_bug(unsigned long bug_addr,
  41. struct pt_regs *regs)
  42. {
  43. return BUG_TRAP_TYPE_BUG;
  44. }
  45. static inline void generic_bug_clear_once(void) {}
  46. #endif /* CONFIG_GENERIC_BUG */
  47. /*
  48. * Since detected data corruption should stop operation on the affected
  49. * structures. Return value must be checked and sanely acted on by caller.
  50. */
  51. static inline __must_check bool check_data_corruption(bool v) { return v; }
  52. #define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
  53. check_data_corruption(({ \
  54. bool corruption = unlikely(condition); \
  55. if (corruption) { \
  56. if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
  57. pr_err(fmt, ##__VA_ARGS__); \
  58. BUG(); \
  59. } else \
  60. WARN(1, fmt, ##__VA_ARGS__); \
  61. } \
  62. corruption; \
  63. }))
  64. #endif /* _LINUX_BUG_H */