BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters
@ 2025-04-08 23:44 Mykyta Yatsenko
  2025-04-08 23:44 ` [PATCH bpf-next v5 1/2] libbpf: add getters for BTF.ext func and line info Mykyta Yatsenko
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mykyta Yatsenko @ 2025-04-08 23:44 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

From: Mykyta Yatsenko <yatsenko@meta.com>

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.

v3 -> v5
 * Fix tests on s390x, nits.
 
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   | 64 +++++++++++++++++++
 .../selftests/bpf/progs/test_btf_ext.c        | 22 +++++++
 5 files changed, 120 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

-- 
2.49.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v5 1/2] libbpf: add getters for BTF.ext func and line info
  2025-04-08 23:44 [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters Mykyta Yatsenko
@ 2025-04-08 23:44 ` Mykyta Yatsenko
  2025-04-08 23:44 ` [PATCH bpf-next v5 2/2] selftests/bpf: add BTF.ext line/func info getter tests Mykyta Yatsenko
  2025-04-09 23:40 ` [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Mykyta Yatsenko @ 2025-04-08 23:44 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

From: Mykyta Yatsenko <yatsenko@meta.com>

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`.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 tools/lib/bpf/libbpf.c   | 24 ++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  6 ++++++
 tools/lib/bpf/libbpf.map |  4 ++++
 3 files changed, 34 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6b85060f07b3..551a8514dc7d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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,				    \
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index e0605403f977..d39f19c8396d 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -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:
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index d8b71f22f197..1205f9a4fe04 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -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;
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v5 2/2] selftests/bpf: add BTF.ext line/func info getter tests
  2025-04-08 23:44 [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters Mykyta Yatsenko
  2025-04-08 23:44 ` [PATCH bpf-next v5 1/2] libbpf: add getters for BTF.ext func and line info Mykyta Yatsenko
@ 2025-04-08 23:44 ` Mykyta Yatsenko
  2025-04-09 23:40 ` [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Mykyta Yatsenko @ 2025-04-08 23:44 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

From: Mykyta Yatsenko <yatsenko@meta.com>

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.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 .../selftests/bpf/prog_tests/test_btf_ext.c   | 64 +++++++++++++++++++
 .../selftests/bpf/progs/test_btf_ext.c        | 22 +++++++
 2 files changed, 86 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

diff --git a/tools/testing/selftests/bpf/prog_tests/test_btf_ext.c b/tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
new file mode 100644
index 000000000000..7d1b478c99a0
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms Inc. */
+#include <test_progs.h>
+#include "test_btf_ext.skel.h"
+#include "btf_helpers.h"
+
+static void subtest_line_func_info(void)
+{
+	struct test_btf_ext *skel;
+	struct bpf_prog_info info;
+	struct bpf_line_info line_info[128], *libbpf_line_info;
+	struct bpf_func_info func_info[128], *libbpf_func_info;
+	__u32 info_len = sizeof(info), libbbpf_line_info_cnt, libbbpf_func_info_cnt;
+	int err, fd;
+
+	skel = test_btf_ext__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+		return;
+
+	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_line_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);
+
+	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_func_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_line_info, "bpf_program__line_info"))
+		goto out;
+	if (!ASSERT_EQ(libbbpf_line_info_cnt, info.nr_line_info, "line_info_cnt"))
+		goto out;
+	if (!ASSERT_OK_PTR(libbpf_func_info, "bpf_program__func_info"))
+		goto out;
+	if (!ASSERT_EQ(libbbpf_func_info_cnt, info.nr_func_info, "func_info_cnt"))
+		goto out;
+	ASSERT_MEMEQ(libbpf_line_info, line_info, libbbpf_line_info_cnt * sizeof(*line_info),
+		     "line_info");
+	ASSERT_MEMEQ(libbpf_func_info, func_info, libbbpf_func_info_cnt * sizeof(*func_info),
+		     "func_info");
+out:
+	test_btf_ext__destroy(skel);
+}
+
+void test_btf_ext(void)
+{
+	if (test__start_subtest("line_func_info"))
+		subtest_line_func_info();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_btf_ext.c b/tools/testing/selftests/bpf/progs/test_btf_ext.c
new file mode 100644
index 000000000000..cdf20331db04
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_btf_ext.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms Inc. */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+__noinline static void f0(void)
+{
+	__u64 a = 1;
+
+	__sink(a);
+}
+
+SEC("xdp")
+__u64 global_func(struct xdp_md *xdp)
+{
+	f0();
+	return XDP_DROP;
+}
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters
  2025-04-08 23:44 [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters Mykyta Yatsenko
  2025-04-08 23:44 ` [PATCH bpf-next v5 1/2] libbpf: add getters for BTF.ext func and line info Mykyta Yatsenko
  2025-04-08 23:44 ` [PATCH bpf-next v5 2/2] selftests/bpf: add BTF.ext line/func info getter tests Mykyta Yatsenko
@ 2025-04-09 23:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-04-09 23:40 UTC (permalink / raw)
  To: Mykyta Yatsenko
  Cc: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87, yatsenko

Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Wed,  9 Apr 2025 00:44:15 +0100 you wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
> 
> 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.
> 
> [...]

Here is the summary with links:
  - [bpf-next,v5,1/2] libbpf: add getters for BTF.ext func and line info
    https://git.kernel.org/bpf/bpf-next/c/243d720e2e53
  - [bpf-next,v5,2/2] selftests/bpf: add BTF.ext line/func info getter tests
    https://git.kernel.org/bpf/bpf-next/c/b8390dd1e09e

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-04-09 23:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-08 23:44 [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters Mykyta Yatsenko
2025-04-08 23:44 ` [PATCH bpf-next v5 1/2] libbpf: add getters for BTF.ext func and line info Mykyta Yatsenko
2025-04-08 23:44 ` [PATCH bpf-next v5 2/2] selftests/bpf: add BTF.ext line/func info getter tests Mykyta Yatsenko
2025-04-09 23:40 ` [PATCH bpf-next v5 0/2] libbpf: introduce line_info and func_info getters patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox