Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add selftests for bpf_cgroup_ancestor() kfunc

bpf_cgroup_ancestor() allows BPF programs to access the ancestor of a
struct cgroup *. This patch adds selftests that validate its expected
behavior.

Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20221122055458.173143-5-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

David Vernet and committed by
Alexei Starovoitov
227a89cf 5ca78670

+47
+1
tools/testing/selftests/bpf/prog_tests/cgrp_kfunc.c
··· 86 86 "test_cgrp_acquire_leave_in_map", 87 87 "test_cgrp_xchg_release", 88 88 "test_cgrp_get_release", 89 + "test_cgrp_get_ancestors", 89 90 }; 90 91 91 92 static struct {
+1
tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h
··· 23 23 struct cgroup *bpf_cgroup_acquire(struct cgroup *p) __ksym; 24 24 struct cgroup *bpf_cgroup_kptr_get(struct cgroup **pp) __ksym; 25 25 void bpf_cgroup_release(struct cgroup *p) __ksym; 26 + struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym; 26 27 27 28 static inline struct __cgrps_kfunc_map_value *cgrps_kfunc_map_value_lookup(struct cgroup *cgrp) 28 29 {
+45
tools/testing/selftests/bpf/progs/cgrp_kfunc_success.c
··· 123 123 124 124 return 0; 125 125 } 126 + 127 + SEC("tp_btf/cgroup_mkdir") 128 + int BPF_PROG(test_cgrp_get_ancestors, struct cgroup *cgrp, const char *path) 129 + { 130 + struct cgroup *self, *ancestor1, *invalid; 131 + 132 + if (!is_test_kfunc_task()) 133 + return 0; 134 + 135 + self = bpf_cgroup_ancestor(cgrp, cgrp->level); 136 + if (!self) { 137 + err = 1; 138 + return 0; 139 + } 140 + 141 + if (self->self.id != cgrp->self.id) { 142 + bpf_cgroup_release(self); 143 + err = 2; 144 + return 0; 145 + } 146 + bpf_cgroup_release(self); 147 + 148 + ancestor1 = bpf_cgroup_ancestor(cgrp, cgrp->level - 1); 149 + if (!ancestor1) { 150 + err = 3; 151 + return 0; 152 + } 153 + bpf_cgroup_release(ancestor1); 154 + 155 + invalid = bpf_cgroup_ancestor(cgrp, 10000); 156 + if (invalid) { 157 + bpf_cgroup_release(invalid); 158 + err = 4; 159 + return 0; 160 + } 161 + 162 + invalid = bpf_cgroup_ancestor(cgrp, -1); 163 + if (invalid) { 164 + bpf_cgroup_release(invalid); 165 + err = 5; 166 + return 0; 167 + } 168 + 169 + return 0; 170 + }