[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <b3c5fd4b-8fb6-4308-81dd-9355a675e5c7@bytedance.com>
Date: Thu, 12 Oct 2023 10:38:51 +0800
From: Chuyi Zhou <zhouchuyi@...edance.com>
To: bpf@...r.kernel.org
Cc: ast@...nel.org, daniel@...earbox.net, andrii@...nel.org,
martin.lau@...nel.org, tj@...nel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH bpf-next v5 8/8] selftests/bpf: Add tests for open-coded
task and css iter
在 2023/10/11 20:08, Chuyi Zhou 写道:
> This patch adds three 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.
>
> Signed-off-by: Chuyi Zhou <zhouchuyi@...edance.com>
> ---
> .../testing/selftests/bpf/prog_tests/iters.c | 161 ++++++++++++++++++
> tools/testing/selftests/bpf/progs/iters_css.c | 74 ++++++++
> .../selftests/bpf/progs/iters_css_task.c | 42 +++++
> .../testing/selftests/bpf/progs/iters_task.c | 41 +++++
> .../selftests/bpf/progs/iters_task_failure.c | 105 ++++++++++++
> 5 files changed, 423 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/progs/iters_css.c
> create mode 100644 tools/testing/selftests/bpf/progs/iters_css_task.c
> create mode 100644 tools/testing/selftests/bpf/progs/iters_task.c
> create mode 100644 tools/testing/selftests/bpf/progs/iters_task_failure.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c
> index 10804ae5ae97..8d7a7bef5c73 100644
> --- a/tools/testing/selftests/bpf/prog_tests/iters.c
> +++ b/tools/testing/selftests/bpf/prog_tests/iters.c
> @@ -1,13 +1,24 @@
> // SPDX-License-Identifier: GPL-2.0
> /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
>
> +#include <sys/syscall.h>
> +#include <sys/mman.h>
> +#include <sys/wait.h>
> +#include <unistd.h>
> +#include <malloc.h>
> +#include <stdlib.h>
> #include <test_progs.h>
> +#include "cgroup_helpers.h"
>
> #include "iters.skel.h"
> #include "iters_state_safety.skel.h"
> #include "iters_looping.skel.h"
> #include "iters_num.skel.h"
> #include "iters_testmod_seq.skel.h"
> +#include "iters_task.skel.h"
> +#include "iters_css_task.skel.h"
> +#include "iters_css.skel.h"
> +#include "iters_task_failure.skel.h"
>
> static void subtest_num_iters(void)
> {
> @@ -90,6 +101,149 @@ static void subtest_testmod_seq_iters(void)
> iters_testmod_seq__destroy(skel);
> }
>
> +static pthread_mutex_t do_nothing_mutex;
> +
> +static void *do_nothing_wait(void *arg)
> +{
> + pthread_mutex_lock(&do_nothing_mutex);
> + pthread_mutex_unlock(&do_nothing_mutex);
> +
> + pthread_exit(arg);
> +}
> +
> +#define thread_num 2
> +
> +static void subtest_task_iters(void)
> +{
> + struct iters_task *skel = NULL;
> + pthread_t thread_ids[thread_num];
> + void *ret;
> + int err;
> +
> + skel = iters_task__open();
> + if (!ASSERT_OK_PTR(skel, "skel_open"))
> + goto cleanup;
> + err = iters_task__load(skel);
> + if (!ASSERT_OK(err, "skel_load"))
> + goto cleanup;
> + skel->bss->target_pid = getpid();
> + err = iters_task__attach(skel);
> + if (!ASSERT_OK(err, "iters_task__attach"))
> + goto cleanup;
> + pthread_mutex_lock(&do_nothing_mutex);
> + for (int i = 0; i < thread_num; i++)
> + ASSERT_OK(pthread_create(&thread_ids[i], NULL, &do_nothing_wait, NULL),
> + "pthread_create");
> +
> + syscall(SYS_getpgid);
> + iters_task__detach(skel);
> + ASSERT_EQ(skel->bss->process_cnt, 1, "process_cnt");
> + ASSERT_EQ(skel->bss->thread_cnt, thread_num + 1, "thread_cnt");
> + ASSERT_EQ(skel->bss->all_thread_cnt, thread_num + 1, "all_thread_cnt");
> + pthread_mutex_unlock(&do_nothing_mutex);
> + for (int i = 0; i < thread_num; i++)
> + pthread_join(thread_ids[i], &ret);
> +cleanup:
> + iters_task__destroy(skel);
> +}
> +
> +extern int stack_mprotect(void);
> +
> +static void subtest_css_task_iters(void)
> +{
> + struct iters_css_task *skel = NULL;
> + int err, cg_fd, cg_id;
> + const char *cgrp_path = "/cg1";
> +
> + err = setup_cgroup_environment();
> + if (!ASSERT_OK(err, "setup_cgroup_environment"))
> + goto cleanup;
> + cg_fd = create_and_get_cgroup(cgrp_path);
> + if (!ASSERT_GE(cg_fd, 0, "cg_create"))
> + goto cleanup;
> + cg_id = get_cgroup_id(cgrp_path);
> + err = join_cgroup(cgrp_path);
> + if (!ASSERT_OK(err, "setup_cgroup_environment"))
> + goto cleanup;
> +
> + skel = iters_css_task__open();
> + if (!ASSERT_OK_PTR(skel, "skel_open"))
> + goto cleanup;
> +
> + err = iters_css_task__load(skel);
> + if (!ASSERT_OK(err, "skel_load"))
> + goto cleanup;
> +
> + skel->bss->target_pid = getpid();
> + skel->bss->cg_id = cg_id;
> + err = iters_css_task__attach(skel);
> +
> + err = stack_mprotect();
> + if (!ASSERT_OK(err, "iters_task__attach"))
> + goto cleanup;
The is incorrect and would fail the lsm_test in prog_test.
Here we should check the stack_mprotect return value is -1 or
-EPERM. In BPF Prog iter_css_task_for_each, we need to return -EPERM.
The whole logic should keep same with lsm_test.c/bpf_cookie.c
After the following fix, CI would works well.
(https://github.com/kernel-patches/bpf/actions/runs/6484774470/job/17609349165?pr=5808)
@@ -177,11 +177,12 @@ static void subtest_css_task_iters(void)
skel->bss->target_pid = getpid();
skel->bss->cg_id = cg_id;
err = iters_css_task__attach(skel);
-
- err = stack_mprotect();
if (!ASSERT_OK(err, "iters_task__attach"))
goto cleanup;
-
+ err = stack_mprotect();
+ if (!ASSERT_EQ(err, -1, "stack_mprotect") ||
+ !ASSERT_EQ(errno, EPERM, "stack_mprotect"))
+ goto cleanup;
iters_css_task__detach(skel);
ASSERT_EQ(skel->bss->css_task_cnt, 1, "css_task_cnt");
diff --git a/tools/testing/selftests/bpf/progs/iters_css_task.c
b/tools/testing/selftests/bpf/progs/iters_css_task.c
index 506a2755234e..9f79a57fde8e 100644
--- a/tools/testing/selftests/bpf/progs/iters_css_task.c
+++ b/tools/testing/selftests/bpf/progs/iters_css_task.c
@@ -2,6 +2,7 @@
/* Copyright (C) 2023 Chuyi Zhou <zhouchuyi@...edance.com> */
#include "vmlinux.h"
+#include <errno.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
@@ -17,7 +18,8 @@ int css_task_cnt = 0;
u64 cg_id;
SEC("lsm/file_mprotect")
-int BPF_PROG(iter_css_task_for_each)
+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;
@@ -25,12 +27,12 @@ int BPF_PROG(iter_css_task_for_each)
struct cgroup *cgrp;
if (cur_task->pid != target_pid)
- return 0;
+ return ret;
cgrp = bpf_cgroup_from_id(cg_id);
- if (cgrp == NULL)
- return 0;
+ if (!cgrp)
+ goto out;
css = &cgrp->self;
bpf_for_each(css_task, task, css, CSS_TASK_ITER_PROCS)
@@ -38,5 +40,6 @@ int BPF_PROG(iter_css_task_for_each)
css_task_cnt += 1;
bpf_cgroup_release(cgrp);
- return 0;
+out:
+ return -EPERM;
}
Powered by blists - more mailing lists