Re: [PATCH v4 1/9] perf evsel: Set BPF output to system-wide

From: Howard Chu
Date: Wed Sep 25 2024 - 03:06:30 EST


Hello,

On Tue, Sep 24, 2024 at 7:53 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> On Wed, Aug 7, 2024 at 8:58 PM Howard Chu <howardchu95@xxxxxxxxx> wrote:
> >
> > Hello,
> >
> > The event does open, but bpf_perf_event_output() in BPF will return
> > -95(-EOPNOTSUPP), so no output. I think this EOPNOTSUPP is not in
> > bpf_trace.c's __bpf_perf_event_output(), but in perf_event's
> > perf_event_output() called by BPF.
> >
> > <idle>-0 [001] d..4. 154921.079230: bpf_trace_printk: err -95
> >
> > This is also a bug in perf trace -p <PID>.
> >
> > Thanks,
> > Howard
> >
> > On Thu, Aug 8, 2024 at 7:21 AM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
> > >
> > > On Wed, Aug 07, 2024 at 11:38:35PM +0800, Howard Chu wrote:
> > > > pid = -1 for bpf-output event.
> > > >
> > > > This makes perf record -p <PID> --off-cpu work. Otherwise bpf-output
> > > > cannot be collected.
> > >
> > > I don't understand why it's necessary. Why isn't it collected?
> > > Is it the kernel to reject the BPF output event to open?
> > >
> > > Thanks,
> > > Namhyung
> > >
> > > >
> > > > Signed-off-by: Howard Chu <howardchu95@xxxxxxxxx>
> > > > ---
> > > > tools/perf/util/evsel.c | 4 ++++
> > > > 1 file changed, 4 insertions(+)
> > > >
> > > > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > > > index bc603193c477..b961467133cf 100644
> > > > --- a/tools/perf/util/evsel.c
> > > > +++ b/tools/perf/util/evsel.c
> > > > @@ -2282,6 +2282,10 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
> > > >
> > > > test_attr__ready();
> > > >
> > > > + /* BPF output event can only be system-wide */
> > > > + if (evsel__is_bpf_output(evsel))
> > > > + pid = -1;
>
> This matches with libbpf:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/lib/bpf/libbpf.c#n11967

Thanks for pointing it out, that's a very good reference. Namhyung
actually came up with a very good solution and based on his idea I
posted this patch series:
https://lore.kernel.org/linux-perf-users/20240827092013.1596-1-howardchu95@xxxxxxxxx/

Thanks,
Howard
>
> Thanks,
> Ian
>
> > > > +
> > > > /* Debug message used by test scripts */
> > > > pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
> > > > pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, evsel->open_flags);
> > > > --
> > > > 2.45.2
> > > >