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