Skip to content

Commit b36888f

Browse files
namhyungacmel
authored andcommitted
perf record: Handle argument change in sched_switch
Recently sched_switch tracepoint added a new argument for prev_state, but it's hard to handle the change in a BPF program. Instead, we can check the function prototype in BTF before loading the program. Signed-off-by: Namhyung Kim <namhyung@kernel.org> Acked-by: Ian Rogers <irogers@google.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Blake Jones <blakejones@google.com> Cc: Hao Luo <haoluo@google.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Milian Wolff <milian.wolff@kdab.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Song Liu <songliubraving@fb.com> Cc: bpf@vger.kernel.org Link: https://lore.kernel.org/r/20220518224725.742882-5-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 10742d0 commit b36888f

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

tools/perf/util/bpf_off_cpu.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ static void off_cpu_finish(void *arg __maybe_unused)
8989
off_cpu_bpf__destroy(skel);
9090
}
9191

92+
/* v5.18 kernel added prev_state arg, so it needs to check the signature */
93+
static void check_sched_switch_args(void)
94+
{
95+
const struct btf *btf = bpf_object__btf(skel->obj);
96+
const struct btf_type *t1, *t2, *t3;
97+
u32 type_id;
98+
99+
type_id = btf__find_by_name_kind(btf, "bpf_trace_sched_switch",
100+
BTF_KIND_TYPEDEF);
101+
if ((s32)type_id < 0)
102+
return;
103+
104+
t1 = btf__type_by_id(btf, type_id);
105+
if (t1 == NULL)
106+
return;
107+
108+
t2 = btf__type_by_id(btf, t1->type);
109+
if (t2 == NULL || !btf_is_ptr(t2))
110+
return;
111+
112+
t3 = btf__type_by_id(btf, t2->type);
113+
if (t3 && btf_is_func_proto(t3) && btf_vlen(t3) == 4) {
114+
/* new format: pass prev_state as 4th arg */
115+
skel->rodata->has_prev_state = true;
116+
}
117+
}
118+
92119
int off_cpu_prepare(struct evlist *evlist, struct target *target)
93120
{
94121
int err, fd, i;
@@ -117,6 +144,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target)
117144
}
118145

119146
set_max_rlimit();
147+
check_sched_switch_args();
120148

121149
err = off_cpu_bpf__load(skel);
122150
if (err) {

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ int enabled = 0;
7272
int has_cpu = 0;
7373
int has_task = 0;
7474

75+
const volatile bool has_prev_state = false;
76+
7577
/*
7678
* Old kernel used to call it task_struct->state and now it's '__state'.
7779
* Use BPF CO-RE "ignored suffix rule" to deal with it like below:
@@ -121,22 +123,13 @@ static inline int can_record(struct task_struct *t, int state)
121123
return 1;
122124
}
123125

124-
SEC("tp_btf/sched_switch")
125-
int on_switch(u64 *ctx)
126+
static int off_cpu_stat(u64 *ctx, struct task_struct *prev,
127+
struct task_struct *next, int state)
126128
{
127129
__u64 ts;
128-
int state;
129130
__u32 stack_id;
130-
struct task_struct *prev, *next;
131131
struct tstamp_data *pelem;
132132

133-
if (!enabled)
134-
return 0;
135-
136-
prev = (struct task_struct *)ctx[1];
137-
next = (struct task_struct *)ctx[2];
138-
state = get_task_state(prev);
139-
140133
ts = bpf_ktime_get_ns();
141134

142135
if (!can_record(prev, state))
@@ -180,4 +173,24 @@ int on_switch(u64 *ctx)
180173
return 0;
181174
}
182175

176+
SEC("tp_btf/sched_switch")
177+
int on_switch(u64 *ctx)
178+
{
179+
struct task_struct *prev, *next;
180+
int prev_state;
181+
182+
if (!enabled)
183+
return 0;
184+
185+
prev = (struct task_struct *)ctx[1];
186+
next = (struct task_struct *)ctx[2];
187+
188+
if (has_prev_state)
189+
prev_state = (int)ctx[3];
190+
else
191+
prev_state = get_task_state(prev);
192+
193+
return off_cpu_stat(ctx, prev, next, prev_state);
194+
}
195+
183196
char LICENSE[] SEC("license") = "Dual BSD/GPL";

0 commit comments

Comments
 (0)