xref: /linux/tools/testing/selftests/bpf/progs/btf_type_tag_percpu.c (revision 64980441d26995ea5599958740dbf6d791e81e27)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Google */
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 
7 struct bpf_testmod_btf_type_tag_1 {
8 	int a;
9 };
10 
11 struct bpf_testmod_btf_type_tag_2 {
12 	struct bpf_testmod_btf_type_tag_1 *p;
13 };
14 
15 __u64 g;
16 
17 SEC("fentry/bpf_testmod_test_btf_type_tag_percpu_1")
BPF_PROG(test_percpu1,struct bpf_testmod_btf_type_tag_1 * arg)18 int BPF_PROG(test_percpu1, struct bpf_testmod_btf_type_tag_1 *arg)
19 {
20 	g = arg->a;
21 	return 0;
22 }
23 
24 SEC("fentry/bpf_testmod_test_btf_type_tag_percpu_2")
BPF_PROG(test_percpu2,struct bpf_testmod_btf_type_tag_2 * arg)25 int BPF_PROG(test_percpu2, struct bpf_testmod_btf_type_tag_2 *arg)
26 {
27 	g = arg->p->a;
28 	return 0;
29 }
30 
31 /* trace_cgroup_mkdir(struct cgroup *cgrp, const char *path)
32  *
33  * struct css_rstat_cpu {
34  *   ...
35  *   struct cgroup_subsys_state *updated_children;
36  *   ...
37  * };
38  *
39  * struct cgroup_subsys_state {
40  *   ...
41  *   struct css_rstat_cpu __percpu *rstat_cpu;
42  *   ...
43  * };
44  *
45  * struct cgroup {
46  *   struct cgroup_subsys_state self;
47  *   ...
48  * };
49  */
50 SEC("tp_btf/cgroup_mkdir")
BPF_PROG(test_percpu_load,struct cgroup * cgrp,const char * path)51 int BPF_PROG(test_percpu_load, struct cgroup *cgrp, const char *path)
52 {
53 	g = (__u64)cgrp->self.rstat_cpu->updated_children;
54 	return 0;
55 }
56 
57 SEC("tp_btf/cgroup_mkdir")
BPF_PROG(test_percpu_helper,struct cgroup * cgrp,const char * path)58 int BPF_PROG(test_percpu_helper, struct cgroup *cgrp, const char *path)
59 {
60 	struct css_rstat_cpu *rstat;
61 	__u32 cpu;
62 
63 	cpu = bpf_get_smp_processor_id();
64 	rstat = (struct css_rstat_cpu *)bpf_per_cpu_ptr(
65 			cgrp->self.rstat_cpu, cpu);
66 	if (rstat) {
67 		/* READ_ONCE */
68 		*(volatile long *)rstat;
69 	}
70 
71 	return 0;
72 }
73 char _license[] SEC("license") = "GPL";
74