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