Skip to content

Commit

Permalink
selftests/bpf: Add a test case for bpf_cgroup_from_id()
Browse files Browse the repository at this point in the history
Add a test case for bpf_cgroup_from_id.

Signed-off-by: Tejun Heo <[email protected]>
Link: https://lore.kernel.org/r/Y/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
htejun authored and Alexei Starovoitov committed Feb 23, 2023
1 parent 332ea1f commit d0093aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/prog_tests/cgrp_kfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
42 changes: 42 additions & 0 deletions tools/testing/selftests/bpf/progs/cgrp_kfunc_success.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit d0093aa

Please sign in to comment.