Commit e566b76e authored by Stephane Eranian's avatar Stephane Eranian Committed by Ingo Molnar

perf_event: Fix cgrp event scheduling bug in perf_enable_on_exec()

There is a bug in perf_event_enable_on_exec() when cgroup events are
active on a CPU: the cgroup events may be scheduled twice causing event
state corruptions which eventually may lead to kernel panics.

The reason is that the function needs to first schedule out the cgroup
events, just like for the per-thread events. The cgroup event are
scheduled back in automatically from the perf_event_context_sched_in()
function.

The patch also adds a WARN_ON_ONCE() is perf_cgroup_switch() to catch any
bogus state.
Signed-off-by: default avatarStephane Eranian <eranian@google.com>
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/20110406005454.GA1062@quadSigned-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 621d2656
...@@ -364,6 +364,7 @@ void perf_cgroup_switch(struct task_struct *task, int mode) ...@@ -364,6 +364,7 @@ void perf_cgroup_switch(struct task_struct *task, int mode)
} }
if (mode & PERF_CGROUP_SWIN) { if (mode & PERF_CGROUP_SWIN) {
WARN_ON_ONCE(cpuctx->cgrp);
/* set cgrp before ctxsw in to /* set cgrp before ctxsw in to
* allow event_filter_match() to not * allow event_filter_match() to not
* have to pass task around * have to pass task around
...@@ -2423,6 +2424,14 @@ static void perf_event_enable_on_exec(struct perf_event_context *ctx) ...@@ -2423,6 +2424,14 @@ static void perf_event_enable_on_exec(struct perf_event_context *ctx)
if (!ctx || !ctx->nr_events) if (!ctx || !ctx->nr_events)
goto out; goto out;
/*
* We must ctxsw out cgroup events to avoid conflict
* when invoking perf_task_event_sched_in() later on
* in this function. Otherwise we end up trying to
* ctxswin cgroup events which are already scheduled
* in.
*/
perf_cgroup_sched_out(current);
task_ctx_sched_out(ctx, EVENT_ALL); task_ctx_sched_out(ctx, EVENT_ALL);
raw_spin_lock(&ctx->lock); raw_spin_lock(&ctx->lock);
...@@ -2447,6 +2456,9 @@ static void perf_event_enable_on_exec(struct perf_event_context *ctx) ...@@ -2447,6 +2456,9 @@ static void perf_event_enable_on_exec(struct perf_event_context *ctx)
raw_spin_unlock(&ctx->lock); raw_spin_unlock(&ctx->lock);
/*
* Also calls ctxswin for cgroup events, if any:
*/
perf_event_context_sched_in(ctx, ctx->task); perf_event_context_sched_in(ctx, ctx->task);
out: out:
local_irq_restore(flags); local_irq_restore(flags);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment