The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@kernel.org>
To: Karl Mehltretter <kmehltretter@gmail.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	peterz@infradead.org, linux-kernel@vger.kernel.org,
	mingo@redhat.com, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
	rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
	vschneid@redhat.com, kprateek.nayak@amd.com, paulmck@kernel.org,
	boqun@kernel.org, shuah@kernel.org, kees@kernel.org,
	viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz,
	oleg@redhat.com, bestswngs@gmail.com, xmei5@asu.edu,
	xujiakai24@mails.ucas.ac.cn, linux-mm@kvack.org,
	linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Dominik Maier <dmnk@google.com>
Subject: Re: [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity
Date: Mon, 30 Nov 2026 15:59:53 +0100	[thread overview]
Message-ID: <87ecc2qs9i.ffs@fw13> (raw)
In-Reply-To: <awyrCYBrJS-CuBE8@gmail.com>

On Sun, Nov 29 2026 at 23:54, Karl Mehltretter wrote:
> On Sun, Nov 29, 2026 at 10:48:39PM +0100, Thomas Gleixner wrote:
> vfork() and CLONE_VM without CLONE_THREAD breaks that assumption:
> de_thread() only terminates threads in the same thread group, not other
> tasks sharing the MM.
>
> When execve() fails in a CLONE_VM child before the MM switch,
> sched_mm_cid_cleanup_execve() modifies the shared MM locklessly while
> the parent's threads are still active. Allocation-failure injection on
> x86 KVM hits:
>
>   WARNING: kernel/sched/core.c:11198 at sched_mm_cid_after_execve+0x309/0x3a0

Groan. This MM sharing is a real pain...

> Could both hooks move into exec_mmap() instead?
>
> Leave the old MM after mm_exit_exec_release() and join the new one after
> mmap_read_unlock(). The only failures in between are killable lock
> waits, which require a pending SIGKILL, so the old MM never needs to be
> rejoined. This removes new_mm and sched_mm_cid_cleanup_execve(), and
> avoids holding the locks across irq_work_sync() and cancel_work_sync().

I thought about that and then discarded the idea because of your ptrace
overriding signal thing. My tired brain obviously did not see the
connection to SIGKILL. Thanks for pointing it out!

That makes the whole thing even simpler. Updated patch below.

Thanks,

        tglx
---
 fs/exec.c           |   13 ++++++++++---
 kernel/sched/core.c |   14 ++++++++------
 2 files changed, 18 insertions(+), 9 deletions(-)

--- a/fs/exec.c
+++ b/fs/exec.c
@@ -858,6 +858,15 @@ static int exec_mmap(struct linux_binprm
 	/* Clean up futexes and release the mm */
 	mm_exit_exec_release(tsk, old_mm);
 
+	/*
+	 * The below lock acquisitions can only fail due to a SIGKILL. In
+	 * that case the task will die and even PTRACE won't be able to
+	 * resurrect it. So this is the safest place to leave MM CID
+	 * management as there is no way that this will go back to user
+	 * space.
+	 */
+	sched_mm_cid_before_execve(current);
+
 	ret = down_write_killable(&tsk->signal->exec_update_lock);
 	if (ret)
 		return ret;
@@ -908,6 +917,7 @@ static int exec_mmap(struct linux_binprm
 	} else {
 		mmdrop_lazy_tlb(active_mm);
 	}
+	sched_mm_cid_after_execve(current);
 	futex_exec_done(tsk);
 	return 0;
 }
@@ -1853,7 +1863,6 @@ static int bprm_execve(struct linux_binp
 	 */
 	check_unsafe_exec(bprm);
 	current->in_execve = 1;
-	sched_mm_cid_before_execve(current);
 
 	sched_exec();
 
@@ -1866,7 +1875,6 @@ static int bprm_execve(struct linux_binp
 	if (retval < 0)
 		goto out;
 
-	sched_mm_cid_after_execve(current);
 	rseq_execve(current);
 	/* execve succeeded */
 	current->in_execve = 0;
@@ -1885,7 +1893,6 @@ static int bprm_execve(struct linux_binp
 	if (bprm->point_of_no_return && !fatal_signal_pending(current))
 		force_fatal_sig(SIGSEGV);
 
-	sched_mm_cid_after_execve(current);
 	rseq_force_update();
 	current->in_execve = 0;
 
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -11063,7 +11063,7 @@ static void sched_mm_cid_fork(struct tas
 		if (!mm->mm_cid.users) {
 			sched_mm_cid_add_user(t, mm);
 			t->mm_cid.cid = mm_get_cid(mm);
-			/* Required for execve() */
+			/* Required for execve() as the task is running */
 			pcp->cid = t->mm_cid.cid;
 			return;
 		}
@@ -11161,10 +11161,12 @@ void sched_mm_cid_exit(struct task_struc
 		}
 		/* Last user */
 		scoped_guard(raw_spinlock_irq, &mm->mm_cid.lock) {
-			/* Required across execve() */
-			if (t == current)
-				mm_cid_transit_to_task(t, this_cpu_ptr(mm->mm_cid.pcpu));
-			/* Ignore mode change. There is nothing to do. */
+			/*
+			 * Ignore mode change. There is nothing to do as the MM
+			 * is going away in the common case. A potential execve()
+			 * failure which requires to re-join the MM's MM CID
+			 * handles a stale transit in sched_mm_cid_after_execve().
+			 */
 			sched_mm_cid_remove_user(t);
 		}
 	}
@@ -11181,7 +11183,7 @@ void sched_mm_cid_exit(struct task_struc
 	cancel_work_sync(&mm->mm_cid.work);
 }
 
-/* Deactivate MM CID allocation across execve() */
+/* Deactivate MM CID allocation across execve() switching the MM */
 void sched_mm_cid_before_execve(struct task_struct *t)
 {
 	sched_mm_cid_exit(t);

      reply	other threads:[~2026-11-30 14:59 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 16:17 [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 01/17] sched/mmcid: Give a CID to a task that joins its mm while running Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
2026-11-29 21:54   ` Thomas Gleixner
2026-11-17 16:17 ` [PATCH 03/17] sched/mmcid: Reset stale state when the first user joins an mm Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 04/17] sched/mmcid: Let a running joiner take its CID after the fixups Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 05/17] sched/mmcid: Take the allowed CPUs of the first user when it joins Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 06/17] sched/mmcid: Synchronize MM teardown with affinity updates Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 07/17] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 08/17] selftests/rseq: Test the concurrency ID after the last user fails execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 09/17] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 10/17] selftests/rseq: Test the upper bound of the concurrency IDs Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 11/17] selftests/rseq: Test affinity changes made by another process Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 12/17] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 13/17] selftests/rseq: Test concurrent thread creation and forced migration Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 14/17] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 15/17] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 16/17] selftests/rseq: Test affinity changes while a task replaces or drops its mm Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 17/17] selftests/rseq: Test the last user after an affinity change during execve Mathieu Desnoyers
2026-11-28 10:28 ` [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity Thomas Gleixner
2026-11-28 19:22   ` Karl Mehltretter
2026-11-28 22:40     ` Thomas Gleixner
2026-11-29 21:48       ` Thomas Gleixner
2026-11-29 22:24         ` Thomas Gleixner
2026-11-29 22:54         ` Karl Mehltretter
2026-11-30 14:59           ` Thomas Gleixner [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=87ecc2qs9i.ffs@fw13 \
    --to=tglx@kernel.org \
    --cc=bestswngs@gmail.com \
    --cc=boqun@kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=dmnk@google.com \
    --cc=jack@suse.cz \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=kmehltretter@gmail.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vschneid@redhat.com \
    --cc=xmei5@asu.edu \
    --cc=xujiakai24@mails.ucas.ac.cn \
    /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