Skip to contentSkip to conversation

sched: Fix performance regression introduced by mm_cid

RFC PATCH Superseded Mathieu Desnoyers 3 participants 6 messages Apr 16, 2023 – Apr 17, 2023

Subsystems (3)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v7 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-rc6 with this patch applied:

("mm: Fix memory leak on mm_init error handling")

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

Fixes af7f588d8f73 ("sched: Introduce per-memory-map concurrency ID")
Cc Aaron Lu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Olivier Dion <[email protected]>

---
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      | 496 +++++++++++++++++++++++++++++++++++++--
 kernel/sched/sched.h     | 237 ++++++++++++++++---
 6 files changed, 776 insertions(+), 56 deletions(-)
include/linux/mm_types.h +74 -8
index a57e6ae78e65..5eab61156f0e 100644
@@ -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 {
@@ -600,15 +607,19 @@ struct mm_struct {
 		atomic_t mm_count;
 #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 */
@@ -873,6 +884,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)
 {
@@ -886,16 +928,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
index 63d242164b1a..41c24f406fcf 100644
@@ -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
index 2a243616f222..f20fc0600fcc 100644
@@ -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
index ea332319dffe..ff198be38a11 100644
@@ -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 +478 -18

568 lines not on this page · raw message

kernel/sched/sched.h +208 -29

269 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Mathieu,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.3-rc7]
[cannot apply to tip/sched/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathieu-Desnoyers/sched-Fix-performance-regression-introduced-by-mm_cid/20230417-063345
patch link:    https://lore.kernel.org/r/20230416223217.191261-1-mathieu.desnoyers%40efficios.com
patch subject: [RFC PATCH v7] sched: Fix performance regression introduced by mm_cid
config: riscv-randconfig-r042-20230417 (https://download.01.org/0day-ci/archive/20230417/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project 9638da200e00bd069e6dd63604e14cbafede9324)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install riscv cross compiling tool for clang build
# apt-get install binutils-riscv64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/3ab4bef6c0bd5b33395bbd475c423ba95b9763d6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Mathieu-Desnoyers/sched-Fix-performance-regression-introduced-by-mm_cid/20230417-063345 git checkout 3ab4bef6c0bd5b33395bbd475c423ba95b9763d6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=riscv olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=riscv SHELL=/bin/bash kernel/sched/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

kernel/sched/core.c:6744:35: warning: no previous prototype for function 'schedule_user' [-Wmissing-prototypes] asmlinkage __visible void __sched schedule_user(void)

                                     ^
   kernel/sched/core.c:6744:22: note: declare 'static' if the function is not intended to be used outside of this translation unit
   asmlinkage __visible void __sched schedule_user(void)
                        ^
                        static 
>> kernel/sched/core.c:11770:15: warning: variable 'next_scan' is uninitialized when used here [-Wuninitialized]
                           old_scan = next_scan;
                                      ^~~~~~~~~
   kernel/sched/core.c:11764:31: note: initialize the variable 'next_scan' to silence this warning
                   unsigned long res, next_scan;
                                               ^
                                                = 0
   kernel/sched/core.c:1689:1: warning: unused function 'uclamp_update_active' [-Wunused-function]
   uclamp_update_active(struct task_struct *p)
   ^
   kernel/sched/core.c:6448:20: warning: unused function 'sched_core_cpu_deactivate' [-Wunused-function]
   static inline void sched_core_cpu_deactivate(unsigned int cpu) {}
                      ^

4 warnings generated.

vim +/next_scan +11770 kernel/sched/core.c

11743
11744 static void task_mm_cid_work(struct callback_head *work)
11745 {
11746 struct task_struct *t = current;
11747 struct mm_struct *mm;
11748 int cpu;
11749 unsigned long now = jiffies, old_scan, next_scan;
11750 struct cpumask *cidmask;
11751 int weight;
11752
11753 SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work));
11754
11755 work->next = work; /* Prevent double-add */
11756 if (t->flags & PF_EXITING)
11757 return;
11758 mm = t->mm;
11759 if (!mm)
11760 return;
11761 old_scan = READ_ONCE(mm->mm_cid_next_scan);
11762 next_scan = now + msecs_to_jiffies(MM_CID_SCAN_DELAY);
11763 if (!old_scan) {
11764 unsigned long res, next_scan;
11765
11766 res = cmpxchg(&mm->mm_cid_next_scan, old_scan, next_scan);
11767 if (res != old_scan)
11768 old_scan = res;
11769 else
11770 old_scan = next_scan;
11771 }
11772 if (time_before(now, old_scan))
11773 return;
11774 if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan))
11775 return;
11776 cidmask = mm_cidmask(mm);
11777 /* Clear cids that were not recently used. */
11778 for_each_possible_cpu(cpu)
11779 sched_mm_cid_remote_clear_old(mm, cpu);
11780 weight = cpumask_weight(cidmask);
11781 /*
11782 * Clear cids that are greater or equal to the cidmask weight to
11783 * recompact it.
11784 */
11785 for_each_possible_cpu(cpu)
11786 sched_mm_cid_remote_clear_weight(mm, cpu, weight);
11787 }
11788

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#2 lore raw
In reply to Mathieu Desnoyers
kernel test robot noticed the following build warnings:

