[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240824230641.385839-4-wentaoz5@illinois.edu>
Date: Sat, 24 Aug 2024 18:06:41 -0500
From: Wentao Zhang <wentaoz5@...inois.edu>
To: linux-kbuild@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux-efi@...r.kernel.org,
linux-um@...ts.infradead.org,
linux-arch@...r.kernel.org,
linux-trace-kernel@...r.kernel.org,
llvm@...ts.linux.dev,
x86@...nel.org
Cc: wentaoz5@...inois.edu,
marinov@...inois.edu,
tyxu@...inois.edu,
jinghao7@...inois.edu,
tingxur@...inois.edu,
steven.h.vanderleest@...ing.com,
chuck.wolber@...ing.com,
matthew.l.weber3@...ing.com,
Matt.Kelly2@...ing.com,
andrew.j.oppelt@...ing.com,
samuel.sarkisian@...ing.com,
morbo@...gle.com,
samitolvanen@...gle.com,
masahiroy@...nel.org,
tglx@...utronix.de,
mingo@...hat.com,
bp@...en8.de,
dave.hansen@...ux.intel.com,
luto@...nel.org,
ardb@...nel.org,
richard@....at,
anton.ivanov@...bridgegreys.com,
johannes@...solutions.net,
arnd@...db.de,
rostedt@...dmis.org,
mhiramat@...nel.org,
oberpar@...ux.ibm.com,
akpm@...ux-foundation.org,
paulmck@...nel.org,
bhelgaas@...gle.com,
kees@...nel.org,
jpoimboe@...nel.org,
peterz@...radead.org,
kent.overstreet@...ux.dev,
nathan@...nel.org,
hpa@...or.com,
mathieu.desnoyers@...icios.com,
ndesaulniers@...gle.com,
justinstitt@...gle.com,
maskray@...gle.com,
dvyukov@...gle.com
Subject: [RFC PATCH 3/3] llvm-cov: add Clang's MC/DC support
Add Clang flags and kconfig options for measuring the kernel's modified
condition/decision coverage (MC/DC).
As of Clang 19, users can determine the max number of conditions in a
decision to measure via option LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS,
which controls -fmcdc-max-conditions flag of Clang cc1 [1]. Since MC/DC
implementation utilizes bitmaps to track the execution of test vectors,
more memory is consumed if larger decisions are getting counted. The
maximum value supported by Clang is 32767. According to local
experiments, the working maximum for Linux kernel is 44, with the
largest decisions in kernel codebase (with 45 conditions) excluded,
otherwise the kernel image size limit will be exceeded. The largest
decisions in kernel are contributed for example by macros checking
CPUID.
Code exceeding LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS will produce compiler
warnings.
As of LLVM 19, certain expressions are still not covered, and will produce
build warnings when they are encountered:
"[...] if a boolean expression is embedded in the nest of another boolean
expression but separated by a non-logical operator, this is also not
supported. For example, in x = (a && b && c && func(d && f)), the d && f
case starts a new boolean expression that is separated from the other
conditions by the operator func(). When this is encountered, a warning
will be generated and the boolean expression will not be
instrumented." [2]
[1] https://discourse.llvm.org/t/rfc-coverage-new-algorithm-and-file-format-for-mc-dc/76798
[2] https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#mc-dc-instrumentation
Signed-off-by: Wentao Zhang <wentaoz5@...inois.edu>
Signed-off-by: Chuck Wolber <chuck.wolber@...ing.com>
---
Makefile | 6 ++++++
kernel/llvm-cov/Kconfig | 36 ++++++++++++++++++++++++++++++++++++
scripts/Makefile.lib | 11 +++++++++++
3 files changed, 53 insertions(+)
diff --git a/Makefile b/Makefile
index 1750a2b7dfe8..4aa263e5f67f 100644
--- a/Makefile
+++ b/Makefile
@@ -740,6 +740,12 @@ all: vmlinux
CFLAGS_LLVM_COV := -fprofile-instr-generate -fcoverage-mapping
export CFLAGS_LLVM_COV
+CFLAGS_LLVM_COV_MCDC := -fcoverage-mcdc
+ifdef CONFIG_LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS
+CFLAGS_LLVM_COV_MCDC += -Xclang -fmcdc-max-conditions=$(CONFIG_LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS)
+endif
+export CFLAGS_LLVM_COV_MCDC
+
CFLAGS_GCOV := -fprofile-arcs -ftest-coverage
ifdef CONFIG_CC_IS_GCC
CFLAGS_GCOV += -fno-tree-loop-im
diff --git a/kernel/llvm-cov/Kconfig b/kernel/llvm-cov/Kconfig
index 505eba5bd23c..40b6a4fd590e 100644
--- a/kernel/llvm-cov/Kconfig
+++ b/kernel/llvm-cov/Kconfig
@@ -26,4 +26,40 @@ config LLVM_COV_KERNEL
Note that the debugfs filesystem has to be mounted to access the raw
profile.
+config LLVM_COV_KERNEL_MCDC
+ bool "Enable measuring modified condition/decision coverage (MC/DC)"
+ depends on LLVM_COV_KERNEL
+ depends on CLANG_VERSION >= 180000
+ help
+ This option enables modified condition/decision coverage (MC/DC)
+ code coverage instrumentation.
+
+ If unsure, say N.
+
+ This will add Clang's Source-based Code Coverage MC/DC
+ instrumentation to your kernel. As of LLVM 19, certain expressions
+ are still not covered, and will produce build warnings when they are
+ encountered.
+
+ "[...] if a boolean expression is embedded in the nest of another
+ boolean expression but separated by a non-logical operator, this is
+ also not supported. For example, in
+ x = (a && b && c && func(d && f)), the d && f case starts a new
+ boolean expression that is separated from the other conditions by the
+ operator func(). When this is encountered, a warning will be
+ generated and the boolean expression will not be instrumented."
+
+ https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#mc-dc-instrumentation
+
+config LLVM_COV_KERNEL_MCDC_MAX_CONDITIONS
+ int "Maximum number of conditions in a decision to instrument"
+ range 6 32767
+ depends on LLVM_COV_KERNEL_MCDC
+ depends on CLANG_VERSION >= 190000
+ default "6"
+ help
+ This value is passed to "-fmcdc-max-conditions" flag of Clang cc1.
+ Expressions whose number of conditions is greater than this value will
+ produce warnings and will not be instrumented.
+
endmenu
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index b9ceaee34b28..b8dfad01cb52 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -168,6 +168,17 @@ _c_flags += $(if $(patsubst n%,, \
$(CFLAGS_LLVM_COV))
endif
+#
+# Flag that turns on modified condition/decision coverage (MC/DC) measurement
+# with Clang's Source-based Code Coverage. Enable the flag for a file or
+# directory depending on variables LLVM_COV_PROFILE_obj.o and LLVM_COV_PROFILE.
+#
+ifeq ($(CONFIG_LLVM_COV_KERNEL_MCDC),y)
+_c_flags += $(if $(patsubst n%,, \
+ $(LLVM_COV_PROFILE_$(basetarget).o)$(LLVM_COV_PROFILE)y), \
+ $(CFLAGS_LLVM_COV_MCDC))
+endif
+
#
# Enable address sanitizer flags for kernel except some files or directories
# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
--
2.45.2
Powered by blists - more mailing lists