cgroup.procs keeps its task iterator between reads. A failed migration used to make the iterator skip the last process of a css_set. Read one byte of cgroup.procs, so that the iterator waits on the last of three processes. Then fail a migration of that process, and check that the rest of the read still lists it. cpuset makes the migration fail: a partition with one CPU gives that CPU to a child partition, which leaves its other child without CPUs. The test is skipped if this can't be set up. The test fails without "cgroup: don't lose a task in css_task_iter when a migration fails". Signed-off-by: Shakeel Butt --- tools/testing/selftests/cgroup/test_core.c | 154 +++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_core.c b/tools/testing/selftests/cgroup/test_core.c index 53592bf309fa..01c268672d6d 100644 --- a/tools/testing/selftests/cgroup/test_core.c +++ b/tools/testing/selftests/cgroup/test_core.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -739,6 +740,158 @@ static int test_cgcore_thread_migration(const char *root) return ret; } +/* Return the highest CPU number in a cpu list like "0-3,8-11", or -1. */ +static int cpulist_last(const char *list) +{ + const char *p = list + strlen(list); + + while (p > list && !isdigit(p[-1])) + p--; + while (p > list && isdigit(p[-1])) + p--; + return isdigit(*p) ? atoi(p) : -1; +} + +/* Is @pid on a line of its own in @buf? */ +static bool pid_listed(const char *buf, pid_t pid) +{ + const char *p = buf; + char *end; + + while (*p) { + if (strtol(p, &end, 10) == pid && end != p && *end == '\n') + return true; + p = strchr(p, '\n'); + if (!p) + break; + p++; + } + return false; +} + +/* + * A migration that fails must not make a cgroup.procs reader miss a process. + * + * cgroup.procs keeps its task iterator between reads. Read one byte, so that + * the iterator waits on the last process, then fail a migration of that + * process: cpuset refuses tasks for a cgroup without CPUs. The process never + * left the cgroup, so the rest of the read must list it. + */ +static int test_cgcore_procs_failed_migration(const char *root) +{ + char *parent = NULL, *full = NULL, *empty = NULL, *cg = NULL; + char cpus[256], cpu[16], buf[4096]; + bool cpuset_enabled = false; + pid_t pids[3] = { 0 }; + int ret = KSFT_FAIL; + int fd = -1, i, last; + ssize_t len, n; + + if (cg_test_v1_named) + return KSFT_SKIP; + + parent = cg_name(root, "cg_test_fail"); + full = cg_name(root, "cg_test_fail/full"); + empty = cg_name(root, "cg_test_fail/empty"); + cg = cg_name(root, "cg_test_src"); + if (!parent || !full || !empty || !cg) + goto cleanup; + + /* + * Make "empty" a cgroup without CPUs: "parent" is a partition with one + * CPU, and its child partition "full" takes that CPU. + */ + ret = KSFT_SKIP; + if (cg_read_strstr(root, "cgroup.subtree_control", "cpuset")) { + if (cg_write(root, "cgroup.subtree_control", "+cpuset")) + goto cleanup; + cpuset_enabled = true; + } + if (cg_read(root, "cpuset.cpus.effective", cpus, sizeof(cpus))) + goto cleanup; + last = cpulist_last(cpus); + if (last <= 0) + goto cleanup; + snprintf(cpu, sizeof(cpu), "%d", last); + + if (cg_create(parent) || cg_write(parent, "cpuset.cpus", cpu) || + cg_write(parent, "cpuset.cpus.partition", "root") || + cg_read_strcmp(parent, "cpuset.cpus.partition", "root\n") || + cg_write(parent, "cgroup.subtree_control", "+cpuset") || + cg_create(full) || cg_write(full, "cpuset.cpus", cpu) || + cg_write(full, "cpuset.cpus.partition", "root") || + cg_read_strcmp(full, "cpuset.cpus.partition", "root\n") || + cg_create(empty) || + cg_read_strcmp(empty, "cpuset.cpus.effective", "\n")) + goto cleanup; + + ret = KSFT_FAIL; + if (cg_create(cg)) + goto cleanup; + + /* Move the processes in one by one, so that they are listed in order. */ + for (i = 0; i < 3; i++) { + pids[i] = fork(); + if (pids[i] < 0) + goto cleanup; + if (!pids[i]) { + while (1) + pause(); + } + if (cg_enter(cg, pids[i])) + goto cleanup; + } + + /* Without a failing migration there is nothing to test. */ + if (cg_enter(empty, pids[2]) != -ENOSPC) { + ret = KSFT_SKIP; + goto cleanup; + } + + fd = cg_open(cg, "cgroup.procs", O_RDONLY); + if (fd < 0) + goto cleanup; + if (read(fd, buf, 1) != 1) + goto cleanup; + len = 1; + + if (cg_enter(empty, pids[2]) != -ENOSPC) + goto cleanup; + + while ((n = read(fd, buf + len, sizeof(buf) - 1 - len)) > 0) + len += n; + if (n < 0) + goto cleanup; + buf[len] = '\0'; + + for (i = 0; i < 3; i++) + if (!pid_listed(buf, pids[i])) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + if (fd >= 0) + close(fd); + for (i = 0; i < 3; i++) { + if (pids[i] > 0) { + kill(pids[i], SIGKILL); + waitpid(pids[i], NULL, 0); + } + } + cg_destroy(cg); + cg_destroy(empty); + cg_destroy(full); + cg_destroy(parent); + if (cpuset_enabled) + cg_write(root, "cgroup.subtree_control", "-cpuset"); + free(cg); + free(empty); + free(full); + free(parent); + return ret; +} + /* * cgroup migration permission check should be performed based on the * credentials at the time of open instead of write. @@ -941,6 +1094,7 @@ struct corecg_test { T(test_cgcore_populated), T(test_cgcore_proc_migration), T(test_cgcore_thread_migration), + T(test_cgcore_procs_failed_migration), T(test_cgcore_destroy), T(test_cgcore_lesser_euid_open), T(test_cgcore_lesser_ns_open), -- 2.53.1-Meta