bpf: Add arena ASAN runtime and BPF library
Subsystems (5)
cover bpf: Add arena ASAN runtime and BPF library
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 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 set of memory allocators that can be reused by BPF programs to handle memory allocation/deletion. The allocators use 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 structure of this patch is:
1-2: Add BPF Streams kfunc for dumping the current program stack, and allow BPF Streams kfuncs to be callable while holding a lock.
3-4: Minor changes to the /testing/selftests/bpf directory headers to prepare for the introduction of libarena.
5-7: Add the base libarena directory and testing scaffolding, and introduce the ASAN runtime.
8-9: Add a arena memory bump allocator along with testing. This allocator is used for permanent allocations during program init.
10-11: Add a stack page-oriented allocator along with testing. This allocator is used for repeated large allocations to avoid constant bpf_arena_{alloc,free}_pages calls.
12-13: Add a buddy allocator along with testing. The allocator acts as a general allocator for arena-based BPF programs.
Emil Tsalapatis (13):
bpf: Add bpf_stream_print_stack stack dumping kfunc
bpf: Allow BPF stream kfuncs while holding a lock
selftests: bpf: Move bpf_arena_spin_lock.h to the top level
selftests: bpf: Make WRITE_ONCE macro in bpf_atomic.h conditional
selftests: bpf: Add basic libarena scaffolding
selftests: bpf: Add arena ASAN runtime to libarena
selftests: bpf: Add ASAN support for libarena selftests
selftest: bpf: Add bump allocator for libarena
selftests: bpf: Add libarena selftests for the bump allocator
selftest: bpf: Add libarena stack allocator
selftests: bpf: Add selftests for the libarena stack allocator
selftests: bpf: Add buddy allocator for libarena
selftests: bpf: Add selftests for the libarena buddy allocator
kernel/bpf/helpers.c | 1 +
kernel/bpf/stream.c | 13 +
kernel/bpf/verifier.c | 13 +-
tools/lib/bpf/bpf_helpers.h | 2 +
tools/testing/selftests/bpf/.gitignore | 2 +
tools/testing/selftests/bpf/Makefile | 24 +
.../bpf/{progs => }/bpf_arena_spin_lock.h | 4 +-
tools/testing/selftests/bpf/bpf_atomic.h | 2 +
tools/testing/selftests/bpf/libarena/Makefile | 69 ++
.../selftests/bpf/libarena/include/asan.h | 133 +++
.../selftests/bpf/libarena/include/buddy.h | 62 ++
.../selftests/bpf/libarena/include/bump.h | 20 +
.../selftests/bpf/libarena/include/common.h | 118 +++
.../selftests/bpf/libarena/include/stack.h | 44 +
.../selftests/bpf/libarena/include/userapi.h | 23 +
.../bpf/libarena/selftests/selftest.c | 328 ++++++++
.../bpf/libarena/selftests/selftest.h | 17 +
.../libarena/selftests/st_asan_buddy.bpf.c | 238 ++++++
.../bpf/libarena/selftests/st_asan_bump.bpf.c | 193 +++++
.../bpf/libarena/selftests/st_asan_common.h | 49 ++
.../libarena/selftests/st_asan_stack.bpf.c | 253 ++++++
.../bpf/libarena/selftests/st_buddy.bpf.c | 231 ++++++
.../bpf/libarena/selftests/st_bump.bpf.c | 275 ++++++
.../selftests/bpf/libarena/src/asan.bpf.c | 463 +++++++++++
.../selftests/bpf/libarena/src/buddy.bpf.c | 784 ++++++++++++++++++
.../selftests/bpf/libarena/src/bump.bpf.c | 212 +++++
.../selftests/bpf/libarena/src/stack.bpf.c | 338 ++++++++
.../selftests/bpf/progs/arena_spin_lock.c | 2 +-
28 files changed, 3909 insertions(+), 4 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/bump.h
create mode 100644 tools/testing/selftests/bpf/libarena/include/common.h
create mode 100644 tools/testing/selftests/bpf/libarena/include/stack.h
create mode 100644 tools/testing/selftests/bpf/libarena/include/userapi.h
create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.c
create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.h
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_bump.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_asan_stack.bpf.c
create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c
create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_bump.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/bump.bpf.c
create mode 100644 tools/testing/selftests/bpf/libarena/src/stack.bpf.cSignature · 1 line
2.47.3
PATCH 1/13 bpf: Add bpf_stream_print_stack stack dumping kfunc
Add a new kfunc called bpf_stream_print_stack to be used by programs that need to print out their current BPF stack. The kfunc is essentially a wrapper around the existing bpf_stream_dump_stack functionality used to generate stack traces for error events like may_goto violations and BPF-side arena page faults.
--- kernel/bpf/helpers.c | 1 + kernel/bpf/stream.c | 13 +++++++++++++ tools/lib/bpf/bpf_helpers.h | 2 ++ 3 files changed, 16 insertions(+)
kernel/bpf/helpers.c +1 -0
@@ -4534,6 +4534,7 @@ BTF_ID_FLAGS(func, bpf_strncasestr); BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)#endif BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_IMPLICIT_ARGS) +BTF_ID_FLAGS(func, bpf_stream_print_stack, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_IMPLICIT_ARGS)BTF_ID_FLAGS (func, bpf_dynptr_from_file)
kernel/bpf/stream.c +13 -0
@@ -245,6 +245,19 @@ __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const voreturn ret; } +/* Directly trigger a stack dump from the program. */ +__bpf_kfunc void bpf_stream_print_stack(struct bpf_prog_aux *aux) +{ +struct bpf_stream_stage ss; +struct bpf_prog *prog; + + prog = aux->main_prog_aux->prog; + +bpf_stream_stage (ss, prog,BPF_STDERR , ({ +bpf_stream_dump_stack (ss); + })); +} +__bpf_kfunc_end_defs ();/* Added kfunc to common_btf_ids */
tools/lib/bpf/bpf_helpers.h +2 -0
@@ -343,6 +343,8 @@ extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *a/* Helper macro to print out debug messages */ #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args) +extern void bpf_stream_print_stack(void) __weak __ksym; +struct bpf_iter_num ; extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __weak __ksym;
Signature · 1 line
2.47.3
PATCH 2/13 bpf: Allow BPF stream kfuncs while holding a lock
In reply to Emil TsalapatisThe BPF stream kfuncs bpf_stream_vprintk and bpf_stream_print_stack do not sleep and so are safe to call while holding a lock. Amend the verifier to allow that.
--- kernel/bpf/verifier.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +12 -1
@@ -12455,6 +12455,8 @@ enum special_kfunc_type { KF_bpf_arena_alloc_pages, KF_bpf_arena_free_pages, KF_bpf_arena_reserve_pages, + KF_bpf_stream_vprintk, + KF_bpf_stream_print_stack, };BTF_ID_LIST (special_kfunc_list)
@@ -12532,6 +12534,8 @@ BTF_ID(func, bpf_task_work_schedule_resume) BTF_ID(func, bpf_arena_alloc_pages) BTF_ID(func, bpf_arena_free_pages) BTF_ID(func, bpf_arena_reserve_pages) +BTF_ID(func, bpf_stream_vprintk) +BTF_ID(func, bpf_stream_print_stack)static bool is_task_work_add_kfunc (u32 func_id) {
@@ -12974,10 +12978,17 @@ static bool is_bpf_arena_kfunc(u32 btf_id) btf_id == special_kfunc_list[KF_bpf_arena_reserve_pages]; } +static bool is_bpf_stream_kfunc (u32 btf_id) +{ +return btf_id == special_kfunc_list[KF_bpf_stream_vprintk] || + btf_id == special_kfunc_list[KF_bpf_stream_print_stack]; +} +static bool kfunc_spin_allowed (u32 btf_id) {return is_bpf_graph_api_kfunc (btf_id) ||is_bpf_iter_num_api_kfunc (btf_id) || -is_bpf_res_spin_lock_kfunc (btf_id) ||is_bpf_arena_kfunc (btf_id); +is_bpf_res_spin_lock_kfunc (btf_id) ||is_bpf_arena_kfunc (btf_id) || +is_bpf_stream_kfunc (btf_id); }static bool is_sync_callback_calling_kfunc (u32 btf_id)
Signature · 1 line
2.47.3
PATCH 3/13 selftests: bpf: Move bpf_arena_spin_lock.h to the top level
In reply to Emil TsalapatisThe 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.
---
tools/testing/selftests/bpf/{progs => }/bpf_arena_spin_lock.h | 4 ++--
tools/testing/selftests/bpf/progs/arena_spin_lock.c | 2 +-
2 files changed, 3 insertions(+), 3 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
@@ -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/progs/arena_spin_lock.c +1 -1
@@ -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.47.3
PATCH 4/13 selftests: bpf: Make WRITE_ONCE macro in bpf_atomic.h conditional
In reply to Emil TsalapatisThe WRITE_ONCE macro is identically defined both in bpf_atomic.h and in bpf_arena_common.h. The bpf_arena_common.h definition is guarded with an ifndef to allow for inclusion after bpf_atomic.h, but the opposite does not hold. Add an extra guard to avoid requiring a specific order for the headers.
--- tools/testing/selftests/bpf/bpf_atomic.h | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/bpf/bpf_atomic.h +2 -0
@@ -42,7 +42,9 @@ extern bool CONFIG_X86_64 __kconfig __weak;#define READ_ONCE (x) (*(volatile typeof(x) *)&(x)) +#ifndef WRITE_ONCE #define WRITE_ONCE (x, val) ((*(volatile typeof(x) *)&(x)) = (val)) +#endif #define cmpxchg (p, old, new) __sync_val_compare_and_swap((p), old, new)
Signature · 1 line
2.47.3
PATCH 5/13 selftests: bpf: Add basic libarena scaffolding
In reply to Emil TsalapatisAdd 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.
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 12 + tools/testing/selftests/bpf/libarena/Makefile | 47 ++++ .../selftests/bpf/libarena/include/common.h | 118 +++++++++ .../selftests/bpf/libarena/include/userapi.h | 22 ++ .../bpf/libarena/selftests/selftest.c | 243 ++++++++++++++++++ .../bpf/libarena/selftests/selftest.h | 17 ++ 7 files changed, 460 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/userapi.h create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.c create mode 100644 tools/testing/selftests/bpf/libarena/selftests/selftest.h
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -48,3 +48,4 @@ verification_cert.h *.BTF *.BTF_ids *.BTF.base +libarena/test_libarena
tools/testing/selftests/bpf/Makefile +12 -0
@@ -915,3 +915,15 @@ 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 all \ + BPFTOOL="$(BPFTOOL)" \ + INCLUDE_DIR="$(HOST_INCLUDE_DIR)" \ + LIBBPF_INCLUDE="$(HOST_INCLUDE_DIR)" \ + BPFOBJ="$(BPFOBJ)" \ + LDLIBS="$(LDLIBS)" \ + CLANG="$(CLANG)" \ + BPF_CFLAGS="$(BPF_CFLAGS) $(CLANG_CFLAGS)" \ + BPF_TARGET_ENDIAN="$(BPF_TARGET_ENDIAN)" \ + Q="$(Q)"
tools/testing/selftests/bpf/libarena/Makefile new file +47 -0
47 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/include/common.h new file +118 -0
118 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/include/userapi.h new file +22 -0
@@ -0,0 +1,22 @@ +#pragma once + +/* +* Header for the userspace C programs that load +* and initialize the BPF code. +*/ + +#define __arena + +typedef uint8_t u8 ; +typedef uint16_t u16 ; +typedef uint32_t u32 ; +typedef uint64_t u64 ; +typedef int8_t s8 ; +typedef int16_t s16 ; +typedef int32_t s32 ; +typedef int64_t s64 ; + +/* Dummy "definition" for userspace. */ +#define arena_spinlock_t u64 + +#include "common.h"
tools/testing/selftests/bpf/libarena/selftests/selftest.c new file +243 -0
243 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/selftests/selftest.h new file +17 -0
@@ -0,0 +1,17 @@ +#pragma once + +#define ALLOC_SELFTEST (func, ...) \ + do { \ + int ret = func(__VA_ARGS__); \ + if (ret) { \ + bpf_printk("SELFTEST %s FAIL: %d", #func, ret); \ + return ret; \ + } \ + } while (0) + +#ifndef __BPF__ + +/* Dummy "definition" for userspace. */ +#define arena_spinlock_t u64 + +#endif /* __BPF__ */
Signature · 1 line
2.47.3
PATCH 6/13 selftests: bpf: Add arena ASAN runtime to libarena
In reply to Emil TsalapatisAdd an address sanitizer (ASAN) runtime to the arena library. The ASAN runtime implements the functions injected into BPF binaries by LLVM sanitization is supported and enabled.
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.
--- tools/testing/selftests/bpf/.gitignore | 1 + .../selftests/bpf/libarena/include/asan.h | 133 +++++ .../selftests/bpf/libarena/src/asan.bpf.c | 463 ++++++++++++++++++ 3 files changed, 597 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/.gitignore +1 -0
@@ -49,3 +49,4 @@ verification_cert.h *.BTF_ids *.BTF.base libarena/test_libarena +libarena/test_libarena_asan
tools/testing/selftests/bpf/libarena/include/asan.h new file +133 -0
133 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +463 -0
463 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 06/13] selftests: bpf: Add arena ASAN runtime to libarena
The format string has four format specifiers (%p, %lx, %s, %ld) but only three arguments are provided (addr, write?"write":"read", sz). Is there a missing argument for the (0x%lx) specifier, or…
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +0
-0
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +4
-0+static __always_inline int asan_report(s8a __arg_arena *addr, size_t sz, +bool write) +{ +/* Only report the first ASAN violation. */
tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 142
Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +10
-0+if (asan_reported && asan_report_once) +return 0 ; + + asan_reported = true; + + asan_violated = (u64 )addr; + +if (asan_emit_stack) { +arena_stderr ("Memory violation for address %p (0x%lx) for %s of size %ld" , + addr, write ?"write" :"read" , sz);
The format string has four format specifiers (%p, %lx, %s, %ld) but only three arguments are provided (addr, write?"write":"read", sz). Is there a missing argument for the (0x%lx) specifier, or should that part of the format string be removed?
tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 147
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +5
-0+bpf_stream_print_stack (); + } + +return 0 ; +}
---
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/21256973563
Emil Tsalapatis
reviewerRe: [PATCH 06/13] selftests: bpf: Add arena ASAN runtime to libarena
Quoted 19 lines · bot+bpf-ci
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +0
-0[ ... ]
Quoted 11 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +11
-0+static __always_inline int asan_report(s8a __arg_arena *addr, size_t sz, +bool write) +{ +/* Only report the first ASAN violation. */ +if (asan_reported && asan_report_once) +return 0 ; + + asan_reported = true; + + asan_violated = (u64 )addr; +
Quoted 9 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 142
Quoted 3 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +3
-0+if (asan_emit_stack) { +arena_stderr ("Memory violation for address %p (0x%lx) for %s of size %ld" , + addr, write ?"write" :"read" , sz);The format string has four format specifiers (%p, %lx, %s, %ld) but only three arguments are provided (addr, write?"write":"read", sz). Is there a missing argument for the (0x%lx) specifier, or should that part of the format string be removed?
Ack, there is a missing argument - the scalar representation of the arena address. Will add.
Quoted 12 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c line 147
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +5
-0+bpf_stream_print_stack (); + } + +return 0 ; +}---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21256973563
PATCH 7/13 selftests: bpf: Add ASAN support for libarena selftests
In reply to Emil TsalapatisExpand 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/Makefile | 14 +++- tools/testing/selftests/bpf/libarena/Makefile | 26 ++++++- .../selftests/bpf/libarena/include/userapi.h | 1 + .../bpf/libarena/selftests/selftest.c | 69 ++++++++++++++++++- .../bpf/libarena/selftests/st_asan_common.h | 49 +++++++++++++ 5 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_common.h
tools/testing/selftests/bpf/Makefile +13 -1
@@ -917,7 +917,19 @@ override define INSTALL_RULE endef test_libarena: $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) - +$(MAKE) -C libarena all \ + +$(MAKE) -C libarena $@ \ + BPFTOOL="$(BPFTOOL)" \ + INCLUDE_DIR="$(HOST_INCLUDE_DIR)" \ + LIBBPF_INCLUDE="$(HOST_INCLUDE_DIR)" \ + BPFOBJ="$(BPFOBJ)" \ + LDLIBS="$(LDLIBS)" \ + CLANG="$(CLANG)" \ + BPF_CFLAGS="$(BPF_CFLAGS) $(CLANG_CFLAGS)" \ + BPF_TARGET_ENDIAN="$(BPF_TARGET_ENDIAN)" \ + Q="$(Q)" + +test_libarena_asan: $(INCLUDE_DIR)/vmlinux.h $(BPFOBJ) + +$(MAKE) -C libarena $@ \ BPFTOOL="$(BPFTOOL)" \ INCLUDE_DIR="$(HOST_INCLUDE_DIR)" \ LIBBPF_INCLUDE="$(HOST_INCLUDE_DIR)" \
tools/testing/selftests/bpf/libarena/Makefile +24 -2
@@ -5,6 +5,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),)
@@ -14,6 +15,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
@@ -25,23 +33,37 @@ CFLAGS += $(INCLUDES) vpath %.bpf.c $(LIBARENA)/src $(LIBARENA)/selftests vpath %.c $(LIBARENA)/src $(LIBARENA)/selftests -all: test_libarena +test_libarena_asan: selftest.c $(BPFOBJ) selftest_asan.skel.h + $(call msg,BINARY,libarena,$@) + $(Q)$(CLANG) $(LDLIBS) $(CFLAGS) -DBPF_ARENA_ASAN $< $(BPFOBJ) -o $@ test_libarena: selftest.c $(BPFOBJ) selftest.skel.h $(call msg,BINARY,libarena,$@) $(Q)$(CLANG) $(LDLIBS) $(CFLAGS) $< $(BPFOBJ) -o $@ +selftest_asan.skel.h: main_asan.bpf.o + $(call msg,GEN-SKEL,libarena,$@) + $(Q)$(BPFTOOL) gen skeleton $< name "selftest_asan" > $@ + selftest.skel.h: main.bpf.o $(call msg,GEN-SKEL,libarena,$@) $(Q)$(BPFTOOL) gen skeleton $< name "selftest" > $@ +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 $< -mcpu=v3 -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 test_libarena test_libarena_asan
tools/testing/selftests/bpf/libarena/include/userapi.h +1 -0
@@ -20,3 +20,4 @@ typedef int64_t s64;#define arena_spinlock_t u64#include "common.h" +#include "asan.h"
tools/testing/selftests/bpf/libarena/selftests/selftest.c +68 -1
96 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/selftests/st_asan_common.h new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 07/13] selftests: bpf: Add ASAN support for libarena selftests
The ASAN detection logic compares against "selftest" (8 chars), but the actual binary names are "test_libarena" (13 chars) and "test_libarena_asan" (18 chars). Since strlen("test_libarena") > 8…
Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +0
-0
[ ... ]
tools/testing/selftests/bpf/libarena/selftests/selftest.c static void · line 280
Quoted 13 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +9
-0@@ -211,8 +269,17 @@ static void banner(const char *progpath) {char *name =basename (progpath); +bool is_asan; + +/* +* Check if our BPF programs are ASAN-capable by inspecting the prog name. +* Command line arguments are guaranteed to be NULL-terminated, use strlen. +* Calculate the hardcoded name's length at compile time. +*/ +printf ("%s\n" , name); + is_asan =strlen (name) > (sizeof ("selftest" ) -1 );
The ASAN detection logic compares against "selftest" (8 chars), but the actual binary names are "test_libarena" (13 chars) and "test_libarena_asan" (18 chars). Since strlen("test_libarena") > 8 evaluates to true, the non-ASAN binary will also be detected as ASAN-enabled.
Should the comparison be against "test_libarena" instead of "selftest"? Or perhaps using strstr(name, "_asan") would be more robust?
tools/testing/selftests/bpf/libarena/selftests/selftest.c static void · line 283
Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +2
-1+ -printf ("=== %s ===\n" ,"libarena selftests" ); +printf ("=== %s %s===\n" ,"libarena selftests" , is_asan ?"(asan) " :"" ); }
---
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/21256973563
Emil Tsalapatis
reviewerRe: [PATCH 07/13] selftests: bpf: Add ASAN support for libarena selftests
Quoted 27 lines · bot+bpf-ci
Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +0
-0[ ... ]
tools/testing/selftests/bpf/libarena/selftests/selftest.c static void · line 280
Quoted 13 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +9
-0@@ -211,8 +269,17 @@ static void banner(const char *progpath) {char *name =basename (progpath); +bool is_asan; + +/* +* Check if our BPF programs are ASAN-capable by inspecting the prog name. +* Command line arguments are guaranteed to be NULL-terminated, use strlen. +* Calculate the hardcoded name's length at compile time. +*/ +printf ("%s\n" , name); + is_asan =strlen (name) > (sizeof ("selftest" ) -1 );The ASAN detection logic compares against "selftest" (8 chars), but the actual binary names are "
test_libarena" (13 chars) and "test_libarena_asan" (18 chars). Sincestrlen("test_libarena")> 8 evaluates to true, the non-ASAN binary will also be detected as ASAN-enabled.
Quoted 3 lines
Should the comparison be against "
test_libarena" instead of "selftest"? Or perhaps usingstrstr(name, "_asan")would be more robust?
Ack, test_libarena was initially called "selftest" and was renamed for uniformity. strstr() is the better idea, will add in the revision.
Quoted 11 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c static void · line 283
Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +2
-1+ -printf ("=== %s ===\n" ,"libarena selftests" ); +printf ("=== %s %s===\n" ,"libarena selftests" , is_asan ?"(asan) " :"" ); }---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21256973563
PATCH 8/13 selftest: bpf: Add bump allocator for libarena
In reply to Emil TsalapatisAdd an arena-based bump allocator to the arenalib. The allocator is aimed towards data allocated at program startup whose lifetime is that of the BPF program. An example is CPU-related information for sched-ext scheduler.
--- .../selftests/bpf/libarena/include/bump.h | 20 ++ .../selftests/bpf/libarena/src/bump.bpf.c | 212 ++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 tools/testing/selftests/bpf/libarena/include/bump.h create mode 100644 tools/testing/selftests/bpf/libarena/src/bump.bpf.c
tools/testing/selftests/bpf/libarena/include/bump.h new file +20 -0
@@ -0,0 +1,20 @@ +#pragma once + +#ifdef __BPF__ + +u64 bump_alloc_internal (size_t bytes,size_t alignment); +#define bump_alloc (bytes, alignment) ((void __arena *)bump_alloc_internal((bytes), (alignment))) +int bump_init (size_t max_alloc_pages); +int bump_destroy (void ); +int bump_memlimit (u64 lim_memusage); + +#endif /* __BPF__ */ + +struct bump { +size_t max_contig_bytes; + void __arena *memory; +size_t off; +size_t lim_memusage; +size_t cur_memusage; +}; +
tools/testing/selftests/bpf/libarena/src/bump.bpf.c new file +212 -0
212 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH 9/13 selftests: bpf: Add libarena selftests for the bump allocator
In reply to Emil TsalapatisAdd testing for the libarena bump allocator. The testing covers the behavior of the allocator both by itself and in combination with ASAN to test that there are no false positives or negatives.
--- .../bpf/libarena/selftests/selftest.c | 12 + .../bpf/libarena/selftests/st_asan_bump.bpf.c | 193 ++++++++++++ .../bpf/libarena/selftests/st_bump.bpf.c | 275 ++++++++++++++++++ 3 files changed, 480 insertions(+) create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c
tools/testing/selftests/bpf/libarena/selftests/selftest.c +12 -0
@@ -265,6 +265,12 @@ error: \return ret; \ } +TEST (bump_selftest); + +#ifdef BPF_ARENA_ASAN +TEST (asan_test_bump); +#endif +static void banner (const char *progpath) {
@@ -306,5 +312,11 @@ int main(int argc, char *argv[])libbpf_set_print (libbpf_print_fn); +run_bump_selftest (); + +#ifdef BPF_ARENA_ASAN +run_asan_test_bump (); +#endif +return 0 ; }
tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c new file +193 -0
193 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +275 -0
275 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 09/13] selftests: bpf: Add libarena selftests for the bump allocator
Is sizeof(blob) intended here? Since blob is declared as a pointer (volatile struct blob __arena *blob), sizeof(blob) returns the size of the pointer (8 bytes on 64-bit) rather than sizeof(struct…
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c new file +0
-0
[ ... ]
tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c line 23
Quoted 6 lines
tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c new file +6
-0+int asan_test_bump_blob_one (void ) +{ + volatile struct blob __arena *blob; +const size_t alignment =1 ; + + blob =bump_alloc (sizeof (blob) -1 , alignment);
^^^^^^^^^^^^
Is sizeof(blob) intended here? Since blob is declared as a pointer (volatile struct blob __arena *blob), sizeof(blob) returns the size of the pointer (8 bytes on 64-bit) rather than sizeof(struct blob) which is 60 bytes.
This means the allocation is only 7 bytes, but the test later accesses blob->oob at offset 59. Should this be sizeof(*blob) - 1 instead?
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +0
-0
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +2
-0+#define ST_MAX_PAGES 8 +#define ST_MAX_BYTES (ST_MAX_PAGES * __PAGE_SIZE)
[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +7
-0+static int bump_selftest_alloc_multiple (u64 bytes,u64 alignment) +{ + void __arena *mem1, *mem2; +int ret; + +/* Initialize the allocator */ + ret =bump_init (ST_MAX_PAGES );
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 156
Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +10
-0+if (ret) { +bpf_printk ("bump_init failed with %d" , ret); +return ret; + } + + mem1 =ALLOC_OR_FAIL (bytes, alignment); +st_memset (mem1,ST_PATTERN1 , bytes); + + mem2 =ALLOC_OR_FAIL (bytes, alignment); +st_memset (mem2,ST_PATTERN1 ,ST_PATTERN2 );
^^^^^^^^^^^ ^^^^^^^^^^^
The st_memset signature is st_memset(mem, byte, size). This call passes ST_PATTERN1 (0xAA) as the byte pattern and ST_PATTERN2 (0x55 = 85) as the size, but later CHECK_OR_FAIL(mem2, ST_PATTERN2, bytes) expects mem2 to contain ST_PATTERN2.
Should this be st_memset(mem2, ST_PATTERN2, bytes)?
Note: This code is currently unreachable due to the loop condition issue described below.
[ ... ]
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 255
Quoted 7 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +7
-0+SEC("syscall") +int bump_selftest (void ) +{ +u64 bytes =128 ; +u64 alignment =1 ; + + for (bytes = __PAGE_SIZE; bytes <= ST_MAX_PAGES && can_loop;
^^^^^^^^^^^^^^^^^^^^^^^
The loop condition compares bytes (in bytes, initialized to __PAGE_SIZE which is typically 4096) against ST_MAX_PAGES (8, a page count). Since 4096 > 8 is always true, the condition bytes <= ST_MAX_PAGES is always false and this loop never executes.
Should this compare against ST_MAX_BYTES instead?
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 267
Quoted 12 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +12
-0+ bytes <<= 1) { +for (alignment =1 ; alignment <=ST_MAX_ALIGNMENT && can_loop; + alignment <<=1 ) { +/* Each test manages its own allocator lifecycle */ +BUMP_ALLOC_SELFTEST (alloc_single, bytes, alignment); +BUMP_ALLOC_SELFTEST (alloc_multiple, bytes, alignment); + } + } + +BUMP_ALLOC_SELFTEST (alloc_aligned); + + for (alignment = __PAGE_SIZE; bytes <= ST_MAX_PAGES && can_loop;
^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 268
Quoted 1 line
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +1
-0+ bytes <<= 1)
This loop initializes alignment but tests and advances bytes. The condition bytes <= ST_MAX_PAGES is false (bytes retains its value from the previous loop), so this loop also never executes.
Was the intent to iterate over different alignment values? If so, both the condition and the increment should use alignment.
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 273
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +5
-0+BUMP_ALLOC_SELFTEST (alloc_exhaustion, +ST_MAX_PAGES * __PAGE_SIZE, alignment); + +return 0 ; +}
---
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/21256973563
Emil Tsalapatis
reviewerRe: [PATCH 09/13] selftests: bpf: Add libarena selftests for the bump allocator
Quoted 21 lines · bot+bpf-ci
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c new file +0
-0[ ... ]
tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c line 23
Quoted 6 lines
tools/testing/selftests/bpf/libarena/selftests/st_asan_bump.bpf.c new file +6
-0+int asan_test_bump_blob_one (void ) +{ + volatile struct blob __arena *blob; +const size_t alignment =1 ; + + blob =bump_alloc (sizeof (blob) -1 , alignment);^^^^^^^^^^^^Is sizeof(blob) intended here? Since blob is declared as a pointer (volatile struct blob
__arena*blob), sizeof(blob) returns the size of the pointer (8 bytes on 64-bit) rather than sizeof(struct blob) which is 60 bytes.
Quoted 5 lines
This means the allocation is only 7 bytes, but the test later accesses
blob->oobat offset 59. Should this besizeof(*blob)- 1 instead?[ ... ]
Ack, the test is currently passing because we test only offsets 0 and sizeof(*blob) + 1. Will fix.
Quoted 37 lines
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +0
-0[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +2
-0+#define ST_MAX_PAGES 8 +#define ST_MAX_BYTES (ST_MAX_PAGES * __PAGE_SIZE)[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +7
-0+static int bump_selftest_alloc_multiple (u64 bytes,u64 alignment) +{ + void __arena *mem1, *mem2; +int ret; + +/* Initialize the allocator */ + ret =bump_init (ST_MAX_PAGES );tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 156
Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +10
-0+if (ret) { +bpf_printk ("bump_init failed with %d" , ret); +return ret; + } + + mem1 =ALLOC_OR_FAIL (bytes, alignment); +st_memset (mem1,ST_PATTERN1 , bytes); + + mem2 =ALLOC_OR_FAIL (bytes, alignment); +st_memset (mem2,ST_PATTERN1 ,ST_PATTERN2 );^^^^^^^^^^^ ^^^^^^^^^^^The
st_memsetsignature isst_memset(mem, byte, size). This call passesST_PATTERN1(0xAA) as the byte pattern andST_PATTERN2(0x55= 85) as the size, but laterCHECK_OR_FAIL(mem2, ST_PATTERN2, bytes)expects mem2 to containST_PATTERN2.
Quoted 5 lines
Should this be
st_memset(mem2, ST_PATTERN2, bytes)?Note: This code is currently unreachable due to the loop condition issue described below.
Ack, will update.
Quoted 11 lines
[ ... ]
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 255
Quoted 7 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +7
-0+SEC("syscall") +int bump_selftest (void ) +{ +u64 bytes =128 ; +u64 alignment =1 ; + + for (bytes = __PAGE_SIZE; bytes <= ST_MAX_PAGES && can_loop;^^^^^^^^^^^^^^^^^^^^^^^
Quoted 7 lines
The loop condition compares bytes (in bytes, initialized to
__PAGE_SIZEwhich is typically 4096) againstST_MAX_PAGES(8, a page count). Since 4096 > 8 is always true, the condition bytes <=ST_MAX_PAGESis always false and this loop never executes.Should this compare against
ST_MAX_BYTESinstead?
Ack, will use ST_MAX_PAGES * __PAGE_SIZE as the bound.
Quoted 13 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 267
Quoted 12 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +12
-0+ bytes <<= 1) { +for (alignment =1 ; alignment <=ST_MAX_ALIGNMENT && can_loop; + alignment <<=1 ) { +/* Each test manages its own allocator lifecycle */ +BUMP_ALLOC_SELFTEST (alloc_single, bytes, alignment); +BUMP_ALLOC_SELFTEST (alloc_multiple, bytes, alignment); + } + } + +BUMP_ALLOC_SELFTEST (alloc_aligned); + + for (alignment = __PAGE_SIZE; bytes <= ST_MAX_PAGES && can_loop;^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
Quoted 9 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 268
Quoted 1 line
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +1
-0+ bytes <<= 1)This loop initializes alignment but tests and advances bytes. The condition bytes <=
ST_MAX_PAGESis false (bytes retains its value from the previous loop), so this loop also never executes.Was the intent to iterate over different alignment values? If so, both the condition and the increment should use alignment.
Ack, same issue and solution as above.
Quoted 12 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c line 273
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/st_bump.bpf.c new file +5
-0+BUMP_ALLOC_SELFTEST (alloc_exhaustion, +ST_MAX_PAGES * __PAGE_SIZE, alignment); + +return 0 ; +}---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21256973563
PATCH 10/13 selftest: bpf: Add libarena stack allocator
In reply to Emil TsalapatisAdd a page-oriented stack allocator to libarena. The allocator is targeted towards large, page-aligned allocations for BPF programs and can serve as an intermediate location for pages freed by custom allocators like slab allocators to avoid constantly allocating and freeing arena pages.
--- .../selftests/bpf/libarena/include/stack.h | 44 +++ .../selftests/bpf/libarena/src/stack.bpf.c | 338 ++++++++++++++++++ 2 files changed, 382 insertions(+) create mode 100644 tools/testing/selftests/bpf/libarena/include/stack.h create mode 100644 tools/testing/selftests/bpf/libarena/src/stack.bpf.c
tools/testing/selftests/bpf/libarena/include/stack.h new file +44 -0
@@ -0,0 +1,44 @@ +#pragma once + +struct stk_seg ; +typedef struct stk_seg __arena stk_seg_t; + +/* +* We devote a single page to stk_seg, and size +* the void * array so it fits exactly in it. +* Account for prev/next pointers (2 * sizeof(void *)). +*/ +#define STK_SEG_MAX ((__PAGE_SIZE - 2 * sizeof(void *)) / sizeof(void *)) + +struct stk_seg { + void __arena *elems[STK_SEG_MAX]; +stk_seg_t *prev; +stk_seg_t *next; +}; + +/* +* Extensible stack struct. +*/ +struct stk { + arena_spinlock_t __arena *lock; + +stk_seg_t *first;/* First stack segment. */ +stk_seg_t *last; + +stk_seg_t *current;/* Current stack segment. */ +__u64 cind; + +__u64 capacity;/* Free slots in the stack. */ +__u64 available;/* Available items in the stack. */ +__u64 data_size; +__u64 nr_pages_per_alloc; +}; + + +u64 stk_alloc (struct stk *stack); +int stk_init(struct stk *stackp, arena_spinlock_t __arena *lock, +__u64 data_size,__u64 nr_pages_per_alloc); +void stk_destroy (struct stk *stack); +int stk_free_internal (struct stk *stack,__u64 elem); + +#define stk_free (stack, elem) stk_free_internal(stack, (__u64)elem)
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +338 -0
338 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 10/13] selftest: bpf: Add libarena stack allocator
Can the data_seg memory be leaked when capacity is non-zero but less than nelems? The check here only allocates a new stack segment when capacity is exactly zero. If, for example, capacity is 5 but…
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +0
-0
[ ... ]
Quoted 14 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +14
-0+static int stk_fill_new_elems (struct stk *stack) +{ +stk_seg_t *data_seg, *stk_seg; +bool need_stk_seg; +__u64 nr_pages; +size_t nelems; +int ret, i; +u64 mem; + + nr_pages = stack->nr_pages_per_alloc; + nelems = (nr_pages * __PAGE_SIZE) / stack->data_size; +if (nelems >STK_SEG_MAX ) { +return -EINVAL ; + }
tools/testing/selftests/bpf/libarena/src/stack.bpf.c line 277
Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +10
-0+ +/* +* If we have more than two empty segments available, +* try to repurpose one of them into an allocation. +*/ + ret =stk_seg_to_data (stack, nelems); +if (!ret) +return 0 ; + + need_stk_seg = !stack->capacity;
^^^^^^^^^^^^^^^^
Can the data_seg memory be leaked when capacity is non-zero but less than nelems? The check here only allocates a new stack segment when capacity is exactly zero. If, for example, capacity is 5 but nelems is 16, the loop below will fail partway through stk_push() with -ENOSPC after pushing only 5 elements. The remaining chunks from data_seg would never be tracked.
Consider this scenario:
1. Stack has one segment with capacity=5 (few free slots)
2. stk_seg_to_data() fails since only one segment exists
3. need_stk_seg = false since capacity > 0
4. stk_get_arena_memory() allocates only data_seg (no stk_seg)
5. Loop tries to push nelems (say 16) chunks
6. After 5 pushes, stk_push() returns -ENOSPC
7. Remaining 11 chunks from data_seg are lost
Should this be checking capacity < nelems instead of !capacity?
Quoted 9 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +9
-0+ +/* Get memory for a new data segment, and possibly a stack segment if necessary. */ + ret =stk_get_arena_memory (stack, nr_pages, &data_seg, need_stk_seg ? &stk_seg :NULL ); +if (ret) +return ret; + +if (need_stk_seg) +stk_extend (stack, stk_seg); +
tools/testing/selftests/bpf/libarena/src/stack.bpf.c line 296
Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +10
-0+ mem = (u64 )data_seg; +for (i = zero; i < nelems && can_loop; i++) { + ret = stk_push(stack, (void __arena *)mem); +if (ret) +return ret; + mem += stack->data_size; + } + +return 0 ; +}
[ ... ]
---
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/21256973563
Emil Tsalapatis
reviewerRe: [PATCH 10/13] selftest: bpf: Add libarena stack allocator
Quoted 50 lines · bot+bpf-ci
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +0
-0[ ... ]
Quoted 14 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +14
-0+static int stk_fill_new_elems (struct stk *stack) +{ +stk_seg_t *data_seg, *stk_seg; +bool need_stk_seg; +__u64 nr_pages; +size_t nelems; +int ret, i; +u64 mem; + + nr_pages = stack->nr_pages_per_alloc; + nelems = (nr_pages * __PAGE_SIZE) / stack->data_size; +if (nelems >STK_SEG_MAX ) { +return -EINVAL ; + }tools/testing/selftests/bpf/libarena/src/stack.bpf.c line 277
Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +10
-0+ +/* +* If we have more than two empty segments available, +* try to repurpose one of them into an allocation. +*/ + ret =stk_seg_to_data (stack, nelems); +if (!ret) +return 0 ; + + need_stk_seg = !stack->capacity;^^^^^^^^^^^^^^^^Can the
data_segmemory be leaked when capacity is non-zero but less than nelems? The check here only allocates a new stack segment when capacity is exactly zero. If, for example, capacity is 5 but nelems is 16, the loop below will fail partway throughstk_push()with-ENOSPCafter pushing only 5 elements. The remaining chunks fromdata_segwould never be tracked.Consider this scenario:
1. Stack has one segment with capacity=5 (few free slots)
2.stk_seg_to_data()fails since only one segment exists
3.need_stk_seg= false since capacity > 0
4.stk_get_arena_memory()allocates onlydata_seg(nostk_seg)
5. Loop tries to push nelems (say 16) chunks
6. After 5 pushes,stk_push()returns-ENOSPC
7. Remaining 11 chunks fromdata_segare lost
Quoted 2 lines
Should this be checking capacity < nelems instead of !capacity?
Ack, the AI's explanation holds. The bug was introduced during refactoring - will fix.
Quoted 28 lines
Quoted 9 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +9
-0+ +/* Get memory for a new data segment, and possibly a stack segment if necessary. */ + ret =stk_get_arena_memory (stack, nr_pages, &data_seg, need_stk_seg ? &stk_seg :NULL ); +if (ret) +return ret; + +if (need_stk_seg) +stk_extend (stack, stk_seg); +tools/testing/selftests/bpf/libarena/src/stack.bpf.c line 296
Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +10
-0+ mem = (u64 )data_seg; +for (i = zero; i < nelems && can_loop; i++) { + ret = stk_push(stack, (void __arena *)mem); +if (ret) +return ret; + mem += stack->data_size; + } + +return 0 ; +}[ ... ]
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/21256973563
PATCH 11/13 selftests: bpf: Add selftests for the libarena stack allocator
In reply to Emil TsalapatisAdd ASAN selftests for the stack allocators to ensure proper ASAN integration.
--- .../bpf/libarena/selftests/selftest.c | 2 + .../libarena/selftests/st_asan_stack.bpf.c | 253 ++++++++++++++++++ 2 files changed, 255 insertions(+) create mode 100644 tools/testing/selftests/bpf/libarena/selftests/st_asan_stack.bpf.c
tools/testing/selftests/bpf/libarena/selftests/selftest.c +2 -0
@@ -269,6 +269,7 @@ TEST(bump_selftest);#ifdef BPF_ARENA_ASAN TEST (asan_test_bump); +TEST (asan_test_stack);#endif static void
@@ -316,6 +317,7 @@ int main(int argc, char *argv[])#ifdef BPF_ARENA_ASAN run_asan_test_bump (); +run_asan_test_stack ();#endif return 0 ;
tools/testing/selftests/bpf/libarena/selftests/st_asan_stack.bpf.c new file +253 -0
253 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH 12/13 selftests: bpf: Add buddy allocator for libarena
In reply to Emil TsalapatisAdd a byte-oriented buddy allocator for libarena. The buddy allocator provides an alloc/free interface for small arena allocations (down to 16 bytes).
--- .../selftests/bpf/libarena/include/buddy.h | 62 ++ .../selftests/bpf/libarena/src/buddy.bpf.c | 784 ++++++++++++++++++ 2 files changed, 846 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 +62 -0
@@ -0,0 +1,62 @@ +#pragma once + +/* Buddy allocator-related structs. */ + +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 { +BUDDY_MIN_ALLOC_SHIFT =4 , +BUDDY_MIN_ALLOC_BYTES =1 <<BUDDY_MIN_ALLOC_SHIFT , +BUDDY_CHUNK_NUM_ORDERS =1 <<4 ,/* 4 bits per order */ +BUDDY_CHUNK_BYTES =BUDDY_MIN_ALLOC_BYTES <<BUDDY_CHUNK_NUM_ORDERS , +BUDDY_HEADER_OFF =8 ,/* header byte offset, see buddy.bpf.c for details */ +BUDDY_CHUNK_PAGES =BUDDY_CHUNK_BYTES / __PAGE_SIZE, +BUDDY_CHUNK_ITEMS =1 <<BUDDY_CHUNK_NUM_ORDERS , +BUDDY_CHUNK_OFFSET_MASK =BUDDY_CHUNK_BYTES -1 , +BUDDY_VADDR_OFFSET =BUDDY_CHUNK_BYTES ,/* Start aligning at chunk */ +BUDDY_VADDR_SIZE =BUDDY_CHUNK_BYTES <<10 /* 1024 chunks maximum */ +}; + +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 1MiB 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 *prev; +buddy_chunk_t *next; +}; + +struct buddy { +buddy_chunk_t *first_chunk;/* Pointer to the chunk linked list. */ + arena_spinlock_t __arena *lock; /* Allocator lock */ +u64 vaddr;/* Allocation into reserved vaddr */ +}; + +#ifdef __BPF__ + +int buddy_init(struct buddy *buddy, arena_spinlock_t __arena *lock); +int buddy_destroy (struct buddy *buddy); +int buddy_free_internal (struct buddy *buddy,u64 free); +#define buddy_free (buddy, ptr) do { buddy_free_internal((buddy), (u64)(ptr)); } while (0) +u64 buddy_alloc_internal (struct buddy *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 +784 -0
784 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH 13/13 selftests: bpf: Add selftests for the libarena buddy allocator
In reply to Emil TsalapatisAdd regular and ASAN testing for the buddy allocator. This tests the allocator's correctness by itself and its integration with ASAN.
--- .../bpf/libarena/selftests/selftest.c | 4 + .../libarena/selftests/st_asan_buddy.bpf.c | 238 ++++++++++++++++++ .../bpf/libarena/selftests/st_buddy.bpf.c | 231 +++++++++++++++++ 3 files changed, 473 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
tools/testing/selftests/bpf/libarena/selftests/selftest.c +4 -0
@@ -266,10 +266,12 @@ error: \ }TEST (bump_selftest); +TEST (buddy_selftest);#ifdef BPF_ARENA_ASAN TEST (asan_test_bump);TEST (asan_test_stack); +TEST (asan_test_buddy);#endif static void
@@ -314,10 +316,12 @@ int main(int argc, char *argv[])libbpf_set_print (libbpf_print_fn);run_bump_selftest (); +run_buddy_selftest ();#ifdef BPF_ARENA_ASAN run_asan_test_bump ();run_asan_test_stack (); +run_asan_test_buddy ();#endif return 0 ;
tools/testing/selftests/bpf/libarena/selftests/st_asan_buddy.bpf.c new file +238 -0
238 lines not on this page · raw message
tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c new file +231 -0
231 lines not on this page · raw message
Signature · 1 line
2.47.3