Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 1 | The Kernel Concurrency Sanitizer (KCSAN) |
| 2 | ======================================== |
| 3 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 4 | The Kernel Concurrency Sanitizer (KCSAN) is a dynamic race detector, which |
| 5 | relies on compile-time instrumentation, and uses a watchpoint-based sampling |
| 6 | approach to detect races. KCSAN's primary purpose is to detect `data races`_. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 7 | |
| 8 | Usage |
| 9 | ----- |
| 10 | |
Marco Elver | e68dcd8 | 2020-06-18 11:31:16 +0200 | [diff] [blame] | 11 | KCSAN is supported by both GCC and Clang. With GCC we require version 11 or |
| 12 | later, and with Clang also require version 11 or later. |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 13 | |
| 14 | To enable KCSAN configure the kernel with:: |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 15 | |
| 16 | CONFIG_KCSAN = y |
| 17 | |
| 18 | KCSAN provides several other configuration options to customize behaviour (see |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 19 | the respective help text in ``lib/Kconfig.kcsan`` for more info). |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 20 | |
| 21 | Error reports |
| 22 | ~~~~~~~~~~~~~ |
| 23 | |
| 24 | A typical data race report looks like this:: |
| 25 | |
| 26 | ================================================================== |
| 27 | BUG: KCSAN: data-race in generic_permission / kernfs_refresh_inode |
| 28 | |
| 29 | write to 0xffff8fee4c40700c of 4 bytes by task 175 on cpu 4: |
| 30 | kernfs_refresh_inode+0x70/0x170 |
| 31 | kernfs_iop_permission+0x4f/0x90 |
| 32 | inode_permission+0x190/0x200 |
| 33 | link_path_walk.part.0+0x503/0x8e0 |
| 34 | path_lookupat.isra.0+0x69/0x4d0 |
| 35 | filename_lookup+0x136/0x280 |
| 36 | user_path_at_empty+0x47/0x60 |
| 37 | vfs_statx+0x9b/0x130 |
| 38 | __do_sys_newlstat+0x50/0xb0 |
| 39 | __x64_sys_newlstat+0x37/0x50 |
| 40 | do_syscall_64+0x85/0x260 |
| 41 | entry_SYSCALL_64_after_hwframe+0x44/0xa9 |
| 42 | |
| 43 | read to 0xffff8fee4c40700c of 4 bytes by task 166 on cpu 6: |
| 44 | generic_permission+0x5b/0x2a0 |
| 45 | kernfs_iop_permission+0x66/0x90 |
| 46 | inode_permission+0x190/0x200 |
| 47 | link_path_walk.part.0+0x503/0x8e0 |
| 48 | path_lookupat.isra.0+0x69/0x4d0 |
| 49 | filename_lookup+0x136/0x280 |
| 50 | user_path_at_empty+0x47/0x60 |
| 51 | do_faccessat+0x11a/0x390 |
| 52 | __x64_sys_access+0x3c/0x50 |
| 53 | do_syscall_64+0x85/0x260 |
| 54 | entry_SYSCALL_64_after_hwframe+0x44/0xa9 |
| 55 | |
| 56 | Reported by Kernel Concurrency Sanitizer on: |
| 57 | CPU: 6 PID: 166 Comm: systemd-journal Not tainted 5.3.0-rc7+ #1 |
| 58 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014 |
| 59 | ================================================================== |
| 60 | |
| 61 | The header of the report provides a short summary of the functions involved in |
| 62 | the race. It is followed by the access types and stack traces of the 2 threads |
| 63 | involved in the data race. |
| 64 | |
| 65 | The other less common type of data race report looks like this:: |
| 66 | |
| 67 | ================================================================== |
| 68 | BUG: KCSAN: data-race in e1000_clean_rx_irq+0x551/0xb10 |
| 69 | |
| 70 | race at unknown origin, with read to 0xffff933db8a2ae6c of 1 bytes by interrupt on cpu 0: |
| 71 | e1000_clean_rx_irq+0x551/0xb10 |
| 72 | e1000_clean+0x533/0xda0 |
| 73 | net_rx_action+0x329/0x900 |
| 74 | __do_softirq+0xdb/0x2db |
| 75 | irq_exit+0x9b/0xa0 |
| 76 | do_IRQ+0x9c/0xf0 |
| 77 | ret_from_intr+0x0/0x18 |
| 78 | default_idle+0x3f/0x220 |
| 79 | arch_cpu_idle+0x21/0x30 |
| 80 | do_idle+0x1df/0x230 |
| 81 | cpu_startup_entry+0x14/0x20 |
| 82 | rest_init+0xc5/0xcb |
| 83 | arch_call_rest_init+0x13/0x2b |
| 84 | start_kernel+0x6db/0x700 |
| 85 | |
| 86 | Reported by Kernel Concurrency Sanitizer on: |
| 87 | CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.3.0-rc7+ #2 |
| 88 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014 |
| 89 | ================================================================== |
| 90 | |
| 91 | This report is generated where it was not possible to determine the other |
| 92 | racing thread, but a race was inferred due to the data value of the watched |
| 93 | memory location having changed. These can occur either due to missing |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 94 | instrumentation or e.g. DMA accesses. These reports will only be generated if |
| 95 | ``CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=y`` (selected by default). |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 96 | |
| 97 | Selective analysis |
| 98 | ~~~~~~~~~~~~~~~~~~ |
| 99 | |
Marco Elver | 7161177 | 2019-12-12 01:07:08 +0100 | [diff] [blame] | 100 | It may be desirable to disable data race detection for specific accesses, |
| 101 | functions, compilation units, or entire subsystems. For static blacklisting, |
| 102 | the below options are available: |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 103 | |
Marco Elver | 7161177 | 2019-12-12 01:07:08 +0100 | [diff] [blame] | 104 | * KCSAN understands the ``data_race(expr)`` annotation, which tells KCSAN that |
| 105 | any data races due to accesses in ``expr`` should be ignored and resulting |
| 106 | behaviour when encountering a data race is deemed safe. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 107 | |
Marco Elver | 7161177 | 2019-12-12 01:07:08 +0100 | [diff] [blame] | 108 | * Disabling data race detection for entire functions can be accomplished by |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 109 | using the function attribute ``__no_kcsan``:: |
| 110 | |
| 111 | __no_kcsan |
| 112 | void foo(void) { |
| 113 | ... |
| 114 | |
| 115 | To dynamically limit for which functions to generate reports, see the |
| 116 | `DebugFS interface`_ blacklist/whitelist feature. |
| 117 | |
Marco Elver | 7161177 | 2019-12-12 01:07:08 +0100 | [diff] [blame] | 118 | * To disable data race detection for a particular compilation unit, add to the |
| 119 | ``Makefile``:: |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 120 | |
| 121 | KCSAN_SANITIZE_file.o := n |
| 122 | |
Marco Elver | 7161177 | 2019-12-12 01:07:08 +0100 | [diff] [blame] | 123 | * To disable data race detection for all compilation units listed in a |
| 124 | ``Makefile``, add to the respective ``Makefile``:: |
| 125 | |
| 126 | KCSAN_SANITIZE := n |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 127 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 128 | Furthermore, it is possible to tell KCSAN to show or hide entire classes of |
| 129 | data races, depending on preferences. These can be changed via the following |
| 130 | Kconfig options: |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 131 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 132 | * ``CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY``: If enabled and a conflicting write |
| 133 | is observed via a watchpoint, but the data value of the memory location was |
| 134 | observed to remain unchanged, do not report the data race. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 135 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 136 | * ``CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC``: Assume that plain aligned writes |
| 137 | up to word size are atomic by default. Assumes that such writes are not |
| 138 | subject to unsafe compiler optimizations resulting in data races. The option |
| 139 | causes KCSAN to not report data races due to conflicts where the only plain |
| 140 | accesses are aligned writes up to word size. |
| 141 | |
| 142 | DebugFS interface |
| 143 | ~~~~~~~~~~~~~~~~~ |
| 144 | |
| 145 | The file ``/sys/kernel/debug/kcsan`` provides the following interface: |
| 146 | |
| 147 | * Reading ``/sys/kernel/debug/kcsan`` returns various runtime statistics. |
| 148 | |
| 149 | * Writing ``on`` or ``off`` to ``/sys/kernel/debug/kcsan`` allows turning KCSAN |
| 150 | on or off, respectively. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 151 | |
| 152 | * Writing ``!some_func_name`` to ``/sys/kernel/debug/kcsan`` adds |
| 153 | ``some_func_name`` to the report filter list, which (by default) blacklists |
| 154 | reporting data races where either one of the top stackframes are a function |
| 155 | in the list. |
| 156 | |
| 157 | * Writing either ``blacklist`` or ``whitelist`` to ``/sys/kernel/debug/kcsan`` |
| 158 | changes the report filtering behaviour. For example, the blacklist feature |
| 159 | can be used to silence frequently occurring data races; the whitelist feature |
| 160 | can help with reproduction and testing of fixes. |
| 161 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 162 | Tuning performance |
| 163 | ~~~~~~~~~~~~~~~~~~ |
| 164 | |
| 165 | Core parameters that affect KCSAN's overall performance and bug detection |
| 166 | ability are exposed as kernel command-line arguments whose defaults can also be |
| 167 | changed via the corresponding Kconfig options. |
| 168 | |
| 169 | * ``kcsan.skip_watch`` (``CONFIG_KCSAN_SKIP_WATCH``): Number of per-CPU memory |
| 170 | operations to skip, before another watchpoint is set up. Setting up |
| 171 | watchpoints more frequently will result in the likelihood of races to be |
| 172 | observed to increase. This parameter has the most significant impact on |
| 173 | overall system performance and race detection ability. |
| 174 | |
| 175 | * ``kcsan.udelay_task`` (``CONFIG_KCSAN_UDELAY_TASK``): For tasks, the |
| 176 | microsecond delay to stall execution after a watchpoint has been set up. |
| 177 | Larger values result in the window in which we may observe a race to |
| 178 | increase. |
| 179 | |
| 180 | * ``kcsan.udelay_interrupt`` (``CONFIG_KCSAN_UDELAY_INTERRUPT``): For |
| 181 | interrupts, the microsecond delay to stall execution after a watchpoint has |
| 182 | been set up. Interrupts have tighter latency requirements, and their delay |
| 183 | should generally be smaller than the one chosen for tasks. |
| 184 | |
| 185 | They may be tweaked at runtime via ``/sys/module/kcsan/parameters/``. |
| 186 | |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 187 | Data Races |
| 188 | ---------- |
| 189 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 190 | In an execution, two memory accesses form a *data race* if they *conflict*, |
| 191 | they happen concurrently in different threads, and at least one of them is a |
| 192 | *plain access*; they *conflict* if both access the same memory location, and at |
| 193 | least one is a write. For a more thorough discussion and definition, see `"Plain |
| 194 | Accesses and Data Races" in the LKMM`_. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 195 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 196 | .. _"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 |
| 197 | |
| 198 | Relationship with the Linux-Kernel Memory Consistency Model (LKMM) |
| 199 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 200 | |
| 201 | The LKMM defines the propagation and ordering rules of various memory |
| 202 | operations, which gives developers the ability to reason about concurrent code. |
| 203 | Ultimately this allows to determine the possible executions of concurrent code, |
| 204 | and if that code is free from data races. |
| 205 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 206 | KCSAN is aware of *marked atomic operations* (``READ_ONCE``, ``WRITE_ONCE``, |
| 207 | ``atomic_*``, etc.), but is oblivious of any ordering guarantees and simply |
| 208 | assumes that memory barriers are placed correctly. In other words, KCSAN |
| 209 | assumes that as long as a plain access is not observed to race with another |
| 210 | conflicting access, memory operations are correctly ordered. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 211 | |
| 212 | This means that KCSAN will not report *potential* data races due to missing |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 213 | memory ordering. Developers should therefore carefully consider the required |
| 214 | memory ordering requirements that remain unchecked. If, however, missing |
| 215 | memory ordering (that is observable with a particular compiler and |
| 216 | architecture) leads to an observable data race (e.g. entering a critical |
| 217 | section erroneously), KCSAN would report the resulting data race. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 218 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 219 | Race Detection Beyond Data Races |
| 220 | -------------------------------- |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 221 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 222 | For code with complex concurrency design, race-condition bugs may not always |
| 223 | manifest as data races. Race conditions occur if concurrently executing |
| 224 | operations result in unexpected system behaviour. On the other hand, data races |
| 225 | are defined at the C-language level. The following macros can be used to check |
| 226 | properties of concurrent code where bugs would not manifest as data races. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 227 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 228 | .. kernel-doc:: include/linux/kcsan-checks.h |
Marco Elver | d8949ef | 2020-03-25 17:41:58 +0100 | [diff] [blame] | 229 | :functions: ASSERT_EXCLUSIVE_WRITER ASSERT_EXCLUSIVE_WRITER_SCOPED |
| 230 | ASSERT_EXCLUSIVE_ACCESS ASSERT_EXCLUSIVE_ACCESS_SCOPED |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 231 | ASSERT_EXCLUSIVE_BITS |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 232 | |
| 233 | Implementation Details |
| 234 | ---------------------- |
| 235 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 236 | KCSAN relies on observing that two accesses happen concurrently. Crucially, we |
| 237 | want to (a) increase the chances of observing races (especially for races that |
| 238 | manifest rarely), and (b) be able to actually observe them. We can accomplish |
| 239 | (a) by injecting various delays, and (b) by using address watchpoints (or |
| 240 | breakpoints). |
| 241 | |
| 242 | If we deliberately stall a memory access, while we have a watchpoint for its |
| 243 | address set up, and then observe the watchpoint to fire, two accesses to the |
| 244 | same address just raced. Using hardware watchpoints, this is the approach taken |
| 245 | in `DataCollider |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 246 | <http://usenix.org/legacy/events/osdi10/tech/full_papers/Erickson.pdf>`_. |
| 247 | Unlike DataCollider, KCSAN does not use hardware watchpoints, but instead |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 248 | relies on compiler instrumentation and "soft watchpoints". |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 249 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 250 | In KCSAN, watchpoints are implemented using an efficient encoding that stores |
| 251 | access type, size, and address in a long; the benefits of using "soft |
| 252 | watchpoints" are portability and greater flexibility. KCSAN then relies on the |
| 253 | compiler instrumenting plain accesses. For each instrumented plain access: |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 254 | |
| 255 | 1. Check if a matching watchpoint exists; if yes, and at least one access is a |
| 256 | write, then we encountered a racing access. |
| 257 | |
| 258 | 2. Periodically, if no matching watchpoint exists, set up a watchpoint and |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 259 | stall for a small randomized delay. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 260 | |
| 261 | 3. Also check the data value before the delay, and re-check the data value |
| 262 | after delay; if the values mismatch, we infer a race of unknown origin. |
| 263 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 264 | To detect data races between plain and marked accesses, KCSAN also annotates |
| 265 | marked accesses, but only to check if a watchpoint exists; i.e. KCSAN never |
| 266 | sets up a watchpoint on marked accesses. By never setting up watchpoints for |
| 267 | marked operations, if all accesses to a variable that is accessed concurrently |
| 268 | are properly marked, KCSAN will never trigger a watchpoint and therefore never |
| 269 | report the accesses. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 270 | |
| 271 | Key Properties |
| 272 | ~~~~~~~~~~~~~~ |
| 273 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 274 | 1. **Memory Overhead:** The overall memory overhead is only a few MiB |
| 275 | depending on configuration. The current implementation uses a small array of |
| 276 | longs to encode watchpoint information, which is negligible. |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 277 | |
| 278 | 2. **Performance Overhead:** KCSAN's runtime aims to be minimal, using an |
| 279 | efficient watchpoint encoding that does not require acquiring any shared |
| 280 | locks in the fast-path. For kernel boot on a system with 8 CPUs: |
| 281 | |
| 282 | - 5.0x slow-down with the default KCSAN config; |
| 283 | - 2.8x slow-down from runtime fast-path overhead only (set very large |
| 284 | ``KCSAN_SKIP_WATCH`` and unset ``KCSAN_SKIP_WATCH_RANDOMIZE``). |
| 285 | |
| 286 | 3. **Annotation Overheads:** Minimal annotations are required outside the KCSAN |
| 287 | runtime. As a result, maintenance overheads are minimal as the kernel |
| 288 | evolves. |
| 289 | |
| 290 | 4. **Detects Racy Writes from Devices:** Due to checking data values upon |
| 291 | setting up watchpoints, racy writes from devices can also be detected. |
| 292 | |
| 293 | 5. **Memory Ordering:** KCSAN is *not* explicitly aware of the LKMM's ordering |
| 294 | rules; this may result in missed data races (false negatives). |
| 295 | |
| 296 | 6. **Analysis Accuracy:** For observed executions, due to using a sampling |
| 297 | strategy, the analysis is *unsound* (false negatives possible), but aims to |
| 298 | be complete (no false positives). |
| 299 | |
| 300 | Alternatives Considered |
| 301 | ----------------------- |
| 302 | |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 303 | An alternative data race detection approach for the kernel can be found in the |
Marco Elver | 905e672 | 2019-11-14 19:02:56 +0100 | [diff] [blame] | 304 | `Kernel Thread Sanitizer (KTSAN) <https://github.com/google/ktsan/wiki>`_. |
| 305 | KTSAN is a happens-before data race detector, which explicitly establishes the |
| 306 | happens-before order between memory operations, which can then be used to |
Marco Elver | e7325b7 | 2020-03-05 15:21:08 +0100 | [diff] [blame] | 307 | determine data races as defined in `Data Races`_. |
| 308 | |
| 309 | To build a correct happens-before relation, KTSAN must be aware of all ordering |
| 310 | rules of the LKMM and synchronization primitives. Unfortunately, any omission |
| 311 | leads to large numbers of false positives, which is especially detrimental in |
| 312 | the context of the kernel which includes numerous custom synchronization |
| 313 | mechanisms. To track the happens-before relation, KTSAN's implementation |
| 314 | requires metadata for each memory location (shadow memory), which for each page |
| 315 | corresponds to 4 pages of shadow memory, and can translate into overhead of |
| 316 | tens of GiB on a large system. |