xref: /linux/tools/testing/selftests/bpf/progs/arena_htab.c (revision b4db9f840283caca0d904436f187ef56a9126eaa)
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 } arena SEC(".maps");
14 
15 #include "bpf_arena_htab.h"
16 
17 void __arena *htab_for_user;
18 bool skip = false;
19 
20 int zero = 0;
21 
22 SEC("syscall")
23 int arena_htab_llvm(void *ctx)
24 {
25 #if defined(__BPF_FEATURE_ADDR_SPACE_CAST) || defined(BPF_ARENA_FORCE_ASM)
26 	struct htab __arena *htab;
27 	__u64 i;
28 
29 	htab = bpf_alloc(sizeof(*htab));
30 	cast_kern(htab);
31 	htab_init(htab);
32 
33 	/* first run. No old elems in the table */
34 	for (i = zero; i < 1000; i++)
35 		htab_update_elem(htab, i, i);
36 
37 	/* should replace all elems with new ones */
38 	for (i = zero; i < 1000; i++)
39 		htab_update_elem(htab, i, i);
40 	cast_user(htab);
41 	htab_for_user = htab;
42 #else
43 	skip = true;
44 #endif
45 	return 0;
46 }
47 
48 char _license[] SEC("license") = "GPL";
49