Skip to content

libbpf: support custom .rodata.*/.data.* sections

Andrii Nakryiko bpf 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest10 patches 0 repliesNew

Changes in v2 from the cover letter

- don't prepend object name for new dot maps;
- add __read_mostly example in selftests (Daniel).

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/10libbpf: deprecate btf__finalize_data() and move it into libbpf.ctools/lib/bpf/btf.c tools/lib/bpf/btf.h tools/lib/bpf/libbpf.c +1 more+102 -1020 replies
2/10libbpf: extract ELF processing state into separate structtools/lib/bpf/libbpf.c+34 -360 replies
3/10libbpf: use Elf64-specific types explicitly for dealing with ELFtools/lib/bpf/libbpf.c tools/lib/bpf/libbpf_internal.h tools/lib/bpf/linker.c+196 -1620 replies
4/10libbpf: remove assumptions about uniqueness of .rodata/.data/.bss mapstools/lib/bpf/libbpf.c+140 -1200 replies
5/10bpftool: support multiple .rodata/.data internal maps in skeletontools/bpf/bpftool/gen.c+60 -470 replies
6/10bpftool: improve skeleton generation for data maps without DATASEC typetools/bpf/bpftool/gen.c+45 -60 replies
7/10libbpf: support multiple .rodata.* and .data.* BPF mapstools/lib/bpf/libbpf.c+101 -290 replies
8/10selftests/bpf: demonstrate use of custom .rodata/.data sectionstools/testing/selftests/bpf/prog_tests/skeleton.c tools/testing/selftests/bpf/progs/test_skeleton.c+47 -00 replies
9/10libbpf: simplify look up by name of internal mapstools/lib/bpf/libbpf.c+10 -00 replies
10/10selftests/bpf: switch to ".bss"/".rodata"/".data" lookups for internal mapstools/testing/selftests/bpf/prog_tests/core_autosize.c tools/testing/selftests/bpf/prog_tests/core_reloc.c tools/testing/selftests/bpf/prog_tests/global_data.c +3 more+14 -70 replies

Reviews trailers per patch

All 10 patches have a Reviewed-by or Acked-by.

PatchSong Liu
1/10A-b
2/10A-b
3/10A-b
4/10A-b
5/10A-b
6/10A-b
7/10A-b
8/10A-b
9/10A-b
10/10A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 bpf-next 00/10] libbpf: support custom .rodata.*/.data.* sections

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/[email protected]/

v1->v2:
- don't prepend object name for new dot maps;
- add __read_mostly example in selftests (Daniel).

Andrii Nakryiko (10):
libbpf: deprecate btf__finalize_data() and move it into libbpf.c
libbpf: extract ELF processing state into separate struct
libbpf: use Elf64-specific types explicitly for dealing with ELF
libbpf: remove assumptions about uniqueness of .rodata/.data/.bss maps
bpftool: support multiple .rodata/.data internal maps in skeleton
bpftool: improve skeleton generation for data maps without DATASEC
type
libbpf: support multiple .rodata.* and .data.* BPF maps selftests/bpf: demonstrate use of custom .rodata/.data sections
libbpf: simplify look up by name of internal maps selftests/bpf: switch to ".bss"/".rodata"/".data" lookups for internal
maps

 tools/bpf/bpftool/gen.c                       | 158 ++--
 tools/lib/bpf/btf.c                           |  93 --
 tools/lib/bpf/btf.h                           |   1 +
 tools/lib/bpf/libbpf.c                        | 893 +++++++++++-------
 tools/lib/bpf/libbpf_internal.h               |   8 +-
 tools/lib/bpf/linker.c                        |   1 -
 .../selftests/bpf/prog_tests/core_autosize.c  |   2 +-
 .../selftests/bpf/prog_tests/core_reloc.c     |   2 +-
 .../selftests/bpf/prog_tests/global_data.c    |  11 +-
 .../bpf/prog_tests/global_data_init.c         |   2 +-
 .../selftests/bpf/prog_tests/kfree_skb.c      |   2 +-
 .../selftests/bpf/prog_tests/rdonly_maps.c    |   2 +-
 .../selftests/bpf/prog_tests/skeleton.c       |  29 +
 .../selftests/bpf/progs/test_skeleton.c       |  18 +
 14 files changed, 731 insertions(+), 491 deletions(-)
Signature · 1 line
2.30.2