linux-stable/kernel/trace
Tengda Wu f299353e7c ftrace: Fix potential warning in trace_printk_seq during ftrace_dump
[ Upstream commit 4013aef2ce ]

When calling ftrace_dump_one() concurrently with reading trace_pipe,
a WARN_ON_ONCE() in trace_printk_seq() can be triggered due to a race
condition.

The issue occurs because:

CPU0 (ftrace_dump)                              CPU1 (reader)
echo z > /proc/sysrq-trigger

!trace_empty(&iter)
trace_iterator_reset(&iter) <- len = size = 0
                                                cat /sys/kernel/tracing/trace_pipe
trace_find_next_entry_inc(&iter)
  __find_next_entry
    ring_buffer_empty_cpu <- all empty
  return NULL

trace_printk_seq(&iter.seq)
  WARN_ON_ONCE(s->seq.len >= s->seq.size)

In the context between trace_empty() and trace_find_next_entry_inc()
during ftrace_dump, the ring buffer data was consumed by other readers.
This caused trace_find_next_entry_inc to return NULL, failing to populate
`iter.seq`. At this point, due to the prior trace_iterator_reset, both
`iter.seq.len` and `iter.seq.size` were set to 0. Since they are equal,
the WARN_ON_ONCE condition is triggered.

Move the trace_printk_seq() into the if block that checks to make sure the
return value of trace_find_next_entry_inc() is non-NULL in
ftrace_dump_one(), ensuring the 'iter.seq' is properly populated before
subsequent operations.

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Ingo Molnar <mingo@elte.hu>
Link: https://lore.kernel.org/20250822033343.3000289-1-wutengda@huaweicloud.com
Fixes: d769041f86 ("ring_buffer: implement new locking")
Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2025-09-04 14:05:53 +02:00
..
blktrace.c
bpf_trace.c bpf: Fix WARN() in get_bpf_raw_tp_regs 2025-06-27 11:02:46 +01:00
fgraph.c
ftrace_internal.h
ftrace.c ftrace: Also allocate and copy hash for reading of filter files 2025-08-28 16:21:32 +02:00
Kconfig
Makefile
power-traces.c
preemptirq_delay_test.c tracing: Add MODULE_DESCRIPTION() to preemptirq_delay_test 2024-07-05 09:08:25 +02:00
ring_buffer_benchmark.c
ring_buffer.c ring-buffer: Fix bytes_dropped calculation issue 2025-04-10 14:29:42 +02:00
rpm-traces.c
trace_benchmark.c
trace_benchmark.h
trace_branch.c
trace_clock.c tracing: Use atomic64_inc_return() in trace_clock_counter() 2024-12-14 19:44:53 +01:00
trace_dynevent.c
trace_dynevent.h
trace_entries.h
trace_event_perf.c trace/trace_event_perf: remove duplicate samples on the first tracepoint event 2024-12-14 19:44:28 +01:00
trace_events_filter_test.h
trace_events_filter.c
trace_events_hist.c
trace_events_trigger.c Revert "tracing/trigger: Fix to return error if failed to alloc snapshot" 2024-05-02 16:18:30 +02:00
trace_events.c tracing: Add down_write(trace_event_sem) when adding trace event 2025-08-28 16:21:34 +02:00
trace_export.c
trace_functions_graph.c tracing: Fix use-after-free in print_graph_function_flags during tracer switching 2025-04-10 14:29:43 +02:00
trace_functions.c
trace_hwlat.c
trace_irqsoff.c tracing: Fix use-after-free in print_graph_function_flags during tracer switching 2025-04-10 14:29:43 +02:00
trace_kdb.c
trace_kprobe_selftest.c
trace_kprobe_selftest.h
trace_kprobe.c tracing/kprobe: Make trace_kprobe's module callback called after jump_label update 2025-01-09 13:23:31 +01:00
trace_mmiotrace.c
trace_nop.c
trace_output.c tracing: Remove precision vsnprintf() check from print event 2024-11-08 16:20:43 +01:00
trace_output.h
trace_preemptirq.c
trace_printk.c
trace_probe_tmpl.h
trace_probe.c tracing: Consider the NULL character when validating the event length 2024-11-08 16:20:51 +01:00
trace_probe.h tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols 2024-11-08 16:20:47 +01:00
trace_sched_switch.c
trace_sched_wakeup.c tracing: Fix use-after-free in print_graph_function_flags during tracer switching 2025-04-10 14:29:43 +02:00
trace_selftest_dynamic.c
trace_selftest.c
trace_seq.c
trace_stack.c
trace_stat.c
trace_stat.h
trace_syscalls.c
trace_uprobe.c
trace.c ftrace: Fix potential warning in trace_printk_seq during ftrace_dump 2025-09-04 14:05:53 +02:00
trace.h
tracing_map.c tracing: Fix cmp_entries_dup() to respect sort() comparison rules 2024-12-14 19:44:48 +01:00
tracing_map.h