Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpftool: Use a local copy of BPF_LINK_TYPE_PERF_EVENT in pid_iter.bpf.c

In order to allow the BPF program in bpftool's pid_iter.bpf.c to compile
correctly on hosts where vmlinux.h does not define
BPF_LINK_TYPE_PERF_EVENT (running kernel versions lower than 5.15, for
example), define and use a local copy of the enum value. This requires
LLVM 12 or newer to build the BPF program.

Fixes: cbdaf71f7e65 ("bpftool: Add bpf_cookie to link output")
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230707095425.168126-4-quentin@isovalent.com

authored by

Quentin Monnet and committed by
Andrii Nakryiko
44ba7b30 67a43462

+9 -2
+9 -2
tools/bpf/bpftool/skeleton/pid_iter.bpf.c
··· 24 24 u64 bpf_cookie; 25 25 } __attribute__((preserve_access_index)); 26 26 27 + enum bpf_link_type___local { 28 + BPF_LINK_TYPE_PERF_EVENT___local = 7, 29 + }; 30 + 27 31 extern const void bpf_link_fops __ksym; 28 32 extern const void bpf_map_fops __ksym; 29 33 extern const void bpf_prog_fops __ksym; ··· 97 93 e.pid = task->tgid; 98 94 e.id = get_obj_id(file->private_data, obj_type); 99 95 100 - if (obj_type == BPF_OBJ_LINK) { 96 + if (obj_type == BPF_OBJ_LINK && 97 + bpf_core_enum_value_exists(enum bpf_link_type___local, 98 + BPF_LINK_TYPE_PERF_EVENT___local)) { 101 99 struct bpf_link *link = (struct bpf_link *) file->private_data; 102 100 103 - if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) { 101 + if (link->type == bpf_core_enum_value(enum bpf_link_type___local, 102 + BPF_LINK_TYPE_PERF_EVENT___local)) { 104 103 e.has_bpf_cookie = true; 105 104 e.bpf_cookie = get_bpf_cookie(link); 106 105 }