This is a note to let you know that I've just added the patch titled ring-buffer: Add ring_buffer_wake_waiters() to the 5.19-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: ring-buffer-add-ring_buffer_wake_waiters.patch and it can be found in the queue-5.19 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From 7e9fbbb1b776d8d7969551565bc246f74ec53b27 Mon Sep 17 00:00:00 2001 From: "Steven Rostedt (Google)" <rostedt@xxxxxxxxxxx> Date: Wed, 28 Sep 2022 13:39:38 -0400 Subject: ring-buffer: Add ring_buffer_wake_waiters() From: Steven Rostedt (Google) <rostedt@xxxxxxxxxxx> commit 7e9fbbb1b776d8d7969551565bc246f74ec53b27 upstream. On closing of a file that represents a ring buffer or flushing the file, there may be waiters on the ring buffer that needs to be woken up and exit the ring_buffer_wait() function. Add ring_buffer_wake_waiters() to wake up the waiters on the ring buffer and allow them to exit the wait loop. Link: https://lkml.kernel.org/r/20220928133938.28dc2c27@xxxxxxxxxxxxxxxxxx Cc: stable@xxxxxxxxxxxxxxx Cc: Ingo Molnar <mingo@xxxxxxxxxx> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> Fixes: 15693458c4bc0 ("tracing/ring-buffer: Move poll wake ups into ring buffer code") Signed-off-by: Steven Rostedt (Google) <rostedt@xxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- include/linux/ring_buffer.h | 2 +- kernel/trace/ring_buffer.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) --- a/include/linux/ring_buffer.h +++ b/include/linux/ring_buffer.h @@ -101,7 +101,7 @@ __ring_buffer_alloc(unsigned long size, int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full); __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu, struct file *filp, poll_table *poll_table); - +void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu); #define RING_BUFFER_ALL_CPUS -1 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -413,6 +413,7 @@ struct rb_irq_work { struct irq_work work; wait_queue_head_t waiters; wait_queue_head_t full_waiters; + long wait_index; bool waiters_pending; bool full_waiters_pending; bool wakeup_full; @@ -925,6 +926,37 @@ static void rb_wake_up_waiters(struct ir } /** + * ring_buffer_wake_waiters - wake up any waiters on this ring buffer + * @buffer: The ring buffer to wake waiters on + * + * In the case of a file that represents a ring buffer is closing, + * it is prudent to wake up any waiters that are on this. + */ +void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + struct rb_irq_work *rbwork; + + if (cpu == RING_BUFFER_ALL_CPUS) { + + /* Wake up individual ones too. One level recursion */ + for_each_buffer_cpu(buffer, cpu) + ring_buffer_wake_waiters(buffer, cpu); + + rbwork = &buffer->irq_work; + } else { + cpu_buffer = buffer->buffers[cpu]; + rbwork = &cpu_buffer->irq_work; + } + + rbwork->wait_index++; + /* make sure the waiters see the new index */ + smp_wmb(); + + rb_wake_up_waiters(&rbwork->work); +} + +/** * ring_buffer_wait - wait for input to the ring buffer * @buffer: buffer to wait on * @cpu: the cpu buffer to wait on @@ -939,6 +971,7 @@ int ring_buffer_wait(struct trace_buffer struct ring_buffer_per_cpu *cpu_buffer; DEFINE_WAIT(wait); struct rb_irq_work *work; + long wait_index; int ret = 0; /* @@ -957,6 +990,7 @@ int ring_buffer_wait(struct trace_buffer work = &cpu_buffer->irq_work; } + wait_index = READ_ONCE(work->wait_index); while (true) { if (full) @@ -1021,6 +1055,11 @@ int ring_buffer_wait(struct trace_buffer } schedule(); + + /* Make sure to see the new wait index */ + smp_rmb(); + if (wait_index != work->wait_index) + break; } if (full) Patches currently in stable-queue which might be from rostedt@xxxxxxxxxxx are queue-5.19/tracing-fix-reading-strings-from-synthetic-events.patch queue-5.19/tracing-add-ioctl-to-force-ring-buffer-waiters-to-wake-up.patch queue-5.19/ring-buffer-have-the-shortest_full-queue-be-the-shortest-not-longest.patch queue-5.19/ring-buffer-add-ring_buffer_wake_waiters.patch queue-5.19/tracing-move-duplicate-code-of-trace_kprobe-eprobe.c-into-header.patch queue-5.19/tracing-add-fault-name-injection-to-kernel-probes.patch queue-5.19/ring-buffer-fix-race-between-reset-page-and-reading-page.patch queue-5.19/tracing-wake-up-ring-buffer-waiters-on-closing-of-the-file.patch queue-5.19/ftrace-still-disable-enabled-records-marked-as-disabled.patch queue-5.19/tracing-disable-interrupt-or-preemption-before-acquiring-arch_spinlock_t.patch queue-5.19/tracing-do-not-free-snapshot-if-tracer-is-on-cmdline.patch queue-5.19/tracing-wake-up-waiters-when-tracing-is-disabled.patch queue-5.19/ring-buffer-allow-splice-to-read-previous-partially-read-pages.patch queue-5.19/ring-buffer-check-pending-waiters-when-doing-wake-ups-as-well.patch queue-5.19/ftrace-properly-unset-ftrace_hash_fl_mod.patch