From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Steven Rostedt <rostedt@goodmis.org>, Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
Masami Hiramatsu <mhiramat@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
Vincent Donnefort <vdonnefort@google.com>,
Vlastimil Babka <vbabka@suse.cz>, Mike Rapoport <rppt@kernel.org>,
Kees Cook <kees@kernel.org>, Tony Luck <tony.luck@intel.com>,
"Guilherme G. Piccoli" <gpiccoli@igalia.com>,
linux-hardening@vger.kernel.org,
Matthew Wilcox <willy@infradead.org>
Subject: Re: [PATCH v2 1/2] tracing: ring-buffer: Have the ring buffer code do the vmap of physical memory
Date: Mon, 31 Mar 2025 22:23:00 -0400 [thread overview]
Message-ID: <db2123c9-4777-4cc5-a00f-3df78edf5cb7@efficios.com> (raw)
In-Reply-To: <20250331215051.21d77cab@gandalf.local.home>
On 2025-03-31 21:50, Steven Rostedt wrote:
> On Tue, 1 Apr 2025 03:28:20 +0200
> Jann Horn <jannh@google.com> wrote:
>
>> I think you probably need flushes on both sides, since you might have
>> to first flush out the dirty cacheline you wrote through the kernel
>> mapping, then discard the stale clean cacheline for the user mapping,
>> or something like that? (Unless these VIVT cache architectures provide
>> stronger guarantees on cache state than I thought.) But when you're
>> adding data to the tracing buffers, I guess maybe you only want to
>> flush the kernel mapping from the kernel, and leave flushing of the
>> user mapping to userspace? I think if you're running in some random
>> kernel context, you probably can't even reliably flush the right
>> userspace context - see how for example vivt_flush_cache_range() does
>> nothing if the MM being flushed is not running on the current CPU.
>
> I'm assuming I need to flush both the kernel (get the updates out to
> memory) and user space (so it can read those updates).
>
> The paths are all done via system calls from user space, so it should be on
> the same CPU. User space will do an ioctl() on the buffer file descriptor
> asking for an update, the kernel will populate the page with that update,
> and then user space will read the update after the ioctl() returns. All
> very synchronous. Thus, we don't need to worry about updates from one CPU
> happening on another CPU.
>
> Even when it wants to read the buffer. The ioctl() will swap out the old
> reader page with one of the write pages making it the new "reader" page,
> where no more updates will happen on that page. The flush happens after
> that and before going back to user space.
FWIW, I have the following in the LTTng kernel tracer to cover this.
LTTng writes to ring buffers through the linear mapping, and reads
the buffers from userspace either through mmap or splice.
When userspace wants to get read access to a sub-buffer (an abstraction
that generalizes your ftrace ring buffer "pages") through mmap,
it does the following through a "get subbuffer" ioctl:
- Use "cpu_dcache_is_aliasing()" to check whether explicit flushing is
needed between the kernel linear mapping and userspace mappings.
- Use flush_dcache_page() to make sure all mappings for a given page
are flushed (both the kernel linear mapping and the userspace virtual
mappings).
I suspect that if you go down the route of the explicit
"flush_cache_range()", then you'll need to issue it on all
mappings that alias your memory.
AFAIU, using flush_dcache_page() saves you the trouble of issuing
flush_cache_range() on all mapping aliases manually.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
next prev parent reply other threads:[~2025-04-01 2:23 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-31 14:34 [PATCH v2 0/2] ring-buffer: Allow persistent memory to be user space mmapped Steven Rostedt
2025-03-31 14:34 ` [PATCH v2 1/2] tracing: ring-buffer: Have the ring buffer code do the vmap of physical memory Steven Rostedt
2025-03-31 16:55 ` Linus Torvalds
2025-03-31 17:39 ` Steven Rostedt
2025-03-31 19:12 ` Linus Torvalds
2025-03-31 20:58 ` Steven Rostedt
2025-03-31 21:42 ` Linus Torvalds
2025-03-31 23:42 ` Steven Rostedt
2025-04-01 0:09 ` Jann Horn
2025-04-01 1:02 ` Steven Rostedt
2025-04-01 1:28 ` Jann Horn
2025-04-01 1:50 ` Steven Rostedt
2025-04-01 2:23 ` Mathieu Desnoyers [this message]
2025-04-01 1:30 ` Linus Torvalds
2025-04-01 1:41 ` Steven Rostedt
2025-04-01 1:55 ` Linus Torvalds
2025-04-01 9:53 ` Ingo Molnar
2025-04-01 0:11 ` Linus Torvalds
2025-04-01 0:27 ` Linus Torvalds
2025-04-01 0:30 ` Steven Rostedt
2025-04-01 0:38 ` Linus Torvalds
2025-04-01 0:49 ` Linus Torvalds
2025-04-01 1:36 ` Steven Rostedt
2025-04-01 1:44 ` Linus Torvalds
2025-04-03 5:59 ` Herbert Xu
2025-04-03 16:47 ` Linus Torvalds
2025-04-01 9:56 ` Mike Rapoport
2025-04-01 15:11 ` Steven Rostedt
2025-04-01 15:26 ` Mike Rapoport
2025-04-01 15:54 ` Steven Rostedt
2025-04-01 17:58 ` Mike Rapoport
2025-04-03 16:45 ` Kees Cook
2025-04-03 16:51 ` Linus Torvalds
2025-04-03 17:15 ` Steven Rostedt
2025-03-31 14:34 ` [PATCH v2 2/2] ring-buffer: Allow persistent ring buffers to be mmapped Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=db2123c9-4777-4cc5-a00f-3df78edf5cb7@efficios.com \
--to=mathieu.desnoyers@efficios.com \
--cc=akpm@linux-foundation.org \
--cc=gpiccoli@igalia.com \
--cc=jannh@google.com \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=tony.luck@intel.com \
--cc=torvalds@linux-foundation.org \
--cc=vbabka@suse.cz \
--cc=vdonnefort@google.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®