xref: /linux/tools/testing/selftests/bpf/progs/arena_htab.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 } arena SEC(".maps");
15 
16 #include "bpf_arena_htab.h"
17 
18 void __arena *htab_for_user;
19 bool skip = false;
20 
21 int zero = 0;
22 char __arena arr1[100000];
23 char arr2[1000];
24 
25 SEC("syscall")
arena_htab_llvm(void * ctx)26 int arena_htab_llvm(void *ctx)
27 {
28 #if defined(__BPF_FEATURE_ADDR_SPACE_CAST) || defined(BPF_ARENA_FORCE_ASM)
29 	struct htab __arena *htab;
30 	char __arena *arr = arr1;
31 	__u64 i;
32 
33 	htab = bpf_alloc(sizeof(*htab));
34 	cast_kern(htab);
35 	htab_init(htab);
36 
37 	cast_kern(arr);
38 
39 	/* first run. No old elems in the table */
40 	for (i = zero; i < 100000 && can_loop; i++) {
41 		htab_update_elem(htab, i, i);
42 		arr[i] = i;
43 	}
44 
45 	/* should replace some elems with new ones */
46 	for (i = zero; i < 1000 && can_loop; i++) {
47 		htab_update_elem(htab, i, i);
48 		/* Access mem to make the verifier use bounded loop logic */
49 		arr2[i] = i;
50 	}
51 	cast_user(htab);
52 	htab_for_user = htab;
53 #else
54 	skip = true;
55 #endif
56 	return 0;
57 }
58 
59 char _license[] SEC("license") = "GPL";
60