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