[tip: perf/urgent] perf: Fix sigtrap vs fast-switch
From: tip-bot2 for Peter Zijlstra
Date: Thu Nov 26 2026 - 07:44:49 EST
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 34dc983cc1628dea5054baa8687477cbb8683e28
Gitweb: https://git.kernel.org/tip/34dc983cc1628dea5054baa8687477cbb8683e28
Author: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
AuthorDate: Thu, 19 Nov 2026 09:51:01
Committer: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
CommitterDate: Thu, 26 Nov 2026 13:41:02 +01:00
perf: Fix sigtrap vs fast-switch
perf_sigtrap() contains:
WARN_ON_ONCE(event->ctx->task != current)
The warning assumes that an event's ctx->task always matches the task
that ends up running the pending task_work. That assumption can be
broken by the context fast-swap path.
The failure sequence is:
1. A -> B switch, perf_event_context_sched_out() takes the fast path
and does WRITE_ONCE(ctx->task, next) / WRITE_ONCE(next_ctx->task, task).
An NMI then hits while the context is swapped, and
__perf_event_overflow() queues pending task_work on the *current*
task (A), even though the event now belongs to B's swapped-in
context view.
2. B -> A switch. Because A now has pending task_work,
nr_no_switch_fast is non-zero, so the slow path is taken and
ctx->task is *not* swapped back.
3. A returns to user mode, task_work runs perf_pending_task() ->
perf_sigtrap(), and event->ctx->task no longer equals current,
tripping the WARN_ON_ONCE().
Therefore; the task_work_add() should in fact use event->ctx->task, rather than
current. They are always the same, except in this narrow window between
perf_event_task_sched_out() and switch_to().
Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
Reported-by: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
Tested-by: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Link: https://patch.msgid.link/20261125161342.GP2669272%40noisy.programming.kicks-ass.net
---
kernel/events/core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 10dc3d0..08f0e0f 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -10970,7 +10970,7 @@ static int __perf_event_overflow(struct perf_event *event,
* claim the pending_work before task_work_add() recurses.
*/
if (cmpxchg_local(&event->pending_work, 0, 1) == 0) {
- if (!task_work_add(current, &event->pending_task, notify_mode)) {
+ if (!task_work_add(event->ctx->task, &event->pending_task, notify_mode)) {
event->pending_work = pending_id;
local_inc(&event->ctx->nr_no_switch_fast);
WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount));