From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171288; b=4oG2DQk01cpB/Z+MSjnRxdk5Pbt50uAwN0/Lv1OS0sHe0k0QOzZDV/T2VeeW7P2NMS+/ DiQFptPiztiCHqnkzer5AOYt2R0/VVZ7vGMkfJqC1PHSfJBQiHVhYU+Hep6TWLLCeMugO seGLvtOcov5aDzYYmLf+bN11mlP5EKv9iuJRU8bHAmglL+enVXGF4zJhZLQRqTejCOlP5 //qxpSNtd7ZfcNRZC+4DB4q9MUWK8NY1SASGmafKvB2RloEurCrm4b5orDpCWVd84kv8e ojsQzqb59bPo2GaVvZH/unV6YCglzgHZwCV1AfXDzqTMEyBaMTBt71OGWN3W0bRAiGA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171288; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=wFjH61R8Kzxc4d1DqGRV557nwlTkxlWvedvbywMqca4=; b=V6vCr4ktVTgl8lQ1+cTb3X91IOrcqRLQH5T2SCqrWhUcThmL9/jmcf+tUcr/Q4QexwQJ GyHwXIFcEbfOsJGogufWTO6gRoa3+9teEXcNn/8OLunZym6PMkIc1FQM5gBi79kWulvgN jl/085XfVZHUee2Kaqqi7StYh6y9YLQHQgI72DDv36TUdzEavI1R+nJ24p6yTFxsKwaPy +GjHgJG97h0LP6ojU+t4EP6en6ycxFYG0iu3Zd7+PE1DGRYy0mHFD6kGOlR1I9+BVHbVa kqO0LzQAMkPDhMTmnrgJXvWUHvGuIcRGePauLPWPQgjpsP7WYN8MRekM8QBbOYvA3lg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=ezETYzur; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76DB4606A32E; Wed, 2 Dec 2026 00:28:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 76DB4606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=ezETYzur DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796171286; bh=wFjH61R8Kzxc4d1DqGRV557nwlTkxlWvedvbywMqca4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ezETYzurZlwaNYV9UVJQMremEPoRwn7vzPyidLO7fPwmExqQpCcdqPdTHFKyI2vFc EC0hNVLH8kXxhPNYHtLBsY/bpMQtHjgyWWKxGuQTYEywCkgSKJaxGQkFjZ48KkymHx N4Akb+i/ipwrshrOnmKMD0e/CUYqK7CXwnllGsEAlVCOcqvIOXSgIjSChz6Ff47YnX 7usYohIVs6xjnusOgnsoUKAAzbAPukkBuqqyy/+txljEUA9C9AF4IbWvZTGlayerYx N3hnpks6h1oj+ssf9SmmDJ4ydrlEG+ClV+kPWTNb+8VKzVWFtvHdncnmmW95AE5f8k VP5lMVM7i/eBg== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jfJWV2rcZzYRB; Tue, 01 Dec 2026 19:28:06 -0500 (EST) From: Mathieu Desnoyers To: tglx@kernel.org, peterz@infradead.org Cc: Mathieu Desnoyers , 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 Message-ID: <863f228c8f0ed0dec6e8aaf0e67c0daa014ed1c9.1796169827.git.mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Reported-by: Closes: https://lore.kernel.org/all/20261110183809.3308868-1-bestswngs@gmail.com/ Suggested-by: Thomas Gleixner Signed-off-by: Mathieu Desnoyers 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