2020-06-23 04:15:38

by Paul E. McKenney

[permalink] [raw]
Subject: [PATCH kcsan 0/10] KCSAN updates for v5.9

Hello!

This series provides KCSAN updates:

1. Annotate a data race in vm_area_dup(), courtesy of Qian Cai.

2. x86/mm/pat: Mark an intentional data race, courtesy of Qian Cai.

3. Add ASSERT_EXCLUSIVE_ACCESS() to __list_splice_init_rcu().

4. Add test suite, courtesy of Marco Elver.

5. locking/osq_lock: Annotate a data race in osq_lock.

6. Prefer '__no_kcsan inline' in test, courtesy of Marco Elver.

7. Silence -Wmissing-prototypes warning with W=1, courtesy of Qian Cai.

8. Rename test.c to selftest.c, courtesy of Marco Elver.

9. Remove existing special atomic rules, courtesy of Marco Elver.

10. Add jiffies test to test suite, courtesy of Marco Elver.

Thanx, Paul

------------------------------------------------------------------------

arch/x86/mm/pat/set_memory.c | 2
include/linux/rculist.h | 2
kernel/fork.c | 8
kernel/kcsan/Makefile | 5
kernel/kcsan/atomic.h | 6
kernel/kcsan/core.c | 9
kernel/kcsan/kcsan-test.c | 1111 ++++++++++++++++++++++++++++++++++++++++++-
kernel/kcsan/selftest.c | 1
kernel/locking/osq_lock.c | 6
lib/Kconfig.kcsan | 23
10 files changed, 1161 insertions(+), 12 deletions(-)


2020-06-23 06:34:31

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH kcsan 0/10] KCSAN updates for v5.9

On Tue, 23 Jun 2020 at 02:43, Paul E. McKenney <[email protected]> wrote:
>
> Hello!
>
> This series provides KCSAN updates:
>
> 1. Annotate a data race in vm_area_dup(), courtesy of Qian Cai.
>
> 2. x86/mm/pat: Mark an intentional data race, courtesy of Qian Cai.
>
> 3. Add ASSERT_EXCLUSIVE_ACCESS() to __list_splice_init_rcu().
>
> 4. Add test suite, courtesy of Marco Elver.
>
> 5. locking/osq_lock: Annotate a data race in osq_lock.
>
> 6. Prefer '__no_kcsan inline' in test, courtesy of Marco Elver.
>
> 7. Silence -Wmissing-prototypes warning with W=1, courtesy of Qian Cai.
>
> 8. Rename test.c to selftest.c, courtesy of Marco Elver.
>
> 9. Remove existing special atomic rules, courtesy of Marco Elver.
>
> 10. Add jiffies test to test suite, courtesy of Marco Elver.

Do we want GCC support back for 5.9?

https://lkml.kernel.org/r/[email protected]

I was hoping it could go into 5.9, because it makes a big difference
in terms of usability as it provides more compiler choice. The only
significant change for GCC support is the addition of the checking of
(CC_IS_GCC && (....)).

Thanks,
-- Marco

2020-06-23 13:47:36

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH kcsan 0/10] KCSAN updates for v5.9

On Tue, Jun 23, 2020 at 08:31:15AM +0200, Marco Elver wrote:
> On Tue, 23 Jun 2020 at 02:43, Paul E. McKenney <[email protected]> wrote:
> >
> > Hello!
> >
> > This series provides KCSAN updates:
> >
> > 1. Annotate a data race in vm_area_dup(), courtesy of Qian Cai.
> >
> > 2. x86/mm/pat: Mark an intentional data race, courtesy of Qian Cai.
> >
> > 3. Add ASSERT_EXCLUSIVE_ACCESS() to __list_splice_init_rcu().
> >
> > 4. Add test suite, courtesy of Marco Elver.
> >
> > 5. locking/osq_lock: Annotate a data race in osq_lock.
> >
> > 6. Prefer '__no_kcsan inline' in test, courtesy of Marco Elver.
> >
> > 7. Silence -Wmissing-prototypes warning with W=1, courtesy of Qian Cai.
> >
> > 8. Rename test.c to selftest.c, courtesy of Marco Elver.
> >
> > 9. Remove existing special atomic rules, courtesy of Marco Elver.
> >
> > 10. Add jiffies test to test suite, courtesy of Marco Elver.
>
> Do we want GCC support back for 5.9?
>
> https://lkml.kernel.org/r/[email protected]
>
> I was hoping it could go into 5.9, because it makes a big difference
> in terms of usability as it provides more compiler choice. The only
> significant change for GCC support is the addition of the checking of
> (CC_IS_GCC && (....)).

