bpf/arena: Add kfunc for reserving arena memory
Subsystems (3)
Emil Tsalapatis
reviewerv4 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
Emil Tsalapatis
reviewerPATCH 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
@@ -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_cntreturn ;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
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().Acked-by Yonghong Song <[email protected]>Signed-off-by Emil Tsalapatis <[email protected]>---
Quoted 1 line
[...]
Emil Tsalapatis
reviewerPATCH v4 2/2 selftests/bpf: add selftests for bpf_arena_reserve_pages
In reply to Emil TsalapatisAdd 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
@@ -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
Re: [PATCH v4 2/2] selftests/bpf: add selftests for bpf_arena_reserve_pages
Quoted 6 lines · Emil Tsalapatis
Add selftests for the new
bpf_arena_reserve_pageskfunc.Acked-by Yonghong Song <[email protected]>Signed-off-by Emil Tsalapatis <[email protected]>---
Quoted 1 line
[...]
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