Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH v5 0/2] perf/core: Fix sigtrap task_work races
@ 2026-11-19  9:50 Tengda Wu
  2026-11-19  9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
  2026-11-19  9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
  0 siblings, 2 replies; 20+ messages in thread
From: Tengda Wu @ 2026-11-19  9:50 UTC (permalink / raw)
  To: Peter Zijlstra, Sebastian Andrzej Siewior
  Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	James Clark, linux-perf-users, linux-kernel, Tengda Wu

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@huaweicloud.com/
Patch 2: https://lore.kernel.org/all/d1b24bbf-9111-4766-87c8-ff97adcac727@huaweicloud.com/

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
=========
v4 -> v5:
  - v4: https://lore.kernel.org/all/cover.1794914794.git.wutengda@huaweicloud.com/
  - Roll back patch 2 to v3 to avoid the fast swap isolation omission in
    the nr_no_switch_fast 1->0 scenario. (Shashiko)
v3 -> v4:
  - v3: https://lore.kernel.org/all/cover.1793870900.git.wutengda@huaweicloud.com/
  - 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@huaweicloud.com/
  - Added Patch 2 to fix WARN_ON_ONCE() in perf_sigtrap()
v1 -> v2:
  - v1: https://lore.kernel.org/all/20261031114520.642476-1-wutengda@huaweicloud.com/
  - 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 |  6 ++---
 kernel/events/core.c       | 48 ++++++++++++++++++++++++++------------
 2 files changed, 35 insertions(+), 19 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-19  9:50 [PATCH v5 0/2] perf/core: Fix sigtrap task_work races Tengda Wu
@ 2026-11-19  9:51 ` Tengda Wu
  2026-11-19  9:59   ` sashiko-bot
                     ` (3 more replies)
  2026-11-19  9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
  1 sibling, 4 replies; 20+ messages in thread
From: Tengda Wu @ 2026-11-19  9:51 UTC (permalink / raw)
  To: Peter Zijlstra, Sebastian Andrzej Siewior
  Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	James Clark, linux-perf-users, linux-kernel, Tengda Wu

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@huaweicloud.com>
---
 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


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  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  9:51 ` Tengda Wu
  2026-11-19 10:03   ` sashiko-bot
  2026-11-19 12:11   ` Sebastian Andrzej Siewior
  1 sibling, 2 replies; 20+ messages in thread
From: Tengda Wu @ 2026-11-19  9:51 UTC (permalink / raw)
  To: Peter Zijlstra, Sebastian Andrzej Siewior
  Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	James Clark, linux-perf-users, linux-kernel, Tengda Wu

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 context fast-swap path was already excluded for events that need
stable task association (inherit + sample read). sigtrap events have
the same requirement: the SIGTRAP is targeted at ctx->task and must not
be delivered after ctx->task has been swapped to another task.

Fix this by also excluding events with attr->sigtrap from the fast-swap
path, i.e. bump nr_no_switch_fast for sigtrap events in
list_add_event()/list_del_event(), and update the comment describing
the invariant accordingly.

Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
---
 include/linux/perf_event.h | 6 ++----
 kernel/events/core.c       | 9 +++++++--
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index e1e11500df09..d2eb22cc5b71 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1067,10 +1067,8 @@ struct perf_event_context {
 	 * should be avoided.
 	 *
 	 * Sum (event->pending_work + events with
-	 *    (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)))
-	 *
-	 * The SIGTRAP is targeted at ctx->task, as such it won't do changing
-	 * that until the signal is delivered.
+	 *    (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) +
+	 *    events with attr->sigtrap)
 	 */
 	local_t				nr_no_switch_fast;
 };
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 857230b023ae..094b42ed6462 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2009,7 +2009,7 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
 		ctx->nr_user++;
 	if (event->attr.inherit_stat)
 		ctx->nr_stat++;
-	if (has_inherit_and_sample_read(&event->attr))
+	if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
 		local_inc(&ctx->nr_no_switch_fast);
 
 	if (event->state > PERF_EVENT_STATE_OFF)
@@ -2235,7 +2235,7 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
 		ctx->nr_user--;
 	if (event->attr.inherit_stat)
 		ctx->nr_stat--;
-	if (has_inherit_and_sample_read(&event->attr))
+	if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
 		local_dec(&ctx->nr_no_switch_fast);
 
 	list_del_rcu(&event->event_entry);
@@ -3841,6 +3841,11 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)
 				 * SAMPLE_READ events they should be switched
 				 * out using the slow path so that they are
 				 * treated as if they were distinct contexts.
+				 *
+				 * A context holding a sigtrap event must also
+				 * take the slow path, even when nothing is
+				 * pending, since the sigtrap delivery relies on
+				 * the ctx->task relation as well.
 				 */
 				raw_spin_unlock(&next_ctx->lock);
 				rcu_read_unlock();
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-19  9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
@ 2026-11-19  9:59   ` sashiko-bot
  2026-11-19 12:09   ` Sebastian Andrzej Siewior
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 20+ messages in thread
From: sashiko-bot @ 2026-11-19  9:59 UTC (permalink / raw)
  To: Tengda Wu; +Cc: linux-perf-users

> 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>
> [ ... ]
> 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@huaweicloud.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795053619.git.wutengda@huaweicloud.com?part=1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19  9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
@ 2026-11-19 10:03   ` sashiko-bot
  2026-11-19 11:39     ` Tengda Wu
  2026-11-19 12:11   ` Sebastian Andrzej Siewior
  1 sibling, 1 reply; 20+ messages in thread
From: sashiko-bot @ 2026-11-19 10:03 UTC (permalink / raw)
  To: Tengda Wu; +Cc: linux-perf-users

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf/core: non-SMP-atomic local_t race on
  ctx->nr_no_switch_fast
--

commit 5e829db733e2fe49cdac7cb99bb9428db1c83359
Author: Tengda Wu <wutengda@huaweicloud.com>

perf/core: Disable context fast-swap for sigtrap events

This commit excludes sigtrap events from the context fast-swap path by
incrementing and decrementing ctx->nr_no_switch_fast when adding and
removing events with attr.sigtrap set.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index da86bc6e8be94..483ba8704d418 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -1999,7 +1999,7 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
>  		ctx->nr_user++;
>  	if (event->attr.inherit_stat)
>  		ctx->nr_stat++;
> -	if (has_inherit_and_sample_read(&event->attr))
> +	if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
>  		local_inc(&ctx->nr_no_switch_fast);
>  
>  	if (event->state > PERF_EVENT_STATE_OFF)
> @@ -2225,7 +2225,7 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
>  		ctx->nr_user--;
>  	if (event->attr.inherit_stat)
>  		ctx->nr_stat--;
> -	if (has_inherit_and_sample_read(&event->attr))
> +	if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
>  		local_dec(&ctx->nr_no_switch_fast);

[Severity: Medium]
Can concurrent local_inc() and local_dec() calls on ctx->nr_no_switch_fast
from different CPUs race and lose updates?

ctx->nr_no_switch_fast is a local_t, so local_inc() and local_dec() are
only atomic on a single CPU (for example, unlocked inc/dec on x86).  When
installing a disabled sigtrap event into a non-empty task context,
perf_install_in_context() runs add_event_to_ctx() directly on the
installer CPU under ctx->lock without an IPI, while an already-active
sigtrap event in that context can update ctx->nr_no_switch_fast on the
target task's CPU without holding ctx->lock:

CPU0                              CPU1 (running ctx->task)
perf_install_in_context()
  raw_spin_lock(&ctx->lock)
  add_event_to_ctx()
    list_add_event()
      local_inc(&ctx->nr_no_switch_fast)
                                  __perf_event_overflow()
                                    local_inc(&ctx->nr_no_switch_fast)
                                      <-- non-SMP-atomic RMW race
                                  ...
                                  perf_pending_task()
                                    local_dec(&ctx->nr_no_switch_fast)

A similar cross-CPU race is possible between list_del_event() in
perf_remove_from_context() (when !ctx->is_active) and perf_pending_task().
A lost increment allows ctx->nr_no_switch_fast to drop to 0 while a
sigtrap event or pending task_work is still present, re-enabling the
context fast-swap path and hitting WARN_ON_ONCE(event->ctx->task !=
current) in perf_sigtrap().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795053619.git.wutengda@huaweicloud.com?part=2

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19 10:03   ` sashiko-bot
@ 2026-11-19 11:39     ` Tengda Wu
  0 siblings, 0 replies; 20+ messages in thread
From: Tengda Wu @ 2026-11-19 11:39 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: linux-perf-users



On 2026/11/19 18:03, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] perf/core: non-SMP-atomic local_t race on
>   ctx->nr_no_switch_fast
> --
> 
> commit 5e829db733e2fe49cdac7cb99bb9428db1c83359
> Author: Tengda Wu <wutengda@huaweicloud.com>
> 
> perf/core: Disable context fast-swap for sigtrap events
> 
> This commit excludes sigtrap events from the context fast-swap path by
> incrementing and decrementing ctx->nr_no_switch_fast when adding and
> removing events with attr.sigtrap set.
> 
>> diff --git a/kernel/events/core.c b/kernel/events/core.c
>> index da86bc6e8be94..483ba8704d418 100644
>> --- a/kernel/events/core.c
>> +++ b/kernel/events/core.c
>> @@ -1999,7 +1999,7 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
>>  		ctx->nr_user++;
>>  	if (event->attr.inherit_stat)
>>  		ctx->nr_stat++;
>> -	if (has_inherit_and_sample_read(&event->attr))
>> +	if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
>>  		local_inc(&ctx->nr_no_switch_fast);
>>  
>>  	if (event->state > PERF_EVENT_STATE_OFF)
>> @@ -2225,7 +2225,7 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
>>  		ctx->nr_user--;
>>  	if (event->attr.inherit_stat)
>>  		ctx->nr_stat--;
>> -	if (has_inherit_and_sample_read(&event->attr))
>> +	if (has_inherit_and_sample_read(&event->attr) || event->attr.sigtrap)
>>  		local_dec(&ctx->nr_no_switch_fast);
> 
> [Severity: Medium]
> Can concurrent local_inc() and local_dec() calls on ctx->nr_no_switch_fast
> from different CPUs race and lose updates?
> 
> ctx->nr_no_switch_fast is a local_t, so local_inc() and local_dec() are
> only atomic on a single CPU (for example, unlocked inc/dec on x86).  When
> installing a disabled sigtrap event into a non-empty task context,
> perf_install_in_context() runs add_event_to_ctx() directly on the
> installer CPU under ctx->lock without an IPI, while an already-active
> sigtrap event in that context can update ctx->nr_no_switch_fast on the
> target task's CPU without holding ctx->lock:
> 
> CPU0                              CPU1 (running ctx->task)
> perf_install_in_context()
>   raw_spin_lock(&ctx->lock)
>   add_event_to_ctx()
>     list_add_event()
>       local_inc(&ctx->nr_no_switch_fast)
>                                   __perf_event_overflow()
>                                     local_inc(&ctx->nr_no_switch_fast)
>                                       <-- non-SMP-atomic RMW race
>                                   ...
>                                   perf_pending_task()
>                                     local_dec(&ctx->nr_no_switch_fast)
> 
> A similar cross-CPU race is possible between list_del_event() in
> perf_remove_from_context() (when !ctx->is_active) and perf_pending_task().
> A lost increment allows ctx->nr_no_switch_fast to drop to 0 while a
> sigtrap event or pending task_work is still present, re-enabling the
> context fast-swap path and hitting WARN_ON_ONCE(event->ctx->task !=
> current) in perf_sigtrap().
> 

Same as this:

  https://lore.kernel.org/all/b5d10cd7-e3af-4e07-a19b-44544077db59@huaweicloud.com/

It is a pre-existing issue, so a separate patch will be submitted and it will not
be fixed within this patch series.

Thanks,
Tengda


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-19  9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
  2026-11-19  9:59   ` sashiko-bot
