Skip to content

Commit ff9ff92

Browse files
Kan LiangPeter Zijlstra
authored andcommitted
perf/core: Factor out functions to allocate/free the task_ctx_data
The method to allocate/free the task_ctx_data is going to be changed in the following patch. Currently, the task_ctx_data is allocated/freed in several different places. To avoid repeatedly modifying the same codes in several different places, alloc_task_ctx_data() and free_task_ctx_data() are factored out to allocate/free the task_ctx_data. The modification only needs to be applied once. Signed-off-by: Kan Liang <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 47125db commit ff9ff92

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

kernel/events/core.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,12 +1238,22 @@ static void get_ctx(struct perf_event_context *ctx)
12381238
refcount_inc(&ctx->refcount);
12391239
}
12401240

1241+
static void *alloc_task_ctx_data(struct pmu *pmu)
1242+
{
1243+
return kzalloc(pmu->task_ctx_size, GFP_KERNEL);
1244+
}
1245+
1246+
static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data)
1247+
{
1248+
kfree(task_ctx_data);
1249+
}
1250+
12411251
static void free_ctx(struct rcu_head *head)
12421252
{
12431253
struct perf_event_context *ctx;
12441254

12451255
ctx = container_of(head, struct perf_event_context, rcu_head);
1246-
kfree(ctx->task_ctx_data);
1256+
free_task_ctx_data(ctx->pmu, ctx->task_ctx_data);
12471257
kfree(ctx);
12481258
}
12491259

@@ -4471,7 +4481,7 @@ find_get_context(struct pmu *pmu, struct task_struct *task,
44714481
goto errout;
44724482

44734483
if (event->attach_state & PERF_ATTACH_TASK_DATA) {
4474-
task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
4484+
task_ctx_data = alloc_task_ctx_data(pmu);
44754485
if (!task_ctx_data) {
44764486
err = -ENOMEM;
44774487
goto errout;
@@ -4529,11 +4539,11 @@ find_get_context(struct pmu *pmu, struct task_struct *task,
45294539
}
45304540
}
45314541

4532-
kfree(task_ctx_data);
4542+
free_task_ctx_data(pmu, task_ctx_data);
45334543
return ctx;
45344544

45354545
errout:
4536-
kfree(task_ctx_data);
4546+
free_task_ctx_data(pmu, task_ctx_data);
45374547
return ERR_PTR(err);
45384548
}
45394549

@@ -12497,8 +12507,7 @@ inherit_event(struct perf_event *parent_event,
1249712507
!child_ctx->task_ctx_data) {
1249812508
struct pmu *pmu = child_event->pmu;
1249912509

12500-
child_ctx->task_ctx_data = kzalloc(pmu->task_ctx_size,
12501-
GFP_KERNEL);
12510+
child_ctx->task_ctx_data = alloc_task_ctx_data(pmu);
1250212511
if (!child_ctx->task_ctx_data) {
1250312512
free_event(child_event);
1250412513
return ERR_PTR(-ENOMEM);

0 commit comments

Comments
 (0)