Skip to content

Commit d591ec3

Browse files
melveringomolnar
authored andcommitted
kcsan: Introduce KCSAN_ACCESS_ASSERT access type
The KCSAN_ACCESS_ASSERT access type may be used to introduce dummy reads and writes to assert certain properties of concurrent code, where bugs could not be detected as normal data races. For example, a variable that is only meant to be written by a single CPU, but may be read (without locking) by other CPUs must still be marked properly to avoid data races. However, concurrent writes, regardless if WRITE_ONCE() or not, would be a bug. Using kcsan_check_access(&x, sizeof(x), KCSAN_ACCESS_ASSERT) would allow catching such bugs. To support KCSAN_ACCESS_ASSERT the following notable changes were made: * If an access is of type KCSAN_ASSERT_ACCESS, disable various filters that only apply to data races, so that all races that KCSAN observes are reported. * Bug reports that involve an ASSERT access type will be reported as "KCSAN: assert: race in ..." instead of "data-race"; this will help more easily distinguish them. * Update a few comments to just mention 'races' where we do not always mean pure data races. Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ed95f95 commit d591ec3

6 files changed

Lines changed: 103 additions & 34 deletions

File tree

‎include/linux/kcsan-checks.h‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
#include <linux/types.h>
77

88
/*
9-
* Access type modifiers.
9+
* ACCESS TYPE MODIFIERS
10+
*
11+
* <none>: normal read access;
12+
* WRITE : write access;
13+
* ATOMIC: access is atomic;
14+
* ASSERT: access is not a regular access, but an assertion;
1015
*/
1116
#define KCSAN_ACCESS_WRITE 0x1
1217
#define KCSAN_ACCESS_ATOMIC 0x2
18+
#define KCSAN_ACCESS_ASSERT 0x4
1319

1420
/*
1521
* __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used
@@ -18,7 +24,7 @@
1824
*/
1925
#ifdef CONFIG_KCSAN
2026
/**
21-
* __kcsan_check_access - check generic access for data races
27+
* __kcsan_check_access - check generic access for races
2228
*
2329
* @ptr address of access
2430
* @size size of access
@@ -43,15 +49,15 @@ static inline void kcsan_check_access(const volatile void *ptr, size_t size,
4349
#endif
4450

4551
/**
46-
* __kcsan_check_read - check regular read access for data races
52+
* __kcsan_check_read - check regular read access for races
4753
*
4854
* @ptr address of access
4955
* @size size of access
5056
*/
5157
#define __kcsan_check_read(ptr, size) __kcsan_check_access(ptr, size, 0)
5258

5359
/**
54-
* __kcsan_check_write - check regular write access for data races
60+
* __kcsan_check_write - check regular write access for races
5561
*
5662
* @ptr address of access
5763
* @size size of access
@@ -60,15 +66,15 @@ static inline void kcsan_check_access(const volatile void *ptr, size_t size,
6066
__kcsan_check_access(ptr, size, KCSAN_ACCESS_WRITE)
6167

6268
/**
63-
* kcsan_check_read - check regular read access for data races
69+
* kcsan_check_read - check regular read access for races
6470
*
6571
* @ptr address of access
6672
* @size size of access
6773
*/
6874
#define kcsan_check_read(ptr, size) kcsan_check_access(ptr, size, 0)
6975

7076
/**
71-
* kcsan_check_write - check regular write access for data races
77+
* kcsan_check_write - check regular write access for races
7278
*
7379
* @ptr address of access
7480
* @size size of access

‎kernel/kcsan/core.c‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
5656

5757
/*
5858
* SLOT_IDX_FAST is used in the fast-path. Not first checking the address's primary
59-
* slot (middle) is fine if we assume that data races occur rarely. The set of
59+
* slot (middle) is fine if we assume that races occur rarely. The set of
6060
* indices {SLOT_IDX(slot, i) | i in [0, NUM_SLOTS)} is equivalent to
6161
* {SLOT_IDX_FAST(slot, i) | i in [0, NUM_SLOTS)}.
6262
*/
@@ -178,6 +178,14 @@ is_atomic(const volatile void *ptr, size_t size, int type)
178178
if ((type & KCSAN_ACCESS_ATOMIC) != 0)
179179
return true;
180180

181+
/*
182+
* Unless explicitly declared atomic, never consider an assertion access
183+
* as atomic. This allows using them also in atomic regions, such as
184+
* seqlocks, without implicitly changing their semantics.
185+
*/
186+
if ((type & KCSAN_ACCESS_ASSERT) != 0)
187+
return false;
188+
181189
if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) &&
182190
(type & KCSAN_ACCESS_WRITE) != 0 && size <= sizeof(long) &&
183191
IS_ALIGNED((unsigned long)ptr, size))
@@ -298,7 +306,11 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
298306
*/
299307
kcsan_counter_inc(KCSAN_COUNTER_REPORT_RACES);
300308
}
301-
kcsan_counter_inc(KCSAN_COUNTER_DATA_RACES);
309+
310+
if ((type & KCSAN_ACCESS_ASSERT) != 0)
311+
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
312+
else
313+
kcsan_counter_inc(KCSAN_COUNTER_DATA_RACES);
302314

303315
user_access_restore(flags);
304316
}
@@ -307,6 +319,7 @@ static noinline void
307319
kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
308320
{
309321
const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0;
322+
const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
310323
atomic_long_t *watchpoint;
311324
union {
312325
u8 _1;
@@ -429,13 +442,32 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
429442
/*
430443
* No need to increment 'data_races' counter, as the racing
431444
* thread already did.
445+
*
446+
* Count 'assert_failures' for each failed ASSERT access,
447+
* therefore both this thread and the racing thread may
448+
* increment this counter.
432449
*/
433-
kcsan_report(ptr, size, type, size > 8 || value_change,
434-
smp_processor_id(), KCSAN_REPORT_RACE_SIGNAL);
450+
if (is_assert)
451+
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
452+
453+
/*
454+
* - If we were not able to observe a value change due to size
455+
* constraints, always assume a value change.
456+
* - If the access type is an assertion, we also always assume a
457+
* value change to always report the race.
458+
*/
459+
value_change = value_change || size > 8 || is_assert;
460+
461+
kcsan_report(ptr, size, type, value_change, smp_processor_id(),
462+
KCSAN_REPORT_RACE_SIGNAL);
435463
} else if (value_change) {
436464
/* Inferring a race, since the value should not have changed. */
465+
437466
kcsan_counter_inc(KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN);
438-
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN))
467+
if (is_assert)
468+
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
469+
470+
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert)
439471
kcsan_report(ptr, size, type, true,
440472
smp_processor_id(),
441473
KCSAN_REPORT_RACE_UNKNOWN_ORIGIN);
@@ -471,7 +503,7 @@ static __always_inline void check_access(const volatile void *ptr, size_t size,
471503
&encoded_watchpoint);
472504
/*
473505
* It is safe to check kcsan_is_enabled() after find_watchpoint in the
474-
* slow-path, as long as no state changes that cause a data race to be
506+
* slow-path, as long as no state changes that cause a race to be
475507
* detected and reported have occurred until kcsan_is_enabled() is
476508
* checked.
477509
*/

‎kernel/kcsan/debugfs.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static const char *counter_to_name(enum kcsan_counter_id id)
4444
case KCSAN_COUNTER_USED_WATCHPOINTS: return "used_watchpoints";
4545
case KCSAN_COUNTER_SETUP_WATCHPOINTS: return "setup_watchpoints";
4646
case KCSAN_COUNTER_DATA_RACES: return "data_races";
47+
case KCSAN_COUNTER_ASSERT_FAILURES: return "assert_failures";
4748
case KCSAN_COUNTER_NO_CAPACITY: return "no_capacity";
4849
case KCSAN_COUNTER_REPORT_RACES: return "report_races";
4950
case KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN: return "races_unknown_origin";

‎kernel/kcsan/kcsan.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ enum kcsan_counter_id {
3939
*/
4040
KCSAN_COUNTER_DATA_RACES,
4141

42+
/*
43+
* Total number of ASSERT failures due to races. If the observed race is
44+
* due to two conflicting ASSERT type accesses, then both will be
45+
* counted.
46+
*/
47+
KCSAN_COUNTER_ASSERT_FAILURES,
48+
4249
/*
4350
* Number of times no watchpoints were available.
4451
*/

‎kernel/kcsan/report.c‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ static struct {
3434
} other_info = { .ptr = NULL };
3535

3636
/*
37-
* Information about reported data races; used to rate limit reporting.
37+
* Information about reported races; used to rate limit reporting.
3838
*/
3939
struct report_time {
4040
/*
41-
* The last time the data race was reported.
41+
* The last time the race was reported.
4242
*/
4343
unsigned long time;
4444

@@ -57,7 +57,7 @@ struct report_time {
5757
*
5858
* Therefore, we use a fixed-size array, which at most will occupy a page. This
5959
* still adequately rate limits reports, assuming that a) number of unique data
60-
* races is not excessive, and b) occurrence of unique data races within the
60+
* races is not excessive, and b) occurrence of unique races within the
6161
* same time window is limited.
6262
*/
6363
#define REPORT_TIMES_MAX (PAGE_SIZE / sizeof(struct report_time))
@@ -74,7 +74,7 @@ static struct report_time report_times[REPORT_TIMES_SIZE];
7474
static DEFINE_SPINLOCK(report_lock);
7575

7676
/*
77-
* Checks if the data race identified by thread frames frame1 and frame2 has
77+
* Checks if the race identified by thread frames frame1 and frame2 has
7878
* been reported since (now - KCSAN_REPORT_ONCE_IN_MS).
7979
*/
8080
static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
@@ -90,7 +90,7 @@ static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
9090

9191
invalid_before = jiffies - msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS);
9292

93-
/* Check if a matching data race report exists. */
93+
/* Check if a matching race report exists. */
9494
for (i = 0; i < REPORT_TIMES_SIZE; ++i) {
9595
struct report_time *rt = &report_times[i];
9696

@@ -114,7 +114,7 @@ static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
114114
if (time_before(rt->time, invalid_before))
115115
continue; /* before KCSAN_REPORT_ONCE_IN_MS ago */
116116

117-
/* Reported recently, check if data race matches. */
117+
/* Reported recently, check if race matches. */
118118
if ((rt->frame1 == frame1 && rt->frame2 == frame2) ||
119119
(rt->frame1 == frame2 && rt->frame2 == frame1))
120120
return true;
@@ -142,11 +142,12 @@ skip_report(bool value_change, unsigned long top_frame)
142142
* 3. write watchpoint, conflicting write (value_change==true): report;
143143
* 4. write watchpoint, conflicting write (value_change==false): skip;
144144
* 5. write watchpoint, conflicting read (value_change==false): skip;
145-
* 6. write watchpoint, conflicting read (value_change==true): impossible;
145+
* 6. write watchpoint, conflicting read (value_change==true): report;
146146
*
147147
* Cases 1-4 are intuitive and expected; case 5 ensures we do not report
148-
* data races where the write may have rewritten the same value; and
149-
* case 6 is simply impossible.
148+
* data races where the write may have rewritten the same value; case 6
149+
* is possible either if the size is larger than what we check value
150+
* changes for or the access type is KCSAN_ACCESS_ASSERT.
150151
*/
151152
if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) && !value_change) {
152153
/*
@@ -178,11 +179,27 @@ static const char *get_access_type(int type)
178179
return "write";
179180
case KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
180181
return "write (marked)";
182+
183+
/*
184+
* ASSERT variants:
185+
*/
186+
case KCSAN_ACCESS_ASSERT:
187+
case KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_ATOMIC:
188+
return "assert no writes";
189+
case KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE:
190+
case KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
191+
return "assert no accesses";
192+
181193
default:
182194
BUG();
183195
}
184196
}
185197

