freenode
Kernel & Low-Level

Linux kernel coverage gets one interface, Clang, and MC/DC

Sasha Levin's rework folds gcov and KCOV into a shared framework, then adds source-level measurement that safety standards actually ask for, after coverage stores were found hanging noinstr paths.

A 19-part posting from Sasha Levin on the Linux kernel mailing list replaces the kernel's separate coverage implementations with one framework, then adds Clang source-based coverage and Modified Condition/Decision Coverage (MC/DC). Earlier attempts to land Clang coverage were blocked because they would have been a third stack beside gcov and KCOV, each with its own flags, annotations, config menu, and debugfs tree, and because instrumentation did not respect noinstr, the regions that must not take exceptions.

The new layout keeps one Code coverage menu and one debugfs root. A backend only supplies what its toolchain needs: compiler flags, the data it exports, and which architectures and modules it can instrument. gcov and KCOV move under that core. Anything that previously keyed off a gcov-specific build option now keys off a shared coverage option, including LTO and kernel control-flow integrity.

Clang's frontend instrumentation is the reason the extra backend is worth it. gcov counts at the object-code level, so optimizations scramble a source condition into extra branches. Source-based coverage maps each condition back to the line regardless of optimization, and exports a raw profile for the built-in image (modules are left alone) under the coverage debugfs tree. Clang 19 through 22 and Clang 23 and later use different profile formats; both are accepted. Counters update atomically. Jump tables are turned off in instrumented code so the compiler cannot hide a switch behind a table of counters that the kernel's object checker cannot strip out of noinstr regions. x86-64 is the first architecture that can use it, including for the whole kernel. The coverage mapping is stripped from the compressed boot image; llvm-cov reads it from vmlinux. Wentao Zhang originated that architecture support.

MC/DC, required by several automotive and aviation certification standards, records whether each condition in a boolean decision independently changed the outcome. Clang has supported it since version 18; GCC has had condition coverage since GCC 14, reported by gcov's conditions mode. A config option caps how large a decision Clang will instrument. Bigger decisions, and ones the compiler cannot instrument, fall back to ordinary branch coverage. The compiler warns about each of those; the kernel silences that noise, and with older Clang plus warnings-as-errors the option is not offered at all.

The noinstr problem is not cosmetic. noinstr already implies that a function itself is not profiled, but compilers emit counter updates before inlining, so those stores ride into entry, NMI, and fault paths. A store can take a data breakpoint. Levin reports that with whole-kernel gcov, the x86 debug handler updates a counter before it has cleared the breakpoint registers, so a breakpoint on that counter makes the next kernel debug exception never return: the handler interrupts itself. On a recent release candidate, a one-CPU guest hung that way about once a second once a second breakpoint, on the jiffies counter, fired. The page-fault handler has the same class of problem: a counter update sits just before it reads the fault address, which is lost if a debug exception is taken there. Hand-maintained lists of data that must stay clear of breakpoints cannot cover thousands of ordinary coverage variables.

Neither GCC nor Clang drops that leftover instrumentation. Objtool, which already strips KCOV calls from noinstr code, now replaces simple counter updates with no-ops. An x86-64 defconfig with KVM, built with GCC 16 and whole-kernel gcov, had 1,823 such updates in 108 noinstr functions of the kernel image and 106 more in the KVM modules. Clang is harder: it sometimes loads a counter address and updates through a register, and MC/DC bitmaps load a byte before testing a bit. Those accesses are retargeted at a sink section objtool adds, rather than deleted. Josh Poimboeuf suggested that approach. A Clang 22 build with source-based coverage and MC/DC had 3,319 updates turned into no-ops and 229 accesses pointed at a 2,048-byte sink. The sink is still ordinary memory; nothing yet keeps a data breakpoint off it.

Related fixes make empty lockdep, restartable-sequences, AMD SEV, and perf stubs always-inline so coverage builds do not leave calls out of noinstr text, and switch SEV's GHCB bit operations to the uninstrumented variants. The SEV object that had been excluded from coverage can be profiled again. gcov's counter merge, reviewed by Peter Oberparleiter, had been adding every counter type; bitwise-OR counters used for condition coverage must be merged with OR, a bug that predates this work and also blocked correct MC/DC data.