]> git.hungrycats.org Git - linux/commit
bpf: Let callers of btf_parse_kptr() track life cycle of prog btf
authorAmery Hung <amery.hung@bytedance.com>
Tue, 13 Aug 2024 21:24:20 +0000 (21:24 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 23 Aug 2024 18:39:33 +0000 (11:39 -0700)
commitc5ef53420f46c9ca6badca4f4cabacd76de8091e
tree32bc477c25eae5c50f92f63ef7917d2b6abad6a9
parentf727b13dbea16c5e117e263aa8aea59d632d5660
bpf: Let callers of btf_parse_kptr() track life cycle of prog btf

btf_parse_kptr() and btf_record_free() do btf_get() and btf_put()
respectively when working on btf_record in program and map if there are
kptr fields. If the kptr is from program BTF, since both callers has
already tracked the life cycle of program BTF, it is safe to remove the
btf_get() and btf_put().

This change prevents memory leak of program BTF later when we start
searching for kptr fields when building btf_record for program. It can
happen when the btf fd is closed. The btf_put() corresponding to the
btf_get() in btf_parse_kptr() was supposed to be called by
btf_record_free() in btf_free_struct_meta_tab() in btf_free(). However,
it will never happen since the invocation of btf_free() depends on the
refcount of the btf to become 0 in the first place.

Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
Acked-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Amery Hung <amery.hung@bytedance.com>
Link: https://lore.kernel.org/r/20240813212424.2871455-2-amery.hung@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/btf.c
kernel/bpf/syscall.c