xref: /linux/tools/testing/selftests/bpf/progs/arena_mem_usage.c (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include "bpf_arena_common.h"
6 
7 struct {
8 	__uint(type, BPF_MAP_TYPE_ARENA);
9 	__uint(map_flags, BPF_F_MMAPABLE);
10 	__uint(max_entries, 1000); /* number of pages */
11 #ifdef __TARGET_ARCH_arm64
12 	__ulong(map_extra, 0x1ull << 32); /* start of mmap() region */
13 #else
14 	__ulong(map_extra, 0x1ull << 44); /* start of mmap() region */
15 #endif
16 } arena SEC(".maps");
17 
18 void __arena *ptr;
19 int alloc_cnt;		/* in:  pages to allocate */
20 long free_byte_off;	/* in:  byte offset within ptr to start freeing */
21 int free_cnt;		/* in:  pages to free */
22 
23 SEC("syscall")
alloc(void * ctx)24 int alloc(void *ctx)
25 {
26 	ptr = bpf_arena_alloc_pages(&arena, NULL, alloc_cnt, NUMA_NO_NODE, 0);
27 	/* Success/failure is checked from user space via skel->bss->ptr. */
28 	return 0;
29 }
30 
31 SEC("syscall")
free_pages(void * ctx)32 int free_pages(void *ctx)
33 {
34 	if (!ptr)
35 		return 1;
36 	bpf_arena_free_pages(&arena, (char __arena *)ptr + free_byte_off, free_cnt);
37 	return 0;
38 }
39 
40 char _license[] SEC("license") = "GPL";
41