Skip to content

Unify code coverage and add Clang source-based coverage with MC/DC

Sasha Levin kvm linux-arch linux-doc linux-kbuild linux-perf-users linux-um lkml llvm workflows 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest20 patches 20 repliesIn review
  2. v1 RFC20 patches 9 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/20coverage: move gcov and kcov under kernel/coverage/MAINTAINERS arch/Kconfig include/linux/sched.h +14 more+92 -792 replies
2/20kbuild: unify static coverage instrumentation as COVERAGE_PROFILEDocumentation/dev-tools/gcov.rst Documentation/translations/zh_CN/dev-tools/gcov.rst Documentation/translations/zh_TW/dev-tools/gcov.rst +49 more+241 -1881 reply
3/20coverage: add a common debugfs interfaceMAINTAINERS kernel/coverage/Makefile kernel/coverage/core.c +3 more+122 -42 replies
4/20compiler_types.h: Don't instrument __always_inline functions for coverageinclude/linux/compiler_types.h+11 -00 replies
5/20context_tracking: Always inline the empty stubs called from noinstr codeinclude/linux/context_tracking.h+4 -40 replies
6/20lockdep: Always inline the empty stubs called from noinstr codeinclude/linux/irqflags.h include/linux/lockdep.h+5 -50 replies
7/20perf: Always inline the empty perf_lopwr_cb() stubinclude/linux/perf_event.h+1 -10 replies
8/20rseq: Always inline the empty stubs called from noinstr codeinclude/linux/rseq_entry.h+7 -70 replies
9/20x86: Always inline empty stubs called from noinstr codearch/x86/include/asm/processor.h arch/x86/include/asm/sev.h+4 -40 replies
10/20x86/sev: Use non-instrumented bitops in the GHCB accessorsarch/x86/include/asm/svm.h+4 -41 reply
11/20objtool: Detect code coverage instrumentation in noinstr codetools/objtool/check.c tools/objtool/include/objtool/elf.h+60 -18 replies
12/20x86/sev: Stop excluding noinstr.o from code coveragearch/x86/coco/sev/Makefile+0 -20 replies
13/20x86/entry: Stop excluding the entry code from KCOVarch/x86/entry/Makefile kernel/entry/Makefile+3 -10 replies
14/20coverage: add Clang's source-based code coverageMAINTAINERS Makefile include/asm-generic/vmlinux.lds.h +7 more+427 -30 replies
15/20x86: Enable Clang's source-based code coveragearch/x86/Kconfig arch/x86/kernel/vmlinux.lds.S+4 -00 replies
16/20coverage: add MC/DC measurementkernel/coverage/Kconfig kernel/coverage/llvm/fs.c scripts/Makefile.llvm-cov+38 -21 reply
17/20gcov: fix gcov_info_add() merge semantics for IOR counterskernel/coverage/gcov/gcc_4_7.c+22 -31 reply
18/20kconfig: add CC_HAS_CONDITION_COVERAGE for MC/DC support detectionscripts/Kconfig.toolchain+3 -01 reply
19/20gcov: add MC/DC condition coverage supportarch/x86/um/vdso/Makefile kernel/coverage/Kconfig kernel/coverage/gcov/fs.c +1 more+15 -62 replies
20/20Documentation: dev-tools: document the common code coverage interfaceDocumentation/dev-tools/coverage.rst Documentation/dev-tools/gcov.rst Documentation/dev-tools/index.rst +2 more+194 -00 replies

Reviews trailers per patch

5 of 20 patches have a Reviewed-by or Acked-by.

PatchWentao ZhangSean ChristophersonPeter OberparleiterNathan Chancellor
1/20R-b R-b
2/20R-b
3/20
4/20
5/20
6/20
7/20
8/20
9/20
10/20R-b
11/20
12/20
13/20
14/20Originally-by
15/20Originally-by
16/20Originally-by
17/20R-b
18/20
19/20R-b
20/20

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v2 thread

