Re: 答复: [External Mail]Re: [PATCH v5 1/4] ring-buffer: add ring_buffer_memory_size()

From: Vincent Donnefort

Date: Tue Nov 17 2026 - 09:15:43 EST


On Tue, Nov 17, 2026 at 10:58:05AM +0000, 高翔 wrote:

Hi Xiang,

Please post your answer inline next time. This is easier to keep track of the
different items.


> Thanks, Vincent.
>
>
> Agreed on the naming — I will rename it to
> ring_buffer_page_memory_size() to make clear it accounts for page
> memory only, not the slab allocations.

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.

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.

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.

>
>
> On free_page: the read is racy by nature, but it is a best-effort
> accounting that is off by at most one page, which is acceptable for
> memory reporting. I will mark the reads with data_race() so KCSAN does
> not complain, rather than drop the accounting entirely. The same applies
> to the mmap metadata page.

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? :-)

>
>
> I would still keep a helper rather than compose the existing APIs in the
> caller, for two reasons: the caller would otherwise need to know
> BUF_PAGE_HDR_SIZE, and the helper reads subbuf_order once with READ_ONCE,
> avoiding the subbuf_size_set() race your formula would hit.

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.

--
Vincent

>
>
> Thanks,
> Xiang
>
>
>
> ________________________________
> 发件人: Vincent Donnefort <vdonnefort@xxxxxxxxxx>
> 发送时间: 2026年11月14日 22:17:29
> 收件人: Xiang Gao
> 抄送: Steven Rostedt; Masami Hiramatsu; Donggeun Yoo; Mathieu Desnoyers; Lorenzo Stoakes; gao xu; 印闯; linux-trace-kernel@xxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx; 高翔
> 主题: [External Mail]Re: [PATCH v5 1/4] ring-buffer: add ring_buffer_memory_size()
>
> [外部邮件] 此邮件来源于小米公司外部,请谨慎处理。若对邮件安全性存疑,请将邮件转发给misec@xxxxxxxxxx进行反馈
>
> On Thu, Nov 05, 2026 at 10:02:18PM +0800, Xiang Gao wrote:
> > Add a helper to report the page memory consumed by a ring buffer for a
> > given CPU. It accounts for the data sub-buffers, the reader page, the
> > cached read page, and the mmap metadata page. Unlike
> > ring_buffer_capacity(), which reports the usable data capacity, this
> > reports the full pages allocated to the buffer.
> >
> > The function is generic and returns a size for any buffer type, so
> > callers that need the memory footprint of remote or persistent buffers
> > can use it as well.
> >
> > Signed-off-by: Xiang Gao <gaoxiang17@xxxxxxxxxx>
> > ---
> > include/linux/ring_buffer.h | 1 +
> > kernel/trace/ring_buffer.c | 37 +++++++++++++++++++++++++++++++++++++
> > 2 files changed, 38 insertions(+)
> >
> > diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h
> > index 649b2276b557..e83e94623e76 100644
> > --- a/include/linux/ring_buffer.h
> > +++ b/include/linux/ring_buffer.h
> > @@ -174,6 +174,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_capacity(struct trace_buffer *buffer, int cpu);
> > +unsigned long ring_buffer_memory_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);
> > diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> > index 1fd28a72e6ee..0396ec671998 100644
> > --- a/kernel/trace/ring_buffer.c
> > +++ b/kernel/trace/ring_buffer.c
> > @@ -6566,6 +6566,43 @@ unsigned long ring_buffer_capacity(struct trace_buffer *buffer, int cpu)
> > }
> > EXPORT_SYMBOL_GPL(ring_buffer_capacity);
> >
> > +/**
> > + * ring_buffer_memory_size - return the memory used by the buffer (in bytes)
> > + * @buffer: The ring buffer.
> > + * @cpu: The CPU to get ring buffer memory from.
> > + *
> > + * Returns the page memory consumed by @cpu, including the data sub-buffers,
> > + * the reader page, the cached read page, and the mmap metadata page. Unlike
> > + * ring_buffer_capacity(), which reports the usable data capacity, this
> > + * accounts for the full pages allocated to the buffer.
> > + */
> > +unsigned long ring_buffer_memory_size(struct trace_buffer *buffer, int cpu)
>
> memory_size() may be confusing because we don't account for all the other
> kmalloc() allocations.
>
> I have also just realised, do we really need yet another accessor? With the
> existing ones:
>
> nr_pages = ring_buffer_capacity(buffer, cpu) / (ring_buffer_subbuf_size_get(buffer) - BUF_PAGE_HDR_SIZE);
gg>
> (Alhough there's a race with ring_buffer_subbuf_size_set() to be resolved!!!)
>
> And if the caller knows if a cpu_buffer is a remote, or a persistent buffer, it
> can just add nr_pages++.
>
> This only leaves free_page unaccounted for. But does that really matter if the
> other solution would be to have a very racy free_page accounting?
>
> --
> Vincent
>
> > +{
> > + struct ring_buffer_per_cpu *cpu_buffer;
> > + unsigned long subbuf_size;
> > + unsigned long size;
> > +
> > + if (!cpumask_test_cpu(cpu, buffer->cpumask))
> gg> + return 0;
> > +
> > + cpu_buffer = buffer->buffers[cpu];
> > + subbuf_size = PAGE_SIZE << READ_ONCE(buffer->subbuf_order);
> > +
> > + /* Data sub-buffers plus the reader page. */
> > + size = (READ_ONCE(cpu_buffer->nr_pages) + 1) * subbuf_size;
> > +
> > + /* The cached read page, if present, is a full sub-buffer page. */
> > + if (READ_ONCE(cpu_buffer->free_page.data))
> > + size += subbuf_size;
> > +
> > + /* The mmap metadata page is a single system page. */
> > + if (READ_ONCE(cpu_buffer->meta_page))
> > + size += PAGE_SIZE;
> > +
> > + return size;
> > +}
> > +EXPORT_SYMBOL_GPL(ring_buffer_memory_size);
> > +
> > /**
> > * ring_buffer_max_event_size - return the max data size of an event
> > * @buffer: The ring buffer.
> > --
> > 2.34.1
> >