[PATCH cgroup/for-7.4-fixes 1/2] cgroup/cpuset: Don't migrate the parent's memory on CLONE_INTO_CGROUP

From: Hang Zhengyang

Date: Fri Dec 04 2026 - 08:51:48 EST


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@xxxxxxxxxxxxxxx # v7.3+
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@xxxxxxxxx>
---
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