Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vincent Donnefort" <vdonnefort@google.com>,
	linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH v10 14/19] tracing/remotes: Add dump_on_panic tracefs file
Date: Thu, 03 Dec 2026 19:52:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-248568@kernel.org> (raw)
In-Reply-To: <20261203190007.2431041-15-vdonnefort@google.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] tracing/remotes: deadlock on reader_lock in
  trace_remote_panic_handler()
- [Medium] tracing/remotes: hotplugged CPUs omitted from
  trace_remote_panic_handler()
- [Medium] tracing/remotes: unbounded dump loop in
  trace_remote_panic_handler()
- [Medium] ring-buffer: IRQ-unsafe reader_lock in
  ring_buffer_poll_remote()
--

commit 2b2612882df3895db81d737752cd2225ff3430b1
Author: Vincent Donnefort <vdonnefort@google.com>

tracing/remotes: Add dump_on_panic tracefs file

This commit adds a dump_on_panic tracefs attribute for trace remotes that
registers a panic notifier and pre-allocates a non-consuming iterator to
dump remote ring buffer contents via pr_emerg() when the system panics.

> diff --git a/kernel/trace/trace_remote.c b/kernel/trace/trace_remote.c
> index c9d2e8731fdc0..a317a417207d8 100644
> --- a/kernel/trace/trace_remote.c
> +++ b/kernel/trace/trace_remote.c

[ ... ]

