Skip to contentSkip to conversation

libbpf: introduce line_info and func_info getters

PATCH Superseded Mykyta Yatsenko 2 participants 4 messages Apr 1, 2025

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover libbpf: introduce line_info and func_info getters

From: Mykyta Yatsenko <[email protected]>

This patchset introduces new libbpf API getters that enable the retrieval of .BTF.ext line and func info.
This change enables users to load bpf_program directly using bpf_prog_load, bypassing the higher-level bpf_object__load API. Providing line and function info is essential for BPF program verification in some cases.

v2 -> v3
* Return ENOTSUPP if func or line info struct size differs from the one in uapi linux headers.
* Add selftests.

v1 -> v2
Move bpf_line_info_min and bpf_func_info_min from libbpf_internal.h to btf.h. Did not remove _min suffix, because there already are bpf_line_info and bpf_func_info structs in uapi/../bpf.h.

Mykyta Yatsenko (2):
libbpf: add getters for BTF.ext func and line info selftests/bpf: add BTF.ext line/func info getter tests

 tools/lib/bpf/libbpf.c                        |  24 ++++
 tools/lib/bpf/libbpf.h                        |   6 +
 tools/lib/bpf/libbpf.map                      |   4 +
 .../selftests/bpf/prog_tests/test_btf_ext.c   | 111 ++++++++++++++++++
 .../selftests/bpf/progs/test_btf_ext.c        |  21 ++++
 5 files changed, 166 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_btf_ext.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH v3 1/2 libbpf: add getters for BTF.ext func and line info

From: Mykyta Yatsenko <[email protected]>

Introducing new libbpf API getters for BTF.ext func and line info, namely:
bpf_program__func_info
bpf_program__func_info_cnt
bpf_program__line_info
bpf_program__line_info_cnt

This change enables scenarios, when user needs to load bpf_program directly using bpf_prog_load, instead of higher-level bpf_object__load. Line and func info are required for checking BTF info in verifier; verification may fail without these fields if, for example, program calls bpf_obj_new.

---
 tools/lib/bpf/libbpf.c   | 24 ++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  4 ++++
 3 files changed, 34 insertions(+)
tools/lib/bpf/libbpf.c +24 -0
index 6b85060f07b3..551a8514dc7d 100644
@@ -9455,6 +9455,30 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log
 	return 0;
 }
 
+struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog)
+{
+	if (prog->func_info_rec_size != sizeof(struct bpf_func_info))
+		return libbpf_err_ptr(-EOPNOTSUPP);
+	return prog->func_info;
+}
+
+__u32 bpf_program__func_info_cnt(const struct bpf_program *prog)
+{
+	return prog->func_info_cnt;
+}
+
+struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog)
+{
+	if (prog->line_info_rec_size != sizeof(struct bpf_line_info))
+		return libbpf_err_ptr(-EOPNOTSUPP);
+	return prog->line_info;
+}
+
+__u32 bpf_program__line_info_cnt(const struct bpf_program *prog)
+{
+	return prog->line_info_cnt;
+}
+
 #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) {			    \
 	.sec = (char *)sec_pfx,						    \
 	.prog_type = BPF_PROG_TYPE_##ptype,				    \
tools/lib/bpf/libbpf.h +6 -0
index e0605403f977..d39f19c8396d 100644
@@ -940,6 +940,12 @@ LIBBPF_API int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_le
 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size);
 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size);
 
+LIBBPF_API struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog);
+LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog);
+
+LIBBPF_API struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog);
+LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog);
+
 /**
  * @brief **bpf_program__set_attach_target()** sets BTF-based attach target
  * for supported BPF program types:
tools/lib/bpf/libbpf.map +4 -0
index d8b71f22f197..1205f9a4fe04 100644
@@ -437,6 +437,10 @@ LIBBPF_1.6.0 {
 		bpf_linker__add_fd;
 		bpf_linker__new_fd;
 		bpf_object__prepare;
+		bpf_program__func_info;
+		bpf_program__func_info_cnt;
+		bpf_program__line_info;
+		bpf_program__line_info_cnt;
 		btf__add_decl_attr;
 		btf__add_type_attr;
 } LIBBPF_1.5.0;
Signature · 1 line
2.49.0
#2 lore raw

PATCH v3 2/2 selftests/bpf: add .BTF.ext line/func info getter tests

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add selftests checking that line and func info retrieved by newly added libbpf APIs are the same as returned by kernel via bpf_prog_get_info_by_fd.

---
 .../selftests/bpf/prog_tests/test_btf_ext.c   | 111 ++++++++++++++++++
 .../selftests/bpf/progs/test_btf_ext.c        |  21 ++++
 2 files changed, 132 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_btf_ext.c
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +111 -0
new file mode 100644
index 000000000000..00aeebc8f863
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include <test_progs.h>
+#include "test_btf_ext.skel.h"
+#include "btf_helpers.h"
+
+static void subtest_line_info(void)
+{
+	struct test_btf_ext *skel;
+	struct bpf_prog_info info;
+	struct bpf_line_info line_info[1024], *libbpf_line_info;
+	__u32 info_len = sizeof(info), libbbpf_line_info_cnt;
+	int err, fd, i;
+
+	skel = test_btf_ext__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	bpf_program__set_autoload(skel->progs.global_func, true);
+
+	err = test_btf_ext__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto out;
+
+	fd = bpf_program__fd(skel->progs.global_func);
+
+	memset(&info, 0, sizeof(info));
+	info.line_info = ptr_to_u64(&line_info);
+	info.nr_line_info = sizeof(line_info);
+	info.line_info_rec_size = sizeof(*line_info);
+	err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+	if (!ASSERT_OK(err, "prog_info"))
+		goto out;
+
+	libbpf_line_info = bpf_program__line_info(skel->progs.global_func);
+	libbbpf_line_info_cnt = bpf_program__line_info_cnt(skel->progs.global_func);
+
+	if (!ASSERT_OK_PTR(libbpf_line_info, "bpf_program__line_info"))
+		goto out;
+	if (!ASSERT_GT(libbbpf_line_info_cnt, 0, "line_info_cnt>0"))
+		goto out;
+	if (!ASSERT_EQ(libbbpf_line_info_cnt, info.nr_line_info, "line_info_cnt"))
+		goto out;
+
+	for (i = 0; i < libbbpf_line_info_cnt; ++i) {
+		int eq = memcmp(libbpf_line_info + i, line_info + i, sizeof(*line_info));
+
+		if (!ASSERT_EQ(eq, 0, "line_info"))
+			goto out;
+	}
+
+out:
+	test_btf_ext__destroy(skel);
+}
+
+static void subtest_func_info(void)
+{
+	struct test_btf_ext *skel;
+	struct bpf_prog_info info;
+	struct bpf_func_info func_info[1024], *libbpf_func_info;
+	__u32 info_len = sizeof(info), libbbpf_func_info_cnt;
+	int err, fd, i;
+
+	skel = test_btf_ext__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	bpf_program__set_autoload(skel->progs.global_func, true);
+
+	err = test_btf_ext__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto out;
+
+	fd = bpf_program__fd(skel->progs.global_func);
+
+	memset(&info, 0, sizeof(info));
+	info.func_info = ptr_to_u64(&func_info);
+	info.nr_func_info = sizeof(func_info);
+	info.func_info_rec_size = sizeof(*func_info);
+	err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+	if (!ASSERT_OK(err, "prog_info"))
+		goto out;
+
+	libbpf_func_info = bpf_program__func_info(skel->progs.global_func);
+	libbbpf_func_info_cnt = bpf_program__func_info_cnt(skel->progs.global_func);
+
+	if (!ASSERT_OK_PTR(libbpf_func_info, "bpf_program__func_info"))
+		goto out;
+	if (!ASSERT_GT(libbbpf_func_info_cnt, 0, "func_info_cnt>0"))
+		goto out;
+	if (!ASSERT_EQ(libbbpf_func_info_cnt, info.nr_func_info, "func_info_cnt"))
+		goto out;
+
+	for (i = 0; i < libbbpf_func_info_cnt; ++i) {
+		int eq = memcmp(libbpf_func_info + i, func_info + i, sizeof(*func_info));
+
+		if (!ASSERT_EQ(eq, 0, "func_info"))
+			goto out;
+	}
+
+out:
+	test_btf_ext__destroy(skel);
+}
+
+void test_btf_ext(void)
+{
+	if (test__start_subtest("func_info"))
+		subtest_func_info();
+	if (test__start_subtest("line_info"))
+		subtest_line_info();
+}
tools/testing/selftests/bpf/progs/test_btf_ext.c new file +21 -0
new file mode 100644
index 000000000000..be92e445a988
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms Inc. */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+const volatile int val = 3;
+
+static __attribute__ ((noinline))
+int f0(int var)
+{
+	int retval = var + val;
+
+	return retval;
+}
+
+SEC("tc")
+int global_func(struct __sk_buff *skb)
+{
+	return f0(skb->len);
+}
Signature · 1 line
2.49.0
#3 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: add .BTF.ext line/func info getter tests

Quoted 6 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add selftests checking that line and func info retrieved by newly added libbpf APIs are the same as returned by kernel via bpf_prog_get_info_by_fd.

Quoted 8 lines
---
 .../selftests/bpf/prog_tests/test_btf_ext.c   | 111 ++++++++++++++++++
 .../selftests/bpf/progs/test_btf_ext.c        |  21 ++++
 2 files changed, 132 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_btf_ext.c

Seems like s390x isn't happy about this test, please check what's going on there. See some nits below as well.

API-wise everything looks good, though.

pw-bot: cr

tools/testing/selftests/bpf/prog_tests/test_btf_ext.c line 2

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +2 -0
new file mode 100644
index 000000000000..00aeebc8f863
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */

outdated copy/paste

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +7 -0
+#include <test_progs.h>
+#include "test_btf_ext.skel.h"
+#include "btf_helpers.h"
+
+static void subtest_line_info(void)
+{
+       struct test_btf_ext *skel;

tools/testing/selftests/bpf/prog_tests/test_btf_ext.c line 19

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +10 -0
+       struct bpf_prog_info info;
+       struct bpf_line_info line_info[1024], *libbpf_line_info;
+       __u32 info_len = sizeof(info), libbbpf_line_info_cnt;
+       int err, fd, i;
+
+       skel = test_btf_ext__open();
+       if (!ASSERT_OK_PTR(skel, "skel_open"))
+               return;
+
+       bpf_program__set_autoload(skel->progs.global_func, true);

no need to set this explicitly, it will be auto-loaded anyways

tools/testing/selftests/bpf/prog_tests/test_btf_ext.c line 23

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +4 -0
+
+       err = test_btf_ext__load(skel);
+       if (!ASSERT_OK(err, "skel_load"))
+               goto out;

you can actually shorten these two steps to test_btf_ext__open_and_load()

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +10 -0
+
+       fd = bpf_program__fd(skel->progs.global_func);
+
+       memset(&info, 0, sizeof(info));
+       info.line_info = ptr_to_u64(&line_info);
+       info.nr_line_info = sizeof(line_info);
+       info.line_info_rec_size = sizeof(*line_info);
+       err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+       if (!ASSERT_OK(err, "prog_info"))
+               goto out;

tools/testing/selftests/bpf/prog_tests/test_btf_ext.c line 43

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +10 -0
+
+       libbpf_line_info = bpf_program__line_info(skel->progs.global_func);
+       libbbpf_line_info_cnt = bpf_program__line_info_cnt(skel->progs.global_func);
+
+       if (!ASSERT_OK_PTR(libbpf_line_info, "bpf_program__line_info"))
+               goto out;
+       if (!ASSERT_GT(libbbpf_line_info_cnt, 0, "line_info_cnt>0"))
+               goto out;
+       if (!ASSERT_EQ(libbbpf_line_info_cnt, info.nr_line_info, "line_info_cnt"))
+               goto out;

hm.., why _GT and _EQ?... Shouldn't just ASSERT_EQ be enough?

tools/testing/selftests/bpf/prog_tests/test_btf_ext.c line 50

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +7 -0
+
+       for (i = 0; i < libbbpf_line_info_cnt; ++i) {
+               int eq = memcmp(libbpf_line_info + i, line_info + i, sizeof(*line_info));
+
+               if (!ASSERT_EQ(eq, 0, "line_info"))
+                       goto out;
+       }

seems like we have ASSERT_MEMEQ(), let's use that? I'd probably get rid of the loop to not spam test output too much. Just one big ASSERT_MEMEQ() should be good enough (though admittedly would be harder to debug, if something breaks... but we don't expect that, so it's probably fine a tradeoff)

Quoted 50 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +50 -0
+
+out:
+       test_btf_ext__destroy(skel);
+}
+
+static void subtest_func_info(void)
+{
+       struct test_btf_ext *skel;
+       struct bpf_prog_info info;
+       struct bpf_func_info func_info[1024], *libbpf_func_info;
+       __u32 info_len = sizeof(info), libbbpf_func_info_cnt;
+       int err, fd, i;
+
+       skel = test_btf_ext__open();
+       if (!ASSERT_OK_PTR(skel, "skel_open"))
+               return;
+
+       bpf_program__set_autoload(skel->progs.global_func, true);
+
+       err = test_btf_ext__load(skel);
+       if (!ASSERT_OK(err, "skel_load"))
+               goto out;
+
+       fd = bpf_program__fd(skel->progs.global_func);
+
+       memset(&info, 0, sizeof(info));
+       info.func_info = ptr_to_u64(&func_info);
+       info.nr_func_info = sizeof(func_info);
+       info.func_info_rec_size = sizeof(*func_info);
+       err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+       if (!ASSERT_OK(err, "prog_info"))
+               goto out;
+
+       libbpf_func_info = bpf_program__func_info(skel->progs.global_func);
+       libbbpf_func_info_cnt = bpf_program__func_info_cnt(skel->progs.global_func);
+
+       if (!ASSERT_OK_PTR(libbpf_func_info, "bpf_program__func_info"))
+               goto out;
+       if (!ASSERT_GT(libbbpf_func_info_cnt, 0, "func_info_cnt>0"))
+               goto out;
+       if (!ASSERT_EQ(libbbpf_func_info_cnt, info.nr_func_info, "func_info_cnt"))
+               goto out;
+
+       for (i = 0; i < libbbpf_func_info_cnt; ++i) {
+               int eq = memcmp(libbpf_func_info + i, func_info + i, sizeof(*func_info));
+
+               if (!ASSERT_EQ(eq, 0, "func_info"))
+                       goto out;
+       }
+

tools/testing/selftests/bpf/prog_tests/test_btf_ext.c line 110

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +10 -0
+out:
+       test_btf_ext__destroy(skel);
+}
+
+void test_btf_ext(void)
+{
+       if (test__start_subtest("func_info"))
+               subtest_func_info();
+       if (test__start_subtest("line_info"))
+               subtest_line_info();

nit: I'd combine those two subtests into one test, same program, same logic to get data, two ASSERT_MEMEQ(), not sure subtest is buying us anything

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +1 -0
+}
tools/testing/selftests/bpf/progs/test_btf_ext.c new file +0 -0
new file mode 100644
index 000000000000..be92e445a988

tools/testing/selftests/bpf/progs/test_btf_ext.c line 9

Quoted 9 lines
tools/testing/selftests/bpf/progs/test_btf_ext.c new file +9 -0
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms Inc. */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+const volatile int val = 3;
+
+static __attribute__ ((noinline))

use __noinline, it's defined in bpf_helpers.h

Quoted 15 lines
tools/testing/selftests/bpf/progs/test_btf_ext.c new file +12 -1
+int f0(int var)
+{
+       int retval = var + val;
+
+       return retval;
+}
+
+SEC("tc")
+int global_func(struct __sk_buff *skb)
+{
+       return f0(skb->len);
+}
--

2.49.0