[PATCH v7 2/2] ftrace: do CPU checking after preemption disabled

王贇 yun.wang at linux.alibaba.com
Tue Oct 26 20:15:11 PDT 2021

With CONFIG_DEBUG_PREEMPT we observed reports like:

  BUG: using smp_processor_id() in preemptible
  caller is perf_ftrace_function_call+0x6f/0x2e0
  CPU: 1 PID: 680 Comm: a.out Not tainted
  Call Trace:
   ? optimize_nops.isra.7+0x230/0x230
   ? text_poke_bp_batch+0x9f/0x310

This telling us the CPU could be changed after task is preempted, and
the checking on CPU before preemption will be invalid.

Since now ftrace_test_recursion_trylock() will help to disable the
preemption, this patch just do the checking after trylock() to address
the issue.

CC: Steven Rostedt <rostedt at goodmis.org>
Reported-by: Abaci <abaci at linux.alibaba.com>
Signed-off-by: Michael Wang <yun.wang at linux.alibaba.com>
 kernel/trace/trace_event_perf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
index 6aed10e..fba8cb7 100644
--- a/kernel/trace/trace_event_perf.c
+++ b/kernel/trace/trace_event_perf.c
@@ -441,13 +441,13 @@ void perf_trace_buf_update(void *record, u16 type)
 	if (!rcu_is_watching())

-	if ((unsigned long)ops->private != smp_processor_id())
-		return;
 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
 	if (bit < 0)

+	if ((unsigned long)ops->private != smp_processor_id())
+		goto out;
 	event = container_of(ops, struct perf_event, ftrace_ops);


More information about the linux-riscv mailing list