Skip to content

Commit 40d0411

Browse files
melverpaulmckrcu
authored andcommitted
x86, kcsan: Enable KCSAN for x86
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>
1 parent e75a679 commit 40d0411

12 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎arch/x86/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ config X86
226226
select VIRT_TO_BUS
227227
select X86_FEATURE_NAMES if PROC_FS
228228
select PROC_PID_ARCH_STATUS if PROC_FS
229+
select HAVE_ARCH_KCSAN if X86_64
229230

230231
config INSTRUCTION_DECODER
231232
def_bool y

‎arch/x86/boot/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
# Changed by many, many contributors over the years.
1010
#
1111

12+
# Sanitizer runtimes are unavailable and cannot be linked for early boot code.
1213
KASAN_SANITIZE := n
14+
KCSAN_SANITIZE := n
1315
OBJECT_FILES_NON_STANDARD := y
1416

1517
# Kernel does not boot with kcov instrumentation here.

‎arch/x86/boot/compressed/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
# (see scripts/Makefile.lib size_append)
1818
# compressed vmlinux.bin.all + u32 size of vmlinux.bin.all
1919

20+
# Sanitizer runtimes are unavailable and cannot be linked for early boot code.
2021
KASAN_SANITIZE := n
22+
KCSAN_SANITIZE := n
2123
OBJECT_FILES_NON_STANDARD := y
2224

2325
# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.

‎arch/x86/entry/vdso/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ ARCH_REL_TYPE_ABS += R_386_GLOB_DAT|R_386_JMP_SLOT|R_386_RELATIVE
1010
include $(srctree)/lib/vdso/Makefile
1111

1212
KBUILD_CFLAGS += $(DISABLE_LTO)
13+
14+
# Sanitizer runtimes are unavailable and cannot be linked here.
1315
KASAN_SANITIZE := n
1416
UBSAN_SANITIZE := n
17+
KCSAN_SANITIZE := n
1518
OBJECT_FILES_NON_STANDARD := y
1619

1720
# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.

‎arch/x86/include/asm/bitops.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ arch_test_and_change_bit(long nr, volatile unsigned long *addr)
201201
return GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, c, "Ir", nr);
202202
}
203203

204-
static __always_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
204+
static __no_kcsan_or_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
205205
{
206+
/*
207+
* Because this is a plain access, we need to disable KCSAN here to
208+
* avoid double instrumentation via instrumented bitops.
209+
*/
206210
return ((1UL << (nr & (BITS_PER_LONG-1))) &
207211
(addr[nr >> _BITOPS_LONG_SHIFT])) != 0;
208212
}

‎arch/x86/kernel/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ KASAN_SANITIZE_dumpstack_$(BITS).o := n
2828
KASAN_SANITIZE_stacktrace.o := n
2929
KASAN_SANITIZE_paravirt.o := n
3030

31+
# With some compiler versions the generated code results in boot hangs, caused
32+
# by several compilation units. To be safe, disable all instrumentation.
33+
KCSAN_SANITIZE := n
34+
3135
OBJECT_FILES_NON_STANDARD_relocate_kernel_$(BITS).o := y
3236
OBJECT_FILES_NON_STANDARD_test_nx.o := y
3337
OBJECT_FILES_NON_STANDARD_paravirt_patch.o := y

‎arch/x86/kernel/cpu/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ endif
1313
KCOV_INSTRUMENT_common.o := n
1414
KCOV_INSTRUMENT_perf_event.o := n
1515

16+
# As above, instrumenting secondary CPU boot code causes boot hangs.
17+
KCSAN_SANITIZE_common.o := n
18+
1619
# Make sure load_percpu_segment has no stackprotector
1720
nostackp := $(call cc-option, -fno-stack-protector)
1821
CFLAGS_common.o := $(nostackp)

‎arch/x86/lib/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
# Produces uninteresting flaky coverage.
77
KCOV_INSTRUMENT_delay.o := n
88

9+
# KCSAN uses udelay for introducing watchpoint delay; avoid recursion.
10+
KCSAN_SANITIZE_delay.o := n
11+
912
# Early boot use of cmdline; don't instrument it
1013
ifdef CONFIG_AMD_MEM_ENCRYPT
1114
KCOV_INSTRUMENT_cmdline.o := n
1215
KASAN_SANITIZE_cmdline.o := n
16+
KCSAN_SANITIZE_cmdline.o := n
1317

1418
ifdef CONFIG_FUNCTION_TRACER
1519
CFLAGS_REMOVE_cmdline.o = -pg

‎arch/x86/mm/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ KCOV_INSTRUMENT_mem_encrypt_identity.o := n
77
KASAN_SANITIZE_mem_encrypt.o := n
88
KASAN_SANITIZE_mem_encrypt_identity.o := n
99

10+
# Disable KCSAN entirely, because otherwise we get warnings that some functions
11+
# reference __initdata sections.
12+
KCSAN_SANITIZE := n
13+
1014
ifdef CONFIG_FUNCTION_TRACER
1115
CFLAGS_REMOVE_mem_encrypt.o = -pg
1216
CFLAGS_REMOVE_mem_encrypt_identity.o = -pg

‎arch/x86/purgatory/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ CFLAGS_sha256.o := -D__DISABLE_EXPORTS
1717
LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib
1818
targets += purgatory.ro
1919

20+
# Sanitizer runtimes are unavailable and cannot be linked here.
2021
KASAN_SANITIZE := n
22+
KCSAN_SANITIZE := n
2123
KCOV_INSTRUMENT := n
2224

2325
# These are adjustments to the compiler flags used for objects that

0 commit comments

Comments
 (0)