Skip to contentSkip to conversation

bpf: Add arena ASAN runtime and BPF library

PATCH In review Emil Tsalapatis 5 participants 32 messages Jan 27 – Jan 28

Subsystems (5)
Kselftest BPF BPF selftests BPF core +1 more subsystems
lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

v2 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.

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 (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     |  26 +
 .../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      | 281 +++++++
 .../selftests/bpf/libarena/src/asan.bpf.c     | 463 +++++++++++
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 784 ++++++++++++++++++
 .../selftests/bpf/libarena/src/bump.bpf.c     | 196 +++++
 .../selftests/bpf/libarena/src/stack.bpf.c    | 338 ++++++++
 .../selftests/bpf/progs/arena_spin_lock.c     |   2 +-
 28 files changed, 3905 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.c
Signature · 1 line
2.49.0
author reviewer
#1 lore raw

PATCH v2 1/13 bpf: Add bpf_stream_print_stack stack dumping kfunc

From: Emil Tsalapatis <[email protected]>

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
index b54ec0e945aa..c30a9f68af6b 100644
@@ -4562,6 +4562,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
index 24730df55e69..6e2cbe5f34e7 100644
@@ -245,6 +245,19 @@ __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const vo
 	return 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
index c145da05a67c..6d8ae3887618 100644
@@ -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.49.0
#2 lore raw

Re: [PATCH v2 01/13] bpf: Add bpf_stream_print_stack stack dumping kfunc

Quoted 56 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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
index b54ec0e945aa..c30a9f68af6b 100644
@@ -4562,6 +4562,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
index 24730df55e69..6e2cbe5f34e7 100644
@@ -245,6 +245,19 @@ __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const vo
  	return 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 +0 -0
index c145da05a67c..6d8ae3887618 100644

tools/lib/bpf/bpf_helpers.h extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *a · line 347

Quoted 5 lines
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;
+

Is this needed? bpf_stream_print_stack() should be available via vmlinux.h, shouldn't it?

Quoted 3 lines
tools/lib/bpf/bpf_helpers.h +0 -0
  struct bpf_iter_num;
 
  extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __weak __ksym;
author reviewer
#3 lore raw

Re: [PATCH v2 01/13] bpf: Add bpf_stream_print_stack stack dumping kfunc

Quoted 60 lines · Mykyta Yatsenko
Quoted 59 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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
index b54ec0e945aa..c30a9f68af6b 100644
@@ -4562,6 +4562,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
index 24730df55e69..6e2cbe5f34e7 100644
@@ -245,6 +245,19 @@ __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const vo
  	return 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 +0 -0
index c145da05a67c..6d8ae3887618 100644
@@ -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)
 
Quoted 4 lines

tools/lib/bpf/bpf_helpers.h extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *a · line 347

Quoted 2 lines
tools/lib/bpf/bpf_helpers.h +2 -0
+extern void bpf_stream_print_stack(void) __weak __ksym;
+

Is this needed? bpf_stream_print_stack() should be available via vmlinux.h, shouldn't it?

Good call, I'll remove it in the next version.

Quoted 3 lines
Quoted 3 lines
tools/lib/bpf/bpf_helpers.h +0 -0
  struct bpf_iter_num;
 
  extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __weak __ksym;
#4 lore raw

Re: [PATCH v2 01/13] bpf: Add bpf_stream_print_stack stack dumping kfunc

In reply to Emil Tsalapatis
Quoted 35 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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
index b54ec0e945aa..c30a9f68af6b 100644
@@ -4562,6 +4562,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 +0 -0
index 24730df55e69..6e2cbe5f34e7 100644
@@ -245,6 +245,19 @@ __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const vo
        return ret;
 }

kernel/bpf/stream.c __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const vo · line 256

Quoted 10 lines
kernel/bpf/stream.c +9 -0
 
+/* 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, ({

I think it would be better not to hardcode the stream, and take an argument.

Quoted 23 lines
kernel/bpf/stream.c +4 -0
+               bpf_stream_dump_stack(ss);
+       }));
+}
+
 __bpf_kfunc_end_defs();
 
 /* Added kfunc to common_btf_ids */
tools/lib/bpf/bpf_helpers.h +2 -1
index c145da05a67c..6d8ae3887618 100644
@@ -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;
--

2.49.0

author reviewer
#5 lore raw

PATCH v2 2/13 bpf: Allow BPF stream kfuncs while holding a lock

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

The 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
index c2f2650db9fd..b7970537eb41 100644
@@ -12485,6 +12485,8 @@ enum special_kfunc_type {
 	KF_bpf_arena_free_pages,
 	KF_bpf_arena_reserve_pages,
 	KF_bpf_session_is_return,
+	KF_bpf_stream_vprintk,
+	KF_bpf_stream_print_stack,
 };
 
 BTF_ID_LIST(special_kfunc_list)
