selftests/bpf: Add a test case for bpf_cgroup_from_id()
[platform/kernel/linux-starfive.git] / tools / testing / selftests / bpf / progs / cgrp_kfunc_success.c
index 0c23ea3..42e13ae 100644 (file)
@@ -168,3 +168,45 @@ int BPF_PROG(test_cgrp_get_ancestors, struct cgroup *cgrp, const char *path)
 
        return 0;
 }
+
+SEC("tp_btf/cgroup_mkdir")
+int BPF_PROG(test_cgrp_from_id, struct cgroup *cgrp, const char *path)
+{
+       struct cgroup *parent, *res;
+       u64 parent_cgid;
+
+       if (!is_test_kfunc_task())
+               return 0;
+
+       /* @cgrp's ID is not visible yet, let's test with the parent */
+       parent = bpf_cgroup_ancestor(cgrp, cgrp->level - 1);
+       if (!parent) {
+               err = 1;
+               return 0;
+       }
+
+       parent_cgid = parent->kn->id;
+       bpf_cgroup_release(parent);
+
+       res = bpf_cgroup_from_id(parent_cgid);
+       if (!res) {
+               err = 2;
+               return 0;
+       }
+
+       bpf_cgroup_release(res);
+
+       if (res != parent) {
+               err = 3;
+               return 0;
+       }
+
+       res = bpf_cgroup_from_id((u64)-1);
+       if (res) {
+               bpf_cgroup_release(res);
+               err = 4;
+               return 0;
+       }
+
+       return 0;
+}