Skip to content

Commit 8e1d58a

Browse files
committed
Merge branch 'for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu into locking/kcsan
Pull the KCSAN subsystem from Paul E. McKenney: "This pull request contains base kernel concurrency sanitizer (KCSAN) enablement for x86, courtesy of Marco Elver. KCSAN is a sampling watchpoint-based data-race detector, and is documented in Documentation/dev-tools/kcsan.rst. KCSAN was announced in September, and much feedback has since been incorporated: http://lkml.kernel.org/r/CANpmjNPJ_bHjfLZCAPV23AXFfiPiyXXqqu72n6TgWzb2Gnu1eA@mail.gmail.com The data races located thus far have resulted in a number of fixes: https://github.com/google/ktsan/wiki/KCSAN#upstream-fixes-of-data-races-found-by-kcsan Additional information may be found here: https://lore.kernel.org/lkml/20191114180303.66955-1-elver@google.com/ " Signed-off-by: Ingo Molnar <mingo@kernel.org>
2 parents af42d34 + 40d0411 commit 8e1d58a

45 files changed

Lines changed: 2623 additions & 206 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/dev-tools/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ whole; patches welcome!
2121
kasan
2222
ubsan
2323
kmemleak
24+
kcsan
2425
gdb-kernel-debugging
2526
kgdb
2627
kselftest

‎Documentation/dev-tools/kcsan.rst‎

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
The Kernel Concurrency Sanitizer (KCSAN)
2+
========================================
3+
4+
Overview
5+
--------
6+
7+
*Kernel Concurrency Sanitizer (KCSAN)* is a dynamic data race detector for
8+
kernel space. KCSAN is a sampling watchpoint-based data race detector. Key
9+
priorities in KCSAN's design are lack of false positives, scalability, and
10+
simplicity. More details can be found in `Implementation Details`_.
11+
12+
KCSAN uses compile-time instrumentation to instrument memory accesses. KCSAN is
13+
supported in both GCC and Clang. With GCC it requires version 7.3.0 or later.
14+
With Clang it requires version 7.0.0 or later.
15+
16+
Usage
17+
-----
18+
19+
To enable KCSAN configure kernel with::
20+
21+
CONFIG_KCSAN = y
22+
23+
KCSAN provides several other configuration options to customize behaviour (see
24+
their respective help text for more info).
25+
26+
Error reports
27+
~~~~~~~~~~~~~
28+
29+
A typical data race report looks like this::
30+
31+
==================================================================
32+
BUG: KCSAN: data-race in generic_permission / kernfs_refresh_inode
33+
34+
write to 0xffff8fee4c40700c of 4 bytes by task 175 on cpu 4:
35+
kernfs_refresh_inode+0x70/0x170
36+
kernfs_iop_permission+0x4f/0x90
37+
inode_permission+0x190/0x200
38+
link_path_walk.part.0+0x503/0x8e0
39+
path_lookupat.isra.0+0x69/0x4d0
40+
filename_lookup+0x136/0x280
41+
user_path_at_empty+0x47/0x60
42+
vfs_statx+0x9b/0x130
43+
__do_sys_newlstat+0x50/0xb0
44+
__x64_sys_newlstat+0x37/0x50
45+
do_syscall_64+0x85/0x260
46+
entry_SYSCALL_64_after_hwframe+0x44/0xa9
47+
48+
read to 0xffff8fee4c40700c of 4 bytes by task 166 on cpu 6:
49+
generic_permission+0x5b/0x2a0
50+
kernfs_iop_permission+0x66/0x90
51+
inode_permission+0x190/0x200
52+
link_path_walk.part.0+0x503/0x8e0
53+
path_lookupat.isra.0+0x69/0x4d0
54+
filename_lookup+0x136/0x280
55+
user_path_at_empty+0x47/0x60
56+
do_faccessat+0x11a/0x390
57+
__x64_sys_access+0x3c/0x50
58+
do_syscall_64+0x85/0x260
59+
entry_SYSCALL_64_after_hwframe+0x44/0xa9
60+
61+
Reported by Kernel Concurrency Sanitizer on:
62+
CPU: 6 PID: 166 Comm: systemd-journal Not tainted 5.3.0-rc7+ #1
63+
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
64+
==================================================================
65+
66+
The header of the report provides a short summary of the functions involved in
67+
the race. It is followed by the access types and stack traces of the 2 threads
68+
involved in the data race.
69+
70+
The other less common type of data race report looks like this::
71+
72+
==================================================================
73+
BUG: KCSAN: data-race in e1000_clean_rx_irq+0x551/0xb10
74+
75+
race at unknown origin, with read to 0xffff933db8a2ae6c of 1 bytes by interrupt on cpu 0:
76+
e1000_clean_rx_irq+0x551/0xb10
77+
e1000_clean+0x533/0xda0
78+
net_rx_action+0x329/0x900
79+
__do_softirq+0xdb/0x2db
80+
irq_exit+0x9b/0xa0
81+
do_IRQ+0x9c/0xf0
82+
ret_from_intr+0x0/0x18
83+
default_idle+0x3f/0x220
84+
arch_cpu_idle+0x21/0x30
85+
do_idle+0x1df/0x230
86+
cpu_startup_entry+0x14/0x20
87+
rest_init+0xc5/0xcb
88+
arch_call_rest_init+0x13/0x2b
89+
start_kernel+0x6db/0x700
90+
91+
Reported by Kernel Concurrency Sanitizer on:
92+
CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.3.0-rc7+ #2
93+
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
94+
==================================================================
95+
96+
This report is generated where it was not possible to determine the other
97+
racing thread, but a race was inferred due to the data value of the watched
98+
memory location having changed. These can occur either due to missing
99+
instrumentation or e.g. DMA accesses.
100+
101+
Selective analysis
102+
~~~~~~~~~~~~~~~~~~
103+
104+
To disable KCSAN data race detection for an entire subsystem, add to the
105+
respective ``Makefile``::
106+
107+
KCSAN_SANITIZE := n
108+
109+
To disable KCSAN on a per-file basis, add to the ``Makefile``::
110+
111+
KCSAN_SANITIZE_file.o := n
112+
113+
KCSAN also understands the ``data_race(expr)`` annotation, which tells KCSAN
114+
that any data races due to accesses in ``expr`` should be ignored and resulting
115+
behaviour when encountering a data race is deemed safe.
116+
117+
debugfs
118+
~~~~~~~
119+
120+
* The file ``/sys/kernel/debug/kcsan`` can be read to get stats.
121+
122+
* KCSAN can be turned on or off by writing ``on`` or ``off`` to
123+
``/sys/kernel/debug/kcsan``.
124+
125+
* Writing ``!some_func_name`` to ``/sys/kernel/debug/kcsan`` adds
126+
``some_func_name`` to the report filter list, which (by default) blacklists
127+
reporting data races where either one of the top stackframes are a function
128+
in the list.
129+
130+
* Writing either ``blacklist`` or ``whitelist`` to ``/sys/kernel/debug/kcsan``
131+
changes the report filtering behaviour. For example, the blacklist feature
132+
can be used to silence frequently occurring data races; the whitelist feature
133+
can help with reproduction and testing of fixes.
134+
135+
Data Races
136+
----------
137+
138+
Informally, two operations *conflict* if they access the same memory location,
139+
and at least one of them is a write operation. In an execution, two memory
140+
operations from different threads form a **data race** if they *conflict*, at
141+
least one of them is a *plain access* (non-atomic), and they are *unordered* in
142+
the "happens-before" order according to the `LKMM
143+
<../../tools/memory-model/Documentation/explanation.txt>`_.
144+
145+
Relationship with the Linux Kernel Memory Model (LKMM)
146+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147+
148+
The LKMM defines the propagation and ordering rules of various memory
149+
operations, which gives developers the ability to reason about concurrent code.
150+
Ultimately this allows to determine the possible executions of concurrent code,
151+
and if that code is free from data races.
152+
153+
KCSAN is aware of *atomic* accesses (``READ_ONCE``, ``WRITE_ONCE``,
154+
``atomic_*``, etc.), but is oblivious of any ordering guarantees. In other
155+
words, KCSAN assumes that as long as a plain access is not observed to race
156+
with another conflicting access, memory operations are correctly ordered.
157+
158+
This means that KCSAN will not report *potential* data races due to missing
159+
memory ordering. If, however, missing memory ordering (that is observable with
160+
a particular compiler and architecture) leads to an observable data race (e.g.
161+
entering a critical section erroneously), KCSAN would report the resulting
162+
data race.
163+
164+
Race conditions vs. data races
165+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166+
167+
Race conditions are logic bugs, where unexpected interleaving of racing
168+
concurrent operations result in an erroneous state.
169+
170+
Data races on the other hand are defined at the *memory model/language level*.
171+
Many data races are also harmful race conditions, which a tool like KCSAN
172+
reports! However, not all data races are race conditions and vice-versa.
173+
KCSAN's intent is to report data races according to the LKMM. A data race
174+
detector can only work at the memory model/language level.
175+
176+
Deeper analysis, to find high-level race conditions only, requires conveying
177+
the intended kernel logic to a tool. This requires (1) the developer writing a
178+
specification or model of their code, and then (2) the tool verifying that the
179+
implementation matches. This has been done for small bits of code using model
180+
checkers and other formal methods, but does not scale to the level of what can
181+
be covered with a dynamic analysis based data race detector such as KCSAN.
182+
183+
For reasons outlined in this `article <https://lwn.net/Articles/793253/>`_,
184+
data races can be much more subtle, but can cause no less harm than high-level
185+
race conditions.
186+
187+
Implementation Details
188+
----------------------
189+
190+
The general approach is inspired by `DataCollider
191+
<http://usenix.org/legacy/events/osdi10/tech/full_papers/Erickson.pdf>`_.
192+
Unlike DataCollider, KCSAN does not use hardware watchpoints, but instead
193+
relies on compiler instrumentation. Watchpoints are implemented using an
194+
efficient encoding that stores access type, size, and address in a long; the
195+
benefits of using "soft watchpoints" are portability and greater flexibility in
196+
limiting which accesses trigger a watchpoint.
197+
198+
More specifically, KCSAN requires instrumenting plain (unmarked, non-atomic)
199+
memory operations; for each instrumented plain access:
200+
201+
1. Check if a matching watchpoint exists; if yes, and at least one access is a
202+
write, then we encountered a racing access.
203+
204+
2. Periodically, if no matching watchpoint exists, set up a watchpoint and
205+
stall for a small delay.
206+
207+
3. Also check the data value before the delay, and re-check the data value
208+
after delay; if the values mismatch, we infer a race of unknown origin.
209+
210+
To detect data races between plain and atomic memory operations, KCSAN also
211+
annotates atomic accesses, but only to check if a watchpoint exists
212+
(``kcsan_check_atomic_*``); i.e. KCSAN never sets up a watchpoint on atomic
213+
accesses.
214+
215+
Key Properties
216+
~~~~~~~~~~~~~~
217+
218+
1. **Memory Overhead:** The current implementation uses a small array of longs
219+
to encode watchpoint information, which is negligible.
220+
221+
2. **Performance Overhead:** KCSAN's runtime aims to be minimal, using an
222+
efficient watchpoint encoding that does not require acquiring any shared
223+
locks in the fast-path. For kernel boot on a system with 8 CPUs:
224+
225+
- 5.0x slow-down with the default KCSAN config;
226+
- 2.8x slow-down from runtime fast-path overhead only (set very large
227+
``KCSAN_SKIP_WATCH`` and unset ``KCSAN_SKIP_WATCH_RANDOMIZE``).
228+
229+
3. **Annotation Overheads:** Minimal annotations are required outside the KCSAN
230+
runtime. As a result, maintenance overheads are minimal as the kernel
231+
evolves.
232+
233+
4. **Detects Racy Writes from Devices:** Due to checking data values upon
234+
setting up watchpoints, racy writes from devices can also be detected.
235+
236+
5. **Memory Ordering:** KCSAN is *not* explicitly aware of the LKMM's ordering
237+
rules; this may result in missed data races (false negatives).
238+
239+
6. **Analysis Accuracy:** For observed executions, due to using a sampling
240+
strategy, the analysis is *unsound* (false negatives possible), but aims to
241+
be complete (no false positives).
242+
243+
Alternatives Considered
244+
-----------------------
245+
246+
An alternative data race detection approach for the kernel can be found in
247+
`Kernel Thread Sanitizer (KTSAN) <https://github.com/google/ktsan/wiki>`_.
248+
KTSAN is a happens-before data race detector, which explicitly establishes the
249+
happens-before order between memory operations, which can then be used to
250+
determine data races as defined in `Data Races`_. To build a correct
251+
happens-before relation, KTSAN must be aware of all ordering rules of the LKMM
252+
and synchronization primitives. Unfortunately, any omission leads to false
253+
positives, which is especially important in the context of the kernel which
254+
includes numerous custom synchronization mechanisms. Furthermore, KTSAN's
255+
implementation requires metadata for each memory location (shadow memory);
256+
currently, for each page, KTSAN requires 4 pages of shadow memory.

