Kconfig.kcsan 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. menuconfig KCSAN
  11. bool "KCSAN: dynamic data race detector"
  12. depends on HAVE_ARCH_KCSAN && HAVE_KCSAN_COMPILER
  13. depends on DEBUG_KERNEL && !KASAN
  14. select STACKTRACE
  15. help
  16. The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
  17. data-race detector that relies on compile-time instrumentation.
  18. KCSAN uses a watchpoint-based sampling approach to detect races.
  19. While KCSAN's primary purpose is to detect data races, it
  20. also provides assertions to check data access constraints.
  21. These assertions can expose bugs that do not manifest as
  22. data races.
  23. See <file:Documentation/dev-tools/kcsan.rst> for more details.
  24. if KCSAN
  25. config CC_HAS_TSAN_COMPOUND_READ_BEFORE_WRITE
  26. def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-compound-read-before-write=1)) || \
  27. (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-compound-read-before-write=1))
  28. help
  29. The compiler instruments plain compound read-write operations
  30. differently (++, --, +=, -=, |=, &=, etc.), which allows KCSAN to
  31. distinguish them from other plain accesses. This is currently
  32. supported by Clang 12 or later.
  33. config KCSAN_VERBOSE
  34. bool "Show verbose reports with more information about system state"
  35. depends on PROVE_LOCKING
  36. help
  37. If enabled, reports show more information about the system state that
  38. may help better analyze and debug races. This includes held locks and
  39. IRQ trace events.
  40. While this option should generally be benign, we call into more
  41. external functions on report generation; if a race report is
  42. generated from any one of them, system stability may suffer due to
  43. deadlocks or recursion. If in doubt, say N.
  44. config KCSAN_SELFTEST
  45. bool "Perform short selftests on boot"
  46. default y
  47. help
  48. Run KCSAN selftests on boot. On test failure, causes the kernel to
  49. panic. Recommended to be enabled, ensuring critical functionality
  50. works as intended.
  51. config KCSAN_KUNIT_TEST
  52. tristate "KCSAN test for integrated runtime behaviour" if !KUNIT_ALL_TESTS
  53. default KUNIT_ALL_TESTS
  54. depends on TRACEPOINTS && KUNIT
  55. select TORTURE_TEST
  56. help
  57. KCSAN test focusing on behaviour of the integrated runtime. Tests
  58. various race scenarios, and verifies the reports generated to
  59. console. Makes use of KUnit for test organization, and the Torture
  60. framework for test thread control.
  61. Each test case may run at least up to KCSAN_REPORT_ONCE_IN_MS
  62. milliseconds. Test run duration may be optimized by building the
  63. kernel and KCSAN test with KCSAN_REPORT_ONCE_IN_MS set to a lower
  64. than default value.
  65. Say Y here if you want the test to be built into the kernel and run
  66. during boot; say M if you want the test to build as a module; say N
  67. if you are unsure.
  68. config KCSAN_EARLY_ENABLE
  69. bool "Early enable during boot"
  70. default y
  71. help
  72. If KCSAN should be enabled globally as soon as possible. KCSAN can
  73. later be enabled/disabled via debugfs.
  74. config KCSAN_NUM_WATCHPOINTS
  75. int "Number of available watchpoints"
  76. default 64
  77. help
  78. Total number of available watchpoints. An address range maps into a
  79. specific watchpoint slot as specified in kernel/kcsan/encoding.h.
  80. Although larger number of watchpoints may not be usable due to
  81. limited number of CPUs, a larger value helps to improve performance
  82. due to reducing cache-line contention. The chosen default is a
  83. conservative value; we should almost never observe "no_capacity"
  84. events (see /sys/kernel/debug/kcsan).
  85. config KCSAN_UDELAY_TASK
  86. int "Delay in microseconds (for tasks)"
  87. default 80
  88. help
  89. For tasks, the microsecond delay after setting up a watchpoint.
  90. config KCSAN_UDELAY_INTERRUPT
  91. int "Delay in microseconds (for interrupts)"
  92. default 20
  93. help
  94. For interrupts, the microsecond delay after setting up a watchpoint.
  95. Interrupts have tighter latency requirements, and their delay should
  96. be lower than for tasks.
  97. config KCSAN_DELAY_RANDOMIZE
  98. bool "Randomize above delays"
  99. default y
  100. help
  101. If delays should be randomized, where the maximum is KCSAN_UDELAY_*.
  102. If false, the chosen delays are always the KCSAN_UDELAY_* values
  103. as defined above.
  104. config KCSAN_SKIP_WATCH
  105. int "Skip instructions before setting up watchpoint"
  106. default 4000
  107. help
  108. The number of per-CPU memory operations to skip, before another
  109. watchpoint is set up, i.e. one in KCSAN_WATCH_SKIP per-CPU
  110. memory operations are used to set up a watchpoint. A smaller value
  111. results in more aggressive race detection, whereas a larger value
  112. improves system performance at the cost of missing some races.
  113. config KCSAN_SKIP_WATCH_RANDOMIZE
  114. bool "Randomize watchpoint instruction skip count"
  115. default y
  116. help
  117. If instruction skip count should be randomized, where the maximum is
  118. KCSAN_WATCH_SKIP. If false, the chosen value is always
  119. KCSAN_WATCH_SKIP.
  120. config KCSAN_INTERRUPT_WATCHER
  121. bool "Interruptible watchers" if !KCSAN_STRICT
  122. default KCSAN_STRICT
  123. help
  124. If enabled, a task that set up a watchpoint may be interrupted while
  125. delayed. This option will allow KCSAN to detect races between
  126. interrupted tasks and other threads of execution on the same CPU.
  127. Currently disabled by default, because not all safe per-CPU access
  128. primitives and patterns may be accounted for, and therefore could
  129. result in false positives.
  130. config KCSAN_REPORT_ONCE_IN_MS
  131. int "Duration in milliseconds, in which any given race is only reported once"
  132. default 3000
  133. help
  134. Any given race is only reported once in the defined time window.
  135. Different races may still generate reports within a duration that is
  136. smaller than the duration defined here. This allows rate limiting
  137. reporting to avoid flooding the console with reports. Setting this
  138. to 0 disables rate limiting.
  139. # The main purpose of the below options is to control reported data races, and
  140. # are not expected to be switched frequently by non-testers or at runtime.
  141. # The defaults are chosen to be conservative, and can miss certain bugs.
  142. config KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
  143. bool "Report races of unknown origin"
  144. default y
  145. help
  146. If KCSAN should report races where only one access is known, and the
  147. conflicting access is of unknown origin. This type of race is
  148. reported if it was only possible to infer a race due to a data value
  149. change while an access is being delayed on a watchpoint.
  150. config KCSAN_STRICT
  151. bool "Strict data-race checking"
  152. help
  153. KCSAN will report data races with the strictest possible rules, which
  154. closely aligns with the rules defined by the Linux-kernel memory
  155. consistency model (LKMM).
  156. config KCSAN_WEAK_MEMORY
  157. bool "Enable weak memory modeling to detect missing memory barriers"
  158. default y
  159. depends on KCSAN_STRICT
  160. # We can either let objtool nop __tsan_func_{entry,exit}() and builtin
  161. # atomics instrumentation in .noinstr.text, or use a compiler that can
  162. # implement __no_kcsan to really remove all instrumentation.
  163. depends on !ARCH_WANTS_NO_INSTR || HAVE_NOINSTR_HACK || \
  164. CC_IS_GCC || CLANG_VERSION >= 140000
  165. select OBJTOOL if HAVE_NOINSTR_HACK
  166. help
  167. Enable support for modeling a subset of weak memory, which allows
  168. detecting a subset of data races due to missing memory barriers.
  169. Depends on KCSAN_STRICT, because the options strenghtening certain
  170. plain accesses by default (depending on !KCSAN_STRICT) reduce the
  171. ability to detect any data races invoving reordered accesses, in
  172. particular reordered writes.
  173. Weak memory modeling relies on additional instrumentation and may
  174. affect performance.
  175. config KCSAN_REPORT_VALUE_CHANGE_ONLY
  176. bool "Only report races where watcher observed a data value change"
  177. default y
  178. depends on !KCSAN_STRICT
  179. help
  180. If enabled and a conflicting write is observed via a watchpoint, but
  181. the data value of the memory location was observed to remain
  182. unchanged, do not report the data race.
  183. config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
  184. bool "Assume that plain aligned writes up to word size are atomic"
  185. default y
  186. depends on !KCSAN_STRICT
  187. help
  188. Assume that plain aligned writes up to word size are atomic by
  189. default, and also not subject to other unsafe compiler optimizations
  190. resulting in data races. This will cause KCSAN to not report data
  191. races due to conflicts where the only plain accesses are aligned
  192. writes up to word size: conflicts between marked reads and plain
  193. aligned writes up to word size will not be reported as data races;
  194. notice that data races between two conflicting plain aligned writes
  195. will also not be reported.
  196. config KCSAN_IGNORE_ATOMICS
  197. bool "Do not instrument marked atomic accesses"
  198. depends on !KCSAN_STRICT
  199. help
  200. Never instrument marked atomic accesses. This option can be used for
  201. additional filtering. Conflicting marked atomic reads and plain
  202. writes will never be reported as a data race, however, will cause
  203. plain reads and marked writes to result in "unknown origin" reports.
  204. If combined with CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n, data
  205. races where at least one access is marked atomic will never be
  206. reported.
  207. Similar to KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, but including unaligned
  208. accesses, conflicting marked atomic reads and plain writes will not
  209. be reported as data races; however, unlike that option, data races
  210. due to two conflicting plain writes will be reported (aligned and
  211. unaligned, if CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n).
  212. config KCSAN_PERMISSIVE
  213. bool "Enable all additional permissive rules"
  214. depends on KCSAN_REPORT_VALUE_CHANGE_ONLY
  215. help
  216. Enable additional permissive rules to ignore certain classes of data
  217. races (also see kernel/kcsan/permissive.h). None of the permissive
  218. rules imply that such data races are generally safe, but can be used
  219. to further reduce reported data races due to data-racy patterns
  220. common across the kernel.
  221. endif # KCSAN