Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp176100lqr; Wed, 5 Jun 2024 02:38:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVc19wTeGBcOrZ5EZnfrxNZBaldwLJ7VPshH7XC44KCus5MAxsauZbk21nO7JjA07nlGCUzHC6guxkYpuW574Tq+LrsqEDgmWK+Q3B6VQ== X-Google-Smtp-Source: AGHT+IFPkAHHc6Pa+cXpwPryBl20CJ1nPNG9mw4e7k5hIf36xGZnMLBaBiPOXDZwX7KAbsJV5DKT X-Received: by 2002:a05:6a21:279a:b0:1b2:87b7:ed0a with SMTP id adf61e73a8af0-1b2b7161bafmr2094581637.57.1717580301090; Wed, 05 Jun 2024 02:38:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717580301; cv=pass; d=google.com; s=arc-20160816; b=f+rjHzmor0Kj75yo43SqzrsRtqneuvKe+lT61+nM35HV3Pqw6Vq75nDTVWVEpWGoJ5 ngUNNPJM/vrFnEOCs1z9dXxjwc6/cUvxlwIDL1wTNL0g61AWp3n22GsERtcVLEjg2I1N +6LBqYDPT+O5dlWpU3Aav6goy7Qdny2/JtujKUayuQ4unU5RJD62dogre+LHfQVcaRrC nnDNe+WH4vNXYFfZDg3Fq1FSRhDIwsQ3UbG/X1jd1ZKirAZ1Z2clfN3QGYY2jyAIFSYD RJTHCoc6+/P3oyGZnMf1fcDOwp8w12ZBOxQQDzEJvH+5QFrlYI/UiQVVj4e/St3qsf2t k/kg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=YIIkd0qfqF3cd1xqtDRqCrMiooCb5pj7Hihyirw1UUA=; fh=e8aRFB1VqXyECAiNE4Apx/kuDV9GzPeSKOPibexPtB0=; b=AxkBuZyXTdbDgGNLLZFiFT8P3NzX30xo4OMC0haro6Kc5e+N2iF0U3uGJ32SCgYX1B vqTAgLldoEq9K6YnxZtFTmaFSsSzkvDfNbuHPCFxmuL9yK23xpXufEna8u7kRBo9Lvii uvqubt3V26SExhB2OnPFElNWfeJ27PLxtKWlOvNUe5IDghf7dC2FeMfPZNfJks934FuO 9QHXx6q9DRrVyPVOO5luwHWy/VAiEcEdl020udsL2IAtbNFn3nm244MRKwWrDrH1TtGJ rS2NO8OH7N5/w1F4rBs5LQzjsiSXRu2MTPpBJoT4iRpeGPK1ZGbqHaH8sVKtP86BMG5W +aZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kZYftunl; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-202118-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202118-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f63236663esi39649655ad.171.2024.06.05.02.38.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 02:38:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202118-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kZYftunl; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-202118-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202118-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A8384281DA4 for ; Wed, 5 Jun 2024 09:38:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A822618FC78; Wed, 5 Jun 2024 09:33:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kZYftunl" Received: from mail-vk1-f179.google.com (mail-vk1-f179.google.com [209.85.221.179]) (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 29B4C18FDBF for ; Wed, 5 Jun 2024 09:33:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717580033; cv=none; b=sT6C6vrKqrTDqex9Fi3z8IFvMAQEb4OCTlPhZAHA4dvwDxFQGYoPpxzmYvHvD6j/doUzRVQU09WEj1ljA/CK3H4dJ0QeHLth13pddJq0s4rGuLWSqLoXN2c2PalIyVL5XzhfD038D4k0B2oPgk/GF/lAuMcN+L9FeTp8erJGqcQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717580033; c=relaxed/simple; bh=0yenf1GqW2sxQ2LAAzM9s07WPp6jrULYlI3dwmd29Tk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=IA15H0UiBrG6gITRQEGAh7T8AmTmmeQ69O4FUrdr0PSnl1suFeJKjCES2N1hl4tCI3QqvCFmITfxEO2uom2JJXhsX7Md24HnS8gQ5qJSaMmxwlFJ1xkPjsVP8JrQWB1ek4BxYuvMqzEjUXihBMrdZljAPmv9Rts+4C4PyqMqfpc= 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=kZYftunl; arc=none smtp.client-ip=209.85.221.179 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 Received: by mail-vk1-f179.google.com with SMTP id 71dfb90a1353d-4eb25f3c203so874840e0c.0 for ; Wed, 05 Jun 2024 02:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717580031; x=1718184831; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=YIIkd0qfqF3cd1xqtDRqCrMiooCb5pj7Hihyirw1UUA=; b=kZYftunlnNTOtXepJF2L6Jb229NunzrAWCF1YndqluolfSuvWyKz7VhXcOA+zifhTa KTY2QhoRHIjbovUOavXA+h8rUmU5dgWCHjyQ2V3XgDlpuhXMDF8a9xCxj+bS859sylnp a+Lcqh1fV1jJ7Y/9s5pVCiflTVfkle55/0ZZ13kl6g0gF7EWNPPlD1gTsO5h9FbpTjVG 5m68ZqfB5+GsNWauUcb4U976DelY1o0eYgg62IWyLZaJeCuopN5lm9qzlPIFy8+XAdeW PY+ctxGD52Ctxu4WvtRJJlxC/r8PBqqFSVzFwvG31yZ90h0/A3T1ikB8DFyx/NKRDS79 8CiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717580031; x=1718184831; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YIIkd0qfqF3cd1xqtDRqCrMiooCb5pj7Hihyirw1UUA=; b=LIjBhyhzklDQNp7THJNd7F+uFx+SDnXwrc/zL2MDJ6/64sBM7EiNv4KMVrTsvc5TB/ zZ/xl6lI1iUuS0KVtJTinsgnYNeCOz3dRGdfV4m9vcGXqoBMw63FflQKr6j8YUWqSFgV N9qZtacFZySRsQHmQtKaw3N22i3PClyENFS45jWW4EmsiIhCvXIbCQ3pv4cTOyR2fdeq Rt08JT24CrAmlm4QtWdCr18M8aoPL67OEOh9rtxd4ff4H4Wm6cMcJlG4IJG344nU/ZEw TZjsO223MqA42uwwnzv0bsDRsJ+5p6Mu6rONe3tXfMbxBkF56R1mk85AlSrUER/3+A5z uYOA== X-Forwarded-Encrypted: i=1; AJvYcCUGZ09wwW1Ym4kXFHRewa7THT0xPTBwEj1JU48lFTHj6e4esKHZ6D5XU/slPh0l8sHMq/NWvg/8C7mArVfGSDoaa4D55MjL1Zfd1VDu X-Gm-Message-State: AOJu0YxZIRhSNoZtsVHThYtLXlc0nMFtpjLAXMTsfLt5h58TJR4uviIW RmB442YWolyLdsK/HOIJEsAv0auQEYGaSzaGvOq+LVJke1HYAZMoQ8qIPR3JlLas99TmiMXXQAc D8TkdAsTmw6ecOSqLQel5J4P4J9gjv7Y6vQ/j X-Received: by 2002:a05:6122:155:b0:4e4:ef42:633a with SMTP id 71dfb90a1353d-4eb3a40681dmr1944417e0c.8.1717580030639; Wed, 05 Jun 2024 02:33:50 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <80972769ad2ebebc7ab0c39bd48f31ce080f0394.1717507310.git.dvyukov@google.com> In-Reply-To: From: Marco Elver Date: Wed, 5 Jun 2024 11:33:13 +0200 Message-ID: Subject: Re: [PATCH 2/4] kcov: add interrupt handling self test To: Dmitry Vyukov Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, linux-kernel@vger.kernel.org, syzkaller@googlegroups.com, glider@google.com, nogikh@google.com, tarasmadan@google.com Content-Type: text/plain; charset="UTF-8" On Wed, 5 Jun 2024 at 11:18, Dmitry Vyukov wrote: > > On Wed, 5 Jun 2024 at 11:10, Marco Elver wrote: > > > > > Add a boot self test that can catch sprious coverage from interrupts. > > > The coverage callback filters out interrupt code, but only after the > > > handler updates preempt count. Some code periodically leaks out > > > of that section and leads to spurious coverage. > > > Add a best-effort (but simple) test that is likely to catch such bugs. > > > If the test is enabled on CI systems that use KCOV, they should catch > > > any issues fast. > > > > > > Signed-off-by: Dmitry Vyukov > > > Cc: x86@kernel.org > > > Cc: linux-kernel@vger.kernel.org > > > Cc: syzkaller@googlegroups.com > > > > > > --- > > > > > > In my local testing w/o the previous fix, > > > it immidiatly produced the following splat: > > > > > > kcov: running selftest > > > BUG: TASK stack guard page was hit at ffffc90000147ff8 > > > Oops: stack guard page: 0000 [#1] PREEMPT SMP KASAN PTI > > > ... > > > kvm_set_cpu_l1tf_flush_l1d+0x5/0x20 > > > sysvec_call_function+0x15/0xb0 > > > asm_sysvec_call_function+0x1a/0x20 > > > kcov_init+0xe4/0x130 > > > do_one_initcall+0xbc/0x470 > > > kernel_init_freeable+0x4fc/0x930 > > > kernel_init+0x1c/0x2b0 > > > --- > > > kernel/kcov.c | 28 ++++++++++++++++++++++++++++ > > > lib/Kconfig.debug | 9 +++++++++ > > > 2 files changed, 37 insertions(+) > > > > > > diff --git a/kernel/kcov.c b/kernel/kcov.c > > > index c3124f6d5536..04136f80042f 100644 > > > --- a/kernel/kcov.c > > > +++ b/kernel/kcov.c > > > @@ -1057,6 +1057,30 @@ u64 kcov_common_handle(void) > > > } > > > EXPORT_SYMBOL(kcov_common_handle); > > > > > > +#ifdef CONFIG_KCOV_TEST > > > +static void __init selftest(void) > > > +{ > > > + volatile int i; > > > + > > > + pr_err("running self test\n"); > > > + /* > > > + * Test that interrupts don't produce spurious coverage. > > > + * The coverage callback filters out interrupt code, but only > > > + * after the handler updates preempt count. Some code periodically > > > + * 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 ~400 ms waiting for a timer interrupt. > > > > Where do the 400 ms come from? I only see that it loops a long time, > > but that the timing is entirely dependent on how fast the CPU executes > > the loop. > > > > > + * We set kcov_mode to enable tracing, but don't setup the area, > > > + * so any attempt to trace will crash. > > > + */ > > > + current->kcov_mode = KCOV_MODE_TRACE_PC; > > > + for (i = 0; i < (1 << 28); i++) > > > + ; > > > > Can't you check jiffies, and e.g. check that actual ~100-500ms have elapsed? > > > > timeout = jiffies + msecs_to_jiffies(300); > > while (!time_after(jiffies, timeout)) { > > cpu_relax(); > > } > > We can't call any functions. If anything is instrumented, the kernel crashes. > > But just reading jiffies should be fine, so we can do: > > unsigned long start = jiffies; > while ((jiffies - start) * MSEC_PER_SEC / HZ < 500) > ; I'm quite sure that those helpers are macros, but who knows if that will ever change. The above open-coded version looks reasonable.