From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
To: Soma Nakata <soma.nakata01@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
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@fomichev.me>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] libbpf: Initialize st_ops->tname with strdup()
Date: Wed, 21 Aug 2024 14:15:59 +0200 [thread overview]
Message-ID: <ecd1af32-8e6b-45d3-8434-0e981fd198ea@wanadoo.fr> (raw)
In-Reply-To: <20240821112344.54299-3-soma.nakata01@gmail.com>
Le 21/08/2024 à 13:23, Soma Nakata a écrit :
> `tname` is returned by `btf__name_by_offset()` as well as `var_name`,
> and these addresses point to strings in the btf. Since their locations
> may change while loading the bpf program, using `strdup()` ensures
> `tname` is safely stored.
>
> Signed-off-by: Soma Nakata <soma.nakata01@gmail.com>
> ---
> tools/lib/bpf/libbpf.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a3be6f8fac09..f4ad1b993ec5 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -496,7 +496,7 @@ struct bpf_program {
> };
>
> struct bpf_struct_ops {
> - const char *tname;
> + char *tname;
> const struct btf_type *type;
> struct bpf_program **progs;
> __u32 *kern_func_off;
> @@ -1423,7 +1423,9 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
> memcpy(st_ops->data,
> data->d_buf + vsi->offset,
> type->size);
> - st_ops->tname = tname;
> + st_ops->tname = strdup(tname);
> + if (!st_ops->tname)
> + return -ENOMEM;
Certainly a matter of taste, but I would personally move it just after
"st_ops->kern_func_off = malloc()" and add the NULL check with the
existing ones.
BTW, there are some memory leaks if 1 or more allocations fail in this
function.
Not sure if it is an issue or not, and what should be done in this case.
CJ
> st_ops->type = type;
> st_ops->type_id = type_id;
>
> @@ -8984,6 +8986,7 @@ static void bpf_map__destroy(struct bpf_map *map)
> map->mmaped = NULL;
>
> if (map->st_ops) {
> + zfree(&map->st_ops->tname);
> zfree(&map->st_ops->data);
> zfree(&map->st_ops->progs);
> zfree(&map->st_ops->kern_func_off);
next prev parent reply other threads:[~2024-08-21 12:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-21 11:23 Soma Nakata
2024-08-21 12:15 ` Christophe JAILLET [this message]
2024-08-21 13:30 ` Soma Nakata
2024-08-21 13:41 ` Christophe JAILLET
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=ecd1af32-8e6b-45d3-8434-0e981fd198ea@wanadoo.fr \
--to=christophe.jaillet@wanadoo.fr \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=soma.nakata01@gmail.com \
--cc=song@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
all inboxes | Powered by JetHome®