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

libbpf: Add the ability to suppress perf event enablement

Automatically enabling a perf event after attaching a BPF prog to it is
not always desirable.

Add a new "dont_enable" field to struct bpf_perf_event_opts. While
introducing "enable" instead would be nicer in that it would avoid
a double negation in the implementation, it would make
DECLARE_LIBBPF_OPTS() less efficient.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Suggested-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Thomas Richter <tmricht@linux.ibm.com>
Co-developed-by: Thomas Richter <tmricht@linux.ibm.com>
Signed-off-by: Thomas Richter <tmricht@linux.ibm.com>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Link: https://lore.kernel.org/r/20250806162417.19666-2-iii@linux.ibm.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Ilya Leoshkevich and committed by
Alexei Starovoitov
9474e27a 1b30d444

+11 -6
+8 -5
tools/lib/bpf/libbpf.c
··· 10965 10965 } 10966 10966 link->link.fd = pfd; 10967 10967 } 10968 - if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { 10969 - err = -errno; 10970 - pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n", 10971 - prog->name, pfd, errstr(err)); 10972 - goto err_out; 10968 + 10969 + if (!OPTS_GET(opts, dont_enable, false)) { 10970 + if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { 10971 + err = -errno; 10972 + pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n", 10973 + prog->name, pfd, errstr(err)); 10974 + goto err_out; 10975 + } 10973 10976 } 10974 10977 10975 10978 return &link->link;
+3 -1
tools/lib/bpf/libbpf.h
··· 499 499 __u64 bpf_cookie; 500 500 /* don't use BPF link when attach BPF program */ 501 501 bool force_ioctl_attach; 502 + /* don't automatically enable the event */ 503 + bool dont_enable; 502 504 size_t :0; 503 505 }; 504 - #define bpf_perf_event_opts__last_field force_ioctl_attach 506 + #define bpf_perf_event_opts__last_field dont_enable 505 507 506 508 LIBBPF_API struct bpf_link * 507 509 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);