Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1672694ybv; Thu, 6 Feb 2020 07:53:06 -0800 (PST) X-Google-Smtp-Source: APXvYqwsdLq/xDFsAsu5h3Iu2p5nJgf8y6lLXMVhnEOes6al8OGUsCxCkZjLLLcUtPo5CE8YkGNq X-Received: by 2002:a9d:545:: with SMTP id 63mr32206363otw.285.1581004386329; Thu, 06 Feb 2020 07:53:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581004386; cv=none; d=google.com; s=arc-20160816; b=a626wbGesd/1EM4EUpul+osOznVdb7HZwqW4xaxiUese5ijiLvRbpPmcrYY3iAnk8O dLzizAHA3QvuN6eJZDhrxtpt4EvTjzqrfko3RnOE8KHxrmXCkQgUZVIg15X4oL5Umvlt wlEdAQvkL9UbTWjQPRID91Bgl3Ch764psL+iRB3q4Pneo8LCQD4on3OTuydfB64NtEuE qQZ0UAkctio7I2JvuU7CiMcYa2JeOpVrwxafXTzqOhxwFYj0IsVn4lHIqPCwafGb8luS 7z3kglnN/xTqfi1exw6V4T3vlM8ViFzl+XiT/4w0QXKiDrPttiR8oQ8DENNiUFAFk/j9 oUFQ== 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=dJMzj3shZa2WtTpFPhLyRyZzYBIlQLsl5gnyIHG8III=; b=hDly+hqldzCZzHgMqTUSyz+w0TzamXE4DloeIagHU26/yMerqvE6ZoNbCnJB/hJHWh xBOwmYHuoaOVE+RahoiT0rUUmaiaM62wbuAf2yUSkcPY21KSErEsQkhAMhuGwHMJhQMJ 0RDbWWw0RDgLhp02yz9tIyQMtQpZIwbL3mkJ2ESNc6J8k6R5DKwLltyXLEe2oWET1URD 6c35LwOFRmsBPs2Q9FZvopEUjKzcfubAqB1LXhkCXno54RXOrZxi1F8A0KvF714Q+RcJ JtBvYtLzzgYXtu0pe+hfe4lcKV9OxSuKaaNUCYTZjUCNZj+kYq4NKDihV810BFsmDIr+ oyzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fS6WBSvH; 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 d26si2031715otc.6.2020.02.06.07.52.54; Thu, 06 Feb 2020 07:53:06 -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=fS6WBSvH; 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 S1727607AbgBFPv4 (ORCPT + 99 others); Thu, 6 Feb 2020 10:51:56 -0500 Received: from mail-wm1-f74.google.com ([209.85.128.74]:33787 "EHLO mail-wm1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727138AbgBFPvz (ORCPT ); Thu, 6 Feb 2020 10:51:55 -0500 Received: by mail-wm1-f74.google.com with SMTP id l11so629791wmi.0 for ; Thu, 06 Feb 2020 07:51:54 -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=dJMzj3shZa2WtTpFPhLyRyZzYBIlQLsl5gnyIHG8III=; b=fS6WBSvHWKHwTXDCVAu4fKASHYayWHCksUseKksJqEPSb5186BEYznh/NpvlIsHwx5 2tCtRb8i3L6HjO3y8at3R8fkeXFBmYOXM+sMlCflzIhaOIwb6a/QUUj29/8X+jQTKSSc yYCjT1dTI8Sv6NbSM4s+9rMDTONHvSmbhgJwDFvBZpU1TfXwPI62foH8Pn/AJk4kBOkY Mrgz3tx3CJzj5gA7536avTYbm1GdCphSvyKtFjiozUUV1Xu46i5FLstPijFkqhKaSx6+ 4k9vvncK9m1CJ/VE67X3jY7sDI9tmf10wEgjNI8ydrCfHAMaSkddTQKE98ZdAI963aR1 N5zw== 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=dJMzj3shZa2WtTpFPhLyRyZzYBIlQLsl5gnyIHG8III=; b=NvObmTTw3VvDnW7iTVtNbx2RC/d0SLLq9g+xw24LPxUE2m+ebyPcvpo+BRBOkF41Wt dGd+10mDxr7mkzrhonYE1tNk0PsM4UWyd5Ir9woTqN6SLZsuva9VCO8tQCIOzAgDsiEs NhPoiY/Y3nJ2oGKxksoq1vLfsAuNhz4yI8i34py2gVwH6f7gmnLJnoTtUOPfUYJopfVB 6LtMmEM49cM2jKBhiLD8lW6jIFla+boKbEx4hHJ8P6jdXFIaI2jme0PG2GbQorFS+QoX dzfTL7/dbcOgWLKVwxW2Ypj4XfBrUJ1NOV/Nh4SoVMSV1I3T4eTBg1imnvrREQlDLITN aCog== X-Gm-Message-State: APjAAAVCaHV/tOKPkAb4GQO/nrFQM3Qh4vicU7apuIM/a9aens00rd+d UyIjwESh/wrYSFAY6a/Z/m4cJA77LA== X-Received: by 2002:a5d:4692:: with SMTP id u18mr4511821wrq.206.1581004313452; Thu, 06 Feb 2020 07:51:53 -0800 (PST) Date: Thu, 6 Feb 2020 16:46:26 +0100 In-Reply-To: <20200206154626.243230-1-elver@google.com> Message-Id: <20200206154626.243230-3-elver@google.com> Mime-Version: 1.0 References: <20200206154626.243230-1-elver@google.com> X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH v2 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 a9dad44130e62..9bbba0e57c9b3 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.25.0.341.g760bfbb309-goog