mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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, stable@vger.kernel.org
Subject: [PATCH cgroup/for-7.4-fixes 1/2] cgroup/cpuset: Don't migrate the parent's memory on CLONE_INTO_CGROUP
Date: Fri,  4 Dec 2026 13:51:25 +0000	[thread overview]
Message-ID: <20261204135126.598380-2-hang.zhengyang1010@gmail.com> (raw)
In-Reply-To: <20261204135126.598380-1-hang.zhengyang1010@gmail.com>

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


  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] " Hang Zhengyang
2026-12-04 13:51 ` Hang Zhengyang [this message]
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

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-2-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=stable@vger.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®