Skip to content

Commit 68282dd

Browse files
committed
ring-buffer: Fix resetting of shortest_full
The "shortest_full" variable is used to keep track of the waiter that is waiting for the smallest amount on the ring buffer before being woken up. When a tasks waits on the ring buffer, it passes in a "full" value that is a percentage. 0 means wake up on any data. 1-100 means wake up from 1% to 100% full buffer. As all waiters are on the same wait queue, the wake up happens for the waiter with the smallest percentage. The problem is that the smallest_full on the cpu_buffer that stores the smallest amount doesn't get reset when all the waiters are woken up. It does get reset when the ring buffer is reset (echo > /sys/kernel/tracing/trace). This means that tasks may be woken up more often then when they want to be. Instead, have the shortest_full field get reset just before waking up all the tasks. If the tasks wait again, they will update the shortest_full before sleeping. Also add locking around setting of shortest_full in the poll logic, and change "work" to "rbwork" to match the variable name for rb_irq_work structures that are used in other places. Link: https://lore.kernel.org/linux-trace-kernel/20240308202431.948914369@goodmis.org Cc: stable@vger.kernel.org Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: linke li <lilinke99@qq.com> Cc: Rabin Vincent <rabin@rab.in> Fixes: 2c2b0a7 ("ring-buffer: Add percentage of ring buffer full to wake up reader") Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent b359457 commit 68282dd

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

kernel/trace/ring_buffer.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,19 @@ static void rb_wake_up_waiters(struct irq_work *work)
755755

756756
wake_up_all(&rbwork->waiters);
757757
if (rbwork->full_waiters_pending || rbwork->wakeup_full) {
758+
/* Only cpu_buffer sets the above flags */
759+
struct ring_buffer_per_cpu *cpu_buffer =
760+
container_of(rbwork, struct ring_buffer_per_cpu, irq_work);
761+
762+
/* Called from interrupt context */
763+
raw_spin_lock(&cpu_buffer->reader_lock);
758764
rbwork->wakeup_full = false;
759765
rbwork->full_waiters_pending = false;
766+
767+
/* Waking up all waiters, they will reset the shortest full */
768+
cpu_buffer->shortest_full = 0;
769+
raw_spin_unlock(&cpu_buffer->reader_lock);
770+
760771
wake_up_all(&rbwork->full_waiters);
761772
}
762773
}
@@ -934,28 +945,33 @@ __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
934945
struct file *filp, poll_table *poll_table, int full)
935946
{
936947
struct ring_buffer_per_cpu *cpu_buffer;
937-
struct rb_irq_work *work;
948+
struct rb_irq_work *rbwork;
938949

939950
if (cpu == RING_BUFFER_ALL_CPUS) {
940-
work = &buffer->irq_work;
951+
rbwork = &buffer->irq_work;
941952
full = 0;
942953
} else {
943954
if (!cpumask_test_cpu(cpu, buffer->cpumask))
944955
return EPOLLERR;
945956

946957
cpu_buffer = buffer->buffers[cpu];
947-
work = &cpu_buffer->irq_work;
958+
rbwork = &cpu_buffer->irq_work;
948959
}
949960

950961
if (full) {
951-
poll_wait(filp, &work->full_waiters, poll_table);
952-
work->full_waiters_pending = true;
962+
unsigned long flags;
963+
964+
poll_wait(filp, &rbwork->full_waiters, poll_table);
965+
966+
raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
967+
rbwork->full_waiters_pending = true;
953968
if (!cpu_buffer->shortest_full ||
954969
cpu_buffer->shortest_full > full)
955970
cpu_buffer->shortest_full = full;
971+
raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
956972
} else {
957-
poll_wait(filp, &work->waiters, poll_table);
958-
work->waiters_pending = true;
973+
poll_wait(filp, &rbwork->waiters, poll_table);
974+
rbwork->waiters_pending = true;
959975
}
960976

961977
/*

0 commit comments

Comments
 (0)