1ed7c1377SFeng Zhou // SPDX-License-Identifier: GPL-2.0
2*7aa424e0SFeng Zhou /* Copyright (c) 2022 Bytedance */
3ed7c1377SFeng Zhou 
4ed7c1377SFeng Zhou #include "vmlinux.h"
5ed7c1377SFeng Zhou #include <bpf/bpf_helpers.h>
6ed7c1377SFeng Zhou 
7*7aa424e0SFeng Zhou __u64 percpu_array_elem_sum = 0;
8*7aa424e0SFeng Zhou __u64 percpu_hash_elem_sum = 0;
9*7aa424e0SFeng Zhou __u64 percpu_lru_hash_elem_sum = 0;
10*7aa424e0SFeng Zhou const volatile int nr_cpus;
11*7aa424e0SFeng Zhou const volatile int my_pid;
12ed7c1377SFeng Zhou 
13ed7c1377SFeng Zhou struct {
14ed7c1377SFeng Zhou 	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
15ed7c1377SFeng Zhou 	__uint(max_entries, 1);
16ed7c1377SFeng Zhou 	__type(key, __u32);
17*7aa424e0SFeng Zhou 	__type(value, __u64);
18ed7c1377SFeng Zhou } percpu_array_map SEC(".maps");
19ed7c1377SFeng Zhou 
20ed7c1377SFeng Zhou struct {
21ed7c1377SFeng Zhou 	__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
22ed7c1377SFeng Zhou 	__uint(max_entries, 1);
23*7aa424e0SFeng Zhou 	__type(key, __u64);
24*7aa424e0SFeng Zhou 	__type(value, __u64);
25ed7c1377SFeng Zhou } percpu_hash_map SEC(".maps");
26ed7c1377SFeng Zhou 
27ed7c1377SFeng Zhou struct {
28ed7c1377SFeng Zhou 	__uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
29ed7c1377SFeng Zhou 	__uint(max_entries, 1);
30*7aa424e0SFeng Zhou 	__type(key, __u64);
31*7aa424e0SFeng Zhou 	__type(value, __u64);
32ed7c1377SFeng Zhou } percpu_lru_hash_map SEC(".maps");
33ed7c1377SFeng Zhou 
34*7aa424e0SFeng Zhou struct read_percpu_elem_ctx {
35*7aa424e0SFeng Zhou 	void *map;
36*7aa424e0SFeng Zhou 	__u64 sum;
37*7aa424e0SFeng Zhou };
38*7aa424e0SFeng Zhou 
read_percpu_elem_callback(__u32 index,struct read_percpu_elem_ctx * ctx)39*7aa424e0SFeng Zhou static int read_percpu_elem_callback(__u32 index, struct read_percpu_elem_ctx *ctx)
40*7aa424e0SFeng Zhou {
41*7aa424e0SFeng Zhou 	__u64 key = 0;
42*7aa424e0SFeng Zhou 	__u64 *value;
43*7aa424e0SFeng Zhou 
44*7aa424e0SFeng Zhou 	value = bpf_map_lookup_percpu_elem(ctx->map, &key, index);
45*7aa424e0SFeng Zhou 	if (value)
46*7aa424e0SFeng Zhou 		ctx->sum += *value;
47*7aa424e0SFeng Zhou 	return 0;
48*7aa424e0SFeng Zhou }
49*7aa424e0SFeng Zhou 
50ed7c1377SFeng Zhou SEC("tp/syscalls/sys_enter_getuid")
sysenter_getuid(const void * ctx)51ed7c1377SFeng Zhou int sysenter_getuid(const void *ctx)
52ed7c1377SFeng Zhou {
53*7aa424e0SFeng Zhou 	struct read_percpu_elem_ctx map_ctx;
54ed7c1377SFeng Zhou 
55*7aa424e0SFeng Zhou 	if (my_pid != (bpf_get_current_pid_tgid() >> 32))
56*7aa424e0SFeng Zhou 		return 0;
57ed7c1377SFeng Zhou 
58*7aa424e0SFeng Zhou 	map_ctx.map = &percpu_array_map;
59*7aa424e0SFeng Zhou 	map_ctx.sum = 0;
60*7aa424e0SFeng Zhou 	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
61*7aa424e0SFeng Zhou 	percpu_array_elem_sum = map_ctx.sum;
62ed7c1377SFeng Zhou 
63*7aa424e0SFeng Zhou 	map_ctx.map = &percpu_hash_map;
64*7aa424e0SFeng Zhou 	map_ctx.sum = 0;
65*7aa424e0SFeng Zhou 	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
66*7aa424e0SFeng Zhou 	percpu_hash_elem_sum = map_ctx.sum;
67*7aa424e0SFeng Zhou 
68*7aa424e0SFeng Zhou 	map_ctx.map = &percpu_lru_hash_map;
69*7aa424e0SFeng Zhou 	map_ctx.sum = 0;
70*7aa424e0SFeng Zhou 	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
71*7aa424e0SFeng Zhou 	percpu_lru_hash_elem_sum = map_ctx.sum;
72ed7c1377SFeng Zhou 
73ed7c1377SFeng Zhou 	return 0;
74ed7c1377SFeng Zhou }
75ed7c1377SFeng Zhou 
76ed7c1377SFeng Zhou char _license[] SEC("license") = "GPL";
77