x86, kcsan: Enable KCSAN for x86
authorMarco Elver <elver@google.com>
Thu, 14 Nov 2019 18:03:03 +0000 (19:03 +0100)
committerPaul E. McKenney <paulmck@kernel.org>
Sat, 16 Nov 2019 15:23:16 +0000 (07:23 -0800)
This patch enables KCSAN for x86, with updates to build rules to not use
KCSAN for several incompatible compilation units.

Signed-off-by: Marco Elver <elver@google.com>
Acked-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
12 files changed:
arch/x86/Kconfig
arch/x86/boot/Makefile
arch/x86/boot/compressed/Makefile
arch/x86/entry/vdso/Makefile
arch/x86/include/asm/bitops.h
arch/x86/kernel/Makefile
arch/x86/kernel/cpu/Makefile
arch/x86/lib/Makefile
arch/x86/mm/Makefile
arch/x86/purgatory/Makefile
arch/x86/realmode/Makefile
arch/x86/realmode/rm/Makefile

index d6e1faa28c58ebaea297e8e450e9edf2319b3a1a..81859be4a005e101e0e8b4a20f749a3b2aa55c87 100644 (file)
@@ -226,6 +226,7 @@ config X86
        select VIRT_TO_BUS
        select X86_FEATURE_NAMES                if PROC_FS
        select PROC_PID_ARCH_STATUS             if PROC_FS
+       select HAVE_ARCH_KCSAN if X86_64
 
 config INSTRUCTION_DECODER
        def_bool y
index e2839b5c246c21e45ee2f783e92c2abb04bb93ea..9c79427941646d3498787206ebb98fe5fc4bae60 100644 (file)
@@ -9,7 +9,9 @@
 # Changed by many, many contributors over the years.
 #
 
+# Sanitizer runtimes are unavailable and cannot be linked for early boot code.
 KASAN_SANITIZE                 := n
+KCSAN_SANITIZE                 := n
 OBJECT_FILES_NON_STANDARD      := y
 
 # Kernel does not boot with kcov instrumentation here.
index 6b84afdd75382c2ee3d0aa0f514784c2fd9e87ea..a1c248b8439f35deef76eb2c74898c0f6d55a2a2 100644 (file)
@@ -17,7 +17,9 @@
 #      (see scripts/Makefile.lib size_append)
 #      compressed vmlinux.bin.all + u32 size of vmlinux.bin.all
 
+# Sanitizer runtimes are unavailable and cannot be linked for early boot code.
 KASAN_SANITIZE                 := n
+KCSAN_SANITIZE                 := n
 OBJECT_FILES_NON_STANDARD      := y
 
 # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
index 0f2154106d01686170056cbdf0ddb76a431bef4a..a23debaad5b91aa0fc0574a5fa63dfea270ccd2e 100644 (file)
@@ -10,8 +10,11 @@ ARCH_REL_TYPE_ABS += R_386_GLOB_DAT|R_386_JMP_SLOT|R_386_RELATIVE
 include $(srctree)/lib/vdso/Makefile
 
 KBUILD_CFLAGS += $(DISABLE_LTO)
+
+# Sanitizer runtimes are unavailable and cannot be linked here.
 KASAN_SANITIZE                 := n
 UBSAN_SANITIZE                 := n
+KCSAN_SANITIZE                 := n
 OBJECT_FILES_NON_STANDARD      := y
 
 # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
index 7d1f6a49bfae1192d25a672e34a438ca65288a89..542b63ddc8aa48487a735f78997353cc5b312e5a 100644 (file)
@@ -201,8 +201,12 @@ arch_test_and_change_bit(long nr, volatile unsigned long *addr)
        return GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, c, "Ir", nr);
 }
 
-static __always_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
+static __no_kcsan_or_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
 {
+       /*
+        * Because this is a plain access, we need to disable KCSAN here to
+        * avoid double instrumentation via instrumented bitops.
+        */
        return ((1UL << (nr & (BITS_PER_LONG-1))) &
                (addr[nr >> _BITOPS_LONG_SHIFT])) != 0;
 }
