Skip to contentSkip to conversation

sched/mmcid: Cure fork()/vfork() related problems

PATCH Applied Thomas Gleixner 5 participants 12 messages Mar 10 – Mar 11

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover sched/mmcid: Cure fork()/vfork() related problems

Matthiue and Jiri reported CPU stalls where a CPU git stuck in mm_get_cid():

https://lore.kernel.org/[email protected]

After some tedious debugging it turned out to be another subtle (or not so subtle) ownership mode change issue.

The logic handling vfork()'ed tasks in sched_mmcid_fixup_tasks_to_cpus() is broken. It is invoked when the number of tasks associated to a process is smaller than the number of MMCID users. It then walks the task list to find the vfork()'ed task, but accounts all the already processed tasks as well.

If that double processing brings the number of to be handled tasks to 0, the walk stops and the vfork()'ed task's CID is not fixed up. As a consequence a subsequent schedule in fails to acquire a (transitional) CID and the machine stalls.

Peter and me discovered also that there is a yet unreported issue vs. concurrent forks. Jiri noticed it independently.

The following series fixes those issues. It applies on top of Linus tree.

Thanks a lot to Matthieu and Jiri for providing valuable debug information and running the debug patches!

Thanks,

tglx

---
 include/linux/rseq_types.h |    6 ++-
 include/linux/sched.h      |    2 -
 kernel/fork.c              |    3 -
 kernel/sched/core.c        |   79 +++++++++++++++------------------------------
 4 files changed, 34 insertions(+), 56 deletions(-)
#1 lore raw

PATCH 1/4 sched/mmcid: Prevent CID stalls due to concurrent forks

A newly forked task is accounted as MMCID user before the task is visible in the process' thread list and the global task list. This creates the following problem:

CPU1 CPU2
fork()
sched_mm_cid_fork(tnew1)

     tnew1->mm.mm_cid_users++;
     tnew1->mm_cid.cid = getcid()
-> preemption
			fork()
			  sched_mm_cid_fork(tnew2)
   			    tnew2->mm.mm_cid_users++;
                            // Reaches the per CPU threshold
			    mm_cid_fixup_tasks_to_cpus()
			    for_each_other(current, p)
			         ....

As tnew1 is not visible yet, this fails to fix up the already allocated CID of tnew1. As a consequence a subsequent schedule in might fail to acquire a (transitional) CID and the machine stalls.

Move the invocation of sched_mm_cid_fork() after the new task becomes visible in the thread and the task list to prevent this.

This also makes it symmetrical vs. exit() where the task is removed as CID user before the task is removed from the thread and task lists.

Fixes fbd0e71dc370 ("sched/mmcid: Provide CID ownership mode fixup functions")
---
 include/linux/sched.h |    2 --
 kernel/fork.c         |    2 --
 kernel/sched/core.c   |   22 +++++++++++++++-------
 3 files changed, 15 insertions(+), 11 deletions(-)
