Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
148 commits
Select commit Hold shift + click to select a range
dfd402a
kcsan: Add Kernel Concurrency Sanitizer infrastructure
melver Nov 14, 2019
c48981e
include/linux/compiler.h: Introduce data_race(expr) macro
melver Nov 14, 2019
905e672
kcsan: Add Documentation entry in dev-tools
melver Nov 14, 2019
5f5c971
objtool, kcsan: Add KCSAN runtime functions to whitelist
melver Nov 14, 2019
0ebba71
build, kcsan: Add KCSAN build exceptions
melver Nov 14, 2019
88ecd15
seqlock, kcsan: Add annotations for KCSAN
melver Nov 14, 2019
bf07132
seqlock: Require WRITE_ONCE surrounding raw_seqcount_barrier
melver Nov 14, 2019
e75a679
locking/atomics, kcsan: Add KCSAN instrumentation
melver Nov 14, 2019
40d0411
x86, kcsan: Enable KCSAN for x86
melver Nov 14, 2019
8e1d58a
Merge branch 'for-mingo' of git://git.kernel.org/pub/scm/linux/kernel…
Nov 19, 2019
5cbaefe
kcsan: Improve various small stylistic details
Nov 20, 2019
28336be
Merge tag 'v5.5-rc4' into locking/kcsan, to resolve conflicts
Dec 30, 2019
31c7ac3
Merge tag 'v5.5-rc5' into locking/kcsan, to resolve conflict
Jan 6, 2020
d47715f
kcsan, ubsan: Make KCSAN+UBSAN work together
melver Nov 19, 2019
c020395
asm-generic/atomic: Use __always_inline for pure wrappers
melver Nov 26, 2019
944bc9c
asm-generic/atomic: Use __always_inline for fallback wrappers
melver Nov 26, 2019
7161177
kcsan: Document static blacklisting options
melver Dec 12, 2019
e33f9a1
kcsan: Add __no_kcsan function attribute
melver Dec 12, 2019
c29a59e
x86/vdso: Enable sanitizers for vma.o
thejh Jan 6, 2020
837171f
Merge tag 'v5.5-rc7' into locking/kcsan, to refresh the tree
Jan 20, 2020
7add787
Merge branch 'kcsan.2020.01.07a' into locking/kcsan
Jan 24, 2020
8efbc51
x86/kexec: Do not reserve EFI setup_data in the kexec e820 table
daveyoung Feb 12, 2020
e2ac07c
x86/purgatory: Disable various profiling and sanitizing options
jwrdegoede Mar 17, 2020
e4160b2
x86/purgatory: Fail the build if purgatory.ro has missing symbols
jwrdegoede Mar 17, 2020
a4654e9
Merge branch 'x86/kdump' into locking/kcsan, to resolve conflicts
Mar 21, 2020
df10846
Merge branch 'linus' into locking/kcsan, to pick up fixes
Mar 21, 2020
5c36142
kcsan: Prefer __always_inline for fast-path
melver Jan 7, 2020
47144ec
kcsan: Show full access type in report
melver Jan 10, 2020
05f9a40
kcsan: Rate-limit reporting per data races
melver Jan 10, 2020
f1bc962
kcsan: Make KCSAN compatible with lockdep
melver Jan 15, 2020
ad4f8ee
kcsan: Address missing case with KCSAN_REPORT_VALUE_CHANGE_ONLY
melver Jan 29, 2020
36e4d4d
include/linux: Add instrumented.h infrastructure
melver Jan 21, 2020
ed8af2e
asm-generic, atomic-instrumented: Use generic instrumented.h
melver Jan 21, 2020
27f937c
asm-generic, kcsan: Add KCSAN instrumentation for bitops
melver Jan 21, 2020
d0ef4c3
iov_iter: Use generic instrumented.h
melver Jan 21, 2020
76d6f06
copy_to_user, copy_from_user: Use generic instrumented.h
melver Jan 21, 2020
7ad900d
kcsan: Add docbook header for data_race()
paulmckrcu Feb 3, 2020
1e6ee2f
kcsan: Add option to assume plain aligned writes up to word size are …
melver Feb 4, 2020
a249a73
kcsan: Clarify Kconfig option KCSAN_IGNORE_ATOMICS
melver Feb 4, 2020
8cfbb04
kcsan: Clean up the main KCSAN Kconfig option
melver Feb 4, 2020
ed95f95
kcsan: Fix 0-sized checks
melver Feb 5, 2020
d591ec3
kcsan: Introduce KCSAN_ACCESS_ASSERT access type
melver Feb 6, 2020
f97f713
kcsan: Introduce ASSERT_EXCLUSIVE_*() macros
melver Feb 6, 2020
a312013
kcsan: Add test to generate conflicts via debugfs
melver Feb 6, 2020
80d4c47
kcsan: Expose core configuration parameters as module params
melver Feb 7, 2020
3a5b45e
kcsan: Fix misreporting if concurrent races on same address
melver Feb 10, 2020
f0f6928
kcsan: Move interfaces that affects checks to kcsan-checks.h
melver Feb 11, 2020
b968a08
compiler.h, seqlock.h: Remove unnecessary kcsan.h includes
melver Feb 11, 2020
b738f61
kcsan: Introduce kcsan_value_change type
melver Feb 11, 2020
81af89e
kcsan: Add kcsan_set_access_mask() support
melver Feb 11, 2020
703b321
kcsan: Introduce ASSERT_EXCLUSIVE_BITS(var, mask)
melver Feb 11, 2020
f5d2313
kcsan, trace: Make KCSAN compatible with tracing
melver Feb 14, 2020
48b1fc1
kcsan: Add option to allow watcher interruptions
melver Feb 21, 2020
2402d0e
kcsan: Add option for verbose reporting
melver Feb 21, 2020
44656d3
kcsan: Add current->state to implicitly atomic accesses
melver Feb 25, 2020
e7b3410
kcsan: Fix a typo in a comment
Mar 5, 2020
e7325b7
kcsan: Update Documentation/dev-tools/kcsan.rst
melver Mar 5, 2020
1443b8c
kcsan: Update API documentation in kcsan-checks.h
melver Mar 5, 2020
3b02a05
Merge tag 'v5.7-rc1' into locking/kcsan, to resolve conflicts and ref…
Apr 13, 2020
135c087
kcsan: Introduce report access_info and other_info
melver Mar 18, 2020
6119418
kcsan: Avoid blocking producers in prepare_report()
melver Mar 18, 2020
757a4ce
kcsan: Add support for scoped accesses
melver Mar 25, 2020
9967683
objtool, kcsan: Add explicit check functions to uaccess whitelist
melver Mar 25, 2020
d8949ef
kcsan: Introduce scoped ASSERT_EXCLUSIVE macros
melver Mar 25, 2020
01b4ff5
kcsan: Move kcsan_{disable,enable}_current() to kcsan-checks.h
melver Mar 31, 2020
d071e91
kcsan: Change data_race() to no longer require marking racing accesses
melver Mar 31, 2020
f770ed1
kcsan: Fix function matching in report
melver Apr 10, 2020
cdb9b07
kcsan: Make reporting aware of KCSAN tests
melver Apr 10, 2020
eba9c44
Improve KCSAN documentation a bit
Apr 13, 2020
52785b6
kcsan: Use GFP_ATOMIC under spin lock
Apr 17, 2020
5099a72
checkpatch: Warn about data_race() without comment
melver Apr 1, 2020
19acd03
kcsan: Add __kcsan_{enable,disable}_current() variants
melver Apr 24, 2020
50a19ad
objtool, kcsan: Add kcsan_disable_current() and kcsan_enable_current_…
melver Apr 24, 2020
97a9474
Merge branch 'kcsan-for-tip' of git://git.kernel.org/pub/scm/linux/ke…
KAGA-KOKO May 8, 2020
140fd4a
x86/reboot/quirks: Add MacBook6,1 reboot quirk
mahiuchun Apr 25, 2020
de308d1
x86/apic: Make TSC deadline timer detection message visible
suryasaimadhu May 25, 2020
429ac8b
x86/split_lock: Add Icelake microserver and Tigerlake CPU models
Apr 30, 2020
f8a4bca
Merge branches 'x86/apic', 'x86/misc' and 'x86/splitlock' into x86/ur…
Jun 1, 2020
a75ca93
block/bio-integrity: don't free 'buf' if bio_integrity_add_page() failed
YuKuai-huawei Jun 1, 2020
fd2206e
io_uring: disallow close of ring itself
axboe Jun 2, 2020
be25d1b
x86/cpu: Add Sapphire Rapids CPU model number
aegl Jun 3, 2020
b91c8c4
lib/vdso: Force inlining of __cvdso_clock_gettime_common()
chleroy Apr 28, 2020
3232dd0
io_uring: fix {SQ,IO}POLL with unsupported opcodes
isilence Jun 3, 2020
25e72d1
io_uring: do build_open_how() only once
isilence Jun 3, 2020
ec65fea
io_uring: deduplicate io_openat{,2}_prep()
isilence Jun 3, 2020
d2b6f48
io_uring: move send/recv IOPOLL check into prep
isilence Jun 3, 2020
dddb3e2
io_uring: re-set iov base/len for buffer select retry
axboe Jun 4, 2020
6ac92fb
loop: Fix wrong masking of status flags
Jun 4, 2020
d24de76
block: remove the error argument to the block_bio_complete tracepoint
Jun 3, 2020
15b81ce
block: nr_sects_write(): Disable preemption on seqcount write
a-darwish Jun 3, 2020
48bc3cd
blktrace: use errno instead of bi_status
ChaitanayaKulkarni Jun 4, 2020
71df3fd
blktrace: fix endianness in get_pdu_int()
ChaitanayaKulkarni Jun 4, 2020
5aec598
blktrace: fix endianness for blk_log_remap()
ChaitanayaKulkarni Jun 4, 2020
d94ecfc
blk-mq: split out a __blk_mq_get_driver_tag helper
Jun 5, 2020
22f614b
blk-mq: fix blk_mq_all_tag_iter
Jun 5, 2020
efe68c1
io_uring: validate the full range of provided buffers for access
Jun 5, 2020
a8c73c1
io_uring: use kvfree() in io_sqe_buffer_register()
evdenis Jun 5, 2020
3af73b2
io_uring: don't derive close state from ->func
isilence Jun 8, 2020
ac45abc
io_uring: remove custom ->func handlers
isilence Jun 8, 2020
d4c81f3
io_uring: don't arm a timeout through work.func
isilence Jun 8, 2020
f5fa38c
io_wq: add per-wq work handler instead of per work
isilence Jun 8, 2020
21998a3
x86/speculation: Avoid force-disabling IBPB based on STIBP and enhanc…
asteinha May 19, 2020
dbbe2ad
x86/speculation: Prevent rogue cross-process SSBD shutdown
asteinha Jan 5, 2020
4d8df8c
x86/speculation: PR_SPEC_FORCE_DISABLE enforcement for indirect branc…
asteinha Jun 7, 2020
d8ad6d3
x86_64: Fix jiffies ODR violation
inglorion Jun 2, 2020
c7f3d43
clocksource: Remove obsolete ifdef
KAGA-KOKO Jun 6, 2020
72ce778
lib/vdso: Provide sanity check for cycles (again)
KAGA-KOKO Jun 6, 2020
7778d84
x86/vdso: Unbreak paravirt VDSO clocks
KAGA-KOKO Jun 6, 2020
c5b8562
io_uring: allow O_NONBLOCK async retry
axboe Jun 10, 2020
7cdaf58
io_uring: avoid whole io_wq_work copy for requests completed inline
Jun 10, 2020
405a5d2
io_uring: avoid unnecessary io_wq_work copy for fast poll feature
Jun 10, 2020
e697dee
io_uring: check file O_NONBLOCK state for accept
Jun 10, 2020
765dcd2
asm-generic/atomic: Use __always_inline for fallback wrappers
melver Nov 26, 2019
37f8173
locking/atomics: Flip fallbacks and instrumentation
Jan 24, 2020
108a585
nvme: do not call del_gendisk() on a disk that was never added
Jun 7, 2020
6acbd96
nvme-tcp: constify nvme_tcp_mq_ops and nvme_tcp_admin_mq_ops
rikardfalkeborn May 28, 2020
a40aae6
nvmet-tcp: constify nvmet_tcp_ops
Jun 1, 2020
c9c12e5
nvme-fc: don't call nvme_cleanup_cmd() for AENs
dwsuse May 29, 2020
b97120b
nvme-pci: use simple suspend when a HMB is enabled
Jun 3, 2020
819f7b8
nvmet: fail outstanding host posted AEN req
ChaitanayaKulkarni Jun 9, 2020
8d20319
pktcdvd: remove redundant initialization of variable ret
Jun 11, 2020
9a6a573
umem: remove redundant initialization of variable ret
Jun 11, 2020
65a6543
io_uring: fix io_kiocb.flags modification race in IOPOLL mode
Jun 11, 2020
37d1a04
Rebase locking/kcsan to locking/urgent
KAGA-KOKO Jun 11, 2020
ea91a1d
ubsan, kcsan: Don't combine sanitizer with kcov on clang
arndb May 21, 2020
17168f5
kcsan: Avoid inserting __tsan_func_entry/exit if possible
melver May 21, 2020
0e1aa5b
kcsan: Restrict supported compilers
melver May 21, 2020
75d75b7
kcsan: Support distinguishing volatile accesses
melver May 21, 2020
d31d4d6
kcsan: Pass option tsan-instrument-read-before-write to Clang
melver May 21, 2020
e3b779d
kcsan: Remove 'noinline' from __no_kcsan_or_inline
melver May 21, 2020
0bb9ab3
kcsan: Update Documentation to change supported compilers
melver May 21, 2020
44b97dc
compiler.h: Remove data_race() and unnecessary checks from {READ,WRIT…
melver May 21, 2020
95c094f
compiler.h: Avoid nested statement expression in data_race()
melver May 21, 2020
eb73876
compiler.h: Move function attributes to compiler_types.h
melver May 21, 2020
1f44328
compiler_types.h, kasan: Use __SANITIZE_ADDRESS__ instead of CONFIG_K…
melver May 21, 2020
92ac971
Merge tag 'timers-urgent-2020-06-11' of git://git.kernel.org/pub/scm/…
torvalds Jun 11, 2020
6a45a65
Merge tag 'x86-urgent-2020-06-11' of git://git.kernel.org/pub/scm/lin…
torvalds Jun 11, 2020
b359794
afs: Fix afs_store_data() to set mtime in new operation descriptor
dhowells Jun 11, 2020
a58dfea
Merge tag 'block-5.8-2020-06-11' of git://git.kernel.dk/linux-block
torvalds Jun 11, 2020
b961f8d
Merge tag 'io_uring-5.8-2020-06-11' of git://git.kernel.dk/linux-block
torvalds Jun 11, 2020
4e018b4
mm/memory-failure: prioritize prctl(PR_MCE_KILL) over vm.memory_failu…
nhoriguchi Jun 12, 2020
03151c6
mm/memory-failure: send SIGBUS(BUS_MCEERR_AR) only to current thread
nhoriguchi Jun 12, 2020
fce1aff
ocfs2: fix build failure when TCP/IP is disabled
tom-seewald Jun 12, 2020
b5265c8
lib/lzo: fix ambiguous encoding bug in lzo-rle
daverodgman Jun 12, 2020
8449d15
amdgpu: a NULL ->mm does not mean a thread is a kthread
Jun 12, 2020
b1a6274
Merge branch 'akpm' (patches from Andrew)
torvalds Jun 12, 2020
9716e57
Merge tag 'locking-urgent-2020-06-11' of git://git.kernel.org/pub/scm…
torvalds Jun 12, 2020
b791d1b
Merge tag 'locking-kcsan-2020-06-11' of git://git.kernel.org/pub/scm/…
torvalds Jun 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/dev-tools/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ whole; patches welcome!
kasan
ubsan
kmemleak
kcsan
gdb-kernel-debugging
kgdb
kselftest
Expand Down
321 changes: 321 additions & 0 deletions Documentation/dev-tools/kcsan.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
The Kernel Concurrency Sanitizer (KCSAN)
========================================

The Kernel Concurrency Sanitizer (KCSAN) is a dynamic race detector, which
relies on compile-time instrumentation, and uses a watchpoint-based sampling
approach to detect races. KCSAN's primary purpose is to detect `data races`_.

Usage
-----

KCSAN requires Clang version 11 or later.

To enable KCSAN configure the kernel with::

CONFIG_KCSAN = y

KCSAN provides several other configuration options to customize behaviour (see
the respective help text in ``lib/Kconfig.kcsan`` for more info).

Error reports
~~~~~~~~~~~~~

A typical data race report looks like this::

==================================================================
BUG: KCSAN: data-race in generic_permission / kernfs_refresh_inode

write to 0xffff8fee4c40700c of 4 bytes by task 175 on cpu 4:
kernfs_refresh_inode+0x70/0x170
kernfs_iop_permission+0x4f/0x90
inode_permission+0x190/0x200
link_path_walk.part.0+0x503/0x8e0
path_lookupat.isra.0+0x69/0x4d0
filename_lookup+0x136/0x280
user_path_at_empty+0x47/0x60
vfs_statx+0x9b/0x130
__do_sys_newlstat+0x50/0xb0
__x64_sys_newlstat+0x37/0x50
do_syscall_64+0x85/0x260
entry_SYSCALL_64_after_hwframe+0x44/0xa9

read to 0xffff8fee4c40700c of 4 bytes by task 166 on cpu 6:
generic_permission+0x5b/0x2a0
kernfs_iop_permission+0x66/0x90
inode_permission+0x190/0x200
link_path_walk.part.0+0x503/0x8e0
path_lookupat.isra.0+0x69/0x4d0
filename_lookup+0x136/0x280
user_path_at_empty+0x47/0x60
do_faccessat+0x11a/0x390
__x64_sys_access+0x3c/0x50
do_syscall_64+0x85/0x260
entry_SYSCALL_64_after_hwframe+0x44/0xa9

Reported by Kernel Concurrency Sanitizer on:
CPU: 6 PID: 166 Comm: systemd-journal Not tainted 5.3.0-rc7+ #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
==================================================================

The header of the report provides a short summary of the functions involved in
the race. It is followed by the access types and stack traces of the 2 threads
involved in the data race.

The other less common type of data race report looks like this::

==================================================================
BUG: KCSAN: data-race in e1000_clean_rx_irq+0x551/0xb10

race at unknown origin, with read to 0xffff933db8a2ae6c of 1 bytes by interrupt on cpu 0:
e1000_clean_rx_irq+0x551/0xb10
e1000_clean+0x533/0xda0
net_rx_action+0x329/0x900
__do_softirq+0xdb/0x2db
irq_exit+0x9b/0xa0
do_IRQ+0x9c/0xf0
ret_from_intr+0x0/0x18
default_idle+0x3f/0x220
arch_cpu_idle+0x21/0x30
do_idle+0x1df/0x230
cpu_startup_entry+0x14/0x20
rest_init+0xc5/0xcb
arch_call_rest_init+0x13/0x2b
start_kernel+0x6db/0x700

Reported by Kernel Concurrency Sanitizer on:
CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.3.0-rc7+ #2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
==================================================================

This report is generated where it was not possible to determine the other
racing thread, but a race was inferred due to the data value of the watched
memory location having changed. These can occur either due to missing
instrumentation or e.g. DMA accesses. These reports will only be generated if
``CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=y`` (selected by default).

Selective analysis
~~~~~~~~~~~~~~~~~~

It may be desirable to disable data race detection for specific accesses,
functions, compilation units, or entire subsystems. For static blacklisting,
the below options are available:

* KCSAN understands the ``data_race(expr)`` annotation, which tells KCSAN that
any data races due to accesses in ``expr`` should be ignored and resulting
behaviour when encountering a data race is deemed safe.

* Disabling data race detection for entire functions can be accomplished by
using the function attribute ``__no_kcsan``::

__no_kcsan
void foo(void) {
...

To dynamically limit for which functions to generate reports, see the
`DebugFS interface`_ blacklist/whitelist feature.

For ``__always_inline`` functions, replace ``__always_inline`` with
``__no_kcsan_or_inline`` (which implies ``__always_inline``)::

static __no_kcsan_or_inline void foo(void) {
...

* To disable data race detection for a particular compilation unit, add to the
``Makefile``::

KCSAN_SANITIZE_file.o := n

* To disable data race detection for all compilation units listed in a
``Makefile``, add to the respective ``Makefile``::

KCSAN_SANITIZE := n

Furthermore, it is possible to tell KCSAN to show or hide entire classes of
data races, depending on preferences. These can be changed via the following
Kconfig options:

* ``CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY``: If enabled and a conflicting write
is observed via a watchpoint, but the data value of the memory location was
observed to remain unchanged, do not report the data race.

* ``CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC``: Assume that plain aligned writes
up to word size are atomic by default. Assumes that such writes are not
subject to unsafe compiler optimizations resulting in data races. The option
causes KCSAN to not report data races due to conflicts where the only plain
accesses are aligned writes up to word size.

DebugFS interface
~~~~~~~~~~~~~~~~~

The file ``/sys/kernel/debug/kcsan`` provides the following interface:

* Reading ``/sys/kernel/debug/kcsan`` returns various runtime statistics.

* Writing ``on`` or ``off`` to ``/sys/kernel/debug/kcsan`` allows turning KCSAN
on or off, respectively.

* Writing ``!some_func_name`` to ``/sys/kernel/debug/kcsan`` adds
``some_func_name`` to the report filter list, which (by default) blacklists
reporting data races where either one of the top stackframes are a function
in the list.

* Writing either ``blacklist`` or ``whitelist`` to ``/sys/kernel/debug/kcsan``
changes the report filtering behaviour. For example, the blacklist feature
can be used to silence frequently occurring data races; the whitelist feature
can help with reproduction and testing of fixes.

Tuning performance
~~~~~~~~~~~~~~~~~~

Core parameters that affect KCSAN's overall performance and bug detection
ability are exposed as kernel command-line arguments whose defaults can also be
changed via the corresponding Kconfig options.

* ``kcsan.skip_watch`` (``CONFIG_KCSAN_SKIP_WATCH``): Number of per-CPU memory
operations to skip, before another watchpoint is set up. Setting up
watchpoints more frequently will result in the likelihood of races to be
observed to increase. This parameter has the most significant impact on
overall system performance and race detection ability.

* ``kcsan.udelay_task`` (``CONFIG_KCSAN_UDELAY_TASK``): For tasks, the
microsecond delay to stall execution after a watchpoint has been set up.
Larger values result in the window in which we may observe a race to
increase.

* ``kcsan.udelay_interrupt`` (``CONFIG_KCSAN_UDELAY_INTERRUPT``): For
interrupts, the microsecond delay to stall execution after a watchpoint has
been set up. Interrupts have tighter latency requirements, and their delay
should generally be smaller than the one chosen for tasks.

They may be tweaked at runtime via ``/sys/module/kcsan/parameters/``.

Data Races
----------

In an execution, two memory accesses form a *data race* if they *conflict*,
they happen concurrently in different threads, and at least one of them is a
*plain access*; they *conflict* if both access the same memory location, and at
least one is a write. For a more thorough discussion and definition, see `"Plain
Accesses and Data Races" in the LKMM`_.

.. _"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

Relationship with the Linux-Kernel Memory Consistency Model (LKMM)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The LKMM defines the propagation and ordering rules of various memory
operations, which gives developers the ability to reason about concurrent code.
Ultimately this allows to determine the possible executions of concurrent code,
and if that code is free from data races.

KCSAN is aware of *marked atomic operations* (``READ_ONCE``, ``WRITE_ONCE``,
``atomic_*``, etc.), but is oblivious of any ordering guarantees and simply
assumes that memory barriers are placed correctly. In other words, KCSAN
assumes that as long as a plain access is not observed to race with another
conflicting access, memory operations are correctly ordered.

This means that KCSAN will not report *potential* data races due to missing
memory ordering. Developers should therefore carefully consider the required
memory ordering requirements that remain unchecked. If, however, missing
memory ordering (that is observable with a particular compiler and
architecture) leads to an observable data race (e.g. entering a critical
section erroneously), KCSAN would report the resulting data race.

Race Detection Beyond Data Races
--------------------------------

For code with complex concurrency design, race-condition bugs may not always
manifest as data races. Race conditions occur if concurrently executing
operations result in unexpected system behaviour. On the other hand, data races
are defined at the C-language level. The following macros can be used to check
properties of concurrent code where bugs would not manifest as data races.

.. kernel-doc:: include/linux/kcsan-checks.h
:functions: ASSERT_EXCLUSIVE_WRITER ASSERT_EXCLUSIVE_WRITER_SCOPED
ASSERT_EXCLUSIVE_ACCESS ASSERT_EXCLUSIVE_ACCESS_SCOPED
ASSERT_EXCLUSIVE_BITS

Implementation Details
----------------------

KCSAN relies on observing that two accesses happen concurrently. Crucially, we
want to (a) increase the chances of observing races (especially for races that
manifest rarely), and (b) be able to actually observe them. We can accomplish
(a) by injecting various delays, and (b) by using address watchpoints (or
breakpoints).

If we deliberately stall a memory access, while we have a watchpoint for its
address set up, and then observe the watchpoint to fire, two accesses to the
same address just raced. Using hardware watchpoints, this is the approach taken
in `DataCollider
<http://usenix.org/legacy/events/osdi10/tech/full_papers/Erickson.pdf>`_.
Unlike DataCollider, KCSAN does not use hardware watchpoints, but instead
relies on compiler instrumentation and "soft watchpoints".

In KCSAN, watchpoints are implemented using an efficient encoding that stores
access type, size, and address in a long; the benefits of using "soft
watchpoints" are portability and greater flexibility. KCSAN then relies on the
compiler instrumenting plain accesses. For each instrumented plain access:

1. Check if a matching watchpoint exists; if yes, and at least one access is a
write, then we encountered a racing access.

2. Periodically, if no matching watchpoint exists, set up a watchpoint and
stall for a small randomized delay.

3. Also check the data value before the delay, and re-check the data value
after delay; if the values mismatch, we infer a race of unknown origin.

To detect data races between plain and marked accesses, KCSAN also annotates
marked accesses, but only to check if a watchpoint exists; i.e. KCSAN never
sets up a watchpoint on marked accesses. By never setting up watchpoints for
marked operations, if all accesses to a variable that is accessed concurrently
are properly marked, KCSAN will never trigger a watchpoint and therefore never
report the accesses.

Key Properties
~~~~~~~~~~~~~~

1. **Memory Overhead:** The overall memory overhead is only a few MiB
depending on configuration. The current implementation uses a small array of
longs to encode watchpoint information, which is negligible.

2. **Performance Overhead:** KCSAN's runtime aims to be minimal, using an
efficient watchpoint encoding that does not require acquiring any shared
locks in the fast-path. For kernel boot on a system with 8 CPUs:

- 5.0x slow-down with the default KCSAN config;
- 2.8x slow-down from runtime fast-path overhead only (set very large
``KCSAN_SKIP_WATCH`` and unset ``KCSAN_SKIP_WATCH_RANDOMIZE``).

3. **Annotation Overheads:** Minimal annotations are required outside the KCSAN
runtime. As a result, maintenance overheads are minimal as the kernel
evolves.

4. **Detects Racy Writes from Devices:** Due to checking data values upon
setting up watchpoints, racy writes from devices can also be detected.

5. **Memory Ordering:** KCSAN is *not* explicitly aware of the LKMM's ordering
rules; this may result in missed data races (false negatives).

6. **Analysis Accuracy:** For observed executions, due to using a sampling
strategy, the analysis is *unsound* (false negatives possible), but aims to
be complete (no false positives).

Alternatives Considered
-----------------------

An alternative data race detection approach for the kernel can be found in the
`Kernel Thread Sanitizer (KTSAN) <https://github.com/google/ktsan/wiki>`_.
KTSAN is a happens-before data race detector, which explicitly establishes the
happens-before order between memory operations, which can then be used to
determine data races as defined in `Data Races`_.

To build a correct happens-before relation, KTSAN must be aware of all ordering
rules of the LKMM and synchronization primitives. Unfortunately, any omission
leads to large numbers of false positives, which is especially detrimental in
the context of the kernel which includes numerous custom synchronization
mechanisms. To track the happens-before relation, KTSAN's implementation
requires metadata for each memory location (shadow memory), which for each page
corresponds to 4 pages of shadow memory, and can translate into overhead of
tens of GiB on a large system.
8 changes: 6 additions & 2 deletions Documentation/lzo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,15 @@ Byte sequences
distance = 16384 + (H << 14) + D
state = S (copy S literals after this block)
End of stream is reached if distance == 16384
In version 1 only, to prevent ambiguity with the RLE case when
((distance & 0x803f) == 0x803f) && (261 <= length <= 264), the
compressor must not emit block copies where distance and length
meet these conditions.

In version 1 only, this instruction is also used to encode a run of
zeros if distance = 0xbfff, i.e. H = 1 and the D bits are all 1.
zeros if distance = 0xbfff, i.e. H = 1 and the D bits are all 1.
In this case, it is followed by a fourth byte, X.
run length = ((X << 3) | (0 0 0 0 0 L L L)) + 4.
run length = ((X << 3) | (0 0 0 0 0 L L L)) + 4

0 0 1 L L L L L (32..63)
Copy of small block within 16kB distance (preferably less than 34B)
Expand Down
11 changes: 11 additions & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -9305,6 +9305,17 @@ F: Documentation/kbuild/kconfig*
F: scripts/Kconfig.include
F: scripts/kconfig/

KCSAN
M: Marco Elver <elver@google.com>
R: Dmitry Vyukov <dvyukov@google.com>
L: kasan-dev@googlegroups.com
S: Maintained
F: Documentation/dev-tools/kcsan.rst
F: include/linux/kcsan*.h
F: kernel/kcsan/
F: lib/Kconfig.kcsan
F: scripts/Makefile.kcsan

KDUMP
M: Dave Young <dyoung@redhat.com>
M: Baoquan He <bhe@redhat.com>
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE

export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_KCSAN
export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
Expand Down Expand Up @@ -965,6 +965,7 @@ endif
include scripts/Makefile.kasan
include scripts/Makefile.extrawarn
include scripts/Makefile.ubsan
include scripts/Makefile.kcsan

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