Cover letter [PATCH v2 00/20] Unify code coverage and add Clang source-based coverage with MC/DC

The previous postings of Clang's source-based code coverage [1][2] drew objections [3][4]: rather than a third coverage implementation with its own interface next to gcov and KCOV, reviewers asked for a unified coverage interface, and for coverage instrumentation to honour noinstr. This series first restructures the existing coverage support into one framework, then adds source-based coverage and MC/DC as part of it.

Architecture
============

Today, each coverage implementation brings its own plumbing. gcov has its compiler flags in scripts/Makefile.gcov, the GCOV_PROFILE annotations, its options under "General architecture-dependent options" and /sys/kernel/debug/gcov/. KCOV has scripts/Makefile.kcov, KCOV_INSTRUMENT, its options under "Kernel hacking" and /sys/kernel/debug/kcov. Anything that depends on the kernel being built with coverage instrumentation spells it GCOV_KERNEL, and the earlier source-based coverage postings added a third copy of all of it.

This series turns that into one framework, kernel/coverage/:

kernel/coverage/

    core.c, coverage.h   debugfs root, backend registration, common reset
    gcov/                gcov backend (moved from kernel/gcov/)
    llvm/                Clang source-based coverage backend (new)
    kcov.c               KCOV (moved from kernel/kcov.c)

A code coverage measurement implementation, or backend, only provides what is specific to its toolchain:

- its compiler flags, as CFLAGS_COVERAGE in scripts/Makefile.<backend>, including the ones for MC/DC;
- a struct coverage_backend registered with the core: its name, its enabled features and how to reset its data;
- its data, reachable under /sys/kernel/debug/coverage/;
- its capabilities: the architectures it supports, and whether it can instrument modules.

Everything else is shared by all backends:

- Configuration: one "Code coverage" menu with a choice of backend (none, gcov or Clang source-based coverage, as each data format is defined by its toolchain), CONFIG_COVERAGE_KERNEL for anything that depends on coverage instrumentation (LTO, KCFI, ...), CONFIG_COVERAGE_PROFILE_ALL, the per-subsystem CONFIG_COVERAGE_PROFILE_<subsystem> options and CONFIG_COVERAGE_MCDC. Existing configurations are migrated.
- Build control: COVERAGE_PROFILE[_obj.o] decides what gets instrumented, whichever backend is selected. Kbuild applies the backend's flags, leaves modules alone for backends that can't export their data, and sets up objtool accordingly.
- Runtime interface: /sys/kernel/debug/coverage/, with "backend" naming the backend and its features, "reset" resetting its data, and the backend's data files. The existing gcov and kcov entries stay where they are, for lcov and syzkaller, and are linked from there.
- noinstr: one mechanism for all backends, see below.

With that in place, adding source-based coverage (patch 14) only takes the backend specific parts.

KCOV is a per-task tracing interface for fuzzers with its own UAPI, so it is not a backend: it moves into kernel/coverage/, the same menu, the same debugfs root and the same documentation, and its noinstr handling is unified, but it keeps its UAPI and KCOV_INSTRUMENT, whose exclusions serve the fuzzing signal and the KCOV runtime rather than marking code that can't be instrumented.

noinstr
=======

gcov and source-based coverage instrument the code before inlining, so __always_inline helpers carried their counters into noinstr code: in an x86 build, 2833 relocations in .noinstr.text and .cpuidle.text referenced coverage counters. objtool didn't notice, as these are plain memory updates rather than calls. KCOV instruments after inlining and was not affected.