@ 2026-11-19 12:09   ` Sebastian Andrzej Siewior
  2026-11-20 12:14   ` Peter Zijlstra
  2026-11-20 15:44   ` Peter Zijlstra
  3 siblings, 0 replies; 20+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-19 12:09 UTC (permalink / raw)
  To: Tengda Wu
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel

On 2026-11-19 09:51:00 [+0000], Tengda Wu wrote:
> 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.

The repeated task_work_add() will corrupt the list.

> 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.

"to revert to previous state".

> 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@huaweicloud.com>

Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

Sebastian

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19  9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
  2026-11-19 10:03   ` sashiko-bot
@ 2026-11-19 12:11   ` Sebastian Andrzej Siewior
  2026-11-19 12:17     ` Tengda Wu
  1 sibling, 1 reply; 20+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-19 12:11 UTC (permalink / raw)
  To: Tengda Wu
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel

On 2026-11-19 09:51:01 [+0000], Tengda Wu wrote:
> 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 context fast-swap path was already excluded for events that need
> stable task association (inherit + sample read). sigtrap events have
> the same requirement: the SIGTRAP is targeted at ctx->task and must not
> be delivered after ctx->task has been swapped to another task.
> 
> Fix this by also excluding events with attr->sigtrap from the fast-swap
> path, i.e. bump nr_no_switch_fast for sigtrap events in
> list_add_event()/list_del_event(), and update the comment describing
> the invariant accordingly.
> 
> Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
> Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>

