Skip to content

Commit 119a5d5

Browse files
committed
ring-buffer: Remove ring_buffer_read_prepare_sync()
When the ring buffer was first introduced, reading the non-consuming "trace" file required disabling the writing of the ring buffer. To make sure the writing was fully disabled before iterating the buffer with a non-consuming read, it would set the disable flag of the buffer and then call an RCU synchronization to make sure all the buffers were synchronized. The function ring_buffer_read_start() originally would initialize the iterator and call an RCU synchronization, but this was for each individual per CPU buffer where this would get called many times on a machine with many CPUs before the trace file could be read. The commit 72c9ddf ("ring-buffer: Make non-consuming read less expensive with lots of cpus.") separated ring_buffer_read_start into ring_buffer_read_prepare(), ring_buffer_read_sync() and then ring_buffer_read_start() to allow each of the per CPU buffers to be prepared, call the read_buffer_read_sync() once, and then the ring_buffer_read_start() for each of the CPUs which made things much faster. The commit 1039221 ("ring-buffer: Do not disable recording when there is an iterator") removed the requirement of disabling the recording of the ring buffer in order to iterate it, but it did not remove the synchronization that was happening that was required to wait for all the buffers to have no more writers. It's now OK for the buffers to have writers and no synchronization is needed. Remove the synchronization and put back the interface for the ring buffer iterator back before commit 72c9ddf was applied. Cc: Mathieu Desnoyers <[email protected]> Link: https://lore.kernel.org/[email protected] Reported-by: David Howells <[email protected]> Fixes: 1039221 ("ring-buffer: Do not disable recording when there is an iterator") Tested-by: David Howells <[email protected]> Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent ca296d3 commit 119a5d5

File tree

4 files changed

+18
-71
lines changed

4 files changed

+18
-71
lines changed

include/linux/ring_buffer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
152152
unsigned long *lost_events);
153153

154154
struct ring_buffer_iter *
155-
ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags);
156-
void ring_buffer_read_prepare_sync(void);
157-
void ring_buffer_read_start(struct ring_buffer_iter *iter);
155+
ring_buffer_read_start(struct trace_buffer *buffer, int cpu, gfp_t flags);
158156
void ring_buffer_read_finish(struct ring_buffer_iter *iter);
159157

160158
struct ring_buffer_event *

kernel/trace/ring_buffer.c

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5943,24 +5943,20 @@ ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
59435943
EXPORT_SYMBOL_GPL(ring_buffer_consume);
59445944

59455945
/**
5946-
* ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
5946+
* ring_buffer_read_start - start a non consuming read of the buffer
59475947
* @buffer: The ring buffer to read from
59485948
* @cpu: The cpu buffer to iterate over
59495949
* @flags: gfp flags to use for memory allocation
59505950
*
5951-
* This performs the initial preparations necessary to iterate
5952-
* through the buffer. Memory is allocated, buffer resizing
5953-
* is disabled, and the iterator pointer is returned to the caller.
5954-
*
5955-
* After a sequence of ring_buffer_read_prepare calls, the user is
5956-
* expected to make at least one call to ring_buffer_read_prepare_sync.
5957-
* Afterwards, ring_buffer_read_start is invoked to get things going
5958-
* for real.
5951+
* This creates an iterator to allow non-consuming iteration through
5952+
* the buffer. If the buffer is disabled for writing, it will produce
5953+
* the same information each time, but if the buffer is still writing
5954+
* then the first hit of a write will cause the iteration to stop.
59595955
*
5960-
* This overall must be paired with ring_buffer_read_finish.
5956+
* Must be paired with ring_buffer_read_finish.
59615957
*/
59625958
struct ring_buffer_iter *
5963-
ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
5959+
ring_buffer_read_start(struct trace_buffer *buffer, int cpu, gfp_t flags)
59645960
{
59655961
struct ring_buffer_per_cpu *cpu_buffer;
59665962
struct ring_buffer_iter *iter;
@@ -5986,51 +5982,12 @@ ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
59865982

59875983
atomic_inc(&cpu_buffer->resize_disabled);
59885984

5989-
return iter;
5990-
}
5991-
EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
5992-
5993-
/**
5994-
* ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
5995-
*
5996-
* All previously invoked ring_buffer_read_prepare calls to prepare
5997-
* iterators will be synchronized. Afterwards, read_buffer_read_start
5998-
* calls on those iterators are allowed.
5999-
*/
6000-
void
6001-
ring_buffer_read_prepare_sync(void)
6002-
{
6003-
synchronize_rcu();
6004-
}
6005-
EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
6006-
6007-
/**
6008-
* ring_buffer_read_start - start a non consuming read of the buffer
6009-
* @iter: The iterator returned by ring_buffer_read_prepare
6010-
*
6011-
* This finalizes the startup of an iteration through the buffer.
6012-
* The iterator comes from a call to ring_buffer_read_prepare and
6013-
* an intervening ring_buffer_read_prepare_sync must have been
6014-
* performed.
6015-
*
6016-
* Must be paired with ring_buffer_read_finish.
6017-
*/
6018-
void
6019-
ring_buffer_read_start(struct ring_buffer_iter *iter)
6020-
{
6021-
struct ring_buffer_per_cpu *cpu_buffer;
6022-
unsigned long flags;
6023-
6024-
if (!iter)
6025-
return;
6026-
6027-
cpu_buffer = iter->cpu_buffer;
6028-
6029-
raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5985+
guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock);
60305986
arch_spin_lock(&cpu_buffer->lock);
60315987
rb_iter_reset(iter);
60325988
arch_spin_unlock(&cpu_buffer->lock);
6033-
raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5989+
5990+
return iter;
60345991
}
60355992
EXPORT_SYMBOL_GPL(ring_buffer_read_start);
60365993

