[PATCH cgroup/for-7.4-fixes 2/2] selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems
From: Hang Zhengyang
Date: Fri Dec 04 2026 - 08:51:44 EST
Add two tests that create a child with CLONE_INTO_CGROUP in a cpuset
whose mems differ from the parent's: one with a fork-like clone3(), and
one with posix_spawn() and a cgroup attribute, which adds CLONE_VM and
CLONE_VFORK. Both check that the memory policy and the pages of a parent
region bound to the parent's node stay as they are. They need two
memory nodes, and the posix_spawn() test needs glibc 2.39 or later;
they are skipped otherwise.
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@xxxxxxxxx>
---
tools/testing/selftests/cgroup/test_cpuset.c | 201 +++++++++++++++++++
1 file changed, 201 insertions(+)
diff --git a/tools/testing/selftests/cgroup/test_cpuset.c b/tools/testing/selftests/cgroup/test_cpuset.c
index 3dfadd280c1c..9bc9fee37e2e 100644
--- a/tools/testing/selftests/cgroup/test_cpuset.c
+++ b/tools/testing/selftests/cgroup/test_cpuset.c
@@ -2,11 +2,19 @@
#define _GNU_SOURCE
#include <assert.h>
+#include <fcntl.h>
#include <linux/limits.h>
+#include <linux/mempolicy.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
+#include <spawn.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
#include <sys/syscall.h>
+#include <sys/wait.h>
#include <unistd.h>
#include "kselftest.h"
@@ -479,6 +487,197 @@ static int test_cpuset_affinity_on_controller_disable(const char *root)
}
+/* Find the first two memory nodes in a list like "0-1" or "0,2". */
+static int two_memory_nodes(int *n0, int *n1)
+{
+ char buf[256], *p;
+
+ if (read_text("/sys/devices/system/node/has_memory", buf, sizeof(buf)) <= 0)
+ return -1;
+ *n0 = strtol(buf, &p, 10);
+ if (p == buf || (*p != '-' && *p != ','))
+ return -1;
+ if (*p == '-') {
+ *n1 = *n0 + 1;
+ return 0;
+ }
+ *n1 = strtol(p + 1, NULL, 10);
+ return 0;
+}
+
+static int page_node(void *addr)
+{
+ int node = -1;
+
+ if (syscall(__NR_get_mempolicy, &node, NULL, 0, addr,
+ MPOL_F_NODE | MPOL_F_ADDR))
+ return -1;
+ return node;
+}
+
+#define MAX_NODES 1024 /* get_mempolicy() wants room for all nodes */
+
+/* Get the policy of the region at @addr: its mode and its first nodes */
+static int region_policy(void *addr, int *mode, unsigned long *nodes)
+{
+ unsigned long mask[MAX_NODES / (8 * sizeof(unsigned long))] = { 0 };
+ size_t i;
+
+ if (syscall(__NR_get_mempolicy, mode, mask, MAX_NODES, addr,
+ MPOL_F_ADDR))
+ return -1;
+ for (i = 1; i < ARRAY_SIZE(mask); i++)
+ if (mask[i])
+ return -1;
+ *nodes = mask[0];
+ return 0;
+}
+
+/*
+ * Create a child in the cgroup of @cgfd, as fork() or as posix_spawn() does.
+ * Return its pid, or 0 if it is gone already.
+ */
+static pid_t start_child_in_cgroup(int cgfd, bool spawn)
+{
+ pid_t pid;
+
+ if (spawn) {
+#ifdef POSIX_SPAWN_SETCGROUP
+ static char *const argv[] = { "true", NULL };
+ posix_spawnattr_t attr;
+ int err;
+
+ posix_spawnattr_init(&attr);
+ posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETCGROUP);
+ posix_spawnattr_setcgroup_np(&attr, cgfd);
+ err = posix_spawnp(&pid, "true", NULL, &attr, argv, environ);
+ posix_spawnattr_destroy(&attr);
+ /* Without true(1), the child was created and failed to exec */
+ if (err == ENOENT)
+ return 0;
+ if (err) {
+ errno = err;
+ return -1;
+ }
+ return pid;
+#else
+ errno = EOPNOTSUPP;
+ return -1;
+#endif
+ }
+
+ pid = clone_into_cgroup(cgfd);
+ if (pid == 0)
+ _exit(0);
+ return pid;
+}
+
+/*
+ * A child created with CLONE_INTO_CGROUP in a cpuset with other memory nodes
+ * must leave the memory of its parent alone, both after a fork, where the two
+ * share pages, and after a posix_spawn() with a cgroup attribute, which uses
+ * CLONE_VM | CLONE_VFORK, where they share the mm.
+ */
+static int clone_into_other_mems(const char *root, bool spawn)
+{
+ char *parent = NULL, *child_a = NULL, *child_b = NULL;
+ size_t i, size = 4 << 20, step = sysconf(_SC_PAGESIZE);
+ char *mem = MAP_FAILED, nodes_a[16], nodes_b[16];
+ int ret = KSFT_FAIL, cgfd = -1, mode, n0, n1;
+ unsigned long nodes, bound;
+ pid_t pid;
+
+ if (two_memory_nodes(&n0, &n1) || n1 >= 8 * sizeof(bound))
+ return KSFT_SKIP;
+
+ parent = cg_name(root, "cpuset_clone_test");
+ child_a = cg_name(parent, "A");
+ child_b = cg_name(parent, "B");
+ if (!parent || !child_a || !child_b)
+ goto cleanup;
+ if (cg_create(parent) || cg_write(parent, "cgroup.subtree_control", "+cpuset"))
+ goto cleanup;
+ if (cg_create(child_a) || cg_create(child_b))
+ goto cleanup;
+ snprintf(nodes_a, sizeof(nodes_a), "%d", n0);
+ snprintf(nodes_b, sizeof(nodes_b), "%d", n1);
+ if (cg_write(child_a, "cpuset.mems", nodes_a) ||
+ cg_write(child_b, "cpuset.mems", nodes_b))
+ goto cleanup;
+ if (cg_enter_current(child_a))
+ goto cleanup;
+
+ /* Bind the memory to n0 and fault it in there */
+ mem = mmap(NULL, size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (mem == MAP_FAILED)
+ goto cleanup;
+ bound = 1UL << n0;
+ if (syscall(__NR_mbind, mem, size, MPOL_BIND, &bound,
+ 8 * sizeof(bound), 0))
+ goto cleanup;
+ memset(mem, 1, size);
+ if (page_node(mem) != n0) {
+ ret = KSFT_SKIP;
+ goto cleanup;
+ }
+
+ cgfd = open(child_b, O_DIRECTORY | O_RDONLY);
+ if (cgfd < 0)
+ goto cleanup;
+ pid = start_child_in_cgroup(cgfd, spawn);
+ if (pid < 0) {
+ if (errno == ENOSYS || errno == EOPNOTSUPP)
+ ret = KSFT_SKIP;
+ goto cleanup;
+ }
+ if (pid > 0)
+ waitpid(pid, NULL, 0);
+
+ if (region_policy(mem, &mode, &nodes))
+ goto cleanup;
+ if (mode != MPOL_BIND || nodes != bound) {
+ ksft_print_msg("FAIL: parent memory policy changed to mode %d nodes %#lx\n",
+ mode, nodes);
+ goto cleanup;
+ }
+ /* A write to cpuset.mems waits for queued migrations to finish */
+ if (cg_write(child_b, "cpuset.mems", nodes_b))
+ goto cleanup;
+ for (i = 0; i < size; i += step) {
+ if (page_node(mem + i) != n0) {
+ ksft_print_msg("FAIL: parent memory moved to node %d\n",
+ page_node(mem + i));
+ goto cleanup;
+ }
+ }
+ ret = KSFT_PASS;
+
+cleanup:
+ cg_enter_current(root);
+ if (cgfd >= 0)
+ close(cgfd);
+ if (mem != MAP_FAILED)
+ munmap(mem, size);
+ cg_destroy(child_b);
+ free(child_b);
+ cg_destroy(child_a);
+ free(child_a);
+ cg_destroy(parent);
+ free(parent);
+ return ret;
+}
+
+static int test_cpuset_fork_into_other_mems(const char *root)
+{
+ return clone_into_other_mems(root, false);
+}
+
+static int test_cpuset_spawn_into_other_mems(const char *root)
+{
+ return clone_into_other_mems(root, true);
+}
+
#define T(x) { x, #x }
struct cpuset_test {
int (*fn)(const char *root);
@@ -488,6 +687,8 @@ struct cpuset_test {
T(test_cpuset_perms_object_deny),
T(test_cpuset_perms_subtree),
T(test_cpuset_affinity_on_controller_disable),
+ T(test_cpuset_fork_into_other_mems),
+ T(test_cpuset_spawn_into_other_mems),
};
#undef T
--
2.53.0