Skip to contentSkip to conversation

bpf: use proper target btf when exporting attach_btf_obj_id

PATCH Superseded Stanislav Fomichev 3 participants 9 messages Aug 1, 2022 – Aug 3, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 bpf: use proper target btf when exporting attach_btf_obj_id

When attaching to program, the program itself might not be attached to anything (and, hence, might not have attach_btf), so we can't unconditionally use 'prog->aux->dst_prog->aux->attach_btf'. Instead, use bpf_prog_get_target_btf to pick proper target btf:

* when attached to dst_prog, use dst_prog->aux->btf
* when attached to kernel btf, use prog->aux->attach_btf

Fixes b79c9fc9551b ("bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP")
---
 kernel/bpf/syscall.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/bpf/syscall.c +3 -4
index 83c7136c5788..7dc3f8003631 100644
@@ -3886,6 +3886,7 @@ static int bpf_prog_get_info_by_fd(struct file *file,
 				   union bpf_attr __user *uattr)
 {
 	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
+	struct btf *attach_btf = bpf_prog_get_target_btf(prog);
 	struct bpf_prog_info info;
 	u32 info_len = attr->info.info_len;
 	struct bpf_prog_kstats stats;
@@ -4088,10 +4089,8 @@ static int bpf_prog_get_info_by_fd(struct file *file,
 	if (prog->aux->btf)
 		info.btf_id = btf_obj_id(prog->aux->btf);
 	info.attach_btf_id = prog->aux->attach_btf_id;
-	if (prog->aux->attach_btf)
-		info.attach_btf_obj_id = btf_obj_id(prog->aux->attach_btf);
-	else if (prog->aux->dst_prog)
-		info.attach_btf_obj_id = btf_obj_id(prog->aux->dst_prog->aux->attach_btf);
+	if (attach_btf)
+		info.attach_btf_obj_id = btf_obj_id(attach_btf);
 
 	ulen = info.nr_func_info;
 	info.nr_func_info = prog->aux->func_info_cnt;
Signature · 1 line
2.37.1.455.g008518b4e5-goog
#1 lore raw

PATCH v2 2/2 selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

---
 .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
 .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +97 -0
new file mode 100644
index 000000000000..eb06f522c0b3
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <bpf/btf.h>
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "attach_to_bpf.skel.h"
+
+static int find_prog_btf_id(const char *name, __u32 attach_prog_fd)
+{
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	struct btf *btf;
+	int ret;
+
+	ret = bpf_obj_get_info_by_fd(attach_prog_fd, &info, &info_len);
+	if (ret)
+		return ret;
+
+	if (!info.btf_id)
+		return -EINVAL;
+
+	btf = btf__load_from_kernel_by_id(info.btf_id);
+	ret = libbpf_get_error(btf);
+	if (ret)
+		return ret;
+
+	ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+	btf__free(btf);
+	return ret;
+}
+
+int load_fentry(int attach_prog_fd, int attach_btf_id)
+{
+	LIBBPF_OPTS(bpf_prog_load_opts, opts,
+		    .expected_attach_type = BPF_TRACE_FENTRY,
+		    .attach_prog_fd = attach_prog_fd,
+		    .attach_btf_id = attach_btf_id,
+	);
+	struct bpf_insn insns[] = {
+		BPF_MOV64_IMM(BPF_REG_0, 0),
+		BPF_EXIT_INSN(),
+	};
+
+	return bpf_prog_load(BPF_PROG_TYPE_TRACING,
+			     "bind4_fentry",
+			     "GPL",
+			     insns,
+			     ARRAY_SIZE(insns),
+			     &opts);
+}
+
+void test_attach_to_bpf(void)
+{
+	struct attach_to_bpf *skel = NULL;
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	int cgroup_fd = -1;
+	int fentry_fd = -1;
+	int btf_id;
+
+	cgroup_fd = test__join_cgroup("/attach_to_bpf");
+	if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+		return;
+
+	skel = attach_to_bpf__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel"))
+		goto cleanup;
+
+	skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
+	if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))
+		goto cleanup;
+
+	btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
+	if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
+		goto cleanup;
+
+	fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
+	if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
+		goto cleanup;
+
+	/* Make sure bpf_obj_get_info_by_fd works correctly when attaching
+	 * to another BPF program.
+	 */
+
+	ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
+		  "bpf_obj_get_info_by_fd");
+
+	ASSERT_EQ(info.btf_id, 0, "info.btf_id");
+	ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
+	ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
+
+cleanup:
+	close(cgroup_fd);
+	close(fentry_fd);
+	attach_to_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/attach_to_bpf.c new file +12 -0
new file mode 100644
index 000000000000..3f111fe96f8f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/bind4")
+int bind4(struct bpf_sock_addr *ctx)
+{
+	return 1;
+}
+
+char _license[] SEC("license") = "GPL";
Signature · 1 line
2.37.1.455.g008518b4e5-goog
Acked-by on v2 2/2
#2 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

