1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
5 #include <bpf/bpf_tracing.h>
6 #include <bpf/bpf_helpers.h>
8 #include "cgrp_kfunc_common.h"
10 char _license[] SEC("license") = "GPL";
12 int err, pid, invocations;
14 /* Prototype for all of the program trace events below:
16 * TRACE_EVENT(cgroup_mkdir,
17 * TP_PROTO(struct cgroup *cgrp, const char *path),
21 static bool is_test_kfunc_task(void)
23 int cur_pid = bpf_get_current_pid_tgid() >> 32;
24 bool same = pid == cur_pid;
27 __sync_fetch_and_add(&invocations, 1);
32 SEC("tp_btf/cgroup_mkdir")
33 int BPF_PROG(test_cgrp_acquire_release_argument, struct cgroup *cgrp, const char *path)
35 struct cgroup *acquired;
37 if (!is_test_kfunc_task())
40 acquired = bpf_cgroup_acquire(cgrp);
44 bpf_cgroup_release(acquired);
49 SEC("tp_btf/cgroup_mkdir")
50 int BPF_PROG(test_cgrp_acquire_leave_in_map, struct cgroup *cgrp, const char *path)
54 if (!is_test_kfunc_task())
57 status = cgrps_kfunc_map_insert(cgrp);
64 SEC("tp_btf/cgroup_mkdir")
65 int BPF_PROG(test_cgrp_xchg_release, struct cgroup *cgrp, const char *path)
67 struct cgroup *kptr, *cg;
68 struct __cgrps_kfunc_map_value *v;
71 if (!is_test_kfunc_task())
74 status = cgrps_kfunc_map_insert(cgrp);
80 v = cgrps_kfunc_map_value_lookup(cgrp);
92 cg = bpf_cgroup_ancestor(kptr, 1);
93 if (cg) /* verifier only check */
94 bpf_cgroup_release(cg);
96 kptr = bpf_kptr_xchg(&v->cgrp, NULL);
102 bpf_cgroup_release(kptr);
107 SEC("tp_btf/cgroup_mkdir")
108 int BPF_PROG(test_cgrp_get_release, struct cgroup *cgrp, const char *path)
111 struct __cgrps_kfunc_map_value *v;
114 if (!is_test_kfunc_task())
117 status = cgrps_kfunc_map_insert(cgrp);
123 v = cgrps_kfunc_map_value_lookup(cgrp);
133 bpf_rcu_read_unlock();
138 SEC("tp_btf/cgroup_mkdir")
139 int BPF_PROG(test_cgrp_get_ancestors, struct cgroup *cgrp, const char *path)
141 struct cgroup *self, *ancestor1, *invalid;
143 if (!is_test_kfunc_task())
146 self = bpf_cgroup_ancestor(cgrp, cgrp->level);
152 if (self->self.id != cgrp->self.id) {
153 bpf_cgroup_release(self);
157 bpf_cgroup_release(self);
159 ancestor1 = bpf_cgroup_ancestor(cgrp, cgrp->level - 1);
164 bpf_cgroup_release(ancestor1);
166 invalid = bpf_cgroup_ancestor(cgrp, 10000);
168 bpf_cgroup_release(invalid);
173 invalid = bpf_cgroup_ancestor(cgrp, -1);
175 bpf_cgroup_release(invalid);
183 SEC("tp_btf/cgroup_mkdir")
184 int BPF_PROG(test_cgrp_from_id, struct cgroup *cgrp, const char *path)
186 struct cgroup *parent, *res;
189 if (!is_test_kfunc_task())
192 /* @cgrp's ID is not visible yet, let's test with the parent */
193 parent = bpf_cgroup_ancestor(cgrp, cgrp->level - 1);
199 parent_cgid = parent->kn->id;
200 bpf_cgroup_release(parent);
202 res = bpf_cgroup_from_id(parent_cgid);
208 bpf_cgroup_release(res);
215 res = bpf_cgroup_from_id((u64)-1);
217 bpf_cgroup_release(res);