[PATCH v2] trace: prevent preemption in perf_ftrace_function_call()

From: 王贇
Date: Fri Sep 24 2021 - 00:38:49 EST


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:
<TASK>
dump_stack_lvl+0x8d/0xcf
check_preemption_disabled+0x104/0x110
? optimize_nops.isra.7+0x230/0x230
? text_poke_bp_batch+0x9f/0x310
perf_ftrace_function_call+0x6f/0x2e0
...
__text_poke+0x5/0x620
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.

This patch just turn off preemption in perf_ftrace_function_call()
to prevent CPU changing.

Reported-by: Abaci <abaci@xxxxxxxxxxxxxxxxx>
Signed-off-by: Michael Wang <yun.wang@xxxxxxxxxxxxxxxxx>
---
kernel/trace/trace_event_perf.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

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

+ /*
+ * Prevent CPU changing from now on. rcu must
+ * be in watching if the task was migrated and
+ * scheduled.
+ */
+ preempt_disable_notrace();
+
if ((unsigned long)ops->private != smp_processor_id())
- return;
+ goto out;

bit = ftrace_test_recursion_trylock(ip, parent_ip);
if (bit < 0)
- return;
+ goto out;

event = container_of(ops, struct perf_event, ftrace_ops);

@@ -468,16 +475,18 @@ void perf_trace_buf_update(void *record, u16 type)

entry = perf_trace_buf_alloc(ENTRY_SIZE, NULL, &rctx);
if (!entry)
- goto out;
+ goto unlock;

entry->ip = ip;
entry->parent_ip = parent_ip;
perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, TRACE_FN,
1, &regs, &head, NULL);

-out:
+unlock:
ftrace_test_recursion_unlock(bit);
#undef ENTRY_SIZE
+out:
+ preempt_enable_notrace();
}

static int perf_ftrace_function_register(struct perf_event *event)
--
1.8.3.1