From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795991057; b=u8azKmfWcQq2LxAWHCw3Q9xOmsDZAxYs7uOjasrGQOE9/6VgErZ3fDspgpC/rNyJG9/N OHAC8kH8oBeXnZk51HQCGMD8J7jqquyMI1Jx0CJPKJeRr4WII7uZdJ9DoXQN615xX1ShQ EJiAly56Fqz8DVig6NvBUao+OYPVBE7rtZL4zoWK9hebTTthc9DRdyPvrQnJfYdp0Vobf vRIqlnI0ETZ0hv8+nbRl4lcKB+V/eoXPs3p+dM2npy57yjgwnbzfgvb+wInZXPpUXMX9P r7bFrqoHHUMfSJOWzrbb/v4umIlPAMOsIQSWBfgmhuD5PWgBZrz9aQ7f/moG1BABzDQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991057; h=Received:DKIM-Signature:From:To:Cc:Subject:In-Reply-To:References: Date:Message-ID:MIME-Version:Content-Type; bh=zQdC6fNAgnGDP9lWllaX6bjBr2l+P035fETr38rlm/0=; b=QzuOKPxPfnc+QsuZQHlKbJbAA89WkElUgJcQnCeqQtRxuoxclR87rxm8l1aJxtPwQyac GU8+UUC/rxm9FTbBeNDKTRn0ENkuTEyWTbeX2iyVESCDM0pNrJlXMZOrqy/luZS6nHgae KPP+8xPk1jCPyFniLQ7Rl5BqLLA9163WLA3vuCW0n9zjTdEkDfYioRe2yQ1zCHTHI5Pru q8CsfpvSO52U8PXsCpbHc/2E8oA4qCXRY2Oc9jgfo55IpZ+ZsRAUde0bUBgyom9gYr/vC jD3svj1xQrTa4zQpTi1sjJCOhI0l3aflptSwb6/XEZX21hYtYkOl7MQsXjOaBkHjAoQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=L+JJOFa2; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.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 5F02A606A31C; Sun, 29 Nov 2026 22:24:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=L+JJOFa2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 349B41F00897; Sun, 29 Nov 2026 22:24:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795991057; bh=zQdC6fNAgnGDP9lWllaX6bjBr2l+P035fETr38rlm/0=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=L+JJOFa2qg1KxgqbLCuCO4Y1vuyfaozzbkTroWDjwqsBzno5Eu03l+HXGVIiX+8wN UTY/kZn69ngs1WEFc1QcrwJYfx41GYuqiGaeVw0F/U57MH4wqmDu230zLSHlB85kU0 FbvQWXlJ3XrAzyrmuBAiPN7dF2VMLtG1+eBffh9fOwAlkNGl7qCoRoYV2A3FlsU5BP KtRJzb1bneqzNFrDnhplvUY1eaaywyyc3+sZPiyLHOB0AZGt2skKDEXaKZWCb/kYnl KCqqe9AHnSH82Qo42JPMvE7rAsScelKS7G1HNxSqwwMAHOchP10gZwALy9LGf3qyt3 0X0GCVZSHR/Zg== From: Thomas Gleixner To: Karl Mehltretter Cc: Karl Mehltretter , Mathieu Desnoyers , 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 Subject: Re: [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity In-Reply-To: <8733sjs408.ffs@fw13> References: <87bj79s11m.ffs@fw13> <20261128192202.93895-1-kmehltretter@gmail.com> <875wxgshou.ffs@fw13> <8733sjs408.ffs@fw13> Date: Sun, 29 Nov 2026 23:24:13 +0100 Message-ID: <87v75fqnsi.ffs@fw13> 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-Type: text/plain On Sun, Nov 29 2026 at 22:48, Thomas Gleixner wrote: > On Sat, Nov 28 2026 at 20:22, Karl Mehltretter wrote: >> - execve_test case 3 (17/17): fails on !RT baseline, skips on all four >> configs with your patch. >> - exec_affinity_test case 2 (12/17): still fails on 4 vCPUs. > > This does not address the affinity related issues as I clearly said in > the previous mail, but I have a different solution for that too which > I'll send out in reply to this mail. The patch below addresses the affinity and associated lifetime problem in a different and IMO more comprehensible way and thereby avoids sprinkling ill defined "barriers" all over the place. The point is that the affinity setting path has the lcoking pattern: lock(&task->pi_lock); lock(&task->mm->mm_cid.lock); where there is no guarantee that task::pi_lock prevents the task:mm changing. So instead of playing silly games with those mm_cid_barriers the obvious solution is to protect the changes sched_mm_cid_add/remove_user() with the same locking pattern. That allows to check whether a affinity change for a task is relevant to evaluating task::mm_cid::active. As a consequence this allows to tackle a the issues in the current code which are related to intermediate affinity changes by: 1) Initializing the MMCID state to zero 2) Taking the affinity of new or re-joining tasks into account so that any intermediate state is covered. Taking task::pi_lock is not expensive in the common case, i.e. fork()/exit(), because any contention is purely academic. That patch is obviously a combo of a preparatory one which changes how mm_init_cid() works and the actual serialization change and wants to be split accordingly. Thanks, tglx --- --- a/fs/exec.c +++ b/fs/exec.c @@ -883,7 +883,6 @@ static int exec_mmap(struct linux_binprm tsk->active_mm = mm; tsk->mm = mm; sched_cache_exec_mmap(tsk, mm); - mm_init_cid(mm, tsk); exec_state = task_exec_state_replace(tsk, exec_state); /* * This prevents preemption while active_mm is being loaded and --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1624,14 +1624,14 @@ static inline unsigned long *mm_cidmask( return (unsigned long *)cid_bitmap; } -void mm_init_cid(struct mm_struct *mm, struct task_struct *p); +void mm_init_cid(struct mm_struct *mm); static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *p) { mm->mm_cid.pcpu = alloc_percpu_noprof(struct mm_cid_pcpu); if (!mm->mm_cid.pcpu) return -ENOMEM; - mm_init_cid(mm, p); + mm_init_cid(mm); return 0; } # define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__)) @@ -1651,7 +1651,6 @@ static inline unsigned int mm_cid_size(v /* Use 2 * NR_CPUS as worse case for static allocation. */ # define MM_CID_STATIC_SIZE (2 * sizeof(cpumask_t)) #else /* CONFIG_SCHED_MM_CID */ -static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p) { } static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p) { return 0; } static inline void mm_destroy_cid(struct mm_struct *mm) { } static inline unsigned int mm_cid_size(void) --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2830,7 +2830,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 @@ -2845,7 +2845,7 @@ void set_cpus_allowed_common(struct task 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 @@ -10877,26 +10877,51 @@ static bool mm_update_max_cids(struct mm return true; } -static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk) +/* + * Invoked from mm_update_cpus_allowed() on affinity changes if the task has + * p->mm_cid.active set and from sched_mm_cid_fork() for each task which joins + * the MM CID management on a MM. + */ +static inline bool __mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk) { - struct cpumask *mm_allowed; - struct mm_mm_cid *mc; + struct cpumask *mm_allowed = mm_cpus_allowed(mm); + struct mm_mm_cid *mc = &mm->mm_cid; unsigned int weight; - if (!mm || !READ_ONCE(mm->mm_cid.users)) - return; + lockdep_assert_held(&mc->lock); + /* * mm::mm_cid::mm_cpus_allowed is the superset of each threads * allowed CPUs mask which means it can only grow. */ - mc = &mm->mm_cid; - guard(raw_spinlock)(&mc->lock); - mm_allowed = mm_cpus_allowed(mm); weight = cpumask_weighted_or(mm_allowed, mm_allowed, affmsk); if (weight == mc->nr_cpus_allowed) - return; + return false; WRITE_ONCE(mc->nr_cpus_allowed, weight); + return true; +} + +static inline void mm_update_cpus_allowed(struct task_struct *p, const struct cpumask *affmsk) +{ + lockdep_assert_held(&p->pi_lock); + + /* + * 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. + */ + if (!p->mm_cid.active) + return; + + 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(p->mm, affmsk)) + return; + __mm_update_max_cids(mc); if (!cid_on_cpu(mc->mode)) return; @@ -11023,6 +11048,11 @@ static void mm_cid_fixup_tasks_to_cpus(v static bool sched_mm_cid_add_user(struct task_struct *t, struct mm_struct *mm) { lockdep_assert_held(&mm->mm_cid.lock); + /* + * Update allowed CPUs to take any affinity change, which happened + * before sched_mm_cid_fork() acquired the locks, into account. + */ + __mm_update_cpus_allowed(mm, &t->cpus_mask); t->mm_cid.active = 1; hlist_add_head(&t->mm_cid.node, &mm->mm_cid.user_list); @@ -11041,9 +11071,12 @@ static void sched_mm_cid_fork(struct tas 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); @@ -11077,6 +11110,7 @@ static void sched_mm_cid_fork(struct tas 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; @@ -11130,7 +11164,9 @@ void sched_mm_cid_exit(struct task_struc 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; /* @@ -11144,8 +11180,10 @@ void sched_mm_cid_exit(struct task_struc 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. A potential execve() @@ -11185,8 +11223,8 @@ static void sched_mm_cid_cleanup_execve( per_cpu(mm->mm_cid.pcpu->cid, cpu) = 0; bitmap_zero(mm_cidmask(mm), num_possible_cpus()); - mm->mm_cid.nr_cpus_allowed = t->nr_cpus_allowed; - cpumask_copy(mm_cpus_allowed(mm), &t->cpus_mask); + mm->mm_cid.nr_cpus_allowed = 1; + cpumask_clear(mm_cpus_allowed(mm)); } /* Reactivate MM CID after execve() */ @@ -11259,11 +11297,11 @@ static void mm_cid_irq_work(struct irq_w schedule_work(&mm->mm_cid.work); } -void mm_init_cid(struct mm_struct *mm, struct task_struct *p) +void mm_init_cid(struct mm_struct *mm) { mm->mm_cid.max_cids = 0; mm->mm_cid.mode = 0; - mm->mm_cid.nr_cpus_allowed = p->nr_cpus_allowed; + mm->mm_cid.nr_cpus_allowed = 0; mm->mm_cid.users = 0; mm->mm_cid.pcpu_thrs = 0; mm->mm_cid.update_deferred = 0; @@ -11272,11 +11310,11 @@ void mm_init_cid(struct mm_struct *mm, s mm->mm_cid.irq_work = IRQ_WORK_INIT_HARD(mm_cid_irq_work); INIT_WORK(&mm->mm_cid.work, mm_cid_work_fn); INIT_HLIST_HEAD(&mm->mm_cid.user_list); - cpumask_copy(mm_cpus_allowed(mm), &p->cpus_mask); + cpumask_clear(mm_cpus_allowed(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 */