Skip to content

Commit

Permalink
libbpf: Free btf_vmlinux when closing bpf_object
Browse files Browse the repository at this point in the history
[ Upstream commit 29d67fd ]

I hit a memory leak when testing bpf_program__set_attach_target().
Basically, set_attach_target() may allocate btf_vmlinux, for example,
when setting attach target for bpf_iter programs. But btf_vmlinux
is freed only in bpf_object_load(), which means if we only open
bpf object but not load it, setting attach target may leak
btf_vmlinux.

So let's free btf_vmlinux in bpf_object__close() anyway.

Signed-off-by: Hao Luo <haoluo@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230822193840.1509809-1-haoluo@google.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
haoluo1022 authored and gregkh committed Sep 23, 2023
1 parent a534434 commit a32f5bc
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.c
Expand Up @@ -8356,6 +8356,7 @@ void bpf_object__close(struct bpf_object *obj)
bpf_object__elf_finish(obj);
bpf_object_unload(obj);
btf__free(obj->btf);
btf__free(obj->btf_vmlinux);
btf_ext__free(obj->btf_ext);

for (i = 0; i < obj->nr_maps; i++)
Expand Down

0 comments on commit a32f5bc

Please sign in to comment.