18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ciconfig HAVE_ARCH_KCSAN
48c2ecf20Sopenharmony_ci	bool
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ciconfig HAVE_KCSAN_COMPILER
78c2ecf20Sopenharmony_ci	def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-distinguish-volatile=1)) || \
88c2ecf20Sopenharmony_ci		 (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-distinguish-volatile=1))
98c2ecf20Sopenharmony_ci	help
108c2ecf20Sopenharmony_ci	  For the list of compilers that support KCSAN, please see
118c2ecf20Sopenharmony_ci	  <file:Documentation/dev-tools/kcsan.rst>.
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciconfig KCSAN_KCOV_BROKEN
148c2ecf20Sopenharmony_ci	def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
158c2ecf20Sopenharmony_ci	depends on CC_IS_CLANG
168c2ecf20Sopenharmony_ci	depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=thread -fsanitize-coverage=trace-pc)
178c2ecf20Sopenharmony_ci	help
188c2ecf20Sopenharmony_ci	  Some versions of clang support either KCSAN and KCOV but not the
198c2ecf20Sopenharmony_ci	  combination of the two.
208c2ecf20Sopenharmony_ci	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
218c2ecf20Sopenharmony_ci	  in newer releases.
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cimenuconfig KCSAN
248c2ecf20Sopenharmony_ci	bool "KCSAN: dynamic data race detector"
258c2ecf20Sopenharmony_ci	depends on HAVE_ARCH_KCSAN && HAVE_KCSAN_COMPILER
268c2ecf20Sopenharmony_ci	depends on DEBUG_KERNEL && !KASAN
278c2ecf20Sopenharmony_ci	depends on !KCSAN_KCOV_BROKEN
288c2ecf20Sopenharmony_ci	select STACKTRACE
298c2ecf20Sopenharmony_ci	help
308c2ecf20Sopenharmony_ci	  The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
318c2ecf20Sopenharmony_ci	  data-race detector that relies on compile-time instrumentation.
328c2ecf20Sopenharmony_ci	  KCSAN uses a watchpoint-based sampling approach to detect races.
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	  While KCSAN's primary purpose is to detect data races, it
358c2ecf20Sopenharmony_ci	  also provides assertions to check data access constraints.
368c2ecf20Sopenharmony_ci	  These assertions can expose bugs that do not manifest as
378c2ecf20Sopenharmony_ci	  data races.
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	  See <file:Documentation/dev-tools/kcsan.rst> for more details.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciif KCSAN
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci# Compiler capabilities that should not fail the test if they are unavailable.
448c2ecf20Sopenharmony_ciconfig CC_HAS_TSAN_COMPOUND_READ_BEFORE_WRITE
458c2ecf20Sopenharmony_ci	def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-compound-read-before-write=1)) || \
468c2ecf20Sopenharmony_ci		 (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-compound-read-before-write=1))
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciconfig KCSAN_VERBOSE
498c2ecf20Sopenharmony_ci	bool "Show verbose reports with more information about system state"
508c2ecf20Sopenharmony_ci	depends on PROVE_LOCKING
518c2ecf20Sopenharmony_ci	help
528c2ecf20Sopenharmony_ci	  If enabled, reports show more information about the system state that
538c2ecf20Sopenharmony_ci	  may help better analyze and debug races. This includes held locks and
548c2ecf20Sopenharmony_ci	  IRQ trace events.
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	  While this option should generally be benign, we call into more
578c2ecf20Sopenharmony_ci	  external functions on report generation; if a race report is
588c2ecf20Sopenharmony_ci	  generated from any one of them, system stability may suffer due to
598c2ecf20Sopenharmony_ci	  deadlocks or recursion.  If in doubt, say N.
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciconfig KCSAN_DEBUG
628c2ecf20Sopenharmony_ci	bool "Debugging of KCSAN internals"
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciconfig KCSAN_SELFTEST
658c2ecf20Sopenharmony_ci	bool "Perform short selftests on boot"
668c2ecf20Sopenharmony_ci	default y
678c2ecf20Sopenharmony_ci	help
688c2ecf20Sopenharmony_ci	  Run KCSAN selftests on boot. On test failure, causes the kernel to
698c2ecf20Sopenharmony_ci	  panic. Recommended to be enabled, ensuring critical functionality
708c2ecf20Sopenharmony_ci	  works as intended.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciconfig KCSAN_TEST
738c2ecf20Sopenharmony_ci	tristate "KCSAN test for integrated runtime behaviour"
748c2ecf20Sopenharmony_ci	depends on TRACEPOINTS && KUNIT
758c2ecf20Sopenharmony_ci	select TORTURE_TEST
768c2ecf20Sopenharmony_ci	help
778c2ecf20Sopenharmony_ci	  KCSAN test focusing on behaviour of the integrated runtime. Tests
788c2ecf20Sopenharmony_ci	  various race scenarios, and verifies the reports generated to
798c2ecf20Sopenharmony_ci	  console. Makes use of KUnit for test organization, and the Torture
808c2ecf20Sopenharmony_ci	  framework for test thread control.
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	  Each test case may run at least up to KCSAN_REPORT_ONCE_IN_MS
838c2ecf20Sopenharmony_ci	  milliseconds. Test run duration may be optimized by building the
848c2ecf20Sopenharmony_ci	  kernel and KCSAN test with KCSAN_REPORT_ONCE_IN_MS set to a lower
858c2ecf20Sopenharmony_ci	  than default value.
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	  Say Y here if you want the test to be built into the kernel and run
888c2ecf20Sopenharmony_ci	  during boot; say M if you want the test to build as a module; say N
898c2ecf20Sopenharmony_ci	  if you are unsure.
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciconfig KCSAN_EARLY_ENABLE
928c2ecf20Sopenharmony_ci	bool "Early enable during boot"
938c2ecf20Sopenharmony_ci	default y
948c2ecf20Sopenharmony_ci	help
958c2ecf20Sopenharmony_ci	  If KCSAN should be enabled globally as soon as possible. KCSAN can
968c2ecf20Sopenharmony_ci	  later be enabled/disabled via debugfs.
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciconfig KCSAN_NUM_WATCHPOINTS
998c2ecf20Sopenharmony_ci	int "Number of available watchpoints"
1008c2ecf20Sopenharmony_ci	default 64
1018c2ecf20Sopenharmony_ci	help
1028c2ecf20Sopenharmony_ci	  Total number of available watchpoints. An address range maps into a
1038c2ecf20Sopenharmony_ci	  specific watchpoint slot as specified in kernel/kcsan/encoding.h.
1048c2ecf20Sopenharmony_ci	  Although larger number of watchpoints may not be usable due to
1058c2ecf20Sopenharmony_ci	  limited number of CPUs, a larger value helps to improve performance
1068c2ecf20Sopenharmony_ci	  due to reducing cache-line contention. The chosen default is a
1078c2ecf20Sopenharmony_ci	  conservative value; we should almost never observe "no_capacity"
1088c2ecf20Sopenharmony_ci	  events (see /sys/kernel/debug/kcsan).
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciconfig KCSAN_UDELAY_TASK
1118c2ecf20Sopenharmony_ci	int "Delay in microseconds (for tasks)"
1128c2ecf20Sopenharmony_ci	default 80
1138c2ecf20Sopenharmony_ci	help
1148c2ecf20Sopenharmony_ci	  For tasks, the microsecond delay after setting up a watchpoint.
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciconfig KCSAN_UDELAY_INTERRUPT
1178c2ecf20Sopenharmony_ci	int "Delay in microseconds (for interrupts)"
1188c2ecf20Sopenharmony_ci	default 20
1198c2ecf20Sopenharmony_ci	help
1208c2ecf20Sopenharmony_ci	  For interrupts, the microsecond delay after setting up a watchpoint.
1218c2ecf20Sopenharmony_ci	  Interrupts have tighter latency requirements, and their delay should
1228c2ecf20Sopenharmony_ci	  be lower than for tasks.
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciconfig KCSAN_DELAY_RANDOMIZE
1258c2ecf20Sopenharmony_ci	bool "Randomize above delays"
1268c2ecf20Sopenharmony_ci	default y
1278c2ecf20Sopenharmony_ci	help
1288c2ecf20Sopenharmony_ci	  If delays should be randomized, where the maximum is KCSAN_UDELAY_*.
1298c2ecf20Sopenharmony_ci	  If false, the chosen delays are always the KCSAN_UDELAY_* values
1308c2ecf20Sopenharmony_ci	  as defined above.
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciconfig KCSAN_SKIP_WATCH
1338c2ecf20Sopenharmony_ci	int "Skip instructions before setting up watchpoint"
1348c2ecf20Sopenharmony_ci	default 4000
1358c2ecf20Sopenharmony_ci	help
1368c2ecf20Sopenharmony_ci	  The number of per-CPU memory operations to skip, before another
1378c2ecf20Sopenharmony_ci	  watchpoint is set up, i.e. one in KCSAN_WATCH_SKIP per-CPU
1388c2ecf20Sopenharmony_ci	  memory operations are used to set up a watchpoint. A smaller value
1398c2ecf20Sopenharmony_ci	  results in more aggressive race detection, whereas a larger value
1408c2ecf20Sopenharmony_ci	  improves system performance at the cost of missing some races.
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciconfig KCSAN_SKIP_WATCH_RANDOMIZE
1438c2ecf20Sopenharmony_ci	bool "Randomize watchpoint instruction skip count"
1448c2ecf20Sopenharmony_ci	default y
1458c2ecf20Sopenharmony_ci	help
1468c2ecf20Sopenharmony_ci	  If instruction skip count should be randomized, where the maximum is
1478c2ecf20Sopenharmony_ci	  KCSAN_WATCH_SKIP. If false, the chosen value is always
1488c2ecf20Sopenharmony_ci	  KCSAN_WATCH_SKIP.
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciconfig KCSAN_INTERRUPT_WATCHER
1518c2ecf20Sopenharmony_ci	bool "Interruptible watchers"
1528c2ecf20Sopenharmony_ci	help
1538c2ecf20Sopenharmony_ci	  If enabled, a task that set up a watchpoint may be interrupted while
1548c2ecf20Sopenharmony_ci	  delayed. This option will allow KCSAN to detect races between
1558c2ecf20Sopenharmony_ci	  interrupted tasks and other threads of execution on the same CPU.
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	  Currently disabled by default, because not all safe per-CPU access
1588c2ecf20Sopenharmony_ci	  primitives and patterns may be accounted for, and therefore could
1598c2ecf20Sopenharmony_ci	  result in false positives.
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ciconfig KCSAN_REPORT_ONCE_IN_MS
1628c2ecf20Sopenharmony_ci	int "Duration in milliseconds, in which any given race is only reported once"
1638c2ecf20Sopenharmony_ci	default 3000
1648c2ecf20Sopenharmony_ci	help
1658c2ecf20Sopenharmony_ci	  Any given race is only reported once in the defined time window.
1668c2ecf20Sopenharmony_ci	  Different races may still generate reports within a duration that is
1678c2ecf20Sopenharmony_ci	  smaller than the duration defined here. This allows rate limiting
1688c2ecf20Sopenharmony_ci	  reporting to avoid flooding the console with reports.  Setting this
1698c2ecf20Sopenharmony_ci	  to 0 disables rate limiting.
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci# The main purpose of the below options is to control reported data races (e.g.
1728c2ecf20Sopenharmony_ci# in fuzzer configs), and are not expected to be switched frequently by other
1738c2ecf20Sopenharmony_ci# users. We could turn some of them into boot parameters, but given they should
1748c2ecf20Sopenharmony_ci# not be switched normally, let's keep them here to simplify configuration.
1758c2ecf20Sopenharmony_ci#
1768c2ecf20Sopenharmony_ci# The defaults below are chosen to be very conservative, and may miss certain
1778c2ecf20Sopenharmony_ci# bugs.
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciconfig KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
1808c2ecf20Sopenharmony_ci	bool "Report races of unknown origin"
1818c2ecf20Sopenharmony_ci	default y
1828c2ecf20Sopenharmony_ci	help
1838c2ecf20Sopenharmony_ci	  If KCSAN should report races where only one access is known, and the
1848c2ecf20Sopenharmony_ci	  conflicting access is of unknown origin. This type of race is
1858c2ecf20Sopenharmony_ci	  reported if it was only possible to infer a race due to a data value
1868c2ecf20Sopenharmony_ci	  change while an access is being delayed on a watchpoint.
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciconfig KCSAN_REPORT_VALUE_CHANGE_ONLY
1898c2ecf20Sopenharmony_ci	bool "Only report races where watcher observed a data value change"
1908c2ecf20Sopenharmony_ci	default y
1918c2ecf20Sopenharmony_ci	help
1928c2ecf20Sopenharmony_ci	  If enabled and a conflicting write is observed via a watchpoint, but
1938c2ecf20Sopenharmony_ci	  the data value of the memory location was observed to remain
1948c2ecf20Sopenharmony_ci	  unchanged, do not report the data race.
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciconfig KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
1978c2ecf20Sopenharmony_ci	bool "Assume that plain aligned writes up to word size are atomic"
1988c2ecf20Sopenharmony_ci	default y
1998c2ecf20Sopenharmony_ci	help
2008c2ecf20Sopenharmony_ci	  Assume that plain aligned writes up to word size are atomic by
2018c2ecf20Sopenharmony_ci	  default, and also not subject to other unsafe compiler optimizations
2028c2ecf20Sopenharmony_ci	  resulting in data races. This will cause KCSAN to not report data
2038c2ecf20Sopenharmony_ci	  races due to conflicts where the only plain accesses are aligned
2048c2ecf20Sopenharmony_ci	  writes up to word size: conflicts between marked reads and plain
2058c2ecf20Sopenharmony_ci	  aligned writes up to word size will not be reported as data races;
2068c2ecf20Sopenharmony_ci	  notice that data races between two conflicting plain aligned writes
2078c2ecf20Sopenharmony_ci	  will also not be reported.
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ciconfig KCSAN_IGNORE_ATOMICS
2108c2ecf20Sopenharmony_ci	bool "Do not instrument marked atomic accesses"
2118c2ecf20Sopenharmony_ci	help
2128c2ecf20Sopenharmony_ci	  Never instrument marked atomic accesses. This option can be used for
2138c2ecf20Sopenharmony_ci	  additional filtering. Conflicting marked atomic reads and plain
2148c2ecf20Sopenharmony_ci	  writes will never be reported as a data race, however, will cause
2158c2ecf20Sopenharmony_ci	  plain reads and marked writes to result in "unknown origin" reports.
2168c2ecf20Sopenharmony_ci	  If combined with CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n, data
2178c2ecf20Sopenharmony_ci	  races where at least one access is marked atomic will never be
2188c2ecf20Sopenharmony_ci	  reported.
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	  Similar to KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, but including unaligned
2218c2ecf20Sopenharmony_ci	  accesses, conflicting marked atomic reads and plain writes will not
2228c2ecf20Sopenharmony_ci	  be reported as data races; however, unlike that option, data races
2238c2ecf20Sopenharmony_ci	  due to two conflicting plain writes will be reported (aligned and
2248c2ecf20Sopenharmony_ci	  unaligned, if CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n).
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciendif # KCSAN
227