Re: [PATCH bpf-next 1/2] bpf/perf: Call perf_prepare_sample() before bpf_prog_run()

From: Peter Zijlstra
Date: Thu Dec 22 2022 - 07:56:07 EST


On Tue, Dec 20, 2022 at 02:01:43PM -0800, Namhyung Kim wrote:
> When the BPF program calls bpf_cast_to_kern_ctx(), it assumes the program will
> access perf sample data directly and call perf_prepare_sample() to make sure
> the sample data is populated.

I don't understand a word of this :/ What are you doing and why?

> Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
> ---
> include/linux/bpf.h | 1 +
> kernel/bpf/verifier.c | 1 +
> kernel/events/core.c | 3 +++
> 3 files changed, 5 insertions(+)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 5fec2d1be6d7..6bd4c21a6dd4 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -1341,6 +1341,7 @@ struct bpf_prog {
> enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
> call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
> call_get_func_ip:1, /* Do we call get_func_ip() */
> + call_cast_kctx:1, /* Do we call bpf_cast_to_kern_ctx() */
> tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
> enum bpf_prog_type type; /* Type of BPF program */
> enum bpf_attach_type expected_attach_type; /* For some prog types */
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index faa358b3d5d7..23a9dc187292 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -9236,6 +9236,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
> regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED;
> regs[BPF_REG_0].btf = desc_btf;
> regs[BPF_REG_0].btf_id = meta.ret_btf_id;
> + env->prog->call_cast_kctx = 1;
> } else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) {
> ret_t = btf_type_by_id(desc_btf, meta.arg_constant.value);
> if (!ret_t || !btf_type_is_struct(ret_t)) {
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index e47914ac8732..a654a0cb6842 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -10332,6 +10332,7 @@ static void bpf_overflow_handler(struct perf_event *event,
> .event = event,
> };
> struct bpf_prog *prog;
> + struct perf_event_header dummy;
> int ret = 0;
>
> ctx.regs = perf_arch_bpf_user_pt_regs(regs);
> @@ -10346,6 +10347,8 @@ static void bpf_overflow_handler(struct perf_event *event,
> data->callchain = perf_callchain(event, regs);
> data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
> }
> + if (prog->call_cast_kctx)
> + perf_prepare_sample(&dummy, data, event, regs);
>
> ret = bpf_prog_run(prog, &ctx);
> }
> --
> 2.39.0.314.g84b9a713c41-goog
>