sched: Fix performance regression introduced by mm_cid
Subsystems (3)
PATCH v10 1/1 sched: Fix performance regression introduced by mm_cid
Introduce per-mm/cpu current concurrency id (mm_cid) to fix a PostgreSQL sysbench regression reported by Aaron Lu.
Keep track of the currently allocated mm_cid for each mm/cpu rather than freeing them immediately on context switch. This eliminates most atomic operations when context switching back and forth between threads belonging to different memory spaces in multi-threaded scenarios (many processes, each with many threads). The per-mm/per-cpu mm_cid values are serialized by their respective runqueue locks.
Thread migration is handled by introducing invocation to sched_mm_cid_migrate_to() (with destination runqueue lock held) in activate_task() for migrating tasks. If the destination cpu's mm_cid is unset, and if the source runqueue is not actively using its mm_cid, then the source cpu's mm_cid is moved to the destination cpu on migration.
Introduce a task-work executed periodically, similarly to NUMA work, which delays reclaim of cid values when they are unused for a period of time.
Keep track of the allocation time for each per-cpu cid, and let the task work clear them when they are observed to be older than SCHED_MM_CID_PERIOD_NS and unused. This task work also clears all mm_cids which are greater or equal to the Hamming weight of the mm cidmask to keep concurrency ids compact.
Because we want to ensure the mm_cid converges towards the smaller values as migrations happen, the prior optimization that was done when context switching between threads belonging to the same mm is removed, because it could delay the lazy release of the destination runqueue mm_cid after it has been replaced by a migration. Removing this prior optimization is not an issue performance-wise because the introduced per-mm/per-cpu mm_cid tracking also covers this more specific case.
This patch is based on v6.3-rc7 with this patch applied:
("mm: Fix memory leak on mm_init error handling")
https://lore.kernel.org/lkml/[email protected]/
---
Changes since v9:
- Use argument rq rather than this_rq() in mm_cid_snapshot_time because it can be invoked from the destination (non-current) runqueue in migration.
Changes since v8:
- In order to take care of infrequently scheduled tasks, bump the time snapshot associated with old cids if an active task using the mm is observed on their associated rq.
- Eliminate this_cpu_ptr() overhead on fast-paths by using __this_cpu_read() and __this_cpu_write().
- Eliminate duplicated this_cpu_ptr() by handing the resulting pointer as argument to the callees in sched_mm_cid_migrate_to and sched_mm_cid_remote_clear.
Changes since v7:
- Call sched_mm_cid_migrate_to() for try-to-wake-up migrations as well,
- Remove next_scan bogus variable which shadows actual initialized variable. Fixed issue reported by kernel test robot,
- Clear the src_cid in sched_mm_cid_migrate_to() in situations where the dst_cid is already set to prevent over-allocation of cids in frequent migration scenarios when atomic_read(&mm->mm_users) < t->nr_cpus_allowed.
- Rebase on v6.3-rc7.
Changes since v6:
- Reintroduce migrate-to hook to hand-off cid from source to destination runqueue. This ensures that a single-threaded process always observes mm_cid==0 when migrating across CPUs.
- Ensure compactness of mm_cid in task-work by clearing mm_cid per-cpu entries that are above the Hamming weight of the mm cidmask.
Changes since v5:
- Introduce task-work and delay-based reclaim.
Changes since v4:
- Retry on filled mm_cid mask,
- Disable interrupts during ownership of cid in migrate-from without rq lock,
- Implement lock-free cid-get, using spinlock only to guarantee forward progress when contention is detected.
- Use try_cmpxchg where appropriate.
Changes since v3:
- Rebase on v6.3-rc6,
- Move switch_mm_cid() from prepare_task_switch() to context_switch() after the switch_to() barrier,
- Adding missing barriers at the beginning of switch_mm_cid(),
- Document the lock-free migrate-from algorithm.
- Add memory barriers in sched_mm_cid_exit_signals(), sched_mm_cid_before_execve(), and sched_mm_cid_after_execve() to order t->mm_cid_active store, per-mm/cpu cid load wrt migrate-from per-mm-cid cid cmpxchg, t->mm_cid_active load.
--- include/linux/mm_types.h | 82 +++++- include/linux/sched.h | 3 + include/linux/sched/mm.h | 5 + kernel/fork.c | 9 +- kernel/sched/core.c | 523 +++++++++++++++++++++++++++++++++++++-- kernel/sched/sched.h | 239 +++++++++++++++--- 6 files changed, 804 insertions(+), 57 deletions(-)
include/linux/mm_types.h +74 -8
@@ -550,6 +550,13 @@ struct vm_area_struct {struct vm_userfaultfd_ctx vm_userfaultfd_ctx; } __randomize_layout; +#ifdef CONFIG_SCHED_MM_CID +struct mm_cid { +u64 time; +int cid; +}; +#endif +struct kioctx_table ; struct mm_struct { struct {
@@ -607,15 +614,19 @@ struct mm_struct {#ifdef CONFIG_SCHED_MM_CID /** -* @cid_lock: Protect cid bitmap updates vs lookups. +* @pcpu_cid: Per-cpu current cid. * -* Prevent situations where updates to the cid bitmap happen -* concurrently with lookups. Those can lead to situations -* where a lookup cannot find a free bit simply because it was -* unlucky enough to load, non-atomically, bitmap words as they -* were being concurrently updated by the updaters. +* Keep track of the currently allocated mm_cid for each cpu. +* The per-cpu mm_cid values are serialized by their respective +* runqueue locks. */ -raw_spinlock_t cid_lock; +struct mm_cid __percpu *pcpu_cid; +/* +* @mm_cid_next_scan: Next mm_cid scan (in jiffies). +* +* When the next mm_cid scan is due (in jiffies). +*/ +unsigned long mm_cid_next_scan;#endif #ifdef CONFIG_MMU atomic_long_t pgtables_bytes;/* size of all page tables */
@@ -880,6 +891,37 @@ static inline void vma_iter_init(struct vma_iterator *vmi, }#ifdef CONFIG_SCHED_MM_CID + +enum mm_cid_state { +MM_CID_UNSET =-1U ,/* Unset state has lazy_put flag set. */ +MM_CID_LAZY_PUT = (1U <<31 ), +}; + +static inline bool mm_cid_is_unset (int cid) +{ +return cid ==MM_CID_UNSET ; +} + +static inline bool mm_cid_is_lazy_put (int cid) +{ +return !mm_cid_is_unset (cid) && (cid &MM_CID_LAZY_PUT ); +} + +static inline bool mm_cid_is_valid (int cid) +{ +return !(cid &MM_CID_LAZY_PUT ); +} + +static inline int mm_cid_set_lazy_put (int cid) +{ +return cid |MM_CID_LAZY_PUT ; +} + +static inline int mm_cid_clear_lazy_put (int cid) +{ +return cid & ~MM_CID_LAZY_PUT ; +} +/* Accessor for struct mm_struct's cidmask. */ static inline cpumask_t *mm_cidmask (struct mm_struct *mm) {
@@ -893,16 +935,40 @@ static inline cpumask_t *mm_cidmask(struct mm_struct *mm)static inline void mm_init_cid (struct mm_struct *mm) { -raw_spin_lock_init (&mm->cid_lock); +int i; + +for_each_possible_cpu (i) { +struct mm_cid *pcpu_cid =per_cpu_ptr (mm->pcpu_cid, i); + + pcpu_cid->cid =MM_CID_UNSET ; + pcpu_cid->time =0 ; + }cpumask_clear (mm_cidmask (mm)); } +static inline int mm_alloc_cid (struct mm_struct *mm) +{ + mm->pcpu_cid = alloc_percpu(struct mm_cid); +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 ;
include/linux/sched.h +3 -0
@@ -1313,7 +1313,10 @@ struct task_struct {#ifdef CONFIG_SCHED_MM_CID int mm_cid;/* Current cid in mm */ +int last_mm_cid;/* Most recent cid in mm */ +int migrate_from_cpu;int mm_cid_active;/* Whether cid bitmap is active */ +struct callback_head cid_work;#endif struct tlbflush_unmap_batch tlb_ubc;
include/linux/sched/mm.h +5 -0
@@ -37,6 +37,11 @@ static inline void mmgrab(struct mm_struct *mm)atomic_inc (&mm->mm_count); } +static inline void smp_mb__after_mmgrab (void ) +{ +smp_mb__after_atomic (); +} +extern void __mmdrop (struct mm_struct *mm);static inline void mmdrop (struct mm_struct *mm)
kernel/fork.c +8 -1
@@ -793,6 +793,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]);
@@ -1057,7 +1058,9 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)#ifdef CONFIG_SCHED_MM_CID tsk->mm_cid =-1 ; + tsk->last_mm_cid =-1 ; tsk->mm_cid_active =0 ; + tsk->migrate_from_cpu =-1 ;#endif return tsk;
@@ -1162,18 +1165,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 +506 -17
594 lines not on this page · raw message
kernel/sched/sched.h +208 -31
269 lines not on this page · raw message
Signature · 1 line
2.25.1