Re: [PATCH bpf-next v3 4/8] selftests/bpf: make arena_base() opaque to GCC's -Warray-bounds

From: Vineet Gupta

Date: Mon Nov 02 2026 - 19:16:23 EST


On 11/2/26 2:38 PM, Alexei Starovoitov wrote:
On Mon, Nov 02, 2026 at 02:07 PM Vineet Gupta <vineet.gupta@xxxxxxxxx> wrote:
The barrier is behind #ifdef __clang__ so clang keeps the plain macro.
It does not need it, and the barrier is not free: it blocks reuse of
the map address, so each call site reloads it. Across the five progs
and 20 call sites that is +35 instructions under GCC
(2528 -> 2563), about 1.4% on those objects and nothing elsewhere.
Clang output is unchanged, bit for bit.
stream.c already has barrier_var() for both compilers.

How about this one instead:

Yeah definitely much better. Just to be clear the intent of series is to not make current compiler look better o anything. As I mentioned in the cover, this was during in my in-works arena compiler testing, ....

Thx for the quick feedback.
-Vineet


diff --git a/tools/testing/selftests/bpf/libarena/include/bpf_arena_common.h b/tools/testing/selftests/bpf/libarena/include/bpf_arena_common.h
index 82aafe8..0eba529 100644
--- a/tools/testing/selftests/bpf/libarena/include/bpf_arena_common.h
+++ b/tools/testing/selftests/bpf/libarena/include/bpf_arena_common.h
@@ -49,7 +49,16 @@ void __arena* bpf_arena_alloc_pages(void *map, void __arena *addr, __u32 page_cn
int bpf_arena_reserve_pages(void *map, void __arena *addr, __u32 page_cnt) __ksym __weak;
void bpf_arena_free_pages(void *map, void __arena *ptr, __u32 page_cnt) __ksym __weak;

-#define arena_base(map) ((void __arena *)((struct bpf_arena *)(map))->user_vm_start)
+/*
+ * The map definition is smaller than struct bpf_arena.
+ * barrier_var() hides that from -Warray-bounds of GCC.
+ */
+#define arena_base(map) \
+ ({ \
+ struct bpf_arena *___arena = (void *)(map); \
+ barrier_var(___arena); \
+ (void __arena *)___arena->user_vm_start; \
+ })

#else /* when compiled as user space code */

diff --git a/tools/testing/selftests/bpf/progs/stream.c b/tools/testing/selftests/bpf/progs/stream.c
index 1ae015c..b4d88d0 100644
--- a/tools/testing/selftests/bpf/progs/stream.c
+++ b/tools/testing/selftests/bpf/progs/stream.c
@@ -178,16 +178,8 @@ __stderr("Call trace:\n"
"|[ \t]+[^\n]+\n)*}}")
int stream_arena_write_fault(void *ctx)
{
- struct bpf_arena *ptr = (void *)&arena;
- u64 user_vm_start;
+ u64 user_vm_start = (u64)arena_base(&arena);

- /* Prevent GCC bounds warning: casting &arena to struct bpf_arena *
- * triggers bounds checking since the map definition is smaller than struct
- * bpf_arena. barrier_var() makes the pointer opaque to GCC, preventing the
- * bounds analysis
- */
- barrier_var(ptr);
- user_vm_start = ptr->user_vm_start;
fault_addr = user_vm_start + 0x7fff;
bpf_addr_space_cast(user_vm_start, 0, 1);
asm volatile (
@@ -212,16 +204,8 @@ __stderr("Call trace:\n"
"|[ \t]+[^\n]+\n)*}}")
int stream_arena_read_fault(void *ctx)
{
- struct bpf_arena *ptr = (void *)&arena;
- u64 user_vm_start;
+ u64 user_vm_start = (u64)arena_base(&arena);

- /* Prevent GCC bounds warning: casting &arena to struct bpf_arena *
- * triggers bounds checking since the map definition is smaller than struct
- * bpf_arena. barrier_var() makes the pointer opaque to GCC, preventing the
- * bounds analysis
- */
- barrier_var(ptr);
- user_vm_start = ptr->user_vm_start;
fault_addr = user_vm_start + 0x7fff;
bpf_addr_space_cast(user_vm_start, 0, 1);
asm volatile (
@@ -252,17 +236,9 @@ int stream_arena_load_acquire_fault(void *ctx)
.off = 0x7fff,
.imm = 0x100, /* BPF_LOAD_ACQ */
};
- struct bpf_arena *ptr = (void *)&arena;
- u64 user_vm_start, val;
+ u64 user_vm_start = (u64)arena_base(&arena);
+ u64 val;

- /*
- * Prevent GCC bounds warning: casting &arena to struct bpf_arena *
- * triggers bounds checking since the map definition is smaller than
- * struct bpf_arena. barrier_var() makes the pointer opaque to GCC,
- * preventing the bounds analysis.
- */
- barrier_var(ptr);
- user_vm_start = ptr->user_vm_start;
fault_addr = user_vm_start + 0x7fff;
bpf_addr_space_cast(user_vm_start, 0, 1);
asm volatile (
@@ -296,17 +272,9 @@ int stream_arena_xchg_fault(void *ctx)
.off = 0x7fff,
.imm = 0xe1, /* BPF_XCHG */
};
- struct bpf_arena *ptr = (void *)&arena;
- u64 user_vm_start, val;
+ u64 user_vm_start = (u64)arena_base(&arena);
+ u64 val;

- /*
- * Prevent GCC bounds warning: casting &arena to struct bpf_arena *
- * triggers bounds checking since the map definition is smaller than
- * struct bpf_arena. barrier_var() makes the pointer opaque to GCC,
- * preventing the bounds analysis.
- */
- barrier_var(ptr);
- user_vm_start = ptr->user_vm_start;
fault_addr = user_vm_start + 0x7fff;
bpf_addr_space_cast(user_vm_start, 0, 1);
/*
@@ -346,17 +314,9 @@ int stream_arena_cmpxchg_fault(void *ctx)
.off = 0x7fff,
.imm = 0xf1, /* BPF_CMPXCHG */
};
- struct bpf_arena *ptr = (void *)&arena;
- u64 user_vm_start, val;
+ u64 user_vm_start = (u64)arena_base(&arena);
+ u64 val;

- /*
- * Prevent GCC bounds warning: casting &arena to struct bpf_arena *
- * triggers bounds checking since the map definition is smaller than
- * struct bpf_arena. barrier_var() makes the pointer opaque to GCC,
- * preventing the bounds analysis.
- */
- barrier_var(ptr);
- user_vm_start = ptr->user_vm_start;
fault_addr = user_vm_start + 0x7fff;
bpf_addr_space_cast(user_vm_start, 0, 1);
/*