@@ -12563,6 +12565,8 @@ 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_session_is_return)
+BTF_ID(func, bpf_stream_vprintk)
+BTF_ID(func, bpf_stream_print_stack)
 
 static bool is_task_work_add_kfunc(u32 func_id)
 {
@@ -13007,10 +13011,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.49.0
maintainer Acked-by on v2 2/13
#6 lore raw

Re: [PATCH v2 02/13] bpf: Allow BPF stream kfuncs while holding a lock

Quoted 9 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

The 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.

---

Quoted 1 line

[...]

author reviewer
#7 lore raw

PATCH v2 3/13 selftests: bpf: Move bpf_arena_spin_lock.h to the top level

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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.

---
 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
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/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.49.0
maintainer Acked-by on v2 3/13
#8 lore raw

Re: [PATCH v2 03/13] selftests: bpf: Move bpf_arena_spin_lock.h to the top level

Quoted 6 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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.

"Since this header will be used in multiple object files that may be linked together, mark global variables and global functions as __weak to avoid linker errors."

Quoted 3 lines

---

Quoted 2 lines

[...]

author reviewer
#9 lore raw

PATCH v2 4/13 selftests: bpf: Make WRITE_ONCE macro in bpf_atomic.h conditional

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

The 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
index c550e5711967..d89a22d63c1c 100644
@@ -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.49.0
#10 lore raw

Re: [PATCH v2 04/13] selftests: bpf: Make WRITE_ONCE macro in bpf_atomic.h conditional

Quoted 18 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

The 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 +0 -0
index c550e5711967..d89a22d63c1c 100644

tools/testing/selftests/bpf/bpf_atomic.h extern bool CONFIG_X86_64 __kconfig __weak; · line 47

Quoted 6 lines
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

I wonder if we can avoid duplicating this macros, for example by including bpf_atomic.h in the bpf_arena_common.h.

Quoted 3 lines
tools/testing/selftests/bpf/bpf_atomic.h +0 -0
  #define cmpxchg(p, old, new) __sync_val_compare_and_swap((p), old, new)
 
author reviewer
#11 lore raw

Re: [PATCH v2 04/13] selftests: bpf: Make WRITE_ONCE macro in bpf_atomic.h conditional

Quoted 20 lines · Mykyta Yatsenko
Quoted 19 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

The 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 +0 -0
index c550e5711967..d89a22d63c1c 100644
@@ -42,7 +42,9 @@ extern bool CONFIG_X86_64 __kconfig __weak;
 
Quoted 7 lines

tools/testing/selftests/bpf/bpf_atomic.h extern bool CONFIG_X86_64 __kconfig __weak; · line 47

Quoted 5 lines
tools/testing/selftests/bpf/bpf_atomic.h +2 -0
  #define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
 
+#ifndef WRITE_ONCE
  #define WRITE_ONCE(x, val) ((*(volatile typeof(x) *)&(x)) = (val))
+#endif

I wonder if we can avoid duplicating this macros, for example by including bpf_atomic.h in the bpf_arena_common.h.

I kept both definitions because the headers correspond to unrelated features (atomics and arenas). Including bpf_atomic.h in bpf_arena_common.h would work, and we'd need to update just a single test that includes bpf_atomic.h directly (lpm_trie_bench). However, any program that wanted to include atomics would also have to include arena-related definitions. If that sounds reasonable I'll update the diff on the next version.

Quoted 3 lines
Quoted 3 lines
tools/testing/selftests/bpf/bpf_atomic.h +0 -0
  #define cmpxchg(p, old, new) __sync_val_compare_and_swap((p), old, new)
 
#12 lore raw

Re: [PATCH v2 04/13] selftests: bpf: Make WRITE_ONCE macro in bpf_atomic.h conditional

Quoted 30 lines · Emil Tsalapatis
Quoted 20 lines · Mykyta Yatsenko
Quoted 19 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

The 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 +0 -0
index c550e5711967..d89a22d63c1c 100644
@@ -42,7 +42,9 @@ extern bool CONFIG_X86_64 __kconfig __weak;
 
Quoted 7 lines

tools/testing/selftests/bpf/bpf_atomic.h extern bool CONFIG_X86_64 __kconfig __weak; · line 47

Quoted 5 lines
tools/testing/selftests/bpf/bpf_atomic.h +2 -0
  #define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
 
+#ifndef WRITE_ONCE
  #define WRITE_ONCE(x, val) ((*(volatile typeof(x) *)&(x)) = (val))
+#endif

I wonder if we can avoid duplicating this macros, for example by including bpf_atomic.h in the bpf_arena_common.h.

Quoted 7 lines

I kept both definitions because the headers correspond to unrelated features (atomics and arenas). Including bpf_atomic.h in bpf_arena_common.h would work, and we'd need to update just a single test that includes bpf_atomic.h directly (lpm_trie_bench). However, any program that wanted to include atomics would also have to include arena-related definitions. If that sounds reasonable I'll update the diff on the next version.

+1 to reusing bpf_atomic.h, it's supposed to work fine regardless of arena or normal memory.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/bpf/bpf_atomic.h +0 -0
  #define cmpxchg(p, old, new) __sync_val_compare_and_swap((p), old, new)
 
author reviewer
#13 lore raw

PATCH v2 5/13 selftests: bpf: Add basic libarena scaffolding

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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.

---
 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
index b8bf51b7a0b0..96b4a7e37427 100644
@@ -48,3 +48,4 @@ verification_cert.h
 *.BTF
 *.BTF_ids
 *.BTF.base
+libarena/test_libarena
tools/testing/selftests/bpf/Makefile +12 -0
index 9488d076c740..49b275a7bfa1 100644
@@ -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) -lzstd" \
+		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
new file mode 100644
index 000000000000..bd5e7042a44f
@@ -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
new file mode 100644
index 000000000000..6580b23355d1
@@ -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.49.0
#14 lore raw

Re: [PATCH v2 05/13] selftests: bpf: Add basic libarena scaffolding

Quoted 112 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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.

---
 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
index b8bf51b7a0b0..96b4a7e37427 100644
@@ -48,3 +48,4 @@ verification_cert.h
 *.BTF
 *.BTF_ids
 *.BTF.base
