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 6/9] ring-buffer: Introduce rb_start_write() and rb_end_write()
Date: Thu, 12 Nov 2026 14:14:37 +0000 [thread overview]
Message-ID: <20261112141440.3050124-7-vdonnefort@google.com> (raw)
In-Reply-To: <20261112141440.3050124-1-vdonnefort@google.com>
Factor the common write-context entry (preemption disabling, disabled
checks, cpumask testing, recursive locking) and exit paths shared by
ring_buffer_lock_reserve() and ring_buffer_write() into a pair of
functions: rb_start_write() and rb_end_write().
No functional change intended.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
kernel/trace/ring_buffer.c | 106 ++++++++++++++++++-------------------
1 file changed, 51 insertions(+), 55 deletions(-)
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 9834f9dae440..02d3cc193d1e 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -4495,6 +4495,45 @@ trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
cpu_buffer->current_context - (1 << cpu_buffer->nest);
}
+static __always_inline struct ring_buffer_per_cpu *
+rb_start_write(struct trace_buffer *buffer)
+{
+ struct ring_buffer_per_cpu *cpu_buffer;
+ int cpu;
+
+ /* If we are tracing schedule, we don't want to recurse */
+ preempt_disable_notrace();
+
+ if (unlikely(atomic_read(&buffer->record_disabled)))
+ goto out;
+
+ cpu = raw_smp_processor_id();
+
+ if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
+ goto out;
+
+ cpu_buffer = buffer->buffers[cpu];
+
+ if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
+ goto out;
+
+ if (unlikely(trace_recursive_lock(cpu_buffer)))
+ goto out;
+
+ return cpu_buffer;
+
+out:
+ preempt_enable_notrace();
+
+ return NULL;
+}
+
+static __always_inline void rb_end_write(struct ring_buffer_per_cpu *cpu_buffer)
+{
+ trace_recursive_unlock(cpu_buffer);
+ preempt_enable_notrace();
+}
+
/* The recursive locking above uses 5 bits */
#define NESTED_BITS 5
@@ -4563,9 +4602,7 @@ int ring_buffer_unlock_commit(struct trace_buffer *buffer)
rb_wakeups(buffer, cpu_buffer);
- trace_recursive_unlock(cpu_buffer);
-
- preempt_enable_notrace();
+ rb_end_write(cpu_buffer);
return 0;
}
@@ -5027,38 +5064,16 @@ ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
{
struct ring_buffer_per_cpu *cpu_buffer;
struct ring_buffer_event *event;
- int cpu;
- /* If we are tracing schedule, we don't want to recurse */
- preempt_disable_notrace();
-
- if (unlikely(atomic_read(&buffer->record_disabled)))
- goto out;
-
- cpu = raw_smp_processor_id();
-
- if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
- goto out;
-
- cpu_buffer = buffer->buffers[cpu];
-
- if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
- goto out;
-
- if (unlikely(trace_recursive_lock(cpu_buffer)))
- goto out;
+ cpu_buffer = rb_start_write(buffer);
+ if (!cpu_buffer)
+ return NULL;
event = rb_reserve_next_event(buffer, cpu_buffer, length);
if (!event)
- goto out_unlock;
+ rb_end_write(cpu_buffer);
return event;
-
- out_unlock:
- trace_recursive_unlock(cpu_buffer);
- out:
- preempt_enable_notrace();
- return NULL;
}
EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
@@ -5143,11 +5158,7 @@ void ring_buffer_discard_commit(struct trace_buffer *buffer,
rb_decrement_entry(cpu_buffer, event);
rb_try_to_discard(cpu_buffer, event);
rb_end_commit(cpu_buffer);
-
- trace_recursive_unlock(cpu_buffer);
-
- preempt_enable_notrace();
-
+ rb_end_write(cpu_buffer);
}
EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
@@ -5170,31 +5181,16 @@ int ring_buffer_write(struct trace_buffer *buffer,
{
struct ring_buffer_per_cpu *cpu_buffer;
struct ring_buffer_event *event;
- void *body;
int ret = -EBUSY;
- int cpu;
+ void *body;
- guard(preempt_notrace)();
-
- if (atomic_read(&buffer->record_disabled))
- return -EBUSY;
-
- cpu = raw_smp_processor_id();
-
- if (!cpumask_test_cpu(cpu, buffer->cpumask))
- return -EBUSY;
-
- cpu_buffer = buffer->buffers[cpu];
-
- if (atomic_read(&cpu_buffer->record_disabled))
- return -EBUSY;
-
- if (unlikely(trace_recursive_lock(cpu_buffer)))
+ cpu_buffer = rb_start_write(buffer);
+ if (!cpu_buffer)
return -EBUSY;
event = rb_reserve_next_event(buffer, cpu_buffer, length);
if (!event)
- goto out_unlock;
+ goto out;
body = rb_event_data(event);
@@ -5206,8 +5202,8 @@ int ring_buffer_write(struct trace_buffer *buffer,
ret = 0;
- out_unlock:
- trace_recursive_unlock(cpu_buffer);
+out:
+ rb_end_write(cpu_buffer);
return ret;
}
EXPORT_SYMBOL_GPL(ring_buffer_write);
--
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 ` Vincent Donnefort [this message]
2026-11-12 14:14 ` [PATCH v1 7/9] ring-buffer: Avoid reading buffer::subbuf_order on the write path Vincent Donnefort
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-7-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