Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp693937ybv; Wed, 5 Feb 2020 12:47:20 -0800 (PST) X-Google-Smtp-Source: APXvYqwAHVoYkG6GBCLhK/BkQ4sl1DQ3uXmDdOKE2bhtCWbAMkPt+ElpS/ixozZhfJRWw9+APLJT X-Received: by 2002:a05:6808:3b2:: with SMTP id n18mr4309579oie.146.1580935640599; Wed, 05 Feb 2020 12:47:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580935640; cv=none; d=google.com; s=arc-20160816; b=M9whCCLPImbC+Ni1HT7omYeKpluOCC7W0Ads1q0wqoKJX6xv5nNodLwwKsDtNDJ2vz 3cVKhm6CwNsZi4jhXg5woLcdcJcuy5bI+z0YA4UankMcL4VcV+ni6v2wHHPdII87ygEN 12kaY9uiWiJXDBQhXWTvI6Dq1sCyFj2QSVDNRm77S7LmaCD3KzPnb/IIKlaZ1cY+iFMU OCB2QzsUmIWePPWuMwRNlirTz15sBc1NOgt10qX3f2273yfRuscMzWmOdIN9yESLrgRk jhFCsq1dnPTFr1eNTB+5e3RsvuhTZw1Ym23fxColBkoNoC2rTGrOGYhhYTjF4t9BJrjR k51w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=Sd30aiJo7Ul/w6KJx1kKYsloupmct0RdFlLDgIoc8Tw=; b=OTDnzMmkTd2ua6/s0rFTlFwYt/4z7Z8vumtktpu9Kc2UInt5a4C9mFSl2zWmIf534C sBNxuRfS/asfjBxdhHOG22nZGbCrmHsCoomdHYqpIAlhkbKIyob32YCBm2SCi/dtxoUS P19Rf6VEqN6sJ41TRvZ6JU7wsze6VDI4lV/xxxmMLrvQAI7lA0i6w1llARxUG/7iqOIO iAsl/KS1feADfMXKap6yrV80aRCzt/8cDPG+RDfy77galbUEUK/N+Hq7u7IaCCdj5n/7 v2wCQFCvaRTUuujiYuy8fKCEHNRXlP+/swb6el/209hKDJm/E9uA90B7/Uv4gUabMx/I 4E2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Qc/9JiZY"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v18si410845otq.209.2020.02.05.12.47.07; Wed, 05 Feb 2020 12:47:20 -0800 (PST) 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=@google.com header.s=20161025 header.b="Qc/9JiZY"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727441AbgBEUoZ (ORCPT + 99 others); Wed, 5 Feb 2020 15:44:25 -0500 Received: from mail-wr1-f74.google.com ([209.85.221.74]:49415 "EHLO mail-wr1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727234AbgBEUoY (ORCPT ); Wed, 5 Feb 2020 15:44:24 -0500 Received: by mail-wr1-f74.google.com with SMTP id w6so2075961wrm.16 for ; Wed, 05 Feb 2020 12:44:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Sd30aiJo7Ul/w6KJx1kKYsloupmct0RdFlLDgIoc8Tw=; b=Qc/9JiZYKEpzdk3Dv4/p/QZGGKufORf6dG8B4ZMwIrgHIlOhgWo6mrsNtS3vLXR2AY 058UBjih4aBEqh2Kb4jILoVN0IpeuUtXx3+UJUqjU0/jTXuKeg1dBE9bcMERCeX/7hnU TdW6rI7RkI2/rggJIsSq2b2Sq3aO2GBtbwyXrJUhjmDtEnBgCw/AGaFFEVgMITLf0VjP 7/43Qr+7upe/FvEEanOmws58UdJ6XPOB0u/WqotwosXLsLuM1mIaZze6antxZ5ekbWoD DKrgGeBPrwuvwyxYQETG1w4VAy3pWJsKVDXkCywThW/X/T0jnBSv0nzZMw8G6cjMdAEz PN3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Sd30aiJo7Ul/w6KJx1kKYsloupmct0RdFlLDgIoc8Tw=; b=UkRvnaFvmZy86bcKpzbO+AmHQSPHKzbT0xRrOSN3git/jKogGfo05UgnGmKKqTEsXg v3P/+3vzFIzJyT2JZLqVOP/vpwsNMEtERrsltdEDP/HD/12j8MU6XnnYNS8Kt9etfpG9 PVHpVLJEQVof+ETPGAvmQzmhupRW2UcSbcGojWJmf220HUujhqLqETs3qoDeHTc9g6kF flcxlg226gYodeAfnK9YgnCy5lX91/i5bZ6Y69huawlKxeBKDnzTHkm/qOXU+YIRkauf qqNmm5OC4tFjA8enZTIErwedZnfBboPeFLdGT5X+po/WwuTWyu/YPAFT0yIpi8eCjKRY pf1Q== X-Gm-Message-State: APjAAAWeL33haDZEjrrSSf0fbf8RFKoto92zmbQbhwAXzsbw+1ftav0k QGz69q4oFapk0wxI9WI3gwhsZr3cNw== X-Received: by 2002:a5d:68cf:: with SMTP id p15mr345787wrw.31.1580935461105; Wed, 05 Feb 2020 12:44:21 -0800 (PST) Date: Wed, 5 Feb 2020 21:43:33 +0100 In-Reply-To: <20200205204333.30953-1-elver@google.com> Message-Id: <20200205204333.30953-3-elver@google.com> Mime-Version: 1.0 References: <20200205204333.30953-1-elver@google.com> X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH 3/3] kcsan: Add test to generate conflicts via debugfs From: Marco Elver To: elver@google.com Cc: paulmck@kernel.org, andreyknvl@google.com, glider@google.com, dvyukov@google.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 bec42dab32ee8..5733f51a6e2c7 100644 --- a/kernel/kcsan/debugfs.c +++ b/kernel/kcsan/debugfs.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -68,9 +69,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; @@ -80,18 +81,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; @@ -241,6 +276,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.25.0.341.g760bfbb309-goog