Very good, I will rebase the following into the KCSAN branch for v5.9:

3e490e3 kcsan: Re-add GCC as a supported compiler
03296de kcsan: Simplify compiler flags
d831090 kcsan: Disable branch tracing in core runtime

Please let me know if any other adjustments are needed.

Thanx, Paul

2020-06-23 15:09:07

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH kcsan 0/10] KCSAN updates for v5.9

On Tue, 23 Jun 2020 at 15:43, Paul E. McKenney <[email protected]> wrote:
>
> On Tue, Jun 23, 2020 at 08:31:15AM +0200, Marco Elver wrote:
> > On Tue, 23 Jun 2020 at 02:43, Paul E. McKenney <[email protected]> wrote:
> > >
> > > Hello!
> > >
> > > This series provides KCSAN updates:
> > >
> > > 1. Annotate a data race in vm_area_dup(), courtesy of Qian Cai.
> > >
> > > 2. x86/mm/pat: Mark an intentional data race, courtesy of Qian Cai.
> > >
> > > 3. Add ASSERT_EXCLUSIVE_ACCESS() to __list_splice_init_rcu().
> > >
> > > 4. Add test suite, courtesy of Marco Elver.
> > >
> > > 5. locking/osq_lock: Annotate a data race in osq_lock.
> > >
> > > 6. Prefer '__no_kcsan inline' in test, courtesy of Marco Elver.
> > >
> > > 7. Silence -Wmissing-prototypes warning with W=1, courtesy of Qian Cai.
> > >
> > > 8. Rename test.c to selftest.c, courtesy of Marco Elver.
> > >
> > > 9. Remove existing special atomic rules, courtesy of Marco Elver.
> > >
> > > 10. Add jiffies test to test suite, courtesy of Marco Elver.
> >
> > Do we want GCC support back for 5.9?
> >
> > https://lkml.kernel.org/r/[email protected]
> >
> > I was hoping it could go into 5.9, because it makes a big difference
> > in terms of usability as it provides more compiler choice. The only
> > significant change for GCC support is the addition of the checking of
> > (CC_IS_GCC && (....)).
>
> Very good, I will rebase the following into the KCSAN branch for v5.9:
>
> 3e490e3 kcsan: Re-add GCC as a supported compiler
> 03296de kcsan: Simplify compiler flags
> d831090 kcsan: Disable branch tracing in core runtime
>
> Please let me know if any other adjustments are needed.

Looks good to me, thank you!

Thanks,
-- Marco

2020-06-23 16:33:59

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH kcsan 0/10] KCSAN updates for v5.9

On Tue, Jun 23, 2020 at 05:06:26PM +0200, Marco Elver wrote:
> On Tue, 23 Jun 2020 at 15:43, Paul E. McKenney <[email protected]> wrote:
> >
> > On Tue, Jun 23, 2020 at 08:31:15AM +0200, Marco Elver wrote:
> > > On Tue, 23 Jun 2020 at 02:43, Paul E. McKenney <[email protected]> wrote:
> > > >
> > > > Hello!
> > > >
> > > > This series provides KCSAN updates:
> > > >
> > > > 1. Annotate a data race in vm_area_dup(), courtesy of Qian Cai.
> > > >
> > > > 2. x86/mm/pat: Mark an intentional data race, courtesy of Qian Cai.
> > > >
> > > > 3. Add ASSERT_EXCLUSIVE_ACCESS() to __list_splice_init_rcu().
> > > >
> > > > 4. Add test suite, courtesy of Marco Elver.
> > > >
> > > > 5. locking/osq_lock: Annotate a data race in osq_lock.
> > > >
> > > > 6. Prefer '__no_kcsan inline' in test, courtesy of Marco Elver.
> > > >
> > > > 7. Silence -Wmissing-prototypes warning with W=1, courtesy of Qian Cai.
> > > >
> > > > 8. Rename test.c to selftest.c, courtesy of Marco Elver.
> > > >
> > > > 9. Remove existing special atomic rules, courtesy of Marco Elver.
> > > >
> > > > 10. Add jiffies test to test suite, courtesy of Marco Elver.
> > >
> > > Do we want GCC support back for 5.9?
> > >
> > > https://lkml.kernel.org/r/[email protected]
> > >
> > > I was hoping it could go into 5.9, because it makes a big difference
> > > in terms of usability as it provides more compiler choice. The only
> > > significant change for GCC support is the addition of the checking of
> > > (CC_IS_GCC && (....)).
> >
> > Very good, I will rebase the following into the KCSAN branch for v5.9:
> >
> > 3e490e3 kcsan: Re-add GCC as a supported compiler
> > 03296de kcsan: Simplify compiler flags
> > d831090 kcsan: Disable branch tracing in core runtime
> >
> > Please let me know if any other adjustments are needed.
>
> Looks good to me, thank you!

And updated on the "dev" branch of -rcu.

Thanx, Paul

2020-06-24 19:03:40

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH kcsan 0/10] KCSAN updates for v5.9

On Mon, Jun 22, 2020 at 05:43:10PM -0700, Paul E. McKenney wrote:
> Hello!
>
> This series provides KCSAN updates:

And three more, so that GCC can join Clang in the KCSAN fun.

> 1. Annotate a data race in vm_area_dup(), courtesy of Qian Cai.
>
> 2. x86/mm/pat: Mark an intentional data race, courtesy of Qian Cai.
>
> 3. Add ASSERT_EXCLUSIVE_ACCESS() to __list_splice_init_rcu().
>
> 4. Add test suite, courtesy of Marco Elver.
>
> 5. locking/osq_lock: Annotate a data race in osq_lock.
>
> 6. Prefer '__no_kcsan inline' in test, courtesy of Marco Elver.
>
> 7. Silence -Wmissing-prototypes warning with W=1, courtesy of Qian Cai.
>
> 8. Rename test.c to selftest.c, courtesy of Marco Elver.
>
> 9. Remove existing special atomic rules, courtesy of Marco Elver.
>
> 10. Add jiffies test to test suite, courtesy of Marco Elver.

11. Re-add GCC as a supported compiler.

12. Simplify compiler flags.

13. Disable branch tracing in core runtime.

Please note that using GCC for KCSAN requires building your own compiler
from recent mainline.

Thanx, Paul

------------------------------------------------------------------------
The added three (#11-#13) only:
------------------------------------------------------------------------

Documentation/dev-tools/kcsan.rst | 3 ++-
kernel/kcsan/Makefile | 6 +++---
lib/Kconfig.kcsan | 3 ++-
scripts/Makefile.kcsan | 2 +-
4 files changed, 8 insertions(+), 6 deletions(-)

2020-06-24 19:04:27

by Paul E. McKenney

[permalink] [raw]
Subject: [PATCH kcsan 1/3] kcsan: Re-add GCC as a supported compiler

From: Marco Elver <[email protected]>

GCC version 11 recently implemented all requirements to correctly
support KCSAN:

1. Correct no_sanitize-attribute inlining behaviour:
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=4089df8ef4a63126b0774c39b6638845244c20d2

2. --param=tsan-distinguish-volatile
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ab2789ec507a94f1a75a6534bca51c7b39037ce0

3. --param=tsan-instrument-func-entry-exit
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=06712fc68dc9843d9af7c7ac10047f49d305ad76

Therefore, we can re-enable GCC for KCSAN, and document the new compiler
requirements.

Signed-off-by: Marco Elver <[email protected]>
Cc: Martin Liska <[email protected]>
Signed-off-by: Paul E. McKenney <[email protected]>
---
Documentation/dev-tools/kcsan.rst | 3 ++-
lib/Kconfig.kcsan | 3 ++-
scripts/Makefile.kcsan | 2 +-
3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/Documentation/dev-tools/kcsan.rst b/Documentation/dev-tools/kcsan.rst
index ce4bbd9..8fa0dd6 100644
--- a/Documentation/dev-tools/kcsan.rst
+++ b/Documentation/dev-tools/kcsan.rst
@@ -8,7 +8,8 @@ approach to detect races. KCSAN's primary purpose is to detect `data races`_.
Usage
-----

-KCSAN requires Clang version 11 or later.
+KCSAN is supported by both GCC and Clang. With GCC we require version 11 or
+later, and with Clang also require version 11 or later.

To enable KCSAN configure the kernel with::

diff --git a/lib/Kconfig.kcsan b/lib/Kconfig.kcsan
index 3f3b5bc..3d282d5 100644
--- a/lib/Kconfig.kcsan
+++ b/lib/Kconfig.kcsan
@@ -4,7 +4,8 @@ config HAVE_ARCH_KCSAN
bool

config HAVE_KCSAN_COMPILER
- def_bool CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-distinguish-volatile=1)
+ def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=thread -mllvm -tsan-distinguish-volatile=1)) || \
+ (CC_IS_GCC && $(cc-option,-fsanitize=thread --param tsan-distinguish-volatile=1))
help
For the list of compilers that support KCSAN, please see
<file:Documentation/dev-tools/kcsan.rst>.
diff --git a/scripts/Makefile.kcsan b/scripts/Makefile.kcsan
index bd4da1a..dd66206 100644
--- a/scripts/Makefile.kcsan
+++ b/scripts/Makefile.kcsan
@@ -6,7 +6,7 @@ ifdef CONFIG_KCSAN
ifdef CONFIG_CC_IS_CLANG
cc-param = -mllvm -$(1)
else
-cc-param = --param -$(1)
+cc-param = --param $(1)
endif

