Introduce a new type of BPF link - BPF perf link. This brings perf_event-based BPF program attachments (perf_event, tracepoints, kprobes, and uprobes) into the common BPF link infrastructure, allowing to list all active perf_event based attachments, auto-detaching BPF program from perf_event when link's FD is closed, get generic BPF link fdinfo/get_info functionality. BPF_LINK_CREATE command expects perf_event's FD as target_fd. No extra flags are currently supported. Force-detaching and atomic BPF program updates are not yet implemented, but with perf_event-based BPF links we now have common framework for this without the need to extend ioctl()-based perf_event interface. One interesting consideration is a new value for bpf_attach_type, which BPF_LINK_CREATE command expects. Generally, it's either 1-to-1 mapping from bpf_attach_type to bpf_prog_type, or many-to-1 mapping from a subset of bpf_attach_types to one bpf_prog_type (e.g., see BPF_PROG_TYPE_SK_SKB or BPF_PROG_TYPE_CGROUP_SOCK). In this case, though, we have three different program types (KPROBE, TRACEPOINT, PERF_EVENT) using the same perf_event-based mechanism, so it's many bpf_prog_types to one bpf_attach_type. I chose to define a single BPF_PERF_EVENT attach type for all of them and adjust link_create()'s logic for checking correspondence between attach type and program type. The alternative would be to define three new attach types (e.g., BPF_KPROBE, BPF_TRACEPOINT, and BPF_PERF_EVENT), but that seemed like unnecessary overkill and BPF_KPROBE will cause naming conflicts with BPF_KPROBE() macro, defined by libbpf. I chose to not do this to avoid unnecessary proliferation of bpf_attach_type enum values and not have to deal with naming conflicts.
Showing the changes to tools/include/uapi/linux (1 of 6 files). Show all
Unified · Split
| @@ -993,6 +993,7 @@ enum bpf_attach_type { · 992 unmodified lines | ||
| 993 | 993 | BPF_SK_SKB_VERDICT, |
| 994 | 994 | BPF_SK_REUSEPORT_SELECT, |
| 995 | 995 | BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, |
| 996 | + BPF_PERF_EVENT, | |
| 996 | 997 | __MAX_BPF_ATTACH_TYPE |
| 997 | 998 | }; |
| 998 | 999 | |
| @@ -1006,6 +1007,7 @@ enum bpf_link_type { · 7 unmodified lines | ||
| 1006 | 1007 | BPF_LINK_TYPE_ITER = 4, |
| 1007 | 1008 | BPF_LINK_TYPE_NETNS = 5, |
| 1008 | 1009 | BPF_LINK_TYPE_XDP = 6, |
| 1010 | + BPF_LINK_TYPE_PERF_EVENT = 7, | |
| 1009 | 1011 | |
| 1010 | 1012 | MAX_BPF_LINK_TYPE, |
| 1011 | 1013 | }; |