From: Giuliano Procida <gprocida@google.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: dwarves@vger.kernel.org,
"Arnaldo Carvalho de Melo" <acme@kernel.org>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Alexei Starovoitov" <ast@kernel.org>,
"Matthias Männich" <maennich@google.com>,
kernel-team@android.com, "Kernel Team" <kernel-team@fb.com>,
bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH dwarves v3 2/5] btf_encoder: Do not use both structs and pointers for the same data
Date: Tue, 9 Feb 2021 14:52:54 +0000 [thread overview]
Message-ID: <CAGvU0HnzLxUsQz2v5qikU6Xts2sPTCKMdLOYS1=ksE6+bGUkcw@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzZP8hWkBUKi15j3E+D63n9269viQyCagsHpJFm=DOE4Gg@mail.gmail.com>
Hi.
On Mon, 8 Feb 2021 at 22:23, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Feb 5, 2021 at 5:42 AM Giuliano Procida <gprocida@google.com> wrote:
> >
> > Many operations in the libelf API return a pointer to a user-provided
> > struct (on success) or NULL (on failure).
> >
> > There are a couple of places in btf_elf__write where both structs and
> > pointers to the same structs are used. Holding on to the pointers
> > raises ownership and lifetime issues unncessarily and the code is
>
> typo: unnecessarily
>
Thanks. Fixed.
> > cleaner with only a single access path for these data.
> >
> > The code now treats the returned pointers as booleans.
> >
> > Signed-off-by: Giuliano Procida <gprocida@google.com>
> > ---
>
> styling nits, but otherwise LGTM
>
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
>
> > libbtf.c | 14 ++++++--------
> > 1 file changed, 6 insertions(+), 8 deletions(-)
> >
> > diff --git a/libbtf.c b/libbtf.c
> > index 7bc49ba..ace8896 100644
> > --- a/libbtf.c
> > +++ b/libbtf.c
> > @@ -698,8 +698,7 @@ int32_t btf_elf__add_datasec_type(struct btf_elf *btfe, const char *section_name
> >
> > static int btf_elf__write(const char *filename, struct btf *btf)
> > {
> > - GElf_Shdr shdr_mem, *shdr;
> > - GElf_Ehdr ehdr_mem, *ehdr;
> > + GElf_Ehdr ehdr;
> > Elf_Data *btf_data = NULL;
> > Elf_Scn *scn = NULL;
> > Elf *elf = NULL;
> > @@ -727,13 +726,12 @@ static int btf_elf__write(const char *filename, struct btf *btf)
> >
> > elf_flagelf(elf, ELF_C_SET, ELF_F_DIRTY);
> >
> > - ehdr = gelf_getehdr(elf, &ehdr_mem);
> > - if (ehdr == NULL) {
> > + if (!gelf_getehdr(elf, &ehdr)) {
> > elf_error("elf_getehdr failed");
> > goto out;
> > }
> >
> > - switch (ehdr_mem.e_ident[EI_DATA]) {
> > + switch (ehdr.e_ident[EI_DATA]) {
> > case ELFDATA2LSB:
> > btf__set_endianness(btf, BTF_LITTLE_ENDIAN);
> > break;
> > @@ -751,10 +749,10 @@ static int btf_elf__write(const char *filename, struct btf *btf)
> >
> > elf_getshdrstrndx(elf, &strndx);
> > while ((scn = elf_nextscn(elf, scn)) != NULL) {
> > - shdr = gelf_getshdr(scn, &shdr_mem);
> > - if (shdr == NULL)
> > + GElf_Shdr shdr;
>
> it's a good style to have an empty line between variable declaration
> block and subsequent instructions
>
The variable in this question is effectively initialised by the
statement on the next line, breaking them apart looks odd.
Also, this is not a variable that needs end-of-scope clean-up. Its
position at the top of the scope is coincidental.
Later commits in the series also place declaration and initialisation
as close together as possible.
The only variables I would intentionally place at the top of a given
scope *and* far from their natural points of initialisation are those
corresponding to resources that need to be released at the end of the
scope, with the labelled exit idiom.
I feel this gives a better balance between readability (keeping things
local) and keeping track of resources (memory, fds, other handles) in
a scope.
However, if that's contrary to the house style, it's easy enough to
pull all the declarations out and move them to the top and separate
them; the compiler should be clever enough to share stack slots in any
case.
Let me know.
Regards,
Giuliano.
>
> > + if (!gelf_getshdr(scn, &shdr))
> > continue;
> > - char *secname = elf_strptr(elf, strndx, shdr->sh_name);
> > + char *secname = elf_strptr(elf, strndx, shdr.sh_name);
> > if (strcmp(secname, ".BTF") == 0) {
> > btf_data = elf_getdata(scn, btf_data);
> > break;
> > --
> > 2.30.0.478.g8a0d178c01-goog
> >
next prev parent reply other threads:[~2021-02-09 14:54 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <87a83353155506cc02141e6e4108d89aa4e7d284>
2021-02-01 17:25 ` [PATCH dwarves v2 0/4] BTF ELF writing changes Giuliano Procida
2021-02-01 17:25 ` [PATCH dwarves v2 1/4] btf_encoder: Add .BTF section using libelf Giuliano Procida
2021-02-04 4:10 ` Andrii Nakryiko
2021-02-04 18:29 ` Giuliano Procida
2021-02-01 17:25 ` [PATCH dwarves v2 2/4] btf_encoder: Manually lay out updated ELF sections Giuliano Procida
2021-02-04 4:13 ` Andrii Nakryiko
2021-02-04 18:34 ` Giuliano Procida
2021-02-04 23:06 ` Andrii Nakryiko
2021-02-01 17:25 ` [PATCH dwarves v2 3/4] btf_encoder: Add .BTF as a loadable segment Giuliano Procida
2021-02-02 10:54 ` Giuliano Procida
2021-02-01 17:25 ` [PATCH dwarves v2 4/4] btf_encoder: Align .BTF section/segment to 8 bytes Giuliano Procida
2021-02-04 4:10 ` Andrii Nakryiko
2021-02-04 15:11 ` Giuliano Procida
2021-02-04 15:11 ` [PATCH] btf_encoder: Align .BTF section " Giuliano Procida
2021-02-05 13:42 ` [PATCH dwarves v3 0/5] ELF writing changes Giuliano Procida
2021-02-05 13:42 ` [PATCH dwarves v3 1/5] btf_encoder: Funnel ELF error reporting through a macro Giuliano Procida
2021-02-08 22:20 ` Andrii Nakryiko
2021-02-05 13:42 ` [PATCH dwarves v3 2/5] btf_encoder: Do not use both structs and pointers for the same data Giuliano Procida
2021-02-08 22:23 ` Andrii Nakryiko
2021-02-09 14:52 ` Giuliano Procida [this message]
2021-02-05 13:42 ` [PATCH dwarves v3 3/5] btf_encoder: Traverse sections using a for-loop Giuliano Procida
2021-02-08 22:24 ` Andrii Nakryiko
2021-02-09 14:59 ` Giuliano Procida
2021-02-05 13:42 ` [PATCH dwarves v3 4/5] btf_encoder: Add .BTF section using libelf Giuliano Procida
2021-02-08 22:29 ` Andrii Nakryiko
2021-02-09 15:04 ` Giuliano Procida
2021-02-05 13:42 ` [PATCH dwarves v3 5/5] btf_encoder: Align .BTF section to 8 bytes Giuliano Procida
2021-02-08 22:29 ` Andrii Nakryiko
2021-02-09 15:05 ` Giuliano Procida
2021-02-17 11:07 ` [PATCH dwarves v4 0/5] ELF writing changes Giuliano Procida
2021-02-17 11:08 ` [PATCH dwarves v4 1/5] btf_encoder: Funnel ELF error reporting through a macro Giuliano Procida
2021-02-17 11:08 ` [PATCH dwarves v4 2/5] btf_encoder: Do not use both structs and pointers for the same data Giuliano Procida
2021-02-17 11:08 ` [PATCH dwarves v4 3/5] btf_encoder: Traverse sections using a for-loop Giuliano Procida
2021-02-17 11:08 ` [PATCH dwarves v4 4/5] btf_encoder: Add .BTF section using libelf Giuliano Procida
2021-02-17 11:08 ` [PATCH dwarves v4 5/5] btf_encoder: Align .BTF section to 8 bytes Giuliano Procida
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='CAGvU0HnzLxUsQz2v5qikU6Xts2sPTCKMdLOYS1=ksE6+bGUkcw@mail.gmail.com' \
--to=gprocida@google.com \
--cc=acme@kernel.org \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=dwarves@vger.kernel.org \
--cc=kernel-team@android.com \
--cc=kernel-team@fb.com \
--cc=maennich@google.com \
/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).