bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Yonghong Song <yhs@fb.com>, Andrii Nakryiko <andrii@kernel.org>,
	bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
	Alexei Starovoitov <ast@fb.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH v2 bpf-next 2/6] libbpf: rename static variables during linking
Date: Mon, 26 Apr 2021 16:11:23 -0700	[thread overview]
Message-ID: <CAEf4BzYZX9YJcoragK20cvQvr_tPTWYBQSRh7diKc1KoCtu4Dg@mail.gmail.com> (raw)
In-Reply-To: <20210426223449.5njjmcjpu63chqbb@ast-mbp.dhcp.thefacebook.com>

On Mon, Apr 26, 2021 at 3:34 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Mon, Apr 26, 2021 at 08:44:04AM -0700, Andrii Nakryiko wrote:
> >
> > >
> > > > Static maps are slightly different, because we use SEC() which marks
> > > > them as used, so they should always be present.
> > >
> > > yes. The used attribute makes the compiler keep the data,
> > > but it can still inline it and lose the reference in the .text.
> >
> > At least if the map is actually used with helpers (e.g.,
> > bpf_map_lookup_elem(&map, ...)) it would be invalid for compiler to do
> > anything crazy with that map reference, because compiler has no
> > visibility into what opaque helpers do with that memory. So I don't
> > think it can alias multiple maps, for instance. So I think static maps
> > should be fine.
>
> Yeah. That makes sense.
>
> > See above about passing a pointer to map into black box functions. I'd
> > bet that the compiler can't merge together two different references at
> > least because of that.
> >
> > For static maps, btw, just like for static functions and vars, there
> > is no symbol, it's an offset into .maps section. We use that offset to
> > identify the map itself.
>
> Ok. Sounds like there is a desire to expose both static and static volatile
> into skeleton.
> Sure, but let's make it such the linking step doesn't change the skeleton.
> Imagine a project that using single .bpf.c file and skeleton.
> It grows and wants to split itself into multiple .bpf.c.
> If such split would change the skeleton generated var/map names
> it would be annoying user experience.

It's surely not ideal, but it's a one-time step and only when user is
ready to switch to linker, so I don't see it as such a big problem.

>
> I see few options to avoid that:
> - keeping the btf names as-is during linking
> The final .o can have multiple vars and maps with the same name.
> The skeleton gen can see the name collision and disambiguate them.
> Here I think it's important to give users a choice. Blindly appending
> file name is not ideal.
> How to express it cleanly in .bpf.c? I don't know. SEC() would be a bit
> ugly. May be similar to core flavors? ___1 and ___2 ? Also not ideal.

___1 vs ___2 doesn't tell you which file you are accessing static
variable from, you need to go and figure out the order of linking. If
you look at bpf_linker__add_file() API, it has opts->object_name which
allows you to specify what should be used as <prefix>__. Sane default
seems to be the object name derived from filename, but it's possible
to override this. To allow end-users customize we can extend bpftool
to allow users to specify this. One way I was thinking would be
something like

bpftool gen object my_obj1.o=my_prefix1 my_obj2.o=my_prefix2

If user doesn't want prefixing (e.g., when linking multi-file BPF
library into a single .o) they would be able to disable this as:

bpftool gen object lib_file1.o= lib_file2.o= and so on

> - another option is to fail skeleton gen if names conflict.
> This way the users wold be able to link just fine and traditonal C style
> linker behavior will be preserved, but if the user wants a skeleton
> then the static map names across .bpf.c files shouldn't conflict.
> imo that's reasonable restriction.

There are two reasons to use static:
1. hide it from BPF code in other files (compilation units)
2. allow name conflicts (i.e., not care about anyone else accidentally
defining static variable with the same name)

I think both are important and I wouldn't want to give up #2. It
basically says: "no other file should interfere with my state neither
through naming or hijacking my state". Obviously it's impossible to
guard from user-space interference due to how BPF maps/progs are
visible to user-space, so those guarantees are mostly about BPF code
side.

