From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06A23C6FA83 for ; Tue, 27 Sep 2022 17:12:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 986818E00EB; Tue, 27 Sep 2022 13:12:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 935C18E00C1; Tue, 27 Sep 2022 13:12:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7D7148E00EB; Tue, 27 Sep 2022 13:12:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6BA268E00C1 for ; Tue, 27 Sep 2022 13:12:03 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3425680F2D for ; Tue, 27 Sep 2022 17:12:03 +0000 (UTC) X-FDA: 79958508126.25.A375A71 Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) by imf21.hostedemail.com (Postfix) with ESMTP id B33671C001A for ; Tue, 27 Sep 2022 17:12:02 +0000 (UTC) Received: by mail-yb1-f170.google.com with SMTP id e187so12966048ybh.10 for ; Tue, 27 Sep 2022 10:12:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=EOArjIky/AkNPpcTEcaVfnMEqn6weqGC+GRvG7ybtic=; b=b0Ze/1uYD/8RIi1lqnNqJlQDdJ1Fg5ARRjGEkVzw6FNjYn7DFAt2OkOIA2gyujCQPl gbM8asM9kr5gpstTiinfcmuVB9t3HY63gOpxoWy0ae7TNrQsH2stkm9FHwlXsRKMUreE T3Fjj20g7wD2vk3m+1IFA6Ta+4dhUBuSjJTYoQaDJvGbZVPvC//ko0NZZ2SRRQlv5oyv pDBJR08J5TTTTGN2+EjV22wRwabDS+fS5I0RnD2IUIXRCPx9sCTOpngD+DqHInt/CL5D MwoQtWImEUfjnh5Xt2FPHwAoTF8Eo55BVjXXcT9tmB0E1tWd9t1/hejgRon5ZrD0BcCg OciQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=EOArjIky/AkNPpcTEcaVfnMEqn6weqGC+GRvG7ybtic=; b=dwQI/ocyatgH1ZOEIlL1QPzh4bFDJy5894Mwq8pz6S1yAud02KNoSax+mhwBS+95tK nHXUVF7JYlxB8XzL7uTEO9Zjuso9/I7XAkLzvGlx7W1XnpoageipBCjL7lQy7R8KD6kv LkICHHRk846+hXE24HfFYuKWoH148Wssuwki6631fhIrWh4xElA+Jep3Pxlz8xR8Yi9g WnHgxrbYuAF2LdxlncYmv1E54HKEVSki4RHg0qRKop9pmTKnWzc6oYkt5ZY++QVjKkvU DeZbw0bLewtfevQszzI+hFFnVPV0IipfB6axcdOX2GGvTliy1t41WM8XDvccJg4pyG0E FGtg== X-Gm-Message-State: ACrzQf3WAdjTgCGeRYlPiHmti+qEXTrCodo16l5+/60lgfa9lE0q/ZmH 1qcx45a95K9juZ/QkBvCbYv2fCbhiScquQIXBQ0lLA== X-Google-Smtp-Source: AMsMyM5CV0z75xSBNmCqxHFluUuOZc04kA3T6zigg/gxJ127Uo0puFzV3o1yXRNUXgAbRM1YjJUOZooBpMt1Mu1iUgQ= X-Received: by 2002:a25:c74b:0:b0:6b4:8d79:ee67 with SMTP id w72-20020a25c74b000000b006b48d79ee67mr26821134ybe.93.1664298721697; Tue, 27 Sep 2022 10:12:01 -0700 (PDT) MIME-Version: 1.0 References: <9345acdd11e953b207b0ed4724ff780e63afeb36.1664298455.git.andreyknvl@google.com> In-Reply-To: <9345acdd11e953b207b0ed4724ff780e63afeb36.1664298455.git.andreyknvl@google.com> From: Marco Elver Date: Tue, 27 Sep 2022 19:11:25 +0200 Message-ID: Subject: Re: [PATCH mm v2 1/3] kasan: switch kunit tests to console tracepoints To: andrey.konovalov@linux.dev Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664298722; a=rsa-sha256; cv=none; b=KxDdvWWnMaSdT3mTFcMQhsncJ8w0IuAbC73HWXPb/oGOiH6bfKa1+SHabkZa1nQa7RI4eD +83iUfJvEgdGd758ryAZzQ6aihAXzJSuUPvE9yhxrRCMTmbbITrFh4iSts6r2FKrrtKqkz 8xBcyDzyAR+ZUU4MedMIJg1JWZ3xG6M= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="b0Ze/1uY"; spf=pass (imf21.hostedemail.com: domain of elver@google.com designates 209.85.219.170 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664298722; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EOArjIky/AkNPpcTEcaVfnMEqn6weqGC+GRvG7ybtic=; b=iqdCn5Vtcp61bHIvFpHdyFxk2MzB2+7MvUMEms7ktnJ88oddl8QzYOlD5C9oG0rKAbPDMB 9S3RkBseplt217uG+Q4ZgOcKzDvZ3BDrR6tXPURDE1uX8Pc1wCRJskuVL0QOS1GCx54pcK Xp/XdOdnP/yENQbomw1YSA8I2RVl1Ck= Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="b0Ze/1uY"; spf=pass (imf21.hostedemail.com: domain of elver@google.com designates 209.85.219.170 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: q5trn6iopriufbk5igd1s97c58ni5orq X-Rspamd-Queue-Id: B33671C001A X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1664298722-511058 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, 27 Sept 2022 at 19:09, wrote: > > From: Andrey Konovalov > > Switch KUnit-compatible KASAN tests from using per-task KUnit resources > to console tracepoints. > > This allows for two things: > > 1. Migrating tests that trigger a KASAN report in the context of a task > other than current to KUnit framework. > This is implemented in the patches that follow. > > 2. Parsing and matching the contents of KASAN reports. > This is not yet implemented. > > Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver > --- > > Changes v1->v2: > - Remove kunit_kasan_status struct definition. > --- > lib/Kconfig.kasan | 2 +- > mm/kasan/kasan.h | 8 ---- > mm/kasan/kasan_test.c | 85 +++++++++++++++++++++++++++++++------------ > mm/kasan/report.c | 31 ---------------- > 4 files changed, 63 insertions(+), 63 deletions(-) > > diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan > index ca09b1cf8ee9..ba5b27962c34 100644 > --- a/lib/Kconfig.kasan > +++ b/lib/Kconfig.kasan > @@ -181,7 +181,7 @@ config KASAN_VMALLOC > > config KASAN_KUNIT_TEST > tristate "KUnit-compatible tests of KASAN bug detection capabilities" if !KUNIT_ALL_TESTS > - depends on KASAN && KUNIT > + depends on KASAN && KUNIT && TRACEPOINTS > default KUNIT_ALL_TESTS > help > A KUnit-based KASAN test suite. Triggers different kinds of > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index abbcc1b0eec5..a84491bc4867 100644 > --- a/mm/kasan/kasan.h > +++ b/mm/kasan/kasan.h > @@ -261,14 +261,6 @@ struct kasan_stack_ring { > > #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ > > -#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) > -/* Used in KUnit-compatible KASAN tests. */ > -struct kunit_kasan_status { > - bool report_found; > - bool sync_fault; > -}; > -#endif > - > #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) > > static inline const void *kasan_shadow_to_mem(const void *shadow_addr) > diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c > index f25692def781..3a2886f85e69 100644 > --- a/mm/kasan/kasan_test.c > +++ b/mm/kasan/kasan_test.c > @@ -5,8 +5,12 @@ > * Author: Andrey Ryabinin > */ > > +#define pr_fmt(fmt) "kasan_test: " fmt > + > +#include > #include > #include > +#include > #include > #include > #include > @@ -14,21 +18,28 @@ > #include > #include > #include > +#include > #include > #include > +#include > #include > -#include > #include > -#include > +#include > > #include > > -#include > - > #include "kasan.h" > > #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE) > > +static bool multishot; > + > +/* Fields set based on lines observed in the console. */ > +static struct { > + bool report_found; > + bool async_fault; > +} test_status; > + > /* > * Some tests use these global variables to store return values from function > * calls that could otherwise be eliminated by the compiler as dead code. > @@ -36,35 +47,61 @@ > void *kasan_ptr_result; > int kasan_int_result; > > -static struct kunit_resource resource; > -static struct kunit_kasan_status test_status; > -static bool multishot; > +/* Probe for console output: obtains test_status lines of interest. */ > +static void probe_console(void *ignore, const char *buf, size_t len) > +{ > + if (strnstr(buf, "BUG: KASAN: ", len)) > + WRITE_ONCE(test_status.report_found, true); > + else if (strnstr(buf, "Asynchronous fault: ", len)) > + WRITE_ONCE(test_status.async_fault, true); > +} > > -/* > - * Temporarily enable multi-shot mode. Otherwise, KASAN would only report the > - * first detected bug and panic the kernel if panic_on_warn is enabled. For > - * hardware tag-based KASAN also allow tag checking to be reenabled for each > - * test, see the comment for KUNIT_EXPECT_KASAN_FAIL(). > - */ > -static int kasan_test_init(struct kunit *test) > +static void register_tracepoints(struct tracepoint *tp, void *ignore) > +{ > + check_trace_callback_type_console(probe_console); > + if (!strcmp(tp->name, "console")) > + WARN_ON(tracepoint_probe_register(tp, probe_console, NULL)); > +} > + > +static void unregister_tracepoints(struct tracepoint *tp, void *ignore) > +{ > + if (!strcmp(tp->name, "console")) > + tracepoint_probe_unregister(tp, probe_console, NULL); > +} > + > +static int kasan_suite_init(struct kunit_suite *suite) > { > if (!kasan_enabled()) { > - kunit_err(test, "can't run KASAN tests with KASAN disabled"); > + pr_err("Can't run KASAN tests with KASAN disabled"); > return -1; > } > > + /* > + * Temporarily enable multi-shot mode. Otherwise, KASAN would only > + * report the first detected bug and panic the kernel if panic_on_warn > + * is enabled. > + */ > multishot = kasan_save_enable_multi_shot(); > - test_status.report_found = false; > - test_status.sync_fault = false; > - kunit_add_named_resource(test, NULL, NULL, &resource, > - "kasan_status", &test_status); > + > + /* > + * Because we want to be able to build the test as a module, we need to > + * iterate through all known tracepoints, since the static registration > + * won't work here. > + */ > + for_each_kernel_tracepoint(register_tracepoints, NULL); > return 0; > } > > -static void kasan_test_exit(struct kunit *test) > +static void kasan_suite_exit(struct kunit_suite *suite) > { > kasan_restore_multi_shot(multishot); > - KUNIT_EXPECT_FALSE(test, test_status.report_found); > + for_each_kernel_tracepoint(unregister_tracepoints, NULL); > + tracepoint_synchronize_unregister(); > +} > + > +static void kasan_test_exit(struct kunit *test) > +{ > + KUNIT_EXPECT_FALSE(test, READ_ONCE(test_status.report_found)); > } > > /** > @@ -106,11 +143,12 @@ static void kasan_test_exit(struct kunit *test) > if (IS_ENABLED(CONFIG_KASAN_HW_TAGS) && \ > kasan_sync_fault_possible()) { \ > if (READ_ONCE(test_status.report_found) && \ > - READ_ONCE(test_status.sync_fault)) \ > + !READ_ONCE(test_status.async_fault)) \ > kasan_enable_tagging(); \ > migrate_enable(); \ > } \ > WRITE_ONCE(test_status.report_found, false); \ > + WRITE_ONCE(test_status.async_fault, false); \ > } while (0) > > #define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do { \ > @@ -1440,9 +1478,10 @@ static struct kunit_case kasan_kunit_test_cases[] = { > > static struct kunit_suite kasan_kunit_test_suite = { > .name = "kasan", > - .init = kasan_test_init, > .test_cases = kasan_kunit_test_cases, > .exit = kasan_test_exit, > + .suite_init = kasan_suite_init, > + .suite_exit = kasan_suite_exit, > }; > > kunit_test_suite(kasan_kunit_test_suite); > diff --git a/mm/kasan/report.c b/mm/kasan/report.c > index 39e8e5a80b82..f23d51a27414 100644 > --- a/mm/kasan/report.c > +++ b/mm/kasan/report.c > @@ -30,8 +30,6 @@ > > #include > > -#include > - > #include "kasan.h" > #include "../slab.h" > > @@ -114,41 +112,12 @@ EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); > > #endif > > -#if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) > -static void update_kunit_status(bool sync) > -{ > - struct kunit *test; > - struct kunit_resource *resource; > - struct kunit_kasan_status *status; > - > - test = current->kunit_test; > - if (!test) > - return; > - > - resource = kunit_find_named_resource(test, "kasan_status"); > - if (!resource) { > - kunit_set_failure(test); > - return; > - } > - > - status = (struct kunit_kasan_status *)resource->data; > - WRITE_ONCE(status->report_found, true); > - WRITE_ONCE(status->sync_fault, sync); > - > - kunit_put_resource(resource); > -} > -#else > -static void update_kunit_status(bool sync) { } > -#endif > - > static DEFINE_SPINLOCK(report_lock); > > static void start_report(unsigned long *flags, bool sync) > { > /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */ > disable_trace_on_warning(); > - /* Update status of the currently running KASAN test. */ > - update_kunit_status(sync); > /* Do not allow LOCKDEP mangling KASAN reports. */ > lockdep_off(); > /* Make sure we don't end up in loop. */ > -- > 2.25.1 >