+libarena/test_libarena
tools/testing/selftests/bpf/Makefile +12 -0
index 9488d076c740..49b275a7bfa1 100644
@@ -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) -lzstd" \
+               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
new file mode 100644
index 000000000000..70901bb5237f
@@ -0,0 +1,47 @@
+.PHONY: clean
+
+LIBARENA=$(abspath .)
+
+
+LIBARENA_SOURCES = $(wildcard $(LIBARENA)/src/*.bpf.c) $(wildcard $(LIBARENA)/selftests/*.bpf.c)
+LIBARENA_OBJECTS = $(notdir $(LIBARENA_SOURCES:.bpf.c=.bpf.o))
+
+INCLUDES = -I$(LIBARENA)/include -I$(LIBARENA)/..
+ifneq ($(INCLUDE_DIR),)
+INCLUDES += -I$(INCLUDE_DIR)
+endif
+ifneq ($(LIBBPF_INCLUDE),)
+INCLUDES += -I$(LIBBPF_INCLUDE)
+endif
+
+# 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
+override BPF_CFLAGS += $(INCLUDES)
+
+CFLAGS = -O2 -no-pie
+CFLAGS += $(INCLUDES)
+
+vpath %.bpf.c $(LIBARENA)/src $(LIBARENA)/selftests
+vpath %.c $(LIBARENA)/src $(LIBARENA)/selftests
+
+all: test_libarena
+
+test_libarena: selftest.c $(BPFOBJ) selftest.skel.h
+       $(call msg,BINARY,libarena,$@)
+       $(Q)$(CLANG) $(LDLIBS) $(CFLAGS) $< $(BPFOBJ) -o $@
+
+selftest.skel.h: main.bpf.o
+       $(call msg,GEN-SKEL,libarena,$@)
+       $(Q)$(BPFTOOL) gen skeleton $< name "selftest" > $@
+
+main.bpf.o: $(LIBARENA_OBJECTS)
+       $(call msg,GEN-OBJ,libarena,$@)
+       $(Q)$(BPFTOOL) gen object $@ $^
+
+%.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
tools/testing/selftests/bpf/libarena/include/common.h new file +0 -0
new file mode 100644
index 000000000000..12fab01d60f9

tools/testing/selftests/bpf/libarena/include/common.h line 1

Quoted 1 line
tools/testing/selftests/bpf/libarena/include/common.h new file +1 -0
@@ -0,0 +1,118 @@
+#pragma once

Make sure there is SPDX and copyright in all new files.

tools/testing/selftests/bpf/libarena/include/common.h line 6

Quoted 5 lines
tools/testing/selftests/bpf/libarena/include/common.h new file +5 -0
+
+#ifdef __BPF__
+
+/* Suppresses definition conflicts between bpf_experimental.h and vmlinux.h. */
+#define BPF_NO_KFUNC_PROTOTYPES

That makes little sense to me.
bpf_experimental.h already includes vmlinux.h
There are no conflicts.

tools/testing/selftests/bpf/libarena/include/common.h line 9

Quoted 3 lines
tools/testing/selftests/bpf/libarena/include/common.h new file +3 -0
+#include <vmlinux.h>
+
+#include "bpf_experimental.h"

pw-bot: cr

#15 lore raw

Re: [PATCH v2 05/13] selftests: bpf: Add basic libarena scaffolding

In reply to Emil Tsalapatis
Quoted 10 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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.

---

I tried running the tests, but didn't see any splat in BPF stdout or stderr. Based on the tests I expected to see something.
Not sure whether I am doing something wrong (I'm using clang from LLVM tip).

I am also wondering whether having this separate runner is the right way to go. Instead, we should be testing properly whether ASAN splat is generated like stream stack traces are tested.
So it would make more sense to integrate the selftests for all allocators in test_progs infra (and use regex in __stdout/__stderr tags).
You can adjust the makefile to only compile asan variants for specific tests.

Also, throughout tests, some errors or assertions are emitted through bpf_printk. It would probably be better to emit everything in streams.

Quoted 1 line

[...]

author reviewer
#16 lore raw

PATCH v2 6/13 selftests: bpf: Add arena ASAN runtime to libarena

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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
index 96b4a7e37427..3d176d3b2cc1 100644
@@ -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.49.0
#17 lore raw

Re: [PATCH v2 06/13] selftests: bpf: Add arena ASAN runtime to libarena

Quoted 212 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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
index 96b4a7e37427..3d176d3b2cc1 100644
@@ -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
new file mode 100644
index 000000000000..be50b23fb491
@@ -0,0 +1,133 @@
+#pragma once
+
+struct asan_init_args {
+       u64 arena_all_pages;
+       u64 arena_globals_pages;
+};
+
+int asan_init(struct asan_init_args *args);
+
+/* Parameters usable by userspace. */
+extern volatile u64 __asan_shadow_memory_dynamic_address;
+extern volatile bool 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
+
+/*
+ * Defined as char * to get 1-byte granularity for pointer arithmetic.
+ */
+typedef s8 __arena s8a;
+
+/*
+ * Address to shadow map translation.
+ */
+static inline
+s8a *mem_to_shadow(void __arena __arg_arena *addr)
+{
+       return (s8a *)(((u32)(u64)addr >> ASAN_SHADOW_SHIFT) + __asan_shadow_memory_dynamic_address);
+}
+
+/*
+ * Helper for directly reading the shadow map.
+ */
+static inline __noasan
+s8 asan_shadow_value(void __arena __arg_arena *addr)
+{
+       return *(s8a *)mem_to_shadow(addr);
+}
+
+__weak __noasan
+bool asan_ready(void)
+{
+       return __asan_shadow_memory_dynamic_address;
+}
+
+/*
+ * Shadow map manipulation helpers.
+ */
+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);
+s8 asan_shadow_value(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);
+
+#define DECLARE_ASAN_LOAD_STORE(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);
+
+#define ASAN_DUMMY_CALLS_SIZE(size, arg)               \
+do {                                                   \
+       __asan_store##size((arg));                      \
+       __asan_store##size##_noabort((arg));            \
+       __asan_load##size((arg));                       \
+       __asan_load##size##_noabort((arg));             \
+       __asan_report_store##size((arg));               \
+       __asan_report_store##size##_noabort((arg));     \
+       __asan_report_load##size((arg));                \
+       __asan_report_load##size##_noabort((arg));      \
+} while (0)
+
+#define ASAN_DUMMY_CALLS_ALL(arg)      \
+do {                                   \
+       ASAN_DUMMY_CALLS_SIZE(1, (arg));        \
+       ASAN_DUMMY_CALLS_SIZE(2, (arg));        \
+       ASAN_DUMMY_CALLS_SIZE(4, (arg));        \
+       ASAN_DUMMY_CALLS_SIZE(8, (arg));        \
+} while (0)
+
+__weak __attribute__((no_sanitize_address))
+int asan_dummy_call() {
+       /* Use the shadow map base to prevent it from being optimized out. */
+       if (__asan_shadow_memory_dynamic_address)
+               ASAN_DUMMY_CALLS_ALL(NULL);
+
+       return 0;
+}
+#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; }
+static inline s8 asan_shadow_value(void __arena *addr) { return 0; }
+__weak bool asan_ready(void) { return true; }
+
+#endif /* BPF_ARENA_ASAN */
+
+#endif /* __BPF__ */
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +38 -0
new file mode 100644
index 000000000000..34b62c6e0aae
@@ -0,0 +1,463 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0
+ * Copyright (c) 2024-2025 Meta Platforms, Inc. and affiliates.
+ * Copyright (c) 2024-2025 Emil Tsalapatis <[email protected]>
+ */
+#include <common.h>
+#include <asan.h>
+
+#ifdef BPF_ARENA_ASAN
+
+#pragma clang attribute push(__attribute__((no_sanitize("address"))), \
+                            apply_to = function)
+
+#define SHADOW_ALL_ZEROES ((u64)-1)
+
+/*
+ * Canary variable for ASAN violations. Set to the offending address.
+ */
+volatile u64 asan_violated = 0;
+
+/*
+ * Shadow map occupancy map.
+ */
+volatile u64 __asan_shadow_memory_dynamic_address;
+
+volatile bool asan_reported = false;
+volatile bool asan_inited = false;
+
+/*
+ * Set during program load.
+ */
+volatile bool asan_report_once = false;
+volatile bool asan_emit_stack = false;
+
+/*
+ * BPF does not currently support the memset/memcpy/memcmp intrinsics.
+ */
+__always_inline int asan_memset(s8a __arg_arena *dst, s8 val, size_t size)

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

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +10 -0
+{
+       int i;
+
+       /*
+        * Switching this to a may_goto confuses the verifier and
+        * prevents verification on bpf-next as of late December 2025.
+        */
+       bpf_for(i, 0, size) {
+               dst[i] = val;
+       }

Have you tried
int zero = 0;
for (int i = zero; i < size; i++)

like existing arena selftest do?

Quoted 41 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +41 -0
+       return 0;
+}
+
+/* Validate a 1-byte access, always within a single byte. */
+static __always_inline bool memory_is_poisoned_1(s8a *addr)
+{
+       s8 shadow_value = asan_shadow_value(addr);
+
+       /* Byte is 0, access is valid. */
+       if (likely(!shadow_value))
+               return false;
+
+       /*
+        * Byte is non-zero. Access is valid if granule offset in [0, shadow_value),
+        * so the memory is poisoned if shadow_value is negative or smaller than
+        * the granule's value.
+        */
+
+       return ASAN_GRANULE(addr) >= shadow_value;
+}
+
+/* Validate a 2- 4-, 8-byte access, spans up to 2 bytes. */
+static __always_inline bool memory_is_poisoned_2_4_8(s8a *addr, u64 size)
+{
+       u64 end = (u64)addr + size - 1;
+
+       /*
+        * Region fully within a single byte (addition didn't
+        * overflow above ASAN_GRANULE).
+        */
+       if (likely(ASAN_GRANULE(end) >= size - 1))
+               return memory_is_poisoned_1((s8a *)end);
+
+       /*
+        * Otherwise first byte must be fully unpoisoned, and second byte
+        * must be unpoisoned up to the end of the accessed region.
+        */
+
+       return asan_shadow_value(addr) || memory_is_poisoned_1((s8a *)end);
+}
+

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

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +10 -0
+/*
+ * Explicit ASAN check.
+ */
+__weak bool asan_shadow_set(void __arena __arg_arena *addr)
+{
+       return memory_is_poisoned_1(addr);
+}
+
+static __always_inline u64 first_nonzero_byte(u64 addr, size_t size)
+{

I'd think this one and asan_report() can weak noinline instead to reduce the load on the verifier.
Have you tried making some of them global?

Quoted 16 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +16 -0
+       while (size && can_loop) {
+               if (unlikely(*(s8a *)addr))
+                       return addr;
+               addr += 1;
+               size -= 1;
+       }
+
+       return SHADOW_ALL_ZEROES;
+}
+
+static __always_inline bool memory_is_poisoned_n(s8a *addr, u64 size)
+{
+       u64 ret;
+       u64 start;
+       u64 end;
+

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

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +10 -0
+       /* Size of [start, end] is end - start + 1. */
+       start = (u64)mem_to_shadow(addr);
+       end = (u64)mem_to_shadow(addr + size - 1);
+
+       ret = first_nonzero_byte(start, (end - start) + 1);
+       if (likely(ret == SHADOW_ALL_ZEROES))
+               return false;
+
+       return __builtin_expect(ret != end || ASAN_GRANULE(addr + size - 1) >=
+                                                     *(s8a *)end, false);

that's the same as unlikely(), right? Why open code?

Quoted 195 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +195 -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;
+
+       if (asan_emit_stack) {
+               arena_stderr("Memory violation for address %p (0x%lx) for %s of size %ld",
+                               addr, (u64)addr, write ? "write" : "read", sz);
+               bpf_stream_print_stack();
+       }
+
+       return 0;
+}
+
+static __always_inline bool check_asan_args(s8a *addr, size_t size,
+                                           bool *result)
+{
+       bool valid = true;
+
+       /* Size 0 accesses are valid even if the address is invalid. */
+       if (unlikely(size == 0))
+               goto confirmed_valid;
+
+       /*
+        * Wraparound is possible for extremely high size. Possible if the size
+        * is a misinterpreted negative number.
+        */
+       if (unlikely(addr + size < addr))
+               goto confirmed_invalid;
+
+       return false;
+
+confirmed_invalid:
+       valid = false;
+
+       /* FALLTHROUGH */
+confirmed_valid:
+       *result = valid;
+
+       return true;
+}
+
+static __always_inline bool check_region_inline(void *ptr, size_t size,
+                                               bool write)
+{
+       s8a *addr = (s8a *)(u64)ptr;
+       bool is_poisoned, is_valid;
+
+       if (check_asan_args(addr, size, &is_valid)) {
+               if (!is_valid)
+                       asan_report(addr, size, write);
+               return is_valid;
+       }
+
+       switch (size) {
+       case 1:
+               is_poisoned = memory_is_poisoned_1(addr);
+               break;
+       case 2:
+       case 4:
+       case 8:
+               is_poisoned = memory_is_poisoned_2_4_8(addr, size);
+               break;
+       default:
+               is_poisoned = memory_is_poisoned_n(addr, size);
+       }
+
+       if (is_poisoned) {
+               asan_report(addr, size, write);
+               return false;
+       }
+
+       return true;
+}
+
+/*
+ * __alias is not supported for BPF so define *__noabort() variants as wrappers.
+ */
+#define DEFINE_ASAN_LOAD_STORE(size)                                           \
+       __hidden void __asan_store##size(void *addr)                           \
+       {                                                                      \
+               check_region_inline(addr, size, true);                         \
+       }                                                                      \
+       __hidden void __always_inline __asan_store##size##_noabort(void *addr) \
+       {                                                                      \
+               check_region_inline(addr, size, true);                         \
+       }                                                                      \
+       __hidden void __asan_load##size(void *addr)                            \
+       {                                                                      \
+               check_region_inline(addr, size, false);                        \
+       }                                                                      \
+       __hidden void __asan_load##size##_noabort(void *addr)                  \
+       {                                                                      \
+               check_region_inline(addr, size, false);                        \
+       }                                                                      \
+       __hidden void __asan_report_store##size(void *addr)                    \
+       {                                                                      \
+               asan_report((s8a *)addr, size, true);                          \
+       }                                                                      \
+       __hidden void __asan_report_store##size##_noabort(void *addr)          \
+       {                                                                      \
+               asan_report((s8a *)addr, size, true);                          \
+       }                                                                      \
+       __hidden void __asan_report_load##size(void *addr)                     \
+       {                                                                      \
+               asan_report((s8a *)addr, size, false);                         \
+       }                                                                      \
+       __hidden void __asan_report_load##size##_noabort(void *addr)           \
+       {                                                                      \
+               asan_report((s8a *)addr, size, false);                         \
+       }
+
+DEFINE_ASAN_LOAD_STORE(1);
+DEFINE_ASAN_LOAD_STORE(2);
+DEFINE_ASAN_LOAD_STORE(4);
+DEFINE_ASAN_LOAD_STORE(8);
+
+void __asan_storeN(void *addr, ssize_t size)
+{
+       check_region_inline(addr, size, true);
+}
+
+void __asan_loadN(void *addr, ssize_t size)
+{
+       check_region_inline(addr, size, false);
+}
+
+/*
+ * We currently do not sanitize globals.
+ */
+void __asan_register_globals(void *globals, size_t n)
+{
+}
+
+void __asan_unregister_globals(void *globals, size_t n)
+{
+}
+
+/*
+ * We do not currently have memcpy/memmove/memset intrinsics
+ * in LLVM. Do not implement sanitization.
+ */
+void *__asan_memcpy(void *d, const void *s, size_t n)
+{
+       return NULL;
+}
+
+void *__asan_memmove(void *d, const void *s, size_t n)
+{
+       return NULL;
+}
+
+void *__asan_memset(void *p, int c, size_t n)
+{
+       return NULL;
+}
+
+/*
+ * Poisoning code, used when we add more freed memory to the allocator by:
+ *     a) pulling memory from the arena segment using bpf_arena_alloc_pages()
+ *     b) freeing memory from application code
+ */
+__hidden __noasan int asan_poison(void __arena *addr, s8 val, size_t size)
+{
+       s8a *shadow;
+       size_t len;
+
+       /*
+        * Poisoning from a non-granule address makes no sense: We can only allocate
+        * memory to the application that with a granule-aligned starting address,
+        * and bpf_arena_alloc_pages returns page-aligned memory. A non-aligned
+        * addr then implies we're freeing a different address than the one we
+        * allocated.
+        */
+       if (unlikely((u64)addr & ASAN_GRANULE_MASK))
+               return -EINVAL;
+
+       /*
+        * We cannot free an unaligned region because it'd be possible that we
+        * cannot describe the resulting poisoning state of the granule in
+        * the ASAN encoding.
+        *
+        * Every granule represents a region of memory that looks like the
+        * following (P for poisoned bytes, C for clear):
+        *
+        * <Clear>  <Poisoned>
+        * [ C C C ... P P ]
+        *

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

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +10 -0
+        * The value of the granule's shadow map is the number of clear bytes in
+        * it. We cannot represent granules with the following state:
+        *
+        * [ P P ... C C ... P P ]
+        *
+        * That would be possible if we could free unaligned regions, so prevent that.
+        *
+        */
+       if (unlikely(size & ASAN_GRANULE_MASK))
+               return -EINVAL;

kasan_poison() in the kernel has the same logic without the comments. The comments are nice, but please make a big comment somewhere that the algorithm is the same as KASAN. Is it actually? So reviewers don't need to triple check the bit math.

Quoted 8 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c new file +8 -0
+
+       shadow = mem_to_shadow(addr);
+       len = size >> ASAN_SHADOW_SHIFT;
+
+       asan_memset(shadow, val, len);
+
+       return 0;
+}
author reviewer
#18 lore raw

PATCH v2 7/13 selftests: bpf: Add ASAN support for libarena selftests

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

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/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
index 49b275a7bfa1..2b59a5c2235b 100644
@@ -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) -lzstd" \
+		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
index 70901bb5237f..1ec1a83c0c04 100644
@@ -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
index bd5e7042a44f..c294b6818bed 100644
@@ -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.49.0
#19 lore raw

Re: [PATCH v2 07/13] selftests: bpf: Add ASAN support for libarena selftests

This isn't a bug, but the comment mentions strlen and compile-time length calculation, while the actual code uses strstr(). Should the comment be updated to match the implementation?
Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +0 -0
index 5b8d8b94323e..f63324a1cdde 100644

[ ... ]

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 = strstr(name, "_asan");

This isn't a bug, but the comment mentions strlen and compile-time length calculation, while the actual code uses strstr(). Should the comment be updated to match the implementation?

tools/testing/selftests/bpf/libarena/selftests/selftest.c static void · line 283

Quoted 3 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +2 -0
+
+	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/21409286496

AI-authorship-score: low
AI-authorship-explanation: Code follows standard kernel patterns with a comment/code mismatch typical of iterative human development rather than AI generation.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Minor documentation issue where comment mentions strlen and compile-time calculation but code uses strstr; does not affect functionality.

