Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp168147rdb; Tue, 19 Dec 2023 12:38:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IGtnzpy/zpG4p5j2MoD4XeI8V7mvv3o2OwmloW7YyBTmSvp9vrbofG7bpKOtZ97XOUK90a/ X-Received: by 2002:a05:6214:21ca:b0:67f:2b76:28f4 with SMTP id d10-20020a05621421ca00b0067f2b7628f4mr8707420qvh.43.1703018316418; Tue, 19 Dec 2023 12:38:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703018316; cv=none; d=google.com; s=arc-20160816; b=ftYY0sM6iuHFf5Catfkw8p6ClfjGqlj7PMx96KMglQ/eiOmln3bgZLqL/4BBTZYJrk NKhrSurl64Q95OQbF4NLZr8swXAD8G3CELgC2aJoCk1itUKoa4/TEE1mxn7Is4JPF0Sx XtTgGCZxnzsH6DS8udB6AFnzWTwxZhpPbGLO7BvbF3h7rqkf2NFx9iBkWvDWGscLWOg2 oYS8vXZj0kyRG+Tz6OWIGmBHsjzvZ8K+Dx+Y/WbIEvjjzFTZEBcfx/QLrZ4zTxayYaao iJbQFpmMWD3uRGed1k/eZgqIJNgyglaPmWPHSFxsvZTLMVyihKvu7KmRf+jasmvHfiR4 oQzQ== 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:references:in-reply-to:message-id :subject:cc:to:from:date; bh=3WScc9FvXDY23heILIuEssIgPZpschWDh46nFbXDoJA=; fh=H7rhZUnZ+NlyyU7vxDXWwpu6VPK6D0cjPor36M8Xy6o=; b=QcTW0a4UAdVdA+MpVR9AKzKLbjh8EGucBjmA0FE4gCh4EmvcRL+OHTnSJSTznC9gh3 MU2JTsvThfs4ov57oER1csNKJc+TP2w4fIukpPAHMYdxCVUv9Zx0Tvpb7b1HiZ6O7gzS HTEQrFVa8i/zcVk3poPa8OvXNVGZgiKIOCeHAzvN9cEhf2scaoDuMbbaGRYLLlr7xCr0 WbBeT9fwh5uw5jy1eTD1LFbPbkLVm0IXbjNAAAD2hXlISaOqNxzztZhgz6EL8db5a+fr 7JTioAdoVEcoC1lpeAoH7rNjZjnzvNGdhx+MVzq/c6bO/tIxHf38IchV2J+mlPeBMKCJ 9rJg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-5929-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5929-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u2-20020a0cdd02000000b0067ad7adc2d8si8424198qvk.475.2023.12.19.12.38.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 12:38:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5929-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-5929-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5929-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 2F8851C24914 for ; Tue, 19 Dec 2023 20:38:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6948139ADE; Tue, 19 Dec 2023 20:38:28 +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 ECEC91DFEF; Tue, 19 Dec 2023 20:38:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC630C433C8; Tue, 19 Dec 2023 20:38:26 +0000 (UTC) Date: Tue, 19 Dec 2023 15:39:24 -0500 From: Steven Rostedt To: Vincent Donnefort Cc: mhiramat@kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v8 0/2] Introducing trace buffer mapping by user-space Message-ID: <20231219153924.2ff9c132@gandalf.local.home> In-Reply-To: <20231219184556.1552951-1-vdonnefort@google.com> References: <20231219184556.1552951-1-vdonnefort@google.com> 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 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 19 Dec 2023 18:45:54 +0000 Vincent Donnefort wrote: > The tracing ring-buffers can be stored on disk or sent to network > without any copy via splice. However the later doesn't allow real time > processing of the traces. A solution is to give userspace direct access > to the ring-buffer pages via a mapping. An application can now become a > consumer of the ring-buffer, in a similar fashion to what trace_pipe > offers. > > Attached to this cover letter an example of consuming read for a > ring-buffer, using libtracefs. > I'm still testing this, but I needed to add this patch to fix two bugs. One is that you are calling rb_wakeup_waiters() for both the buffer and the cpu_buffer, and it needs to know which one to use the container_of() macro. The other is a "goto unlock" that unlocks two locks where only one was taken. -- Steve diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 35f3736f660b..987ad7bd1e8b 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -389,6 +389,7 @@ struct rb_irq_work { bool waiters_pending; bool full_waiters_pending; bool wakeup_full; + bool is_cpu_buffer; }; /* @@ -771,10 +772,20 @@ static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer) static void rb_wake_up_waiters(struct irq_work *work) { struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work); - struct ring_buffer_per_cpu *cpu_buffer = - container_of(rbwork, struct ring_buffer_per_cpu, irq_work); + struct ring_buffer_per_cpu *cpu_buffer; + struct trace_buffer *buffer; + int cpu; - rb_update_meta_page(cpu_buffer); + if (rbwork->is_cpu_buffer) { + cpu_buffer = container_of(rbwork, struct ring_buffer_per_cpu, irq_work); + rb_update_meta_page(cpu_buffer); + } else { + buffer = container_of(rbwork, struct trace_buffer, irq_work); + for_each_buffer_cpu(buffer, cpu) { + cpu_buffer = buffer->buffers[cpu]; + rb_update_meta_page(cpu_buffer); + } + } wake_up_all(&rbwork->waiters); if (rbwork->full_waiters_pending || rbwork->wakeup_full) { @@ -1569,6 +1580,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) init_waitqueue_head(&cpu_buffer->irq_work.waiters); init_waitqueue_head(&cpu_buffer->irq_work.full_waiters); mutex_init(&cpu_buffer->mapping_lock); + cpu_buffer->irq_work.is_cpu_buffer = true; bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), GFP_KERNEL, cpu_to_node(cpu)); @@ -6209,7 +6221,8 @@ int ring_buffer_map(struct trace_buffer *buffer, int cpu) if (cpu_buffer->mapped) { WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped + 1); - goto unlock; + mutex_unlock(&cpu_buffer->mapping_lock); + return 0; } /* prevent another thread from changing buffer sizes */