From patchwork Fri Dec 15 18:25:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 179510 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9486446dys; Fri, 15 Dec 2023 10:25:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IEc1uP8x8p2EM8+pyHoZ9IiDqV9N8cv4yHKAK43VRIMRjujCO0YXSKHJ3GN8ErletLSDrjD X-Received: by 2002:a17:906:55:b0:a1b:7700:2c0b with SMTP id 21-20020a170906005500b00a1b77002c0bmr13460240ejg.19.1702664724347; Fri, 15 Dec 2023 10:25:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702664724; cv=none; d=google.com; s=arc-20160816; b=xDiFE3WefscXjEqd6in74a+/w18UMW965aGzuAKtZRgVkCqXjxD6Y2TL6hy58Tn0J8 4XdcASRGQA7AnZaJFdURTJHcHQoxr/evcILuNVF7uYRxtsM4QDoUDb8t1+vVRoK82pM6 dBabG9lN36cXswDR6jVH6sJ/Xd01v1kTc1fM9mIWIcKnrqDOqMh4nbWc6Pv938Wib6eQ QoZbiN1GikddepmM9OOvvxZbokamNtRjJNaiuQSGu23r4ocDypyfs1h4XT4NzGrvBEKC JsD0sIapltRqkzsZceXIfFKVlwiGagAwXc7ffw8OIxBxwxuwTwuV92k+KJMecpgYA7q5 vm6Q== 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=qLtwRvLgtLk4BI/AxzZ9P9We6reLZLR6fHi3ar9xqZE=; fh=10neyeg9SpQc0WdV4aMamIFr7tikcy7JJ0bptR+ICfw=; b=hlktGcLx1pKfRBArWJikaOlpl1cKRfW5mBLvwZiySHUyOqN4oigyVhHz8s4un8224I qoCSKo4c1Ae5knd42MO5j1ISMcDOl0H2uJmlZY4OjGl05l1441qJgqMU6Y4UsUgmx2Mh LZDOEKNjT3+yv/OhamWshgRiuKf1kdyNfCap9gXI6l/Q7rut2NsyMmlMPwKcNHF8Bw+c sXkzd4vzqslJvTK2TyyEEESfCsES3F+TElRakEpRTnYC0xacIj7HINipJY2KTBy8qGII 6EeNR6VD1tITRWa5/PiV33XCLoFnUIaULIuMYOGJV7HgaLGguQ2tFd7xeCbvfe9qlyf9 QsDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-1523-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1523-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t15-20020a1709063e4f00b00a230ff037e5si1674371eji.748.2023.12.15.10.25.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 10:25:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1523-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-1523-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1523-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 C8FB61F22F62 for ; Fri, 15 Dec 2023 18:25:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 71DCF3EA92; Fri, 15 Dec 2023 18:25:06 +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 ED46E41846; Fri, 15 Dec 2023 18:25:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0040AC433C7; Fri, 15 Dec 2023 18:25:03 +0000 (UTC) Date: Fri, 15 Dec 2023 13:25:02 -0500 From: Steven Rostedt To: LKML , Linux trace kernel Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers Subject: [PATCH v2] tracing: Add filter-buffer option Message-ID: <20231215132502.1ae9c1a6@rorschach.local.home> X-Mailer: Claws Mail 3.17.8 (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: 1785373365832108009 X-GMAIL-MSGID: 1785373365832108009 From 62a1de0f0f9d942934565e625a7880fd85ae216a Mon Sep 17 00:00:00 2001 From: "Steven Rostedt (Google)" Date: Fri, 15 Dec 2023 10:26:33 -0500 Subject: [PATCH] tracing: Add filter-buffer option 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-buffer option will allow the user to disable this feature. By default, it is enabled. When disabled, 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 can be beneficial if most events are recorded and not discarded, or simply for debugging the discard functionality of the ring buffer. Also fix some whitespace (that was fixed by editing this in vscode). Signed-off-by: Steven Rostedt (Google) --- Changes since v1: https://lore.kernel.org/linux-trace-kernel/20231215102633.7a24cb77@rorschach.local.home - Renamed "disable-filter-buffer" to "filter-buffer" and made it default enabled, where the user needs to disable it. (Mathieu Desnoyers) Documentation/trace/ftrace.rst | 23 ++++++++++++++++++++ kernel/trace/trace.c | 39 ++++++++++++++++++++-------------- kernel/trace/trace.h | 1 + 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Documentation/trace/ftrace.rst b/Documentation/trace/ftrace.rst index 23572f6697c0..7ec26eb814e9 100644 --- a/Documentation/trace/ftrace.rst +++ b/Documentation/trace/ftrace.rst @@ -1239,6 +1239,29 @@ Here are the available options: When the free_buffer is closed, tracing will stop (tracing_on set to 0). + filter-buffer + 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. + + This option, when cleared, will disable the temporary buffer and always + write 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 can be beneficial if most events are recorded and + not discarded, or simply for debugging the discard functionality + of the ring buffer. + irq-info Shows the interrupt, preempt count, need resched data. When disabled, the trace looks like:: diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 55dabee4c78b..e18c83104e24 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -466,7 +466,7 @@ EXPORT_SYMBOL_GPL(unregister_ftrace_export); TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \ TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \ TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | \ - TRACE_ITER_HASH_PTR) + TRACE_ITER_HASH_PTR | TRACE_ITER_FILTER_BUF) /* trace_options that are only supported by global_trace */ #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \ @@ -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,9 @@ 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); + if (mask == TRACE_ITER_FILTER_BUF) + __tracing_set_filter_buffering(tr, !enabled); + if (mask == TRACE_ITER_OVERWRITE) { ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled); #ifdef CONFIG_TRACER_MAX_TRACE @@ -6464,7 +6469,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 +7557,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..f82dce7ea3ff 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1251,6 +1251,7 @@ extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf, C(EVENT_FORK, "event-fork"), \ C(PAUSE_ON_TRACE, "pause-on-trace"), \ C(HASH_PTR, "hash-ptr"), /* Print hashed pointer */ \ + C(FILTER_BUF, "filter-buffer"), \ FUNCTION_FLAGS \ FGRAPH_FLAGS \ STACK_FLAGS \