Re: [PATCH v8] perf: Sharing PMU counters across compatible events

From: Song Liu
Date: Thu Dec 12 2019 - 18:50:19 EST




> On Dec 12, 2019, at 10:52 AM, Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> On Thu, Dec 12, 2019 at 03:45:49PM +0000, Song Liu wrote:
>>> On Dec 12, 2019, at 7:39 AM, Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
>>> Yuck!
>>>
>>> Why do you do a full reschedule when you take out a master?
>>
>> If there is active slave using this master, we need to schedule out
>> them before removing the master.
>>
>> We can improve the check though. We only need to do it if the master
>> is in state PERF_EVENT_STATE_ENABLED.
>>
>> Or we can add a different function to only schedule out slaves.
>
> So I've been thinking, this is because an NMI from another event can
> come in and then does PERF_SAMPLE_READ which covers our event, right?
>
> AFAICT every other case will run under ctx->lock, which we own at this
> point.

Right, we hold ctx->lock here, so it should be safe in most case.

>
> So can't we:
>
> 1 - stop the current master (such that the counts are frozen)
> 2 - pick the new master
> 3 - initialize the new master (such that the counts match)
> 4 - set the new master on all other events
> 5 - start the new master (counters run again)
>
> Then, no matter where the NMI lands, it will always find either the old
> or the new master and their counts will match.
>
> You really don't need to stop all events.

I think this should work. Let me try it.

Thanks for the suggestion,
Song