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: 2c2b0a78b3739 ("ring-buffer: Add percentage of ring buffer full to wake up reader")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Changed files
+23 -7
kernel
+23 -7
kernel/trace/ring_buffer.c
··· 755 755 756 756 wake_up_all(&rbwork->waiters); 757 757 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); 758 764 rbwork->wakeup_full = false; 759 765 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 + 760 771 wake_up_all(&rbwork->full_waiters); 761 772 } 762 773 } ··· 945 934 struct file *filp, poll_table *poll_table, int full) 946 935 { 947 936 struct ring_buffer_per_cpu *cpu_buffer; 948 - struct rb_irq_work *work; 937 + struct rb_irq_work *rbwork; 949 938 950 939 if (cpu == RING_BUFFER_ALL_CPUS) { 951 - work = &buffer->irq_work; 940 + rbwork = &buffer->irq_work; 952 941 full = 0; 953 942 } else { 954 943 if (!cpumask_test_cpu(cpu, buffer->cpumask)) 955 944 return EPOLLERR; 956 945 957 946 cpu_buffer = buffer->buffers[cpu]; 958 - work = &cpu_buffer->irq_work; 947 + rbwork = &cpu_buffer->irq_work; 959 948 } 960 949 961 950 if (full) { 962 - poll_wait(filp, &work->full_waiters, poll_table); 963 - work->full_waiters_pending = true; 951 + unsigned long flags; 952 + 953 + poll_wait(filp, &rbwork->full_waiters, poll_table); 954 + 955 + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); 956 + rbwork->full_waiters_pending = true; 964 957 if (!cpu_buffer->shortest_full || 965 958 cpu_buffer->shortest_full > full) 966 959 cpu_buffer->shortest_full = full; 960 + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); 967 961 } else { 968 - poll_wait(filp, &work->waiters, poll_table); 969 - work->waiters_pending = true; 962 + poll_wait(filp, &rbwork->waiters, poll_table); 963 + rbwork->waiters_pending = true; 970 964 } 971 965 972 966 /*