> @@ -990,6 +1030,113 @@ static int dmesg_show(struct seq_file *s, void *unused)
>  }
>  DEFINE_TRACE_REMOTE_ATTRIBUTE(dmesg);
>  
> +static int trace_remote_panic_handler(struct notifier_block *self, unsigned long ev, void *v)
> +{
> +	struct trace_remote *remote = container_of(self, struct trace_remote, panic_notifier);
> +	struct trace_remote_iterator *iter = smp_load_acquire(&remote->panic_iter);
> +	int cpu;
> +
> +	if (!iter) {
> +		pr_warn("Unexpected error: no panic iterator for the trace remote\n");
> +		return NOTIFY_DONE;
> +	}
> +
> +	for_each_possible_cpu(cpu)
> +		ring_buffer_iter_reset(iter->rb_iters[cpu]);

[Severity: Medium]
Can trace_remote_panic_handler() deadlock on cpu_buffer->reader_lock during
a non-NMI panic?

vpanic() halts all other CPUs via panic_other_cpus_shutdown() before
calling panic_notifier_list.  ring_buffer_iter_reset(),
trace_remote_iter_read_event(), and trace_remote_iter_move() all acquire
cpu_buffer->reader_lock via rb_reader_lock(), which only falls back to
raw_spin_trylock() when in_nmi() is true and otherwise spins
unconditionally without checking panic_in_progress().

CPU0                              CPU1
trace_pipe_read()
  ...
    raw_spin_lock(&reader_lock)
                                  vpanic()
                                    panic_other_cpus_shutdown()
<halted while holding lock>
                                    atomic_notifier_call_chain()
                                      trace_remote_panic_handler()
                                        ring_buffer_iter_reset()
                                          rb_reader_lock()
                                            raw_spin_lock(&reader_lock)
                                              <-- spins forever

The panicking CPU can also self-deadlock if a hypervisor panic occurs while
it holds reader_lock in hyp_trace_swap_reader_page().  Because this
notifier runs at priority INT_MAX - 1, a deadlock hangs the panic path
before remaining panic notifiers, kmsg_dump_desc(), or automatic reboot
can run.

[Severity: Medium]
Does ring_buffer_poll_remote() need raw_spinlock_irqsave for
cpu_buffer->reader_lock now that trace_remote_panic_handler() acquires it
in panic context?

Every other acquisition of cpu_buffer->reader_lock in
kernel/trace/ring_buffer.c disables IRQs (including rb_wake_up_waiters()
in hardirq context), but ring_buffer_poll_remote() uses
guard(raw_spinlock)(&cpu_buffer->reader_lock).

ring_buffer_poll_remote()
  guard(raw_spinlock)(&cpu_buffer->reader_lock)
  <hardirq>
    vpanic()
      atomic_notifier_call_chain()
        trace_remote_panic_handler()
          ring_buffer_iter_reset()
            rb_reader_lock()
              raw_spin_lock(&cpu_buffer->reader_lock)  <-- spins forever

With IRQs left enabled, a hardirq panic (such as sysrq-c) preempting
ring_buffer_poll_remote() self-deadlocks on the same CPU, and a non-NMI
stop IPI can halt the polling CPU while it holds reader_lock.

> +
> +	while (trace_remote_iter_read_event(iter)) {
> +		trace_seq_init(&iter->seq);
> +
> +		trace_remote_iter_print_event(iter);
> +		trace_seq_putc(&iter->seq, 0);
> +		pr_emerg("%s", seq_buf_str(&iter->seq.seq));
> +
> +		trace_remote_iter_move(iter);
> +		touch_nmi_watchdog();
> +	}

[Severity: Medium]
Is this dump loop bounded if the remote producer keeps writing during the
panic?

Unlike ftrace_dump_one(), trace_remote_panic_handler() does not stop the
remote writer before dumping, and ring_buffer_iter_empty() re-reads the
live commit offset from the shared page on each iteration.  If an
independent firmware or coprocessor remote continues writing (or an EL2
hypervisor logs console traps from pr_emerg()) and overwrites
iter->head_page, rb_iter_head_event() resets iter->head to 0 with the new
page timestamp so rb_iter_peek() keeps reading indefinitely.

Because touch_nmi_watchdog() runs on every iteration, an active writer can
livelock the panic path and suppress watchdog recovery, preventing
kmsg_dump_desc() and automatic reboot from completing.  Stopping the remote
writer or bounding the dump to the current ring buffer size would avoid
this.

> +
> +	return NOTIFY_DONE;
> +}
> +
> +static int trace_remote_panic_load(struct trace_remote *remote)
> +{
> +	struct notifier_block *notifier = &remote->panic_notifier;
> +	struct trace_remote_iterator *iter;
> +
> +	lockdep_assert_held(&remote->lock);
> +
> +	if (remote->panic_iter)
> +		return 0;
> +
> +	iter = trace_remote_iter(remote, RING_BUFFER_ALL_CPUS, TRI_PANIC);
> +	if (IS_ERR(iter))
> +		return PTR_ERR(iter);

[Severity: Medium]
Will events from CPUs hotplugged online after trace_remote_panic_load() be
silently omitted from the panic dump?

trace_remote_panic_load() allocates remote->panic_iter once for the loaded
lifetime of the remote.  In __alloc_ring_buffer_iter(),
ring_buffer_read_start() returns NULL for any CPU that is currently offline
because it is not yet in buffer->cpumask, leaving iter->rb_iters[cpu] as
NULL.  When that CPU is later brought online, trace_rb_cpu_prepare()
allocates its ring buffer and adds it to buffer->cpumask, but nothing
populates remote->panic_iter->rb_iters[cpu].

On panic, __peek_event() sees a NULL iterator and skips that CPU, losing
all trace events recorded on the newly onlined CPU.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203190007.2431041-1-vdonnefort@google.com?part=14

  reply	other threads:[~2026-12-03 19:52 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:59 [PATCH v10 00/19] tracing/remotes: Add printk, dump_on_panic and boot parameters Vincent Donnefort
2026-12-03 18:59 ` [PATCH v10 01/19] tracing/remotes: Gate tracefs files opening on trace remote registration Vincent Donnefort
2026-12-03 19:20   ` sashiko-bot
2026-12-03 18:59 ` [PATCH v10 02/19] tracing/remotes: Release tracefs,eventfs on registration failure Vincent Donnefort
2026-12-03 19:24   ` sashiko-bot
2026-12-03 18:59 ` [PATCH v10 03/19] tracing/remotes: Use kstrtobool for boolean tracefs files Vincent Donnefort
2026-12-03 18:59 ` [PATCH v10 04/19] tracing/remotes: Initialise pos in trace_start() Vincent Donnefort
2026-12-03 19:18   ` sashiko-bot
2026-12-03 18:59 ` [PATCH v10 05/19] tracing/remotes: Annotate remote shared fields with READ_ONCE/WRITE_ONCE Vincent Donnefort
2026-12-03 19:19   ` sashiko-bot
2026-12-03 18:59 ` [PATCH v10 06/19] tracing/remotes: Use a single per-remote polling work Vincent Donnefort
2026-12-03 18:59 ` [PATCH v10 07/19] tracing/simple_ring_buffer: Add support for compressed length Vincent Donnefort
2026-12-03 18:59 ` [PATCH v10 08/19] tracing/remotes: Add dmesg tracefs file Vincent Donnefort
2026-12-03 19:43   ` sashiko-bot
2026-12-03 18:59 ` [PATCH v10 09/19] tracing/remotes: selftests: Add a test for the " Vincent Donnefort
2026-12-03 19:39   ` sashiko-bot
2026-12-03 18:59 ` [PATCH v10 10/19] tracing/remotes: selftests: Prefix hypervisor folder Vincent Donnefort
2026-12-03 18:59 ` [PATCH v10 11/19] ring-buffer: Use panic-friendly locking in ring_buffer_iter interface Vincent Donnefort
2026-12-03 19:38   ` sashiko-bot
2026-12-03 19:00 ` [PATCH v10 12/19] ring-buffer: Add kerneldoc for ring_buffer_poll_remote Vincent Donnefort
2026-12-03 19:00 ` [PATCH v10 13/19] tracing/remotes: Use ring_buffer_capacity() in trace_remote_has_cpu() Vincent Donnefort
2026-12-03 19:00 ` [PATCH v10 14/19] tracing/remotes: Add dump_on_panic tracefs file Vincent Donnefort
2026-12-03 19:52   ` sashiko-bot [this message]
2026-12-03 19:00 ` [PATCH v10 15/19] tracing/remotes: selftests: Add a test for the " Vincent Donnefort
2026-12-03 19:50   ` sashiko-bot
2026-12-03 19:00 ` [PATCH v10 16/19] tracing/remotes: Add poll_ms " Vincent Donnefort
2026-12-03 19:50   ` sashiko-bot
2026-12-03 19:00 ` [PATCH v10 17/19] tracing/remotes: Add trace_remote cmdline options Vincent Donnefort
2026-12-03 19:58   ` sashiko-bot
2026-12-03 19:00 ` [PATCH v10 18/19] Documentation: tracing/remotes: Add detailed tracefs layout Vincent Donnefort
2026-12-03 19:48   ` sashiko-bot
2026-12-03 19:00 ` [PATCH v10 19/19] Documentation/kernel-parameters: Add trace_remote Vincent Donnefort
2026-12-03 19:53   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-248568@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vdonnefort@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox