* [PATCH bpf v4 0/2] bpf: bound arena allocations to the user VMA
@ 2026-11-22 12:35 ` YanXin Li
0 siblings, 0 replies; 7+ messages in thread
From: YanXin Li via B4 Relay @ 2026-11-22 12:35 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel, stable
When map_extra is omitted, a short first mmap() can leave free arena
pages beyond the user VMA. Automatic allocation can select those pages;
near a 4 GiB boundary, the kernel mapping can extend past the arena's
vmalloc reservation. Patch 1 confines the range tree to the VMA, and
patch 2 adds a regression selftest.
---
Changes in v4:
- Check the first allocation in the selftest and remove the incorrect
offset-zero NULL comment. Patch 1 is unchanged.
- Link to v3: https://lore.kernel.org/r/20261122-b4-03-bpf-arena-vmalloc-oob-v3-da2dc21683c6-v3-0-1f94ae9a884a@pm.me
To: Alexei Starovoitov <ast@kernel.org>
To: Daniel Borkmann <daniel@iogearbox.net>
To: Andrii Nakryiko <andrii@kernel.org>
To: Eduard Zingerman <eddyz87@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Song Liu <song@kernel.org>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Emil Tsalapatis <emil@etsalapatis.com>
Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
Cc: bpf@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
YanXin Li (2):
bpf: bound arena allocations to the user VMA
selftests/bpf: test arena allocation after a short mmap
kernel/bpf/arena.c | 25 ++++++++++
.../testing/selftests/bpf/prog_tests/arena_mmap.c | 53 ++++++++++++++++++++++
.../testing/selftests/bpf/progs/arena_mmap_alloc.c | 33 ++++++++++++++
3 files changed, 111 insertions(+)
---
base-commit: c44104f3642da720164042623e4832836aefdd8e
change-id: 20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-f2eb9eff6db8
Best regards,
--
YanXin Li <fadouse@pm.me>
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH bpf v4 0/2] bpf: bound arena allocations to the user VMA @ 2026-11-22 12:35 ` YanXin Li 0 siblings, 0 replies; 7+ messages in thread From: YanXin Li @ 2026-11-22 12:35 UTC (permalink / raw) To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel, stable When map_extra is omitted, a short first mmap() can leave free arena pages beyond the user VMA. Automatic allocation can select those pages; near a 4 GiB boundary, the kernel mapping can extend past the arena's vmalloc reservation. Patch 1 confines the range tree to the VMA, and patch 2 adds a regression selftest. --- Changes in v4: - Check the first allocation in the selftest and remove the incorrect offset-zero NULL comment. Patch 1 is unchanged. - Link to v3: https://lore.kernel.org/r/20261122-b4-03-bpf-arena-vmalloc-oob-v3-da2dc21683c6-v3-0-1f94ae9a884a@pm.me To: Alexei Starovoitov <ast@kernel.org> To: Daniel Borkmann <daniel@iogearbox.net> To: Andrii Nakryiko <andrii@kernel.org> To: Eduard Zingerman <eddyz87@gmail.com> To: Kumar Kartikeya Dwivedi <memxor@gmail.com> Cc: Martin KaFai Lau <martin.lau@linux.dev> Cc: Song Liu <song@kernel.org> Cc: Yonghong Song <yonghong.song@linux.dev> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Emil Tsalapatis <emil@etsalapatis.com> Cc: Ihor Solodrai <ihor.solodrai@linux.dev> Cc: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- YanXin Li (2): bpf: bound arena allocations to the user VMA selftests/bpf: test arena allocation after a short mmap kernel/bpf/arena.c | 25 ++++++++++ .../testing/selftests/bpf/prog_tests/arena_mmap.c | 53 ++++++++++++++++++++++ .../testing/selftests/bpf/progs/arena_mmap_alloc.c | 33 ++++++++++++++ 3 files changed, 111 insertions(+) --- base-commit: c44104f3642da720164042623e4832836aefdd8e change-id: 20261122-b4-03-bpf-arena-vmalloc-oob-v4-8090cdc58ff8-f2eb9eff6db8 Best regards, -- YanXin Li <fadouse@pm.me> ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf v4 1/2] bpf: bound arena allocations to the user VMA 2026-11-22 12:35 ` YanXin Li @ 2026-11-22 12:35 ` YanXin Li -1 siblings, 0 replies; 7+ messages in thread From: YanXin Li via B4 Relay @ 2026-11-22 12:35 UTC (permalink / raw) To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel, stable From: YanXin Li <fadouse@pm.me> An arena map initializes its free range tree to max_entries pages. When map_extra is zero, the first mmap() may cover fewer pages. Fixed-address allocations are bounded by the VMA size, but automatic allocations use range_tree_find() across the whole tree and can select a range beyond the VMA. arena_free_pages() clips frees to the VMA, so these pages cannot be released through the arena kfunc. With 4 KiB pages, set max_entries to 1 << 20 and mmap() a 288-page VMA at 0xffee0000, ending at the next 4 GiB boundary. A 287-page automatic allocation takes offsets [0, 287). A subsequent 288-page allocation starts at offset 287, so its kernel mapping starts at kern_vm_start + 0xfffff000. One page lies within the 4 GiB data band, eight lie in the arena's upper half-guard, and 279 lie beyond its requested vmalloc area. The first of those 279 is the vmalloc guard page. The remaining 278 lie beyond the full reservation. The current apply_to_page_range() path can install PTEs there. An unpatched local test returned a non-NULL pointer for this allocation. Another run triggered a WARNING in apply_range_set_cb() on a non-scratch PTE. Out-of-VMA offsets need not wrap the low 32 bits. For example, when the user VMA starts at a 4 GiB aligned address, they can still map inside the arena's kernel area. They remain invalid because the VMA does not cover them. The range-tree conversion replaced the bounded mtree_alloc_range() search with range_tree_find() over max_entries. The later switch from vm_area_map_pages() to apply_to_page_range() removed the vm_struct bounds check, allowing these selections to reach beyond the arena's vmalloc reservation. On the first mmap() without map_extra, remove [vma_pages, max_entries) from the free range tree before publishing the VMA bounds. The VMA size is fixed after this mmap(), and the fault, reserve and free paths do not add offsets outside it. Automatic allocations consequently stay inside the VMA. This issue was found through LLM-assisted code audit and manual code inspection of kernel/bpf/arena.c, then checked with a Z3 SMT model and a custom BPF test in an x86-64 QEMU/KVM guest. The test kernel identified itself as 7.4.0-rc3-84074-gc44104f3642d. KASAN, UBSAN, lockdep and DEBUG_OBJECTS were enabled. After the patch, the 288-page request returns NULL, the 287-page in-VMA request succeeds, and the test reports no kernel warnings. Fixes: b795379757eb ("bpf: Introduce range_tree data structure and use it in bpf arena") Cc: stable@vger.kernel.org Signed-off-by: YanXin Li <fadouse@pm.me> --- kernel/bpf/arena.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index ec2bda1a6eb..94321dec638 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -694,6 +694,31 @@ static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma) if (remember_vma(arena, vma)) return -ENOMEM; + if (!arena->user_vm_start && vma_pages(vma) < map->max_entries) { + /* + * Without map_extra, the first mmap() can be shorter than + * max_entries. Remove offsets outside the VMA from the free + * range tree so automatic allocations stay within the VMA. + */ + unsigned long flags; + int ret = -EBUSY; + + if (!raw_res_spin_lock_irqsave(&arena->spinlock, flags)) { + ret = range_tree_clear(&arena->rt, vma_pages(vma), + map->max_entries - vma_pages(vma)); + raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + WARN_ON_ONCE(ret); + } + if (ret) { + struct vma_list *vml = vma->vm_private_data; + + list_del(&vml->head); + vma->vm_private_data = NULL; + kfree(vml); + return ret; + } + } + arena->user_vm_start = vma->vm_start; arena->user_vm_end = vma->vm_end; /* -- Git-157) ^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH bpf v4 1/2] bpf: bound arena allocations to the user VMA @ 2026-11-22 12:35 ` YanXin Li 0 siblings, 0 replies; 7+ messages in thread From: YanXin Li @ 2026-11-22 12:35 UTC (permalink / raw) To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel, stable An arena map initializes its free range tree to max_entries pages. When map_extra is zero, the first mmap() may cover fewer pages. Fixed-address allocations are bounded by the VMA size, but automatic allocations use range_tree_find() across the whole tree and can select a range beyond the VMA. arena_free_pages() clips frees to the VMA, so these pages cannot be released through the arena kfunc. With 4 KiB pages, set max_entries to 1 << 20 and mmap() a 288-page VMA at 0xffee0000, ending at the next 4 GiB boundary. A 287-page automatic allocation takes offsets [0, 287). A subsequent 288-page allocation starts at offset 287, so its kernel mapping starts at kern_vm_start + 0xfffff000. One page lies within the 4 GiB data band, eight lie in the arena's upper half-guard, and 279 lie beyond its requested vmalloc area. The first of those 279 is the vmalloc guard page. The remaining 278 lie beyond the full reservation. The current apply_to_page_range() path can install PTEs there. An unpatched local test returned a non-NULL pointer for this allocation. Another run triggered a WARNING in apply_range_set_cb() on a non-scratch PTE. Out-of-VMA offsets need not wrap the low 32 bits. For example, when the user VMA starts at a 4 GiB aligned address, they can still map inside the arena's kernel area. They remain invalid because the VMA does not cover them. The range-tree conversion replaced the bounded mtree_alloc_range() search with range_tree_find() over max_entries. The later switch from vm_area_map_pages() to apply_to_page_range() removed the vm_struct bounds check, allowing these selections to reach beyond the arena's vmalloc reservation. On the first mmap() without map_extra, remove [vma_pages, max_entries) from the free range tree before publishing the VMA bounds. The VMA size is fixed after this mmap(), and the fault, reserve and free paths do not add offsets outside it. Automatic allocations consequently stay inside the VMA. This issue was found through LLM-assisted code audit and manual code inspection of kernel/bpf/arena.c, then checked with a Z3 SMT model and a custom BPF test in an x86-64 QEMU/KVM guest. The test kernel identified itself as 7.4.0-rc3-84074-gc44104f3642d. KASAN, UBSAN, lockdep and DEBUG_OBJECTS were enabled. After the patch, the 288-page request returns NULL, the 287-page in-VMA request succeeds, and the test reports no kernel warnings. Fixes: b795379757eb ("bpf: Introduce range_tree data structure and use it in bpf arena") Cc: stable@vger.kernel.org Signed-off-by: YanXin Li <fadouse@pm.me> --- kernel/bpf/arena.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index ec2bda1a6eb..94321dec638 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -694,6 +694,31 @@ static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma) if (remember_vma(arena, vma)) return -ENOMEM; + if (!arena->user_vm_start && vma_pages(vma) < map->max_entries) { + /* + * Without map_extra, the first mmap() can be shorter than + * max_entries. Remove offsets outside the VMA from the free + * range tree so automatic allocations stay within the VMA. + */ + unsigned long flags; + int ret = -EBUSY; + + if (!raw_res_spin_lock_irqsave(&arena->spinlock, flags)) { + ret = range_tree_clear(&arena->rt, vma_pages(vma), + map->max_entries - vma_pages(vma)); + raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + WARN_ON_ONCE(ret); + } + if (ret) { + struct vma_list *vml = vma->vm_private_data; + + list_del(&vml->head); + vma->vm_private_data = NULL; + kfree(vml); + return ret; + } + } + arena->user_vm_start = vma->vm_start; arena->user_vm_end = vma->vm_end; /* -- Git-157) ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH bpf v4 1/2] bpf: bound arena allocations to the user VMA 2026-11-22 12:35 ` YanXin Li (?) @ 2026-12-01 5:16 ` Alexei Starovoitov -1 siblings, 0 replies; 7+ messages in thread From: Alexei Starovoitov @ 2026-12-01 5:16 UTC (permalink / raw) To: fadouse, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel, stable On Sun, Nov 22, 2026 at 04:35 AM YanXin Li via B4 Relay <devnull+fadouse.pm.me@kernel.org> wrote: > An arena map initializes its free range tree to max_entries pages. When > map_extra is zero, the first mmap() may cover fewer pages. Fixed-address > allocations are bounded by the VMA size, but automatic allocations use Already fixed differently. pw-bot: cr ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf v4 2/2] selftests/bpf: test arena allocation after a short mmap 2026-11-22 12:35 ` YanXin Li @ 2026-11-22 12:35 ` YanXin Li via B4 Relay -1 siblings, 0 replies; 7+ messages in thread From: YanXin Li @ 2026-11-22 12:35 UTC (permalink / raw) To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel Without map_extra, the first mmap of an arena can be shorter than the map's max_entries. Automatic allocations must stay within that VMA. Create an eight-page arena and mmap only four pages before loading a BPF program. Reuse the map FD so libbpf does not mmap the full arena. The program occupies offset zero and allocates two more pages, leaving one page within the VMA. A two-page request must fail, and a final one-page request must succeed. Before the preceding fix, the two-page request succeeds outside the VMA. When JIT is disabled, arena map creation can succeed while loading a program that uses the map returns -EOPNOTSUPP. Skip the subtest in that case. Signed-off-by: YanXin Li <fadouse@pm.me> --- .../testing/selftests/bpf/prog_tests/arena_mmap.c | 53 ++++++++++++++++++++++ .../testing/selftests/bpf/progs/arena_mmap_alloc.c | 33 ++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c index f55d950720f..ff1fe0a6f22 100644 --- a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c +++ b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c @@ -2,6 +2,7 @@ #include <test_progs.h> #include <sys/mman.h> +#include "arena_mmap_alloc.skel.h" #define ARENA_PAGES 8 @@ -34,6 +35,56 @@ static void test_refused(__u64 map_extra, __u64 addr, int pages, int flags, int close(fd); } +static void test_short_mmap_alloc(void) +{ + LIBBPF_OPTS(bpf_map_create_opts, map_opts, .map_flags = BPF_F_MMAPABLE); + LIBBPF_OPTS(bpf_test_run_opts, run_opts); + struct arena_mmap_alloc *skel = NULL; + size_t size = 4 * getpagesize(); + void *area; + int fd, err; + + fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena", 0, 0, + ARENA_PAGES, &map_opts); + if (fd == -EOPNOTSUPP) { + test__skip(); + return; + } + if (!ASSERT_GE(fd, 0, "bpf_map_create")) + return; + + /* The short mapping must be the first mmap() of this arena. */ + area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (!ASSERT_NEQ(area, MAP_FAILED, "short_mmap")) + goto out_close; + + /* Reuse the map so libbpf does not mmap all ARENA_PAGES on load. */ + skel = arena_mmap_alloc__open(); + if (!ASSERT_OK_PTR(skel, "arena_mmap_alloc__open")) + goto out_unmap; + err = bpf_map__reuse_fd(skel->maps.arena, fd); + if (!ASSERT_OK(err, "reuse_arena_fd")) + goto out_destroy; + err = arena_mmap_alloc__load(skel); + if (err == -EOPNOTSUPP) { + test__skip(); + goto out_destroy; + } + if (!ASSERT_OK(err, "arena_mmap_alloc__load")) + goto out_destroy; + err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.alloc_beyond_vma), + &run_opts); + if (ASSERT_OK(err, "alloc_beyond_vma")) + ASSERT_OK(run_opts.retval, "alloc_beyond_vma_retval"); + +out_destroy: + arena_mmap_alloc__destroy(skel); +out_unmap: + munmap(area, size); +out_close: + close(fd); +} + void serial_test_arena_mmap(void) { __u64 page = getpagesize(), last = -(ARENA_PAGES * page); @@ -50,4 +101,6 @@ void serial_test_arena_mmap(void) test_refused(0, -page, ARENA_PAGES, MAP_FIXED, 0); if (test__start_subtest("map_extra_last_pages")) test_refused(last, last, ARENA_PAGES, MAP_FIXED, 0); + if (test__start_subtest("short_mmap_alloc")) + test_short_mmap_alloc(); } diff --git a/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c new file mode 100644 index 00000000000..9c46a393eae --- /dev/null +++ b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define BPF_NO_KFUNC_PROTOTYPES +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_arena_common.h" + +struct { + __uint(type, BPF_MAP_TYPE_ARENA); + __uint(map_flags, BPF_F_MMAPABLE); + __uint(max_entries, 8); +} arena SEC(".maps"); + +SEC("syscall") +int alloc_beyond_vma(void *ctx) +{ + void __arena *page; + + page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + if (!page) + return 4; + page = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0); + if (!page) + return 1; + /* Only one of the four mmap()ed pages remains available. */ + page = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0); + if (page) + return 2; + page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + return page ? 0 : 3; +} + +char _license[] SEC("license") = "GPL"; -- Git-157) ^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH bpf v4 2/2] selftests/bpf: test arena allocation after a short mmap @ 2026-11-22 12:35 ` YanXin Li via B4 Relay 0 siblings, 0 replies; 7+ messages in thread From: YanXin Li via B4 Relay @ 2026-11-22 12:35 UTC (permalink / raw) To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi Cc: Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, bpf, linux-kernel From: YanXin Li <fadouse@pm.me> Without map_extra, the first mmap of an arena can be shorter than the map's max_entries. Automatic allocations must stay within that VMA. Create an eight-page arena and mmap only four pages before loading a BPF program. Reuse the map FD so libbpf does not mmap the full arena. The program occupies offset zero and allocates two more pages, leaving one page within the VMA. A two-page request must fail, and a final one-page request must succeed. Before the preceding fix, the two-page request succeeds outside the VMA. When JIT is disabled, arena map creation can succeed while loading a program that uses the map returns -EOPNOTSUPP. Skip the subtest in that case. Signed-off-by: YanXin Li <fadouse@pm.me> --- .../testing/selftests/bpf/prog_tests/arena_mmap.c | 53 ++++++++++++++++++++++ .../testing/selftests/bpf/progs/arena_mmap_alloc.c | 33 ++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c index f55d950720f..ff1fe0a6f22 100644 --- a/tools/testing/selftests/bpf/prog_tests/arena_mmap.c +++ b/tools/testing/selftests/bpf/prog_tests/arena_mmap.c @@ -2,6 +2,7 @@ #include <test_progs.h> #include <sys/mman.h> +#include "arena_mmap_alloc.skel.h" #define ARENA_PAGES 8 @@ -34,6 +35,56 @@ static void test_refused(__u64 map_extra, __u64 addr, int pages, int flags, int close(fd); } +static void test_short_mmap_alloc(void) +{ + LIBBPF_OPTS(bpf_map_create_opts, map_opts, .map_flags = BPF_F_MMAPABLE); + LIBBPF_OPTS(bpf_test_run_opts, run_opts); + struct arena_mmap_alloc *skel = NULL; + size_t size = 4 * getpagesize(); + void *area; + int fd, err; + + fd = bpf_map_create(BPF_MAP_TYPE_ARENA, "arena", 0, 0, + ARENA_PAGES, &map_opts); + if (fd == -EOPNOTSUPP) { + test__skip(); + return; + } + if (!ASSERT_GE(fd, 0, "bpf_map_create")) + return; + + /* The short mapping must be the first mmap() of this arena. */ + area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (!ASSERT_NEQ(area, MAP_FAILED, "short_mmap")) + goto out_close; + + /* Reuse the map so libbpf does not mmap all ARENA_PAGES on load. */ + skel = arena_mmap_alloc__open(); + if (!ASSERT_OK_PTR(skel, "arena_mmap_alloc__open")) + goto out_unmap; + err = bpf_map__reuse_fd(skel->maps.arena, fd); + if (!ASSERT_OK(err, "reuse_arena_fd")) + goto out_destroy; + err = arena_mmap_alloc__load(skel); + if (err == -EOPNOTSUPP) { + test__skip(); + goto out_destroy; + } + if (!ASSERT_OK(err, "arena_mmap_alloc__load")) + goto out_destroy; + err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.alloc_beyond_vma), + &run_opts); + if (ASSERT_OK(err, "alloc_beyond_vma")) + ASSERT_OK(run_opts.retval, "alloc_beyond_vma_retval"); + +out_destroy: + arena_mmap_alloc__destroy(skel); +out_unmap: + munmap(area, size); +out_close: + close(fd); +} + void serial_test_arena_mmap(void) { __u64 page = getpagesize(), last = -(ARENA_PAGES * page); @@ -50,4 +101,6 @@ void serial_test_arena_mmap(void) test_refused(0, -page, ARENA_PAGES, MAP_FIXED, 0); if (test__start_subtest("map_extra_last_pages")) test_refused(last, last, ARENA_PAGES, MAP_FIXED, 0); + if (test__start_subtest("short_mmap_alloc")) + test_short_mmap_alloc(); } diff --git a/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c new file mode 100644 index 00000000000..9c46a393eae --- /dev/null +++ b/tools/testing/selftests/bpf/progs/arena_mmap_alloc.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define BPF_NO_KFUNC_PROTOTYPES +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_arena_common.h" + +struct { + __uint(type, BPF_MAP_TYPE_ARENA); + __uint(map_flags, BPF_F_MMAPABLE); + __uint(max_entries, 8); +} arena SEC(".maps"); + +SEC("syscall") +int alloc_beyond_vma(void *ctx) +{ + void __arena *page; + + page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + if (!page) + return 4; + page = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0); + if (!page) + return 1; + /* Only one of the four mmap()ed pages remains available. */ + page = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0); + if (page) + return 2; + page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); + return page ? 0 : 3; +} + +char _license[] SEC("license") = "GPL"; -- Git-157) ^ permalink raw reply related [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-01 5:16 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-22 12:35 [PATCH bpf v4 0/2] bpf: bound arena allocations to the user VMA YanXin Li via B4 Relay 2026-11-22 12:35 ` YanXin Li 2026-11-22 12:35 ` [PATCH bpf v4 1/2] " YanXin Li via B4 Relay 2026-11-22 12:35 ` YanXin Li 2026-12-01 5:16 ` Alexei Starovoitov 2026-11-22 12:35 ` [PATCH bpf v4 2/2] selftests/bpf: test arena allocation after a short mmap YanXin Li 2026-11-22 12:35 ` YanXin Li via B4 Relay
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.