1258057Sbr// SPDX-License-Identifier: GPL-2.0
2261406Sbr/* Copyright (c) 2022 Bytedance */
3258057Sbr
4258057Sbr#include "vmlinux.h"
5258057Sbr#include <bpf/bpf_helpers.h>
6258057Sbr
7258057Sbr__u64 percpu_array_elem_sum = 0;
8258057Sbr__u64 percpu_hash_elem_sum = 0;
9258057Sbr__u64 percpu_lru_hash_elem_sum = 0;
10258057Sbrconst volatile int nr_cpus;
11258057Sbrconst volatile int my_pid;
12258057Sbr
13258057Sbrstruct {
14258057Sbr	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
15258057Sbr	__uint(max_entries, 1);
16258057Sbr	__type(key, __u32);
17258057Sbr	__type(value, __u64);
18258057Sbr} percpu_array_map SEC(".maps");
19258057Sbr
20258057Sbrstruct {
21258057Sbr	__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
22258057Sbr	__uint(max_entries, 1);
23258057Sbr	__type(key, __u64);
24258057Sbr	__type(value, __u64);
25258057Sbr} percpu_hash_map SEC(".maps");
26258057Sbr
27258057Sbrstruct {
28258057Sbr	__uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
29258057Sbr	__uint(max_entries, 1);
30258057Sbr	__type(key, __u64);
31261406Sbr	__type(value, __u64);
32258057Sbr} percpu_lru_hash_map SEC(".maps");
33258057Sbr
34258057Sbrstruct read_percpu_elem_ctx {
35258057Sbr	void *map;
36258057Sbr	__u64 sum;
37258057Sbr};
38258057Sbr
39258057Sbrstatic int read_percpu_elem_callback(__u32 index, struct read_percpu_elem_ctx *ctx)
40258057Sbr{
41261406Sbr	__u64 key = 0;
42261406Sbr	__u64 *value;
43261406Sbr
44261406Sbr	value = bpf_map_lookup_percpu_elem(ctx->map, &key, index);
45261406Sbr	if (value)
46261406Sbr		ctx->sum += *value;
47261406Sbr	return 0;
48261406Sbr}
49261406Sbr
50258057SbrSEC("tp/syscalls/sys_enter_getuid")
51258057Sbrint sysenter_getuid(const void *ctx)
52258057Sbr{
53258057Sbr	struct read_percpu_elem_ctx map_ctx;
54258057Sbr
55258057Sbr	if (my_pid != (bpf_get_current_pid_tgid() >> 32))
56258057Sbr		return 0;
57258057Sbr
58258057Sbr	map_ctx.map = &percpu_array_map;
59258057Sbr	map_ctx.sum = 0;
60258057Sbr	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
61258057Sbr	percpu_array_elem_sum = map_ctx.sum;
62258057Sbr
63258057Sbr	map_ctx.map = &percpu_hash_map;
64258057Sbr	map_ctx.sum = 0;
65258057Sbr	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
66258057Sbr	percpu_hash_elem_sum = map_ctx.sum;
67258057Sbr
68258057Sbr	map_ctx.map = &percpu_lru_hash_map;
69258057Sbr	map_ctx.sum = 0;
70258057Sbr	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
71258057Sbr	percpu_lru_hash_elem_sum = map_ctx.sum;
72258057Sbr
73258057Sbr	return 0;
74258057Sbr}
75258057Sbr
76258057Sbrchar _license[] SEC("license") = "GPL";
77258057Sbr