bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Giuliano Procida <gprocida@google.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: dwarves@vger.kernel.org, kernel-team@android.com,
	"Matthias Männich" <maennich@google.com>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>, bpf <bpf@vger.kernel.org>,
	"Kernel Team" <kernel-team@fb.com>
Subject: Re: [PATCH 3/3] btf_encoder: Set .BTF section alignment to 16
Date: Tue, 26 Jan 2021 11:43:01 +0000	[thread overview]
Message-ID: <CAGvU0H=++8jWJQKg-BJoi1qxCJe=bzJqsYhRLwpwH51dXLO=Jw@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzZPSEsKn6DiFwffTW81iFPVO329RAnA+bm0NPPiBqnqag@mail.gmail.com>

Hi.

On Tue, 26 Jan 2021 at 00:28, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Mon, Jan 25, 2021 at 4:53 AM Giuliano Procida <gprocida@google.com> wrote:
> >
> > Hi.
> >
> > On Thu, 21 Jan 2021 at 20:08, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
> > >
> > > On Thu, Jan 21, 2021 at 3:07 AM Giuliano Procida <gprocida@google.com> wrote:
> > > >
> > > > Hi.
> > > >
> > > > On Thu, 21 Jan 2021 at 07:16, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
> > > >>
> > > >> On Mon, Jan 18, 2021 at 8:01 AM Giuliano Procida <gprocida@google.com> wrote:
> > > >> >
> > > >> > This is to avoid misaligned access when memory-mapping ELF sections.
> > > >> >
> > > >> > Signed-off-by: Giuliano Procida <gprocida@google.com>
> > > >> > ---
> > > >> >  libbtf.c | 8 ++++++++
> > > >> >  1 file changed, 8 insertions(+)
> > > >> >
> > > >> > diff --git a/libbtf.c b/libbtf.c
> > > >> > index 7552d8e..2f12d53 100644
> > > >> > --- a/libbtf.c
> > > >> > +++ b/libbtf.c
> > > >> > @@ -797,6 +797,14 @@ static int btf_elf__write(const char *filename, struct btf *btf)
> > > >> >                         goto unlink;
> > > >> >                 }
> > > >> >
> > > >> > +               snprintf(cmd, sizeof(cmd), "%s --set-section-alignment .BTF=16 %s",
> > > >> > +                        llvm_objcopy, filename);
> > > >>
> > > >> does it align inside the ELF file to 16 bytes, or does it request the
> > > >> linker to align it at 16 byte alignment in memory? Given .BTF section
> > > >> is not loadable, trying to understand the implications.
> > > >>
> > > >
> > > > We have a tool that loads BTF from ELF files. It uses mmap and "parses" the BTF as structs in memory. The ELF file is mapped with page alignment but the BTF section within it has no alignment at all. Using MSAN (IIRC) we get warnings about misaligned accesses. Everything within BTF itself is naturally aligned, so it makes sense to align the section within ELF as well. There are probably some architectures where this makes the difference between working and SIGBUS.
> > > >
> > >
> > > Right, ok, thanks for explaining!
> > >
> > > > I did try to get objcopy to set alignment at the point the section is added. However, this didn't work.
> > > >
> > > >>
> > > >>
> > > >> > +               if (system(cmd)) {
> > > >>
> > > >> Also curious, if objcopy emits error (saying that
> > > >> --set-section-alignment argument is not recognized), will that error
> > > >> be shown in stdout? or system() consumes it without redirecting it to
> > > >> stdout?
> > > >>
> > > >
> > > > I believe it goes to stderr. I would need to check. system() will not consume this. I'm not keen to write stderr (or stdout) post-processing code in plain C.
> > > >
> > >
> > > You can use popen() to capture/hide output, this is a better
> > > alternative to system() in this case. We don't want "expected
> > > warnings" in kernel build process.
> > >
> > > >>
> > > >> > +                       /* non-fatal, this is a nice-to-have and it's only supported from LLVM 10 */
> > > >> > +                       fprintf(stderr, "%s: warning: failed to align .BTF section in '%s': %d!\n",
> > > >> > +                               __func__, filename, errno);
> > > >>
> > > >> Probably better to emit this warning only in verbose mode, otherwise
> > > >> lots of people will start complaining that they get some new warnings
> > > >> from pahole.
> > > >>
> > > >
> > > > It may be better to just use POSIX and ELF APIs directly instead of objcopy. This way the section can be added with the right alignment directly. pahole is already linked against libelf and if we could get rid of the external dependency on objcopy it would be a win in more than one way.
> > >
> > > This would be great, yes. At some point I remember giving it a try,
> > > but for some reason I couldn't make libelf flush data and update
> > > section headers properly. Maybe you'll have better luck. Though I
> > > think I was trying to mark section loadable, and eventually I probably
> > > managed to do that, but still abandoned it (it's not enough to mark
> > > section loadable, you have to assign it to ELF segment as well, which
> > > libelf doesn't allow to do and you need linker support). Anyways, give
> > > it a try, it should work.
> >
> > I struggled for a day and a bit and have got this (ELF_F_LAYOUT etc.)
> > working. There are some caveats:
> >
> > 1. Laying out only the new / updated sections can leave gaps.
> >
> > In practice, for vmlinux, it's a very small hole. To fix this, I'd
> > need to reposition .strtab as well as .BTF and .shstrtab.
> >
> > 2. vmlinux increases in size as llvm-objcopy was trimming down .strtab.
> >
> > I know very little about this, but I'd guess that the kernel linker
> > scripts are leaving strings in .strtab that are not referenced by
> > .symtab.
> >
> > I'll send a short series out for review soon.
>
> 1. Hm.. I realized I don't get why you need 16-byte alignment. Can you
> comment on why 8 doesn't work?
>

Sorry, 16 was a number that was guaranteed to work. However, after
looking in more detail, 8 seems fine too.

> 2. If you care about vmlinux BTF only, I think an easier way to ensure
> proper alignment is to adjust include/asm-generic/vmlinux.lds.h and
> add `. = ALIGN(8);` before .BTF (see how we ensure 4-byte alignment
> for .BTF_ids)
>

Firstly, while we do care mostly about vmlinux, we also care about
modules and even potentially plain userspace .o and .so files. In
terms of testing and development, working with simple objects is
simpler and faster.

Does the above mean vmlinux will always contain an empty .BTF, ready
to be populated?

If not, we and others may add .BTF with pahole -J and that's the code
I've been working on.

> If you have code ready to get rid of llvm-objcopy requirement for
> pahole, please still post, but we'll need to test it very thoroughly
> to ensure there are no regressions before landing in pahole.

It's stuck in a vger/kernel.org queue somewhere.

The updated vmlinux my code generates is I believe much closer to the
original vmlinux than what the existing code produces using
llvm-objcopy. But that's not necessarily a good thing.

Regards.

>
> >
> > Giuliano.
> >
> > >
> > > >
> > > >>
> > > >>
> > > >> > +               }
> > > >> > +
> > > >> >                 err = 0;
> > > >> >         unlink:
> > > >> >                 unlink(tmp_fn);
> > > >> > --
> > > >> > 2.30.0.284.gd98b1dd5eaa7-goog
> > > >> >
> > > >
> > > >
> > > > I'll see if I can spend a little time on this idea instead.
> > > >
> > > > Regards,
> > > > Giuliano.
> > >
> > > --
> > > To unsubscribe from this group and stop receiving emails from it, send an email to kernel-team+unsubscribe@android.com.
> > >

  reply	other threads:[~2021-01-26 11:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-18 16:01 [PATCH 0/3] Small fixes and improvements Giuliano Procida
2021-01-18 16:01 ` [PATCH 1/3] btf_encoder: Fix handling of restrict qualifier Giuliano Procida
2021-01-21  7:07   ` Andrii Nakryiko
2021-01-21 13:04     ` Arnaldo Carvalho de Melo
2021-01-18 16:01 ` [PATCH 2/3] btf_encoder: Improve error-handling around objcopy Giuliano Procida
2021-01-21  7:09   ` Andrii Nakryiko
2021-01-18 16:01 ` [PATCH 3/3] btf_encoder: Set .BTF section alignment to 16 Giuliano Procida
2021-01-21  7:16   ` Andrii Nakryiko
     [not found]     ` <CAGvU0HmE+gs8eNQcXmFrEERHaiGEnMgqxBho4Ny3DLCe6WR55Q@mail.gmail.com>
2021-01-21 20:07       ` Andrii Nakryiko
2021-01-25 12:53         ` Giuliano Procida
2021-01-26  0:28           ` Andrii Nakryiko
2021-01-26 11:43             ` Giuliano Procida [this message]
2021-01-27 15:08         ` Giuliano Procida
2021-01-27 18:06           ` Giuliano Procida
2021-01-27 19:56             ` Andrii Nakryiko
2021-01-21 11:35 ` [PATCH dwarves v2 0/3] Small fixes and improvements Giuliano Procida
2021-01-21 11:35   ` [PATCH dwarves v2 1/3] btf_encoder: Fix handling of restrict qualifier Giuliano Procida
2021-01-21 13:21     ` Arnaldo Carvalho de Melo
2021-01-21 11:35   ` [PATCH dwarves v2 2/3] btf_encoder: Improve error-handling around objcopy Giuliano Procida
2021-01-21 13:24     ` Arnaldo Carvalho de Melo
2021-01-21 11:35   ` [PATCH dwarves v2 3/3] btf_encoder: Set .BTF section alignment to 16 Giuliano Procida
2021-01-21 13:23     ` Arnaldo Carvalho de Melo

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='CAGvU0H=++8jWJQKg-BJoi1qxCJe=bzJqsYhRLwpwH51dXLO=Jw@mail.gmail.com' \
    --to=gprocida@google.com \
    --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).