xref: /linux/tools/testing/selftests/bpf/progs/arena_list.c (revision 7255fcc80d4b525cc10cfaaf7f485830d4ed2000)
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 
9 struct {
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 
23 struct elem {
24 	struct arena_list_node node;
25 	__u64 value;
26 };
27 
28 struct arena_list_head __arena *list_head;
29 int list_sum;
30 int cnt;
31 bool skip = false;
32 
33 #ifdef __BPF_FEATURE_ADDR_SPACE_CAST
34 long __arena arena_sum;
35 int __arena test_val = 1;
36 struct arena_list_head __arena global_head;
37 #else
38 long arena_sum SEC(".addr_space.1");
39 int test_val SEC(".addr_space.1");
40 #endif
41 
42 int zero;
43 
44 SEC("syscall")
45 int 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 
66 SEC("syscall")
67 int 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 
87 char _license[] SEC("license") = "GPL";
88