kcsan: Add support for scoped accesses
Subsystems (4)
Marco Elver
maintainerPATCH 1/3 kcsan: Add support for scoped accesses
This adds support for scoped accesses, where the memory range is checked for the duration of the scope. The feature is implemented by inserting the relevant access information into a list of scoped accesses for the current execution context, which are then checked (until removed) on every call (through instrumentation) into the KCSAN runtime.
An alternative, more complex, implementation could set up a watchpoint for the scoped access, and keep the watchpoint set up. This, however, would require first exposing a handle to the watchpoint, as well as dealing with cases such as accesses by the same thread while the watchpoint is still set up (and several more cases). It is also doubtful if this would provide any benefit, since the majority of delay where the watchpoint is set up is likely due to the injected delays by KCSAN. Therefore, the implementation in this patch is simpler, nor hurts KCSAN's main use-case (normal data race detection); it also implicitly increases scoped-access race-detection-ability due to increased probability of setting up watchpoints by repeatedly calling __kcsan_check_access() throughout the scope of the access.
The implementation required adding an additional conditional branch to the fast-path. Using the microbenchmark, however, a *speedup* of ~5% of the fast-path is measured. This appears to be due to subtly improved codegen by GCC from moving get_ctx() and associated load of preempt_count earlier.
--- include/linux/kcsan-checks.h | 57 +++++++++++++++++++++++++ include/linux/kcsan.h | 3 ++ init/init_task.c | 1 + kernel/kcsan/core.c | 83 ++++++++++++++++++++++++++++++++---- kernel/kcsan/report.c | 33 +++++++++----- 5 files changed, 158 insertions(+), 19 deletions(-)
include/linux/kcsan-checks.h +57 -0
@@ -3,6 +3,8 @@#ifndef _LINUX_KCSAN_CHECKS_H#define _LINUX_KCSAN_CHECKS_H +/* Note: Only include what is already included by compiler.h. */ +#include <linux/compiler_attributes.h> #include <linux/types.h> /*
@@ -12,10 +14,12 @@* WRITE : write access; * ATOMIC: access is atomic; * ASSERT: access is not a regular access, but an assertion; +* SCOPED: access is a scoped access; */ #define KCSAN_ACCESS_WRITE 0x1#define KCSAN_ACCESS_ATOMIC 0x2#define KCSAN_ACCESS_ASSERT 0x4 +#define KCSAN_ACCESS_SCOPED 0x8/* * __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used
@@ -78,6 +82,52 @@ void kcsan_atomic_next(int n);*/ void kcsan_set_access_mask (unsigned long mask); +/* Scoped access information. */ +struct kcsan_scoped_access { +struct list_head list; +const volatile void *ptr; +size_t size; +int type; +}; +/* +* Automatically call kcsan_end_scoped_access() when kcsan_scoped_access goes +* out of scope; relies on attribute "cleanup", which is supported by all +* compilers that support KCSAN. +*/ +#define __kcsan_cleanup_scoped \ + __maybe_unused __attribute__((__cleanup__(kcsan_end_scoped_access))) + +/** +* kcsan_begin_scoped_access - begin scoped access +* +* Begin scoped access and initialize @sa, which will cause KCSAN to +* continuously check the memory range in the current thread until +* kcsan_end_scoped_access() is called for @sa. +* +* Scoped accesses are implemented by appending @sa to an internal list for the +* current execution context, and then checked on every call into the KCSAN +* runtime. +* +* @ptr: address of access +* @size: size of access +* @type: access type modifier +* @sa: struct kcsan_scoped_access to use for the scope of the access +*/ +struct kcsan_scoped_access * +kcsan_begin_scoped_access (const volatile void *ptr,size_t size,int type, +struct kcsan_scoped_access *sa); + +/** +* kcsan_end_scoped_access - end scoped access +* +* End a scoped access, which will stop KCSAN checking the memory range. +* Requires that kcsan_begin_scoped_access() was previously called once for @sa. +* +* @sa: a previously initialized struct kcsan_scoped_access +*/ +void kcsan_end_scoped_access (struct kcsan_scoped_access *sa); + +#else /* CONFIG_KCSAN */ static inline void __kcsan_check_access(const volatile void *ptr, size_t size,
@@ -90,6 +140,13 @@ static inline void kcsan_flat_atomic_end(void) { }static inline void kcsan_atomic_next (int n) { }static inline void kcsan_set_access_mask (unsigned long mask) { } +struct kcsan_scoped_access { }; +#define __kcsan_cleanup_scoped __maybe_unused +static inline struct kcsan_scoped_access * +kcsan_begin_scoped_access (const volatile void *ptr,size_t size,int type, +struct kcsan_scoped_access *sa) {return sa; } +static inline void kcsan_end_scoped_access (struct kcsan_scoped_access *sa) { } +#endif /* CONFIG_KCSAN */ /*
include/linux/kcsan.h +3 -0
@@ -40,6 +40,9 @@ struct kcsan_ctx {* Access mask for all accesses if non-zero. */ unsigned long access_mask; + +/* List of scoped accesses. */ +struct list_head scoped_accesses; };/**
init/init_task.c +1 -0
@@ -168,6 +168,7 @@ struct task_struct init_task .atomic_nest_count = 0, .in_flat_atomic = false, .access_mask = 0, + .scoped_accesses = {LIST_POISON1, NULL}, }, #endif #ifdef CONFIG_TRACE_IRQFLAGS
kernel/kcsan/core.c +75 -8
134 lines not on this page · raw message
kernel/kcsan/report.c +22 -11
@@ -205,6 +205,20 @@ skip_report(enum kcsan_value_change value_change, unsigned long top_frame)static const char *get_access_type (int type) { +if (type &KCSAN_ACCESS_ASSERT ) { +if (type &KCSAN_ACCESS_SCOPED ) { +if (type &KCSAN_ACCESS_WRITE ) +return "assert no accesses (scoped)" ; +else +return "assert no writes (scoped)" ; + }else { +if (type &KCSAN_ACCESS_WRITE ) +return "assert no accesses" ; +else +return "assert no writes" ; + } + } +switch (type) {case 0 :return "read" ;
@@ -214,17 +228,14 @@ static const char *get_access_type(int type)return "write" ;case KCSAN_ACCESS_WRITE |KCSAN_ACCESS_ATOMIC :return "write (marked)" ; - -/* -* ASSERT variants: -*/ -case KCSAN_ACCESS_ASSERT : -case KCSAN_ACCESS_ASSERT |KCSAN_ACCESS_ATOMIC : -return "assert no writes" ; -case KCSAN_ACCESS_ASSERT |KCSAN_ACCESS_WRITE : -case KCSAN_ACCESS_ASSERT |KCSAN_ACCESS_WRITE |KCSAN_ACCESS_ATOMIC : -return "assert no accesses" ; - +case KCSAN_ACCESS_SCOPED : +return "read (scoped)" ; +case KCSAN_ACCESS_SCOPED |KCSAN_ACCESS_ATOMIC : +return "read (marked, scoped)" ; +case KCSAN_ACCESS_SCOPED |KCSAN_ACCESS_WRITE : +return "write (scoped)" ; +case KCSAN_ACCESS_SCOPED |KCSAN_ACCESS_WRITE |KCSAN_ACCESS_ATOMIC : +return "write (marked, scoped)" ;default :BUG (); }
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog
Marco Elver
maintainerPATCH 2/3 objtool, kcsan: Add explicit check functions to uaccess whitelist
Add explicitly invoked KCSAN check functions to objtool uaccess whitelist. This is needed, to permit calling into kcsan_check_scoped_accesses() from the fast-path, which in turn calls __kcsan_check_access(). __kcsan_check_access() is the generic variant of the already whitelisted specializations __tsan_{read,write}N.
--- tools/objtool/check.c | 2 ++ 1 file changed, 2 insertions(+)
tools/objtool/check.c +2 -0
@@ -468,8 +468,10 @@ static const char *uaccess_safe_builtin[] = {"__asan_report_store8_noabort" ,"__asan_report_store16_noabort" ,/* KCSAN */ +"__kcsan_check_access" ,"kcsan_found_watchpoint" ,"kcsan_setup_watchpoint" , +"kcsan_check_scoped_accesses" ,/* KCSAN/TSAN */ "__tsan_func_entry" , "__tsan_func_exit",
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog
Marco Elver
maintainerPATCH 3/3 kcsan: Introduce scoped ASSERT_EXCLUSIVE macros
In reply to Marco ElverIntroduces ASSERT_EXCLUSIVE_*_SCOPED() which provide an intuitive interface to use the scoped-access feature, without having to explicitly mark the start and end of the desired scope. Basing duration of the checks on scope avoids accidental misuse and resulting false positives, which may be hard to debug. See added comments for usage.
The macros are implemented using __attribute__((__cleanup__(func))), which is supported by all compilers that currently support KCSAN.
--- Documentation/dev-tools/kcsan.rst | 3 +- include/linux/kcsan-checks.h | 73 ++++++++++++++++++++++++++++++- kernel/kcsan/debugfs.c | 16 ++++++- 3 files changed, 89 insertions(+), 3 deletions(-)
Documentation/dev-tools/kcsan.rst +2 -1
@@ -238,7 +238,8 @@ 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_ACCESS + :functions: ASSERT_EXCLUSIVE_WRITER ASSERT_EXCLUSIVE_WRITER_SCOPED + ASSERT_EXCLUSIVE_ACCESS ASSERT_EXCLUSIVE_ACCESS_SCOPED ASSERT_EXCLUSIVE_BITS Implementation Details
include/linux/kcsan-checks.h +72 -1
99 lines not on this page · raw message
kernel/kcsan/debugfs.c +15 -1
@@ -110,6 +110,7 @@ static noinline void microbenchmark(unsigned long iters)*/ static long test_dummy;static long test_flags; +static long test_scoped; static noinline void test_thread(unsigned long iters) {const long CHANGE_BITS =0xff00ff00ff00ff00L ;
@@ -120,7 +121,8 @@ static noinline void test_thread(unsigned long iters)memset (¤t->kcsan_ctx,0 ,sizeof (current->kcsan_ctx));pr_info ("KCSAN: %s begin | iters: %lu\n" , __func__, iters); -pr_info ("test_dummy@%px, test_flags@%px\n" , &test_dummy, &test_flags); +pr_info ("test_dummy@%px, test_flags@%px, test_scoped@%px,\n" , + &test_dummy, &test_flags, &test_scoped); cycles =get_cycles ();while (iters--) {
@@ -141,6 +143,18 @@ static noinline void test_thread(unsigned long iters) test_flags ^=CHANGE_BITS ;/* generate value-change */ __kcsan_check_write (&test_flags,sizeof (test_flags)); + +BUG_ON (current->kcsan_ctx.scoped_accesses.prev); + { +/* Should generate reports anywhere in this block. */ +ASSERT_EXCLUSIVE_WRITER_SCOPED (test_scoped); +ASSERT_EXCLUSIVE_ACCESS_SCOPED (test_scoped); +BUG_ON (!current->kcsan_ctx.scoped_accesses.prev); +/* Unrelated accesses. */ +__kcsan_check_access (&cycles,sizeof (cycles),0 ); +__kcsan_check_access (&cycles,sizeof (cycles),KCSAN_ACCESS_ATOMIC ); + } +BUG_ON (current->kcsan_ctx.scoped_accesses.prev); } cycles =get_cycles () - cycles;
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog