Skip to content

Commit

Permalink
perf_counter: Ammend cleanup in fork() fail
Browse files Browse the repository at this point in the history
When fork() fails we cannot use perf_counter_exit_task() since that
assumes to operate on current. Write a new helper that cleans up
unused/clean contexts.

Signed-off-by: Peter Zijlstra <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Corey Ashford <[email protected]>
Cc: Marcelo Tosatti <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: John Kacur <[email protected]>
LKML-Reference: <new-submission>
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed May 29, 2009
1 parent 665c214 commit bbbee90
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/linux/perf_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ extern void perf_counter_task_sched_out(struct task_struct *task,
extern void perf_counter_task_tick(struct task_struct *task, int cpu);
extern int perf_counter_init_task(struct task_struct *child);
extern void perf_counter_exit_task(struct task_struct *child);
extern void perf_counter_free_task(struct task_struct *task);
extern void perf_counter_do_pending(void);
extern void perf_counter_print_debug(void);
extern void __perf_disable(void);
Expand Down Expand Up @@ -644,6 +645,7 @@ static inline void
perf_counter_task_tick(struct task_struct *task, int cpu) { }
static inline int perf_counter_init_task(struct task_struct *child) { return 0; }
static inline void perf_counter_exit_task(struct task_struct *child) { }
static inline void perf_counter_free_task(struct task_struct *task) { }
static inline void perf_counter_do_pending(void) { }
static inline void perf_counter_print_debug(void) { }
static inline void perf_disable(void) { }
Expand Down
2 changes: 1 addition & 1 deletion kernel/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
bad_fork_cleanup_audit:
audit_free(p);
bad_fork_cleanup_policy:
perf_counter_exit_task(p);
perf_counter_free_task(p);
#ifdef CONFIG_NUMA
mpol_put(p->mempolicy);
bad_fork_cleanup_cgroup:
Expand Down
43 changes: 40 additions & 3 deletions kernel/perf_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3538,8 +3538,7 @@ static void sync_child_counter(struct perf_counter *child_counter,
}

static void
__perf_counter_exit_task(struct task_struct *child,
struct perf_counter *child_counter,
__perf_counter_exit_task(struct perf_counter *child_counter,
struct perf_counter_context *child_ctx)
{
struct perf_counter *parent_counter;
Expand Down Expand Up @@ -3605,7 +3604,7 @@ void perf_counter_exit_task(struct task_struct *child)
again:
list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
list_entry)
__perf_counter_exit_task(child, child_counter, child_ctx);
__perf_counter_exit_task(child_counter, child_ctx);

/*
* If the last counter was a group counter, it will have appended all
Expand All @@ -3620,6 +3619,44 @@ void perf_counter_exit_task(struct task_struct *child)
put_ctx(child_ctx);
}

/*
* free an unexposed, unused context as created by inheritance by
* init_task below, used by fork() in case of fail.
*/
void perf_counter_free_task(struct task_struct *task)
{
struct perf_counter_context *ctx = task->perf_counter_ctxp;
struct perf_counter *counter, *tmp;

if (!ctx)
return;

mutex_lock(&ctx->mutex);
again:
list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
struct perf_counter *parent = counter->parent;

if (WARN_ON_ONCE(!parent))
continue;

mutex_lock(&parent->child_mutex);
list_del_init(&counter->child_list);
mutex_unlock(&parent->child_mutex);

fput(parent->filp);

list_del_counter(counter, ctx);
free_counter(counter);
}

if (!list_empty(&ctx->counter_list))
goto again;

mutex_unlock(&ctx->mutex);

put_ctx(ctx);
}

/*
* Initialize the perf_counter context in task_struct
*/
Expand Down

0 comments on commit bbbee90

Please sign in to comment.