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