Skip to contentSkip to conversation

bpf: A stack in arena

PATCH Applied Alexei Starovoitov 6 participants 32 messages Nov 4

Subsystems (10)
x86 Kselftest BPF arm64 +6 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover bpf: A stack in arena

From: Alexei Starovoitov <[email protected]>

The goal is to load Rust programs as rustc and LLVM compile them. The test programs are in https://github.com/4ast/rust-bpf

This is part 2 of "bpf: Stack in arena and a verifier that joins states" [1], as a series of its own. Part 1 is merged. This series does not depend on part 3, and it does not change the main pass of the verifier.

The problem
===========

A Rust reference does not say where the object is:

struct Node<'a> { val: u32, next: Option<&'a Node<'a>> }

  static IN_RODATA: Node = Node { val: 3, next: None };

  fn sum(mut node: Option<&Node>) -> u32 {
      let mut sum = 0;
      while let Some(n) = node { sum += n.val; node = n.next; }
      sum
  }

  let heap = Box::new(Node { val: 20, next: Some(&IN_RODATA) });
  let local = Node { val: 100, next: Some(&*heap) };
  sum(Some(&local));

sum() reads the three nodes with the same two insns:

     local            heap          IN_RODATA
  +---------+      +---------+      +---------+
  | next  --+----->| next  --+----->| None    |
  | 100     |      | 20      |      | 3       |
  +---------+      +---------+      +---------+
       ^                ^                ^
       +----------------+----------------+
                        |
          w0 = *(u32 *)(r1 + 8)      n.val
          r1 = *(u64 *)(r1 + 0)      n.next

These two insns must work for the three areas. Hence the decisions:

  step  memory               where  why
  ----  -------------------  -----  ---------------------------------------
  1     heap                 arena  The prog allocates it and links it with

plain pointers.
2 .data .bss .rodata arena heap.next is a load from arena, so it's
a number. A number can be an address in arena and nothing else.
3 address-taken local arena r1 is &local, then heap.next. One insn
has one form in JIT.
4 the rest of the r10 The verifier doesn't track arena, and
stack user space writes to it.

The size is the second reason. Rust progs routinely need 32 Kbyte of stack. The kernel stack cannot supply them. The private stack has one frame for each function and cpu. Thus it supports neither recursion nor a second run on the same cpu.

The patterns in real code that the tracking of the r10 stack cannot handle:

1. One insn, two kinds of memory.

sum(&local_array) r1 = fp-64 --+
+--> r2 = *(u64 *)(r1 + 0)
sum(&vec) r1 = arena --+

A copy of the function for each kind of memory does not help:

case why
--------------------------------- ----------------------------------
the pointer comes from memory no kind for the compiler, a number
for the verifier
an indirect call: &dyn Trait, fmt one function for the two kinds
if c { &local } else { &*boxed } one pointer, two kinds
n pointer arguments up to 2^n copies

For example, core::fmt::num writes the digits to a local array. It passes a part of the array as &str to Formatter::pad_integral(), which calls write_str() of a &mut dyn Write. The same write_str() gets strings from .rodata and from the heap.

2. The address of a local goes to memory. A load gives a number. The

    memory can be the heap, when the object in the heap is dropped first:

      let x = [1, 2, 3];
      let v: Vec<&i32> = x.iter().collect();

The design
==========

The series adds a second stack, in arena. A new register, r12 (BPF_REG_ARENA_SP), points to it.

kernel memory arena

  +---------------------+      +--------------------------+
  | BPF stack    <- r10 |      | heap, statics            |
  |                     |      +--------------------------+
  |  spilled registers  |      | stack in arena    <- r12 |
  |  other locals       |      |                          |
  |                     |      |  locals whose address    |
  +---------------------+      |  the prog takes          |
                               +--------------------------+
  The verifier tracks          The verifier tracks nothing.
  each slot.                   An access cannot leave arena.

- The compiler selects the stack for each local.
- Each run of a prog takes a stack at its start and gives it back at its exit. The usual case is 8 and 4 insns without a call.
- The verifier accepts r12 in four insns only: 'r12 += N' as the first insn of a function, a load, a store, and 'rX = r12'.
- x86 and arm64 JITs support r12.

The other places for an address-taken local that were tried:

  where the local is                      what breaks it
  --------------------------------------  ---------------------------------
  structs by value, more inlining         an optimization. Pattern 2 stays
  a second vm area or arena, a map value  a different base: pattern 1
  arena, a fixed frame for each function  recursion, a second run on the
  and cpu, as in the private stack        same cpu
  arena stack, the kernel supplies it     this series

The logs of patches 8, 9, 11 and 12 give the reasons for the other
choices: where the stack pointer is, how a run gets a stack, what the verifier accepts, and which x86 register r12 is.

The compiler
============

The compiler selects the stack by use, after all IR optimizations, for the full local and its full life:

rule 1 r10 The function that owns the local passes its address to
a helper or a kfunc: iterator, irq flag, dynptr, map key.
rule 2 r10 The prog stores a kernel pointer in it and loads it again.
Also a local that holds the address of such a local.
else r12

Thus open coded iterators, bpf_local_irq_save() and dynptrs work as today. When the compiler puts such a local in r12 by mistake, the prog does not
load: its address and what is loaded from it are numbers for the verifier. It is not a memory error.

  what      clang for C today     with the flag, for Rust
  --------  --------------------  -----------------------------------------
  statics   __arena variables:    statics: .addr_space.1 and
            .addr_space.1         .bss.addr_space.1
  locals    r10                   r10 or r12
  casts     by type: __arena      by origin: one cast for each pointer that
            pointers              doesn't come from the kernel
  data in .rodata                  where
  -------------------------------  -------------
  statics of Rust                  arena
  tables of functions              read-only map
  strings for helpers and kfuncs   read-only map

The price: .rodata in arena is writable, and its values are not constants for the verifier.

No released compiler emits this code. The change to the BPF backend of LLVM is in review: https://github.com/llvm/llvm-project/pull/235577

The patches
===========

patches 1-2 The default size of arena.
patches 3-4 addr_space_cast of a pointer that is not in arena. The
compiler casts a pointer when it does not know its origin.
patches 5-7 Statics: .bss.addr_space.1, and pointers that are stored
in arena.
patches 8-14 r12: the register, the stacks, the verifier, the JITs, the
tests.

Patches 8-14 do not change the code of progs without r12. A prog with r12 pays 0.3 to 2.3 nsec in each run on x86-64 (Zen 5) with hot caches. Loads, stores and calls cost the same as with r10. Patches 9-12 have the tables. Patch 3 changes the code of a prog in C only where the verifier finds a kernel pointer at a cast.

v2 -> v3:
- Rebased. The three fixes that v2 needed are merged. BPF CI could not apply v2 without them.
- Patch 11: bpf_patch_insn_data() is gone. The code that takes a stack and gives it back goes through the list of deferred patches. A call that the verifier emits is marked in the insn (BPF_EMIT_KERNEL_CALL), not in insn_aux_data.
- Patch 11: "bpf: End bpf_loop() when the time of may_goto is up" added is_called to struct bpf_subprog_info, for functions that BPF code calls and for callbacks of other helpers. A callback of another helper can take a stack, a function that BPF code calls cannot. The callbacks have a bit of their own now, is_other_cb.
- Patch 8: BPF_REG_AX is r13 also in the tests of may_goto and of the patch list that were merged after v2.
- Patch 14: three tests of may_goto in progs with r12. The test of callx is built when CAN_USE_CALLX is defined.
- Patch 4: two tests: the cast of an untrusted kernel pointer is a move.
- Patch 11: a call of the main prog is rejected, as a call of a callback that takes a stack.
- Patches 2, 7 and 14: arena_default_size, arena_data_ptrs and arena_stack are serial, as the other tests in prog_tests/arena_*.c.
- Patch 7: the test fails when .bss.addr_space.1 is not aligned.
- Corrections in the logs of patches 3, 9, 11 and 14.
- The log of patch 1 says why libbpf needs the default also when the kernel accepts 0 (Andrii, Kumar).
- The cover letter has an example for pattern 2, and it says what happens when the compiler selects r12 by mistake (Kumar).
- The code of patches 1, 3, 5, 6, 9, 10, 12 and 13 is the same.

v2: https://lore.kernel.org/bpf/[email protected]/

v1 -> v2:
- A series of its own, on bpf-next.
- The default size of arena is 4 Gbyte (Andrii, Kumar).
- The cover letter and the logs give the reasons for the design (Kumar).
- A cast that is a move has 64 bits. JITs that need explicit zero extension lost the upper half of the pointer (Ihor, CI on s390x).
- callx of a callback that takes a stack is rejected, as a call is.
- For the analysis of live stack slots the result of a cast is never a certain pointer to a slot. A store to arena counted as a write to the stack, and a prog with an access out of bounds loaded.
- The code that takes a stack is added before gen_prologue() and gen_epilogue(). A BPF qdisc without a stack skipped the two.
- BPF_LD_ABS and BPF_LD_IND are rejected in progs with r12.
- gotox to the first insn of a function does not take a stack again. A call as the first insn of a callback keeps its target.
- x86: bpf_throw() in a function with stack arguments restored the callee saved registers of the kernel from incorrect slots.
- A slot for a stack does not name pages that are freed.
- The static linker accepts pointers in arena to static variables.
- arm64 is tested in QEMU.
- bpf_alloc(), bpf_free(), bpf_arena_memcmp() and bpf_arena_memcpy() are not in the series.

[1] https://lore.kernel.org/bpf/[email protected]/

Alexei Starovoitov (14):
libbpf: Make arena 4 Gbyte when max_entries is not set selftests/bpf: Test the default size of arena
bpf: Treat addr_space_cast of a pointer that is not in arena as a move selftests/bpf: Test addr_space_cast of pointers that are not in arena
libbpf: Support .bss.addr_space.1 section
libbpf: Relocate pointers that are stored in arena selftests/bpf: Test pointers in arena and .bss.addr_space.1
bpf: Introduce bpf register BPF_REG_ARENA_SP
bpf: Add stacks for programs to arena
bpf, x86: Add BPF_ATOMIC_LOCAL
bpf: Support BPF_REG_ARENA_SP in the verifier
bpf, x86: Support BPF_REG_ARENA_SP
bpf, arm64: Support BPF_REG_ARENA_SP
selftests/bpf: Add tests for BPF_REG_ARENA_SP

 arch/arm64/net/bpf_jit_comp.c                 |   12 +
 arch/x86/net/bpf_jit_comp.c                   |  150 ++-
 include/linux/bpf.h                           |   32 +
 include/linux/bpf_verifier.h                  |   17 +-
 include/linux/filter.h                        |   72 +-
 kernel/bpf/arena.c                            |  142 ++
 kernel/bpf/backtrack.c                        |    2 +-
 kernel/bpf/const_fold.c                       |    7 +
 kernel/bpf/core.c                             |   34 +-
 kernel/bpf/disasm.c                           |    7 +
 kernel/bpf/fixups.c                           |  222 +++-
 kernel/bpf/liveness.c                         |   23 +-
 kernel/bpf/scev.c                             |    9 +-
 kernel/bpf/verifier.c                         |  248 +++-
 tools/lib/bpf/libbpf.c                        |  132 +-
 tools/lib/bpf/linker.c                        |   21 +-
 tools/testing/selftests/bpf/Makefile.skel     |    3 +-
 .../bpf/prog_tests/arena_data_ptrs.c          |   70 +
 .../bpf/prog_tests/arena_default_size.c       |   22 +
 .../selftests/bpf/prog_tests/arena_stack.c    |  323 +++++
 .../selftests/bpf/prog_tests/ctx_rewrite.c    |   14 +-
 .../selftests/bpf/prog_tests/pro_epilogue.c   |    2 +
 .../prog_tests/test_struct_ops_refcounted.c   |    2 +
 .../selftests/bpf/prog_tests/verifier.c       |    4 +
 .../selftests/bpf/progs/arena_data_ptrs.c     |  138 ++
 .../bpf/progs/arena_data_ptrs_fail.c          |   25 +
 .../selftests/bpf/progs/arena_default_size.c  |   21 +
 .../selftests/bpf/progs/arena_noarena.c       |   14 +
 .../testing/selftests/bpf/progs/arena_stack.c |  334 +++++
 .../selftests/bpf/progs/arena_stack_fentry.c  |   13 +
 .../selftests/bpf/progs/arena_stack_grow.c    |  151 +++
 .../selftests/bpf/progs/arena_stack_loop.c    |   77 ++
 .../selftests/bpf/progs/arena_stack_nomem.c   |   41 +
 .../selftests/bpf/progs/arena_stack_throw.c   |  110 ++
 .../bpf/progs/pro_epilogue_arena_stack.c      |  199 +++
 .../struct_ops_refcounted_fail__arena_stack.c |   61 +
 .../selftests/bpf/progs/verifier_arena_cast.c |  800 +++++++++++
 .../bpf/progs/verifier_arena_stack.c          | 1169 +++++++++++++++++
 .../bpf/progs/verifier_bpf_fastcall.c         |   24 +-
 .../selftests/bpf/progs/verifier_may_goto_1.c |   12 +-
 .../selftests/bpf/progs/verifier_patch_list.c |    6 +-
 .../selftests/bpf/progs/verifier_sdiv.c       |   64 +-
 .../selftests/bpf/verifier/basic_stx_ldx.c    |    4 +-
 43 files changed, 4721 insertions(+), 112 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_data_ptrs.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_default_size.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_data_ptrs.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_data_ptrs_fail.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_default_size.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_fentry.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_grow.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_loop.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_nomem.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_throw.c
 create mode 100644 tools/testing/selftests/bpf/progs/pro_epilogue_arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_cast.c
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_stack.c

base-commit: 7b8b803c3ce6e961e9c32a166c7169fcb17da6d4

Signature · 1 line
2.55.0
#1 lore raw

PATCH v3 1/14 libbpf: Make arena 4 Gbyte when max_entries is not set

From: Alexei Starovoitov <[email protected]>

max_entries of an arena is its size in pages. The kernel rejects 0. Thus each prog must select a number, and the same number is a different size on a kernel with a different page size.

A small arena is not cheaper than a large one. For each arena the kernel reserves 4 Gbyte of kernel addresses and allocates the page tables for all of them: 8 Mbyte with pages of 4 Kbyte. max_entries changes neither. After that only allocated pages use memory.

When max_entries is not set, make the arena 4 Gbyte, which is the maximum. libbpf calculates max_entries from the page size.

The default is in libbpf. There it also works on the kernels that reject 0, and it is easier to change. For example, a smaller default can be better when the kernel allocates page tables only for the pages that an arena can have. A kernel that accepts 0 is a separate change. libbpf needs the default with it too.

libbpf maps the full arena into the process that loads the object. Thus VmSize of this process grows by 4 Gbyte. If the process called mlockall(MCL_FUTURE), the kernel populates all these pages.

---
 tools/lib/bpf/libbpf.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/lib/bpf/libbpf.c +8 -0
index 02e75b183ed0..76d22a739e00 100644
@@ -552,6 +552,12 @@ struct bpf_struct_ops {
 #define STRUCT_OPS_SEC ".struct_ops"
 #define STRUCT_OPS_LINK_SEC ".struct_ops.link"
 #define ARENA_SEC ".addr_space.1"
+/*
+ * The size of the arena map when max_entries is not set: the largest one.
+ * It's in bytes: max_entries of arena is in pages and the size of a page
+ * depends on the kernel. What an empty arena costs doesn't depend on its size.
+ */
+#define ARENA_DEFAULT_SZ (1ULL << 32)
 
 enum libbpf_map_type {
 	LIBBPF_MAP_UNSPEC,
@@ -3165,6 +3171,8 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
 			return -EINVAL;
 		}
 		obj->arena_map_idx = i;
+		if (!map->def.max_entries)
+			map->def.max_entries = ARENA_DEFAULT_SZ / sysconf(_SC_PAGE_SIZE);
 
 		if (obj->efile.arena_data) {
 			err = init_arena_map_data(obj, map, obj->efile.arena_data->d_buf,
Signature · 1 line
2.55.0
#2 lore raw

PATCH v3 2/14 selftests/bpf: Test the default size of arena

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

The test declares an arena without max_entries. The kernel rejects such a map with -EINVAL, thus libbpf must set the size. See
"libbpf: Make arena 4 Gbyte when max_entries is not set".

The test makes sure that:
- After open, max_entries is 4 Gbyte divided by the page size.
- The object loads.
- The prog can allocate pages in the arena.

The test is serial, as the other tests in prog_tests/arena_*.c. See commit a14f9ba080c1 ("selftests/bpf: Run arena tests serially").

---
 .../bpf/prog_tests/arena_default_size.c       | 22 +++++++++++++++++++
 .../selftests/bpf/progs/arena_default_size.c  | 21 ++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_default_size.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_default_size.c
tools/testing/selftests/bpf/prog_tests/arena_default_size.c new file +22 -0
new file mode 100644
index 000000000000..a65b3f8baece
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <linux/sizes.h>
+#include "arena_default_size.skel.h"
+
+void serial_test_arena_default_size(void)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct arena_default_size *skel;
+
+	skel = arena_default_size__open();
+	if (!ASSERT_OK_PTR(skel, "open"))
+		return;
+	ASSERT_EQ(bpf_map__max_entries(skel->maps.arena), SZ_4G / getpagesize(), "max_entries");
+	if (!ASSERT_OK(arena_default_size__load(skel), "load"))
+		goto out;
+	ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(skel->progs.alloc), &opts), "run");
+	ASSERT_OK(opts.retval, "retval");
+out:
+	arena_default_size__destroy(skel);
+}
tools/testing/selftests/bpf/progs/arena_default_size.c new file +21 -0
new file mode 100644
index 000000000000..1fae38126094
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define BPF_NO_KFUNC_PROTOTYPES
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_experimental.h"
+#include <bpf_arena_common.h>
+
+/* max_entries is not set */
+struct {
+	__uint(type, BPF_MAP_TYPE_ARENA);
+	__uint(map_flags, BPF_F_MMAPABLE);
+} arena SEC(".maps");
+
+SEC("syscall")
+int alloc(void *ctx)
+{
+	return bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0) ? 0 : 1;
+}
+
+char _license[] SEC("license") = "GPL";
Signature · 1 line
2.55.0
#3 lore raw

PATCH v3 3/14 bpf: Treat addr_space_cast of a pointer that is not in arena as a move

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

'rX = addr_space_cast(rY, 0, 1)' makes a pointer to arena from an address in arena. The verifier gives rX the type PTR_TO_ARENA and replaces the insn with 'wX = wY', which keeps the low 32 bits. The JIT adds the base of the arena in each access. Thus an access through PTR_TO_ARENA cannot leave the arena, and the verifier does not check it.

In C the __arena attribute marks the pointers to arena, and clang casts only these pointers. Rust has no address spaces. The compiler follows each pointer back to its origin, and it does not cast a pointer that comes from the kernel. But it cannot always find the origin. For example, two progs of an object call the same function. One prog passes a ring buffer record, and the other prog passes an object in arena:

fn fill(e: &mut Event)

r6 = addr_space_cast(r1, 0, 1) ; a ring buffer record, or a number
*(u64 *)(r6 + 0) = r2

When the compiler does not know the origin, it casts the pointer. The verifier does not examine the type of r1. r6 becomes PTR_TO_ARENA in the two progs. The first prog loads, and it writes to arena instead of the record.

The verifier knows the type of rY. Let the type select what the cast does:

  rY                             the cast becomes      rX
  -----------------------------  --------------------  ------------
  number (scalar), PTR_TO_ARENA  wX = wY, as before    PTR_TO_ARENA
  all other pointers             rX = rY, or nothing   a copy of rY
                                 when X == Y

All other pointers are kernel pointers for the verifier, trusted or not. A kernel pointer is in address space 0 already. Thus a move is the correct cast. rX is a copy of rY. The verifier applies the same checks to the two
registers: bounds, NULL, references.

