* [PATCH] libbpf: Use the correct fd when attaching to perf events @ 2021-03-12 21:43 Sultan Alsawaf 2021-03-13 1:31 ` Andrii Nakryiko 0 siblings, 1 reply; 6+ messages in thread From: Sultan Alsawaf @ 2021-03-12 21:43 UTC (permalink / raw) Cc: Sultan Alsawaf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev, netdev, bpf, linux-kernel From: Sultan Alsawaf <sultan@kerneltoast.com> We should be using the program fd here, not the perf event fd. Fixes: 63f2f5ee856ba ("libbpf: add ability to attach/detach BPF program to perf event") Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com> --- tools/lib/bpf/libbpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index d43cc3f29dae..3d20d57d4af5 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9538,7 +9538,7 @@ struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, if (!link) return ERR_PTR(-ENOMEM); link->detach = &bpf_link__detach_perf_event; - link->fd = pfd; + link->fd = prog_fd; if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { err = -errno; -- 2.30.2 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] libbpf: Use the correct fd when attaching to perf events 2021-03-12 21:43 [PATCH] libbpf: Use the correct fd when attaching to perf events Sultan Alsawaf @ 2021-03-13 1:31 ` Andrii Nakryiko 2021-03-13 2:22 ` Sultan Alsawaf 0 siblings, 1 reply; 6+ messages in thread From: Andrii Nakryiko @ 2021-03-13 1:31 UTC (permalink / raw) To: Sultan Alsawaf Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev, Networking, bpf, open list On Fri, Mar 12, 2021 at 1:43 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > From: Sultan Alsawaf <sultan@kerneltoast.com> > > We should be using the program fd here, not the perf event fd. Why? Can you elaborate on what issue you ran into with the current code? > > Fixes: 63f2f5ee856ba ("libbpf: add ability to attach/detach BPF program to perf event") > Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com> > --- > tools/lib/bpf/libbpf.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index d43cc3f29dae..3d20d57d4af5 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -9538,7 +9538,7 @@ struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, > if (!link) > return ERR_PTR(-ENOMEM); > link->detach = &bpf_link__detach_perf_event; > - link->fd = pfd; > + link->fd = prog_fd; > > if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { > err = -errno; > -- > 2.30.2 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] libbpf: Use the correct fd when attaching to perf events 2021-03-13 1:31 ` Andrii Nakryiko @ 2021-03-13 2:22 ` Sultan Alsawaf 2021-03-13 2:33 ` Andrii Nakryiko 0 siblings, 1 reply; 6+ messages in thread From: Sultan Alsawaf @ 2021-03-13 2:22 UTC (permalink / raw) To: Andrii Nakryiko Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev, Networking, bpf, open list On Fri, Mar 12, 2021 at 05:31:14PM -0800, Andrii Nakryiko wrote: > On Fri, Mar 12, 2021 at 1:43 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > > > From: Sultan Alsawaf <sultan@kerneltoast.com> > > > > We should be using the program fd here, not the perf event fd. > > Why? Can you elaborate on what issue you ran into with the current code? bpf_link__pin() would fail with -EINVAL when using tracepoints, kprobes, or uprobes. The failure would happen inside the kernel, in bpf_link_get_from_fd() right here: if (f.file->f_op != &bpf_link_fops) { fdput(f); return ERR_PTR(-EINVAL); } Since bpf wasn't looking for the perf event fd, I swapped it for the program fd and bpf_link__pin() worked. Sultan ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] libbpf: Use the correct fd when attaching to perf events 2021-03-13 2:22 ` Sultan Alsawaf @ 2021-03-13 2:33 ` Andrii Nakryiko 2021-03-13 2:43 ` Sultan Alsawaf 0 siblings, 1 reply; 6+ messages in thread From: Andrii Nakryiko @ 2021-03-13 2:33 UTC (permalink / raw) To: Sultan Alsawaf Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev, Networking, bpf, open list On Fri, Mar 12, 2021 at 6:22 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > On Fri, Mar 12, 2021 at 05:31:14PM -0800, Andrii Nakryiko wrote: > > On Fri, Mar 12, 2021 at 1:43 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > > > > > From: Sultan Alsawaf <sultan@kerneltoast.com> > > > > > > We should be using the program fd here, not the perf event fd. > > > > Why? Can you elaborate on what issue you ran into with the current code? > > bpf_link__pin() would fail with -EINVAL when using tracepoints, kprobes, or > uprobes. The failure would happen inside the kernel, in bpf_link_get_from_fd() > right here: > if (f.file->f_op != &bpf_link_fops) { > fdput(f); > return ERR_PTR(-EINVAL); > } kprobe/tracepoint/perf_event attachments behave like bpf_link (so libbpf uses user-space high-level bpf_link APIs for it), but they are not bpf_link-based in the kernel. So bpf_link__pin() won't work for such types of programs until we actually have bpf_link-backed attachment support in the kernel itself. I never got to implementing this because we already had auto-detachment properties from perf_event FD itself. But it would be nice to have that done as a real bpf_link in the kernel (with all the observability, program update, force-detach support). Looking for volunteers to make this happen ;) > > Since bpf wasn't looking for the perf event fd, I swapped it for the program fd > and bpf_link__pin() worked. But you were pinning the BPF program, not a BPF link. Which is not what should have happen. > > Sultan ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] libbpf: Use the correct fd when attaching to perf events 2021-03-13 2:33 ` Andrii Nakryiko @ 2021-03-13 2:43 ` Sultan Alsawaf 2021-03-13 2:50 ` Andrii Nakryiko 0 siblings, 1 reply; 6+ messages in thread From: Sultan Alsawaf @ 2021-03-13 2:43 UTC (permalink / raw) To: Andrii Nakryiko Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev, Networking, bpf, open list On Fri, Mar 12, 2021 at 06:33:01PM -0800, Andrii Nakryiko wrote: > On Fri, Mar 12, 2021 at 6:22 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > > > On Fri, Mar 12, 2021 at 05:31:14PM -0800, Andrii Nakryiko wrote: > > > On Fri, Mar 12, 2021 at 1:43 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > > > > > > > From: Sultan Alsawaf <sultan@kerneltoast.com> > > > > > > > > We should be using the program fd here, not the perf event fd. > > > > > > Why? Can you elaborate on what issue you ran into with the current code? > > > > bpf_link__pin() would fail with -EINVAL when using tracepoints, kprobes, or > > uprobes. The failure would happen inside the kernel, in bpf_link_get_from_fd() > > right here: > > if (f.file->f_op != &bpf_link_fops) { > > fdput(f); > > return ERR_PTR(-EINVAL); > > } > > kprobe/tracepoint/perf_event attachments behave like bpf_link (so > libbpf uses user-space high-level bpf_link APIs for it), but they are > not bpf_link-based in the kernel. So bpf_link__pin() won't work for > such types of programs until we actually have bpf_link-backed > attachment support in the kernel itself. I never got to implementing > this because we already had auto-detachment properties from perf_event > FD itself. But it would be nice to have that done as a real bpf_link > in the kernel (with all the observability, program update, > force-detach support). > > Looking for volunteers to make this happen ;) > > > > > > Since bpf wasn't looking for the perf event fd, I swapped it for the program fd > > and bpf_link__pin() worked. > > But you were pinning the BPF program, not a BPF link. Which is not > what should have happen. This is the code in question: link = bpf_program__attach(prog); // make sure `link` is valid, blah blah... bpf_link__pin(link, some_path); Are you saying that this usage is incorrect? Sultan ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] libbpf: Use the correct fd when attaching to perf events 2021-03-13 2:43 ` Sultan Alsawaf @ 2021-03-13 2:50 ` Andrii Nakryiko 0 siblings, 0 replies; 6+ messages in thread From: Andrii Nakryiko @ 2021-03-13 2:50 UTC (permalink / raw) To: Sultan Alsawaf Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev, Networking, bpf, open list On Fri, Mar 12, 2021 at 6:43 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > On Fri, Mar 12, 2021 at 06:33:01PM -0800, Andrii Nakryiko wrote: > > On Fri, Mar 12, 2021 at 6:22 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > > > > > On Fri, Mar 12, 2021 at 05:31:14PM -0800, Andrii Nakryiko wrote: > > > > On Fri, Mar 12, 2021 at 1:43 PM Sultan Alsawaf <sultan@kerneltoast.com> wrote: > > > > > > > > > > From: Sultan Alsawaf <sultan@kerneltoast.com> > > > > > > > > > > We should be using the program fd here, not the perf event fd. > > > > > > > > Why? Can you elaborate on what issue you ran into with the current code? > > > > > > bpf_link__pin() would fail with -EINVAL when using tracepoints, kprobes, or > > > uprobes. The failure would happen inside the kernel, in bpf_link_get_from_fd() > > > right here: > > > if (f.file->f_op != &bpf_link_fops) { > > > fdput(f); > > > return ERR_PTR(-EINVAL); > > > } > > > > kprobe/tracepoint/perf_event attachments behave like bpf_link (so > > libbpf uses user-space high-level bpf_link APIs for it), but they are > > not bpf_link-based in the kernel. So bpf_link__pin() won't work for > > such types of programs until we actually have bpf_link-backed > > attachment support in the kernel itself. I never got to implementing > > this because we already had auto-detachment properties from perf_event > > FD itself. But it would be nice to have that done as a real bpf_link > > in the kernel (with all the observability, program update, > > force-detach support). > > > > Looking for volunteers to make this happen ;) > > > > > > > > > > Since bpf wasn't looking for the perf event fd, I swapped it for the program fd > > > and bpf_link__pin() worked. > > > > But you were pinning the BPF program, not a BPF link. Which is not > > what should have happen. > > This is the code in question: > link = bpf_program__attach(prog); > // make sure `link` is valid, blah blah... > bpf_link__pin(link, some_path); > > Are you saying that this usage is incorrect? Right, for kprobe/tracepoint/perf_event attachments it's not supported. cgroup, xdp, raw_tracepoint and fentry/fexit/fmod_ret/freplace (and a few more) attachments are bpf_links in the kernel, so it works for them. > > Sultan ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-03-13 2:51 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-03-12 21:43 [PATCH] libbpf: Use the correct fd when attaching to perf events Sultan Alsawaf 2021-03-13 1:31 ` Andrii Nakryiko 2021-03-13 2:22 ` Sultan Alsawaf 2021-03-13 2:33 ` Andrii Nakryiko 2021-03-13 2:43 ` Sultan Alsawaf 2021-03-13 2:50 ` Andrii Nakryiko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).