kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU()
Subsystems (2)
Marco Elver
maintainerPATCH 1/1 kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU()
READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() identify accesses to per-CPU data that are intended to race only with other contexts on the same CPU. So far they are plain READ_ONCE() and WRITE_ONCE(): no marked accesses involving them are reported, incl. from other CPUs.
Make the new accessors assert exclusivity but ignore same-CPU accesses:READ_ONCE_THIS_CPU() asserts no concurrent writers, WRITE_ONCE_THIS_CPU() asserts no concurrent accesses.
For KCSAN to ignore racing accesses from other contexts on the same CPU, introduce KCSAN_ACCESS_THIS_CPU, which makes KCSAN keep interrupts disabled while the watchpoint is set up.
With that, any racing accesses (incl. marked ones) from other CPUs involving READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() will be reported as "KCSAN: assert: race ...".
--- include/asm-generic/rwonce.h | 22 +++++++-- include/linux/kcsan-checks.h | 1 + kernel/kcsan/core.c | 6 ++- kernel/kcsan/kcsan_test.c | 90 ++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 5 deletions(-)
include/asm-generic/rwonce.h +18 -4
@@ -66,11 +66,25 @@ do { \* which are intended to race only with accesses from other execution * contexts on the same CPU, e.g. task and interrupt context. * -* For now, these are simple wrappers around READ_ONCE() and -* WRITE_ONCE(). +* With KCSAN, concurrent writes (for READ_ONCE_THIS_CPU()) or any concurrent +* accesses (for WRITE_ONCE_THIS_CPU()) from other CPUs are reported. */ -#define READ_ONCE_THIS_CPU (x) READ_ONCE(x) -#define WRITE_ONCE_THIS_CPU (x, val) WRITE_ONCE(x, val) +#define READ_ONCE_THIS_CPU (x) \ +({ \ + auto __tc_p = &(x); \ + kcsan_check_access(__tc_p, sizeof(*__tc_p), \ + KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_THIS_CPU);\ + READ_ONCE(*__tc_p); \ +}) + +#define WRITE_ONCE_THIS_CPU (x, val) \ +do { \ + auto __tc_p = &(x); \ + kcsan_check_access(__tc_p, sizeof(*__tc_p), \ + KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | \ + KCSAN_ACCESS_THIS_CPU); \ + WRITE_ONCE(*__tc_p, val); \ +} while (0)static __no_sanitize_or_inline unsigned long __read_once_word_nocheck (const void *addr)
include/linux/kcsan-checks.h +1 -0
@@ -20,6 +20,7 @@/* The following are special, and never due to compiler instrumentation. */ #define KCSAN_ACCESS_ASSERT (1 << 3)/* Access is an assertion. */ #define KCSAN_ACCESS_SCOPED (1 << 4)/* Access is a scoped access. */ +#define KCSAN_ACCESS_THIS_CPU (1 << 5)/* Ignore races from same-CPU accesses. */ /* * __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used
kernel/kcsan/core.c +5 -1
@@ -567,9 +567,13 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned* 1. Races of the reordered access with interrupts. To avoid, if * the current access is reorder_access, disable interrupts. * 2. Avoid races of scoped accesses from nested interrupts (below). +* +* Similarly, KCSAN_ACCESS_THIS_CPU accesses may only race with +* interrupts on the same CPU: disable interrupts, so that only races +* with other CPUs are detected. */ is_reorder_access =find_reorder_access (ctx, ptr, size, type, ip); -if (is_reorder_access) +if (is_reorder_access || (type &KCSAN_ACCESS_THIS_CPU )) interrupt_watcher = false;/* * Avoid races of scoped accesses from nested interrupts (or scheduler).
kernel/kcsan/kcsan_test.c +90 -0
120 lines not on this page · raw message
Signature · 1 line
2.56.1.750.gf70dc682cf-goog