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

From: Tengda Wu

Date: Thu Nov 19 2026 - 07:17:30 EST




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@xxxxxxxxxxxxxxx>
>
> 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@xxxxxxxxxxxxx>
>
> Sebastian

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

Thanks,
Tengda