Skip to content

Commit 9474e27

Browse files
iii-iAlexei Starovoitov
authored andcommitted
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 <[email protected]> Suggested-by: Jiri Olsa <[email protected]> Tested-by: Thomas Richter <[email protected]> Co-developed-by: Thomas Richter <[email protected]> Signed-off-by: Thomas Richter <[email protected]> Signed-off-by: Ilya Leoshkevich <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1b30d44 commit 9474e27

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10965,11 +10965,14 @@ struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *p
1096510965
}
1096610966
link->link.fd = pfd;
1096710967
}
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+
}
1097310976
}
1097410977

1097510978
return &link->link;

tools/lib/bpf/libbpf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,11 @@ struct bpf_perf_event_opts {
499499
__u64 bpf_cookie;
500500
/* don't use BPF link when attach BPF program */
501501
bool force_ioctl_attach;
502+
/* don't automatically enable the event */
503+
bool dont_enable;
502504
size_t :0;
503505
};
504-
#define bpf_perf_event_opts__last_field force_ioctl_attach
506+
#define bpf_perf_event_opts__last_field dont_enable
505507

506508
LIBBPF_API struct bpf_link *
507509
bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);

0 commit comments

Comments
 (0)