Add percpu variable support for bpf arena
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest16 patches 3 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/16 | libbpf: Collect arena percpu data sectiontools/ | +21 | 0 replies | |
| 2/16 | libbpf: Create arena percpu chunk from the arena percpu ELF sectiontools/ tools/ | +79 | 0 replies | |
| 3/16 | libbpf: Place the percpu chunks in the bpf arenatools/ | +7 | 0 replies | |
| 4/16 | libbpf: Copy the percpu chunks into the bpf arenatools/ | +46 | 0 replies | |
| 5/16 | libbpf: Add the arena percpu chunk attribute to map creation optionstools/ tools/ tools/ +1 more | +17 | 0 replies | |
| 6/16 | libbpf: Relocate arena percpu access instructionsinclude/ tools/ tools/ | +60 | 0 replies | |
| 7/16 | libbpf: Add arena percpu feature probetools/ tools/ tools/ | +58 | 0 replies | |
| 8/16 | bpf: Extend syscall to accept arena percpu informationinclude/ kernel/ kernel/ | +23 | 0 replies | |
| 9/16 | bpf: Teach verifier about arena percpu instructionsinclude/ include/ kernel/ +2 more | +113 | 0 replies | |
| 10/16 | bpf, x86: Add support for arena percpuarch/ | +33 | 0 replies | |
| 11/16 | bpf, arm64: Add support for arena percpuarch/ include/ kernel/ | +43 | 0 replies | |
| 12/16 | libbpf: Introduce arena percpu helpers for user spacetools/ tools/ tools/ | +77 | 0 replies | |
| 13/16 | bpftool: Emit the arena percpu datasec into skeletontools/ | +64 | 0 replies | |
| 14/16 | selftests/bpf: Add test for arena percpu-only objecttools/ tools/ tools/ | +171 | 0 replies | |
| 15/16 | selftests/bpf: Add test for mixed arena percpu objecttools/ tools/ | +125 | 0 replies | |
| 16/16 | selftests/bpf: Add test for arena percpu boundstools/ | +117 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH bpf-next 00/16] Add percpu variable support for bpf arena
From: Xu Kuohai <[email protected]>
To use percpu data in a bpf arena today, we need to define an array sized to the maximum possible cpu number, like the cpu_ctxs array defined in tools/sched_ext/scx_qmap.h. This is not a convenient pattern. It lacks performance and scalability, and wastes memory.
Inspired by [1], this series introduces percpu variables for the arena. The basic idea is to use a separate ELF section to collect the percpu variables declared in the program. At load time, a separate chunk is created for each cpu, and the initial data from the ELF section is copied to each cpu's chunk. Every chunk is page aligned, and is populated on the target cpu's NUMA node at creation time.
To declare a percpu variable in the arena, a bpf prog declares the variable with the __arena_percpu macro:
__u64 __arena_percpu hits;
The macro tells the compiler to collect the variable into the dedicated ELF section. The variable itself is read or written by the bpf prog as a normal variable. At run time, its address is mapped by the JIT to the address of the replica on the running cpu.
User space sees the same data through its arena mapping and can address any cpu's copy directly:
total += *bpf_map__arena_percpu_ptr(arena, &skel->arena_percpu->hits, cpu);
Cross cpu access is only allowed for user space. A bpf prog can only read or write the replica of the running cpu. The other replicas are not visible to the bpf prog.
# arena layout
When the global arena data sits at the end of the arena, the percpu chunks are placed right before it:
+------------+---------+---------+----+---------+--------+ | | percpu | percpu |... | percpu | global | | | chunk 0 | chunk 1 | | chunk N | data | +------------+---------+---------+----+---------+--------+
Otherwise, the global data stays at the start of the arena, and the percpu chunks are placed right after it:
+---------+---------+---------+----+---------+------------+ | global | percpu | percpu |... | percpu | | | data | chunk 0 | chunk 1 | | chunk N | | +---------+---------+---------+----+---------+------------+
Any kernel that supports arena percpu variables is new enough to accept these offsets, so in practice the first layout is the one in use.
A chunk is created for every possible cpu, so N + 1 is the number of possible cpus on the system.
# pseudo instruction
A pseudo instruction is introduced for arena percpu variables. The normal ld_imm64 instruction that references an arena percpu variable is rewritten to the pseudo instruction by libbpf at relocation time. The pseudo instruction carries the symbol offset in the arena, and the verifier checks the offset to make sure it is inside cpu 0's chunk. The pseudo instruction is JITed to compute the actual address in the running cpu's replica, using:
dst_reg = address in chunk 0 + chunk_size * smp_processor_id()
[1] https://lore.kernel.org/bpf/[email protected]/
Xu Kuohai (16):
libbpf: Collect arena percpu data section
libbpf: Create arena percpu chunk from the arena percpu ELF section
libbpf: Place the percpu chunks in the bpf arena
libbpf: Copy the percpu chunks into the bpf arena
libbpf: Add the arena percpu chunk attribute to map creation options
libbpf: Relocate arena percpu access instructions
libbpf: Add arena percpu feature probe
bpf: Extend syscall to accept arena percpu information
bpf: Teach verifier about arena percpu instructions
bpf, x86: Add support for arena percpu
bpf, arm64: Add support for arena percpu
libbpf: Introduce arena percpu helpers for user space
bpftool: Emit the arena percpu datasec into skeleton selftests/bpf: Add test for arena percpu-only object selftests/bpf: Add test for mixed arena percpu object selftests/bpf: Add test for arena percpu bounds
arch/arm64/net/bpf_jit_comp.c | 51 ++- arch/x86/net/bpf_jit_comp.c | 33 ++ include/linux/bpf.h | 5 +- include/linux/filter.h | 1 + include/uapi/linux/bpf.h | 17 + kernel/bpf/arena.c | 43 ++- kernel/bpf/core.c | 22 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 69 +++- tools/bpf/bpftool/gen.c | 69 +++- tools/include/uapi/linux/bpf.h | 17 + tools/lib/bpf/bpf.c | 4 +- tools/lib/bpf/bpf.h | 4 + tools/lib/bpf/features.c | 50 +++ tools/lib/bpf/libbpf.c | 248 +++++++++++- tools/lib/bpf/libbpf.h | 33 ++ tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_internal.h | 2 + .../bpf/libarena/include/bpf_arena_common.h | 2 + .../selftests/bpf/prog_tests/arena_percpu.c | 361 ++++++++++++++++++ .../selftests/bpf/progs/arena_percpu_mixed.c | 29 ++ .../selftests/bpf/progs/arena_percpu_only.c | 21 + 22 files changed, 1054 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_percpu.c create mode 100644 tools/testing/selftests/bpf/progs/arena_percpu_mixed.c create mode 100644 tools/testing/selftests/bpf/progs/arena_percpu_only.c
Signature · 1 line
2.47.3