Skip to contentSkip to conversation

kcsan: Re-add GCC support, and compiler flags improvements

PATCH In review Marco Elver 2 participants 6 messages Jun 18, 2020 – Jun 19, 2020

Subsystems (5)
lore
Share & tools
lore

Marco Elver

author maintainer
#0 lore raw

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

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.

---

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(-)
Documentation/dev-tools/kcsan.rst +2 -1
index ce4bbd918648..8fa0dd6c8614 100644
@@ -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::
 
lib/Kconfig.kcsan +2 -1
index 3f3b5bca7a8f..3d282d51849b 100644
@@ -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>.
scripts/Makefile.kcsan +1 -1
index bd4da1af5953..dd66206f4578 100644
@@ -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
Signature · 1 line
2.27.0.290.gba653c62da-goog
author maintainer
#2 lore raw

PATCH 2/3 kcsan: Simplify compiler flags

In reply to Marco Elver

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.

---
 kernel/kcsan/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/kcsan/Makefile +2 -2
index 092ce58d2e56..fea064afc4f7 100644
@@ -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
Signature · 1 line
2.27.0.290.gba653c62da-goog
author maintainer
#3 lore raw

PATCH 3/3 kcsan: Disable branch tracing in core runtime

In reply to Marco Elver

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/

---
 kernel/kcsan/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kcsan/Makefile +1 -1
index fea064afc4f7..65ca5539c470 100644
@@ -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
Signature · 1 line
2.27.0.290.gba653c62da-goog
#4 lore raw
In reply to Marco Elver
Quoted 5 lines · Marco Elver

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:

Quoted 8 lines
	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

Quoted 14 lines

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(-)
Signature · 1 line
2.27.0.290.gba653c62da-goog
#5 lore raw
Quoted 15 lines · Paul E. McKenney
Quoted 5 lines · Marco Elver

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:

Quoted 8 lines
	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
Quoted 2 lines

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

Hearing no objections, queued and pushd, thank you!

Thanx, Paul

Quoted 14 lines
Quoted 14 lines

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