- Patch 4 stops instrumenting __always_inline functions in coverage builds, which costs about 1-2% of the coverage data. Neither GCC nor Clang can drop instrumentation that inlining moves into a noinstr function, so this is needed until they can.
- Patches 5-10 make the empty stubs that noinstr code calls __always_inline, and switch the GHCB accessors to the arch_ bitops.
- Patch 11 makes objtool report coverage counter updates in noinstr code.
- Patches 12 and 13 drop the coverage exclusions that only existed for
noinstr: arch/x86/coco/sev/noinstr.o, added by commit 9efb74f84ba8 ("x86/sev: Disable GCOV on noinstr object"), and the KCOV exclusion of the x86 entry code. The generic entry code stays excluded from KCOV for a different reason, which patch 13 documents.

objtool only validates noinstr code on x86. On other architectures, patch 4 removes the instrumentation that comes from __always_inline functions, but nothing checks for plain static inline helpers that noinstr code uses. On arm64 with gcov profiling the whole kernel, Clang 22 leaves no coverage counters in .noinstr.text (4129 without this series), and calls from noinstr code to out of line copies of trivial helpers go from 35 to 27. GCC inlines helpers such as __kern_my_cpu_offset() and preempt_count() after instrumenting them, so about 190 counters remain there, as without this series. Making those helpers __always_inline, as on x86, is left to follow-up work.

Source-based coverage and MC/DC
===============================

Patches 14-20 add Clang's source-based code coverage for code built into the kernel image (raw profile formats of Clang 19-22 and 23+), enable it on x86_64, add MC/DC measurement for it and for gcov with GCC 14+ [5], including the gcov merge fix MC/DC needs, and document the result.

Merging
=======

The series touches kbuild, x86, objtool, gcov and KCOV. gcov and KCOV changes usually go through Andrew's tree, so I'd suggest taking it there, with acks from the x86, objtool and kbuild maintainers.

Testing
=======

On v7.4-rc1:

- x86_64 with CONFIG_DEBUG_ENTRY: every patch builds with GCC 16 (gcov, KCOV and CONFIG_WERROR; gcov with MC/DC, lockdep and CONFIG_RSEQ=n from patch 8 on) and, from patch 14 on, with Clang 24 (source-based coverage with MC/DC, and KCOV). gcov with GCC 16 and source-based coverage with Clang 19, 22 and 24, with MC/DC, lockdep and CONFIG_RSEQ on and off:
objtool is clean and no coverage counter is left in noinstr code. Without patch 4, objtool reports coverage counter updates in 60 noinstr functions.
- arm64 builds with gcov (GCC 15 and Clang 22), and so do i386 and UML.
- Booted in a VM: gcov and source-based coverage data, reset, MC/DC reports from llvm-cov and gcov --conditions, gcov data of a module across reloads, and KCOV through both /sys/kernel/debug/kcov and /sys/kernel/debug/coverage/kcov.
- Kernels without coverage produce identical compiler invocations.

Changes since the RFC [6]
=========================

- Rebased onto v7.4-rc1. Patch 2 builds on scripts/Makefile.gcov from commit f541906e89f9 ("kbuild: move GCOV flags to scripts/Makefile.gcov") and only renames the variable there.
- Patch 4: no exception for unoptimized objects, none of them needs one (Peter).
- Patch 8: make all the !CONFIG_RSEQ stubs, and the !CONFIG_GENERIC_ENTRY rseq_exit_to_user_mode_restart() stub, __always_inline (Sashiko).
- Patch 11: cache the counter classification in struct symbol, and fold the check into the existing noinstr conditional (Peter).
- Patches 16, 19 and 20: disable the warnings about decisions that MC/DC leaves out instead of keeping them from becoming errors; building with KCFLAGS=-Wpgo-coverage or -Wcoverage-too-many-conditions lists them (Nathan).

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/

Sasha Levin (20):
coverage: move gcov and kcov under kernel/coverage/
kbuild: unify static coverage instrumentation as COVERAGE_PROFILE
coverage: add a common debugfs interface
compiler_types.h: Don't instrument __always_inline functions for
coverage
context_tracking: Always inline the empty stubs called from noinstr
code
lockdep: Always inline the empty stubs called from noinstr code
perf: Always inline the empty perf_lopwr_cb() stub
rseq: Always inline the empty stubs called from noinstr code
x86: Always inline empty stubs called from noinstr code x86/sev: Use non-instrumented bitops in the GHCB accessors
objtool: Detect code coverage instrumentation in noinstr code x86/sev: Stop excluding noinstr.o from code coverage x86/entry: Stop excluding the entry code from KCOV
coverage: add Clang's source-based code coverage
x86: Enable Clang's source-based code coverage
coverage: add MC/DC measurement
gcov: fix gcov_info_add() merge semantics for IOR counters
kconfig: add CC_HAS_CONDITION_COVERAGE for MC/DC support detection
gcov: add MC/DC condition coverage support
Documentation: dev-tools: document the common code coverage interface

 Documentation/dev-tools/coverage.rst          | 161 +++++++++++++
 Documentation/dev-tools/gcov.rst              |  46 +++-
 Documentation/dev-tools/index.rst             |   1 +
 Documentation/dev-tools/kcov.rst              |   3 +
 .../translations/zh_CN/dev-tools/gcov.rst     |  16 +-
 .../translations/zh_TW/dev-tools/gcov.rst     |  16 +-
 MAINTAINERS                                   |  19 +-
 Makefile                                      |   1 +
 arch/Kconfig                                  |   8 +-
 arch/alpha/boot/compressed/Makefile           |   2 +-
 arch/parisc/kernel/vdso32/Makefile            |   2 +-
 arch/parisc/kernel/vdso64/Makefile            |   2 +-
 arch/powerpc/kernel/Makefile                  |   6 +-
 arch/powerpc/kernel/trace/Makefile            |   4 +-
 arch/powerpc/kexec/Makefile                   |   2 +-
 arch/powerpc/xmon/Makefile                    |   2 +-
 arch/s390/boot/Makefile                       |   2 +-
 arch/s390/kernel/Makefile                     |   2 +-
 arch/sh/mm/Makefile                           |   2 +-
 arch/x86/Kconfig                              |   2 +
 arch/x86/coco/sev/Makefile                    |   2 -
 arch/x86/entry/Makefile                       |   1 -
 arch/x86/include/asm/processor.h              |   2 +-
 arch/x86/include/asm/sev.h                    |   6 +-
 arch/x86/include/asm/svm.h                    |   8 +-
 arch/x86/kernel/vmlinux.lds.S                 |   2 +
 arch/x86/platform/efi/Makefile                |   2 +-
 arch/x86/um/vdso/Makefile                     |   4 +-
 arch/xtensa/boot/lib/Makefile                 |   2 +-
 drivers/gpu/drm/amd/amdgpu/Kconfig            |  12 +-
 drivers/gpu/drm/amd/amdgpu/Makefile           |   4 +-
 .../gpu/drm/amd/display/amdgpu_dm/Makefile    |   2 +-
 drivers/gpu/drm/i915/Makefile                 |   2 +-
 drivers/gpu/drm/xe/Makefile                   |   2 +-
 drivers/s390/char/Makefile                    |   2 +-
 include/asm-generic/vmlinux.lds.h             |  36 +++
 include/linux/compiler_types.h                |  11 +
 include/linux/context_tracking.h              |   8 +-
 include/linux/irqflags.h                      |   6 +-
 include/linux/lockdep.h                       |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/rseq_entry.h                    |  14 +-
 include/linux/sched.h                         |   2 +-
 init/Kconfig                                  |   9 +-
 io_uring/Makefile                             |   4 +-
 kernel/Makefile                               |  15 +-
 kernel/coverage/Kconfig                       | 226 ++++++++++++++++++
 kernel/coverage/Makefile                      |  18 ++
 kernel/coverage/core.c                        |  71 ++++++
 kernel/coverage/coverage.h                    |  22 ++
 kernel/{ => coverage}/gcov/Makefile           |   0
 kernel/{ => coverage}/gcov/base.c             |   0
 kernel/{ => coverage}/gcov/clang.c            |   2 +-
 kernel/{ => coverage}/gcov/fs.c               |  22 +-
 kernel/{ => coverage}/gcov/gcc_4_7.c          |  25 +-
 kernel/{ => coverage}/gcov/gcc_base.c         |   0
 kernel/{ => coverage}/gcov/gcov.h             |   0
 kernel/{ => coverage}/kcov.c                  |   3 +
 kernel/coverage/llvm/Makefile                 |   4 +
 kernel/coverage/llvm/fs.c                     | 202 ++++++++++++++++
 kernel/coverage/llvm/llvm-cov.h               | 139 +++++++++++
 kernel/entry/Makefile                         |   3 +
 kernel/gcov/Kconfig                           |  55 -----
 kernel/trace/Kconfig                          |   9 +-
 kernel/trace/Makefile                         |   4 +-
 lib/Kconfig.debug                             |  61 +----
 lib/Makefile                                  |   2 +-
 lib/crypto/Makefile                           |   2 +-
 net/bridge/Makefile                           |   8 +-
 net/bridge/netfilter/Makefile                 |   4 +-
 net/ipv4/Makefile                             |   4 +-
 net/ipv4/netfilter/Makefile                   |   4 +-
 net/ipv6/Makefile                             |   4 +-
 net/ipv6/netfilter/Makefile                   |   4 +-
 net/netfilter/Kconfig                         |   9 +-
 net/netfilter/Makefile                        |   4 +-
 net/netfilter/ipset/Kconfig                   |   9 +-
 net/netfilter/ipset/Makefile                  |   4 +-
 net/netfilter/ipvs/Kconfig                    |   9 +-
 net/netfilter/ipvs/Makefile                   |   4 +-
 net/rds/Kconfig                               |   9 +-
 net/rds/Makefile                              |   4 +-
 scripts/Kconfig.toolchain                     |   3 +
 scripts/Makefile.gcov                         |  17 +-
 scripts/Makefile.lib                          |  19 +-
 scripts/Makefile.llvm-cov                     |  15 ++
 scripts/Makefile.modfinal                     |   2 +-
 scripts/sbom/sbom/environment.py              |   2 +-
 tools/objtool/check.c                         |  60 ++++-
 tools/objtool/include/objtool/elf.h           |   1 +
 tools/testing/selftests/net/rds/config.sh     |   9 +-
 tools/testing/selftests/net/rds/rds_run.sh    |   8 +-
 tools/unittests/test_cmatch.py                |   2 +-
 93 files changed, 1247 insertions(+), 304 deletions(-)
 create mode 100644 Documentation/dev-tools/coverage.rst
 create mode 100644 kernel/coverage/Kconfig
 create mode 100644 kernel/coverage/Makefile
 create mode 100644 kernel/coverage/core.c
 create mode 100644 kernel/coverage/coverage.h
 rename kernel/{ => coverage}/gcov/Makefile (100%)
 rename kernel/{ => coverage}/gcov/base.c (100%)
 rename kernel/{ => coverage}/gcov/clang.c (99%)
 rename kernel/{ => coverage}/gcov/fs.c (97%)
 rename kernel/{ => coverage}/gcov/gcc_4_7.c (94%)
 rename kernel/{ => coverage}/gcov/gcc_base.c (100%)
 rename kernel/{ => coverage}/gcov/gcov.h (100%)
 rename kernel/{ => coverage}/kcov.c (99%)
 create mode 100644 kernel/coverage/llvm/Makefile
 create mode 100644 kernel/coverage/llvm/fs.c
 create mode 100644 kernel/coverage/llvm/llvm-cov.h
 delete mode 100644 kernel/gcov/Kconfig
 create mode 100644 scripts/Makefile.llvm-cov

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.53.0