selftests/bpf: Add tests for open-coded task and css iter
This patch adds 4 subtests to demonstrate these patterns and validating
correctness.
subtest1:
1) We use task_iter to iterate all process in the system and search for the
current process with a given pid.
2) We create some threads in current process context, and use
BPF_TASK_ITER_PROC_THREADS to iterate all threads of current process. As
expected, we would find all the threads of current process.
3) We create some threads and use BPF_TASK_ITER_ALL_THREADS to iterate all
threads in the system. As expected, we would find all the threads which was
created.
subtest2:
We create a cgroup and add the current task to the cgroup. In the
BPF program, we would use bpf_for_each(css_task, task, css) to iterate all
tasks under the cgroup. As expected, we would find the current process.
subtest3:
1) We create a cgroup tree. In the BPF program, we use
bpf_for_each(css, pos, root, XXX) to iterate all descendant under the root
with pre and post order. As expected, we would find all descendant and the
last iterating cgroup in post-order is root cgroup, the first iterating
cgroup in pre-order is root cgroup.
2) We wse BPF_CGROUP_ITER_ANCESTORS_UP to traverse the cgroup tree starting
from leaf and root separately, and record the height. The diff of the
hights would be the total tree-high - 1.
subtest4:
Add some failure testcase when using css_task, task and css iters, e.g,
unlock when using task-iters to iterate tasks.
Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com>
Link: https://lore.kernel.org/r/20231018061746.111364-9-zhouchuyi@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-10-18 14:17:46 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright (C) 2023 Chuyi Zhou <zhouchuyi@bytedance.com> */
|
|
|
|
|
|
|
|
#include "vmlinux.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
#include "bpf_misc.h"
|
|
|
|
#include "bpf_experimental.h"
|
|
|
|
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
|
2023-10-31 13:04:37 +08:00
|
|
|
struct cgroup *bpf_cgroup_acquire(struct cgroup *p) __ksym;
|
selftests/bpf: Add tests for open-coded task and css iter
This patch adds 4 subtests to demonstrate these patterns and validating
correctness.
subtest1:
1) We use task_iter to iterate all process in the system and search for the
current process with a given pid.
2) We create some threads in current process context, and use
BPF_TASK_ITER_PROC_THREADS to iterate all threads of current process. As
expected, we would find all the threads of current process.
3) We create some threads and use BPF_TASK_ITER_ALL_THREADS to iterate all
threads in the system. As expected, we would find all the threads which was
created.
subtest2:
We create a cgroup and add the current task to the cgroup. In the
BPF program, we would use bpf_for_each(css_task, task, css) to iterate all
tasks under the cgroup. As expected, we would find the current process.
subtest3:
1) We create a cgroup tree. In the BPF program, we use
bpf_for_each(css, pos, root, XXX) to iterate all descendant under the root
with pre and post order. As expected, we would find all descendant and the
last iterating cgroup in post-order is root cgroup, the first iterating
cgroup in pre-order is root cgroup.
2) We wse BPF_CGROUP_ITER_ANCESTORS_UP to traverse the cgroup tree starting
from leaf and root separately, and record the height. The diff of the
hights would be the total tree-high - 1.
subtest4:
Add some failure testcase when using css_task, task and css iters, e.g,
unlock when using task-iters to iterate tasks.
Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com>
Link: https://lore.kernel.org/r/20231018061746.111364-9-zhouchuyi@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-10-18 14:17:46 +08:00
|
|
|
struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
|
|
|
|
void bpf_cgroup_release(struct cgroup *p) __ksym;
|
|
|
|
|
|
|
|
pid_t target_pid;
|
|
|
|
int css_task_cnt;
|
|
|
|
u64 cg_id;
|
|
|
|
|
|
|
|
SEC("lsm/file_mprotect")
|
|
|
|
int BPF_PROG(iter_css_task_for_each, struct vm_area_struct *vma,
|
|
|
|
unsigned long reqprot, unsigned long prot, int ret)
|
|
|
|
{
|
|
|
|
struct task_struct *cur_task = bpf_get_current_task_btf();
|
|
|
|
struct cgroup_subsys_state *css;
|
|
|
|
struct task_struct *task;
|
|
|
|
struct cgroup *cgrp;
|
|
|
|
|
|
|
|
if (cur_task->pid != target_pid)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
cgrp = bpf_cgroup_from_id(cg_id);
|
|
|
|
|
|
|
|
if (!cgrp)
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
css = &cgrp->self;
|
|
|
|
css_task_cnt = 0;
|
|
|
|
|
|
|
|
bpf_for_each(css_task, task, css, CSS_TASK_ITER_PROCS)
|
|
|
|
if (task->pid == target_pid)
|
|
|
|
css_task_cnt++;
|
|
|
|
|
|
|
|
bpf_cgroup_release(cgrp);
|
|
|
|
|
|
|
|
return -EPERM;
|
|
|
|
}
|
2023-10-31 13:04:37 +08:00
|
|
|
|
|
|
|
static inline u64 cgroup_id(struct cgroup *cgrp)
|
|
|
|
{
|
|
|
|
return cgrp->kn->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("?iter/cgroup")
|
|
|
|
int cgroup_id_printer(struct bpf_iter__cgroup *ctx)
|
|
|
|
{
|
|
|
|
struct seq_file *seq = ctx->meta->seq;
|
2023-11-07 21:22:04 +08:00
|
|
|
struct cgroup *cgrp = ctx->cgroup;
|
2023-10-31 13:04:37 +08:00
|
|
|
struct cgroup_subsys_state *css;
|
|
|
|
struct task_struct *task;
|
|
|
|
|
|
|
|
/* epilogue */
|
|
|
|
if (cgrp == NULL) {
|
|
|
|
BPF_SEQ_PRINTF(seq, "epilogue\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prologue */
|
|
|
|
if (ctx->meta->seq_num == 0)
|
|
|
|
BPF_SEQ_PRINTF(seq, "prologue\n");
|
|
|
|
|
2023-11-07 21:22:04 +08:00
|
|
|
BPF_SEQ_PRINTF(seq, "%8llu\n", cgroup_id(cgrp));
|
2023-10-31 13:04:37 +08:00
|
|
|
|
2023-11-07 21:22:04 +08:00
|
|
|
css = &cgrp->self;
|
2023-10-31 13:04:37 +08:00
|
|
|
css_task_cnt = 0;
|
|
|
|
bpf_for_each(css_task, task, css, CSS_TASK_ITER_PROCS) {
|
|
|
|
if (task->pid == target_pid)
|
|
|
|
css_task_cnt++;
|
|
|
|
}
|
2023-11-07 21:22:04 +08:00
|
|
|
|
2023-10-31 13:04:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2023-10-31 13:04:38 +08:00
|
|
|
|
|
|
|
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
|
|
|
|
int BPF_PROG(iter_css_task_for_each_sleep)
|
|
|
|
{
|
|
|
|
u64 cgrp_id = bpf_get_current_cgroup_id();
|
|
|
|
struct cgroup *cgrp = bpf_cgroup_from_id(cgrp_id);
|
|
|
|
struct cgroup_subsys_state *css;
|
|
|
|
struct task_struct *task;
|
|
|
|
|
|
|
|
if (cgrp == NULL)
|
|
|
|
return 0;
|
|
|
|
css = &cgrp->self;
|
|
|
|
|
|
|
|
bpf_for_each(css_task, task, css, CSS_TASK_ITER_PROCS) {
|
|
|
|
|
|
|
|
}
|
|
|
|
bpf_cgroup_release(cgrp);
|
|
|
|
return 0;
|
|
|
|
}
|