[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAADnVQK4tdefa3s=sim69Sc+ztd-hHohPEDXaUNVTU-mLNYUiw@mail.gmail.com>
Date: Mon, 11 Mar 2024 18:55:32 -0700
From: Alexei Starovoitov <alexei.starovoitov@...il.com>
To: Menglong Dong <dongmenglong.8@...edance.com>
Cc: Andrii Nakryiko <andrii@...nel.org>, Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>, Martin KaFai Lau <martin.lau@...ux.dev>, Eddy Z <eddyz87@...il.com>,
Song Liu <song@...nel.org>, Yonghong Song <yonghong.song@...ux.dev>,
John Fastabend <john.fastabend@...il.com>, KP Singh <kpsingh@...nel.org>,
Stanislav Fomichev <sdf@...gle.com>, Hao Luo <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>,
Alexander Gordeev <agordeev@...ux.ibm.com>, Christian Borntraeger <borntraeger@...ux.ibm.com>,
Sven Schnelle <svens@...ux.ibm.com>, "David S. Miller" <davem@...emloft.net>,
David Ahern <dsahern@...nel.org>, Dave Hansen <dave.hansen@...ux.intel.com>,
X86 ML <x86@...nel.org>, Steven Rostedt <rostedt@...dmis.org>,
Mathieu Desnoyers <mathieu.desnoyers@...icios.com>, Quentin Monnet <quentin@...valent.com>,
bpf <bpf@...r.kernel.org>,
linux-arm-kernel <linux-arm-kernel@...ts.infradead.org>, LKML <linux-kernel@...r.kernel.org>,
linux-riscv <linux-riscv@...ts.infradead.org>, linux-s390 <linux-s390@...r.kernel.org>,
Network Development <netdev@...r.kernel.org>, linux-trace-kernel@...r.kernel.org,
"open list:KERNEL SELFTEST FRAMEWORK" <linux-kselftest@...r.kernel.org>, linux-stm32@...md-mailman.stormreply.com
Subject: Re: [PATCH bpf-next v2 7/9] libbpf: don't free btf if program of
multi-link tracing existing
On Mon, Mar 11, 2024 at 2:35 AM Menglong Dong
<dongmenglong.8@...edance.com> wrote:
>
> By default, the kernel btf that we load during loading program will be
> freed after the programs are loaded in bpf_object_load(). However, we
> still need to use these btf for tracing of multi-link during attaching.
> Therefore, we don't free the btfs until the bpf object is closed if any
> bpf programs of the type multi-link tracing exist.
>
> Meanwhile, introduce the new api bpf_object__free_btf() to manually free
> the btfs after attaching.
>
> Signed-off-by: Menglong Dong <dongmenglong.8@...edance.com>
> ---
> tools/lib/bpf/libbpf.c | 47 ++++++++++++++++++++++++++++++----------
> tools/lib/bpf/libbpf.h | 2 ++
> tools/lib/bpf/libbpf.map | 1 +
> 3 files changed, 38 insertions(+), 12 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 567ad367e7aa..fd5428494a7e 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -8267,6 +8267,39 @@ static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
> return 0;
> }
>
> +void bpf_object__free_btfs(struct bpf_object *obj)
> +{
> + int i;
> +
> + /* clean up module BTFs */
> + for (i = 0; i < obj->btf_module_cnt; i++) {
> + close(obj->btf_modules[i].fd);
> + btf__free(obj->btf_modules[i].btf);
> + free(obj->btf_modules[i].name);
> + }
> + free(obj->btf_modules);
> + obj->btf_modules = NULL;
> + obj->btf_module_cnt = 0;
> +
> + /* clean up vmlinux BTF */
> + btf__free(obj->btf_vmlinux);
> + obj->btf_vmlinux = NULL;
> +}
> +
> +static void bpf_object_early_free_btf(struct bpf_object *obj)
> +{
> + struct bpf_program *prog;
> +
> + bpf_object__for_each_program(prog, obj) {
> + if (prog->expected_attach_type == BPF_TRACE_FENTRY_MULTI ||
> + prog->expected_attach_type == BPF_TRACE_FEXIT_MULTI ||
> + prog->expected_attach_type == BPF_MODIFY_RETURN_MULTI)
> + return;
> + }
> +
> + bpf_object__free_btfs(obj);
> +}
> +
> static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path)
> {
> int err, i;
> @@ -8307,18 +8340,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
> /* clean up fd_array */
> zfree(&obj->fd_array);
>
> - /* clean up module BTFs */
> - for (i = 0; i < obj->btf_module_cnt; i++) {
> - close(obj->btf_modules[i].fd);
> - btf__free(obj->btf_modules[i].btf);
> - free(obj->btf_modules[i].name);
> - }
> - free(obj->btf_modules);
> -
> - /* clean up vmlinux BTF */
> - btf__free(obj->btf_vmlinux);
> - obj->btf_vmlinux = NULL;
> -
> + bpf_object_early_free_btf(obj);
> obj->loaded = true; /* doesn't matter if successfully or not */
>
> if (err)
> @@ -8791,6 +8813,7 @@ void bpf_object__close(struct bpf_object *obj)
> usdt_manager_free(obj->usdt_man);
> obj->usdt_man = NULL;
>
> + bpf_object__free_btfs(obj);
> bpf_gen__free(obj->gen_loader);
> bpf_object__elf_finish(obj);
> bpf_object_unload(obj);
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 5723cbbfcc41..c41a909ea4c1 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -299,6 +299,8 @@ LIBBPF_API struct bpf_program *
> bpf_object__find_program_by_name(const struct bpf_object *obj,
> const char *name);
>
> +LIBBPF_API void bpf_object__free_btfs(struct bpf_object *obj);
> +
It shouldn't be exported.
libbpf should clean it up when bpf_object is freed.
Powered by blists - more mailing lists