Context |
Check |
Description |
bpf/vmtest-bpf-next-VM_Test-0 |
success
|
Logs for Lint
|
bpf/vmtest-bpf-next-VM_Test-1 |
success
|
Logs for ShellCheck
|
bpf/vmtest-bpf-next-VM_Test-2 |
success
|
Logs for Unittests
|
bpf/vmtest-bpf-next-VM_Test-3 |
success
|
Logs for Validate matrix.py
|
bpf/vmtest-bpf-next-VM_Test-5 |
success
|
Logs for aarch64-gcc / build-release
|
bpf/vmtest-bpf-next-VM_Test-33 |
success
|
Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
|
bpf/vmtest-bpf-next-VM_Test-11 |
success
|
Logs for s390x-gcc / build / build for s390x with gcc
|
bpf/vmtest-bpf-next-VM_Test-34 |
success
|
Logs for x86_64-llvm-17 / veristat
|
bpf/vmtest-bpf-next-VM_Test-17 |
success
|
Logs for s390x-gcc / veristat
|
bpf/vmtest-bpf-next-VM_Test-10 |
success
|
Logs for aarch64-gcc / veristat
|
bpf/vmtest-bpf-next-VM_Test-9 |
success
|
Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-4 |
success
|
Logs for aarch64-gcc / build / build for aarch64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-28 |
success
|
Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
|
bpf/vmtest-bpf-next-VM_Test-35 |
success
|
Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
|
bpf/vmtest-bpf-next-VM_Test-18 |
success
|
Logs for set-matrix
|
bpf/vmtest-bpf-next-VM_Test-12 |
success
|
Logs for s390x-gcc / build-release
|
bpf/vmtest-bpf-next-VM_Test-19 |
success
|
Logs for x86_64-gcc / build / build for x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-23 |
fail
|
Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-20 |
success
|
Logs for x86_64-gcc / build-release
|
bpf/vmtest-bpf-next-VM_Test-30 |
success
|
Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
|
bpf/vmtest-bpf-next-VM_Test-27 |
success
|
Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-26 |
success
|
Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-24 |
success
|
Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-22 |
fail
|
Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-21 |
success
|
Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-25 |
success
|
Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-37 |
success
|
Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
|
bpf/vmtest-bpf-next-VM_Test-41 |
success
|
Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
|
bpf/vmtest-bpf-next-VM_Test-42 |
success
|
Logs for x86_64-llvm-18 / veristat
|
bpf/vmtest-bpf-next-VM_Test-32 |
fail
|
Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
|
bpf/vmtest-bpf-next-VM_Test-31 |
fail
|
Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
|
bpf/vmtest-bpf-next-VM_Test-7 |
fail
|
Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-8 |
fail
|
Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-6 |
success
|
Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
|
bpf/vmtest-bpf-next-VM_Test-13 |
success
|
Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
|
bpf/vmtest-bpf-next-VM_Test-36 |
success
|
Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
|
bpf/vmtest-bpf-next-VM_Test-38 |
fail
|
Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
|
bpf/vmtest-bpf-next-VM_Test-39 |
fail
|
Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
|
bpf/vmtest-bpf-next-VM_Test-40 |
fail
|
Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
|
bpf/vmtest-bpf-next-VM_Test-16 |
fail
|
Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
|
bpf/vmtest-bpf-next-VM_Test-29 |
success
|
Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
|
bpf/vmtest-bpf-next-PR |
fail
|
PR summary
|
bpf/vmtest-bpf-next-VM_Test-14 |
fail
|
Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
|
bpf/vmtest-bpf-next-VM_Test-15 |
fail
|
Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
|
netdev/series_format |
warning
|
Series does not have a cover letter
|
netdev/tree_selection |
success
|
Clearly marked for bpf-next
|
netdev/ynl |
success
|
Generated files up to date;
no warnings/errors;
no diff in generated;
|
netdev/fixes_present |
success
|
Fixes tag not required for -next series
|
netdev/header_inline |
success
|
No static functions without inline keyword in header files
|
netdev/build_32bit |
success
|
Errors and warnings before: 950 this patch: 950
|
netdev/build_tools |
success
|
Errors and warnings before: 0 this patch: 0
|
netdev/cc_maintainers |
success
|
CCed 17 of 17 maintainers
|
netdev/build_clang |
success
|
Errors and warnings before: 956 this patch: 956
|
netdev/verify_signedoff |
success
|
Signed-off-by tag matches author and committer
|
netdev/deprecated_api |
success
|
None detected
|
netdev/check_selftest |
success
|
No net selftest shell script
|
netdev/verify_fixes |
success
|
No Fixes tag
|
netdev/build_allmodconfig_warn |
success
|
Errors and warnings before: 962 this patch: 962
|
netdev/checkpatch |
warning
|
CHECK: Alignment should match open parenthesis
WARNING: Missing a blank line after declarations
WARNING: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
WARNING: line length of 81 exceeds 80 columns
WARNING: line length of 85 exceeds 80 columns
|
netdev/build_clang_rust |
success
|
No Rust files in patch. Skipping build
|
netdev/kdoc |
success
|
Errors and warnings before: 0 this patch: 0
|
netdev/source_inline |
success
|
Was 0 now: 0
|
new file mode 100644
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Isovalent */
+
+#include <sys/syscall.h>
+#include <test_progs.h>
+#include <cgroup_helpers.h>
+#include <unistd.h>
+#include "test_task_freeze_cgroup.skel.h"
+
+#define FOO "/test-task-freeze-cgroup"
+
+static int bpf_sleepable(struct test_task_freeze_cgroup *skel)
+{
+ int err, foo;
+ pid_t pid;
+
+ foo = test__join_cgroup(FOO);
+ if (!ASSERT_OK(foo < 0, "cgroup_join_foo"))
+ return -errno;
+
+ skel = test_task_freeze_cgroup__open();
+ if (!ASSERT_OK_PTR(skel, "test_task_freeze_cgroup__open"))
+ return -errno;
+
+ skel->rodata->parent_pid = getppid();
+ skel->rodata->monitor_pid = getpid();
+ skel->rodata->cgid = get_cgroup_id(FOO);
+ skel->bss->new_pid = getpid();
+ skel->bss->freeze = 1;
+
+ err = test_task_freeze_cgroup__load(skel);
+ if (!ASSERT_OK(err, "test_task_freeze_cgroup__load"))
+ goto cleanup;
+
+ /* First, attach the LSM program, and then it will be triggered when the
+ * TP_BTF program is attached.
+ */
+ skel->links.lsm_freeze_cgroup =
+ bpf_program__attach_lsm(skel->progs.lsm_freeze_cgroup);
+ if (!ASSERT_OK_PTR(skel->links.lsm_freeze_cgroup, "attach_lsm")) {
+ err = -errno;
+ goto cleanup;
+ }
+
+ /* This will fail */
+ skel->links.tp_newchild =
+ bpf_program__attach_trace(skel->progs.tp_newchild);
+ if (!ASSERT_EQ(errno, EPERM, "attach_trace")) {
+ err = -EINVAL;
+ goto cleanup;
+ }
+
+ /* Try again now */
+ skel->links.tp_newchild =
+ bpf_program__attach_trace(skel->progs.tp_newchild);
+ if (!ASSERT_OK_PTR(skel->links.tp_newchild, "attach_trace")) {
+ err = -EINVAL;
+ goto cleanup;
+ }
+
+ /* Trigger a new child and assert unfrozen state */
+ pid = fork();
+ if (pid == 0)
+ exit(0);
+
+ err = (pid == -1);
+ if (ASSERT_OK(err, "fork process"))
+ wait(NULL);
+
+ /* Now we should continue, assert that new_pid reflects child */
+ ASSERT_NEQ(skel->rodata->monitor_pid, skel->bss->new_pid,
+ "test task_freeze_cgroup failed at monitor_pid != new_pid");
+ ASSERT_NEQ(0, skel->bss->new_pid,
+ "test task_freeze_cgroup failed at remote_pid != 0");
+
+ /* Assert that bpf set new_pid to new forked child pid */
+ ASSERT_EQ(pid, skel->bss->new_pid,
+ "test task_freeze_cgroup failed at pid == new_pid");
+
+ test_task_freeze_cgroup__detach(skel);
+
+cleanup:
+ test_task_freeze_cgroup__destroy(skel);
+ close(foo);
+ return err;
+}
+
+void test_task_freeze_cgroup(void)
+{
+ pid_t pid, result;
+ char buf[512] = {0};
+ char path[PATH_MAX] = {0};
+ int ret, status, attempts, frozen = 0;
+ struct test_task_freeze_cgroup *skel = NULL;
+
+ pid = fork();
+ ret = (pid == -1);
+ if (!ASSERT_OK(ret, "fork process"))
+ return;
+
+ if (pid == 0) {
+ ret = bpf_sleepable(skel);
+ ASSERT_EQ(0, ret, "bpf_sleepable failed");
+ exit(ret);
+ }
+
+ skel = test_task_freeze_cgroup__open();
+ if (!ASSERT_OK_PTR(skel, "test_task_freeze_cgroup__open"))
+ goto out;
+
+ snprintf(path, sizeof(path),
+ "/sys/fs/cgroup/cgroup-test-work-dir%d%s/cgroup.freeze",
+ pid, FOO);
+
+ for (attempts = 5; attempts >= 0; attempts--) {
+ ret = 0;
+ int fd = open(path, O_RDONLY);
+ if (fd > 0)
+ ret = read(fd, buf, sizeof(buf) - 1);
+ if (ret > 0) {
+ errno = 0;
+ frozen = strtol(buf, NULL, 10);
+ if (errno)
+ frozen = 0;
+ }
+
+ close(fd);
+ if (frozen)
+ break;
+ sleep(1);
+ }
+
+ /* Assert that child cgroup is frozen */
+ if (!ASSERT_EQ(1, frozen, "child cgroup not frozen"))
+ goto out;
+
+ ret = test_task_freeze_cgroup__load(skel);
+ if (!ASSERT_OK(ret, "test_task_freeze_cgroup__load"))
+ goto out;
+
+ /* Unthaw child cgroup from parent */
+ skel->links.lsm_task_free =
+ bpf_program__attach_lsm(skel->progs.lsm_task_free);
+ if (!ASSERT_OK_PTR(skel->links.lsm_task_free, "attach_lsm"))
+ goto out;
+
+ result = waitpid(pid, &status, WUNTRACED);
+ if (!ASSERT_NEQ(result, -1, "waitpid"))
+ goto detach;
+
+ result = WIFEXITED(status);
+ if (!ASSERT_EQ(result, 1, "forked process did not terminate normally"))
+ goto detach;
+
+ result = WEXITSTATUS(status);
+ if (!ASSERT_EQ(result, 0, "forked process did not exit successfully"))
+ goto detach;
+
+detach:
+ test_task_freeze_cgroup__detach(skel);
+
+out:
+ if (skel)
+ test_task_freeze_cgroup__destroy(skel);
+}
new file mode 100644
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Isovalent */
+
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+#include <errno.h>
+#include "bpf_misc.h"
+
+struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
+long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancestor) __ksym;
+void bpf_cgroup_release(struct cgroup *p) __ksym;
+struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
+struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
+void bpf_task_release(struct task_struct *p) __ksym;
+
+int bpf_task_freeze_cgroup(struct task_struct *task, int freeze) __ksym;
+
+const volatile int parent_pid;
+const volatile int monitor_pid;
+const volatile __u64 cgid;
+int new_pid;
+int freeze;
+
+SEC("tp_btf/task_newtask")
+int BPF_PROG(tp_newchild, struct task_struct *task, u64 clone_flags)
+{
+ struct cgroup *cgrp = NULL;
+ struct task_struct *acquired;
+
+ if (monitor_pid != (bpf_get_current_pid_tgid() >> 32))
+ return 0;
+
+ acquired = bpf_task_acquire(task);
+ if (!acquired)
+ return 0;
+
+ cgrp = bpf_cgroup_from_id(cgid);
+ if (!cgrp)
+ goto out;
+
+ if (bpf_task_under_cgroup(acquired, cgrp))
+ new_pid = acquired->tgid;
+
+out:
+ if (cgrp)
+ bpf_cgroup_release(cgrp);
+ bpf_task_release(acquired);
+
+ return 0;
+}
+
+/* This is attached from parent to trigger the bpf lsm hook, so parent
+ * can unthaw the child.
+ */
+SEC("lsm/task_free")
+int BPF_PROG(lsm_task_free, struct task_struct *task)
+{
+ return 0;
+}
+
+SEC("lsm.s/bpf")
+int BPF_PROG(lsm_freeze_cgroup, int cmd, union bpf_attr *attr, unsigned int size)
+{
+ int ret = 0;
+ struct cgroup *cgrp = NULL;
+ struct task_struct *task;
+
+ if (cmd != BPF_LINK_CREATE)
+ return ret;
+
+ task = bpf_get_current_task_btf();
+ if (parent_pid == task->pid) {
+ /* Unthaw child from parent */
+ task = bpf_task_from_pid(monitor_pid);
+ if (!task)
+ return -ENOENT;
+
+ ret = bpf_task_freeze_cgroup(task, 0);
+ bpf_task_release(task);
+ return ret;
+ }
+
+ if (monitor_pid != task->pid)
+ return 0;
+
+ /* Freeze the child cgroup from its context */
+ cgrp = bpf_cgroup_from_id(cgid);
+ if (!cgrp)
+ goto out;
+
+ if (!bpf_task_under_cgroup(task, cgrp))
+ goto out;
+
+ if (freeze) {
+ /* Schedule freeze task and return -EPERM */
+ ret = bpf_task_freeze_cgroup(task, 1);
+ if (!ret) {
+ ret = -EPERM;
+ /* reset for next call */
+ freeze = 0;
+ }
+ }
+out:
+ if (cgrp)
+ bpf_cgroup_release(cgrp);
+ return ret;
+}
+
+char _license[] SEC("license") = "GPL";
This adds a selftest for `bpf_task_freeze_cgroup` kfunc. The test works by forking a child then: 1. Child: - Migrate to a new cgroup - Loads bpf programs - Trigger the 'lsm_freeze_cgroup' bpf program so it freeze itself. by calling "bpf_task_freeze_cgroup(child, 1)" <- wait for parent to unthaw - On unthaw it continues, forks another process and triggers the 'tp_newchild' bpf program to set some monitored pids of the new process, that are asserted at user space, to ensure that we resumed correctly. 2. Parent: - Keeps reading the 'cgroup.freeze' file of the child cgroup until it prints 1 which means the child cgroup is frozen - Attaches the sample 'lsm_task_free' so it triggers the bpf program and then calls "bpf_task_freeze_cgroup(task, 0);" on the child task to unthaw its cgroup. - Then waits for a clean exit of the child process. The scenario allows to test both: freeze and unthaw a task cgroup. Signed-off-by: Djalal Harouni <tixxdz@gmail.com> --- .../bpf/prog_tests/task_freeze_cgroup.c | 165 ++++++++++++++++++ .../bpf/progs/test_task_freeze_cgroup.c | 110 ++++++++++++ 2 files changed, 275 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_freeze_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_freeze_cgroup.c