On Mon, Feb 20, 2023 at 6:54 PM <menglong8.dong@xxxxxxxxx> wrote: > > From: Menglong Dong <imagedong@xxxxxxxxxxx> > > By default, libbpf will attach the kprobe/uprobe eBPF program in the > latest mode that supported by kernel. In this patch, we add the support > to let users manually attach kprobe/uprobe in legacy or perf mode. > > There are 3 mode that supported by the kernel to attach kprobe/uprobe: > > LEGACY: create perf event in legacy way and don't use bpf_link > PERF: create perf event with perf_event_open() and don't use bpf_link > LINK: create perf event with perf_event_open() and use bpf_link > > Users now can manually choose the mode with > bpf_program__attach_uprobe_opts()/bpf_program__attach_kprobe_opts(). > > Link: https://lore.kernel.org/bpf/20230113093427.1666466-1-imagedong@xxxxxxxxxxx/ > Reviewed-by: Biao Jiang <benbjiang@xxxxxxxxxxx> > Signed-off-by: Menglong Dong <imagedong@xxxxxxxxxxx> > Reviewed-by: Alan Maguire <alan.maguire@xxxxxxxxxx> > --- > v2: > - rename no_link to force_ioctl_attach > - rename probe_mode to probe_attach_mode > - add more doc for probe_attach_mode > - return -ENOTSUP when necessray in bpf_program__attach_uprobe_opts and > bpf_program__attach_kprobe_opts > --- > tools/lib/bpf/libbpf.c | 42 +++++++++++++++++++++++++++++++++++++++++- > tools/lib/bpf/libbpf.h | 31 ++++++++++++++++++++++++++++--- > 2 files changed, 69 insertions(+), 4 deletions(-) > [...] > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h > index 2efd80f6f7b9..ef8f68da42f9 100644 > --- a/tools/lib/bpf/libbpf.h > +++ b/tools/lib/bpf/libbpf.h > @@ -451,8 +451,11 @@ struct bpf_perf_event_opts { > size_t sz; > /* custom user-provided value fetchable through bpf_get_attach_cookie() */ > __u64 bpf_cookie; > + /* don't use bpf_link when attach eBPF program */ please also use "BPF" consistently, libbpf doesn't use "eBPF" terminology, except literally 3 very old references (which we should update) > + bool force_ioctl_attach; > + size_t :0; > }; > -#define bpf_perf_event_opts__last_field bpf_cookie > +#define bpf_perf_event_opts__last_field force_ioctl_attach > > LIBBPF_API struct bpf_link * > bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd); [...]