Re: [PATCH] perf/core: fix multiplexing event scheduling issue

From: Peter Zijlstra
Date: Wed Oct 23 2019 - 05:38:07 EST


On Wed, Oct 23, 2019 at 12:06:43AM -0700, Stephane Eranian wrote:
> On Mon, Oct 21, 2019 at 3:06 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> > On Thu, Oct 17, 2019 at 05:27:46PM -0700, Stephane Eranian wrote:

> > > + * others removed. There is a way to get removed and not be disabled first.
> > > + */
> > > + if (ctx->rotate_necessary && ctx->nr_events) {
> > > + int type = get_event_type(event);
> > > + /*
> > > + * In case we removed a pinned event, then we need to
> > > + * resched for both pinned and flexible events. The
> > > + * opposite is not true. A pinned event can never be
> > > + * inactive due to multiplexing.
> > > + */
> > > + if (type & EVENT_PINNED)
> > > + type |= EVENT_FLEXIBLE;
> > > + ctx_resched(cpuctx, cpuctx->task_ctx, type);
> > > + }
> >
> > What you're relying on is that ->rotate_necessary implies ->is_active
> > and there's pending events. And if we tighten ->rotate_necessary you can
> > remove the && ->nr_events.
> >
> Imagine I have 6 events and 4 counters and I do delete them all before
> the timer expires. Then, I can be in a situation where
> rotate_necessary is still true and yet have no more events in the
> context. That is because only ctx_sched_out() clears rotate_necessary,
> IIRC. So that is why there is the && nr_events. Now, calling
> ctx_resched() with no events wouldn't probably cause any harm, just
> wasted work.

> So if by tightening, I am guessing you mean clearing rotate_necessary
> earlier. But that would be tricky because the only reliable way of
> clearing it is when you know you are about the reschedule everything.
> Removing an event by itself may not be enough to eliminate
> multiplexing.

I think you're over-thinking things. The thing you test 'ctx->nr_events'
has a clear place where it drops to 0.

If we add

ctx->nr_events--;
+ if (!ctx->nr_events && ctx->rotate_necessary)
+ ctx->rotate_necessary = 0;

to list_del_event(), we can get rid of that.

Further, since we set it on reschedule, I propose you change the above
like:

if (ctx->rotate_necessary) {
int type = get_event_type(event);
/*
* comment..
*/
if (type & EVENT_PINNED)
type |= EVENT_FLEXIBLE;
+ /*
+ * Will be reset by ctx_resched()'s flexible_sched_in().
+ */
+ ctx->rotate_necessary = 0;
ctx_resched(cpuctx, cpuctx->task_ctx, type);
}

Then rotate_necessary will be tight.