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

From: Sumanth Korikkar
Date: Thu Oct 06 2022 - 12:02:10 EST


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).

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