From patchwork Fri Jan 15 13:03:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12022689 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78837C433E0 for ; Fri, 15 Jan 2021 13:03:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2ED602256F for ; Fri, 15 Jan 2021 13:03:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2ED602256F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9EB638D0166; Fri, 15 Jan 2021 08:03:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C15C8D0165; Fri, 15 Jan 2021 08:03:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B0E18D0166; Fri, 15 Jan 2021 08:03:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0246.hostedemail.com [216.40.44.246]) by kanga.kvack.org (Postfix) with ESMTP id 6EFDC8D0165 for ; Fri, 15 Jan 2021 08:03:44 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 18AE8824556B for ; Fri, 15 Jan 2021 13:03:44 +0000 (UTC) X-FDA: 77708026368.30.songs45_580a0d12752f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id CBC82180B3C85 for ; Fri, 15 Jan 2021 13:03:43 +0000 (UTC) X-HE-Tag: songs45_580a0d12752f X-Filterd-Recvd-Size: 8226 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 13:03:43 +0000 (UTC) Received: by mail-wr1-f73.google.com with SMTP id g17so4125944wrr.11 for ; Fri, 15 Jan 2021 05:03:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=0Sfvm3HijLTxMIkalgPZehN/JMQjFZGSx3Id5apZcBk=; b=uPrTay/imcPULPNls0Mtk1WZkZoq/HSa2GtNhwnLAZ4/7vKSh6Z2BhjbC16uPDnBJc Pyav+84MX3O+j6zJGBdHTfyajgZxnJ4BQUfVVuf2aqP9d4dbdgk9duMPjwLg01VOR2iq DBjdYljZlnVpt8G1+MWE5etRwEO/rNMH241Yu0fT9nAMZruspL3O5mH+FzaeN8JP2xLf 2aOZP2rNIKZB7KViJeesmg1gEo0AabTd6jO/iWd7bbChPKR2P5iGDCoEDkQtrhAIq/bn Mzgv3Ci/ZrQan7CtzP7eUnaaHiBx1uyowDA/uCAV3AyKS6oWTCfv/r1IKWo0L6sV6Hx1 FEHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=0Sfvm3HijLTxMIkalgPZehN/JMQjFZGSx3Id5apZcBk=; b=IOAPas6aN2oJXMteuDeYjHSBpmwEcVDyWClTthDkUNpKRNwKgB/bknczc6A7VUP0wO T1h4FfqDbdYcGZX4ZWFRTjqDq+72NE8/ktt1aUVHhjitspp5TdtfbJfWHnpbgAs0JAUS ZfHUEftDRQOZj/vYRYCTnsq9rNXrtsh3IdHDukn2Z7F3R0mL6WNPxqWhwy8RXh8hrZ7q E6IIHL9hmFRB9b38Vq6yIs56Ht+HuuE5nziNEfnXLNkSCkVwFavRBm5k7VHixljErbYe /s7dfTw2XEKjwXldmxDw+jwlrAnIlZStnqrYxzcm/zJF2mY7sIlVmN4L9ZvkFJO1Xc0D 0YcA== X-Gm-Message-State: AOAM532KTGfcSqj9U/kNUpEgnGbSGwXrPn+3P5SpbCfk2Vt0e5U3bmpN 48h1JIw/KNVR/sQ8Gd6gfxeSeLefMQ0= X-Google-Smtp-Source: ABdhPJwsPbgtxFhOrkkTGSft6oAz3er3XSA+s50wGbXmAapn6NAssDXnX9QGmjP4AKPQLDC0ZXkaDz5Z28I= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:9ff]) (user=glider job=sendgmr) by 2002:adf:8342:: with SMTP id 60mr13209464wrd.140.1610715822120; Fri, 15 Jan 2021 05:03:42 -0800 (PST) Date: Fri, 15 Jan 2021 14:03:32 +0100 In-Reply-To: <20210115130336.2520663-1-glider@google.com> Message-Id: <20210115130336.2520663-2-glider@google.com> Mime-Version: 1.0 References: <20210115130336.2520663-1-glider@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 1/5] tracing: add error_report trace points From: Alexander Potapenko To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org Cc: andreyknvl@google.com, dvyukov@google.com, mingo@redhat.com, elver@google.com, pmladek@suse.com, rostedt@goodmis.org, sergey.senozhatsky@gmail.com, glider@google.com, linux-mm@kvack.org 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: Introduce error_report_start and error_report_end tracepoints. Those can be used in debugging tools like KASAN, KFENCE, etc. to provide extensions to the error reporting mechanisms (e.g. allow tests hook into error reporting, ease error report collection from production kernels). Another benefit would be making use of ftrace for debugging or benchmarking the tools themselves. Suggested-by: Marco Elver Cc: Andrew Morton Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Ingo Molnar Cc: Marco Elver Cc: Petr Mladek Cc: Steven Rostedt Cc: Sergey Senozhatsky Cc: linux-mm@kvack.org Signed-off-by: Alexander Potapenko --- v2: - change error_report_start and error_report_end prototypes to accept enum error_detector instead of char* (as suggested by Steven Rostedt) --- include/trace/events/error_report.h | 84 +++++++++++++++++++++++++++++ kernel/trace/Makefile | 1 + kernel/trace/error_report-traces.c | 11 ++++ 3 files changed, 96 insertions(+) create mode 100644 include/trace/events/error_report.h create mode 100644 kernel/trace/error_report-traces.c diff --git a/include/trace/events/error_report.h b/include/trace/events/error_report.h new file mode 100644 index 000000000000..bbf8543fc31a --- /dev/null +++ b/include/trace/events/error_report.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM error_report + +#if !defined(_TRACE_ERROR_REPORT_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_ERROR_REPORT_H + +#include + +#ifndef __ERROR_REPORT_DECLARE_TRACE_ENUMS_ONCE_ONLY +#define __ERROR_REPORT_DECLARE_TRACE_ENUMS_ONCE_ONLY + +enum error_detector { + ERROR_DETECTOR_KFENCE, + ERROR_DETECTOR_KASAN +}; + +#endif /* __ERROR_REPORT_DECLARE_TRACE_ENUMS_ONCE_ONLY */ + +#define error_detector_list \ + EM(ERROR_DETECTOR_KFENCE, "kfence") \ + EMe(ERROR_DETECTOR_KASAN, "kasan") +/* Always end the list with an EMe. */ + +#undef EM +#undef EMe + +#define EM(a, b) TRACE_DEFINE_ENUM(a); +#define EMe(a, b) TRACE_DEFINE_ENUM(a); + +error_detector_list + +#undef EM +#undef EMe + +#define EM(a, b) { a, b }, +#define EMe(a, b) { a, b } + +#define show_error_detector_list(val) \ + __print_symbolic(val, error_detector_list) + +DECLARE_EVENT_CLASS(error_report_template, + TP_PROTO(enum error_detector error_detector, unsigned long id), + TP_ARGS(error_detector, id), + TP_STRUCT__entry(__field(enum error_detector, error_detector) + __field(unsigned long, id)), + TP_fast_assign(__entry->error_detector = error_detector; + __entry->id = id;), + TP_printk("[%s] %lx", + show_error_detector_list(__entry->error_detector), + __entry->id)); + +/** + * error_report_start - called before printing the error report + * @error_detector: short string describing the error detection tool + * @id: pseudo-unique descriptor that can help distinguish reports + * from one another. Depending on the tool, good examples + * could be: memory access address, call site, allocation + * site, etc. + * + * This event occurs right before a debugging tool starts printing the error + * report. + */ +DEFINE_EVENT(error_report_template, error_report_start, + TP_PROTO(enum error_detector error_detector, unsigned long id), + TP_ARGS(error_detector, id)); + +/** + * error_report_end - called after printing the error report + * @error_detector: short string describing the error detection tool + * @id: pseudo-unique descriptor, matches that passed to + * error_report_start + * + * This event occurs right after a debugging tool finishes printing the error + * report. + */ +DEFINE_EVENT(error_report_template, error_report_end, + TP_PROTO(enum error_detector error_detector, unsigned long id), + TP_ARGS(error_detector, id)); + +#endif /* _TRACE_ERROR_REPORT_H */ + +/* This part must be outside protection */ +#include diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile index 7e44cea89fdc..b28d3e5013cd 100644 --- a/kernel/trace/Makefile +++ b/kernel/trace/Makefile @@ -81,6 +81,7 @@ obj-$(CONFIG_SYNTH_EVENTS) += trace_events_synth.o obj-$(CONFIG_HIST_TRIGGERS) += trace_events_hist.o obj-$(CONFIG_BPF_EVENTS) += bpf_trace.o obj-$(CONFIG_KPROBE_EVENTS) += trace_kprobe.o +obj-$(CONFIG_TRACEPOINTS) += error_report-traces.o obj-$(CONFIG_TRACEPOINTS) += power-traces.o ifeq ($(CONFIG_PM),y) obj-$(CONFIG_TRACEPOINTS) += rpm-traces.o diff --git a/kernel/trace/error_report-traces.c b/kernel/trace/error_report-traces.c new file mode 100644 index 000000000000..80960c52c705 --- /dev/null +++ b/kernel/trace/error_report-traces.c @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Error reporting trace points + */ + +#define CREATE_TRACE_POINTS +#include + +EXPORT_TRACEPOINT_SYMBOL_GPL(error_report_start); +EXPORT_TRACEPOINT_SYMBOL_GPL(error_report_end); + From patchwork Fri Jan 15 13:03:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12022691 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8BDDBC433DB for ; Fri, 15 Jan 2021 13:03:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2C55D221FA for ; Fri, 15 Jan 2021 13:03:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2C55D221FA Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 30C958D0168; Fri, 15 Jan 2021 08:03:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C0218D0167; Fri, 15 Jan 2021 08:03:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 185628D0168; Fri, 15 Jan 2021 08:03:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id ED6CA8D0167 for ; Fri, 15 Jan 2021 08:03:45 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id AE3D21E16 for ; Fri, 15 Jan 2021 13:03:45 +0000 (UTC) X-FDA: 77708026410.06.water57_0101f392752f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 9FDEF1004E3ED for ; Fri, 15 Jan 2021 13:03:45 +0000 (UTC) X-HE-Tag: water57_0101f392752f X-Filterd-Recvd-Size: 15527 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 13:03:45 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id b206so7862143qkc.14 for ; Fri, 15 Jan 2021 05:03:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=l8WxWOfr3FkIGE+fyTuz9WjeqQaz3VriJuPOeaywOdQ=; b=iZHQRnZo5yHPWQJQQ/RFTuMLLyLkgnxzaVjarRzSNL4MQK9QhsbW4n7+F3xlNebd6m 0D2j8L3to0+gXx88UyC/XM3j8K+kBTCcJH9q9ACLU7x+PMFo0DDDIJAKxNo7yzo7j08H 3cM4iWyIVFZXReFo3na0oqpmfPzdatZtWr3On5W4YYIcaFJNiu10MEZi1Aglb4DYNaeY FWSfIMtjU6OJKO/Ah16SdCLkY5jGXwd39W8VyoidVKGULcD3giGvju0cTHHOcd9IiZRY 3JXw8foHkE8rjiU9QoSR1EqJi5iatKpuLa7fgITxhDmoaxvfEuMvxECqP0eA+JUe+2kt omhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=l8WxWOfr3FkIGE+fyTuz9WjeqQaz3VriJuPOeaywOdQ=; b=pcSLLWQqkM9XuDzKW34qYMyXMFz7VyXlXhPp+U0jYWTEq/G63LAjR3NcWDyOj4AmX4 UPKhQOVhX0QqExgJVPQQOctKYtmLoaEMHExgEBekIn9eJ5qn3+XLct9GMmQpVb04BYqw hx05RHI4vwwwCX6OpDBCRfK0s/gPEcRill9+Ed2zk7wGLrqv+BT6QTUdcoHbsM00OMLc ILa3yKY44vA68uEX+9FLFq04T0D5D4jrCdc+RVZbGv2XFMNl9vGs0cfUdfZL+CPLRkzQ APjwuGsFiKi8Wy7cADZUZtxn2fufnBshf6612SXVQNQpevOIZ8vSnefxHabBKdpFr5hq na3Q== X-Gm-Message-State: AOAM530mL3DPYHy7aQ74mLHpw+c8HHJRc/QGv0h+eVPDt6X/+NTuDsdv 9M3TpHZHQh8KgQiYafQEJ8hT9138zug= X-Google-Smtp-Source: ABdhPJwouldA38t9gpv7Z5+mrr7Mp/l02Mp5ahH0k0OxEVkX3ARckdOaLNJewW0gAVapzQnqbR9vgGrWG/g= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:9ff]) (user=glider job=sendgmr) by 2002:ad4:4b21:: with SMTP id s1mr12024258qvw.59.1610715824256; Fri, 15 Jan 2021 05:03:44 -0800 (PST) Date: Fri, 15 Jan 2021 14:03:33 +0100 In-Reply-To: <20210115130336.2520663-1-glider@google.com> Message-Id: <20210115130336.2520663-3-glider@google.com> Mime-Version: 1.0 References: <20210115130336.2520663-1-glider@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 2/5] lib: add error_report_notify to collect debugging tools' reports From: Alexander Potapenko To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org Cc: andreyknvl@google.com, dvyukov@google.com, mingo@redhat.com, elver@google.com, pmladek@suse.com, rostedt@goodmis.org, sergey.senozhatsky@gmail.com, glider@google.com, linux-mm@kvack.org 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: With the introduction of various production error-detection tools, such as MTE-based KASAN and KFENCE, the need arises to efficiently notify the userspace OS components about kernel errors. Currently, no facility exists to notify userspace about a kernel error from such bug-detection tools. The problem is obviously not restricted to the above bug detection tools, and applies to any error reporting mechanism that does not panic the kernel; this series, however, will only add support for KASAN and KFENCE reporting. All such error reports appear in the kernel log. But, when such errors occur, userspace would normally need to read the entire kernel log and parse the relevant errors. This is error prone and inefficient, as userspace needs to continuously monitor the kernel log for error messages. On certain devices, this is unfortunately not acceptable. Therefore, we need to revisit how reports are propagated to userspace. The library added, error_report_notify (CONFIG_ERROR_REPORT_NOTIFY), solves the above by using the error_report_start/error_report_end tracing events and exposing the last report and the total report count to the userspace via /sys/kernel/error_report/last_report and /sys/kernel/error_report/report_count. Userspace apps can call poll(POLLPRI) on those files to get notified about the new reports without having to watch dmesg in a loop. Suggested-by: Marco Elver Cc: Andrew Morton Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Ingo Molnar Cc: Marco Elver Cc: Petr Mladek Cc: Steven Rostedt Cc: Sergey Senozhatsky Cc: linux-mm@kvack.org Signed-off-by: Alexander Potapenko --- v2: - s/memory error reports/error reports from debugging tools/ (per Andrew Morton's comment) - change error_report_start and error_report_end prototypes to accept enum error_detector instead of char* (as suggested by Steven Rostedt) --- lib/Kconfig.debug | 14 ++ lib/Makefile | 2 + lib/error_report_notify.c | 278 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 lib/error_report_notify.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 4b8f9e018f0f..b950bf21b15c 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -209,6 +209,20 @@ config DEBUG_BUGVERBOSE of the BUG call as well as the EIP and oops trace. This aids debugging but costs about 70-100K of memory. +config ERROR_REPORT_NOTIFY + bool "Expose error reports from debugging tools to the userspace" + depends on TRACING + help + When enabled, captures error reports from debugging tools (such as + KFENCE or KASAN) using console tracing, and exposes reports in + /sys/kernel/error_report/: the file last_report contains the last + report (with maximum report length of PAGE_SIZE), and report_count, + the total report count. + + Userspace programs can call poll(POLLPRI) on those files to get + notified about the new reports without having to watch dmesg in a + loop. + endmenu # "printk and dmesg options" menu "Compile-time checks and compiler options" diff --git a/lib/Makefile b/lib/Makefile index afeff05fa8c5..96ed8bc0cc5b 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -240,6 +240,8 @@ obj-$(CONFIG_CPU_RMAP) += cpu_rmap.o obj-$(CONFIG_DQL) += dynamic_queue_limits.o +obj-$(CONFIG_ERROR_REPORT_NOTIFY) += error_report_notify.o + obj-$(CONFIG_GLOB) += glob.o obj-$(CONFIG_GLOB_SELFTEST) += globtest.o diff --git a/lib/error_report_notify.c b/lib/error_report_notify.c new file mode 100644 index 000000000000..66176cd94ba0 --- /dev/null +++ b/lib/error_report_notify.c @@ -0,0 +1,278 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Userspace notification interface for debugging tools. + * + * Provide two sysfs files: + * - /sys/kernel/error_report/last_report + * - /sys/kernel/error_report/report_count + * that contain the last debugging tool report (taken from dmesg, delimited by + * the error_report_start/error_report_end tracing events) and the total report + * count. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct kobject *error_report_kobj; + +/* sysfs files are capped at PAGE_SIZE. */ +#define BUF_SIZE PAGE_SIZE +/* Two buffers to store the finished report and the report being recorded. */ +static char report_buffer[2][BUF_SIZE]; +/* + * Total report count. Also serves as a latch for report_buffer: + * report_buffer[num_reports % 2] is the currently available report, + * report_buffer[(num_reports + 1) % 2] is the report being recorded. + */ +static atomic_t num_reports; + +/* + * PID of the task currently recording the report, as returned by + * get_encoded_pid(), or -1. Used as a writer lock for report_buffer. + * A regular spinlock couldn't be used here, as probe_console() can be called + * from any thread, and it needs to know whether that thread is holding the + * lock. + */ +static atomic_t current_pid = ATOMIC_INIT(-1); + +static size_t current_pos; +static bool truncated; +static const char TRUNC_MSG[] = "\n"; + +static struct delayed_work reporting_done; + +static void error_report_notify(struct work_struct *work) +{ + sysfs_notify(error_report_kobj, NULL, "last_report"); + sysfs_notify(error_report_kobj, NULL, "report_count"); +} +static DECLARE_DELAYED_WORK(reporting_done, error_report_notify); + +/* + * Return the current PID combined together with in_task(). This lets us + * distinguish between normal task context and IRQ context. + */ +static int get_encoded_pid(void) +{ + return (current->pid << 1) | (!!in_task()); +} + +/* + * Trace hook for the error_report_start event. In an unlikely case of another + * task already printing a report bail out, otherwise save the current pid + * together with in_task() return value. + * + * Because reporting code can be called from low-level routines (e.g. locking + * primitives or allocator guts), report recording is implemented using a + * seqlock lock-free algorithm. + */ +static void probe_report_start(void *ignore, enum error_detector detector, + unsigned long id) +{ + /* + * Acquire the writer lock. Any racing probe_report_start will not + * record anything. Pairs with the release in probe_report_end(). + */ + if (atomic_cmpxchg_acquire(¤t_pid, -1, get_encoded_pid()) != -1) + return; + current_pos = 0; + truncated = false; +} + +/* + * Trace hook for the error_report_end event. If an event from the mismatching + * error_report_start is received, it is ignored. Otherwise, null-terminate the + * buffer, increase the report count (effectively releasing the report to + * last_report_show() and schedule a notification about a new report. + */ +static void probe_report_end(void *ignore, enum error_detector detector, + unsigned long id) +{ + pid_t pid = atomic_read(¤t_pid); + int idx; + + if (pid != get_encoded_pid()) + return; + + idx = (atomic_read(&num_reports) + 1) % 2; + if (current_pos == BUF_SIZE) + report_buffer[idx][current_pos - 1] = 0; + else + report_buffer[idx][current_pos] = 0; + + /* Pairs with acquire in last_report_show(). */ + atomic_inc_return_release(&num_reports); + schedule_delayed_work(&reporting_done, 0); + /* + * Release the writer lock. Pairs with the acquire in + * probe_report_start(). + */ + atomic_set_release(¤t_pid, -1); +} + +/* + * Skip one or two leading pair of brackets containing the log timestamp and + * the task/CPU ID, plus the leading space, from the report line, e.g.: + * [ 0.698431][ T7] BUG: KFENCE: use-after-free ... + * becomes: + * BUG: KFENCE: use-after-free ... + * + * Report size is only 4K, and this boilerplate can easily account for half of + * that amount. + */ +static void skip_extra_info(const char **buf, size_t *len) +{ + int num_brackets = IS_ENABLED(CONFIG_PRINTK_TIME) + + IS_ENABLED(CONFIG_PRINTK_CALLER); + const char *found; + + if (!buf || !len) + return; + + while (num_brackets--) { + if (!*len || *buf[0] != '[') + return; + found = strnchr(*buf, *len, ']'); + if (!found) + return; + *len -= found - *buf + 1; + *buf = found + 1; + } + if (*len && *buf[0] == ' ') { + ++*buf; + --*len; + } +} + +/* + * Trace hook for the console event. If a line comes from a task/CPU that did + * not send the error_report_start event, that line is ignored. Otherwise, it + * is stored in the report_buffer[(num_reports + 1) % 2]. + * + * To save space, the leading timestamps and (when enabled) CPU/task info is + * stripped away. The buffer may contain newlines, so this procedure is + * repeated for every line. + */ +static void probe_console(void *ignore, const char *buf, size_t len) +{ + int pid = atomic_read(¤t_pid); + size_t to_copy, cur_len; + char *newline; + int idx; + + if (pid != get_encoded_pid() || truncated) + return; + + idx = (atomic_read(&num_reports) + 1) % 2; + while (len) { + newline = strnchr(buf, len, '\n'); + if (newline) + cur_len = newline - buf + 1; + else + cur_len = len; + /* Adjust len now, because skip_extra_info() may change cur_len. */ + len -= cur_len; + skip_extra_info(&buf, &cur_len); + to_copy = min(cur_len, BUF_SIZE - current_pos); + memcpy(report_buffer[idx] + current_pos, buf, to_copy); + current_pos += to_copy; + if (cur_len > to_copy) { + truncated = true; + memcpy(report_buffer[idx] + current_pos - sizeof(TRUNC_MSG), + TRUNC_MSG, sizeof(TRUNC_MSG)); + break; + } + buf += cur_len; + } +} + +static void register_tracepoints(void) +{ + register_trace_console(probe_console, NULL); + register_trace_error_report_start(probe_report_start, NULL); + register_trace_error_report_end(probe_report_end, NULL); +} + +/* + * read() handler for /sys/kernel/error_report/last_report. + * Because the number of reports can change under our feet, check it again + * after copying the report, and retry if the numbers mismatch. + */ +static ssize_t last_report_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + ssize_t ret; + int index; + + do { + /* Pairs with release in probe_report_end(). */ + index = atomic_read_acquire(&num_reports); + /* + * If index and old_index mismatch, we might be accessing + * report_buffer concurrently with a writer thread. In that + * case the read data will be discarded. + */ + ret = data_race(strscpy(buf, report_buffer[index % 2], BUF_SIZE)); + /* + * Prevent reordering between the memcpy above and the atomic + * read below. + * See the comments in include/linux/seqlock.h for more + * details. + */ + smp_rmb(); + } while (index != atomic_read(&num_reports)); + return ret; +} + +/* + * read() handler for /sys/kernel/error_report/report_count. + */ +static ssize_t report_count_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&num_reports)); +} + +static struct kobj_attribute last_report_attr = __ATTR_RO(last_report); +static struct kobj_attribute report_count_attr = __ATTR_RO(report_count); +static struct attribute *error_report_sysfs_attrs[] = { + &last_report_attr.attr, + &report_count_attr.attr, + NULL, +}; + +static const struct attribute_group error_report_sysfs_attr_group = { + .attrs = error_report_sysfs_attrs, +}; + +/* + * Set up report notification: register tracepoints and create + * /sys/kernel/error_report/. + */ +static void error_report_notify_setup(void) +{ + int err; + + register_tracepoints(); + error_report_kobj = kobject_create_and_add("error_report", kernel_kobj); + if (!error_report_kobj) + goto error; + err = sysfs_create_group(error_report_kobj, + &error_report_sysfs_attr_group); + if (err) + goto error; + return; + +error: + if (error_report_kobj) + kobject_del(error_report_kobj); +} +late_initcall(error_report_notify_setup); From patchwork Fri Jan 15 13:03:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12022693 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD878C433E0 for ; Fri, 15 Jan 2021 13:03:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 742A822473 for ; Fri, 15 Jan 2021 13:03:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 742A822473 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 04E0B8D016A; Fri, 15 Jan 2021 08:03:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F40018D0169; Fri, 15 Jan 2021 08:03:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2F968D016A; Fri, 15 Jan 2021 08:03:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id CAD018D0169 for ; Fri, 15 Jan 2021 08:03:48 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 97FDB1264 for ; Fri, 15 Jan 2021 13:03:48 +0000 (UTC) X-FDA: 77708026536.26.money05_0700d882752f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id 72EFA18012894 for ; Fri, 15 Jan 2021 13:03:48 +0000 (UTC) X-HE-Tag: money05_0700d882752f X-Filterd-Recvd-Size: 5568 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 13:03:47 +0000 (UTC) Received: by mail-wr1-f73.google.com with SMTP id b8so4132533wrv.14 for ; Fri, 15 Jan 2021 05:03:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=jOOfDKQx1+jPnbv33agbvLGtXZ2MbhOFvMWdPLPRW0k=; b=Dlq9uaH+pIB75P7MKmxH+aUEe4eGD10sBNXqDWp/qunB8HR5dcReV4Z2eIviWjc/P6 Vt9jvadWbhJp0D/aZyhiP5wGE9wQUsQOTnCiPaeWNPQtfFPRCz03VlSdmi7ysHZYwIZI 5Xl/6HCkGZ4QSXNEmK3ER8XR4fdzYRouvHBhg/CCMlMVhbhHGQ8OiXqSWNCNhiWDwY44 tgRMA4O3D/h+Kl1w9S9GWC/D0XgVxFyq3CcgG+q4XB7UdyoFR1t7RCDatIifBKIzb2vg m8QhBN3U2fPqoxqC4zcLDc26MpSEyLwLG/ezS0R+ihp/jH0mtIbqNBEKqNPZiYOjtcKL RM8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jOOfDKQx1+jPnbv33agbvLGtXZ2MbhOFvMWdPLPRW0k=; b=niQlfTOPjmoDhEznYZcyUuT0wDMSY0KlIy2nLNu2ipR4vfgEi/1c6mPPwcwdy+kuvS fZlhJU7VhDc6ihZBI5NCxe5NgD0g7RC//DIKhE/qzRVTz8hR+TXS3CjDaXTVWriHlqxK m84s9RjaKvlPgpADPgunlz8hAF59m6hBrKCzX5OPmjT6t2vSMICSMaZY96U1VNMpKwMQ qP4Pase9ZtoRRpau2LdvwaE94brvESEVGckpxLGWqVfdiN5mQK1BHBzXGx2nP3Hs/AT4 d7I0EpPJP2LEb/JCgBQCUzrkHYsfF3YIXee/D4yvw18jCKR/YvLWQ5qtXm2Bms7Y0yHe uBDA== X-Gm-Message-State: AOAM531sfn8Yhe8AZ20mF9vHL0G4w9pPN7Z6k76O2KjvSqmlY/WWUV7A GLIKNlnBseemSF8VPqdd3QkDopqwToY= X-Google-Smtp-Source: ABdhPJxlZI7UXjDHh8f5aiGTii0hBsMm1kPklYbJotEJlDg6aIGyTVeSV+JktC6XgXB8pT4mP8GUISHL+JI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:9ff]) (user=glider job=sendgmr) by 2002:a1c:2c89:: with SMTP id s131mr934933wms.0.1610715826432; Fri, 15 Jan 2021 05:03:46 -0800 (PST) Date: Fri, 15 Jan 2021 14:03:34 +0100 In-Reply-To: <20210115130336.2520663-1-glider@google.com> Message-Id: <20210115130336.2520663-4-glider@google.com> Mime-Version: 1.0 References: <20210115130336.2520663-1-glider@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 3/5] docs: ABI: add /sys/kernel/error_report/ documentation From: Alexander Potapenko To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org Cc: andreyknvl@google.com, dvyukov@google.com, mingo@redhat.com, elver@google.com, pmladek@suse.com, rostedt@goodmis.org, sergey.senozhatsky@gmail.com, glider@google.com, linux-mm@kvack.org 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: Add ABI documentation for files in /sys/kernel/error_report/ Requested-by: Andrew Morton Cc: Andrew Morton Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Ingo Molnar Cc: Marco Elver Cc: Petr Mladek Cc: Steven Rostedt Cc: Sergey Senozhatsky Cc: linux-mm@kvack.org Signed-off-by: Alexander Potapenko --- .../ABI/testing/sysfs-kernel-error_report | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-kernel-error_report diff --git a/Documentation/ABI/testing/sysfs-kernel-error_report b/Documentation/ABI/testing/sysfs-kernel-error_report new file mode 100644 index 000000000000..666d039f93a9 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-kernel-error_report @@ -0,0 +1,41 @@ +What: /sys/kernel/error_report/ +Date: January 2021 +Contact: Alexander Potapenko , + Marco Elver +Description: + /sys/kernel/error_report/ contains two files: "report_count" + and "last_report". These files are used to notify userspace + about error reports from the enrolled kernel subsystems (those + that use error_report_start/error_report_end tracepoints). + + "report_count" contains the current number of reported errors. + This number is incremented every time the error_report_end + trace event occurs in the kernel. + + "last_report" contains the most recent error report; concurrent + report generation results in collection of any one report + ("last_report" may not be the last shown on the console). + A "report" is everything the task had printed to the console + between issuing the error_report_start and error_report_end + trace events. + + Due to sysfs limitations, the report size is truncated at + PAGE_SIZE. To save space, the leading info in square brackets + printed by CONFIG_PRINTK_TIME and CONFIG_PRINTK_CALLER is + trimmed from the output lines. + + Both files use sysfs_notify() to notify userspace about + changes. Userspace programs can use poll() to block until an + error is reported: + + pfd.fd = fd; + pfd.events = POLLPRI; + while (1) { + lseek(pfd.fd, 0, SEEK_SET); + poll(&pfd, 1, -1); + read(pfd.fd, buffer, PAGE_SIZE); + /* Process the report in @buffer. */ + } + + Files in /sys/kernel/error_report/ are available when + CONFIG_ERROR_REPORT_NOTIFY is enabled. From patchwork Fri Jan 15 13:03:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12022695 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D68BC433E0 for ; Fri, 15 Jan 2021 13:03:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 09E2022473 for ; Fri, 15 Jan 2021 13:03:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 09E2022473 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E2EB88D016C; Fri, 15 Jan 2021 08:03:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E09F18D016B; Fri, 15 Jan 2021 08:03:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CCF1F8D016C; Fri, 15 Jan 2021 08:03:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0009.hostedemail.com [216.40.44.9]) by kanga.kvack.org (Postfix) with ESMTP id B3EE08D016B for ; Fri, 15 Jan 2021 08:03:51 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 74E21181AEF1A for ; Fri, 15 Jan 2021 13:03:51 +0000 (UTC) X-FDA: 77708026662.25.girl87_280e9b82752f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin25.hostedemail.com (Postfix) with ESMTP id 0AED11804E3BE for ; Fri, 15 Jan 2021 13:03:50 +0000 (UTC) X-HE-Tag: girl87_280e9b82752f X-Filterd-Recvd-Size: 4751 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 13:03:50 +0000 (UTC) Received: by mail-wr1-f73.google.com with SMTP id w8so4134106wrv.18 for ; Fri, 15 Jan 2021 05:03:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=954/Z+Hv6LNpyU9utqCctaUUru/bs3BWptIPfUnFtmI=; b=pm4+xcjwk+Pr+UXPk5mF0s3bJV0j+KWfPkIsrbpPDRrSGfSXWBhGeKkX90a+jelTbP KobHEq/XLzDeNe8BVOYEBsHJt+9fd4Fe6SXeDxDdH8twkCSYLrd6w5n9kb5cNWd2An61 tGxF3QVINmmir4xsF/oOxOBZHJt6pgFg/Y/5XUxgAvDAH27gJb8dnxRW1eXv22rxVshe yiK42IHcUltxQuoaL3rkds8EwUjK149viM/GwhS7MDBL2Yv5nMWcUrRmeKkK/x6mUTSs TWyfFxXakDmtc0W2MI8DMYz5U3IuCtGiId61uFrkRxKcC/N8C8JsJAfp5Gp28rVv312K TOTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=954/Z+Hv6LNpyU9utqCctaUUru/bs3BWptIPfUnFtmI=; b=fLYhYWb+FKLHUSEDtg6871ICOEgrTFIy2OMhhkTcyfK5CkR7fUrVaIoK5UT3fJQQxI JQYxVPP+R2ZfoFqVSDSiuRqjMAadp8U8uW1GG82ju8Mo983QgZ90BBx28TJsdqnLzhoA iEtiBpS6rPK4gEuBcXgeDBZGEZ/epp6Lvj9ueU7MOx0KT2OcT+RtujZnts4lbGS4jSbB 8CqBsNdQp+1SY/a1PbdrTfS/e6yrYGzM9vfppnB4NJ0Qmnc9ZBKxs6rGsp0VFksMKrlf yG35r2punyMVU4wMuZUJSXl2w6fBvfLnd0FkY4b58bbBd8LKqoHLy+au/jg54VVM1zw5 bhLg== X-Gm-Message-State: AOAM533BeguqMgGmEccdsAXV5HTuAfYMgvy3suqf0EEla7/QxqorYS5R dMDwrXo83Oejzvq3W4VrwaPeo3E3wzY= X-Google-Smtp-Source: ABdhPJxtwFBxiD7OEYrh/qyl7LW2IZQ1kWN66X1jJsjmKqpZeYgm33eaNA3UEIbQL3cBZ7Drz1VxX8M5jug= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:9ff]) (user=glider job=sendgmr) by 2002:a1c:2c89:: with SMTP id s131mr934969wms.0.1610715829142; Fri, 15 Jan 2021 05:03:49 -0800 (PST) Date: Fri, 15 Jan 2021 14:03:35 +0100 In-Reply-To: <20210115130336.2520663-1-glider@google.com> Message-Id: <20210115130336.2520663-5-glider@google.com> Mime-Version: 1.0 References: <20210115130336.2520663-1-glider@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 4/5] kfence: use error_report_start and error_report_end tracepoints From: Alexander Potapenko To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org Cc: andreyknvl@google.com, dvyukov@google.com, mingo@redhat.com, elver@google.com, pmladek@suse.com, rostedt@goodmis.org, sergey.senozhatsky@gmail.com, glider@google.com, linux-mm@kvack.org 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: Make it possible to trace KFENCE error reporting (in particular, for compatibility with CONFIG_ERROR_REPORT_NOTIFY). Suggested-by: Marco Elver Cc: Andrew Morton Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Ingo Molnar Cc: Marco Elver Cc: Petr Mladek Cc: Steven Rostedt Cc: Sergey Senozhatsky Cc: linux-mm@kvack.org Signed-off-by: Alexander Potapenko --- v2: - change error_report_start and error_report_end prototypes to accept enum error_detector instead of char* (as suggested by Steven Rostedt) --- mm/kfence/report.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/kfence/report.c b/mm/kfence/report.c index 4dedc2ff8f28..bbe7dad5f249 100644 --- a/mm/kfence/report.c +++ b/mm/kfence/report.c @@ -9,6 +9,7 @@ #include #include #include +#include #include @@ -184,6 +185,7 @@ void kfence_report_error(unsigned long address, struct pt_regs *regs, lockdep_off(); pr_err("==================================================================\n"); + trace_error_report_start(ERROR_DETECTOR_KFENCE, address); /* Print report header. */ switch (type) { case KFENCE_ERROR_OOB: { @@ -232,6 +234,7 @@ void kfence_report_error(unsigned long address, struct pt_regs *regs, show_regs(regs); else dump_stack_print_info(KERN_ERR); + trace_error_report_end(ERROR_DETECTOR_KFENCE, address); pr_err("==================================================================\n"); lockdep_on(); From patchwork Fri Jan 15 13:03:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12022697 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 858FFC433E0 for ; Fri, 15 Jan 2021 13:03:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 19CE022473 for ; Fri, 15 Jan 2021 13:03:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 19CE022473 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 52EB88D016D; Fri, 15 Jan 2021 08:03:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 506A98D016B; Fri, 15 Jan 2021 08:03:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F5688D016D; Fri, 15 Jan 2021 08:03:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0058.hostedemail.com [216.40.44.58]) by kanga.kvack.org (Postfix) with ESMTP id 2A94E8D016B for ; Fri, 15 Jan 2021 08:03:53 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id DBA962C1F for ; Fri, 15 Jan 2021 13:03:52 +0000 (UTC) X-FDA: 77708026704.28.order25_3c16afe2752f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id AEB376C36 for ; Fri, 15 Jan 2021 13:03:52 +0000 (UTC) X-HE-Tag: order25_3c16afe2752f X-Filterd-Recvd-Size: 6234 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Fri, 15 Jan 2021 13:03:52 +0000 (UTC) Received: by mail-qv1-f74.google.com with SMTP id cc1so7588928qvb.3 for ; Fri, 15 Jan 2021 05:03:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=hLav+6cEwiPVPNQ10H8mUnfXDXSTPqRFsNjQotE+jzA=; b=Sh2HR2ssv22qj/Ox0Y413CCPV307dbT5KXC2u/9JMQT47kKYkHhqe+TBznrMD4j545 zwfZJu/moFJjgslO5+Z7LAOidJv6WifGvAYC/7O4n9t6th4ICiC0SvHHnnCfnPF8gY+0 0/vyBQtKyVn2YEGQ2MU95deZJIvVOgVPz5W0KVc8OCqTyv3V5UOECCjA2LYhdyxphVg8 7PsWmj//xJCAdfcM/cpfBMzQD/jP4Ondyz4a5K1SXb9h1Trm/kOFV2OmmXzkmbq7ACwV 27N3pJtJHvUV/aSDWZqoQMgMRQJdenMHlImacDTD1pXymW4TyqHp9jgfrUibN88WBRKv 2F6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=hLav+6cEwiPVPNQ10H8mUnfXDXSTPqRFsNjQotE+jzA=; b=NOEOcEp+1EDDAWbQ+Y5VDjsGLCp8QbqmChFdANJ7HvvZ9wAoIlQD+byAGsfNeiPiq4 FmRI5oFu8ZuWMqUeZ34Rjqb45Q0x3n04ZP8ZAwFbNZVFPsQjYzXBEpdhE+8TqQ1FWxTo LJN1Qlc5w2PipuBRtnZMvKgiAV4zyyuKVOAPl18AYeK4+Z4f60dQW+yCGUh8lCpQ/NpO rsii86zWfcakrwpC7Cx5LY4f2TDJTdUuAYhJGQxMjeRmBONyeBCGLhNPeOgB3Y6v4Za8 bxGPKwTibcJXLk7jXz630zd9HKq07zvHr+k13SneB/ofJnqs1g5bInwYwsrim15Ee/GF AWjQ== X-Gm-Message-State: AOAM532hpIXHiLe+2VSppyGsk1QzBvKbNnXls5KOo07N5LvmnlSW4MfN YgIMpzjECyknsYn5YL4MER6as5dyIc4= X-Google-Smtp-Source: ABdhPJwcxbW4F0oXAeIpK+OvuTtdxUQ7SsZtruNCXPi/u8cr2SIDC13l6xM4zNss2rTVBUqx4VJstHIKwJ8= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:9ff]) (user=glider job=sendgmr) by 2002:a05:6214:140d:: with SMTP id n13mr11772979qvx.45.1610715831400; Fri, 15 Jan 2021 05:03:51 -0800 (PST) Date: Fri, 15 Jan 2021 14:03:36 +0100 In-Reply-To: <20210115130336.2520663-1-glider@google.com> Message-Id: <20210115130336.2520663-6-glider@google.com> Mime-Version: 1.0 References: <20210115130336.2520663-1-glider@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 5/5] kasan: use error_report_start and error_report_end tracepoints From: Alexander Potapenko To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org Cc: andreyknvl@google.com, dvyukov@google.com, mingo@redhat.com, elver@google.com, pmladek@suse.com, rostedt@goodmis.org, sergey.senozhatsky@gmail.com, glider@google.com, linux-mm@kvack.org 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: Make it possible to trace KASAN error reporting (in particular, for compatibility with CONFIG_ERROR_REPORT_NOTIFY). Suggested-by: Marco Elver Cc: Andrew Morton Cc: Andrey Konovalov Cc: Dmitry Vyukov Cc: Ingo Molnar Cc: Marco Elver Cc: Petr Mladek Cc: Steven Rostedt Cc: Sergey Senozhatsky Cc: linux-mm@kvack.org Signed-off-by: Alexander Potapenko --- v2: - change error_report_start and error_report_end prototypes to accept enum error_detector instead of char* (as suggested by Steven Rostedt) --- mm/kasan/report.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mm/kasan/report.c b/mm/kasan/report.c index c0fb21797550..78b8bbc84dc2 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -74,7 +75,7 @@ static void print_error_description(struct kasan_access_info *info) static DEFINE_SPINLOCK(report_lock); -static void start_report(unsigned long *flags) +static void start_report(unsigned long *flags, unsigned long addr) { /* * Make sure we don't end up in loop. @@ -82,10 +83,12 @@ static void start_report(unsigned long *flags) kasan_disable_current(); spin_lock_irqsave(&report_lock, *flags); pr_err("==================================================================\n"); + trace_error_report_start(ERROR_DETECTOR_KASAN, addr); } -static void end_report(unsigned long *flags) +static void end_report(unsigned long *flags, unsigned long addr) { + trace_error_report_end(ERROR_DETECTOR_KASAN, addr); pr_err("==================================================================\n"); add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); spin_unlock_irqrestore(&report_lock, *flags); @@ -348,14 +351,14 @@ void kasan_report_invalid_free(void *object, unsigned long ip) kasan_update_kunit_status(current->kunit_test); #endif /* IS_ENABLED(CONFIG_KUNIT) */ - start_report(&flags); + start_report(&flags, (unsigned long)object); pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); print_tags(tag, object); pr_err("\n"); print_address_description(object, tag); pr_err("\n"); print_memory_metadata(object); - end_report(&flags); + end_report(&flags, (unsigned long)object); } static void __kasan_report(unsigned long addr, size_t size, bool is_write, @@ -385,7 +388,7 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write, info.is_write = is_write; info.ip = ip; - start_report(&flags); + start_report(&flags, addr); print_error_description(&info); if (addr_has_metadata(untagged_addr)) @@ -400,7 +403,7 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write, dump_stack(); } - end_report(&flags); + end_report(&flags, addr); } bool kasan_report(unsigned long addr, size_t size, bool is_write,