Skip to contentSkip to conversation

Introduce arena library and runtime

PATCH Superseded Emil Tsalapatis 7 participants 34 messages Apr 12 – Apr 17

Subsystems (4)

v7 of 9 Superseded 1 of 9 applied Latest is v9 →

lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

v7 cover Introduce arena library and runtime

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

The patchset has the following stucture:

1-2: Addresses a verification failure triggered by SCALAR += PTR_TO_ARENA instructions.

3-4: Minor changes to selftest headers to prepare for the introduction of libarena.

5-7: Add the libarena directory and testing scaffolding, and introduce the ASAN runtime.

8-9: Add the new buddy memory allocator along with self-contained and prog-tests-based selftests.

HISTORY
=======

v6->v7 (https://lore.kernel.org/bpf/[email protected])
- Modify patch 1 to allow operations between PTR_TO_ARENA src_reg and dst_reg of any type. Adjust selftests accordingly (Alexei)
- Remove unnecessary include in patch 5 (Song)
- Removed unused definitions/assignments in patches 8/9, update patch descriptions

v5->v6 (https://lore.kernel.org/bpf/[email protected])
- Fix subreg_def management for SCALAR += PTR_TO_ARENA operations (AI)
- Add more selftests for the SCALAR += PTR_TO_ARENA patch (Sashiko)
- Adjust fls() operation to be in line with the kernel version (Sashiko)
- Address Sashiko selftests and debugging nits
- Add ASAN loadN and storeN _noabort variants and associated BTF anchor
- Remove unnecessary bit freeing of buddies during block splitting

v4->v5 (https://lore.kernel.org/bpf/[email protected])
Omitting various nits and fixups.
- Properly adjust subreg_def for scalar += ptr_to_arena calls (Sashiko)
- Remove extraneous definition from prog_tests/arena_spin_lock.c (Song)
- Trim extraneous comments from ASAN and buddy (Alexei)
- Remove asan_dummy call and replace with function pointer array (Alexei)
- Remove usersapi.h header and merge it into common.h (Alexei)
- Replace ASAN macros with function calls (Alexei)
- Embed buddy lock into the struct and move the buddy allocator to __arena_global (Alexei)
- Add commenting for buddy allocator constants (Alexei)
- Add default buddy allocator directly in common.bpf.c, so that the user does not need to define it.
- Expand test harnesses to dynamically find individual selftests. Now the selftests also reports each test individually (e.g., 5 entries for the buddy allocator instead of 1). This brings them to par with the rest of the test_progs.

v3->v4 (https://lore.kernel.org/bpf/[email protected])
- Add Acks by Song to patches 1-4.
- Expand the verifier's handling of scalar/arena operations to include all 3-operand operations in Patch 1 (Alexei)
- Add additional tests for arena/arena (allowed) and arena/pointer (not allowed) operations in Patch 2
- Remove ASAN version of the library from default compilation since it requires LLVM 22 and up (CI)
- Rework buddy allocator locking for clarity and add comments
- Fix From: email to be consistent with SOB
- Address (most) Sashiko comments

v2->v3 (https://lore.kernel.org/bpf/[email protected])
Nonexhaustive due to significant patch rework.
- Do not duplicate WRITE_ONCE macro (Mykyta, Kumar)
- Add SPDX headers (Alexei)
- Remove bump/stack allocators (Alexei)
- Integrate testing with test_progs (Kumar)
- Add short description of ASAN algorithm at the top of the file (Alexei)

v1->v2 (https://lore.kernel.org/bpf/[email protected]/)

- Added missing format string argument (AI)
- Fix outdated selftests prog name check (AI)
- Fixed stack allocation check for segment creation (AI)
- Fix errors in non-ASAN bump allocator selftests (AI)
- Propagate error value from individual selftests in selftest.c
- Removed embedded metadata from bump allocator as it was needlessly complicating its behavior

Emil Tsalapatis (9):
bpf: Allow instructions with arena source and non-arena dest registers selftests/bpf: Add tests for non-arena/arena operations selftests/bpf: Move bpf_arena_spin_lock.h to the top level selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h selftests/bpf: Add basic libarena scaffolding selftests/bpf: Add arena ASAN runtime to libarena selftests/bpf: Add ASAN support for libarena selftests selftests/bpf: Add buddy allocator for libarena selftests/bpf: Add selftests for libarena buddy allocator

 kernel/bpf/verifier.c                         |  14 +-
 tools/testing/selftests/bpf/.gitignore        |   2 +
 tools/testing/selftests/bpf/Makefile          |  28 +-
 .../testing/selftests/bpf/bpf_arena_common.h  |   4 -
 .../bpf/{progs => }/bpf_arena_spin_lock.h     |   4 +-
 tools/testing/selftests/bpf/bpf_atomic.h      |   4 -
 .../testing/selftests/bpf/bpf_experimental.h  |   3 +
 tools/testing/selftests/bpf/libarena/Makefile |  77 ++
 .../selftests/bpf/libarena/include/asan.h     | 118 +++
 .../selftests/bpf/libarena/include/buddy.h    |  92 ++
 .../selftests/bpf/libarena/include/common.h   |  67 ++
 .../bpf/libarena/include/selftest_helpers.h   | 121 +++
 .../bpf/libarena/selftests/selftest.c         | 188 ++++
 .../libarena/selftests/st_asan_buddy.bpf.c    | 241 +++++
 .../bpf/libarena/selftests/st_asan_common.h   |  47 +
 .../bpf/libarena/selftests/st_buddy.bpf.c     | 208 ++++
 .../selftests/bpf/libarena/src/asan.bpf.c     | 559 +++++++++++
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 903 ++++++++++++++++++
 .../selftests/bpf/libarena/src/common.bpf.c   |  74 ++
 .../bpf/prog_tests/arena_spin_lock.c          |   7 -
 .../selftests/bpf/prog_tests/libarena.c       |  62 ++
 .../selftests/bpf/progs/arena_spin_lock.c     |   2 +-
 .../selftests/bpf/progs/verifier_arena.c      | 130 +++
 23 files changed, 2934 insertions(+), 21 deletions(-)
 rename tools/testing/selftests/bpf/{progs => }/bpf_arena_spin_lock.h (99%)
 create mode 100644 tools/testing/selftests/bpf/libarena/Makefile
 create mode 100644 tools/testing/selftests/bpf/libarena/include/asan.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/buddy.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/common.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/selftest_helpers.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_common.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/asan.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/common.bpf.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/libarena.c
Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH v7 1/9 bpf: Allow instructions with arena source and non-arena dest registers

The compiler sometimes stores the result of a PTR_TO_ARENA and SCALAR operation into the scalar register rather than the pointer register. Relax the verifier to allow operations between a source arena register and a destination non-arena register, marking the destination's value as a PTR_TO_ARENA.

Fixes 6082b6c328b5 ("bpf: Recognize addr_space_cast instruction in the verifier.")
---
 kernel/bpf/verifier.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +12 -2
index 9c1135d373e2..9280b3aaae46 100644
@@ -16655,11 +16655,21 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
 	int err;
 
 	dst_reg = &regs[insn->dst_reg];
-	src_reg = NULL;
+	if (BPF_SRC(insn->code) == BPF_X)
+		src_reg = &regs[insn->src_reg];
+	else
+		src_reg = NULL;
 
-	if (dst_reg->type == PTR_TO_ARENA) {
+	/* Case where at least one operand is an arena. */
+	if (dst_reg->type == PTR_TO_ARENA || (src_reg && src_reg->type == PTR_TO_ARENA)) {
 		struct bpf_insn_aux_data *aux = cur_aux(env);
 
+		/* If dst_reg wasn't PTR_TO_ARENA, it is now. */
+		if (dst_reg->type != PTR_TO_ARENA)
+			*dst_reg = *src_reg;
+
+		dst_reg->subreg_def = env->insn_idx + 1;
+
 		if (BPF_CLASS(insn->code) == BPF_ALU64)
 			/*
 			 * 32-bit operations zero upper bits automatically.
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH bpf-next v7 1/9] bpf: Allow instructions with arena source and non-arena dest registers

Quoted 19 lines · Emil Tsalapatis

The compiler sometimes stores the result of a PTR_TO_ARENA and SCALAR operation into the scalar register rather than the pointer register. Relax the verifier to allow operations between a source arena register and a destination non-arena register, marking the destination's value as a PTR_TO_ARENA.

Fixes 6082b6c328b5 ("bpf: Recognize addr_space_cast instruction in the verifier.")
---
 kernel/bpf/verifier.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -0
index 9c1135d373e2..9280b3aaae46 100644

kernel/bpf/verifier.c static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, · line 16659

Quoted 6 lines
kernel/bpf/verifier.c +2 -1
@@ -16655,11 +16655,21 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
        int err;
 
        dst_reg = &regs[insn->dst_reg];
-       src_reg = NULL;
+       if (BPF_SRC(insn->code) == BPF_X)
+               src_reg = &regs[insn->src_reg];

removal of that re-assignment few lines below got lost. I removed it manually and applied the first 2 patches.

author reviewer
#3 lore raw

PATCH v7 2/9 selftests/bpf: Add tests for non-arena/arena operations

In reply to Emil Tsalapatis

Add a selftest that ensures instructions with arena source and non-arena destination registers are accepted by the verifier.

---
 .../selftests/bpf/progs/verifier_arena.c      | 130 ++++++++++++++++++
 1 file changed, 130 insertions(+)
tools/testing/selftests/bpf/progs/verifier_arena.c +130 -0
index c4b8daac4388..62e282f4448a 100644
@@ -477,4 +477,134 @@ int arena_kfuncs_under_bpf_lock(void *ctx)
 
 	return 0;
 }
+
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+
+/*
+ * Test that scalar += PTR_TO_ARENA correctly upgrades the
+ * destination register to a PTR_TO_ARENA.
+ */
+SEC("syscall")
+__success __retval(0)
+int scalar_add_arena_ptr(void *ctx)
+{
+	int __arena *scalar, *arena_ptr;
+
+	volatile char __arena *base = arena_base(&arena);
+
+	asm volatile (
+		"%[arena_ptr] = 8192;"
+		"%[arena_ptr] = addr_space_cast(%[arena_ptr], 0x0, 0x1);"
+		"%[scalar] = 12;"
+		"%[scalar] += %[arena_ptr];"
+		: [scalar] "=r"(scalar),
+		  [arena_ptr] "=&r"(arena_ptr)
+		: "r"(base)
+		:
+	);
+	return 0;
+}
+
+/*
+ * Tests that PTR_TO_ARENA + PTR_TO_ARENA is allowed.
+ */
+SEC("syscall")
+__success __retval(0)
+int arena_ptr_add_arena_ptr(void *ctx)
+{
+	int __arena *arena_ptr2, *arena_ptr1;
+
+	/* Needed for the verifier to link the arena to the subprog. */
+	volatile char __arena *base = arena_base(&arena);
+
+	asm volatile (
+		"%[arena_ptr1] = 8192;"
+		"%[arena_ptr1] = addr_space_cast(%[arena_ptr1], 0x0, 0x1);"
+		"%[arena_ptr2] = 4096;"
+		"%[arena_ptr2] = addr_space_cast(%[arena_ptr2], 0x0, 0x1);"
+		"%[arena_ptr2] += %[arena_ptr1];"
+		: [arena_ptr2] "=r"(arena_ptr2),
+		  [arena_ptr1] "=&r"(arena_ptr1)
+		: "r"(base)
+		:
+	);
+	return 0;
+}
+
+SEC("syscall")
+__success __retval(0)
+int scalar_xor_arena_ptr(void *ctx)
+{
+	int __arena *scalar, *arena_ptr;
+
+	volatile char __arena *base = arena_base(&arena);
+
+	asm volatile (
+		"%[arena_ptr] = 8192;"
+		"%[arena_ptr] = addr_space_cast(%[arena_ptr], 0x0, 0x1);"
+		"%[scalar] = 12;"
+		"%[scalar] ^= %[arena_ptr];"
+		: [scalar] "=r"(scalar),
+		  [arena_ptr] "=&r"(arena_ptr)
+		: "r"(base)
+		:
+	);
+	return 0;
+}
+
+/*
+ * Tests that PTR_TO_ARENA and non-arena pointers can be added.
+ */
+SEC("syscall")
+__success __retval(0)
+int arena_ptr_add_to_non_arena_ptr(void *ctx)
+{
+	register int __arena *arena_ptr asm("r3");
+	register void *dst asm("r4");
+
+	volatile char __arena *base = arena_base(&arena);
+
+	asm volatile (
+		"%[arena_ptr] = 8192;"
+		"%[arena_ptr] = addr_space_cast(%[arena_ptr], 0x0, 0x1);"
+		"%[dst] = %[ctx];"
+		"%[dst] += %[arena_ptr];"
+		: [arena_ptr] "=&r"(arena_ptr),
+		  [dst] "=&r"(dst)
+		: [ctx] "r"(ctx), "r"(base)
+		:
+	);
+
+	(void)ctx;
+
+	return 0;
+}
+
+SEC("syscall")
+__success __retval(0)
+int non_arena_ptr_add_to_arena_ptr(void *ctx)
+{
+	register int __arena *arena_ptr asm("r3");
+	register void *src asm("r4");
+
+	volatile char __arena *base = arena_base(&arena);
+
+	asm volatile (
+		"%[arena_ptr] = 8192;"
+		"%[arena_ptr] = addr_space_cast(%[arena_ptr], 0x0, 0x1);"
+		"%[src] = %[ctx];"
+		"%[arena_ptr] += %[src];"
+		: [arena_ptr] "=&r"(arena_ptr),
+		  [src] "=&r"(src)
+		: [ctx] "r"(ctx), "r"(base)
+		:
+	);
+
+	(void)ctx;
+
+	return 0;
+}
+
+#endif
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.53.0
author reviewer
#4 lore raw

PATCH v7 3/9 selftests/bpf: Move bpf_arena_spin_lock.h to the top level

In reply to Emil Tsalapatis

The bpf_arena_spin_lock.h header is useful for all programs and not just the selftests. Move it to the top level of the BPF selftests to make it more readily accessible.

---
 .../selftests/bpf/{progs => }/bpf_arena_spin_lock.h        | 4 ++--
 tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c   | 7 -------
 tools/testing/selftests/bpf/progs/arena_spin_lock.c        | 2 +-
 3 files changed, 3 insertions(+), 10 deletions(-)
 rename tools/testing/selftests/bpf/{progs => }/bpf_arena_spin_lock.h (99%)
tools/testing/selftests/bpf/bpf_arena_spin_lock.h renamed from tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h +2 -2
similarity index 99%
rename from tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h
rename to tools/testing/selftests/bpf/bpf_arena_spin_lock.h
index f90531cf3ee5..680c9e6cb35d 100644
@@ -107,7 +107,7 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL		(1U << _Q_PENDING_OFFSET)
 
-struct arena_qnode __arena qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
+struct arena_qnode __weak __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
 static inline u32 encode_tail(int cpu, int idx)
 {
@@ -240,7 +240,7 @@ static __always_inline int arena_spin_trylock(arena_spinlock_t __arena *lock)
 	return likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL));
 }
 
-__noinline
+__noinline __weak
 int arena_spin_lock_slowpath(arena_spinlock_t __arena __arg_arena *lock, u32 val)
 {
 	struct arena_mcs_spinlock __arena *prev, *next, *node0, *node;
tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c +0 -7
index 693fd86fbde6..acb9d53b5973 100644
@@ -5,13 +5,6 @@
 #include <sys/sysinfo.h>
 
 struct __qspinlock { int val; };
-typedef struct __qspinlock arena_spinlock_t;
-
-struct arena_qnode {
-	unsigned long next;
-	int count;
-	int locked;
-};
 
 #include "arena_spin_lock.skel.h"
 
tools/testing/selftests/bpf/progs/arena_spin_lock.c +1 -1
index 086b57a426cf..6c04e7707644 100644
@@ -4,7 +4,7 @@
 #include <bpf/bpf_tracing.h>
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
-#include "bpf_arena_spin_lock.h"
+#include "../bpf_arena_spin_lock.h"
 
 struct {
 	__uint(type, BPF_MAP_TYPE_ARENA);
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH bpf-next v7 3/9] selftests/bpf: Move bpf_arena_spin_lock.h to the top level

Quoted 5 lines · Emil Tsalapatis

The bpf_arena_spin_lock.h header is useful for all programs and not just the selftests. Move it to the top level of the BPF selftests to make it more readily accessible.

Please add something to the log to explain why __weak is needed, as
indicated in https://lore.kernel.org/bpf/CAP01T77GwHgS04TM8yTODyiXh2VJq-=L0FPoRvjpfF4dc6T9zw@mail.gmail.com.

Also, since we discussed already, why not move this to libarena?

Quoted 69 lines
---
 .../selftests/bpf/{progs => }/bpf_arena_spin_lock.h        | 4 ++--
 tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c   | 7 -------
 tools/testing/selftests/bpf/progs/arena_spin_lock.c        | 2 +-
 3 files changed, 3 insertions(+), 10 deletions(-)
 rename tools/testing/selftests/bpf/{progs => }/bpf_arena_spin_lock.h (99%)
tools/testing/selftests/bpf/bpf_arena_spin_lock.h renamed from tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h +2 -2
similarity index 99%
rename from tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h
rename to tools/testing/selftests/bpf/bpf_arena_spin_lock.h
index f90531cf3ee5..680c9e6cb35d 100644
@@ -107,7 +107,7 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL          (1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL         (1U << _Q_PENDING_OFFSET)
 
-struct arena_qnode __arena qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
+struct arena_qnode __weak __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
 static inline u32 encode_tail(int cpu, int idx)
 {
@@ -240,7 +240,7 @@ static __always_inline int arena_spin_trylock(arena_spinlock_t __arena *lock)
        return likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL));
 }
 
-__noinline
+__noinline __weak
 int arena_spin_lock_slowpath(arena_spinlock_t __arena __arg_arena *lock, u32 val)
 {
        struct arena_mcs_spinlock __arena *prev, *next, *node0, *node;
tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c +0 -7
index 693fd86fbde6..acb9d53b5973 100644
@@ -5,13 +5,6 @@
 #include <sys/sysinfo.h>
 
 struct __qspinlock { int val; };
-typedef struct __qspinlock arena_spinlock_t;
-
-struct arena_qnode {
-       unsigned long next;
-       int count;
-       int locked;
-};
 
 #include "arena_spin_lock.skel.h"
 
tools/testing/selftests/bpf/progs/arena_spin_lock.c +1 -2
index 086b57a426cf..6c04e7707644 100644
@@ -4,7 +4,7 @@
 #include <bpf/bpf_tracing.h>
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
-#include "bpf_arena_spin_lock.h"
+#include "../bpf_arena_spin_lock.h"
 
 struct {
        __uint(type, BPF_MAP_TYPE_ARENA);
--

2.53.0

author reviewer
#6 lore raw

Re: [PATCH bpf-next v7 3/9] selftests/bpf: Move bpf_arena_spin_lock.h to the top level

Quoted 9 lines · Kumar Kartikeya Dwivedi
Quoted 5 lines · Emil Tsalapatis

The bpf_arena_spin_lock.h header is useful for all programs and not just the selftests. Move it to the top level of the BPF selftests to make it more readily accessible.

Please add something to the log to explain why __weak is needed, as
indicated in https://lore.kernel.org/bpf/CAP01T77GwHgS04TM8yTODyiXh2VJq-=L0FPoRvjpfF4dc6T9zw@mail.gmail.com.

Will do.

Quoted 3 lines

Also, since we discussed already, why not move this to libarena?

I left it as (imminent) followup work to make libarena completely self-contained but it makes sense to add it from the very beginning. I'll add move the headers in these patches.

Quoted 69 lines
Quoted 69 lines
---
 .../selftests/bpf/{progs => }/bpf_arena_spin_lock.h        | 4 ++--
 tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c   | 7 -------
 tools/testing/selftests/bpf/progs/arena_spin_lock.c        | 2 +-
 3 files changed, 3 insertions(+), 10 deletions(-)
 rename tools/testing/selftests/bpf/{progs => }/bpf_arena_spin_lock.h (99%)
tools/testing/selftests/bpf/bpf_arena_spin_lock.h renamed from tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h +2 -2
similarity index 99%
rename from tools/testing/selftests/bpf/progs/bpf_arena_spin_lock.h
rename to tools/testing/selftests/bpf/bpf_arena_spin_lock.h
index f90531cf3ee5..680c9e6cb35d 100644
@@ -107,7 +107,7 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL          (1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL         (1U << _Q_PENDING_OFFSET)
 
-struct arena_qnode __arena qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
+struct arena_qnode __weak __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
 static inline u32 encode_tail(int cpu, int idx)
 {
@@ -240,7 +240,7 @@ static __always_inline int arena_spin_trylock(arena_spinlock_t __arena *lock)
        return likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL));
 }
 
-__noinline
+__noinline __weak
 int arena_spin_lock_slowpath(arena_spinlock_t __arena __arg_arena *lock, u32 val)
 {
        struct arena_mcs_spinlock __arena *prev, *next, *node0, *node;
tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c +0 -7
index 693fd86fbde6..acb9d53b5973 100644
@@ -5,13 +5,6 @@
 #include <sys/sysinfo.h>
 
 struct __qspinlock { int val; };
-typedef struct __qspinlock arena_spinlock_t;
-
-struct arena_qnode {
-       unsigned long next;
-       int count;
-       int locked;
-};
 
 #include "arena_spin_lock.skel.h"
 
tools/testing/selftests/bpf/progs/arena_spin_lock.c +1 -2
index 086b57a426cf..6c04e7707644 100644
@@ -4,7 +4,7 @@
 #include <bpf/bpf_tracing.h>
 #include <bpf/bpf_helpers.h>
 #include "bpf_misc.h"
-#include "bpf_arena_spin_lock.h"
+#include "../bpf_arena_spin_lock.h"
 
 struct {
        __uint(type, BPF_MAP_TYPE_ARENA);
--

2.53.0

author reviewer
#7 lore raw

PATCH v7 4/9 selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h

In reply to Emil Tsalapatis

The WRITE_ONCE macro is identically defined both in bpf_atomic.h and in bpf_arena_common.h. Deduplicate the definitions by moving the macro to the commonly used bpf_experimental.h header. Also move the READ_ONCE macro to the same file for uniformity.

---
 tools/testing/selftests/bpf/bpf_arena_common.h | 4 ----
 tools/testing/selftests/bpf/bpf_atomic.h       | 4 ----
 tools/testing/selftests/bpf/bpf_experimental.h | 3 +++
 3 files changed, 3 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/bpf_arena_common.h +0 -4
index 16f8ce832004..a0503ecbc315 100644
@@ -2,10 +2,6 @@
 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
 #pragma once
 
-#ifndef WRITE_ONCE
-#define WRITE_ONCE(x, val) ((*(volatile typeof(x) *) &(x)) = (val))
-#endif
-
 #ifndef NUMA_NO_NODE
 #define	NUMA_NO_NODE	(-1)
 #endif
tools/testing/selftests/bpf/bpf_atomic.h +0 -4
index c550e5711967..d77338ac7e6b 100644
@@ -40,10 +40,6 @@ extern bool CONFIG_X86_64 __kconfig __weak;
 /* No-op for BPF */
 #define cpu_relax() ({})
 
-#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
-
-#define WRITE_ONCE(x, val) ((*(volatile typeof(x) *)&(x)) = (val))
-
 #define cmpxchg(p, old, new) __sync_val_compare_and_swap((p), old, new)
 
 #define try_cmpxchg(p, pold, new)                                 \
tools/testing/selftests/bpf/bpf_experimental.h +3 -0
index 2234bd6bc9d3..56214f7051c6 100644
@@ -11,6 +11,9 @@
 /* Convenience macro to wrap over bpf_obj_new */
 #define bpf_obj_new(type) ((type *)bpf_obj_new(bpf_core_type_id_local(type)))
 
+#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
+#define WRITE_ONCE(x, val) ((*(volatile typeof(x) *)&(x)) = (val))
+
 /* Convenience macro to wrap over bpf_percpu_obj_new */
 #define bpf_percpu_obj_new(type) ((type __percpu_kptr *)bpf_percpu_obj_new(bpf_core_type_id_local(type)))
 
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH bpf-next v7 4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h

Quoted 6 lines · Emil Tsalapatis

The WRITE_ONCE macro is identically defined both in bpf_atomic.h and in bpf_arena_common.h. Deduplicate the definitions by moving the macro to the commonly used bpf_experimental.h header. Also move the READ_ONCE macro to the same file for uniformity.

The right header for these is bpf_atomic.h, not bpf_experimental.h. We want volatile loads to be shipped with bpf_atomic.h and all other atomic primitives.
https://lore.kernel.org/bpf/CAP01T754eOs2DCjMmkK=CppEwfk5EX84tc70ZqS0_OxTfgxE=w@mail.gmail.com

Quoted 4 lines

---
[...]

author reviewer
#9 lore raw

Re: [PATCH bpf-next v7 4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h

Quoted 8 lines · Kumar Kartikeya Dwivedi
Quoted 6 lines · Emil Tsalapatis

The WRITE_ONCE macro is identically defined both in bpf_atomic.h and in bpf_arena_common.h. Deduplicate the definitions by moving the macro to the commonly used bpf_experimental.h header. Also move the READ_ONCE macro to the same file for uniformity.

Quoted 4 lines

The right header for these is bpf_atomic.h, not bpf_experimental.h. We want volatile loads to be shipped with bpf_atomic.h and all other atomic primitives.
https://lore.kernel.org/bpf/CAP01T754eOs2DCjMmkK=CppEwfk5EX84tc70ZqS0_OxTfgxE=w@mail.gmail.com

bpf_experimental.h is included in bpf_atomic.h, so there's no possible breakage there. A bunch of arena-related code like bpf_arena_list.h also uses WRITE_ONCE but doesn't include bpf_atomic.h, so we'd have to manually add an extra headers for no reason. Even if we do that, bpf_atomic.h includes vmlinux.h and so any header like bpf_arena_list.h I mentioned that requires WRITE_ONCE won't be importable by userspace.

Besides, WRITE_ONCE/READ_ONCE are ultimately compiler shorthands even if they are used most heavily in atomics and don't stick out in bpf_experimental.h.

Quoted 5 lines
Quoted 5 lines

---
[...]

#10 lore raw

Re: [PATCH bpf-next v7 4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h

Quoted 22 lines · Emil Tsalapatis
Quoted 8 lines · Kumar Kartikeya Dwivedi
Quoted 6 lines · Emil Tsalapatis

The WRITE_ONCE macro is identically defined both in bpf_atomic.h and in bpf_arena_common.h. Deduplicate the definitions by moving the macro to the commonly used bpf_experimental.h header. Also move the READ_ONCE macro to the same file for uniformity.

Quoted 4 lines

The right header for these is bpf_atomic.h, not bpf_experimental.h. We want volatile loads to be shipped with bpf_atomic.h and all other atomic primitives.
https://lore.kernel.org/bpf/CAP01T754eOs2DCjMmkK=CppEwfk5EX84tc70ZqS0_OxTfgxE=w@mail.gmail.com

bpf_experimental.h is included in bpf_atomic.h, so there's no possible breakage there. A bunch of arena-related code like bpf_arena_list.h also uses WRITE_ONCE but doesn't include bpf_atomic.h, so we'd have to manually add an extra headers for no reason. Even if we do that, bpf_atomic.h includes vmlinux.h and so any header like bpf_arena_list.h I mentioned that requires WRITE_ONCE won't be importable by userspace.

Quoted 4 lines

Besides, WRITE_ONCE/READ_ONCE are ultimately compiler shorthands even if they are used most heavily in atomics and don't stick out in bpf_experimental.h.

It's a sad situation indeed. It just feels odd not to provide the equivalent of relaxed loads in the header encapsulating all atomic access primitives.
bpf_experimental.h is a kitchen sink header for everything we need to include that doesn't get its own definition somewhere.
I think trying to be consistent and deliberating on having topical headers for a library you want to export is a good exercise to have right now, while we're at it.

It might be cleaner in the end to just place ifndef around all of the macro definitions in bpf_atomic.h. That's probably good hygiene too, since I presume the naming is universal and programs themselves may have their own versions, etc.

Maybe others have better opinions. I would have suggested namespacing everything with arena_, but then they work on non-arena just as well, so that doesn't make too much sense.

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

---
[...]

author reviewer
#11 lore raw

Re: [PATCH bpf-next v7 4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h

Quoted 23 lines · Kumar Kartikeya Dwivedi
Quoted 22 lines · Emil Tsalapatis
Quoted 8 lines · Kumar Kartikeya Dwivedi
Quoted 6 lines · Emil Tsalapatis

The WRITE_ONCE macro is identically defined both in bpf_atomic.h and in bpf_arena_common.h. Deduplicate the definitions by moving the macro to the commonly used bpf_experimental.h header. Also move the READ_ONCE macro to the same file for uniformity.

Quoted 4 lines

The right header for these is bpf_atomic.h, not bpf_experimental.h. We want volatile loads to be shipped with bpf_atomic.h and all other atomic primitives.
https://lore.kernel.org/bpf/CAP01T754eOs2DCjMmkK=CppEwfk5EX84tc70ZqS0_OxTfgxE=w@mail.gmail.com

bpf_experimental.h is included in bpf_atomic.h, so there's no possible breakage there. A bunch of arena-related code like bpf_arena_list.h also uses WRITE_ONCE but doesn't include bpf_atomic.h, so we'd have to manually add an extra headers for no reason. Even if we do that, bpf_atomic.h includes vmlinux.h and so any header like bpf_arena_list.h I mentioned that requires WRITE_ONCE won't be importable by userspace.

Quoted 8 lines
Quoted 4 lines

Besides, WRITE_ONCE/READ_ONCE are ultimately compiler shorthands even if they are used most heavily in atomics and don't stick out in bpf_experimental.h.

It's a sad situation indeed. It just feels odd not to provide the equivalent of relaxed loads in the header encapsulating all atomic access primitives.

I agree it's not the best, my first attempt was to move the definitions to bpf_atomic.h but that ended up being so much extra adjustments that it felt silly to do just for this macro.

Quoted 2 lines

bpf_experimental.h is a kitchen sink header for everything we need to include that doesn't get its own definition somewhere.

Maybe that is the problem? bpf_experimental.h has outgrown its original purpose, it it worth splitting into multiple headers to avoid carrying around all the compat scaffolding just to get access to a couple definitions? For example, cast_kern/cast_user and the hardcoded address cast bytecode are useless to most programs (and most arena programs since they require LLVM >= 19), but they are still included everywhere.

Quoted 23 lines

I think trying to be consistent and deliberating on having topical headers for a library you want to export is a good exercise to have right now, while we're at it.

It might be cleaner in the end to just place ifndef around all of the macro definitions in bpf_atomic.h. That's probably good hygiene too, since I presume the naming is universal and programs themselves may have their own versions, etc.

Maybe others have better opinions. I would have suggested namespacing everything with arena_, but then they work on non-arena just as well, so that doesn't make too much sense.

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

---
[...]

author reviewer
#12 lore raw

PATCH v7 5/9 selftests/bpf: Add basic libarena scaffolding

In reply to Emil Tsalapatis

Add initial code for an arena-based BPF library. The current commit introduces a test runner and Makefile for the library. Library code can be added just by including the source file in the library's src/ subdirectory. Future commits will introduce the library code itself.

The current commit also includes a standalone test runner. This is to keep libarena self-contained and testable even when copied out of the kernel tree. Subsequent commits add integration with test_progs.

---
 tools/testing/selftests/bpf/.gitignore        |   1 +
 tools/testing/selftests/bpf/Makefile          |  15 ++
 tools/testing/selftests/bpf/libarena/Makefile |  50 ++++++
 .../selftests/bpf/libarena/include/common.h   |  66 ++++++++
 .../bpf/libarena/include/selftest_helpers.h   |  86 +++++++++++
 .../bpf/libarena/selftests/selftest.c         | 146 ++++++++++++++++++
 .../selftests/bpf/libarena/src/common.bpf.c   |  62 ++++++++
 7 files changed, 426 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/Makefile
 create mode 100644 tools/testing/selftests/bpf/libarena/include/common.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/selftest_helpers.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/common.bpf.c
tools/testing/selftests/bpf/.gitignore +1 -0
index bfdc5518ecc8..7f1960d6b59e 100644
@@ -49,3 +49,4 @@ verification_cert.h
 *.BTF.base
 usdt_1
 usdt_2
+libarena/test_libarena
tools/testing/selftests/bpf/Makefile +15 -0
index f75c4f52c028..0d332c991023 100644
@@ -153,6 +153,7 @@ override define CLEAN
 	$(Q)$(RM) -r $(TEST_KMODS)
 	$(Q)$(RM) -r $(EXTRA_CLEAN)
 	$(Q)$(MAKE) -C test_kmods clean
+	$(Q)$(MAKE) -C libarena clean
 	$(Q)$(MAKE) docs-clean
 endef
 
@@ -739,6 +740,17 @@ $(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
 	 echo "};"; \
 	 echo "unsigned int test_progs_verification_cert_len = $$(wc -c < $<);") > $@
 
+LIBARENA_MAKE_ARGS = \
+		BPFTOOL="$(BPFTOOL)" \
+		INCLUDE_DIR="$(HOST_INCLUDE_DIR)" \
+		LIBBPF_INCLUDE="$(HOST_INCLUDE_DIR)" \
+		BPFOBJ="$(BPFOBJ)" \
+		LDLIBS="$(LDLIBS) -lzstd" \
+		CLANG="$(CLANG)" \
+		BPF_CFLAGS="$(BPF_CFLAGS) $(CLANG_CFLAGS)" \
+		BPF_TARGET_ENDIAN="$(BPF_TARGET_ENDIAN)" \
+		Q="$(Q)"
+
 # Define test_progs test runner.
 TRUNNER_TESTS_DIR := prog_tests
 TRUNNER_BPF_PROGS_DIR := progs
@@ -931,3 +943,6 @@ override define INSTALL_RULE
 		rsync -a $(OUTPUT)/$$DIR/*.bpf.o $(INSTALL_PATH)/$$DIR;\
 	done
 endef
+
+test_libarena: $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ)
+	+$(MAKE) -C libarena $@ $(LIBARENA_MAKE_ARGS)
tools/testing/selftests/bpf/libarena/Makefile new file +50 -0

50 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/include/common.h new file +66 -0

66 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/include/selftest_helpers.h new file +86 -0

86 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/selftests/selftest.c new file +146 -0

146 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/src/common.bpf.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.53.0
#13 lore raw

Re: [PATCH bpf-next v7 5/9] selftests/bpf: Add basic libarena scaffolding

Alexei raised this in v4 review:
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c new file +0 -0
new file mode 100644
index 000000000000..c692e20ea7b1

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/selftest.c line 27

Quoted 7 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c new file +7 -0
+#include "../libarena.skel.h"
+typedef struct libarena selftest;
+#define selftest__open libarena__open
+#define selftest__open_and_load libarena__open_and_load
+#define selftest__load libarena__load
+#define selftest__attach libarena__attach
+#define selftest__destroy libarena__destroy

Alexei raised this in v4 review:

https://lore.kernel.org/bpf/CAADnVQLCyP+NzO9-332JO+dBe=EDEOr+ax+-ZrvAqJ9yTNYyEA@mail.gmail.com/

asking about the purpose of these #defines and calling them obfuscation. These aliases don't appear to have been removed or discussed in subsequent versions.

Would it be simpler to use the libarena skeleton functions directly (libarena__open_and_load, libarena__attach, libarena__destroy, etc.) and drop the selftest typedef and alias macros?

---
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/24313021445

author reviewer
#14 lore raw

Re: [PATCH bpf-next v7 5/9] selftests/bpf: Add basic libarena scaffolding

Quoted 20 lines · bot+bpf-ci
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c new file +0 -0
new file mode 100644
index 000000000000..c692e20ea7b1

[ ... ]

tools/testing/selftests/bpf/libarena/selftests/selftest.c line 27

Quoted 7 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c new file +7 -0
+#include "../libarena.skel.h"
+typedef struct libarena selftest;
+#define selftest__open libarena__open
+#define selftest__open_and_load libarena__open_and_load
+#define selftest__load libarena__load
+#define selftest__attach libarena__attach
+#define selftest__destroy libarena__destroy

Alexei raised this in v4 review:

https://lore.kernel.org/bpf/CAADnVQLCyP+NzO9-332JO+dBe=EDEOr+ax+-ZrvAqJ9yTNYyEA@mail.gmail.com/

Quoted 8 lines

asking about the purpose of these #defines and calling them obfuscation. These aliases don't appear to have been removed or discussed in subsequent versions.

Would it be simpler to use the libarena skeleton functions directly (libarena__open_and_load, libarena__attach, libarena__destroy, etc.) and drop the selftest typedef and alias macros?

This is resolved, the renaming is useful for patch 7.

Quoted 6 lines

---
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/24313021445

author reviewer
#15 lore raw

PATCH v7 6/9 selftests/bpf: Add arena ASAN runtime to libarena

In reply to Emil Tsalapatis

Add an address sanitizer (ASAN) runtime to the arena library. The ASAN runtime implements the functions injected into BPF binaries by LLVM sanitization when ASAN is enabled during compilation.

The runtime also includes functions called explicitly by memory allocation code to mark memory as poisoned/unpoisoned to ASAN. This code is a no-op when sanitization is turned off.

---
 .../selftests/bpf/libarena/include/asan.h     | 118 ++++
 .../selftests/bpf/libarena/include/common.h   |   1 +
 .../selftests/bpf/libarena/src/asan.bpf.c     | 559 ++++++++++++++++++
 3 files changed, 678 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/include/asan.h
 create mode 100644 tools/testing/selftests/bpf/libarena/src/asan.bpf.c
tools/testing/selftests/bpf/libarena/include/asan.h new file +118 -0
new file mode 100644
index 000000000000..7ad1f6997c06
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#pragma once
+
+struct asan_init_args {
+	u64 arena_all_pages;
+	u64 arena_globals_pages;
+};
+
+int asan_init(struct asan_init_args *args);
+
+extern volatile u64 __asan_shadow_memory_dynamic_address;
+extern volatile u32 asan_reported;
+extern volatile bool asan_inited;
+extern volatile bool asan_report_once;
+extern volatile bool asan_emit_stack;
+
+#ifdef __BPF__
+
+#define ASAN_SHADOW_SHIFT 3
+#define ASAN_SHADOW_SCALE (1ULL << ASAN_SHADOW_SHIFT)
+#define ASAN_GRANULE_MASK ((1ULL << ASAN_SHADOW_SHIFT) - 1)
+#define ASAN_GRANULE(addr) ((s8)((u32)(u64)((addr)) & ASAN_GRANULE_MASK))
+
+#define __noasan __attribute__((no_sanitize("address")))
+
+#ifdef BPF_ARENA_ASAN
+
+typedef s8 __arena s8a;
+
+static inline
+s8a *mem_to_shadow(void __arena __arg_arena *addr)
+{
+	return (s8a *)(((u32)(u64)addr >> ASAN_SHADOW_SHIFT) +
+			__asan_shadow_memory_dynamic_address);
+}
+
+__weak __noasan
+bool asan_ready(void)
+{
+	return __asan_shadow_memory_dynamic_address;
+}
+
+int asan_poison(void __arena *addr, s8 val, size_t size);
+int asan_unpoison(void __arena *addr, size_t size);
+bool asan_shadow_set(void __arena *addr);
+
+/*
+ * Dummy calls to ensure the ASAN runtime's BTF information is present
+ * in every object file when compiling the runtime and local BPF code
+ * separately. The runtime calls are injected into the LLVM IR file
+ */
+#define DECLARE_ASAN_LOAD_STORE_SIZE(size)				\
+	void __asan_store##size(void *addr);				\
+	void __asan_store##size##_noabort(void *addr);	\
+	void __asan_load##size(void *addr);				\
+	void __asan_load##size##_noabort(void *addr);	\
+	void __asan_report_store##size(void *addr);			\
+	void __asan_report_store##size##_noabort(void *addr);		\
+	void __asan_report_load##size(void *addr);			\
+	void __asan_report_load##size##_noabort(void *addr);
+
+DECLARE_ASAN_LOAD_STORE_SIZE(1);
+DECLARE_ASAN_LOAD_STORE_SIZE(2);
+DECLARE_ASAN_LOAD_STORE_SIZE(4);
+DECLARE_ASAN_LOAD_STORE_SIZE(8);
+
+void __asan_storeN(void *addr, ssize_t size);
+void __asan_storeN_noabort(void *addr, ssize_t size);
+void __asan_loadN(void *addr, ssize_t size);
+void __asan_loadN_noabort(void *addr, ssize_t size);
+
+/*
+ * Force LLVM to emit BTF information for the stubs,
+ * because the ASAN pass in LLVM by itself doesn't.
+ */
+#define ASAN_LOAD_STORE_SIZE(size)		\
+	__asan_store##size,			\
+	__asan_store##size##_noabort,		\
+	__asan_load##size,			\
+	__asan_load##size##_noabort,		\
+	__asan_report_store##size,		\
+	__asan_report_store##size##_noabort,	\
+	__asan_report_load##size,		\
+	__asan_report_load##size##_noabort
+
+/*
+ * Force LLVM to generate BTF information for the ASAN function
+ * declarations without invoking them. The used attribute prevents
+ * the function pointer table from being optimized out by the
+ * compiler.
+ */
+__attribute__((used))
+static void (*__asan_btf_anchors[])(void *) = {
+	ASAN_LOAD_STORE_SIZE(1),
+	ASAN_LOAD_STORE_SIZE(2),
+	ASAN_LOAD_STORE_SIZE(4),
+	ASAN_LOAD_STORE_SIZE(8)
+};
+
+__attribute__((used))
+static void (*__asan_btf_anchors_n[])(void *, ssize_t) = {
+	__asan_storeN,
+	__asan_storeN_noabort,
+	__asan_loadN,
+	__asan_loadN_noabort
+};
+
+#else /* BPF_ARENA_ASAN */
+
+static inline int asan_poison(void __arena *addr, s8 val, size_t size) { return 0; }
+static inline int asan_unpoison(void __arena *addr, size_t size) { return 0; }
+static inline bool asan_shadow_set(void __arena *addr) { return 0; }
+__weak bool asan_ready(void) { return true; }
+
+#endif /* BPF_ARENA_ASAN */
+
+#endif /* __BPF__ */
tools/testing/selftests/bpf/libarena/include/common.h +1 -0
index f1cf34e34ae5..ccfc2bb94e59 100644
@@ -38,6 +38,7 @@ struct {
 } arena __weak SEC(".maps");
 
 extern const volatile u32 zero;
+extern volatile u64 asan_violated;
 
 int arena_fls(__u64 word);
 
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +559 -0

559 lines not on this page · raw message

Signature · 1 line
2.53.0
#16 lore raw

Re: [PATCH bpf-next v7 6/9] selftests/bpf: Add arena ASAN runtime to libarena

Alexei Starovoitov flagged this comment as unhelpful in v4 review. The validation checks below with their error messages are self-documenting. Was the intent to remove this comment or replace it…
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +0 -0
new file mode 100644
index 000000000000..fff12ff50f26

[ ... ]

Quoted 11 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +11 -0
+SEC("syscall")
+__weak __noasan int asan_init(struct asan_init_args *args)
+{
+	u64 globals_pages = args->arena_globals_pages;
+	u64 all_pages = args->arena_all_pages;
+	u64 shadow_map, shadow_pgoff;
+	u64 shadow_pages;
+
+	if (asan_inited)
+		return 0;
+

tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 507

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +10 -0
+	/*
+	 * Round up the shadow map size to the nearest page.
+	 */
+	shadow_pages = all_pages >> ASAN_SHADOW_SHIFT;
+	if ((all_pages & ((1 << ASAN_SHADOW_SHIFT) - 1)))
+		shadow_pages += 1;
+
+	/*
+	 * Make sure the numbers provided by userspace are sane.
+	 */

Alexei Starovoitov flagged this comment as unhelpful in v4 review. The validation checks below with their error messages are self-documenting. Was the intent to remove this comment or replace it with something more descriptive?

https://lore.kernel.org/bpf/CAADnVQLHFZE3AFzP3gtZKvcaX+3hbp3pCw3JLqEtpdoQ+OUf7g@mail.gmail.com/

tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 511

Quoted 4 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +4 -0
+	if (all_pages > (1ULL << 32) / __PAGE_SIZE) {
+		arena_stderr("error: arena size %lx too large", all_pages);
+		return -EINVAL;
+	}

[ ... ]

---
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/24313021445

author reviewer
#17 lore raw

Re: [PATCH bpf-next v7 6/9] selftests/bpf: Add arena ASAN runtime to libarena

Quoted 35 lines · bot+bpf-ci
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +0 -0
new file mode 100644
index 000000000000..fff12ff50f26

[ ... ]

Quoted 11 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +11 -0
+SEC("syscall")
+__weak __noasan int asan_init(struct asan_init_args *args)
+{
+	u64 globals_pages = args->arena_globals_pages;
+	u64 all_pages = args->arena_all_pages;
+	u64 shadow_map, shadow_pgoff;
+	u64 shadow_pages;
+
+	if (asan_inited)
+		return 0;
+

tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 507

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +10 -0
+	/*
+	 * Round up the shadow map size to the nearest page.
+	 */
+	shadow_pages = all_pages >> ASAN_SHADOW_SHIFT;
+	if ((all_pages & ((1 << ASAN_SHADOW_SHIFT) - 1)))
+		shadow_pages += 1;
+
+	/*
+	 * Make sure the numbers provided by userspace are sane.
+	 */

Alexei Starovoitov flagged this comment as unhelpful in v4 review. The validation checks below with their error messages are self-documenting. Was the intent to remove this comment or replace it with something more descriptive?

Quoted 1 line

https://lore.kernel.org/bpf/CAADnVQLHFZE3AFzP3gtZKvcaX+3hbp3pCw3JLqEtpdoQ+OUf7g@mail.gmail.com/

Missed this during the comment removal pass, holding off on updating for now as this is relatively minor.

Quoted 14 lines

tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 511

Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +4 -0
+	if (all_pages > (1ULL << 32) / __PAGE_SIZE) {
+		arena_stderr("error: arena size %lx too large", all_pages);
+		return -EINVAL;
+	}

[ ... ]

---
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/24313021445

author reviewer
#18 lore raw

PATCH v7 7/9 selftests/bpf: Add ASAN support for libarena selftests

In reply to Emil Tsalapatis

Expand the arena library selftest infrastructure to support address sanitization. Add the compiler flags necessary to compile the library under ASAN when supported.

---
 tools/testing/selftests/bpf/.gitignore        |  1 +
 tools/testing/selftests/bpf/Makefile          | 13 ++++-
 tools/testing/selftests/bpf/libarena/Makefile | 31 +++++++++++-
 .../bpf/libarena/include/selftest_helpers.h   | 28 +++++++++++
 .../bpf/libarena/selftests/selftest.c         | 38 ++++++++++++++-
 .../bpf/libarena/selftests/st_asan_common.h   | 47 +++++++++++++++++++
 6 files changed, 153 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_common.h
tools/testing/selftests/bpf/.gitignore +1 -0
index 7f1960d6b59e..50fa79b2daac 100644
@@ -50,3 +50,4 @@ verification_cert.h
 usdt_1
 usdt_2
 libarena/test_libarena
+libarena/test_libarena_asan
tools/testing/selftests/bpf/Makefile +12 -1
index 0d332c991023..f10e865338f6 100644
@@ -751,6 +751,13 @@ LIBARENA_MAKE_ARGS = \
 		BPF_TARGET_ENDIAN="$(BPF_TARGET_ENDIAN)" \
 		Q="$(Q)"
 
+# libarena_asan.skel.h currently requires LLVM 22. For now,
+# only integrate the non-ASAN version with test_progs.
+LIBARENA_SKEL := libarena/libarena.skel.h
+
+$(LIBARENA_SKEL): $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ)
+	+$(MAKE) -C libarena libarena.skel.h $(LIBARENA_MAKE_ARGS)
+
 # Define test_progs test runner.
 TRUNNER_TESTS_DIR := prog_tests
 TRUNNER_BPF_PROGS_DIR := progs
@@ -774,7 +781,8 @@ TRUNNER_EXTRA_SOURCES := test_progs.c		\
 			 flow_dissector_load.h	\
 			 ip_check_defrag_frags.h	\
 			 bpftool_helpers.c	\
-			 usdt_1.c usdt_2.c
+			 usdt_1.c usdt_2.c	\
+			 $(LIBARENA_SKEL)
 TRUNNER_LIB_SOURCES := find_bit.c
 TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read				\
 		       $(OUTPUT)/liburandom_read.so			\
@@ -946,3 +954,6 @@ endef
 
 test_libarena: $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ)
 	+$(MAKE) -C libarena $@ $(LIBARENA_MAKE_ARGS)
+
+test_libarena_asan: $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ)
+	+$(MAKE) -C libarena $@ $(LIBARENA_MAKE_ARGS)
tools/testing/selftests/bpf/libarena/Makefile +29 -2
index 151ab6d0509c..c988da56c863 100644
@@ -8,6 +8,7 @@ LIBARENA=$(abspath .)
 
 LIBARENA_SOURCES = $(wildcard $(LIBARENA)/src/*.bpf.c) $(wildcard $(LIBARENA)/selftests/*.bpf.c)
 LIBARENA_OBJECTS = $(notdir $(LIBARENA_SOURCES:.bpf.c=.bpf.o))
+LIBARENA_OBJECTS_ASAN = $(notdir $(LIBARENA_SOURCES:.bpf.c=_asan.bpf.o))
 
 INCLUDES = -I$(LIBARENA)/include -I$(LIBARENA)/..
 ifneq ($(INCLUDE_DIR),)
@@ -17,6 +18,13 @@ ifneq ($(LIBBPF_INCLUDE),)
 INCLUDES += -I$(LIBBPF_INCLUDE)
 endif
 
+ASAN_FLAGS = -fsanitize=kernel-address -fno-stack-protector -fno-builtin
+ASAN_FLAGS += -mllvm -asan-instrument-address-spaces=1 -mllvm -asan-shadow-addr-space=1
+ASAN_FLAGS += -mllvm -asan-use-stack-safety=0 -mllvm -asan-stack=0
+ASAN_FLAGS += -mllvm -asan-kernel=1
+ASAN_FLAGS += -mllvm -asan-constructor-kind=none
+ASAN_FLAGS += -mllvm -asan-destructor-kind=none
+
 # ENABLE_ATOMICS_TESTS required because we use arena spinlocks
 override BPF_CFLAGS += -DENABLE_ATOMICS_TESTS
 override BPF_CFLAGS += -O2 -Wno-incompatible-pointer-types-discards-qualifiers
@@ -28,23 +36,42 @@ CFLAGS += $(INCLUDES)
 vpath %.bpf.c $(LIBARENA)/src $(LIBARENA)/selftests
 vpath %.c $(LIBARENA)/src $(LIBARENA)/selftests
 
-all: test_libarena
+all: test_libarena test_libarena_asan
+
+test_libarena_asan: selftest.c $(BPFOBJ) libarena_asan.skel.h
+	$(call msg,BINARY,libarena,$@)
+	$(Q)$(CLANG) $(CFLAGS) -DBPF_ARENA_ASAN $< $(BPFOBJ) $(LDLIBS) -o $@
 
 test_libarena: selftest.c $(BPFOBJ) libarena.skel.h
 	$(call msg,BINARY,libarena,$@)
 	$(Q)$(CLANG) $(CFLAGS) $< $(BPFOBJ) $(LDLIBS) -o $@
 
+skeletons: libarena.skel.h libarena_asan.skel.h
+.PHONY: skeletons
+
+libarena_asan.skel.h: main_asan.bpf.o
+	$(call msg,GEN-SKEL,libarena,$@)
+	$(Q)$(BPFTOOL) gen skeleton $< name "libarena_asan" > $@
+
 libarena.skel.h: main.bpf.o
 	$(call msg,GEN-SKEL,libarena,$@)
 	$(Q)$(BPFTOOL) gen skeleton $< name "libarena" > $@
 
+main_asan.bpf.o: $(LIBARENA_OBJECTS_ASAN)
+	$(call msg,GEN-OBJ,libarena,$@)
+	$(Q)$(BPFTOOL) gen object $@ $^
+
 main.bpf.o: $(LIBARENA_OBJECTS)
 	$(call msg,GEN-OBJ,libarena,$@)
 	$(Q)$(BPFTOOL) gen object $@ $^
 
+%_asan.bpf.o: %.bpf.c
+	$(call msg,CLNG-BPF,libarena,$@)
+	$(Q)$(CLANG) $(BPF_CFLAGS) $(ASAN_FLAGS) -DBPF_ARENA_ASAN $(BPF_TARGET_ENDIAN) -c $< -o $@
+
 %.bpf.o: %.bpf.c
 	$(call msg,CLNG-BPF,libarena,$@)
 	$(Q)$(CLANG) $(BPF_CFLAGS) $(BPF_TARGET_ENDIAN) -c $< -o $@
 
 clean:
-	$(Q)rm -f *.skel.h *.bpf.o test_libarena
+	$(Q)rm -f *.skel.h *.bpf.o *.linked*.o test_libarena test_libarena_asan
tools/testing/selftests/bpf/libarena/include/selftest_helpers.h +28 -0
index 78db8bc0fc67..36e4f36ae8e6 100644
@@ -84,3 +84,31 @@ static inline int libarena_get_globals_pages(int arena_get_base_fd,
 	free(vec);
 	return 0;
 }
+
+static inline int libarena_asan_init(int arena_get_base_fd,
+				     int asan_init_fd,
+				     size_t arena_all_pages)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct asan_init_args args;
+	u64 globals_pages;
+	int ret;
+
+	ret = libarena_get_globals_pages(arena_get_base_fd,
+					 arena_all_pages, &globals_pages);
+	if (ret)
+		return ret;
+
+	args = (struct asan_init_args){
+		.arena_all_pages = arena_all_pages,
+		.arena_globals_pages = globals_pages,
+	};
+
+	opts.ctx_in = &args;
+	opts.ctx_size_in = sizeof(args);
+
+	ret = bpf_prog_test_run_opts(asan_init_fd, &opts);
+	if (ret)
+		return ret;
+	return opts.retval;
+}
tools/testing/selftests/bpf/libarena/selftests/selftest.c +36 -2
index c692e20ea7b1..3639cfe41217 100644
@@ -16,8 +16,18 @@
 #include <sys/sysinfo.h>
 
 #include <common.h>
+#include <asan.h>
 #include <selftest_helpers.h>
 
+#ifdef BPF_ARENA_ASAN
+#include "../libarena_asan.skel.h"
+typedef struct libarena_asan selftest;
+#define selftest__open libarena_asan__open
+#define selftest__open_and_load libarena_asan__open_and_load
+#define selftest__load libarena_asan__load
+#define selftest__attach libarena_asan__attach
+#define selftest__destroy libarena_asan__destroy
+#else
 #include "../libarena.skel.h"
 typedef struct libarena selftest;
 #define selftest__open libarena__open
@@ -25,6 +35,7 @@ typedef struct libarena selftest;
 #define selftest__load libarena__load
 #define selftest__attach libarena__attach
 #define selftest__destroy libarena__destroy
+#endif
 
 static bool verbose;
 static int testno = 1;
@@ -67,6 +78,26 @@ static int libbpf_print_fn(enum libbpf_print_level level,
 	return vfprintf(stderr, format, args);
 }
 
+static int init_arena(selftest *skel)
+{
+	int ret;
+
+	ret = libarena_run_prog(bpf_program__fd(skel->progs.arena_alloc_reserve));
+	if (ret)
+		return ret;
+
+#ifdef BPF_ARENA_ASAN
+	ret = libarena_asan_init(
+		bpf_program__fd(skel->progs.arena_get_base),
+		bpf_program__fd(skel->progs.asan_init),
+		(1ULL << 32) / sysconf(_SC_PAGESIZE));
+	if (ret)
+		return ret;
+#endif
+
+	return 0;
+}
+
 static int run_test(selftest *skel, struct bpf_program *prog)
 {
 	int prog_fd;
@@ -82,10 +113,13 @@ static void
 banner(const char *progpath)
 {
 	char *name = basename(progpath);
+	bool is_asan;
 
+	/* Check if our BPF programs are ASAN-capable using strstr on the prog name. */
 	printf("%s\n", name);
+	is_asan = strstr(name, "_asan");
 
-	printf("=== %s ===\n", "libarena selftests");
+	printf("=== %s %s===\n", "libarena selftests", is_asan ? "(asan) " : "");
 }
 
 int main(int argc, char *argv[])
@@ -117,7 +151,7 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	ret = libarena_run_prog(bpf_program__fd(skel->progs.arena_alloc_reserve));
+	ret = init_arena(skel);
 	if (ret) {
 		fprintf(stderr, "Failed to initialize arena: %d\n", ret);
 		selftest__destroy(skel);
tools/testing/selftests/bpf/libarena/selftests/st_asan_common.h new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.53.0
author reviewer
#19 lore raw

PATCH v7 8/9 selftests/bpf: Add buddy allocator for libarena

In reply to Emil Tsalapatis

Add a byte-oriented buddy allocator for libarena. The buddy allocator provides an alloc/free interface for small arena allocations ranging from 16 bytes to 512 KiB. Lower allocations values are rounded up to 16 bytes. The buddy allocator does not handle larger allocations that can instead use the existing bpf_arena_{alloc, free}_pages() kfunc.

---
 .../selftests/bpf/libarena/include/buddy.h    |  92 ++
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 903 ++++++++++++++++++
 .../selftests/bpf/libarena/src/common.bpf.c   |  12 +
 3 files changed, 1007 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/include/buddy.h
 create mode 100644 tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
tools/testing/selftests/bpf/libarena/include/buddy.h new file +92 -0
new file mode 100644
index 000000000000..00e2437128ef
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#pragma once
+
+struct buddy_chunk;
+typedef struct buddy_chunk __arena buddy_chunk_t;
+
+struct buddy_header;
+typedef struct buddy_header __arena buddy_header_t;
+
+enum buddy_consts {
+	/*
+	 * Minimum allocation is 1 << BUDDY_MIN_ALLOC_SHIFT.
+	 * Larger sizes increase internal fragmentation, but smaller
+	 * sizes increase the space overhead of the block metadata.
+	 */
+	BUDDY_MIN_ALLOC_SHIFT	= 4,
+	BUDDY_MIN_ALLOC_BYTES	= 1 << BUDDY_MIN_ALLOC_SHIFT,
+
+	/*
+	 * How many orders the buddy allocator can serve. Minimum block
+	 * size is 1 << BUDDY_MIN_ALLOC_SHIFT, maximum block size is
+	 * 1 << (BUDDY_MIN_ALLOC_SHIFT + BUDDY_CHUNK_NUM_ORDERS - 1):
+	 * Each block has size 1 << BUDDY_MIN_ALLOC_SHIFT, and the
+	 * allocation orders are in [0, BUDDY_CHUNK_NUM_ORDERS).
+	 * We keep two blocks of the maximum size to retain the
+	 * property in the code that all blocks have a buddy.
+	 * Higher values increase the maximum allocation size,
+	 * but also the size of the metadata for each block.
+	 */
+	BUDDY_CHUNK_NUM_ORDERS	= 1 << 4,
+	BUDDY_CHUNK_BYTES	= BUDDY_MIN_ALLOC_BYTES << (BUDDY_CHUNK_NUM_ORDERS),
+
+	/* Offset of the buddy header within a free block, see buddy.bpf.c for details */
+	BUDDY_HEADER_OFF	= 8,
+
+	/* The maximum number of blocks a chunk may have to track. */
+	BUDDY_CHUNK_ITEMS	= 1 << (BUDDY_CHUNK_NUM_ORDERS),
+	BUDDY_CHUNK_OFFSET_MASK	= BUDDY_CHUNK_BYTES - 1,
+
+	/*
+	 * Alignment for chunk allocations based on bpf_arena_alloc_pages.
+	 * The arena allocation kfunc does not have an alignment argument,
+	 * but that is required for all block calculations in the chunk to
+	 * work.
+	 */
+	BUDDY_VADDR_OFFSET	= BUDDY_CHUNK_BYTES,
+
+	/* Total arena virtual address space the allocator can consume. */
+	BUDDY_VADDR_SIZE	= BUDDY_CHUNK_BYTES << 10
+};
+
+struct buddy_header {
+	u32 prev_index;	/* "Pointer" to the previous available allocation of the same size. */
+	u32 next_index; /* Same for the next allocation. */
+};
+
+/*
+ * We bring memory into the allocator 1 MiB at a time.
+ */
+struct buddy_chunk {
+	/* The order of the current allocation for a item. 4 bits per order. */
+	u8		orders[BUDDY_CHUNK_ITEMS / 2];
+	/*
+	 * Bit to denote whether chunk is allocated. Size of the allocated/free
+	 * chunk found from the orders array.
+	 */
+	u8		allocated[BUDDY_CHUNK_ITEMS / 8];
+	/* Freelists for O(1) allocation. */
+	u64		freelists[BUDDY_CHUNK_NUM_ORDERS];
+	buddy_chunk_t	*next;
+};
+
+struct buddy {
+	buddy_chunk_t *first_chunk;		/* Pointer to the chunk linked list. */
+	arena_spinlock_t lock;			/* Allocator lock */
+	u64 vaddr;				/* Allocation into reserved vaddr */
+};
+
+typedef struct buddy __arena buddy_t;
+
+#ifdef __BPF__
+
+int buddy_init(buddy_t *buddy);
+int buddy_destroy(buddy_t *buddy);
+int buddy_free_internal(buddy_t *buddy, u64 free);
+#define buddy_free(buddy, ptr) buddy_free_internal((buddy), (u64)(ptr))
+u64 buddy_alloc_internal(buddy_t *buddy, size_t size);
+#define buddy_alloc(alloc, size) ((void __arena *)buddy_alloc_internal((alloc), (size)))
+
+
+#endif /* __BPF__  */
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c new file +903 -0

903 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/src/common.bpf.c +12 -0
index db6dd8a011ae..6d53f6e6e46f 100644
@@ -1,9 +1,13 @@
 // SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
 #include <common.h>
+#include <asan.h>
+#include <buddy.h>
 
 const volatile u32 zero = 0;
 
+buddy_t buddy;
+
 /* How many pages do we reserve at the beginning of the arena segment? */
 #define RESERVE_ALLOC (8)
 
@@ -59,4 +63,12 @@ __weak int arena_alloc_reserve(void)
 	return bpf_arena_reserve_pages(&arena, NULL, RESERVE_ALLOC);
 }
 
+SEC("syscall")
+__weak int arena_buddy_reset(void)
+{
+	buddy_destroy(&buddy);
+
+	return buddy_init(&buddy);
+}
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.53.0
author reviewer
#20 lore raw

PATCH v7 9/9 selftests/bpf: Add selftests for libarena buddy allocator

In reply to Emil Tsalapatis

Introduce selftests for the buddy allocator without ASAN. Add the libarena selftests both to the libarena test runner and to test_progs, so that they are a) available when libarena is pulled as a standalone library, and b) exercised along with all other test programs in this directory.

Currently ASAN for libarena requires LLVM 22, which is not available in the CI. Delay testing the ASAN case directly through test_progs until that is the case. The ASAN version is available for testing on demand through the library's own harness.

---
 .../bpf/libarena/include/selftest_helpers.h   |   7 +
 .../bpf/libarena/selftests/selftest.c         |   8 +
 .../libarena/selftests/st_asan_buddy.bpf.c    | 241 ++++++++++++++++++
 .../bpf/libarena/selftests/st_buddy.bpf.c     | 208 +++++++++++++++
 .../selftests/bpf/prog_tests/libarena.c       |  62 +++++
 5 files changed, 526 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/libarena.c
tools/testing/selftests/bpf/libarena/include/selftest_helpers.h +7 -0
index 36e4f36ae8e6..cb8bf9b7cd71 100644
@@ -112,3 +112,10 @@ static inline int libarena_asan_init(int arena_get_base_fd,
 		return ret;
 	return opts.retval;
 }
+
+static inline bool libarena_must_setup_alloc(struct bpf_program *prog)
+{
+	const char *name = bpf_program__name(prog);
+
+	return !strstr(name, "test_buddy");
+}
tools/testing/selftests/bpf/libarena/selftests/selftest.c +8 -0
index 3639cfe41217..cfa435027b73 100644
@@ -17,6 +17,7 @@
 
 #include <common.h>
 #include <asan.h>
+#include <buddy.h>
 #include <selftest_helpers.h>
 
 #ifdef BPF_ARENA_ASAN
@@ -101,6 +102,13 @@ static int init_arena(selftest *skel)
 static int run_test(selftest *skel, struct bpf_program *prog)
 {
 	int prog_fd;
+	int ret;
+
+	if (libarena_must_setup_alloc(prog)) {
+		ret = libarena_run_prog(bpf_program__fd(skel->progs.arena_buddy_reset));
+		if (ret)
+			return ret;
+	}
 
 	prog_fd = bpf_program__fd(prog);
 	if (prog_fd < 0)
tools/testing/selftests/bpf/libarena/selftests/st_asan_buddy.bpf.c new file +241 -0

241 lines not on this page · raw message

tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c new file +208 -0

208 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/libarena.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.53.0
#21 lore raw
In reply to Emil Tsalapatis
Quoted 18 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

Quoted 6 lines

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

I am back in the same position as I was with comments in [0].

I.e., again, I don't see anything in the stream output (passing -v to test_libarena_asan) when I build it with clang 23 (LLVM tip). What am I doing wrong? (Leaving aside all feedback about integrating it in the regular test runner that is still unaddressed, thus tests do not run by default).

[0]: https://lore.kernel.org/bpf/CAP01T75+BoQfQN=47NhYYviZttjCPb_CXNpa=DUC0=xSvdNZdA@mail.gmail.com

Quoted 1 line

[...]

author reviewer
#22 lore raw
Quoted 28 lines · Kumar Kartikeya Dwivedi
Quoted 18 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

Quoted 6 lines

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

I am back in the same position as I was with comments in [0].

Quoted 7 lines

I.e., again, I don't see anything in the stream output (passing -v to test_libarena_asan) when I build it with clang 23 (LLVM tip). What am I doing wrong? (Leaving aside all feedback about integrating it in the regular test runner that is still unaddressed, thus tests do not run by default).

[0]: https://lore.kernel.org/bpf/CAP01T75+BoQfQN=47NhYYviZttjCPb_CXNpa=DUC0=xSvdNZdA@mail.gmail.com

The series introduces a selftest for the test runner called libarena. It is introduced in patch 9 to avoid creating a runner that does nothing in the previous patches. Tests run with test_progs like regular selftests. For ASAN we do not have LLVM22 support in the CI yet so test_progs doesn't exercise it.

The stream output is suppressed by a switch in asan.c to make the output of test_libarena_asan legible. Each test that expects to generate ASAN will generate a ton of output. I will add userspace logic to consume the splats instead of emitting them to keep the output sane.

Quoted 2 lines
Quoted 2 lines

[...]

#23 lore raw
Quoted 44 lines · Emil Tsalapatis
Quoted 28 lines · Kumar Kartikeya Dwivedi
Quoted 18 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

Quoted 6 lines

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

I am back in the same position as I was with comments in [0].

Quoted 7 lines

I.e., again, I don't see anything in the stream output (passing -v to test_libarena_asan) when I build it with clang 23 (LLVM tip). What am I doing wrong? (Leaving aside all feedback about integrating it in the regular test runner that is still unaddressed, thus tests do not run by default).

[0]: https://lore.kernel.org/bpf/CAP01T75+BoQfQN=47NhYYviZttjCPb_CXNpa=DUC0=xSvdNZdA@mail.gmail.com

The series introduces a selftest for the test runner called libarena. It is introduced in patch 9 to avoid creating a runner that does nothing in the previous patches. Tests run with test_progs like regular selftests. For ASAN we do not have LLVM22 support in the CI yet so test_progs doesn't exercise it.

Quoted 4 lines

The stream output is suppressed by a switch in asan.c to make the output of test_libarena_asan legible. Each test that expects to generate ASAN will generate a ton of output. I will add userspace logic to consume the splats instead of emitting them to keep the output sane.

Why can't we put everything in test_progs instead of separate test_libarena? Then add some logic like existing stream tests for verifying / testing the generated output in ASAN mode.
For example, libbpf doesn't have its own separate test suite. I think the same logic should apply to libarena.

If LLVM 22 is a problem just detect and mark the test as skipped for now when unavailable.

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

[...]

author reviewer
#24 lore raw
Quoted 50 lines · Kumar Kartikeya Dwivedi
Quoted 44 lines · Emil Tsalapatis
Quoted 28 lines · Kumar Kartikeya Dwivedi
Quoted 18 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

Quoted 6 lines

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

I am back in the same position as I was with comments in [0].

Quoted 7 lines

I.e., again, I don't see anything in the stream output (passing -v to test_libarena_asan) when I build it with clang 23 (LLVM tip). What am I doing wrong? (Leaving aside all feedback about integrating it in the regular test runner that is still unaddressed, thus tests do not run by default).

[0]: https://lore.kernel.org/bpf/CAP01T75+BoQfQN=47NhYYviZttjCPb_CXNpa=DUC0=xSvdNZdA@mail.gmail.com

The series introduces a selftest for the test runner called libarena. It is introduced in patch 9 to avoid creating a runner that does nothing in the previous patches. Tests run with test_progs like regular selftests. For ASAN we do not have LLVM22 support in the CI yet so test_progs doesn't exercise it.

Quoted 4 lines

The stream output is suppressed by a switch in asan.c to make the output of test_libarena_asan legible. Each test that expects to generate ASAN will generate a ton of output. I will add userspace logic to consume the splats instead of emitting them to keep the output sane.

Quoted 6 lines

Why can't we put everything in test_progs instead of separate test_libarena? Then add some logic like existing stream tests for verifying / testing the generated output in ASAN mode.
For example, libbpf doesn't have its own separate test suite. I think the same logic should apply to libarena.

libarena is different in that it's BPF code we distribute as source. Plus, the test runner is an example of how to set up the arena state. It's very little code and automatically identifies the selftests without having to explicitly enumerate them, so it's not extra maintenance burden. For followup patches it's been 0 LOC to either the libarena selftest or the test_progs test.

Quoted 3 lines

If LLVM 22 is a problem just detect and mark the test as skipped for now when unavailable.

Fair enough we can make it in the Makefile to check if the ASAN flag is supported by the BPF backend before compiling the selftest, otherwise make it a no-op.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

[...]

#25 lore raw
Quoted 59 lines · Emil Tsalapatis
Quoted 50 lines · Kumar Kartikeya Dwivedi
Quoted 44 lines · Emil Tsalapatis
Quoted 28 lines · Kumar Kartikeya Dwivedi
Quoted 18 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

Quoted 6 lines

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

I am back in the same position as I was with comments in [0].

Quoted 7 lines

I.e., again, I don't see anything in the stream output (passing -v to test_libarena_asan) when I build it with clang 23 (LLVM tip). What am I doing wrong? (Leaving aside all feedback about integrating it in the regular test runner that is still unaddressed, thus tests do not run by default).

[0]: https://lore.kernel.org/bpf/CAP01T75+BoQfQN=47NhYYviZttjCPb_CXNpa=DUC0=xSvdNZdA@mail.gmail.com

The series introduces a selftest for the test runner called libarena. It is introduced in patch 9 to avoid creating a runner that does nothing in the previous patches. Tests run with test_progs like regular selftests. For ASAN we do not have LLVM22 support in the CI yet so test_progs doesn't exercise it.

Quoted 4 lines

The stream output is suppressed by a switch in asan.c to make the output of test_libarena_asan legible. Each test that expects to generate ASAN will generate a ton of output. I will add userspace logic to consume the splats instead of emitting them to keep the output sane.

Quoted 6 lines

Why can't we put everything in test_progs instead of separate test_libarena? Then add some logic like existing stream tests for verifying / testing the generated output in ASAN mode.
For example, libbpf doesn't have its own separate test suite. I think the same logic should apply to libarena.

Quoted 6 lines

libarena is different in that it's BPF code we distribute as source. Plus, the test runner is an example of how to set up the arena state. It's very little code and automatically identifies the selftests without having to explicitly enumerate them, so it's not extra maintenance burden. For followup patches it's been 0 LOC to either the libarena selftest or the test_progs test.

I think what Kumar is saying is that we have only one runner in CI:
test_progs, so all libarena tests need to be covered by test_progs.

author reviewer
#26 lore raw
Quoted 67 lines · Alexei Starovoitov
Quoted 59 lines · Emil Tsalapatis
Quoted 50 lines · Kumar Kartikeya Dwivedi
Quoted 44 lines · Emil Tsalapatis
Quoted 28 lines · Kumar Kartikeya Dwivedi
Quoted 18 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

Quoted 6 lines

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

I am back in the same position as I was with comments in [0].

Quoted 7 lines

I.e., again, I don't see anything in the stream output (passing -v to test_libarena_asan) when I build it with clang 23 (LLVM tip). What am I doing wrong? (Leaving aside all feedback about integrating it in the regular test runner that is still unaddressed, thus tests do not run by default).

[0]: https://lore.kernel.org/bpf/CAP01T75+BoQfQN=47NhYYviZttjCPb_CXNpa=DUC0=xSvdNZdA@mail.gmail.com

The series introduces a selftest for the test runner called libarena. It is introduced in patch 9 to avoid creating a runner that does nothing in the previous patches. Tests run with test_progs like regular selftests. For ASAN we do not have LLVM22 support in the CI yet so test_progs doesn't exercise it.

Quoted 4 lines

The stream output is suppressed by a switch in asan.c to make the output of test_libarena_asan legible. Each test that expects to generate ASAN will generate a ton of output. I will add userspace logic to consume the splats instead of emitting them to keep the output sane.

Quoted 6 lines

Why can't we put everything in test_progs instead of separate test_libarena? Then add some logic like existing stream tests for verifying / testing the generated output in ASAN mode.
For example, libbpf doesn't have its own separate test suite. I think the same logic should apply to libarena.

Quoted 6 lines

libarena is different in that it's BPF code we distribute as source. Plus, the test runner is an example of how to set up the arena state. It's very little code and automatically identifies the selftests without having to explicitly enumerate them, so it's not extra maintenance burden. For followup patches it's been 0 LOC to either the libarena selftest or the test_progs test.

Quoted 2 lines

I think what Kumar is saying is that we have only one runner in CI:
test_progs, so all libarena tests need to be covered by test_progs.

In that case I misunderstood. I agree that everything should be tested by test_progs. There's currently the ASAN version of libarena missing, but we can add it and turn it off if the compiler doesn't support it.

There is the secondary question: Do we want to have a standalone libarena runner in addition to the one in test_progs in the codebase? Imo we do, for the reasons above.

#27 lore raw
Quoted 10 lines · Emil Tsalapatis
Quoted 9 lines
Quoted 6 lines

libarena is different in that it's BPF code we distribute as source. Plus, the test runner is an example of how to set up the arena state. It's very little code and automatically identifies the selftests without having to explicitly enumerate them, so it's not extra maintenance burden. For followup patches it's been 0 LOC to either the libarena selftest or the test_progs test.

I think what Kumar is saying is that we have only one runner in CI:
test_progs, so all libarena tests need to be covered by test_progs.

Quoted 7 lines

In that case I misunderstood. I agree that everything should be tested by test_progs. There's currently the ASAN version of libarena missing, but we can add it and turn it off if the compiler doesn't support it.

There is the secondary question: Do we want to have a standalone libarena runner in addition to the one in test_progs in the codebase? Imo we do, for the reasons above.

I think it's fine to have standalone libarena runner as well, but it may bit rot, since CI won't be running it.

#28 lore raw
In reply to Emil Tsalapatis
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 8 lines · On Sun, 12 Apr 2026 13:45:37 -0400 you wrote:

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

[...]

Here is the summary with links:
- [bpf-next,v7,1/9] bpf: Allow instructions with arena source and non-arena dest registers (no matching commit)
- [bpf-next,v7,2/9] selftests/bpf: Add tests for non-arena/arena operations

    https://git.kernel.org/bpf/bpf-next/c/4c5f21d4dfa4

- [bpf-next,v7,3/9] selftests/bpf: Move bpf_arena_spin_lock.h to the top level (no matching commit)
- [bpf-next,v7,4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h (no matching commit)
- [bpf-next,v7,5/9] selftests/bpf: Add basic libarena scaffolding
(no matching commit)
- [bpf-next,v7,6/9] selftests/bpf: Add arena ASAN runtime to libarena
(no matching commit)
- [bpf-next,v7,7/9] selftests/bpf: Add ASAN support for libarena selftests (no matching commit)
- [bpf-next,v7,8/9] selftests/bpf: Add buddy allocator for libarena
(no matching commit)
- [bpf-next,v7,9/9] selftests/bpf: Add selftests for libarena buddy allocator (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#29 lore raw
In reply to Emil Tsalapatis
Quoted 28 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

The patchset has the following stucture:

1-2: Addresses a verification failure triggered by SCALAR += PTR_TO_ARENA instructions.

Quoted 8 lines

3-4: Minor changes to selftest headers to prepare for the introduction of libarena.

5-7: Add the libarena directory and testing scaffolding, and introduce the ASAN runtime.

8-9: Add the new buddy memory allocator along with self-contained and prog-tests-based selftests.

I haven't had an opportunity to go through this patch series just yet, but can I just say that it excites me considerably! I've been wanting to carve out some time to implement something exactly like this for a very long time (since BPF arena maps were initially introduced) as I think it's going to be incredibly valuable for BPF users moving forward.

Quoted 122 lines

HISTORY
=======

v6->v7 (https://lore.kernel.org/bpf/[email protected])
- Modify patch 1 to allow operations between PTR_TO_ARENA src_reg and dst_reg of any type. Adjust selftests accordingly (Alexei)
- Remove unnecessary include in patch 5 (Song)
- Removed unused definitions/assignments in patches 8/9, update patch descriptions

v5->v6 (https://lore.kernel.org/bpf/[email protected])
- Fix subreg_def management for SCALAR += PTR_TO_ARENA operations (AI)
- Add more selftests for the SCALAR += PTR_TO_ARENA patch (Sashiko)
- Adjust fls() operation to be in line with the kernel version (Sashiko)
- Address Sashiko selftests and debugging nits
- Add ASAN loadN and storeN _noabort variants and associated BTF anchor
- Remove unnecessary bit freeing of buddies during block splitting

v4->v5 (https://lore.kernel.org/bpf/[email protected])
Omitting various nits and fixups.
- Properly adjust subreg_def for scalar += ptr_to_arena calls (Sashiko)
- Remove extraneous definition from prog_tests/arena_spin_lock.c (Song)
- Trim extraneous comments from ASAN and buddy (Alexei)
- Remove asan_dummy call and replace with function pointer array (Alexei)
- Remove usersapi.h header and merge it into common.h (Alexei)
- Replace ASAN macros with function calls (Alexei)
- Embed buddy lock into the struct and move the buddy allocator to __arena_global (Alexei)
- Add commenting for buddy allocator constants (Alexei)
- Add default buddy allocator directly in common.bpf.c, so that the user does not need to define it.
- Expand test harnesses to dynamically find individual selftests. Now the selftests also reports each test individually (e.g., 5 entries for the buddy allocator instead of 1). This brings them to par with the rest of the test_progs.

v3->v4 (https://lore.kernel.org/bpf/[email protected])
- Add Acks by Song to patches 1-4.
- Expand the verifier's handling of scalar/arena operations to include all 3-operand operations in Patch 1 (Alexei)
- Add additional tests for arena/arena (allowed) and arena/pointer (not allowed) operations in Patch 2
- Remove ASAN version of the library from default compilation since it requires LLVM 22 and up (CI)
- Rework buddy allocator locking for clarity and add comments
- Fix From: email to be consistent with SOB
- Address (most) Sashiko comments

v2->v3 (https://lore.kernel.org/bpf/[email protected])
Nonexhaustive due to significant patch rework.
- Do not duplicate WRITE_ONCE macro (Mykyta, Kumar)
- Add SPDX headers (Alexei)
- Remove bump/stack allocators (Alexei)
- Integrate testing with test_progs (Kumar)
- Add short description of ASAN algorithm at the top of the file (Alexei)

v1->v2 (https://lore.kernel.org/bpf/[email protected]/)

- Added missing format string argument (AI)
- Fix outdated selftests prog name check (AI)
- Fixed stack allocation check for segment creation (AI)
- Fix errors in non-ASAN bump allocator selftests (AI)
- Propagate error value from individual selftests in selftest.c
- Removed embedded metadata from bump allocator as it was needlessly complicating its behavior

Emil Tsalapatis (9):
bpf: Allow instructions with arena source and non-arena dest registers selftests/bpf: Add tests for non-arena/arena operations selftests/bpf: Move bpf_arena_spin_lock.h to the top level selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h selftests/bpf: Add basic libarena scaffolding selftests/bpf: Add arena ASAN runtime to libarena selftests/bpf: Add ASAN support for libarena selftests selftests/bpf: Add buddy allocator for libarena selftests/bpf: Add selftests for libarena buddy allocator

 kernel/bpf/verifier.c                         |  14 +-
 tools/testing/selftests/bpf/.gitignore        |   2 +
 tools/testing/selftests/bpf/Makefile          |  28 +-
 .../testing/selftests/bpf/bpf_arena_common.h  |   4 -
 .../bpf/{progs => }/bpf_arena_spin_lock.h     |   4 +-
 tools/testing/selftests/bpf/bpf_atomic.h      |   4 -
 .../testing/selftests/bpf/bpf_experimental.h  |   3 +
 tools/testing/selftests/bpf/libarena/Makefile |  77 ++
 .../selftests/bpf/libarena/include/asan.h     | 118 +++
 .../selftests/bpf/libarena/include/buddy.h    |  92 ++
 .../selftests/bpf/libarena/include/common.h   |  67 ++
 .../bpf/libarena/include/selftest_helpers.h   | 121 +++
 .../bpf/libarena/selftests/selftest.c         | 188 ++++
 .../libarena/selftests/st_asan_buddy.bpf.c    | 241 +++++
 .../bpf/libarena/selftests/st_asan_common.h   |  47 +
 .../bpf/libarena/selftests/st_buddy.bpf.c     | 208 ++++
 .../selftests/bpf/libarena/src/asan.bpf.c     | 559 +++++++++++
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 903 ++++++++++++++++++
 .../selftests/bpf/libarena/src/common.bpf.c   |  74 ++
 .../bpf/prog_tests/arena_spin_lock.c          |   7 -
 .../selftests/bpf/prog_tests/libarena.c       |  62 ++
 .../selftests/bpf/progs/arena_spin_lock.c     |   2 +-
 .../selftests/bpf/progs/verifier_arena.c      | 130 +++
 23 files changed, 2934 insertions(+), 21 deletions(-)
 rename tools/testing/selftests/bpf/{progs => }/bpf_arena_spin_lock.h (99%)
 create mode 100644 tools/testing/selftests/bpf/libarena/Makefile
 create mode 100644 tools/testing/selftests/bpf/libarena/include/asan.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/buddy.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/common.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/selftest_helpers.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_common.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/asan.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/common.bpf.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/libarena.c
Signature · 1 line
2.53.0
#30 lore raw
Quoted 38 lines · Matt Bobrowski
Quoted 28 lines · Emil Tsalapatis

Add a new subdirectory to tools/testing/selftests/bpf called libarena, along with programs useful for writing arena-based BPF code. This patchset adds the following:

1) libarena, a subdirectory where arena BPF code that is generally useful to BPF arena programs can be easily added and tested.

2) An ASAN runtime for BPF arena programs. BPF arenas allow for accessing memory after it has been freed or if it is out of bounds, making it more difficult to triage bugs combined to regular BPF. Use LLVM's recently added support for address-space based sanitization to selectively sanitize just the arena accesses.

3) A buddy memory allocator that can be reused by BPF programs to handle memory allocation/deletion. The allocator uses the ASAN runtime to add address sanitization if requested.

The patch includes testing for the new allocators and ASAN features that can be built from the top directory using "make libarena_test" and "make libarena_test_asan". The generated binaries reside in libarena/. The patch also adds test-progs-based selftests to the codebase for the libarena code, so the new tests are run by ./test_progs.

The patchset has the following stucture:

1-2: Addresses a verification failure triggered by SCALAR += PTR_TO_ARENA instructions.

Quoted 8 lines

3-4: Minor changes to selftest headers to prepare for the introduction of libarena.

5-7: Add the libarena directory and testing scaffolding, and introduce the ASAN runtime.

8-9: Add the new buddy memory allocator along with self-contained and prog-tests-based selftests.

Quoted 6 lines

I haven't had an opportunity to go through this patch series just yet, but can I just say that it excites me considerably! I've been wanting to carve out some time to implement something exactly like this for a very long time (since BPF arena maps were initially introduced) as I think it's going to be incredibly valuable for BPF users moving forward.

I swear that I also saw a notification stating that this series was applied to bpf/bpf-next.git (master), but upon checking I only see that commit 4c5f21d4dfa ("selftests/bpf: Add tests for non-arena/arena operations") from the series was applied. What's going on there?

Quoted 122 lines
Quoted 122 lines

HISTORY
=======

v6->v7 (https://lore.kernel.org/bpf/[email protected])
- Modify patch 1 to allow operations between PTR_TO_ARENA src_reg and dst_reg of any type. Adjust selftests accordingly (Alexei)
- Remove unnecessary include in patch 5 (Song)
- Removed unused definitions/assignments in patches 8/9, update patch descriptions

v5->v6 (https://lore.kernel.org/bpf/[email protected])
- Fix subreg_def management for SCALAR += PTR_TO_ARENA operations (AI)
- Add more selftests for the SCALAR += PTR_TO_ARENA patch (Sashiko)
- Adjust fls() operation to be in line with the kernel version (Sashiko)
- Address Sashiko selftests and debugging nits
- Add ASAN loadN and storeN _noabort variants and associated BTF anchor
- Remove unnecessary bit freeing of buddies during block splitting

v4->v5 (https://lore.kernel.org/bpf/[email protected])
Omitting various nits and fixups.
- Properly adjust subreg_def for scalar += ptr_to_arena calls (Sashiko)
- Remove extraneous definition from prog_tests/arena_spin_lock.c (Song)
- Trim extraneous comments from ASAN and buddy (Alexei)
- Remove asan_dummy call and replace with function pointer array (Alexei)
- Remove usersapi.h header and merge it into common.h (Alexei)
- Replace ASAN macros with function calls (Alexei)
- Embed buddy lock into the struct and move the buddy allocator to __arena_global (Alexei)
- Add commenting for buddy allocator constants (Alexei)
- Add default buddy allocator directly in common.bpf.c, so that the user does not need to define it.
- Expand test harnesses to dynamically find individual selftests. Now the selftests also reports each test individually (e.g., 5 entries for the buddy allocator instead of 1). This brings them to par with the rest of the test_progs.

v3->v4 (https://lore.kernel.org/bpf/[email protected])
- Add Acks by Song to patches 1-4.
- Expand the verifier's handling of scalar/arena operations to include all 3-operand operations in Patch 1 (Alexei)
- Add additional tests for arena/arena (allowed) and arena/pointer (not allowed) operations in Patch 2
- Remove ASAN version of the library from default compilation since it requires LLVM 22 and up (CI)
- Rework buddy allocator locking for clarity and add comments
- Fix From: email to be consistent with SOB
- Address (most) Sashiko comments

v2->v3 (https://lore.kernel.org/bpf/[email protected])
Nonexhaustive due to significant patch rework.
- Do not duplicate WRITE_ONCE macro (Mykyta, Kumar)
- Add SPDX headers (Alexei)
- Remove bump/stack allocators (Alexei)
- Integrate testing with test_progs (Kumar)
- Add short description of ASAN algorithm at the top of the file (Alexei)

v1->v2 (https://lore.kernel.org/bpf/[email protected]/)

- Added missing format string argument (AI)
- Fix outdated selftests prog name check (AI)
- Fixed stack allocation check for segment creation (AI)
- Fix errors in non-ASAN bump allocator selftests (AI)
- Propagate error value from individual selftests in selftest.c
- Removed embedded metadata from bump allocator as it was needlessly complicating its behavior

Emil Tsalapatis (9):
bpf: Allow instructions with arena source and non-arena dest registers selftests/bpf: Add tests for non-arena/arena operations selftests/bpf: Move bpf_arena_spin_lock.h to the top level selftests/bpf: Move READ_ONCE/WRITE_ONCE macros to bpf_experimental.h selftests/bpf: Add basic libarena scaffolding selftests/bpf: Add arena ASAN runtime to libarena selftests/bpf: Add ASAN support for libarena selftests selftests/bpf: Add buddy allocator for libarena selftests/bpf: Add selftests for libarena buddy allocator

 kernel/bpf/verifier.c                         |  14 +-
 tools/testing/selftests/bpf/.gitignore        |   2 +
 tools/testing/selftests/bpf/Makefile          |  28 +-
 .../testing/selftests/bpf/bpf_arena_common.h  |   4 -
 .../bpf/{progs => }/bpf_arena_spin_lock.h     |   4 +-
 tools/testing/selftests/bpf/bpf_atomic.h      |   4 -
 .../testing/selftests/bpf/bpf_experimental.h  |   3 +
 tools/testing/selftests/bpf/libarena/Makefile |  77 ++
 .../selftests/bpf/libarena/include/asan.h     | 118 +++
 .../selftests/bpf/libarena/include/buddy.h    |  92 ++
 .../selftests/bpf/libarena/include/common.h   |  67 ++
 .../bpf/libarena/include/selftest_helpers.h   | 121 +++
 .../bpf/libarena/selftests/selftest.c         | 188 ++++
 .../libarena/selftests/st_asan_buddy.bpf.c    | 241 +++++
 .../bpf/libarena/selftests/st_asan_common.h   |  47 +
 .../bpf/libarena/selftests/st_buddy.bpf.c     | 208 ++++
 .../selftests/bpf/libarena/src/asan.bpf.c     | 559 +++++++++++
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 903 ++++++++++++++++++
 .../selftests/bpf/libarena/src/common.bpf.c   |  74 ++
 .../bpf/prog_tests/arena_spin_lock.c          |   7 -
 .../selftests/bpf/prog_tests/libarena.c       |  62 ++
 .../selftests/bpf/progs/arena_spin_lock.c     |   2 +-
 .../selftests/bpf/progs/verifier_arena.c      | 130 +++
 23 files changed, 2934 insertions(+), 21 deletions(-)
 rename tools/testing/selftests/bpf/{progs => }/bpf_arena_spin_lock.h (99%)
 create mode 100644 tools/testing/selftests/bpf/libarena/Makefile
 create mode 100644 tools/testing/selftests/bpf/libarena/include/asan.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/buddy.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/common.h
 create mode 100644 tools/testing/selftests/bpf/libarena/include/selftest_helpers.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_common.h
 create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/asan.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
 create mode 100644 tools/testing/selftests/bpf/libarena/src/common.bpf.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/libarena.c
Signature · 1 line
2.53.0
reviewer
#31 lore raw

On Thu, Apr 16, 2026 at 1:31 PM Matt Bobrowski <[email protected]> wrote:
[...]

Quoted 8 lines
Quoted 7 lines

I haven't had an opportunity to go through this patch series just yet, but can I just say that it excites me considerably! I've been wanting to carve out some time to implement something exactly like this for a very long time (since BPF arena maps were initially introduced) as I think it's going to be incredibly valuable for BPF users moving forward.

Quoted 4 lines

I swear that I also saw a notification stating that this series was applied to bpf/bpf-next.git (master), but upon checking I only see that commit 4c5f21d4dfa ("selftests/bpf: Add tests for non-arena/arena operations") from the series was applied. What's going on there?

Part of it was committed. From patchwork-bot:

Here is the summary with links:
- [bpf-next,v7,1/9] bpf: Allow instructions with arena source and
non-arena dest registers
(no matching commit)
- [bpf-next,v7,2/9] selftests/bpf: Add tests for non-arena/arena operations

    https://git.kernel.org/bpf/bpf-next/c/4c5f21d4dfa4

- [bpf-next,v7,3/9] selftests/bpf: Move bpf_arena_spin_lock.h to the top level (no matching commit)
- [bpf-next,v7,4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros
to bpf_experimental.h
(no matching commit)
- [bpf-next,v7,5/9] selftests/bpf: Add basic libarena scaffolding (no matching commit)
- [bpf-next,v7,6/9] selftests/bpf: Add arena ASAN runtime to libarena (no matching commit)
- [bpf-next,v7,7/9] selftests/bpf: Add ASAN support for libarena selftests (no matching commit)
- [bpf-next,v7,8/9] selftests/bpf: Add buddy allocator for libarena (no matching commit)
- [bpf-next,v7,9/9] selftests/bpf: Add selftests for libarena buddy allocator (no matching commit)

#32 lore raw
Quoted 23 lines · Song Liu

On Thu, Apr 16, 2026 at 1:31 PM Matt Bobrowski <[email protected]> wrote:
[...]

Quoted 8 lines
Quoted 7 lines

I haven't had an opportunity to go through this patch series just yet, but can I just say that it excites me considerably! I've been wanting to carve out some time to implement something exactly like this for a very long time (since BPF arena maps were initially introduced) as I think it's going to be incredibly valuable for BPF users moving forward.

Quoted 4 lines

I swear that I also saw a notification stating that this series was applied to bpf/bpf-next.git (master), but upon checking I only see that commit 4c5f21d4dfa ("selftests/bpf: Add tests for non-arena/arena operations") from the series was applied. What's going on there?

Part of it was committed. From patchwork-bot:

Here is the summary with links:
- [bpf-next,v7,1/9] bpf: Allow instructions with arena source and
non-arena dest registers
(no matching commit)
- [bpf-next,v7,2/9] selftests/bpf: Add tests for non-arena/arena operations

    https://git.kernel.org/bpf/bpf-next/c/4c5f21d4dfa4
Quoted 15 lines

- [bpf-next,v7,3/9] selftests/bpf: Move bpf_arena_spin_lock.h to the top level (no matching commit)
- [bpf-next,v7,4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros
to bpf_experimental.h
(no matching commit)
- [bpf-next,v7,5/9] selftests/bpf: Add basic libarena scaffolding (no matching commit)
- [bpf-next,v7,6/9] selftests/bpf: Add arena ASAN runtime to libarena (no matching commit)
- [bpf-next,v7,7/9] selftests/bpf: Add ASAN support for libarena selftests (no matching commit)
- [bpf-next,v7,8/9] selftests/bpf: Add buddy allocator for libarena (no matching commit)
- [bpf-next,v7,9/9] selftests/bpf: Add selftests for libarena buddy allocator (no matching commit)

Ah, yeah, I realized only after I had sent out my email.

author reviewer
#33 lore raw
Quoted 11 lines · Matt Bobrowski
Quoted 10 lines · Song Liu

On Thu, Apr 16, 2026 at 1:31 PM Matt Bobrowski <[email protected]> wrote:
[...]

Quoted 8 lines
Quoted 7 lines

I haven't had an opportunity to go through this patch series just yet, but can I just say that it excites me considerably! I've been wanting to carve out some time to implement something exactly like this for a very long time (since BPF arena maps were initially introduced) as I think it's going to be incredibly valuable for BPF users moving forward.

Thank you!

Quoted 29 lines
Quoted 13 lines
Quoted 4 lines

I swear that I also saw a notification stating that this series was applied to bpf/bpf-next.git (master), but upon checking I only see that commit 4c5f21d4dfa ("selftests/bpf: Add tests for non-arena/arena operations") from the series was applied. What's going on there?

Part of it was committed. From patchwork-bot:

Here is the summary with links:
- [bpf-next,v7,1/9] bpf: Allow instructions with arena source and
non-arena dest registers
(no matching commit)
- [bpf-next,v7,2/9] selftests/bpf: Add tests for non-arena/arena operations

    https://git.kernel.org/bpf/bpf-next/c/4c5f21d4dfa4
Quoted 15 lines

- [bpf-next,v7,3/9] selftests/bpf: Move bpf_arena_spin_lock.h to the top level (no matching commit)
- [bpf-next,v7,4/9] selftests/bpf: Move READ_ONCE/WRITE_ONCE macros
to bpf_experimental.h
(no matching commit)
- [bpf-next,v7,5/9] selftests/bpf: Add basic libarena scaffolding (no matching commit)
- [bpf-next,v7,6/9] selftests/bpf: Add arena ASAN runtime to libarena (no matching commit)
- [bpf-next,v7,7/9] selftests/bpf: Add ASAN support for libarena selftests (no matching commit)
- [bpf-next,v7,8/9] selftests/bpf: Add buddy allocator for libarena (no matching commit)
- [bpf-next,v7,9/9] selftests/bpf: Add selftests for libarena buddy allocator (no matching commit)

Quoted 1 line

Ah, yeah, I realized only after I had sent out my email.

Yes, only patches 1-2 were applied. The rest of the series is self-contained within the library itself so it should be compatible with 7.1 regardless of when it gets merged.