Skip to content

Commit

Permalink
perf_event: Allocate children's perf_event_ctxp at the right time
Browse files Browse the repository at this point in the history
In current code, children task will allocate memory for
'child->perf_event_ctxp' if the parent is counted, we can
do it only if the parent allowed children inherit it.

It can save memory and reduce overhead.

Signed-off-by: Xiao Guangrong <xiaoguangrong@cn.fujitsu.com>
Reviewed-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Paul Mackerras <paulus@samba.org>
LKML-Reference: <4B1F19A8.5040805@cn.fujitsu.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Xiao Guangrong authored and Ingo Molnar committed Dec 9, 2009
1 parent aa5452d commit b93f797
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions kernel/perf_event.c
Original file line number Diff line number Diff line change
@@ -5083,7 +5083,7 @@ void perf_event_free_task(struct task_struct *task)
*/
int perf_event_init_task(struct task_struct *child)
{
struct perf_event_context *child_ctx, *parent_ctx;
struct perf_event_context *child_ctx = NULL, *parent_ctx;
struct perf_event_context *cloned_ctx;
struct perf_event *event;
struct task_struct *parent = current;
@@ -5098,20 +5098,6 @@ int perf_event_init_task(struct task_struct *child)
if (likely(!parent->perf_event_ctxp))
return 0;

/*
* This is executed from the parent task context, so inherit
* events that have been marked for cloning.
* First allocate and initialize a context for the child.
*/

child_ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
if (!child_ctx)
return -ENOMEM;

__perf_event_init_context(child_ctx, child);
child->perf_event_ctxp = child_ctx;
get_task_struct(child);

/*
* If the parent's context is a clone, pin it so it won't get
* swapped under us.
@@ -5142,6 +5128,26 @@ int perf_event_init_task(struct task_struct *child)
continue;
}

if (!child->perf_event_ctxp) {
/*
* This is executed from the parent task context, so
* inherit events that have been marked for cloning.
* First allocate and initialize a context for the
* child.
*/

child_ctx = kzalloc(sizeof(struct perf_event_context),
GFP_KERNEL);
if (!child_ctx) {
ret = -ENOMEM;
goto exit;
}

__perf_event_init_context(child_ctx, child);
child->perf_event_ctxp = child_ctx;
get_task_struct(child);
}

ret = inherit_group(event, parent, parent_ctx,
child, child_ctx);
if (ret) {
@@ -5170,6 +5176,7 @@ int perf_event_init_task(struct task_struct *child)
get_ctx(child_ctx->parent_ctx);
}

exit:
mutex_unlock(&parent_ctx->mutex);

perf_unpin_context(parent_ctx);

0 comments on commit b93f797

Please sign in to comment.