author reviewer
#20 lore raw

Re: [PATCH v2 07/13] selftests: bpf: Add ASAN support for libarena selftests

Quoted 16 lines · bot+bpf-ci
Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +0 -0
index 5b8d8b94323e..f63324a1cdde 100644

[ ... ]

Quoted 9 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +5 -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.
Quoted 9 lines

tools/testing/selftests/bpf/libarena/selftests/selftest.c static void · line 280

Quoted 4 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +4 -0
+	 * Calculate the hardcoded name's length at compile time.
+	 */
+	printf("%s\n", name);
+	is_asan = strstr(name, "_asan");

This isn't a bug, but the comment mentions strlen and compile-time length calculation, while the actual code uses strstr(). Should the comment be updated to match the implementation?

Ack, will update.

Quoted 16 lines

tools/testing/selftests/bpf/libarena/selftests/selftest.c static void · line 283

Quoted 3 lines
tools/testing/selftests/bpf/libarena/selftests/selftest.c +2 -0
+
+	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/21409286496

AI-authorship-score: low
AI-authorship-explanation: Code follows standard kernel patterns with a comment/code mismatch typical of iterative human development rather than AI generation.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Minor documentation issue where comment mentions strlen and compile-time calculation but code uses strstr; does not affect functionality.

author reviewer
#21 lore raw