‎MAINTAINERS‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8850,6 +8850,17 @@ F: Documentation/kbuild/kconfig*
88508850
F: scripts/kconfig/
88518851
F: scripts/Kconfig.include
88528852

8853+
KCSAN
8854+
M: Marco Elver <elver@google.com>
8855+
R: Dmitry Vyukov <dvyukov@google.com>
8856+
L: kasan-dev@googlegroups.com
8857+
S: Maintained
8858+
F: Documentation/dev-tools/kcsan.rst
8859+
F: include/linux/kcsan*.h
8860+
F: kernel/kcsan/
8861+
F: lib/Kconfig.kcsan
8862+
F: scripts/Makefile.kcsan
8863+
88538864
KDUMP
88548865
M: Dave Young <dyoung@redhat.com>
88558866
M: Baoquan He <bhe@redhat.com>

‎Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
478478

479479
export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
480480
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
481-
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN
481+
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_KCSAN
482482
export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
483483
export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
484484
export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
@@ -900,6 +900,7 @@ endif
900900
include scripts/Makefile.kasan
901901
include scripts/Makefile.extrawarn
902902
include scripts/Makefile.ubsan
903+
include scripts/Makefile.kcsan
903904

904905
# Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments
905906
KBUILD_CPPFLAGS += $(KCPPFLAGS)

