Re: [PATCH v5 2/2] perf/core: Disable context fast-swap for sigtrap events
From: Tengda Wu
Date: Wed Nov 25 2026 - 04:29:54 EST
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.
> But also, we could probably write this to do:
>
> if (*count == 1) {
> pmu->pmu_enable(pmu);
> barrier();
> (*count)--;
> }
>
Yeah, that's one approach.
>> Maybe my original nr_no_switch_fast approach is still worth considering.
>> -- it would account for sigtrap events at attach time and keep them off
>> the fast path entirely, rather than trying to filter the overflow after
>> the swap has already happened.
>
> 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.
Thank,
Tengda