linux-stable/kernel/trace
Tengda Wu a6f0f8873c 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:28:44 +02:00
..
blktrace.c
bpf_trace.c bpf: Fix WARN() in get_bpf_raw_tp_regs 2025-06-27 11:05:13 +01:00
bpf_trace.h
error_report-traces.c
fgraph.c
ftrace_internal.h
ftrace.c ftrace: Also allocate and copy hash for reading of filter files 2025-08-28 16:24:34 +02:00
Kconfig
kprobe_event_gen_test.c
Makefile
pid_list.c
pid_list.h
power-traces.c
preemptirq_delay_test.c kernel: trace: preemptirq_delay_test: use offstack cpu mask 2025-08-28 16:24:10 +02:00
ring_buffer_benchmark.c
ring_buffer.c ring-buffer: Fix bytes_dropped calculation issue 2025-04-10 14:32:02 +02:00
rpm-traces.c
synth_event_gen_test.c
trace_benchmark.c
trace_benchmark.h
trace_boot.c
trace_branch.c
trace_clock.c tracing: Use atomic64_inc_return() in trace_clock_counter() 2024-12-14 19:51:40 +01:00
trace_dynevent.c tracing: probes: Fix a possible race in trace_probe_log APIs 2025-05-22 14:08:21 +02:00
trace_dynevent.h tracing: probes: Fix a possible race in trace_probe_log APIs 2025-05-22 14:08:21 +02:00
trace_entries.h
trace_eprobe.c tracing/eprobe: Fix to release eprobe when failed to add dyn_event 2024-12-14 19:51:44 +01:00
trace_event_perf.c trace/trace_event_perf: remove duplicate samples on the first tracepoint event 2024-12-14 19:51:00 +01:00
trace_events_filter_test.h
trace_events_filter.c tracing: Fix filter string testing 2025-05-02 07:44:13 +02:00
trace_events_hist.c
trace_events_inject.c
trace_events_synth.c tracing: Do not use PERF enums when perf is not defined 2025-04-10 14:32:07 +02:00
trace_events_trigger.c ftrace: Fix preemption accounting for stacktrace trigger command 2025-05-22 14:08:25 +02:00
trace_events.c tracing: Add down_write(trace_event_sem) when adding trace event 2025-08-28 16:23:59 +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:32:06 +02:00
trace_functions.c ftrace: Fix preemption accounting for stacktrace filter command 2025-05-22 14:08:25 +02:00
trace_hwlat.c tracing/hwlat: Fix a race during cpuhp processing 2024-10-17 15:11:42 +02:00
trace_irqsoff.c tracing: Fix use-after-free in print_graph_function_flags during tracer switching 2025-04-10 14:32:06 +02:00
trace_kdb.c
trace_kprobe_selftest.c
trace_kprobe_selftest.h
trace_kprobe.c tracing: probes: Fix a possible race in trace_probe_log APIs 2025-05-22 14:08:21 +02:00
trace_mmiotrace.c
trace_nop.c
trace_osnoise.c tracing/osnoise: Fix possible recursive locking for cpus_read_lock() 2025-04-10 14:32:06 +02:00
trace_output.c tracing: Remove precision vsnprintf() check from print event 2024-10-17 15:11:48 +02:00
trace_output.h
trace_preemptirq.c
trace_printk.c
trace_probe_kernel.h
trace_probe_tmpl.h
trace_probe.c tracing: probes: Fix a possible race in trace_probe_log APIs 2025-05-22 14:08:21 +02:00
trace_probe.h
trace_recursion_record.c
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:32:06 +02:00
trace_selftest_dynamic.c
trace_selftest.c
trace_seq.c
trace_stack.c
trace_stat.c
trace_stat.h
trace_synth.h
trace_syscalls.c
trace_uprobe.c tracing: probes: Fix a possible race in trace_probe_log APIs 2025-05-22 14:08:21 +02:00
trace.c ftrace: Fix potential warning in trace_printk_seq during ftrace_dump 2025-09-04 14:28:44 +02:00
trace.h tracing: Limit access to parser->buffer when trace_get_user failed 2025-08-28 16:24:37 +02:00
tracing_map.c tracing: Fix cmp_entries_dup() to respect sort() comparison rules 2024-12-14 19:51:31 +01:00
tracing_map.h