Move a thread into a cgroup before its start and into another while it runs, and check through bpf_get_current_cgroup_id() in its body that each move took effect by its next iteration. The thread populates the cgroup it is in, which cgroup.events reports, and the cgroup it left is empty again; after the stop the second cgroup empties too, once the thread's exit has left it, which the test waits for since kthread_stop() returns before cgroup_exit() runs. The test's work directory has controllers enabled for its children, so a move into it is refused with -EBUSY, the constraint cgroup.procs enforces; the check needs a domain controller enabled there, since threaded controllers alone leave the parent able to hold processes, and is skipped otherwise. Nonzero flags and a task that is no BPF kthread are refused as by the other control kfuncs. Signed-off-by: Kumar Kartikeya Dwivedi --- .../testing/selftests/bpf/bpf_experimental.h | 2 + .../selftests/bpf/prog_tests/bpf_kthread.c | 88 +++++++++++++++++++ .../testing/selftests/bpf/progs/bpf_kthread.c | 43 ++++++++- 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index e41d326a0017..49d7a45f9707 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -358,6 +358,8 @@ extern struct task_struct *bpf_kthread_create(int (callback_fn)(__u64 arg), __u6 __u64 flags) __weak __ksym; extern int bpf_kthread_start(struct task_struct *task, __u64 flags) __weak __ksym; extern int bpf_kthread_bind(struct task_struct *task, __u32 cpu, __u64 flags) __weak __ksym; +extern int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp, + __u64 flags) __weak __ksym; extern int bpf_kthread_stop(struct task_struct *task, __u64 flags) __weak __ksym; extern bool bpf_kthread_should_stop(void) __weak __ksym; diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c index e32a975c7a0d..fabd6b9e5242 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c @@ -3,6 +3,7 @@ #include #include +#include "cgroup_helpers.h" #include "bpf_kthread.skel.h" #include "verifier_bpf_kthread.skel.h" @@ -13,6 +14,7 @@ struct kthread_args { __u32 body; __u32 bind; __u32 cpu; + __u64 cgroup_id; }; enum body { @@ -67,6 +69,16 @@ static int bind_thread(struct bpf_kthread *skel, __u32 idx, __u32 cpu) return retval; } +static int attach_cgroup(struct bpf_kthread *skel, __u32 idx, __u64 cgroup_id) +{ + struct kthread_args args = { .idx = idx, .cgroup_id = cgroup_id }; + int retval; + + if (!ASSERT_OK(run_prog(skel->progs.attach_cgroup, &args, &retval), "attach_cgroup")) + return -EFAULT; + return retval; +} + /* Returns what the program returned, which for stop_thread is what the stop returned. */ static int control_thread(struct bpf_program *prog, __u32 idx) { @@ -383,6 +395,7 @@ static void test_misuse(void) ASSERT_EQ(retval, 0, "foreign ret"); ASSERT_EQ(skel->bss->foreign_start_ret, -ESRCH, "foreign start ret"); ASSERT_EQ(skel->bss->foreign_stop_ret, -ESRCH, "foreign stop ret"); + ASSERT_EQ(skel->bss->foreign_attach_ret, -ESRCH, "foreign attach ret"); } if (ASSERT_OK(run_prog(skel->progs.misuse, NULL, &retval), "misuse")) ASSERT_EQ(retval, 0, "misuse ret"); @@ -520,6 +533,79 @@ static void test_self_bind(void) bpf_kthread__destroy(skel); } +/* Whether cgroup.events of the cgroup reports it populated, polling until it says @populated. */ +static bool wait_populated(const char *path, bool populated) +{ + const char *want = populated ? "populated 1" : "populated 0"; + char buf[256]; + int i; + + for (i = 0; i < 2000; i++) { + if (!read_cgroup_file(path, "cgroup.events", buf, sizeof(buf)) && strstr(buf, want)) + return true; + usleep(1000); + } + return false; +} + +/* + * A thread moved into a cgroup, parked or running, runs there from its next + * iteration on and populates the cgroup until it exits. The test's work + * directory has controllers enabled for its children, so it cannot take the + * thread itself. + */ +static void test_attach_cgroup(void) +{ + struct bpf_kthread *skel = NULL; + int fd1 = -1, fd2 = -1; + char subtree[256] = {}; + __u64 cg1, cg2; + __u32 loops; + + if (!ASSERT_OK(setup_cgroup_environment(), "setup_cgroup_environment")) + return; + fd1 = create_and_get_cgroup("/kthread1"); + fd2 = create_and_get_cgroup("/kthread2"); + if (!ASSERT_GE(fd1, 0, "create kthread1") || !ASSERT_GE(fd2, 0, "create kthread2")) + goto out; + cg1 = get_cgroup_id("/kthread1"); + cg2 = get_cgroup_id("/kthread2"); + skel = bpf_kthread__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + goto out; + if (create_thread(skel, 0, false, BODY_WORKER)) + goto out; + /* Only a domain controller in the children makes the parent refuse processes. */ + if (!read_cgroup_file("", "cgroup.subtree_control", subtree, sizeof(subtree)) && + (strstr(subtree, "memory") || strstr(subtree, "io"))) + ASSERT_EQ(attach_cgroup(skel, 0, get_cgroup_id("")), -EBUSY, + "attach to inner cgroup"); + ASSERT_EQ(attach_cgroup(skel, 0, cg1), 0, "attach parked ret"); + ASSERT_EQ(start_thread(skel, 0), 0, "start ret"); + if (!ASSERT_TRUE(wait_past(&skel->bss->loops[0], 0), "ran")) + goto out; + ASSERT_EQ(skel->bss->cgids[0], cg1, "cgroup after start"); + ASSERT_TRUE(wait_populated("/kthread1", true), "kthread1 populated"); + + ASSERT_EQ(attach_cgroup(skel, 0, cg2), 0, "attach running ret"); + loops = skel->bss->loops[0]; + ASSERT_GE(wake_thread(skel, 0), 0, "wake ret"); + if (ASSERT_TRUE(wait_past(&skel->bss->loops[0], loops), "looped")) + ASSERT_EQ(skel->bss->cgids[0], cg2, "cgroup after move"); + ASSERT_TRUE(wait_populated("/kthread1", false), "kthread1 empty"); + ASSERT_TRUE(wait_populated("/kthread2", true), "kthread2 populated"); + + ASSERT_EQ(stop_thread(skel, 0), 0, "stop ret"); + ASSERT_TRUE(wait_populated("/kthread2", false), "kthread2 empty"); +out: + bpf_kthread__destroy(skel); + if (fd1 >= 0) + close(fd1); + if (fd2 >= 0) + close(fd2); + cleanup_cgroup_environment(); +} + void test_bpf_kthread(void) { RUN_TESTS(verifier_bpf_kthread); @@ -550,4 +636,6 @@ void test_bpf_kthread(void) test_bind(); if (test__start_subtest("self_bind")) test_self_bind(); + if (test__start_subtest("attach_cgroup")) + test_attach_cgroup(); } diff --git a/tools/testing/selftests/bpf/progs/bpf_kthread.c b/tools/testing/selftests/bpf/progs/bpf_kthread.c index 99ec48339158..3eac7366514f 100644 --- a/tools/testing/selftests/bpf/progs/bpf_kthread.c +++ b/tools/testing/selftests/bpf/progs/bpf_kthread.c @@ -30,6 +30,7 @@ struct kthread_args { __u32 body; /* create: which callback, see enum body */ __u32 bind; /* create: bind the thread to cpu before starting it */ __u32 cpu; /* create and bind: the CPU */ + __u64 cgroup_id; /* attach_cgroup: the cgroup to move the thread into */ }; enum body { @@ -46,11 +47,13 @@ __u32 tids[NR_THREADS]; /* the thread's PID, per slot */ char comms[NR_THREADS][16]; /* the comm the thread read for itself, per slot */ __u32 cpus[NR_THREADS]; /* the CPU the thread last ran on, per slot */ __u32 loops[NR_THREADS]; /* iterations of the body's loop, per slot */ +__u64 cgids[NR_THREADS]; /* the cgroup the thread last ran in, per slot */ __u32 exit_now[NR_THREADS]; /* set by the test: the callback returns idx + 1 */ int self_stop_ret; /* bpf_kthread_stop() on the calling thread */ int spawn_ret; /* bpf_kthread_start() by the spawner */ int foreign_start_ret; /* bpf_kthread_start() on a task that is no BPF kthread */ int foreign_stop_ret; /* bpf_kthread_stop() on the same */ +int foreign_attach_ret; /* bpf_kthread_attach_cgroup() on the same */ __u32 ring_size; /* set by the test: threads in the ring */ __u32 ring_ready; /* ring threads about to stop their successor */ int ring_stop_ret[NR_THREADS]; /* what stopping the successor returned, per slot */ @@ -82,6 +85,7 @@ static int worker(__u64 arg) bpf_repeat(LOOP_BOUND) { cpus[idx] = bpf_get_smp_processor_id(); + cgids[idx] = bpf_get_current_cgroup_id(); __sync_fetch_and_add(&loops[idx], 1); if (bpf_kthread_should_stop()) return 0; @@ -311,6 +315,40 @@ int bind_thread(struct kthread_args *args) return ret; } +/* Move the thread of slot idx into the cgroup args->cgroup_id, running or not. */ +SEC("syscall") +int attach_cgroup(struct kthread_args *args) +{ + struct task_struct *task; + struct cgroup *cgrp; + int ret = -ENOENT; + + task = take(args->idx); + if (!task) + return -ESRCH; + cgrp = bpf_cgroup_from_id(args->cgroup_id); + if (cgrp) { + ret = bpf_kthread_attach_cgroup(task, cgrp, 0); + bpf_cgroup_release(cgrp); + } + publish(args->idx, task); + return ret; +} + +/* bpf_kthread_attach_cgroup() on the root cgroup with @flags. */ +static int attach_root_cgroup(struct task_struct *task, __u64 flags) +{ + struct cgroup *cgrp; + int ret; + + cgrp = bpf_cgroup_from_id(1); + if (!cgrp) + return -ENOENT; + ret = bpf_kthread_attach_cgroup(task, cgrp, flags); + bpf_cgroup_release(cgrp); + return ret; +} + /* Stop the thread of slot idx and empty the slot; returns what the stop returned. */ SEC("syscall") int stop_thread(struct kthread_args *args) @@ -337,6 +375,7 @@ int control_foreign_task(void *ctx) return -ESRCH; foreign_start_ret = bpf_kthread_start(task, 0); foreign_stop_ret = bpf_kthread_stop(task, 0); + foreign_attach_ret = attach_root_cgroup(task, 0); bpf_task_release(task); return 0; } @@ -366,8 +405,10 @@ int misuse(void *ctx) ret = 4; else if (bpf_kthread_stop(task, 1) != -EINVAL) ret = 5; + else if (attach_root_cgroup(task, 1) != -EINVAL) + ret = 6; else - ret = bpf_kthread_stop(task, 0) == -EINTR ? 0 : 6; + ret = bpf_kthread_stop(task, 0) == -EINTR ? 0 : 7; bpf_task_release(task); return ret; } -- 2.53.0