This makes sense. How did you run into this? Is it possible to make a
testcase out of it?

Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

Sebastian

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19 12:11   ` Sebastian Andrzej Siewior
@ 2026-11-19 12:17     ` Tengda Wu
  2026-11-19 13:07       ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 20+ messages in thread
From: Tengda Wu @ 2026-11-19 12:17 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel



On 2026/11/19 20:11, Sebastian Andrzej Siewior wrote:
> On 2026-11-19 09:51:01 [+0000], Tengda Wu wrote:
>> 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 context fast-swap path was already excluded for events that need
>> stable task association (inherit + sample read). sigtrap events have
>> the same requirement: the SIGTRAP is targeted at ctx->task and must not
>> be delivered after ctx->task has been swapped to another task.
>>
>> Fix this by also excluding events with attr->sigtrap from the fast-swap
>> path, i.e. bump nr_no_switch_fast for sigtrap events in
>> list_add_event()/list_del_event(), and update the comment describing
>> the invariant accordingly.
>>
>> Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.")
>> Signed-off-by: Tengda Wu <wutengda@huaweicloud.com>
> 
> This makes sense. How did you run into this? Is it possible to make a
> testcase out of it?
> 
> Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> 
> Sebastian

There is a test case, see this:
https://lore.kernel.org/all/d1b24bbf-9111-4766-87c8-ff97adcac727@huaweicloud.com/

Thanks,
Tengda


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19 12:17     ` Tengda Wu
@ 2026-11-19 13:07       ` Sebastian Andrzej Siewior
  2026-11-19 13:19         ` Tengda Wu
  0 siblings, 1 reply; 20+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-19 13:07 UTC (permalink / raw)
  To: Tengda Wu
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel

On 2026-11-19 20:17:20 [+0800], Tengda Wu wrote:
> There is a test case, see this:
> https://lore.kernel.org/all/d1b24bbf-9111-4766-87c8-ff97adcac727@huaweicloud.com/

Now I remember. Would it make sense to include it to
	tools/testing/selftests/perf_events/sigtrap_threads.c

?

> Thanks,
> Tengda

Sebastian

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19 13:07       ` Sebastian Andrzej Siewior
@ 2026-11-19 13:19         ` Tengda Wu
  2026-11-19 13:48           ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 20+ messages in thread
From: Tengda Wu @ 2026-11-19 13:19 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel



On 2026/11/19 21:07, Sebastian Andrzej Siewior wrote:
> On 2026-11-19 20:17:20 [+0800], Tengda Wu wrote:
>> There is a test case, see this:
>> https://lore.kernel.org/all/d1b24bbf-9111-4766-87c8-ff97adcac727@huaweicloud.com/
> 
> Now I remember. Would it make sense to include it to
> 	tools/testing/selftests/perf_events/sigtrap_threads.c
> 
> ?
> 

Well, because the trigger window is very short, it is not easy to reproduce.
The test case I provided above must be stress-tested through multiple
rounds of a while loop. Is it inappropriate to put this kind of test,
which requires a while loop and has a certain degree of randomness, into
deterministic test cases like sigtrap_threads.c?

Thanks,
Tengda


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19 13:19         ` Tengda Wu
@ 2026-11-19 13:48           ` Sebastian Andrzej Siewior
  2026-11-20  0:55             ` Tengda Wu
  0 siblings, 1 reply; 20+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-19 13:48 UTC (permalink / raw)
  To: Tengda Wu
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel

On 2026-11-19 21:19:59 [+0800], Tengda Wu wrote:
> Well, because the trigger window is very short, it is not easy to reproduce.
> The test case I provided above must be stress-tested through multiple
> rounds of a while loop. Is it inappropriate to put this kind of test,
> which requires a while loop and has a certain degree of randomness, into
> deterministic test cases like sigtrap_threads.c?

Then maybe not. I would have to run it myself to be able to judge it.
However this thread has a reference to the test case should someone need
it in a few years…

> Thanks,
> Tengda

Sebastian

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
  2026-11-19 13:48           ` Sebastian Andrzej Siewior
@ 2026-11-20  0:55             ` Tengda Wu
  0 siblings, 0 replies; 20+ messages in thread
From: Tengda Wu @ 2026-11-20  0:55 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel



On 2026/11/19 21:48, Sebastian Andrzej Siewior wrote:
> On 2026-11-19 21:19:59 [+0800], Tengda Wu wrote:
>> Well, because the trigger window is very short, it is not easy to reproduce.
>> The test case I provided above must be stress-tested through multiple
>> rounds of a while loop. Is it inappropriate to put this kind of test,
>> which requires a while loop and has a certain degree of randomness, into
>> deterministic test cases like sigtrap_threads.c?
> 
> Then maybe not. I would have to run it myself to be able to judge it.
> However this thread has a reference to the test case should someone need
> it in a few years…
> 
>> Thanks,
>> Tengda
> 
> Sebastian

Yeah, thanks for the clarification. I'll leave it as-is then.

Best regards,
Tengda


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-19  9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
  2026-11-19  9:59   ` sashiko-bot
  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:44   ` Peter Zijlstra
  3 siblings, 1 reply; 20+ messages in thread
From: Peter Zijlstra @ 2026-11-20 12:14 UTC (permalink / raw)
  To: Tengda Wu
  Cc: Sebastian Andrzej Siewior, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel

On Thu, Nov 19, 2026 at 09:51:00AM +0000, Tengda Wu wrote:
> 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.

I am confused by the use of smp_*() and cmpxchg() here. Where is the
multi-cpu scenario?


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-20 12:14   ` Peter Zijlstra
@ 2026-11-20 14:16     ` Sebastian Andrzej Siewior
  2026-11-20 15:04       ` Peter Zijlstra
  0 siblings, 1 reply; 20+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-20 14:16 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Tengda Wu, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-perf-users, linux-kernel

On 2026-11-20 13:14:36 [+0100], Peter Zijlstra wrote:
> > Also use smp_load_acquire() when checking pending_work in
> > the exclude_kernel warning path to ensure the read is properly
> > ordered.
> 
> I am confused by the use of smp_*() and cmpxchg() here. Where is the
> multi-cpu scenario?

The same event is used in process context and NMI
|        perf-tc1-7647    [001] d..6.   114.513688: __perf_event_overflow: ffff888100d68540
|        perf-tc1-7647    [001] d.Z2.   114.513703: __perf_event_overflow: ffff888100d68540

I don't see how to deal with this without cmpxchg. And the same event is
also used by other task

|        perf-tc1-7657    [001] d.Z2.   114.598356: __perf_event_overflow: ffff888100d68540

which makes me thing that it could be pinned to another CPU (unless
there is something that impossible and it is strictly per-CPU).

The smp load/store macros is my fault as it looked better to do this
than plain stores (given that we have the cmpxchg already).

Sebastian

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-20 14:16     ` Sebastian Andrzej Siewior
@ 2026-11-20 15:04       ` Peter Zijlstra
  2026-11-20 15:10         ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 20+ messages in thread
From: Peter Zijlstra @ 2026-11-20 15:04 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Tengda Wu, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-perf-users, linux-kernel

On Fri, Nov 20, 2026 at 03:16:06PM +0100, Sebastian Andrzej Siewior wrote:
> On 2026-11-20 13:14:36 [+0100], Peter Zijlstra wrote:
> > > Also use smp_load_acquire() when checking pending_work in
> > > the exclude_kernel warning path to ensure the read is properly
> > > ordered.
> > 
> > I am confused by the use of smp_*() and cmpxchg() here. Where is the
> > multi-cpu scenario?
> 
> The same event is used in process context and NMI
> |        perf-tc1-7647    [001] d..6.   114.513688: __perf_event_overflow: ffff888100d68540
> |        perf-tc1-7647    [001] d.Z2.   114.513703: __perf_event_overflow: ffff888100d68540
> 
> I don't see how to deal with this without cmpxchg. And the same event is
> also used by other task

You can do CMPXCHG (without the LOCK) prefix; the helper is called
cmpxchg_local().

> |        perf-tc1-7657    [001] d.Z2.   114.598356: __perf_event_overflow: ffff888100d68540
> 
> which makes me thing that it could be pinned to another CPU (unless
> there is something that impossible and it is strictly per-CPU).
> 
> The smp load/store macros is my fault as it looked better to do this
> than plain stores (given that we have the cmpxchg already).

It is confusing as all heck. This is all task-local, and a task can only
ever run on one CPU at the time.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-20 15:04       ` Peter Zijlstra
@ 2026-11-20 15:10         ` Sebastian Andrzej Siewior
  2026-11-20 15:24           ` Peter Zijlstra
  0 siblings, 1 reply; 20+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-20 15:10 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Tengda Wu, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-perf-users, linux-kernel

On 2026-11-20 16:04:52 [+0100], Peter Zijlstra wrote:
> On Fri, Nov 20, 2026 at 03:16:06PM +0100, Sebastian Andrzej Siewior wrote:
> > On 2026-11-20 13:14:36 [+0100], Peter Zijlstra wrote:
> > > > Also use smp_load_acquire() when checking pending_work in
> > > > the exclude_kernel warning path to ensure the read is properly
> > > > ordered.
> > > 
> > > I am confused by the use of smp_*() and cmpxchg() here. Where is the
> > > multi-cpu scenario?
> > 
> > The same event is used in process context and NMI
> > |        perf-tc1-7647    [001] d..6.   114.513688: __perf_event_overflow: ffff888100d68540
> > |        perf-tc1-7647    [001] d.Z2.   114.513703: __perf_event_overflow: ffff888100d68540
> > 
> > I don't see how to deal with this without cmpxchg. And the same event is
> > also used by other task
> 
> You can do CMPXCHG (without the LOCK) prefix; the helper is called
> cmpxchg_local().
> 
> > |        perf-tc1-7657    [001] d.Z2.   114.598356: __perf_event_overflow: ffff888100d68540
> > 
> > which makes me thing that it could be pinned to another CPU (unless
> > there is something that impossible and it is strictly per-CPU).
> > 
> > The smp load/store macros is my fault as it looked better to do this
> > than plain stores (given that we have the cmpxchg already).
> 
> It is confusing as all heck. This is all task-local, and a task can only
> ever run on one CPU at the time.

Given the two pids I was assuming (!knowing) that two CPUs could be
involved.

So you are saying cmpxchg_local() and a plain assign/WRITE_ONCE rather
than the smp.* given that this is all task local.

Sebastian

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-20 15:10         ` Sebastian Andrzej Siewior
@ 2026-11-20 15:24           ` Peter Zijlstra
  0 siblings, 0 replies; 20+ messages in thread
From: Peter Zijlstra @ 2026-11-20 15:24 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Tengda Wu, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-perf-users, linux-kernel

On Fri, Nov 20, 2026 at 04:10:17PM +0100, Sebastian Andrzej Siewior wrote:
> On 2026-11-20 16:04:52 [+0100], Peter Zijlstra wrote:
> > On Fri, Nov 20, 2026 at 03:16:06PM +0100, Sebastian Andrzej Siewior wrote:
> > > On 2026-11-20 13:14:36 [+0100], Peter Zijlstra wrote:
> > > > > Also use smp_load_acquire() when checking pending_work in
> > > > > the exclude_kernel warning path to ensure the read is properly
> > > > > ordered.
> > > > 
> > > > I am confused by the use of smp_*() and cmpxchg() here. Where is the
> > > > multi-cpu scenario?
> > > 
> > > The same event is used in process context and NMI
> > > |        perf-tc1-7647    [001] d..6.   114.513688: __perf_event_overflow: ffff888100d68540
> > > |        perf-tc1-7647    [001] d.Z2.   114.513703: __perf_event_overflow: ffff888100d68540
> > > 
> > > I don't see how to deal with this without cmpxchg. And the same event is
> > > also used by other task
> > 
> > You can do CMPXCHG (without the LOCK) prefix; the helper is called
> > cmpxchg_local().
> > 
> > > |        perf-tc1-7657    [001] d.Z2.   114.598356: __perf_event_overflow: ffff888100d68540
> > > 
> > > which makes me thing that it could be pinned to another CPU (unless
> > > there is something that impossible and it is strictly per-CPU).
> > > 
> > > The smp load/store macros is my fault as it looked better to do this
> > > than plain stores (given that we have the cmpxchg already).
> > 
> > It is confusing as all heck. This is all task-local, and a task can only
> > ever run on one CPU at the time.
> 
> Given the two pids I was assuming (!knowing) that two CPUs could be
> involved.
> 
> So you are saying cmpxchg_local() and a plain assign/WRITE_ONCE rather
> than the smp.* given that this is all task local.

Well, from what I can remember, only task bound self-monitoring events
can signal, and the whole task_work is per necessity task local. Which
makes it all local.

It is indeed {READ,WRITE}_ONCE() combined with barrier() if you really
need to beat the compiler into submission.

But let me go try and read this Changelog in more detail.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-19  9:51 ` [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue Tengda Wu
                     ` (2 preceding siblings ...)
  2026-11-20 12:14   ` Peter Zijlstra
@ 2026-11-20 15:44   ` Peter Zijlstra
  2026-11-20 15:49     ` Sebastian Andrzej Siewior
  3 siblings, 1 reply; 20+ messages in thread
From: Peter Zijlstra @ 2026-11-20 15:44 UTC (permalink / raw)
  To: Tengda Wu
  Cc: Sebastian Andrzej Siewior, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-perf-users,
	linux-kernel

On Thu, Nov 19, 2026 at 09:51:00AM +0000, Tengda Wu wrote:
> 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()

How? If an event is NMI driven, it will only ever cause an overflow from
NMI context.

If it is not, then the same event will not trigger from NMI.

Some key ingredient is missing here. Is this some software event that is
missing recursion checks?

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v5 1/2] perf/core: Prevent duplicate sigtrap task work enqueue
  2026-11-20 15:44   ` Peter Zijlstra
@ 2026-11-20 15:49     ` Sebastian Andrzej Siewior
  0 siblings, 0 replies; 20+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-20 15:49 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Tengda Wu, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-perf-users, linux-kernel

On 2026-11-20 16:44:57 [+0100], Peter Zijlstra wrote:
> Some key ingredient is missing here. Is this some software event that is
> missing recursion checks?
If you navigate to
	https://lore.kernel.org/all/29aa123c-36a2-460f-b76b-a8dfe1eb542f@huaweicloud.com/
and /PoC then you find what I used for testing.

Sebastian

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2026-11-20 15:50 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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  9:59   ` sashiko-bot
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-19  9:51 ` [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events Tengda Wu
2026-11-19 10:03   ` sashiko-bot
2026-11-19 11:39     ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox