[PATCH v4 0/2] perf/core: Fix sigtrap task_work races

From: Tengda Wu

Date: Tue Nov 17 2026 - 06:28:43 EST


Two fixes for sigtrap task_work handling since the switch to always
enqueue SIGTRAP via task_work.

The warning splats and PoCs for both patches are too long to inline
in the commit messages, so they are provided as separate replies at
the following links:

Patch 1: https://lore.kernel.org/all/29aa123c-36a2-460f-b76b-a8dfe1eb542f@xxxxxxxxxxxxxxx/
Patch 2: https://lore.kernel.org/all/d1b24bbf-9111-4766-87c8-ff97adcac727@xxxxxxxxxxxxxxx/

Patch 1 fixes a re-entrancy race in __perf_event_overflow(): pending_work
was set only after task_work_add(), so an NMI could re-enter and enqueue
the same pending_task twice, creating a cycle in the task work list and
unbalancing the event refcount. Claim pending_work before enqueuing, and
clear it if the enqueue fails.

Patch 2 fixes a WARN_ON_ONCE() in perf_sigtrap() triggered by the context
fast-swap path swapping ctx->task while a sigtrap event has pending task
work. SIGTRAP is targeted at ctx->task, so exclude sigtrap events from
fast-swap alongside inherit+sample-read events.

Changelog
=========
v3 -> v4:
- v3: https://lore.kernel.org/all/cover.1793870900.git.wutengda@xxxxxxxxxxxxxxx/
- Patch 1: Use acquire/release pairs for pending_work updates (Sebastian)
- Patch 2: Remove redundant nr_no_switch_fast updates (Sebastian)
v2 -> v3:
- v2: https://lore.kernel.org/all/20261102021355.650671-1-wutengda@xxxxxxxxxxxxxxx/
- Added Patch 2 to fix WARN_ON_ONCE() in perf_sigtrap()
v1 -> v2:
- v1: https://lore.kernel.org/all/20261031114520.642476-1-wutengda@xxxxxxxxxxxxxxx/
- Drop the redundant event->pending_work assignment. (Sashiko)

Tengda Wu (2):
perf/core: Prevent duplicate sigtrap task work enqueue
perf/core: Disable context fast-swap for sigtrap events

include/linux/perf_event.h | 8 +++----
kernel/events/core.c | 48 +++++++++++++++++++++++++-------------
2 files changed, 35 insertions(+), 21 deletions(-)

--
2.34.1