Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2744074ybh; Mon, 9 Mar 2020 12:06:29 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtW+YL8boH2TZ1VgksPpfciuSuSkhGSnT/OFyjWbqgzodIwF4RZ6kF3vkb6TF+Mr3L7I6e3 X-Received: by 2002:aca:a895:: with SMTP id r143mr90775oie.150.1583780788863; Mon, 09 Mar 2020 12:06:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583780788; cv=none; d=google.com; s=arc-20160816; b=BWWkunhCVylNWG77H0ggsuH3xKrdB9xy+VqijTfsQtK/23cnFijMRcF8lEI6N4UAG5 zkp4kS7mgdTNHPE2UhLsVf0peucfrLmNTHVeGxVmNEfanQtdQIarRgFuYKd8isovDd2F qgM9tt/5J55LoFLO/c2FgNve0Obx67yt8s2rElDKSFNB4LjOEkrsGLYQFv6aDVVbjVvH V5oL5m8OzSSTtXeak4dyyOygg4Z92jl1o33vBD7OQYBujx/XtWnW/Dv8O0M0nljOpsQI Nqfv3KH7vZeEkVlUZIfYK2cMoLunXlcjR0au62nca40UQ0wSeUSKKty6NDfdUb4WlW4R 5VGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=l1oJTceACW2Ram5YdqfcMZvjr574AoIwvlIjW1xUbCc=; b=yGdzj5kNThsiHK8oJkzUzAI11N65DyKCFpBQS9ELAnvcpRe5PwrMX7YvbHrjIVKItD ELej2QcQ7s1fT4rM+JcigLlmyrxUIBDBHmj4UNkjlb/im1G2rwdFWxaRPQf0B0yw7/ES XPwQViUKitWfkhUrQkqjM7wGwdbiFA1MjEo2rD62xNt3L+l+F4ucXY5z3czkwN4WmHj9 LPrHzGNK78d/QKnEgOJ42jD6C/RylGUupMyXGgErJQQEFYYBkcrvyq/EElxBuyoHopZo iA/D997SdCDyFyuU3NBWHZtyB3XCPpmUltaSYx63sPYnplivvQVJU5OZ8/5hRdndDRbw t96A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gI9orPO8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t25si3320939ott.228.2020.03.09.12.06.15; Mon, 09 Mar 2020 12:06:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gI9orPO8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727802AbgCITFP (ORCPT + 99 others); Mon, 9 Mar 2020 15:05:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:47842 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727606AbgCITE1 (ORCPT ); Mon, 9 Mar 2020 15:04:27 -0400 Received: from paulmck-ThinkPad-P72.home (50-39-105-78.bvtn.or.frontiernet.net [50.39.105.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9267A24671; Mon, 9 Mar 2020 19:04:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583780666; bh=2qRYs3AkcvUa9jHTA5GkpCiMXL6SrOQVADR4ZNytqkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gI9orPO8oQrhwRLAlgU6+neZjuooOQXx6RoKGvY2+R/bav3X4X8NldOVTFyAxESZJ M7xdg80utDDp/1htkxPOWR8HMcuNYIXAyf0rcew+bEEC2HQNlHgNAFLPp0QCLf4d1Q pY9xoomq6Scgxy4rhakyOxj2CeyzDa+kBU59UY8Q= From: paulmck@kernel.org To: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, kernel-team@fb.com, mingo@kernel.org Cc: elver@google.com, andreyknvl@google.com, glider@google.com, dvyukov@google.com, cai@lca.pw, boqun.feng@gmail.com, "Paul E . McKenney" Subject: [PATCH kcsan 18/32] kcsan: Add test to generate conflicts via debugfs Date: Mon, 9 Mar 2020 12:04:06 -0700 Message-Id: <20200309190420.6100-18-paulmck@kernel.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20200309190359.GA5822@paulmck-ThinkPad-P72> References: <20200309190359.GA5822@paulmck-ThinkPad-P72> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marco Elver Add 'test=' option to KCSAN's debugfs interface to invoke KCSAN checks on a dummy variable. By writing 'test=' to the debugfs file from multiple tasks, we can generate real conflicts, and trigger data race reports. Signed-off-by: Marco Elver Signed-off-by: Paul E. McKenney --- kernel/kcsan/debugfs.c | 51 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/kernel/kcsan/debugfs.c b/kernel/kcsan/debugfs.c index a9dad44..9bbba0e 100644 --- a/kernel/kcsan/debugfs.c +++ b/kernel/kcsan/debugfs.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -69,9 +70,9 @@ void kcsan_counter_dec(enum kcsan_counter_id id) /* * The microbenchmark allows benchmarking KCSAN core runtime only. To run * multiple threads, pipe 'microbench=' from multiple tasks into the - * debugfs file. + * debugfs file. This will not generate any conflicts, and tests fast-path only. */ -static void microbenchmark(unsigned long iters) +static noinline void microbenchmark(unsigned long iters) { cycles_t cycles; @@ -81,18 +82,52 @@ static void microbenchmark(unsigned long iters) while (iters--) { /* * We can run this benchmark from multiple tasks; this address - * calculation increases likelyhood of some accesses overlapping - * (they still won't conflict because all are reads). + * calculation increases likelyhood of some accesses + * overlapping. Make the access type an atomic read, to never + * set up watchpoints and test the fast-path only. */ unsigned long addr = iters % (CONFIG_KCSAN_NUM_WATCHPOINTS * PAGE_SIZE); - __kcsan_check_read((void *)addr, sizeof(long)); + __kcsan_check_access((void *)addr, sizeof(long), KCSAN_ACCESS_ATOMIC); } cycles = get_cycles() - cycles; pr_info("KCSAN: %s end | cycles: %llu\n", __func__, cycles); } +/* + * Simple test to create conflicting accesses. Write 'test=' to KCSAN's + * debugfs file from multiple tasks to generate real conflicts and show reports. + */ +static long test_dummy; +static noinline void test_thread(unsigned long iters) +{ + const struct kcsan_ctx ctx_save = current->kcsan_ctx; + cycles_t cycles; + + /* We may have been called from an atomic region; reset context. */ + memset(¤t->kcsan_ctx, 0, sizeof(current->kcsan_ctx)); + + pr_info("KCSAN: %s begin | iters: %lu\n", __func__, iters); + + cycles = get_cycles(); + while (iters--) { + __kcsan_check_read(&test_dummy, sizeof(test_dummy)); + __kcsan_check_write(&test_dummy, sizeof(test_dummy)); + ASSERT_EXCLUSIVE_WRITER(test_dummy); + ASSERT_EXCLUSIVE_ACCESS(test_dummy); + + /* not actually instrumented */ + WRITE_ONCE(test_dummy, iters); /* to observe value-change */ + } + cycles = get_cycles() - cycles; + + pr_info("KCSAN: %s end | cycles: %llu\n", __func__, cycles); + + /* restore context */ + current->kcsan_ctx = ctx_save; +} + static int cmp_filterlist_addrs(const void *rhs, const void *lhs) { const unsigned long a = *(const unsigned long *)rhs; @@ -242,6 +277,12 @@ debugfs_write(struct file *file, const char __user *buf, size_t count, loff_t *o if (kstrtoul(&arg[sizeof("microbench=") - 1], 0, &iters)) return -EINVAL; microbenchmark(iters); + } else if (!strncmp(arg, "test=", sizeof("test=") - 1)) { + unsigned long iters; + + if (kstrtoul(&arg[sizeof("test=") - 1], 0, &iters)) + return -EINVAL; + test_thread(iters); } else if (!strcmp(arg, "whitelist")) { set_report_filterlist_whitelist(true); } else if (!strcmp(arg, "blacklist")) { -- 2.9.5