From patchwork Mon Dec 18 15:32:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 180489 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1326155dyi; Mon, 18 Dec 2023 07:36:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IENx5wPGswH8/aQngplLP8Kj3miMWfCR4maydZci5l2O8/5Cjqkh6rLbK9HcL8sE4tQVY/d X-Received: by 2002:a50:d683:0:b0:553:6848:7da6 with SMTP id r3-20020a50d683000000b0055368487da6mr794806edi.45.1702913769077; Mon, 18 Dec 2023 07:36:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702913769; cv=none; d=google.com; s=arc-20160816; b=cyg6su4fKq1psrWQIBnZzPejaKw5aRn/hliZr59QVArnaUYxoXxuxDLjPE3ZT7W7C5 utZJ88tH4vlCr7rrdDurNnnRGRKt87yX+TVZQmbGmJhoggPEj5PACNzw/dSm7LJN5f2M jfMI91oGGp+2i5KMGTnoWlQ0nJCxfrOTBLFNu+6FrgmkHtYkLMf73dCJpSKm0/CYxhAV ttGhMmn793LvQo7EKP+MQfC27n90zCvXv7IyxXwrRviVT8eMj7OI/Hmr1R7PnBUvwpuG ewQ9fJDMDzjyp4CNsuOLHI2UpYhl9+LgdRUaiWbQPeVzaz7S/xpa9CPMYeilZC/pkinr JfiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:subject:cc:to:from :date; bh=4JmazpsJNqg8iKdWosqiNNsdSssGVVd5v+Oea/RzPco=; fh=R8JZsVKDlS7lXIAvTNV55bsjlDBI0i4sqiOYXO4tZQA=; b=eb6cmDVkFcYPHsmczObYoI6E66d/tg7YM6lV746s/FRcV4S4PdC13KGt59joaT6Us6 34jKSZT4RFcrEVL8fk1URHhaZYQqpJsryKmLYeWBPyf2yW+T0Zr8wpv8nFRarwsjCrk/ 35vY37OcF9iEh77GfPDUbUIrNCVokoyRFdw3J6iiWJ0l+Hd43Yc4ER/Rq7xIUUCc8sPb a6tG74Jff5hI+DGp8mUROoYQINiBQVSjZkEN7cOq1KeTmzHY1gexv/AESR/Pg+FOWfQP Yo46uyUBtiVbkG6HsrF+emVRvZhZasz7wN0Ohy/+omKHJNvHs1IqsFGS/oHTLWYISPJA YhEQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-3934-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3934-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id dz12-20020a0564021d4c00b0054c943a07bbsi10144702edb.246.2023.12.18.07.36.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 07:36:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3934-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-3934-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3934-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 803651F25110 for ; Mon, 18 Dec 2023 15:32:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B0F8842372; Mon, 18 Dec 2023 15:32:05 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2883B1D137; Mon, 18 Dec 2023 15:32:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 08CBCC433C7; Mon, 18 Dec 2023 15:32:02 +0000 (UTC) Date: Mon, 18 Dec 2023 10:32:59 -0500 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers Subject: [PATCH v3] tracing: Add filter-direct option Message-ID: <20231218103259.75f1ac6a@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785634508246164302 X-GMAIL-MSGID: 1785634508246164302 From: "Steven Rostedt (Google)" Normally, when the filter is enabled, a temporary buffer is created to copy the event data into it to perform the filtering logic. If the filter passes and the event should be recorded, then the event is copied from the temporary buffer into the ring buffer. If the event is to be discarded then it is simply dropped. If another event comes in via an interrupt, it will not use the temporary buffer as it is busy and will write directly into the ring buffer. The filter-direct option will allow the user to disable this feature. By default, it is disabled. When enabled, it disables the temporary buffer and always writes into the ring buffer. This will avoid the copy when the event is to be recorded, but also adds a bit more overhead on the discard, and if another event were to interrupt the event that is to be discarded, then the event will not be removed from the ring buffer but instead converted to padding that will not be read by the reader. Padding will still take up space on the ring buffer. This option is mainly used for kselftests to stress test the ring buffer discard logic. Also fix some whitespace (that was fixed by editing this in vscode). Signed-off-by: Steven Rostedt (Google) --- Changes since v2: https://lore.kernel.org/linux-trace-kernel/20231215132502.1ae9c1a6@rorschach.local.home - Changed the option name to filter-direct - Added the config option CONFIG_TRACE_FILTER_DIRECT - Moved the documentation from Documentation and into tho Kconfig This is because the option is focused on being used for kselftest purposes kernel/trace/Kconfig | 28 ++++++++++++++++++++++++++++ kernel/trace/trace.c | 39 ++++++++++++++++++++++++--------------- kernel/trace/trace.h | 8 ++++++++ 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 61c541c36596..5362206e6e82 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -949,6 +949,34 @@ config TRACE_EVAL_MAP_FILE If unsure, say N. +config TRACE_FILTER_DIRECT + bool "Add filter-direct option to test direct ring buffer filtering" + help + Normally, when the filter is enabled, a temporary buffer is + created to copy the event data into it to perform the + filtering logic. If the filter passes and the event should + be recorded, then the event is copied from the temporary + buffer into the ring buffer. If the event is to be discarded + then it is simply dropped. If another event comes in via + an interrupt, it will not use the temporary buffer as it is + busy and will write directly into the ring buffer. + + Enabling this config will create a trace option "filter-direct", + that, when enabled, will disable the temporary buffer and always + write directly into the ring buffer. This will avoid the copy when + the event is to be recorded, but also adds a bit more + overhead on the discard, and if another event were to interrupt + the event that is to be discarded, then the event will not + be removed from the ring buffer but instead converted to + padding that will not be read by the reader. Padding will + still take up space on the ring buffer. + + This option is to allow kselftest to test the ring buffer filter + direct functionality that sometimes gets performed. This option + degrades the performance of trace filtering. + + Unless you are running kselftests, say N + config FTRACE_RECORD_RECURSION bool "Record functions that recurse in function tracing" depends on FUNCTION_TRACER diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 55dabee4c78b..36df0364cb9e 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -5398,6 +5398,8 @@ int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set) return 0; } +static int __tracing_set_filter_buffering(struct trace_array *tr, bool set); + int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) { int *map; @@ -5451,6 +5453,11 @@ int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) if (mask == TRACE_ITER_FUNC_FORK) ftrace_pid_follow_fork(tr, enabled); +#ifdef CONFIG_TRACE_FILTER_DIRECT + if (mask == TRACE_ITER_FILTER_DIRECT) + __tracing_set_filter_buffering(tr, enabled); +#endif + if (mask == TRACE_ITER_OVERWRITE) { ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled); #ifdef CONFIG_TRACER_MAX_TRACE @@ -6464,7 +6471,7 @@ static void tracing_set_nop(struct trace_array *tr) { if (tr->current_trace == &nop_trace) return; - + tr->current_trace->enabled--; if (tr->current_trace->reset) @@ -7552,27 +7559,29 @@ u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_eve return ring_buffer_event_time_stamp(buffer, rbe); } -/* - * Set or disable using the per CPU trace_buffer_event when possible. - */ -int tracing_set_filter_buffering(struct trace_array *tr, bool set) +static int __tracing_set_filter_buffering(struct trace_array *tr, bool set) { - int ret = 0; - - mutex_lock(&trace_types_lock); - if (set && tr->no_filter_buffering_ref++) - goto out; + return 0; if (!set) { - if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) { - ret = -EINVAL; - goto out; - } + if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) + return -EINVAL; --tr->no_filter_buffering_ref; } - out: + return 0; +} + +/* + * Set or disable using the per CPU trace_buffer_event when possible. + */ +int tracing_set_filter_buffering(struct trace_array *tr, bool set) +{ + int ret; + + mutex_lock(&trace_types_lock); + ret = __tracing_set_filter_buffering(tr, set); mutex_unlock(&trace_types_lock); return ret; diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 79180aed13ee..53ab9ef847e9 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1218,6 +1218,13 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf, # define STACK_FLAGS #endif +#ifdef CONFIG_TRACE_FILTER_DIRECT +# define DIRECT_FILTER \ + C(FILTER_DIRECT, "filter-direct"), +#else +# define DIRECT_FILTER +#endif + /* * trace_iterator_flags is an enumeration that defines bit * positions into trace_flags that controls the output. @@ -1254,6 +1261,7 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf, FUNCTION_FLAGS \ FGRAPH_FLAGS \ STACK_FLAGS \ + DIRECT_FILTER \ BRANCH_FLAGS /*