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 2F45F606A325 for ; Sun, 29 Nov 2026 21:48:48 +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=cWqX24wo Received: by kanga.kvack.org (Postfix) id 169966B05B0; Sun, 29 Nov 2026 16:48:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11B046B05B1; Sun, 29 Nov 2026 16:48:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 030196B05B2; Sun, 29 Nov 2026 16:48:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C56496B05B0 for ; Sun, 29 Nov 2026 16:48:46 -0500 (EST) Received: from smtpin15.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3BD0C1408F6 for ; Sun, 29 Nov 2026 21:48:46 +0000 (UTC) X-FDA: 85489496652.15.5D0B3AE Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf16.hostedemail.com (Postfix) with ESMTP id 94B08180009 for ; Sun, 29 Nov 2026 21:48:44 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=cWqX24wo; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.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=1795988924; b=cnnnWaPbd/sJQgBvf9HxL54wjKQlQrkfL5txB1X/ZJoGu8dmV9zQzB3Zv6acq6C08F+kJw rA5yiQAZ2W1ADdA60RgSzHWEQwgqJTwJPZxaL0vDjiuTxYLij/zNZcbihnwm+8BvW9m3F7 Rfcl7ks85wA91ke4krwX67Cws6hu1eY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795988924; 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=FI7o+YDw9VEwJPQxodXexoEIMHwyfGOJNi7bQ1lq5So=; b=iojwfuaXmcuvuQzUWKEetHwwv+T/UdNuEByoISza5X8bi6abCfT+uNhhSBnzj6IbdMfzxE ecEBSF/tL60Waq6auNIN9qUJrz7LQg9uT0riayjJO/FqH2R7GpQHF+8l8eVpYePd0Gg98n WqtadjvgrL6y/NWawLlDT8XTyacjWjo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=cWqX24wo; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.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 244B240DA9; Sun, 29 Nov 2026 21:48:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA5591F0089C; Sun, 29 Nov 2026 21:48:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795988922; bh=FI7o+YDw9VEwJPQxodXexoEIMHwyfGOJNi7bQ1lq5So=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=cWqX24wo0OxQmsIIW+JLmdvSOdhVLyS4UOHUx0OcRywqV5s9aYXcaNeTkPOzo85F2 GrY5+B6beqpVCZqSLFPcUuN9YQAAtyJlUwclRRehW7u3xONPZbtIbF12uUhK57KDXl uqjmvlrU9Wb8TPbAjnT+AmQSLIoBKJWIX2ybBwwsgm3GLyC3LjSj/98YtiRoZpE5OG 8enbxMNdOl8GjuMiAXjv2TMoDW8VXxjNQp8OcFN5zV/iPaI7jollJvB9zwkJYNAxeu cYhpZYKo1krfizXIvZNBfpwvBpaEYmCRcZppD8m0lSe4UO4V/rPfALldX1rnahI9kP ZhyiY9AJcHwiA== 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: <875wxgshou.ffs@fw13> References: <87bj79s11m.ffs@fw13> <20261128192202.93895-1-kmehltretter@gmail.com> <875wxgshou.ffs@fw13> Date: Sun, 29 Nov 2026 22:48:39 +0100 Message-ID: <8733sjs408.ffs@fw13> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 94B08180009 X-Rspam-User: X-Stat-Signature: hfnbn7xafgwkp8s1axh7j39o1w845sp8 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_P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2fFaWSjAd2NkeEH8Gbdeb1Ww4dyCnvyzapvTxXwU3c5sF87bU5odUQ/ONYzYfGR/i7DZ44wtbX0QzgeZXXwFIFRnNp1R7+F4f3MLYhvGDcLuhCqpcQgqUn351RRYn3iITUXDMw0/Ftd6/dNBIPorLcykC7s85Cj16+Phqrl/+yE7QKn4qa4xKN9ElX9rioU93rHf7WBLSEFYDQLguyCXmQ6PCr35gxYo7XaLyHJcxd6PQUoYSv0j4x7QFSZuFBLNOYYJB/8g5aWRlI0EqfI74sw+365U50CeMneW8c11HWXSc6DvcnM+haQ1Bv6gjXhFkMzwqOHkb5i5agZlfdCQYb3KpvMAbQHzkOo644aYRfa/qZwJ0d6T/6Hq0vGCE4QMEUvk3kMTwRNKiO9ygU6GxCXpFU2vPGCa33fkSQF5tekJnTdpYRQSBU8B2+BvXbMoK0QiiOU6UbuwdRvl0/MTlW3Zgg4jUhdQ== 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 Sat, Nov 28 2026 at 23:40, Thomas Gleixner wrote: > On Sat, Nov 28 2026 at 20:22, Karl Mehltretter wrote: >> On Sat, 28 Nov 2026 11:28:05 +0100, Thomas Gleixner wrote: >>> + /* >>> + * Reactivating MM CID is only required for the success case. The >>> + * exec()'ing task deactivates MM CID after bprm->point_of_no_return has >>> + * been set true, so any failure after that point will prevent the task >>> + * from going back to user space. >>> + */ >>> sched_mm_cid_after_execve(current); >> >> That doesn't hold under ptrace. >> >> bprm_execve() uses force_fatal_sig(SIGSEGV), so a tracer can suppress the >> SIGSEGV with PTRACE_CONT(pid, 0) and let the task return to user space with >> mm_cid.active == 0 and mm_cid == MM_CID_UNSET. >> >> I reproduced this on tip/sched/core (arm64 KVM, RT and !RT). A traced >> child unregisters its rseq area and calls execve() on a static ELF with >> an invalid e_entry, which fails in load_elf_binary() after >> begin_new_exec() has already run. >> >> The tracer catches SIGSEGV, sets PC to the mapped _start, and continues with >> signal 0. The child then registers a new rseq area and reads mm_cid: > > TBH, I hate ptrace and the completely made up use cases which come with it. > > I'll think about that tomorrow with brain awake, but I really want to > avoid all these corner case conditionals and the re-initialization. This > code has already grown too many warts again... I found a solution which avoids all the extra handling for a running task re-joining an in-use MM/MMCID. Though that still requires re-initialization for the stupid ptrace corner case, but that's now confined to the after execve() path. Patch below. > - execve_test case 1 (07/17): fails on baseline (0x80000000), passes on > all four configs with your patch. > - execve_test case 2 (08/17): fails on 2 vCPUs (mm_cid 1 on CPU 1). That test is modeled to test Mathiue's implementation details, which are different from what I did. My approach is to move the sched_mm_cid_before_execve() invocation _after_ de_thread() to avoid the whole 'running task rejoins an active MM/MMCID' handling mess in sched_mm_cid_fork(). That means that if execve() fails _before_ that point then nothing changes, which causes this test case to fail and according to the ad hoc trace_printk()'s I added that's the case. Avoiding all the 'running task joins active mm::mm_cid' hackery in sched_mm_cid_frok() is definitely preferred and testing for correctness of that is not rocket science either. > - 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. Thanks, tglx --- --- a/fs/exec.c +++ b/fs/exec.c @@ -1171,6 +1171,19 @@ int begin_new_exec(struct linux_binprm * */ posixtimer_exec(); + /* + * Remove the task from the MM CID management which is associated to the + * original MM. This is done here to avoid leaving and rejoining the MM + * CID management _before_ all threads of the the process have been + * terrminated. Doing it here guarantees that the MM has only the + * current task left. + * + * That means independent of a failure/success re-joining the MM MMCID + * mechanism is guaranteed to be uncontended independent whether this + * task managed to swap MMs in exec_mmap() or not. + */ + sched_mm_cid_before_execve(current); + /* see the comment in check_unsafe_exec() */ current->fs->in_exec = 0; @@ -1853,7 +1866,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 +1878,7 @@ static int bprm_execve(struct linux_binp if (retval < 0) goto out; - sched_mm_cid_after_execve(current); + sched_mm_cid_after_execve(current, true); rseq_execve(current); /* execve succeeded */ current->in_execve = 0; @@ -1885,7 +1897,8 @@ 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); + /* Indicate whether the MM has been switched already */ + sched_mm_cid_after_execve(current, !bprm->mm); rseq_force_update(); current->in_execve = 0; --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2357,7 +2357,7 @@ static __always_inline void alloc_tag_re /* Avoids recursive inclusion hell */ #ifdef CONFIG_SCHED_MM_CID void sched_mm_cid_before_execve(struct task_struct *t); -void sched_mm_cid_after_execve(struct task_struct *t); +void sched_mm_cid_after_execve(struct task_struct *t, bool new_mm); void sched_mm_cid_exit(struct task_struct *t); static __always_inline int task_mm_cid(struct task_struct *t) { @@ -2365,7 +2365,7 @@ static __always_inline int task_mm_cid(s } #else static inline void sched_mm_cid_before_execve(struct task_struct *t) { } -static inline void sched_mm_cid_after_execve(struct task_struct *t) { } +static inline void sched_mm_cid_after_execve(struct task_struct *t, bool new_mm) { } static inline void sched_mm_cid_exit(struct task_struct *t) { } static __always_inline int task_mm_cid(struct task_struct *t) { --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -11048,7 +11048,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; } @@ -11146,10 +11146,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); } } @@ -11166,17 +11168,60 @@ 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); } +static void sched_mm_cid_cleanup_execve(struct task_struct *t, struct mm_struct *mm) +{ + WARN_ON_ONCE(mm->mm_cid.mode & MM_CID_ONCPU); + mm->mm_cid.mode = 0; + + unsigned int cpu; + + for_each_possible_cpu(cpu) + 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); +} + /* Reactivate MM CID after execve() */ -void sched_mm_cid_after_execve(struct task_struct *t) +void sched_mm_cid_after_execve(struct task_struct *t, bool new_mm) { - if (t->mm) - sched_mm_cid_fork(t); + if (!t->mm) + return; + + /* + * If the MM has been swapped, nothing to do because t::mm::mm_cid is + * freshly initialized. + */ + if (unlikely(!new_mm)) { + /* + * If the task is still active then the failure happened before + * sched_mm_cid_before_execve() had been invoked. Nothing to do. + */ + if (t->mm_cid.active) + return; + /* + * The failure happend after sched_mm_cid_before_execve() and + * before swapping the MM. MM CID might be in transit mode as + * sched_mm_cid_exit() did not do a transfer because @t was the + * last user. In that rare case cleanup the stale state. + * + * Note: The only reason to do this is because PTRACE might + * override the fatal signal which would be sent due to such a + * failure. Otherwise this whole non-sensical exercise would be + * not required at all. + */ + if (unlikely(t->mm->mm_cid.mode)) + sched_mm_cid_cleanup_execve(t, t->mm); + } + + sched_mm_cid_fork(t); } static void mm_cid_work_fn(struct work_struct *work)