Commit eb184479 authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Ingo Molnar

perf: Clean up perf_event_context allocation

Unify the two perf_event_context allocation sites.
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
Cc: paulus <paulus@samba.org>
Cc: stephane eranian <eranian@googlemail.com>
Cc: Robert Richter <robert.richter@amd.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Lin Ming <ming.m.lin@intel.com>
Cc: Yanmin <yanmin_zhang@linux.intel.com>
LKML-Reference: <new-submission>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 97dee4f3
...@@ -1959,9 +1959,7 @@ static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) ...@@ -1959,9 +1959,7 @@ static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
/* /*
* Initialize the perf_event context in a task_struct: * Initialize the perf_event context in a task_struct:
*/ */
static void static void __perf_event_init_context(struct perf_event_context *ctx)
__perf_event_init_context(struct perf_event_context *ctx,
struct task_struct *task)
{ {
raw_spin_lock_init(&ctx->lock); raw_spin_lock_init(&ctx->lock);
mutex_init(&ctx->mutex); mutex_init(&ctx->mutex);
...@@ -1969,7 +1967,25 @@ __perf_event_init_context(struct perf_event_context *ctx, ...@@ -1969,7 +1967,25 @@ __perf_event_init_context(struct perf_event_context *ctx,
INIT_LIST_HEAD(&ctx->flexible_groups); INIT_LIST_HEAD(&ctx->flexible_groups);
INIT_LIST_HEAD(&ctx->event_list); INIT_LIST_HEAD(&ctx->event_list);
atomic_set(&ctx->refcount, 1); atomic_set(&ctx->refcount, 1);
ctx->task = task; }
static struct perf_event_context *
alloc_perf_context(struct pmu *pmu, struct task_struct *task)
{
struct perf_event_context *ctx;
ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
if (!ctx)
return NULL;
__perf_event_init_context(ctx);
if (task) {
ctx->task = task;
get_task_struct(task);
}
ctx->pmu = pmu;
return ctx;
} }
static struct perf_event_context * static struct perf_event_context *
...@@ -2036,22 +2052,22 @@ find_get_context(struct pmu *pmu, pid_t pid, int cpu) ...@@ -2036,22 +2052,22 @@ find_get_context(struct pmu *pmu, pid_t pid, int cpu)
} }
if (!ctx) { if (!ctx) {
ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); ctx = alloc_perf_context(pmu, task);
err = -ENOMEM; err = -ENOMEM;
if (!ctx) if (!ctx)
goto errout; goto errout;
__perf_event_init_context(ctx, task);
ctx->pmu = pmu;
get_ctx(ctx); get_ctx(ctx);
if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) { if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
/* /*
* We raced with some other task; use * We raced with some other task; use
* the context they set. * the context they set.
*/ */
put_task_struct(task);
kfree(ctx); kfree(ctx);
goto retry; goto retry;
} }
get_task_struct(task);
} }
put_task_struct(task); put_task_struct(task);
...@@ -5044,7 +5060,7 @@ int perf_pmu_register(struct pmu *pmu) ...@@ -5044,7 +5060,7 @@ int perf_pmu_register(struct pmu *pmu)
struct perf_cpu_context *cpuctx; struct perf_cpu_context *cpuctx;
cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
__perf_event_init_context(&cpuctx->ctx, NULL); __perf_event_init_context(&cpuctx->ctx);
cpuctx->ctx.pmu = pmu; cpuctx->ctx.pmu = pmu;
cpuctx->timer_interval = TICK_NSEC; cpuctx->timer_interval = TICK_NSEC;
hrtimer_init(&cpuctx->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); hrtimer_init(&cpuctx->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
...@@ -5866,15 +5882,11 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent, ...@@ -5866,15 +5882,11 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent,
* child. * child.
*/ */
child_ctx = kzalloc(sizeof(struct perf_event_context), child_ctx = alloc_perf_context(event->pmu, child);
GFP_KERNEL);
if (!child_ctx) if (!child_ctx)
return -ENOMEM; return -ENOMEM;
__perf_event_init_context(child_ctx, child);
child_ctx->pmu = event->pmu;
child->perf_event_ctxp = child_ctx; child->perf_event_ctxp = child_ctx;
get_task_struct(child);
} }
ret = inherit_group(event, parent, parent_ctx, ret = inherit_group(event, parent, parent_ctx,
...@@ -5886,7 +5898,6 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent, ...@@ -5886,7 +5898,6 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent,
return ret; return ret;
} }
/* /*
* Initialize the perf_event context in task_struct * Initialize the perf_event context in task_struct
*/ */
......
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