Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4653405rdb; Tue, 12 Dec 2023 06:00:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IGmuVtD4Jn97h28Kh1jErS/720XQ4mFhO5Jgc3Vwu/1euLhtIWuLajm05JHLZ/EJA8PzQh3 X-Received: by 2002:a05:6a21:a5a0:b0:190:230:8ea1 with SMTP id gd32-20020a056a21a5a000b0019002308ea1mr8314228pzc.108.1702389634754; Tue, 12 Dec 2023 06:00:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702389634; cv=none; d=google.com; s=arc-20160816; b=OMah5d/OHLgA9EK8y9qIMOIOYACvar+yCp2XF8Br0mo1kMQkiSzm2CUYABJ21Za8RL 8F6+GNzFbpZSe1QgOSQzmDSQo8d5gnIpfq6gpAJCtLmAkmyIvlYXoJpO+jfmbzw4t0Jf +RfY2DFyrkFdJE1SBtBwIHNW4Q6n3gDiY80IaI++2V2xojoKfOQIdKmpOo7rJKU73W67 KJe9Fu+nrUhOxLrzm5iTMPSHAGcAE0cKniA7hUlt6Dhrfjj9v8KLZAk0eBCz2FMM2sAy AIxQLBP3cRQlQ+OGNXiXQqOJhBFf4y06MZSM3wySEjTXWJ9M55+J//rRHkExTi4J6Pd5 Pzxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:subject:cc:to:from:date; bh=H09Edjcl5RENJHgxsUVe2prnyH4gJ7FdhPXTASnkn3E=; fh=R8JZsVKDlS7lXIAvTNV55bsjlDBI0i4sqiOYXO4tZQA=; b=sHNiXIzr23u4Ulz92dSE9uv0kV8bS9WvwULkEBnBLe48lVY9KA7SSQECzgQwoWoWTl XqZBaVOG0YB0Ksk6RpUVbBn6DjAX4sYS47I+jQPE3N5Rqr7/ctuj717CwMMHgHJTyxgg 1kwpTCix3HvScX2RJ7QzQGuqAnwRccRHzBr2nXAV4ob6RxoJ9VaHX4uDgpzrzxDp2I89 JqY1gjbwBEPuoYn6t3X1l+X/C/oplvwOb7idxucc6e6B8tk7P32jjTUxLm6Jv8xtY0Rd /CZzLHhtjYwc6k5hMlCGszZgNaEGfplsxE5DnG9MXztSRvpSSPKzSPVDKNOdJXSZDgq8 KHKg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id a1-20020a654181000000b005c14fc66cc1si7642397pgq.22.2023.12.12.06.00.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 06:00:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 193538082862; Tue, 12 Dec 2023 06:00:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376357AbjLLOAL (ORCPT + 99 others); Tue, 12 Dec 2023 09:00:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376341AbjLLOAK (ORCPT ); Tue, 12 Dec 2023 09:00:10 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5FF9B3 for ; Tue, 12 Dec 2023 06:00:16 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95CF0C433C8; Tue, 12 Dec 2023 14:00:15 +0000 (UTC) Date: Tue, 12 Dec 2023 09:00:57 -0500 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers Subject: [PATCH v2] tracing: Allow for max buffer data size trace_marker writes Message-ID: <20231212090057.41b28efe@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 06:00:28 -0800 (PST) From: "Steven Rostedt (Google)" Allow a trace write to be as big as the ring buffer tracing data will allow. Currently, it only allows writes of 1KB in size, but there's no reason that it cannot allow what the ring buffer can hold. Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Signed-off-by: Steven Rostedt (Google) --- Changes since v1: https://lore.kernel.org/linux-trace-kernel/20231209175003.63db40ab@gandalf.local.home - Now that there's a new fix for the max event size, there's no more BUF_MAX_EVENT_SIZE macro. Now the BUF_MAX_DATA_SIZE can be used again. - Check if the buffer itself is requesting forced timestamps, and if so, decrement from the max size, the timestamp size. - This no longer depends on the previous fix change, as it's now using existing macros. include/linux/ring_buffer.h | 1 + kernel/trace/ring_buffer.c | 15 +++++++++++++++ kernel/trace/trace.c | 28 +++++++++++++++++++++------- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h index 782e14f62201..b1b03b2c0f08 100644 --- a/include/linux/ring_buffer.h +++ b/include/linux/ring_buffer.h @@ -141,6 +141,7 @@ int ring_buffer_iter_empty(struct ring_buffer_iter *iter); bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter); unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu); +unsigned long ring_buffer_max_event_size(struct trace_buffer *buffer); void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu); void ring_buffer_reset_online_cpus(struct trace_buffer *buffer); diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index a3eaa052f4de..882aab2bede3 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -5250,6 +5250,21 @@ unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu) } EXPORT_SYMBOL_GPL(ring_buffer_size); +/** + * ring_buffer_max_event_size - return the max data size of an event + * @buffer: The ring buffer. + * + * Returns the maximum size an event can be. + */ +unsigned long ring_buffer_max_event_size(struct trace_buffer *buffer) +{ + /* If abs timestamp is requested, events have a timestamp too */ + if (ring_buffer_time_stamp_abs(buffer)) + return BUF_MAX_DATA_SIZE - RB_LEN_TIME_EXTEND; + return BUF_MAX_DATA_SIZE; +} +EXPORT_SYMBOL_GPL(ring_buffer_max_event_size); + static void rb_clear_buffer_page(struct buffer_page *page) { local_set(&page->write, 0); diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index ef86379555e4..bd6d28dad05d 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -7272,6 +7272,7 @@ tracing_mark_write(struct file *filp, const char __user *ubuf, enum event_trigger_type tt = ETT_NONE; struct trace_buffer *buffer; struct print_entry *entry; + int meta_size; ssize_t written; int size; int len; @@ -7286,12 +7287,9 @@ tracing_mark_write(struct file *filp, const char __user *ubuf, if (!(tr->trace_flags & TRACE_ITER_MARKERS)) return -EINVAL; - if (cnt > TRACE_BUF_SIZE) - cnt = TRACE_BUF_SIZE; - - BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); - - size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */ + meta_size = sizeof(*entry) + 2; /* add '\0' and possible '\n' */ + again: + size = cnt + meta_size; /* If less than "", then make sure we can still add that */ if (cnt < FAULTED_SIZE) @@ -7300,9 +7298,25 @@ tracing_mark_write(struct file *filp, const char __user *ubuf, buffer = tr->array_buffer.buffer; event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, tracing_gen_ctx()); - if (unlikely(!event)) + if (unlikely(!event)) { + /* + * If the size was greated than what was allowed, then + * make it smaller and try again. + */ + if (size > ring_buffer_max_event_size(buffer)) { + /* cnt < FAULTED size should never be bigger than max */ + if (WARN_ON_ONCE(cnt < FAULTED_SIZE)) + return -EBADF; + cnt = ring_buffer_max_event_size(buffer) - meta_size; + /* The above should only happen once */ + if (WARN_ON_ONCE(cnt + meta_size == size)) + return -EBADF; + goto again; + } + /* Ring buffer disabled, return as if not open for write */ return -EBADF; + } entry = ring_buffer_event_data(event); entry->ip = _THIS_IP_; -- 2.42.0