Kconfig.kcsan 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. config HAVE_ARCH_KCSAN
  3. bool
  4. config HAVE_KCSAN_COMPILER
  5. def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-distinguish-volatile=1)) || \
  6. (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-distinguish-volatile=1))
  7. help
  8. For the list of compilers that support KCSAN, please see
  9. <file:Documentation/dev-tools/kcsan.rst>.
  10. config KCSAN_KCOV_BROKEN
  11. def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
  12. depends on CC_IS_CLANG
  13. depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=thread -fsanitize-coverage=trace-pc)
  14. help
  15. Some versions of clang support either KCSAN and KCOV but not the
  16. combination of the two.
  17. See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
  18. in newer releases.
  19. menuconfig KCSAN
  20. bool "KCSAN: dynamic data race detector"
  21. depends on HAVE_ARCH_KCSAN && HAVE_KCSAN_COMPILER
  22. depends on DEBUG_KERNEL && !KASAN
  23. depends on !KCSAN_KCOV_BROKEN
  24. select STACKTRACE
  25. help
  26. The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
  27. data-race detector that relies on compile-time instrumentation.
  28. KCSAN uses a watchpoint-based sampling approach to detect races.
  29. While KCSAN's primary purpose is to detect data races, it
  30. also provides assertions to check data access constraints.
  31. These assertions can expose bugs that do not manifest as
  32. data races.
  33. See <file:Documentation/dev-tools/kcsan.rst> for more details.
  34. if KCSAN
  35. # Compiler capabilities that should not fail the test if they are unavailable.
  36. config CC_HAS_TSAN_COMPOUND_READ_BEFORE_WRITE
  37. def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-compound-read-before-write=1)) || \
  38. (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-compound-read-before-write=1))
  39. config KCSAN_VERBOSE
  40. bool "Show verbose reports with more information about system state"
  41. depends on PROVE_LOCKING
  42. help
  43. If enabled, reports show more information about the system state that
  44. may help better analyze and debug races. This includes held locks and
  45. IRQ trace events.
  46. While this option should generally be benign, we call into more
  47. external functions on report generation; if a race report is
  48. generated from any one of them, system stability may suffer due to
  49. deadlocks or recursion. If in doubt, say N.
  50. config KCSAN_DEBUG
  51. bool "Debugging of KCSAN internals"
  52. config KCSAN_SELFTEST
  53. bool "Perform short selftests on boot"
  54. default y
  55. help
  56. Run KCSAN selftests on boot. On test failure, causes the kernel to
  57. panic. Recommended to be enabled, ensuring critical functionality
  58. works as intended.
  59. config KCSAN_TEST
  60. tristate "KCSAN test for integrated runtime behaviour"
  61. depends on TRACEPOINTS && KUNIT
  62. select TORTURE_TEST
  63. help
  64. KCSAN test focusing on behaviour of the integrated runtime. Tests
  65. various race scenarios, and verifies the reports generated to
  66. console. Makes use of KUnit for test organization, and the Torture
  67. framework for test thread control.
  68. Each test case may run at least up to KCSAN_REPORT_ONCE_IN_MS
  69. milliseconds. Test run duration may be optimized by building the
  70. kernel and KCSAN test with KCSAN_REPORT_ONCE_IN_MS set to a lower
  71. than default value.
  72. Say Y here if you want the test to be built into the kernel and run
  73. during boot; say M if you want the test to build as a module; say N
  74. if you are unsure.
  75. config KCSAN_EARLY_ENABLE
  76. bool "Early enable during boot"
  77. default y
  78. help
  79. If KCSAN should be enabled globally as soon as possible. KCSAN can
  80. later be enabled/disabled via debugfs.
  81. config KCSAN_NUM_WATCHPOINTS
  82. int "Number of available watchpoints"
  83. default 64
  84. help
  85. Total number of available watchpoints. An address range maps into a
  86. specific watchpoint slot as specified in kernel/kcsan/encoding.h.
  87. Although larger number of watchpoints may not be usable due to
  88. limited number of CPUs, a larger value helps to improve performance
  89. due to reducing cache-line contention. The chosen default is a
  90. conservative value; we should almost never observe "no_capacity"
  91. events (see /sys/kernel/debug/kcsan).
  92. config KCSAN_UDELAY_TASK
  93. int "Delay in microseconds (for tasks)"
  94. default 80
  95. help
  96. For tasks, the microsecond delay after setting up a watchpoint.
  97. config KCSAN_UDELAY_INTERRUPT
  98. int "Delay in microseconds (for interrupts)"
  99. default 20
  100. help
  101. For interrupts, the microsecond delay after setting up a watchpoint.
  102. Interrupts have tighter latency requirements, and their delay should
  103. be lower than for tasks.
  104. config KCSAN_DELAY_RANDOMIZE
  105. bool "Randomize above delays"
  106. default y
  107. help
  108. If delays should be randomized, where the maximum is KCSAN_UDELAY_*.
  109. If false, the chosen delays are always the KCSAN_UDELAY_* values
  110. as defined above.
  111. config KCSAN_SKIP_WATCH
  112. int "Skip instructions before setting up watchpoint"
  113. default 4000
  114. help
  115. The number of per-CPU memory operations to skip, before another
  116. watchpoint is set up, i.e. one in KCSAN_WATCH_SKIP per-CPU
  117. memory operations are used to set up a watchpoint. A smaller value
  118. results in more aggressive race detection, whereas a larger value
  119. improves system performance at the cost of missing some races.
  120. config KCSAN_SKIP_WATCH_RANDOMIZE
  121. bool "Randomize watchpoint instruction skip count"
  122. default y
  123. help
  124. If instruction skip count should be randomized, where the maximum is
  125. KCSAN_WATCH_SKIP. If false, the chosen value is always
  126. KCSAN_WATCH_SKIP.
  127. config KCSAN_INTERRUPT_WATCHER
  128. bool "Interruptible watchers"
  129. help
  130. If enabled, a task that set up a watchpoint may be interrupted while
  131. delayed. This option will allow KCSAN to detect races between
  132. interrupted tasks and other threads of execution on the same CPU.
  133. Currently disabled by default, because not all safe per-CPU access
  134. primitives and patterns may be accounted for, and therefore could
  135. result in false positives.
  136. config KCSAN_REPORT_ONCE_IN_MS
  137. int "Duration in milliseconds, in which any given race is only reported once"
  138. default 3000
  139. help
  140. Any given race is only reported once in the defined time window.
  141. Different races may still generate reports within a duration that is
  142. smaller than the duration defined here. This allows rate limiting
  143. reporting to avoid flooding the console with reports. Setting this
  144. to 0 disables rate limiting.
  145. # The main purpose of the below options is to control reported data races (e.g.
  146. # in fuzzer configs), and are not expected to be switched frequently by other
  147. # users. We could turn some of them into boot parameters, but given they should
  148. # not be switched normally, let's keep them here to simplify configuration.
  149. #
  150. # The defaults below are chosen to be very conservative, and may miss certain
  151. # bugs.
  152. config KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
  153. bool "Report races of unknown origin"
  154. default y
  155. help
  156. If KCSAN should report races where only one access is known, and the
  157. conflicting access is of unknown origin. This type of race is
  158. reported if it was only possible to infer a race due to a data value
  159. change while an access is being delayed on a watchpoint.
  160. config KCSAN_REPORT_VALUE_CHANGE_ONLY
  161. bool "Only report races where watcher observed a data value change"
  162. default y
  163. help
  164. If enabled and a conflicting write is observed via a watchpoint, but
  165. the data value of the memory location was observed to remain
  166. unchanged, do not report the data race.
  167. config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
  168. bool "Assume that plain aligned writes up to word size are atomic"
  169. default y
  170. help
  171. Assume that plain aligned writes up to word size are atomic by
  172. default, and also not subject to other unsafe compiler optimizations
  173. resulting in data races. This will cause KCSAN to not report data
  174. races due to conflicts where the only plain accesses are aligned
  175. writes up to word size: conflicts between marked reads and plain
  176. aligned writes up to word size will not be reported as data races;
  177. notice that data races between two conflicting plain aligned writes
  178. will also not be reported.
  179. config KCSAN_IGNORE_ATOMICS
  180. bool "Do not instrument marked atomic accesses"
  181. help
  182. Never instrument marked atomic accesses. This option can be used for
  183. additional filtering. Conflicting marked atomic reads and plain
  184. writes will never be reported as a data race, however, will cause
  185. plain reads and marked writes to result in "unknown origin" reports.
  186. If combined with CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n, data
  187. races where at least one access is marked atomic will never be
  188. reported.
  189. Similar to KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, but including unaligned
  190. accesses, conflicting marked atomic reads and plain writes will not
  191. be reported as data races; however, unlike that option, data races
  192. due to two conflicting plain writes will be reported (aligned and
  193. unaligned, if CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n).
  194. endif # KCSAN