linux-stable/tools/lib/bpf
Andrii Nakryiko 2e7ba4f8fd libbpf: Recognize __arena global variables.
LLVM automatically places __arena variables into ".arena.1" ELF section.
In order to use such global variables bpf program must include definition
of arena map in ".maps" section, like:
struct {
       __uint(type, BPF_MAP_TYPE_ARENA);
       __uint(map_flags, BPF_F_MMAPABLE);
       __uint(max_entries, 1000);         /* number of pages */
       __ulong(map_extra, 2ull << 44);    /* start of mmap() region */
} arena SEC(".maps");

libbpf recognizes both uses of arena and creates single `struct bpf_map *`
instance in libbpf APIs.
".arena.1" ELF section data is used as initial data image, which is exposed
through skeleton and bpf_map__initial_value() to the user, if they need to tune
it before the load phase. During load phase, this initial image is copied over
into mmap()'ed region corresponding to arena, and discarded.

Few small checks here and there had to be added to make sure this
approach works with bpf_map__initial_value(), mostly due to hard-coded
assumption that map->mmaped is set up with mmap() syscall and should be
munmap()'ed. For arena, .arena.1 can be (much) smaller than maximum
arena size, so this smaller data size has to be tracked separately.
Given it is enforced that there is only one arena for entire bpf_object
instance, we just keep it in a separate field. This can be generalized
if necessary later.

All global variables from ".arena.1" section are accessible from user space
via skel->arena->name_of_var.

For bss/data/rodata the skeleton/libbpf perform the following sequence:
1. addr = mmap(MAP_ANONYMOUS)
2. user space optionally modifies global vars
3. map_fd = bpf_create_map()
4. bpf_update_map_elem(map_fd, addr) // to store values into the kernel
5. mmap(addr, MAP_FIXED, map_fd)
after step 5 user spaces see the values it wrote at step 2 at the same addresses

arena doesn't support update_map_elem. Hence skeleton/libbpf do:
1. addr = malloc(sizeof SEC ".arena.1")
2. user space optionally modifies global vars
3. map_fd = bpf_create_map(MAP_TYPE_ARENA)
4. real_addr = mmap(map->map_extra, MAP_SHARED | MAP_FIXED, map_fd)
5. memcpy(real_addr, addr) // this will fault-in and allocate pages

At the end look and feel of global data vs __arena global data is the same from
bpf prog pov.

Another complication is:
struct {
  __uint(type, BPF_MAP_TYPE_ARENA);
} arena SEC(".maps");

int __arena foo;
int bar;

  ptr1 = &foo;   // relocation against ".arena.1" section
  ptr2 = &arena; // relocation against ".maps" section
  ptr3 = &bar;   // relocation against ".bss" section

Fo the kernel ptr1 and ptr2 has point to the same arena's map_fd
while ptr3 points to a different global array's map_fd.
For the verifier:
ptr1->type == unknown_scalar
ptr2->type == const_ptr_to_map
ptr3->type == ptr_to_map_value

After verification, from JIT pov all 3 ptr-s are normal ld_imm64 insns.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20240308010812.89848-11-alexei.starovoitov@gmail.com
2024-03-11 15:43:35 -07:00
..
.gitignore libbpf: Make libbpf_version.h non-auto-generated 2021-09-13 15:36:47 -07:00
Build libbpf: Move feature detection code into its own file 2024-01-24 16:21:02 -08:00
Makefile libbpf: fix typos in Makefile 2023-08-02 13:58:51 -07:00
bpf.c libbpf: Wire up token_fd into feature probing logic 2024-01-24 16:21:02 -08:00
bpf.h bpf: Clarify batch lookup/lookup_and_delete semantics 2024-02-22 10:24:38 -08:00
bpf_core_read.h libbpf: Add support to GCC in CORE macro definitions 2024-02-13 11:28:12 -08:00
bpf_endian.h
bpf_gen_internal.h libbpf: Support kfunc detection in light skeleton. 2023-03-22 09:31:05 -07:00
bpf_helpers.h libbpf: Add __arg_arena to bpf_helpers.h 2024-03-11 15:37:24 -07:00
bpf_prog_linfo.c libbpf: Streamline error reporting for high-level APIs 2021-05-25 17:32:35 -07:00
bpf_tracing.h libbpf: Fix syscall access arguments on riscv 2023-10-04 13:19:13 -07:00
btf.c libbpf: Correct debug message in btf__load_vmlinux_btf 2024-03-04 14:33:51 +01:00
btf.h libbpf: Don't require full struct enum64 in UAPI headers 2022-09-27 20:45:17 +02:00
btf_dump.c libbpf: btf_dump_type_data_check_overflow needs to consider BTF_MEMBER_BITFIELD_SIZE 2023-05-01 15:37:38 +02:00
elf.c libbpf: Move feature detection code into its own file 2024-01-24 16:21:02 -08:00
features.c libbpf: Rewrite btf datasec names starting from '?' 2024-03-06 15:18:16 -08:00
gen_loader.c libbpf: Store zero fd to fd_array for loader kfunc relocation 2023-05-16 22:09:23 -07:00
hashmap.c libbpf: Hashmap interface update to allow both long and void* keys/values 2022-11-09 20:45:14 -08:00
hashmap.h libbpf: Remove HASHMAP_INIT static initialization helper 2023-07-11 09:40:05 -07:00
libbpf.c libbpf: Recognize __arena global variables. 2024-03-11 15:43:35 -07:00
libbpf.h libbpf: Recognize __arena global variables. 2024-03-11 15:43:35 -07:00
libbpf.map libbpf: Add missed btf_ext__raw_data() API 2024-02-01 22:16:12 +01:00
libbpf.pc.template
libbpf_common.h libbpf: Fix potential uninitialized tail padding with LIBBPF_OPTS_RESET 2023-11-09 19:07:51 -08:00
libbpf_errno.c libbpf: Optimized return value in libbpf_strerror when errno is libbpf errno 2022-12-14 18:39:33 +01:00
libbpf_internal.h libbpf: Rewrite btf datasec names starting from '?' 2024-03-06 15:18:16 -08:00
libbpf_legacy.h libbpf: Clean up deprecated and legacy aliases 2022-08-17 22:42:56 +02:00
libbpf_probes.c libbpf: Add support for bpf_arena. 2024-03-11 15:37:24 -07:00
libbpf_version.h libbpf: Start v1.4 development cycle 2023-11-23 22:49:41 +01:00
linker.c libbpf: Add missed btf_ext__raw_data() API 2024-02-01 22:16:12 +01:00
netlink.c libbpf: Use OPTS_SET() macro in bpf_xdp_query() 2024-02-06 09:51:26 -08:00
nlattr.c libbpf: Fix alen calculation in libbpf_nla_dump_errormsg() 2023-02-10 15:27:22 -08:00
nlattr.h libbpf: add API to get XDP/XSK supported features 2023-02-02 20:48:24 -08:00
relo_core.c libbpf: fix signedness determination in CO-RE relo handling logic 2023-08-23 21:13:48 -07:00
relo_core.h bpf, libbpf: Add type match support 2022-07-05 21:14:25 -07:00
ringbuf.c libbpf: Add ring__consume 2023-09-25 16:22:43 -07:00
skel_internal.h libbpf: add map_get_fd_by_id and map_delete_elem in light skeleton 2022-08-25 18:52:29 -07:00
str_error.c
str_error.h libbpf: Move feature detection code into its own file 2024-01-24 16:21:02 -08:00
strset.c libbpf: Hashmap interface update to allow both long and void* keys/values 2022-11-09 20:45:14 -08:00
strset.h libbpf: Extract internal set-of-strings datastructure APIs 2021-03-18 16:14:22 -07:00
usdt.bpf.h libbpf: Use local includes inside the library 2023-08-04 15:06:46 -07:00
usdt.c libbpf: Add uprobe multi link support to bpf_program__attach_usdt 2023-08-21 15:51:26 -07:00
zip.c libbpf: Ignore warnings about "inefficient alignment" 2023-03-16 18:20:08 +01:00
zip.h libbpf: Implement basic zip archive parsing support 2023-03-01 16:05:34 -08:00