Quoted 6 lines · Stanislav Fomichev

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

Quoted 6 lines

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

I see Martin has comments based on v1, but v2 looks good to me.

Quoted 131 lines
---
 .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
 .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +97 -0
new file mode 100644
index 000000000000..eb06f522c0b3
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <bpf/btf.h>
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "attach_to_bpf.skel.h"
+
+static int find_prog_btf_id(const char *name, __u32 attach_prog_fd)
+{
+       struct bpf_prog_info info = {};
+       __u32 info_len = sizeof(info);
+       struct btf *btf;
+       int ret;
+
+       ret = bpf_obj_get_info_by_fd(attach_prog_fd, &info, &info_len);
+       if (ret)
+               return ret;
+
+       if (!info.btf_id)
+               return -EINVAL;
+
+       btf = btf__load_from_kernel_by_id(info.btf_id);
+       ret = libbpf_get_error(btf);
+       if (ret)
+               return ret;
+
+       ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+       btf__free(btf);
+       return ret;
+}
+
+int load_fentry(int attach_prog_fd, int attach_btf_id)
+{
+       LIBBPF_OPTS(bpf_prog_load_opts, opts,
+                   .expected_attach_type = BPF_TRACE_FENTRY,
+                   .attach_prog_fd = attach_prog_fd,
+                   .attach_btf_id = attach_btf_id,
+       );
+       struct bpf_insn insns[] = {
+               BPF_MOV64_IMM(BPF_REG_0, 0),
+               BPF_EXIT_INSN(),
+       };
+
+       return bpf_prog_load(BPF_PROG_TYPE_TRACING,
+                            "bind4_fentry",
+                            "GPL",
+                            insns,
+                            ARRAY_SIZE(insns),
+                            &opts);
+}
+
+void test_attach_to_bpf(void)
+{
+       struct attach_to_bpf *skel = NULL;
+       struct bpf_prog_info info = {};
+       __u32 info_len = sizeof(info);
+       int cgroup_fd = -1;
+       int fentry_fd = -1;
+       int btf_id;
+
+       cgroup_fd = test__join_cgroup("/attach_to_bpf");
+       if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+               return;
+
+       skel = attach_to_bpf__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel"))
+               goto cleanup;
+
+       skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
+       if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))
+               goto cleanup;
+
+       btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
+       if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
+               goto cleanup;
+
+       fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
+       if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
+               goto cleanup;
+
+       /* Make sure bpf_obj_get_info_by_fd works correctly when attaching
+        * to another BPF program.
+        */
+
+       ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
+                 "bpf_obj_get_info_by_fd");
+
+       ASSERT_EQ(info.btf_id, 0, "info.btf_id");
+       ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
+       ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
+
+cleanup:
+       close(cgroup_fd);
+       close(fentry_fd);
+       attach_to_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/attach_to_bpf.c new file +12 -1
new file mode 100644
index 000000000000..3f111fe96f8f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/bind4")
+int bind4(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
--

2.37.1.455.g008518b4e5-goog

#3 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

Quoted 13 lines · Hao Luo
Quoted 11 lines · Stanislav Fomichev

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

Quoted 5 lines
Quoted 1 line

I see Martin has comments based on v1, but v2 looks good to me.

Thank you for the review! I'll move some stuff around for v3, so I'll have to ask you to have one more :-(

#4 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

In reply to Stanislav Fomichev
Quoted 19 lines · Stanislav Fomichev

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

---
 .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
 .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c

[...]

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 33

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +7 -0
+
+       ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+       btf__free(btf);
+       return ret;
+}
+
+int load_fentry(int attach_prog_fd, int attach_btf_id)

static?

Quoted 28 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +28 -0
+{
+       LIBBPF_OPTS(bpf_prog_load_opts, opts,
+                   .expected_attach_type = BPF_TRACE_FENTRY,
+                   .attach_prog_fd = attach_prog_fd,
+                   .attach_btf_id = attach_btf_id,
+       );
+       struct bpf_insn insns[] = {
+               BPF_MOV64_IMM(BPF_REG_0, 0),
+               BPF_EXIT_INSN(),
+       };
+
+       return bpf_prog_load(BPF_PROG_TYPE_TRACING,
+                            "bind4_fentry",
+                            "GPL",
+                            insns,
+                            ARRAY_SIZE(insns),
+                            &opts);
+}
+
+void test_attach_to_bpf(void)
+{
+       struct attach_to_bpf *skel = NULL;
+       struct bpf_prog_info info = {};
+       __u32 info_len = sizeof(info);
+       int cgroup_fd = -1;
+       int fentry_fd = -1;
+       int btf_id;
+

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 71

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +10 -0
+       cgroup_fd = test__join_cgroup("/attach_to_bpf");
+       if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+               return;
+
+       skel = attach_to_bpf__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel"))
+               goto cleanup;
+
+       skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
+       if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))