PATCH v2 8/13 selftest: bpf: Add bump allocator for libarena

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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     |  26 +++
 .../selftests/bpf/libarena/src/bump.bpf.c     | 196 ++++++++++++++++++
 2 files changed, 222 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 +26 -0
new file mode 100644
index 000000000000..ac643f29acc0
@@ -0,0 +1,26 @@
+#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 contig_pages);
+int bump_destroy(void);
+int bump_memlimit(u64 lim_memusage);
+
+#endif /* __BPF__ */
+
+#define BUMP_ALLOCS_MAX (64)
+
+struct bump {
+	size_t max_contig_bytes;
+	void __arena *memory;
+	size_t off;
+	size_t lim_memusage;
+	size_t cur_memusage;
+
+	void __arena *past_allocs[BUMP_ALLOCS_MAX];
+
+	size_t past_allocs_ind;
+};
+
tools/testing/selftests/bpf/libarena/src/bump.bpf.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.49.0
#22 lore raw

Re: [PATCH v2 08/13] selftest: bpf: Add bump allocator for libarena

Quoted 15 lines · Emil Tsalapatis
tools/testing/selftests/bpf/libarena/src/bump.bpf.c new file +15 -0
+
+private(STATIC_ALLOC_LOCK) struct bpf_spin_lock static_lock;
+
+private(STATIC_ALLOC) struct bump bump;
+
+const s8 STATIC_POISON_UNINIT = 0xff;
+
+__weak u64 bump_alloc_internal(size_t bytes, size_t alignment)
+{
+       void __arena *memory;
+       size_t  alloc_bytes;
+       size_t  alloc_pages;
+       void __arena *ptr;
+       size_t padding;
+       size_t ind;

tools/testing/selftests/bpf/libarena/src/bump.bpf.c line 46

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/bump.bpf.c new file +10 -0
+       u64 addr;
+
+       /*
+        * Allocated addresses must be aligned to the nearest granule,
+        * and since we're stack allocating this implies that allocations
+        * sizes are also aligned.
+        */
+       alignment = round_up(alignment, 1 << ASAN_SHADOW_SHIFT);
+
+       bpf_spin_lock(&static_lock);

In the stack allocator you're using arena_spin_lock and I'm assuming it works fine there. Why use the old one here?

author reviewer
#23 lore raw

PATCH v2 9/13 selftests: bpf: Add libarena selftests for the bump allocator

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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      | 281 ++++++++++++++++++
 3 files changed, 486 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
index f63324a1cdde..a0fd7a2e6c46 100644
@@ -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 +281 -0

281 lines not on this page · raw message

Signature · 1 line
2.49.0
author reviewer
#24 lore raw

PATCH v2 10/13 selftest: bpf: Add libarena stack allocator

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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
new file mode 100644
index 000000000000..9f9d8fb47935
@@ -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.49.0
#25 lore raw

Re: [PATCH v2 10/13] selftest: bpf: Add libarena stack allocator

Should this be using stack->data_size instead of sizeof(struct stk_seg)?
Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +0 -0
new file mode 100644
index 000000000000..068cdd75bffc

[ ... ]

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +10 -0
+static int stk_seg_to_data(struct stk *stack, size_t nelems)
+{
+	int ret, i;
+	u64 data;
+
+	/* Do we have enough empty segments for the conversion? */
+	if (!stack->first || stack->first == stack->last)
+		return -ENOMEM;
+
+	data = (u64)stack->last;

tools/testing/selftests/bpf/libarena/src/stack.bpf.c line 143

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +10 -0
+
+	stack->last->prev->next = NULL;
+	stack->last = stack->last->prev;
+
+	/* We removed a segment. */
+	stack->capacity -= STK_SEG_MAX;
+
+	for (i = zero; i < nelems && can_loop; i++) {
+		asan_poison((void __arena *)data, STACK_POISONED,
+			    sizeof(struct stk_seg));

Should this be using stack->data_size instead of sizeof(struct stk_seg)?

The loop advances data by stack->data_size on each iteration, but poisons sizeof(struct stk_seg) bytes. If data_size differs from sizeof(struct stk_seg), the ASAN shadow map will be incorrectly populated. When data_size is smaller, this over-poisons and can corrupt the shadow map for adjacent elements.

Quoted 11 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +11 -0
+
+		/* This operation should never fail. */
+		ret = stk_push(stack, (void __arena *)data);
+		if (ret)
+			return ret;
+
+		data += stack->data_size;
+	}
+
+	return 0;
+}

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +7 -0
+static int stk_get_arena_memory(struct stk *stack, __u64 nr_pages, stk_seg_t **data_seg, stk_seg_t **stk_seg)
+{
+	size_t nstk_segs;
+	u64 mem;
+
+	_Static_assert(sizeof(struct stk_seg) <= __PAGE_SIZE,
+		       "segment must fit into a page");

[ ... ]

tools/testing/selftests/bpf/libarena/src/stack.bpf.c line 249

Quoted 11 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +11 -0
+	/* Poison the entire data segment allocated */
+	*data_seg = (stk_seg_t *)mem;
+	asan_poison((void __arena *)mem, STACK_POISONED, nr_pages * __PAGE_SIZE);
+
+	if (!stk_seg)
+		return 0;
+
+	/* Skip the segment metadata when poisoning. */
+	*stk_seg = (stk_seg_t *)(mem + nr_pages * __PAGE_SIZE);
+	asan_poison((void __arena *)(mem + sizeof(**stk_seg)), STACK_POISONED,
+		nr_pages * __PAGE_SIZE);

Is the base address for this asan_poison call correct? The stack segment is located at (mem + nr_pages * __PAGE_SIZE), but the poison starts at (mem + sizeof(**stk_seg)). With nr_pages=4, the stack segment starts at offset 16384 from mem, but this poisons starting at offset 4096.

The comment says to skip the segment metadata, so the intended poisoning should start at the stack segment address plus sizeof(**stk_seg), not at mem plus sizeof(**stk_seg). This appears to poison part of the data segment and the beginning of the stack segment incorrectly.

tools/testing/selftests/bpf/libarena/src/stack.bpf.c line 38

Quoted 3 lines
tools/testing/selftests/bpf/libarena/src/stack.bpf.c new file +3 -0
+
+	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/21409286496

AI-authorship-score: low
AI-authorship-explanation: Code shows typical human patterns with domain-specific BPF arena knowledge and standard kernel coding style; bugs found are typical human errors rather than AI-generated patterns
issues-found: 2
issue-severity-score: low
issue-severity-explanation: Two ASAN poisoning issues in selftest code that could cause incorrect shadow map population but do not affect production kernel behavior

author reviewer
#26 lore raw

PATCH v2 11/13 selftests: bpf: Add selftests for the libarena stack allocator

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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
index a0fd7a2e6c46..3e4b8754889f 100644
@@ -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.49.0
#27 lore raw

Re: [PATCH v2 11/13] selftests: bpf: Add selftests for the libarena stack allocator

Quoted 112 lines · Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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
index a0fd7a2e6c46..3e4b8754889f 100644
@@ -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 +73 -0
new file mode 100644
index 000000000000..a4c1780e4247
@@ -0,0 +1,253 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0
+ * Copyright (c) 2025 Meta Platforms, Inc. and affiliates.
+ * Copyright (c) 2025 Emil Tsalapatis <[email protected]>
+ */
+
+#include <common.h>
+
+#include <asan.h>
+#include <stack.h>
+
+#include "selftest.h"
+
+#ifdef BPF_ARENA_ASAN
+
+#include "st_asan_common.h"
+
+#define STACK_PAGES_PER_ALLOC (4)
+#define STACK_ALLOCS (4)
+
+/*
+ * Keep this test-related array in BSS to avoid
+ * overly burdening the function stack.
+ */
+u64 __arena stk_blks[STACK_ALLOCS];
+
+/*
+ * Spinlock used by the stack allocator.
+ */
+private(ST_STACK) struct stk st_stack;
+u64 __arena st_asan_stack_lock;
+
+static __maybe_unused void stk_blks_dump(void)
+{
+       int i;
+
+       for (i = 0; i < STACK_ALLOCS && can_loop; i++)
+               bpf_printk("[%d] 0x%lx", i, stk_blks[i]);
+}
+
+struct qsort_limits {
+       int lo;
+       int hi;
+};
+
+__always_inline void swap(unsigned int i, unsigned int j)
+{
+       u64 tmp;
+
+       tmp = stk_blks[i];
+       stk_blks[i] = stk_blks[j];
+       stk_blks[j] = tmp;
+}
+
+static __always_inline int qsort_partition(unsigned int lo, unsigned hi)
+{
+       unsigned int i;
+       u64 pivotval;
+       int pivot;
+
+       if (lo >= STACK_ALLOCS || hi >= STACK_ALLOCS) {
+               bpf_printk("%s:%d invalid lo/hi indices %d/%d", __func__,
+                          __LINE__, lo, hi);
+               return 0;
+       }
+
+       pivotval = stk_blks[hi];
+       pivot = lo;
+
+       for (i = lo; i < hi && can_loop; i++) {
+               if (stk_blks[i] > pivotval)
+                       continue;
+

tools/testing/selftests/bpf/libarena/selftests/st_asan_stack.bpf.c line 83

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/st_asan_stack.bpf.c new file +10 -0
+               swap(i, pivot);
+               pivot += 1;
+       }
+
+       swap(pivot, hi);
+
+       return pivot;
+}
+
+static __always_inline int qsort_stack_blocks(void)

Amount of always_inline here and throughout looks excessive. Especially for a heavy function like qsort.

Try removing all of them and add only where it's absolutely necessary and add a comment why.
Like in core asan_loadN() functions it's clear why you want full inline there, but in selftest... ?

author reviewer
#28 lore raw

PATCH v2 12/13 selftests: bpf: Add buddy allocator for libarena

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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
new file mode 100644
index 000000000000..6154169a0f9c
@@ -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.49.0
#29 lore raw

Re: [PATCH v2 12/13] selftests: bpf: Add buddy allocator for libarena

This comment appears to be truncated mid-sentence. Was there additional text intended after "and the "?
Quoted 6 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c new file +0 -0
new file mode 100644
index 000000000000..f43e1db209be
@@ -0,0 +1,784 @@

[ ... ]

Quoted 8 lines

+/*
+ * Carve out part of the reserved address space and hand it over
+ * to the buddy allocator.
+ *
+ * We are assuming the buddy allocator is the only allocator in the
+ * system, so there is no race between this function unreserving a
+ * page range and the
+ */

This comment appears to be truncated mid-sentence. Was there additional text intended after "and the "?

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

AI-authorship-score: low
AI-authorship-explanation: The incomplete comment is a strong indicator of human authorship, and the deep domain-specific knowledge of BPF arenas and buddy allocator algorithms suggests human expertise.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: One minor documentation issue found - an incomplete comment that ends mid-sentence.

author reviewer
#30 lore raw

PATCH v2 13/13 selftests: bpf: Add selftests for the libarena buddy allocator

In reply to Emil Tsalapatis

From: Emil Tsalapatis <[email protected]>

Add 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
index 3e4b8754889f..268749c687a1 100644
@@ -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.49.0
#31 lore raw

Re: [PATCH v2 13/13] selftests: bpf: Add selftests for the libarena buddy allocator

The comment says "Cycle through each size" but idx is initialized to 0 and never incremented in this loop. This means sz will always be alloc_multiple_sizes[0] (which is 3) for all 512 allocations.…
Quoted 5 lines
tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c new file +0 -0
new file mode 100644
index 000000000000..256d9a865647

[ ... ]

Quoted 14 lines
tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c new file +14 -0
+static int buddy_selftest_alloc_multiple()
+{
+	int ret, j;
+	u32 i, idx;
+	u8 __arena *mem;
+	size_t sz;
+	u8 poison;
+
+	ret = buddy_init(&st_buddy,
+			     (arena_spinlock_t __arena *)&st_buddy_lock);
+	if (ret)
+		return ret;
+
+	/*

tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c line 125

Quoted 10 lines
tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c new file +10 -0
+	 * Cycle through each size, allocating an entry in the
+	 * segarr. Continue for SEGARRLEN iterations. For every
+	 * allocation write down the size, use the current index
+	 * as a poison value, and log it with the pointer in the
+	 * segarr entry. Use the poison value to poison the entire
+	 * allocated memory according to the size given.
+	 */
+	idx = 0;
+	bpf_for(i, 0, SEGARRLEN) {
+		sz = alloc_multiple_sizes[idx % 9];
                                       ^^^

The comment says "Cycle through each size" but idx is initialized to 0 and never incremented in this loop. This means sz will always be alloc_multiple_sizes[0] (which is 3) for all 512 allocations. Should idx be i here to cycle through the different sizes in the array?

tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c line 128

Quoted 3 lines
tools/testing/selftests/bpf/libarena/selftests/st_buddy.bpf.c new file +3 -0
+		poison = (u8)i;
+
+		mem = buddy_alloc(&st_buddy, sz);

[ ... ]

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

AI-authorship-score: low
AI-authorship-explanation: The code follows existing selftest patterns and the idx variable bug is a typical human copy-paste error that AI would be less likely to make.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: The idx variable bug causes all allocations to use the same size (3 bytes) instead of cycling through different sizes, reducing test coverage but not causing system instability.