bug.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. void bug_get_file_line(struct bug_entry *bug, const char **file,
  31. unsigned int *line);
  32. struct bug_entry *find_bug(unsigned long bugaddr);
  33. enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
  34. /* These are defined by the architecture */
  35. int is_valid_bugaddr(unsigned long addr);
  36. void generic_bug_clear_once(void);
  37. #else /* !CONFIG_GENERIC_BUG */
  38. static inline void *find_bug(unsigned long bugaddr)
  39. {
  40. return NULL;
  41. }
  42. static inline enum bug_trap_type report_bug(unsigned long bug_addr,
  43. struct pt_regs *regs)
  44. {
  45. return BUG_TRAP_TYPE_BUG;
  46. }
  47. struct bug_entry;
  48. static inline void bug_get_file_line(struct bug_entry *bug, const char **file,
  49. unsigned int *line)
  50. {
  51. *file = NULL;
  52. *line = 0;
  53. }
  54. static inline void generic_bug_clear_once(void) {}
  55. #endif /* CONFIG_GENERIC_BUG */
  56. /*
  57. * Since detected data corruption should stop operation on the affected
  58. * structures. Return value must be checked and sanely acted on by caller.
  59. */
  60. static inline __must_check bool check_data_corruption(bool v) { return v; }
  61. #define CHECK_DATA_CORRUPTION(condition, fmt, ...) \
  62. check_data_corruption(({ \
  63. bool corruption = unlikely(condition); \
  64. if (corruption) { \
  65. if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) { \
  66. pr_err(fmt, ##__VA_ARGS__); \
  67. BUG(); \
  68. } else \
  69. WARN(1, fmt, ##__VA_ARGS__); \
  70. } \
  71. corruption; \
  72. }))
  73. #endif /* _LINUX_BUG_H */