Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
From: Tengda Wu
Date: Wed Nov 25 2026 - 05:04:08 EST
On 2026/11/25 17:38, Peter Zijlstra wrote:
> On Wed, Nov 25, 2026 at 05:29:38PM +0800, Tengda Wu wrote:
>>
>>
>> On 2026/11/25 17:08, Peter Zijlstra wrote:
>>> On Tue, Nov 24, 2026 at 04:42:34PM +0800, Tengda Wu wrote:
>>>
>>>> From the stack, we can see:
>>>>
>>>> perf_event_context_sched_out
>>>> // fast path
>>>> perf_ctx_enable(ctx, 0);
>>>> perf_pmu_enable
>>>> if (!--(*count)) // equals 0
>>>> pmu->pmu_enable(pmu);
>>>> __intel_pmu_enable_all.constprop
>>>> do_trace_write_msr
>>>> perf_tp_event
>>>>
>>>> In perf_tp_event():
>>>> if (perf_tp_event_match(event, &raw, regs)) {
>>>> if (!perf_pmu_enabled(event->pmu)) return 0; // not satisfied, continue downward
>>>> perf_swevent_event
>>>> __perf_event_overflow
>>>> task_work_add // event->ctx->task mismatches 'current'
>>>> }
>>>>
>>>> Because pmu_disable_count is already 0, the subsequent perf_tp_event_match()
>>>> cannot block it via perf_pmu_enabled(), so the problem can be reproduced.
>>>
>>> Right, but at this point there isn't really a problem, all the pointers
>>> have been swapped already and state should be consistent again.
>>>
>>> I *think* the problem is that task_work_add() is using current while it
>>> should be using ctx->task. In that case the above will enqueue on the
>>> next task, which is correct.
>>>
>>
>> I considered this before, but it involves the issue of whether the count
>> overflow in the sched_out stage should be attributed to the current or next
>> task, and I wasn't very confident about it, so I didn't adopt it.
>
> All of switch_fast suffers that problem. Mostly we've ignored it since
> kernel events around a context switch are hard to attribute to any one
> particular task, so a little fuzz left or right of the switch doesn't
> matter too much, as long as its consistent.
>
Thanks for the clarification.
>>> Disabling switch_fast really hurts performance; doing that should be a
>>> last resort. I think I can fix this. Let me try and see if the
>>> reproducer works for me so I can more easily poke at this.
>>>
>>
>> OK. Just a reminder that the PoC I provided needs to be run in an Intel
>> environment, so that the tracepoint can be hit when pmu_enable is executed.
>
> Ah, I'm currently running it on an SPR Xeon; do I need to use
> non-default arguments or just run the thing in a loop and drink tea
> until it goes pop?
No need to pass any other arguments; running it as follows should be enough:
for i {1..100}; do ./fast_swap ; done
Thanks,
Tengda