Skip to contentSkip to conversation

bpftool: Fix pretty print dump for maps without BTF loaded

PATCH Applied Jiri Olsa 3 participants 3 messages Feb 16, 2022

Subsystems (4)
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 bpftool: Fix pretty print dump for maps without BTF loaded

The commit e5043894b21f ("bpftool: Use libbpf_get_error() to check error") fails to dump map without BTF loaded in pretty mode (-p option).

Fixing this by making sure get_map_kv_btf won't fail in case there's no BTF available for the map.

Cc Yinjun Zhang <[email protected]>
Fixes e5043894b21f ("bpftool: Use libbpf_get_error() to check error")
---
 tools/bpf/bpftool/map.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
tools/bpf/bpftool/map.c +13 -16
index 7a341a472ea4..8562add7417d 100644
@@ -805,29 +805,28 @@ static int maps_have_btf(int *fds, int nb_fds)
 
 static struct btf *btf_vmlinux;
 
-static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
+static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
 {
-	struct btf *btf = NULL;
+	int err = 0;
 
 	if (info->btf_vmlinux_value_type_id) {
 		if (!btf_vmlinux) {
 			btf_vmlinux = libbpf_find_kernel_btf();
-			if (libbpf_get_error(btf_vmlinux))
+			err = libbpf_get_error(btf_vmlinux);
+			if (err) {
 				p_err("failed to get kernel btf");
+				return err;
+			}
 		}
-		return btf_vmlinux;
+		*btf = btf_vmlinux;
 	} else if (info->btf_value_type_id) {
-		int err;
-
-		btf = btf__load_from_kernel_by_id(info->btf_id);
-		err = libbpf_get_error(btf);
-		if (err) {
+		*btf = btf__load_from_kernel_by_id(info->btf_id);
+		err = libbpf_get_error(*btf);
+		if (err)
 			p_err("failed to get btf");
-			btf = ERR_PTR(err);
-		}
 	}
 
-	return btf;
+	return err;
 }
 
 static void free_map_kv_btf(struct btf *btf)
@@ -862,8 +861,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
 	prev_key = NULL;
 
 	if (wtr) {
-		btf = get_map_kv_btf(info);
-		err = libbpf_get_error(btf);
+		err = get_map_kv_btf(info, &btf);
 		if (err) {
 			goto exit_free;
 		}
@@ -1054,8 +1052,7 @@ static void print_key_value(struct bpf_map_info *info, void *key,
 	json_writer_t *btf_wtr;
 	struct btf *btf;
 
-	btf = get_map_kv_btf(info);
-	if (libbpf_get_error(btf))
+	if (get_map_kv_btf(info, &btf))
 		return;
 
 	if (json_output) {
Signature · 1 line
2.35.1
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 6 lines · On Wed, 16 Feb 2022 10:21:02 +0100 you wrote:

The commit e5043894b21f ("bpftool: Use libbpf_get_error() to check error") fails to dump map without BTF loaded in pretty mode (-p option).

Fixing this by making sure get_map_kv_btf won't fail in case there's no BTF available for the map.

Quoted 6 lines
Cc Yinjun Zhang <[email protected]>
Fixes e5043894b21f ("bpftool: Use libbpf_get_error() to check error")

[...]

Here is the summary with links:
- [bpf-next] bpftool: Fix pretty print dump for maps without BTF loaded

    https://git.kernel.org/bpf/bpf-next/c/2e3f7bed2837

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#2 lore raw
In reply to Jiri Olsa
Quoted 44 lines · Jiri Olsa

The commit e5043894b21f ("bpftool: Use libbpf_get_error() to check error") fails to dump map without BTF loaded in pretty mode (-p option).

Fixing this by making sure get_map_kv_btf won't fail in case there's no BTF available for the map.

Cc Yinjun Zhang <[email protected]>
Fixes e5043894b21f ("bpftool: Use libbpf_get_error() to check error")
---
 tools/bpf/bpftool/map.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
tools/bpf/bpftool/map.c +7 -6
index 7a341a472ea4..8562add7417d 100644
@@ -805,29 +805,28 @@ static int maps_have_btf(int *fds, int nb_fds)
 
 static struct btf *btf_vmlinux;
 
-static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
+static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
 {
-       struct btf *btf = NULL;
+       int err = 0;
 
        if (info->btf_vmlinux_value_type_id) {
                if (!btf_vmlinux) {
                        btf_vmlinux = libbpf_find_kernel_btf();
-                       if (libbpf_get_error(btf_vmlinux))
+                       err = libbpf_get_error(btf_vmlinux);
+                       if (err) {
                                p_err("failed to get kernel btf");
+                               return err;
+                       }
                }
-               return btf_vmlinux;
+               *btf = btf_vmlinux;
        } else if (info->btf_value_type_id) {
-               int err;
-

tools/bpf/bpftool/map.c static int maps_have_btf(int *fds, int nb_fds) · line 827

Quoted 10 lines
tools/bpf/bpftool/map.c +3 -5
-               btf = btf__load_from_kernel_by_id(info->btf_id);
-               err = libbpf_get_error(btf);
-               if (err) {
+               *btf = btf__load_from_kernel_by_id(info->btf_id);
+               err = libbpf_get_error(*btf);
+               if (err)
                        p_err("failed to get btf");
-                       btf = ERR_PTR(err);
-               }
        }

get_map_kv_btf is supposed to set btf to NULL, otherwise you can get a crash in the caller

I've added

else {
    *btf = NULL;
}

and force-pushed

Quoted 29 lines
tools/bpf/bpftool/map.c +3 -6
-       return btf;
+       return err;
 }
 
 static void free_map_kv_btf(struct btf *btf)
@@ -862,8 +861,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
        prev_key = NULL;
 
        if (wtr) {
-               btf = get_map_kv_btf(info);
-               err = libbpf_get_error(btf);
+               err = get_map_kv_btf(info, &btf);
                if (err) {
                        goto exit_free;
                }
@@ -1054,8 +1052,7 @@ static void print_key_value(struct bpf_map_info *info, void *key,
        json_writer_t *btf_wtr;
        struct btf *btf;
 
-       btf = get_map_kv_btf(info);
-       if (libbpf_get_error(btf))
+       if (get_map_kv_btf(info, &btf))
                return;
 
        if (json_output) {
--

2.35.1