Skip to contentSkip to conversation

Add a new scheme to support demotion on tiered memory system

PATCH Superseded Baolin Wang 3 participants 14 messages Dec 21, 2021 – Dec 23, 2021

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Baolin Wang

author reviewer
#0 lore raw

cover Add a new scheme to support demotion on tiered memory system

Hi,

Now on tiered memory system with different memory types, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion. So a new method from user space demoting cold pages proactively will be more helpful.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state.

This patch set introduces a new scheme named DAMOS_DEMOTE to support this feature, and works well from my testing. Any comments are welcome. Thanks.

Baolin Wang (2):
mm: Export the alloc_demote_page() function
mm/damon: Add a new scheme to support demotion on tiered memory system

 include/linux/damon.h |   3 +
 mm/damon/dbgfs.c      |   1 +
 mm/damon/vaddr.c      | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mm/internal.h         |   1 +
 mm/vmscan.c           |   2 +-
 5 files changed, 162 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.1
author reviewer
#1 lore raw

PATCH 1/2 mm: Export the alloc_demote_page() function

Export the alloc_demote_page() function to the head file as a preparation to support page demotion for DAMON monitor.

---
 mm/internal.h | 1 +
 mm/vmscan.c   | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
mm/internal.h +1 -0
index deb9bda..99ea5fb 100644
@@ -181,6 +181,7 @@ static inline void set_page_refcounted(struct page *page)
 extern int isolate_lru_page(struct page *page);
 extern void putback_lru_page(struct page *page);
 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason);
+extern struct page *alloc_demote_page(struct page *page, unsigned long node);
 
 /*
  * in mm/rmap.c:
mm/vmscan.c +1 -1
index f3162a5..bf38327 100644
@@ -1458,7 +1458,7 @@ static void page_check_dirty_writeback(struct page *page,
 		mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
 }
 
-static struct page *alloc_demote_page(struct page *page, unsigned long node)
+struct page *alloc_demote_page(struct page *page, unsigned long node)
 {
 	struct migration_target_control mtc = {
 		/*
Signature · 1 line
1.8.3.1
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm: Export the alloc_demote_page() function

Quoted 4 lines · On Tue, 21 Dec 2021 17:18:03 +0800 Baolin Wang <[email protected]>…

Export the alloc_demote_page() function to the head file as a preparation to support page demotion for DAMON monitor.

Thanks,
SJ

Quoted 32 lines
---
 mm/internal.h | 1 +
 mm/vmscan.c   | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
mm/internal.h +1 -0
index deb9bda..99ea5fb 100644
@@ -181,6 +181,7 @@ static inline void set_page_refcounted(struct page *page)
 extern int isolate_lru_page(struct page *page);
 extern void putback_lru_page(struct page *page);
 extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason);
+extern struct page *alloc_demote_page(struct page *page, unsigned long node);
 
 /*
  * in mm/rmap.c:
mm/vmscan.c +1 -1
index f3162a5..bf38327 100644
@@ -1458,7 +1458,7 @@ static void page_check_dirty_writeback(struct page *page,
 		mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
 }
 
-static struct page *alloc_demote_page(struct page *page, unsigned long node)
+struct page *alloc_demote_page(struct page *page, unsigned long node)
 {
 	struct migration_target_control mtc = {
 		/*
Signature · 1 line
1.8.3.1
author reviewer
#3 lore raw

PATCH 2/2 mm/damon: Add a new scheme to support demotion on tiered memory system

In reply to Baolin Wang

On tiered memory system, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state. Thus this patch introduces a new scheme named DAMOS_DEMOTE to support this feature.

---
 include/linux/damon.h |   3 +
 mm/damon/dbgfs.c      |   1 +
 mm/damon/vaddr.c      | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
include/linux/damon.h +3 -0
index af64838..da9957c 100644
@@ -87,6 +87,8 @@ struct damon_target {
  * @DAMOS_PAGEOUT:	Call ``madvise()`` for the region with MADV_PAGEOUT.
  * @DAMOS_HUGEPAGE:	Call ``madvise()`` for the region with MADV_HUGEPAGE.
  * @DAMOS_NOHUGEPAGE:	Call ``madvise()`` for the region with MADV_NOHUGEPAGE.
+ * @DAMOS_DEMOTE:	Migate cold pages from fast memory type (DRAM) to slow
+ * memory type (persistent memory).
  * @DAMOS_STAT:		Do nothing but count the stat.
  */
 enum damos_action {
@@ -95,6 +97,7 @@ enum damos_action {
 	DAMOS_PAGEOUT,
 	DAMOS_HUGEPAGE,
 	DAMOS_NOHUGEPAGE,
+	DAMOS_DEMOTE,
 	DAMOS_STAT,		/* Do nothing but only record the stat */
 };
 
mm/damon/dbgfs.c +1 -0
index 58dbb96..43355ab 100644
@@ -168,6 +168,7 @@ static bool damos_action_valid(int action)
 	case DAMOS_PAGEOUT:
 	case DAMOS_HUGEPAGE:
 	case DAMOS_NOHUGEPAGE:
+	case DAMOS_DEMOTE:
 	case DAMOS_STAT:
 		return true;
 	default:
mm/damon/vaddr.c +156 -0

174 lines not on this page · raw message

Signature · 1 line
1.8.3.1
maintainer
#4 lore raw

Re: [PATCH 2/2] mm/damon: Add a new scheme to support demotion on tiered memory system

Hi Baolin,

Thank you for this great patch! I left some comments below.

Quoted 24 lines · On Tue, 21 Dec 2021 17:18:04 +0800 Baolin Wang <[email protected]>…

On tiered memory system, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state. Thus this patch introduces a new scheme named DAMOS_DEMOTE to support this feature.

---
 include/linux/damon.h |   3 +
 mm/damon/dbgfs.c      |   1 +
 mm/damon/vaddr.c      | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
include/linux/damon.h +0 -0
index af64838..da9957c 100644

include/linux/damon.h struct damon_target { · line 91

Quoted 5 lines
include/linux/damon.h +2 -0
@@ -87,6 +87,8 @@ struct damon_target {
  * @DAMOS_PAGEOUT:	Call ``madvise()`` for the region with MADV_PAGEOUT.
  * @DAMOS_HUGEPAGE:	Call ``madvise()`` for the region with MADV_HUGEPAGE.
  * @DAMOS_NOHUGEPAGE:	Call ``madvise()`` for the region with MADV_NOHUGEPAGE.
+ * @DAMOS_DEMOTE:	Migate cold pages from fast memory type (DRAM) to slow
+ * memory type (persistent memory).

s/Migate/Migrate/

Also, could you make the second line of the description aligned with the first line? e.g.,

+ * @DAMOS_DEMOTE: Migate cold pages from fast memory type (DRAM) to slow
+ * memory type (persistent memory).

include/linux/damon.h enum damos_action { · line 101

Quoted 9 lines
include/linux/damon.h +1 -0
  * @DAMOS_STAT:		Do nothing but count the stat.
  */
 enum damos_action {
@@ -95,6 +97,7 @@ enum damos_action {
 	DAMOS_PAGEOUT,
 	DAMOS_HUGEPAGE,
 	DAMOS_NOHUGEPAGE,
+	DAMOS_DEMOTE,
 	DAMOS_STAT,		/* Do nothing but only record the stat */

This would make user space people who were using 'DAMOS_STAT' be confused. Could you put 'DAMOS_DEMOTE' after 'DAMOS_STAT'?

Quoted 43 lines
include/linux/damon.h +0 -0
 };
 
mm/damon/dbgfs.c +1 -0
index 58dbb96..43355ab 100644
@@ -168,6 +168,7 @@ static bool damos_action_valid(int action)
 	case DAMOS_PAGEOUT:
 	case DAMOS_HUGEPAGE:
 	case DAMOS_NOHUGEPAGE:
+	case DAMOS_DEMOTE:
 	case DAMOS_STAT:
 		return true;
 	default:
mm/damon/vaddr.c +14 -0
index 9e213a1..b354d3e 100644
@@ -14,6 +14,10 @@
 #include <linux/page_idle.h>
 #include <linux/pagewalk.h>
 #include <linux/sched/mm.h>
+#include <linux/migrate.h>
+#include <linux/mm_inline.h>
+#include <linux/swapops.h>
+#include "../internal.h"
 
 #include "prmtv-common.h"
 
@@ -693,6 +697,156 @@ static unsigned long damos_madvise(struct damon_target *target,
 }
 #endif	/* CONFIG_ADVISE_SYSCALLS */
 
+static bool damos_isolate_page(struct page *page, struct list_head *demote_list)
+{
+	struct page *head = compound_head(page);
+
+	/* Do not interfere with other mappings of this page */
+	if (page_mapcount(head) != 1)
+		return false;
+
+	/* No need migration if the target demotion node is empty. */
+	if (next_demotion_node(page_to_nid(head)) == NUMA_NO_NODE)

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 719

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+		return false;
+
+	if (isolate_lru_page(head))
+		return false;
+
+	list_add_tail(&head->lru, demote_list);
+	mod_node_page_state(page_pgdat(head),
+			    NR_ISOLATED_ANON + page_is_file_lru(head),
+			    thp_nr_pages(head));
+	return true;

The return value is not used by callers. If not needed, let's remove it.

Quoted 25 lines
mm/damon/vaddr.c +25 -0
+}
+
+static int damos_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
+				   unsigned long end, struct mm_walk *walk)
+{
+	struct vm_area_struct *vma = walk->vma;
+	struct list_head *demote_list = walk->private;
+	spinlock_t *ptl;
+	struct page *page;
+	pte_t *pte, *mapped_pte;
+
+	if (!vma_migratable(vma))
+		return -EFAULT;
+
+	ptl = pmd_trans_huge_lock(pmd, vma);
+	if (ptl) {
+		/* Bail out if THP migration is not supported. */
+		if (!thp_migration_supported())
+			goto thp_out;
+
+		/* If the THP pte is under migration, do not bother it. */
+		if (unlikely(is_pmd_migration_entry(*pmd)))
+			goto thp_out;
+
+		page = damon_get_page(pmd_pfn(*pmd));

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 754

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+		if (!page)
+			goto thp_out;
+
+		damos_isolate_page(page, demote_list);
+
+		put_page(page);
+thp_out:
+		spin_unlock(ptl);
+		return 0;
+	}

Could we wrap above THP handling code with '#ifdef CONFIG_TRANSPARENT_HUGEPAGE'?

Quoted 15 lines
mm/damon/vaddr.c +15 -0
+
+	/* regular page handling */
+	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+		return -EINVAL;
+
+	mapped_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
+	for (; addr != end; pte++, addr += PAGE_SIZE) {
+		if (pte_none(*pte) || !pte_present(*pte))
+			continue;
+
+		page = damon_get_page(pte_pfn(*pte));
+		if (!page)
+			continue;
+
+		damos_isolate_page(page, demote_list);

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 779

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+		put_page(page);
+	}
+	pte_unmap_unlock(mapped_pte, ptl);
+	cond_resched();
+
+	return 0;
+}
+
+static const struct mm_walk_ops damos_migrate_pages_walk_ops = {
+	.pmd_entry              = damos_migrate_pmd_entry,

The names are little bit confusing to me. How about 's/migrate/isolate/'?

Quoted 5 lines
mm/damon/vaddr.c +5 -0
+};
+
+/*
+ * damos_demote() - demote cold pages from fast memory to slow memory
+ * @target:    the given target

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 794

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+ * @r:         region of the target
+ *
+ * On tiered memory system, if DAMON monitored cold pages on fast memory
+ * node (DRAM), we can demote them to slow memory node proactively in case
+ * accumulating much more cold memory on fast memory node (DRAM) to reclaim.
+ *
+ * Return:
+ * = 0 means no pages were demoted.
+ * > 0 means how many cold pages were demoted successfully.
+ * < 0 means errors happened.

damon_va_apply_scheme(), which returns what this function returns when DAMOS_DEMOTE action is given, should return bytes of the region that the action is successfully applied.

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+ */
+static int damos_demote(struct damon_target *target, struct damon_region *r)
+{
+	struct mm_struct *mm;
+	LIST_HEAD(demote_pages);
+	LIST_HEAD(pagelist);
+	int target_nid, nr_pages, ret = 0;
+	unsigned int nr_succeeded, demoted_pages = 0;
+	struct page *page, *page2;
+

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 814

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+	/* Validate if allowing to do page demotion */
+	if (!numa_demotion_enabled)
+		return -EINVAL;
+
+	mm = damon_get_mm(target);
+	if (!mm)
+		return -ENOMEM;
+
+	mmap_read_lock(mm);
+	walk_page_range(mm, PAGE_ALIGN(r->ar.start), PAGE_ALIGN(r->ar.end),

I guess PAGE_ALIGN()s are not necessary here?

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 822

Quoted 8 lines
mm/damon/vaddr.c +8 -0
+			&damos_migrate_pages_walk_ops, &demote_pages);
+	mmap_read_unlock(mm);
+
+	mmput(mm);
+	if (list_empty(&demote_pages))
+		return 0;
+
+	list_for_each_entry_safe(page, page2, &demote_pages, lru) {

I'd prefer 'next' or 'next_page' instead of 'page2'.

Quoted 9 lines
mm/damon/vaddr.c +9 -0
+		list_add(&page->lru, &pagelist);
+		target_nid = next_demotion_node(page_to_nid(page));
+		nr_pages = thp_nr_pages(page);
+
+		ret = migrate_pages(&pagelist, alloc_demote_page, NULL,
+				    target_nid, MIGRATE_SYNC, MR_DEMOTION,
+				    &nr_succeeded);
+		if (ret) {
+			if (!list_empty(&pagelist)) {

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 841

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+				list_del(&page->lru);
+				mod_node_page_state(page_pgdat(page),
+						    NR_ISOLATED_ANON + page_is_file_lru(page),
+						    -nr_pages);
+				putback_lru_page(page);
+			}
+		} else {
+			__count_vm_events(PGDEMOTE_DIRECT, nr_succeeded);
+			demoted_pages += nr_succeeded;
+		}

Why should we do above work for each page on our own instead of exporting and calling 'demote_page_list()'?

Thanks,
SJ

Quoted 22 lines
mm/damon/vaddr.c +10 -0
+
+		INIT_LIST_HEAD(&pagelist);
+		cond_resched();
+	}
+
+	return demoted_pages;
+}
+
 static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx,
 		struct damon_target *t, struct damon_region *r,
 		struct damos *scheme)
@@ -715,6 +869,8 @@ static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx,
 	case DAMOS_NOHUGEPAGE:
 		madv_action = MADV_NOHUGEPAGE;
 		break;
+	case DAMOS_DEMOTE:
+		return damos_demote(t, r);
 	case DAMOS_STAT:
 		return 0;
 	default:
Signature · 1 line
1.8.3.1
author reviewer
#5 lore raw

Re: [PATCH 2/2] mm/damon: Add a new scheme to support demotion on tiered memory system

Hi SeongJae,

On 12/21/2021 9:24 PM, SeongJae Park Wrote:

Quoted 39 lines

Hi Baolin,

Thank you for this great patch! I left some comments below.

Quoted 24 lines · On Tue, 21 Dec 2021 17:18:04 +0800 Baolin Wang <[email protected]>…

On tiered memory system, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state. Thus this patch introduces a new scheme named DAMOS_DEMOTE to support this feature.

---
  include/linux/damon.h |   3 +
  mm/damon/dbgfs.c      |   1 +
  mm/damon/vaddr.c      | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 160 insertions(+)
include/linux/damon.h +0 -0
index af64838..da9957c 100644

include/linux/damon.h struct damon_target { · line 91

Quoted 5 lines
include/linux/damon.h +2 -0
@@ -87,6 +87,8 @@ struct damon_target {
   * @DAMOS_PAGEOUT:	Call ``madvise()`` for the region with MADV_PAGEOUT.
   * @DAMOS_HUGEPAGE:	Call ``madvise()`` for the region with MADV_HUGEPAGE.
   * @DAMOS_NOHUGEPAGE:	Call ``madvise()`` for the region with MADV_NOHUGEPAGE.
+ * @DAMOS_DEMOTE:	Migate cold pages from fast memory type (DRAM) to slow
+ * memory type (persistent memory).

s/Migate/Migrate/

Quoted 5 lines

Also, could you make the second line of the description aligned with the first line? e.g.,

+ * @DAMOS_DEMOTE: Migate cold pages from fast memory type (DRAM) to slow
+ * memory type (persistent memory).

Sure.

Quoted 13 lines

include/linux/damon.h enum damos_action { · line 101

Quoted 10 lines
include/linux/damon.h +1 -0
   * @DAMOS_STAT:		Do nothing but count the stat.
   */
  enum damos_action {
@@ -95,6 +97,7 @@ enum damos_action {
  	DAMOS_PAGEOUT,
  	DAMOS_HUGEPAGE,
  	DAMOS_NOHUGEPAGE,
+	DAMOS_DEMOTE,
  	DAMOS_STAT,		/* Do nothing but only record the stat */

This would make user space people who were using 'DAMOS_STAT' be confused. Could you put 'DAMOS_DEMOTE' after 'DAMOS_STAT'?

Ah, right, will do.

Quoted 48 lines
Quoted 48 lines
include/linux/damon.h +0 -0
  };
 
mm/damon/dbgfs.c +1 -0
index 58dbb96..43355ab 100644
@@ -168,6 +168,7 @@ static bool damos_action_valid(int action)
  	case DAMOS_PAGEOUT:
  	case DAMOS_HUGEPAGE:
  	case DAMOS_NOHUGEPAGE:
+	case DAMOS_DEMOTE:
  	case DAMOS_STAT:
  		return true;
  	default:
mm/damon/vaddr.c +18 -0
index 9e213a1..b354d3e 100644
@@ -14,6 +14,10 @@
  #include <linux/page_idle.h>
  #include <linux/pagewalk.h>
  #include <linux/sched/mm.h>
+#include <linux/migrate.h>
+#include <linux/mm_inline.h>
+#include <linux/swapops.h>
+#include "../internal.h"
 
  #include "prmtv-common.h"
 
@@ -693,6 +697,156 @@ static unsigned long damos_madvise(struct damon_target *target,
  }
  #endif	/* CONFIG_ADVISE_SYSCALLS */
 
+static bool damos_isolate_page(struct page *page, struct list_head *demote_list)
+{
+	struct page *head = compound_head(page);
+
+	/* Do not interfere with other mappings of this page */
+	if (page_mapcount(head) != 1)
+		return false;
+
+	/* No need migration if the target demotion node is empty. */
+	if (next_demotion_node(page_to_nid(head)) == NUMA_NO_NODE)
+		return false;
+
+	if (isolate_lru_page(head))
+		return false;
Quoted 8 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 719

Quoted 6 lines
mm/damon/vaddr.c +6 -0
+
+	list_add_tail(&head->lru, demote_list);
+	mod_node_page_state(page_pgdat(head),
+			    NR_ISOLATED_ANON + page_is_file_lru(head),
+			    thp_nr_pages(head));
+	return true;

The return value is not used by callers. If not needed, let's remove it.

Yes.

Quoted 30 lines
Quoted 30 lines
mm/damon/vaddr.c +29 -0
+}
+
+static int damos_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
+				   unsigned long end, struct mm_walk *walk)
+{
+	struct vm_area_struct *vma = walk->vma;
+	struct list_head *demote_list = walk->private;
+	spinlock_t *ptl;
+	struct page *page;
+	pte_t *pte, *mapped_pte;
+
+	if (!vma_migratable(vma))
+		return -EFAULT;
+
+	ptl = pmd_trans_huge_lock(pmd, vma);
+	if (ptl) {
+		/* Bail out if THP migration is not supported. */
+		if (!thp_migration_supported())
+			goto thp_out;
+
+		/* If the THP pte is under migration, do not bother it. */
+		if (unlikely(is_pmd_migration_entry(*pmd)))
+			goto thp_out;
+
+		page = damon_get_page(pmd_pfn(*pmd));
+		if (!page)
+			goto thp_out;
+
+		damos_isolate_page(page, demote_list);
Quoted 8 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 754

Quoted 6 lines
mm/damon/vaddr.c +6 -0
+
+		put_page(page);
+thp_out:
+		spin_unlock(ptl);
+		return 0;
+	}

Could we wrap above THP handling code with '#ifdef CONFIG_TRANSPARENT_HUGEPAGE'?

The pmd_trans_huge_lock() will return NULL if the CONFIG_TRANSPARENT_HUGEPAGE is not defined, meanwhile I think the compiler will optimize the code if the CONFIG_TRANSPARENT_HUGEPAGE is not select. Se we can use it usually instead of adding annoying "#ifdef CONFIG_XXX" in many places to handle THP, which looks simpler and more readable for me.

Quoted 20 lines
Quoted 20 lines
mm/damon/vaddr.c +19 -0
+
+	/* regular page handling */
+	if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+		return -EINVAL;
+
+	mapped_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
+	for (; addr != end; pte++, addr += PAGE_SIZE) {
+		if (pte_none(*pte) || !pte_present(*pte))
+			continue;
+
+		page = damon_get_page(pte_pfn(*pte));
+		if (!page)
+			continue;
+
+		damos_isolate_page(page, demote_list);
+		put_page(page);
+	}
+	pte_unmap_unlock(mapped_pte, ptl);
+	cond_resched();
Quoted 8 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 779

Quoted 6 lines
mm/damon/vaddr.c +6 -0
+
+	return 0;
+}
+
+static const struct mm_walk_ops damos_migrate_pages_walk_ops = {
+	.pmd_entry              = damos_migrate_pmd_entry,

The names are little bit confusing to me. How about 's/migrate/isolate/'?

Sure.

Quoted 12 lines
Quoted 12 lines
mm/damon/vaddr.c +11 -0
+};
+
+/*
+ * damos_demote() - demote cold pages from fast memory to slow memory
+ * @target:    the given target
+ * @r:         region of the target
+ *
+ * On tiered memory system, if DAMON monitored cold pages on fast memory
+ * node (DRAM), we can demote them to slow memory node proactively in case
+ * accumulating much more cold memory on fast memory node (DRAM) to reclaim.
+ *
Quoted 8 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 794

Quoted 4 lines
mm/damon/vaddr.c +4 -0
+ * Return:
+ * = 0 means no pages were demoted.
+ * > 0 means how many cold pages were demoted successfully.
+ * < 0 means errors happened.

damon_va_apply_scheme(), which returns what this function returns when DAMOS_DEMOTE action is given, should return bytes of the region that the action is successfully applied.

OK, I will change the return values as you suggested in next version.

Quoted 14 lines
Quoted 14 lines
mm/damon/vaddr.c +14 -0
+ */
+static int damos_demote(struct damon_target *target, struct damon_region *r)
+{
+	struct mm_struct *mm;
+	LIST_HEAD(demote_pages);
+	LIST_HEAD(pagelist);
+	int target_nid, nr_pages, ret = 0;
+	unsigned int nr_succeeded, demoted_pages = 0;
+	struct page *page, *page2;
+
+	/* Validate if allowing to do page demotion */
+	if (!numa_demotion_enabled)
+		return -EINVAL;
+
Quoted 8 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 814

Quoted 6 lines
mm/damon/vaddr.c +6 -0
+	mm = damon_get_mm(target);
+	if (!mm)
+		return -ENOMEM;
+
+	mmap_read_lock(mm);
+	walk_page_range(mm, PAGE_ALIGN(r->ar.start), PAGE_ALIGN(r->ar.end),

I guess PAGE_ALIGN()s are not necessary here?

Yes, I think so too, will remove it.

Quoted 11 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 822

Quoted 9 lines
mm/damon/vaddr.c +8 -0
+			&damos_migrate_pages_walk_ops, &demote_pages);
+	mmap_read_unlock(mm);
+
+	mmput(mm);
+	if (list_empty(&demote_pages))
+		return 0;
+
+	list_for_each_entry_safe(page, page2, &demote_pages, lru) {

I'd prefer 'next' or 'next_page' instead of 'page2'.

Sure.

Quoted 15 lines
Quoted 15 lines
mm/damon/vaddr.c +14 -0
+		list_add(&page->lru, &pagelist);
+		target_nid = next_demotion_node(page_to_nid(page));
+		nr_pages = thp_nr_pages(page);
+
+		ret = migrate_pages(&pagelist, alloc_demote_page, NULL,
+				    target_nid, MIGRATE_SYNC, MR_DEMOTION,
+				    &nr_succeeded);
+		if (ret) {
+			if (!list_empty(&pagelist)) {
+				list_del(&page->lru);
+				mod_node_page_state(page_pgdat(page),
+						    NR_ISOLATED_ANON + page_is_file_lru(page),
+						    -nr_pages);
+				putback_lru_page(page);
Quoted 8 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 841

Quoted 5 lines
mm/damon/vaddr.c +5 -0
+			}
+		} else {
+			__count_vm_events(PGDEMOTE_DIRECT, nr_succeeded);
+			demoted_pages += nr_succeeded;
+		}

Why should we do above work for each page on our own instead of exporting and calling 'demote_page_list()'?

Cuase demote_page_list() is used for demote cold pages which are from one same fast memory node, they can have one same target demotion node.

But for the regions for the process, we can get some cold pages from different fast memory nodes (suppose we have mulptiple dram node on the system), so its target demotion node may be different. Thus we should migrate each cold pages with getting the correct target demotion node.

Thanks for your comments.

maintainer
#6 lore raw

Re: [PATCH 2/2] mm/damon: Add a new scheme to support demotion on tiered memory system

Quoted 24 lines · On Tue, 21 Dec 2021 22:18:06 +0800 Baolin Wang <[email protected]>…

Hi SeongJae,

On 12/21/2021 9:24 PM, SeongJae Park Wrote:

Quoted 21 lines

Hi Baolin,

Thank you for this great patch! I left some comments below.

Quoted 14 lines · On Tue, 21 Dec 2021 17:18:04 +0800 Baolin Wang <[email protected]>…

On tiered memory system, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state. Thus this patch introduces a new scheme named DAMOS_DEMOTE to support this feature.

---

[...]

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

+ */

mm/damon/vaddr.c +19 -0
+static int damos_demote(struct damon_target *target, struct damon_region *r)
+{
+	struct mm_struct *mm;
+	LIST_HEAD(demote_pages);
+	LIST_HEAD(pagelist);
+	int target_nid, nr_pages, ret = 0;
+	unsigned int nr_succeeded, demoted_pages = 0;
+	struct page *page, *page2;
+
+	/* Validate if allowing to do page demotion */
+	if (!numa_demotion_enabled)
+		return -EINVAL;
+
+	mm = damon_get_mm(target);
+	if (!mm)
+		return -ENOMEM;
+
+	mmap_read_lock(mm);
+	walk_page_range(mm, PAGE_ALIGN(r->ar.start), PAGE_ALIGN(r->ar.end),

[...]

Quoted 37 lines
Quoted 10 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 822

Quoted 8 lines
mm/damon/vaddr.c +8 -0
+			&damos_migrate_pages_walk_ops, &demote_pages);
+	mmap_read_unlock(mm);
+
+	mmput(mm);
+	if (list_empty(&demote_pages))
+		return 0;
+
+	list_for_each_entry_safe(page, page2, &demote_pages, lru) {

I'd prefer 'next' or 'next_page' instead of 'page2'.

Sure.

Quoted 15 lines
Quoted 15 lines
mm/damon/vaddr.c +14 -0
+		list_add(&page->lru, &pagelist);
+		target_nid = next_demotion_node(page_to_nid(page));
+		nr_pages = thp_nr_pages(page);
+
+		ret = migrate_pages(&pagelist, alloc_demote_page, NULL,
+				    target_nid, MIGRATE_SYNC, MR_DEMOTION,
+				    &nr_succeeded);
+		if (ret) {
+			if (!list_empty(&pagelist)) {
+				list_del(&page->lru);
+				mod_node_page_state(page_pgdat(page),
+						    NR_ISOLATED_ANON + page_is_file_lru(page),
+						    -nr_pages);
+				putback_lru_page(page);
Quoted 8 lines

mm/damon/vaddr.c static unsigned long damos_madvise(struct damon_target *target, · line 841

Quoted 5 lines
mm/damon/vaddr.c +5 -0
+			}
+		} else {
+			__count_vm_events(PGDEMOTE_DIRECT, nr_succeeded);
+			demoted_pages += nr_succeeded;
+		}

Why should we do above work for each page on our own instead of exporting and calling 'demote_page_list()'?

Quoted 7 lines

Cuase demote_page_list() is used for demote cold pages which are from one same fast memory node, they can have one same target demotion node.

But for the regions for the process, we can get some cold pages from different fast memory nodes (suppose we have mulptiple dram node on the system), so its target demotion node may be different. Thus we should migrate each cold pages with getting the correct target demotion node.

Thank you for the kind explanation. But, I still want to reuse the code rather than copying if possible. How about below dumb ideas off the top of my head?

1. Call demote_page_list() for each page from here
2. Call demote_page_list() for each page from damos_migrate_pmd_entry()
3. Make damos_migrate_pages_walk_ops to configure multiple demote_pages lists, each per fast memory node, and calling demote_page_list() here for each per-fast-memory-node demote_pages list.
4. Make demote_page_list() handles this case and use it. e.g., NULL pgdat parameter means the pages are not from same node.

Please let me know if I'm missing something.

Thanks,
SJ

Quoted 2 lines

Thanks for your comments.

author reviewer
#7 lore raw

Re: [PATCH 2/2] mm/damon: Add a new scheme to support demotion on tiered memory system

Quoted 26 lines · SeongJae Park
Quoted 24 lines · On Tue, 21 Dec 2021 22:18:06 +0800 Baolin Wang <[email protected]>…

Hi SeongJae,

On 12/21/2021 9:24 PM, SeongJae Park Wrote:

Quoted 21 lines

Hi Baolin,

Thank you for this great patch! I left some comments below.

Quoted 14 lines · On Tue, 21 Dec 2021 17:18:04 +0800 Baolin Wang <[email protected]>…

On tiered memory system, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state. Thus this patch introduces a new scheme named DAMOS_DEMOTE to support this feature.

---

[snip]

Quoted 33 lines
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
(quoted diff) +14 -0
+		list_add(&page->lru, &pagelist);
+		target_nid = next_demotion_node(page_to_nid(page));
+		nr_pages = thp_nr_pages(page);
+
+		ret = migrate_pages(&pagelist, alloc_demote_page, NULL,
+				    target_nid, MIGRATE_SYNC, MR_DEMOTION,
+				    &nr_succeeded);
+		if (ret) {
+			if (!list_empty(&pagelist)) {
+				list_del(&page->lru);
+				mod_node_page_state(page_pgdat(page),
+						    NR_ISOLATED_ANON + page_is_file_lru(page),
+						    -nr_pages);
+				putback_lru_page(page);
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+			}
+		} else {
+			__count_vm_events(PGDEMOTE_DIRECT, nr_succeeded);
+			demoted_pages += nr_succeeded;
+		}

Why should we do above work for each page on our own instead of exporting and calling 'demote_page_list()'?

Quoted 7 lines

Cuase demote_page_list() is used for demote cold pages which are from one same fast memory node, they can have one same target demotion node.

But for the regions for the process, we can get some cold pages from different fast memory nodes (suppose we have mulptiple dram node on the system), so its target demotion node may be different. Thus we should migrate each cold pages with getting the correct target demotion node.

Quoted 4 lines

Thank you for the kind explanation. But, I still want to reuse the code rather than copying if possible. How about below dumb ideas off the top of my head?

1. Call demote_page_list() for each page from here

Sounds reasonable.

Quoted 1 line

2. Call demote_page_list() for each page from damos_migrate_pmd_entry()

We are under mmap lock in damos_migrate_pmd_entry(), it is not suitable to do page migration.

Quoted 3 lines

3. Make damos_migrate_pages_walk_ops to configure multiple demote_pages lists, each per fast memory node, and calling demote_page_list() here for each per-fast-memory-node demote_pages list.

Which will bring more complexity I think, and we should avoid doing migration under mmap lock.

Quoted 2 lines

4. Make demote_page_list() handles this case and use it. e.g., NULL pgdat parameter means the pages are not from same node.

Thanks for your suggestion, actually after more thinking, I think we can reuse the demote_page_list() and it can be easy to change. Somethink like below on top of my current patch, how do you think? Thanks.

mm/damon/vaddr.c +4 -9
@@ -796,7 +796,7 @@ static unsigned long damos_demote(struct damon_target *target,
        struct mm_struct *mm;
        LIST_HEAD(demote_pages);
        LIST_HEAD(pagelist);
-       int target_nid, nr_pages, ret = 0;
+       int nr_pages;
        unsigned int nr_succeeded, demoted_pages = 0;
        struct page *page, *next;
 
@@ -818,14 +818,11 @@ static unsigned long damos_demote(struct damon_target *target,
                return 0;
 
        list_for_each_entry_safe(page, next, &demote_pages, lru) {
-               list_add(&page->lru, &pagelist);
-               target_nid = next_demotion_node(page_to_nid(page));
                nr_pages = thp_nr_pages(page);
+               list_add(&page->lru, &pagelist);
 
-               ret = migrate_pages(&pagelist, alloc_demote_page, NULL,
-                                   target_nid, MIGRATE_SYNC, MR_DEMOTION,
-                                   &nr_succeeded);
-               if (ret) {
+               nr_succeeded = demote_page_list(pagelist, page_pgdat(page));
+               if (!nr_succeeded) {
                        if (!list_empty(&pagelist)) {
                                list_del(&page->lru);
                                mod_node_page_state(page_pgdat(page),
@@ -834,11 +831,9 @@ static unsigned long damos_demote(struct damon_target *target,
                                putback_lru_page(page);
                        }
                } else {
-                       __count_vm_events(PGDEMOTE_DIRECT, nr_succeeded);
                        demoted_pages += nr_succeeded;
                }
 
-               INIT_LIST_HEAD(&pagelist);
                cond_resched();
        }
 
maintainer
#8 lore raw

Re: [PATCH 2/2] mm/damon: Add a new scheme to support demotion on tiered memory system

Quoted 84 lines · On Wed, 22 Dec 2021 17:15:18 +0800 Baolin Wang <[email protected]>…
Quoted 26 lines · SeongJae Park
Quoted 24 lines · On Tue, 21 Dec 2021 22:18:06 +0800 Baolin Wang <[email protected]>…

Hi SeongJae,

On 12/21/2021 9:24 PM, SeongJae Park Wrote:

Quoted 21 lines

Hi Baolin,

Thank you for this great patch! I left some comments below.

Quoted 14 lines · On Tue, 21 Dec 2021 17:18:04 +0800 Baolin Wang <[email protected]>…

On tiered memory system, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state. Thus this patch introduces a new scheme named DAMOS_DEMOTE to support this feature.

---

[snip]

Quoted 33 lines
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
(quoted diff) +14 -0
+		list_add(&page->lru, &pagelist);
+		target_nid = next_demotion_node(page_to_nid(page));
+		nr_pages = thp_nr_pages(page);
+
+		ret = migrate_pages(&pagelist, alloc_demote_page, NULL,
+				    target_nid, MIGRATE_SYNC, MR_DEMOTION,
+				    &nr_succeeded);
+		if (ret) {
+			if (!list_empty(&pagelist)) {
+				list_del(&page->lru);
+				mod_node_page_state(page_pgdat(page),
+						    NR_ISOLATED_ANON + page_is_file_lru(page),
+						    -nr_pages);
+				putback_lru_page(page);
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+			}
+		} else {
+			__count_vm_events(PGDEMOTE_DIRECT, nr_succeeded);
+			demoted_pages += nr_succeeded;
+		}

Why should we do above work for each page on our own instead of exporting and calling 'demote_page_list()'?

Quoted 7 lines

Cuase demote_page_list() is used for demote cold pages which are from one same fast memory node, they can have one same target demotion node.

But for the regions for the process, we can get some cold pages from different fast memory nodes (suppose we have mulptiple dram node on the system), so its target demotion node may be different. Thus we should migrate each cold pages with getting the correct target demotion node.

Quoted 4 lines

Thank you for the kind explanation. But, I still want to reuse the code rather than copying if possible. How about below dumb ideas off the top of my head?

1. Call demote_page_list() for each page from here

Sounds reasonable.

Quoted 1 line

2. Call demote_page_list() for each page from damos_migrate_pmd_entry()

We are under mmap lock in damos_migrate_pmd_entry(), it is not suitable to do page migration.

Quoted 3 lines

3. Make damos_migrate_pages_walk_ops to configure multiple demote_pages lists, each per fast memory node, and calling demote_page_list() here for each per-fast-memory-node demote_pages list.

Which will bring more complexity I think, and we should avoid doing migration under mmap lock.

Quoted 6 lines
Quoted 2 lines

4. Make demote_page_list() handles this case and use it. e.g., NULL pgdat parameter means the pages are not from same node.

Thanks for your suggestion, actually after more thinking, I think we can reuse the demote_page_list() and it can be easy to change. Somethink like below on top of my current patch, how do you think? Thanks.

So, you selected the option 1. I personally think option 3 or 4 would be more optimal, but also agree that it could increase the complexity. As we already have the time/space quota feature for schemes overhead control, I think starting with this simple approach makes sense to me.

Thanks,
SJ

[...]

#9 lore raw

Re: [PATCH 2/2] mm/damon: Add a new scheme to support demotion on tiered memory system

In reply to Baolin Wang
I love your patch! Yet something to improve:

Hi Baolin,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20211220]
[cannot apply to hnaz-mm/master linux/master linus/master v5.16-rc6 v5.16-rc5 v5.16-rc4 v5.16-rc6] [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]

url:    https://github.com/0day-ci/linux/commits/Baolin-Wang/Add-a-new-scheme-to-support-demotion-on-tiered-memory-system/20211221-172017
base:    07f8c60fe60f84977dc815ec8a6b1100827c34dd
config: arc-randconfig-r043-20211222 (https://download.01.org/0day-ci/archive/20211222/202112221923.4m9tlPoy-lkp(a)intel.com/config)
compiler: arc-elf-gcc (GCC) 11.2.0

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
# https://github.com/0day-ci/linux/commit/ed1c1ea9c5b5ea81916c10e50c4a1613bce4b5a9
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Baolin-Wang/Add-a-new-scheme-to-support-demotion-on-tiered-memory-system/20211221-172017 git checkout ed1c1ea9c5b5ea81916c10e50c4a1613bce4b5a9
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arc SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

mm/damon/vaddr.c: In function 'damos_migrate_pmd_entry':

>> mm/damon/vaddr.c:635:14: error: implicit declaration of function 'vma_migratable'; did you mean 'HPageMigratable'? [-Werror=implicit-function-declaration]
     635 |         if (!vma_migratable(vma))
         |              ^~~~~~~~~~~~~~
         |              HPageMigratable
>> mm/damon/vaddr.c:648:39: error: implicit declaration of function 'pmd_pfn'; did you mean 'pmd_off'? [-Werror=implicit-function-declaration]
     648 |                 page = damon_get_page(pmd_pfn(*pmd));
         |                                       ^~~~~~~
         |                                       pmd_off

cc1: some warnings being treated as errors

vim +635 mm/damon/vaddr.c

625
626 static int damos_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
627 unsigned long end, struct mm_walk *walk)
628 {
629 struct vm_area_struct *vma = walk->vma;
630 struct list_head *demote_list = walk->private;
631 spinlock_t *ptl;
632 struct page *page;
633 pte_t *pte, *mapped_pte;
634

 > 635		if (!vma_migratable(vma))
   636			return -EFAULT;
   637	
   638		ptl = pmd_trans_huge_lock(pmd, vma);
   639		if (ptl) {
   640			/* Bail out if THP migration is not supported. */
   641			if (!thp_migration_supported())
   642				goto thp_out;
   643	
   644			/* If the THP pte is under migration, do not bother it. */
   645			if (unlikely(is_pmd_migration_entry(*pmd)))
   646				goto thp_out;
   647	
 > 648			page = damon_get_page(pmd_pfn(*pmd));

649 if (!page)
650 goto thp_out;
651
652 damos_isolate_page(page, demote_list);
653
654 put_page(page);
655 thp_out:
656 spin_unlock(ptl);
657 return 0;
658 }
659
660 /* regular page handling */
661 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
662 return -EINVAL;
663
664 mapped_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
665 for (; addr != end; pte++, addr += PAGE_SIZE) {
666 if (pte_none(*pte) || !pte_present(*pte))
667 continue;
668
669 page = damon_get_page(pte_pfn(*pte));
670 if (!page)
671 continue;
672
673 damos_isolate_page(page, demote_list);
674 put_page(page);
675 }
676 pte_unmap_unlock(mapped_pte, ptl);
677 cond_resched();
678
679 return 0;
680 }
681

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#10 lore raw
In reply to Baolin Wang

Hi Baolin,

Quoted 9 lines · On Tue, 21 Dec 2021 17:18:02 +0800 Baolin Wang <[email protected]>…

Hi,

Now on tiered memory system with different memory types, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion. So a new method from user space demoting cold pages proactively will be more helpful.

Quoted 6 lines

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state.

This patch set introduces a new scheme named DAMOS_DEMOTE to support this feature, and works well from my testing. Any comments are welcome. Thanks.

I like the idea, thank you for these patches! If possible, could you share some details about your tests?

Thanks,
SJ

Quoted 15 lines

Baolin Wang (2):
mm: Export the alloc_demote_page() function
mm/damon: Add a new scheme to support demotion on tiered memory system

 include/linux/damon.h |   3 +
 mm/damon/dbgfs.c      |   1 +
 mm/damon/vaddr.c      | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mm/internal.h         |   1 +
 mm/vmscan.c           |   2 +-
 5 files changed, 162 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.1
author reviewer
#11 lore raw
Quoted 17 lines · SeongJae Park

Hi Baolin,

Quoted 13 lines · On Tue, 21 Dec 2021 17:18:02 +0800 Baolin Wang <[email protected]>…

Hi,

Now on tiered memory system with different memory types, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion. So a new method from user space demoting cold pages proactively will be more helpful.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state.

Quoted 5 lines
Quoted 2 lines

This patch set introduces a new scheme named DAMOS_DEMOTE to support this feature, and works well from my testing. Any comments are welcome. Thanks.

I like the idea, thank you for these patches! If possible, could you share some details about your tests?

Sure, sorry for not adding more information about my tests.

My machine contains 64G DRAM + 256G AEP(persistent memory), and you should enable the demotion firstly by:
echo "true" > /sys/kernel/mm/numa/demotion_enabled

Then I just write a simple test case like below to mmap some anon memory, and then just read and write half of the mmap buffer to let another half to be cold enough to demote.

int main()

{
        int len = 50 * 1024 * 1024;
        int scan_len = len / 2;
        int i, ret, j;
        unsigned long *p;

        p = mmap(NULL, len, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (p == MAP_FAILED) {
                printf("failed to get memory\n");
                return -1;
        }

        for (i = 0; i < len / sizeof(*p); i++)
                p[i] = 0x55aa;

        /* Let another half of buffer to be cold */
        do {
                for (i = 0; i < scan_len / sizeof(*p); i++)
                        p[i] = 0x55aa;

                sleep(2);

                for (i = 0; i < scan_len / sizeof(*p); i++)
                        j +=  p[i] >> 2;
        } while (1);

        munmap(p, len);
        return 0;
}

After setting the atts/schemes/target_ids, then start monitoring:
echo 100000 1000000 1000000 10 1000 > /sys/kernel/debug/damon/attrs
echo 4096 8192000 0 5 10 2000 5 1000 2097152 5000 0 0 0 0 0 3 2 1 > /sys/kernel/debug/damon/schemes

After a while, you can check the demote statictics by below command, and you can find the demote scheme is applied by demoting some cold pages to slow memory (AEP) node.

cat /proc/vmstat | grep "demote"
pgdemote_direct 6881

maintainer
#12 lore raw
Quoted 20 lines · On Tue, 21 Dec 2021 22:32:24 +0800 Baolin Wang <[email protected]>…
Quoted 17 lines · SeongJae Park

Hi Baolin,

Quoted 13 lines · On Tue, 21 Dec 2021 17:18:02 +0800 Baolin Wang <[email protected]>…

Hi,

Now on tiered memory system with different memory types, the reclaim path in shrink_page_list() already support demoting pages to slow memory node instead of discarding the pages. However, at that time the fast memory node memory wartermark is already tense, which will increase the memory allocation latency during page demotion. So a new method from user space demoting cold pages proactively will be more helpful.

We can rely on the DAMON in user space to help to monitor the cold memory on fast memory node, and demote the cold pages to slow memory node proactively to keep the fast memory node in a healthy state.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This patch set introduces a new scheme named DAMOS_DEMOTE to support this feature, and works well from my testing. Any comments are welcome. Thanks.

I like the idea, thank you for these patches! If possible, could you share some details about your tests?

Sure, sorry for not adding more information about my tests.

No problem!

Quoted 46 lines

My machine contains 64G DRAM + 256G AEP(persistent memory), and you should enable the demotion firstly by:
echo "true" > /sys/kernel/mm/numa/demotion_enabled

Then I just write a simple test case like below to mmap some anon memory, and then just read and write half of the mmap buffer to let another half to be cold enough to demote.

int main()

{
         int len = 50 * 1024 * 1024;
         int scan_len = len / 2;
         int i, ret, j;
         unsigned long *p;

         p = mmap(NULL, len, PROT_READ | PROT_WRITE,
                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
         if (p == MAP_FAILED) {
                 printf("failed to get memory\n");
                 return -1;
         }

         for (i = 0; i < len / sizeof(*p); i++)
                 p[i] = 0x55aa;

         /* Let another half of buffer to be cold */
         do {
                 for (i = 0; i < scan_len / sizeof(*p); i++)
                         p[i] = 0x55aa;

                 sleep(2);

                 for (i = 0; i < scan_len / sizeof(*p); i++)
                         j +=  p[i] >> 2;
         } while (1);

         munmap(p, len);
         return 0;
}

After setting the atts/schemes/target_ids, then start monitoring:
echo 100000 1000000 1000000 10 1000 > /sys/kernel/debug/damon/attrs echo 4096 8192000 0 5 10 2000 5 1000 2097152 5000 0 0 0 0 0 3 2 1 > /sys/kernel/debug/damon/schemes

Quoted 6 lines

After a while, you can check the demote statictics by below command, and you can find the demote scheme is applied by demoting some cold pages to slow memory (AEP) node.

cat /proc/vmstat | grep "demote"
pgdemote_direct 6881

Thank you for sharing this great details!

I was just wondering if you have tested and measured the effects of the memory allocation latency increase during the page demotion, which invoked by shrink_page_list(), and also if you have measured how much improvement can be achieved with DAMON-based demotion in the scenario. Seems that's not the case, and I personally think that information is not essential for this patch, so I see no problem here. But, if you have tested or have a plan to do that, and if you could, I think sharing the results on this cover letter would make this even greater.

Thanks,
SJ

author reviewer
#13 lore raw

On 12/22/2021 4:54 PM, SeongJae Park wrote:
[snip]

Quoted 53 lines
Quoted 46 lines

My machine contains 64G DRAM + 256G AEP(persistent memory), and you should enable the demotion firstly by:
echo "true" > /sys/kernel/mm/numa/demotion_enabled

Then I just write a simple test case like below to mmap some anon memory, and then just read and write half of the mmap buffer to let another half to be cold enough to demote.

int main()

{
          int len = 50 * 1024 * 1024;
          int scan_len = len / 2;
          int i, ret, j;
          unsigned long *p;

          p = mmap(NULL, len, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
          if (p == MAP_FAILED) {
                  printf("failed to get memory\n");
                  return -1;
          }

          for (i = 0; i < len / sizeof(*p); i++)
                  p[i] = 0x55aa;

          /* Let another half of buffer to be cold */
          do {
                  for (i = 0; i < scan_len / sizeof(*p); i++)
                          p[i] = 0x55aa;

                  sleep(2);

                  for (i = 0; i < scan_len / sizeof(*p); i++)
                          j +=  p[i] >> 2;
          } while (1);

          munmap(p, len);
          return 0;
}

After setting the atts/schemes/target_ids, then start monitoring:
echo 100000 1000000 1000000 10 1000 > /sys/kernel/debug/damon/attrs echo 4096 8192000 0 5 10 2000 5 1000 2097152 5000 0 0 0 0 0 3 2 1 > /sys/kernel/debug/damon/schemes

Quoted 6 lines

After a while, you can check the demote statictics by below command, and you can find the demote scheme is applied by demoting some cold pages to slow memory (AEP) node.

cat /proc/vmstat | grep "demote"
pgdemote_direct 6881

Quoted 6 lines

Thank you for sharing this great details!

I was just wondering if you have tested and measured the effects of the memory allocation latency increase during the page demotion, which invoked by shrink_page_list(), and also if you have measured how much improvement can be achieved with DAMON-based demotion in the scenario. Seems that's not the case,

Not yet testing on the real workload with DAMON demote scheme now, and I think DAMON is lack of some functions to tune performance on tiered memory system. At least I think we also need add a new promotion scheme for DAMON to promote hot memory from slow memory node to the fast memory node, which is on my TODO list.

Quoted 4 lines

and I personally think that information is not essential for this patch, so I see no problem here. But, if you have tested or have a plan to do that, and if you could, I think sharing the results on this cover letter would make this even greater.

Sure, will do if we find some funny results with DAMON on tiered memory system in future. Thanks.