Skip to content

Commit b738f61

Browse files
melverIngo Molnar
authored andcommitted
kcsan: Introduce kcsan_value_change type
Introduces kcsan_value_change type, which explicitly points out if we either observed a value-change (TRUE), or we could not observe one but cannot rule out a value-change happened (MAYBE). The MAYBE state can either be reported or not, depending on configuration preferences. A follow-up patch introduces the FALSE state, which should never be reported. No functional change intended. Acked-by: John Hubbard <[email protected]> Signed-off-by: Marco Elver <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]> Signed-off-by: Ingo Molnar <[email protected]>
1 parent b968a08 commit b738f61

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

kernel/kcsan/core.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
341341
u32 _4;
342342
u64 _8;
343343
} expect_value;
344-
bool value_change = false;
344+
enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE;
345345
unsigned long ua_flags = user_access_save();
346346
unsigned long irq_flags;
347347

@@ -398,6 +398,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
398398
* Read the current value, to later check and infer a race if the data
399399
* was modified via a non-instrumented access, e.g. from a device.
400400
*/
401+
expect_value._8 = 0;
401402
switch (size) {
402403
case 1:
403404
expect_value._1 = READ_ONCE(*(const u8 *)ptr);
@@ -436,23 +437,36 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
436437
*/
437438
switch (size) {
438439
case 1:
439-
value_change = expect_value._1 != READ_ONCE(*(const u8 *)ptr);
440+
expect_value._1 ^= READ_ONCE(*(const u8 *)ptr);
440441
break;
441442
case 2:
442-
value_change = expect_value._2 != READ_ONCE(*(const u16 *)ptr);
443+
expect_value._2 ^= READ_ONCE(*(const u16 *)ptr);
443444
break;
444445
case 4:
445-
value_change = expect_value._4 != READ_ONCE(*(const u32 *)ptr);
446+
expect_value._4 ^= READ_ONCE(*(const u32 *)ptr);
446447
break;
447448
case 8:
448-
value_change = expect_value._8 != READ_ONCE(*(const u64 *)ptr);
449+
expect_value._8 ^= READ_ONCE(*(const u64 *)ptr);
449450
break;
450451
default:
451452
break; /* ignore; we do not diff the values */
452453
}
453454

455+
/* Were we able to observe a value-change? */
456+
if (expect_value._8 != 0)
457+
value_change = KCSAN_VALUE_CHANGE_TRUE;
458+
454459
/* Check if this access raced with another. */
455460
if (!remove_watchpoint(watchpoint)) {
461+
/*
462+
* Depending on the access type, map a value_change of MAYBE to
463+
* TRUE (require reporting).
464+
*/
465+
if (value_change == KCSAN_VALUE_CHANGE_MAYBE && (size > 8 || is_assert)) {
466+
/* Always assume a value-change. */
467+
value_change = KCSAN_VALUE_CHANGE_TRUE;
468+
}
469+
456470
/*
457471
* No need to increment 'data_races' counter, as the racing
458472
* thread already did.
@@ -461,28 +475,20 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
461475
* therefore both this thread and the racing thread may
462476
* increment this counter.
463477
*/
464-
if (is_assert)
478+
if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
465479
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
466480

467-
/*
468-
* - If we were not able to observe a value change due to size
469-
* constraints, always assume a value change.
470-
* - If the access type is an assertion, we also always assume a
471-
* value change to always report the race.
472-
*/
473-
value_change = value_change || size > 8 || is_assert;
474-
475481
kcsan_report(ptr, size, type, value_change, smp_processor_id(),
476482
KCSAN_REPORT_RACE_SIGNAL);
477-
} else if (value_change) {
483+
} else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
478484
/* Inferring a race, since the value should not have changed. */
479485

480486
kcsan_counter_inc(KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN);
481487
if (is_assert)
482488
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
483489

484490
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert)
485-
kcsan_report(ptr, size, type, true,
491+
kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_TRUE,
486492
smp_processor_id(),
487493
KCSAN_REPORT_RACE_UNKNOWN_ORIGIN);
488494
}

kernel/kcsan/kcsan.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ extern void kcsan_counter_dec(enum kcsan_counter_id id);
8888
*/
8989
extern bool kcsan_skip_report_debugfs(unsigned long func_addr);
9090

