Re: [PATCH v3 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands

From: Emil Tsalapatis

Date: Tue Jun 23 2026 - 17:45:25 EST


On Tue Jun 23, 2026 at 3:22 AM 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@xxxxxxxxxxxxx>
> ---
> tools/bpf/bpftool/struct_ops.c | 11 ++++++++++-
> 1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/tools/bpf/bpftool/struct_ops.c b/tools/bpf/bpftool/struct_ops.c
> index aa43dead249c..4dba7c96e5f6 100644
> --- a/tools/bpf/bpftool/struct_ops.c
> +++ b/tools/bpf/bpftool/struct_ops.c
> @@ -21,6 +21,15 @@ static __u32 map_info_alloc_len;
> static struct btf *btf_vmlinux;
> static __s32 map_info_type_id;
>
> +static void free_btf_vmlinux(void)
> +{
> + btf__free(btf_vmlinux);
> + btf_vmlinux = NULL;
> + map_info_type = NULL;
> + map_info_alloc_len = 0;
> + map_info_type_id = 0;
> +}

Nit but I don't think we need this to be a separate helper. We use it
once, and it has the exact same name and type as the one in the previous
patch.

pw-bot: cr

> +
> struct res {
> unsigned int nr_maps;
> unsigned int nr_errs;
> @@ -642,7 +651,7 @@ int do_struct_ops(int argc, char **argv)
>
> err = cmd_select(cmds, argc, argv, do_help);
>
> - btf__free(btf_vmlinux);
> + free_btf_vmlinux();
>
> return err;
> }