From: Tengda Wu <wutengda@huaweicloud.com>
To: Peter Zijlstra <peterz@infradead.org>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>
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>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
Date: Mon, 23 Nov 2026 10:10:47 +0800 [thread overview]
Message-ID: <42094cd9-6b18-4877-b99d-9e72c8e6e613@huaweicloud.com> (raw)
In-Reply-To: <20261120174942.GU2668804@noisy.programming.kicks-ass.net>
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@huaweicloud.com>
> Date: Thu, 19 Nov 2026 09:51:00 +0000
>
> From: Tengda Wu <wutengda@huaweicloud.com>
>
> 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@huaweicloud.com>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
> 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
next prev parent reply other threads:[~2026-11-23 2:10 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 9:50 [PATCH v5 0/2] perf/core: Fix sigtrap task_work races Tengda Wu
2026-11-19 9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
2026-11-19 12:09 ` Sebastian Andrzej Siewior
2026-11-20 12:14 ` Peter Zijlstra
2026-11-20 14:16 ` Sebastian Andrzej Siewior
2026-11-20 15:04 ` Peter Zijlstra
2026-11-20 15:10 ` Sebastian Andrzej Siewior
2026-11-20 15:24 ` Peter Zijlstra
2026-11-20 15:44 ` Peter Zijlstra
2026-11-20 15:49 ` Sebastian Andrzej Siewior
2026-11-20 16:01 ` Peter Zijlstra
2026-11-20 16:26 ` Peter Zijlstra
2026-11-20 17:28 ` Sebastian Andrzej Siewior
2026-11-20 17:49 ` Peter Zijlstra
2026-11-23 2:10 ` Tengda Wu [this message]
2026-11-23 1:17 ` Tengda Wu
2026-11-19 9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
2026-11-19 12:11 ` Sebastian Andrzej Siewior
2026-11-19 12:17 ` Tengda Wu
2026-11-19 13:07 ` Sebastian Andrzej Siewior
2026-11-19 13:19 ` Tengda Wu
2026-11-19 13:48 ` Sebastian Andrzej Siewior
2026-11-20 0:55 ` Tengda Wu
2026-11-20 16:30 ` Peter Zijlstra
2026-11-20 17:06 ` Sebastian Andrzej Siewior
2026-11-20 17:37 ` Peter Zijlstra
2026-11-20 17:46 ` Peter Zijlstra
2026-11-23 3:11 ` 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=42094cd9-6b18-4877-b99d-9e72c8e6e613@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®