2020-06-16 12:39:12

by Marco Elver

[permalink] [raw]
Subject: [PATCH 0/4] kcsan: Minor cleanups

Minor KCSAN cleanups, none of which should affect functionality.

Marco Elver (4):
kcsan: Silence -Wmissing-prototypes warning with W=1
kcsan: Rename test.c to selftest.c
kcsan: Remove existing special atomic rules
kcsan: Add jiffies test to test suite

kernel/kcsan/Makefile | 2 +-
kernel/kcsan/atomic.h | 6 ++----
kernel/kcsan/core.c | 9 +++++++++
kernel/kcsan/kcsan-test.c | 23 +++++++++++++++++++++++
kernel/kcsan/{test.c => selftest.c} | 0
5 files changed, 35 insertions(+), 5 deletions(-)
rename kernel/kcsan/{test.c => selftest.c} (100%)

--
2.27.0.290.gba653c62da-goog


2020-06-16 12:39:17

by Marco Elver

[permalink] [raw]
Subject: [PATCH 2/4] kcsan: Rename test.c to selftest.c

Rename 'test.c' to 'selftest.c' to better reflect its purpose (Kconfig
variable and code inside already match this). This is to avoid confusion
with the test suite module in 'kcsan-test.c'.

No functional change.

Signed-off-by: Marco Elver <[email protected]>
---
kernel/kcsan/Makefile | 2 +-
kernel/kcsan/{test.c => selftest.c} | 0
2 files changed, 1 insertion(+), 1 deletion(-)
rename kernel/kcsan/{test.c => selftest.c} (100%)

diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
index 14533cf24bc3..092ce58d2e56 100644
--- a/kernel/kcsan/Makefile
+++ b/kernel/kcsan/Makefile
@@ -11,7 +11,7 @@ CFLAGS_core.o := $(call cc-option,-fno-conserve-stack,) \
$(call cc-option,-fno-stack-protector,)

obj-y := core.o debugfs.o report.o
-obj-$(CONFIG_KCSAN_SELFTEST) += test.o
+obj-$(CONFIG_KCSAN_SELFTEST) += selftest.o

CFLAGS_kcsan-test.o := $(CFLAGS_KCSAN) -g -fno-omit-frame-pointer
obj-$(CONFIG_KCSAN_TEST) += kcsan-test.o
diff --git a/kernel/kcsan/test.c b/kernel/kcsan/selftest.c
similarity index 100%
rename from kernel/kcsan/test.c
rename to kernel/kcsan/selftest.c
--
2.27.0.290.gba653c62da-goog

2020-06-16 12:39:30

by Marco Elver

[permalink] [raw]
Subject: [PATCH 1/4] kcsan: Silence -Wmissing-prototypes warning with W=1

The functions here should not be forward declared for explicit use
elsewhere in the kernel, as they should only be emitted by the compiler
due to sanitizer instrumentation. Add forward declarations a line above
their definition to shut up warnings in W=1 builds.

Link: https://lkml.kernel.org/r/202006060103.jSCpnV1g%[email protected]
Reported-by: kernel test robot <[email protected]>
Signed-off-by: Marco Elver <[email protected]>
---
kernel/kcsan/core.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
index 15f67949d11e..1866bafda4fd 100644
--- a/kernel/kcsan/core.c
+++ b/kernel/kcsan/core.c
@@ -754,6 +754,7 @@ EXPORT_SYMBOL(__kcsan_check_access);
*/

