bug.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 enum bug_trap_type report_bug(unsigned long bug_addr,
  37. struct pt_regs *regs)
  38. {
  39. return BUG_TRAP_TYPE_BUG;
  40. }
  41. static inline void generic_bug_clear_once(void) {}
  42. #endif /* CONFIG_GENERIC_BUG */
  43. /*
  44. * Since detected data corruption should stop operation on the affected
  45. * structures. Return value must be checked and sanely acted on by caller.
  46. */
  47. static inline __must_check bool check_data_corruption(bool v) { return v; }
  48. #define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
  49. check_data_corruption(({ \
  50. bool corruption = unlikely(condition); \
  51. if (corruption) { \
  52. if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
  53. pr_err(fmt, ##__VA_ARGS__); \
  54. BUG(); \
  55. } else \
  56. WARN(1, fmt, ##__VA_ARGS__); \
  57. } \
  58. corruption; \
  59. }))
  60. #endif /* _LINUX_BUG_H */