All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vincent Donnefort <vdonnefort@google.com>
To: 高翔 <gaoxiang17@xiaomi.com>
Cc: "Xiang Gao" <gxxa03070307@gmail.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Masami Hiramatsu" <mhiramat@kernel.org>,
	"Donggeun Yoo" <donggeunyoo.kernel@gmail.com>,
	"Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
	"Lorenzo Stoakes" <ljs@kernel.org>, "gao xu" <gaoxu2@honor.com>,
	印闯 <yinchuang1@xiaomi.com>,
	"linux-trace-kernel@vger.kernel.org"
	<linux-trace-kernel@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: 答复: [External Mail]Re: [PATCH v5 1/4] ring-buffer: add ring_buffer_memory_size()
Date: Tue, 17 Nov 2026 14:13:04 +0000	[thread overview]
Message-ID: <avxg8M4dcs7Gphc0@google.com> (raw)
In-Reply-To: <aac878d9f74e49d7b3bccd0f352b700c@xiaomi.com>

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@google.com>
> 发送时间: 2026年11月14日 22:17:29
> 收件人: Xiang Gao
> 抄送: Steven Rostedt; Masami Hiramatsu; Donggeun Yoo; Mathieu Desnoyers; Lorenzo Stoakes; gao xu; 印闯; linux-trace-kernel@vger.kernel.org; linux-kernel@vger.kernel.org; 高翔
> 主题: [External Mail]Re: [PATCH v5 1/4] ring-buffer: add ring_buffer_memory_size()
> 
> [外部邮件] 此邮件来源于小米公司外部,请谨慎处理。若对邮件安全性存疑,请将邮件转发给misec@xiaomi.com进行反馈
> 
> 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@xiaomi.com>
> > ---
> >  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
> >

  parent reply	other threads:[~2026-11-17 14:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05 14:02 [PATCH v5 0/4] tracing: add ring-buffer memory usage statistics Xiang Gao
2026-11-05 14:02 ` [PATCH v5 1/4] ring-buffer: add ring_buffer_memory_size() Xiang Gao
2026-11-14 14:17   ` Vincent Donnefort
     [not found]     ` <aac878d9f74e49d7b3bccd0f352b700c@xiaomi.com>
2026-11-17 14:13       ` Vincent Donnefort [this message]
2026-11-05 14:02 ` [PATCH v5 2/4] tracing: add aggregate buffer memory usage statistics Xiang Gao
2026-11-05 14:02 ` [PATCH v5 3/4] tracing: add per-CPU " Xiang Gao
2026-11-05 14:12   ` sashiko-bot
2026-11-05 14:02 ` [PATCH v5 4/4] Documentation: document trace_stats memory_usage_kb Xiang Gao

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=avxg8M4dcs7Gphc0@google.com \
    --to=vdonnefort@google.com \
    --cc=donggeunyoo.kernel@gmail.com \
    --cc=gaoxiang17@xiaomi.com \
    --cc=gaoxu2@honor.com \
    --cc=gxxa03070307@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=yinchuang1@xiaomi.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.