* [PATCH v1 1/2] ring-buffer: Use irqsave for the reader lock in ring_buffer_poll_remote
2026-11-30 15:23 [PATCH v1 0/2] ring-buffer: Fix reader_lock deadlocks Vincent Donnefort
@ 2026-11-30 15:23 ` Vincent Donnefort
2026-11-30 15:23 ` [PATCH v1 2/2] ring-buffer: Avoid reader_lock deadlock during panic Vincent Donnefort
1 sibling, 0 replies; 3+ messages in thread
From: Vincent Donnefort @ 2026-11-30 15:23 UTC (permalink / raw)
To: rostedt, mhiramat, linux-trace-kernel
Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort,
Sashiko
Calling rb_wakeups with the reader lock held but interrupts enabled can
lead to a deadlock: the irq_work might run on the same CPU, but will
block when acquiring that very same reader spinlock.
First, rb_wakeups does not even need to be called under the reader lock.
Move the function outside of the reader lock scope.
Second, the reader lock must be acquired with IRQs disabled anyway. Use
the irqsave variant of the spinlock.
Fixes: 2e67fabd8b77 ("ring-buffer: Introduce ring-buffer remotes")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260602171146.2238998-1-vdonnefort%40google.com # patch 8
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
kernel/trace/ring_buffer.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index a5a0adc934fa..9a268361762a 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -6928,14 +6928,19 @@ int ring_buffer_poll_remote(struct trace_buffer *buffer, int cpu)
struct ring_buffer_per_cpu *cpu_buffer;
if (cpu != RING_BUFFER_ALL_CPUS) {
+ bool wakeup;
+
if (!cpumask_test_cpu(cpu, buffer->cpumask))
return -EINVAL;
cpu_buffer = buffer->buffers[cpu];
- guard(raw_spinlock)(&cpu_buffer->reader_lock);
- rb_read_remote_meta_page(cpu_buffer);
- if (rb_num_of_entries(cpu_buffer))
+ scoped_guard(raw_spinlock_irqsave, &cpu_buffer->reader_lock) {
+ rb_read_remote_meta_page(cpu_buffer);
+ wakeup = rb_num_of_entries(cpu_buffer);
+ }
+
+ if (wakeup)
rb_wakeups(buffer, cpu_buffer);
return 0;
@@ -6950,7 +6955,7 @@ int ring_buffer_poll_remote(struct trace_buffer *buffer, int cpu)
for_each_buffer_cpu(buffer, cpu) {
cpu_buffer = buffer->buffers[cpu];
- guard(raw_spinlock)(&cpu_buffer->reader_lock);
+ guard(raw_spinlock_irqsave)(&cpu_buffer->reader_lock);
rb_read_remote_meta_page(cpu_buffer);
}
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH v1 2/2] ring-buffer: Avoid reader_lock deadlock during panic
2026-11-30 15:23 [PATCH v1 0/2] ring-buffer: Fix reader_lock deadlocks Vincent Donnefort
2026-11-30 15:23 ` [PATCH v1 1/2] ring-buffer: Use irqsave for the reader lock in ring_buffer_poll_remote Vincent Donnefort
@ 2026-11-30 15:23 ` Vincent Donnefort
1 sibling, 0 replies; 3+ messages in thread
From: Vincent Donnefort @ 2026-11-30 15:23 UTC (permalink / raw)
To: rostedt, mhiramat, linux-trace-kernel
Cc: mathieu.desnoyers, kernel-team, linux-kernel, Vincent Donnefort,
Sashiko
Although reader_lock is taken with IRQs disabled, panic shutdown can
stop CPUs with NMIs (most likely if the first attempt with maskable
interrupts failed). Those NMIs may stop a CPU while it holds the
reader_lock. Use trylock when panic_on_this_cpu() just like in_nmi().
Fixes: 289a5a25c5d0 ("ring-buffer: Give NMIs a chance to lock the reader_lock")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/linux-trace-kernel/20260924151509.23AEE1F000FF@smtp.kernel.org/
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
kernel/trace/ring_buffer.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 9a268361762a..59ea54990d93 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -22,6 +22,7 @@
#include <linux/kthread.h> /* for self test */
#include <linux/module.h>
#include <linux/percpu.h>
+#include <linux/panic.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/slab.h>
@@ -6323,7 +6324,7 @@ EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
{
- if (likely(!in_nmi())) {
+ if (likely(!in_nmi() && !panic_on_this_cpu())) {
raw_spin_lock(&cpu_buffer->reader_lock);
return true;
}
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread