提交 0cf7e44d 编写于 作者: Q Quentin Monnet 提交者: Zheng Zengkai

tools: Replace btf__get_from_id() with btf__load_from_kernel_by_id()

mainline inclusion
from mainline-5.15-rc1
commit 86f4b7f2
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=86f4b7f2578f69284fa782be54e700c42c757897

-------------------------------------------------

Replace the calls to function btf__get_from_id(), which we plan to
deprecate before the library reaches v1.0, with calls to
btf__load_from_kernel_by_id() in tools/ (bpftool, perf, selftests).
Update the surrounding code accordingly (instead of passing a pointer to
the btf struct, get it as a return value from the function).
Signed-off-by: NQuentin Monnet <quentin@isovalent.com>
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Acked-by: NJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20210729162028.29512-6-quentin@isovalent.com
(cherry picked from commit 86f4b7f2)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 59c1b296
...@@ -560,16 +560,12 @@ static int do_dump(int argc, char **argv) ...@@ -560,16 +560,12 @@ static int do_dump(int argc, char **argv)
} }
if (!btf) { if (!btf) {
err = btf__get_from_id(btf_id, &btf); btf = btf__load_from_kernel_by_id(btf_id);
err = libbpf_get_error(btf);
if (err) { if (err) {
p_err("get btf by id (%u): %s", btf_id, strerror(err)); p_err("get btf by id (%u): %s", btf_id, strerror(err));
goto done; goto done;
} }
if (!btf) {
err = -ENOENT;
p_err("can't find btf with ID (%u)", btf_id);
goto done;
}
} }
if (dump_c) { if (dump_c) {
......
...@@ -64,8 +64,10 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, ...@@ -64,8 +64,10 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,
} }
info = &prog_info->info; info = &prog_info->info;
if (!info->btf_id || !info->nr_func_info || if (!info->btf_id || !info->nr_func_info)
btf__get_from_id(info->btf_id, &prog_btf)) goto print;
prog_btf = btf__load_from_kernel_by_id(info->btf_id);
if (libbpf_get_error(prog_btf))
goto print; goto print;
finfo = u64_to_ptr(info->func_info); finfo = u64_to_ptr(info->func_info);
func_type = btf__type_by_id(prog_btf, finfo->type_id); func_type = btf__type_by_id(prog_btf, finfo->type_id);
......
...@@ -806,10 +806,11 @@ static struct btf *get_map_kv_btf(const struct bpf_map_info *info) ...@@ -806,10 +806,11 @@ static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
} else if (info->btf_value_type_id) { } else if (info->btf_value_type_id) {
int err; int err;
err = btf__get_from_id(info->btf_id, &btf); btf = btf__load_from_kernel_by_id(info->btf_id);
if (err || !btf) { err = libbpf_get_error(btf);
if (err) {
p_err("failed to get btf"); p_err("failed to get btf");
btf = err ? ERR_PTR(err) : ERR_PTR(-ESRCH); btf = ERR_PTR(err);
} }
} }
...@@ -1038,11 +1039,10 @@ static void print_key_value(struct bpf_map_info *info, void *key, ...@@ -1038,11 +1039,10 @@ static void print_key_value(struct bpf_map_info *info, void *key,
void *value) void *value)
{ {
json_writer_t *btf_wtr; json_writer_t *btf_wtr;
struct btf *btf = NULL; struct btf *btf;
int err;
err = btf__get_from_id(info->btf_id, &btf); btf = btf__load_from_kernel_by_id(info->btf_id);
if (err) { if (libbpf_get_error(btf)) {
p_err("failed to get btf"); p_err("failed to get btf");
return; return;
} }
......
...@@ -249,10 +249,10 @@ static void show_prog_metadata(int fd, __u32 num_maps) ...@@ -249,10 +249,10 @@ static void show_prog_metadata(int fd, __u32 num_maps)
struct bpf_map_info map_info; struct bpf_map_info map_info;
struct btf_var_secinfo *vsi; struct btf_var_secinfo *vsi;
bool printed_header = false; bool printed_header = false;
struct btf *btf = NULL;
unsigned int i, vlen; unsigned int i, vlen;
void *value = NULL; void *value = NULL;
const char *name; const char *name;
struct btf *btf;
int err; int err;
if (!num_maps) if (!num_maps)
...@@ -263,8 +263,8 @@ static void show_prog_metadata(int fd, __u32 num_maps) ...@@ -263,8 +263,8 @@ static void show_prog_metadata(int fd, __u32 num_maps)
if (!value) if (!value)
return; return;
err = btf__get_from_id(map_info.btf_id, &btf); btf = btf__load_from_kernel_by_id(map_info.btf_id);
if (err || !btf) if (libbpf_get_error(btf))
goto out_free; goto out_free;
t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id); t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id);
...@@ -648,10 +648,13 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, ...@@ -648,10 +648,13 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
member_len = info->xlated_prog_len; member_len = info->xlated_prog_len;
} }
if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) { if (info->btf_id) {
btf = btf__load_from_kernel_by_id(info->btf_id);
if (libbpf_get_error(btf)) {
p_err("failed to get btf"); p_err("failed to get btf");
return -1; return -1;
} }
}
func_info = u64_to_ptr(info->func_info); func_info = u64_to_ptr(info->func_info);
...@@ -1988,12 +1991,17 @@ static char *profile_target_name(int tgt_fd) ...@@ -1988,12 +1991,17 @@ static char *profile_target_name(int tgt_fd)
return NULL; return NULL;
} }
if (info_linear->info.btf_id == 0 || if (info_linear->info.btf_id == 0) {
btf__get_from_id(info_linear->info.btf_id, &btf)) {
p_err("prog FD %d doesn't have valid btf", tgt_fd); p_err("prog FD %d doesn't have valid btf", tgt_fd);
goto out; goto out;
} }
btf = btf__load_from_kernel_by_id(info_linear->info.btf_id);
if (libbpf_get_error(btf)) {
p_err("failed to load btf for prog FD %d", tgt_fd);
goto out;
}
func_info = u64_to_ptr(info_linear->info.func_info); func_info = u64_to_ptr(info_linear->info.func_info);
t = btf__type_by_id(btf, func_info[0].type_id); t = btf__type_by_id(btf, func_info[0].type_id);
if (!t) { if (!t) {
......
...@@ -220,10 +220,10 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session, ...@@ -220,10 +220,10 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
err = -1; err = -1;
goto out; goto out;
} }
if (btf__get_from_id(info->btf_id, &btf)) { btf = btf__load_from_kernel_by_id(info->btf_id);
if (libbpf_get_error(btf)) {
pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id); pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
err = -1; err = -1;
btf = NULL;
goto out; goto out;
} }
perf_env__fetch_btf(env, info->btf_id, btf); perf_env__fetch_btf(env, info->btf_id, btf);
...@@ -475,7 +475,8 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id) ...@@ -475,7 +475,8 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
if (btf_id == 0) if (btf_id == 0)
goto out; goto out;
if (btf__get_from_id(btf_id, &btf)) { btf = btf__load_from_kernel_by_id(btf_id);
if (libbpf_get_error(btf)) {
pr_debug("%s: failed to get BTF of id %u, aborting\n", pr_debug("%s: failed to get BTF of id %u, aborting\n",
__func__, btf_id); __func__, btf_id);
goto out; goto out;
......
...@@ -73,12 +73,17 @@ static char *bpf_target_prog_name(int tgt_fd) ...@@ -73,12 +73,17 @@ static char *bpf_target_prog_name(int tgt_fd)
return NULL; return NULL;
} }
if (info_linear->info.btf_id == 0 || if (info_linear->info.btf_id == 0) {
btf__get_from_id(info_linear->info.btf_id, &btf)) {
pr_debug("prog FD %d doesn't have valid btf\n", tgt_fd); pr_debug("prog FD %d doesn't have valid btf\n", tgt_fd);
goto out; goto out;
} }
btf = btf__load_from_kernel_by_id(info_linear->info.btf_id);
if (libbpf_get_error(btf)) {
pr_debug("failed to load btf for prog FD %d\n", tgt_fd);
goto out;
}
func_info = u64_to_ptr(info_linear->info.func_info); func_info = u64_to_ptr(info_linear->info.func_info);
t = btf__type_by_id(btf, func_info[0].type_id); t = btf__type_by_id(btf, func_info[0].type_id);
if (!t) { if (!t) {
......
...@@ -4218,7 +4218,8 @@ static void do_test_file(unsigned int test_num) ...@@ -4218,7 +4218,8 @@ static void do_test_file(unsigned int test_num)
goto done; goto done;
} }
err = btf__get_from_id(info.btf_id, &btf); btf = btf__load_from_kernel_by_id(info.btf_id);
err = libbpf_get_error(btf);
if (CHECK(err, "cannot get btf from kernel, err: %d", err)) if (CHECK(err, "cannot get btf from kernel, err: %d", err))
goto done; goto done;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册