# Keep most options here optional, to allow enabling more compilers if absence
--
2.9.5

2020-06-24 19:05:41

by Paul E. McKenney

[permalink] [raw]
Subject: [PATCH kcsan 2/3] kcsan: Simplify compiler flags

From: Marco Elver <[email protected]>

Simplify the set of compiler flags for the runtime by removing cc-option
from -fno-stack-protector, because all supported compilers support it.
This saves us one compiler invocation during build.

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

diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
index 092ce58..fea064a 100644
--- a/kernel/kcsan/Makefile
+++ b/kernel/kcsan/Makefile
@@ -7,8 +7,8 @@ CFLAGS_REMOVE_core.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_debugfs.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_report.o = $(CC_FLAGS_FTRACE)

-CFLAGS_core.o := $(call cc-option,-fno-conserve-stack,) \
- $(call cc-option,-fno-stack-protector,)
+CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \
+ -fno-stack-protector

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

2020-06-24 19:07:58

by Paul E. McKenney

[permalink] [raw]
Subject: [PATCH kcsan 3/3] kcsan: Disable branch tracing in core runtime

From: Marco Elver <[email protected]>

Disable branch tracing in core KCSAN runtime if branches are being
traced (TRACE_BRANCH_PROFILING). This it to avoid its performance
impact, but also avoid recursion in case KCSAN is enabled for the branch
tracing runtime.

The latter had already been a problem for KASAN:
https://lore.kernel.org/lkml/CANpmjNOeXmD5E3O50Z3MjkiuCYaYOPyi+1rq=GZvEKwBvLR0Ug@mail.gmail.com/

Signed-off-by: Marco Elver <[email protected]>
Signed-off-by: Paul E. McKenney <[email protected]>
---
kernel/kcsan/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
index fea064a..65ca553 100644
--- a/kernel/kcsan/Makefile
+++ b/kernel/kcsan/Makefile
@@ -8,7 +8,7 @@ CFLAGS_REMOVE_debugfs.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_report.o = $(CC_FLAGS_FTRACE)

CFLAGS_core.o := $(call cc-option,-fno-conserve-stack) \
- -fno-stack-protector
+ -fno-stack-protector -DDISABLE_BRANCH_PROFILING

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

2020-06-24 20:11:06

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH kcsan 0/10] KCSAN updates for v5.9

On Wed, Jun 24, 2020 at 12:02:36PM -0700, Paul E. McKenney wrote:
> On Mon, Jun 22, 2020 at 05:43:10PM -0700, Paul E. McKenney wrote:
> > Hello!
> >
> > This series provides KCSAN updates:
>
> And three more, so that GCC can join Clang in the KCSAN fun.
>
> > 1. Annotate a data race in vm_area_dup(), courtesy of Qian Cai.
> >
> > 2. x86/mm/pat: Mark an intentional data race, courtesy of Qian Cai.
> >
> > 3. Add ASSERT_EXCLUSIVE_ACCESS() to __list_splice_init_rcu().
> >
> > 4. Add test suite, courtesy of Marco Elver.
> >
> > 5. locking/osq_lock: Annotate a data race in osq_lock.
> >
> > 6. Prefer '__no_kcsan inline' in test, courtesy of Marco Elver.
> >
> > 7. Silence -Wmissing-prototypes warning with W=1, courtesy of Qian Cai.
> >
> > 8. Rename test.c to selftest.c, courtesy of Marco Elver.
> >
> > 9. Remove existing special atomic rules, courtesy of Marco Elver.
> >
> > 10. Add jiffies test to test suite, courtesy of Marco Elver.
>
> 11. Re-add GCC as a supported compiler.
>
> 12. Simplify compiler flags.
>
> 13. Disable branch tracing in core runtime.

All three of which, I should hasten to add, are courtesy of Marco Elver.

> Please note that using GCC for KCSAN requires building your own compiler
> from recent mainline.

Thanx, Paul

> ------------------------------------------------------------------------
> The added three (#11-#13) only:
> ------------------------------------------------------------------------
>
> Documentation/dev-tools/kcsan.rst | 3 ++-
> kernel/kcsan/Makefile | 6 +++---
> lib/Kconfig.kcsan | 3 ++-
> scripts/Makefile.kcsan | 2 +-
> 4 files changed, 8 insertions(+), 6 deletions(-)