The verifier replaces the cast one time, for all paths. Thus it rejects a prog when it finds a number at a cast on one path and a kernel pointer at the same cast on a different path:

same addr_space_cast insn cannot be used with a number and with a pointer

NULL is a number too.

The analysis of live stack slots follows a pointer to the stack through a move. Make arg_track_xfer() follow it through the cast too. Otherwise the analysis does not see a read of the stack through rX, and the verifier forgets a slot that the prog reads. This analysis runs before the verifier knows the type of rY, and it can take a number for a pointer to the stack. Then rX points to arena. Thus rX is "the slot of rY, or not a pointer" for the analysis: an access through rX can read or write that slot, and it is never a certain write.

The cast continues to be one insn at most. A prog in C gets different code only where the verifier finds a kernel pointer at a cast.

The kernel marks the pointers to arena in its structs with btf_type_tag("arena"). gcc does not emit the tag. Then such a member is a kernel pointer for the verifier, and the cast is a move now. The members 'u64 __arena *' and 'void __arena *' of an object from bpf_obj_new() are rdonly_untrusted_mem for the verifier, and the cast is a move too.

---
 include/linux/bpf_verifier.h |  3 ++
 kernel/bpf/fixups.c          | 20 +++++++++++++
 kernel/bpf/liveness.c        | 15 ++++++++--
 kernel/bpf/verifier.c        | 54 ++++++++++++++++++++++++++++++++++--
 4 files changed, 86 insertions(+), 6 deletions(-)
include/linux/bpf_verifier.h +3 -0
index 4a514f27f239..9ae6e109d211 100644
@@ -795,6 +795,8 @@ struct bpf_insn_aux_data {
 	u64 non_sleepable:1; /* helper/kfunc may be called from non-sleepable context */
 	u64 is_iter_next:1; /* bpf_iter_<type>_next() kfunc call */
 	u64 call_with_percpu_alloc_ptr:1; /* {this,per}_cpu_ptr() with prog percpu alloc */
+	u64 cast_arena:1; /* addr_space_cast of an address in arena */
+	u64 cast_mov:1; /* addr_space_cast of a pointer that is not in arena, it's a move */
 	u64 bb_end:1; /* last instruction of a basic block */
 	u64 need_scev:1;
 	u64 alu_state:5; /* used in combination with alu_limit */
@@ -2089,6 +2091,7 @@ int bpf_optimize_bpf_loop(struct bpf_verifier_env *env);
 void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env);
 int bpf_opt_remove_dead_code(struct bpf_verifier_env *env);
 int bpf_opt_remove_nops(struct bpf_verifier_env *env);
+void bpf_lower_cast_mov(struct bpf_verifier_env *env);
 int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, const union bpf_attr *attr);
 int bpf_convert_ctx_accesses(struct bpf_verifier_env *env);
 int bpf_jit_subprogs(struct bpf_verifier_env *env);
kernel/bpf/fixups.c +20 -0
index 6b719a28be68..eb5756cbc7e5 100644
@@ -709,6 +709,26 @@ static bool insn_is_nop(struct bpf_verifier_env *env, int i, int next)
 	       !memcmp(&insn, &MAY_GOTO_0, sizeof(MAY_GOTO_0));
 }
 
