Skip to content

Commit 757a4ce

Browse files
melverpaulmckrcu
authored andcommitted
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 and avoids hurting 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. However, the microbenchmark showed a *speedup* of ~5% on the fast-path. This appears to be due to subtly improved codegen by GCC from moving get_ctx() and associated load of preempt_count earlier. Suggested-by: Boqun Feng <boqun.feng@gmail.com> Suggested-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent 6119418 commit 757a4ce

5 files changed

Lines changed: 158 additions & 19 deletions

File tree

‎include/linux/kcsan-checks.h‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#ifndef _LINUX_KCSAN_CHECKS_H
44
#define _LINUX_KCSAN_CHECKS_H
55

6+
/* Note: Only include what is already included by compiler.h. */
7+
#include <linux/compiler_attributes.h>
68
#include <linux/types.h>
79

810
/*
@@ -12,10 +14,12 @@
1214
* WRITE : write access;
1315
* ATOMIC: access is atomic;
1416
* ASSERT: access is not a regular access, but an assertion;
17+
* SCOPED: access is a scoped access;
1518
*/
1619
#define KCSAN_ACCESS_WRITE 0x1
1720
#define KCSAN_ACCESS_ATOMIC 0x2
1821
#define KCSAN_ACCESS_ASSERT 0x4
22+
#define KCSAN_ACCESS_SCOPED 0x8
1923

2024
/*
2125
* __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used
@@ -78,6 +82,52 @@ void kcsan_atomic_next(int n);
7882
*/
7983
void kcsan_set_access_mask(unsigned long mask);
8084

85+
/* Scoped access information. */
86+
struct kcsan_scoped_access {
87+
struct list_head list;
88+
const volatile void *ptr;
89+
size_t size;
90+
int type;
91+
};
92+
/*
93+
* Automatically call kcsan_end_scoped_access() when kcsan_scoped_access goes
94+
* out of scope; relies on attribute "cleanup", which is supported by all
95+
* compilers that support KCSAN.
96+
*/
97+
#define __kcsan_cleanup_scoped \
98+
__maybe_unused __attribute__((__cleanup__(kcsan_end_scoped_access)))
99+
100+
/**
101+
* kcsan_begin_scoped_access - begin scoped access
102+
*
103+
* Begin scoped access and initialize @sa, which will cause KCSAN to
104+
* continuously check the memory range in the current thread until
105+
* kcsan_end_scoped_access() is called for @sa.
106+
*
107+
* Scoped accesses are implemented by appending @sa to an internal list for the
108+
* current execution context, and then checked on every call into the KCSAN
109+
* runtime.
110+
*
111+
* @ptr: address of access
112+
* @size: size of access
113+
* @type: access type modifier
114+
* @sa: struct kcsan_scoped_access to use for the scope of the access
115+
*/
116+
struct kcsan_scoped_access *
117+
kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
118+
struct kcsan_scoped_access *sa);
119+
120+
/**
121+
* kcsan_end_scoped_access - end scoped access
122+
*
123+
* End a scoped access, which will stop KCSAN checking the memory range.
124+
* Requires that kcsan_begin_scoped_access() was previously called once for @sa.
125+
*
126+
* @sa: a previously initialized struct kcsan_scoped_access
127+
*/
128+
void kcsan_end_scoped_access(struct kcsan_scoped_access *sa);
129+
130+
81131
#else /* CONFIG_KCSAN */
82132

83133
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) { }
90140
static inline void kcsan_atomic_next(int n) { }
91141
static inline void kcsan_set_access_mask(unsigned long mask) { }
92142

143+
struct kcsan_scoped_access { };
144+
#define __kcsan_cleanup_scoped __maybe_unused
145+
static inline struct kcsan_scoped_access *
146+
kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
147+
struct kcsan_scoped_access *sa) { return sa; }
148+
static inline void kcsan_end_scoped_access(struct kcsan_scoped_access *sa) { }
149+
93150
#endif /* CONFIG_KCSAN */
94151