kernel/trace/trace.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4735,21 +4735,15 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot)
47354735
if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
47364736
for_each_tracing_cpu(cpu) {
47374737
iter->buffer_iter[cpu] =
4738-
ring_buffer_read_prepare(iter->array_buffer->buffer,
4739-
cpu, GFP_KERNEL);
4740-
}
4741-
ring_buffer_read_prepare_sync();
4742-
for_each_tracing_cpu(cpu) {
4743-
ring_buffer_read_start(iter->buffer_iter[cpu]);
4738+
ring_buffer_read_start(iter->array_buffer->buffer,
4739+
cpu, GFP_KERNEL);
47444740
tracing_iter_reset(iter, cpu);
47454741
}
47464742
} else {
47474743
cpu = iter->cpu_file;
47484744
iter->buffer_iter[cpu] =
4749-
ring_buffer_read_prepare(iter->array_buffer->buffer,
4750-
cpu, GFP_KERNEL);
4751-
ring_buffer_read_prepare_sync();
4752-
ring_buffer_read_start(iter->buffer_iter[cpu]);
4745+
ring_buffer_read_start(iter->array_buffer->buffer,
4746+
cpu, GFP_KERNEL);
47534747
tracing_iter_reset(iter, cpu);
47544748
}
47554749

kernel/trace/trace_kdb.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ static void ftrace_dump_buf(int skip_entries, long cpu_file)
4343
if (cpu_file == RING_BUFFER_ALL_CPUS) {
4444
for_each_tracing_cpu(cpu) {
4545
iter.buffer_iter[cpu] =
46-
ring_buffer_read_prepare(iter.array_buffer->buffer,
47-
cpu, GFP_ATOMIC);
48-
ring_buffer_read_start(iter.buffer_iter[cpu]);
46+
ring_buffer_read_start(iter.array_buffer->buffer,
47+
cpu, GFP_ATOMIC);
4948
tracing_iter_reset(&iter, cpu);
5049
}
5150
} else {
5251
iter.cpu_file = cpu_file;
5352
iter.buffer_iter[cpu_file] =
54-
ring_buffer_read_prepare(iter.array_buffer->buffer,
53+
ring_buffer_read_start(iter.array_buffer->buffer,
5554
cpu_file, GFP_ATOMIC);
56-
ring_buffer_read_start(iter.buffer_iter[cpu_file]);
5755
tracing_iter_reset(&iter, cpu_file);
5856
}
5957

0 commit comments

Comments
 (0)