mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: tglx@kernel.org, peterz@infradead.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	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, kmehltretter@gmail.com,
	co+cab19e2ebe4d535a@bugs.sh, stable@vger.kernel.org
Subject: [PATCH v2 03/13] sched/mmcid: Serialize joins and exits with affinity changes by pi_lock
Date: Tue,  1 Dec 2026 19:27:51 -0500	[thread overview]
Message-ID: <863f228c8f0ed0dec6e8aaf0e67c0daa014ed1c9.1796169827.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796169827.git.mathieu.desnoyers@efficios.com>

An mm keeps the union of the allowed CPUs of its tasks, to bound its
CIDs. set_cpus_allowed_common() adds the new CPUs of a task to p->mm
with mm_update_cpus_allowed(). Commit 7e019dcc470f ("sched: Improve
cache locality of RSEQ concurrency IDs for intermittent workloads") put
this update under p->pi_lock and the runqueue lock. These locks do not
keep p->mm alive.

In this example, task T runs execve() on CPU 0. Its old mm has no other
user. Task A calls sched_setaffinity() on T from CPU 1. The kernel has
CONFIG_FUTEX=n.

  CPU 0 (T)                                CPU 1 (A)
                                           sched_setaffinity(T)
                                             task_rq_lock(T)
                                             set_cpus_allowed_common()
                                               mm = T->mm (the old mm)
  exec_mmap()
    sched_mm_cid_before_execve()
      T leaves the old mm
    T->mm = the new mm
    sched_mm_cid_after_execve()
      T joins the new mm
    futex_exec_done(): empty
  setup_new_exec()
    mmput(the old mm): the old mm is freed
                                               mm_update_cpus_allowed(mm)
                                                 reads mm->mm_cid.users:
                                                 use after free

Weiming Shi reported this with KASAN. Any process which may change the
affinity of T can trigger it. Without KASAN, the update would write to
freed memory.

Since commit c5f0bc9fd1ce ("futex/pi: Plug private futex exec() race"),
this needs CONFIG_FUTEX=n. Otherwise futex_exec_done() takes
T->pi_lock, and CPU 0 waits there for CPU 1.

Two more windows of this kind exist. Neither was reproduced on a
kernel:
- A kthread can switch to another mm with kthread_unuse_mm() and
  kthread_use_mm(). It then drops its lazy reference on the old mm in
  mmdrop_lazy_tlb(). No rq lock is held there. So the old mm can be
  freed under the same update, on any architecture. A review of the code
  found this.
- The Spin model reaches the same window in exit_mm(), with
  CONFIG_MMU_LAZY_TLB_SHOOTDOWN (powerpc). Elsewhere, the lazy TLB
  reference of the task keeps the mm alive until a context switch.

A Spin model of the CID management reaches a second race after
"sched/mmcid: Add the allowed CPUs of each joining task to the mm". It
was not reproduced on a kernel. Here task T on CPU 0 is the first
task to join mm M. It joins in execve() or as a fork() child. Task A
widens the CPUs of T from CPU 1.

  CPU 0 (T)                                CPU 1 (A)
  sched_mm_cid_fork(T)
    takes M->mm_cid.lock
    sched_mm_cid_add_user()
      __mm_update_cpus_allowed(M, &T->cpus_mask)
        M gets the old CPUs of T
                                           sched_setaffinity(T)
                                             task_rq_lock(T)
                                             set_cpus_allowed_common()
                                               T->cpus_mask = the new CPUs
                                               mm_update_cpus_allowed(M)
                                                 M->mm_cid.users == 0: return
      M->mm_cid.users = 1
    releases M->mm_cid.lock

M misses the new CPUs. Its IDs are then no longer compact, as described
in that patch.

Fix both by taking p->pi_lock around mm_cid.lock in sched_mm_cid_fork()
and sched_mm_cid_exit(). p->mm_cid.active then changes only under
pi_lock. mm_update_cpus_allowed() takes the task instead of the mm. It
does nothing when the task is not active. A task leaves the CID
management of its mm before it switches or drops the mm. It joins the
new mm after the switch. A kthread never joins. So while pi_lock is
held, the mm of an active task cannot change. This also closes the two
windows above: neither a kthread nor an exited task is active. A join
reads the allowed CPUs under pi_lock, so it sees every earlier affinity
change. A later change finds the task active and updates the mm.

An affinity change to a task which is not active yet no longer reaches
the mm. Examples are sched_setaffinity() on a new task, and clone3()
with CLONE_INTO_CGROUP into another cpuset. The join widens the mm
instead, and recomputes pcpu_thrs in per CPU mode.

The same model reaches the use after free without this change, and not
with it. No test of this series reproduces the use after free.

Fixes: 7e019dcc470f ("sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads")
Reported-by: Weiming Shi <bestswngs@gmail.com>
Reported-by: <co+cab19e2ebe4d535a@bugs.sh>
Closes: https://lore.kernel.org/all/20261110183809.3308868-1-bestswngs@gmail.com/
Suggested-by: Thomas Gleixner <tglx@kernel.org>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
---
 kernel/sched/core.c | 50 +++++++++++++++++++++++++++++++++------------
 1 file changed, 37 insertions(+), 13 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 80239800c566..575813b1ae97 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2839,7 +2839,7 @@ int push_cpu_stop(void *arg)
 	return 0;
 }
 