95152
/*

‎include/linux/kcsan.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ struct kcsan_ctx {
4040
* Access mask for all accesses if non-zero.
4141
*/
4242
unsigned long access_mask;
43+
44+
/* List of scoped accesses. */
45+
struct list_head scoped_accesses;
4346
};
4447

4548
/**

‎init/init_task.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ struct task_struct init_task
168168
.atomic_nest_count = 0,
169169
.in_flat_atomic = false,
170170
.access_mask = 0,
171+
.scoped_accesses = {LIST_POISON1, NULL},
171172
},
172173
#endif
173174
#ifdef CONFIG_TRACE_IRQFLAGS

‎kernel/kcsan/core.c‎

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/export.h>
77
#include <linux/init.h>
88
#include <linux/kernel.h>
9+
#include <linux/list.h>
910
#include <linux/moduleparam.h>
1011
#include <linux/percpu.h>
1112
#include <linux/preempt.h>
@@ -42,6 +43,7 @@ static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
4243
.atomic_nest_count = 0,
4344
.in_flat_atomic = false,
4445
.access_mask = 0,
46+
.scoped_accesses = {LIST_POISON1, NULL},
4547
};
4648

4749
/*
@@ -191,12 +193,23 @@ static __always_inline struct kcsan_ctx *get_ctx(void)
191193
return in_task() ? &current->kcsan_ctx : raw_cpu_ptr(&kcsan_cpu_ctx);
192194
}
193195

196+
/* Check scoped accesses; never inline because this is a slow-path! */
197+
static noinline void kcsan_check_scoped_accesses(void)
198+
{
199+
struct kcsan_ctx *ctx = get_ctx();
200+
struct list_head *prev_save = ctx->scoped_accesses.prev;
201+
struct kcsan_scoped_access *scoped_access;
202+
203+
ctx->scoped_accesses.prev = NULL; /* Avoid recursion. */
204+
list_for_each_entry(scoped_access, &ctx->scoped_accesses, list)
205+
__kcsan_check_access(scoped_access->ptr, scoped_access->size, scoped_access->type);
206+
ctx->scoped_accesses.prev = prev_save;
207+
}
208+
194209
/* Rules for generic atomic accesses. Called from fast-path. */
195210
static __always_inline bool
196-
is_atomic(const volatile void *ptr, size_t size, int type)
211+
is_atomic(const volatile void *ptr, size_t size, int type, struct kcsan_ctx *ctx)
197212
{
198-
struct kcsan_ctx *ctx;
199-
200213
if (type & KCSAN_ACCESS_ATOMIC)
201214
return true;
202215

@@ -213,7 +226,6 @@ is_atomic(const volatile void *ptr, size_t size, int type)
213226
IS_ALIGNED((unsigned long)ptr, size))
214227
return true; /* Assume aligned writes up to word size are atomic. */
215228

216-
ctx = get_ctx();
217229
if (ctx->atomic_next > 0) {
218230
/*
219231
* Because we do not have separate contexts for nested
@@ -233,7 +245,7 @@ is_atomic(const volatile void *ptr, size_t size, int type)
233245
}
234246

235247
static __always_inline bool
236-
should_watch(const volatile void *ptr, size_t size, int type)
248+
should_watch(const volatile void *ptr, size_t size, int type, struct kcsan_ctx *ctx)
237249
{
238250
/*
239251
* Never set up watchpoints when memory operations are atomic.
@@ -242,7 +254,7 @@ should_watch(const volatile void *ptr, size_t size, int type)
242254
* should not count towards skipped instructions, and (2) to actually
243255
* decrement kcsan_atomic_next for consecutive instruction stream.
244256
*/
245-
if (is_atomic(ptr, size, type))
257+
if (is_atomic(ptr, size, type, ctx))
246258
return false;
247259

248260
if (this_cpu_dec_return(kcsan_skip) >= 0)
@@ -563,8 +575,14 @@ static __always_inline void check_access(const volatile void *ptr, size_t size,
563575
if (unlikely(watchpoint != NULL))
564576
kcsan_found_watchpoint(ptr, size, type, watchpoint,
565577
encoded_watchpoint);
566-
else if (unlikely(should_watch(ptr, size, type)))
567-
kcsan_setup_watchpoint(ptr, size, type);
578+
else {
579+
struct kcsan_ctx *ctx = get_ctx(); /* Call only once in fast-path. */
580+
581+
if (unlikely(should_watch(ptr, size, type, ctx)))
582+
kcsan_setup_watchpoint(ptr, size, type);
583+
else if (unlikely(ctx->scoped_accesses.prev))
584+
kcsan_check_scoped_accesses();
585+
}
568586
}
569587

570588
/* === Public interface ===================================================== */
@@ -660,6 +678,55 @@ void kcsan_set_access_mask(unsigned long mask)
660678
}
661679
EXPORT_SYMBOL(kcsan_set_access_mask);
662680

