1. Oct 26, 2021
  2. Oct 23, 2021
  3. Oct 22, 2021
    • Alexei Starovoitov's avatar
      Merge branch 'libbpf: support custom .rodata.*/.data.* sections' · 29da17c4
      Alexei Starovoitov authored
      Andrii Nakryiko says:
      
      ====================
      
      This patch set refactors internals of libbpf to enable support for multiple
      custom .rodata.* and .data.* sections. Each such section is backed by its own
      BPF_MAP_TYPE_ARRAY, memory-mappable just like .rodata/.data. This is not
      extended to .bss because .bss is not a great name, it is generated by compiler
      with name that reflects completely irrelevant historical implementation
      details. Given that users have to annotate their variables with
      SEC(".data.my_sec") explicitly, standardizing on .rodata. and .data. prefixes
      makes more sense and keeps things simpler.
      
      Additionally, this patch set makes it simpler to work with those special
      internal maps by allowing to look them up by their full ELF section name.
      
      Patch #1 is a preparatory patch that deprecates one libbpf API and moves
      custom logic into libbpf.c, where it's used. This code is later refactored
      with the rest of libbpf.c logic to support multiple data section maps.
      
      See individual patches for all the details.
      
      For new custom "dot maps", their full ELF section names are used as the names
      that are sent into the kernel. Object name isn't prepended like for
      .data/.rodata/.bss. The reason is that with longer custom names, there isn't
      much space left for object name anyways. Also, if BTF is supported,
      btf_value_type_id points to DATASEC BTF type, which contains full original ELF
      name of the section, so tools like bpftool could use that to recover full
      name. This patch set doesn't add this logic yet, this is left for follow up
      patches.
      
      One interesting possibility that is now open by these changes is that it's
      possible to do:
      
          bpf_trace_printk("My fmt %s", sizeof("My fmt %s"), "blah");
      
      and it will work as expected. I haven't updated libbpf-provided helpers in
      bpf_helpers.h for snprintf, seq_printf, and printk, because using
      `static const char ___fmt[] = fmt;` trick is still efficient and doesn't fill
      out the buffer at runtime (no copying). But we might consider updating them in
      the future, especially with the array check that Kumar proposed (see [0]).
      
        [0] https://lore.kernel.org/bpf/20211012041524.udytbr2xs5wid6x2@apollo.localdomain/
      
      
      
      v1->v2:
        - don't prepend object name for new dot maps;
        - add __read_mostly example in selftests (Daniel).
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      29da17c4
    • Andrii Nakryiko's avatar
      selftests/bpf: Switch to ".bss"/".rodata"/".data" lookups for internal maps · 4f2511e1
      Andrii Nakryiko authored
      
      
      Utilize libbpf's feature of allowing to lookup internal maps by their
      ELF section names. No need to guess or calculate the exact truncated
      prefix taken from the object name.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarSong Liu <songliubraving@fb.com>
      Link: https://lore.kernel.org/bpf/20211021014404.2635234-11-andrii@kernel.org
      4f2511e1
    • Andrii Nakryiko's avatar
      libbpf: Simplify look up by name of internal maps · 26071635
      Andrii Nakryiko authored
      Map name that's assigned to internal maps (.rodata, .data, .bss, etc)
      consist of a small prefix of bpf_object's name and ELF section name as
      a suffix. This makes it hard for users to "guess" the name to use for
      looking up by name with bpf_object__find_map_by_name() API.
      
      One proposal was to drop object name prefix from the map name and just
      use ".rodata", ".data", etc, names. One downside called out was that
      when multiple BPF applications are active on the host, it will be hard
      to distinguish between multiple instances of .rodata and know which BPF
      object (app) they belong to. Having few first characters, while quite
      limiting, still can give a bit of a clue, in general.
      
      Note, though, that btf_value_type_id for such global data maps (ARRAY)
      points to DATASEC type, which encodes full ELF name, so tools like
      bpftool can take advantage of this fact to "recover" full original name
      of the map. This is also the reason why for custom .data.* and .rodata.*
      maps libbpf uses only their ELF names and doesn't prepend object name at
      all.
      
      Another downside of such approach is that it is not backwards compatible
      and, among direct use of bpf_object__find_map_by_name() API, will break
      any BPF skeleton generated using bpftool that was compiled with older
      libbpf version.
      
      Instead of causing all this pain, libbpf will still generate map name
      using a combination of object name and ELF section name, but it will
      allow looking such maps up by their natural names, which correspond to
      their respective ELF section names. This means non-truncated ELF section
      names longer than 15 characters are going to be expected and supported.
      
      With such set up, we get the best of both worlds: leave small bits of
      a clue about BPF application that instantiated such maps, as well as
      making it easy for user apps to lookup such maps at runtime. In this
      sense it closes corresponding libbpf 1.0 issue ([0]).
      
      BPF skeletons will continue using full names for lookups.
      
        [0] Closes: https://github.com/libbpf/libbpf/issues/275
      
      
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarSong Liu <songliubraving@fb.com>
      Link: https://lore.kernel.org/bpf/20211021014404.2635234-10-andrii@kernel.org
      26071635
    • Andrii Nakryiko's avatar
      selftests/bpf: Demonstrate use of custom .rodata/.data sections · 30c5bd96
      Andrii Nakryiko authored
      
      
      Enhance existing selftests to demonstrate the use of custom
      .data/.rodata sections.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarSong Liu <songliubraving@fb.com>
      Link: https://lore.kernel.org/bpf/20211021014404.2635234-9-andrii@kernel.org
      30c5bd96
    • Andrii Nakryiko's avatar
      libbpf: Support multiple .rodata.* and .data.* BPF maps · aed65917
      Andrii Nakryiko authored
      Add support for having multiple .rodata and .data data sections ([0]).
      .rodata/.data are supported like the usual, but now also
      .rodata.<whatever> and .data.<whatever> are also supported. Each such
      section will get its own backing BPF_MAP_TYPE_ARRAY, just like
      .rodata and .data.
      
      Multiple .bss maps are not supported, as the whole '.bss' name is
      confusing and might be deprecated soon, as well as user would need to
      specify custom ELF section with SEC() attribute anyway, so might as well
      stick to just .data.* and .rodata.* convention.
      
      User-visible map name for such new maps is going to be just their ELF
      section names.
      
        [0] https://github.com/libbpf/libbpf/issues/274
      
      
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarSong Liu <songliubraving@fb.com>
      Link: https://lore.kernel.org/bpf/20211021014404.2635234-8-andrii@kernel.org
      aed65917
    • Andrii Nakryiko's avatar
      bpftool: Improve skeleton generation for data maps without DATASEC type · ef9356d3
      Andrii Nakryiko authored
      
      
      It can happen that some data sections (e.g., .rodata.cst16, containing
      compiler populated string constants) won't have a corresponding BTF
      DATASEC type. Now that libbpf supports .rodata.* and .data.* sections,
      situation like that will cause invalid BPF skeleton to be generated that
      won't compile successfully, as some parts of skeleton would assume
      memory-mapped struct definitions for each special data section.
      
      Fix this by generating empty struct definitions for such data sections.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarSong Liu <songliubraving@fb.com>
      Link: https://lore.kernel.org/bpf/20211021014404.2635234-7-andrii@kernel.org
      ef9356d3