[PATCH bpf v3 2/3] libbpf: provide a valid attach_prog_fd before probing BPF_PROG_TYPE_EXT type

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Since an empty attach_prog_fd is no longer allowed when loading EXT bpf
program, to avoid BPF_PROG_TYPE_EXT type detection failure, just provide
a valid one before probing.

Signed-off-by: Tengda Wu <wutengda@xxxxxxxxxxxxxxx>
---
 tools/lib/bpf/libbpf_probes.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
index 9dfbe7750f56..2b09acc81be7 100644
--- a/tools/lib/bpf/libbpf_probes.c
+++ b/tools/lib/bpf/libbpf_probes.c
@@ -112,6 +112,7 @@ static int probe_prog_load(enum bpf_prog_type prog_type,
 	int fd, err, exp_err = 0;
 	const char *exp_msg = NULL;
 	char buf[4096];
+	int attach_prog_fd = -1;
 
 	switch (prog_type) {
 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
@@ -148,9 +149,17 @@ static int probe_prog_load(enum bpf_prog_type prog_type,
 		opts.log_size = sizeof(buf);
 		opts.log_level = 1;
 		opts.attach_btf_id = 1;
+		/* use socket_filter as the target program to attach, because it
+		 * is the earliest and most likely BPF program to be supported.
+		 */
+		attach_prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL,
+					       "GPL", insns, insns_cnt, NULL);
+		if (attach_prog_fd < 0)
+			return attach_prog_fd;
+		opts.attach_prog_fd = attach_prog_fd;
 
 		exp_err = -EINVAL;
-		exp_msg = "Cannot replace kernel functions";
+		exp_msg = "FENTRY/FEXIT program can only be attached to another program annotated with BTF";
 		break;
 	case BPF_PROG_TYPE_SYSCALL:
 		opts.prog_flags = BPF_F_SLEEPABLE;
@@ -192,6 +201,8 @@ static int probe_prog_load(enum bpf_prog_type prog_type,
 	err = -errno;
 	if (fd >= 0)
 		close(fd);
+	if (attach_prog_fd >= 0)
+		close(attach_prog_fd);
 	if (exp_err) {
 		if (fd >= 0 || err != exp_err)
 			return 0;
-- 
2.34.1





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux