Re: [PATCH v3 11/19] objtool: Point the remaining coverage accesses in noinstr code at a sink
From: Josh Poimboeuf
Date: Wed Dec 02 2026 - 02:25:58 EST
On Tue, Nov 24, 2026 at 09:05:55PM -0500, Sasha Levin wrote:
> Not every reference to a coverage counter in noinstr code is an update
> that a NOP can replace. Clang's source-based code coverage may load
> the address of one of several counters into a register and update the
> counter through it:
>
> movq $__llvm_prf_cnts+0x20,%rax
> ...
> lock incq (%rax)
>
> and its MC/DC bitmap updates load the byte of the bitmap first, to test
> the bit. The update at the end can be replaced by a NOP, the load can't:
>
> movzbl __llvm_prf_bits(%rdi),%ecx
> btl %esi,%ecx
> jae ...
> lock orb %r8b,__llvm_prf_bits(%rdi)
>
> Clang's gcov, whose counter updates aren't atomic, loads a counter,
> keeps it in a register across a loop and stores it back. Removing these
> takes following the values through the registers.
>
> Change where they point instead. Retarget the relocation of every such
> access to a sink, a new .bss section that objtool adds to the object,
> and leave the instructions as they are. The counters of every function
> are mapped to the same place in the sink with their offsets kept, and
> the sink has room for the largest of them on either side of that place,
> so that an indexed access stays within it whether it is relative to the
> start of its counters or to their end.
>
> With the configuration of the previous patch, a kernel built with
> Clang 22, source-based code coverage and MC/DC gets 3319 counter updates
> in noinstr code replaced by NOPs and 229 accesses pointed at a sink of
> 2048 bytes. With Clang's gcov that is 2020 and 177. GCC's atomic counter
> updates don't need a sink.
>
> Clang's gcov can also update a counter through its address in a
> register, which it loads as an immediate. Such an address looks just
> like the end of the object in front of the counter, so those are left
> alone. The kernel above has none.
>
> The sink is ordinary memory, and nothing keeps a data breakpoint away
> from it yet.
>
> Suggested-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
> Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
I think patch 10 isn't necessary; the patch 11 approach could be used
universally. But also even patch 11 is overly complicated and I suspect
something like the below would be sufficient:
diff --git a/kernel/coverage/Kconfig b/kernel/coverage/Kconfig
index d16fa049995c6..6e70065be06b9 100644
--- a/kernel/coverage/Kconfig
+++ b/kernel/coverage/Kconfig
@@ -42,6 +42,7 @@ config GCOV_KERNEL
bool "gcov-based kernel profiling"
depends on DEBUG_FS
select CONSTRUCTORS
+ select OBJTOOL if HAVE_NOINSTR_HACK
help
This option enables gcov-based code profiling (e.g. for code coverage
measurements), with GCC or Clang. The coverage data of the kernel
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 4ef5bf97d3335..171c31c40bdd8 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4,6 +4,7 @@
*/
#define _GNU_SOURCE /* memmem() */
+#include <ctype.h>
#include <fnmatch.h>
#include <string.h>
#include <stdlib.h>
@@ -4820,6 +4821,107 @@ const char *objtool_disas_insn(struct instruction *insn)
return disas_result(dctx);
}
+/* GCC's __gcov<N>.<func> or Clang's __llvm_gcov_ctr* */
+static bool is_gcov_counter(const char *name)
+{
+ if (strstarts(name, "__llvm_gcov_ctr"))
+ return true;
+
+ if (!strstarts(name, "__gcov") || !isdigit((unsigned char)name[6]))
+ return false;
+
+ for (name += 6; isdigit((unsigned char)*name); name++)
+ ;
+
+ return *name == '.';
+}
+
+/* Clang source-based coverage: one section per function */
+static bool is_llvm_counter_sec(struct section *sec)
+{
+ return !strcmp(sec->name, "__llvm_prf_cnts") ||
+ !strcmp(sec->name, "__llvm_prf_bits");
+}
+
+static bool is_coverage_reloc(struct instruction *insn, struct reloc *reloc)
+{
+ struct section *sec = reloc->sym->sec;
+ struct symbol *sym;
+
+ if (is_llvm_counter_sec(sec))
+ return true;
+
+ if (!arch_pc_relative_reloc(reloc))
+ return false;
+
+ sym = find_symbol_containing(sec, reloc->sym->offset +
+ arch_insn_adjusted_addend(insn, reloc));
+
+ return sym && is_gcov_counter(sym->name);
+}
+
+static struct symbol *create_coverage_sink(struct objtool_file *file)
+{
+ struct section *sec;
+ unsigned long size;
+
+ /* Counters only need 8 bytes, Clang MC/DC bitmaps may need more. */
+ size = 8;
+ for_each_sec(file->elf, sec) {
+ if (!strcmp(sec->name, "__llvm_prf_bits"))
+ size = max(size, sec_size(sec));
+ }
+
+ sec = elf_create_section(file->elf, ".bss.coverage_sink", size, 0,
+ SHT_NOBITS, 8, SHF_ALLOC | SHF_WRITE);
+ if (!sec)
+ return NULL;
+
+ return elf_create_section_symbol(file->elf, sec);
+}
+
+/*
+ * noinstr functions can inherit code coverage writes from their inlined
+ * functions, breaking noinstr rules. Redirect all such accesses to a single
+ * dummy "sink" at .bss.coverage_sink+0.
+ */
+static int fixup_noinstr_coverage(struct objtool_file *file)
+{
+ struct symbol *sink = NULL;
+ struct instruction *insn;
+ struct section *sec;
+ struct reloc *reloc;
+ s64 addend;
+
+ for_each_sec(file->elf, sec) {
+ if (!sec->noinstr)
+ continue;
+
+ sec_for_each_insn(file, sec, insn) {
+ reloc = insn_reloc(file, insn);
+ if (!reloc)
+ continue;
+
+ if (!is_coverage_reloc(insn, reloc))
+ continue;
+
+ if (!sink) {
+ sink = create_coverage_sink(file);
+ if (!sink)
+ return -1;
+ }
+
+ reloc->sym = sink;
+ set_reloc_sym(file->elf, reloc, sink->idx);
+
+ addend = reloc_addend(reloc) - arch_insn_adjusted_addend(insn, reloc);
+ set_reloc_addend(file->elf, reloc, addend);
+ }
+ }
+
+ return 0;
+}
+
int check(struct objtool_file *file)
{
struct disas_context *disas_ctx = NULL;
@@ -4845,6 +4947,12 @@ int check(struct objtool_file *file)
if (!nr_insns)
goto out;
+ if (opts.hack_noinstr) {
+ ret = fixup_noinstr_coverage(file);
+ if (ret)
+ goto out;
+ }
+
if (opts.retpoline)
warnings += validate_retpoline(file);