BPF List
 help / color / mirror / Atom feed
* [PATCH v4 1/2] tools/bpf/bpftool: Reset vmlinux BTF after map commands
       [not found] <20260624025055.1574875-1-chenyichong@uniontech.com>
@ 2026-06-24  2:50 ` Yichong Chen
  2026-06-24  2:50 ` [PATCH v4 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands Yichong Chen
  1 sibling, 0 replies; 3+ messages in thread
From: Yichong Chen @ 2026-06-24  2:50 UTC (permalink / raw)
  To: emil, qmo, ast, daniel, andrii, eddyz87, memxor
  Cc: chenyichong, martin.lau, song, yonghong.song, jolsa, jiayuan.chen,
	bpf, linux-kernel

get_map_kv_btf() caches the vmlinux BTF object when a map uses
btf_vmlinux_value_type_id. map dump released that object when the
command completed, but left the global pointer stale.

The same cached object can also be returned to print_key_value(), which
freed it directly. That leaves btf_vmlinux dangling before the command
cleanup path runs.

Use free_map_kv_btf() for per-entry cleanup, and reset the cached
btf_vmlinux pointer when the map command releases the object. This keeps
batch mode from reusing a freed BTF object.

Fixes: 4e1ea33292ff ("bpftool: Support dumping a map with btf_vmlinux_value_type_id")
Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
---
 tools/bpf/bpftool/map.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 7ebf7dbcfba4..a265ad3afbb0 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -790,6 +790,12 @@ static int maps_have_btf(int *fds, int nb_fds)
 
 static struct btf *btf_vmlinux;
 
+static void free_btf_vmlinux(void)
+{
+	btf__free(btf_vmlinux);
+	btf_vmlinux = NULL;
+}
+
 static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
 {
 	int err = 0;
@@ -958,7 +964,7 @@ static int do_dump(int argc, char **argv)
 		close(fds[i]);
 exit_free:
 	free(fds);
-	btf__free(btf_vmlinux);
+	free_btf_vmlinux();
 	return err;
 }
 
@@ -1049,7 +1055,7 @@ static void print_key_value(struct bpf_map_info *info, void *key,
 		btf_wtr = get_btf_writer();
 		if (!btf_wtr) {
 			p_info("failed to create json writer for btf. falling back to plain output");
-			btf__free(btf);
+			free_map_kv_btf(btf);
 			btf = NULL;
 			print_entry_plain(info, key, value);
 		} else {
@@ -1065,7 +1071,7 @@ static void print_key_value(struct bpf_map_info *info, void *key,
 	} else {
 		print_entry_plain(info, key, value);
 	}
-	btf__free(btf);
+	free_map_kv_btf(btf);
 }
 
 static int do_lookup(int argc, char **argv)
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v4 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
       [not found] <20260624025055.1574875-1-chenyichong@uniontech.com>
  2026-06-24  2:50 ` [PATCH v4 1/2] tools/bpf/bpftool: Reset vmlinux BTF after map commands Yichong Chen
@ 2026-06-24  2:50 ` Yichong Chen
  2026-06-29  7:45   ` Emil Tsalapatis
  1 sibling, 1 reply; 3+ messages in thread
From: Yichong Chen @ 2026-06-24  2:50 UTC (permalink / raw)
  To: emil, qmo, ast, daniel, andrii, eddyz87, memxor
  Cc: chenyichong, martin.lau, song, yonghong.song, jolsa, jiayuan.chen,
	bpf, linux-kernel

struct_ops frees the global btf_vmlinux object.

In batch mode, a later struct_ops command can reuse stale state.

Reset the BTF pointer and cached map info state.

Fixes: 65c93628599d ("bpftool: Add struct_ops support")
Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
---
 tools/bpf/bpftool/struct_ops.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/bpf/bpftool/struct_ops.c b/tools/bpf/bpftool/struct_ops.c
index aa43dead249c..835e5e561f7f 100644
--- a/tools/bpf/bpftool/struct_ops.c
+++ b/tools/bpf/bpftool/struct_ops.c
@@ -643,6 +643,10 @@ int do_struct_ops(int argc, char **argv)
 	err = cmd_select(cmds, argc, argv, do_help);
 
 	btf__free(btf_vmlinux);
+	btf_vmlinux = NULL;
+	map_info_type = NULL;
+	map_info_alloc_len = 0;
+	map_info_type_id = 0;
 
 	return err;
 }
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
  2026-06-24  2:50 ` [PATCH v4 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands Yichong Chen
@ 2026-06-29  7:45   ` Emil Tsalapatis
  0 siblings, 0 replies; 3+ messages in thread
From: Emil Tsalapatis @ 2026-06-29  7:45 UTC (permalink / raw)
  To: Yichong Chen, emil, qmo, ast, daniel, andrii, eddyz87, memxor
  Cc: martin.lau, song, yonghong.song, jolsa, jiayuan.chen, bpf,
	linux-kernel

On Tue Jun 23, 2026 at 10:50 PM EDT, Yichong Chen wrote:
> struct_ops frees the global btf_vmlinux object.
>
> In batch mode, a later struct_ops command can reuse stale state.
>
> Reset the BTF pointer and cached map info state.
>
> Fixes: 65c93628599d ("bpftool: Add struct_ops support")
> Signed-off-by: Yichong Chen <chenyichong@uniontech.com>

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

> ---
>  tools/bpf/bpftool/struct_ops.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/tools/bpf/bpftool/struct_ops.c b/tools/bpf/bpftool/struct_ops.c
> index aa43dead249c..835e5e561f7f 100644
> --- a/tools/bpf/bpftool/struct_ops.c
> +++ b/tools/bpf/bpftool/struct_ops.c
> @@ -643,6 +643,10 @@ int do_struct_ops(int argc, char **argv)
>  	err = cmd_select(cmds, argc, argv, do_help);
>  
>  	btf__free(btf_vmlinux);
> +	btf_vmlinux = NULL;
> +	map_info_type = NULL;
> +	map_info_alloc_len = 0;
> +	map_info_type_id = 0;
>  
>  	return err;
>  }


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-29  7:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20260624025055.1574875-1-chenyichong@uniontech.com>
2026-06-24  2:50 ` [PATCH v4 1/2] tools/bpf/bpftool: Reset vmlinux BTF after map commands Yichong Chen
2026-06-24  2:50 ` [PATCH v4 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands Yichong Chen
2026-06-29  7:45   ` Emil Tsalapatis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox