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);
41 bpf_cgroup_release(acquired);
46 SEC("tp_btf/cgroup_mkdir")
47 int BPF_PROG(test_cgrp_acquire_leave_in_map, struct cgroup *cgrp, const char *path)
51 if (!is_test_kfunc_task())
54 status = cgrps_kfunc_map_insert(cgrp);
61 SEC("tp_btf/cgroup_mkdir")
62 int BPF_PROG(test_cgrp_xchg_release, struct cgroup *cgrp, const char *path)
65 struct __cgrps_kfunc_map_value *v;
68 if (!is_test_kfunc_task())
71 status = cgrps_kfunc_map_insert(cgrp);
77 v = cgrps_kfunc_map_value_lookup(cgrp);
83 kptr = bpf_kptr_xchg(&v->cgrp, NULL);
89 bpf_cgroup_release(kptr);
94 SEC("tp_btf/cgroup_mkdir")
95 int BPF_PROG(test_cgrp_get_release, struct cgroup *cgrp, const char *path)
98 struct __cgrps_kfunc_map_value *v;
101 if (!is_test_kfunc_task())
104 status = cgrps_kfunc_map_insert(cgrp);
110 v = cgrps_kfunc_map_value_lookup(cgrp);
116 kptr = bpf_cgroup_kptr_get(&v->cgrp);
122 bpf_cgroup_release(kptr);
127 SEC("tp_btf/cgroup_mkdir")
128 int BPF_PROG(test_cgrp_get_ancestors, struct cgroup *cgrp, const char *path)
130 struct cgroup *self, *ancestor1, *invalid;
132 if (!is_test_kfunc_task())
135 self = bpf_cgroup_ancestor(cgrp, cgrp->level);
141 if (self->self.id != cgrp->self.id) {
142 bpf_cgroup_release(self);
146 bpf_cgroup_release(self);
148 ancestor1 = bpf_cgroup_ancestor(cgrp, cgrp->level - 1);
153 bpf_cgroup_release(ancestor1);
155 invalid = bpf_cgroup_ancestor(cgrp, 10000);
157 bpf_cgroup_release(invalid);
162 invalid = bpf_cgroup_ancestor(cgrp, -1);
164 bpf_cgroup_release(invalid);
172 SEC("tp_btf/cgroup_mkdir")
173 int BPF_PROG(test_cgrp_from_id, struct cgroup *cgrp, const char *path)
175 struct cgroup *parent, *res;
178 if (!is_test_kfunc_task())
181 /* @cgrp's ID is not visible yet, let's test with the parent */
182 parent = bpf_cgroup_ancestor(cgrp, cgrp->level - 1);
188 parent_cgid = parent->kn->id;
189 bpf_cgroup_release(parent);
191 res = bpf_cgroup_from_id(parent_cgid);
197 bpf_cgroup_release(res);
204 res = bpf_cgroup_from_id((u64)-1);
206 bpf_cgroup_release(res);