Skip to content

Commit 44656d3

Browse files
melverpaulmckrcu
authored andcommitted
kcsan: Add current->state to implicitly atomic accesses
Add volatile current->state to list of implicitly atomic accesses. This is in preparation to eventually enable KCSAN on kernel/sched (which currently still has KCSAN_SANITIZE := n). Since accesses that match the special check in atomic.h are rare, it makes more sense to move this check to the slow-path, avoiding the additional compare in the fast-path. With the microbenchmark, a speedup of ~6% is measured. Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent 2402d0e commit 44656d3

3 files changed

Lines changed: 40 additions & 30 deletions

File tree

‎kernel/kcsan/atomic.h‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44
#define _KERNEL_KCSAN_ATOMIC_H
55

66
#include <linux/jiffies.h>
7+
#include <linux/sched.h>
78

89
/*
9-
* Helper that returns true if access to @ptr should be considered an atomic
10-
* access, even though it is not explicitly atomic.
11-
*
12-
* List all volatile globals that have been observed in races, to suppress
13-
* data race reports between accesses to these variables.
14-
*
15-
* For now, we assume that volatile accesses of globals are as strong as atomic
16-
* accesses (READ_ONCE, WRITE_ONCE cast to volatile). The situation is still not
17-
* entirely clear, as on some architectures (Alpha) READ_ONCE/WRITE_ONCE do more
18-
* than cast to volatile. Eventually, we hope to be able to remove this
19-
* function.
10+
* Special rules for certain memory where concurrent conflicting accesses are
11+
* common, however, the current convention is to not mark them; returns true if
12+
* access to @ptr should be considered atomic. Called from slow-path.
2013
*/
21-
static __always_inline bool kcsan_is_atomic(const volatile void *ptr)
14+
static bool kcsan_is_atomic_special(const volatile void *ptr)
2215
{
23-
/* only jiffies for now */
24-
return ptr == &jiffies;
16+
/* volatile globals that have been observed in data races. */
17+
return ptr == &jiffies || ptr == &current->state;
2518
}
2619

2720
#endif /* _KERNEL_KCSAN_ATOMIC_H */

‎kernel/kcsan/core.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,29 +188,30 @@ static __always_inline struct kcsan_ctx *get_ctx(void)
188188
return in_task() ? &current->kcsan_ctx : raw_cpu_ptr(&kcsan_cpu_ctx);
189189
}
190190

191+
/* Rules for generic atomic accesses. Called from fast-path. */
191192
static __always_inline bool
192193
is_atomic(const volatile void *ptr, size_t size, int type)
193194
{
194195
struct kcsan_ctx *ctx;
195196

196-
if ((type & KCSAN_ACCESS_ATOMIC) != 0)
197+
if (type & KCSAN_ACCESS_ATOMIC)
197198
return true;
198199

199200
/*
200201
* Unless explicitly declared atomic, never consider an assertion access
201202
* as atomic. This allows using them also in atomic regions, such as
202203
* seqlocks, without implicitly changing their semantics.
203204
*/
204-
if ((type & KCSAN_ACCESS_ASSERT) != 0)
205+
if (type & KCSAN_ACCESS_ASSERT)
205206
return false;
206207

207208
if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) &&
208-
(type & KCSAN_ACCESS_WRITE) != 0 && size <= sizeof(long) &&
209+
(type & KCSAN_ACCESS_WRITE) && size <= sizeof(long) &&
209210
IS_ALIGNED((unsigned long)ptr, size))
210211
return true; /* Assume aligned writes up to word size are atomic. */
211212

212213
ctx = get_ctx();
213-
if (unlikely(ctx->atomic_next > 0)) {
214+
if (ctx->atomic_next > 0) {
214215
/*
215216
* Because we do not have separate contexts for nested
216217
* interrupts, in case atomic_next is set, we simply assume that
@@ -224,10 +225,8 @@ is_atomic(const volatile void *ptr, size_t size, int type)
224225
--ctx->atomic_next; /* in task, or outer interrupt */
225226
return true;
226227
}
227-
if (unlikely(ctx->atomic_nest_count > 0 || ctx->in_flat_atomic))
228-
return true;
229228

230-
return kcsan_is_atomic(ptr);
229+
return ctx->atomic_nest_count > 0 || ctx->in_flat_atomic;
231230
}
232231

233232
static __always_inline bool
@@ -367,6 +366,15 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
367366
if (!kcsan_is_enabled())
368367
goto out;
369368

369+
/*
370+
* Special atomic rules: unlikely to be true, so we check them here in
371+
* the slow-path, and not in the fast-path in is_atomic(). Call after
372+
* kcsan_is_enabled(), as we may access memory that is not yet
373+
* initialized during early boot.
374+
*/
375+
if (!is_assert && kcsan_is_atomic_special(ptr))
376+
goto out;
377+
370378
if (!check_encodable((unsigned long)ptr, size)) {
371379
kcsan_counter_inc(KCSAN_COUNTER_UNENCODABLE_ACCESSES);
372380
goto out;

‎kernel/kcsan/debugfs.c‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,34 @@ void kcsan_counter_dec(enum kcsan_counter_id id)
7474
*/
7575
static noinline void microbenchmark(unsigned long iters)
7676
{
77+
const struct kcsan_ctx ctx_save = current->kcsan_ctx;
78+
const bool was_enabled = READ_ONCE(kcsan_enabled);
7779
cycles_t cycles;
7880

81+
/* We may have been called from an atomic region; reset context. */
82+
memset(&current->kcsan_ctx, 0, sizeof(current->kcsan_ctx));
83+
/*
84+
* Disable to benchmark fast-path for all accesses, and (expected
85+
* negligible) call into slow-path, but never set up watchpoints.
86+
*/
87+
WRITE_ONCE(kcsan_enabled, false);
88+
7989
pr_info("KCSAN: %s begin | iters: %lu\n", __func__, iters);
8090

8191
cycles = get_cycles();
8292
while (iters--) {
83-
/*
84-
* We can run this benchmark from multiple tasks; this address
85-
* calculation increases likelyhood of some accesses
86-
* overlapping. Make the access type an atomic read, to never
87-
* set up watchpoints and test the fast-path only.
88-
*/
89-
unsigned long addr =
90-
iters % (CONFIG_KCSAN_NUM_WATCHPOINTS * PAGE_SIZE);
91-
__kcsan_check_access((void *)addr, sizeof(long), KCSAN_ACCESS_ATOMIC);
93+
unsigned long addr = iters & ((PAGE_SIZE << 8) - 1);
94+
int type = !(iters & 0x7f) ? KCSAN_ACCESS_ATOMIC :
95+
(!(iters & 0xf) ? KCSAN_ACCESS_WRITE : 0);
96+
__kcsan_check_access((void *)addr, sizeof(long), type);
9297
}
9398
cycles = get_cycles() - cycles;
9499

95100
pr_info("KCSAN: %s end | cycles: %llu\n", __func__, cycles);
101+
102+
WRITE_ONCE(kcsan_enabled, was_enabled);
103+
/* restore context */
104+
current->kcsan_ctx = ctx_save;
96105
}
97106

98107
/*

0 commit comments

Comments
 (0)