mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH cgroup/for-7.4-fixes 0/2] cgroup/cpuset: Don't migrate the parent's memory on CLONE_INTO_CGROUP
@ 2026-12-04 13:51 Hang Zhengyang
  2026-12-04 13:51 ` [PATCH cgroup/for-7.4-fixes 1/2] " Hang Zhengyang
  2026-12-04 13:51 ` [PATCH cgroup/for-7.4-fixes 2/2] selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems Hang Zhengyang
  0 siblings, 2 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-04 13:51 UTC (permalink / raw)
  To: Waiman Long, Ridong Chen, Tejun Heo
  Cc: Johannes Weiner, Michal Koutný,
	Shuah Khan, Christian Brauner, Matthew Ruffell, Caron Wills,
	cgroups, linux-kselftest, linux-kernel

Since v7.3, a child created with CLONE_INTO_CGROUP in a cpuset with
other mems has its mm migrated at fork. This moves the parent's memory:
the pages the two share, and with CLONE_VM, as posix_spawn() uses, also
the policies of the parent's mappings. Patch 1 stops the migration at
fork and leaves a shared mm alone. Patch 2 adds tests.

Caron Wills's "cgroup/cpuset: Don't make a forking task wait for memory
migration" [1], posted by Matthew Ruffell, removes the wait for this
migration, and Tejun asked for it to be split. With patch 1 there is
nothing to wait for at fork, so only its change for cpuset_attach() is
still needed. That part applies on top of patch 1; the cpuset_fork()
part conflicts.

Tested on cgroup/for-7.4-fixes in a KVM guest with two NUMA nodes: the
new tests fail without patch 1, also with only [1] applied, and pass
with patch 1. test_core, test_cpuset_prs.sh and the other test_cpuset
tests pass.

I used Claude Code to find the issue, write the patches and run the
tests. I reviewed the patches.

[1] https://lore.kernel.org/all/20261103075940.110308-7-matthew.ruffell@canonical.com/

Hang Zhengyang (2):
  cgroup/cpuset: Don't migrate the parent's memory on CLONE_INTO_CGROUP
  selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems

 kernel/cgroup/cpuset.c                       |  23 ++-
 tools/testing/selftests/cgroup/test_cpuset.c | 201 +++++++++++++++++++
 2 files changed, 220 insertions(+), 4 deletions(-)


base-commit: f7d2a428108d90a4e3fdd2c4d7e4660e780855ff
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH cgroup/for-7.4-fixes 1/2] cgroup/cpuset: Don't migrate the parent's memory on CLONE_INTO_CGROUP
  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 ` Hang Zhengyang
  2026-12-04 13:51 ` [PATCH cgroup/for-7.4-fixes 2/2] selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems Hang Zhengyang
  1 sibling, 0 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-04 13:51 UTC (permalink / raw)
  To: Waiman Long, Ridong Chen, Tejun Heo
  Cc: Johannes Weiner, Michal Koutný,
	Shuah Khan, Christian Brauner, Matthew Ruffell, Caron Wills,
	cgroups, linux-kselftest, linux-kernel, stable

Since commit 65e510cd30d0 ("cgroup/cpuset: Move
mpol_rebind_mm/cpuset_migrate_mm() calls inside cpuset_attach_task()"),
cpuset_fork() rebinds and migrates the mm of a child created with
CLONE_INTO_CGROUP in a cpuset with other mems. The child has not run
yet, so its pages are still shared with the parent, which stays in its
own cpuset. With CLONE_VM, the mm is the parent's.

posix_spawn() with a cgroup attribute, which systemd uses to start
services, passes CLONE_VM | CLONE_VFORK | CLONE_INTO_CGROUP. With the
parent in mems 0 and the child in mems 1, a region the parent bound to
node 0 with mbind() goes in /proc/self/numa_maps from

  bind:0 anon=4096 dirty=4096 active=0 N0=4096

to

  bind:1 anon=4096 dirty=4096 active=0 N1=4096

Don't migrate the mm in cpuset_fork(): its pages can't move without the
parent's, and
commit 42a11bf5c543 ("cgroup/cpuset: Make cpuset_fork() handle
CLONE_INTO_CGROUP properly") already noted that the memory use of a new
task isn't known. Drop the flush of the migration, and rebind the mm
only if the child doesn't share it.

Fixes: 65e510cd30d0 ("cgroup/cpuset: Move mpol_rebind_mm/cpuset_migrate_mm() calls inside cpuset_attach_task()")
Cc: stable@vger.kernel.org # v7.3+
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---
 kernel/cgroup/cpuset.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index aa82080dd1fa..420d519b11ae 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -364,6 +364,7 @@ static struct {
 	bool mems_updated;
 	bool task_work_queued;
 	bool many_dest_cs;	/* Have many destination cpusets */
+	bool in_fork;		/* Called from cpuset_fork() */
 	struct cpuset *old_cs;	/* Source cpuset */
 	nodemask_t nodemask_to;
 } attach_ctx;
@@ -3350,6 +3351,22 @@ static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task)
 	if ((task != task->group_leader) || !attach_ctx.mems_updated)
 		return;
 
+	/*
+	 * A child created with CLONE_INTO_CGROUP hasn't run yet: its pages are
+	 * still shared with the parent (current), which stays in its cpuset.
+	 * Don't migrate them, and with CLONE_VM, don't rebind the parent's mm.
+	 */
+	if (attach_ctx.in_fork) {
+		if (task->mm != current->mm) {
+			mm = get_task_mm(task);
+			if (mm) {
+				mpol_rebind_mm(mm, &cs->effective_mems);
+				mmput(mm);
+			}
+		}
+		return;
+	}
+
 	/*
 	 * Change mm for threadgroup leader. This is expensive and may
 	 * sleep and should be moved outside migration path proper.
@@ -3877,11 +3894,9 @@ static void cpuset_fork(struct task_struct *task)
 	 * and so there shouldn't be a competing cpuset_attach() operation.
 	 */
 	attach_ctx.cpus_updated = attach_ctx.mems_updated = true;
-	attach_ctx.task_work_queued = false;
-	attach_ctx.old_cs = oldcs;
+	attach_ctx.in_fork = true;
 	cpuset_attach_task(cs, task);
-	if (attach_ctx.task_work_queued)
-		schedule_flush_migrate_mm();
+	attach_ctx.in_fork = false;
 
 	dec_attach_in_progress_locked();
 	mutex_unlock(&cpuset_mutex);
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH cgroup/for-7.4-fixes 2/2] selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-04 13:51 UTC (permalink / raw)
  To: Waiman Long, Ridong Chen, Tejun Heo
  Cc: Johannes Weiner, Michal Koutný,
	Shuah Khan, Christian Brauner, Matthew Ruffell, Caron Wills,
	cgroups, linux-kselftest, linux-kernel

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 13:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH cgroup/for-7.4-fixes 2/2] selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems Hang Zhengyang

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®