Skip to content

Add percpu variable support for bpf arena

Xu Kuohai bpf lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest16 patches 3 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/16libbpf: Collect arena percpu data sectiontools/lib/bpf/libbpf.c+21 -10 replies
2/16libbpf: Create arena percpu chunk from the arena percpu ELF sectiontools/lib/bpf/libbpf.c tools/lib/bpf/libbpf.h+79 -30 replies
3/16libbpf: Place the percpu chunks in the bpf arenatools/lib/bpf/libbpf.c+7 -20 replies
4/16libbpf: Copy the percpu chunks into the bpf arenatools/lib/bpf/libbpf.c+46 -00 replies
5/16libbpf: Add the arena percpu chunk attribute to map creation optionstools/include/uapi/linux/bpf.h tools/lib/bpf/bpf.c tools/lib/bpf/bpf.h +1 more+17 -10 replies
6/16libbpf: Relocate arena percpu access instructionsinclude/uapi/linux/bpf.h tools/include/uapi/linux/bpf.h tools/lib/bpf/libbpf.c+60 -10 replies
7/16libbpf: Add arena percpu feature probetools/lib/bpf/features.c tools/lib/bpf/libbpf.c tools/lib/bpf/libbpf_internal.h+58 -00 replies
8/16bpf: Extend syscall to accept arena percpu informationinclude/uapi/linux/bpf.h kernel/bpf/arena.c kernel/bpf/syscall.c+23 -10 replies
9/16bpf: Teach verifier about arena percpu instructionsinclude/linux/bpf.h include/linux/filter.h kernel/bpf/arena.c +2 more+113 -50 replies
10/16bpf, x86: Add support for arena percpuarch/x86/net/bpf_jit_comp.c+33 -00 replies
11/16bpf, arm64: Add support for arena percpuarch/arm64/net/bpf_jit_comp.c include/linux/bpf.h kernel/bpf/arena.c+43 -120 replies
12/16libbpf: Introduce arena percpu helpers for user spacetools/lib/bpf/libbpf.c tools/lib/bpf/libbpf.h tools/lib/bpf/libbpf.map+77 -00 replies
13/16bpftool: Emit the arena percpu datasec into skeletontools/bpf/bpftool/gen.c+64 -50 replies
14/16selftests/bpf: Add test for arena percpu-only objecttools/testing/selftests/bpf/libarena/include/bpf_arena_common.h tools/testing/selftests/bpf/prog_tests/arena_percpu.c tools/testing/selftests/bpf/progs/arena_percpu_only.c+171 -00 replies
15/16selftests/bpf: Add test for mixed arena percpu objecttools/testing/selftests/bpf/prog_tests/arena_percpu.c tools/testing/selftests/bpf/progs/arena_percpu_mixed.c+125 -00 replies
16/16selftests/bpf: Add test for arena percpu boundstools/testing/selftests/bpf/prog_tests/arena_percpu.c+117 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

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