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 v3 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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Memory leak of btf_vmlinux in cgroup.c subcommands --
commit 20f01631a8680469e5ce832fb9e313bcd34e8d13
Author: Yichong Chen <[email protected]>
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. This commit introduces free_btf_vmlinux() to properly clean up the cached btf_vmlinux pointer when the map command releases the object, keeping batch mode from reusing a freed BTF object.
tools/bpf/bpftool/map.c static int maps_have_btf(int *fds, int nb_fds) · line 797
Quoted 13 lines
tools/bpf/bpftool/map.c +5
-0@@ -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 ; +}
[Severity: High]
This isn't a bug introduced by this patch, but since you are fixing the btf_vmlinux leak here, could there be a similar memory leak of btf_vmlinux in tools/bpf/bpftool/cgroup.c?
In do_show_tree_fn() and do_show() in cgroup.c, a new BTF object is allocated with libbpf_find_kernel_btf() and unconditionally assigned to btf_vmlinux without freeing the previous instance:
tools/bpf/bpftool/cgroup.c:do_show_tree_fn() {
...
btf_vmlinux = libbpf_find_kernel_btf();
...
}Since do_show_tree_fn() is called for each cgroup during nftw() traversal, could this repeatedly leak the kernel BTF object (which can be several megabytes) on each cgroup processed? This seems like it could lead to an out-of-memory crash when running the "bpftool cgroup tree" command on a system with a moderate number of cgroups.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 8 lines · Yichong Chen
get_map_kv_btf()caches the vmlinux BTF object when a map usesbtf_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 leavesbtf_vmlinuxdangling before the command cleanup path runs.
Quoted 8 lines
Use
free_map_kv_btf()for per-entry cleanup, and reset the cachedbtf_vmlinuxpointer 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 <[email protected]>--- tools/bpf/bpftool/map.c | 12 +++++++++---
Quoted 46 lines
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)
PATCH v3 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 | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
tools/bpf/bpftool/struct_ops.c +10 -1
@@ -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 ; +} +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; }
Signature · 1 line
2.51.0
[email protected]
AI reviewRe: [PATCH v3 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Medium] Severe memory leak of the btf_vmlinux object during cgroup tree traversal or repeated cgroup commands in batch mode. --
commit f6cbf97292c743366f83c8ee625e792b14e79b4f
Author: Yichong Chen <[email protected]>
tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
struct_ops frees the global btf_vmlinux object. In batch mode, a later struct_ops command can reuse stale state. This resets the BTF pointer and cached map info state.
Quoted 4 lines
tools/bpf/bpftool/struct_ops.c +0
-0
[ ... ]
tools/bpf/bpftool/struct_ops.c int do_struct_ops(int argc, char **argv) · line 655
Quoted 7 lines
tools/bpf/bpftool/struct_ops.c +1
-1@@ -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 ();
[Severity: Medium]
This is a pre-existing issue, but while you are fixing the batch mode state for struct_ops, could a similar btf_vmlinux memory leak be occurring in cgroup commands?
Looking at tools/bpf/bpftool/cgroup.c:do_show():
btf_vmlinux = libbpf_find_kernel_btf();
In batch mode, does every cgroup show command overwrite the global btf_vmlinux pointer without freeing the previous BTF object?
Similarly, in tools/bpf/bpftool/cgroup.c:do_show_tree_fn():
btf_vmlinux = libbpf_find_kernel_btf();
During a cgroup tree command, nftw() calls this function for every cgroup directory. Does it load a new kernel BTF object each time and overwrite the global pointer without freeing the previous one, creating a leak that scales with the size of the cgroup hierarchy?
Quoted 2 lines
tools/bpf/bpftool/struct_ops.c +0
-0return err; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Emil Tsalapatis
reviewerRe: [PATCH v3 2/2] tools/bpf/bpftool: Reset vmlinux BTF after struct_ops commands
In reply to Yichong ChenQuoted 18 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]>--- tools/bpf/bpftool/struct_ops.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)tools/bpf/bpftool/struct_ops.c +0
-0@@ -21,6 +21,15 @@ static __u32 map_info_alloc_len;static struct btf *btf_vmlinux;
tools/bpf/bpftool/struct_ops.c static __u32 map_info_alloc_len; · line 31
Quoted 10 lines
tools/bpf/bpftool/struct_ops.c +8
-0static __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
Quoted 13 lines
tools/bpf/bpftool/struct_ops.c +2
-1+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; }