Re: [External Mail]Re: [PATCH v5 1/4] ring-buffer: add ring_buffer_memory_size()
From: Steven Rostedt
Date: Mon Nov 23 2026 - 12:05:26 EST
On Mon, 23 Nov 2026 11:13:06 +0000
高翔 <gaoxiang17@xxxxxxxxxx> wrote:
>
> > Actually I am more and more convinced a more generic helper is better.
> > This could be ring_buffer_nr_subbufs() which only reads
> > cpu_buffer->nr_pages.
>
>
> Agreed. One thing I want to confirm: the reader page is one page per CPU
> for every buffer type. normal, range, and remote all allocate it in
> rb_allocate_cpu_buffer(), so trace_stat would add one sub-buffer page
> per CPU unconditionally. Your earlier "if the caller knows if a
> cpu_buffer is a remote or persistent" comment made it sound like the +1
> is conditional, but I think it always applies.
It should still get the information directly from the ring buffer and not
make any assumptions.
>
>
> > Regarding the "meta" pages, we can have a separate accessor. e.g.
> > ring_buffer_nr_meta_pages(). The caller could easily "guess" this
> > value, but as they are ring buffer internals, I suppose an accessor is
> > alright.
>
>
> Agreed. To confirm the semantics: meta_page is one page per CPU and is
> only allocated when the buffer is user-mapped (rb_alloc_meta_page()), so
> ring_buffer_nr_meta_pages() would return 0 or 1 per CPU.
It can also be allocated at boot up for the persistent ring buffer. Again,
something the ring buffer needs to supply information about.
>
>
> > And actually writing the paragraph above made me realise it would be
> > worth splitting the commit between ring buffer data allocations and
> > meta-page allocations (and perhaps showing the difference in
> > trace_stat). The sync for data and meta-page is slightly different.
>
>
> That makes sense. I was thinking of splitting trace_stat as well: the
> buffer categories report data pages only, and the meta pages are summed
> into a separate line, for example:
>
>
> global: XXX (data pages)
> instances: XXX (data pages)
> snapshots: XXX (data pages)
> persistents: XXX (data pages)
> remotes: 0
> meta_pages: XXX (sum over global and all instances)
> total_system: XXX (= global + instances + snapshots + remotes
> + meta_pages)
> total_carveout: XXX (= persistents)
>
>
> Do you have a preference between a single meta_pages line and a
> per-category data/meta split?
Not sure what you mean by the above. The meta pages should include all
allocations that contain meta data only.
>
>
> > If being off by at most one page is acceptable... (well, one subbuf
> > per trace_pipe_raw reader) then why accounting for it at all? :-)
>
>
> Fair enough. To make sure I understand: free_page is not just the single
> cached page in cpu_buffer->free_page, but also one page held by each
> trace_pipe_raw reader while it consumes the buffer. That makes the count
> depend on the number of readers, so it cannot be accounted without a
> racy read. I will drop it.
Yes, the free page is simply a cache that is stored for reuse and is not
created per-se. It is simply reserved when a previous free happens.
>
>
> > ring_buffer_memory_size() still hit a race with
> > ring_buffer_set_subbuf_order(). and existing accessors like
> > ring_buffer_capacity() do too. It is down to the caller to resolve
> > this race, if necessary.
>
>
> Understood. trace_stat already holds trace_types_lock while walking
> ftrace_trace_arrays, but it does not hold buffer->mutex, so subbuf_order
> and nr_pages are read without the resize lock. The reported value can
> therefore be momentarily stale while a resize is in flight. I think that
> is acceptable for a memory-usage statistic; please tell me if you would
> rather have trace_stat serialize against resize.
Yes that is acceptable as even if we had locking, the information could
become stale as soon as it reaches user space. No need to protect against races that can happen when user space is reading it anyway.
-- Steve