681+
struct kcsan_scoped_access *
682+
kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
683+
struct kcsan_scoped_access *sa)
684+
{
685+
struct kcsan_ctx *ctx = get_ctx();
686+
687+
__kcsan_check_access(ptr, size, type);
688+
689+
ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
690+
691+
INIT_LIST_HEAD(&sa->list);
692+
sa->ptr = ptr;
693+
sa->size = size;
694+
sa->type = type;
695+
696+
if (!ctx->scoped_accesses.prev) /* Lazy initialize list head. */
697+
INIT_LIST_HEAD(&ctx->scoped_accesses);
698+
list_add(&sa->list, &ctx->scoped_accesses);
699+
700+
ctx->disable_count--;
701+
return sa;
702+
}
703+
EXPORT_SYMBOL(kcsan_begin_scoped_access);
704+
705+
void kcsan_end_scoped_access(struct kcsan_scoped_access *sa)
706+
{
707+
struct kcsan_ctx *ctx = get_ctx();
708+
709+
if (WARN(!ctx->scoped_accesses.prev, "Unbalanced %s()?", __func__))
710+
return;
711+
712+
ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
713+
714+
list_del(&sa->list);
715+
if (list_empty(&ctx->scoped_accesses))
716+
/*
717+
* Ensure we do not enter kcsan_check_scoped_accesses()
718+
* slow-path if unnecessary, and avoids requiring list_empty()
719+
* in the fast-path (to avoid a READ_ONCE() and potential
720+
* uaccess warning).
721+
*/
722+
ctx->scoped_accesses.prev = NULL;
723+
724+
ctx->disable_count--;
725+
726+
__kcsan_check_access(sa->ptr, sa->size, sa->type);
727+
}
728+
EXPORT_SYMBOL(kcsan_end_scoped_access);
729+
663730
void __kcsan_check_access(const volatile void *ptr, size_t size, int type)
664731
{
665732
check_access(ptr, size, type);

‎kernel/kcsan/report.c‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ skip_report(enum kcsan_value_change value_change, unsigned long top_frame)
205205

206206
static const char *get_access_type(int type)
207207
{
208+
if (type & KCSAN_ACCESS_ASSERT) {
209+
if (type & KCSAN_ACCESS_SCOPED) {
210+
if (type & KCSAN_ACCESS_WRITE)
211+
return "assert no accesses (scoped)";
212+
else
213+
return "assert no writes (scoped)";
214+
} else {
215+
if (type & KCSAN_ACCESS_WRITE)
216+
return "assert no accesses";
217+
else
218+
return "assert no writes";
219+
}
220+
}
221+
208222
switch (type) {
209223
case 0:
210224
return "read";
@@ -214,17 +228,14 @@ static const char *get_access_type(int type)
214228
return "write";
215229
case KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
216230
return "write (marked)";
217-
218-
/*
219-
* ASSERT variants:
220-
*/
221-
case KCSAN_ACCESS_ASSERT:
222-
case KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_ATOMIC:
223-
return "assert no writes";
224-
case KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE:
225-
case KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
226-
return "assert no accesses";
227-
231+
case KCSAN_ACCESS_SCOPED:
232+
return "read (scoped)";
233+
case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_ATOMIC:
234+
return "read (marked, scoped)";
235+
case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE:
236+
return "write (scoped)";
237+
case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
238+
return "write (marked, scoped)";
228239
default:
229240
BUG();
230241
}

0 commit comments

Comments
 (0)