Skip to contentSkip to conversation

Various memory tiering fixes

PATCH Applied Zi Yan 3 participants 8 messages Jul 24, 2024 – Jul 25, 2024

Subsystems (7)
lore
Share & tools
lore

Zi Yan

author reviewer
#0 lore raw

v3 cover Various memory tiering fixes

Changelog
===

From v2:
1. Renamed folio_has_cpupid() to folio_use_access_time() and reverse the code logic, since folio_has_cpupid() = !folio_use_access_time().
2. Wrapped folio_use_access_time() with CONFIG_NUMA_BALANCING to avoid the function redefinition compilation error.

From v1:
1. Added a memory tiering runtime check in migrate_misplaced_folio() to correct PGPROMOTE_SUCCESS counting.
2. Moved folio_has_cpupid() from memory-tiers.h to mm.h.

From RFC:
1. Added Reported-by and Closes flags to first patch

Zi Yan (3):
memory tiering: read last_cpupid correctly in do_huge_pmd_numa_page() memory tiering: introduce folio_use_access_time() check memory tiering: count PGPROMOTE_SUCCESS when mem tiering is enabled.

 include/linux/mm.h  |  6 ++++++
 kernel/sched/fair.c |  3 +--
 mm/huge_memory.c    |  5 ++---
 mm/memory-tiers.c   | 19 +++++++++++++++++++
 mm/memory.c         |  3 +--
 mm/migrate.c        |  4 +++-
 mm/mprotect.c       |  3 +--
 7 files changed, 33 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v3 1/3 memory tiering: read last_cpupid correctly in do_huge_pmd_numa_page()

last_cpupid is only available when memory tiering is off or the folio is in toptier node. Complete the check to read last_cpupid when it is available.

Before the fix, the default last_cpupid will be used even if memory tiering mode is turned off at runtime instead of the actual value. This can prevent task_numa_fault() from getting right numa fault stats, but should not cause any crash. User might see performance changes after the fix.

---
 mm/huge_memory.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/huge_memory.c +2 -1
index 88dffc0023b5..15234b2e252e 100644
@@ -1707,7 +1707,8 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
 	 * For memory tiering mode, cpupid of slow memory page is used
 	 * to record page access time.  So use default value.
 	 */
-	if (node_is_toptier(nid))
+	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) ||
+	    node_is_toptier(nid))
 		last_cpupid = folio_last_cpupid(folio);
 	target_nid = numa_migrate_prep(folio, vmf, haddr, nid, &flags);
 	if (target_nid == NUMA_NO_NODE)
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH v3 2/3 memory tiering: introduce folio_use_access_time() check

In reply to Zi Yan

If memory tiering mode is on and a folio is not in the top tier memory, folio's cpupid field is repurposed to store page access time. Instead of an open coded check, use a function to encapsulate the check.

---
 include/linux/mm.h  |  6 ++++++
 kernel/sched/fair.c |  3 +--
 mm/huge_memory.c    |  6 ++----
 mm/memory-tiers.c   | 19 +++++++++++++++++++
 mm/memory.c         |  3 +--
 mm/mprotect.c       |  3 +--
 6 files changed, 30 insertions(+), 10 deletions(-)
include/linux/mm.h +6 -0
index 17753a463e01..2c6ccf088c7b 100644
@@ -1717,6 +1717,8 @@ static inline void vma_set_access_pid_bit(struct vm_area_struct *vma)
 		__set_bit(pid_bit, &vma->numab_state->pids_active[1]);
 	}
 }
+
+bool folio_use_access_time(struct folio *folio);
 #else /* !CONFIG_NUMA_BALANCING */
 static inline int folio_xchg_last_cpupid(struct folio *folio, int cpupid)
 {
@@ -1770,6 +1772,10 @@ static inline bool cpupid_match_pid(struct task_struct *task, int cpupid)
 static inline void vma_set_access_pid_bit(struct vm_area_struct *vma)
 {
 }
+static inline bool folio_use_access_time(struct folio *folio)
+{
+	return false;
+}
 #endif /* CONFIG_NUMA_BALANCING */
 
 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
kernel/sched/fair.c +1 -2
index 9057584ec06d..416e29b56cc4 100644
@@ -1840,8 +1840,7 @@ bool should_numa_migrate_memory(struct task_struct *p, struct folio *folio,
 	 * The pages in slow memory node should be migrated according
 	 * to hot/cold instead of private/shared.
 	 */
-	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
-	    !node_is_toptier(src_nid)) {
+	if (folio_use_access_time(folio)) {
 		struct pglist_data *pgdat;
 		unsigned long rate_limit;
 		unsigned int latency, th, def_th;
mm/huge_memory.c +2 -4
index 15234b2e252e..5c0a6a4e3a6e 100644
@@ -1707,8 +1707,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
 	 * For memory tiering mode, cpupid of slow memory page is used
 	 * to record page access time.  So use default value.
 	 */
-	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) ||
-	    node_is_toptier(nid))
+	if (!folio_use_access_time(folio))
 		last_cpupid = folio_last_cpupid(folio);
 	target_nid = numa_migrate_prep(folio, vmf, haddr, nid, &flags);
 	if (target_nid == NUMA_NO_NODE)
@@ -2061,8 +2060,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		    toptier)
 			goto unlock;
 
-		if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
-		    !toptier)
+		if (folio_use_access_time(folio))
 			folio_xchg_access_time(folio,
 					       jiffies_to_msecs(jiffies));
 	}
mm/memory-tiers.c +19 -0
index 4775b3a3dabe..2a642ea86cb2 100644
@@ -6,6 +6,7 @@
 #include <linux/memory.h>
 #include <linux/memory-tiers.h>
 #include <linux/notifier.h>
+#include <linux/sched/sysctl.h>
 
 #include "internal.h"
 
@@ -50,6 +51,24 @@ static const struct bus_type memory_tier_subsys = {
 	.dev_name = "memory_tier",
 };
 
+#ifdef CONFIG_NUMA_BALANCING
+/**
+ * folio_use_access_time - check if a folio reuses cpupid for page access time
+ * @folio: folio to check
+ *
+ * folio's _last_cpupid field is repurposed by memory tiering. In memory
+ * tiering mode, cpupid of slow memory folio (not toptier memory) is used to
+ * record page access time.
+ *
+ * Return: the folio _last_cpupid is used to record page access time
+ */
+bool folio_use_access_time(struct folio *folio)
+{
+	return (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
+	       !node_is_toptier(folio_nid(folio));
+}
+#endif
+
 #ifdef CONFIG_MIGRATION
 static int top_tier_adistance;
 /*
mm/memory.c +1 -2
index 802d0d8a40f9..833d2cad6eb2 100644
@@ -5337,8 +5337,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
 	 * For memory tiering mode, cpupid of slow memory page is used
 	 * to record page access time.  So use default value.
 	 */
-	if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
-	    !node_is_toptier(nid))
+	if (folio_use_access_time(folio))
 		last_cpupid = (-1 & LAST_CPUPID_MASK);
 	else
 		last_cpupid = folio_last_cpupid(folio);
mm/mprotect.c +1 -2
index 222ab434da54..37cf8d249405 100644
@@ -161,8 +161,7 @@ static long change_pte_range(struct mmu_gather *tlb,
 				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
 				    toptier)
 					continue;
-				if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
-				    !toptier)
+				if (folio_use_access_time(folio))
 					folio_xchg_access_time(folio,
 						jiffies_to_msecs(jiffies));
 			}
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v3 2/3] memory tiering: introduce folio_use_access_time() check

Quoted 8 lines · Zi Yan

If memory tiering mode is on and a folio is not in the top tier memory, folio's cpupid field is repurposed to store page access time. Instead of an open coded check, use a function to encapsulate the check.

---

Yes, that is much clearer, thanks!

Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] memory tiering: introduce folio_use_access_time() check

In reply to Zi Yan
Quoted 7 lines · Zi Yan

If memory tiering mode is on and a folio is not in the top tier memory, folio's cpupid field is repurposed to store page access time. Instead of an open coded check, use a function to encapsulate the check.

Quoted 136 lines
---
  include/linux/mm.h  |  6 ++++++
  kernel/sched/fair.c |  3 +--
  mm/huge_memory.c    |  6 ++----
  mm/memory-tiers.c   | 19 +++++++++++++++++++
  mm/memory.c         |  3 +--
  mm/mprotect.c       |  3 +--
  6 files changed, 30 insertions(+), 10 deletions(-)
include/linux/mm.h +6 -0
index 17753a463e01..2c6ccf088c7b 100644
@@ -1717,6 +1717,8 @@ static inline void vma_set_access_pid_bit(struct vm_area_struct *vma)
  		__set_bit(pid_bit, &vma->numab_state->pids_active[1]);
  	}
  }
+
+bool folio_use_access_time(struct folio *folio);
  #else /* !CONFIG_NUMA_BALANCING */
  static inline int folio_xchg_last_cpupid(struct folio *folio, int cpupid)
  {
@@ -1770,6 +1772,10 @@ static inline bool cpupid_match_pid(struct task_struct *task, int cpupid)
  static inline void vma_set_access_pid_bit(struct vm_area_struct *vma)
  {
  }
+static inline bool folio_use_access_time(struct folio *folio)
+{
+	return false;
+}
  #endif /* CONFIG_NUMA_BALANCING */
 
  #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
kernel/sched/fair.c +1 -2
index 9057584ec06d..416e29b56cc4 100644
@@ -1840,8 +1840,7 @@ bool should_numa_migrate_memory(struct task_struct *p, struct folio *folio,
  	 * The pages in slow memory node should be migrated according
  	 * to hot/cold instead of private/shared.
  	 */
-	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
-	    !node_is_toptier(src_nid)) {
+	if (folio_use_access_time(folio)) {
  		struct pglist_data *pgdat;
  		unsigned long rate_limit;
  		unsigned int latency, th, def_th;
mm/huge_memory.c +2 -4
index 15234b2e252e..5c0a6a4e3a6e 100644
@@ -1707,8 +1707,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
  	 * For memory tiering mode, cpupid of slow memory page is used
  	 * to record page access time.  So use default value.
  	 */
-	if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) ||
-	    node_is_toptier(nid))
+	if (!folio_use_access_time(folio))
  		last_cpupid = folio_last_cpupid(folio);
  	target_nid = numa_migrate_prep(folio, vmf, haddr, nid, &flags);
  	if (target_nid == NUMA_NO_NODE)
@@ -2061,8 +2060,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  		    toptier)
  			goto unlock;
 
-		if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
-		    !toptier)
+		if (folio_use_access_time(folio))
  			folio_xchg_access_time(folio,
  					       jiffies_to_msecs(jiffies));
  	}
mm/memory-tiers.c +19 -0
index 4775b3a3dabe..2a642ea86cb2 100644
@@ -6,6 +6,7 @@
  #include <linux/memory.h>
  #include <linux/memory-tiers.h>
  #include <linux/notifier.h>
+#include <linux/sched/sysctl.h>
 
  #include "internal.h"
 
@@ -50,6 +51,24 @@ static const struct bus_type memory_tier_subsys = {
  	.dev_name = "memory_tier",
  };
 
+#ifdef CONFIG_NUMA_BALANCING
+/**
+ * folio_use_access_time - check if a folio reuses cpupid for page access time
+ * @folio: folio to check
+ *
+ * folio's _last_cpupid field is repurposed by memory tiering. In memory
+ * tiering mode, cpupid of slow memory folio (not toptier memory) is used to
+ * record page access time.
+ *
+ * Return: the folio _last_cpupid is used to record page access time
+ */
+bool folio_use_access_time(struct folio *folio)
+{
+	return (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
+	       !node_is_toptier(folio_nid(folio));
+}
+#endif
+
  #ifdef CONFIG_MIGRATION
  static int top_tier_adistance;
  /*
mm/memory.c +1 -2
index 802d0d8a40f9..833d2cad6eb2 100644
@@ -5337,8 +5337,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
  	 * For memory tiering mode, cpupid of slow memory page is used
  	 * to record page access time.  So use default value.
  	 */
-	if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
-	    !node_is_toptier(nid))
+	if (folio_use_access_time(folio))
  		last_cpupid = (-1 & LAST_CPUPID_MASK);
  	else
  		last_cpupid = folio_last_cpupid(folio);
mm/mprotect.c +1 -2
index 222ab434da54..37cf8d249405 100644
@@ -161,8 +161,7 @@ static long change_pte_range(struct mmu_gather *tlb,
  				if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
  				    toptier)
  					continue;
-				if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
-				    !toptier)
+				if (folio_use_access_time(folio))
  					folio_xchg_access_time(folio,
  						jiffies_to_msecs(jiffies));
  			}
author reviewer
#5 lore raw

PATCH v3 3/3 memory tiering: count PGPROMOTE_SUCCESS when mem tiering is enabled.

In reply to Zi Yan

memory tiering can be enabled/disabled at runtime and sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING is used to check it. In migrate_misplaced_folio(), the check is missing when PGPROMOTE_SUCCESS is incremented. Add the missing check.

Fixes 33024536bafd ("memory tiering: hot page selection with hint page fault latency")
---
 mm/migrate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/migrate.c +3 -1
index e7296c0fb5d5..826afb82ca56 100644
@@ -2630,7 +2630,9 @@ int migrate_misplaced_folio(struct folio *folio, struct vm_area_struct *vma,
 		putback_movable_pages(&migratepages);
 	if (nr_succeeded) {
 		count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
-		if (!node_is_toptier(folio_nid(folio)) && node_is_toptier(node))
+		if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		    && !node_is_toptier(folio_nid(folio))
+		    && node_is_toptier(node))
 			mod_node_page_state(pgdat, PGPROMOTE_SUCCESS,
 					    nr_succeeded);
 	}
Signature · 1 line
2.43.0
Acked-by on v3 3/3
#6 lore raw

Re: [PATCH v3 3/3] memory tiering: count PGPROMOTE_SUCCESS when mem tiering is enabled.

Quoted 18 lines · Zi Yan

memory tiering can be enabled/disabled at runtime and sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING is used to check it. In migrate_misplaced_folio(), the check is missing when PGPROMOTE_SUCCESS is incremented. Add the missing check.

Fixes 33024536bafd ("memory tiering: hot page selection with hint page fault latency")
---
  mm/migrate.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
mm/migrate.c +0 -0
index e7296c0fb5d5..826afb82ca56 100644

mm/migrate.c int migrate_misplaced_folio(struct folio *folio, struct vm_area_struct *vma, · line 2638

Quoted 10 lines
mm/migrate.c +3 -1
@@ -2630,7 +2630,9 @@ int migrate_misplaced_folio(struct folio *folio, struct vm_area_struct *vma,
  		putback_movable_pages(&migratepages);
  	if (nr_succeeded) {
  		count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
-		if (!node_is_toptier(folio_nid(folio)) && node_is_toptier(node))
+		if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		    && !node_is_toptier(folio_nid(folio))
+		    && node_is_toptier(node))
  			mod_node_page_state(pgdat, PGPROMOTE_SUCCESS,
  					    nr_succeeded);
  	}
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v3 3/3
#7 lore raw

Re: [PATCH v3 3/3] memory tiering: count PGPROMOTE_SUCCESS when mem tiering is enabled.

In reply to Zi Yan
Quoted 9 lines · Zi Yan

memory tiering can be enabled/disabled at runtime and sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING is used to check it. In migrate_misplaced_folio(), the check is missing when PGPROMOTE_SUCCESS is incremented. Add the missing check.

Fixes 33024536bafd ("memory tiering: hot page selection with hint page fault latency")
Quoted 19 lines
---
  mm/migrate.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
mm/migrate.c +3 -1
index e7296c0fb5d5..826afb82ca56 100644
@@ -2630,7 +2630,9 @@ int migrate_misplaced_folio(struct folio *folio, struct vm_area_struct *vma,
  		putback_movable_pages(&migratepages);
  	if (nr_succeeded) {
  		count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
-		if (!node_is_toptier(folio_nid(folio)) && node_is_toptier(node))
+		if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		    && !node_is_toptier(folio_nid(folio))
+		    && node_is_toptier(node))
  			mod_node_page_state(pgdat, PGPROMOTE_SUCCESS,
  					    nr_succeeded);
  	}