mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH bpf-next 00/16] Add percpu variable support for bpf arena
@ 2026-10-20 13:22 Xu Kuohai
  2026-10-20 13:22 ` [PATCH bpf-next 01/16] libbpf: Collect arena percpu data section Xu Kuohai
                   ` (16 more replies)
  0 siblings, 17 replies; 30+ messages in thread
From: Xu Kuohai @ 2026-10-20 13:22 UTC (permalink / raw)
  To: bpf, linux-kernel
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Emil Tsalapatis,
	Yonghong Song

From: Xu Kuohai <xukuohai@huawei.com>

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/20260813152324.97937-1-leon.hwang@linux.dev/

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

-- 
2.47.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2026-10-20 14:48 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20 13:22 [PATCH bpf-next 00/16] Add percpu variable support for bpf arena Xu Kuohai
2026-10-20 13:22 ` [PATCH bpf-next 01/16] libbpf: Collect arena percpu data section Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 02/16] libbpf: Create arena percpu chunk from the arena percpu ELF section Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 03/16] libbpf: Place the percpu chunks in the bpf arena Xu Kuohai
2026-10-20 13:22 ` [PATCH bpf-next 04/16] libbpf: Copy the percpu chunks into " Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 05/16] libbpf: Add the arena percpu chunk attribute to map creation options Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 06/16] libbpf: Relocate arena percpu access instructions Xu Kuohai
2026-10-20 14:29   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 07/16] libbpf: Add arena percpu feature probe Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 08/16] bpf: Extend syscall to accept arena percpu information Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 09/16] bpf: Teach verifier about arena percpu instructions Xu Kuohai
2026-10-20 14:29   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 10/16] bpf, x86: Add support for arena percpu Xu Kuohai
2026-10-20 13:22 ` [PATCH bpf-next 11/16] bpf, arm64: " Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 12/16] libbpf: Introduce arena percpu helpers for user space Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:22 ` [PATCH bpf-next 13/16] bpftool: Emit the arena percpu datasec into skeleton Xu Kuohai
2026-10-20 13:22 ` [PATCH bpf-next 14/16] selftests/bpf: Add test for arena percpu-only object Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 13:23 ` [PATCH bpf-next 15/16] selftests/bpf: Add test for mixed arena percpu object Xu Kuohai
2026-10-20 13:23 ` [PATCH bpf-next 16/16] selftests/bpf: Add test for arena percpu bounds Xu Kuohai
2026-10-20 14:17   ` bot+bpf-ci
2026-10-20 14:47 ` [PATCH bpf-next 00/16] Add percpu variable support for bpf arena Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®