you probably meant to check skel->links.bind4 instead of just skel (which you already checked)

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +12 -0
+               goto cleanup;
+
+       btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
+       if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
+               goto cleanup;
+
+       fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
+       if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
+               goto cleanup;
+
+       /* Make sure bpf_obj_get_info_by_fd works correctly when attaching
+        * to another BPF program.

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 93

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +10 -0
+        */
+
+       ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
+                 "bpf_obj_get_info_by_fd");
+
+       ASSERT_EQ(info.btf_id, 0, "info.btf_id");
+       ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
+       ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
+
+cleanup:

if (cgroup_fd >= 0)

Quoted 1 line

+ close(cgroup_fd);

if (fentry_fd >= 0)

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +3 -0
+       close(fentry_fd);
+       attach_to_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/attach_to_bpf.c new file +12 -1
new file mode 100644
index 000000000000..3f111fe96f8f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/bind4")
+int bind4(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
--

2.37.1.455.g008518b4e5-goog

#5 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

Quoted 67 lines · Andrii Nakryiko
Quoted 19 lines · Stanislav Fomichev

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

---
 .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
 .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c

[...]

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 33

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +7 -0
+
+       ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+       btf__free(btf);
+       return ret;
+}
+
+int load_fentry(int attach_prog_fd, int attach_btf_id)

static?

Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +33 -0
+{
+       LIBBPF_OPTS(bpf_prog_load_opts, opts,
+                   .expected_attach_type = BPF_TRACE_FENTRY,
+                   .attach_prog_fd = attach_prog_fd,
+                   .attach_btf_id = attach_btf_id,
+       );
+       struct bpf_insn insns[] = {
+               BPF_MOV64_IMM(BPF_REG_0, 0),
+               BPF_EXIT_INSN(),
+       };
+
+       return bpf_prog_load(BPF_PROG_TYPE_TRACING,
+                            "bind4_fentry",
+                            "GPL",
+                            insns,
+                            ARRAY_SIZE(insns),
+                            &opts);
+}
+
+void test_attach_to_bpf(void)
+{
+       struct attach_to_bpf *skel = NULL;
+       struct bpf_prog_info info = {};
+       __u32 info_len = sizeof(info);
+       int cgroup_fd = -1;
+       int fentry_fd = -1;
+       int btf_id;
+
+       cgroup_fd = test__join_cgroup("/attach_to_bpf");
+       if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+               return;
+
+       skel = attach_to_bpf__open_and_load();
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 71

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +5 -0
+       if (!ASSERT_OK_PTR(skel, "skel"))
+               goto cleanup;
+
+       skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
+       if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))

you probably meant to check skel->links.bind4 instead of just skel (which you already checked)

Oh, good catch, thanks!

