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

From: Peter Zijlstra
Date: Thu Dec 12 2019 - 13:52:31 EST


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.

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.