libbpf: Add extra BPF_PROG_TYPE check to bpf_object__probe_loading
authorJonathan Edwards <jonathan.edwards@165gc.onmicrosoft.com>
Sat, 19 Jun 2021 15:10:07 +0000 (11:10 -0400)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 21 Jun 2021 15:21:07 +0000 (17:21 +0200)
eBPF has been backported for RHEL 7 w/ kernel 3.10-940+ [0]. However only
the following program types are supported [1]:

  BPF_PROG_TYPE_KPROBE
  BPF_PROG_TYPE_TRACEPOINT
  BPF_PROG_TYPE_PERF_EVENT

For libbpf this causes an EINVAL return during the bpf_object__probe_loading
call which only checks to see if programs of type BPF_PROG_TYPE_SOCKET_FILTER
can load.

The following will try BPF_PROG_TYPE_TRACEPOINT as a fallback attempt before
erroring out. BPF_PROG_TYPE_KPROBE was not a good candidate because on some
kernels it requires knowledge of the LINUX_VERSION_CODE.

  [0] https://www.redhat.com/en/blog/introduction-ebpf-red-hat-enterprise-linux-7
  [1] https://access.redhat.com/articles/3550581

Signed-off-by: Jonathan Edwards <jonathan.edwards@165gc.onmicrosoft.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210619151007.GA6963@165gc.onmicrosoft.com
tools/lib/bpf/libbpf.c

index 48c0ade..1e04ce7 100644 (file)
@@ -4001,6 +4001,10 @@ bpf_object__probe_loading(struct bpf_object *obj)
 
        ret = bpf_load_program_xattr(&attr, NULL, 0);
        if (ret < 0) {
+               attr.prog_type = BPF_PROG_TYPE_TRACEPOINT;
+               ret = bpf_load_program_xattr(&attr, NULL, 0);
+       }
+       if (ret < 0) {
                ret = errno;
                cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
                pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "