Merge branch 'libbpf-introduce-line_info-and-func_info-getters'

Mykyta Yatsenko says:

====================
libbpf: introduce line_info and func_info getters

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

Link: https://patch.msgid.link/20250408234417.452565-1-mykyta.yatsenko5@gmail.com
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
This commit is contained in:
Andrii Nakryiko 2025-04-09 16:16:56 -07:00
commit 527b33dda1
5 changed files with 120 additions and 0 deletions

View File

@ -9446,6 +9446,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, \

View File

@ -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:

View File

@ -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;

View File

@ -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();
}

View File

@ -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;
}