Skip to contentSkip to conversation

libbpf: Free btf_vmlinux when closing bpf_object

PATCH Applied Hao Luo 2 participants 2 messages Aug 22, 2023

Subsystems (2)
lore
Share & tools
lore

Hao Luo

author
#0 lore raw

PATCH 1/1 libbpf: Free btf_vmlinux when closing bpf_object

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.

---
 tools/lib/bpf/libbpf.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +1 -0
index b8afe2f5bc93..4c3967d94b6d 100644
@@ -8423,6 +8423,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++)
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Tue, 22 Aug 2023 12:38:40 -0700 you wrote:

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.

[...]

Here is the summary with links:
- [bpf-next] libbpf: Free btf_vmlinux when closing bpf_object

    https://git.kernel.org/bpf/bpf-next/c/29d67fdebc42

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html