Re: [PATCH 1/4] perf: Fix system-wide events miscounting during cgroup monitoring

From: Peter Zijlstra
Date: Tue Apr 30 2019 - 04:56:55 EST


On Mon, Apr 29, 2019 at 07:44:02AM -0700, kan.liang@xxxxxxxxxxxxxxx wrote:

> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index e47ef76..039e2f2 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -795,6 +795,7 @@ struct perf_cpu_context {
> #ifdef CONFIG_CGROUP_PERF
> struct perf_cgroup *cgrp;
> struct list_head cgrp_cpuctx_entry;
> + unsigned int cgrp_switch :1;

If you're not adding more bits, why not just keep it an int?

> #endif
>
> struct list_head sched_cb_entry;
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index dc7dead..388dd42 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -809,6 +809,7 @@ static void perf_cgroup_switch(struct task_struct *task, int mode)
>
> perf_ctx_lock(cpuctx, cpuctx->task_ctx);
> perf_pmu_disable(cpuctx->ctx.pmu);
> + cpuctx->cgrp_switch = true;
>
> if (mode & PERF_CGROUP_SWOUT) {
> cpu_ctx_sched_out(cpuctx, EVENT_ALL);
> @@ -832,6 +833,7 @@ static void perf_cgroup_switch(struct task_struct *task, int mode)
> &cpuctx->ctx);
> cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
> }
> + cpuctx->cgrp_switch = false;
> perf_pmu_enable(cpuctx->ctx.pmu);
> perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
> }

That is a bit of a hack...

> @@ -2944,13 +2946,25 @@ static void ctx_sched_out(struct perf_event_context *ctx,
>
> perf_pmu_disable(ctx->pmu);
> if (is_active & EVENT_PINNED) {
> - list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list)
> + list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list) {
> +#ifdef CONFIG_CGROUP_PERF
> + /* Don't sched system-wide event when cgroup context switch */
> + if (cpuctx->cgrp_switch && !event->cgrp)
> + continue;
> +#endif
> group_sched_out(event, cpuctx, ctx);
> + }
> }

This works by accident, however..

>
> if (is_active & EVENT_FLEXIBLE) {
> - list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list)
> + list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list) {
> +#ifdef CONFIG_CGROUP_PERF
> + /* Don't sched system-wide event when cgroup context switch */
> + if (cpuctx->cgrp_switch && !event->cgrp)
> + continue;
> +#endif
> group_sched_out(event, cpuctx, ctx);
> + }
> }
> perf_pmu_enable(ctx->pmu);
> }

this one is just wrong afaict.

Suppose the new cgroup has pinned events, which we cannot schedule
because you left !cgroup flexible events on.