From: Tengda Wu <wutengda@huaweicloud.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
Tengda Wu <wutengda@huaweicloud.com>
Subject: [PATCH v3 2/2] perf/core: Disable context fast-swap for sigtrap events
Date: Thu, 5 Nov 2026 09:30:44 +0000 [thread overview]
Message-ID: <f19b29b90fe1d0126fa8f5cd0e7c8d7da07e5a34.1793870900.git.wutengda@huaweicloud.com> (raw)
In-Reply-To: <cover.1793870900.git.wutengda@huaweicloud.com>
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().
The context fast-swap path was already excluded for events that need
stable task association (inherit + sample read). sigtrap events have
the same requirement: the SIGTRAP is targeted at ctx->task and must not
be delivered after ctx->task has been swapped to another task.
Fix this by also excluding events with attr->sigtrap from the fast-swap
path, i.e. bump nr_no_switch_fast for sigtrap events in
list_add_event()/list_del_event(), and update the comment describing
the invariant accordingly.
Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
---
include/linux/perf_event.h | 6 ++----
kernel/events/core.c | 9 +++++++--
2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index eb3f7bab498c..5cc0fb144e04 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1068,10 +1068,8 @@ struct perf_event_context {
* should be avoided.
*
* Sum (event->pending_work + events with
- * (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)))
- *
- * The SIGTRAP is targeted at ctx->task, as such it won't do changing
- * that until the signal is delivered.
+ * (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) +
+ * events with attr->sigtrap)
*/
local_t nr_no_switch_fast;
};
diff --git a/kernel/events/core.c b/kernel/events/core.c
index ee5c59158b89..d693a14ca50d 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1999,7 +1999,7 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
ctx->nr_user++;
if (event->attr.inherit_stat)
ctx->nr_stat++;
- if (has_inherit_and_sample_read(&event->attr))
+ if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
local_inc(&ctx->nr_no_switch_fast);
if (event->state > PERF_EVENT_STATE_OFF)
@@ -2225,7 +2225,7 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
ctx->nr_user--;
if (event->attr.inherit_stat)
ctx->nr_stat--;
- if (has_inherit_and_sample_read(&event->attr))
+ if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
local_dec(&ctx->nr_no_switch_fast);
list_del_rcu(&event->event_entry);
@@ -3831,6 +3831,11 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)
* SAMPLE_READ events they should be switched
* out using the slow path so that they are
* treated as if they were distinct contexts.
+ *
+ * A context holding a sigtrap event must also
+ * take the slow path, even when nothing is
+ * pending, since the sigtrap delivery relies on
+ * the ctx->task relation as well.
*/
raw_spin_unlock(&next_ctx->lock);
rcu_read_unlock();
--
2.34.1
next prev parent reply other threads:[~2026-11-05 9:31 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 9:30 [PATCH v3 0/2] perf/core: Fix sigtrap task_work races Tengda Wu
2026-11-05 9:30 ` [PATCH v3 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
2026-11-16 9:47 ` Sebastian Andrzej Siewior
2026-11-16 14:56 ` Tengda Wu
2026-11-05 9:30 ` Tengda Wu [this message]
2026-11-16 10:17 ` [PATCH v3 2/2] perf/core: Disable context fast-swap for sigtrap events Sebastian Andrzej Siewior
2026-11-16 15:00 ` Tengda Wu
2026-11-05 9:43 ` [PATCH v3 0/2] perf/core: Fix sigtrap task_work races Tengda Wu
2026-11-05 9:47 ` Tengda Wu
2026-11-16 6:12 ` Tengda Wu
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=f19b29b90fe1d0126fa8f5cd0e7c8d7da07e5a34.1793870900.git.wutengda@huaweicloud.com \
--to=wutengda@huaweicloud.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bigeasy@linutronix.de \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.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®