1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2022 Rong Tao */
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_core_read.h>
6 #include <bpf/bpf_tracing.h>
7 #include "slabratetop.h"
8
9 #define MAX_ENTRIES 10240
10
11 const volatile pid_t target_pid = 0;
12
13 static struct slabrate_info slab_zero_value = {};
14
15 struct {
16 __uint(type, BPF_MAP_TYPE_HASH);
17 __uint(max_entries, MAX_ENTRIES);
18 __type(key, char *);
19 __type(value, struct slabrate_info);
20 } slab_entries SEC(".maps");
21
probe_entry(struct kmem_cache * cachep)22 static int probe_entry(struct kmem_cache *cachep)
23 {
24 __u64 pid_tgid = bpf_get_current_pid_tgid();
25 __u32 pid = pid_tgid >> 32;
26 struct slabrate_info *valuep;
27 const char *name = BPF_CORE_READ(cachep, name);
28
29 if (target_pid && target_pid != pid)
30 return 0;
31
32 valuep = bpf_map_lookup_elem(&slab_entries, &name);
33 if (!valuep) {
34 bpf_map_update_elem(&slab_entries, &name, &slab_zero_value, BPF_ANY);
35 valuep = bpf_map_lookup_elem(&slab_entries, &name);
36 if (!valuep)
37 return 0;
38 bpf_probe_read_kernel(&valuep->name, sizeof(valuep->name), name);
39 }
40
41 valuep->count++;
42 valuep->size += BPF_CORE_READ(cachep, size);
43
44 return 0;
45 }
46
47 SEC("kprobe/kmem_cache_alloc")
BPF_KPROBE(kmem_cache_alloc,struct kmem_cache * cachep)48 int BPF_KPROBE(kmem_cache_alloc, struct kmem_cache *cachep)
49 {
50 return probe_entry(cachep);
51 }
52
53 char LICENSE[] SEC("license") = "Dual BSD/GPL";
54