xref: /openbmc/linux/lib/Kconfig.kcsan (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1dfd402a4SMarco Elver# SPDX-License-Identifier: GPL-2.0-only
2dfd402a4SMarco Elver
3dfd402a4SMarco Elverconfig HAVE_ARCH_KCSAN
4dfd402a4SMarco Elver	bool
5dfd402a4SMarco Elver
60e1aa5b6SMarco Elverconfig HAVE_KCSAN_COMPILER
7e68dcd8eSMarco Elver	def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-distinguish-volatile=1)) || \
8e68dcd8eSMarco Elver		 (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-distinguish-volatile=1))
90e1aa5b6SMarco Elver	help
100e1aa5b6SMarco Elver	  For the list of compilers that support KCSAN, please see
110e1aa5b6SMarco Elver	  <file:Documentation/dev-tools/kcsan.rst>.
120e1aa5b6SMarco Elver
13dfd402a4SMarco Elvermenuconfig KCSAN
14eba9c444SIngo Molnar	bool "KCSAN: dynamic data race detector"
150e1aa5b6SMarco Elver	depends on HAVE_ARCH_KCSAN && HAVE_KCSAN_COMPILER
160e1aa5b6SMarco Elver	depends on DEBUG_KERNEL && !KASAN
17*6ba912f1SArnd Bergmann	select CONSTRUCTORS
188cfbb04fSMarco Elver	select STACKTRACE
19dfd402a4SMarco Elver	help
20eba9c444SIngo Molnar	  The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
21eba9c444SIngo Molnar	  data-race detector that relies on compile-time instrumentation.
22eba9c444SIngo Molnar	  KCSAN uses a watchpoint-based sampling approach to detect races.
23d591ec3dSMarco Elver
24eba9c444SIngo Molnar	  While KCSAN's primary purpose is to detect data races, it
25eba9c444SIngo Molnar	  also provides assertions to check data access constraints.
26eba9c444SIngo Molnar	  These assertions can expose bugs that do not manifest as
27eba9c444SIngo Molnar	  data races.
288cfbb04fSMarco Elver
298cfbb04fSMarco Elver	  See <file:Documentation/dev-tools/kcsan.rst> for more details.
30dfd402a4SMarco Elver
31dfd402a4SMarco Elverif KCSAN
32dfd402a4SMarco Elver
33bec4a247SMarco Elverconfig CC_HAS_TSAN_COMPOUND_READ_BEFORE_WRITE
34bec4a247SMarco Elver	def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-compound-read-before-write=1)) || \
35bec4a247SMarco Elver		 (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-compound-read-before-write=1))
36eb32f9f9SMarco Elver	help
37eb32f9f9SMarco Elver	  The compiler instruments plain compound read-write operations
38eb32f9f9SMarco Elver	  differently (++, --, +=, -=, |=, &=, etc.), which allows KCSAN to
39eb32f9f9SMarco Elver	  distinguish them from other plain accesses. This is currently
40eb32f9f9SMarco Elver	  supported by Clang 12 or later.
41bec4a247SMarco Elver
422402d0eaSMarco Elverconfig KCSAN_VERBOSE
432402d0eaSMarco Elver	bool "Show verbose reports with more information about system state"
442402d0eaSMarco Elver	depends on PROVE_LOCKING
452402d0eaSMarco Elver	help
462402d0eaSMarco Elver	  If enabled, reports show more information about the system state that
472402d0eaSMarco Elver	  may help better analyze and debug races. This includes held locks and
482402d0eaSMarco Elver	  IRQ trace events.
492402d0eaSMarco Elver
502402d0eaSMarco Elver	  While this option should generally be benign, we call into more
512402d0eaSMarco Elver	  external functions on report generation; if a race report is
522402d0eaSMarco Elver	  generated from any one of them, system stability may suffer due to
532402d0eaSMarco Elver	  deadlocks or recursion.  If in doubt, say N.
542402d0eaSMarco Elver
55dfd402a4SMarco Elverconfig KCSAN_SELFTEST
56dfd402a4SMarco Elver	bool "Perform short selftests on boot"
57dfd402a4SMarco Elver	default y
58dfd402a4SMarco Elver	help
591fe84fd4SMarco Elver	  Run KCSAN selftests on boot. On test failure, causes the kernel to
601fe84fd4SMarco Elver	  panic. Recommended to be enabled, ensuring critical functionality
611fe84fd4SMarco Elver	  works as intended.
621fe84fd4SMarco Elver
63a146fed5SMarco Elverconfig KCSAN_KUNIT_TEST
64a146fed5SMarco Elver	tristate "KCSAN test for integrated runtime behaviour" if !KUNIT_ALL_TESTS
65a146fed5SMarco Elver	default KUNIT_ALL_TESTS
661fe84fd4SMarco Elver	depends on TRACEPOINTS && KUNIT
671fe84fd4SMarco Elver	select TORTURE_TEST
681fe84fd4SMarco Elver	help
691fe84fd4SMarco Elver	  KCSAN test focusing on behaviour of the integrated runtime. Tests
701fe84fd4SMarco Elver	  various race scenarios, and verifies the reports generated to
711fe84fd4SMarco Elver	  console. Makes use of KUnit for test organization, and the Torture
721fe84fd4SMarco Elver	  framework for test thread control.
731fe84fd4SMarco Elver
741fe84fd4SMarco Elver	  Each test case may run at least up to KCSAN_REPORT_ONCE_IN_MS
751fe84fd4SMarco Elver	  milliseconds. Test run duration may be optimized by building the
761fe84fd4SMarco Elver	  kernel and KCSAN test with KCSAN_REPORT_ONCE_IN_MS set to a lower
771fe84fd4SMarco Elver	  than default value.
781fe84fd4SMarco Elver
791fe84fd4SMarco Elver	  Say Y here if you want the test to be built into the kernel and run
801fe84fd4SMarco Elver	  during boot; say M if you want the test to build as a module; say N
811fe84fd4SMarco Elver	  if you are unsure.
82dfd402a4SMarco Elver
83dfd402a4SMarco Elverconfig KCSAN_EARLY_ENABLE
84dfd402a4SMarco Elver	bool "Early enable during boot"
85dfd402a4SMarco Elver	default y
86dfd402a4SMarco Elver	help
87dfd402a4SMarco Elver	  If KCSAN should be enabled globally as soon as possible. KCSAN can
88dfd402a4SMarco Elver	  later be enabled/disabled via debugfs.
89dfd402a4SMarco Elver
90dfd402a4SMarco Elverconfig KCSAN_NUM_WATCHPOINTS
91dfd402a4SMarco Elver	int "Number of available watchpoints"
92dfd402a4SMarco Elver	default 64
93dfd402a4SMarco Elver	help
94dfd402a4SMarco Elver	  Total number of available watchpoints. An address range maps into a
95dfd402a4SMarco Elver	  specific watchpoint slot as specified in kernel/kcsan/encoding.h.
96dfd402a4SMarco Elver	  Although larger number of watchpoints may not be usable due to
97dfd402a4SMarco Elver	  limited number of CPUs, a larger value helps to improve performance
98dfd402a4SMarco Elver	  due to reducing cache-line contention. The chosen default is a
99dfd402a4SMarco Elver	  conservative value; we should almost never observe "no_capacity"
100dfd402a4SMarco Elver	  events (see /sys/kernel/debug/kcsan).
101dfd402a4SMarco Elver
102dfd402a4SMarco Elverconfig KCSAN_UDELAY_TASK
103dfd402a4SMarco Elver	int "Delay in microseconds (for tasks)"
104dfd402a4SMarco Elver	default 80
105dfd402a4SMarco Elver	help
106dfd402a4SMarco Elver	  For tasks, the microsecond delay after setting up a watchpoint.
107dfd402a4SMarco Elver
108dfd402a4SMarco Elverconfig KCSAN_UDELAY_INTERRUPT
109dfd402a4SMarco Elver	int "Delay in microseconds (for interrupts)"
110dfd402a4SMarco Elver	default 20
111dfd402a4SMarco Elver	help
112dfd402a4SMarco Elver	  For interrupts, the microsecond delay after setting up a watchpoint.
113dfd402a4SMarco Elver	  Interrupts have tighter latency requirements, and their delay should
114dfd402a4SMarco Elver	  be lower than for tasks.
115dfd402a4SMarco Elver
116dfd402a4SMarco Elverconfig KCSAN_DELAY_RANDOMIZE
117dfd402a4SMarco Elver	bool "Randomize above delays"
118dfd402a4SMarco Elver	default y
119dfd402a4SMarco Elver	help
120dfd402a4SMarco Elver	  If delays should be randomized, where the maximum is KCSAN_UDELAY_*.
1215cbaefe9SIngo Molnar	  If false, the chosen delays are always the KCSAN_UDELAY_* values
1225cbaefe9SIngo Molnar	  as defined above.
123dfd402a4SMarco Elver
124dfd402a4SMarco Elverconfig KCSAN_SKIP_WATCH
125dfd402a4SMarco Elver	int "Skip instructions before setting up watchpoint"
126dfd402a4SMarco Elver	default 4000
127dfd402a4SMarco Elver	help
128dfd402a4SMarco Elver	  The number of per-CPU memory operations to skip, before another
129144b9152SRyosuke Yasuoka	  watchpoint is set up, i.e. one in KCSAN_SKIP_WATCH per-CPU
130dfd402a4SMarco Elver	  memory operations are used to set up a watchpoint. A smaller value
131dfd402a4SMarco Elver	  results in more aggressive race detection, whereas a larger value
132dfd402a4SMarco Elver	  improves system performance at the cost of missing some races.
133dfd402a4SMarco Elver
134dfd402a4SMarco Elverconfig KCSAN_SKIP_WATCH_RANDOMIZE
135dfd402a4SMarco Elver	bool "Randomize watchpoint instruction skip count"
136dfd402a4SMarco Elver	default y
137dfd402a4SMarco Elver	help
138dfd402a4SMarco Elver	  If instruction skip count should be randomized, where the maximum is
139144b9152SRyosuke Yasuoka	  KCSAN_SKIP_WATCH. If false, the chosen value is always
140144b9152SRyosuke Yasuoka	  KCSAN_SKIP_WATCH.
141dfd402a4SMarco Elver
14248b1fc19SMarco Elverconfig KCSAN_INTERRUPT_WATCHER
143e0493804SMarco Elver	bool "Interruptible watchers" if !KCSAN_STRICT
144e0493804SMarco Elver	default KCSAN_STRICT
14548b1fc19SMarco Elver	help
14648b1fc19SMarco Elver	  If enabled, a task that set up a watchpoint may be interrupted while
14748b1fc19SMarco Elver	  delayed. This option will allow KCSAN to detect races between
14848b1fc19SMarco Elver	  interrupted tasks and other threads of execution on the same CPU.
14948b1fc19SMarco Elver
15048b1fc19SMarco Elver	  Currently disabled by default, because not all safe per-CPU access
15148b1fc19SMarco Elver	  primitives and patterns may be accounted for, and therefore could
15248b1fc19SMarco Elver	  result in false positives.
15348b1fc19SMarco Elver
15405f9a406SMarco Elverconfig KCSAN_REPORT_ONCE_IN_MS
155d591ec3dSMarco Elver	int "Duration in milliseconds, in which any given race is only reported once"
15605f9a406SMarco Elver	default 3000
15705f9a406SMarco Elver	help
158d591ec3dSMarco Elver	  Any given race is only reported once in the defined time window.
159d591ec3dSMarco Elver	  Different races may still generate reports within a duration that is
160d591ec3dSMarco Elver	  smaller than the duration defined here. This allows rate limiting
161d591ec3dSMarco Elver	  reporting to avoid flooding the console with reports.  Setting this
162d591ec3dSMarco Elver	  to 0 disables rate limiting.
16305f9a406SMarco Elver
164eb32f9f9SMarco Elver# The main purpose of the below options is to control reported data races, and
165eb32f9f9SMarco Elver# are not expected to be switched frequently by non-testers or at runtime.
166eb32f9f9SMarco Elver# The defaults are chosen to be conservative, and can miss certain bugs.
167dfd402a4SMarco Elver
168dfd402a4SMarco Elverconfig KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
169dfd402a4SMarco Elver	bool "Report races of unknown origin"
170dfd402a4SMarco Elver	default y
171dfd402a4SMarco Elver	help
172dfd402a4SMarco Elver	  If KCSAN should report races where only one access is known, and the
173dfd402a4SMarco Elver	  conflicting access is of unknown origin. This type of race is
174dfd402a4SMarco Elver	  reported if it was only possible to infer a race due to a data value
175dfd402a4SMarco Elver	  change while an access is being delayed on a watchpoint.
176dfd402a4SMarco Elver
177e675d253SMarco Elverconfig KCSAN_STRICT
178e675d253SMarco Elver	bool "Strict data-race checking"
179e675d253SMarco Elver	help
180e675d253SMarco Elver	  KCSAN will report data races with the strictest possible rules, which
181e675d253SMarco Elver	  closely aligns with the rules defined by the Linux-kernel memory
182e675d253SMarco Elver	  consistency model (LKMM).
183e675d253SMarco Elver
18469562e49SMarco Elverconfig KCSAN_WEAK_MEMORY
18569562e49SMarco Elver	bool "Enable weak memory modeling to detect missing memory barriers"
18669562e49SMarco Elver	default y
18769562e49SMarco Elver	depends on KCSAN_STRICT
18869562e49SMarco Elver	# We can either let objtool nop __tsan_func_{entry,exit}() and builtin
18969562e49SMarco Elver	# atomics instrumentation in .noinstr.text, or use a compiler that can
19069562e49SMarco Elver	# implement __no_kcsan to really remove all instrumentation.
19122102f45SJosh Poimboeuf	depends on !ARCH_WANTS_NO_INSTR || HAVE_NOINSTR_HACK || \
19222102f45SJosh Poimboeuf		   CC_IS_GCC || CLANG_VERSION >= 140000
19322102f45SJosh Poimboeuf	select OBJTOOL if HAVE_NOINSTR_HACK
19469562e49SMarco Elver	help
19569562e49SMarco Elver	  Enable support for modeling a subset of weak memory, which allows
19669562e49SMarco Elver	  detecting a subset of data races due to missing memory barriers.
19769562e49SMarco Elver
198ca0f2cfcSRandy Dunlap	  Depends on KCSAN_STRICT, because the options strengthening certain
19969562e49SMarco Elver	  plain accesses by default (depending on !KCSAN_STRICT) reduce the
20069562e49SMarco Elver	  ability to detect any data races invoving reordered accesses, in
20169562e49SMarco Elver	  particular reordered writes.
20269562e49SMarco Elver
20369562e49SMarco Elver	  Weak memory modeling relies on additional instrumentation and may
20469562e49SMarco Elver	  affect performance.
20569562e49SMarco Elver
206dfd402a4SMarco Elverconfig KCSAN_REPORT_VALUE_CHANGE_ONLY
207dfd402a4SMarco Elver	bool "Only report races where watcher observed a data value change"
208dfd402a4SMarco Elver	default y
209e675d253SMarco Elver	depends on !KCSAN_STRICT
210dfd402a4SMarco Elver	help
2115cbaefe9SIngo Molnar	  If enabled and a conflicting write is observed via a watchpoint, but
212dfd402a4SMarco Elver	  the data value of the memory location was observed to remain
213dfd402a4SMarco Elver	  unchanged, do not report the data race.
214dfd402a4SMarco Elver
2151e6ee2f0SMarco Elverconfig KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
2161e6ee2f0SMarco Elver	bool "Assume that plain aligned writes up to word size are atomic"
2171e6ee2f0SMarco Elver	default y
218e675d253SMarco Elver	depends on !KCSAN_STRICT
2191e6ee2f0SMarco Elver	help
2201e6ee2f0SMarco Elver	  Assume that plain aligned writes up to word size are atomic by
2211e6ee2f0SMarco Elver	  default, and also not subject to other unsafe compiler optimizations
2221e6ee2f0SMarco Elver	  resulting in data races. This will cause KCSAN to not report data
2231e6ee2f0SMarco Elver	  races due to conflicts where the only plain accesses are aligned
2241e6ee2f0SMarco Elver	  writes up to word size: conflicts between marked reads and plain
2251e6ee2f0SMarco Elver	  aligned writes up to word size will not be reported as data races;
2261e6ee2f0SMarco Elver	  notice that data races between two conflicting plain aligned writes
2271e6ee2f0SMarco Elver	  will also not be reported.
2281e6ee2f0SMarco Elver
229dfd402a4SMarco Elverconfig KCSAN_IGNORE_ATOMICS
230dfd402a4SMarco Elver	bool "Do not instrument marked atomic accesses"
231e675d253SMarco Elver	depends on !KCSAN_STRICT
232dfd402a4SMarco Elver	help
233a249a732SMarco Elver	  Never instrument marked atomic accesses. This option can be used for
234a249a732SMarco Elver	  additional filtering. Conflicting marked atomic reads and plain
235a249a732SMarco Elver	  writes will never be reported as a data race, however, will cause
236a249a732SMarco Elver	  plain reads and marked writes to result in "unknown origin" reports.
237a249a732SMarco Elver	  If combined with CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n, data
238a249a732SMarco Elver	  races where at least one access is marked atomic will never be
239a249a732SMarco Elver	  reported.
240a249a732SMarco Elver
241a249a732SMarco Elver	  Similar to KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, but including unaligned
242a249a732SMarco Elver	  accesses, conflicting marked atomic reads and plain writes will not
243a249a732SMarco Elver	  be reported as data races; however, unlike that option, data races
244a249a732SMarco Elver	  due to two conflicting plain writes will be reported (aligned and
245a249a732SMarco Elver	  unaligned, if CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n).
246dfd402a4SMarco Elver
24749f72d53SMarco Elverconfig KCSAN_PERMISSIVE
24849f72d53SMarco Elver	bool "Enable all additional permissive rules"
24949f72d53SMarco Elver	depends on KCSAN_REPORT_VALUE_CHANGE_ONLY
25049f72d53SMarco Elver	help
25149f72d53SMarco Elver	  Enable additional permissive rules to ignore certain classes of data
25249f72d53SMarco Elver	  races (also see kernel/kcsan/permissive.h). None of the permissive
25349f72d53SMarco Elver	  rules imply that such data races are generally safe, but can be used
25449f72d53SMarco Elver	  to further reduce reported data races due to data-racy patterns
25549f72d53SMarco Elver	  common across the kernel.
25649f72d53SMarco Elver
257dfd402a4SMarco Elverendif # KCSAN
258