Re: [PATCH 4/4] perf record: Handle argument change in sched_switch

Namhyung Kim
2022-04-27T18:15:09Z

Hello,

On Tue, Apr 26, 2022 at 4:55 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Apr 22, 2022 at 3:49 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > 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.
> >
> > Thus I make two copies of the tracepoint handler and select one based
> > on the BTF info.
> >
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> > tools/perf/util/bpf_off_cpu.c | 32 +++++++++++++++
> > tools/perf/util/bpf_skel/off_cpu.bpf.c | 55 ++++++++++++++++++++------
> > 2 files changed, 76 insertions(+), 11 deletions(-)
> >
>
> [...]
>
> >
> > +SEC("tp_btf/sched_switch")
> > +int on_switch3(u64 *ctx)
> > +{
> > + struct task_struct *prev, *next;
> > + int state;
> > +
> > + if (!enabled)
> > + return 0;
> > +
> > + /*
> > + * TP_PROTO(bool preempt, struct task_struct *prev,
> > + * struct task_struct *next)
> > + */
> > + prev = (struct task_struct *)ctx[1];
> > + next = (struct task_struct *)ctx[2];
>
>
> you don't have to have two BPF programs for this, you can use
> read-only variable to make this choice.
>
> On BPF side
>
> const volatile bool has_prev_state = false;
>
> ...
>
> if (has_prev_state) {
> prev = (struct task_struct *)ctx[2];
> next = (struct task_struct *)ctx[3];
> } else {
> prev = (struct task_struct *)ctx[1];
> next = (struct task_struct *)ctx[2];
> }
>
>
> And from user-space side you do your detection and before skeleton is loaded:
>
> skel->rodata->has_prev_state = <whatever you detected>

Nice, thanks for the tip!

Actually I tried something similar but it was with a variable (in bss)
so the verifier in an old kernel rejected it due to invalid arg access.

I guess now the const makes the verifier ignore the branch as if
it's dead but the compiler still generates the code, right?

>
> But I'm still hoping that this prev_state argument can be moved to the
> end ([0]) to make all this unnecessary.
>
> [0] https://lore.kernel.org/lkml/93a20759600c05b6d9e4359a1517c88e06b44834.camel@fb.com/

Yeah, that would make life easier. :)

Thanks,
Namhyung