[External] Re: [PATCH bpf-next v2 7/9] libbpf: don't free btf if program of multi-link tracing existing
梦龙董
dongmenglong.8 at bytedance.com
Mon Mar 11 19:05:35 PDT 2024
On Tue, Mar 12, 2024 at 9:55 AM Alexei Starovoitov
<alexei.starovoitov at gmail.com> wrote:
>
> On Mon, Mar 11, 2024 at 2:35 AM Menglong Dong
> <dongmenglong.8 at bytedance.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 at bytedance.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.
Yes, libbpf will clean up the btfs when bpf_object is freed in
this commit. And I'm trying to offer a way to early free the btfs
by the users manual to reduce the memory usage. Or, the
btfs that we opened will keep existing until we close the
bpf_object.
This is optional, I can remove it if you prefer.
Thanks!
Menglong Dong
More information about the linux-riscv
mailing list