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