Re: [PATCH] Re: [tip: perf/core] perf: Use sample_flags for raw_data

From: Namhyung Kim
Date: Thu Oct 06 2022 - 13:13:37 EST


Hello,

On Thu, Oct 6, 2022 at 9:01 AM Sumanth Korikkar <sumanthk@xxxxxxxxxxxxx> wrote:
>
> Hi,
>
> This causes segfaults.
>
> Steps to recreate:
> * Run ./samples/bpf/trace_output
> BUG pid 9 cookie 1001000000004 sized 4
> BUG pid 9 cookie 1001000000004 sized 4
> BUG pid 9 cookie 1001000000004 sized 4
> Segmentation fault (core dumped)
>
> Problem:
> * The following commit sets data->raw to NULL, when the raw data is not filled
> by PMU driver. This leads to stale data.
>
> * raw data could also be filled by bpf_perf_event_output(), bpf_event_output()
> ...
> 686 perf_sample_data_init(sd, 0, 0);
> 687 sd->raw = &raw;
> 688
> 689 err = __bpf_perf_event_output(regs, map, flags, sd);
> ...
>
> * The below patch eliminates segfaults. However, contradicts with
> the description mentioned in this commit (Filled by only PMU driver).

Thank you for the fix. Don't worry about the description - it said
it's usually filled by PMU drivers and it should be fine as long as
you set the sample flags after filling the raw data.

Acked-by: Namhyung Kim <namhyung@xxxxxxxxxx>

Thanks,
Namhyung

>
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 49fb9ec8366d..1ed08967fb97 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -687,6 +687,7 @@ BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
>
> perf_sample_data_init(sd, 0, 0);
> sd->raw = &raw;
> + sd->sample_flags |= PERF_SAMPLE_RAW;
>
> err = __bpf_perf_event_output(regs, map, flags, sd);
>
> @@ -745,6 +746,7 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
> perf_fetch_caller_regs(regs);
> perf_sample_data_init(sd, 0, 0);
> sd->raw = &raw;
> + sd->sample_flags |= PERF_SAMPLE_RAW;
>
> ret = __bpf_perf_event_output(regs, map, flags, sd);
> out:
>
> --
> Thanks,
> Sumanth