selftests/bpf: Add a test case for bpf_cgroup_from_id()

Add a test case for bpf_cgroup_from_id.

Signed-off-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/Y/bBlt+tPozcQgws@slm.duckdns.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Tejun Heo 2023-02-22 15:29:58 -10:00 committed by Alexei Starovoitov
parent 332ea1f697
commit d0093aaefa
3 changed files with 44 additions and 0 deletions

View file

@ -84,6 +84,7 @@ static const char * const success_tests[] = {
"test_cgrp_xchg_release",
"test_cgrp_get_release",
"test_cgrp_get_ancestors",
"test_cgrp_from_id",
};
void test_cgrp_kfunc(void)

View file

@ -24,6 +24,7 @@ struct cgroup *bpf_cgroup_acquire(struct cgroup *p) __ksym;
struct cgroup *bpf_cgroup_kptr_get(struct cgroup **pp) __ksym;
void bpf_cgroup_release(struct cgroup *p) __ksym;
struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym;
struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
static inline struct __cgrps_kfunc_map_value *cgrps_kfunc_map_value_lookup(struct cgroup *cgrp)
{

View 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;
}