Skip to contentSkip to conversation

kcsan: Minor cleanups

PATCH In review Marco Elver 2 participants 6 messages Jun 16, 2020 – Jun 17, 2020 KCSAN

lore
Share & tools
lore

Marco Elver

author maintainer
#0 lore raw

cover 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%)
Signature · 1 line
2.27.0.290.gba653c62da-goog
author maintainer
#1 lore raw

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.

---
 kernel/kcsan/core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/kcsan/core.c +9 -0
index 15f67949d11e..1866bafda4fd 100644
@@ -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)
 {
 }
Signature · 1 line
2.27.0.290.gba653c62da-goog
author maintainer
#2 lore raw

PATCH 2/4 kcsan: Rename test.c to selftest.c

In reply to Marco Elver

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.

---
 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%)
kernel/kcsan/Makefile +1 -1
index 14533cf24bc3..092ce58d2e56 100644
@@ -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
kernel/kcsan/selftest.c renamed from kernel/kcsan/test.c +0 -0
similarity index 100%
rename from kernel/kcsan/test.c
rename to kernel/kcsan/selftest.c
Signature · 1 line
2.27.0.290.gba653c62da-goog
author maintainer
#3 lore raw

PATCH 3/4 kcsan: Remove existing special atomic rules

In reply to Marco Elver

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.

---
 kernel/kcsan/atomic.h | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/kcsan/atomic.h +2 -4
index be9e625227f3..75fe701f4127 100644
@@ -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 */
Signature · 1 line
2.27.0.290.gba653c62da-goog
author maintainer
#4 lore raw

PATCH 4/4 kcsan: Add jiffies test to test suite

In reply to Marco Elver

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

---
 kernel/kcsan/kcsan-test.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
kernel/kcsan/kcsan-test.c +23 -0
index 3af420ad6ee7..fed6fcb5768c 100644
@@ -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),
 	{},
 };
Signature · 1 line
2.27.0.290.gba653c62da-goog
#5 lore raw
In reply to Marco Elver
Quoted 1 line · Marco Elver

Minor KCSAN cleanups, none of which should affect functionality.

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

Thanx, Paul

Quoted 17 lines

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%)
Signature · 1 line
2.27.0.290.gba653c62da-goog