From nobody Mon Apr 6 18:13:31 2026 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D43F3FFABE for ; Wed, 18 Mar 2026 16:27:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773851240; cv=none; b=qTmGkwx8DRnbvKE09U4q03HKJlKI3ixhIoeyKQUyuzeRt5iuIEXqQVnwsAUkEvAAn46LlV3Ynzwrq3YQyv91TIcMkxfFK+IdoNbIwgQ9unYnfHacQYkTmYxLFHvQBSD05u7cKelkrss+xbE9L0gSbwIAE9+LShtZjGqId8FF1GM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773851240; c=relaxed/simple; bh=/5iKMb3ycj7sI45IkWou/fRJrMIENtP9YU3ZYrOJJ2E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rR3t18iFuBr3/o4fbgAO76gg6b9li/UCKbWXCvbF3u0eKXTrjbm+Py7HyBbQtwneDGXGJhGtUTkuXrV33z3Ou/am7wKX3cVjrhYfsxICvycOedJSGdHhSxxB2bf1YARMZiRsh3+h8NAKuzxryBDnzxlZGYqjpi/OBPYVZWj/XXw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qpJWhpt7; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qpJWhpt7" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-485317b6bd0so75665e9.1 for ; Wed, 18 Mar 2026 09:27:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773851231; x=1774456031; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=mYXkU8zgzVWbz7gW/kc26lZQTOVhBoB0e4m0vdnJjs4=; b=qpJWhpt7BnvjXnur/EMPYGN865Zzj8v/+M95aD/IOtVlIx5eboN4fxWAMvXwRI2sFQ XWdQ4cFZzHupZ1+YN2OEMjC136zibSe6O2DsL16+Y/yutLNw04zN/V5W4wlmjqZ1YGdE zUY/KsvLnTTY574HS/mYNBU+kVxFO1eH6jAr7ucuHQ10BWo0VO2qlrpv3yzchyyajih8 TGgaTfasylSNhGgFUHOftcjYubyrIh5LWCsp+yRWgYrUtv6+lbKjcpczsrxcnUWhHFHC 6d5ybl0vrMMR3IgxNSYSSOxz0wCLlVU4aC2FuxQAjD/vlbVFPiGjg48AIQXD8zlqV4rA NyNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773851231; x=1774456031; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=mYXkU8zgzVWbz7gW/kc26lZQTOVhBoB0e4m0vdnJjs4=; b=L/YuJVP5IxkFGEU38Bow/8YPq1bC0w/HdJDRZDKRBuog766YeRSG3WOY7b2rOQ3Zxa UZH8EK82tVQbCAvsSbFn3LFA3Axat+pvWnZLmBQzaw/BCUw9iG7rRN0MPw+01x8tkbqM 9XajDTjP1TpUHC44XbeffLmNdDsLEWhou07gCfUvK8HzQ9xvt1jnD25UQ85l0CXBMLzE fqTDhYWr2zz+fVfHRTBlZ9rcINr6Br6el9UJVzBG1vaPjhUc64kHBwnwkgSumTm9BBsy Yfni3LHZw3nVJCSZzaakRFx9xHrH9wzBCLAVxGWh0ApZgtKAvnY/yABV3g728Dq/n1tD rJPA== X-Forwarded-Encrypted: i=1; AJvYcCX/BJEGgBYETWH0VKf5Kmu1F9++VZNxN015o7a3dlJ4N7H8OJMsTTLrZb/GAtm3jU1dn3sJHhUM9mIuoGc=@vger.kernel.org X-Gm-Message-State: AOJu0YzRXX67+lnQmeukFsxWMOxii66WLfeqsCcjktg+2A7eDQ7bUR38 BGg2dlZliPr1lYgnvKcGB1Hg0IIThucf45++vrp78fBBHN4Yf51q3L0mmbIDjmiDPg== X-Gm-Gg: ATEYQzwAh0oeoleoJQCKi8hEOfl7qxEfUqR11LY9SQtQ6Ot3AGR6rMwkaP3Jd4Kq7cM dexoI7qG4MexyvLMTMFX9Q7475CPmR54nXJTn4ex6y7z5JbzI4IMpKip20KP35rj5/OCJyClCpx K8QVx+wpkrsT+3L4eY8BLplFHZQiGEBHDlrTa5dhYwYnuOalGawImG2++bLwPSfO/Vmf0ufZ66U HBKYvQNnBvtt6sVo3lMVKF3joDCGMuCMi4W1CBH0purSvLWwLrlRT6jD9vjkQ/Y2n3Aafs6SCVL B1ZkVDPUR9fnMFpfJvsrIIB1ql0ThYSusdUrZWhY6Hp7XPWvtmPs3rlk9bVy+FFeMNBMcA6nv59 qHPDVChXX77EErEIbI95D4xFmA41xwQm8vA9VTdfK22HLS5lm5ME/XdfmtbhCR9lwTxZURzN88W UeKH6F+W2AiqTiTjOrzh55CVf0pfsgZVPYCd2mCRVN/vZeKXey3w== X-Received: by 2002:a05:600c:5912:b0:486:f772:91c4 with SMTP id 5b1f17b1804b1-486f77292bemr489125e9.8.1773851230735; Wed, 18 Mar 2026 09:27:10 -0700 (PDT) Received: from localhost ([2a00:79e0:288a:8:c66d:1e51:595a:925]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-43b5184961csm9805144f8f.6.2026.03.18.09.27.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Mar 2026 09:27:10 -0700 (PDT) From: Jann Horn Date: Wed, 18 Mar 2026 17:26:59 +0100 Subject: [PATCH v2 2/4] kcov: wire up compiler instrumentation for CONFIG_KCOV_EXT_RECORDS Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260318-kcov-extrecord-v2-2-2522da6fcd3f@google.com> References: <20260318-kcov-extrecord-v2-0-2522da6fcd3f@google.com> In-Reply-To: <20260318-kcov-extrecord-v2-0-2522da6fcd3f@google.com> To: Dmitry Vyukov , Andrey Konovalov , Alexander Potapenko Cc: Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, llvm@lists.linux.dev, Jann Horn , Josh Poimboeuf , Peter Zijlstra X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1773851224; l=5575; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=/5iKMb3ycj7sI45IkWou/fRJrMIENtP9YU3ZYrOJJ2E=; b=Pvlz50le1Rl6k4pchGWj+xOX1hDMosBffmKuAV4aHVkb4OKCez0m7tlYUJsGdHDNeLmsLo+rH gLx2i4q5CnICKzlmfIv0hpQoOCa0p9MHRul4gkWlLugTxFO0sx8iu52 X-Developer-Key: i=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= This is the first half of CONFIG_KCOV_EXT_RECORDS. Set the appropriate compiler flags to call separate hooks for function entry/exit, and provide these hooks, but don't make it visible in the KCOV UAPI yet. With -fsanitize-coverage=3Dtrace-pc-entry-exit, the compiler behavior chang= es as follows: - The __sanitizer_cov_trace_pc() call on function entry is replaced with a call to __sanitizer_cov_trace_pc_entry(); so for now, __sanitizer_cov_trace_pc_entry() must be treated the same way as __sanitizer_cov_trace_pc(). - On function exit, an extra call to __sanitizer_cov_trace_pc_exit() happens; since function exit produced no coverage in the old UAPI, __sanitizer_cov_trace_pc_exit() should do nothing for now. This feature was added to LLVM in commit: https://github.com/llvm/llvm-project/commit/dc5c6d008f487eea8f5d646011f9b3d= ca6caebd7 Cc: Josh Poimboeuf Cc: Peter Zijlstra Reviewed-by: Dmitry Vyukov Signed-off-by: Jann Horn --- include/linux/kcov.h | 2 ++ kernel/kcov.c | 34 +++++++++++++++++++++++++++------- lib/Kconfig.debug | 12 ++++++++++++ scripts/Makefile.kcov | 2 ++ tools/objtool/check.c | 2 ++ 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/include/linux/kcov.h b/include/linux/kcov.h index 0143358874b0..e5502d674029 100644 --- a/include/linux/kcov.h +++ b/include/linux/kcov.h @@ -81,6 +81,8 @@ typedef unsigned long long kcov_u64; #endif =20 void __sanitizer_cov_trace_pc(void); +void __sanitizer_cov_trace_pc_entry(void); +void __sanitizer_cov_trace_pc_exit(void); void __sanitizer_cov_trace_cmp1(u8 arg1, u8 arg2); void __sanitizer_cov_trace_cmp2(u16 arg1, u16 arg2); void __sanitizer_cov_trace_cmp4(u32 arg1, u32 arg2); diff --git a/kernel/kcov.c b/kernel/kcov.c index 0b369e88c7c9..86b681c7865c 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -202,15 +202,10 @@ static notrace unsigned long canonicalize_ip(unsigned= long ip) return ip; } =20 -/* - * Entry point from instrumented code. - * This is called once per basic-block/edge. - */ -void notrace __sanitizer_cov_trace_pc(void) +static __always_inline void notrace kcov_add_pc_record(unsigned long recor= d) { struct task_struct *t; unsigned long *area; - unsigned long ip =3D canonicalize_ip(_RET_IP_); unsigned long pos; =20 t =3D current; @@ -230,11 +225,36 @@ void notrace __sanitizer_cov_trace_pc(void) */ WRITE_ONCE(area[0], pos); barrier(); - area[pos] =3D ip; + area[pos] =3D record; } } + +/* + * Entry point from instrumented code. + * This is called once per basic-block/edge. + */ +void notrace __sanitizer_cov_trace_pc(void) +{ + kcov_add_pc_record(canonicalize_ip(_RET_IP_)); +} EXPORT_SYMBOL(__sanitizer_cov_trace_pc); =20 +#ifdef CONFIG_KCOV_EXT_RECORDS +void notrace __sanitizer_cov_trace_pc_entry(void) +{ + unsigned long record =3D canonicalize_ip(_RET_IP_); + + /* + * This hook replaces __sanitizer_cov_trace_pc() for the function entry + * basic block; it should still emit a record even in classic kcov mode. + */ + kcov_add_pc_record(record); +} +void notrace __sanitizer_cov_trace_pc_exit(void) +{ +} +#endif + #ifdef CONFIG_KCOV_ENABLE_COMPARISONS static void notrace write_comp_data(u64 type, u64 arg1, u64 arg2, u64 ip) { diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 93f356d2b3d9..58686a99c40a 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2215,6 +2215,18 @@ config KCOV =20 For more details, see Documentation/dev-tools/kcov.rst. =20 +config KCOV_EXT_RECORDS + bool "Support extended KCOV records with function entry/exit records" + depends on KCOV + depends on 64BIT + depends on $(cc-option,-fsanitize-coverage=3Dtrace-pc-entry-exit) + help + Extended KCOV records allow distinguishing between multiple types of + records: Normal edge coverage, function entry, and function exit. + + This will likely cause a small additional slowdown compared to normal + KCOV. + config KCOV_ENABLE_COMPARISONS bool "Enable comparison operands collection by KCOV" depends on KCOV diff --git a/scripts/Makefile.kcov b/scripts/Makefile.kcov index 78305a84ba9d..aa0be904268f 100644 --- a/scripts/Makefile.kcov +++ b/scripts/Makefile.kcov @@ -1,10 +1,12 @@ # SPDX-License-Identifier: GPL-2.0-only kcov-flags-y +=3D -fsanitize-coverage=3Dtrace-pc +kcov-flags-$(CONFIG_KCOV_EXT_RECORDS) +=3D -fsanitize-coverage=3Dtrace-pc= -entry-exit kcov-flags-$(CONFIG_KCOV_ENABLE_COMPARISONS) +=3D -fsanitize-coverage=3Dtr= ace-cmp =20 kcov-rflags-y +=3D -Cpasses=3Dsancov-module kcov-rflags-y +=3D -Cllvm-args=3D-sanitizer-coverage-level=3D3 kcov-rflags-y +=3D -Cllvm-args=3D-sanitizer-coverage-trace-pc +kcov-rflags-$(CONFIG_KCOV_EXT_RECORDS) +=3D -Cllvm-args=3D-sanitizer-cove= rage-trace-pc-entry-exit kcov-rflags-$(CONFIG_KCOV_ENABLE_COMPARISONS) +=3D -Cllvm-args=3D-sanitize= r-coverage-trace-compares =20 export CFLAGS_KCOV :=3D $(kcov-flags-y) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index a30379e4ff97..ae3127227621 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1251,6 +1251,8 @@ static const char *uaccess_safe_builtin[] =3D { "write_comp_data", "check_kcov_mode", "__sanitizer_cov_trace_pc", + "__sanitizer_cov_trace_pc_entry", + "__sanitizer_cov_trace_pc_exit", "__sanitizer_cov_trace_const_cmp1", "__sanitizer_cov_trace_const_cmp2", "__sanitizer_cov_trace_const_cmp4", --=20 2.53.0.851.ga537e3e6e9-goog