Skip to content

Commit d6838ec

Browse files
namhyungacmel
authored andcommitted
perf offcpu: Fix build failure on old kernels
Old kernels have a 'struct task_struct' which contains a "state" field and newer kernels have "__state" instead. While the get_task_state() in the BPF code handles that in some way, it assumed the current kernel has the new definition and it caused a build error on old kernels. We should not assume anything and access them carefully. Do not use 'task struct' directly access it instead using new and old definitions in a row. Fixes: edc41a1 ("perf record: Enable off-cpu analysis with BPF") Reported-by: Ian Rogers <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Cc: Blake Jones <[email protected]> Cc: Hao Luo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Milian Wolff <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Song Liu <[email protected]> Cc: [email protected] Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 941e3e7 commit d6838ec

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ struct {
7171
__uint(max_entries, 1);
7272
} cgroup_filter SEC(".maps");
7373

74+
/* new kernel task_struct definition */
75+
struct task_struct___new {
76+
long __state;
77+
} __attribute__((preserve_access_index));
78+
7479
/* old kernel task_struct definition */
7580
struct task_struct___old {
7681
long state;
@@ -93,14 +98,17 @@ const volatile bool uses_cgroup_v1 = false;
9398
*/
9499
static inline int get_task_state(struct task_struct *t)
95100
{
96-
if (bpf_core_field_exists(t->__state))
97-
return BPF_CORE_READ(t, __state);
101+
/* recast pointer to capture new type for compiler */
102+
struct task_struct___new *t_new = (void *)t;
98103

99-
/* recast pointer to capture task_struct___old type for compiler */
100-
struct task_struct___old *t_old = (void *)t;
104+
if (bpf_core_field_exists(t_new->__state)) {
105+
return BPF_CORE_READ(t_new, __state);
106+
} else {
107+
/* recast pointer to capture old type for compiler */
108+
struct task_struct___old *t_old = (void *)t;
101109

102-
/* now use old "state" name of the field */
103-
return BPF_CORE_READ(t_old, state);
110+
return BPF_CORE_READ(t_old, state);
111+
}
104112
}
105113

106114
static inline __u64 get_cgroup_id(struct task_struct *t)

0 commit comments

Comments
 (0)