xref: /linux/tools/testing/selftests/bpf/progs/bpf_ma_ttrace.c (revision d5a821f66047c2effd06147b84775f9b2ce0f4aa)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <vmlinux.h>
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_core_read.h>
5 #include "bpf_kfuncs.h"
6 
7 struct {
8 	__uint(type, BPF_MAP_TYPE_HASH);
9 	__uint(map_flags, BPF_F_NO_PREALLOC);
10 	__uint(max_entries, 4096);
11 	__type(key, int);
12 	__type(value, long);
13 } htab SEC(".maps");
14 
15 extern const void __per_cpu_offset __ksym;
16 
17 int nr_cpus;
18 int nr_caches;
19 int in_progress;
20 int not_freed;
21 
22 /* Look at bpf_mem_cache of every cpu that htab allocates its elements from */
23 SEC("syscall")
check_ttrace(void * ctx)24 int check_ttrace(void *ctx)
25 {
26 	struct bpf_htab *h = bpf_core_cast(&htab, struct bpf_htab);
27 	unsigned long cache = (unsigned long)BPF_CORE_READ(h, ma.cache);
28 	const unsigned long *offsets = &__per_cpu_offset;
29 	struct bpf_mem_cache *c;
30 	unsigned long off;
31 	int cpu;
32 
33 	nr_caches = 0;
34 	in_progress = 0;
35 	not_freed = 0;
36 	bpf_for(cpu, 0, nr_cpus) {
37 		if (bpf_probe_read_kernel(&off, sizeof(off), offsets + cpu))
38 			return -1;
39 		c = bpf_core_cast((void *)(cache + off), struct bpf_mem_cache);
40 		if (c->unit_size)
41 			nr_caches++;
42 		if (c->call_rcu_ttrace_in_progress.counter)
43 			in_progress++;
44 		if (c->free_by_rcu_ttrace.first || c->waiting_for_gp_ttrace.first)
45 			not_freed++;
46 	}
47 	return 0;
48 }
49 
50 char _license[] SEC("license") = "GPL";
51