From: Thomas Gleixner <tglx@kernel.org>
To: Karl Mehltretter <kmehltretter@gmail.com>
Cc: Karl Mehltretter <kmehltretter@gmail.com>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
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 <dmnk@google.com>
Subject: Re: [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity
Date: Sun, 29 Nov 2026 23:24:13 +0100 [thread overview]
Message-ID: <87v75fqnsi.ffs@fw13> (raw)
In-Reply-To: <8733sjs408.ffs@fw13>
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 */
next prev parent reply other threads:[~2026-11-29 22:24 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 16:17 Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 01/17] sched/mmcid: Give a CID to a task that joins its mm while running Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
2026-11-29 21:54 ` Thomas Gleixner
2026-11-17 16:17 ` [PATCH 03/17] sched/mmcid: Reset stale state when the first user joins an mm Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 04/17] sched/mmcid: Let a running joiner take its CID after the fixups Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 05/17] sched/mmcid: Take the allowed CPUs of the first user when it joins Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 06/17] sched/mmcid: Synchronize MM teardown with affinity updates Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 07/17] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 08/17] selftests/rseq: Test the concurrency ID after the last user fails execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 09/17] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 10/17] selftests/rseq: Test the upper bound of the concurrency IDs Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 11/17] selftests/rseq: Test affinity changes made by another process Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 12/17] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 13/17] selftests/rseq: Test concurrent thread creation and forced migration Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 14/17] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 15/17] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 16/17] selftests/rseq: Test affinity changes while a task replaces or drops its mm Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 17/17] selftests/rseq: Test the last user after an affinity change during execve Mathieu Desnoyers
2026-11-28 10:28 ` [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity Thomas Gleixner
2026-11-28 19:22 ` Karl Mehltretter
2026-11-28 22:40 ` Thomas Gleixner
2026-11-29 21:48 ` Thomas Gleixner
2026-11-29 22:24 ` Thomas Gleixner [this message]
2026-11-29 22:54 ` 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=87v75fqnsi.ffs@fw13 \
--to=tglx@kernel.org \
--cc=bestswngs@gmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=dmnk@google.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=mathieu.desnoyers@efficios.com \
--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=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®