tools/bpf/bpftool: Reset vmlinux BTF after map commands
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v4 1/2 tools/bpf/bpftool: Reset vmlinux BTF after map commands
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.
--- tools/bpf/bpftool/map.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
tools/bpf/bpftool/map.c +9 -3
@@ -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)
Signature · 1 line
2.51.0
PATCH v4 2/2 tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
In reply to a message not in the archivestruct_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.
--- tools/bpf/bpftool/struct_ops.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/bpf/bpftool/struct_ops.c +4 -0
@@ -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; }
Signature · 1 line
2.51.0
Re: [PATCH v4 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
Quoted 8 lines · Yichong Chen
struct_opsfrees the globalbtf_vmlinuxobject.In batch mode, a later
struct_opscommand 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 <[email protected]>
Quoted 19 lines
--- tools/bpf/bpftool/struct_ops.c | 4 ++++ 1 file changed, 4 insertions(+)tools/bpf/bpftool/struct_ops.c +4
-0@@ -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; }