1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3#include <vmlinux.h>
4#include <bpf/bpf_helpers.h>
5#include <bpf/bpf_tracing.h>
6#include <bpf/bpf_core_read.h>
7#include "bpf_experimental.h"
8
9struct {
10	__uint(type, BPF_MAP_TYPE_ARENA);
11	__uint(map_flags, BPF_F_MMAPABLE);
12	__uint(max_entries, 100); /* number of pages */
13#ifdef __TARGET_ARCH_arm64
14	__ulong(map_extra, 0x1ull << 32); /* start of mmap() region */
15#else
16	__ulong(map_extra, 0x1ull << 44); /* start of mmap() region */
17#endif
18} arena SEC(".maps");
19
20#include "bpf_arena_alloc.h"
21#include "bpf_arena_list.h"
22
23struct elem {
24	struct arena_list_node node;
25	__u64 value;
26};
27
28struct arena_list_head __arena *list_head;
29int list_sum;
30int cnt;
31bool skip = false;
32
33#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
34long __arena arena_sum;
35int __arena test_val = 1;
36struct arena_list_head __arena global_head;
37#else
38long arena_sum SEC(".addr_space.1");
39int test_val SEC(".addr_space.1");
40#endif
41
42int zero;
43
44SEC("syscall")
45int arena_list_add(void *ctx)
46{
47#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
48	__u64 i;
49
50	list_head = &global_head;
51
52	for (i = zero; i < cnt; cond_break, i++) {
53		struct elem __arena *n = bpf_alloc(sizeof(*n));
54
55		test_val++;
56		n->value = i;
57		arena_sum += i;
58		list_add_head(&n->node, list_head);
59	}
60#else
61	skip = true;
62#endif
63	return 0;
64}
65
66SEC("syscall")
67int arena_list_del(void *ctx)
68{
69#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
70	struct elem __arena *n;
71	int sum = 0;
72
73	arena_sum = 0;
74	list_for_each_entry(n, list_head, node) {
75		sum += n->value;
76		arena_sum += n->value;
77		list_del(&n->node);
78		bpf_free(n);
79	}
80	list_sum = sum;
81#else
82	skip = true;
83#endif
84	return 0;
85}
86
87char _license[] SEC("license") = "GPL";
88