2020-06-18 09:34:14

by Marco Elver

[permalink] [raw]
Subject: [PATCH 0/3] kcsan: Re-add GCC support, and compiler flags improvements

Re-add GCC as a supported compiler and clean up compiler flags.

To use KCSAN with GCC before GCC 11 is released, the following will get
a stable GCC 10 and cherry-pick the patches required for KCSAN support:

git clone git://gcc.gnu.org/git/gcc.git && cd gcc
git checkout -b gcc-10-for-kcsan releases/gcc-10.1.0
git cherry-pick \
4089df8ef4a63126b0774c39b6638845244c20d2 \
ab2789ec507a94f1a75a6534bca51c7b39037ce0 \
06712fc68dc9843d9af7c7ac10047f49d305ad76
./configure --prefix <your-prefix> --enable-languages=c,c++
make -j$(nproc) && make install

Marco Elver (3):
kcsan: Re-add GCC as a supported compiler
kcsan: Simplify compiler flags
kcsan: Disable branch tracing in core runtime

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

--
2.27.0.290.gba653c62da-goog


2020-06-18 09:34:29

by Marco Elver

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

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]>
---
kernel/kcsan/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
index fea064afc4f7..65ca5539c470 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.27.0.290.gba653c62da-goog

2020-06-18 10:03:50

by Marco Elver

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

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]>
---

For anyone interested to try it out before GCC 11 is released, I'd
recommend building a stable GCC 10 with the patches applied:

git clone git://gcc.gnu.org/git/gcc.git && cd gcc
git checkout -b gcc-10-for-kcsan releases/gcc-10.1.0
git cherry-pick \
4089df8ef4a63126b0774c39b6638845244c20d2 \
ab2789ec507a94f1a75a6534bca51c7b39037ce0 \
06712fc68dc9843d9af7c7ac10047f49d305ad76
./configure --prefix <your-prefix> --enable-languages=c,c++
make -j$(nproc) && make install
---
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 ce4bbd918648..8fa0dd6c8614 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 3f3b5bca7a8f..3d282d51849b 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 bd4da1af5953..dd66206f4578 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.27.0.290.gba653c62da-goog

2020-06-18 17:50:07

by Marco Elver

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

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]>
---
kernel/kcsan/Makefile | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
index 092ce58d2e56..fea064afc4f7 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.27.0.290.gba653c62da-goog

2020-06-19 03:39:53

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 0/3] kcsan: Re-add GCC support, and compiler flags improvements

On Thu, Jun 18, 2020 at 11:31:15AM +0200, Marco Elver wrote:
> Re-add GCC as a supported compiler and clean up compiler flags.
>
> To use KCSAN with GCC before GCC 11 is released, the following will get
> a stable GCC 10 and cherry-pick the patches required for KCSAN support:
>
> git clone git://gcc.gnu.org/git/gcc.git && cd gcc
> git checkout -b gcc-10-for-kcsan releases/gcc-10.1.0
> git cherry-pick \
> 4089df8ef4a63126b0774c39b6638845244c20d2 \
> ab2789ec507a94f1a75a6534bca51c7b39037ce0 \
> 06712fc68dc9843d9af7c7ac10047f49d305ad76
> ./configure --prefix <your-prefix> --enable-languages=c,c++
> make -j$(nproc) && make install

Unless there are objections, I will pull this in Friday (tomorrow)
afternoon, Pacific Time.

Thanx, Paul

> Marco Elver (3):
> kcsan: Re-add GCC as a supported compiler
> kcsan: Simplify compiler flags
> kcsan: Disable branch tracing in core runtime
>
> Documentation/dev-tools/kcsan.rst | 3 ++-
> kernel/kcsan/Makefile | 4 ++--
> lib/Kconfig.kcsan | 3 ++-
> scripts/Makefile.kcsan | 2 +-
> 4 files changed, 7 insertions(+), 5 deletions(-)
>
> --
> 2.27.0.290.gba653c62da-goog
>

2020-06-20 04:48:40

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 0/3] kcsan: Re-add GCC support, and compiler flags improvements

On Thu, Jun 18, 2020 at 08:25:30PM -0700, Paul E. McKenney wrote:
> On Thu, Jun 18, 2020 at 11:31:15AM +0200, Marco Elver wrote:
> > Re-add GCC as a supported compiler and clean up compiler flags.
> >
> > To use KCSAN with GCC before GCC 11 is released, the following will get
> > a stable GCC 10 and cherry-pick the patches required for KCSAN support:
> >
> > git clone git://gcc.gnu.org/git/gcc.git && cd gcc
> > git checkout -b gcc-10-for-kcsan releases/gcc-10.1.0
> > git cherry-pick \
> > 4089df8ef4a63126b0774c39b6638845244c20d2 \
> > ab2789ec507a94f1a75a6534bca51c7b39037ce0 \
> > 06712fc68dc9843d9af7c7ac10047f49d305ad76
> > ./configure --prefix <your-prefix> --enable-languages=c,c++
> > make -j$(nproc) && make install
>
> Unless there are objections, I will pull this in Friday (tomorrow)
> afternoon, Pacific Time.

Hearing no objections, queued and pushd, thank you!

Thanx, Paul

> > Marco Elver (3):
> > kcsan: Re-add GCC as a supported compiler
> > kcsan: Simplify compiler flags
> > kcsan: Disable branch tracing in core runtime
> >
> > Documentation/dev-tools/kcsan.rst | 3 ++-
> > kernel/kcsan/Makefile | 4 ++--
> > lib/Kconfig.kcsan | 3 ++-
> > scripts/Makefile.kcsan | 2 +-
> > 4 files changed, 7 insertions(+), 5 deletions(-)
> >
> > --
> > 2.27.0.290.gba653c62da-goog
> >