Quoted 25 lines
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +12 -0
+               goto cleanup;
+
+       btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
+       if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
+               goto cleanup;
+
+       fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
+       if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
+               goto cleanup;
+
+       /* Make sure bpf_obj_get_info_by_fd works correctly when attaching
+        * to another BPF program.

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 93

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +10 -0
+        */
+
+       ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
+                 "bpf_obj_get_info_by_fd");
+
+       ASSERT_EQ(info.btf_id, 0, "info.btf_id");
+       ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
+       ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
+
+cleanup:

if (cgroup_fd >= 0)

Quoted 3 lines
Quoted 1 line

+ close(cgroup_fd);

if (fentry_fd >= 0)

Should be safe to do unconditional close(-1), right? Why bother with the checks here? Seems like a common pattern we do elsewhere?

Quoted 24 lines
Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +3 -0
+       close(fentry_fd);
+       attach_to_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/attach_to_bpf.c new file +12 -1
new file mode 100644
index 000000000000..3f111fe96f8f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/bind4")
+int bind4(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
--

2.37.1.455.g008518b4e5-goog

#6 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

Quoted 106 lines · Stanislav Fomichev
Quoted 67 lines · Andrii Nakryiko
Quoted 19 lines · Stanislav Fomichev

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

---
 .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
 .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c

[...]

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 33

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +7 -0
+
+       ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+       btf__free(btf);
+       return ret;
+}
+
+int load_fentry(int attach_prog_fd, int attach_btf_id)

static?

Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +33 -0
+{
+       LIBBPF_OPTS(bpf_prog_load_opts, opts,
+                   .expected_attach_type = BPF_TRACE_FENTRY,
+                   .attach_prog_fd = attach_prog_fd,
+                   .attach_btf_id = attach_btf_id,
+       );
+       struct bpf_insn insns[] = {
+               BPF_MOV64_IMM(BPF_REG_0, 0),
+               BPF_EXIT_INSN(),
+       };
+
+       return bpf_prog_load(BPF_PROG_TYPE_TRACING,
+                            "bind4_fentry",
+                            "GPL",
+                            insns,
+                            ARRAY_SIZE(insns),
+                            &opts);
+}
+
+void test_attach_to_bpf(void)
+{
+       struct attach_to_bpf *skel = NULL;
+       struct bpf_prog_info info = {};
+       __u32 info_len = sizeof(info);
+       int cgroup_fd = -1;
+       int fentry_fd = -1;
+       int btf_id;
+
+       cgroup_fd = test__join_cgroup("/attach_to_bpf");
+       if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+               return;
+
+       skel = attach_to_bpf__open_and_load();
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 71

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +5 -0
+       if (!ASSERT_OK_PTR(skel, "skel"))
+               goto cleanup;
+
+       skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
+       if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))

you probably meant to check skel->links.bind4 instead of just skel (which you already checked)

Oh, good catch, thanks!

Quoted 25 lines
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +12 -0
+               goto cleanup;
+
+       btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
+       if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
+               goto cleanup;
+
+       fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
+       if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
+               goto cleanup;
+
+       /* Make sure bpf_obj_get_info_by_fd works correctly when attaching
+        * to another BPF program.

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 93

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +10 -0
+        */
+
+       ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
+                 "bpf_obj_get_info_by_fd");
+
+       ASSERT_EQ(info.btf_id, 0, "info.btf_id");
+       ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
+       ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
+
+cleanup:

if (cgroup_fd >= 0)

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+ close(cgroup_fd);

if (fentry_fd >= 0)

Should be safe to do unconditional close(-1), right? Why bother with the checks here? Seems like a common pattern we do elsewhere?

I don't think we consciously do close(-1), libbpf definitely tries hard to not attempt to close invalid fd, and so do (most?) of selftests. Where do you see use doing close(-1)?

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +3 -0
+       close(fentry_fd);
+       attach_to_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/attach_to_bpf.c new file +12 -1
new file mode 100644
index 000000000000..3f111fe96f8f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/bind4")
+int bind4(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
--

2.37.1.455.g008518b4e5-goog

#7 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

Quoted 112 lines · Andrii Nakryiko
Quoted 110 lines · Stanislav Fomichev
Quoted 67 lines · Andrii Nakryiko
Quoted 19 lines · Stanislav Fomichev

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

---
 .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
 .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c

[...]

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 33

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +7 -0
+
+       ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+       btf__free(btf);
+       return ret;
+}
+
+int load_fentry(int attach_prog_fd, int attach_btf_id)

