From: "tip-bot2 for Peter Zijlstra" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Tengda Wu <wutengda@huaweicloud.com>,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: perf/urgent] perf: Fix sigtrap vs fast-switch
Date: Thu, 26 Nov 2026 12:44:40 -0000 [thread overview]
Message-ID: <179569708042.92412.17777095770950546772.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20261125161342.GP2669272@noisy.programming.kicks-ass.net>
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@infradead.org>
AuthorDate: Thu, 19 Nov 2026 09:51:01
Committer: Peter Zijlstra <peterz@infradead.org>
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@huaweicloud.com>
Tested-by: Tengda Wu <wutengda@huaweicloud.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
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));
next prev parent reply other threads:[~2026-11-26 12:44 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 9:50 [PATCH v5 0/2] perf/core: Fix sigtrap task_work races Tengda Wu
2026-11-19 9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
2026-11-19 12:09 ` Sebastian Andrzej Siewior
2026-11-20 12:14 ` Peter Zijlstra
2026-11-20 14:16 ` Sebastian Andrzej Siewior
2026-11-20 15:04 ` Peter Zijlstra
2026-11-20 15:10 ` Sebastian Andrzej Siewior
2026-11-20 15:24 ` Peter Zijlstra
2026-11-20 15:44 ` Peter Zijlstra
2026-11-20 15:49 ` Sebastian Andrzej Siewior
2026-11-20 16:01 ` Peter Zijlstra
2026-11-20 16:26 ` Peter Zijlstra
2026-11-20 17:28 ` Sebastian Andrzej Siewior
2026-11-20 17:49 ` Peter Zijlstra
2026-11-23 2:10 ` Tengda Wu
2026-11-23 8:00 ` Sebastian Andrzej Siewior
2026-11-25 8:27 ` Peter Zijlstra
2026-11-25 10:55 ` Sebastian Andrzej Siewior
2026-11-25 16:17 ` Peter Zijlstra
2026-11-25 16:45 ` Sebastian Andrzej Siewior
2026-11-23 1:17 ` Tengda Wu
2026-11-23 7:54 ` Sebastian Andrzej Siewior
2026-11-19 9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
2026-11-19 12:11 ` Sebastian Andrzej Siewior
2026-11-19 12:17 ` Tengda Wu
2026-11-19 13:07 ` Sebastian Andrzej Siewior
2026-11-19 13:19 ` Tengda Wu
2026-11-19 13:48 ` Sebastian Andrzej Siewior
2026-11-20 0:55 ` Tengda Wu
2026-11-20 16:30 ` Peter Zijlstra
2026-11-20 17:06 ` Sebastian Andrzej Siewior
2026-11-20 17:37 ` Peter Zijlstra
2026-11-20 17:46 ` Peter Zijlstra
2026-11-23 3:11 ` Tengda Wu
2026-11-24 8:42 ` Tengda Wu
2026-11-25 9:08 ` Peter Zijlstra
2026-11-25 9:29 ` Tengda Wu
2026-11-25 9:38 ` Peter Zijlstra
2026-11-25 10:03 ` Tengda Wu
2026-11-25 10:09 ` Tengda Wu
2026-11-25 13:36 ` Peter Zijlstra
2026-11-25 16:13 ` Peter Zijlstra
2026-11-26 1:59 ` Tengda Wu
2026-11-26 11:01 ` Peter Zijlstra
2026-11-26 12:44 ` tip-bot2 for Peter Zijlstra [this message]
2026-11-26 12:44 ` [tip: perf/urgent] perf: Fix sigtrap for software events tip-bot2 for Peter Zijlstra
2026-11-25 9:30 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179569708042.92412.17777095770950546772.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=wutengda@huaweicloud.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®