kcsan.h 3.6 KB

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