[PATCH v4 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
From: Tengda Wu
Date: Tue Nov 17 2026 - 06:28:48 EST
The sigtrap handling in __perf_event_overflow() checks
event->pending_work before calling task_work_add(), but only sets
pending_work after the task work has been queued.
This leaves a window where an NMI can interrupt task_work_add()
and re-enter __perf_event_overflow() for the same event. Since
pending_work is still zero, the nested invocation can enqueue
the same pending_task again.
The re-entrant call path looks like this:
__perf_event_overflow()
task_work_add()
kasan_record_aux_stack()
<NMI>
__perf_event_overflow()
task_work_add()
The nested invocation may enqueue pending_task before the
outer invocation does. When the outer invocation resumes, it
inserts the same task work entry again, potentially creating
a cycle in the task work list.
As a result, perf_pending_task() may be executed repeatedly,
leading to unbalanced event reference counting.
Fix this by atomically claiming pending_work with
cmpxchg_acquire() before calling task_work_add(). This ensures
that only one invocation can enqueue the task work, even when
another overflow occurs re-entrantly from an NMI.
If task_work_add() fails, clear the claim with
smp_store_release() to allow a subsequent overflow to retry.
In perf_pending_task(), defer clearing pending_work until
after perf_sigtrap() has completed. Use smp_store_release()
to ensure that a subsequent overflow cannot re-queue the task
work before the previous invocation has finished handling it.
Also use smp_load_acquire() when checking pending_work in
the exclude_kernel warning path to ensure the read is properly
ordered.
Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
Signed-off-by: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
---
kernel/events/core.c | 39 ++++++++++++++++++++++++++-------------
1 file changed, 26 insertions(+), 13 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 614f9f0e6500..857230b023ae 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7775,10 +7775,14 @@ static void perf_pending_task(struct callback_head *head)
*/
rctx = perf_swevent_get_recursion_context();
- if (event->pending_work) {
- event->pending_work = 0;
+ if (smp_load_acquire(&event->pending_work)) {
perf_sigtrap(event);
local_dec(&event->ctx->nr_no_switch_fast);
+ /*
+ * Ensure the pending work is fully handled before allowing
+ * another overflow to queue it. See __perf_event_overflow().
+ */
+ smp_store_release(&event->pending_work, 0);
}
put_event(event);
@@ -10950,16 +10954,25 @@ static int __perf_event_overflow(struct perf_event *event,
notify_mode = in_nmi() ? TWA_NMI_CURRENT : TWA_RESUME;
- if (!event->pending_work &&
- !task_work_add(current, &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));
-
- event->pending_addr = 0;
- if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR))
- event->pending_addr = data->addr;
-
+ /*
+ * Claim pending_work before queueing the task work to prevent
+ * another overflow from queueing it concurrently.
+ */
+ if (cmpxchg_acquire(&event->pending_work, 0, pending_id) == 0) {
+ if (!task_work_add(current, &event->pending_task, notify_mode)) {
+ local_inc(&event->ctx->nr_no_switch_fast);
+ WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount));
+
+ event->pending_addr = 0;
+ if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR))
+ event->pending_addr = data->addr;
+ } else {
+ /*
+ * Publish the failed queueing attempt so
+ * a subsequent overflow can retry.
+ */
+ smp_store_release(&event->pending_work, 0);
+ }
} else if (event->attr.exclude_kernel && valid_sample) {
/*
* Should not be able to return to user space without
@@ -10973,7 +10986,7 @@ static int __perf_event_overflow(struct perf_event *event,
* To approximate progress (with false negatives),
* check 32-bit hash of the current IP.
*/
- WARN_ON_ONCE(event->pending_work != pending_id);
+ WARN_ON_ONCE(smp_load_acquire(&event->pending_work) != pending_id);
}
}
--
2.34.1