index 3578ad248bc98319e3a0f5769615c72e191d5569..a9a1cab437bcaa329645a4fda5a4d518806e6fe7 100644 (file)
@@ -28,6 +28,10 @@ KASAN_SANITIZE_dumpstack_$(BITS).o                   := n
 KASAN_SANITIZE_stacktrace.o                            := n
 KASAN_SANITIZE_paravirt.o                              := n
 
+# With some compiler versions the generated code results in boot hangs, caused
+# by several compilation units. To be safe, disable all instrumentation.
+KCSAN_SANITIZE := n
+
 OBJECT_FILES_NON_STANDARD_relocate_kernel_$(BITS).o    := y
 OBJECT_FILES_NON_STANDARD_test_nx.o                    := y
 OBJECT_FILES_NON_STANDARD_paravirt_patch.o             := y
index d7a1e5a9331ca82216a5a5c6f93504c3a347c385..1f1b0edc018731fd804fbdaf4d304e1c26dbf2ea 100644 (file)
@@ -13,6 +13,9 @@ endif
 KCOV_INSTRUMENT_common.o := n
 KCOV_INSTRUMENT_perf_event.o := n
 
+# As above, instrumenting secondary CPU boot code causes boot hangs.
+KCSAN_SANITIZE_common.o := n
+
 # Make sure load_percpu_segment has no stackprotector
 nostackp := $(call cc-option, -fno-stack-protector)
 CFLAGS_common.o                := $(nostackp)
index 5246db42de4576e7f0bbbf5899bed8ab6f06cb74..432a0770567756f8ae175e2f4fd1150d82567432 100644 (file)
@@ -6,10 +6,14 @@
 # Produces uninteresting flaky coverage.
 KCOV_INSTRUMENT_delay.o        := n
 
+# KCSAN uses udelay for introducing watchpoint delay; avoid recursion.
+KCSAN_SANITIZE_delay.o := n
+
 # Early boot use of cmdline; don't instrument it
 ifdef CONFIG_AMD_MEM_ENCRYPT
 KCOV_INSTRUMENT_cmdline.o := n
 KASAN_SANITIZE_cmdline.o  := n
+KCSAN_SANITIZE_cmdline.o  := n
 
 ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_cmdline.o = -pg
index 84373dc9b341e6541647eebcbf2194c771ace8f2..3559f4297ee1be1725e0440255a241471d4b8f18 100644 (file)
@@ -7,6 +7,10 @@ KCOV_INSTRUMENT_mem_encrypt_identity.o := n
 KASAN_SANITIZE_mem_encrypt.o           := n
 KASAN_SANITIZE_mem_encrypt_identity.o  := n
 
+# Disable KCSAN entirely, because otherwise we get warnings that some functions
+# reference __initdata sections.
+KCSAN_SANITIZE := n
+
 ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_mem_encrypt.o            = -pg
 CFLAGS_REMOVE_mem_encrypt_identity.o   = -pg
index fb4ee54443799363029a258e0b1b0bf01445f741..69379bce9574c35721a30ce9a23b527f9b659a8a 100644 (file)
@@ -17,7 +17,9 @@ CFLAGS_sha256.o := -D__DISABLE_EXPORTS
 LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib
 targets += purgatory.ro
 
+# Sanitizer runtimes are unavailable and cannot be linked here.
 KASAN_SANITIZE := n
+KCSAN_SANITIZE := n
 KCOV_INSTRUMENT := n
 
 # These are adjustments to the compiler flags used for objects that
index 682c895753d96a7c3a9b231cbf5f01dcbc8420ba..6b1f3a4eeb44e9875db40e188756d798186d48fa 100644 (file)
@@ -6,7 +6,10 @@
 # for more details.
 #
 #
+
+# Sanitizer runtimes are unavailable and cannot be linked here.
 KASAN_SANITIZE                 := n
+KCSAN_SANITIZE                 := n
 OBJECT_FILES_NON_STANDARD      := y
 
 subdir- := rm
index f60501a384f947b32941ad6a146491685161956b..fdbbb945c2168ce0569dc3c15b15c091b789c63d 100644 (file)
@@ -6,7 +6,10 @@
 # for more details.
 #
 #
+
+# Sanitizer runtimes are unavailable and cannot be linked here.
 KASAN_SANITIZE                 := n
+KCSAN_SANITIZE                 := n
 OBJECT_FILES_NON_STANDARD      := y
 
 # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.