+/* addr_space_cast of a pointer that is not in arena is a move, see is_cast_mov() */
+void bpf_lower_cast_mov(struct bpf_verifier_env *env)
+{
+	struct bpf_insn_aux_data *aux = env->insn_aux_data;
+	struct bpf_insn *insn = env->prog->insnsi;
+	int i;
+
+	for (i = 0; i < env->prog->len; i++, insn++) {
+		if (!aux[i].cast_mov)
+			continue;
+		/* an address in arena has 32 bits, this pointer has 64 */
+		aux[i].zext_dst = false;
+		if (insn->dst_reg == insn->src_reg)
+			/* bpf_opt_remove_nops() removes it */
+			*insn = NOP;
+		else
+			*insn = BPF_MOV64_REG(insn->dst_reg, insn->src_reg);
+	}
+}
+
 int bpf_opt_remove_nops(struct bpf_verifier_env *env)
 {
 	int insn_cnt = env->prog->len;
kernel/bpf/liveness.c +12 -3
index f51445ead384..1a4cfb575077 100644
@@ -1456,8 +1456,16 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		if (code == BPF_MOV) {
 			if (insn->off == 0) {
 				*dst = *src;
+			} else if (insn->off == BPF_ADDR_SPACE_CAST && insn->imm == 1) {
+				/*
+				 * The cast of a pointer to stack to as(0) is a move. But src
+				 * can be a stale spill, see below. Then it's a number for the
+				 * verifier, and dst points to arena. A store through dst must
+				 * not count as a certain store to a slot of the stack.
+				 */
+				*dst = __arg_track_join(*src, none);
 			} else {
-				/* addr_space_cast destroys a pointer */
+				/* the cast to as(1) and sign extension destroy a pointer */
 				*dst = none;
 			}
 		} else {
@@ -1474,8 +1482,9 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		 * Helpers, kfuncs and global subprogs can scratch stack slots. The verifier reads
 		 * a scalar from a scratched slot, but at_stack_out[slot] is not cleared, and
 		 * fill_from_stack() returns the stale spill: an FP-derived arg_track for a scalar.
-		 * A scalar can't be dereferenced, so the stale value can only set liveness bits
-		 * for stack accesses that don't happen. That is conservative.
+		 * A scalar can't be dereferenced before addr_space_cast, see above. So the stale
+		 * value can only set liveness bits for stack accesses that don't happen. That is
+		 * conservative.
 		 *
 		 * Static subprogs and callbacks can't store an FP-derived pointer into the frame of
 		 * the caller, but they can store any other pointer there. The caller can fill it
kernel/bpf/verifier.c +51 -3

72 lines not on this page · raw message

Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH bpf-next v3 03/14] bpf: Treat addr_space_cast of a pointer that is not in arena as a move

TL;DR: is_cast_mov() may treat arena pointers loaded from bpf_obj_new() objects (untrusted mem) as as(0); previously working programs may now fail to load on stores or silently read zeros on loads.…
Quoted 1 line

bpf: Treat addr_space_cast of a pointer that is not in arena as a move

TL;DR: is_cast_mov() may treat arena pointers loaded from bpf_obj_new() objects (untrusted mem) as as(0); previously working programs may now fail to load on stores or silently read zeros on loads. Commit message doesn't mention this.

Quoted 3 lines
kernel/bpf/verifier.c +0 -0

[ ... ]

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+static bool is_cast_mov(const struct bpf_insn *insn, const struct bpf_reg_state *src_reg)
+{
+	/* cast from as(1) to as(0) */
+	if (insn->imm != 1)

kernel/bpf/verifier.c int bpf_reg_union(struct bpf_verifier_env *env, struct bpf_reg_state *acc, · line 17917

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+		return false;
+
+	switch (base_type(src_reg->type)) {
+	case SCALAR_VALUE:
+	case PTR_TO_ARENA:
+		return false;
+	default:
+		return true;
+	}
+}

Is it correct for is_cast_mov() to treat every pointer type other than PTR_TO_ARENA as already being in as(0)?

A load of a non-struct pointer member (for example 'u64 __arena *data') from an object described by program BTF, such as one from bpf_obj_new(), bpf_percpu_obj_new() or a local kptr, goes through btf_struct_walk() which returns WALK_PTR_UNTRUSTED. Unlike WALK_PTR, btf_struct_access() has no type_is_alloc() exception for it:

    case WALK_PTR_UNTRUSTED:
        *flag = MEM_RDONLY | PTR_UNTRUSTED;
        ret = PTR_TO_MEM;

So after something like 'e->data = bpf_arena_alloc_pages(...)', loading e->data gives PTR_TO_MEM | MEM_RDONLY | PTR_UNTRUSTED, but the value in the register is an arena user address, since arena_alloc_pages() returns clear_lo32(arena->user_vm_start) + uaddr32. When the program dereferences it, clang emits 'rX = addr_space_cast(rY, 0, 1)'.

Before this patch rX became PTR_TO_ARENA, the access was rewritten to BPF_PROBE_MEM32, and it reached the arena page. After this patch the cast is lowered to 'rX = rY' and rX stays rdonly_untrusted_mem:

- A store through rX is now rejected at load time ("cannot write into rdonly_untrusted_mem").

- A load through rX becomes a BPF_PROBE_MEM load from the 64-bit user VA. The x86 JIT emits its "src_reg + off > TASK_SIZE_MAX + PAGE_SIZE" check for that load, so the program reads zeros instead of the arena contents, and nothing reports it.

The commit message says these members are rdonly_untrusted_mem and "the cast is a move too", but it does not say that these registers hold as(1) addresses rather than kernel addresses, or that programs which loaded and worked before now either fail to load or silently read 0.

Could btf_struct_access() return SCALAR_VALUE for WALK_PTR_UNTRUSTED when type_is_alloc(reg->type), as it already does for WALK_PTR, so that such a member takes the cast_arena path? Alternatively, should is_cast_mov() not treat untrusted memory derived from program BTF as as(0)?

The same applies to the case in the commit message of an untagged (gcc-built) kernel __arena member: there, writes would stop loading instead of reaching the arena.

No later commit in the series changes is_cast_mov() or the WALK_PTR_UNTRUSTED handling.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41755299448

#5 lore raw

Re: [PATCH bpf-next v3 03/14] bpf: Treat addr_space_cast of a pointer that is not in arena as a move

Quoted 4 lines · On Tue, Nov 03, 2026 at 06:12 PM [email protected] <[email protected]>…

The commit message says these members are rdonly_untrusted_mem and "the cast is a move too", but it does not say that these registers hold as(1) addresses rather than kernel addresses, or that programs which loaded and worked before now either fail to load or silently read 0.

It's already explained in the commit log.

Quoted 4 lines

Could btf_struct_access() return SCALAR_VALUE for WALK_PTR_UNTRUSTED when type_is_alloc(reg->type), as it already does for WALK_PTR, so that such a member takes the cast_arena path? Alternatively, should is_cast_mov() not treat untrusted memory derived from program BTF as as(0)?

No.

Quoted 3 lines

The same applies to the case in the commit message of an untagged (gcc-built) kernel __arena member: there, writes would stop loading instead of reaching the arena.

No.

#6 lore raw

PATCH v3 4/14 selftests/bpf: Test addr_space_cast of pointers that are not in arena

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

The verifier does not check an access through a pointer to arena. Thus a kernel pointer must keep its type after the cast to address space 0. See "bpf: Treat addr_space_cast of a pointer that is not in arena as a move".

  test                         what the prog does or shows           result
  ---------------------------  ------------------------------------  ------
  map_value                    the cast is 'r6 = r0'                 loads
  map_value_same_reg           the cast of r0 to r0 is removed       loads
  map_value_bounds             access beyond the map value           reject
  map_value_or_null            access without a NULL check           reject
  map_value_null_check_of_src  NULL check of r0 applies to r6        loads
  ctx                          r6 is ctx                             loads
  btf_id                       r6 is trusted_ptr_task_struct         loads
  untrusted_btf_id             r6 is untrusted_ptr_task_struct       loads
  untrusted_mem                r6 is rdonly_untrusted_mem            loads
  ref_released                 releases a reference through r1       loads
  ref_leaked                   does not release a reference          reject
  stack                        r6 is fp-8                            loads
  spill_through_cast           spills ctx through r6, fills it       loads
  callee_writes                callee writes the caller's stack      loads
  callee_reads                 callee reads the caller's stack       loads
  number                       r6 is arena, the cast is 'w6 = w6'    loads
  number_and_pointer           one cast gets a number and ctx        reject
  pointer_and_number           the same, the paths are exchanged     reject
  cast_user                    cast of ctx to as(1) gives a scalar   loads

The progs that return the content of a map value or of a stack slot also run. The result shows that they read the object and not arena. callee_reads needs the change in arg_track_xfer().

The analysis of live stack slots can take a number for a pointer to the
stack: a slot held such a pointer, and a helper or a short store overwrote it. The cast of this number gives a pointer to arena. A store through it must not count as a write to the stack:

  test                             what the prog does or shows       result
  -------------------------------  --------------------------------  ------
  stale_spill_store                the store hides a write to the    reject
                                   counter of a loop
  stale_spill_store_r10            the same, the stale value is r10  reject
  stale_spill_live_slot            a slot is read after the store    loads
  stale_spill_live_slot_of_caller  the same, a slot of the caller    loads
  store_over_spilled_r10           a pointer to a map value is       reject
                                   stored over r10 through the cast
  r10_stored_to_arena              r10 is stored to arena            reject
  callee_writes_in_loop            the loop stays counted when the   loads

callee writes through the cast

---
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../selftests/bpf/progs/verifier_arena_cast.c | 800 ++++++++++++++++++
 2 files changed, 802 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_cast.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 99f0551c8b59..3d0eda3b4da4 100644
@@ -12,6 +12,7 @@
 #include "verifier_align.skel.h"
 #include "verifier_and.skel.h"
 #include "verifier_arena.skel.h"
+#include "verifier_arena_cast.skel.h"
 #include "verifier_arena_large.skel.h"
 #include "verifier_arena_globals1.skel.h"
 #include "verifier_arena_globals2.skel.h"
@@ -207,6 +208,7 @@ void test_verifier_aggregate_ret(void)        { RUN_TESTS(verifier_aggregate_ret
 void test_verifier_align(void)                { RUN(verifier_align); }
 void test_verifier_and(void)                  { RUN(verifier_and); }
 void test_verifier_arena(void)                { RUN(verifier_arena); }
+void test_verifier_arena_cast(void)           { RUN(verifier_arena_cast); }
 void test_verifier_arena_large(void)          { RUN(verifier_arena_large); }
 void test_verifier_arena_globals1(void)       { RUN(verifier_arena_globals1); }
 void test_verifier_arena_globals2(void)       { RUN(verifier_arena_globals2); }
tools/testing/selftests/bpf/progs/verifier_arena_cast.c new file +800 -0

800 lines not on this page · raw message

Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH bpf-next v3 04/14] selftests/bpf: Test addr_space_cast of pointers that are not in arena

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/bpf: wrong stack offset in r10_stored_to_arena() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: wrong stack offset in r10_stored_to_arena()
--

commit 54fbcf6815ff6f19ca4963d449d29e574fbc1941
Author: Alexei Starovoitov <[email protected]>

selftests/bpf: Test addr_space_cast of pointers that are not in arena

This commit adds BPF verifier selftests in verifier_arena_cast.c to test addr_space_cast behavior on non-arena pointers, numbers, and stack slot liveness tracking across casts.

Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_arena_cast.c new file +0 -0
new file mode 100644
index 0000000000000..14a199318d0d5
@@ -0,0 +1,800 @@

[ ... ]

Quoted 8 lines
tools/testing/selftests/bpf/progs/verifier_arena_cast.c new file +8 -0
+SEC("syscall")
+__description("arena cast: r10 is stored to arena, a pointer to map value is in the stack")
+__failure __msg("invalid access to map value, value_size=8 off=4096 size=1")
+__naked void r10_stored_to_arena(void)
+{
+	asm volatile (
+	"r1 = %[arena] ll;"
+	UPDATE_AND_LOOKUP

tools/testing/selftests/bpf/progs/verifier_arena_cast.c line 682

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_arena_cast.c new file +10 -0
+	"if r0 == 0 goto 2f;"
+	"r9 = r0;"
+	STALE_SPILL_R7(-8)
+	".8byte %[cast_r7_r7];"
+	"r1 = r9;"
+	"r1 += 8;"
+	"*(u64 *)(r10 - 32) = r1;"
+	/* to arena, not to fp-32 */
+	"*(u64 *)(r7 + 0) = r10;"
+	"r7 = *(u64 *)(r10 - 32);"

[Severity: Medium]
In r10_stored_to_arena() in tools/testing/selftests/bpf/progs/verifier_arena_cast.c, should STALE_SPILL_R7(-8) be STALE_SPILL_R7(-32) instead?

As written, STALE_SPILL_R7(-8) gives r7 a stale stack offset of fp-8 rather than fp-32. When *(u64 *)(r7 + 0) = r10 executes, static stack analysis tracks the store against slot fp-8 (which is immediately overwritten by SPILL_FP24_TO_FP8) and never touches slot fp-32, so the test does not exercise joining the map value pointer in fp-32 (ARG_NONE) with r10 (fp+0).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#8 lore raw

PATCH v3 5/14 libbpf: Support .bss.addr_space.1 section

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Global variables in arena are in the section .addr_space.1. Its type is SHT_PROGBITS. Thus the variables that are zero use space in the object file too.

Support .bss.addr_space.1 as the .bss of arena. The name starts with .bss. Thus compilers make the section SHT_NOBITS, and it has no bytes in the file:

  long __arena big[8192] SEC(".bss.addr_space.1");

An object with 8 Mbyte of zeros shrinks from 8410296 to 21696 bytes.

A declaration in a different file needs the same section:

  extern long __arena big[8192] SEC(".bss.addr_space.1");

Without SEC() the compiler puts the declaration in .addr_space.1. The static linker took the index of the variable in one section for an index in the other section, and it crashed or wrote incorrect BTF. Make it report a section mismatch, as it does when the definition comes first.

libbpf puts the new section after .addr_space.1 in arena and fills it with zeros. It adds the offset of the section to the relocations that refer to it.

Only the file becomes smaller. At load the new variables get pages in arena, as the variables of .addr_space.1 do. The layout of skel->arena does not change, and the new variables are not in it.

---
 tools/lib/bpf/libbpf.c | 41 ++++++++++++++++++++++++++++++++---------
 tools/lib/bpf/linker.c | 11 +++++++++++
 2 files changed, 43 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.c +32 -9
index 76d22a739e00..a7c53dc5b91d 100644
@@ -552,6 +552,11 @@ struct bpf_struct_ops {
 #define STRUCT_OPS_SEC ".struct_ops"
 #define STRUCT_OPS_LINK_SEC ".struct_ops.link"
 #define ARENA_SEC ".addr_space.1"
+/*
+ * Variables in arena that are zero. The name starts with .bss, so compilers make
+ * it SHT_NOBITS and it takes no space in the file. It's placed after ARENA_SEC.
+ */
+#define ARENA_BSS_SEC ".bss.addr_space.1"
 /*
  * The size of the arena map when max_entries is not set: the largest one.
  * It's in bytes: max_entries of arena is in pages and the size of a page
@@ -697,6 +702,8 @@ struct elf_state {
 	Elf64_Ehdr *ehdr;
 	Elf_Data *symbols;
 	Elf_Data *arena_data;
+	Elf_Data *arena_bss;
+	size_t arena_bss_align;
 	size_t shstrndx; /* section index for section name strings */
 	size_t strtabidx;
 	struct elf_sec_desc *secs;
@@ -707,6 +714,7 @@ struct elf_state {
 	int symbols_shndx;
 	bool has_st_ops;
 	int arena_data_shndx;
+	int arena_bss_shndx;
 	int jumptables_data_shndx;
 };
 
@@ -788,6 +796,7 @@ struct bpf_object {
 	void *arena_data;
 	size_t arena_data_sz;
 	size_t arena_data_off;
+	size_t arena_bss_off; /* from the start of arena_data */
 
 	void *jumptables_data;
 	size_t jumptables_data_sz;
@@ -1629,6 +1638,7 @@ static struct bpf_object *bpf_object__new(const char *path,
 	obj->efile.obj_buf = obj_buf;
 	obj->efile.obj_buf_sz = obj_buf_sz;
 	obj->efile.btf_maps_shndx = -1;
+	obj->efile.arena_bss_shndx = -1;
 	obj->kconfig_map_idx = -1;
 	obj->arena_map_idx = -1;
 
@@ -1648,6 +1658,7 @@ static void bpf_object__elf_finish(struct bpf_object *obj)
 	obj->efile.ehdr = NULL;
 	obj->efile.symbols = NULL;
 	obj->efile.arena_data = NULL;
+	obj->efile.arena_bss = NULL;
 
 	zfree(&obj->efile.secs);
 	obj->efile.sec_cnt = 0;
@@ -3095,13 +3106,20 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,
 	return 0;
 }
 
-static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
-			       void *data, size_t data_sz)
+static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map)
 {
-	obj->arena_data = malloc(data_sz);
+	Elf_Data *data = obj->efile.arena_data, *bss = obj->efile.arena_bss;
+	size_t data_sz = data ? data->d_size : 0;
+
+	if (bss) {
+		obj->arena_bss_off = roundup(data_sz, obj->efile.arena_bss_align);
+		data_sz = obj->arena_bss_off + bss->d_size;
+	}
+	obj->arena_data = calloc(1, data_sz);
 	if (!obj->arena_data)
 		return -ENOMEM;
-	memcpy(obj->arena_data, data, data_sz);
+	if (data)
+		memcpy(obj->arena_data, data->d_buf, data->d_size);
 	obj->arena_data_sz = data_sz;
 
 	/* make bpf_map__init_value() work for ARENA maps */
@@ -3174,14 +3192,13 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
 		if (!map->def.max_entries)
 			map->def.max_entries = ARENA_DEFAULT_SZ / sysconf(_SC_PAGE_SIZE);
 
-		if (obj->efile.arena_data) {
-			err = init_arena_map_data(obj, map, obj->efile.arena_data->d_buf,
-						  obj->efile.arena_data->d_size);
+		if (obj->efile.arena_data || obj->efile.arena_bss) {
+			err = init_arena_map_data(obj, map);
 			if (err)
 				return err;
 		}
 	}
-	if (obj->efile.arena_data && obj->arena_map_idx < 0) {
+	if ((obj->efile.arena_data || obj->efile.arena_bss) && obj->arena_map_idx < 0) {
 		pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n",
 			ARENA_SEC);
 		return -ENOENT;
@@ -4176,6 +4193,10 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 			sec_desc->sec_type = SEC_RELO;
 			sec_desc->shdr = sh;
 			sec_desc->data = data;
+		} else if (sh->sh_type == SHT_NOBITS && strcmp(name, ARENA_BSS_SEC) == 0) {
+			obj->efile.arena_bss = data;
+			obj->efile.arena_bss_align = sh->sh_addralign ?: 1;
+			obj->efile.arena_bss_shndx = idx;
 		} else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 ||
 							 str_has_pfx(name, BSS_SEC "."))) {
 			sec_desc->sec_type = SEC_BSS;
@@ -4826,7 +4847,7 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
 	sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
 
 	/* arena data relocation */
-	if (shdr_idx == obj->efile.arena_data_shndx) {
+	if (shdr_idx == obj->efile.arena_data_shndx || shdr_idx == obj->efile.arena_bss_shndx) {
 		if (obj->arena_map_idx < 0) {
 			pr_warn("prog '%s': bad arena data relocation at insn %u, no arena maps defined\n",
 				prog->name, insn_idx);
@@ -4836,6 +4857,8 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
 		reloc_desc->insn_idx = insn_idx;
 		reloc_desc->map_idx = obj->arena_map_idx;
 		reloc_desc->sym_off = sym->st_value;
+		if (shdr_idx == obj->efile.arena_bss_shndx)
+			reloc_desc->sym_off += obj->arena_bss_off;
 
 		map = &obj->maps[obj->arena_map_idx];
 		pr_debug("prog '%s': found arena map %d (%s, sec %d, off %zu) for insn %u\n",
tools/lib/bpf/linker.c +11 -0
index f3f71c452f00..dba88f4fd6cd 100644
@@ -2123,6 +2123,17 @@ static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
 		if (!glob_sym->is_extern && sym_bind == STB_WEAK)
 			return 0;
 
+		/*
+		 * The symbol has an entry in the DATASEC of a different section.
+		 * var_idx is an index in that section. linker_append_btf() reports
+		 * the same when the definition is first.
+		 */
+		if (glob_sym->var_idx >= 0 && glob_sym->sec_id != dst_sec->id) {
+			pr_warn("global '%s': section mismatch %d vs %d\n",
+				sym_name, glob_sym->sec_id, dst_sec->id);
+			return -EINVAL;
+		}
+
 		/* At this point, new symbol is strong non-extern,
 		 * so overwrite glob_sym with new symbol information.
 		 * Preserve binding and visibility.
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH bpf-next v3 05/14] libbpf: Support .bss.addr_space.1 section

TL;DR: In init_arena_map_data(), unvalidated ELF sh_addralign/sh_size in the new .bss.addr_space.1 handling may wrap data_sz, so a malformed ELF could cause a heap buffer overflow in memcpy.
Quoted 1 line

libbpf: Support .bss.addr_space.1 section

TL;DR: In init_arena_map_data(), unvalidated ELF sh_addralign/sh_size in the new .bss.addr_space.1 handling may wrap data_sz, so a malformed ELF could cause a heap buffer overflow in memcpy.

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0

[ ... ]

Quoted 13 lines
tools/lib/bpf/libbpf.c +5 -3
@@ -3095,13 +3106,20 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,
 	return 0;
 }
 
-static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
-			       void *data, size_t data_sz)
+static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map)
 {
-	obj->arena_data = malloc(data_sz);
+	Elf_Data *data = obj->efile.arena_data, *bss = obj->efile.arena_bss;
+	size_t data_sz = data ? data->d_size : 0;
+
+	if (bss) {

tools/lib/bpf/libbpf.c static int bpf_object__init_user_btf_map(struct bpf_object *obj, · line 3123

Quoted 10 lines
tools/lib/bpf/libbpf.c +6 -1
+		obj->arena_bss_off = roundup(data_sz, obj->efile.arena_bss_align);
+		data_sz = obj->arena_bss_off + bss->d_size;
+	}
+	obj->arena_data = calloc(1, data_sz);
 	if (!obj->arena_data)
 		return -ENOMEM;
-	memcpy(obj->arena_data, data, data_sz);
+	if (data)
+		memcpy(obj->arena_data, data->d_buf, data->d_size);
 	obj->arena_data_sz = data_sz;

Can data_sz wrap here? Both arena_bss_align (from sh_addralign) and bss->d_size (sh_size of a SHT_NOBITS section) come straight from the ELF section header and are not validated.

For example, with data->d_size = 4096 and sh_addralign = 0xffffffffffffffff, roundup() computes (4096 + (__y - 1)) / __y * __y, which wraps to arena_bss_off = 0. With sh_size = 8, data_sz = 8.

Similarly, with a sane alignment (arena_bss_off = 4096) and sh_size = 2^64 - 4096 + 8, data_sz also wraps to 8.

In both cases calloc(1, 8) succeeds and the following memcpy(obj->arena_data, data->d_buf, data->d_size) copies 4096 bytes into an 8-byte buffer. The path is:

bpf_object__open_mem()
bpf_object_open()

    bpf_object__elf_collect()
    bpf_object__init_maps()
      bpf_object__init_user_btf_maps()
        init_arena_map_data()

The old code was malloc(d_size) followed by memcpy of d_size, so it could not overflow. libbpf has fixed other malformed-ELF arena crashes before, for example 42be23e8f2dc ("libbpf: Verify that arena map exists when adding arena relocations").

Should this reject a sh_addralign that is not a power of two, and fail when arena_bss_off < data_sz or bss->d_size > SIZE_MAX - arena_bss_off? Capping the total at the 4 GB arena maximum would also work.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41755299448

#10 lore raw

PATCH v3 6/14 libbpf: Relocate pointers that are stored in arena

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

A variable in arena that points to a different variable in arena gets an incorrect value:

  int __arena x = 42;
  int __arena * __arena px = &x;

The compiler emits an R_BPF_64_ABS64 relocation for px. libbpf ignores the relocations of .addr_space.1. The object loads without an error, and px is 0.

The address of x is not known until libbpf maps the arena. Thus libbpf does the work in two steps:
- At open it records where each such pointer is and which variable it points to.
- At load it maps the arena, copies the variables, and adds the address of the variable to each pointer.

The pointer gets the address in user space. The prog can use the same value, because an access to arena from a prog uses only the low 32 bits.

A pointer in arena to a variable that is not in arena has no correct value. Fail the open.

For a pointer to a static variable the relocation refers to the section, and the offset of the variable is stored in place. The static linker rejects such a relocation in a data section. Make it add the new offset of the section, as it does for pointers to static functions. All pointers in the arena data of Rust objects have this form.

---
 tools/lib/bpf/libbpf.c | 83 +++++++++++++++++++++++++++++++++++++++++-
 tools/lib/bpf/linker.c | 10 +++--
 2 files changed, 87 insertions(+), 6 deletions(-)
tools/lib/bpf/libbpf.c +81 -2
index a7c53dc5b91d..1172fe7992bf 100644
@@ -797,6 +797,12 @@ struct bpf_object {
 	size_t arena_data_sz;
 	size_t arena_data_off;
 	size_t arena_bss_off; /* from the start of arena_data */
+	/* pointers to variables in arena that are stored in variables in arena */
+	struct arena_ptr {
+		size_t off;	 /* where the pointer is, from the start of arena_data */
+		size_t targ_off; /* the symbol it points to, from the start of arena_data */
+	} *arena_ptrs;
+	size_t arena_ptr_cnt;
 
 	void *jumptables_data;
 	size_t jumptables_data_sz;
@@ -4171,10 +4177,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 
 			/*
 			 * Only do relo for section with exec instructions,
-			 * struct_ops, maps, and read-only data that might
-			 * have pointers to functions.
+			 * struct_ops, maps, read-only data that might
+			 * have pointers to functions, and arena.
 			 */
 			if (!section_have_execinstr(obj, targ_sec_idx) &&
+			    strcmp(name, ".rel" ARENA_SEC) &&
 			    strcmp(name, ".rel" RODATA_SEC) &&
 			    !str_has_pfx(name, ".rel" RODATA_SEC ".") &&
 			    strcmp(name, ".rel" DATA_REL_RO_SEC) &&
@@ -5430,6 +5437,21 @@ bpf_object__reuse_map(struct bpf_map *map)
 	return 0;
 }
 
+/* The arena is mapped. The address of a variable is the same for the program and for user space. */
+static void relocate_arena_ptrs(struct bpf_object *obj, const struct bpf_map *arena)
+{
+	void *data = arena->mmaped + obj->arena_data_off;
+	struct arena_ptr *p = obj->arena_ptrs;
+	__u64 val;
+	size_t i;
+
+	for (i = 0; i < obj->arena_ptr_cnt; i++, p++) {
+		memcpy(&val, data + p->off, sizeof(val));
+		val += (__u64)(unsigned long)data + p->targ_off;
+		memcpy(data + p->off, &val, sizeof(val));
+	}
+}
+
 static int
 bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
 {
@@ -5856,6 +5878,7 @@ bpf_object__create_maps(struct bpf_object *obj)
 					memcpy(map->mmaped + obj->arena_data_off, obj->arena_data,
 						obj->arena_data_sz);
 					zfree(&obj->arena_data);
+					relocate_arena_ptrs(obj, map);
 				}
 			}
 			if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
@@ -8223,6 +8246,59 @@ static int bpf_object__collect_rodata_relos(struct bpf_object *obj,
 	return 0;
 }
 
+/*
+ * Collect pointers that are stored in variables in arena, like
+ *   int __arena * __arena p = &var;
+ * They are R_BPF_64_ABS64 relocations. The addend is stored in place.
+ */
+static int bpf_object__collect_arena_relos(struct bpf_object *obj,
+					   Elf64_Shdr *shdr, Elf_Data *data)
+{
+	int i, nrels = shdr->sh_size / shdr->sh_entsize;
+	struct arena_ptr *ptrs;
+	const char *sym_name;
+	size_t sym_idx;
+	Elf64_Sym *sym;
+	Elf64_Rel *rel;
+
+	ptrs = libbpf_reallocarray(NULL, nrels, sizeof(*ptrs));
+	if (!ptrs)
+		return -ENOMEM;
+	obj->arena_ptrs = ptrs;
+
+	for (i = 0; i < nrels; i++, ptrs++) {
+		rel = elf_rel_by_idx(data, i);
+		if (!rel) {
+			pr_warn("sec '.rel%s': failed to get relo #%d\n", ARENA_SEC, i);
+			return -LIBBPF_ERRNO__FORMAT;
+		}
+
+		sym_idx = ELF64_R_SYM(rel->r_info);
+		sym = elf_sym_by_idx(obj, sym_idx);
+		if (!sym) {
+			pr_warn("sec '.rel%s': symbol #%zu not found for relo #%d\n",
+				ARENA_SEC, sym_idx, i);
+			return -LIBBPF_ERRNO__FORMAT;
+		}
+
+		if (ELF64_R_TYPE(rel->r_info) != R_BPF_64_ABS64 ||
+		    (sym->st_shndx != obj->efile.arena_data_shndx &&
+		     sym->st_shndx != obj->efile.arena_bss_shndx)) {
+			sym_name = elf_sym_str(obj, sym->st_name) ?: "<?>";
+			pr_warn("sec '%s': pointer to '%s' at offset %zu: it's not in arena\n",
+				ARENA_SEC, sym_name, (size_t)rel->r_offset);
+			return -LIBBPF_ERRNO__RELOC;
+		}
+
+		ptrs->off = rel->r_offset;
+		ptrs->targ_off = sym->st_value;
+		if (sym->st_shndx == obj->efile.arena_bss_shndx)
+			ptrs->targ_off += obj->arena_bss_off;
+		obj->arena_ptr_cnt++;
+	}
+	return 0;
+}
+
 static int cmp_func_ptrs(const void *_a, const void *_b)
 {
 	const struct func_ptr *a = _a;
@@ -8263,6 +8339,8 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
 			err = bpf_object__collect_st_ops_relos(obj, shdr, data);
 		else if (idx == obj->efile.btf_maps_shndx)
 			err = bpf_object__collect_map_relos(obj, shdr, data);
+		else if (idx == obj->efile.arena_data_shndx)
+			err = bpf_object__collect_arena_relos(obj, shdr, data);
 		else
 			err = bpf_object__collect_prog_relos(obj, shdr, data);
 		if (err)
@@ -10384,6 +10462,7 @@ void bpf_object__close(struct bpf_object *obj)
 		close(obj->token_fd);
 
 	zfree(&obj->arena_data);
+	zfree(&obj->arena_ptrs);
 
 	zfree(&obj->jumptables_data);
 	obj->jumptables_data_sz = 0;
tools/lib/bpf/linker.c +6 -4
index dba88f4fd6cd..d537388d495c 100644
@@ -2291,12 +2291,14 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
 					else
 						insn->imm += sec->dst_off;
 				} else if (sym_type == R_BPF_64_ABS64 &&
-					   (sec->shdr->sh_flags & SHF_EXECINSTR)) {
+					   ((sec->shdr->sh_flags & SHF_EXECINSTR) ||
+					    !strcmp(src_linked_sec->sec_name, ".addr_space.1"))) {
 					/*
 					 * A pointer to a static function in a data section,
-					 * which is stored in place as an offset of the
-					 * function in its section. Data sections are kept
-					 * in the byte order of the object.
+					 * or a pointer in arena to a static variable. It's
+					 * stored in place as an offset of the function or
+					 * the variable in its section. Data sections are
+					 * kept in the byte order of the object.
 					 */
 					void *ptr = dst_linked_sec->raw_data + dst_rel->r_offset;
 					__u64 off;
Signature · 1 line
2.55.0
#11 lore raw

Re: [PATCH bpf-next v3 06/14] libbpf: Relocate pointers that are stored in arena

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] libbpf: memory leak and OOB access in bpf_object__collect_arena_relos() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] libbpf: memory leak and OOB access in bpf_object__collect_arena_relos() --

commit 146cf79dea60b56b79e9a35a046106f0fa3e6d27
Author: Alexei Starovoitov <[email protected]>

libbpf: Relocate pointers that are stored in arena

This commit teaches libbpf and the BPF static linker to relocate R_BPF_64_ABS64 pointers stored in arena data sections that reference other arena variables. At open time, libbpf records the relocation offsets, and at load time, it adds the mapped arena base address and target offsets in place.

Quoted 4 lines
tools/lib/bpf/libbpf.c +0 -0
index a7c53dc5b91df..1172fe7992bf5 100644

[ ... ]

Quoted 46 lines
tools/lib/bpf/libbpf.c +42 -0
@@ -8223,6 +8246,59 @@ static int bpf_object__collect_rodata_relos(struct bpf_object *obj,
 	return 0;
 }
 
+/*
+ * Collect pointers that are stored in variables in arena, like
+ *   int __arena * __arena p = &var;
+ * They are R_BPF_64_ABS64 relocations. The addend is stored in place.
+ */
+static int bpf_object__collect_arena_relos(struct bpf_object *obj,
+					   Elf64_Shdr *shdr, Elf_Data *data)
+{
+	int i, nrels = shdr->sh_size / shdr->sh_entsize;
+	struct arena_ptr *ptrs;
+	const char *sym_name;
+	size_t sym_idx;
+	Elf64_Sym *sym;
+	Elf64_Rel *rel;
+
+	ptrs = libbpf_reallocarray(NULL, nrels, sizeof(*ptrs));
+	if (!ptrs)
+		return -ENOMEM;
+	obj->arena_ptrs = ptrs;
+
+	for (i = 0; i < nrels; i++, ptrs++) {
+		rel = elf_rel_by_idx(data, i);
+		if (!rel) {
+			pr_warn("sec '.rel%s': failed to get relo #%d\n", ARENA_SEC, i);
+			return -LIBBPF_ERRNO__FORMAT;
+		}
+
+		sym_idx = ELF64_R_SYM(rel->r_info);
+		sym = elf_sym_by_idx(obj, sym_idx);
+		if (!sym) {
+			pr_warn("sec '.rel%s': symbol #%zu not found for relo #%d\n",
+				ARENA_SEC, sym_idx, i);
+			return -LIBBPF_ERRNO__FORMAT;
+		}
+
+		if (ELF64_R_TYPE(rel->r_info) != R_BPF_64_ABS64 ||
+		    (sym->st_shndx != obj->efile.arena_data_shndx &&
+		     sym->st_shndx != obj->efile.arena_bss_shndx)) {
+			sym_name = elf_sym_str(obj, sym->st_name) ?: "<?>";
+			pr_warn("sec '%s': pointer to '%s' at offset %zu: it's not in arena\n",
+				ARENA_SEC, sym_name, (size_t)rel->r_offset);
+			return -LIBBPF_ERRNO__RELOC;

tools/lib/bpf/libbpf.c static int bpf_object__collect_rodata_relos(struct bpf_object *obj, · line 8300

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+		}
+
+		ptrs->off = rel->r_offset;
+		ptrs->targ_off = sym->st_value;
+		if (sym->st_shndx == obj->efile.arena_bss_shndx)
+			ptrs->targ_off += obj->arena_bss_off;
+		obj->arena_ptr_cnt++;
+	}
+	return 0;
+}

[Severity: Medium]
Can bpf_object__collect_arena_relos() leak obj->arena_ptrs and overflow it during relocate_arena_ptrs(), or fail with -ENOMEM when nrels is 0?

In bpf_object__elf_collect(), every SHT_REL section matching .rel.addr_space.1 is marked as SEC_RELO without checking sh_size > 0 or deduplicating target sections. bpf_object__collect_relos() then calls bpf_object__collect_arena_relos() for each SEC_RELO section whose sh_info matches obj->efile.arena_data_shndx.

Because libbpf_reallocarray(NULL, nrels, sizeof(*ptrs)) passes NULL instead of reallocating obj->arena_ptrs for obj->arena_ptr_cnt + nrels, while the loop still increments obj->arena_ptr_cnt++ cumulatively, two issues can occur:

1. If multiple SEC_RELO sections target arena_data_shndx, the second call overwrites obj->arena_ptrs (leaking the prior allocation) and allocates only nrels entries, while obj->arena_ptr_cnt holds the sum across both calls. When bpf_object__create_maps() -> relocate_arena_ptrs() later runs:

	for (i = 0; i < obj->arena_ptr_cnt; i++, p++) {
		memcpy(&val, data + p->off, sizeof(val));
		val += (__u64)(unsigned long)data + p->targ_off;
		memcpy(data + p->off, &val, sizeof(val));
	}

it reads past the end of obj->arena_ptrs and corrupts memory at data + p->off.

2. If an empty .rel.addr_space.1 section has sh_size == 0 (nrels == 0), libbpf_reallocarray(NULL, 0, sizeof(*ptrs)) calls realloc(NULL, 0), which may return NULL and cause bpf_object__collect_arena_relos() to return -ENOMEM.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#12 lore raw

Re: [PATCH bpf-next v3 06/14] libbpf: Relocate pointers that are stored in arena

Quoted 4 lines · On Tue, Nov 03, 2026 at 05:44 PM [email protected]…
Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+	ptrs = libbpf_reallocarray(NULL, nrels, sizeof(*ptrs));
+	if (!ptrs)
+		return -ENOMEM;
+	obj->arena_ptrs = ptrs;

[...]

Quoted 4 lines

1. If multiple SEC_RELO sections target arena_data_shndx, the second call overwrites obj->arena_ptrs (leaking the prior allocation) and allocates only nrels entries, while obj->arena_ptr_cnt holds the sum across both calls. When bpf_object__create_maps() -> relocate_arena_ptrs() later runs:

Not a bug. Only a hand-made ELF has two SHT_REL sections for one section. clang emits one for .addr_space.1, and the static linker merges them by name in linker_append_elf_relos().

Quoted 3 lines

2. If an empty .rel.addr_space.1 section has sh_size == 0 (nrels == 0), libbpf_reallocarray(NULL, 0, sizeof(*ptrs)) calls realloc(NULL, 0), which may return NULL and cause bpf_object__collect_arena_relos() to return -ENOMEM.

No. Nothing emits an empty SHT_REL section.
glibc returns a pointer for realloc(NULL, 0). Where it's NULL the open of such a file fails, and that's fine.

I'm replying to a bot hoping that Roman will teach it to learn from maintainers and stop reporting such bugs in libbpf where input is trusted.

#13 lore raw

Re: [PATCH bpf-next v3 06/14] libbpf: Relocate pointers that are stored in arena

Quoted 23 lines · Alexei Starovoitov
Quoted 4 lines · On Tue, Nov 03, 2026 at 05:44 PM [email protected]…
Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+    ptrs = libbpf_reallocarray(NULL, nrels, sizeof(*ptrs));
+    if (!ptrs)
+        return -ENOMEM;
+    obj->arena_ptrs = ptrs;

[...]

Quoted 4 lines

1. If multiple SEC_RELO sections target arena_data_shndx, the second call overwrites obj->arena_ptrs (leaking the prior allocation) and allocates only nrels entries, while obj->arena_ptr_cnt holds the sum across both calls. When bpf_object__create_maps() -> relocate_arena_ptrs() later runs:

Not a bug. Only a hand-made ELF has two SHT_REL sections for one section. clang emits one for .addr_space.1, and the static linker merges them by name in linker_append_elf_relos().

Quoted 3 lines

2. If an empty .rel.addr_space.1 section has sh_size == 0 (nrels == 0), libbpf_reallocarray(NULL, 0, sizeof(*ptrs)) calls realloc(NULL, 0), which may return NULL and cause bpf_object__collect_arena_relos() to return -ENOMEM.

Quoted 6 lines

No. Nothing emits an empty SHT_REL section.
glibc returns a pointer for realloc(NULL, 0). Where it's NULL the open of such a file fails, and that's fine.

I'm replying to a bot hoping that Roman will teach it to learn from maintainers and stop reporting such bugs in libbpf where input is trusted.

Thanks, well appreciated!
It’s going into that direction, now at least it should not raise it again in next versions.

#14 lore raw

Re: [PATCH bpf-next v3 06/14] libbpf: Relocate pointers that are stored in arena

Quoted 27 lines · Roman Gushchin
Quoted 27 lines · Alexei Starovoitov
Quoted 4 lines · On Tue, Nov 03, 2026 at 05:44 PM [email protected]…
Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+    ptrs = libbpf_reallocarray(NULL, nrels, sizeof(*ptrs));
+    if (!ptrs)
+        return -ENOMEM;
+    obj->arena_ptrs = ptrs;

[...]

Quoted 4 lines

1. If multiple SEC_RELO sections target arena_data_shndx, the second call overwrites obj->arena_ptrs (leaking the prior allocation) and allocates only nrels entries, while obj->arena_ptr_cnt holds the sum across both calls. When bpf_object__create_maps() -> relocate_arena_ptrs() later runs:

Not a bug. Only a hand-made ELF has two SHT_REL sections for one section. clang emits one for .addr_space.1, and the static linker merges them by name in linker_append_elf_relos().

Quoted 3 lines

2. If an empty .rel.addr_space.1 section has sh_size == 0 (nrels == 0), libbpf_reallocarray(NULL, 0, sizeof(*ptrs)) calls realloc(NULL, 0), which may return NULL and cause bpf_object__collect_arena_relos() to return -ENOMEM.

No. Nothing emits an empty SHT_REL section.
glibc returns a pointer for realloc(NULL, 0). Where it's NULL the open of such a file fails, and that's fine.

Quoted 5 lines
Quoted 2 lines

I'm replying to a bot hoping that Roman will teach it to learn from maintainers and stop reporting such bugs in libbpf where input is trusted.

Thanks, well appreciated!
It’s going into that direction, now at least it should not raise it again in next versions.

Thank you!

#15 lore raw

Re: [PATCH bpf-next v3 06/14] libbpf: Relocate pointers that are stored in arena

In reply to Alexei Starovoitov
TL;DR: - libbpf arena relocation may not bounds-check r_offset, so a crafted object could crash or corrupt memory at load - a second arena relocation section may leak the array and let the count…
Quoted 1 line

libbpf: Relocate pointers that are stored in arena

TL;DR:
- libbpf arena relocation may not bounds-check r_offset, so a crafted object could crash or corrupt memory at load
- a second arena relocation section may leak the array and let the count overrun it

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0

[ ... ]

Quoted 14 lines
tools/lib/bpf/libbpf.c +14 -0
+/* The arena is mapped. The address of a variable is the same for the program and for user space. */
+static void relocate_arena_ptrs(struct bpf_object *obj, const struct bpf_map *arena)
+{
+	void *data = arena->mmaped + obj->arena_data_off;
+	struct arena_ptr *p = obj->arena_ptrs;
+	__u64 val;
+	size_t i;
+
+	for (i = 0; i < obj->arena_ptr_cnt; i++, p++) {
+		memcpy(&val, data + p->off, sizeof(val));
+		val += (__u64)(unsigned long)data + p->targ_off;
+		memcpy(data + p->off, &val, sizeof(val));
+	}
+}

[ ... ]

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+static int bpf_object__collect_arena_relos(struct bpf_object *obj,
+					   Elf64_Shdr *shdr, Elf_Data *data)
+{

[ ... ]

Quoted 4 lines
tools/lib/bpf/libbpf.c +4 -0
+	ptrs = libbpf_reallocarray(NULL, nrels, sizeof(*ptrs));
+	if (!ptrs)
+		return -ENOMEM;
+	obj->arena_ptrs = ptrs;

[ ... ]

tools/lib/bpf/libbpf.c static int bpf_object__collect_rodata_relos(struct bpf_object *obj, · line 8300

Quoted 8 lines
tools/lib/bpf/libbpf.c +8 -0
+		ptrs->off = rel->r_offset;
+		ptrs->targ_off = sym->st_value;
+		if (sym->st_shndx == obj->efile.arena_bss_shndx)
+			ptrs->targ_off += obj->arena_bss_off;
+		obj->arena_ptr_cnt++;
+	}
+	return 0;
+}

Can rel->r_offset point past the end of the arena data here? It is stored in ptrs->off without being compared against the size of the .addr_space.1 data.

At load, relocate_arena_ptrs() reads and writes 8 bytes at data + p->off:

    memcpy(&val, data + p->off, sizeof(val));
    ...
    memcpy(data + p->off, &val, sizeof(val));

When FEAT_LDIMM64_FULL_RANGE_OFF is supported, arena_data_off is mmap_sz - roundup(arena_data_sz, page), so an r_offset larger than roundup(arena_data_sz, page) - 8 lands beyond the arena mapping. A crafted object would open fine and then crash or corrupt memory in bpf_object__load().

The sibling collectors reject such offsets, for example bpf_object__collect_rodata_relos():

    rel->r_offset >= scn_data->d_size ||
    scn_data->d_size - rel->r_offset < sizeof(__u64)

Should this function do a similar check and return -LIBBPF_ERRNO__FORMAT?

Also, obj->arena_ptrs is overwritten on every call while obj->arena_ptr_cnt keeps accumulating. If a malformed object has two SHT_REL sections whose sh_info refers to the arena section, the first array is leaked and arena_ptr_cnt can exceed the size of the second array:

    obj->arena_ptrs = ptrs;
    ...
    obj->arena_ptr_cnt++;

relocate_arena_ptrs() would then read past the heap allocation and use garbage off values as write targets. Should a second call be rejected when obj->arena_ptrs is already set?

No later commit in the series touches tools/lib/bpf, so neither case is addressed later.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41755299448

#16 lore raw

Re: [PATCH bpf-next v3 06/14] libbpf: Relocate pointers that are stored in arena

Quoted 6 lines · On Tue, Nov 03, 2026 at 05:59 PM [email protected] <[email protected]>…

tools/lib/bpf/libbpf.c static int bpf_object__collect_rodata_relos(struct bpf_object *obj, · line 8294

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+		ptrs->off = rel->r_offset;
+		ptrs->targ_off = sym->st_value;

Can rel->r_offset point past the end of the arena data here? It is stored in ptrs->off without being compared against the size of the .addr_space.1 data.

[...]

Quoted 1 line

Should this function do a similar check and return -LIBBPF_ERRNO__FORMAT?

Not a bug. Only a hand-made ELF has such r_offset. The input of libbpf is trusted.
I thought bpf CI knows this better than sashiko :(

Quoted 3 lines

relocate_arena_ptrs() would then read past the heap allocation and use garbage off values as write targets. Should a second call be rejected when obj->arena_ptrs is already set?

No. See my reply to sashiko.

#17 lore raw

PATCH v3 7/14 selftests/bpf: Test pointers in arena and .bss.addr_space.1

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

The initial values of variables in arena point to other variables in arena:

  pointer       points to
  ------------  -------------------------------------------
  px            an int
  parr          the third element of an array
  head, next    the elements of a list
  pbig          an element of an array in .bss.addr_space.1
  psx           a static int
  psbig         an element of a static array in .bss.addr_space.1

The prog and user space must see the same pointers:
- The prog follows each pointer and examines the value that it finds.
- User space compares px, parr, head and next with the addresses in the skeleton.
- User space reads through pbig, psx and psbig the values that the prog stored there. Static variables are not in the skeleton.

The selftests link each object with the static linker. Thus the object with psx and psbig does not build without the change in linker.c.

The variables in .bss.addr_space.1 must be zero and aligned, and the object file must be smaller than these variables. The size of .addr_space.1 is not a multiple of the alignment, thus libbpf must align the second section.

A second object has a pointer in arena to a variable that is not in arena. The open of this object must fail.

The test is serial, as the other tests in prog_tests/arena_*.c. See commit a14f9ba080c1 ("selftests/bpf: Run arena tests serially").

---
 tools/testing/selftests/bpf/Makefile.skel     |   3 +-
 .../bpf/prog_tests/arena_data_ptrs.c          |  70 +++++++++
 .../selftests/bpf/progs/arena_data_ptrs.c     | 138 ++++++++++++++++++
 .../bpf/progs/arena_data_ptrs_fail.c          |  25 ++++
 4 files changed, 235 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_data_ptrs.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_data_ptrs.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_data_ptrs_fail.c
tools/testing/selftests/bpf/Makefile.skel +2 -1
index e8699903feef..b8eaecc7f429 100644
@@ -20,7 +20,8 @@ ifneq ($(BPF_CC),)
 BPF_SRCS := $(notdir $(wildcard progs/*.c))
 BPF_OBJS := $(patsubst %.c,$(RDIR)/%.bpf.o,$(BPF_SRCS))
 
-SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
+SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c	\
+		  arena_data_ptrs_fail.c
 
 LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
 		linked_vars.skel.h linked_maps.skel.h linked_arena.skel.h \
tools/testing/selftests/bpf/prog_tests/arena_data_ptrs.c new file +70 -0

70 lines not on this page · raw message

tools/testing/selftests/bpf/progs/arena_data_ptrs.c new file +138 -0

138 lines not on this page · raw message

tools/testing/selftests/bpf/progs/arena_data_ptrs_fail.c new file +25 -0
new file mode 100644
index 000000000000..93533dea5f71
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define BPF_NO_KFUNC_PROTOTYPES
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_experimental.h"
+#include <bpf_arena_common.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARENA);
+	__uint(map_flags, BPF_F_MMAPABLE);
+	__uint(max_entries, 1);
+} arena SEC(".maps");
+
+/* the pointer is in arena, what it points to is not */
+int y = 1;
+int *py SEC(".addr_space.1") = &y;
+
+SEC("syscall")
+int prog(void *ctx)
+{
+	return y;
+}
+
+char _license[] SEC("license") = "GPL";
Signature · 1 line
2.55.0
#18 lore raw

PATCH v3 8/14 bpf: Introduce bpf register BPF_REG_ARENA_SP

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

A Rust function that gets &T does not know where T is. T can be on the heap, a static, or a local variable of the caller. The same insn accesses all three. The heap and the statics of a Rust prog are in arena. Thus a local variable whose address is taken must be in arena too, and the prog needs a second stack there.

Each function must find the end of this stack. The places for the stack pointer that were tried:

  stack pointer in         what is wrong with it
  -----------------------  ----------------------------------------------
  a per-cpu variable       A second run on the same cpu moves it, and the

first run can continue before the second ends.
bpf_run_ctx Load, add and store in each function.
a hidden argument It takes an argument register and a callee
saved register. BTF does not match the code.
r9, reserved The verifier changes too, and the prog has one
register less.
a new register It costs verifier code and a register in each
JIT. This series.

Introduce BPF_REG_ARENA_SP, r12, the stack pointer of the stack in arena. BPF_REG_AX stays the last register and becomes r13:

  register            before    after
  ----------------    ------    -----
  BPF_REG_PARAMS      r11       r11
  BPF_REG_ARENA_SP              r12
  BPF_REG_AX          r12       r13

This patch only reserves the number. The verifier continues to reject insns with r12.

BPF_REG_AX is visible in xlated insns. Adjust the selftests that match it there.

---
 include/linux/filter.h                        |  5 +-
 kernel/bpf/core.c                             |  3 +-
 .../selftests/bpf/prog_tests/ctx_rewrite.c    | 14 ++--
 .../bpf/progs/verifier_bpf_fastcall.c         | 24 +++----
 .../selftests/bpf/progs/verifier_may_goto_1.c | 12 ++--
 .../selftests/bpf/progs/verifier_patch_list.c |  6 +-
 .../selftests/bpf/progs/verifier_sdiv.c       | 64 +++++++++----------
 7 files changed, 65 insertions(+), 63 deletions(-)
include/linux/filter.h +3 -2
index e9f619deb2a8..abc759ad14a6 100644
@@ -61,8 +61,9 @@ struct ctl_table_header;
 
 /* Kernel hidden auxiliary/helper register. */
 #define BPF_REG_PARAMS		MAX_BPF_REG
-#define BPF_REG_AX		(MAX_BPF_REG + 1)
-#define MAX_BPF_EXT_REG		(MAX_BPF_REG + 2)
+#define BPF_REG_ARENA_SP	(MAX_BPF_REG + 1)
+#define BPF_REG_AX		(MAX_BPF_REG + 2)
+#define MAX_BPF_EXT_REG		(MAX_BPF_REG + 3)
 #define MAX_BPF_JIT_REG		MAX_BPF_EXT_REG
 
 /* unused opcode to mark special call to bpf_tail_call() helper */
kernel/bpf/core.c +2 -1
index dcaf9cf13ce9..06d6d45b1c0e 100644
@@ -1578,7 +1578,8 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,
 	u32 imm_rnd = get_random_u32();
 	int off;
 
-	BUILD_BUG_ON(BPF_REG_PARAMS + 2 != MAX_BPF_JIT_REG);
+	BUILD_BUG_ON(BPF_REG_PARAMS + 3 != MAX_BPF_JIT_REG);
+	BUILD_BUG_ON(BPF_REG_ARENA_SP + 2 != MAX_BPF_JIT_REG);
 	BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG);
 
 	/* Constraints on AX register:
tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c +7 -7
index 2c3124092b73..2d0bb771626d 100644
@@ -69,19 +69,19 @@ static struct test_case test_cases[] = {
 #if defined(__x86_64__) || defined(__aarch64__)
 	{
 		N(SCHED_CLS, struct __sk_buff, tstamp),
-		.read  = "r12 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);"
-			 "if w12 & 0x4 goto pc+1;"
+		.read  = "r13 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);"
+			 "if w13 & 0x4 goto pc+1;"
 			 "goto pc+4;"
-			 "if w12 & 0x3 goto pc+1;"
+			 "if w13 & 0x3 goto pc+1;"
 			 "goto pc+2;"
 			 "$dst = 0;"
 			 "goto pc+1;"
 			 "$dst = *(u64 *)($ctx + sk_buff::tstamp);",
-		.write = "r12 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);"
-			 "if w12 & 0x4 goto pc+1;"
+		.write = "r13 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);"
+			 "if w13 & 0x4 goto pc+1;"
 			 "goto pc+2;"
-			 "w12 &= -4;"
-			 "*(u8 *)($ctx + sk_buff::__mono_tc_offset) = r12;"
+			 "w13 &= -4;"
+			 "*(u8 *)($ctx + sk_buff::__mono_tc_offset) = r13;"
 			 "*(u64 *)($ctx + sk_buff::tstamp) = $src;",
 	},
 #endif
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +12 -12
index d908298b9fc7..314652a0006d 100644
@@ -915,13 +915,13 @@ __xlated("...")
 /* may_goto expansion starts, r0 and r1 are live */
 __xlated("10: r2 = ")
 __xlated("12: r2 = &(void __percpu *)(r2)")
-__xlated("13: r12 = *(u64 *)(r2 +0)")
-__xlated("14: if r12 == 0x0 goto pc+6")
-__xlated("15: r12 -= 1")
-__xlated("16: if r12 != 0x0 goto pc+2")
-__xlated("17: r12 = r2")
+__xlated("13: r13 = *(u64 *)(r2 +0)")
+__xlated("14: if r13 == 0x0 goto pc+6")
+__xlated("15: r13 -= 1")
+__xlated("16: if r13 != 0x0 goto pc+2")
+__xlated("17: r13 = r2")
 __xlated("18: call unknown")
-__xlated("19: *(u64 *)(r2 +0) = r12")
+__xlated("19: *(u64 *)(r2 +0) = r13")
 /* may_goto expansion ends */
 __xlated("20: *(u64 *)(r10 -8) = r1")
 __xlated("21: exit")
@@ -958,13 +958,13 @@ __xlated("7: call bpf_get_smp_processor_id")
 /* may_goto expansion starts, r0 and r1 are live */
 __xlated("8: r2 = ")
 __xlated("10: r2 = &(void __percpu *)(r2)")
-__xlated("11: r12 = *(u64 *)(r2 +0)")
-__xlated("12: if r12 == 0x0 goto pc+6")
-__xlated("13: r12 -= 1")
-__xlated("14: if r12 != 0x0 goto pc+2")
-__xlated("15: r12 = r2")
+__xlated("11: r13 = *(u64 *)(r2 +0)")
+__xlated("12: if r13 == 0x0 goto pc+6")
+__xlated("13: r13 -= 1")
+__xlated("14: if r13 != 0x0 goto pc+2")
+__xlated("15: r13 = r2")
 __xlated("16: call unknown")
-__xlated("17: *(u64 *)(r2 +0) = r12")
+__xlated("17: *(u64 *)(r2 +0) = r13")
 /* may_goto expansion ends */
 __xlated("18: *(u64 *)(r10 -8) = r1")
 __xlated("19: exit")
tools/testing/selftests/bpf/progs/verifier_may_goto_1.c +6 -6
index 20152a15ca36..8d231fb2a317 100644
@@ -97,13 +97,13 @@ __xlated("5: *(u64 *)(r0 +16) = 0")
 /* may_goto, r0 is dead */
 __xlated("6: r0 = ")
 __xlated("8: r0 = &(void __percpu *)(r0)")
-__xlated("9: r12 = *(u64 *)(r0 +0)")
-__xlated("10: if r12 == 0x0 goto pc+6")
-__xlated("11: r12 -= 1")
-__xlated("12: if r12 != 0x0 goto pc+2")
-__xlated("13: r12 = r0")
+__xlated("9: r13 = *(u64 *)(r0 +0)")
+__xlated("10: if r13 == 0x0 goto pc+6")
+__xlated("11: r13 -= 1")
+__xlated("12: if r13 != 0x0 goto pc+2")
+__xlated("13: r13 = r0")
 __xlated("14: call unknown")
-__xlated("15: *(u64 *)(r0 +0) = r12")
+__xlated("15: *(u64 *)(r0 +0) = r13")
 __xlated("16: r0 = 1")
 __xlated("17: r0 = 2")
 __xlated("18: exit")
tools/testing/selftests/bpf/progs/verifier_patch_list.c +3 -3
index 581b75b7b5c5..5e8166bb79cc 100644
@@ -103,8 +103,8 @@ __xlated("10: goto pc+1")
 __xlated("11: r0 /= r1")
 __xlated("12: r2 = ")
 __xlated("14: r2 = &(void __percpu *)(r2)")
-__xlated("15: r12 = *(u64 *)(r2 +0)")
-__xlated("16: if r12 == 0x0 goto pc+9")
+__xlated("15: r13 = *(u64 *)(r2 +0)")
+__xlated("16: if r13 == 0x0 goto pc+9")
 __xlated("...")
 __xlated("22: if r1 != 0x0 goto pc+2")
 __xlated("23: w0 ^= w0")
@@ -220,7 +220,7 @@ __arch_arm64
 __success
 __xlated("12: if r6 != 0x0 goto pc-3")
 __xlated("...")
-__xlated("32783: if r12 != 0x0 goto pc+1")
+__xlated("32783: if r13 != 0x0 goto pc+1")
 __xlated("32784: gotol pc-32773")
 __naked void patch_list_may_goto_gotol(void)
 {
tools/testing/selftests/bpf/progs/verifier_sdiv.c +32 -32

112 lines not on this page · raw message

Signature · 1 line
2.55.0
#19 lore raw

PATCH v3 9/14 bpf: Add stacks for programs to arena

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

A prog that keeps local variables in arena needs a stack in arena while it runs. r12 (BPF_REG_ARENA_SP) is the stack pointer of this stack.

One stack for each cpu is not sufficient. A run of a prog is interrupted, is preempted or sleeps, and a second run starts on the same cpu. The second run cannot put its frames after the frames of the first run, because the first run can continue before the second run ends:

cpu 0 run 1: f() run 2: h() run 1: f() calls g()

stack | f | | f | h | | f | g |
^ g overwrites h

Thus each run takes a stack when it starts and gives it back when it exits. Usually there is only one run on a cpu. Make this case a few insns without a call:

  stack that a run gets     condition                      nsec per run
  ------------------------  -----------------------------  ------------
  the per-cpu stack         no other run is on this cpu    2.2
  one of 16 more per cpu    the per-cpu stack is taken     9.1 more
  new pages for this run    all 17 stacks are taken        1300 to 1600

The times are for x86-64 (Zen 5), in a KVM guest. The first time is the difference to a prog that has the same variables in the r10 stack. The third time is for a stack of 1 page.

Each run increments the per-cpu counter 'busy'. The run that finds 0 has the per-cpu stack, and it stores 0 when it exits. The other runs call bpf_arena_stack_get() and bpf_arena_stack_put(). One bit of r12, BPF_ARENA_STACK_SLOW, shows at the exit which of the two ways is necessary.

The other choices:

  in this patch                  what was rejected, and why
  -----------------------------  ----------------------------------------
  The kernel supplies the        The compiler or libbpf supplies them:
  stacks, one set for each       the code that takes a stack is in each
  arena.                         object file, for ever.
  A stack is contiguous. It      4 fixed stacks per cpu: 512 Mbyte of
  has the size that the prog     addresses. Segments: each function can
  needs: 32 Kbyte of frames at   fail. Pages on a fault: a prog can fail
  most. It gets pages at the     in the middle of a run.
  first use.
  The pages are regular pages    Pages that user space does not see: no
  of the arena.                  gain. User space can corrupt all other
                                 data of the prog.
  A run takes a stack: a         Preemption off: no sleepable progs. Two
  counter, then more stacks,     calls, get and put: 4 nsec. One way for
  then new pages.                sleepable progs and one for the others:

two mechanisms. Skip the second run: not possible for a scheduler.

The third row is for a burst. The kernel frees pages of arena later, in a work item. It takes 1700 to 2000 nsec more for each such run, on the same cpu, and that is slower than the allocation. While all 17 stacks of a cpu stay taken, runs that follow each other without a pause take pages faster than the work item frees them. When the arena is full, runs are skipped and allocations of the prog fail until the work item catches up.

This patch adds the data and the two functions.
"bpf: Support BPF_REG_ARENA_SP in the verifier" emits the insns that use them. Progs without r12 get the same code.

Memory:
- A stack gets pages of the arena at its first use, in bpf_arena_stack_get(), and keeps them until a prog needs a larger stack.
- The bookkeeping has 152 bytes per cpu. It is in kernel memory, because the prog and user space can write to arena.
- struct bpf_arena has one more pointer and no longer fits in 512 bytes. Each arena, with or without stacks, takes 512 bytes more.
- When there is no memory for a stack, the prog does not run. The kernel counts a miss and writes a message to the stderr of the prog. A callback that gets no stack does not run either: it returns 0 to the helper, and the prog continues.

---
 include/linux/bpf.h |  30 ++++++++++
 kernel/bpf/arena.c  | 142 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 172 insertions(+)
include/linux/bpf.h +30 -0
index 5413fa5d2b2b..5c059a05f89d 100644
@@ -673,6 +673,31 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
 		      struct bpf_spin_lock *spin_lock);
 u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena);
 u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena);
+/*
+ * The stack in arena of a cpu. The invocation of a program that finds it free
+ * takes it. The others, that interrupt or preempt that one, take a stack from
+ * bpf_arena_stack_get(). bpf_insert_arena_stack() emits the code.
+ */
+struct bpf_arena_cpu_stack {
+	/*
+	 * Every invocation increments it. The one that finds 0 has the stack
+	 * and sets it to 0 when it exits.
+	 */
+	u64 busy;
+	u32 base;	/* r12 when the main program starts, the low 32 bits of the address */
+	u32 size;
+};
+
+/*
+ * r12 is a multiple of 32 when the main program starts in the stack of the cpu.
+ * It has this bit set in all other stacks. They go back with bpf_arena_stack_put().
+ */
+#define BPF_ARENA_STACK_SLOW 16
+
+int bpf_arena_stack_init(struct bpf_arena *arena);
+void __percpu *bpf_arena_stack_percpu(struct bpf_arena *arena);
+u64 bpf_arena_stack_get(struct bpf_prog_aux *aux, u64 busy);
+void bpf_arena_stack_put(struct bpf_prog_aux *aux, u32 base);
 u64 bpf_arena_map_kern_vm_start(struct bpf_map *map);
 struct bpf_map *bpf_prog_arena(struct bpf_prog *prog);
 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
@@ -1919,6 +1944,11 @@ struct bpf_prog_aux {
 	u64 prog_array_member_cnt; /* counts how many times as member of prog_array */
 	struct mutex ext_mutex; /* mutex for freplace_link_cnt and prog_array_member_cnt */
 	struct bpf_arena *arena;
+	/*
+	 * The size of the stack in arena that the program needs, a multiple of
+	 * the page size. 0 when r12 is not used.
+	 */
+	u32 arena_stack_size;
 	void (*recursion_detected)(struct bpf_prog *prog); /* callback if recursion is detected */
 	/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
 	const struct btf_type *attach_func_proto;
kernel/bpf/arena.c +142 -0

160 lines not on this page · raw message

Signature · 1 line
2.55.0
#20 lore raw

PATCH v3 10/14 bpf, x86: Add BPF_ATOMIC_LOCAL

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

A prog that has a stack in arena takes the per-cpu stack each time it starts. It does this with fetch_add on a per-cpu word. Only this cpu accesses the word. Thus the insn must be atomic only for an interrupt or for a different task on the same cpu. But the fetch_add of BPF is atomic for all cpus. On x86 it has the lock prefix, which waits for the stores in the store buffer.

Add BPF_ATOMIC_LOCAL, a flag in imm of fetch_add. It means that the insn is atomic only for code that runs on this cpu. One x86 insn is atomic for such code. Thus the x86 JIT does not emit the lock prefix:

  BPF_ADD | BPF_FETCH                       lock xadd
  BPF_ADD | BPF_FETCH | BPF_ATOMIC_LOCAL    xadd

The flag is for per-cpu memory only. It is correct there because progs do not migrate.

Only the verifier emits the flag, and only when the JIT reports bpf_jit_supports_local_atomics(). The other JITs get the usual fetch_add. The first user is "bpf: Support BPF_REG_ARENA_SP in the verifier". The verifier continues to reject the flag in the insns of a prog. The atomics of progs get the same code.

The time that the flag saves depends on the stores that are in the store buffer when the prog starts. On x86-64 (Zen 5), in nsec per run:

  hot caches                                 0.44
  after a store to 16 Mbyte of data (L3)     1.6
  after a store that misses all caches       47
---
 arch/x86/net/bpf_jit_comp.c | 10 +++++++++-
 include/linux/filter.h      |  8 ++++++++
 kernel/bpf/core.c           |  6 ++++++
 kernel/bpf/disasm.c         |  7 +++++++
 4 files changed, 30 insertions(+), 1 deletion(-)
arch/x86/net/bpf_jit_comp.c +9 -1
index 8ade87bd56e3..711d774b40e1 100644
@@ -1625,7 +1625,10 @@ static int emit_atomic_rmw(u8 **pprog, u32 atomic_op,
 {
 	u8 *prog = *pprog;
 
-	if (atomic_op != BPF_XCHG)
+	/* one insn is atomic for what runs on this cpu */
+	if (atomic_op & BPF_ATOMIC_LOCAL)
+		atomic_op &= ~BPF_ATOMIC_LOCAL;
+	else if (atomic_op != BPF_XCHG)
 		EMIT1(0xF0); /* lock prefix */
 
 	maybe_emit_mod(&prog, dst_reg, src_reg, bpf_size == BPF_DW);
@@ -5187,6 +5190,11 @@ bool bpf_jit_supports_private_stack(void)
 	return true;
 }
 
+bool bpf_jit_supports_local_atomics(void)
+{
+	return true;
+}
+
 void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)
 {
 #if defined(CONFIG_UNWINDER_ORC)
include/linux/filter.h +8 -0
index abc759ad14a6..7b730b6488ec 100644
@@ -81,6 +81,13 @@ struct ctl_table_header;
 /* unused opcode to mark special atomic instruction */
 #define BPF_PROBE_ATOMIC 0xe0
 
+/*
+ * Flag in imm of BPF_ATOMIC fetch_add: atomic for what runs on this cpu only,
+ * an interrupt or another task. The memory is per-cpu and the program
+ * doesn't migrate. It's cheaper where one insn without a lock does it.
+ */
+#define BPF_ATOMIC_LOCAL 0x1000
+
 /* unused opcode to mark special ldsx instruction. Same as BPF_NOSPEC */
 #define BPF_PROBE_MEM32SX 0xc0
 
@@ -1330,6 +1337,7 @@ bool bpf_jit_supports_ptr_xchg(void);
 bool bpf_jit_supports_arena(void);
 bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
 bool bpf_jit_supports_private_stack(void);
+bool bpf_jit_supports_local_atomics(void);
 bool bpf_jit_supports_large_stack(void);
 bool bpf_jit_supports_timed_may_goto(void);
 bool bpf_jit_supports_fsession(void);
kernel/bpf/core.c +6 -0
index 06d6d45b1c0e..1466a85c2b1a 100644
@@ -3874,6 +3874,12 @@ bool __weak bpf_jit_supports_private_stack(void)
 	return false;
 }
 
+/* Whether JIT supports BPF_ATOMIC_LOCAL */
+bool __weak bpf_jit_supports_local_atomics(void)
+{
+	return false;
+}
+
 /*
  * Return TRUE if the JIT lays out frames of up to MAX_BPF_STACK_JIT bytes.
  * Its prologue, epilogue and tail call sequences must encode such frame
kernel/bpf/disasm.c +7 -0
index 0b1bbb27e9e4..2e377a7ea262 100644
@@ -9,6 +9,7 @@
 
 /* Only defined by the non-UAPI linux/filter.h, which this file cannot use. */
 #define BPF_PROBE_ATOMIC 0xe0
+#define BPF_ATOMIC_LOCAL 0x1000
 
 #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
 static const char * const func_id_str[] = {
@@ -257,6 +258,12 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
 				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
 				insn->dst_reg,
 				insn->off, insn->src_reg);
+		else if (atomic && insn->imm == (BPF_ADD | BPF_FETCH | BPF_ATOMIC_LOCAL))
+			verbose(cbs->private_data, "(%02x) r%d = local%s_fetch_add((%s *)(r%d %+d), r%d)",
+				insn->code, insn->src_reg,
+				BPF_SIZE(insn->code) == BPF_DW ? "64" : "",
+				bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
+				insn->dst_reg, insn->off, insn->src_reg);
 		else if (atomic &&
 			 (insn->imm == BPF_ADD || insn->imm == BPF_AND ||
 			  insn->imm == BPF_OR || insn->imm == BPF_XOR)) {
Signature · 1 line
2.55.0
#21 lore raw

PATCH v3 11/14 bpf: Support BPF_REG_ARENA_SP in the verifier

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

In Rust &local, &heap and &static are the same type of pointer, and the same insn dereferences all of them. The heap and the statics of a Rust prog are in arena. Thus a local variable whose address is taken must be in arena too. Spills and all other local variables stay in the r10 stack.

Add a second stack, in arena. r12 (BPF_REG_ARENA_SP) is its stack pointer. The stack grows up, and r12 points to the end of the frame of the current function:

f:

    r12 += 32                  ; the frame of f, 32 bytes in arena
    *(u64 *)(r12 - 8) = r1     ; a local variable
    r1 = r12
    r1 += -8                   ; its address, a number (scalar)
    call g                     ; g accesses the variable through r1
    exit                       ; the verifier adds 'r12 += -32' here

The verifier tracks neither r12 nor the content of this stack. It does not track the rest of arena either. r12 holds the low 32 bits of the address, and the JIT adds the base of the arena. Thus an access through r12 stays in arena. But the code at the exit of the prog needs r12 to find the stack that it must give back. Thus accept r12 in 4 insns only:

  r12 += N                           first insn of a function, N % 16 == 0
  rX = r12                           number (scalar): user space address
  rX = *(size *)(r12 - off)          off is not checked against N

*(size *)(r12 - off) = rX or imm

Only the compiler knows N, the size of the frame. The verifier cannot calculate it, because callees access the frame through numbers. The frames of a call chain have 32 Kbyte at most.

The reasons for these rules:

  in this patch                  what was rejected, and why
  -----------------------------  ----------------------------------------
  One 'r12 += N', the first      'r12 -= rX': the depth has no bound.
  insn of a function.            More than one 'r12 += imm': r12 becomes
                                 verifier state.
  The verifier tracks nothing    Tracked slots: each store through a
  in this stack.                 number invalidates them.
  'rX = r12' gives a number.     PTR_TO_ARENA: after a store to memory

and a load it is a number. Then one insn sees two types.
The stack grows up, and the Down, with r12 + off: harder to read.
offsets are negative, as with
r10.

Each run takes a stack, see "bpf: Add stacks for programs to arena". The verifier adds this code to the main prog. busy, size and base are per-cpu:

entry before each exit

  old = busy++                           if (r12 & BPF_ARENA_STACK_SLOW)
  if (old == 0 && size is sufficient)            bpf_arena_stack_put()
          r12 = base                     else
  else                                           busy = 0
          r12 = bpf_arena_stack_get()
          no memory: return 0

The verifier adds this code before gen_prologue() and gen_epilogue() add theirs. Thus the prologue of the prog type runs before the prog takes a stack, and the epilogue runs after each exit, also after 'return 0'.

The usual path has 8 insns at the entry and 4 insns at the exit. The kernel does not pass r12 to a callback. Thus a callback that uses r12 gets the same code and takes a stack too. To reject r12 in callbacks is not
possible: real schedulers need it.

Progs without r12 get the same code. A prog that uses r12 in one of its functions pays in each run: up to 22 more x86 insns, no call, no lock prefix. The cost on x86-64 (Zen 5), in nsec more than the same prog with r10:

  each run, hot caches                               0.28 to 2.30
  each run, cold caches and TLB                      45 to 70
  each run that finds the per-cpu stack taken        8.7
  iteration of bpf_loop(), the callback uses r12     17.5 (1.13 -> 18.6)
  call of a function with a frame, load, store       below 0.23

A callback of bpf_loop() always finds the per-cpu stack taken, because the main prog has it. The verifier also does not inline such a loop.

Limitations:
- The JIT must report bpf_jit_supports_arena_stack(). No JIT does in this patch.
- The verifier rejects tail calls, because the prog would not give the stack back.
- It rejects extension progs. The code at the entry uses r2-r5, and an extension prog has arguments there.
- It rejects BPF_LD_ABS and BPF_LD_IND. They end the function when the offset is not in the packet, and the prog would not give the stack back.
- It rejects a jump and a gotox to 'r12 += N', because the insn would run again.
- It rejects a struct_ops prog that has a __ref argument. A run without a stack is skipped, and nothing would release the argument.
- It rejects a call of the main prog, and a call and a callx of a callback that takes a stack, because the caller would get the r12 of the callee.
- Nothing can attach to a subprog, because the x86 trampoline does not keep r12.
- The private stack is off, because the x86 JIT has one register for the two.
- bpf_throw() does not give the stack back. The stack stays taken until the arena is freed, for all progs of the arena on that cpu. After 17 throws on a cpu each run there allocates a stack, and each more throw loses the pages of one stack. Rust progs unwind and do not throw.

may_goto sets is_called in struct bpf_subprog_info for a function that BPF code calls, and for a callback of a helper other than bpf_loop() and bpf_for_each_map_elem(). Such a callback can take a stack. A function that BPF code calls cannot. Give these callbacks a bit of their own, is_other_cb. may_goto examines the two bits.

The test "invalid src register in LDX" of test_verifier used r12. Use r13.

---
 include/linux/bpf.h                           |   2 +
 include/linux/bpf_verifier.h                  |  14 +-
 include/linux/filter.h                        |  59 +++++
 kernel/bpf/backtrack.c                        |   2 +-
 kernel/bpf/const_fold.c                       |   7 +
 kernel/bpf/core.c                             |  25 +++
 kernel/bpf/fixups.c                           | 202 +++++++++++++++++-
 kernel/bpf/liveness.c                         |   8 +
 kernel/bpf/scev.c                             |   9 +-
 kernel/bpf/verifier.c                         | 194 ++++++++++++++++-
 .../selftests/bpf/verifier/basic_stx_ldx.c    |   4 +-
 11 files changed, 516 insertions(+), 10 deletions(-)
include/linux/bpf.h +2 -0
index 5c059a05f89d..acc8e0050cde 100644
@@ -1949,6 +1949,8 @@ struct bpf_prog_aux {
 	 * the page size. 0 when r12 is not used.
 	 */
 	u32 arena_stack_size;
+	/* a callback that takes a stack in arena, like the main program does */
+	bool arena_stack_entry;
 	void (*recursion_detected)(struct bpf_prog *prog); /* callback if recursion is detected */
 	/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
 	const struct btf_type *attach_func_proto;
include/linux/bpf_verifier.h +13 -1
index 9ae6e109d211..f5a4fe8a58fe 100644
@@ -944,6 +944,9 @@ enum priv_stack_mode {
 	PRIV_STACK_ADAPTIVE,
 };
 
+/* The most that the frames of a call chain take in the stack in arena */
+#define BPF_ARENA_STACK_MAX (32 << 10)
+
 struct bpf_subprog_info {
 	const char *name; /* name extracted from BTF */
 	u32 start; /* insn idx of function entry point */
@@ -957,6 +960,7 @@ struct bpf_subprog_info {
 	struct bpf_iarray *jt; /* jump table shared by all gotox of this subprogram */
 	u16 stack_depth; /* max. stack depth used by this function */
 	u16 stack_extra;
+	u16 arena_stack_depth; /* size of the frame in the stack in arena */
 	u32 insns_total;
 	u32 insns_self;
 	/* offsets in range [stack_depth .. fastcall_stack_off)
@@ -973,7 +977,9 @@ struct bpf_subprog_info {
 	bool is_exception_cb: 1;
 	/* bpf_loop() or bpf_for_each_map_elem() call it in a loop */
 	bool is_loop_cb: 1;
-	/* BPF code, another helper or a kfunc call it */
+	/* another helper or a kfunc calls it */
+	bool is_other_cb: 1;
+	/* a call insn or callx calls it */
 	bool is_called: 1;
 	bool args_cached: 1;
 	/* true if the return value is passed in the R0:R2 register pair */
@@ -982,6 +988,9 @@ struct bpf_subprog_info {
 	bool keep_fastcall_stack: 1;
 	bool changes_pkt_data: 1;
 	bool might_sleep: 1;
+	bool uses_arena_sp: 1;
+	/* a callback that takes a stack in arena, like the main program does */
+	bool arena_stack_entry: 1;
 	u8 arg_slot_cnt:4;
 
 	enum priv_stack_mode priv_stack_mode;
@@ -1145,6 +1154,8 @@ struct bpf_verifier_env {
 	bool strict_alignment;		/* perform strict pointer alignment checks */
 	bool test_state_freq;		/* test verifier with different pruning frequency */
 	bool test_reg_invariants;	/* fail verification on register invariants violations */
+	bool uses_arena_sp;		/* the program has insns with BPF_REG_ARENA_SP */
+	u32 max_arena_stack_depth;	/* of all call chains */
 	struct bpf_verifier_state *cur_state; /* current verifier state */
 	/* Search pruning optimization, array of list_heads for
 	 * lists of struct bpf_verifier_state_list.
@@ -2092,6 +2103,7 @@ void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env);
 int bpf_opt_remove_dead_code(struct bpf_verifier_env *env);
 int bpf_opt_remove_nops(struct bpf_verifier_env *env);
 void bpf_lower_cast_mov(struct bpf_verifier_env *env);
+int bpf_insert_arena_stack(struct bpf_verifier_env *env);
 int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, const union bpf_attr *attr);
 int bpf_convert_ctx_accesses(struct bpf_verifier_env *env);
 int bpf_jit_subprogs(struct bpf_verifier_env *env);
include/linux/filter.h +59 -0

77 lines not on this page · raw message

kernel/bpf/backtrack.c +1 -1
index 2d6535f271a6..b9653965e525 100644
@@ -325,7 +325,7 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
 				 * sreg needs precision before this insn
 				 */
 				bt_clear_reg(bt, dreg);
-				if (sreg != BPF_REG_FP)
+				if (sreg != BPF_REG_FP && sreg != BPF_REG_ARENA_SP)
 					bt_set_reg(bt, sreg);
 			} else {
 				/* dreg = K
kernel/bpf/const_fold.c +7 -0
index f9edfde965dc..0e0cad2b4b8e 100644
@@ -65,6 +65,13 @@ static void const_reg_xfer(struct bpf_verifier_env *env, struct const_arg_info *
 		ci_out[insn->dst_reg] = unknown;
 		return;
 	}
+	/* r12 is outside of it too */
+	if (is_arena_sp_adj(insn) || is_arena_sp_st(insn))
+		return;
+	if (is_arena_sp_mov(insn) || is_arena_sp_ldx(insn)) {
+		ci_out[insn->dst_reg] = unknown;
+		return;
+	}
 
 	switch (class) {
 	case BPF_ALU:
kernel/bpf/core.c +25 -0
index 1466a85c2b1a..8df24e6f7c1f 100644
@@ -3880,6 +3880,12 @@ bool __weak bpf_jit_supports_local_atomics(void)
 	return false;
 }
 
+/* Whether JIT supports BPF_REG_ARENA_SP */
+bool __weak bpf_jit_supports_arena_stack(void)
+{
+	return false;
+}
+
 /*
  * Return TRUE if the JIT lays out frames of up to MAX_BPF_STACK_JIT bytes.
  * Its prologue, epilogue and tail call sequences must encode such frame
@@ -3984,6 +3990,25 @@ __weak u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena)
 	return 0;
 }
 
+__weak int bpf_arena_stack_init(struct bpf_arena *arena)
+{
+	return -EOPNOTSUPP;
+}
+
+__weak void __percpu *bpf_arena_stack_percpu(struct bpf_arena *arena)
+{
+	return NULL;
+}
+
+__weak u64 bpf_arena_stack_get(struct bpf_prog_aux *aux, u64 busy)
+{
+	return 0;
+}
+
+__weak void bpf_arena_stack_put(struct bpf_prog_aux *aux, u32 base)
+{
+}
+
 #ifdef CONFIG_BPF_SYSCALL
 __weak bool bpf_arena_handle_page_fault(unsigned long addr, bool is_write,
 					unsigned long fault_ip)
kernel/bpf/fixups.c +200 -2

232 lines not on this page · raw message

kernel/bpf/liveness.c +8 -0
index 1a4cfb575077..f9e80e39c4f0 100644
@@ -1440,6 +1440,11 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	} else if (is_stack_arg_ldx(insn)) {
 		slot = stack_arg_off_to_slot(insn->off);
 		at_out[insn->dst_reg] = (slot >= 0) ? at_stack_arg_entry[slot] : none;
+	} else if (is_arena_sp_adj(insn) || is_arena_sp_st(insn)) {
+		/* at_out[12] is a stack arg slot, not r12 */
+	} else if (is_arena_sp_mov(insn) || is_arena_sp_ldx(insn)) {
+		/* an address in arena, or what is loaded from arena */
+		*dst = none;
 	} else if (class == BPF_ALU64 && BPF_SRC(insn->code) == BPF_K) {
 		if (code == BPF_MOV) {
 			*dst = none;
@@ -1673,6 +1678,9 @@ static int record_load_store_access(struct bpf_verifier_env *env,
 	 */
 	if (is_stack_arg_stx(insn) || is_stack_arg_st(insn) || is_stack_arg_ldx(insn))
 		return 0;
+	/* Likewise at[12] is not r12, and the access is to arena. */
+	if (is_arena_sp_st(insn) || is_arena_sp_ldx(insn))
+		return 0;
 
 	switch (class) {
 	case BPF_LDX:
kernel/bpf/scev.c +8 -1
index 06dee7e421ea..093dd6fdcfe5 100644
@@ -670,6 +670,12 @@ static int transfer(struct bpf_verifier_env *env, struct env *e, int idx)
 	int i, id;
 	s64 imm;
 
+	/* reg2expr[12] is a stack slot, not r12 */
+	if (is_arena_sp_adj(insn) || is_arena_sp_st(insn))
+		return 0;
+	if (is_arena_sp_mov(insn) || is_arena_sp_ldx(insn))
+		goto mark_dst_unknown;
+
 	switch (class) {
 	case BPF_ALU:
 	case BPF_ALU64:
@@ -1156,7 +1162,8 @@ static void collect_store_base_regs(struct bpf_verifier_env *env,
 	u16 base_regs = 0;
 	u32 r;
 
-	if (size == BPF_W || size == BPF_DW) {
+	if ((size == BPF_W || size == BPF_DW) &&
+	    !is_arena_sp_st(insn) && !is_arena_sp_ldx(insn)) {
 		if ((class == BPF_STX || class == BPF_ST) && insn->dst_reg != BPF_REG_FP)
 			base_regs |= BIT(insn->dst_reg);
 		else if (class == BPF_LDX && insn->src_reg != BPF_REG_FP)
kernel/bpf/verifier.c +191 -3

285 lines not on this page · raw message

tools/testing/selftests/bpf/verifier/basic_stx_ldx.c +2 -2
index 7a0aab3f2cd2..66fde558df33 100644
@@ -28,10 +28,10 @@
 {
 	"invalid src register in LDX",
 	.insns = {
-	BPF_LDX_MEM(BPF_B, BPF_REG_0, 12, 0),
+	BPF_LDX_MEM(BPF_B, BPF_REG_0, 13, 0),
 	BPF_EXIT_INSN(),
 	},
-	.errstr = "R12 is invalid",
+	.errstr = "R13 is invalid",
 	.result = REJECT,
 },
 {
Signature · 1 line
2.55.0
#22 lore raw

Re: [PATCH bpf-next v3 11/14] bpf: Support BPF_REG_ARENA_SP in the verifier

TL;DR: Nit only: the new arena stack block in check_max_stack_depth_subprog() separates the tail call comment from the check it documents; consider moving the block above the comment or after the…
Quoted 1 line

bpf: Support BPF_REG_ARENA_SP in the verifier

TL;DR: Nit only: the new arena stack block in check_max_stack_depth_subprog() separates the tail call comment from the check it documents; consider moving the block above the comment or after the check.

Quoted 3 lines
kernel/bpf/verifier.c +0 -0

[ ... ]

Quoted 13 lines
kernel/bpf/verifier.c +9 -0
@@ -5832,6 +5833,24 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,
 	 * tailcall will unwind the current stack frame but it will not get rid
 	 * of caller's stack as shown on the example above.
 	 */
+	/* The kernel calls a callback. It knows nothing about r12. */
+	if (subprog[idx].uses_arena_sp) {
+		for (tmp = idx; tmp >= 0; tmp = dinfo[tmp].caller) {
+			if (!subprog[tmp].is_cb)
+				continue;
+			subprog[tmp].arena_stack_entry = true;
+			break;
+		}
+	}

kernel/bpf/verifier.c static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx, · line 5854

Quoted 10 lines
kernel/bpf/verifier.c +9 -0
+	arena_depth += subprog[idx].arena_stack_depth;
+	if (arena_depth > env->max_arena_stack_depth)
+		env->max_arena_stack_depth = arena_depth;
+	if (arena_depth > BPF_ARENA_STACK_MAX) {
+		verbose(env, "combined size of the frames in the stack in arena is %d. Too large\n",
+			arena_depth);
+		return -EACCES;
+	}
+
 	if (idx && subprog[idx].has_tail_call && depth >= 256) {

This isn't a bug, but the new arena stack block now sits between the existing comment about limiting the caller's stack depth to 256 bytes when bpf2bpf calls are combined with tail calls, and the check it documents:

    if (idx && subprog[idx].has_tail_call && depth >= 256) {

The tail call comment now ends directly above "if (subprog[idx].uses_arena_sp)".

Could the new block be moved above that comment, or after the tail call check, so the comment stays next to the code it describes?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41755299448

#23 lore raw

PATCH v3 12/14 bpf, x86: Support BPF_REG_ARENA_SP

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

BPF r12 (BPF_REG_ARENA_SP) is the stack pointer of the stack in arena. It needs an x86 register for the full run of the prog. R9 is the only x86 register that has no fixed function. Map r12 to R9.

R9 has other uses too. In a prog that uses r12 the JIT changes them:

  R9 is also                        in a prog with r12
  --------------------------------  -------------------------------------
  clobbered by helpers and kfuncs   store R9 in the frame before the call,

load it after the call
arg 6 of a BPF-to-BPF call arg 6 is on the stack, before arg 7
arg 6 of a kfunc load arg 6 from the stack into R9,
move rsp to arg 7 for the call. Only for a kfunc with more than 5 args
frame pointer of private stack the private stack is off

To reject 6 arguments in such progs is not possible, because Rust functions have them. r12 in a slot of the frame costs a load for each access.

Only JITed code sees the arguments of a BPF-to-BPF call, because no trampoline attaches to a subprog of a prog with r12. Thus the JIT can select the location of arg 6.

Progs without r12 get the same JITed code. The cost in a prog with r12, in a hot loop on x86-64 (Zen 5), in nsec:

  call of a helper or a kfunc       store and load of R9      0.012
  function that has such a call     push r9, add rsp, 8       < 0.01
  BPF-to-BPF call with 6 or 7 args  arg 6 is on the stack     < 0.007
  call of a kfunc with 10 args      load of R9, moves of rsp  0.13 to 0.27

The last time depends on the address of the kfunc.

---
 arch/x86/net/bpf_jit_comp.c | 140 +++++++++++++++++++++++++++++++-----
 1 file changed, 124 insertions(+), 16 deletions(-)
arch/x86/net/bpf_jit_comp.c +124 -16
index 711d774b40e1..320cf9b526b3 100644
@@ -155,8 +155,9 @@ static int bpf_size_to_x86_bytes(int bpf_size)
  * register in load/store instructions, it always needs an
  * extra byte of encoding and is callee saved.
  *
- * x86-64 register R9 is not used by BPF programs, but can be used by BPF
- * trampoline. x86-64 register R10 is used for blinding (if enabled).
+ * x86-64 register R9 is the 6th argument, the frame pointer of the private
+ * stack, or BPF_REG_ARENA_SP. It can be used by BPF trampoline.
+ * x86-64 register R10 is used for blinding (if enabled).
  */
 static const int reg2hex[] = {
 	[BPF_REG_0] = 0,  /* RAX */
@@ -170,6 +171,7 @@ static const int reg2hex[] = {
 	[BPF_REG_8] = 6,  /* R14 callee saved */
 	[BPF_REG_9] = 7,  /* R15 callee saved */
 	[BPF_REG_FP] = 5, /* RBP readonly */
+	[BPF_REG_ARENA_SP] = 1, /* R9 */
 	[BPF_REG_AX] = 2, /* R10 temp register */
 	[AUX_REG] = 3,    /* R11 temp register */
 	[X86_REG_R9] = 1, /* R9 register, 6th function argument */
@@ -187,6 +189,7 @@ static const int reg2pt_regs[] = {
 	[BPF_REG_7] = offsetof(struct pt_regs, r13),
 	[BPF_REG_8] = offsetof(struct pt_regs, r14),
 	[BPF_REG_9] = offsetof(struct pt_regs, r15),
+	[BPF_REG_ARENA_SP] = offsetof(struct pt_regs, r9),
 	[BPF_REG_AX] = offsetof(struct pt_regs, r10),
 };
 
@@ -211,6 +214,7 @@ static bool is_ereg(u32 reg)
 			     BIT(BPF_REG_9) |
 			     BIT(X86_REG_R9) |
 			     BIT(X86_REG_R12) |
+			     BIT(BPF_REG_ARENA_SP) |
 			     BIT(BPF_REG_AX));
 }
 
@@ -2326,6 +2330,37 @@ static int emit_spectre_bhb_barrier(u8 **pprog, u8 *ip,
 	return 0;
 }
 
+/*
+ * Helpers and kfuncs clobber R9, which is BPF_REG_ARENA_SP. It's set after
+ * bpf_arena_stack_get() and is not used after bpf_arena_stack_put().
+ */
+static bool call_clobbers_arena_sp(const struct bpf_insn *insn)
+{
+	return insn->code == (BPF_JMP | BPF_CALL) && insn->src_reg != BPF_PSEUDO_CALL &&
+	       insn->imm != BPF_CALL_IMM(bpf_arena_stack_get) &&
+	       insn->imm != BPF_CALL_IMM(bpf_arena_stack_put);
+}
+
+/* Does the kfunc take more arguments than R1-R5 hold? */
+static bool kfunc_has_stack_args(const struct btf_func_model *fm)
+{
+	u32 i, nslots = 0;
+
+	for (i = 0; i < fm->nr_args; i++)
+		nslots += btf_func_model_arg_slots(fm, i);
+	return nslots > MAX_BPF_FUNC_REG_ARGS;
+}
+
+static bool calls_clobber_arena_sp(const struct bpf_prog *prog)
+{
+	int i;
+
+	for (i = 0; i < prog->len; i++)
+		if (call_clobbers_arena_sp(&prog->insnsi[i]))
+			return true;
+	return false;
+}
+
 static const struct bpf_jit_arg_abi x86_arg_abi = {
 	.nr_arg_regs		= 6,
 	.backfill_after_stack	= true,
@@ -2533,16 +2568,23 @@ static u64 bpf_unwind_target(u64 ret_addr)
 	return e ? ret_addr + e->fixup : ret_addr;
 }
 
-/* unwind_exit: fix the return address of this frame. r0 and r2 are returned. */
-static int emit_unwind_ret_addr(u8 **pprog, u8 *ip)
+/*
+ * unwind_exit: fix the return address of this frame. r0 and r2 are returned.
+ * So is BPF_REG_ARENA_SP, when it's used.
+ */
+static int emit_unwind_ret_addr(u8 **pprog, u8 *ip, bool arena_sp)
 {
 	u8 *prog = *pprog, *start = prog;
 
 	EMIT2(0x50, 0x56);			/* push rax; push rsi */
+	if (arena_sp)
+		push_r9(&prog);
 	EMIT4(0x48, 0x8B, 0x7D, 0x08);		/* mov rdi, [rbp + 8] */
 	if (emit_rsb_call(&prog, bpf_unwind_target, ip + (prog - start)))
 		return -EINVAL;
 	EMIT4(0x48, 0x89, 0x45, 0x08);		/* mov [rbp + 8], rax */
+	if (arena_sp)
+		pop_r9(&prog);
 	EMIT2(0x5E, 0x58);			/* pop rsi; pop rax */
 	*pprog = prog;
 	return 0;
@@ -2567,6 +2609,11 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 	u32 stack_depth;
 	int callee_saved_size;
 	s32 outgoing_arg_base;
+	/* the program uses BPF_REG_ARENA_SP, which is R9 */
+	bool arena_sp = bpf_prog->aux->arena_stack_size;
+	/* it has to be saved around calls of helpers and kfuncs */
+	bool arena_sp_save = arena_sp && calls_clobber_arena_sp(bpf_prog);
+	s32 arena_sp_off = 0;
 	int err;
 
 	stack_depth = bpf_prog->aux->stack_depth;
@@ -2591,12 +2638,21 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 	 * is disabled by the JIT when stack args exist, so R9 is always
 	 * available.
 	 *
+	 * Unless the program uses BPF_REG_ARENA_SP. Then for BPF-to-BPF calls
+	 * arg 6 is on the stack too: incoming args 6+ are at [rbp + 16], ...
+	 * and outgoing args 6+ are at [rsp], ... No trampoline is attached to
+	 * subprogs of such program. For a kfunc call arg 6 is moved to R9 and
+	 * rsp is moved to arg 7.
+	 * Helpers and kfuncs clobber R9. BPF_REG_ARENA_SP is stored in the frame
+	 * before the call and is loaded from there after it.
+	 *
 	 * Stack layout (high to low):
 	 *   [rbp + 16 + ...]    incoming stack args 7+ (from caller)
 	 *   [rbp + 8]           return address
 	 *   [rbp]               saved rbp
 	 *   [rbp - prog_stack]  program stack
 	 *   [below]             callee-saved regs
+	 *   [below]             BPF_REG_ARENA_SP, when it's used and there are calls
 	 *   [below]             outgoing args 7+ (= rsp)
 	 */
 	if (bpf_prog_uses_arena_insns(bpf_prog)) {
@@ -2655,19 +2711,29 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 	 * stack args exist, so tcc pushes need not be accounted for.
 	 */
 	outgoing_arg_base = -(round_up(stack_depth, 8) + callee_saved_size);
+	if (arena_sp_save) {
+		/* make the slot */
+		push_r9(&prog);
+		outgoing_arg_base -= 8;
+		arena_sp_off = outgoing_arg_base;
+	}
 
 	/*
 	 * Allocate outgoing stack arg area for args 7+ only.
 	 * Arg 6 goes into r9 register, not on stack.
 	 */
 	outgoing_rsp = out_stack_arg_cnt > 1 ? (out_stack_arg_cnt - 1) * 8 : 0;
+	if (arena_sp)
+		outgoing_rsp = out_stack_arg_cnt * 8;
 	/*
 	 * bpf_throw() sees rsp of this frame as it is at a call. The callee-saved
 	 * regs are above the outgoing args, or above r9, which is pushed around
-	 * every call when the stack is private.
+	 * every call when the stack is private. With BPF_REG_ARENA_SP the slot
+	 * that keeps r9 over calls is between the two.
 	 */
 	if (bpf_prog->aux->exception_boundary)
-		bpf_prog->aux->stack_arg_sp_adjust = outgoing_rsp + (priv_frame_ptr ? 8 : 0);
+		bpf_prog->aux->stack_arg_sp_adjust = outgoing_rsp +
+						     (priv_frame_ptr || arena_sp_save ? 8 : 0);
 	emit_sub_rsp(&prog, outgoing_rsp);
 
 	if (arena_vm_start)
@@ -3089,7 +3155,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		case BPF_ST | BPF_MEM | BPF_H:
 		case BPF_ST | BPF_MEM | BPF_W:
 		case BPF_ST | BPF_MEM | BPF_DW:
-			if (dst_reg == BPF_REG_PARAMS && insn->off == -8) {
+			if (dst_reg == BPF_REG_PARAMS && insn->off == -8 && !arena_sp) {
 				/* Arg 6: store immediate in r9 register */
 				emit_mov_imm64(&prog, X86_REG_R9, imm32 >> 31,
 					       imm32);
@@ -3103,7 +3169,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				 * BPF off=-16 → [rsp+0], off=-24 → [rsp+8], ...
 				 */
 				insn_off = outgoing_arg_base - outgoing_rsp -
-					   insn_off - 16;
+					   insn_off - (arena_sp ? 8 : 16);
 				dst_reg = BPF_REG_FP;
 			}
 			if (!accesses_stack_only) {
@@ -3123,14 +3189,15 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		case BPF_STX | BPF_MEM | BPF_H:
 		case BPF_STX | BPF_MEM | BPF_W:
 		case BPF_STX | BPF_MEM | BPF_DW:
-			if (dst_reg == BPF_REG_PARAMS && insn->off == -8) {
+			if (dst_reg == BPF_REG_PARAMS && insn->off == -8 && !arena_sp) {
 				/* Arg 6: store register value in r9 */
 				EMIT_mov(X86_REG_R9, src_reg);
 				break;
 			}
 			insn_off = insn->off;
 			if (dst_reg == BPF_REG_PARAMS) {
-				insn_off = outgoing_arg_base - outgoing_rsp - insn_off - 16;
+				insn_off = outgoing_arg_base - outgoing_rsp - insn_off -
+					   (arena_sp ? 8 : 16);
 				dst_reg = BPF_REG_FP;
 			}
 			if (!accesses_stack_only) {
@@ -3239,11 +3306,13 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
 			insn_off = insn->off;
 			if (src_reg == BPF_REG_PARAMS) {
-				if (insn_off == 8) {
+				if (insn_off == 8 && !arena_sp) {
 					/* Incoming arg 6: read from r9 */
 					EMIT_mov(dst_reg, X86_REG_R9);
 					break;
 				}
+				if (arena_sp)
+					insn_off += 8;
 				src_reg = BPF_REG_FP;
 				/*
 				 * Incoming args 7+: native_off == bpf_off
@@ -3471,6 +3540,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			/* call */
 		case BPF_JMP | BPF_CALL: {
 			const struct btf_func_model *fm = NULL;
+			bool arena_sp_args = false;
 
 			func = (u8 *) __bpf_call_base + imm32;
 			if (src_reg == BPF_PSEUDO_CALL && tail_call_reachable) {
@@ -3481,6 +3551,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			}
 			if (!imm32)
 				return -EINVAL;
+			if (arena_sp && call_clobbers_arena_sp(insn)) {
+				u8 *start = prog;
+
+				emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_ARENA_SP, arena_sp_off);
+				ip += prog - start;
+			}
 			if (src_reg == BPF_PSEUDO_KFUNC_CALL) {
 				fm = bpf_jit_find_kfunc_model(bpf_prog, insn);
 				if (!fm)
@@ -3489,8 +3565,31 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				if (err < 0)
 					return err;
 				ip += err;
-				ip += emit_kfunc_arg_moves(fm, outgoing_arg_base -
-							   outgoing_rsp, &prog);
+				/*
+				 * Other kfuncs leave rsp alone. bpf_throw() is one of
+				 * them, see stack_arg_sp_adjust.
+				 */
+				arena_sp_args = arena_sp && kfunc_has_stack_args(fm);
+				if (arena_sp_args) {
+					u8 *start = prog;
+
+					/*
+					 * Args 6+ are at [rsp]. Put arg 6 where
+					 * emit_kfunc_arg_moves() expects it.
+					 */
+					emit_ldx(&prog, BPF_DW, X86_REG_R9, BPF_REG_FP,
+						 outgoing_arg_base - outgoing_rsp);
+					ip += prog - start;
+				}
+				ip += emit_kfunc_arg_moves(fm, outgoing_arg_base - outgoing_rsp +
+							   (arena_sp ? 8 : 0), &prog);
+				if (arena_sp_args) {
+					u8 *start = prog;
+
+					/* arg 7 is at [rsp] */
+					emit_add_rsp(&prog, 8);
+					ip += prog - start;
+				}
 			}
 			if (priv_frame_ptr) {
 				push_r9(&prog);
@@ -3501,6 +3600,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				return -EINVAL;
 			if (priv_frame_ptr)
 				pop_r9(&prog);
+			if (arena_sp_args)
+				emit_sub_rsp(&prog, 8);
+			if (arena_sp && call_clobbers_arena_sp(insn))
+				emit_ldx(&prog, BPF_DW, BPF_REG_ARENA_SP, BPF_REG_FP, arena_sp_off);
 			/*
 			 * A kfunc returning more than 8 bytes hands the second
 			 * half back in RDX (the native ABI's second return reg),
@@ -3827,7 +3930,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 
 		case BPF_JMP | BPF_EXIT:
 			if (src_reg == BPF_UNWIND_EXIT && bpf_prog_can_unwind(bpf_prog) &&
-			    emit_unwind_ret_addr(&prog, ip))
+			    emit_unwind_ret_addr(&prog, ip, arena_sp))
 				return -EINVAL;
 			if (seen_exit) {
 				jmp_offset = ctx->cleanup_addr - addrs[i] + moved;
@@ -3841,8 +3944,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				if (emit_spectre_bhb_barrier(&prog, ip, bpf_prog))
 					return -EINVAL;
 			}
-			/* Deallocate outgoing args 7+ area. */
-			emit_add_rsp(&prog, outgoing_rsp);
+			/* Deallocate outgoing args 7+ area and the slot of BPF_REG_ARENA_SP. */
+			emit_add_rsp(&prog, outgoing_rsp + (arena_sp_save ? 8 : 0));
 			if (bpf_prog->aux->exception_boundary) {
 				pop_callee_regs(&prog, all_callee_regs_used);
 				pop_r12(&prog);
@@ -5190,6 +5293,11 @@ bool bpf_jit_supports_private_stack(void)
 	return true;
 }
 
+bool bpf_jit_supports_arena_stack(void)
+{
+	return true;
+}
+
 bool bpf_jit_supports_local_atomics(void)
 {
 	return true;
Signature · 1 line
2.55.0
#24 lore raw

PATCH v3 13/14 bpf, arm64: Support BPF_REG_ARENA_SP

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

r12 (BPF_REG_ARENA_SP) is the stack pointer of the second stack of a prog. This stack is in arena. The verifier replaces the insns that use r12 with insns that the JIT already knows: a 32-bit add, addr_space_cast, and loads and stores in arena. Thus the JIT only has to supply a register.

Map r12 to x23. The JIT does not use x23 for other data. x23 is callee saved, thus helpers and kfuncs keep its value. It is not an argument register, thus calls do not change.

The kernel calls the main prog and callbacks, and it does not know about r12. The main prog, and a callback that uses r12, take a stack at entry and set r12. Thus they save x23 at entry and restore it at exit. Other subprogs get x23 from the caller.

Progs without r12 get the same code. The cost in progs with r12:

save and restore of x23 0 or 2 insns (stp, ldp). The JIT saves
registers in pairs.
load or store through r12 3 insns: add of the base of arena, mov of
the negative offset, ldr or str.
load or store through r10 1 insn, for reference.

Tested in QEMU with the selftests of this series and bpf_testmod: with LSE atomics and without, at EL1 and at EL2 (VHE), with 2 and 4 cpus, with bpf_jit_harden 0 and 2, with pages of 4 Kbyte and of 64 Kbyte, with KASAN and lockdep.

---
 arch/arm64/net/bpf_jit_comp.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/net/bpf_jit_comp.c +12 -0
index e99f105b7f61..e6d6df96fa99 100644
@@ -61,6 +61,8 @@ static const int bpf2a64[] = {
 	[BPF_REG_9] = A64_R(22),
 	/* read-only frame pointer to access stack */
 	[BPF_REG_FP] = A64_R(25),
+	/* callee saved register for the pointer to the stack in arena */
+	[BPF_REG_ARENA_SP] = A64_R(23),
 	/* temporary registers for BPF JIT */
 	[TMP_REG_1] = A64_R(10),
 	[TMP_REG_2] = A64_R(11),
@@ -430,6 +432,11 @@ static void find_used_callee_regs(struct jit_ctx *ctx)
 	if (ctx->arena_vm_start)
 		ctx->used_callee_reg[i++] = bpf2a64[ARENA_VM_START];
 
+	/* subprogs get it from the caller and leave it as it was, unless the kernel calls them */
+	if (prog->aux->arena_stack_size &&
+	    (!bpf_is_subprog(prog) || prog->aux->arena_stack_entry))
+		ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_ARENA_SP];
+
 	ctx->nr_used_callee_reg = i;
 }
 
@@ -2548,6 +2555,11 @@ bool bpf_jit_supports_private_stack(void)
 	return true;
 }
 
+bool bpf_jit_supports_arena_stack(void)
+{
+	return true;
+}
+
 bool bpf_jit_supports_kfunc_call(void)
 {
 	return true;
Signature · 1 line
2.55.0
#25 lore raw

PATCH v3 14/14 selftests/bpf: Add tests for BPF_REG_ARENA_SP

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

r12 (BPF_REG_ARENA_SP) is the stack pointer of the second stack of a prog. This stack is in arena. Each run of a prog takes a stack at entry and gives it back at exit. Runs that preempt or interrupt each other on one cpu must not get the same stack.

arena_stack runs progs:

  subtest             what it does
  ------------------  ---------------------------------------------------
  preemption          8 threads on one cpu run a prog that fills its

frames, spins, and then examines the frames.
nesting The same with preemption disabled. A prog in the
timer interrupt writes to its own frames.
timer A timer callback takes a stack of its own.
throw bpf_throw() does not give the stack back. After many
throws other progs continue to run correctly.
no_memory The arena has no free page. The prog does not run,
returns 0, and its stderr has a message.
attach_to_subprog fentry to the main prog loads. fentry to a subprog
is rejected.
jump_to_first_insn A jump and a gotox to the first insn do not take
a stack again.
slot_grows A run needs a larger stack than the slot has. The
next run on the cpu does not get the same stack. Skipped when the stack does not grow (64 Kbyte pages) or the compiler inlined arena_free_pages().
throw_stack_args The main prog has stack arguments and calls
bpf_throw(). The kernel gets its registers back.

The first two subtests fail if all runs on a cpu get the same stack. For the first one the kernel must preempt a thread in the middle of the prog, as it does with CONFIG_PREEMPT=y (config.x86_64). With lazy preemption and without HRTICK, the defaults of arm64, it switches the threads after the prog exits.

arena_stack is serial, as the other tests in prog_tests/arena_*.c. See commit a14f9ba080c1 ("selftests/bpf: Run arena tests serially").

verifier_arena_stack has the tests of the verifier:
- Progs with the four insns that can use r12 load and return correct values.
- All other insns with r12 are rejected. So are a frame of an incorrect size, call chains with more than 32 Kbyte of frames, tail calls, BPF_LD_ABS, a gotox to 'r12 += N', and a call and a callx of a callback that takes a stack.
- A callback that starts with a call, or with the address of a function, keeps the target when the verifier adds code in front of this insn.
- The prologue of a prog that writes to the packet is in front of the code that takes a stack.
- r12 and the frame stay correct after a helper, a subprog with or without a frame, a callback and unwind_exit of a callee.
- On x86 r12 is in the register of the 6th argument. Thus a subprog with 7 arguments and a kfunc with 10 arguments are tested too.
- may_goto: r12 stays correct after arch_bpf_timed_may_goto(). When the time is up a callback of bpf_loop() returns before it takes a stack.
- The callback of bpf_user_ringbuf_drain() can use r12.

pro_epilogue_arena_stack uses the struct_ops of bpf_testmod, which has gen_prologue() and gen_epilogue(). The two run also when the arena has no page for a stack and the prog is skipped.

struct_ops_refcounted_fail__arena_stack has a __ref argument and uses r12. It is rejected.

arena_noarena gets a prog that uses r12 and has no arena. It is rejected.

Compilers do not emit r12. Thus the tests encode these insns with .8byte.

---
 .../selftests/bpf/prog_tests/arena_stack.c    |  323 +++++
 .../selftests/bpf/prog_tests/pro_epilogue.c   |    2 +
 .../prog_tests/test_struct_ops_refcounted.c   |    2 +
 .../selftests/bpf/prog_tests/verifier.c       |    2 +
 .../selftests/bpf/progs/arena_noarena.c       |   14 +
 .../testing/selftests/bpf/progs/arena_stack.c |  334 +++++
 .../selftests/bpf/progs/arena_stack_fentry.c  |   13 +
 .../selftests/bpf/progs/arena_stack_grow.c    |  151 +++
 .../selftests/bpf/progs/arena_stack_loop.c    |   77 ++
 .../selftests/bpf/progs/arena_stack_nomem.c   |   41 +
 .../selftests/bpf/progs/arena_stack_throw.c   |  110 ++
 .../bpf/progs/pro_epilogue_arena_stack.c      |  199 +++
 .../struct_ops_refcounted_fail__arena_stack.c |   61 +
 .../bpf/progs/verifier_arena_stack.c          | 1169 +++++++++++++++++
 14 files changed, 2498 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_fentry.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_grow.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_loop.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_nomem.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_stack_throw.c
 create mode 100644 tools/testing/selftests/bpf/progs/pro_epilogue_arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__arena_stack.c
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_stack.c
tools/testing/selftests/bpf/prog_tests/arena_stack.c new file +323 -0
new file mode 100644
index 000000000000..20950a8df3d8
@@ -0,0 +1,323 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define _GNU_SOURCE
+#include <test_progs.h>
+#include <pthread.h>
+#include <sched.h>
+#include <linux/perf_event.h>
+#include <sys/syscall.h>
+#include "arena_stack.skel.h"
+#include "arena_stack_nomem.skel.h"
+#include "arena_stack_fentry.skel.h"
+#include "arena_stack_loop.skel.h"
+#include "arena_stack_grow.skel.h"
+#include "arena_stack_throw.skel.h"
+
+static int run_prog(int fd)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+
+	if (bpf_prog_test_run_opts(fd, &opts))
+		return -1;
+	return opts.retval;
+}
+
+static int run_tc_prog(int fd)
+{
+	char pkt[64] = {};
+	LIBBPF_OPTS(bpf_test_run_opts, opts, .data_in = pkt, .data_size_in = sizeof(pkt));
+
+	if (bpf_prog_test_run_opts(fd, &opts))
+		return -1;
+	return opts.retval;
+}
+
+static int pin_to_cpu0(cpu_set_t *old)
+{
+	cpu_set_t set;
+
+	if (sched_getaffinity(0, sizeof(*old), old))
+		return -1;
+	CPU_ZERO(&set);
+	CPU_SET(0, &set);
+	return sched_setaffinity(0, sizeof(set), &set);
+}
+
+#define NR_THREADS 8
+#define NR_RUNS 20
+
+static int preempted_fd;
+
+static void *preempted_thread(void *arg)
+{
+	long bad = 0;
+	int i;
+
+	for (i = 0; i < NR_RUNS; i++)
+		bad += run_prog(preempted_fd) != 0;
+	return (void *)bad;
+}
+
+/* Programs on one cpu preempt each other. They don't exit in the order they started. */
+static void test_preemption(struct arena_stack *skel)
+{
+	pthread_t thread[NR_THREADS];
+	long bad = 0;
+	cpu_set_t old;
+	void *ret;
+	int i, n;
+
+	if (!ASSERT_OK(pin_to_cpu0(&old), "pin"))
+		return;
+	preempted_fd = bpf_program__fd(skel->progs.preempted);
+	/* threads inherit the affinity */
+	for (n = 0; n < NR_THREADS; n++)
+		if (!ASSERT_OK(pthread_create(&thread[n], NULL, preempted_thread, NULL), "thread"))
+			break;
+	for (i = 0; i < n; i++) {
+		pthread_join(thread[i], &ret);
+		bad += (long)ret;
+	}
+	ASSERT_EQ(bad, 0, "runs with a corrupted frame");
+	sched_setaffinity(0, sizeof(old), &old);
+}
+
+/*
+ * A program that runs with preemption disabled is interrupted by a program in
+ * the timer interrupt
+ */
+static void test_nesting(struct arena_stack *skel)
+{
+	struct perf_event_attr attr = {
+		.size = sizeof(attr),
+		.type = PERF_TYPE_SOFTWARE,
+		.config = PERF_COUNT_SW_CPU_CLOCK,
+		.sample_period = 50000, /* nsec */
+	};
+	struct bpf_link *link = NULL;
+	int i, pfd, bad = 0;
+	cpu_set_t old;
+
+	if (!ASSERT_OK(pin_to_cpu0(&old), "pin"))
+		return;
+	pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, 0);
+	if (!ASSERT_GE(pfd, 0, "perf_event_open"))
+		goto out;
+	link = bpf_program__attach_perf_event(skel->progs.in_interrupt, pfd);
+	if (!ASSERT_OK_PTR(link, "attach_perf_event")) {
+		close(pfd);
+		goto out;
+	}
+	for (i = 0; i < 200; i++)
+		bad += run_tc_prog(bpf_program__fd(skel->progs.interrupted)) != 0;
+	ASSERT_EQ(bad, 0, "runs with a corrupted frame");
+	/* the program was interrupted */
+	ASSERT_GT(skel->bss->interrupts, 10, "interrupts");
+	bpf_link__destroy(link);
+out:
+	sched_setaffinity(0, sizeof(old), &old);
+}
+
+/* bpf_throw() doesn't return the stack. Programs run when all stacks of the cpu are taken. */
+static void test_throw(struct arena_stack *skel)
+{
+	cpu_set_t old;
+	int i;
+
+	if (!ASSERT_OK(pin_to_cpu0(&old), "pin"))
+		return;
+	for (i = 0; i < 40; i++)
+		run_prog(bpf_program__fd(skel->progs.throws));
+	for (i = 0; i < 40; i++)
+		if (!ASSERT_EQ(run_prog(bpf_program__fd(skel->progs.prog_returns_7)), 7, "returns_7"))
+			break;
+	/* the same with preemption disabled */
+	for (i = 0; i < 200; i++)
+		run_tc_prog(bpf_program__fd(skel->progs.throws_nested));
+	for (i = 0; i < 40; i++)
+		if (!ASSERT_EQ(run_tc_prog(bpf_program__fd(skel->progs.returns_8_nested)), 8,
+			       "returns_8_nested"))
+			break;
+	sched_setaffinity(0, sizeof(old), &old);
+}
+
+/* A callback that the kernel calls takes a stack of its own */
+static void test_timer(struct arena_stack *skel)
+{
+	int i, j;
+
+	for (i = 0; i < 100; i++) {
+		if (!ASSERT_OK(run_prog(bpf_program__fd(skel->progs.start_timer)), "start_timer"))
+			return;
+		for (j = 0; j < 1000 && skel->bss->timer_runs <= i; j++)
+			usleep(100);
+	}
+	ASSERT_EQ(skel->bss->timer_runs, 100, "runs of the callback");
+	ASSERT_EQ(skel->bss->timer_bad, 0, "runs with a wrong frame");
+}
+
+static void test_attach_to_subprog(struct arena_stack *skel)
+{
+	struct arena_stack_fentry *fentry;
+	int tgt_fd = bpf_program__fd(skel->progs.preempted);
+
+	/* the main program is fine */
+	fentry = arena_stack_fentry__open();
+	if (!ASSERT_OK_PTR(fentry, "open"))
+		return;
+	ASSERT_OK(bpf_program__set_attach_target(fentry->progs.on_entry, tgt_fd, "preempted"),
+		  "set_attach_target");
+	ASSERT_OK(arena_stack_fentry__load(fentry), "load for the main program");
+	arena_stack_fentry__destroy(fentry);
+
+	fentry = arena_stack_fentry__open();
+	if (!ASSERT_OK_PTR(fentry, "open"))
+		return;
+	ASSERT_OK(bpf_program__set_attach_target(fentry->progs.on_entry, tgt_fd, "spin"),
+		  "set_attach_target");
+	ASSERT_ERR(arena_stack_fentry__load(fentry), "load for a subprog");
+	arena_stack_fentry__destroy(fentry);
+}
+
+/* The insns that take the stack are in front of the first insn. A jump to it doesn't run them. */
+static void test_jump_to_first_insn(void)
+{
+	struct arena_stack_loop *skel;
+	cpu_set_t old;
+	int i;
+
+	skel = arena_stack_loop__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (!ASSERT_OK(pin_to_cpu0(&old), "pin"))
+		goto out;
+	for (i = 0; i < 1000; i++) {
+		if (!ASSERT_EQ(run_tc_prog(bpf_program__fd(skel->progs.loop)), 7, "loop"))
+			break;
+		if (!ASSERT_EQ(run_tc_prog(bpf_program__fd(skel->progs.nop_loop)), 7, "nop_loop"))
+			break;
+		if (!ASSERT_EQ(run_tc_prog(bpf_program__fd(skel->progs.gotox_loop)), 7,
+			       "gotox_loop"))
+			break;
+	}
+	sched_setaffinity(0, sizeof(old), &old);
+out:
+	arena_stack_loop__destroy(skel);
+}
+
+/*
+ * The stack that a callback finds is too small. It gets a new one and the pages of the old
+ * one are freed. Before the callback starts another program gets these pages as its stack
+ * and gives it back. The stack of the callback has to stay taken.
+ */
+static void test_slot_grows(void)
+{
+	struct arena_stack_grow *skel;
+	cpu_set_t old;
+
+	/* the compiler may inline it */
+	if (libbpf_find_vmlinux_btf_id("arena_free_pages", BPF_TRACE_FEXIT) <= 0) {
+		test__skip();
+		return;
+	}
+	skel = arena_stack_grow__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (!ASSERT_OK(pin_to_cpu0(&old), "pin"))
+		goto out;
+	/* the stack of the cpu is large enough for both programs */
+	ASSERT_OK(run_prog(bpf_program__fd(skel->progs.big)), "big");
+	ASSERT_OK(run_prog(bpf_program__fd(skel->progs.small)), "small");
+	ASSERT_OK(arena_stack_grow__attach(skel), "attach");
+	skel->bss->nest = 1;
+	ASSERT_OK(run_prog(bpf_program__fd(skel->progs.big)), "big with callbacks");
+	ASSERT_EQ(skel->bss->frame, 1, "frame of outer_cb");
+	/* the stack did not grow, or arena_free_pages() is inlined where it grows */
+	if (!skel->bss->waited)
+		test__skip();
+	sched_setaffinity(0, sizeof(old), &old);
+out:
+	arena_stack_grow__destroy(skel);
+}
+
+static void test_no_memory(void)
+{
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	struct arena_stack_nomem *skel;
+	char buf[256] = {};
+	int fd;
+
+	skel = arena_stack_nomem__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	ASSERT_EQ(run_prog(bpf_program__fd(skel->progs.take_page)), 0, "take_page");
+	fd = bpf_program__fd(skel->progs.returns_1);
+	ASSERT_EQ(run_prog(fd), 0, "retval");
+	ASSERT_EQ(skel->bss->runs, 0, "runs");
+	ASSERT_OK(bpf_prog_get_info_by_fd(fd, &info, &info_len), "info");
+	ASSERT_EQ(info.recursion_misses, 1, "misses");
+	ASSERT_GT(bpf_prog_stream_read(fd, BPF_STREAM_STDERR, buf, sizeof(buf) - 1, NULL), 0,
+		  "stream_read");
+	ASSERT_HAS_SUBSTR(buf, "No memory for the stack in arena", "stderr");
+	arena_stack_nomem__destroy(skel);
+}
+
+/*
+ * bpf_throw() in a main program that has stack arguments. The exception callback restores the
+ * registers of the caller.
+ */
+static void test_throw_stack_args(void)
+{
+	struct arena_stack_throw *skel;
+	int i;
+
+	skel = arena_stack_throw__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (!skel->rodata->has_stack_arg) {
+		test__skip();
+		goto out;
+	}
+	if (!ASSERT_OK(arena_stack_throw__attach(skel), "attach"))
+		goto out;
+	ASSERT_OK(run_prog(bpf_program__fd(skel->progs.calls_kfunc)), "calls_kfunc");
+	ASSERT_GE(skel->bss->throws, 1, "throws");
+	for (i = 0; i < 4; i++)
+		ASSERT_EQ(skel->bss->regs[i], 6 + i, "register");
+out:
+	arena_stack_throw__destroy(skel);
+}
+
+void serial_test_arena_stack(void)
+{
+	struct arena_stack *skel;
+
+#if !defined(__x86_64__) && !defined(__aarch64__)
+	/* other JITs don't support r12 */
+	test__skip();
+	return;
+#endif
+	skel = arena_stack__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (test__start_subtest("preemption"))
+		test_preemption(skel);
+	if (test__start_subtest("nesting"))
+		test_nesting(skel);
+	if (test__start_subtest("attach_to_subprog"))
+		test_attach_to_subprog(skel);
+	if (test__start_subtest("timer"))
+		test_timer(skel);
+	if (test__start_subtest("throw"))
+		test_throw(skel);
+	arena_stack__destroy(skel);
+	if (test__start_subtest("throw_stack_args"))
+		test_throw_stack_args();
+	if (test__start_subtest("jump_to_first_insn"))
+		test_jump_to_first_insn();
+	if (test__start_subtest("slot_grows"))
+		test_slot_grows();
+	if (test__start_subtest("no_memory"))
+		test_no_memory();
+}
tools/testing/selftests/bpf/prog_tests/pro_epilogue.c +2 -0
index 5d3c00a08a88..403aaaf12d1c 100644
@@ -7,6 +7,7 @@
 #include "pro_epilogue_goto_start.skel.h"
 #include "epilogue_exit.skel.h"
 #include "pro_epilogue_with_kfunc.skel.h"
+#include "pro_epilogue_arena_stack.skel.h"
 
 struct st_ops_args {
 	__u64 a;
@@ -57,6 +58,7 @@ void test_pro_epilogue(void)
 	RUN_TESTS(pro_epilogue_goto_start);
 	RUN_TESTS(epilogue_exit);
 	RUN_TESTS(pro_epilogue_with_kfunc);
+	RUN_TESTS(pro_epilogue_arena_stack);
 	if (test__start_subtest("tailcall"))
 		test_tailcall();
 }
tools/testing/selftests/bpf/prog_tests/test_struct_ops_refcounted.c +2 -0
index da60c715fc59..db79ac9b98d3 100644
@@ -4,6 +4,7 @@
 #include "struct_ops_refcounted_fail__ref_leak.skel.h"
 #include "struct_ops_refcounted_fail__global_subprog.skel.h"
 #include "struct_ops_refcounted_fail__tail_call.skel.h"
+#include "struct_ops_refcounted_fail__arena_stack.skel.h"
 
 void test_struct_ops_refcounted(void)
 {
@@ -11,4 +12,5 @@ void test_struct_ops_refcounted(void)
 	RUN_TESTS(struct_ops_refcounted_fail__ref_leak);
 	RUN_TESTS(struct_ops_refcounted_fail__global_subprog);
 	RUN_TESTS(struct_ops_refcounted_fail__tail_call);
+	RUN_TESTS(struct_ops_refcounted_fail__arena_stack);
 }
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 3d0eda3b4da4..01286be73335 100644
@@ -13,6 +13,7 @@
 #include "verifier_and.skel.h"
 #include "verifier_arena.skel.h"
 #include "verifier_arena_cast.skel.h"
+#include "verifier_arena_stack.skel.h"
 #include "verifier_arena_large.skel.h"
 #include "verifier_arena_globals1.skel.h"
 #include "verifier_arena_globals2.skel.h"
@@ -209,6 +210,7 @@ void test_verifier_align(void)                { RUN(verifier_align); }
 void test_verifier_and(void)                  { RUN(verifier_and); }
 void test_verifier_arena(void)                { RUN(verifier_arena); }
 void test_verifier_arena_cast(void)           { RUN(verifier_arena_cast); }
+void test_verifier_arena_stack(void)          { RUN_TESTS(verifier_arena_stack); }
 void test_verifier_arena_large(void)          { RUN(verifier_arena_large); }
 void test_verifier_arena_globals1(void)       { RUN(verifier_arena_globals1); }
 void test_verifier_arena_globals2(void)       { RUN(verifier_arena_globals2); }
tools/testing/selftests/bpf/progs/arena_noarena.c +14 -0
index 1f46e5aa403b..8a7ce3e36703 100644
@@ -21,4 +21,18 @@ int arena_arg_no_arena(void *ctx)
 	return 0;
 }
 
+SEC("syscall")
+__arch_x86_64
+__arch_arm64
+__description("arena stack: no arena")
+__failure __msg("R12 can only be used in a program that has an associated arena")
+__naked void r12_no_arena(void)
+{
+	asm volatile (
+	".8byte 0xc1bf;"	/* r1 = r12 */
+	"r0 = 0;"
+	"exit;"
+	::: __clobber_all);
+}
+
 char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/arena_stack.c new file +334 -0

334 lines not on this page · raw message

tools/testing/selftests/bpf/progs/arena_stack_fentry.c new file +13 -0
new file mode 100644
index 000000000000..4dd870c51166
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+SEC("fentry")
+int BPF_PROG(on_entry)
+{
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/arena_stack_grow.c new file +151 -0

151 lines not on this page · raw message

tools/testing/selftests/bpf/progs/arena_stack_loop.c new file +77 -0

77 lines not on this page · raw message

tools/testing/selftests/bpf/progs/arena_stack_nomem.c new file +41 -0

41 lines not on this page · raw message

tools/testing/selftests/bpf/progs/arena_stack_throw.c new file +110 -0

110 lines not on this page · raw message

tools/testing/selftests/bpf/progs/pro_epilogue_arena_stack.c new file +199 -0

199 lines not on this page · raw message

tools/testing/selftests/bpf/progs/struct_ops_refcounted_fail__arena_stack.c new file +61 -0

61 lines not on this page · raw message

tools/testing/selftests/bpf/progs/verifier_arena_stack.c new file +1169 -0

1,169 lines not on this page · raw message

Signature · 1 line
2.55.0
#26 lore raw

Re: [PATCH bpf-next v3 14/14] selftests/bpf: Add tests for BPF_REG_ARENA_SP

TL;DR: Nit in selftests: the r12 instruction macros (ADJ, LDX, ST) are duplicated across several arena_stack test files, some hand-encoded. Suggests moving them to one shared header.
Quoted 1 line

selftests/bpf: Add tests for BPF_REG_ARENA_SP

TL;DR: Nit in selftests: the r12 instruction macros (ADJ, LDX, ST) are duplicated across several arena_stack test files, some hand-encoded. Suggests moving them to one shared header.

Quoted 5 lines
tools/testing/selftests/bpf/progs/arena_stack_grow.c new file +0 -0
new file mode 100644
index 000000000000..be64296431c3

[ ... ]

tools/testing/selftests/bpf/progs/arena_stack_grow.c

Quoted 5 lines
tools/testing/selftests/bpf/progs/arena_stack_grow.c new file +5 -0
+/* Compilers don't know r12 yet */
+#define R12 12
+#define ADJ(n)		BPF_RAW_INSN(BPF_ALU64 | BPF_ADD | BPF_K, R12, 0, 0, n)
+#define LDX(dst, off)	BPF_RAW_INSN(BPF_LDX | BPF_MEM | BPF_DW, dst, R12, off, 0)
+#define ST(off, imm)	BPF_RAW_INSN(BPF_ST | BPF_MEM | BPF_DW, R12, 0, off, imm)

This isn't a bug, but this block of r12 insn macros (R12, ADJ, LDX, ST) is the same as the one in arena_stack.c, while arena_stack_loop.c and arena_stack_nomem.c write the same encodings out by hand, e.g. __imm_insn(adj, BPF_RAW_INSN(BPF_ALU64 | BPF_ADD | BPF_K, 12, 0, 0, 16)) with a "r12 += 16" comment. verifier_arena_stack.c defines a third, size-taking variant.

Could these macros live in one shared place, for example bpf_misc.h or a small arena-stack header, so that arena_stack_loop.c and arena_stack_nomem.c can use ADJ(16) like their neighbours, and the encodings can be dropped in one spot once compilers emit r12?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41755299448

#27 lore raw

Re: [PATCH bpf-next v3 14/14] selftests/bpf: Add tests for BPF_REG_ARENA_SP

Quoted 3 lines · On Tue, Nov 03, 2026 at 06:12 PM [email protected] <[email protected]>…

TL;DR: Nit in selftests: the r12 instruction macros (ADJ, LDX, ST) are duplicated across several arena_stack test files, some hand-encoded. Suggests moving them to one shared header.

Not worth a respin.
The macros are temporary. They go away when compilers know r12.

#28 lore raw
In reply to Alexei Starovoitov
Quoted 2 lines · Alexei Starovoitov

No released compiler emits this code. The change to the BPF backend of LLVM is in review: https://github.com/llvm/llvm-project/pull/235577

The flags are made for Rust. I tried them on C.
C without __arena works. The kernel side of this series is enough for it. The LLVM side needs more work.

-- libarena without __arena --

'#define __arena' is empty. The compiler moves the globals, the heap and the locals to arena.

63 libarena progs out of 63 pass the verifier.

The price against libarena with __arena: two times more casts, JITed code +18%, arena_malloc() +7%.

So probably not a recommended approach, but works for "lazy" C developers.

#29 lore raw
Quoted 13 lines · Alexei Starovoitov
Quoted 2 lines · Alexei Starovoitov

No released compiler emits this code. The change to the BPF backend of LLVM is in review: https://github.com/llvm/llvm-project/pull/235577

The flags are made for Rust. I tried them on C.
C without __arena works. The kernel side of this series is enough for it. The LLVM side needs more work.

-- libarena without __arena --

'#define __arena' is empty. The compiler moves the globals, the heap and the locals to arena.

Quoted 6 lines

63 libarena progs out of 63 pass the verifier.

The price against libarena with __arena: two times more casts, JITed code +18%, arena_malloc() +7%.

So probably not a recommended approach, but works for "lazy" C developers.

Could you elaborate on this? Are the extra casts due to the fact that globals, heaps, and locals on the stack are moving to arena memory? My understanding is that 1-1, the compiler approach is still more efficient in terms of the casts emitted (and 32-bit narrowing) vs BPF_F_ARENA_SCALAR.

#30 lore raw
Quoted 5 lines · Kumar Kartikeya Dwivedi
Quoted 2 lines

The price against libarena with __arena: two times more casts, JITed code +18%, arena_malloc() +7%.

Could you elaborate on this? Are the extra casts due to the fact that globals, heaps, and locals on the stack are moving to arena memory?

exactly.

Quoted 3 lines

My understanding is
that 1-1, the compiler approach is still more efficient in terms of the casts emitted (and 32-bit narrowing) vs BPF_F_ARENA_SCALAR.

Right. llvm adding casts is a clear win over BPF_F_ARENA_SCALAR.

#31 lore raw
In reply to Alexei Starovoitov
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Wed, 4 Nov 2026 01:23:46 +0000 you wrote:

From: Alexei Starovoitov <[email protected]>

The goal is to load Rust programs as rustc and LLVM compile them. The test programs are in https://github.com/4ast/rust-bpf

This is part 2 of "bpf: Stack in arena and a verifier that joins states" [1], as a series of its own. Part 1 is merged. This series does not depend on part 3, and it does not change the main pass of the verifier.

[...]

Here is the summary with links:
- [bpf-next,v3,01/14] libbpf: Make arena 4 Gbyte when max_entries is not set

    https://git.kernel.org/bpf/bpf-next/c/a3405322d2f2
  - [bpf-next,v3,02/14] selftests/bpf: Test the default size of arena
    https://git.kernel.org/bpf/bpf-next/c/e7ef59089c74
  - [bpf-next,v3,03/14] bpf: Treat addr_space_cast of a pointer that is not in arena as a move
    https://git.kernel.org/bpf/bpf-next/c/8adaa3791632
  - [bpf-next,v3,04/14] selftests/bpf: Test addr_space_cast of pointers that are not in arena
    https://git.kernel.org/bpf/bpf-next/c/6a52d60c0932
  - [bpf-next,v3,05/14] libbpf: Support .bss.addr_space.1 section
    https://git.kernel.org/bpf/bpf-next/c/f9420eb3626e
  - [bpf-next,v3,06/14] libbpf: Relocate pointers that are stored in arena
    https://git.kernel.org/bpf/bpf-next/c/32d56ccb1d30
  - [bpf-next,v3,07/14] selftests/bpf: Test pointers in arena and .bss.addr_space.1
    https://git.kernel.org/bpf/bpf-next/c/66c4a3b9bf82
  - [bpf-next,v3,08/14] bpf: Introduce bpf register BPF_REG_ARENA_SP
    https://git.kernel.org/bpf/bpf-next/c/51a641232390
  - [bpf-next,v3,09/14] bpf: Add stacks for programs to arena
    https://git.kernel.org/bpf/bpf-next/c/df664dbd6e98
  - [bpf-next,v3,10/14] bpf, x86: Add BPF_ATOMIC_LOCAL
    https://git.kernel.org/bpf/bpf-next/c/f02597ebe9dc
  - [bpf-next,v3,11/14] bpf: Support BPF_REG_ARENA_SP in the verifier
    https://git.kernel.org/bpf/bpf-next/c/c3c27a5b489b
  - [bpf-next,v3,12/14] bpf, x86: Support BPF_REG_ARENA_SP
    https://git.kernel.org/bpf/bpf-next/c/1298d494a71d
  - [bpf-next,v3,13/14] bpf, arm64: Support BPF_REG_ARENA_SP
    https://git.kernel.org/bpf/bpf-next/c/9eb00cb851d2
  - [bpf-next,v3,14/14] selftests/bpf: Add tests for BPF_REG_ARENA_SP
    https://git.kernel.org/bpf/bpf-next/c/cf8e78c1073e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html