summaryrefslogtreecommitdiffstats
path: root/kernel/kcsan/core.c
diff options
context:
space:
mode:
authorMarco Elver <elver@google.com>2020-02-11 17:04:21 +0100
committerIngo Molnar <mingo@kernel.org>2020-03-21 09:44:03 +0100
commitb738f6169f1260b4ed5bd9f220b1c84d79f3ab8d (patch)
treeb15d523664449bf30e59c034e4517eb5e399744d /kernel/kcsan/core.c
parentb968a08f242d51982e46041c506115b5e11a7570 (diff)
downloadlinux-b738f6169f1260b4ed5bd9f220b1c84d79f3ab8d.tar.gz
linux-b738f6169f1260b4ed5bd9f220b1c84d79f3ab8d.tar.bz2
linux-b738f6169f1260b4ed5bd9f220b1c84d79f3ab8d.zip
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 <jhubbard@nvidia.com> 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>
Diffstat (limited to 'kernel/kcsan/core.c')
-rw-r--r--kernel/kcsan/core.c38
1 files changed, 22 insertions, 16 deletions
diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
index 498b1eb3c1cd..3f89801161d3 100644
--- a/kernel/kcsan/core.c
+++ b/kernel/kcsan/core.c
@@ -341,7 +341,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
u32 _4;
u64 _8;
} expect_value;
- bool value_change = false;
+ enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE;
unsigned long ua_flags = user_access_save();
unsigned long irq_flags;
@@ -398,6 +398,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
* Read the current value, to later check and infer a race if the data
* was modified via a non-instrumented access, e.g. from a device.
*/
+ expect_value._8 = 0;
switch (size) {
case 1:
expect_value._1 = READ_ONCE(*(const u8 *)ptr);
@@ -436,24 +437,37 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
*/
switch (size) {
case 1:
- value_change = expect_value._1 != READ_ONCE(*(const u8 *)ptr);
+ expect_value._1 ^= READ_ONCE(*(const u8 *)ptr);
break;
case 2:
- value_change = expect_value._2 != READ_ONCE(*(const u16 *)ptr);
+ expect_value._2 ^= READ_ONCE(*(const u16 *)ptr);
break;
case 4:
- value_change = expect_value._4 != READ_ONCE(*(const u32 *)ptr);
+ expect_value._4 ^= READ_ONCE(*(const u32 *)ptr);
break;
case 8:
- value_change = expect_value._8 != READ_ONCE(*(const u64 *)ptr);
+ expect_value._8 ^= READ_ONCE(*(const u64 *)ptr);
break;
default:
break; /* ignore; we do not diff the values */
}
+ /* Were we able to observe a value-change? */
+ if (expect_value._8 != 0)
+ value_change = KCSAN_VALUE_CHANGE_TRUE;
+
/* Check if this access raced with another. */
if (!remove_watchpoint(watchpoint)) {
/*
+ * Depending on the access type, map a value_change of MAYBE to
+ * TRUE (require reporting).
+ */
+ if (value_change == KCSAN_VALUE_CHANGE_MAYBE && (size > 8 || is_assert)) {
+ /* Always assume a value-change. */
+ value_change = KCSAN_VALUE_CHANGE_TRUE;
+ }
+
+ /*
* No need to increment 'data_races' counter, as the racing
* thread already did.
*
@@ -461,20 +475,12 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
* therefore both this thread and the racing thread may
* increment this counter.
*/
- if (is_assert)
+ if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
- /*
- * - If we were not able to observe a value change due to size
- * constraints, always assume a value change.
- * - If the access type is an assertion, we also always assume a
- * value change to always report the race.
- */
- value_change = value_change || size > 8 || is_assert;
-
kcsan_report(ptr, size, type, value_change, smp_processor_id(),
KCSAN_REPORT_RACE_SIGNAL);
- } else if (value_change) {
+ } else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
/* Inferring a race, since the value should not have changed. */
kcsan_counter_inc(KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN);
@@ -482,7 +488,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert)
- kcsan_report(ptr, size, type, true,
+ kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_TRUE,
smp_processor_id(),
KCSAN_REPORT_RACE_UNKNOWN_ORIGIN);
}