xref: /openbmc/linux/Documentation/dev-tools/kcsan.rst (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1bd0ccc4aSMarco Elver.. SPDX-License-Identifier: GPL-2.0
2bd0ccc4aSMarco Elver.. Copyright (C) 2019, Google LLC.
3bd0ccc4aSMarco Elver
4905e672bSMarco ElverThe Kernel Concurrency Sanitizer (KCSAN)
5905e672bSMarco Elver========================================
6905e672bSMarco Elver
7e7325b77SMarco ElverThe Kernel Concurrency Sanitizer (KCSAN) is a dynamic race detector, which
8e7325b77SMarco Elverrelies on compile-time instrumentation, and uses a watchpoint-based sampling
9e7325b77SMarco Elverapproach to detect races. KCSAN's primary purpose is to detect `data races`_.
10905e672bSMarco Elver
11905e672bSMarco ElverUsage
12905e672bSMarco Elver-----
13905e672bSMarco Elver
14e68dcd8eSMarco ElverKCSAN is supported by both GCC and Clang. With GCC we require version 11 or
15e68dcd8eSMarco Elverlater, and with Clang also require version 11 or later.
16e7325b77SMarco Elver
17e7325b77SMarco ElverTo enable KCSAN configure the kernel with::
18905e672bSMarco Elver
19905e672bSMarco Elver    CONFIG_KCSAN = y
20905e672bSMarco Elver
21905e672bSMarco ElverKCSAN provides several other configuration options to customize behaviour (see
22e7325b77SMarco Elverthe respective help text in ``lib/Kconfig.kcsan`` for more info).
23905e672bSMarco Elver
24905e672bSMarco ElverError reports
25905e672bSMarco Elver~~~~~~~~~~~~~
26905e672bSMarco Elver
27905e672bSMarco ElverA typical data race report looks like this::
28905e672bSMarco Elver
29905e672bSMarco Elver    ==================================================================
30b930226fSMarco Elver    BUG: KCSAN: data-race in test_kernel_read / test_kernel_write
31905e672bSMarco Elver
32b930226fSMarco Elver    write to 0xffffffffc009a628 of 8 bytes by task 487 on cpu 0:
33b930226fSMarco Elver     test_kernel_write+0x1d/0x30
34b930226fSMarco Elver     access_thread+0x89/0xd0
35b930226fSMarco Elver     kthread+0x23e/0x260
36b930226fSMarco Elver     ret_from_fork+0x22/0x30
37905e672bSMarco Elver
38b930226fSMarco Elver    read to 0xffffffffc009a628 of 8 bytes by task 488 on cpu 6:
39b930226fSMarco Elver     test_kernel_read+0x10/0x20
40b930226fSMarco Elver     access_thread+0x89/0xd0
41b930226fSMarco Elver     kthread+0x23e/0x260
42b930226fSMarco Elver     ret_from_fork+0x22/0x30
43b930226fSMarco Elver
44b930226fSMarco Elver    value changed: 0x00000000000009a6 -> 0x00000000000009b2
45905e672bSMarco Elver
46905e672bSMarco Elver    Reported by Kernel Concurrency Sanitizer on:
47b930226fSMarco Elver    CPU: 6 PID: 488 Comm: access_thread Not tainted 5.12.0-rc2+ #1
48b930226fSMarco Elver    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014
49905e672bSMarco Elver    ==================================================================
50905e672bSMarco Elver
51905e672bSMarco ElverThe header of the report provides a short summary of the functions involved in
52905e672bSMarco Elverthe race. It is followed by the access types and stack traces of the 2 threads
53b930226fSMarco Elverinvolved in the data race. If KCSAN also observed a value change, the observed
54b930226fSMarco Elverold value and new value are shown on the "value changed" line respectively.
55905e672bSMarco Elver
56905e672bSMarco ElverThe other less common type of data race report looks like this::
57905e672bSMarco Elver
58905e672bSMarco Elver    ==================================================================
59b930226fSMarco Elver    BUG: KCSAN: data-race in test_kernel_rmw_array+0x71/0xd0
60905e672bSMarco Elver
61b930226fSMarco Elver    race at unknown origin, with read to 0xffffffffc009bdb0 of 8 bytes by task 515 on cpu 2:
62b930226fSMarco Elver     test_kernel_rmw_array+0x71/0xd0
63b930226fSMarco Elver     access_thread+0x89/0xd0
64b930226fSMarco Elver     kthread+0x23e/0x260
65b930226fSMarco Elver     ret_from_fork+0x22/0x30
66b930226fSMarco Elver
67b930226fSMarco Elver    value changed: 0x0000000000002328 -> 0x0000000000002329
68905e672bSMarco Elver
69905e672bSMarco Elver    Reported by Kernel Concurrency Sanitizer on:
70b930226fSMarco Elver    CPU: 2 PID: 515 Comm: access_thread Not tainted 5.12.0-rc2+ #1
71b930226fSMarco Elver    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014
72905e672bSMarco Elver    ==================================================================
73905e672bSMarco Elver
74905e672bSMarco ElverThis report is generated where it was not possible to determine the other
75905e672bSMarco Elverracing thread, but a race was inferred due to the data value of the watched
76b930226fSMarco Elvermemory location having changed. These reports always show a "value changed"
77b930226fSMarco Elverline. A common reason for reports of this type are missing instrumentation in
78b930226fSMarco Elverthe racing thread, but could also occur due to e.g. DMA accesses. Such reports
79b930226fSMarco Elverare shown only if ``CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=y``, which is
80b930226fSMarco Elverenabled by default.
81905e672bSMarco Elver
82905e672bSMarco ElverSelective analysis
83905e672bSMarco Elver~~~~~~~~~~~~~~~~~~
84905e672bSMarco Elver
8571611774SMarco ElverIt may be desirable to disable data race detection for specific accesses,
8671611774SMarco Elverfunctions, compilation units, or entire subsystems.  For static blacklisting,
8771611774SMarco Elverthe below options are available:
88905e672bSMarco Elver
8971611774SMarco Elver* KCSAN understands the ``data_race(expr)`` annotation, which tells KCSAN that
9071611774SMarco Elver  any data races due to accesses in ``expr`` should be ignored and resulting
91ea048464SPaul E. McKenney  behaviour when encountering a data race is deemed safe.  Please see
92117232c0SAkira Yokosawa  `"Marking Shared-Memory Accesses" in the LKMM`_ for more information.
93905e672bSMarco Elver
9471611774SMarco Elver* Disabling data race detection for entire functions can be accomplished by
95e7325b77SMarco Elver  using the function attribute ``__no_kcsan``::
96e7325b77SMarco Elver
97e7325b77SMarco Elver    __no_kcsan
98e7325b77SMarco Elver    void foo(void) {
99e7325b77SMarco Elver        ...
100e7325b77SMarco Elver
101e7325b77SMarco Elver  To dynamically limit for which functions to generate reports, see the
102e7325b77SMarco Elver  `DebugFS interface`_ blacklist/whitelist feature.
103e7325b77SMarco Elver
10471611774SMarco Elver* To disable data race detection for a particular compilation unit, add to the
10571611774SMarco Elver  ``Makefile``::
106905e672bSMarco Elver
107905e672bSMarco Elver    KCSAN_SANITIZE_file.o := n
108905e672bSMarco Elver
10971611774SMarco Elver* To disable data race detection for all compilation units listed in a
11071611774SMarco Elver  ``Makefile``, add to the respective ``Makefile``::
11171611774SMarco Elver
11271611774SMarco Elver    KCSAN_SANITIZE := n
113905e672bSMarco Elver
114117232c0SAkira Yokosawa.. _"Marking Shared-Memory Accesses" in the LKMM: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/memory-model/Documentation/access-marking.txt
115117232c0SAkira Yokosawa
116e7325b77SMarco ElverFurthermore, it is possible to tell KCSAN to show or hide entire classes of
117e7325b77SMarco Elverdata races, depending on preferences. These can be changed via the following
118e7325b77SMarco ElverKconfig options:
119905e672bSMarco Elver
120e7325b77SMarco Elver* ``CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY``: If enabled and a conflicting write
121e7325b77SMarco Elver  is observed via a watchpoint, but the data value of the memory location was
122e7325b77SMarco Elver  observed to remain unchanged, do not report the data race.
123905e672bSMarco Elver
124e7325b77SMarco Elver* ``CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC``: Assume that plain aligned writes
125e7325b77SMarco Elver  up to word size are atomic by default. Assumes that such writes are not
126e7325b77SMarco Elver  subject to unsafe compiler optimizations resulting in data races. The option
127e7325b77SMarco Elver  causes KCSAN to not report data races due to conflicts where the only plain
128e7325b77SMarco Elver  accesses are aligned writes up to word size.
129e7325b77SMarco Elver
13049f72d53SMarco Elver* ``CONFIG_KCSAN_PERMISSIVE``: Enable additional permissive rules to ignore
13149f72d53SMarco Elver  certain classes of common data races. Unlike the above, the rules are more
13249f72d53SMarco Elver  complex involving value-change patterns, access type, and address. This
13349f72d53SMarco Elver  option depends on ``CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY=y``. For details
13449f72d53SMarco Elver  please see the ``kernel/kcsan/permissive.h``. Testers and maintainers that
13549f72d53SMarco Elver  only focus on reports from specific subsystems and not the whole kernel are
13649f72d53SMarco Elver  recommended to disable this option.
13749f72d53SMarco Elver
138e675d253SMarco ElverTo use the strictest possible rules, select ``CONFIG_KCSAN_STRICT=y``, which
139e675d253SMarco Elverconfigures KCSAN to follow the Linux-kernel memory consistency model (LKMM) as
140e675d253SMarco Elverclosely as possible.
141e675d253SMarco Elver
142e7325b77SMarco ElverDebugFS interface
143e7325b77SMarco Elver~~~~~~~~~~~~~~~~~
144e7325b77SMarco Elver
145e7325b77SMarco ElverThe file ``/sys/kernel/debug/kcsan`` provides the following interface:
146e7325b77SMarco Elver
147e7325b77SMarco Elver* Reading ``/sys/kernel/debug/kcsan`` returns various runtime statistics.
148e7325b77SMarco Elver
149e7325b77SMarco Elver* Writing ``on`` or ``off`` to ``/sys/kernel/debug/kcsan`` allows turning KCSAN
150e7325b77SMarco Elver  on or off, respectively.
151905e672bSMarco Elver
152905e672bSMarco Elver* Writing ``!some_func_name`` to ``/sys/kernel/debug/kcsan`` adds
153905e672bSMarco Elver  ``some_func_name`` to the report filter list, which (by default) blacklists
154905e672bSMarco Elver  reporting data races where either one of the top stackframes are a function
155905e672bSMarco Elver  in the list.
156905e672bSMarco Elver
157905e672bSMarco Elver* Writing either ``blacklist`` or ``whitelist`` to ``/sys/kernel/debug/kcsan``
158905e672bSMarco Elver  changes the report filtering behaviour. For example, the blacklist feature
159905e672bSMarco Elver  can be used to silence frequently occurring data races; the whitelist feature
160905e672bSMarco Elver  can help with reproduction and testing of fixes.
161905e672bSMarco Elver
162e7325b77SMarco ElverTuning performance
163e7325b77SMarco Elver~~~~~~~~~~~~~~~~~~
164e7325b77SMarco Elver
165e7325b77SMarco ElverCore parameters that affect KCSAN's overall performance and bug detection
166e7325b77SMarco Elverability are exposed as kernel command-line arguments whose defaults can also be
167e7325b77SMarco Elverchanged via the corresponding Kconfig options.
168e7325b77SMarco Elver
169e7325b77SMarco Elver* ``kcsan.skip_watch`` (``CONFIG_KCSAN_SKIP_WATCH``): Number of per-CPU memory
170e7325b77SMarco Elver  operations to skip, before another watchpoint is set up. Setting up
171e7325b77SMarco Elver  watchpoints more frequently will result in the likelihood of races to be
172e7325b77SMarco Elver  observed to increase. This parameter has the most significant impact on
173e7325b77SMarco Elver  overall system performance and race detection ability.
174e7325b77SMarco Elver
175e7325b77SMarco Elver* ``kcsan.udelay_task`` (``CONFIG_KCSAN_UDELAY_TASK``): For tasks, the
176e7325b77SMarco Elver  microsecond delay to stall execution after a watchpoint has been set up.
177e7325b77SMarco Elver  Larger values result in the window in which we may observe a race to
178e7325b77SMarco Elver  increase.
179e7325b77SMarco Elver
180e7325b77SMarco Elver* ``kcsan.udelay_interrupt`` (``CONFIG_KCSAN_UDELAY_INTERRUPT``): For
181e7325b77SMarco Elver  interrupts, the microsecond delay to stall execution after a watchpoint has
182e7325b77SMarco Elver  been set up. Interrupts have tighter latency requirements, and their delay
183e7325b77SMarco Elver  should generally be smaller than the one chosen for tasks.
184e7325b77SMarco Elver
185e7325b77SMarco ElverThey may be tweaked at runtime via ``/sys/module/kcsan/parameters/``.
186e7325b77SMarco Elver
187905e672bSMarco ElverData Races
188905e672bSMarco Elver----------
189905e672bSMarco Elver
190e7325b77SMarco ElverIn an execution, two memory accesses form a *data race* if they *conflict*,
191e7325b77SMarco Elverthey happen concurrently in different threads, and at least one of them is a
192e7325b77SMarco Elver*plain access*; they *conflict* if both access the same memory location, and at
193e7325b77SMarco Elverleast one is a write. For a more thorough discussion and definition, see `"Plain
194e7325b77SMarco ElverAccesses and Data Races" in the LKMM`_.
195905e672bSMarco Elver
196e7325b77SMarco Elver.. _"Plain Accesses and Data Races" in the LKMM: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/memory-model/Documentation/explanation.txt#n1922
197e7325b77SMarco Elver
198e7325b77SMarco ElverRelationship with the Linux-Kernel Memory Consistency Model (LKMM)
199e7325b77SMarco Elver~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200905e672bSMarco Elver
201905e672bSMarco ElverThe LKMM defines the propagation and ordering rules of various memory
202905e672bSMarco Elveroperations, which gives developers the ability to reason about concurrent code.
203905e672bSMarco ElverUltimately this allows to determine the possible executions of concurrent code,
204905e672bSMarco Elverand if that code is free from data races.
205905e672bSMarco Elver
206e7325b77SMarco ElverKCSAN is aware of *marked atomic operations* (``READ_ONCE``, ``WRITE_ONCE``,
207*82eb6911SMarco Elver``atomic_*``, etc.), and a subset of ordering guarantees implied by memory
208*82eb6911SMarco Elverbarriers. With ``CONFIG_KCSAN_WEAK_MEMORY=y``, KCSAN models load or store
209*82eb6911SMarco Elverbuffering, and can detect missing ``smp_mb()``, ``smp_wmb()``, ``smp_rmb()``,
210*82eb6911SMarco Elver``smp_store_release()``, and all ``atomic_*`` operations with equivalent
211*82eb6911SMarco Elverimplied barriers.
212905e672bSMarco Elver
213*82eb6911SMarco ElverNote, KCSAN will not report all data races due to missing memory ordering,
214*82eb6911SMarco Elverspecifically where a memory barrier would be required to prohibit subsequent
215*82eb6911SMarco Elvermemory operation from reordering before the barrier. Developers should
216*82eb6911SMarco Elvertherefore carefully consider the required memory ordering requirements that
217*82eb6911SMarco Elverremain unchecked.
218905e672bSMarco Elver
219e7325b77SMarco ElverRace Detection Beyond Data Races
220e7325b77SMarco Elver--------------------------------
221905e672bSMarco Elver
222e7325b77SMarco ElverFor code with complex concurrency design, race-condition bugs may not always
223e7325b77SMarco Elvermanifest as data races. Race conditions occur if concurrently executing
224e7325b77SMarco Elveroperations result in unexpected system behaviour. On the other hand, data races
225e7325b77SMarco Elverare defined at the C-language level. The following macros can be used to check
226e7325b77SMarco Elverproperties of concurrent code where bugs would not manifest as data races.
227905e672bSMarco Elver
228e7325b77SMarco Elver.. kernel-doc:: include/linux/kcsan-checks.h
229d8949ef1SMarco Elver    :functions: ASSERT_EXCLUSIVE_WRITER ASSERT_EXCLUSIVE_WRITER_SCOPED
230d8949ef1SMarco Elver                ASSERT_EXCLUSIVE_ACCESS ASSERT_EXCLUSIVE_ACCESS_SCOPED
231e7325b77SMarco Elver                ASSERT_EXCLUSIVE_BITS
232905e672bSMarco Elver
233905e672bSMarco ElverImplementation Details
234905e672bSMarco Elver----------------------
235905e672bSMarco Elver
236e7325b77SMarco ElverKCSAN relies on observing that two accesses happen concurrently. Crucially, we
237e7325b77SMarco Elverwant to (a) increase the chances of observing races (especially for races that
238e7325b77SMarco Elvermanifest rarely), and (b) be able to actually observe them. We can accomplish
239e7325b77SMarco Elver(a) by injecting various delays, and (b) by using address watchpoints (or
240e7325b77SMarco Elverbreakpoints).
241e7325b77SMarco Elver
242e7325b77SMarco ElverIf we deliberately stall a memory access, while we have a watchpoint for its
243e7325b77SMarco Elveraddress set up, and then observe the watchpoint to fire, two accesses to the
244e7325b77SMarco Elversame address just raced. Using hardware watchpoints, this is the approach taken
245e7325b77SMarco Elverin `DataCollider
246905e672bSMarco Elver<http://usenix.org/legacy/events/osdi10/tech/full_papers/Erickson.pdf>`_.
247905e672bSMarco ElverUnlike DataCollider, KCSAN does not use hardware watchpoints, but instead
248e7325b77SMarco Elverrelies on compiler instrumentation and "soft watchpoints".
249905e672bSMarco Elver
250e7325b77SMarco ElverIn KCSAN, watchpoints are implemented using an efficient encoding that stores
251e7325b77SMarco Elveraccess type, size, and address in a long; the benefits of using "soft
252e7325b77SMarco Elverwatchpoints" are portability and greater flexibility. KCSAN then relies on the
253e7325b77SMarco Elvercompiler instrumenting plain accesses. For each instrumented plain access:
254905e672bSMarco Elver
255905e672bSMarco Elver1. Check if a matching watchpoint exists; if yes, and at least one access is a
256905e672bSMarco Elver   write, then we encountered a racing access.
257905e672bSMarco Elver
258905e672bSMarco Elver2. Periodically, if no matching watchpoint exists, set up a watchpoint and
259e7325b77SMarco Elver   stall for a small randomized delay.
260905e672bSMarco Elver
261905e672bSMarco Elver3. Also check the data value before the delay, and re-check the data value
262905e672bSMarco Elver   after delay; if the values mismatch, we infer a race of unknown origin.
263905e672bSMarco Elver
264e7325b77SMarco ElverTo detect data races between plain and marked accesses, KCSAN also annotates
265e7325b77SMarco Elvermarked accesses, but only to check if a watchpoint exists; i.e. KCSAN never
266e7325b77SMarco Elversets up a watchpoint on marked accesses. By never setting up watchpoints for
267e7325b77SMarco Elvermarked operations, if all accesses to a variable that is accessed concurrently
268e7325b77SMarco Elverare properly marked, KCSAN will never trigger a watchpoint and therefore never
269e7325b77SMarco Elverreport the accesses.
270905e672bSMarco Elver
271*82eb6911SMarco ElverModeling Weak Memory
272*82eb6911SMarco Elver~~~~~~~~~~~~~~~~~~~~
273*82eb6911SMarco Elver
274*82eb6911SMarco ElverKCSAN's approach to detecting data races due to missing memory barriers is
275*82eb6911SMarco Elverbased on modeling access reordering (with ``CONFIG_KCSAN_WEAK_MEMORY=y``).
276*82eb6911SMarco ElverEach plain memory access for which a watchpoint is set up, is also selected for
277*82eb6911SMarco Elversimulated reordering within the scope of its function (at most 1 in-flight
278*82eb6911SMarco Elveraccess).
279*82eb6911SMarco Elver
280*82eb6911SMarco ElverOnce an access has been selected for reordering, it is checked along every
281*82eb6911SMarco Elverother access until the end of the function scope. If an appropriate memory
282*82eb6911SMarco Elverbarrier is encountered, the access will no longer be considered for simulated
283*82eb6911SMarco Elverreordering.
284*82eb6911SMarco Elver
285*82eb6911SMarco ElverWhen the result of a memory operation should be ordered by a barrier, KCSAN can
286*82eb6911SMarco Elverthen detect data races where the conflict only occurs as a result of a missing
287*82eb6911SMarco Elverbarrier. Consider the example::
288*82eb6911SMarco Elver
289*82eb6911SMarco Elver    int x, flag;
290*82eb6911SMarco Elver    void T1(void)
291*82eb6911SMarco Elver    {
292*82eb6911SMarco Elver        x = 1;                  // data race!
293*82eb6911SMarco Elver        WRITE_ONCE(flag, 1);    // correct: smp_store_release(&flag, 1)
294*82eb6911SMarco Elver    }
295*82eb6911SMarco Elver    void T2(void)
296*82eb6911SMarco Elver    {
297*82eb6911SMarco Elver        while (!READ_ONCE(flag));   // correct: smp_load_acquire(&flag)
298*82eb6911SMarco Elver        ... = x;                    // data race!
299*82eb6911SMarco Elver    }
300*82eb6911SMarco Elver
301*82eb6911SMarco ElverWhen weak memory modeling is enabled, KCSAN can consider ``x`` in ``T1`` for
302*82eb6911SMarco Elversimulated reordering. After the write of ``flag``, ``x`` is again checked for
303*82eb6911SMarco Elverconcurrent accesses: because ``T2`` is able to proceed after the write of
304*82eb6911SMarco Elver``flag``, a data race is detected. With the correct barriers in place, ``x``
305*82eb6911SMarco Elverwould not be considered for reordering after the proper release of ``flag``,
306*82eb6911SMarco Elverand no data race would be detected.
307*82eb6911SMarco Elver
308*82eb6911SMarco ElverDeliberate trade-offs in complexity but also practical limitations mean only a
309*82eb6911SMarco Elversubset of data races due to missing memory barriers can be detected. With
310*82eb6911SMarco Elvercurrently available compiler support, the implementation is limited to modeling
311*82eb6911SMarco Elverthe effects of "buffering" (delaying accesses), since the runtime cannot
312*82eb6911SMarco Elver"prefetch" accesses. Also recall that watchpoints are only set up for plain
313*82eb6911SMarco Elveraccesses, and the only access type for which KCSAN simulates reordering. This
314*82eb6911SMarco Elvermeans reordering of marked accesses is not modeled.
315*82eb6911SMarco Elver
316*82eb6911SMarco ElverA consequence of the above is that acquire operations do not require barrier
317*82eb6911SMarco Elverinstrumentation (no prefetching). Furthermore, marked accesses introducing
318*82eb6911SMarco Elveraddress or control dependencies do not require special handling (the marked
319*82eb6911SMarco Elveraccess cannot be reordered, later dependent accesses cannot be prefetched).
320*82eb6911SMarco Elver
321905e672bSMarco ElverKey Properties
322905e672bSMarco Elver~~~~~~~~~~~~~~
323905e672bSMarco Elver
324e7325b77SMarco Elver1. **Memory Overhead:**  The overall memory overhead is only a few MiB
325e7325b77SMarco Elver   depending on configuration. The current implementation uses a small array of
326e7325b77SMarco Elver   longs to encode watchpoint information, which is negligible.
327905e672bSMarco Elver
328905e672bSMarco Elver2. **Performance Overhead:** KCSAN's runtime aims to be minimal, using an
329905e672bSMarco Elver   efficient watchpoint encoding that does not require acquiring any shared
330905e672bSMarco Elver   locks in the fast-path. For kernel boot on a system with 8 CPUs:
331905e672bSMarco Elver
332905e672bSMarco Elver   - 5.0x slow-down with the default KCSAN config;
333905e672bSMarco Elver   - 2.8x slow-down from runtime fast-path overhead only (set very large
334905e672bSMarco Elver     ``KCSAN_SKIP_WATCH`` and unset ``KCSAN_SKIP_WATCH_RANDOMIZE``).
335905e672bSMarco Elver
336905e672bSMarco Elver3. **Annotation Overheads:** Minimal annotations are required outside the KCSAN
337905e672bSMarco Elver   runtime. As a result, maintenance overheads are minimal as the kernel
338905e672bSMarco Elver   evolves.
339905e672bSMarco Elver
340905e672bSMarco Elver4. **Detects Racy Writes from Devices:** Due to checking data values upon
341905e672bSMarco Elver   setting up watchpoints, racy writes from devices can also be detected.
342905e672bSMarco Elver
343*82eb6911SMarco Elver5. **Memory Ordering:** KCSAN is aware of only a subset of LKMM ordering rules;
344*82eb6911SMarco Elver   this may result in missed data races (false negatives).
345905e672bSMarco Elver
346905e672bSMarco Elver6. **Analysis Accuracy:** For observed executions, due to using a sampling
347905e672bSMarco Elver   strategy, the analysis is *unsound* (false negatives possible), but aims to
348905e672bSMarco Elver   be complete (no false positives).
349905e672bSMarco Elver
350905e672bSMarco ElverAlternatives Considered
351905e672bSMarco Elver-----------------------
352905e672bSMarco Elver
353e7325b77SMarco ElverAn alternative data race detection approach for the kernel can be found in the
354905e672bSMarco Elver`Kernel Thread Sanitizer (KTSAN) <https://github.com/google/ktsan/wiki>`_.
355905e672bSMarco ElverKTSAN is a happens-before data race detector, which explicitly establishes the
356905e672bSMarco Elverhappens-before order between memory operations, which can then be used to
357e7325b77SMarco Elverdetermine data races as defined in `Data Races`_.
358e7325b77SMarco Elver
359e7325b77SMarco ElverTo build a correct happens-before relation, KTSAN must be aware of all ordering
360e7325b77SMarco Elverrules of the LKMM and synchronization primitives. Unfortunately, any omission
361e7325b77SMarco Elverleads to large numbers of false positives, which is especially detrimental in
362e7325b77SMarco Elverthe context of the kernel which includes numerous custom synchronization
363e7325b77SMarco Elvermechanisms. To track the happens-before relation, KTSAN's implementation
364e7325b77SMarco Elverrequires metadata for each memory location (shadow memory), which for each page
365e7325b77SMarco Elvercorresponds to 4 pages of shadow memory, and can translate into overhead of
366e7325b77SMarco Elvertens of GiB on a large system.
367