[tip: perf/urgent] perf: Fix sigtrap for software events
From: tip-bot2 for Peter Zijlstra
Date: Thu Nov 26 2026 - 07:44:52 EST
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 38606d2911f7f2fe80388315da0c4a7fc98af0f8
Gitweb: https://git.kernel.org/tip/38606d2911f7f2fe80388315da0c4a7fc98af0f8
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 for software events
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 nr_no_switch_fast bits hard rely on perf_pmu_disable() actually working. It
turns out the software events do not implement the pmu::pmu_{dis,en}able()
methods and generally ignore the state, making the above scenario worse in that
an overflow can happen in the middle of swapping task contexts, making it
impossible to reconstruct state.
Ensure software and tracepoint events respect the disable state.
Fixes: ca6c21327c6a ("perf: Fix missing SIGTRAPs")
Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
Reported-by: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Tested-by: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
Link: https://patch.msgid.link/20261125161342.GP2669272%40noisy.programming.kicks-ass.net
---
include/linux/perf_event.h | 1 +
kernel/events/core.c | 12 ++++++++++++
2 files changed, 13 insertions(+)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index e1e1150..b52348e 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1236,6 +1236,7 @@ extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
extern void perf_event_print_debug(void);
extern void perf_pmu_disable(struct pmu *pmu);
extern void perf_pmu_enable(struct pmu *pmu);
+extern bool perf_pmu_enabled(struct pmu *pmu);
extern void perf_sched_cb_dec(struct pmu *pmu);
extern void perf_sched_cb_inc(struct pmu *pmu);
extern int perf_event_task_disable(void);
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 5925752..10dc3d0 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1366,6 +1366,12 @@ void perf_pmu_enable(struct pmu *pmu)
pmu->pmu_enable(pmu);
}
+bool perf_pmu_enabled(struct pmu *pmu)
+{
+ int *count = &this_cpc(pmu)->pmu_disable_count;
+ return *count == 0;
+}
+
static void perf_assert_pmu_disabled(struct pmu *pmu)
{
int *count = &this_cpc(pmu)->pmu_disable_count;
@@ -11159,6 +11165,9 @@ static int perf_swevent_match(struct perf_event *event,
u32 event_id,
struct pt_regs *regs)
{
+ if (!perf_pmu_enabled(event->pmu))
+ return 0;
+
if (event->attr.type != type)
return 0;
@@ -11537,6 +11546,9 @@ static int perf_tp_event_match(struct perf_event *event,
struct perf_raw_record *raw,
struct pt_regs *regs)
{
+ if (!perf_pmu_enabled(event->pmu))
+ return 0;
+
if (event->hw.state & PERF_HES_STOPPED)
return 0;
/*