Name prefixing only affects BPF skeleton generation and user-space use
of those static variables, both of which are highly-specific use
patterns "bridging two worlds", BPF and user-space. So I think it's
totally reasonable to specify that such variables will have naming
prefixes. Especially that BPF static variables inside functions
already use similar naming conventions and are similarly exposed in
BPF skeleton.

> - maybe adopt __hidden for vars and maps? Only not hidden (which is default now)
> would be seen in skeleton?

This is similar to the above, it gives up the ability to not care
about naming so much, because everything is forced to be global.

  reply	other threads:[~2021-04-26 23:11 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-23 18:53 [PATCH v2 bpf-next 0/6] BPF static linker: support static vars and maps Andrii Nakryiko
2021-04-23 18:53 ` [PATCH v2 bpf-next 1/6] bpftool: strip const/volatile/restrict modifiers from .bss and .data vars Andrii Nakryiko
2021-04-23 20:02   ` Yonghong Song
2021-04-23 18:53 ` [PATCH v2 bpf-next 2/6] libbpf: rename static variables during linking Andrii Nakryiko
2021-04-23 20:24   ` Yonghong Song
2021-04-23 21:38     ` Andrii Nakryiko
2021-04-23 21:56       ` Yonghong Song
2021-04-23 23:05         ` Alexei Starovoitov
2021-04-23 23:26           ` Yonghong Song
2021-04-23 23:35             ` Alexei Starovoitov
2021-04-23 23:35           ` Andrii Nakryiko
2021-04-23 23:48             ` Alexei Starovoitov
2021-04-24  0:13               ` Andrii Nakryiko
2021-04-24  0:22                 ` Alexei Starovoitov
2021-04-26 15:44                   ` Andrii Nakryiko
2021-04-26 22:34                     ` Alexei Starovoitov
2021-04-26 23:11                       ` Andrii Nakryiko [this message]
2021-04-27  2:22                         ` Alexei Starovoitov
2021-04-27 21:27                           ` Andrii Nakryiko
2021-04-28  4:55                             ` Alexei Starovoitov
2021-04-28 19:33                               ` Andrii Nakryiko
2021-05-04  4:42                                 ` bpf libraries and static variables. Was: " Alexei Starovoitov
2021-05-05  5:22                                   ` Alexei Starovoitov
2021-05-06 22:54                                     ` Daniel Borkmann
2021-05-11 17:57                                       ` Andrii Nakryiko
2021-05-11 18:05                                         ` Andrii Nakryiko
2021-05-11 14:20                                     ` Lorenz Bauer
2021-05-11 18:04                                       ` Andrii Nakryiko
2021-05-11 18:59                                     ` Andrii Nakryiko
2021-05-11 23:05                                       ` Alexei Starovoitov
2021-05-12 13:40                                         ` Lorenz Bauer
2021-05-12 18:50                                         ` Andrii Nakryiko
2021-05-12 23:39                                           ` Alexei Starovoitov
2021-05-13  8:37                                           ` Lorenz Bauer
2021-05-13 15:41                                             ` Alexei Starovoitov
2021-04-24  2:36                 ` Yonghong Song
2021-04-26 15:45                   ` Andrii Nakryiko
2021-04-26 16:34                     ` Yonghong Song
2021-04-23 18:53 ` [PATCH v2 bpf-next 3/6] libbpf: support static map definitions Andrii Nakryiko
2021-04-23 20:25   ` Yonghong Song
2021-04-23 18:53 ` [PATCH v2 bpf-next 4/6] bpftool: handle transformed static map names in BPF skeleton Andrii Nakryiko
2021-04-23 22:59   ` Yonghong Song
2021-04-23 18:53 ` [PATCH v2 bpf-next 5/6] selftests/bpf: extend linked_vars selftests with static variables Andrii Nakryiko
2021-04-23 23:03   ` Yonghong Song
2021-04-23 23:03   ` Yonghong Song
2021-04-23 18:53 ` [PATCH v2 bpf-next 6/6] selftests/bpf: extend linked_maps selftests with static maps Andrii Nakryiko
2021-04-23 23:11   ` Yonghong Song

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=CAEf4BzYZX9YJcoragK20cvQvr_tPTWYBQSRh7diKc1KoCtu4Dg@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=yhs@fb.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).