linux/tools/testing/selftests/bpf/progs/arena_mem_usage.c
Jiayuan Chen 29e4bcf126
selftests/bpf: Add tests for memory usage for arena
Allocate and free arena pages, both from BPF and via user-space fault-in,
and check that the map's memlock in fdinfo tracks the number of pages that
are actually populated.

Like the other arena tests it runs serially.

test:
  ./test_progs -a arena_mem_usage
  #5       arena_mem_usage:OK
  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Link: https://lore.kernel.org/bpf/20260717114117.350851-5-jiayuan.chen@linux.dev
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
2026-07-19 17:56:10 +02:00

41 lines
1007 B
C

// SPDX-License-Identifier: GPL-2.0
#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, 1000); /* number of pages */
#ifdef __TARGET_ARCH_arm64
__ulong(map_extra, 0x1ull << 32); /* start of mmap() region */
#else
__ulong(map_extra, 0x1ull << 44); /* start of mmap() region */
#endif
} arena SEC(".maps");
void __arena *ptr;
int alloc_cnt; /* in: pages to allocate */
long free_byte_off; /* in: byte offset within ptr to start freeing */
int free_cnt; /* in: pages to free */
SEC("syscall")
int alloc(void *ctx)
{
ptr = bpf_arena_alloc_pages(&arena, NULL, alloc_cnt, NUMA_NO_NODE, 0);
/* Success/failure is checked from user space via skel->bss->ptr. */
return 0;
}
SEC("syscall")
int free_pages(void *ctx)
{
if (!ptr)
return 1;
bpf_arena_free_pages(&arena, (char __arena *)ptr + free_byte_off, free_cnt);
return 0;
}
char _license[] SEC("license") = "GPL";