Skip to content

Commit e42c9c5

Browse files
namhyungacmel
authored andcommitted
perf tools: Get a perf cgroup more portably in BPF
The perf_event_cgrp_id can be different on other configurations. To be more portable as CO-RE, it needs to get the cgroup subsys id using the bpf_core_enum_value() helper. Suggested-by: Ian Rogers <irogers@google.com> Reviewed-by: Ian Rogers <irogers@google.com> Signed-off-by: Namhyung Kim <namhyung@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Hao Luo <haoluo@google.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Song Liu <songliubraving@fb.com> Cc: bpf@vger.kernel.org Link: https://lore.kernel.org/r/20220923063205.772936-1-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent f76349c commit e42c9c5

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

tools/perf/util/bpf_skel/bperf_cgroup.bpf.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const volatile __u32 num_cpus = 1;
4848

4949
int enabled = 0;
5050
int use_cgroup_v2 = 0;
51+
int perf_subsys_id = -1;
5152

5253
static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
5354
{
@@ -58,7 +59,15 @@ static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
5859
int level;
5960
int cnt;
6061

61-
cgrp = BPF_CORE_READ(p, cgroups, subsys[perf_event_cgrp_id], cgroup);
62+
if (perf_subsys_id == -1) {
63+
#if __has_builtin(__builtin_preserve_enum_value)
64+
perf_subsys_id = bpf_core_enum_value(enum cgroup_subsys_id,
65+
perf_event_cgrp_id);
66+
#else
67+
perf_subsys_id = perf_event_cgrp_id;
68+
#endif
69+
}
70+
cgrp = BPF_CORE_READ(p, cgroups, subsys[perf_subsys_id], cgroup);
6271
level = BPF_CORE_READ(cgrp, level);
6372

6473
for (cnt = 0; i < MAX_LEVELS; i++) {

tools/perf/util/bpf_skel/off_cpu.bpf.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ const volatile bool has_prev_state = false;
9494
const volatile bool needs_cgroup = false;
9595
const volatile bool uses_cgroup_v1 = false;
9696

97+
int perf_subsys_id = -1;
98+
9799
/*
98100
* Old kernel used to call it task_struct->state and now it's '__state'.
99101
* Use BPF CO-RE "ignored suffix rule" to deal with it like below:
@@ -119,11 +121,19 @@ static inline __u64 get_cgroup_id(struct task_struct *t)
119121
{
120122
struct cgroup *cgrp;
121123

122-
if (uses_cgroup_v1)
123-
cgrp = BPF_CORE_READ(t, cgroups, subsys[perf_event_cgrp_id], cgroup);
124-
else
125-
cgrp = BPF_CORE_READ(t, cgroups, dfl_cgrp);
124+
if (!uses_cgroup_v1)
125+
return BPF_CORE_READ(t, cgroups, dfl_cgrp, kn, id);
126+
127+
if (perf_subsys_id == -1) {
128+
#if __has_builtin(__builtin_preserve_enum_value)
129+
perf_subsys_id = bpf_core_enum_value(enum cgroup_subsys_id,
130+
perf_event_cgrp_id);
131+
#else
132+
perf_subsys_id = perf_event_cgrp_id;
133+
#endif
134+
}
126135

136+
cgrp = BPF_CORE_READ(t, cgroups, subsys[perf_subsys_id], cgroup);
127137
return BPF_CORE_READ(cgrp, kn, id);
128138
}
129139

0 commit comments

Comments
 (0)