Hi Mathieu,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.3-rc7]
[cannot apply to tip/sched/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathieu-Desnoyers/sched-Fix-performance-regression-introduced-by-mm_cid/20230417-063345
patch link:    https://lore.kernel.org/r/20230416223217.191261-1-mathieu.desnoyers%40efficios.com
patch subject: [RFC PATCH v7] sched: Fix performance regression introduced by mm_cid
config: s390-randconfig-r024-20230417 (https://download.01.org/0day-ci/archive/20230417/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project 9638da200e00bd069e6dd63604e14cbafede9324)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install s390 cross compiling tool for clang build
# apt-get install binutils-s390x-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/3ab4bef6c0bd5b33395bbd475c423ba95b9763d6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Mathieu-Desnoyers/sched-Fix-performance-regression-introduced-by-mm_cid/20230417-063345 git checkout 3ab4bef6c0bd5b33395bbd475c423ba95b9763d6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from kernel/sched/core.c:33:
In file included from include/linux/sched/isolation.h:6:
In file included from include/linux/tick.h:8:
In file included from include/linux/clockchips.h:14:
In file included from include/linux/clocksource.h:22:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]

           val = __raw_readb(PCI_IOBASE + addr);
                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
   #define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
                                                             ^
   include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
   #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
                                                        ^
   In file included from kernel/sched/core.c:33:
   In file included from include/linux/sched/isolation.h:6:
   In file included from include/linux/tick.h:8:
   In file included from include/linux/clockchips.h:14:
   In file included from include/linux/clocksource.h:22:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
   #define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
                                                             ^
   include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
   #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
                                                        ^
   In file included from kernel/sched/core.c:33:
   In file included from include/linux/sched/isolation.h:6:
   In file included from include/linux/tick.h:8:
   In file included from include/linux/clockchips.h:14:
   In file included from include/linux/clocksource.h:22:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writeb(value, PCI_IOBASE + addr);
                               ~~~~~~~~~~ ^
   include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:692:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsb(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:700:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsw(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:708:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsl(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:717:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesb(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:726:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesw(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:735:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesl(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
>> kernel/sched/core.c:11770:15: warning: variable 'next_scan' is uninitialized when used here [-Wuninitialized]
                           old_scan = next_scan;
                                      ^~~~~~~~~
   kernel/sched/core.c:11764:31: note: initialize the variable 'next_scan' to silence this warning
                   unsigned long res, next_scan;
                                               ^
                                                = 0
   kernel/sched/core.c:6448:20: warning: unused function 'sched_core_cpu_deactivate' [-Wunused-function]
   static inline void sched_core_cpu_deactivate(unsigned int cpu) {}
                      ^

14 warnings generated.

vim +/next_scan +11770 kernel/sched/core.c

11743
11744 static void task_mm_cid_work(struct callback_head *work)
11745 {
11746 struct task_struct *t = current;
11747 struct mm_struct *mm;
11748 int cpu;
11749 unsigned long now = jiffies, old_scan, next_scan;
11750 struct cpumask *cidmask;
11751 int weight;
11752
11753 SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work));
11754
11755 work->next = work; /* Prevent double-add */
11756 if (t->flags & PF_EXITING)
11757 return;
11758 mm = t->mm;
11759 if (!mm)
11760 return;
11761 old_scan = READ_ONCE(mm->mm_cid_next_scan);
11762 next_scan = now + msecs_to_jiffies(MM_CID_SCAN_DELAY);
11763 if (!old_scan) {
11764 unsigned long res, next_scan;
11765
11766 res = cmpxchg(&mm->mm_cid_next_scan, old_scan, next_scan);
11767 if (res != old_scan)
11768 old_scan = res;
11769 else
11770 old_scan = next_scan;
11771 }
11772 if (time_before(now, old_scan))
11773 return;
11774 if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan))
11775 return;
11776 cidmask = mm_cidmask(mm);
11777 /* Clear cids that were not recently used. */
11778 for_each_possible_cpu(cpu)
11779 sched_mm_cid_remote_clear_old(mm, cpu);
11780 weight = cpumask_weight(cidmask);
11781 /*
11782 * Clear cids that are greater or equal to the cidmask weight to
11783 * recompact it.
11784 */
11785 for_each_possible_cpu(cpu)
11786 sched_mm_cid_remote_clear_weight(mm, cpu, weight);
11787 }
11788

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#3 lore raw
In reply to Mathieu Desnoyers

kernel/sched/core.c static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) · line 2089

Quoted 12 lines · Mathieu Desnoyers
kernel/sched/core.c +2 -1
index 0d18c3969f90..9532cf1a2a44 100644
@@ -2084,8 +2084,10 @@ static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
 
 void activate_task(struct rq *rq, struct task_struct *p, int flags)
 {
-	if (task_on_rq_migrating(p))
+	if (task_on_rq_migrating(p)) {
 		flags |= ENQUEUE_MIGRATED;
+		sched_mm_cid_migrate_to(rq, p);

I noticed you did this in previous version too but forgot to ask:
is it your intention to only invoke sched_mm_cid_migrate_to() for queued tasks, i.e. tasks that are being migrated due to load/idle balance etc, but not those tasks that migrated on wakeup?

I saw you did the corresonding sched_mm_cid_migrate_from() in set_task_cpu(), which on the other hand includes tasks that migrated due to wakeup, so it kind of feel weird.

kernel/sched/core.c void set_task_cpu(struct task_struct *p, unsigned int new_cpu) · line 3202

Quoted 11 lines
kernel/sched/core.c +2 -0
+	}
 
 	enqueue_task(rq, p, flags);
 
@@ -3195,6 +3197,7 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
 			p->sched_class->migrate_task_rq(p, new_cpu);
 		p->se.nr_migrations++;
 		rseq_migrate(p);
+		sched_mm_cid_migrate_from(p);
 		perf_event_task_migrate(p);
 	}

Thanks,
Aaron

#4 lore raw
Quoted 7 lines · Aaron Lu
Quoted 6 lines · Mathieu Desnoyers
kernel/sched/core.c +0 -0
index 0d18c3969f90..9532cf1a2a44 100644
@@ -2084,8 +2084,10 @@ static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
 
Quoted 12 lines

kernel/sched/core.c static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) · line 2089

Quoted 6 lines
kernel/sched/core.c +2 -1
  void activate_task(struct rq *rq, struct task_struct *p, int flags)
  {
-	if (task_on_rq_migrating(p))
+	if (task_on_rq_migrating(p)) {
  		flags |= ENQUEUE_MIGRATED;
+		sched_mm_cid_migrate_to(rq, p);

I noticed you did this in previous version too but forgot to ask:
is it your intention to only invoke sched_mm_cid_migrate_to() for queued tasks, i.e. tasks that are being migrated due to load/idle balance etc, but not those tasks that migrated on wakeup?

My intent is to also cover tasks migrated on wakeup.

Quoted 4 lines

I saw you did the corresonding sched_mm_cid_migrate_from() in set_task_cpu(), which on the other hand includes tasks that migrated due to wakeup, so it kind of feel weird.

I'm probably missing something here. AFAIU, when try_to_wake_up() moves the target process to a different cpu:

        cpu = select_task_rq(p, p->wake_cpu, wake_flags | WF_TTWU);
        if (task_cpu(p) != cpu) {

it ends up calling ttwu_queue() with wake_flags |= WF_MIGRATED bit set.

Then ttwu_queue() ends up calling ttwu_queue_wakelist(), which may initiate an IPI to the target cpu which executes sched_ttwu_pending(). This function will take the target cpu's runqueue lock and call ttwu_do_activate() with wake_flags=WF_MIGRATED.

The other path that ttwu_queue() can take is to issue ttwu_do_activate() with the target cpu's rq lock held.

ttwu_do_activate() calls activate_task() with flags having ENQUEUE_MIGRATED set.

OK I think I see what I missed here, I should change this to:

void activate_task(struct rq *rq, struct task_struct *p, int flags)

{
        if (task_on_rq_migrating(p))
                flags |= ENQUEUE_MIGRATED;
	if (flags & ENQUEUE_MIGRATED)
                sched_mm_cid_migrate_to(rq, p);

[...]

Because flags is received as input parameter as well.

Do I get your meaning correctly ?

Thanks!

Mathieu

Quoted 15 lines

kernel/sched/core.c void set_task_cpu(struct task_struct *p, unsigned int new_cpu) · line 3202

Quoted 12 lines
kernel/sched/core.c +2 -0
+	}
 
  	enqueue_task(rq, p, flags);
 
@@ -3195,6 +3197,7 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  			p->sched_class->migrate_task_rq(p, new_cpu);
  		p->se.nr_migrations++;
  		rseq_migrate(p);
+		sched_mm_cid_migrate_from(p);
  		perf_event_task_migrate(p);
  	}

Thanks,
Aaron

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#5 lore raw
Quoted 54 lines · Mathieu Desnoyers
Quoted 10 lines · Aaron Lu
Quoted 9 lines · Mathieu Desnoyers
kernel/sched/core.c +1 -1
index 0d18c3969f90..9532cf1a2a44 100644
@@ -2084,8 +2084,10 @@ static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
  void activate_task(struct rq *rq, struct task_struct *p, int flags)
  {
-	if (task_on_rq_migrating(p))
+	if (task_on_rq_migrating(p)) {
Quoted 8 lines

kernel/sched/core.c static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) · line 2089

Quoted 2 lines
kernel/sched/core.c +1 -0
  		flags |= ENQUEUE_MIGRATED;
+		sched_mm_cid_migrate_to(rq, p);

I noticed you did this in previous version too but forgot to ask:
is it your intention to only invoke sched_mm_cid_migrate_to() for queued tasks, i.e. tasks that are being migrated due to load/idle balance etc, but not those tasks that migrated on wakeup?

My intent is to also cover tasks migrated on wakeup.

Quoted 4 lines

I saw you did the corresonding sched_mm_cid_migrate_from() in set_task_cpu(), which on the other hand includes tasks that migrated due to wakeup, so it kind of feel weird.

I'm probably missing something here. AFAIU, when try_to_wake_up() moves the target process to a different cpu:

        cpu = select_task_rq(p, p->wake_cpu, wake_flags | WF_TTWU);
        if (task_cpu(p) != cpu) {

it ends up calling ttwu_queue() with wake_flags |= WF_MIGRATED bit set.

Then ttwu_queue() ends up calling ttwu_queue_wakelist(), which may initiate an IPI to the target cpu which executes sched_ttwu_pending(). This function will take the target cpu's runqueue lock and call ttwu_do_activate() with wake_flags=WF_MIGRATED.

The other path that ttwu_queue() can take is to issue ttwu_do_activate() with the target cpu's rq lock held.

ttwu_do_activate() calls activate_task() with flags having ENQUEUE_MIGRATED set.

OK I think I see what I missed here, I should change this to:

void activate_task(struct rq *rq, struct task_struct *p, int flags)

{
        if (task_on_rq_migrating(p))
                flags |= ENQUEUE_MIGRATED;
	if (flags & ENQUEUE_MIGRATED)
                sched_mm_cid_migrate_to(rq, p);
Quoted 5 lines

[...]

Because flags is received as input parameter as well.

Do I get your meaning correctly ?

Yes, that's what I meant.

Tasks that have on_rq equals to TASK_ON_RQ_MIGRATING are only those migrated by load balance etc., it doesn't apply to tasks that migrated on wake up.

I think your new change looks correct.

Thanks,
Aaron