mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@kernel.org>
To: Sasha Levin <sashal@kernel.org>
Cc: linux-kernel@vger.kernel.org, Bill Wendling <morbo@google.com>,
	 Chuck Wolber <chuck@wolber.net>,
	Justin Stitt <justinstitt@google.com>,
	llvm@lists.linux.dev,  Marco Elver <elver@google.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	 Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	 Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@kernel.org>
Subject: Re: [PATCH v3 11/19] objtool: Point the remaining coverage accesses in noinstr code at a sink
Date: Tue, 1 Dec 2026 23:25:50 -0800	[thread overview]
Message-ID: <aw_HdaWNvTi7Bn9M@jpoimboe> (raw)
In-Reply-To: <20261125020603.3934222-12-sashal@kernel.org>

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@kernel.org>
> Signed-off-by: Sasha Levin <sashal@kernel.org>

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);
 

  reply	other threads:[~2026-12-02  7:25 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  2:05 [PATCH v3 00/19] Unify code coverage and add Clang source-based coverage with MC/DC Sasha Levin
2026-11-25  2:05 ` [PATCH v3 01/19] coverage: move gcov and kcov under kernel/coverage/ Sasha Levin
2026-11-25  2:05 ` [PATCH v3 02/19] kbuild: unify static coverage instrumentation as COVERAGE_PROFILE Sasha Levin
2026-11-25  2:05 ` [PATCH v3 03/19] coverage: add a common debugfs interface Sasha Levin
2026-11-25  2:05 ` [PATCH v3 04/19] context_tracking: Always inline the empty stubs called from noinstr code Sasha Levin
2026-11-25  2:05 ` [PATCH v3 05/19] lockdep: " Sasha Levin
2026-11-25  2:05 ` [PATCH v3 06/19] perf: Always inline the empty perf_lopwr_cb() stub Sasha Levin
2026-11-25  2:05 ` [PATCH v3 07/19] rseq: Always inline the empty stubs called from noinstr code Sasha Levin
2026-11-25  2:05 ` [PATCH v3 08/19] x86: Always inline " Sasha Levin
2026-11-25  2:05 ` [PATCH v3 09/19] x86/sev: Use non-instrumented bitops in the GHCB accessors Sasha Levin
2026-11-25  2:05 ` [PATCH v3 10/19] objtool: Remove coverage counter updates from noinstr code Sasha Levin
2026-11-25  2:05 ` [PATCH v3 11/19] objtool: Point the remaining coverage accesses in noinstr code at a sink Sasha Levin
2026-12-02  7:25   ` Josh Poimboeuf [this message]
2026-11-25  2:05 ` [PATCH v3 12/19] x86/sev: Stop excluding noinstr.o from code coverage Sasha Levin
2026-11-25  2:05 ` [PATCH v3 13/19] coverage: add Clang's source-based " Sasha Levin
2026-11-25  2:05 ` [PATCH v3 14/19] x86: Enable " Sasha Levin
2026-11-25  2:05 ` [PATCH v3 15/19] coverage: add MC/DC measurement Sasha Levin
2026-11-25  2:06 ` [PATCH v3 16/19] gcov: fix gcov_info_add() merge semantics for IOR counters Sasha Levin
2026-11-25  2:06 ` [PATCH v3 17/19] kconfig: add CC_HAS_CONDITION_COVERAGE for MC/DC support detection Sasha Levin
2026-11-25  2:06 ` [PATCH v3 18/19] gcov: add MC/DC condition coverage support Sasha Levin
2026-11-25  2:06 ` [PATCH v3 19/19] Documentation: dev-tools: document the common code coverage interface Sasha Levin
2026-12-01  6:35 ` [PATCH v3 00/19] Unify code coverage and add Clang source-based coverage with MC/DC Chuck Wolber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aw_HdaWNvTi7Bn9M@jpoimboe \
    --to=jpoimboe@kernel.org \
    --cc=chuck@wolber.net \
    --cc=elver@google.com \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mhiramat@kernel.org \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=sashal@kernel.org \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®