Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp690106ybv; Wed, 19 Feb 2020 07:17:48 -0800 (PST) X-Google-Smtp-Source: APXvYqy4418rDKLC3oTdMf1o0tZJO2Cbsck5LXIyf0D2Mw9XU+ALGac43+AVFeCxIq1vFgm1Zcr5 X-Received: by 2002:aca:1b17:: with SMTP id b23mr5027142oib.95.1582125468280; Wed, 19 Feb 2020 07:17:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582125468; cv=none; d=google.com; s=arc-20160816; b=nH9qvHu4VvqPGjJTcKKRIbJyeYnguOhg8gV30mN0rtZbPxPtKEOq6m2qSSQOQILdKP Frz4PQV/tDCYaZObqWZcr4xwSkj+BaCWcKI1NJih4TPSJbWt8BiuCmZsiKAjBnKZHzM0 hm/20j7aP+BAwk2HOpH7WfmGjKRUmitN0cru0Iz7fJe6nGNYSwwL5bL1PUVSdFBcl5o+ /V3p80QVdyk2sj0qa/ybPtdlNA50OYGugYaRFokSP6t6ktsZ2TEBU5ik6lrfET+lYJd+ z3gS4WXVy+RYzQsb5frX1uQQIQwl+h9awo5Ut3OY9KIsELLImC5WwMhf3rRyY2ru13Oq 7wtg== 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:mime-version :message-id:date:dkim-signature; bh=Byu8soxgkprIbakxZbQOBFe9yLNNMXZxXhnHj/Q3XIE=; b=tflkv4oOE48l/RYqmkX4mmrh3Ftt1amiw5Ow4Yy0+I1tXAi/GXtXO3bnFZMbZArrOa VXyjEMD3Grtm71YMyOHf8x84/ltxBS6MVSFyZi+wslZWl8NtpenBl3Hi+OzmdCk79X6Y hzR+le+lFFKQTQdnVpPfLvUUT0j/4FyBXL/JD4pGJhqtWA2tmvx168942UXWPPeEzF8X 4VhhIvoTPK0h8gLvpeXoJp83N+HiwMlAjZGxp4hMoySITj0+Tdtn/YlXoZYf+Qa7Sdwo p0qVYe+eGsFngnbxZOPX95IzbqHXeG7DUxb8HI1P0g9Tb2hiIRGmQd9erTj9jHfV8f7k mnqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cW2cAcBP; 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 k19si17926otp.33.2020.02.19.07.17.29; Wed, 19 Feb 2020 07:17:48 -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=cW2cAcBP; 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 S1726634AbgBSPPo (ORCPT + 99 others); Wed, 19 Feb 2020 10:15:44 -0500 Received: from mail-wr1-f73.google.com ([209.85.221.73]:49791 "EHLO mail-wr1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727636AbgBSPPn (ORCPT ); Wed, 19 Feb 2020 10:15:43 -0500 Received: by mail-wr1-f73.google.com with SMTP id w6so204503wrm.16 for ; Wed, 19 Feb 2020 07:15:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=Byu8soxgkprIbakxZbQOBFe9yLNNMXZxXhnHj/Q3XIE=; b=cW2cAcBPaoRfi740A0YFL1zfRrWwQT3Ywad2o0tS1cjb3DWhwcokkOpkWIQVHOyuij Y9vktaPxXwfSZZm3OKWDkMNSP6w6IeDMe+Rx1UPkBMZOdd7hXHE2m6JrxXBX8ZdrrAsY Vi6M+PauJdkMZTMVfR62A965PBQ93m/Zl3MqSlBcp0SndV+8dVMqze7nCnc5TdYMYNsp U073aNUXW+L0Rfodeag1IePWwgMVdV/2SWiEYbTFe3oBVg4NllG4GVa8GpI56SL9/tRm ap4rV4BXKIOKcbdiGdz8BLtaMT4L94XMhsU7cqYBrnPkbU0YcVxUjjyA8XX0nj5s7LUu ETqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Byu8soxgkprIbakxZbQOBFe9yLNNMXZxXhnHj/Q3XIE=; b=ka29pDUuvN57zavjvLfE1uoY4CsFvS1rTxtkuQ618UZXst4TDzSonyUFS8UeTEdFEh cLBQOCBUuMn2Xuo4C4ZXOyvLpbf2eccCRex3O4EajHWXuLnhmLIg0K2Xa9XVXrNlp0Zl /fIiT9L2x3i6oDpXZWm085lZoi2lP7B7c7HCfxRks6pLqP56APoUPpHk1e7Hg8w10cUR kfsHzKMh7aqiMXllevtiqu0qR560KLlqUH3BaKLhcGkjAKTDyRqw1BAw/2Kki8a6HnFT HQ/qatrRfo9wLRnRqzYXXjy5nPpOBdQKMEU8G2ifXyIxsVJeUPszMJDc8b6sezqtneTe 3tNA== X-Gm-Message-State: APjAAAWkNnKf286RqimaAMZU95ohWbT/7F9Sph75mmOFQUO9CLgxUpFn 34YbR3Uo3fXwduzdf+xaLk+0I3RQ8w== X-Received: by 2002:a5d:6404:: with SMTP id z4mr9741809wru.262.1582125340973; Wed, 19 Feb 2020 07:15:40 -0800 (PST) Date: Wed, 19 Feb 2020 16:15:31 +0100 Message-Id: <20200219151531.161515-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH] kcsan: Add option for verbose reporting 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, Qian Cai 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 Adds CONFIG_KCSAN_VERBOSE to optionally enable more verbose reports. Currently information about the reporting task's held locks and IRQ trace events are shown, if they are enabled. Signed-off-by: Marco Elver Suggested-by: Qian Cai --- kernel/kcsan/report.c | 48 +++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.kcsan | 13 ++++++++++++ 2 files changed, 61 insertions(+) diff --git a/kernel/kcsan/report.c b/kernel/kcsan/report.c index 11c791b886f3c..f14becb6f1537 100644 --- a/kernel/kcsan/report.c +++ b/kernel/kcsan/report.c @@ -1,10 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include #include #include #include #include +#include #include #include #include @@ -245,6 +247,29 @@ static int sym_strcmp(void *addr1, void *addr2) return strncmp(buf1, buf2, sizeof(buf1)); } +static void print_verbose_info(struct task_struct *task) +{ + if (!task) + return; + + if (task != current && task->state == TASK_RUNNING) + /* + * Showing held locks for a running task is unreliable, so just + * skip this. The printed locks are very likely inconsistent, + * since the stack trace was obtained when the actual race + * occurred and the task has since continued execution. Since we + * cannot display the below information from the racing thread, + * but must print it all from the watcher thread, bail out. + * Note: Even if the task is not running, there is a chance that + * the locks held may be inconsistent. + */ + return; + + pr_err("\n"); + debug_show_held_locks(task); + print_irqtrace_events(task); +} + /* * Returns true if a report was generated, false otherwise. */ @@ -319,6 +344,26 @@ static bool print_report(const volatile void *ptr, size_t size, int access_type, other_info.num_stack_entries - other_skipnr, 0); + if (IS_ENABLED(CONFIG_KCSAN_VERBOSE) && other_info.task_pid != -1) { + struct task_struct *other_task; + + /* + * Rather than passing @current from the other task via + * @other_info, obtain task_struct here. The problem + * with passing @current via @other_info is that, we + * would have to get_task_struct/put_task_struct, and if + * we race with a task being released, we would have to + * release it in release_report(). This may result in + * deadlock if we want to use KCSAN on the allocators. + * Instead, make this best-effort, and if the task was + * already released, we just do not print anything here. + */ + rcu_read_lock(); + other_task = find_task_by_pid_ns(other_info.task_pid, &init_pid_ns); + print_verbose_info(other_task); + rcu_read_unlock(); + } + pr_err("\n"); pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n", get_access_type(access_type), ptr, size, @@ -340,6 +385,9 @@ static bool print_report(const volatile void *ptr, size_t size, int access_type, stack_trace_print(stack_entries + skipnr, num_stack_entries - skipnr, 0); + if (IS_ENABLED(CONFIG_KCSAN_VERBOSE)) + print_verbose_info(current); + /* Print report footer. */ pr_err("\n"); pr_err("Reported by Kernel Concurrency Sanitizer on:\n"); diff --git a/lib/Kconfig.kcsan b/lib/Kconfig.kcsan index f0b791143c6ab..ba9268076cfbc 100644 --- a/lib/Kconfig.kcsan +++ b/lib/Kconfig.kcsan @@ -20,6 +20,19 @@ menuconfig KCSAN if KCSAN +config KCSAN_VERBOSE + bool "Show verbose reports with more information about system state" + depends on PROVE_LOCKING + help + If enabled, reports show more information about the system state that + may help better analyze and debug races. This includes held locks and + IRQ trace events. + + While this option should generally be benign, we call into more + external functions on report generation; if a race report is + generated from any one of them, system stability may suffer due to + deadlocks or recursion. If in doubt, say N. + config KCSAN_DEBUG bool "Debugging of KCSAN internals" -- 2.25.0.265.gbab2e86ba0-goog