Skip to content

Commit 0074ade

Browse files
committed
Merge tag 'trace-ringbuffer-v6.14' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull trace ring-buffer updates from Steven Rostedt: - Clean up the __rb_map_vma() logic The logic of __rb_map_vma() has a error check with WARN_ON() that makes sure that the index does not go past the end of the array of buffers. The test in the loop pretty much guarantees that it will never happen, but since the relation of the variables used is a little complex, the WARN_ON() check was added. It was noticed that the array was dereferenced before this check and if the logic does break and for some reason the logic goes past the array, there will be an out of bounds access here. Move the access to after the WARN_ON(). - Consolidate how the ring buffer is determined to be empty Currently there's two ways that are used to determine if the ring buffer is empty. One relies on the status of the commit and reader pages and what was read, and the other is on what was written vs what was read. By using the number of entries (written) method, it can be used for reading events that are out of the kernel's control (what pKVM will use). Move to this method to make it easier to implement a pKVM ring buffer that the kernel can read. * tag 'trace-ringbuffer-v6.14' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: ring-buffer: Make reading page consistent with the code logic ring-buffer: Check for empty ring-buffer with rb_num_of_entries()
2 parents 9f3ee94 + 6e31b75 commit 0074ade

File tree

1 file changed

+17
-46
lines changed

1 file changed

+17
-46
lines changed

kernel/trace/ring_buffer.c

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4682,40 +4682,22 @@ int ring_buffer_write(struct trace_buffer *buffer,
46824682
}
46834683
EXPORT_SYMBOL_GPL(ring_buffer_write);
46844684

4685-
static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
4685+
/*
4686+
* The total entries in the ring buffer is the running counter
4687+
* of entries entered into the ring buffer, minus the sum of
4688+
* the entries read from the ring buffer and the number of
4689+
* entries that were overwritten.
4690+
*/
4691+
static inline unsigned long
4692+
rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
46864693
{
4687-
struct buffer_page *reader = cpu_buffer->reader_page;
4688-
struct buffer_page *head = rb_set_head_page(cpu_buffer);
4689-
struct buffer_page *commit = cpu_buffer->commit_page;
4690-
4691-
/* In case of error, head will be NULL */
4692-
if (unlikely(!head))
4693-
return true;
4694-
4695-
/* Reader should exhaust content in reader page */
4696-
if (reader->read != rb_page_size(reader))
4697-
return false;
4698-
4699-
/*
4700-
* If writers are committing on the reader page, knowing all
4701-
* committed content has been read, the ring buffer is empty.
4702-
*/
4703-
if (commit == reader)
4704-
return true;
4705-
4706-
/*
4707-
* If writers are committing on a page other than reader page
4708-
* and head page, there should always be content to read.
4709-
*/
4710-
if (commit != head)
4711-
return false;
4694+
return local_read(&cpu_buffer->entries) -
4695+
(local_read(&cpu_buffer->overrun) + cpu_buffer->read);
4696+
}
47124697

4713-
/*
4714-
* Writers are committing on the head page, we just need
4715-
* to care about there're committed data, and the reader will
4716-
* swap reader page with head page when it is to read data.
4717-
*/
4718-
return rb_page_commit(commit) == 0;
4698+
static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
4699+
{
4700+
return !rb_num_of_entries(cpu_buffer);
47194701
}
47204702

47214703
/**
@@ -4861,19 +4843,6 @@ void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
48614843
}
48624844
EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
48634845

4864-
/*
4865-
* The total entries in the ring buffer is the running counter
4866-
* of entries entered into the ring buffer, minus the sum of
4867-
* the entries read from the ring buffer and the number of
4868-
* entries that were overwritten.
4869-
*/
4870-
static inline unsigned long
4871-
rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
4872-
{
4873-
return local_read(&cpu_buffer->entries) -
4874-
(local_read(&cpu_buffer->overrun) + cpu_buffer->read);
4875-
}
4876-
48774846
/**
48784847
* ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
48794848
* @buffer: The ring buffer
@@ -7059,14 +7028,16 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
70597028
}
70607029

70617030
while (p < nr_pages) {
7062-
struct page *page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
7031+
struct page *page;
70637032
int off = 0;
70647033

70657034
if (WARN_ON_ONCE(s >= nr_subbufs)) {
70667035
err = -EINVAL;
70677036
goto out;
70687037
}
70697038

7039+
page = virt_to_page((void *)cpu_buffer->subbuf_ids[s]);
7040+
70707041
for (; off < (1 << (subbuf_order)); off++, page++) {
70717042
if (p >= nr_pages)
70727043
break;

0 commit comments

Comments
 (0)