kcsan.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * The Kernel Concurrency Sanitizer (KCSAN) infrastructure. For more info please
  4. * see Documentation/dev-tools/kcsan.rst.
  5. */
  6. #ifndef _KERNEL_KCSAN_KCSAN_H
  7. #define _KERNEL_KCSAN_KCSAN_H
  8. #include <linux/atomic.h>
  9. #include <linux/kcsan.h>
  10. #include <linux/sched.h>
  11. /* The number of adjacent watchpoints to check. */
  12. #define KCSAN_CHECK_ADJACENT 1
  13. #define NUM_SLOTS (1 + 2*KCSAN_CHECK_ADJACENT)
  14. extern unsigned int kcsan_udelay_task;
  15. extern unsigned int kcsan_udelay_interrupt;
  16. /*
  17. * Globally enable and disable KCSAN.
  18. */
  19. extern bool kcsan_enabled;
  20. /*
  21. * Save/restore IRQ flags state trace dirtied by KCSAN.
  22. */
  23. void kcsan_save_irqtrace(struct task_struct *task);
  24. void kcsan_restore_irqtrace(struct task_struct *task);
  25. /*
  26. * Statistics counters displayed via debugfs; should only be modified in
  27. * slow-paths.
  28. */
  29. enum kcsan_counter_id {
  30. /*
  31. * Number of watchpoints currently in use.
  32. */
  33. KCSAN_COUNTER_USED_WATCHPOINTS,
  34. /*
  35. * Total number of watchpoints set up.
  36. */
  37. KCSAN_COUNTER_SETUP_WATCHPOINTS,
  38. /*
  39. * Total number of data races.
  40. */
  41. KCSAN_COUNTER_DATA_RACES,
  42. /*
  43. * Total number of ASSERT failures due to races. If the observed race is
  44. * due to two conflicting ASSERT type accesses, then both will be
  45. * counted.
  46. */
  47. KCSAN_COUNTER_ASSERT_FAILURES,
  48. /*
  49. * Number of times no watchpoints were available.
  50. */
  51. KCSAN_COUNTER_NO_CAPACITY,
  52. /*
  53. * A thread checking a watchpoint raced with another checking thread;
  54. * only one will be reported.
  55. */
  56. KCSAN_COUNTER_REPORT_RACES,
  57. /*
  58. * Observed data value change, but writer thread unknown.
  59. */
  60. KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN,
  61. /*
  62. * The access cannot be encoded to a valid watchpoint.
  63. */
  64. KCSAN_COUNTER_UNENCODABLE_ACCESSES,
  65. /*
  66. * Watchpoint encoding caused a watchpoint to fire on mismatching
  67. * accesses.
  68. */
  69. KCSAN_COUNTER_ENCODING_FALSE_POSITIVES,
  70. KCSAN_COUNTER_COUNT, /* number of counters */
  71. };
  72. extern atomic_long_t kcsan_counters[KCSAN_COUNTER_COUNT];
  73. /*
  74. * Returns true if data races in the function symbol that maps to func_addr
  75. * (offsets are ignored) should *not* be reported.
  76. */
  77. extern bool kcsan_skip_report_debugfs(unsigned long func_addr);
  78. /*
  79. * Value-change states.
  80. */
  81. enum kcsan_value_change {
  82. /*
  83. * Did not observe a value-change, however, it is valid to report the
  84. * race, depending on preferences.
  85. */
  86. KCSAN_VALUE_CHANGE_MAYBE,
  87. /*
  88. * Did not observe a value-change, and it is invalid to report the race.
  89. */
  90. KCSAN_VALUE_CHANGE_FALSE,
  91. /*
  92. * The value was observed to change, and the race should be reported.
  93. */
  94. KCSAN_VALUE_CHANGE_TRUE,
  95. };
  96. enum kcsan_report_type {
  97. /*
  98. * The thread that set up the watchpoint and briefly stalled was
  99. * signalled that another thread triggered the watchpoint.
  100. */
  101. KCSAN_REPORT_RACE_SIGNAL,
  102. /*
  103. * A thread found and consumed a matching watchpoint.
  104. */
  105. KCSAN_REPORT_CONSUMED_WATCHPOINT,
  106. /*
  107. * No other thread was observed to race with the access, but the data
  108. * value before and after the stall differs.
  109. */
  110. KCSAN_REPORT_RACE_UNKNOWN_ORIGIN,
  111. };
  112. /*
  113. * Print a race report from thread that encountered the race.
  114. */
  115. extern void kcsan_report(const volatile void *ptr, size_t size, int access_type,
  116. enum kcsan_value_change value_change,
  117. enum kcsan_report_type type, int watchpoint_idx);
  118. #endif /* _KERNEL_KCSAN_KCSAN_H */