include/linux/sched.h +0 -2
@@ -2354,7 +2354,6 @@ static __always_inline void alloc_tag_re
 #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_fork(struct task_struct *t);
 void sched_mm_cid_exit(struct task_struct *t);
 static __always_inline int task_mm_cid(struct task_struct *t)
 {
@@ -2363,7 +2362,6 @@ 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_fork(struct task_struct *t) { }
 static inline void sched_mm_cid_exit(struct task_struct *t) { }
 static __always_inline int task_mm_cid(struct task_struct *t)
 {
kernel/fork.c +0 -2
@@ -1586,7 +1586,6 @@ static int copy_mm(u64 clone_flags, stru
 
 	tsk->mm = mm;
 	tsk->active_mm = mm;
-	sched_mm_cid_fork(tsk);
 	return 0;
 }
 
@@ -2498,7 +2497,6 @@ static bool need_futex_hash_allocate_def
 	exit_nsproxy_namespaces(p);
 bad_fork_cleanup_mm:
 	if (p->mm) {
-		sched_mm_cid_exit(p);
 		mm_clear_owner(p->mm, p);
 		mmput(p->mm);
 	}
kernel/sched/core.c +15 -7
@@ -4729,8 +4729,12 @@ void sched_cancel_fork(struct task_struc
 	scx_cancel_fork(p);
 }
 
+static void sched_mm_cid_fork(struct task_struct *t);
+
 void sched_post_fork(struct task_struct *p)
 {
+	if (IS_ENABLED(CONFIG_SCHED_MM_CID))
+		sched_mm_cid_fork(p);
 	uclamp_post_fork(p);
 	scx_post_fork(p);
 }
@@ -10646,12 +10650,13 @@ static void mm_cid_do_fixup_tasks_to_cpu
 	 * possible switch back to per task mode happens either in the
 	 * deferred handler function or in the next fork()/exit().
 	 *
-	 * The caller has already transferred. The newly incoming task is
-	 * already accounted for, but not yet visible.
+	 * The caller has already transferred so remove it from the users
+	 * count. The incoming task is already visible and has mm_cid.active,
+	 * but has task::mm_cid::cid == UNSET. Still it needs to be accounted
+	 * for. Concurrent fork()s might add more threads, but all of them have
+	 * task::mm_cid::active = 0, so they don't affect the accounting here.
 	 */
-	users = mm->mm_cid.users - 2;
-	if (!users)
-		return;
+	users = mm->mm_cid.users - 1;
 
 	guard(rcu)();
 	for_other_threads(current, t) {
@@ -10688,12 +10693,15 @@ static bool sched_mm_cid_add_user(struct
 	return mm_update_max_cids(mm);
 }
 
-void sched_mm_cid_fork(struct task_struct *t)
+static void sched_mm_cid_fork(struct task_struct *t)
 {
 	struct mm_struct *mm = t->mm;
 	bool percpu;
 
-	WARN_ON_ONCE(!mm || t->mm_cid.cid != MM_CID_UNSET);
+	if (!mm)
+		return;
+
+	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) {
#5 lore raw

PATCH 2/4 sched/mmcid: Handle vfork()/CLONE_VM correctly

In reply to Thomas Gleixner

Matthieu and Jiri reported stalls where a task endlessly loops in mm_get_cid() when scheduling in.

It turned out that the logic which handles vfork()'ed tasks is broken. It is invoked when the number of tasks associated to a process is smaller than the number of MMCID users. It then walks the task list to find the vfork()'ed task, but accounts all the already processed tasks as well.

If that double processing brings the number of to be handled tasks to 0, the walk stops and the vfork()'ed task's CID is not fixed up. As a consequence a subsequent schedule in fails to acquire a (transitional) CID and the machine stalls.

Cure this by removing the accounting condition and make the fixup always walk the full task list if it could not find the exact number of users in the process' thread list.

Fixes fbd0e71dc370 ("sched/mmcid: Provide CID ownership mode fixup functions")
---
 kernel/sched/core.c |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/core.c +1 -4
@@ -10671,10 +10671,7 @@ static void mm_cid_do_fixup_tasks_to_cpu
 	for_each_process_thread(p, t) {
 		if (t == current || t->mm != mm)
 			continue;
-		if (mm_cid_fixup_task_to_cpu(t, mm)) {
-			if (--users == 0)
-				return;
-		}
+		mm_cid_fixup_task_to_cpu(t, mm);
 	}
 }
 
#7 lore raw

PATCH 3/4 sched/mmcid: Remove pointless preempt guard

In reply to Thomas Gleixner

This is a leftover from the early versions of this function where it could be invoked without mm::mm_cid::lock held.

Remove it and add lockdep asserts instead.

Fixes 653fda7ae73d ("sched/mmcid: Switch over to the new mechanism")
---
 kernel/sched/core.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/sched/core.c +7 -5
@@ -10685,6 +10685,8 @@ 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);
+
 	t->mm_cid.active = 1;
 	mm->mm_cid.users++;
 	return mm_update_max_cids(mm);
@@ -10737,12 +10739,12 @@ static void sched_mm_cid_fork(struct tas
 
 static bool sched_mm_cid_remove_user(struct task_struct *t)
 {
+	lockdep_assert_held(&t->mm->mm_cid.lock);
+
 	t->mm_cid.active = 0;
-	scoped_guard(preempt) {
-		/* Clear the transition bit */
-		t->mm_cid.cid = cid_from_transit_cid(t->mm_cid.cid);
-		mm_unset_cid_on_task(t);
-	}
+	/* Clear the transition bit */
+	t->mm_cid.cid = cid_from_transit_cid(t->mm_cid.cid);
+	mm_unset_cid_on_task(t);
 	t->mm->mm_cid.users--;
 	return mm_update_max_cids(t->mm);
 }
#9 lore raw

PATCH 4/4 sched/mmcid: Avoid full tasklist walks

In reply to Thomas Gleixner

Chasing vfork()'ed tasks on a CID ownership mode switch requires a full task list walk, which is obviously expensive on large systems.

Avoid that by keeping a list of tasks using a mm MMCID entity in mm::mm_cid and walk this list instead. This removes the proven to be flaky counting logic and avoids a full task list walk in the case of vfork()'ed tasks.

Fixes fbd0e71dc370 ("sched/mmcid: Provide CID ownership mode fixup functions")
---
 include/linux/rseq_types.h |    6 ++++-
 kernel/fork.c              |    1 
 kernel/sched/core.c        |   54 ++++++++++-----------------------------------
 3 files changed, 18 insertions(+), 43 deletions(-)
include/linux/rseq_types.h +5 -1
@@ -133,10 +133,12 @@ struct rseq_data { };
  * @active:	MM CID is active for the task
  * @cid:	The CID associated to the task either permanently or
  *		borrowed from the CPU
+ * @node:	Queued in the per MM MMCID list
  */
 struct sched_mm_cid {
 	unsigned int		active;
 	unsigned int		cid;
+	struct hlist_node	node;
 };
 
 /**
@@ -157,6 +159,7 @@ struct mm_cid_pcpu {
  * @work:		Regular work to handle the affinity mode change case
  * @lock:		Spinlock to protect against affinity setting which can't take @mutex
  * @mutex:		Mutex to serialize forks and exits related to this mm
+ * @user_list:		List of the MM CID users of a MM
  * @nr_cpus_allowed:	The number of CPUs in the per MM allowed CPUs map. The map
  *			is growth only.
  * @users:		The number of tasks sharing this MM. Separate from mm::mm_users
@@ -177,13 +180,14 @@ struct mm_mm_cid {
 
 	raw_spinlock_t		lock;
 	struct mutex		mutex;
+	struct hlist_head	user_list;
 
 	/* Low frequency modified */
 	unsigned int		nr_cpus_allowed;
 	unsigned int		users;
 	unsigned int		pcpu_thrs;
 	unsigned int		update_deferred;
-}____cacheline_aligned_in_smp;
+} ____cacheline_aligned;
 #else /* CONFIG_SCHED_MM_CID */
 struct mm_mm_cid { };
 struct sched_mm_cid { };
kernel/fork.c +1 -0
@@ -1000,6 +1000,7 @@ static struct task_struct *dup_task_stru
 #ifdef CONFIG_SCHED_MM_CID
 	tsk->mm_cid.cid = MM_CID_UNSET;
 	tsk->mm_cid.active = 0;
+	INIT_HLIST_NODE(&tsk->mm_cid.node);
 #endif
 	return tsk;
 
kernel/sched/core.c +12 -42

94 lines not on this page · raw message

#2 lore raw

Re: [patch 1/4] sched/mmcid: Prevent CID stalls due to concurrent forks

In reply to Thomas Gleixner

Thomas,

Quoted 3 lines · Thomas Gleixner

A newly forked task is accounted as MMCID user before the task is visible in the process' thread list and the global task list. This creates the following problem:

...

kernel/sched/core.c void sched_cancel_fork(struct task_struc · line 4732

Quoted 7 lines
kernel/sched/core.c +1 -0
@@ -4729,8 +4729,12 @@ void sched_cancel_fork(struct task_struc
  	scx_cancel_fork(p);
  }
 
+static void sched_mm_cid_fork(struct task_struct *t);

This introduces:

[   97s] ../kernel/sched/core.c:4711:13: warning: ‘sched_mm_cid_fork’ used but never defined
[   97s]  4711 | static void sched_mm_cid_fork(struct task_struct *t);
[   97s]       |             ^~~~~~~~~~~~~~~~~

on !CONFIG_SCHED_MM_CID configs. (gcc15-15.2.1+git10776-2.3)

thanks,

Signature · 2 lines
js
suse labs
maintainer
#3 lore raw

Re: [patch 1/4] sched/mmcid: Prevent CID stalls due to concurrent forks

Quoted 17 lines · Jiri Slaby

Thomas,

Quoted 3 lines · Thomas Gleixner

A newly forked task is accounted as MMCID user before the task is visible in the process' thread list and the global task list. This creates the following problem:

...

kernel/sched/core.c void sched_cancel_fork(struct task_struc · line 4731

Quoted 6 lines
kernel/sched/core.c +1 -0
@@ -4729,8 +4729,12 @@ void sched_cancel_fork(struct task_struc
  	scx_cancel_fork(p);
  }
+static void sched_mm_cid_fork(struct task_struct *t);

This introduces:
[ 97s] ../kernel/sched/core.c:4711:13: warning: ‘sched_mm_cid_fork’
used but never defined

Quoted 4 lines
[   97s]  4711 | static void sched_mm_cid_fork(struct task_struct *t);
[   97s]       |             ^~~~~~~~~~~~~~~~~

on !CONFIG_SCHED_MM_CID configs. (gcc15-15.2.1+git10776-2.3)

Indeed. I'll fix it before applying.

Tested-by on the v1 series
#11 lore raw
In reply to Thomas Gleixner

Hi Thomas,

Quoted 17 lines · Thomas Gleixner

Matthiue and Jiri reported CPU stalls where a CPU git stuck in mm_get_cid():

https://lore.kernel.org/[email protected]

After some tedious debugging it turned out to be another subtle (or not so subtle) ownership mode change issue.

The logic handling vfork()'ed tasks in sched_mmcid_fixup_tasks_to_cpus() is broken. It is invoked when the number of tasks associated to a process is smaller than the number of MMCID users. It then walks the task list to find the vfork()'ed task, but accounts all the already processed tasks as well.

If that double processing brings the number of to be handled tasks to 0, the walk stops and the vfork()'ed task's CID is not fixed up. As a consequence a subsequent schedule in fails to acquire a (transitional) CID and the machine stalls.

Quoted 4 lines

Peter and me discovered also that there is a yet unreported issue vs. concurrent forks. Jiri noticed it independently.

The following series fixes those issues. It applies on top of Linus tree.

Thank you for this series!

My CI also complained about the missing "#ifdef CONFIG_SCHED_MM_CID" with "make tinyconfig", but for the rest, I didn't have any issues to boot 200 times! Just in case you need this tag:

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#10 lore raw

[tip: sched/urgent] sched/mmcid: Avoid full tasklist walks

In reply to Thomas Gleixner
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     192d852129b1b7c4f0ddbab95d0de1efd5ee1405
Gitweb:        https://git.kernel.org/tip/192d852129b1b7c4f0ddbab95d0de1efd5ee1405
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 10 Mar 2026 21:29:09 +01:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 11 Mar 2026 12:01:07 +01:00

sched/mmcid: Avoid full tasklist walks

Chasing vfork()'ed tasks on a CID ownership mode switch requires a full task list walk, which is obviously expensive on large systems.

Avoid that by keeping a list of tasks using a mm MMCID entity in mm::mm_cid and walk this list instead. This removes the proven to be flaky counting logic and avoids a full task list walk in the case of vfork()'ed tasks.

Fixes fbd0e71dc370 ("sched/mmcid: Provide CID ownership mode fixup functions")
---
 include/linux/rseq_types.h |  6 +++-
 kernel/fork.c              |  1 +-
 kernel/sched/core.c        | 54 ++++++++-----------------------------
 3 files changed, 18 insertions(+), 43 deletions(-)
include/linux/rseq_types.h +5 -1
index da5fa6f..0b42045 100644
@@ -133,10 +133,12 @@ struct rseq_data { };
  * @active:	MM CID is active for the task
  * @cid:	The CID associated to the task either permanently or
  *		borrowed from the CPU
+ * @node:	Queued in the per MM MMCID list
  */
 struct sched_mm_cid {
 	unsigned int		active;
 	unsigned int		cid;
+	struct hlist_node	node;
 };
 
 /**
@@ -157,6 +159,7 @@ struct mm_cid_pcpu {
  * @work:		Regular work to handle the affinity mode change case
  * @lock:		Spinlock to protect against affinity setting which can't take @mutex
  * @mutex:		Mutex to serialize forks and exits related to this mm
+ * @user_list:		List of the MM CID users of a MM
  * @nr_cpus_allowed:	The number of CPUs in the per MM allowed CPUs map. The map
  *			is growth only.
  * @users:		The number of tasks sharing this MM. Separate from mm::mm_users
@@ -177,13 +180,14 @@ struct mm_mm_cid {
 
 	raw_spinlock_t		lock;
 	struct mutex		mutex;
+	struct hlist_head	user_list;
 
 	/* Low frequency modified */
 	unsigned int		nr_cpus_allowed;
 	unsigned int		users;
 	unsigned int		pcpu_thrs;
 	unsigned int		update_deferred;
-}____cacheline_aligned_in_smp;
+} ____cacheline_aligned;
 #else /* CONFIG_SCHED_MM_CID */
 struct mm_mm_cid { };
 struct sched_mm_cid { };
kernel/fork.c +1 -0
index 7febf4c..bc2bf58 100644
@@ -1000,6 +1000,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 #ifdef CONFIG_SCHED_MM_CID
 	tsk->mm_cid.cid = MM_CID_UNSET;
 	tsk->mm_cid.active = 0;
+	INIT_HLIST_NODE(&tsk->mm_cid.node);
 #endif
 	return tsk;
 
kernel/sched/core.c +12 -42

94 lines not on this page · raw message

#8 lore raw

[tip: sched/urgent] sched/mmcid: Remove pointless preempt guard

In reply to Thomas Gleixner
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     7574ac6e49789ddee1b1be9b2afb42b4a1b4b1f4
Gitweb:        https://git.kernel.org/tip/7574ac6e49789ddee1b1be9b2afb42b4a1b4b1f4
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 10 Mar 2026 21:29:04 +01:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 11 Mar 2026 12:01:06 +01:00

sched/mmcid: Remove pointless preempt guard

This is a leftover from the early versions of this function where it could be invoked without mm::mm_cid::lock held.

Remove it and add lockdep asserts instead.

---
 kernel/sched/core.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/sched/core.c +7 -5
index ee59cd1..f56156f 100644
@@ -10684,6 +10684,8 @@ static void mm_cid_fixup_tasks_to_cpus(void)
 
 static bool sched_mm_cid_add_user(struct task_struct *t, struct mm_struct *mm)
 {
+	lockdep_assert_held(&mm->mm_cid.lock);
+
 	t->mm_cid.active = 1;
 	mm->mm_cid.users++;
 	return mm_update_max_cids(mm);
@@ -10736,12 +10738,12 @@ static void sched_mm_cid_fork(struct task_struct *t)
 
 static bool sched_mm_cid_remove_user(struct task_struct *t)
 {
+	lockdep_assert_held(&t->mm->mm_cid.lock);
+
 	t->mm_cid.active = 0;
-	scoped_guard(preempt) {
-		/* Clear the transition bit */
-		t->mm_cid.cid = cid_from_transit_cid(t->mm_cid.cid);
-		mm_unset_cid_on_task(t);
-	}
+	/* Clear the transition bit */
+	t->mm_cid.cid = cid_from_transit_cid(t->mm_cid.cid);
+	mm_unset_cid_on_task(t);
 	t->mm->mm_cid.users--;
 	return mm_update_max_cids(t->mm);
 }
#6 lore raw

[tip: sched/urgent] sched/mmcid: Handle vfork()/CLONE_VM correctly

In reply to Thomas Gleixner
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     28b5a1395036d6c7a6c8034d85ad3d7d365f192c
Gitweb:        https://git.kernel.org/tip/28b5a1395036d6c7a6c8034d85ad3d7d365f192c
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 10 Mar 2026 21:28:58 +01:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 11 Mar 2026 12:01:06 +01:00

sched/mmcid: Handle vfork()/CLONE_VM correctly

Matthieu and Jiri reported stalls where a task endlessly loops in mm_get_cid() when scheduling in.

It turned out that the logic which handles vfork()'ed tasks is broken. It is invoked when the number of tasks associated to a process is smaller than the number of MMCID users. It then walks the task list to find the vfork()'ed task, but accounts all the already processed tasks as well.

If that double processing brings the number of to be handled tasks to 0, the walk stops and the vfork()'ed task's CID is not fixed up. As a consequence a subsequent schedule in fails to acquire a (transitional) CID and the machine stalls.

Cure this by removing the accounting condition and make the fixup always walk the full task list if it could not find the exact number of users in the process' thread list.

---
 kernel/sched/core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/core.c +1 -4
index d254278..ee59cd1 100644
@@ -10670,10 +10670,7 @@ static void mm_cid_do_fixup_tasks_to_cpus(struct mm_struct *mm)
 	for_each_process_thread(p, t) {
 		if (t == current || t->mm != mm)
 			continue;
-		if (mm_cid_fixup_task_to_cpu(t, mm)) {
-			if (--users == 0)
-				return;
-		}
+		mm_cid_fixup_task_to_cpu(t, mm);
 	}
 }
 
#4 lore raw

[tip: sched/urgent] sched/mmcid: Prevent CID stalls due to concurrent forks

In reply to Thomas Gleixner
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     b2e48c429ec54715d16fefa719dd2fbded2e65be
Gitweb:        https://git.kernel.org/tip/b2e48c429ec54715d16fefa719dd2fbded2e65be
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 10 Mar 2026 21:28:53 +01:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 11 Mar 2026 12:01:06 +01:00

sched/mmcid: Prevent CID stalls due to concurrent forks

A newly forked task is accounted as MMCID user before the task is visible in the process' thread list and the global task list. This creates the following problem:

CPU1 CPU2
fork()
sched_mm_cid_fork(tnew1)

     tnew1->mm.mm_cid_users++;
     tnew1->mm_cid.cid = getcid()
-> preemption
			fork()
			  sched_mm_cid_fork(tnew2)
			    tnew2->mm.mm_cid_users++;
                            // Reaches the per CPU threshold
			    mm_cid_fixup_tasks_to_cpus()
			    for_each_other(current, p)
			         ....

As tnew1 is not visible yet, this fails to fix up the already allocated CID of tnew1. As a consequence a subsequent schedule in might fail to acquire a (transitional) CID and the machine stalls.

Move the invocation of sched_mm_cid_fork() after the new task becomes visible in the thread and the task list to prevent this.

This also makes it symmetrical vs. exit() where the task is removed as CID user before the task is removed from the thread and task lists.

Fixes fbd0e71dc370 ("sched/mmcid: Provide CID ownership mode fixup functions")
---
 include/linux/sched.h |  2 --
 kernel/fork.c         |  2 --
 kernel/sched/core.c   | 22 +++++++++++++++-------
 3 files changed, 15 insertions(+), 11 deletions(-)
include/linux/sched.h +0 -2
index a7b4a98..5a5d3db 100644
@@ -2354,7 +2354,6 @@ static __always_inline void alloc_tag_restore(struct alloc_tag *tag, struct allo
 #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_fork(struct task_struct *t);
 void sched_mm_cid_exit(struct task_struct *t);
 static __always_inline int task_mm_cid(struct task_struct *t)
 {
@@ -2363,7 +2362,6 @@ static __always_inline int task_mm_cid(struct task_struct *t)
 #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_fork(struct task_struct *t) { }
 static inline void sched_mm_cid_exit(struct task_struct *t) { }
 static __always_inline int task_mm_cid(struct task_struct *t)
 {
kernel/fork.c +0 -2
index 65113a3..7febf4c 100644
@@ -1586,7 +1586,6 @@ static int copy_mm(u64 clone_flags, struct task_struct *tsk)
 
 	tsk->mm = mm;
 	tsk->active_mm = mm;
-	sched_mm_cid_fork(tsk);
 	return 0;
 }
 
@@ -2498,7 +2497,6 @@ bad_fork_cleanup_namespaces:
 	exit_nsproxy_namespaces(p);
 bad_fork_cleanup_mm:
 	if (p->mm) {
-		sched_mm_cid_exit(p);
 		mm_clear_owner(p->mm, p);
 		mmput(p->mm);
 	}
kernel/sched/core.c +15 -7
index b7f77c1..d254278 100644
@@ -4729,8 +4729,11 @@ void sched_cancel_fork(struct task_struct *p)
 	scx_cancel_fork(p);
 }
 
+static void sched_mm_cid_fork(struct task_struct *t);
+
 void sched_post_fork(struct task_struct *p)
 {
+	sched_mm_cid_fork(p);
 	uclamp_post_fork(p);
 	scx_post_fork(p);
 }
@@ -10646,12 +10649,13 @@ static void mm_cid_do_fixup_tasks_to_cpus(struct mm_struct *mm)
 	 * possible switch back to per task mode happens either in the
 	 * deferred handler function or in the next fork()/exit().
 	 *
-	 * The caller has already transferred. The newly incoming task is
-	 * already accounted for, but not yet visible.
+	 * The caller has already transferred so remove it from the users
+	 * count. The incoming task is already visible and has mm_cid.active,
+	 * but has task::mm_cid::cid == UNSET. Still it needs to be accounted
+	 * for. Concurrent fork()s might add more threads, but all of them have
+	 * task::mm_cid::active = 0, so they don't affect the accounting here.
 	 */
-	users = mm->mm_cid.users - 2;
-	if (!users)
-		return;
+	users = mm->mm_cid.users - 1;
 
 	guard(rcu)();
 	for_other_threads(current, t) {
@@ -10688,12 +10692,15 @@ static bool sched_mm_cid_add_user(struct task_struct *t, struct mm_struct *mm)
 	return mm_update_max_cids(mm);
 }
 
-void sched_mm_cid_fork(struct task_struct *t)
+static void sched_mm_cid_fork(struct task_struct *t)
 {
 	struct mm_struct *mm = t->mm;
 	bool percpu;
 
-	WARN_ON_ONCE(!mm || t->mm_cid.cid != MM_CID_UNSET);
+	if (!mm)
+		return;
+
+	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) {
@@ -10885,6 +10892,7 @@ void mm_init_cid(struct mm_struct *mm, struct task_struct *p)
 }
 #else /* CONFIG_SCHED_MM_CID */
 static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk) { }
+static inline void sched_mm_cid_fork(struct task_struct *t) { }
 #endif /* !CONFIG_SCHED_MM_CID */
 
 static DEFINE_PER_CPU(struct sched_change_ctx, sched_change_ctx);