From patchwork Thu Dec 28 14:51:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 183745 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp2038137dyb; Thu, 28 Dec 2023 06:52:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IHGY0feFrHAku3iIVSim8nrs6o8gIzSNlrotNWWDUQ1/j0KBTQwmENNqs+AyGQpSOhxtJPQ X-Received: by 2002:a05:6a20:2593:b0:194:f631:e31a with SMTP id k19-20020a056a20259300b00194f631e31amr13705970pzd.9.1703775158625; Thu, 28 Dec 2023 06:52:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703775158; cv=none; d=google.com; s=arc-20160816; b=UPoq4RAz3Sknrj9JEQapZXbQKowFxKY3tNoNsY+WQi161EuS+DBUjlC1kp7WZ9aQjh JlcjRrYwWpF8tlafAyLztZBZZg2inErKtl6u4nKeQ2zGs+dILNr9v4VhJj9ekbLCZ1oQ sfYITWyJuR4X/iI2ZeeyA8rPA4anC+BO/CuZCB/B8M6sk3titMsLMVe52ElFk8KpxBZJ xZ9sRdg0OwV0lL9RKKZfKyAJSIdk1M9lWx/27xIIEIfSbXl0dS0u40KKiGbLm0f8r849 ijjJ6AYSsxerPvOjESdhX3IF2GNb1jNgIAH8o2yKQTkE/TV6SADhZW/tjv0e+cCPDG5s 7QEg== 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=DaxSbgFXS7RT11CUNHhsNIQy0JrznCUmaKV2fAQ4uLo=; fh=zUYoqPPyjvQ6rCrD+afgiWfy/Dsjd8P+Tjoa0MxCZSY=; b=nAP/rmbIup4XQeFGRLR+yGzb0CiovNXMFIZNlvY3nFr5rPHMlEY4c4j4UzPYsWV3Af osYHmEtdK74Ut2iFk7h6MDJwh0JYBbdAgKxM7hXetq3sniDXcOGQstPBNnwjKoZ4/JV2 u1TxKTdadTcFmxbAOUfAtFDpGI/sSKycheX48T/g0eH4mYAQ4lOeRDEX2GSiyMPijai0 X/yq8d0cuSp0G2RevOGCgfnRP1sxamdmDu2FSeX/o65qAim3yB9PGFBCqhZhLASUZStf JY4Jxf4TYhTQF0IPuNtQYQ98iMCPCjUevjOkqoveKL4BcljDx1MXEM0VgOIUIhVJ6AAn LH3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-12716-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12716-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 127-20020a630185000000b005cdf9a34830si10704483pgb.588.2023.12.28.06.52.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 06:52:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-12716-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-12716-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12716-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 2F21BB23D44 for ; Thu, 28 Dec 2023 14:51:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 69130DDC8; Thu, 28 Dec 2023 14:51:03 +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 F21A0F4E3; Thu, 28 Dec 2023 14:51:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4951C433C7; Thu, 28 Dec 2023 14:51:00 +0000 (UTC) Date: Thu, 28 Dec 2023 09:51:49 -0500 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mathieu Desnoyers , Mark Rutland Subject: [PATCH] tracing: Fix blocked reader of snapshot buffer Message-ID: <20231228095149.77f5b45d@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: 1786537740375802502 X-GMAIL-MSGID: 1786537740375802502 From: "Steven Rostedt (Google)" If an application blocks on the snapshot or snapshot_raw files, expecting to be woken up when a snapshot occurs, it will not happen. Or it may happen with an unexpected result. That result is that the application will be reading the main buffer instead of the snapshot buffer. That is because when the snapshot occurs, the main and snapshot buffers are swapped. But the reader has a descriptor still pointing to the buffer that it originally connected to. This is fine for the main buffer readers, as they may be blocked waiting for a watermark to be hit, and when a snapshot occurs, the data that the main readers want is now on the snapshot buffer. But for waiters of the snapshot buffer, they are waiting for an event to occur that will trigger the snapshot and they can then consume it quickly to save the snapshot before the next snapshot occurs. But to do this, they need to read the new snapshot buffer, not the old one that is now receiving new data. Also, it does not make sense to have a watermark "buffer_percent" on the snapshot buffer, as the snapshot buffer is static and does not receive new data except all at once. Cc: stable@vger.kernel.org Fixes: debdd57f5145f ("tracing: Make a snapshot feature available from userspace") Signed-off-by: Steven Rostedt (Google) Acked-by: Masami Hiramatsu (Google) --- kernel/trace/ring_buffer.c | 3 ++- kernel/trace/trace.c | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index e476d42c84c5..07dae67424a9 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -838,7 +838,8 @@ void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu) /* make sure the waiters see the new index */ smp_wmb(); - rb_wake_up_waiters(&rbwork->work); + /* This can be called in any context */ + irq_work_queue(&rbwork->work); } /** diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index cfeaf2cd204e..606787edae8c 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1902,6 +1902,9 @@ update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu, __update_max_tr(tr, tsk, cpu); arch_spin_unlock(&tr->max_lock); + + /* Any waiters on the old snapshot buffer need to wake up */ + ring_buffer_wake_waiters(tr->array_buffer.buffer, RING_BUFFER_ALL_CPUS); } /** @@ -1953,12 +1956,23 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) static int wait_on_pipe(struct trace_iterator *iter, int full) { + int ret; + /* Iterators are static, they should be filled or empty */ if (trace_buffer_iter(iter, iter->cpu_file)) return 0; - return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, - full); + ret = ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, full); + +#ifdef CONFIG_TRACER_MAX_TRACE + /* + * Make sure this is still the snapshot buffer, as if a snapshot were + * to happen, this would now be the main buffer. + */ + if (iter->snapshot) + iter->array_buffer = &iter->tr->max_buffer; +#endif + return ret; } #ifdef CONFIG_FTRACE_STARTUP_TEST @@ -8560,7 +8574,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos, wait_index = READ_ONCE(iter->wait_index); - ret = wait_on_pipe(iter, iter->tr->buffer_percent); + ret = wait_on_pipe(iter, iter->snapshot ? 0 : iter->tr->buffer_percent); if (ret) goto out;