linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] kcsan: Minor cleanups
@ 2020-06-16 12:36 Marco Elver
  2020-06-16 12:36 ` [PATCH 1/4] kcsan: Silence -Wmissing-prototypes warning with W=1 Marco Elver
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Marco Elver @ 2020-06-16 12:36 UTC (permalink / raw)
  To: elver, paulmck; +Cc: dvyukov, kasan-dev, linux-kernel

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] kcsan: Silence -Wmissing-prototypes warning with W=1
  2020-06-16 12:36 [PATCH 0/4] kcsan: Minor cleanups Marco Elver
@ 2020-06-16 12:36 ` Marco Elver
  2020-06-16 12:36 ` [PATCH 2/4] kcsan: Rename test.c to selftest.c Marco Elver
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Marco Elver @ 2020-06-16 12:36 UTC (permalink / raw)
  To: elver, paulmck; +Cc: dvyukov, kasan-dev, linux-kernel, kernel test robot

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%lkp@intel.com
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Marco Elver <elver@google.com>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] kcsan: Rename test.c to selftest.c
  2020-06-16 12:36 [PATCH 0/4] kcsan: Minor cleanups Marco Elver
  2020-06-16 12:36 ` [PATCH 1/4] kcsan: Silence -Wmissing-prototypes warning with W=1 Marco Elver
@ 2020-06-16 12:36 ` Marco Elver
  2020-06-16 12:36 ` [PATCH 3/4] kcsan: Remove existing special atomic rules Marco Elver
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Marco Elver @ 2020-06-16 12:36 UTC (permalink / raw)
  To: elver, paulmck; +Cc: dvyukov, kasan-dev, linux-kernel

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 <elver@google.com>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] kcsan: Remove existing special atomic rules
  2020-06-16 12:36 [PATCH 0/4] kcsan: Minor cleanups Marco Elver
  2020-06-16 12:36 ` [PATCH 1/4] kcsan: Silence -Wmissing-prototypes warning with W=1 Marco Elver
  2020-06-16 12:36 ` [PATCH 2/4] kcsan: Rename test.c to selftest.c Marco Elver
@ 2020-06-16 12:36 ` Marco Elver
  2020-06-16 12:36 ` [PATCH 4/4] kcsan: Add jiffies test to test suite Marco Elver
  2020-06-17 15:55 ` [PATCH 0/4] kcsan: Minor cleanups Paul E. McKenney
  4 siblings, 0 replies; 6+ messages in thread
From: Marco Elver @ 2020-06-16 12:36 UTC (permalink / raw)
  To: elver, paulmck; +Cc: dvyukov, kasan-dev, linux-kernel

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 <elver@google.com>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] kcsan: Add jiffies test to test suite
  2020-06-16 12:36 [PATCH 0/4] kcsan: Minor cleanups Marco Elver
                   ` (2 preceding siblings ...)
  2020-06-16 12:36 ` [PATCH 3/4] kcsan: Remove existing special atomic rules Marco Elver
@ 2020-06-16 12:36 ` Marco Elver
  2020-06-17 15:55 ` [PATCH 0/4] kcsan: Minor cleanups Paul E. McKenney
  4 siblings, 0 replies; 6+ messages in thread
From: Marco Elver @ 2020-06-16 12:36 UTC (permalink / raw)
  To: elver, paulmck; +Cc: dvyukov, kasan-dev, linux-kernel

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

Signed-off-by: Marco Elver <elver@google.com>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/4] kcsan: Minor cleanups
  2020-06-16 12:36 [PATCH 0/4] kcsan: Minor cleanups Marco Elver
                   ` (3 preceding siblings ...)
  2020-06-16 12:36 ` [PATCH 4/4] kcsan: Add jiffies test to test suite Marco Elver
@ 2020-06-17 15:55 ` Paul E. McKenney
  4 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2020-06-17 15:55 UTC (permalink / raw)
  To: Marco Elver; +Cc: dvyukov, kasan-dev, linux-kernel

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
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-06-17 15:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-16 12:36 [PATCH 0/4] kcsan: Minor cleanups Marco Elver
2020-06-16 12:36 ` [PATCH 1/4] kcsan: Silence -Wmissing-prototypes warning with W=1 Marco Elver
2020-06-16 12:36 ` [PATCH 2/4] kcsan: Rename test.c to selftest.c Marco Elver
2020-06-16 12:36 ` [PATCH 3/4] kcsan: Remove existing special atomic rules Marco Elver
2020-06-16 12:36 ` [PATCH 4/4] kcsan: Add jiffies test to test suite Marco Elver
2020-06-17 15:55 ` [PATCH 0/4] kcsan: Minor cleanups Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).