From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
To: Waiman Long <longman@redhat.com>,
Ridong Chen <ridong.chen@linux.dev>, Tejun Heo <tj@kernel.org>
Cc: "Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Shuah Khan" <shuah@kernel.org>,
"Christian Brauner" <brauner@kernel.org>,
"Matthew Ruffell" <matthew.ruffell@canonical.com>,
"Caron Wills" <caron.wills@pulsar.com>,
cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH cgroup/for-7.4-fixes 2/2] selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems
Date: Fri, 4 Dec 2026 13:51:26 +0000 [thread overview]
Message-ID: <20261204135126.598380-3-hang.zhengyang1010@gmail.com> (raw)
In-Reply-To: <20261204135126.598380-1-hang.zhengyang1010@gmail.com>
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@gmail.com>
---
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
prev parent reply other threads:[~2026-12-04 13:51 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 13:51 [PATCH cgroup/for-7.4-fixes 0/2] cgroup/cpuset: Don't migrate the parent's memory on CLONE_INTO_CGROUP Hang Zhengyang
2026-12-04 13:51 ` [PATCH cgroup/for-7.4-fixes 1/2] " Hang Zhengyang
2026-12-04 13:51 ` Hang Zhengyang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204135126.598380-3-hang.zhengyang1010@gmail.com \
--to=hang.zhengyang1010@gmail.com \
--cc=brauner@kernel.org \
--cc=caron.wills@pulsar.com \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=longman@redhat.com \
--cc=matthew.ruffell@canonical.com \
--cc=mkoutny@suse.com \
--cc=ridong.chen@linux.dev \
--cc=shuah@kernel.org \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®