-static inline void mm_update_cpus_allowed(struct mm_struct *mm, const cpumask_t *affmask);
+static inline void mm_update_cpus_allowed(struct task_struct *p, const struct cpumask *affmsk);
 
 /*
  * sched_class::set_cpus_allowed must do the below, but is not required to
@@ -2854,7 +2854,7 @@ void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx
 
 	cpumask_copy(&p->cpus_mask, ctx->new_mask);
 	p->nr_cpus_allowed = cpumask_weight(ctx->new_mask);
-	mm_update_cpus_allowed(p->mm, ctx->new_mask);
+	mm_update_cpus_allowed(p, ctx->new_mask);
 
 	/*
 	 * Swap in a new user_cpus_ptr if SCA_USER flag set
@@ -10712,6 +10712,11 @@ void call_trace_sched_update_nr_running(struct rq *rq, int count)
  *			mm_update_cpus_allowed(). Nests in mm_cid::mutex
  *			and runqueue lock.
  *
+ * task::pi_lock:	Serializes the updates of task::mm_cid::active
+ *			with mm_update_cpus_allowed(). task::mm_cid::active
+ *			is written with task::pi_lock and mm::mm_cid::lock
+ *			held. task::pi_lock is taken first.
+ *
  * The mm_cidmask bitmap is not protected by any of the mm::mm_cid locks
  * and can only be modified with atomic operations.
  *
@@ -10891,8 +10896,9 @@ static bool mm_update_max_cids(struct mm_struct *mm)
 
 /*
  * Add @affmsk to the allowed CPUs of @mm. Return true if the set grew.
- * Called from mm_update_cpus_allowed() on an affinity change, and from
- * sched_mm_cid_add_user() when a task joins the MM CID management.
+ * Called from mm_update_cpus_allowed() on an affinity change of an active
+ * task, and from sched_mm_cid_add_user() when a task joins the MM CID
+ * management.
  */
 static inline bool __mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
 {
@@ -10914,18 +10920,28 @@ static inline bool __mm_update_cpus_allowed(struct mm_struct *mm, const struct c
 	return true;
 }
 
-static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk)
+static inline void mm_update_cpus_allowed(struct task_struct *p, const struct cpumask *affmsk)
 {
-	struct mm_mm_cid *mc;
+	lockdep_assert_held(&p->pi_lock);
 
-	if (!mm || !READ_ONCE(mm->mm_cid.users))
+	/*
+	 * p::mm_cid::active is updated with pi_lock and mm_cid::lock held. So
+	 * pi_lock is sufficient to check the active bit. If the active bit is
+	 * not set then the task is either not involved or exiting.
+	 *
+	 * An active task leaves the MM CID management before it drops or
+	 * replaces p->mm. So while pi_lock is held, p->mm of an active task
+	 * stays valid.
+	 */
+	if (!p->mm_cid.active)
 		return;
 
-	mc = &mm->mm_cid;
+	struct mm_mm_cid *mc = &p->mm->mm_cid;
+
 	guard(raw_spinlock)(&mc->lock);
 
 	/* Apply the update and check whether deferred work if required */
-	if (!__mm_update_cpus_allowed(mm, affmsk))
+	if (!__mm_update_cpus_allowed(p->mm, affmsk))
 		return;
 
 	__mm_update_max_cids(mc);
@@ -11081,9 +11097,12 @@ static void sched_mm_cid_fork(struct task_struct *t)
 	WARN_ON_ONCE(t->mm_cid.cid != MM_CID_UNSET);
 
 	guard(mutex)(&mm->mm_cid.mutex);
-	scoped_guard(raw_spinlock_irq, &mm->mm_cid.lock) {
+	/* Protect against concurrent affinity changes */
+	scoped_guard(raw_spinlock_irq, &t->pi_lock) {
 		struct mm_cid_pcpu *pcp = this_cpu_ptr(mm->mm_cid.pcpu);
 
+		guard(raw_spinlock)(&mm->mm_cid.lock);
+
 		/* First user ? */
 		if (!mm->mm_cid.users) {
 			sched_mm_cid_add_user(t, mm);
@@ -11117,6 +11136,7 @@ static void sched_mm_cid_fork(struct task_struct *t)
 
 static bool sched_mm_cid_remove_user(struct task_struct *t)
 {
+	lockdep_assert_held(&t->pi_lock);
 	lockdep_assert_held(&t->mm->mm_cid.lock);
 
 	t->mm_cid.active = 0;
@@ -11170,7 +11190,9 @@ void sched_mm_cid_exit(struct task_struct *t)
 	scoped_guard(mutex, &mm->mm_cid.mutex) {
 		/* mm_cid::mutex is sufficient to protect mm_cid::users */
 		if (likely(mm->mm_cid.users > 1)) {
-			scoped_guard(raw_spinlock_irq, &mm->mm_cid.lock) {
+			/* Protect against concurrent affinity changes */
+			scoped_guard(raw_spinlock_irq, &t->pi_lock) {
+				guard(raw_spinlock)(&mm->mm_cid.lock);
 				if (!__sched_mm_cid_exit(t))
 					return;
 				/*
@@ -11184,8 +11206,10 @@ void sched_mm_cid_exit(struct task_struct *t)
 			mm_cid_fixup_cpus_to_tasks(mm);
 			return;
 		}
+
 		/* Last user */
-		scoped_guard(raw_spinlock_irq, &mm->mm_cid.lock) {
+		scoped_guard(raw_spinlock_irq, &t->pi_lock) {
+			guard(raw_spinlock)(&mm->mm_cid.lock);
 			/*
 			 * Ignore mode change. There is nothing to do as the MM
 			 * is going away in the common case.
@@ -11271,7 +11295,7 @@ void mm_init_cid(struct mm_struct *mm)
 	bitmap_zero(mm_cidmask(mm), num_possible_cpus());
 }
 #else /* CONFIG_SCHED_MM_CID */
-static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk) { }
+static inline void mm_update_cpus_allowed(struct task_struct *p, const struct cpumask *affmsk) { }
 static inline void sched_mm_cid_fork(struct task_struct *t) { }
 #endif /* !CONFIG_SCHED_MM_CID */
 
-- 
2.47.3


  parent reply	other threads:[~2026-12-02  0:28 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:27 [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 01/13] sched/mmcid: Move the execve() hooks into exec_mmap() Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 02/13] sched/mmcid: Add the allowed CPUs of each joining task to the mm Mathieu Desnoyers
2026-12-02  0:27 ` Mathieu Desnoyers [this message]
2026-12-02  0:27 ` [PATCH v2 04/13] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 05/13] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 06/13] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 07/13] selftests/rseq: Test the upper bound of the concurrency IDs Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 08/13] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 09/13] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 10/13] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 11/13] selftests/rseq: Test the last user after an affinity change during execve Mathieu Desnoyers
2026-12-02  0:28 ` [PATCH v2 12/13] selftests/rseq: Test the concurrency IDs with an io_uring SQPOLL thread Mathieu Desnoyers
2026-12-02  0:28 ` [PATCH v2 13/13] selftests/rseq: Test the concurrency IDs after clone3() into a wider cpuset Mathieu Desnoyers
2026-12-02  2:49 ` [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Karl Mehltretter

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=863f228c8f0ed0dec6e8aaf0e67c0daa014ed1c9.1796169827.git.mathieu.desnoyers@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=bestswngs@gmail.com \
    --cc=boqun@kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=co+cab19e2ebe4d535a@bugs.sh \
    --cc=dietmar.eggemann@arm.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=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=stable@vger.kernel.org \
    --cc=tglx@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

all inboxes | Powered by JetHome®