From: Vincent Donnefort <vdonnefort@google.com>
To: rostedt@goodmis.org, mhiramat@kernel.org,
linux-trace-kernel@vger.kernel.org
Cc: mathieu.desnoyers@efficios.com, kernel-team@android.com,
linux-kernel@vger.kernel.org,
Vincent Donnefort <vdonnefort@google.com>
Subject: [PATCH v1 7/9] ring-buffer: Avoid reading buffer::subbuf_order on the write path
Date: Thu, 12 Nov 2026 14:14:38 +0000 [thread overview]
Message-ID: <20261112141440.3050124-8-vdonnefort@google.com> (raw)
In-Reply-To: <20261112141440.3050124-1-vdonnefort@google.com>
buffer::subbuf_order is protected by buffer::mutex, which cannot be
held on the write path. While reading it is safe because writers are
disabled during order changes, reading the order from commit_page (which
is pinned for the duration of the commit) avoids confusing context
analysis.
Because rb_subbuf_size() and rb_subbuf_capacity() are no longer on the
write hot path, drop their __always_inline annotations.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
kernel/trace/ring_buffer.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 02d3cc193d1e..82f6e3df4627 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -606,7 +606,7 @@ struct trace_buffer {
unsigned int subbuf_order;
};
-static __always_inline unsigned int rb_subbuf_size(struct trace_buffer *buffer)
+static unsigned int rb_subbuf_size(struct trace_buffer *buffer)
{
return PAGE_SIZE << buffer->subbuf_order;
}
@@ -620,7 +620,7 @@ static __always_inline unsigned int rb_subbuf_size(struct trace_buffer *buffer)
*
* Return: The maximum size available for events in a trace buffer subbuffer.
*/
-static __always_inline unsigned int rb_subbuf_capacity(struct trace_buffer *buffer)
+static unsigned int rb_subbuf_capacity(struct trace_buffer *buffer)
{
return rb_subbuf_size(buffer) - BUF_PAGE_HDR_SIZE;
}
@@ -632,15 +632,15 @@ static unsigned long rb_calculate_nr_pages(struct trace_buffer *buffer, unsigned
/**
* rb_subbuf_start - Get the start address of a subbuffer
- * @buffer: A trace buffer
+ * @cpu_buffer: A per-CPU ring buffer
* @addr: An address of an event on a subbuffer
*
* Return: The start of the subbuffer for where @addr sits
*/
static __always_inline
-unsigned long rb_subbuf_start(struct trace_buffer *buffer, unsigned long addr)
+unsigned long rb_subbuf_start(struct ring_buffer_per_cpu *cpu_buffer, unsigned long addr)
{
- return addr & ~((unsigned long)(rb_subbuf_size(buffer) - 1));
+ return addr & ~((unsigned long)(PAGE_SIZE << cpu_buffer->commit_page->order) - 1);
}
static bool rb_is_static(struct ring_buffer_per_cpu *cpu_buffer)
@@ -3582,7 +3582,7 @@ rb_event_index(struct ring_buffer_per_cpu *cpu_buffer, struct ring_buffer_event
{
unsigned long addr = (unsigned long)event;
- addr &= (unsigned long)rb_subbuf_size(cpu_buffer->buffer) - 1;
+ addr &= (unsigned long)(PAGE_SIZE << cpu_buffer->commit_page->order) - 1;
return addr - BUF_PAGE_HDR_SIZE;
}
@@ -4168,7 +4168,7 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
new_index = rb_event_index(cpu_buffer, event);
old_index = new_index + rb_event_ts_length(event);
- addr = rb_subbuf_start(cpu_buffer->buffer, (unsigned long)event);
+ addr = rb_subbuf_start(cpu_buffer, (unsigned long)event);
bpage = READ_ONCE(cpu_buffer->tail_page);
@@ -5091,7 +5091,7 @@ rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
struct buffer_page *bpage = cpu_buffer->commit_page;
struct buffer_page *start;
- addr = rb_subbuf_start(cpu_buffer->buffer, addr);
+ addr = rb_subbuf_start(cpu_buffer, addr);
/* Do the likely case first */
if (likely(bpage->page == (void *)addr)) {
--
2.56.0.567.gdb80ae8cd5-goog
next prev parent reply other threads:[~2026-11-12 14:14 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 14:14 [PATCH v1 0/9] ring-buffer: Enable Clang context analysis Vincent Donnefort
2026-11-12 14:14 ` [PATCH v1 1/9] ring-buffer: Always read buffer->buffers after testing the cpumask Vincent Donnefort
2026-11-12 14:22 ` sashiko-bot
2026-11-12 14:14 ` [PATCH v1 2/9] ring-buffer: Drop cpus_read_lock() from ring_buffer_poll_remote() Vincent Donnefort
2026-11-12 14:14 ` [PATCH v1 3/9] ring-buffer: Hold buffer->mutex in trace_rb_cpu_prepare() Vincent Donnefort
2026-11-12 14:14 ` [PATCH v1 4/9] tracing: Cache ring_buffer_max_event_size() for trace_marker Vincent Donnefort
2026-11-12 14:25 ` sashiko-bot
2026-11-12 14:14 ` [PATCH v1 5/9] ring-buffer: Validate event length against rb_page_capacity() Vincent Donnefort
2026-11-12 14:14 ` [PATCH v1 6/9] ring-buffer: Introduce rb_start_write() and rb_end_write() Vincent Donnefort
2026-11-12 14:14 ` Vincent Donnefort [this message]
2026-11-12 14:14 ` [PATCH v1 8/9] ring-buffer: Enable context analysis Vincent Donnefort
2026-11-12 14:28 ` sashiko-bot
2026-11-12 14:14 ` [PATCH v1 9/9] ring-buffer: Annotate subbuf_order with __guarded_by(&mutex) Vincent Donnefort
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=20261112141440.3050124-8-vdonnefort@google.com \
--to=vdonnefort@google.com \
--cc=kernel-team@android.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=rostedt@goodmis.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