‎arch/x86/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ config X86
226226
select VIRT_TO_BUS
227227
select X86_FEATURE_NAMES if PROC_FS
228228
select PROC_PID_ARCH_STATUS if PROC_FS
229+
select HAVE_ARCH_KCSAN if X86_64
229230

230231
config INSTRUCTION_DECODER
231232
def_bool y

‎arch/x86/boot/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
# Changed by many, many contributors over the years.
1010
#
1111

12+
# Sanitizer runtimes are unavailable and cannot be linked for early boot code.
1213
KASAN_SANITIZE := n
14+
KCSAN_SANITIZE := n
1315
OBJECT_FILES_NON_STANDARD := y
1416

1517
# Kernel does not boot with kcov instrumentation here.

‎arch/x86/boot/compressed/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
# (see scripts/Makefile.lib size_append)
1818
# compressed vmlinux.bin.all + u32 size of vmlinux.bin.all
1919

20+
# Sanitizer runtimes are unavailable and cannot be linked for early boot code.
2021
KASAN_SANITIZE := n
22+
KCSAN_SANITIZE := n
2123
OBJECT_FILES_NON_STANDARD := y
2224

2325
# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.

‎arch/x86/entry/vdso/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ ARCH_REL_TYPE_ABS += R_386_GLOB_DAT|R_386_JMP_SLOT|R_386_RELATIVE
1010
include $(srctree)/lib/vdso/Makefile
1111

1212
KBUILD_CFLAGS += $(DISABLE_LTO)
13+
14+
# Sanitizer runtimes are unavailable and cannot be linked here.
1315
KASAN_SANITIZE := n
1416
UBSAN_SANITIZE := n
17+
KCSAN_SANITIZE := n
1518
OBJECT_FILES_NON_STANDARD := y
1619

1720
# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.

‎arch/x86/include/asm/bitops.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ arch_test_and_change_bit(long nr, volatile unsigned long *addr)
201201
return GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, c, "Ir", nr);
202202
}
203203

204-
static __always_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
204+
static __no_kcsan_or_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
205205
{
206+
/*
207+
* Because this is a plain access, we need to disable KCSAN here to
208+
* avoid double instrumentation via instrumented bitops.
209+
*/
206210
return ((1UL << (nr & (BITS_PER_LONG-1))) &
207211
(addr[nr >> _BITOPS_LONG_SHIFT])) != 0;
208212
}

‎arch/x86/kernel/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ KASAN_SANITIZE_dumpstack_$(BITS).o := n
2828
KASAN_SANITIZE_stacktrace.o := n
2929
KASAN_SANITIZE_paravirt.o := n
3030

31+
# With some compiler versions the generated code results in boot hangs, caused
32+
# by several compilation units. To be safe, disable all instrumentation.
33+
KCSAN_SANITIZE := n
34+
3135
OBJECT_FILES_NON_STANDARD_relocate_kernel_$(BITS).o := y
3236
OBJECT_FILES_NON_STANDARD_test_nx.o := y
3337
OBJECT_FILES_NON_STANDARD_paravirt_patch.o := y

0 commit comments

Comments
 (0)