From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Menglong Dong <dongmenglong.8@bytedance.com>
Cc: Andrii Nakryiko <andrii@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eddy Z <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
Alexander Gordeev <agordeev@linux.ibm.com>,
Christian Borntraeger <borntraeger@linux.ibm.com>,
Sven Schnelle <svens@linux.ibm.com>,
"David S. Miller" <davem@davemloft.net>,
David Ahern <dsahern@kernel.org>,
Dave Hansen <dave.hansen@linux.intel.com>,
X86 ML <x86@kernel.org>, Steven Rostedt <rostedt@goodmis.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Quentin Monnet <quentin@isovalent.com>,
bpf <bpf@vger.kernel.org>,
linux-arm-kernel <linux-arm-kernel@lists.infradead.org>,
LKML <linux-kernel@vger.kernel.org>,
linux-riscv <linux-riscv@lists.infradead.org>,
linux-s390 <linux-s390@vger.kernel.org>,
Network Development <netdev@vger.kernel.org>,
linux-trace-kernel@vger.kernel.org,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@vger.kernel.org>,
linux-stm32@st-md-mailman.stormreply.com
Subject: Re: [PATCH bpf-next v2 7/9] libbpf: don't free btf if program of multi-link tracing existing
Date: Mon, 11 Mar 2024 18:55:32 -0700 [thread overview]
Message-ID: <CAADnVQK4tdefa3s=sim69Sc+ztd-hHohPEDXaUNVTU-mLNYUiw@mail.gmail.com> (raw)
In-Reply-To: <20240311093526.1010158-8-dongmenglong.8@bytedance.com>
On Mon, Mar 11, 2024 at 2:35 AM Menglong Dong
<dongmenglong.8@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@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.
next prev parent reply other threads:[~2024-03-12 1:55 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-11 9:35 [PATCH bpf-next v2 0/9] bpf: make tracing program support multi-link Menglong Dong
2024-03-11 9:35 ` [PATCH bpf-next v2 1/9] bpf: tracing: add support to record and check the accessed args Menglong Dong
2024-03-12 1:46 ` Alexei Starovoitov
2024-03-12 2:01 ` [External] " 梦龙董
2024-03-12 2:09 ` Alexei Starovoitov
2024-03-12 2:42 ` 梦龙董
2024-03-12 2:49 ` 梦龙董
2024-03-12 16:42 ` Alexei Starovoitov
2024-03-13 1:53 ` 梦龙董
2024-03-14 0:25 ` Alexei Starovoitov
2024-03-14 6:27 ` Jiri Olsa
2024-03-15 8:17 ` 梦龙董
2024-03-15 8:00 ` 梦龙董
2024-03-28 14:43 ` 梦龙董
2024-03-28 15:13 ` Steven Rostedt
2024-03-28 23:17 ` Alexei Starovoitov
2024-03-30 3:36 ` 梦龙董
2024-03-29 23:28 ` Andrii Nakryiko
2024-03-29 23:28 ` Andrii Nakryiko
2024-03-29 23:28 ` Andrii Nakryiko
2024-03-30 4:16 ` 梦龙董
2024-03-30 12:27 ` Steven Rostedt
2024-03-30 17:52 ` Jiri Olsa
2024-03-31 2:34 ` Andrii Nakryiko
2024-03-31 2:34 ` Andrii Nakryiko
2024-03-30 3:18 ` 梦龙董
2024-03-30 19:37 ` Steven Rostedt
2024-04-01 2:28 ` 梦龙董
2024-04-01 15:59 ` Steven Rostedt
2024-03-11 9:35 ` [PATCH bpf-next v2 2/9] bpf: refactor the modules_array to ptr_array Menglong Dong
2024-03-12 1:48 ` Alexei Starovoitov
2024-03-12 1:53 ` [External] " 梦龙董
2024-03-11 9:35 ` [PATCH bpf-next v2 3/9] bpf: trampoline: introduce struct bpf_tramp_link_conn Menglong Dong
2024-03-11 9:35 ` [PATCH bpf-next v2 4/9] bpf: trampoline: introduce bpf_tramp_multi_link Menglong Dong
2024-03-11 9:35 ` [PATCH bpf-next v2 5/9] bpf: verifier: add btf to the function args of bpf_check_attach_target Menglong Dong
2024-03-12 1:51 ` Alexei Starovoitov
2024-03-12 3:13 ` [External] " 梦龙董
2024-03-11 9:35 ` [PATCH bpf-next v2 6/9] bpf: tracing: add multi-link support Menglong Dong
2024-03-11 18:59 ` kernel test robot
2024-03-11 21:36 ` kernel test robot
2024-03-11 9:35 ` [PATCH bpf-next v2 7/9] libbpf: don't free btf if program of multi-link tracing existing Menglong Dong
2024-03-12 1:55 ` Alexei Starovoitov [this message]
2024-03-12 2:05 ` [External] " 梦龙董
2024-03-12 2:13 ` Alexei Starovoitov
2024-03-12 2:56 ` 梦龙董
2024-03-11 9:35 ` [PATCH bpf-next v2 8/9] libbpf: add support for the multi-link of tracing Menglong Dong
2024-03-11 15:29 ` Quentin Monnet
2024-03-12 1:43 ` [External] " 梦龙董
2024-03-12 1:56 ` Alexei Starovoitov
2024-03-12 2:44 ` [External] " 梦龙董
2024-03-12 16:11 ` Alexei Starovoitov
2024-03-13 1:14 ` 梦龙董
2024-03-11 9:35 ` [PATCH bpf-next v2 9/9] selftests/bpf: add testcases for " Menglong Dong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAADnVQK4tdefa3s=sim69Sc+ztd-hHohPEDXaUNVTU-mLNYUiw@mail.gmail.com' \
--to=alexei.starovoitov@gmail.com \
--cc=agordeev@linux.ibm.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=borntraeger@linux.ibm.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=davem@davemloft.net \
--cc=dongmenglong.8@bytedance.com \
--cc=dsahern@kernel.org \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-s390@vger.kernel.org \
--cc=linux-stm32@st-md-mailman.stormreply.com \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mathieu.desnoyers@efficios.com \
--cc=netdev@vger.kernel.org \
--cc=quentin@isovalent.com \
--cc=rostedt@goodmis.org \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=svens@linux.ibm.com \
--cc=x86@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).