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=1795020057; b=JJBL5UXqKMM3sQBJhVX07SUE300unQJaaHLs0wbsYM12NAd+2vgWnlA7p3aQGwssvJ4n gmzAGxuuJtMn1SaayLSikVG+X2rVmPk5VcMVaFTv0iqx0Oyf7qGyYIP2Wr4e48BeAvYal kBP8lwAgCcJNEOjXRLBjpBj9veBZd5mEjfIgDkZgLDK1u6hTUzmu/yI0PxmbBdzO5k4zD KR1IGO4ESgCT2s+x5Aij0/B0teaglt4mHtWw0ilUWaf0C41KmixBaEw9qGXTfsgHAk1h3 lzctSRWFr1owUtgA/0HLFf/3rAz8Q7dnK/VhPs0tqE53w8VxBLuDvBrhiVrkcadR29A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795020057; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=GZhew7Qv1dYPeJylyVLgY0uQFNw9RdxY34q+6AjMgkA=; b=rYWoPuZ/8/hZYFbdSwD7ITT2Z2ueTJ/61PGo4JWjQSJ10WIA1mkS8a1c0oMRQCaG21cf 0hI2zGqCO/SyCid+yiwPXGQpKKa9f7VvrzyDYPx8eQ3iYSkvqtjIWzlKgSxGjdhzeXAi8 6qi1pnrire2RJfjHhLViWEshP8Sn7ZA2DALS5Sd2hnn2VSa9b/QQIgmD5Ia3wJUs1jKKG +ftIdsAWPxJ3I9FFw5T9hRQldFPVJgvHqqIF3IBC+tIULqa8p75HXrrbFh2X8Bez/HRNT zMzeMp59ToLm2CKmSe95vw9rSwycA1sX8+2uUjMaxl+/fTcurQtsjkpgOXf+wJyACmA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=KkZlMHDt; 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 11E81606A33D; Wed, 18 Nov 2026 16:40:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=KkZlMHDt Received: by smtp.kernel.org (Postfix) with ESMTPSA id 715BF1F000C4; Wed, 18 Nov 2026 16:40:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795020056; bh=GZhew7Qv1dYPeJylyVLgY0uQFNw9RdxY34q+6AjMgkA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=KkZlMHDtPpqiV331eytNAmzb9oInLH5D6lMe7SK6Zn0aBVtLn0/uLy8lTx4utJfw5 faVQ+pkL2diVJQHJJhPTg/2vgk7RfClTshnBN9KA5eVd4JZQxQqMX1ybwA3b+y9dk7 7W2NHirlplK50MQmvoP7r1GC07KCjlpRAwm3I5JI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Karl Mehltretter , Andrew Morton , Alexander Potapenko , Bradley Morgan , Andrey Konovalov , Dmitry Vyukov , Marco Elver , Bradley Morgan , Thomas Gleixner , Sasha Levin Subject: [PATCH 7.3 1816/2651] kcov: report spurious PCs in the interrupt selftest Date: Wed, 18 Nov 2026 15:50:56 +0100 Message-ID: <20261118150352.585910781@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150247.972732815@linuxfoundation.org> References: <20261118150247.972732815@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Karl Mehltretter [ Upstream commit 5d9f31ed855fa89e533b222d0365da81ad3b10cc ] The KCOV interrupt selftest enables KCOV_MODE_TRACE_PC without a coverage area so that spurious coverage causes a fault. If the fault path is instrumented, the coverage callback faults recursively. Observed failure modes include a stack overflow on x86_64 and arm32 getting stuck in abort handling, neither of which identifies the original coverage event. The recursive failure is not new, but commit 9a79524d1420 ("kcov: use WRITE_ONCE() for selftest mode stores") made the selftest effective on configurations where the compiler had previously removed the mode store, exposing it more broadly. Replace the open-coded kcov_mode stores with kcov_start() and kcov_stop(), passing a 16-word buffer that records up to 15 spurious PCs, with the first word holding the count. Keep KCOV enabled for the original 300 ms window, then disable it, print all recorded PCs and panic if any were recorded. Once the buffer fills, further PCs are discarded by the existing coverage callback. This preserves the selftest's hard failure while avoiding the recursive fault path. Add decanonicalize_ip() as the inverse of canonicalize_ip(), which subtracts kaslr_offset() from recorded PCs. Restore that offset before printing the PCs with %pB, since KCOV records return addresses. Link: https://lore.kernel.org/20261002181357.14293-1-kmehltretter@gmail.com Fixes: 6cd0dd934b03 ("kcov: Add interrupt handling self test") Signed-off-by: Karl Mehltretter Signed-off-by: Andrew Morton Tested-by: Alexander Potapenko Reviewed-by: Alexander Potapenko Tested-by: Bradley Morgan # Power10 Reviewed-by: Bradley Morgan Assisted-by: LLM Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Marco Elver Cc: Bradley Morgan Cc: Thomas Gleixner Signed-off-by: Sasha Levin --- kernel/kcov.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/kernel/kcov.c b/kernel/kcov.c index 54eaae97bfd67..9402227bd7ba8 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -198,6 +198,16 @@ static notrace unsigned long canonicalize_ip(unsigned long ip) return ip; } +#ifdef CONFIG_KCOV_SELFTEST +static unsigned long __init decanonicalize_ip(unsigned long ip) +{ +#ifdef CONFIG_RANDOMIZE_BASE + ip += kaslr_offset(); +#endif + return ip; +} +#endif + /* * Entry point from instrumented code. * This is called once per basic-block/edge. @@ -1107,9 +1117,11 @@ struct kcov_common_handle_id kcov_common_handle(void) EXPORT_SYMBOL(kcov_common_handle); #ifdef CONFIG_KCOV_SELFTEST +static unsigned long selftest_area[16] __initdata; + static void __init selftest(void) { - unsigned long start; + unsigned long start, i, ip, count; pr_err("running self test\n"); /* @@ -1119,15 +1131,26 @@ static void __init selftest(void) * leaks out of that section and leads to spurious coverage. * It's hard to call the actual interrupt handler directly, * so we just loop here for a bit waiting for a timer interrupt. - * We set kcov_mode to enable tracing, but don't setup the area, - * so any attempt to trace will crash. Note: we must not call any + * A small coverage area records the PCs of any leaks. The first word + * holds the count, leaving room for 15 PCs. Note: we must not call any * potentially traced functions in this region. */ + kcov_start(current, NULL, ARRAY_SIZE(selftest_area), + selftest_area, KCOV_MODE_TRACE_PC, 0); start = jiffies; - WRITE_ONCE(current->kcov_mode, KCOV_MODE_TRACE_PC); while ((jiffies - start) * MSEC_PER_SEC / HZ < 300) - ; - WRITE_ONCE(current->kcov_mode, 0); + cpu_relax(); + kcov_stop(current); + + count = READ_ONCE(selftest_area[0]); + if (count) { + pr_err("spurious coverage detected during interrupt selftest:\n"); + for (i = 1; i <= count; i++) { + ip = decanonicalize_ip(selftest_area[i]); + pr_err(" %pB\n", (void *)ip); + } + panic("kcov: interrupt selftest detected spurious coverage"); + } pr_err("done running self test\n"); } #endif -- 2.53.0