Skip to content

Commit c917e0f

Browse files
liu-song-6Ingo Molnar
authored andcommitted
perf/cgroup: Fix child event counting bug
When a perf_event is attached to parent cgroup, it should count events for all children cgroups: parent_group <---- perf_event \ - child_group <---- process(es) However, in our tests, we found this perf_event cannot report reliable results. Here is an example case: # create cgroups mkdir -p /sys/fs/cgroup/p/c # start perf for parent group perf stat -e instructions -G "p" # on another console, run test process in child cgroup: stressapptest -s 2 -M 1000 & echo $! > /sys/fs/cgroup/p/c/cgroup.procs # after the test process is done, stop perf in the first console shows <not counted> instructions p The instruction should not be "not counted" as the process runs in the child cgroup. We found this is because perf_event->cgrp and cpuctx->cgrp are not identical, thus perf_event->cgrp are not updated properly. This patch fixes this by updating perf_cgroup properly for ancestor cgroup(s). Reported-by: Ephraim Park <[email protected]> Signed-off-by: Song Liu <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: <[email protected]> Cc: <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vince Weaver <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 320b065 commit c917e0f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

kernel/events/core.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,15 @@ static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
724724

725725
static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
726726
{
727-
struct perf_cgroup *cgrp_out = cpuctx->cgrp;
728-
if (cgrp_out)
729-
__update_cgrp_time(cgrp_out);
727+
struct perf_cgroup *cgrp = cpuctx->cgrp;
728+
struct cgroup_subsys_state *css;
729+
730+
if (cgrp) {
731+
for (css = &cgrp->css; css; css = css->parent) {
732+
cgrp = container_of(css, struct perf_cgroup, css);
733+
__update_cgrp_time(cgrp);
734+
}
735+
}
730736
}
731737

732738
static inline void update_cgrp_time_from_event(struct perf_event *event)
@@ -754,6 +760,7 @@ perf_cgroup_set_timestamp(struct task_struct *task,
754760
{
755761
struct perf_cgroup *cgrp;
756762
struct perf_cgroup_info *info;
763+
struct cgroup_subsys_state *css;
757764

758765
/*
759766
* ctx->lock held by caller
@@ -764,8 +771,12 @@ perf_cgroup_set_timestamp(struct task_struct *task,
764771
return;
765772

766773
cgrp = perf_cgroup_from_task(task, ctx);
767-
info = this_cpu_ptr(cgrp->info);
768-
info->timestamp = ctx->timestamp;
774+
775+
for (css = &cgrp->css; css; css = css->parent) {
776+
cgrp = container_of(css, struct perf_cgroup, css);
777+
info = this_cpu_ptr(cgrp->info);
778+
info->timestamp = ctx->timestamp;
779+
}
769780
}
770781

771782
static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list);

0 commit comments

Comments
 (0)