Skip to content

Commit

Permalink
bpftool: Use bpf_{btf,link,map,prog}_get_info_by_fd()
Browse files Browse the repository at this point in the history
Use the new type-safe wrappers around bpf_obj_get_info_by_fd().

Split the bpf_obj_get_info_by_fd() call in build_btf_type_table() in
two, since knowing the type helps with the Memory Sanitizer.

Improve map_parse_fd_and_info() type safety by using
struct bpf_map_info * instead of void * for info.

Signed-off-by: Ilya Leoshkevich <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Reviewed-by: Quentin Monnet <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
iii-i authored and anakryiko committed Feb 16, 2023
1 parent 629dfc6 commit 38f0408
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 36 deletions.
13 changes: 8 additions & 5 deletions tools/bpf/bpftool/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ static bool btf_is_kernel_module(__u32 btf_id)
len = sizeof(btf_info);
btf_info.name = ptr_to_u64(btf_name);
btf_info.name_len = sizeof(btf_name);
err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
err = bpf_btf_get_info_by_fd(btf_fd, &btf_info, &len);
close(btf_fd);
if (err) {
p_err("can't get BTF (ID %u) object info: %s", btf_id, strerror(errno));
Expand Down Expand Up @@ -606,7 +606,7 @@ static int do_dump(int argc, char **argv)
if (fd < 0)
return -1;

err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_prog_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get prog info: %s", strerror(errno));
goto done;
Expand Down Expand Up @@ -789,7 +789,10 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
}

memset(info, 0, *len);
err = bpf_obj_get_info_by_fd(fd, info, len);
if (type == BPF_OBJ_PROG)
err = bpf_prog_get_info_by_fd(fd, info, len);
else
err = bpf_map_get_info_by_fd(fd, info, len);
close(fd);
if (err) {
p_err("can't get %s info: %s", names[type],
Expand Down Expand Up @@ -931,7 +934,7 @@ show_btf(int fd, struct hashmap *btf_prog_table,
int err;

memset(&info, 0, sizeof(info));
err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_btf_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get BTF object info: %s", strerror(errno));
return -1;
Expand All @@ -943,7 +946,7 @@ show_btf(int fd, struct hashmap *btf_prog_table,
info.name = ptr_to_u64(name);
len = sizeof(info);

err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_btf_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get BTF object info: %s", strerror(errno));
return -1;
Expand Down
4 changes: 2 additions & 2 deletions tools/bpf/bpftool/btf_dumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,
if (prog_fd < 0)
goto print;

err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
if (err)
goto print;

Expand All @@ -70,7 +70,7 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,
info.func_info_rec_size = finfo_rec_size;
info.func_info = ptr_to_u64(&finfo);

err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
if (err)
goto print;

Expand Down
4 changes: 2 additions & 2 deletions tools/bpf/bpftool/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static void guess_vmlinux_btf_id(__u32 attach_btf_obj_id)
if (fd < 0)
return;

err = bpf_obj_get_info_by_fd(fd, &btf_info, &btf_len);
err = bpf_btf_get_info_by_fd(fd, &btf_info, &btf_len);
if (err)
goto out;

Expand All @@ -108,7 +108,7 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
if (prog_fd < 0)
return -1;

if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len)) {
if (bpf_prog_get_info_by_fd(prog_fd, &info, &info_len)) {
close(prog_fd);
return -1;
}
Expand Down
13 changes: 7 additions & 6 deletions tools/bpf/bpftool/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
info.func_info_rec_size = sizeof(finfo);
info.func_info = ptr_to_u64(&finfo);

if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len))
if (bpf_prog_get_info_by_fd(prog_fd, &info, &info_len))
goto copy_name;

prog_btf = btf__load_from_kernel_by_id(info.btf_id);
Expand Down Expand Up @@ -488,7 +488,7 @@ static int do_build_table_cb(const char *fpath, const struct stat *sb,
goto out_close;

memset(&pinned_info, 0, sizeof(pinned_info));
if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len))
if (bpf_prog_get_info_by_fd(fd, &pinned_info, &len))
goto out_close;

path = strdup(fpath);
Expand Down Expand Up @@ -756,7 +756,7 @@ static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
goto err_close_fds;
}

