From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796196354; b=28J0Cy8TKDatPaPKawwT7+qY1EUcnzQT2Sd/2eyiUWpeJmjm/Gnsp95bXec8eZDlKjYf wTaCPZoywA2RmZsIWVG7AL4yvG9DCJpcL2yCj87g7YPdE4mWedx2C2UlAMLPTE21p7bRy 5T6RNeG2kvfh9G8dEt8Bx8hpT5ygSq8jiqKvIP88riMZf5mLocRSq1hvJ62CgDk4/rSE2 OrMjYeJ8/xwkgq46w4cytVEDovRjOAid+dJVlFZ+lrzqcVxzXYwpyJebuChnqG7NITdK+ rpQDUJYrrWw+mg0JmNzIBmkui7eFlQqPzLwmjgX7IpV5GAUpjCDQsTC8pjqaLtjkQQw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796196354; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=Z1U2ElfaiNZlHr+0SdOwaJKV21dTTczRz7ghsbt7HDM=; b=OcLhjoFtPqxMWnqPN8CChxvyJcPpotdWJGQJ+paGvk+dPKJKxlvenHlOT+fokBM7MbTE ROHbgT3x3dsCaYq987tGWpsImDz4jwm6TqPgR0YOCBb8RZIS8upo61aP/Z94625oGpM9O H81x2JKifNL5jGtzIubN6aNG163yRk0MN3HdodQfvzc4QpLD2W7aBDAZBAfAuK8NmvmSE HWyO4YIySnzMdRCJMB6tXEVNvDiJQqngfO3PS78ftz4EYpaRVVX3pBr08O4oq55JByzdO 2bdmVS2RKKA+iN/kSrSx2J14xuI6APNN5pv3zyk0uUHljKsZK0GfHBrQVNTV3/EFNaw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XzC8mOaG; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B60D5606A31A; Wed, 2 Dec 2026 07:25:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XzC8mOaG Received: by smtp.kernel.org (Postfix) with ESMTPSA id B12E11F00893; Wed, 2 Dec 2026 07:25:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796196354; bh=Z1U2ElfaiNZlHr+0SdOwaJKV21dTTczRz7ghsbt7HDM=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=XzC8mOaGaLIlbHWiw2Kv5dvl6b5/xgknEP2gHOBC3eO5jPNTYcq69soJQerDRyB4W FojQtV6CW5HWCtuNa8siu0qJkfHXWt0NYaOk8TyEF3Z1X8y/U5oeSV6QPoqYA6/g0+ MzjuoSY0zzbzDV+iq7JOyWIPiUZolggv/9ntJvbN/ZJugcyIjnqQ3SSFNqf1vD29Mq PDrZ8xhay3d1uaVZr7Apw5V1MDJ667yodB/XqWdkPYBZBISCdbie8IzwkHCMoyktg/ EhE3qYC8Q9jf9aCqU3kJBYSYN/2JEQluLJbuK4nJMShU9d6QunewYiGRi7+m9EDTZ0 WbC2rUlcyFOKg== Date: Tue, 1 Dec 2026 23:25:50 -0800 From: Josh Poimboeuf To: Sasha Levin Cc: linux-kernel@vger.kernel.org, Bill Wendling , Chuck Wolber , Justin Stitt , llvm@lists.linux.dev, Marco Elver , Masami Hiramatsu , Nathan Chancellor , Nick Desaulniers , Peter Zijlstra , Thomas Gleixner Subject: Re: [PATCH v3 11/19] objtool: Point the remaining coverage accesses in noinstr code at a sink Message-ID: References: <20261125020603.3934222-1-sashal@kernel.org> <20261125020603.3934222-12-sashal@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 > Signed-off-by: Sasha Levin 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 #include #include #include @@ -4820,6 +4821,107 @@ const char *objtool_disas_insn(struct instruction *insn) return disas_result(dctx); } +/* GCC's __gcov. 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);