* [PATCH] libbpf: optimize the redundant code in the bpf_object__init_user_btf_maps() function.
@ 2025-10-24 6:07 Jianyun Gao
2025-10-24 6:57 ` Jiri Olsa
0 siblings, 1 reply; 3+ messages in thread
From: Jianyun Gao @ 2025-10-24 6:07 UTC (permalink / raw)
To: linux-kernel
Cc: Jianyun Gao, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
Hao Luo, Jiri Olsa,
open list:BPF [GENERAL] (Safe Dynamic Programs and Tools)
In the elf_sec_data() function, the input parameter 'scn' will be
evaluated. If it is NULL, then it will directly return NULL. Therefore,
the return value of the elf_sec_data() function already takes into
account the case where the input parameter scn is NULL. Therefore,
subsequently, the code only needs to check whether the return value of
the elf_sec_data() function is NULL.
Signed-off-by: Jianyun Gao <jianyungao89@gmail.com>
---
tools/lib/bpf/libbpf.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index b90574f39d1c..9e66104a61eb 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -2988,15 +2988,15 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
int nr_types, i, vlen, err;
const struct btf_type *t;
const char *name;
- Elf_Data *data;
+ Elf_Data *scn_data;
Elf_Scn *scn;
if (obj->efile.btf_maps_shndx < 0)
return 0;
scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
- data = elf_sec_data(obj, scn);
- if (!scn || !data) {
+ scn_data = elf_sec_data(obj, scn);
+ if (!scn_data) {
pr_warn("elf: failed to get %s map definitions for %s\n",
MAPS_ELF_SEC, obj->path);
return -EINVAL;
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] libbpf: optimize the redundant code in the bpf_object__init_user_btf_maps() function.
2025-10-24 6:07 [PATCH] libbpf: optimize the redundant code in the bpf_object__init_user_btf_maps() function Jianyun Gao
@ 2025-10-24 6:57 ` Jiri Olsa
2025-10-24 7:55 ` Jianyun Gao
0 siblings, 1 reply; 3+ messages in thread
From: Jiri Olsa @ 2025-10-24 6:57 UTC (permalink / raw)
To: Jianyun Gao
Cc: linux-kernel, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
Hao Luo,
open list:BPF [GENERAL] (Safe Dynamic Programs and Tools)
On Fri, Oct 24, 2025 at 02:07:20PM +0800, Jianyun Gao wrote:
> In the elf_sec_data() function, the input parameter 'scn' will be
> evaluated. If it is NULL, then it will directly return NULL. Therefore,
> the return value of the elf_sec_data() function already takes into
> account the case where the input parameter scn is NULL. Therefore,
> subsequently, the code only needs to check whether the return value of
> the elf_sec_data() function is NULL.
>
> Signed-off-by: Jianyun Gao <jianyungao89@gmail.com>
> ---
> tools/lib/bpf/libbpf.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index b90574f39d1c..9e66104a61eb 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -2988,15 +2988,15 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
> int nr_types, i, vlen, err;
> const struct btf_type *t;
> const char *name;
> - Elf_Data *data;
> + Elf_Data *scn_data;
makes sense to me, but this rename breaks compilation later on
libbpf.c:3027:53: error: ‘data’ undeclared (first use in this function)
jirka
> Elf_Scn *scn;
>
> if (obj->efile.btf_maps_shndx < 0)
> return 0;
>
> scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
> - data = elf_sec_data(obj, scn);
> - if (!scn || !data) {
> + scn_data = elf_sec_data(obj, scn);
> + if (!scn_data) {
> pr_warn("elf: failed to get %s map definitions for %s\n",
> MAPS_ELF_SEC, obj->path);
> return -EINVAL;
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] libbpf: optimize the redundant code in the bpf_object__init_user_btf_maps() function.
2025-10-24 6:57 ` Jiri Olsa
@ 2025-10-24 7:55 ` Jianyun Gao
0 siblings, 0 replies; 3+ messages in thread
From: Jianyun Gao @ 2025-10-24 7:55 UTC (permalink / raw)
To: Jiri Olsa
Cc: linux-kernel, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
Hao Luo,
open list:BPF [GENERAL] (Safe Dynamic Programs and Tools)
Hi Jiri, thank you for your review. And I have realized my mistake in
this patch. I will fix it in the next patch!
On Fri, Oct 24, 2025 at 2:57 PM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Fri, Oct 24, 2025 at 02:07:20PM +0800, Jianyun Gao wrote:
> > In the elf_sec_data() function, the input parameter 'scn' will be
> > evaluated. If it is NULL, then it will directly return NULL. Therefore,
> > the return value of the elf_sec_data() function already takes into
> > account the case where the input parameter scn is NULL. Therefore,
> > subsequently, the code only needs to check whether the return value of
> > the elf_sec_data() function is NULL.
> >
> > Signed-off-by: Jianyun Gao <jianyungao89@gmail.com>
> > ---
> > tools/lib/bpf/libbpf.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index b90574f39d1c..9e66104a61eb 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -2988,15 +2988,15 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
> > int nr_types, i, vlen, err;
> > const struct btf_type *t;
> > const char *name;
> > - Elf_Data *data;
> > + Elf_Data *scn_data;
>
> makes sense to me, but this rename breaks compilation later on
>
> libbpf.c:3027:53: error: ‘data’ undeclared (first use in this function)
>
> jirka
>
> > Elf_Scn *scn;
> >
> > if (obj->efile.btf_maps_shndx < 0)
> > return 0;
> >
> > scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
> > - data = elf_sec_data(obj, scn);
> > - if (!scn || !data) {
> > + scn_data = elf_sec_data(obj, scn);
> > + if (!scn_data) {
> > pr_warn("elf: failed to get %s map definitions for %s\n",
> > MAPS_ELF_SEC, obj->path);
> > return -EINVAL;
> > --
> > 2.34.1
> >
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-10-24 7:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-24 6:07 [PATCH] libbpf: optimize the redundant code in the bpf_object__init_user_btf_maps() function Jianyun Gao
2025-10-24 6:57 ` Jiri Olsa
2025-10-24 7:55 ` Jianyun Gao
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®