[PATCH v3 1/2] libbpf: Add the ability to suppress perf event enablement

Ilya Leoshkevich posted 2 patches 2 months ago
[PATCH v3 1/2] libbpf: Add the ability to suppress perf event enablement
Posted by Ilya Leoshkevich 2 months ago
Automatically enabling a perf event after attaching a BPF prog to it is
not always desirable.

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

Suggested-by: Jiri Olsa <jolsa@kernel.org>
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>
---
 tools/lib/bpf/libbpf.c | 13 ++++++++-----
 tools/lib/bpf/libbpf.h |  4 +++-
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index fb4d92c5c339..414c566c4650 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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, no_ioctl_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;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index d1cf813a057b..2d3cc436cdbf 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -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 no_ioctl_enable;
 	size_t :0;
 };
-#define bpf_perf_event_opts__last_field force_ioctl_attach
+#define bpf_perf_event_opts__last_field no_ioctl_enable
 
 LIBBPF_API struct bpf_link *
 bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
-- 
2.50.1
Re: [PATCH v3 1/2] libbpf: Add the ability to suppress perf event enablement
Posted by Alexei Starovoitov 2 months ago
On Tue, Aug 5, 2025 at 6:04 AM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> Automatically enabling a perf event after attaching a BPF prog to it is
> not always desirable.
>
> Add a new no_ioctl_enable field to struct bpf_perf_event_opts. While
> introducing ioctl_enable instead would be nicer in that it would avoid
> a double negation in the implementation, it would make
> DECLARE_LIBBPF_OPTS() less efficient.
>
> Suggested-by: Jiri Olsa <jolsa@kernel.org>
> 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>
> ---
>  tools/lib/bpf/libbpf.c | 13 ++++++++-----
>  tools/lib/bpf/libbpf.h |  4 +++-
>  2 files changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index fb4d92c5c339..414c566c4650 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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, no_ioctl_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;
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index d1cf813a057b..2d3cc436cdbf 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -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 no_ioctl_enable;

The patch logic looks fine, but I feel the knob name is too
implementation oriented.
imo "dont_auto_enable" is more descriptive and easier
to reason about.

Let's wait for Eduard/Andrii reviews. This patch has to go
via bpf trees first while the latter via perf.
Re: [PATCH v3 1/2] libbpf: Add the ability to suppress perf event enablement
Posted by Eduard Zingerman 2 months ago
On Tue, 2025-08-05 at 09:45 -0700, Alexei Starovoitov wrote:
> On Tue, Aug 5, 2025 at 6:04 AM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
> > 
> > Automatically enabling a perf event after attaching a BPF prog to it is
> > not always desirable.
> > 
> > Add a new no_ioctl_enable field to struct bpf_perf_event_opts. While
> > introducing ioctl_enable instead would be nicer in that it would avoid
> > a double negation in the implementation, it would make
> > DECLARE_LIBBPF_OPTS() less efficient.
> > 
> > Suggested-by: Jiri Olsa <jolsa@kernel.org>
> > 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>
> > ---

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

[...]

> > --- a/tools/lib/bpf/libbpf.h
> > +++ b/tools/lib/bpf/libbpf.h
> > @@ -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 no_ioctl_enable;
> 
> The patch logic looks fine, but I feel the knob name is too
> implementation oriented.
> imo "dont_auto_enable" is more descriptive and easier
> to reason about.
> 
> Let's wait for Eduard/Andrii reviews. This patch has to go
> via bpf trees first while the latter via perf.

Agree with Alexei,
something like "dont_enable" should be simpler to read.