Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events

From: Sebastian Andrzej Siewior

Date: Fri Nov 20 2026 - 12:07:02 EST


On 2026-11-20 17:30:07 [+0100], Peter Zijlstra wrote:
> This patch cannot be right, because it leaves in place the existing
> sigtrap / nr_no_switch_fast bits.

I said the same thing and then sashiko came with
https://lore.kernel.org/all/sashiko-outbox-218736@xxxxxxxxxx/

| [Severity: Medium]
| Can ctx->nr_no_switch_fast drop to zero while event->pending_task is still
| queued if the sigtrap event is detached before perf_pending_task() runs?
|
| Detaching an event does not cancel or flush event->pending_task; instead,
| pending_task holds a reference to the event until the task returns to user
| space. When a parent event with attr.inherit and attr.sigtrap is closed while
| a child event has pending_task queued, perf_event_release_kernel() detaches the
| child events from their cloned contexts without uncloning them:
|
| CPU0 (task A) CPU1
| __perf_event_overflow(child_A)
| task_work_add(A, &child_A->pending_task)
| perf_event_release_kernel(parent)
| perf_remove_from_context(child_A, ...)
| list_del_event(child_A, ctx_A)
| local_dec(&ctx_A->nr_no_switch_fast)
| perf_remove_from_context(child_B, ...)
| list_del_event(child_B, ctx_B)
| local_dec(&ctx_B->nr_no_switch_fast)
| schedule() (A -> B)
| perf_event_context_sched_out(A, B)
| /* context_equiv(ctx_A, ctx_B) && nr_no_switch_fast == 0 */
| WRITE_ONCE(ctx_A->task, B)
| ...
| perf_pending_task(&child_A->pending_task)
| perf_sigtrap(child_A)
| WARN_ON_ONCE(event->ctx->task != current) <-- trips WARN, drops SIGTRAP
|
| Keeping the increment in __perf_event_overflow() and decrement in
| perf_pending_task() alongside list_add_event() and list_del_event() would keep
| nr_no_switch_fast elevated until pending_task completes. Note that because
| list_add_event() and list_del_event() can run on a remote CPU under ctx->lock
| while __perf_event_overflow() or perf_pending_task() runs on ctx->task's CPU
| without ctx->lock, nr_no_switch_fast would also need to be atomic_t rather than
| CPU-local local_t.

Sebastian