91+
/*
92+
* Value-change states.
93+
*/
94+
enum kcsan_value_change {
95+
/*
96+
* Did not observe a value-change, however, it is valid to report the
97+
* race, depending on preferences.
98+
*/
99+
KCSAN_VALUE_CHANGE_MAYBE,
100+
101+
/*
102+
* The value was observed to change, and the race should be reported.
103+
*/
104+
KCSAN_VALUE_CHANGE_TRUE,
105+
};
106+
91107
enum kcsan_report_type {
92108
/*
93109
* The thread that set up the watchpoint and briefly stalled was
@@ -111,6 +127,7 @@ enum kcsan_report_type {
111127
* Print a race report from thread that encountered the race.
112128
*/
113129
extern void kcsan_report(const volatile void *ptr, size_t size, int access_type,
114-
bool value_change, int cpu_id, enum kcsan_report_type type);
130+
enum kcsan_value_change value_change, int cpu_id,
131+
enum kcsan_report_type type);
115132

116133
#endif /* _KERNEL_KCSAN_KCSAN_H */

kernel/kcsan/report.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,27 @@ static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
130130
* Special rules to skip reporting.
131131
*/
132132
static bool
133-
skip_report(bool value_change, unsigned long top_frame)
133+
skip_report(enum kcsan_value_change value_change, unsigned long top_frame)
134134
{
135135
/*
136-
* The first call to skip_report always has value_change==true, since we
136+
* The first call to skip_report always has value_change==TRUE, since we
137137
* cannot know the value written of an instrumented access. For the 2nd
138138
* call there are 6 cases with CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY:
139139
*
140-
* 1. read watchpoint, conflicting write (value_change==true): report;
141-
* 2. read watchpoint, conflicting write (value_change==false): skip;
142-
* 3. write watchpoint, conflicting write (value_change==true): report;
143-
* 4. write watchpoint, conflicting write (value_change==false): skip;
144-
* 5. write watchpoint, conflicting read (value_change==false): skip;
145-
* 6. write watchpoint, conflicting read (value_change==true): report;
140+
* 1. read watchpoint, conflicting write (value_change==TRUE): report;
141+
* 2. read watchpoint, conflicting write (value_change==MAYBE): skip;
142+
* 3. write watchpoint, conflicting write (value_change==TRUE): report;
143+
* 4. write watchpoint, conflicting write (value_change==MAYBE): skip;
144+
* 5. write watchpoint, conflicting read (value_change==MAYBE): skip;
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
148148
* data races where the write may have rewritten the same value; case 6
149149
* is possible either if the size is larger than what we check value
150150
* changes for or the access type is KCSAN_ACCESS_ASSERT.
151151
*/
152-
if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) && !value_change) {
152+
if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) &&
153+
value_change == KCSAN_VALUE_CHANGE_MAYBE) {
153154
/*
154155
* The access is a write, but the data value did not change.
155156
*
@@ -245,7 +246,7 @@ static int sym_strcmp(void *addr1, void *addr2)
245246
* Returns true if a report was generated, false otherwise.
246247
*/
247248
static bool print_report(const volatile void *ptr, size_t size, int access_type,
248-
bool value_change, int cpu_id,
249+
enum kcsan_value_change value_change, int cpu_id,
249250
enum kcsan_report_type type)
250251
{
251252
unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 };
@@ -258,7 +259,7 @@ static bool print_report(const volatile void *ptr, size_t size, int access_type,
258259
/*
259260
* Must check report filter rules before starting to print.
260261
*/
261-
if (skip_report(true, stack_entries[skipnr]))
262+
if (skip_report(KCSAN_VALUE_CHANGE_TRUE, stack_entries[skipnr]))
262263
return false;
263264

264265
if (type == KCSAN_REPORT_RACE_SIGNAL) {
@@ -477,7 +478,8 @@ static bool prepare_report(unsigned long *flags, const volatile void *ptr,
477478
}
478479

479480
void kcsan_report(const volatile void *ptr, size_t size, int access_type,
480-
bool value_change, int cpu_id, enum kcsan_report_type type)
481+
enum kcsan_value_change value_change, int cpu_id,
482+
enum kcsan_report_type type)
481483
{
482484
unsigned long flags = 0;
483485

0 commit comments

Comments
 (0)