Skip to contentSkip to conversation

bpf/arena: Add kfunc for reserving arena memory

PATCH Applied Emil Tsalapatis 3 participants 6 messages Jul 9, 2025 – Jul 11, 2025

Subsystems (3)
lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

v4 cover bpf/arena: Add kfunc for reserving arena memory

Add a new kfunc for BPF arenas that reserves a region of the mapping to prevent it from being mapped. These regions serve as guards against out-of-bounds accesses and are useful for debugging arena-related code.

CHANGELOG
=========

Quoted 1 line

From v3 ([email protected])

------------------------------------------------------

- Added Acked-by tags by Yonghong.
- Replace hardcoded error numbers in selftests (Yonghong).
- Fixed selftest for partially freeing a reserved region (Yonghong).

Quoted 1 line

From v2 ([email protected])

------------------------------------------------------

- Removed -EALREADY and replaced with -EINVAL to bring error handling in line with the rest of the BPF code (Alexei).

Quoted 1 line

From v1 ([email protected])

------------------------------------------------------

- Removed the additional guard range tree. Adjusted tests accordingly. Reserved regions now behave like allocated regions, and can be unreserved using bpf_arena_free_pages(). They can also be allocated from userspace through minor faults. It is up to the user to prevent erroneous frees and/or use the BPF_F_SEGV_ON_FAULT flag to catch stray userspace accesses (Alexei).
- Changed terminology from guard pages to reserved pages (Alexei, Kartikeya).

Emil Tsalapatis (2):
bpf/arena: add bpf_arena_reserve_pages kfunc selftests/bpf: add selftests for bpf_arena_reserve_pages

 kernel/bpf/arena.c                            |  43 +++++++
 .../testing/selftests/bpf/bpf_arena_common.h  |   3 +
 .../selftests/bpf/progs/verifier_arena.c      | 106 ++++++++++++++++++
 .../bpf/progs/verifier_arena_large.c          |  98 ++++++++++++++++
 4 files changed, 250 insertions(+)
Signature · 1 line
2.49.0
author reviewer
#1 lore raw

PATCH v4 1/2 bpf/arena: add bpf_arena_reserve_pages kfunc

Add a new BPF arena kfunc for reserving a range of arena virtual addresses without backing them with pages. This prevents the range from being populated using bpf_arena_alloc_pages().

---
 kernel/bpf/arena.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
kernel/bpf/arena.c +43 -0
index 0d56cea71602..5b37753799d2 100644
@@ -550,6 +550,34 @@ static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt)
 	}
 }
 
+/*
+ * Reserve an arena virtual address range without populating it. This call stops
+ * bpf_arena_alloc_pages from adding pages to this range.
+ */
+static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cnt)
+{
+	long page_cnt_max = (arena->user_vm_end - arena->user_vm_start) >> PAGE_SHIFT;
+	long pgoff;
+	int ret;
+
+	if (uaddr & ~PAGE_MASK)
+		return 0;
+
+	pgoff = compute_pgoff(arena, uaddr);
+	if (pgoff + page_cnt > page_cnt_max)
+		return -EINVAL;
+
+	guard(mutex)(&arena->lock);
+
+	/* Cannot guard already allocated pages. */
+	ret = is_range_tree_set(&arena->rt, pgoff, page_cnt);
+	if (ret)
+		return -EBUSY;
+
+	/* "Allocate" the region to prevent it from being allocated. */
+	return range_tree_clear(&arena->rt, pgoff, page_cnt);
+}
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, void *addr__ign, u32 page_cnt,
@@ -573,11 +601,26 @@ __bpf_kfunc void bpf_arena_free_pages(void *p__map, void *ptr__ign, u32 page_cnt
 		return;
 	arena_free_pages(arena, (long)ptr__ign, page_cnt);
 }
+
+__bpf_kfunc int bpf_arena_reserve_pages(void *p__map, void *ptr__ign, u32 page_cnt)
+{
+	struct bpf_map *map = p__map;
+	struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
+
+	if (map->map_type != BPF_MAP_TYPE_ARENA)
+		return -EINVAL;
+
+	if (!page_cnt)
+		return 0;
+
+	return arena_reserve_pages(arena, (long)ptr__ign, page_cnt);
+}
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(arena_kfuncs)
 BTF_ID_FLAGS(func, bpf_arena_alloc_pages, KF_TRUSTED_ARGS | KF_SLEEPABLE | KF_ARENA_RET | KF_ARENA_ARG2)
 BTF_ID_FLAGS(func, bpf_arena_free_pages, KF_TRUSTED_ARGS | KF_SLEEPABLE | KF_ARENA_ARG2)
+BTF_ID_FLAGS(func, bpf_arena_reserve_pages, KF_TRUSTED_ARGS | KF_SLEEPABLE | KF_ARENA_ARG2)
 BTF_KFUNCS_END(arena_kfuncs)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.49.0
maintainer Acked-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] bpf/arena: add bpf_arena_reserve_pages kfunc

Quoted 8 lines · Emil Tsalapatis

Add a new BPF arena kfunc for reserving a range of arena virtual addresses without backing them with pages. This prevents the range from being populated using bpf_arena_alloc_pages().

---

Quoted 1 line

[...]

author reviewer
#3 lore raw

PATCH v4 2/2 selftests/bpf: add selftests for bpf_arena_reserve_pages

In reply to Emil Tsalapatis

Add selftests for the new bpf_arena_reserve_pages kfunc.

---
 .../testing/selftests/bpf/bpf_arena_common.h  |   3 +
 .../selftests/bpf/progs/verifier_arena.c      | 106 ++++++++++++++++++
 .../bpf/progs/verifier_arena_large.c          |  98 ++++++++++++++++
 3 files changed, 207 insertions(+)
tools/testing/selftests/bpf/bpf_arena_common.h +3 -0
index 68a51dcc0669..16f8ce832004 100644
@@ -46,8 +46,11 @@
 
 void __arena* bpf_arena_alloc_pages(void *map, void __arena *addr, __u32 page_cnt,
 				    int node_id, __u64 flags) __ksym __weak;
+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)
+
 #else /* when compiled as user space code */
 
 #define __arena
tools/testing/selftests/bpf/progs/verifier_arena.c +106 -0

118 lines not on this page · raw message

tools/testing/selftests/bpf/progs/verifier_arena_large.c +98 -0

104 lines not on this page · raw message

Signature · 1 line
2.49.0
#5 lore raw
In reply to Emil Tsalapatis
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

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

Quoted 8 lines · On Wed, 9 Jul 2025 15:13:10 -0400 you wrote:

Add a new kfunc for BPF arenas that reserves a region of the mapping to prevent it from being mapped. These regions serve as guards against out-of-bounds accesses and are useful for debugging arena-related code.

CHANGELOG
=========

[...]

Here is the summary with links:
- [v4,1/2] bpf/arena: add bpf_arena_reserve_pages kfunc

    https://git.kernel.org/bpf/bpf-next/c/8fc3d2d8b501
  - [v4,2/2] selftests/bpf: add selftests for bpf_arena_reserve_pages
    https://git.kernel.org/bpf/bpf-next/c/9f9559f0acc4

You are awesome, thank you!

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