From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D01FF606A32A for ; Sun, 29 Nov 2026 22:24:22 +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 kanga.kvack.org (Postfix) id 8716F6B05B1; Sun, 29 Nov 2026 17:24:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 822276B05B3; Sun, 29 Nov 2026 17:24:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 764F16B05B4; Sun, 29 Nov 2026 17:24:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C9E1D6B05B1 for ; Sun, 29 Nov 2026 17:24:20 -0500 (EST) Received: from smtpin12.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 100DAA0932 for ; Sun, 29 Nov 2026 22:24:20 +0000 (UTC) X-FDA: 85489586280.12.D71527F Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf03.hostedemail.com (Postfix) with ESMTP id 6631E2000A for ; Sun, 29 Nov 2026 22:24:18 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=L+JJOFa2; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of tglx@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=tglx@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795991058; b=WmbawIn8jt4cRrdFxnN+XB23Dk3RQ0kUVkbn4UQun/ptU/fwpJeIN0dkKZhrz880HrK+Ie GU5uV/+ChzNN82tUHeoMJcOP4bjHvuE+lod/ekT0gwB+2YIq9Jn9tW+svN04iVPtwPc86U lVIH0YVtqy1s8W82x4fSf74MMYRF5L8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795991058; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zQdC6fNAgnGDP9lWllaX6bjBr2l+P035fETr38rlm/0=; b=TJ/hUKGl4w9ebLw1gcAEj3NgUxZ2mx0AEmIw4UYPlCohOxhWn4qbL6tKdqAmizoG73lS1h uz8jbZlNWGC7iwK7ghOPsniV/1Qx/pCtDGdp3QUXTXUjKyppQkyqReQhODdB41UZP+R1SR DH/NB49UcZqjzjytDuT3+kYcju6/fHA= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=L+JJOFa2; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of tglx@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=tglx@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 1E00F43CDD; Sun, 29 Nov 2026 22:24:17 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 Content-Type: text/plain X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 6631E2000A X-Stat-Signature: 8rm3g3kk95cp1j4zgz3h7f8g69ybjwdf X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1795991058-477817 X-HE-Meta: U2FsdGVkX1/mGfO9U+uAS2DddaQGgv++HqzjUvbwfad+A8Nj0b1TNTQlXHicd37Pg16mK/EufVGXsfqKcFkBtSXdnieJgfyTzj6bZq9msoYckj9yraB9ztJcLFaZxzNcOorypNbv4pQDq0UQaVuppdT68fphQbwRN0PQrHqggMg2K26JhRABv/5yWhJ62uzemjT4zenTG0Sv5jQG21mdcAM66pgoAnQVkVNpj8aAda5R1EvZw0CyWilwtg6N9a4ql7EgFugC+GZz3HyUMzTsf/vMas7i/urI0FvGus4s/QPqPRSZUWAQ56dYSMxnzK1D9PWV3HqO1i94CMMW5oxcdVkimKhnVBsNir8qIN1QbLsyjeIIlVOHYub4QWaiGeu+t8ZEG65wNFBWU0hsA2MqOML4HPmclE7w2pvXVBjEkxe58voTZ6+qPomleYtiWF6kBxsupOK9Cl/R3wHzLUKySOjdOobxqoaaLbVs7k0M2IK1zx/JWeV54RxK/FXq+fVkIOhkpocHO7zQwF6ak6EO2yM5egGPOfPzckhYGo7huxoyzmbzh8RbN18r+7PixHWLi7uYsNpC3D94EBwyoIJQuHwC0R62LjVObXXDPiKAfZNc+cWGjhpnFfaHYgH3G6r0n9TL2SGOIjExeMgoozpNK/bziLm118EJXPCij6/AEEn6P07tlVAK+qT/sQmqVgnencEiEekJCvk3zfeuvTWQu8IQgOWigVu61c+Sc/4q2VIQMg3dE5sTwx4jFb8V3Fngcx8YbdTaHvsJ7OSzSLEt2hVYoeAwhZQj8bPPZbrEWD67VZoERzBG4R4PAl5gxYrlU0tzvCtVhDDJkVK57i043rh/37wLGRUQj/ipEEE9MrBVuJni3jMmEZvYj0wRKaRE+I3DcmKNs5j5rx3HS1FIF2/EozXxCEZawd1nTrS+I76OB6SSBqRz0Piq3JVgh6i5YUCY7Bt0pCU3J+9VvaM qicg4tK7 IgoI+jM6ZDzRo36IK+ip4smgSjKkDSgQj506T/3Xt1VE2AMOaXoEafhJItw9WPwatAjdSFAqFLglS0rPawwfhGSGK7SOcFNZFl0O4hbiElqEjcw3aqXq8MjktY5KsMs0IEBLPsiJip7bsrrZVrR8S0spD8nmxu4lnc5uekpTethAhO5Z7j3jv0mXN6bDv/MrfFTN/YqTEoS/b4hDFGeq6I/8nX9vn8GNafv/TUmu48eP8VPumwKaw8D3NXYY1rUS8QEYs1HTYnoCYF6SzSo9OGvs0Bgr4vXR7id42CShjpzE1YGYwr0Ag5ULDET9RbnT81Wipi6h5KQI2GM96q2IhI7jl5QdCaTsAK1M7lBpnH6PWqwqef9yqCxcGHDh1GYtHLKnvw4x/bbWNzCwq6R6BOqkPez9YI7pDpDVRTy+P6FMn1zL3Ra3YMBxRtev2n20wS3lsKjZTd9jcz2iQ3gFnorfpkw== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 */