err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_prog_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get prog info (%u): %s",
id, strerror(errno));
Expand Down Expand Up @@ -916,7 +916,7 @@ static int map_fd_by_name(char *name, int **fds)
goto err_close_fds;
}

err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_map_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get map info (%u): %s",
id, strerror(errno));
Expand Down Expand Up @@ -1026,7 +1026,8 @@ int map_parse_fd(int *argc, char ***argv)
return fd;
}

int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
int map_parse_fd_and_info(int *argc, char ***argv, struct bpf_map_info *info,
__u32 *info_len)
{
int err;
int fd;
Expand All @@ -1035,7 +1036,7 @@ int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
if (fd < 0)
return -1;

err = bpf_obj_get_info_by_fd(fd, info, info_len);
err = bpf_map_get_info_by_fd(fd, info, info_len);
if (err) {
p_err("can't get map info: %s", strerror(errno));
close(fd);
Expand Down
4 changes: 2 additions & 2 deletions tools/bpf/bpftool/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
return prog_fd;

memset(info, 0, sizeof(*info));
err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
err = bpf_prog_get_info_by_fd(prog_fd, info, &len);
if (err)
p_err("can't get prog info: %s", strerror(errno));
close(prog_fd);
Expand Down Expand Up @@ -327,7 +327,7 @@ static int do_show_link(int fd)

memset(&info, 0, sizeof(info));
again:
err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_link_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get link info: %s",
strerror(errno));
Expand Down
3 changes: 2 additions & 1 deletion tools/bpf/bpftool/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ int prog_parse_fd(int *argc, char ***argv);
int prog_parse_fds(int *argc, char ***argv, int **fds);
int map_parse_fd(int *argc, char ***argv);
int map_parse_fds(int *argc, char ***argv, int **fds);
int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
int map_parse_fd_and_info(int *argc, char ***argv, struct bpf_map_info *info,
__u32 *info_len);

struct bpf_prog_linfo;
#if defined(HAVE_LLVM_SUPPORT) || defined(HAVE_LIBBFD_SUPPORT)
Expand Down
8 changes: 4 additions & 4 deletions tools/bpf/bpftool/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ static int do_show_subset(int argc, char **argv)
if (json_output && nb_fds > 1)
jsonw_start_array(json_wtr); /* root array */
for (i = 0; i < nb_fds; i++) {
err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
err = bpf_map_get_info_by_fd(fds[i], &info, &len);
if (err) {
p_err("can't get map info: %s",
strerror(errno));
Expand Down Expand Up @@ -708,7 +708,7 @@ static int do_show(int argc, char **argv)
break;
}

err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_map_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get map info: %s", strerror(errno));
close(fd);
Expand Down Expand Up @@ -764,7 +764,7 @@ static int maps_have_btf(int *fds, int nb_fds)
int err, i;

for (i = 0; i < nb_fds; i++) {
err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
err = bpf_map_get_info_by_fd(fds[i], &info, &len);
if (err) {
p_err("can't get map info: %s", strerror(errno));
return -1;
Expand Down Expand Up @@ -925,7 +925,7 @@ static int do_dump(int argc, char **argv)
if (wtr && nb_fds > 1)
jsonw_start_array(wtr); /* root array */
for (i = 0; i < nb_fds; i++) {
if (bpf_obj_get_info_by_fd(fds[i], &info, &len)) {
if (bpf_map_get_info_by_fd(fds[i], &info, &len)) {
p_err("can't get map info: %s", strerror(errno));
break;
}
Expand Down
22 changes: 11 additions & 11 deletions tools/bpf/bpftool/prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static void show_prog_maps(int fd, __u32 num_maps)
info.nr_map_ids = num_maps;
info.map_ids = ptr_to_u64(map_ids);

err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_prog_get_info_by_fd(fd, &info, &len);
if (err || !info.nr_map_ids)
return;

Expand Down Expand Up @@ -231,7 +231,7 @@ static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)

memset(&prog_info, 0, sizeof(prog_info));
prog_info_len = sizeof(prog_info);
ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
if (ret)
return NULL;

Expand All @@ -248,7 +248,7 @@ static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)
prog_info.map_ids = ptr_to_u64(map_ids);
prog_info_len = sizeof(prog_info);

ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
if (ret)
goto free_map_ids;

Expand All @@ -259,7 +259,7 @@ static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)

memset(map_info, 0, sizeof(*map_info));
map_info_len = sizeof(*map_info);
ret = bpf_obj_get_info_by_fd(map_fd, map_info, &map_info_len);
ret = bpf_map_get_info_by_fd(map_fd, map_info, &map_info_len);
if (ret < 0) {
close(map_fd);
goto free_map_ids;
Expand Down Expand Up @@ -580,7 +580,7 @@ static int show_prog(int fd)
__u32 len = sizeof(info);
int err;

err = bpf_obj_get_info_by_fd(fd, &info, &len);
err = bpf_prog_get_info_by_fd(fd, &info, &len);
if (err) {
p_err("can't get prog info: %s", strerror(errno));
return -1;
Expand Down Expand Up @@ -949,7 +949,7 @@ static int do_dump(int argc, char **argv)
for (i = 0; i < nb_fds; i++) {
memset(&info, 0, sizeof(info));

err = bpf_obj_get_info_by_fd(fds[i], &info, &info_len);
err = bpf_prog_get_info_by_fd(fds[i], &info, &info_len);
if (err) {
p_err("can't get prog info: %s", strerror(errno));
break;
Expand All @@ -961,7 +961,7 @@ static int do_dump(int argc, char **argv)
break;
}

err = bpf_obj_get_info_by_fd(fds[i], &info, &info_len);
err = bpf_prog_get_info_by_fd(fds[i], &info, &info_len);
if (err) {
p_err("can't get prog info: %s", strerror(errno));
break;
Expand Down Expand Up @@ -2170,9 +2170,9 @@ static char *profile_target_name(int tgt_fd)
char *name = NULL;
int err;

err = bpf_obj_get_info_by_fd(tgt_fd, &info, &info_len);
err = bpf_prog_get_info_by_fd(tgt_fd, &info, &info_len);
if (err) {
p_err("failed to bpf_obj_get_info_by_fd for prog FD %d", tgt_fd);
p_err("failed to get info for prog FD %d", tgt_fd);
goto out;
}

Expand All @@ -2183,7 +2183,7 @@ static char *profile_target_name(int tgt_fd)

func_info_rec_size = info.func_info_rec_size;
if (info.nr_func_info == 0) {
p_err("bpf_obj_get_info_by_fd for prog FD %d found 0 func_info", tgt_fd);
p_err("found 0 func_info for prog FD %d", tgt_fd);
goto out;
}

Expand All @@ -2192,7 +2192,7 @@ static char *profile_target_name(int tgt_fd)
info.func_info_rec_size = func_info_rec_size;
info.func_info = ptr_to_u64(&func_info);

err = bpf_obj_get_info_by_fd(tgt_fd, &info, &info_len);
err = bpf_prog_get_info_by_fd(tgt_fd, &info, &info_len);
if (err) {
p_err("failed to get func_info for prog FD %d", tgt_fd);
goto out;
Expand Down
6 changes: 3 additions & 3 deletions tools/bpf/bpftool/struct_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static int get_next_struct_ops_map(const char *name, int *res_fd,
return -1;
}

err = bpf_obj_get_info_by_fd(fd, info, &info_len);
err = bpf_map_get_info_by_fd(fd, info, &info_len);
if (err) {
p_err("can't get map info: %s", strerror(errno));
close(fd);
Expand Down Expand Up @@ -262,7 +262,7 @@ static struct res do_one_id(const char *id_str, work_func func, void *data,
goto done;
}

if (bpf_obj_get_info_by_fd(fd, info, &info_len)) {
if (bpf_map_get_info_by_fd(fd, info, &info_len)) {
p_err("can't get map info: %s", strerror(errno));
res.nr_errs++;
goto done;
Expand Down Expand Up @@ -522,7 +522,7 @@ static int do_register(int argc, char **argv)
bpf_link__disconnect(link);
bpf_link__destroy(link);

if (!bpf_obj_get_info_by_fd(bpf_map__fd(map), &info,
if (!bpf_map_get_info_by_fd(bpf_map__fd(map), &info,
&info_len))
p_info("Registered %s %s id %u",
get_kern_struct_ops_name(&info),
Expand Down

0 comments on commit 38f0408

Please sign in to comment.