Re: [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity

From: Thomas Gleixner

Date: Sun Nov 29 2026 - 17:24:20 EST


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 */