Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
From: Tengda Wu
Date: Sun Nov 22 2026 - 21:11:03 EST
On 2026/11/21 1:49, Peter Zijlstra wrote:
> On Fri, Nov 20, 2026 at 06:28:31PM +0100, Sebastian Andrzej Siewior wrote:
>> On 2026-11-20 17:26:04 [+0100], Peter Zijlstra wrote:
>>> Also, sending signals on software events seems utterly insane. But I
>>> don't support we can go limit that now :-(
>>
>> Tengda: Is this an actual valid use case that you have here or did an AI
>> figured out that this broken?
>
> I *think* the below is sufficient.
>
> - sigtrap implies a per-task event; task_work is local to the current
> task (+- the context switch thing from 2/2).
>
> - task_work_run() is written such that the work handler can do
> task_work_add(), therefore the early clear in the handler doesn't
> matter.
>
> And while recursive software events are tedious, I think the below fix
> is simple enough to not worry about too much.
>
> ---
> Subject: perf/core: Prevent duplicate sigtrap task work enqueue
> From: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
> Date: Thu, 19 Nov 2026 09:51:00 +0000
>
> From: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
>
> 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 (software/tracepoint)
> 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()
> <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_local()
> 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.
>
> Notably: sigtrap events must be per-task, meaning that the there is no
> concurrency, only re-entrancy.
>
> [peterz: clarify and rewrite]
> Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
> Signed-off-by: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
> ---
> kernel/events/core.c | 24 +++++++++++++++---------
> 1 file changed, 15 insertions(+), 9 deletions(-)
>
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -10950,16 +10950,22 @@ static int __perf_event_overflow(struct
>
> 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;
> + /*
> + * Software events can nest and attempt this recursively;
> + * 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)) {
> + 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;
> + } else {
> + event->pending_work = 0;
> + }
> } else if (event->attr.exclude_kernel && valid_sample) {
> /*
> * Should not be able to return to user space without
Hmm, this is basically similar to my original version, and I re-tested with this
and it's fine. LGTM.
Thanks,
Tengda