198+
static const char *get_bug_type(int type)
199+
{
200+
return (type & KCSAN_ACCESS_ASSERT) != 0 ? "assert: race" : "data-race";
201+
}
202+
186203
/* Return thread description: in task or interrupt. */
187204
static const char *get_thread_desc(int task_id)
188205
{
@@ -268,13 +285,15 @@ static bool print_report(const volatile void *ptr, size_t size, int access_type,
268285
* Do not print offset of functions to keep title short.
269286
*/
270287
cmp = sym_strcmp((void *)other_frame, (void *)this_frame);
271-
pr_err("BUG: KCSAN: data-race in %ps / %ps\n",
288+
pr_err("BUG: KCSAN: %s in %ps / %ps\n",
289+
get_bug_type(access_type | other_info.access_type),
272290
(void *)(cmp < 0 ? other_frame : this_frame),
273291
(void *)(cmp < 0 ? this_frame : other_frame));
274292
} break;
275293

276294
case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN:
277-
pr_err("BUG: KCSAN: data-race in %pS\n", (void *)this_frame);
295+
pr_err("BUG: KCSAN: %s in %pS\n", get_bug_type(access_type),
296+
(void *)this_frame);
278297
break;
279298

280299
default:
@@ -427,7 +446,7 @@ void kcsan_report(const volatile void *ptr, size_t size, int access_type,
427446
/*
428447
* With TRACE_IRQFLAGS, lockdep's IRQ trace state becomes corrupted if
429448
* we do not turn off lockdep here; this could happen due to recursion
430-
* into lockdep via KCSAN if we detect a data race in utilities used by
449+
* into lockdep via KCSAN if we detect a race in utilities used by
431450
* lockdep.
432451
*/
433452
lockdep_off();

‎lib/Kconfig.kcsan‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ config HAVE_ARCH_KCSAN
44
bool
55

66
menuconfig KCSAN
7-
bool "KCSAN: dynamic data race detector"
7+
bool "KCSAN: dynamic race detector"
88
depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
99
select STACKTRACE
1010
help
11-
The Kernel Concurrency Sanitizer (KCSAN) is a dynamic data race
12-
detector, which relies on compile-time instrumentation, and uses a
13-
watchpoint-based sampling approach to detect data races.
11+
The Kernel Concurrency Sanitizer (KCSAN) is a dynamic race detector,
12+
which relies on compile-time instrumentation, and uses a
13+
watchpoint-based sampling approach to detect races.
14+
15+
KCSAN's primary purpose is to detect data races. KCSAN can also be
16+
used to check properties, with the help of provided assertions, of
17+
concurrent code where bugs do not manifest as data races.
1418

1519
See <file:Documentation/dev-tools/kcsan.rst> for more details.
1620

@@ -85,14 +89,14 @@ config KCSAN_SKIP_WATCH_RANDOMIZE
8589
KCSAN_WATCH_SKIP.
8690

8791
config KCSAN_REPORT_ONCE_IN_MS
88-
int "Duration in milliseconds, in which any given data race is only reported once"
92+
int "Duration in milliseconds, in which any given race is only reported once"
8993
default 3000
9094
help
91-
Any given data race is only reported once in the defined time window.
92-
Different data races may still generate reports within a duration
93-
that is smaller than the duration defined here. This allows rate
94-
limiting reporting to avoid flooding the console with reports.
95-
Setting this to 0 disables rate limiting.
95+
Any given race is only reported once in the defined time window.
96+
Different races may still generate reports within a duration that is
97+
smaller than the duration defined here. This allows rate limiting
98+
reporting to avoid flooding the console with reports. Setting this
99+
to 0 disables rate limiting.
96100

97101
# The main purpose of the below options is to control reported data races (e.g.
98102
# in fuzzer configs), and are not expected to be switched frequently by other

0 commit comments

Comments
 (0)