#define DEFINE_TSAN_READ_WRITE(size) \
+ void __tsan_read##size(void *ptr); \
void __tsan_read##size(void *ptr) \
{ \
check_access(ptr, size, 0); \
@@ -762,6 +763,7 @@ EXPORT_SYMBOL(__kcsan_check_access);
void __tsan_unaligned_read##size(void *ptr) \
__alias(__tsan_read##size); \
EXPORT_SYMBOL(__tsan_unaligned_read##size); \
+ void __tsan_write##size(void *ptr); \
void __tsan_write##size(void *ptr) \
{ \
check_access(ptr, size, KCSAN_ACCESS_WRITE); \
@@ -777,12 +779,14 @@ DEFINE_TSAN_READ_WRITE(4);
DEFINE_TSAN_READ_WRITE(8);
DEFINE_TSAN_READ_WRITE(16);

+void __tsan_read_range(void *ptr, size_t size);
void __tsan_read_range(void *ptr, size_t size)
{
check_access(ptr, size, 0);
}
EXPORT_SYMBOL(__tsan_read_range);

+void __tsan_write_range(void *ptr, size_t size);
void __tsan_write_range(void *ptr, size_t size)
{
check_access(ptr, size, KCSAN_ACCESS_WRITE);
@@ -799,6 +803,7 @@ EXPORT_SYMBOL(__tsan_write_range);
* the size-check of compiletime_assert_rwonce_type().
*/
#define DEFINE_TSAN_VOLATILE_READ_WRITE(size) \
+ void __tsan_volatile_read##size(void *ptr); \
void __tsan_volatile_read##size(void *ptr) \
{ \
const bool is_atomic = size <= sizeof(long long) && \
@@ -811,6 +816,7 @@ EXPORT_SYMBOL(__tsan_write_range);
void __tsan_unaligned_volatile_read##size(void *ptr) \
__alias(__tsan_volatile_read##size); \
EXPORT_SYMBOL(__tsan_unaligned_volatile_read##size); \
+ void __tsan_volatile_write##size(void *ptr); \
void __tsan_volatile_write##size(void *ptr) \
{ \
const bool is_atomic = size <= sizeof(long long) && \
@@ -836,14 +842,17 @@ DEFINE_TSAN_VOLATILE_READ_WRITE(16);
* The below are not required by KCSAN, but can still be emitted by the
* compiler.
*/
+void __tsan_func_entry(void *call_pc);
void __tsan_func_entry(void *call_pc)
{
}
EXPORT_SYMBOL(__tsan_func_entry);
+void __tsan_func_exit(void);
void __tsan_func_exit(void)
{
}
EXPORT_SYMBOL(__tsan_func_exit);
+void __tsan_init(void);
void __tsan_init(void)
{
}
--
2.27.0.290.gba653c62da-goog

2020-06-16 12:39:43

by Marco Elver

[permalink] [raw]
Subject: [PATCH 4/4] kcsan: Add jiffies test to test suite

Add a test that KCSAN nor the compiler gets confused about accesses to
jiffies on different architectures.

Signed-off-by: Marco Elver <[email protected]>
---
kernel/kcsan/kcsan-test.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/kernel/kcsan/kcsan-test.c b/kernel/kcsan/kcsan-test.c
index 3af420ad6ee7..fed6fcb5768c 100644
--- a/kernel/kcsan/kcsan-test.c
+++ b/kernel/kcsan/kcsan-test.c
@@ -366,6 +366,11 @@ static noinline void test_kernel_read_struct_zero_size(void)
kcsan_check_read(&test_struct.val[3], 0);
}

+static noinline void test_kernel_jiffies_reader(void)
+{
+ sink_value((long)jiffies);
+}
+
static noinline void test_kernel_seqlock_reader(void)
{
unsigned int seq;
@@ -817,6 +822,23 @@ static void test_assert_exclusive_access_scoped(struct kunit *test)
KUNIT_EXPECT_TRUE(test, match_expect_inscope);
}

+/*
+ * jiffies is special (declared to be volatile) and its accesses are typically
+ * not marked; this test ensures that the compiler nor KCSAN gets confused about
+ * jiffies's declaration on different architectures.
+ */
+__no_kcsan
+static void test_jiffies_noreport(struct kunit *test)
+{
+ bool match_never = false;
+
+ begin_test_checks(test_kernel_jiffies_reader, test_kernel_jiffies_reader);
+ do {
+ match_never = report_available();
+ } while (!end_test_checks(match_never));
+ KUNIT_EXPECT_FALSE(test, match_never);
+}
+
/* Test that racing accesses in seqlock critical sections are not reported. */
__no_kcsan
static void test_seqlock_noreport(struct kunit *test)
@@ -867,6 +889,7 @@ static struct kunit_case kcsan_test_cases[] = {
KCSAN_KUNIT_CASE(test_assert_exclusive_bits_nochange),
KCSAN_KUNIT_CASE(test_assert_exclusive_writer_scoped),
KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped),
+ KCSAN_KUNIT_CASE(test_jiffies_noreport),
KCSAN_KUNIT_CASE(test_seqlock_noreport),
{},
};
--
2.27.0.290.gba653c62da-goog

2020-06-16 12:39:51

by Marco Elver

[permalink] [raw]
Subject: [PATCH 3/4] kcsan: Remove existing special atomic rules

Remove existing special atomic rules from kcsan_is_atomic_special()
because they are no longer needed. Since we rely on the compiler
emitting instrumentation distinguishing volatile accesses, the rules
have become redundant.

Let's keep kcsan_is_atomic_special() around, so that we have an obvious
place to add special rules should the need arise in future.

Signed-off-by: Marco Elver <[email protected]>
---
kernel/kcsan/atomic.h | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/kernel/kcsan/atomic.h b/kernel/kcsan/atomic.h
index be9e625227f3..75fe701f4127 100644
--- a/kernel/kcsan/atomic.h
+++ b/kernel/kcsan/atomic.h
@@ -3,8 +3,7 @@
#ifndef _KERNEL_KCSAN_ATOMIC_H
#define _KERNEL_KCSAN_ATOMIC_H

-#include <linux/jiffies.h>
-#include <linux/sched.h>
+#include <linux/types.h>

/*
* Special rules for certain memory where concurrent conflicting accesses are
@@ -13,8 +12,7 @@
*/
static bool kcsan_is_atomic_special(const volatile void *ptr)
{
- /* volatile globals that have been observed in data races. */
- return ptr == &jiffies || ptr == &current->state;
+ return false;
}

#endif /* _KERNEL_KCSAN_ATOMIC_H */
--
2.27.0.290.gba653c62da-goog

2020-06-17 15:58:13

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 0/4] kcsan: Minor cleanups

On Tue, Jun 16, 2020 at 02:36:21PM +0200, Marco Elver wrote:
> Minor KCSAN cleanups, none of which should affect functionality.

Hearing no objections, I have queued and pushed all four, thank you!

Thanx, Paul

> Marco Elver (4):
> kcsan: Silence -Wmissing-prototypes warning with W=1
> kcsan: Rename test.c to selftest.c
> kcsan: Remove existing special atomic rules
> kcsan: Add jiffies test to test suite
>
> kernel/kcsan/Makefile | 2 +-
> kernel/kcsan/atomic.h | 6 ++----
> kernel/kcsan/core.c | 9 +++++++++
> kernel/kcsan/kcsan-test.c | 23 +++++++++++++++++++++++
> kernel/kcsan/{test.c => selftest.c} | 0
> 5 files changed, 35 insertions(+), 5 deletions(-)
> rename kernel/kcsan/{test.c => selftest.c} (100%)
>
> --
> 2.27.0.290.gba653c62da-goog
>