sched: Introduce per-mm/cpu concurrency id state
Subsystems (3)
RFC PATCH 1/1 sched: Introduce per-mm/cpu concurrency id state
Keep track of the currently allocated mm_cid for each mm/cpu rather than freeing them immediately. This eliminates most atomic ops when context switching back and forth between threads belonging to different memory spaces in multi-threaded scenarios (many processes, each with many threads).
This patch is based on v6.3-rc4 with this patch applied:
("mm: Fix memory leak on mm_init error handling")
https://lore.kernel.org/lkml/[email protected]/
--- include/linux/mm_types.h | 32 ++++++++++++++++ kernel/fork.c | 7 +++- kernel/sched/core.c | 79 ++++++++++++++++++++++++++++++++++----- kernel/sched/sched.h | 81 ++++++++++++++++++++++++++++++---------- 4 files changed, 169 insertions(+), 30 deletions(-)
include/linux/mm_types.h +32 -0
@@ -609,6 +609,7 @@ struct mm_struct {* were being concurrently updated by the updaters. */ raw_spinlock_t cid_lock; +int __percpu *pcpu_cid;#endif #ifdef CONFIG_MMU atomic_long_t pgtables_bytes;/* size of all page tables */
@@ -872,6 +873,16 @@ static inline void vma_iter_init(struct vma_iterator *vmi, }#ifdef CONFIG_SCHED_MM_CID + +enum pcpu_cid_state { +PCPU_CID_UNSET =-1U , +}; + +static inline bool pcpu_cid_is_unset (int cid) +{ +return cid ==PCPU_CID_UNSET ; +} +/* Accessor for struct mm_struct's cidmask. */ static inline cpumask_t *mm_cidmask (struct mm_struct *mm) {
@@ -885,16 +896,37 @@ static inline cpumask_t *mm_cidmask(struct mm_struct *mm)static inline void mm_init_cid (struct mm_struct *mm) { +int i; +raw_spin_lock_init (&mm->cid_lock); +for_each_possible_cpu (i) + *per_cpu_ptr (mm->pcpu_cid, i) =PCPU_CID_UNSET ;cpumask_clear (mm_cidmask (mm)); } +static inline int mm_alloc_cid (struct mm_struct *mm) +{ + mm->pcpu_cid =alloc_percpu (int); +if (!mm->pcpu_cid) +return -ENOMEM ; +mm_init_cid (mm); +return 0 ; +} + +static inline void mm_destroy_cid (struct mm_struct *mm) +{ +free_percpu (mm->pcpu_cid); + mm->pcpu_cid =NULL ; +} +static inline unsigned int mm_cid_size (void ) {return cpumask_size (); }#else /* CONFIG_SCHED_MM_CID */ static inline void mm_init_cid (struct mm_struct *mm) { } +static inline int mm_alloc_cid (struct mm_struct *mm) {return 0 ; } +static inline void mm_destroy_cid (struct mm_struct *mm) { }static inline unsigned int mm_cid_size (void ) {return 0 ;
kernel/fork.c +6 -1
@@ -790,6 +790,7 @@ void __mmdrop(struct mm_struct *mm)check_mm (mm);put_user_ns (mm->user_ns);mm_pasid_drop (mm); +mm_destroy_cid (mm);for (i =0 ; i <NR_MM_COUNTERS ; i++)percpu_counter_destroy (&mm->rss_stat[i]);
@@ -1159,18 +1160,22 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,if (init_new_context (p, mm)) goto fail_nocontext; +if (mm_alloc_cid (mm)) + goto fail_cid; +for (i =0 ; i <NR_MM_COUNTERS ; i++)if (percpu_counter_init (&mm->rss_stat[i],0 ,GFP_KERNEL_ACCOUNT )) goto fail_pcpu; mm->user_ns =get_user_ns (user_ns);lru_gen_init_mm (mm); -mm_init_cid (mm);return mm; fail_pcpu:while (i >0 )percpu_counter_destroy (&mm->rss_stat[--i]); +mm_destroy_cid (mm); +fail_cid:destroy_context (mm); fail_nocontext:mm_free_pgd (mm);
kernel/sched/core.c +70 -9
131 lines not on this page · raw message
kernel/sched/sched.h +61 -20
114 lines not on this page · raw message
Signature · 1 line
2.25.1
Review
Quoted 5 lines · Mathieu Desnoyers
void
sched_mm_cid_exit_signals(struct task_struct *t){(quoted diff) +1
-1struct mm_struct *mm = t->mm; - unsigned long flags; + struct rq *rq = this_rq();
Got many below messages due to the above line:
[ 19.294089] BUG: using smp_processor_id() in preemptible [00000000] code: kworker/u449:0/1621
Quoted 8 lines
(quoted diff) +2
-1+ struct rq_flags rf; if (!mm) return; - local_irq_save(flags); + rq_lock_irqsave(rq, &rf); mm_cid_put(mm, t->mm_cid); t->mm_cid = -1;
Review
Quoted 10 lines
(quoted diff) +2
-2t->mm_cid_active = 0; - local_irq_restore(flags); + rq_unlock_irqrestore(rq, &rf); } void sched_mm_cid_before_execve(struct task_struct *t) { struct mm_struct *mm = t->mm; - unsigned long flags; + struct rq *rq = this_rq();
Also here;
Quoted 8 lines
(quoted diff) +2
-1+ struct rq_flags rf; if (!mm) return; - local_irq_save(flags); + rq_lock_irqsave(rq, &rf); mm_cid_put(mm, t->mm_cid); t->mm_cid = -1;
Review
Quoted 10 lines
(quoted diff) +2
-2t->mm_cid_active = 0; - local_irq_restore(flags); + rq_unlock_irqrestore(rq, &rf); } void sched_mm_cid_after_execve(struct task_struct *t) { struct mm_struct *mm = t->mm; - unsigned long flags; + struct rq *rq = this_rq();
And here.
Review
Quoted 12 lines
(quoted diff) +3
-2+ struct rq_flags rf; if (!mm) return; - local_irq_save(flags); + rq_lock_irqsave(rq, &rf); t->mm_cid = mm_cid_get(mm); t->mm_cid_active = 1; - local_irq_restore(flags); + rq_unlock_irqrestore(rq, &rf); rseq_set_notify_resume(t); }
I used below diff to get rid of these messages without understanding the purpose of these functions:
kernel/sched/core.c +15 -3
@@ -11444,45 +11444,57 @@ void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t, int src_cvoid sched_mm_cid_exit_signals (struct task_struct *t) {struct mm_struct *mm = t->mm; -struct rq *rq =this_rq ();struct rq_flags rf; +struct rq *rq;if (!mm)return ; + +preempt_disable (); + rq =this_rq ();rq_lock_irqsave (rq, &rf);mm_cid_put (mm, t->mm_cid); t->mm_cid =-1 ; t->mm_cid_active =0 ;rq_unlock_irqrestore (rq, &rf); +preempt_enable (); }void sched_mm_cid_before_execve (struct task_struct *t) {struct mm_struct *mm = t->mm; -struct rq *rq =this_rq ();struct rq_flags rf; +struct rq *rq;if (!mm)return ; + +preempt_disable (); + rq =this_rq ();rq_lock_irqsave (rq, &rf);mm_cid_put (mm, t->mm_cid); t->mm_cid =-1 ; t->mm_cid_active =0 ;rq_unlock_irqrestore (rq, &rf); +preempt_enable (); }void sched_mm_cid_after_execve (struct task_struct *t) {struct mm_struct *mm = t->mm; -struct rq *rq =this_rq ();struct rq_flags rf; +struct rq *rq;if (!mm)return ; + +preempt_disable (); + rq =this_rq ();rq_lock_irqsave (rq, &rf); t->mm_cid =mm_cid_get (mm); t->mm_cid_active =1 ;rq_unlock_irqrestore (rq, &rf); +preempt_enable ();rseq_set_notify_resume (t); }
Signature · 1 line
2.34.1
Quoted 56 lines · Aaron Lu
Review
Quoted 5 lines · Mathieu Desnoyers
void sched_mm_cid_exit_signals(struct task_struct *t) { struct mm_struct *mm = t->mm;(quoted diff) +1
-1- unsigned long flags; + struct rq *rq = this_rq();Got many below messages due to the above line:
[ 19.294089] BUG: using smp_processor_id() in preemptible [00000000] code: kworker/u449:0/1621Quoted 8 lines
kernel/sched/core.c +2
-1+struct rq_flags rf;if (!mm)return ; -local_irq_save (flags); +rq_lock_irqsave (rq, &rf);mm_cid_put (mm, t->mm_cid); t->mm_cid =-1 ;kernel/sched/core.c void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t, int src_c
Quoted 10 lines
kernel/sched/core.c +2
-2t->mm_cid_active =0 ; -local_irq_restore (flags); +rq_unlock_irqrestore (rq, &rf); }void sched_mm_cid_before_execve (struct task_struct *t) {struct mm_struct *mm = t->mm; -unsigned long flags; +struct rq *rq =this_rq ();Also here;
Quoted 8 lines
kernel/sched/core.c +2
-1+struct rq_flags rf;if (!mm)return ; -local_irq_save (flags); +rq_lock_irqsave (rq, &rf);mm_cid_put (mm, t->mm_cid); t->mm_cid =-1 ;kernel/sched/core.c void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t, int src_c
Quoted 10 lines
kernel/sched/core.c +2
-2t->mm_cid_active =0 ; -local_irq_restore (flags); +rq_unlock_irqrestore (rq, &rf); }void sched_mm_cid_after_execve (struct task_struct *t) {struct mm_struct *mm = t->mm; -unsigned long flags; +struct rq *rq =this_rq ();And here.
Quoted 2 lines
(quoted diff) +1
-0+ struct rq_flags rf;
Quoted 13 lines
kernel/sched/core.c void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t, int src_c · line 11499
Quoted 10 lines
if (!mm)
return;kernel/sched/core.c +2
-2-local_irq_save (flags); +rq_lock_irqsave (rq, &rf); t->mm_cid =mm_cid_get (mm); t->mm_cid_active =1 ; -local_irq_restore (flags); +rq_unlock_irqrestore (rq, &rf);rseq_set_notify_resume (t); }I used below diff to get rid of these messages without understanding the purpose of these functions:
I'll fold this fix into the next round, thanks!
Mathieu
Quoted 66 lines
kernel/sched/core.c +15
-3@@ -11444,45 +11444,57 @@ void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t, int src_cvoid sched_mm_cid_exit_signals (struct task_struct *t) {struct mm_struct *mm = t->mm; -struct rq *rq =this_rq ();struct rq_flags rf; +struct rq *rq;if (!mm)return ; + +preempt_disable (); + rq =this_rq ();rq_lock_irqsave (rq, &rf);mm_cid_put (mm, t->mm_cid); t->mm_cid =-1 ; t->mm_cid_active =0 ;rq_unlock_irqrestore (rq, &rf); +preempt_enable (); }void sched_mm_cid_before_execve (struct task_struct *t) {struct mm_struct *mm = t->mm; -struct rq *rq =this_rq ();struct rq_flags rf; +struct rq *rq;if (!mm)return ; + +preempt_disable (); + rq =this_rq ();rq_lock_irqsave (rq, &rf);mm_cid_put (mm, t->mm_cid); t->mm_cid =-1 ; t->mm_cid_active =0 ;rq_unlock_irqrestore (rq, &rf); +preempt_enable (); }void sched_mm_cid_after_execve (struct task_struct *t) {struct mm_struct *mm = t->mm; -struct rq *rq =this_rq ();struct rq_flags rf; +struct rq *rq;if (!mm)return ; + +preempt_disable (); + rq =this_rq ();rq_lock_irqsave (rq, &rf); t->mm_cid =mm_cid_get (mm); t->mm_cid_active =1 ;rq_unlock_irqrestore (rq, &rf); +preempt_enable ();rseq_set_notify_resume (t); }
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 5 lines · Mathieu Desnoyers
Keep track of the currently allocated
mm_cidfor each mm/cpu rather than freeing them immediately. This eliminates most atomic ops when context switching back and forth between threads belonging to different memory spaces in multi-threaded scenarios (many processes, each with many threads).
Good news, the lock contention is now gone and back to v6.2 level:
node0_0.profile: 0.07% 0.07% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_1.profile: 0.06% 0.06% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_2.profile: 0.09% 0.09% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_3.profile: 0.08% 0.08% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_4.profile: 0.09% 0.09% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_5.profile: 0.10% 0.10% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_6.profile: 0.10% 0.10% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_7.profile: 0.07% 0.07% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_8.profile: 0.08% 0.08% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_9.profile: 0.06% 0.06% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_0.profile: 0.41% 0.41% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_1.profile: 0.38% 0.38% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_2.profile: 0.44% 0.44% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_3.profile: 5.64% 5.64% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_4.profile: 6.08% 6.08% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_5.profile: 3.45% 3.45% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_6.profile: 2.09% 2.09% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_7.profile: 2.72% 2.72% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_8.profile: 0.16% 0.16% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_9.profile: 0.15% 0.15% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath
(those few profiles from node1's cpus that have more than 2% contention are from thermal functions)
Quoted 8 lines · Aaron Lu
Quoted 5 lines · Mathieu Desnoyers
Keep track of the currently allocated
mm_cidfor each mm/cpu rather than freeing them immediately. This eliminates most atomic ops when context switching back and forth between threads belonging to different memory spaces in multi-threaded scenarios (many processes, each with many threads).Good news, the lock contention is now gone and back to v6.2 level:
Hi Aaron,
Can you please test the updated patch I've sent ? I have updated the subject to make it clear that this is a fix for a performance regression, improved comments, and it now passes more thorough testing. See:
https://lore.kernel.org/lkml/[email protected]/
Thanks,
Mathieu
Quoted 25 lines
node0_0.profile: 0.07% 0.07% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_1.profile: 0.06% 0.06% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_2.profile: 0.09% 0.09% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_3.profile: 0.08% 0.08% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_4.profile: 0.09% 0.09% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_5.profile: 0.10% 0.10% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_6.profile: 0.10% 0.10% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_7.profile: 0.07% 0.07% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_8.profile: 0.08% 0.08% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node0_9.profile: 0.06% 0.06% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_0.profile: 0.41% 0.41% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_1.profile: 0.38% 0.38% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_2.profile: 0.44% 0.44% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_3.profile: 5.64% 5.64% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_4.profile: 6.08% 6.08% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_5.profile: 3.45% 3.45% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_6.profile: 2.09% 2.09% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_7.profile: 2.72% 2.72% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_8.profile: 0.16% 0.16% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath node1_9.profile: 0.15% 0.15% [kernel.vmlinux] [k] native_queued_spin_lock_slowpath(those few profiles from node1's cpus that have more than 2% contention are from thermal functions)
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com