libbpf: introduce line_info and func_info getters
Subsystems (4)
v4 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.
v3 -> v4
* Fix nits in selftests, disable some asserts for s390x, as it seems that
kernel inserts preamble into function entry point.
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 | 69 +++++++++++++++++++ .../selftests/bpf/progs/test_btf_ext.c | 26 +++++++ 5 files changed, 129 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
PATCH v4 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
@@ -9455,6 +9455,30 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t logreturn 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
@@ -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
@@ -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
PATCH v4 2/2 selftests/bpf: add BTF.ext line/func info getter tests
In reply to Mykyta YatsenkoFrom: 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 | 69 +++++++++++++++++++ .../selftests/bpf/progs/test_btf_ext.c | 26 +++++++ 2 files changed, 95 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 +69 -0
@@ -0,0 +1,69 @@ +// 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; +/* Skip checks on s390x as it seems to be adding some preamble to function entry point +* shifting instruction offsets by 1 byte +*/ +#if !defined(__TARGET_ARCH_s390) && !defined (__s390x__) +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" ); +#endif +out: +test_btf_ext__destroy (skel); +} + +void test_btf_ext (void ) +{ +if (test__start_subtest ("line_func_info" )) +subtest_line_func_info (); +}
tools/testing/selftests/bpf/progs/test_btf_ext.c new file +26 -0
@@ -0,0 +1,26 @@ +// 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) +{ +__u8 a =1 ; +__u8 b =2 ; +__u8 c =3 ; + +__sink (a); +__sink (b); +__sink (c); +} + +SEC("xdp") +int global_func (struct xdp_md *xdp) +{ +f0 (); +return XDP_DROP ; +}
Signature · 1 line
2.49.0
Re: [PATCH bpf-next v4 2/2] selftests/bpf: add BTF.ext line/func info getter tests
Quoted 97 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.Signed-off-by Mykyta Yatsenko <[email protected]>--- .../selftests/bpf/prog_tests/test_btf_ext.c | 69 +++++++++++++++++++ .../selftests/bpf/progs/test_btf_ext.c | 26 +++++++ 2 files changed, 95 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.ctools/testing/selftests/bpf/prog_tests/test_btf_ext.c new file +69
-0@@ -0,0 +1,69 @@ +// 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; +/* Skip checks on s390x as it seems to be adding some preamble to function entry point +* shifting instruction offsets by 1 byte +*/ +#if !defined(__TARGET_ARCH_s390) && !defined (__s390x__) +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" ); +#endif +out: +test_btf_ext__destroy (skel); +} + +void test_btf_ext (void ) +{ +if (test__start_subtest ("line_func_info" )) +subtest_line_func_info (); +}tools/testing/selftests/bpf/progs/test_btf_ext.c new file +2
-0@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2025 Meta Platforms Inc. */
tools/testing/selftests/bpf/progs/test_btf_ext.c line 12
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_btf_ext.c new file +10
-0+ +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +__noinline static void f0(void) +{ +__u8 a =1 ;
ok, so this will generate
0: b4 01 00 00 01 00 00 00 w1 = 0x1
1: 73 1a ff ff 00 00 00 00 *(u8 *)(r10 - 0x1) = w1And on s390x (and a bunch of other architectures), verifier will inject extra special zero-extension instruction after w1 = 0x1, which shifts all the instruction indices. You can look at how bpf_jit_needs_zext() is defined and used in verifier.c, if you are curious for details.
But given the purpose of the test is to make sure that libbpf gives us full and correct information, all that zext business is a distraction and we should just avoid all this.
So, let's just use full __u64 variables here and be done with this (meaning, no s390x exceptions, just simple BPF code that doesn't get enhanced or transformed by the verifier).
pw-bot: cr
Quoted 17 lines
tools/testing/selftests/bpf/progs/test_btf_ext.c new file +14
-1+__u8 b =2 ; +__u8 c =3 ; + +__sink (a); +__sink (b); +__sink (c); +} + +SEC("xdp") +int global_func (struct xdp_md *xdp) +{ +f0 (); +return XDP_DROP ; +} --2.49.0