static?

Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +33 -0
+{
+       LIBBPF_OPTS(bpf_prog_load_opts, opts,
+                   .expected_attach_type = BPF_TRACE_FENTRY,
+                   .attach_prog_fd = attach_prog_fd,
+                   .attach_btf_id = attach_btf_id,
+       );
+       struct bpf_insn insns[] = {
+               BPF_MOV64_IMM(BPF_REG_0, 0),
+               BPF_EXIT_INSN(),
+       };
+
+       return bpf_prog_load(BPF_PROG_TYPE_TRACING,
+                            "bind4_fentry",
+                            "GPL",
+                            insns,
+                            ARRAY_SIZE(insns),
+                            &opts);
+}
+
+void test_attach_to_bpf(void)
+{
+       struct attach_to_bpf *skel = NULL;
+       struct bpf_prog_info info = {};
+       __u32 info_len = sizeof(info);
+       int cgroup_fd = -1;
+       int fentry_fd = -1;
+       int btf_id;
+
+       cgroup_fd = test__join_cgroup("/attach_to_bpf");
+       if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+               return;
+
+       skel = attach_to_bpf__open_and_load();
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 71

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +5 -0
+       if (!ASSERT_OK_PTR(skel, "skel"))
+               goto cleanup;
+
+       skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
+       if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))

you probably meant to check skel->links.bind4 instead of just skel (which you already checked)

Oh, good catch, thanks!

Quoted 28 lines
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +12 -0
+               goto cleanup;
+
+       btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
+       if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
+               goto cleanup;
+
+       fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
+       if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
+               goto cleanup;
+
+       /* Make sure bpf_obj_get_info_by_fd works correctly when attaching
+        * to another BPF program.

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 93

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +10 -0
+        */
+
+       ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
+                 "bpf_obj_get_info_by_fd");
+
+       ASSERT_EQ(info.btf_id, 0, "info.btf_id");
+       ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
+       ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
+
+cleanup:

if (cgroup_fd >= 0)

Quoted 1 line

+ close(cgroup_fd);

if (fentry_fd >= 0)

Quoted 7 lines
Quoted 3 lines

Should be safe to do unconditional close(-1), right? Why bother with the checks here? Seems like a common pattern we do elsewhere?

I don't think we consciously do close(-1), libbpf definitely tries hard to not attempt to close invalid fd, and so do (most?) of selftests. Where do you see use doing close(-1)?

I might have contributed to this :-/ Everything is from prog_tests:
sockopt_multi.c (test_sockopt_multi)
lsm_cgroup.c (test_lsm_cgroup_functional)

But there is more that haven't been added by me:
d_path.c (trigger_fstat_events) cgroup_attach_override.c (serial_test_cgroup_attach_override) cg_storage_multi.c (connect_send) test_local_storage.c (test_test_local_storage) bpf_cookie.c (kprobe_multi_link_api_subtest) fexit_bpf2bpf.c (test_fentry_to_cgroup_bpf)
(I stopped here, maybe there is more?)

I'm not sure how much we should care about these 'if (fd >= 0)' checks. It might be it's easier to always do close(-1), otherwise we get bugs like the one in test_unpriv_bpf_disabled_positive from unpriv_bpf_disabled.c:
if (link_fd)
close(link_fd);

(but I'm also happy to add those 'ifs' if you prefer, lmk)

?

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +3 -0
+       close(fentry_fd);
+       attach_to_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/attach_to_bpf.c new file +12 -1
new file mode 100644
index 000000000000..3f111fe96f8f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/bind4")
+int bind4(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
--

2.37.1.455.g008518b4e5-goog

#8 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf

Quoted 143 lines · Stanislav Fomichev
Quoted 112 lines · Andrii Nakryiko
Quoted 110 lines · Stanislav Fomichev
Quoted 67 lines · Andrii Nakryiko
Quoted 19 lines · Stanislav Fomichev

Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program
should return non-zero btf_id/btf_obj_id instead of crashing the kernel.

v2:
- use ret instead of err in find_prog_btf_id (Hao)
- remove verifier log (Hao)
- drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao)

---
 .../selftests/bpf/prog_tests/attach_to_bpf.c  | 97 +++++++++++++++++++
 .../selftests/bpf/progs/attach_to_bpf.c       | 12 +++
 2 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c

[...]

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 33

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +7 -0
+
+       ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
+       btf__free(btf);
+       return ret;
+}
+
+int load_fentry(int attach_prog_fd, int attach_btf_id)

static?

Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +33 -0
+{
+       LIBBPF_OPTS(bpf_prog_load_opts, opts,
+                   .expected_attach_type = BPF_TRACE_FENTRY,
+                   .attach_prog_fd = attach_prog_fd,
+                   .attach_btf_id = attach_btf_id,
+       );
+       struct bpf_insn insns[] = {
+               BPF_MOV64_IMM(BPF_REG_0, 0),
+               BPF_EXIT_INSN(),
+       };
+
+       return bpf_prog_load(BPF_PROG_TYPE_TRACING,
+                            "bind4_fentry",
+                            "GPL",
+                            insns,
+                            ARRAY_SIZE(insns),
+                            &opts);
+}
+
+void test_attach_to_bpf(void)
+{
+       struct attach_to_bpf *skel = NULL;
+       struct bpf_prog_info info = {};
+       __u32 info_len = sizeof(info);
+       int cgroup_fd = -1;
+       int fentry_fd = -1;
+       int btf_id;
+
+       cgroup_fd = test__join_cgroup("/attach_to_bpf");
+       if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+               return;
+
+       skel = attach_to_bpf__open_and_load();
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 71

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +5 -0
+       if (!ASSERT_OK_PTR(skel, "skel"))
+               goto cleanup;
+
+       skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd);
+       if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup"))

you probably meant to check skel->links.bind4 instead of just skel (which you already checked)

Oh, good catch, thanks!

Quoted 28 lines
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +12 -0
+               goto cleanup;
+
+       btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4));
+       if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id"))
+               goto cleanup;
+
+       fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id);
+       if (!ASSERT_GE(fentry_fd, 0, "load_fentry"))
+               goto cleanup;
+
+       /* Make sure bpf_obj_get_info_by_fd works correctly when attaching
+        * to another BPF program.

tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c line 93

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +10 -0
+        */
+
+       ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len),
+                 "bpf_obj_get_info_by_fd");
+
+       ASSERT_EQ(info.btf_id, 0, "info.btf_id");
+       ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id");
+       ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id");
+
+cleanup:

if (cgroup_fd >= 0)

Quoted 1 line

+ close(cgroup_fd);

if (fentry_fd >= 0)

Quoted 7 lines
Quoted 3 lines

Should be safe to do unconditional close(-1), right? Why bother with the checks here? Seems like a common pattern we do elsewhere?

I don't think we consciously do close(-1), libbpf definitely tries hard to not attempt to close invalid fd, and so do (most?) of selftests. Where do you see use doing close(-1)?

I might have contributed to this :-/ Everything is from prog_tests:
sockopt_multi.c (test_sockopt_multi)
lsm_cgroup.c (test_lsm_cgroup_functional)

But there is more that haven't been added by me:
d_path.c (trigger_fstat_events) cgroup_attach_override.c (serial_test_cgroup_attach_override) cg_storage_multi.c (connect_send) test_local_storage.c (test_test_local_storage) bpf_cookie.c (kprobe_multi_link_api_subtest) fexit_bpf2bpf.c (test_fentry_to_cgroup_bpf)
(I stopped here, maybe there is more?)

I'm not sure how much we should care about these 'if (fd >= 0)' checks. It might be it's easier to always do close(-1), otherwise we get bugs like the one in test_unpriv_bpf_disabled_positive from unpriv_bpf_disabled.c:
if (link_fd)
close(link_fd);

Quoted 4 lines

(but I'm also happy to add those 'ifs' if you prefer, lmk)

?

I'd rather have non-sloppy clean up sections.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file +3 -0
+       close(fentry_fd);
+       attach_to_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/attach_to_bpf.c new file +12 -1
new file mode 100644
index 000000000000..3f111fe96f8f
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("cgroup/bind4")
+int bind4(struct bpf_sock_addr *ctx)
+{
+       return 1;
+}
+
+char _license[] SEC("license") = "GPL";
--

2.37.1.455.g008518b4e5-goog