]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
libbpf: Add the ability to suppress perf event enablement
authorIlya Leoshkevich <iii@linux.ibm.com>
Wed, 6 Aug 2025 16:22:41 +0000 (18:22 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 7 Aug 2025 16:01:41 +0000 (09:01 -0700)
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>
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h

index fb4d92c5c339413c0661d5efcf49ef93ea258fbd..8f5a81b672e1b8546852d5d8dd34518b33443421 100644 (file)
@@ -10965,11 +10965,14 @@ struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *p
                }
                link->link.fd = pfd;
        }
-       if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
-               err = -errno;
-               pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
-                       prog->name, pfd, errstr(err));
-               goto err_out;
+
+       if (!OPTS_GET(opts, dont_enable, false)) {
+               if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
+                       err = -errno;
+                       pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
+                               prog->name, pfd, errstr(err));
+                       goto err_out;
+               }
        }
 
        return &link->link;
index d1cf813a057bc7db6912b9039c55e2c94b7b0dba..455a957cb702cab53266ea948fec061f3b65c9ee 100644 (file)
@@ -499,9 +499,11 @@ struct bpf_perf_event_opts {
        __u64 bpf_cookie;
        /* don't use BPF link when attach BPF program */
        bool force_ioctl_attach;
+       /* don't automatically enable the event */
+       bool dont_enable;
        size_t :0;
 };
-#define bpf_perf_event_opts__last_field force_ioctl_attach
+#define bpf_perf_event_opts__last_field dont_enable
 
 LIBBPF_API struct bpf_link *
 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);