Skip to contentSkip to conversation

Improve khugepaged scan logic

PATCH Superseded Vernon Yang 5 participants 31 messages Jan 23 – Jan 29

Subsystems (8)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover Improve khugepaged scan logic

From: Vernon Yang <[email protected]>

Hi all,

This series is improve the khugepaged scan logic, reduce CPU consumption, prioritize scanning task that access memory frequently.

The following data is traced by bpftrace[1] on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged has below phenomenon: the khugepaged list is scanned in a FIFO manner, as long as the task is not destroyed,
1. the task no longer has memory that can be collapsed into hugepage, continues scan it always.
2. the task at the front of the khugepaged scan list is cold, they are still scanned first.
3. everyone scan at intervals of khugepaged_scan_sleep_millisecs (default 10s). If we always scan the above two cases first, the valid scan will have to wait for a long time.

For the first case, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it.

For the second case, if the user has explicitly informed us via MADV_FREE that these folios will be freed, just skip it only.

The below is some performance test results.

kernbench results (testing on x86_64 machine):

baseline w/o patches test w/ patches

Amean     user-32    18522.51 (   0.00%)    18333.64 *   1.02%*
Amean     syst-32     1137.96 (   0.00%)     1113.79 *   2.12%*
Amean     elsp-32      666.04 (   0.00%)      659.44 *   0.99%*
BAmean-95 user-32    18520.01 (   0.00%)    18323.57 (   1.06%)
BAmean-95 syst-32     1137.68 (   0.00%)     1110.50 (   2.39%)
BAmean-95 elsp-32      665.92 (   0.00%)      659.06 (   1.03%)
BAmean-99 user-32    18520.01 (   0.00%)    18323.57 (   1.06%)
BAmean-99 syst-32     1137.68 (   0.00%)     1110.50 (   2.39%)
BAmean-99 elsp-32      665.92 (   0.00%)      659.06 (   1.03%)

Create three task[2]: hot1 -> cold -> hot2. After all three task are created, each allocate memory 128MB. the hot1/hot2 task continuously access 128 MB memory, while the cold task only accesses its memory briefly andthen call madvise(MADV_FREE). Here are the performance test results:
(Throughput bigger is better, other smaller is better)

Testing on x86_64 machine:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.14 sec     |  2.93 sec     | -6.69%  |
| cycles per access   |  4.96         |  2.21         | -55.44% |
| Throughput          |  104.38 M/sec |  111.89 M/sec | +7.19%  |
| dTLB-load-misses    |  284814532    |  69597236     | -75.56% |

Testing on qemu-system-x86_64 -enable-kvm:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.35 sec     |  2.96 sec     | -11.64% |
| cycles per access   |  7.29         |  2.07         | -71.60% |
| Throughput          |  97.67 M/sec  |  110.77 M/sec | +13.41% |
| dTLB-load-misses    |  241600871    |  3216108      | -98.67% |

This series is based on mm-new.

Thank you very much for your comments and discussions.

V4 -> V5:
- Patch #3 are squashed to Patch #2
- File patch utilize "xas->xa_index" to fix issue.
- folio_is_lazyfree() to folio_test_lazyfree()
- Just skip lazyfree folio simply.
- Again test kernbench in the performance mode to improve stability.
- pickup Acked-by and Reviewed-by.

V3 -> V4:
- Rebase on mm-new.
- Make Patch #2 cleaner
- Fix the lazyfree folio continue to be collapsed when skipped ahead.

V2 -> V3:
- Refine scan progress number, add folio_is_lazyfree helper
- Fix warnings at SCAN_PTE_MAPPED_HUGEPAGE.
- For MADV_FREE, we will skip the lazy-free folios instead.
- For MADV_COLD, remove it.
- Used hpage_collapse_test_exit_or_disable() instead of vma = NULL.
- pickup Reviewed-by.

V1 -> V2:
- Rename full to full_scan_finished, pickup Acked-by.
- Just skip SCAN_PMD_MAPPED/NO_PTE_TABLE memory, not remove mm.
- Set VM_NOHUGEPAGE flag when MADV_COLD/MADV_FREE to just skip, not move mm.
- Again test performance at the v6.19-rc2.

V4 : https://lore.kernel.org/linux-mm/[email protected]
V3 : https://lore.kernel.org/linux-mm/[email protected]
V2 : https://lore.kernel.org/linux-mm/[email protected]
V1 : https://lore.kernel.org/linux-mm/[email protected]

[1] https://github.com/vernon2gh/app_and_module/blob/main/khugepaged/khugepaged_mm.bt
[2] https://github.com/vernon2gh/app_and_module/blob/main/khugepaged/app.c

Vernon Yang (5):
mm: khugepaged: add trace_mm_khugepaged_scan event
mm: khugepaged: refine scan progress number
mm: add folio_test_lazyfree helper
mm: khugepaged: skip lazy-free folios
mm: khugepaged: set to next mm direct when mm has
MMF_DISABLE_THP_COMPLETELY

 include/linux/page-flags.h         |  5 +++
 include/linux/xarray.h             |  9 +++++
 include/trace/events/huge_memory.h | 26 +++++++++++++
 mm/khugepaged.c                    | 62 +++++++++++++++++++++++++-----
 mm/rmap.c                          |  2 +-
 mm/vmscan.c                        |  5 +--
 6 files changed, 95 insertions(+), 14 deletions(-)

--
2.51.0

#1 lore raw

PATCH v5 1/5 mm: khugepaged: add trace_mm_khugepaged_scan event

From: Vernon Yang <[email protected]>

Add mm_khugepaged_scan event to track the total time for full scan and the total number of pages scanned of khugepaged.

---
 include/trace/events/huge_memory.h | 25 +++++++++++++++++++++++++
 mm/khugepaged.c                    |  2 ++
 2 files changed, 27 insertions(+)
include/trace/events/huge_memory.h +25 -0
index 4e41bff31888..384e29f6bef0 100644
@@ -237,5 +237,30 @@ TRACE_EVENT(mm_khugepaged_collapse_file,
 		__print_symbolic(__entry->result, SCAN_STATUS))
 );
 
+TRACE_EVENT(mm_khugepaged_scan,
+
+	TP_PROTO(struct mm_struct *mm, unsigned int progress,
+		 bool full_scan_finished),
+
+	TP_ARGS(mm, progress, full_scan_finished),
+
+	TP_STRUCT__entry(
+		__field(struct mm_struct *, mm)
+		__field(unsigned int, progress)
+		__field(bool, full_scan_finished)
+	),
+
+	TP_fast_assign(
+		__entry->mm = mm;
+		__entry->progress = progress;
+		__entry->full_scan_finished = full_scan_finished;
+	),
+
+	TP_printk("mm=%p, progress=%u, full_scan_finished=%d",
+		__entry->mm,
+		__entry->progress,
+		__entry->full_scan_finished)
+);
+
 #endif /* __HUGE_MEMORY_H */
 #include <trace/define_trace.h>
mm/khugepaged.c +2 -0
index e76f42243534..6f0f05148765 100644
@@ -2535,6 +2535,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 		collect_mm_slot(slot);
 	}
 
+	trace_mm_khugepaged_scan(mm, progress, khugepaged_scan.mm_slot == NULL);
+
 	return progress;
 }
 
Signature · 1 line
2.51.0
reviewer Reviewed-by on v5 1/5
#2 lore raw

Re: [PATCH mm-new v5 1/5] mm: khugepaged: add trace_mm_khugepaged_scan event

Quoted 9 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Add mm_khugepaged_scan event to track the total time for full scan and the total number of pages scanned of khugepaged.

Quoted 53 lines
---
 include/trace/events/huge_memory.h | 25 +++++++++++++++++++++++++
 mm/khugepaged.c                    |  2 ++
 2 files changed, 27 insertions(+)
include/trace/events/huge_memory.h +25 -0
index 4e41bff31888..384e29f6bef0 100644
@@ -237,5 +237,30 @@ TRACE_EVENT(mm_khugepaged_collapse_file,
 		__print_symbolic(__entry->result, SCAN_STATUS))
 );
 
+TRACE_EVENT(mm_khugepaged_scan,
+
+	TP_PROTO(struct mm_struct *mm, unsigned int progress,
+		 bool full_scan_finished),
+
+	TP_ARGS(mm, progress, full_scan_finished),
+
+	TP_STRUCT__entry(
+		__field(struct mm_struct *, mm)
+		__field(unsigned int, progress)
+		__field(bool, full_scan_finished)
+	),
+
+	TP_fast_assign(
+		__entry->mm = mm;
+		__entry->progress = progress;
+		__entry->full_scan_finished = full_scan_finished;
+	),
+
+	TP_printk("mm=%p, progress=%u, full_scan_finished=%d",
+		__entry->mm,
+		__entry->progress,
+		__entry->full_scan_finished)
+);
+
 #endif /* __HUGE_MEMORY_H */
 #include <trace/define_trace.h>
mm/khugepaged.c +2 -0
index e76f42243534..6f0f05148765 100644
@@ -2535,6 +2535,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 		collect_mm_slot(slot);
 	}
 
+	trace_mm_khugepaged_scan(mm, progress, khugepaged_scan.mm_slot == NULL);
+
 	return progress;
 }
 
#3 lore raw

PATCH v5 2/5 mm: khugepaged: refine scan progress number

In reply to Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
 	xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +38 -9

119 lines not on this page · raw message

Signature · 1 line
2.51.0
reviewer
#4 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 72 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +0 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
 	xas->xa_node = XAS_RESTART;
 }

include/linux/xarray.h static inline void xas_set(struct xa_state *xas, unsigned long index) · line 1657

Quoted 10 lines
include/linux/xarray.h +9 -0
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+

Why is this needed?

Quoted 30 lines
include/linux/xarray.h +0 -0
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+		struct vm_area_struct *vma, unsigned long start_addr,
+		bool *mmap_locked, unsigned int *cur_progress,
 		struct collapse_control *cc)
 {
 	pmd_t *pmd;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1264

Quoted 6 lines
mm/khugepaged.c +3 -0
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
 	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+	if (cur_progress)
+		*cur_progress += 1;
+

1. Why do we need to do if (cur_progress). Isn't it guaranteed that the pointer will never be NULL.
2. Why do we increment this on function entry?

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1411

Quoted 13 lines
mm/khugepaged.c +6 -0
 	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
 	if (result != SCAN_SUCCEED)
 		goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 		result = SCAN_SUCCEED;
 	}
 out_unmap:
+	if (cur_progress) {
+		if (_pte >= pte + HPAGE_PMD_NR)
+			*cur_progress += HPAGE_PMD_NR - 1;
+		else
+			*cur_progress += _pte - pte;
+	}

Why are two cases required here - shouldn't it just be _pte - pte?

Quoted 89 lines
mm/khugepaged.c +23 -7
 	pte_unmap_unlock(pte, ptl);
 	if (result == SCAN_SUCCEED) {
 		result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-		struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+		unsigned long addr, struct file *file, pgoff_t start,
+		unsigned int *cur_progress, struct collapse_control *cc)
 {
 	struct folio *folio = NULL;
 	struct address_space *mapping = file->f_mapping;
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
 			cond_resched_rcu();
 		}
 	}
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;
+
+		if (folio == NULL)
+			*cur_progress += HPAGE_PMD_NR;
+		else if (xa_is_value(folio))
+			*cur_progress += idx + (1 << xas_get_order(&xas));
+		else if (folio_order(folio) == HPAGE_PMD_ORDER)
+			*cur_progress += idx + 1;
+		else
+			*cur_progress += idx + folio_nr_pages(folio);
+	}
 	rcu_read_unlock();
 
 	if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 		while (khugepaged_scan.address < hend) {
 			bool mmap_locked = true;
+			unsigned int cur_progress = 0;
 
 			cond_resched();
 			if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				mmap_read_unlock(mm);
 				mmap_locked = false;
 				*result = hpage_collapse_scan_file(mm,
-					khugepaged_scan.address, file, pgoff, cc);
+					khugepaged_scan.address, file, pgoff,
+					&cur_progress, cc);
 				fput(file);
 				if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
 					mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				}
 			} else {
 				*result = hpage_collapse_scan_pmd(mm, vma,
-					khugepaged_scan.address, &mmap_locked, cc);
+					khugepaged_scan.address, &mmap_locked,
+					&cur_progress, cc);
 			}
 
 			if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 			/* move to next address */
 			khugepaged_scan.address += HPAGE_PMD_SIZE;
-			progress += HPAGE_PMD_NR;
+			progress += cur_progress;
 			if (!mmap_locked)
 				/*
 				 * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			mmap_locked = false;
 			*lock_dropped = true;
 			result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-							  cc);
+							  NULL, cc);
 
 			if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
 			    mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			fput(file);
 		} else {
 			result = hpage_collapse_scan_pmd(mm, vma, addr,
-							 &mmap_locked, cc);
+							 &mmap_locked, NULL, cc);
 		}
 		if (!mmap_locked)
 			*lock_dropped = true;
#5 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 75 lines · Dev Jain
Quoted 73 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
      for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
           _pte++, addr += PAGE_SIZE) {
              pte_t pteval = ptep_get(_pte);
              ...
              if (pte_uffd_wp(pteval)) { <-- first scan hit
                      result = SCAN_PTE_UFFD_WP;
                      goto out_unmap;
              }
      }
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +0 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
      xas->xa_node = XAS_RESTART;
 }
 
Quoted 11 lines

include/linux/xarray.h static inline void xas_set(struct xa_state *xas, unsigned long index) · line 1657

Quoted 9 lines
include/linux/xarray.h +9 -0
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+     return xas->xa_index;
+}
+

Why is this needed?

Please refer to hpage_collapse_scan_file(), which obtains the starting address of the last folio when exiting xas_for_each().

Using "xas->xa_index" directly is also acceptable.

Quoted 31 lines
Quoted 31 lines
include/linux/xarray.h +0 -0
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-             struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+             struct vm_area_struct *vma, unsigned long start_addr,
+             bool *mmap_locked, unsigned int *cur_progress,
              struct collapse_control *cc)
 {
      pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1264

Quoted 5 lines
mm/khugepaged.c +3 -0
      VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+     if (cur_progress)
+             *cur_progress += 1;
+

1. Why do we need to do if (cur_progress). Isn't it guaranteed that the pointer will never be NULL.

cur_progress is NULL at madvise_collapse()

Quoted 1 line

2. Why do we increment this on function entry?

When the memory has been collapsed to PMD, we increment one only, same as the previous PATCH #3 [1].

[1] https://lore.kernel.org/linux-mm/[email protected]

Quoted 16 lines
Quoted 5 lines
mm/khugepaged.c +0 -0
      result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
      if (result != SCAN_SUCCEED)
              goto out;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1411

Quoted 9 lines
mm/khugepaged.c +6 -0
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
              result = SCAN_SUCCEED;
      }
 out_unmap:
+     if (cur_progress) {
+             if (_pte >= pte + HPAGE_PMD_NR)
+                     *cur_progress += HPAGE_PMD_NR - 1;
+             else
+                     *cur_progress += _pte - pte;
+     }

Why are two cases required here - shouldn't it just be _pte - pte?

At the function entry, "cur_progress += 1", If this PMD is SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, return directly. otherwise,

When "_pte == pte + HPAGE_PMD_NR" to exit loop, HPAGE_PMD_NR (also "_pte - pte") PTEs have been scanned, the "cur_progress" needs to be incremented by "HPAGE_PMD_NR - 1".

When "_pte < pte + HPAGE_PMD_NR" to break loop, "_pte - pte + 1" PTEs have been scanned, and "cur_progress" needs to be incremented by "_pte - pte".

Quoted 89 lines
Quoted 89 lines
mm/khugepaged.c +23 -7
      pte_unmap_unlock(pte, ptl);
      if (result == SCAN_SUCCEED) {
              result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
      return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-             struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+             unsigned long addr, struct file *file, pgoff_t start,
+             unsigned int *cur_progress, struct collapse_control *cc)
 {
      struct folio *folio = NULL;
      struct address_space *mapping = file->f_mapping;
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
                      cond_resched_rcu();
              }
      }
+     if (cur_progress) {
+             unsigned long idx = xas_get_index(&xas) - start;
+
+             if (folio == NULL)
+                     *cur_progress += HPAGE_PMD_NR;
+             else if (xa_is_value(folio))
+                     *cur_progress += idx + (1 << xas_get_order(&xas));
+             else if (folio_order(folio) == HPAGE_PMD_ORDER)
+                     *cur_progress += idx + 1;
+             else
+                     *cur_progress += idx + folio_nr_pages(folio);
+     }
      rcu_read_unlock();
 
      if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
              while (khugepaged_scan.address < hend) {
                      bool mmap_locked = true;
+                     unsigned int cur_progress = 0;
 
                      cond_resched();
                      if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
                              mmap_read_unlock(mm);
                              mmap_locked = false;
                              *result = hpage_collapse_scan_file(mm,
-                                     khugepaged_scan.address, file, pgoff, cc);
+                                     khugepaged_scan.address, file, pgoff,
+                                     &cur_progress, cc);
                              fput(file);
                              if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
                                      mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
                              }
                      } else {
                              *result = hpage_collapse_scan_pmd(mm, vma,
-                                     khugepaged_scan.address, &mmap_locked, cc);
+                                     khugepaged_scan.address, &mmap_locked,
+                                     &cur_progress, cc);
                      }
 
                      if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
                      /* move to next address */
                      khugepaged_scan.address += HPAGE_PMD_SIZE;
-                     progress += HPAGE_PMD_NR;
+                     progress += cur_progress;
                      if (!mmap_locked)
                              /*
                               * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                      mmap_locked = false;
                      *lock_dropped = true;
                      result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-                                                       cc);
+                                                       NULL, cc);
 
                      if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
                          mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                      fput(file);
              } else {
                      result = hpage_collapse_scan_pmd(mm, vma, addr,
-                                                      &mmap_locked, cc);
+                                                      &mmap_locked, NULL, cc);
              }
              if (!mmap_locked)
                      *lock_dropped = true;
maintainer
#6 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

In reply to Vernon Yang

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2394

Quoted 2 lines · Vernon Yang
mm/khugepaged.c +2 -0
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;

I agree with Dev. This is less readable than:

		unsigned long idx = xas.xa_index - start;
#7 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 8 lines · Matthew Wilcox

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2394

Quoted 2 lines · Vernon Yang
mm/khugepaged.c +2 -0
+     if (cur_progress) {
+             unsigned long idx = xas_get_index(&xas) - start;

I agree with Dev. This is less readable than:

                unsigned long idx = xas.xa_index - start;

LGTM, I will use "xas->xa_index" directly in the next version. Thanks!

reviewer
#8 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

In reply to Vernon Yang
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 74 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
 	xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+		struct vm_area_struct *vma, unsigned long start_addr,
+		bool *mmap_locked, unsigned int *cur_progress,
 		struct collapse_control *cc)
 {
 	pmd_t *pmd;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 5 lines
mm/khugepaged.c +2 -0
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
 	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+	if (cur_progress)
+		*cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

Quoted 30 lines
mm/khugepaged.c +10 -2
+
 	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
 	if (result != SCAN_SUCCEED)
 		goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 		result = SCAN_SUCCEED;
 	}
 out_unmap:
+	if (cur_progress) {
+		if (_pte >= pte + HPAGE_PMD_NR)
+			*cur_progress += HPAGE_PMD_NR - 1;
+		else
+			*cur_progress += _pte - pte;
+	}
 	pte_unmap_unlock(pte, ptl);
 	if (result == SCAN_SUCCEED) {
 		result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-		struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+		unsigned long addr, struct file *file, pgoff_t start,
+		unsigned int *cur_progress, struct collapse_control *cc)
 {
 	struct folio *folio = NULL;
 	struct address_space *mapping = file->f_mapping;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 8 lines
mm/khugepaged.c +5 -0
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
 			cond_resched_rcu();
 		}
 	}
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;
+
+		if (folio == NULL)
+			*cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 65 lines
mm/khugepaged.c +15 -5
+		else if (xa_is_value(folio))
+			*cur_progress += idx + (1 << xas_get_order(&xas));
+		else if (folio_order(folio) == HPAGE_PMD_ORDER)
+			*cur_progress += idx + 1;
+		else
+			*cur_progress += idx + folio_nr_pages(folio);
+	}
 	rcu_read_unlock();
 
 	if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 		while (khugepaged_scan.address < hend) {
 			bool mmap_locked = true;
+			unsigned int cur_progress = 0;
 
 			cond_resched();
 			if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				mmap_read_unlock(mm);
 				mmap_locked = false;
 				*result = hpage_collapse_scan_file(mm,
-					khugepaged_scan.address, file, pgoff, cc);
+					khugepaged_scan.address, file, pgoff,
+					&cur_progress, cc);
 				fput(file);
 				if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
 					mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				}
 			} else {
 				*result = hpage_collapse_scan_pmd(mm, vma,
-					khugepaged_scan.address, &mmap_locked, cc);
+					khugepaged_scan.address, &mmap_locked,
+					&cur_progress, cc);
 			}
 
 			if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 			/* move to next address */
 			khugepaged_scan.address += HPAGE_PMD_SIZE;
-			progress += HPAGE_PMD_NR;
+			progress += cur_progress;
 			if (!mmap_locked)
 				/*
 				 * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			mmap_locked = false;
 			*lock_dropped = true;
 			result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-							  cc);
+							  NULL, cc);
 
 			if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
 			    mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			fput(file);
 		} else {
 			result = hpage_collapse_scan_pmd(mm, vma, addr,
-							 &mmap_locked, cc);
+							 &mmap_locked, NULL, cc);
 		}
 		if (!mmap_locked)
 			*lock_dropped = true;
#9 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 36 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 6 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Quoted 75 lines
Quoted 75 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
 	xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+		struct vm_area_struct *vma, unsigned long start_addr,
+		bool *mmap_locked, unsigned int *cur_progress,
 		struct collapse_control *cc)
 {
 	pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 8 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
 	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+	if (cur_progress)
+		*cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		if (cur_progress)
			*cur_progress += 1; // here
		...
	}
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		...
	}
	...
out_unmap:
	if (cur_progress) {
		if (_pte >= pte + HPAGE_PMD_NR)
			*cur_progress += HPAGE_PMD_NR;   // here
		else
			*cur_progress += _pte - pte + 1; // here
	}
}

case 3:
current patch, and add more comments to clearer.

Quoted 42 lines
Quoted 30 lines
mm/khugepaged.c +10 -2
+
 	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
 	if (result != SCAN_SUCCEED)
 		goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 		result = SCAN_SUCCEED;
 	}
 out_unmap:
+	if (cur_progress) {
+		if (_pte >= pte + HPAGE_PMD_NR)
+			*cur_progress += HPAGE_PMD_NR - 1;
+		else
+			*cur_progress += _pte - pte;
+	}
 	pte_unmap_unlock(pte, ptl);
 	if (result == SCAN_SUCCEED) {
 		result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-		struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+		unsigned long addr, struct file *file, pgoff_t start,
+		unsigned int *cur_progress, struct collapse_control *cc)
 {
 	struct folio *folio = NULL;
 	struct address_space *mapping = file->f_mapping;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 8 lines
mm/khugepaged.c +5 -0
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
 			cond_resched_rcu();
 		}
 	}
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;
+
+		if (folio == NULL)
+			*cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2404

Quoted 7 lines
mm/khugepaged.c +7 -0
+		else if (xa_is_value(folio))
+			*cur_progress += idx + (1 << xas_get_order(&xas));
+		else if (folio_order(folio) == HPAGE_PMD_ORDER)
+			*cur_progress += idx + 1;
+		else
+			*cur_progress += idx + folio_nr_pages(folio);
+	}

The "idx" represent PTEs number already scanned when exiting xas_for_each().

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

	if (cur_progress) {
		*cur_progress += xas.xa_index - start;

		if (folio) {
			if (xa_is_value(folio))
				*cur_progress += 1 << xas_get_order(&xas);
			else if (folio_order(folio) == HPAGE_PMD_ORDER)
				*cur_progress += 1;
			else
				*cur_progress += folio_nr_pages(folio);
		}
	}
Quoted 47 lines
Quoted 47 lines
mm/khugepaged.c +7 -4
 	rcu_read_unlock();
 
 	if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 		while (khugepaged_scan.address < hend) {
 			bool mmap_locked = true;
+			unsigned int cur_progress = 0;
 
 			cond_resched();
 			if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				mmap_read_unlock(mm);
 				mmap_locked = false;
 				*result = hpage_collapse_scan_file(mm,
-					khugepaged_scan.address, file, pgoff, cc);
+					khugepaged_scan.address, file, pgoff,
+					&cur_progress, cc);
 				fput(file);
 				if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
 					mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				}
 			} else {
 				*result = hpage_collapse_scan_pmd(mm, vma,
-					khugepaged_scan.address, &mmap_locked, cc);
+					khugepaged_scan.address, &mmap_locked,
+					&cur_progress, cc);
 			}
 
 			if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 			/* move to next address */
 			khugepaged_scan.address += HPAGE_PMD_SIZE;
-			progress += HPAGE_PMD_NR;
+			progress += cur_progress;
 			if (!mmap_locked)
 				/*
 				 * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			mmap_locked = false;
 			*lock_dropped = true;
 			result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-							  cc);
+							  NULL, cc);
 
Quoted 12 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2867

Quoted 11 lines
mm/khugepaged.c +1 -1
 			if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
 			    mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			fput(file);
 		} else {
 			result = hpage_collapse_scan_pmd(mm, vma, addr,
-							 &mmap_locked, cc);
+							 &mmap_locked, NULL, cc);
 		}
 		if (!mmap_locked)
 			*lock_dropped = true;

--
Thanks,
Vernon

reviewer
#10 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 41 lines · Vernon Yang
Quoted 35 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 5 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 8 lines

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Thanks. Can you please mention this in the patch description. You can simply right it like this:

"From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the following statuses were observed, with frequency mentioned next to them:

SCAN_SUCCEED: 1
SCAN_PMD_MAPPED: 142
....."

and so on.

Quoted 127 lines
Quoted 76 lines
Quoted 76 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
 	xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+		struct vm_area_struct *vma, unsigned long start_addr,
+		bool *mmap_locked, unsigned int *cur_progress,
 		struct collapse_control *cc)
 {
 	pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
 	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+	if (cur_progress)
+		*cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		if (cur_progress)
			*cur_progress += 1; // here
		...
	}
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

Quoted 6 lines
{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here

Let us be more explicit and set this equal to 1, instead of adding 1.

Quoted 7 lines
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here

Same comment as above.

Quoted 9 lines
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		...
	}
	...
Quoted 8 lines

out_unmap:

	if (cur_progress) {
		if (_pte >= pte + HPAGE_PMD_NR)
			*cur_progress += HPAGE_PMD_NR;   // here
		else
			*cur_progress += _pte - pte + 1; // here
	}
}

I will vote case 2. In case 1 I don't like the fact that the if (cur_progress) branch will be checked each iteration - and I don't think the compiler can optimize this since the body of the loop is complex, so this check cannot be hoisted out of the loop.

Quoted 55 lines

case 3:
current patch, and add more comments to clearer.

Quoted 41 lines
Quoted 30 lines
mm/khugepaged.c +10 -2
+
 	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
 	if (result != SCAN_SUCCEED)
 		goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 		result = SCAN_SUCCEED;
 	}
 out_unmap:
+	if (cur_progress) {
+		if (_pte >= pte + HPAGE_PMD_NR)
+			*cur_progress += HPAGE_PMD_NR - 1;
+		else
+			*cur_progress += _pte - pte;
+	}
 	pte_unmap_unlock(pte, ptl);
 	if (result == SCAN_SUCCEED) {
 		result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-		struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+		unsigned long addr, struct file *file, pgoff_t start,
+		unsigned int *cur_progress, struct collapse_control *cc)
 {
 	struct folio *folio = NULL;
 	struct address_space *mapping = file->f_mapping;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 8 lines
mm/khugepaged.c +5 -0
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
 			cond_resched_rcu();
 		}
 	}
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;
+
+		if (folio == NULL)
+			*cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2404

Quoted 7 lines
mm/khugepaged.c +7 -0
+		else if (xa_is_value(folio))
+			*cur_progress += idx + (1 << xas_get_order(&xas));
+		else if (folio_order(folio) == HPAGE_PMD_ORDER)
+			*cur_progress += idx + 1;
+		else
+			*cur_progress += idx + folio_nr_pages(folio);
+	}

The "idx" represent PTEs number already scanned when exiting xas_for_each().

Quoted 3 lines

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

But, the number of PTEs you account in these three cases, are *not* scanned, right? So we can simply drop these 3 cases.

Quoted 8 lines

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

Quoted 12 lines
	if (cur_progress) {
		*cur_progress += xas.xa_index - start;

		if (folio) {
			if (xa_is_value(folio))
				*cur_progress += 1 << xas_get_order(&xas);
			else if (folio_order(folio) == HPAGE_PMD_ORDER)
				*cur_progress += 1;
			else
				*cur_progress += folio_nr_pages(folio);
		}
	}

Yep, this is unneeded complexity. This looks really ugly and the benefits of this are not clear. You can simply do

if (cur_progress)

	*cur_progress = xas.xa_index - start;
Quoted 62 lines
Quoted 48 lines
Quoted 48 lines
mm/khugepaged.c +7 -4
 	rcu_read_unlock();
 
 	if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 		while (khugepaged_scan.address < hend) {
 			bool mmap_locked = true;
+			unsigned int cur_progress = 0;
 
 			cond_resched();
 			if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				mmap_read_unlock(mm);
 				mmap_locked = false;
 				*result = hpage_collapse_scan_file(mm,
-					khugepaged_scan.address, file, pgoff, cc);
+					khugepaged_scan.address, file, pgoff,
+					&cur_progress, cc);
 				fput(file);
 				if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
 					mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				}
 			} else {
 				*result = hpage_collapse_scan_pmd(mm, vma,
-					khugepaged_scan.address, &mmap_locked, cc);
+					khugepaged_scan.address, &mmap_locked,
+					&cur_progress, cc);
 			}
 
 			if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 			/* move to next address */
 			khugepaged_scan.address += HPAGE_PMD_SIZE;
-			progress += HPAGE_PMD_NR;
+			progress += cur_progress;
 			if (!mmap_locked)
 				/*
 				 * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			mmap_locked = false;
 			*lock_dropped = true;
 			result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-							  cc);
+							  NULL, cc);
 
Quoted 11 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2867

Quoted 11 lines
mm/khugepaged.c +1 -1
 			if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
 			    mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			fput(file);
 		} else {
 			result = hpage_collapse_scan_pmd(mm, vma, addr,
-							 &mmap_locked, cc);
+							 &mmap_locked, NULL, cc);
 		}
 		if (!mmap_locked)
 			*lock_dropped = true;

--
Thanks,
Vernon

#11 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 55 lines · Dev Jain
Quoted 41 lines · Vernon Yang
Quoted 35 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 5 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 8 lines

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Thanks. Can you please mention this in the patch description. You can simply right it like this:

Quoted 8 lines

"From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the following statuses were observed, with frequency mentioned next to them:

SCAN_SUCCEED: 1
SCAN_PMD_MAPPED: 142
....."

and so on.

LGTM, I will do it in the next version. Thanks!

Quoted 134 lines
Quoted 127 lines
Quoted 76 lines
Quoted 76 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
 	xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+		struct vm_area_struct *vma, unsigned long start_addr,
+		bool *mmap_locked, unsigned int *cur_progress,
 		struct collapse_control *cc)
 {
 	pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
 	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+	if (cur_progress)
+		*cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		if (cur_progress)
			*cur_progress += 1; // here
		...
	}
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

Quoted 6 lines
{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
Quoted 1 line

Let us be more explicit and set this equal to 1, instead of adding 1.

LGTM, I will do it in the next version. Thanks!

Quoted 89 lines
Quoted 7 lines
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here

Same comment as above.

Quoted 9 lines
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		...
	}
	...
Quoted 8 lines

out_unmap:

	if (cur_progress) {
		if (_pte >= pte + HPAGE_PMD_NR)
			*cur_progress += HPAGE_PMD_NR;   // here
		else
			*cur_progress += _pte - pte + 1; // here
	}
}

I will vote case 2. In case 1 I don't like the fact that the if (cur_progress) branch will be checked each iteration - and I don't think the compiler can optimize this since the body of the loop is complex, so this check cannot be hoisted out of the loop.

Quoted 55 lines

case 3:
current patch, and add more comments to clearer.

Quoted 41 lines
Quoted 30 lines
mm/khugepaged.c +10 -2
+
 	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
 	if (result != SCAN_SUCCEED)
 		goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 		result = SCAN_SUCCEED;
 	}
 out_unmap:
+	if (cur_progress) {
+		if (_pte >= pte + HPAGE_PMD_NR)
+			*cur_progress += HPAGE_PMD_NR - 1;
+		else
+			*cur_progress += _pte - pte;
+	}
 	pte_unmap_unlock(pte, ptl);
 	if (result == SCAN_SUCCEED) {
 		result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-		struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+		unsigned long addr, struct file *file, pgoff_t start,
+		unsigned int *cur_progress, struct collapse_control *cc)
 {
 	struct folio *folio = NULL;
 	struct address_space *mapping = file->f_mapping;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 8 lines
mm/khugepaged.c +5 -0
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
 			cond_resched_rcu();
 		}
 	}
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;
+
+		if (folio == NULL)
+			*cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2404

Quoted 7 lines
mm/khugepaged.c +7 -0
+		else if (xa_is_value(folio))
+			*cur_progress += idx + (1 << xas_get_order(&xas));
+		else if (folio_order(folio) == HPAGE_PMD_ORDER)
+			*cur_progress += idx + 1;
+		else
+			*cur_progress += idx + folio_nr_pages(folio);
+	}

The "idx" represent PTEs number already scanned when exiting xas_for_each().

Quoted 6 lines
Quoted 3 lines

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

But, the number of PTEs you account in these three cases, are *not* scanned, right? So we can simply drop these 3 cases.

No, these three cases are the last scanning folio to break, I think we need to add them. Imagine that if we trigger HPAGE_PMD_ORDER folio firstly, "idx" is equal to 0.

Quoted 90 lines
Quoted 8 lines

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

Quoted 12 lines
	if (cur_progress) {
		*cur_progress += xas.xa_index - start;

		if (folio) {
			if (xa_is_value(folio))
				*cur_progress += 1 << xas_get_order(&xas);
			else if (folio_order(folio) == HPAGE_PMD_ORDER)
				*cur_progress += 1;
			else
				*cur_progress += folio_nr_pages(folio);
		}
	}

Yep, this is unneeded complexity. This looks really ugly and the benefits of this are not clear. You can simply do

if (cur_progress)

	*cur_progress = xas.xa_index - start;
Quoted 62 lines
Quoted 48 lines
Quoted 48 lines
mm/khugepaged.c +7 -4
 	rcu_read_unlock();
 
 	if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 		while (khugepaged_scan.address < hend) {
 			bool mmap_locked = true;
+			unsigned int cur_progress = 0;
 
 			cond_resched();
 			if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				mmap_read_unlock(mm);
 				mmap_locked = false;
 				*result = hpage_collapse_scan_file(mm,
-					khugepaged_scan.address, file, pgoff, cc);
+					khugepaged_scan.address, file, pgoff,
+					&cur_progress, cc);
 				fput(file);
 				if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
 					mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				}
 			} else {
 				*result = hpage_collapse_scan_pmd(mm, vma,
-					khugepaged_scan.address, &mmap_locked, cc);
+					khugepaged_scan.address, &mmap_locked,
+					&cur_progress, cc);
 			}
 
 			if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 			/* move to next address */
 			khugepaged_scan.address += HPAGE_PMD_SIZE;
-			progress += HPAGE_PMD_NR;
+			progress += cur_progress;
 			if (!mmap_locked)
 				/*
 				 * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			mmap_locked = false;
 			*lock_dropped = true;
 			result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-							  cc);
+							  NULL, cc);
 
Quoted 11 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2867

Quoted 11 lines
mm/khugepaged.c +1 -1
 			if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
 			    mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			fput(file);
 		} else {
 			result = hpage_collapse_scan_pmd(mm, vma, addr,
-							 &mmap_locked, cc);
+							 &mmap_locked, NULL, cc);
 		}
 		if (!mmap_locked)
 			*lock_dropped = true;

--
Thanks,
Vernon

reviewer
#12 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 285 lines · Vernon Yang
Quoted 53 lines · Dev Jain
Quoted 41 lines · Vernon Yang
Quoted 35 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 5 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 8 lines

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Thanks. Can you please mention this in the patch description. You can simply right it like this:

Quoted 8 lines

"From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the following statuses were observed, with frequency mentioned next to them:

SCAN_SUCCEED: 1
SCAN_PMD_MAPPED: 142
....."

and so on.

LGTM, I will do it in the next version. Thanks!

Quoted 132 lines
Quoted 126 lines
Quoted 75 lines
Quoted 75 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
 	xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+		struct vm_area_struct *vma, unsigned long start_addr,
+		bool *mmap_locked, unsigned int *cur_progress,
 		struct collapse_control *cc)
 {
 	pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
 	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+	if (cur_progress)
+		*cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		if (cur_progress)
			*cur_progress += 1; // here
		...
	}
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

Quoted 6 lines
{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
Quoted 1 line

Let us be more explicit and set this equal to 1, instead of adding 1.

LGTM, I will do it in the next version. Thanks!

Quoted 86 lines
Quoted 7 lines
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here

Same comment as above.

Quoted 9 lines
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		...
	}
	...
Quoted 8 lines

out_unmap:

	if (cur_progress) {
		if (_pte >= pte + HPAGE_PMD_NR)
			*cur_progress += HPAGE_PMD_NR;   // here
		else
			*cur_progress += _pte - pte + 1; // here
	}
}

I will vote case 2. In case 1 I don't like the fact that the if (cur_progress) branch will be checked each iteration - and I don't think the compiler can optimize this since the body of the loop is complex, so this check cannot be hoisted out of the loop.

Quoted 54 lines

case 3:
current patch, and add more comments to clearer.

Quoted 41 lines
Quoted 30 lines
mm/khugepaged.c +10 -2
+
 	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
 	if (result != SCAN_SUCCEED)
 		goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 		result = SCAN_SUCCEED;
 	}
 out_unmap:
+	if (cur_progress) {
+		if (_pte >= pte + HPAGE_PMD_NR)
+			*cur_progress += HPAGE_PMD_NR - 1;
+		else
+			*cur_progress += _pte - pte;
+	}
 	pte_unmap_unlock(pte, ptl);
 	if (result == SCAN_SUCCEED) {
 		result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
 	return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-		struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+		unsigned long addr, struct file *file, pgoff_t start,
+		unsigned int *cur_progress, struct collapse_control *cc)
 {
 	struct folio *folio = NULL;
 	struct address_space *mapping = file->f_mapping;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 8 lines
mm/khugepaged.c +5 -0
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
 			cond_resched_rcu();
 		}
 	}
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;
+
+		if (folio == NULL)
+			*cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2404

Quoted 7 lines
mm/khugepaged.c +7 -0
+		else if (xa_is_value(folio))
+			*cur_progress += idx + (1 << xas_get_order(&xas));
+		else if (folio_order(folio) == HPAGE_PMD_ORDER)
+			*cur_progress += idx + 1;
+		else
+			*cur_progress += idx + folio_nr_pages(folio);
+	}

The "idx" represent PTEs number already scanned when exiting xas_for_each().

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

But, the number of PTEs you account in these three cases, are *not* scanned, right? So we can simply drop these 3 cases.

No, these three cases are the last scanning folio to break, I think we need to add them. Imagine that if we trigger HPAGE_PMD_ORDER folio firstly, "idx" is equal to 0.

If you hit a large folio and break, you don't consume any extra iterations. The number of iterations is completely determined by xa_index. This is kind of analogous to SCAN_PMD_MAPPED - in one "iteration", you decide to stop scanning, and set progress to 1.

In any case, the problem which you describe in the patch description is completely solved by setting progress to 1 upon failure of find_pmd_or_thp_or_none. So anything else which you do (like counting iterations in hpage_collapse_scan_pmd or hpage_collapse_scan_file) is extra and it is not clear if it has a practical benefit. The patch benefits us because there are a lot of SCAN_PMD_MAPPED and SCAN_PMD_NONE. Will we practically also encounter a large number of SCAN_EXCEED_PTE_SWAP, PTE_NONE, etc?

I tilt towards keeping the other bits of the patch, if they can be simplified, and because this patch is relatively harmless. Just like you count the number of iterations in hpage_collapse_scan_pmd(), you can do the same here using xa_index.

Quoted 87 lines
Quoted 87 lines
Quoted 8 lines

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

Quoted 12 lines
	if (cur_progress) {
		*cur_progress += xas.xa_index - start;

		if (folio) {
			if (xa_is_value(folio))
				*cur_progress += 1 << xas_get_order(&xas);
			else if (folio_order(folio) == HPAGE_PMD_ORDER)
				*cur_progress += 1;
			else
				*cur_progress += folio_nr_pages(folio);
		}
	}

Yep, this is unneeded complexity. This looks really ugly and the benefits of this are not clear. You can simply do

if (cur_progress)

	*cur_progress = xas.xa_index - start;
Quoted 61 lines
Quoted 47 lines
Quoted 47 lines
mm/khugepaged.c +7 -4
 	rcu_read_unlock();
 
 	if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 		while (khugepaged_scan.address < hend) {
 			bool mmap_locked = true;
+			unsigned int cur_progress = 0;
 
 			cond_resched();
 			if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				mmap_read_unlock(mm);
 				mmap_locked = false;
 				*result = hpage_collapse_scan_file(mm,
-					khugepaged_scan.address, file, pgoff, cc);
+					khugepaged_scan.address, file, pgoff,
+					&cur_progress, cc);
 				fput(file);
 				if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
 					mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 				}
 			} else {
 				*result = hpage_collapse_scan_pmd(mm, vma,
-					khugepaged_scan.address, &mmap_locked, cc);
+					khugepaged_scan.address, &mmap_locked,
+					&cur_progress, cc);
 			}
 
 			if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
 			/* move to next address */
 			khugepaged_scan.address += HPAGE_PMD_SIZE;
-			progress += HPAGE_PMD_NR;
+			progress += cur_progress;
 			if (!mmap_locked)
 				/*
 				 * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			mmap_locked = false;
 			*lock_dropped = true;
 			result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-							  cc);
+							  NULL, cc);
 
Quoted 11 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2867

Quoted 11 lines
mm/khugepaged.c +1 -1
 			if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
 			    mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 			fput(file);
 		} else {
 			result = hpage_collapse_scan_pmd(mm, vma, addr,
-							 &mmap_locked, cc);
+							 &mmap_locked, NULL, cc);
 		}
 		if (!mmap_locked)
 			*lock_dropped = true;

--
Thanks,
Vernon

#13 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 308 lines · Dev Jain
Quoted 285 lines · Vernon Yang
Quoted 53 lines · Dev Jain
Quoted 41 lines · Vernon Yang
Quoted 35 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()
{

  for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
       _pte++, addr += PAGE_SIZE) {
          pte_t pteval = ptep_get(_pte);
          ...
          if (pte_uffd_wp(pteval)) { <-- first scan hit
                  result = SCAN_PTE_UFFD_WP;
                  goto out_unmap;
          }

}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 5 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 8 lines

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Thanks. Can you please mention this in the patch description. You can simply right it like this:

Quoted 8 lines

"From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the following statuses were observed, with frequency mentioned next to them:

SCAN_SUCCEED: 1
SCAN_PMD_MAPPED: 142
....."

and so on.

LGTM, I will do it in the next version. Thanks!

Quoted 132 lines
Quoted 126 lines
Quoted 75 lines
Quoted 75 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
  xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+ return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-         struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+         struct vm_area_struct *vma, unsigned long start_addr,
+         bool *mmap_locked, unsigned int *cur_progress,
          struct collapse_control *cc)
 {
  pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
  VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+ if (cur_progress)
+         *cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
    ...
    result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
    if (result != SCAN_SUCCEED) {
            if (cur_progress)
                    *cur_progress += 1; // here
            goto out;
    }
    ...
    pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
    if (!pte) {
            if (cur_progress)
                    *cur_progress += 1; // here
            result = SCAN_NO_PTE_TABLE;
            goto out;
    }

    for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
         _pte++, addr += PAGE_SIZE) {
            if (cur_progress)
                    *cur_progress += 1; // here
            ...
    }
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

Quoted 6 lines
{
    ...
    result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
    if (result != SCAN_SUCCEED) {
            if (cur_progress)
                    *cur_progress += 1; // here
Quoted 1 line

Let us be more explicit and set this equal to 1, instead of adding 1.

LGTM, I will do it in the next version. Thanks!

Quoted 86 lines
Quoted 7 lines
            goto out;
    }
    ...
    pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
    if (!pte) {
            if (cur_progress)
                    *cur_progress += 1; // here

Same comment as above.

Quoted 9 lines
            result = SCAN_NO_PTE_TABLE;
            goto out;
    }

    for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
         _pte++, addr += PAGE_SIZE) {
            ...
    }
    ...
Quoted 8 lines

out_unmap:

    if (cur_progress) {
            if (_pte >= pte + HPAGE_PMD_NR)
                    *cur_progress += HPAGE_PMD_NR;   // here
            else
                    *cur_progress += _pte - pte + 1; // here
    }
}

I will vote case 2. In case 1 I don't like the fact that the if (cur_progress) branch will be checked each iteration - and I don't think the compiler can optimize this since the body of the loop is complex, so this check cannot be hoisted out of the loop.

Quoted 54 lines

case 3:
current patch, and add more comments to clearer.

Quoted 32 lines
Quoted 32 lines
mm/khugepaged.c +10 -2
+
  result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
  if (result != SCAN_SUCCEED)
          goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
          result = SCAN_SUCCEED;
  }
 out_unmap:
+ if (cur_progress) {
+         if (_pte >= pte + HPAGE_PMD_NR)
+                 *cur_progress += HPAGE_PMD_NR - 1;
+         else
+                 *cur_progress += _pte - pte;
+ }
  pte_unmap_unlock(pte, ptl);
  if (result == SCAN_SUCCEED) {
          result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
  return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-         struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+         unsigned long addr, struct file *file, pgoff_t start,
+         unsigned int *cur_progress, struct collapse_control *cc)
 {
  struct folio *folio = NULL;
  struct address_space *mapping = file->f_mapping;
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
                  cond_resched_rcu();
          }
Quoted 16 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 6 lines
mm/khugepaged.c +5 -0
  }
+ if (cur_progress) {
+         unsigned long idx = xas_get_index(&xas) - start;
+
+         if (folio == NULL)
+                 *cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines
mm/khugepaged.c +7 -0
+         else if (xa_is_value(folio))
+                 *cur_progress += idx + (1 << xas_get_order(&xas));
+         else if (folio_order(folio) == HPAGE_PMD_ORDER)
+                 *cur_progress += idx + 1;
+         else
+                 *cur_progress += idx + folio_nr_pages(folio);
+ }

The "idx" represent PTEs number already scanned when exiting xas_for_each().

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

But, the number of PTEs you account in these three cases, are *not* scanned, right? So we can simply drop these 3 cases.

No, these three cases are the last scanning folio to break, I think we need to add them. Imagine that if we trigger HPAGE_PMD_ORDER folio firstly, "idx" is equal to 0.

If you hit a large folio and break, you don't consume any extra iterations. The number of iterations is completely determined by xa_index. This is kind of analogous to SCAN_PMD_MAPPED - in one "iteration", you decide to stop scanning, and set progress to 1.

In any case, the problem which you describe in the patch description is completely solved by setting progress to 1 upon failure of find_pmd_or_thp_or_none. So anything else which you do (like counting iterations in hpage_collapse_scan_pmd or hpage_collapse_scan_file) is extra and it is not clear if it has a practical benefit. The patch benefits us because there are a lot of SCAN_PMD_MAPPED and SCAN_PMD_NONE. Will we practically also encounter a large number of SCAN_EXCEED_PTE_SWAP, PTE_NONE, etc?

Quoted 3 lines

I tilt towards keeping the other bits of the patch, if they can be simplified, and because this patch is relatively harmless. Just like you count the number of iterations in hpage_collapse_scan_pmd(), you can do the same here using xa_index.

The semantics of hpage_collapse_scan_pmd() _pte and hpage_collapse_scan_file() xas.xa_index are different.

Let me give a detailed example :)

xas->xa_index represents the starting address of the last folio when exiting xas_for_each().

Assuming each folio size is 32KB, when we iterate "folio1 -> folio2" and break, xas->xa_index equals the starting address of folio2, so "idx = 8". However, folio2 is not counted in "idx".

In reality, folio2 has been scanned, so we need to add the folio2 size, making "idx = 16" the correct value.

There are two ways for folio2:
1. shmem swap entries (xa_is_value), breaking out of the xas_for_each loop due to SCAN_EXCEED_SWAP_PTE.
2. Normal folio, breaking out of the xas_for_each loop due to SCAN_SCAN_ABORT/SCAN_PAGE_LRU/SCAN_PAGE_COUNT.

Move Lance suggestion to here.

Quoted 2 lines

if (cur_progress)

      *cur_progress = max(xas.xa_index - start, 1UL);

If we use the code suggested by Lance, we will miss the folio2 size, the result is "idx = 8".
Is this the result we wanted? If Yes, Lance's suggested code is perfect.

Another more specific scenario is when the first iterated folio is HPAGE_PMD_ORDER, "idx = 0", Yep, we can directly set "cur_progress = 1", it's simple enough.

Quoted 87 lines
Quoted 87 lines
Quoted 87 lines
Quoted 8 lines

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

Quoted 12 lines
    if (cur_progress) {
            *cur_progress += xas.xa_index - start;

            if (folio) {
                    if (xa_is_value(folio))
                            *cur_progress += 1 << xas_get_order(&xas);
                    else if (folio_order(folio) == HPAGE_PMD_ORDER)
                            *cur_progress += 1;
                    else
                            *cur_progress += folio_nr_pages(folio);
            }
    }

Yep, this is unneeded complexity. This looks really ugly and the benefits of this are not clear. You can simply do

if (cur_progress)

     *cur_progress = xas.xa_index - start;
Quoted 61 lines
Quoted 47 lines
Quoted 47 lines
mm/khugepaged.c +7 -4
  rcu_read_unlock();
 
  if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
          while (khugepaged_scan.address < hend) {
                  bool mmap_locked = true;
+                 unsigned int cur_progress = 0;
 
                  cond_resched();
                  if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
                          mmap_read_unlock(mm);
                          mmap_locked = false;
                          *result = hpage_collapse_scan_file(mm,
-                                 khugepaged_scan.address, file, pgoff, cc);
+                                 khugepaged_scan.address, file, pgoff,
+                                 &cur_progress, cc);
                          fput(file);
                          if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
                                  mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
                          }
                  } else {
                          *result = hpage_collapse_scan_pmd(mm, vma,
-                                 khugepaged_scan.address, &mmap_locked, cc);
+                                 khugepaged_scan.address, &mmap_locked,
+                                 &cur_progress, cc);
                  }
 
                  if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
                  /* move to next address */
                  khugepaged_scan.address += HPAGE_PMD_SIZE;
-                 progress += HPAGE_PMD_NR;
+                 progress += cur_progress;
                  if (!mmap_locked)
                          /*
                           * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                  mmap_locked = false;
                  *lock_dropped = true;
                  result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-                                                   cc);
+                                                   NULL, cc);
 
Quoted 11 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2867

Quoted 11 lines
mm/khugepaged.c +1 -1
                  if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
                      mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                  fput(file);
          } else {
                  result = hpage_collapse_scan_pmd(mm, vma, addr,
-                                                  &mmap_locked, cc);
+                                                  &mmap_locked, NULL, cc);
          }
          if (!mmap_locked)
                  *lock_dropped = true;

--
Thanks,
Vernon

reviewer
#14 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 318 lines · Vernon Yang
Quoted 306 lines · Dev Jain
Quoted 285 lines · Vernon Yang
Quoted 53 lines · Dev Jain
Quoted 41 lines · Vernon Yang
Quoted 35 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()
{

  for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
       _pte++, addr += PAGE_SIZE) {
          pte_t pteval = ptep_get(_pte);
          ...
          if (pte_uffd_wp(pteval)) { <-- first scan hit
                  result = SCAN_PTE_UFFD_WP;
                  goto out_unmap;
          }

}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 5 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 8 lines

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Thanks. Can you please mention this in the patch description. You can simply right it like this:

Quoted 8 lines

"From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the following statuses were observed, with frequency mentioned next to them:

SCAN_SUCCEED: 1
SCAN_PMD_MAPPED: 142
....."

and so on.

LGTM, I will do it in the next version. Thanks!

Quoted 132 lines
Quoted 126 lines
Quoted 75 lines
Quoted 75 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
 include/linux/xarray.h |  9 ++++++++
 mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
  xas->xa_node = XAS_RESTART;
 }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+ return xas->xa_index;
+}
+
 /**
  * xas_advance() - Skip over sibling entries.
  * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
 static struct task_struct *khugepaged_thread __read_mostly;
 static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
 static unsigned int khugepaged_pages_to_scan __read_mostly;
 static unsigned int khugepaged_pages_collapsed;
 static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 }
 
 static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-         struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+         struct vm_area_struct *vma, unsigned long start_addr,
+         bool *mmap_locked, unsigned int *cur_progress,
          struct collapse_control *cc)
 {
  pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
  VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+ if (cur_progress)
+         *cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
    ...
    result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
    if (result != SCAN_SUCCEED) {
            if (cur_progress)
                    *cur_progress += 1; // here
            goto out;
    }
    ...
    pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
    if (!pte) {
            if (cur_progress)
                    *cur_progress += 1; // here
            result = SCAN_NO_PTE_TABLE;
            goto out;
    }

    for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
         _pte++, addr += PAGE_SIZE) {
            if (cur_progress)
                    *cur_progress += 1; // here
            ...
    }
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

Quoted 6 lines
{
    ...
    result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
    if (result != SCAN_SUCCEED) {
            if (cur_progress)
                    *cur_progress += 1; // here
Quoted 1 line

Let us be more explicit and set this equal to 1, instead of adding 1.

LGTM, I will do it in the next version. Thanks!

Quoted 86 lines
Quoted 7 lines
            goto out;
    }
    ...
    pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
    if (!pte) {
            if (cur_progress)
                    *cur_progress += 1; // here

Same comment as above.

Quoted 9 lines
            result = SCAN_NO_PTE_TABLE;
            goto out;
    }

    for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
         _pte++, addr += PAGE_SIZE) {
            ...
    }
    ...
Quoted 8 lines

out_unmap:

    if (cur_progress) {
            if (_pte >= pte + HPAGE_PMD_NR)
                    *cur_progress += HPAGE_PMD_NR;   // here
            else
                    *cur_progress += _pte - pte + 1; // here
    }
}

I will vote case 2. In case 1 I don't like the fact that the if (cur_progress) branch will be checked each iteration - and I don't think the compiler can optimize this since the body of the loop is complex, so this check cannot be hoisted out of the loop.

Quoted 54 lines

case 3:
current patch, and add more comments to clearer.

Quoted 32 lines
Quoted 32 lines
mm/khugepaged.c +10 -2
+
  result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
  if (result != SCAN_SUCCEED)
          goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
          result = SCAN_SUCCEED;
  }
 out_unmap:
+ if (cur_progress) {
+         if (_pte >= pte + HPAGE_PMD_NR)
+                 *cur_progress += HPAGE_PMD_NR - 1;
+         else
+                 *cur_progress += _pte - pte;
+ }
  pte_unmap_unlock(pte, ptl);
  if (result == SCAN_SUCCEED) {
          result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
  return result;
 }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-         struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+         unsigned long addr, struct file *file, pgoff_t start,
+         unsigned int *cur_progress, struct collapse_control *cc)
 {
  struct folio *folio = NULL;
  struct address_space *mapping = file->f_mapping;
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
                  cond_resched_rcu();
          }
Quoted 16 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 6 lines
mm/khugepaged.c +5 -0
  }
+ if (cur_progress) {
+         unsigned long idx = xas_get_index(&xas) - start;
+
+         if (folio == NULL)
+                 *cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines
mm/khugepaged.c +7 -0
+         else if (xa_is_value(folio))
+                 *cur_progress += idx + (1 << xas_get_order(&xas));
+         else if (folio_order(folio) == HPAGE_PMD_ORDER)
+                 *cur_progress += idx + 1;
+         else
+                 *cur_progress += idx + folio_nr_pages(folio);
+ }

The "idx" represent PTEs number already scanned when exiting xas_for_each().

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

But, the number of PTEs you account in these three cases, are *not* scanned, right? So we can simply drop these 3 cases.

No, these three cases are the last scanning folio to break, I think we need to add them. Imagine that if we trigger HPAGE_PMD_ORDER folio firstly, "idx" is equal to 0.

If you hit a large folio and break, you don't consume any extra iterations. The number of iterations is completely determined by xa_index. This is kind of analogous to SCAN_PMD_MAPPED - in one "iteration", you decide to stop scanning, and set progress to 1.

In any case, the problem which you describe in the patch description is completely solved by setting progress to 1 upon failure of find_pmd_or_thp_or_none. So anything else which you do (like counting iterations in hpage_collapse_scan_pmd or hpage_collapse_scan_file) is extra and it is not clear if it has a practical benefit. The patch benefits us because there are a lot of SCAN_PMD_MAPPED and SCAN_PMD_NONE. Will we practically also encounter a large number of SCAN_EXCEED_PTE_SWAP, PTE_NONE, etc?

Quoted 3 lines

I tilt towards keeping the other bits of the patch, if they can be simplified, and because this patch is relatively harmless. Just like you count the number of iterations in hpage_collapse_scan_pmd(), you can do the same here using xa_index.

The semantics of hpage_collapse_scan_pmd() _pte and hpage_collapse_scan_file() xas.xa_index are different.

Let me give a detailed example :)

xas->xa_index represents the starting address of the last folio when exiting xas_for_each().

Quoted 6 lines

Assuming each folio size is 32KB, when we iterate "folio1 -> folio2" and break, xas->xa_index equals the starting address of folio2, so "idx = 8". However, folio2 is not counted in "idx".

In reality, folio2 has been scanned, so we need to add the folio2 size, making "idx = 16" the correct value.

The "scanning" of folio2 is O(1). It is *not* proportional to the number of ptes covered by folio2 (which is 8), as we break immediately.

As I said above, you will be contradicting the patch's objective here - if you see a PMD_ORDER folio and set progress = HPAGE_PMD_NR, that exactly corresponds to seeing a PMD mapping in the anon collapse path, and setting progress = HPAGE_PMD_NR. But that is not appropriate - deducing that the PMD maps a PMD folio, is an O(1) operation w.r.t the number of ptes.

Quoted 10 lines

There are two ways for folio2:
1. shmem swap entries (xa_is_value), breaking out of the xas_for_each loop due to SCAN_EXCEED_SWAP_PTE.
2. Normal folio, breaking out of the xas_for_each loop due to SCAN_SCAN_ABORT/SCAN_PAGE_LRU/SCAN_PAGE_COUNT.

Move Lance suggestion to here.

Quoted 2 lines

if (cur_progress)

      *cur_progress = max(xas.xa_index - start, 1UL);
Quoted 7 lines

If we use the code suggested by Lance, we will miss the folio2 size, the result is "idx = 8".
Is this the result we wanted? If Yes, Lance's suggested code is perfect.

Another more specific scenario is when the first iterated folio is HPAGE_PMD_ORDER, "idx = 0", Yep, we can directly set "cur_progress = 1", it's simple enough.

Yes, let us do that.

Quoted 87 lines
Quoted 87 lines
Quoted 87 lines
Quoted 87 lines
Quoted 8 lines

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

Quoted 12 lines
    if (cur_progress) {
            *cur_progress += xas.xa_index - start;

            if (folio) {
                    if (xa_is_value(folio))
                            *cur_progress += 1 << xas_get_order(&xas);
                    else if (folio_order(folio) == HPAGE_PMD_ORDER)
                            *cur_progress += 1;
                    else
                            *cur_progress += folio_nr_pages(folio);
            }
    }

Yep, this is unneeded complexity. This looks really ugly and the benefits of this are not clear. You can simply do

if (cur_progress)

     *cur_progress = xas.xa_index - start;
Quoted 61 lines
Quoted 47 lines
Quoted 47 lines
mm/khugepaged.c +7 -4
  rcu_read_unlock();
 
  if (result == SCAN_SUCCEED) {
@@ -2456,6 +2482,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
          while (khugepaged_scan.address < hend) {
                  bool mmap_locked = true;
+                 unsigned int cur_progress = 0;
 
                  cond_resched();
                  if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
@@ -2472,7 +2499,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
                          mmap_read_unlock(mm);
                          mmap_locked = false;
                          *result = hpage_collapse_scan_file(mm,
-                                 khugepaged_scan.address, file, pgoff, cc);
+                                 khugepaged_scan.address, file, pgoff,
+                                 &cur_progress, cc);
                          fput(file);
                          if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
                                  mmap_read_lock(mm);
@@ -2486,7 +2514,8 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
                          }
                  } else {
                          *result = hpage_collapse_scan_pmd(mm, vma,
-                                 khugepaged_scan.address, &mmap_locked, cc);
+                                 khugepaged_scan.address, &mmap_locked,
+                                 &cur_progress, cc);
                  }
 
                  if (*result == SCAN_SUCCEED)
@@ -2494,7 +2523,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 
                  /* move to next address */
                  khugepaged_scan.address += HPAGE_PMD_SIZE;
-                 progress += HPAGE_PMD_NR;
+                 progress += cur_progress;
                  if (!mmap_locked)
                          /*
                           * We released mmap_lock so break loop.  Note
@@ -2817,7 +2846,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                  mmap_locked = false;
                  *lock_dropped = true;
                  result = hpage_collapse_scan_file(mm, addr, file, pgoff,
-                                                   cc);
+                                                   NULL, cc);
 
Quoted 11 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2867

Quoted 11 lines
mm/khugepaged.c +1 -1
                  if (result == SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb &&
                      mapping_can_writeback(file->f_mapping)) {
@@ -2832,7 +2861,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
                  fput(file);
          } else {
                  result = hpage_collapse_scan_pmd(mm, vma, addr,
-                                                  &mmap_locked, cc);
+                                                  &mmap_locked, NULL, cc);
          }
          if (!mmap_locked)
                  *lock_dropped = true;

--
Thanks,
Vernon

reviewer
#15 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

In reply to Dev Jain
Quoted 317 lines · Dev Jain
Quoted 41 lines · Vernon Yang
Quoted 35 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		pte_t pteval = ptep_get(_pte);
		...
		if (pte_uffd_wp(pteval)) { <-- first scan hit
			result = SCAN_PTE_UFFD_WP;
			goto out_unmap;
		}
	}
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 5 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 8 lines

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Thanks. Can you please mention this in the patch description. You can simply right it like this:

"From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the following statuses were observed, with frequency mentioned next to them:

SCAN_SUCCEED: 1
SCAN_PMD_MAPPED: 142
....."

and so on.

Quoted 127 lines
Quoted 76 lines
Quoted 76 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
  include/linux/xarray.h |  9 ++++++++
  mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
  2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
  	xas->xa_node = XAS_RESTART;
  }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+	return xas->xa_index;
+}
+
  /**
   * xas_advance() - Skip over sibling entries.
   * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
  static struct task_struct *khugepaged_thread __read_mostly;
  static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
  static unsigned int khugepaged_pages_to_scan __read_mostly;
  static unsigned int khugepaged_pages_collapsed;
  static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
  }
 
  static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-		struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+		struct vm_area_struct *vma, unsigned long start_addr,
+		bool *mmap_locked, unsigned int *cur_progress,
  		struct collapse_control *cc)
  {
  	pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
  	VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+	if (cur_progress)
+		*cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		if (cur_progress)
			*cur_progress += 1; // here
		...
	}
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

Quoted 6 lines
{
	...
	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
	if (result != SCAN_SUCCEED) {
		if (cur_progress)
			*cur_progress += 1; // here

Let us be more explicit and set this equal to 1, instead of adding 1.

Quoted 7 lines
		goto out;
	}
	...
	pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
	if (!pte) {
		if (cur_progress)
			*cur_progress += 1; // here

Same comment as above.

Quoted 9 lines
		result = SCAN_NO_PTE_TABLE;
		goto out;
	}

	for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
	     _pte++, addr += PAGE_SIZE) {
		...
	}
	...
Quoted 8 lines

out_unmap:

	if (cur_progress) {
		if (_pte >= pte + HPAGE_PMD_NR)
			*cur_progress += HPAGE_PMD_NR;   // here
		else
			*cur_progress += _pte - pte + 1; // here
	}
}

I will vote case 2. In case 1 I don't like the fact that the if (cur_progress) branch will be checked each iteration - and I don't think the compiler can optimize this since the body of the loop is complex, so this check cannot be hoisted out of the loop.

Quoted 55 lines

case 3:
current patch, and add more comments to clearer.

Quoted 41 lines
Quoted 30 lines
mm/khugepaged.c +10 -2
+
  	result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
  	if (result != SCAN_SUCCEED)
  		goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
  		result = SCAN_SUCCEED;
  	}
  out_unmap:
+	if (cur_progress) {
+		if (_pte >= pte + HPAGE_PMD_NR)
+			*cur_progress += HPAGE_PMD_NR - 1;
+		else
+			*cur_progress += _pte - pte;
+	}
  	pte_unmap_unlock(pte, ptl);
  	if (result == SCAN_SUCCEED) {
  		result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
  	return result;
  }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-		struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+		unsigned long addr, struct file *file, pgoff_t start,
+		unsigned int *cur_progress, struct collapse_control *cc)
  {
  	struct folio *folio = NULL;
  	struct address_space *mapping = file->f_mapping;

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 8 lines
mm/khugepaged.c +5 -0
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
  			cond_resched_rcu();
  		}
  	}
+	if (cur_progress) {
+		unsigned long idx = xas_get_index(&xas) - start;
+
+		if (folio == NULL)
+			*cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2404

Quoted 7 lines
mm/khugepaged.c +7 -0
+		else if (xa_is_value(folio))
+			*cur_progress += idx + (1 << xas_get_order(&xas));
+		else if (folio_order(folio) == HPAGE_PMD_ORDER)
+			*cur_progress += idx + 1;
+		else
+			*cur_progress += idx + folio_nr_pages(folio);
+	}

The "idx" represent PTEs number already scanned when exiting xas_for_each().

Quoted 3 lines

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

But, the number of PTEs you account in these three cases, are *not* scanned, right? So we can simply drop these 3 cases.

Quoted 8 lines

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

Quoted 12 lines
	if (cur_progress) {
		*cur_progress += xas.xa_index - start;

		if (folio) {
			if (xa_is_value(folio))
				*cur_progress += 1 << xas_get_order(&xas);
			else if (folio_order(folio) == HPAGE_PMD_ORDER)
				*cur_progress += 1;
			else
				*cur_progress += folio_nr_pages(folio);
		}
	}
Quoted 6 lines

Yep, this is unneeded complexity. This looks really ugly and the benefits of this are not clear. You can simply do

if (cur_progress)

	*cur_progress = xas.xa_index - start;

I agree with Dev here. The extra complexity in hpage_collapse_scan_file() doesn't seem worth it.

Suggest:

if (cur_progress)

	*cur_progress = max(xas.xa_index - start, 1UL);

Just keeps it simple, and handles the idx=0 case you mentioned as well.

[...]

#16 lore raw

Re: [PATCH mm-new v5 2/5] mm: khugepaged: refine scan progress number

Quoted 329 lines · Lance Yang
Quoted 317 lines · Dev Jain
Quoted 41 lines · Vernon Yang
Quoted 35 lines · Dev Jain
Quoted 34 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Currently, each scan always increases "progress" by HPAGE_PMD_NR, even if only scanning a single PTE/PMD entry.

- When only scanning a sigle PTE entry, let me provide a detailed example:

static int hpage_collapse_scan_pmd()

{
   for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
        _pte++, addr += PAGE_SIZE) {
           pte_t pteval = ptep_get(_pte);
           ...
           if (pte_uffd_wp(pteval)) { <-- first scan hit
                   result = SCAN_PTE_UFFD_WP;
                   goto out_unmap;
           }
   }
}

During the first scan, if pte_uffd_wp(pteval) is true, the loop exits directly. In practice, only one PTE is scanned before termination. Here, "progress += 1" reflects the actual number of PTEs scanned, but previously "progress += HPAGE_PMD_NR" always.

- When the memory has been collapsed to PMD, let me provide a detailed example:

The following data is traced by bpftrace on a desktop system. After the system has been left idle for 10 minutes upon booting, a lot of SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE are observed during a full scan by khugepaged.

Quoted 5 lines
Quoted 4 lines
@scan_pmd_status[1]: 1           ## SCAN_SUCCEED
@scan_pmd_status[6]: 2           ## SCAN_EXCEED_SHARED_PTE
@scan_pmd_status[3]: 142         ## SCAN_PMD_MAPPED
@scan_pmd_status[2]: 178         ## SCAN_NO_PTE_TABLE

Could you elaborate what is [1], [6] etc and 1,2,142, etc?

Quoted 8 lines

These 1,6 are value of "enum scan_result", you can directly refer to the notes on the right.

These 1,2,142,178 are number of different "enum scan_result" from trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file.

as example, SCAN_PMD_MAPPED has 142 times during a full scan by khugepaged.

Thanks. Can you please mention this in the patch description. You can simply right it like this:

"From trace_mm_khugepaged_scan_pmd and trace_mm_khugepaged_scan_file, the following statuses were observed, with frequency mentioned next to them:

SCAN_SUCCEED: 1
SCAN_PMD_MAPPED: 142
....."

and so on.

Quoted 127 lines
Quoted 76 lines
Quoted 76 lines

total progress size: 674 MB
Total time : 419 seconds ## include khugepaged_scan_sleep_millisecs

The khugepaged_scan list save all task that support collapse into hugepage, as long as the task is not destroyed, khugepaged will not remove it from the khugepaged_scan list. This exist a phenomenon where task has already collapsed all memory regions into hugepage, but khugepaged continues to scan it, which wastes CPU time and invalid, and due to khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for scanning a large number of invalid task, so scanning really valid task is later.

After applying this patch, when the memory is either SCAN_PMD_MAPPED or SCAN_NO_PTE_TABLE, just skip it, as follow:

@scan_pmd_status[6]: 2
@scan_pmd_status[3]: 147
@scan_pmd_status[2]: 173
total progress size: 45 MB
Total time : 20 seconds

---
  include/linux/xarray.h |  9 ++++++++
  mm/khugepaged.c        | 47 ++++++++++++++++++++++++++++++++++--------
  2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/xarray.h +9 -0
index be850174e802..f77d97d7b957 100644
@@ -1646,6 +1646,15 @@ static inline void xas_set(struct xa_state *xas, unsigned long index)
   xas->xa_node = XAS_RESTART;
  }
 
+/**
+ * xas_get_index() - Get XArray operation state for a different index.
+ * @xas: XArray operation state.
+ */
+static inline unsigned long xas_get_index(struct xa_state *xas)
+{
+  return xas->xa_index;
+}
+
  /**
   * xas_advance() - Skip over sibling entries.
   * @xas: XArray operation state.
mm/khugepaged.c +6 -2
index 6f0f05148765..de95029e3763 100644
@@ -68,7 +68,10 @@ enum scan_result {
  static struct task_struct *khugepaged_thread __read_mostly;
  static DEFINE_MUTEX(khugepaged_mutex);
 
-/* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */
+/*
+ * default scan 8*HPAGE_PMD_NR ptes, pmd_mapped, no_pte_table or vmas
+ * every 10 second.
+ */
  static unsigned int khugepaged_pages_to_scan __read_mostly;
  static unsigned int khugepaged_pages_collapsed;
  static unsigned int khugepaged_full_scans;
@@ -1240,7 +1243,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
  }
 
  static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
-          struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked,
+          struct vm_area_struct *vma, unsigned long start_addr,
+          bool *mmap_locked, unsigned int *cur_progress,
           struct collapse_control *cc)
  {
   pmd_t *pmd;
@@ -1255,6 +1259,9 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 
Quoted 7 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1263

Quoted 4 lines
mm/khugepaged.c +2 -0
   VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK);
 
+  if (cur_progress)
+          *cur_progress += 1;

Why not be a little more explicit, and do this addition if find_pmd_or_thp_or_none fails, or pte_offset_map_lock fails? The way you do it right now is not readable - it gives no idea as to why on function entry we do a +1 right away. Please do add some comments too.

If this way is not clear enough, we can directly add 1 in find_pmd_or_thp_or_none() etc, BUT it's a bit redundant. Please take a look at which one is better.

case 1:
as the V4 PATCH #2 [1] and #3 [2], only hpage_collapse_scan_pmd().
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

{
     ...
     result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
     if (result != SCAN_SUCCEED) {
             if (cur_progress)
                     *cur_progress += 1; // here
             goto out;
     }
     ...
     pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
     if (!pte) {
             if (cur_progress)
                     *cur_progress += 1; // here
             result = SCAN_NO_PTE_TABLE;
             goto out;
     }

     for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
          _pte++, addr += PAGE_SIZE) {
             if (cur_progress)
                     *cur_progress += 1; // here
             ...
     }
}

case 2:

static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, unsigned int *cur_progress, struct collapse_control *cc)

Quoted 6 lines
{
     ...
     result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
     if (result != SCAN_SUCCEED) {
             if (cur_progress)
                     *cur_progress += 1; // here

Let us be more explicit and set this equal to 1, instead of adding 1.

Quoted 7 lines
             goto out;
     }
     ...
     pte = pte_offset_map_lock(mm, pmd, start_addr, &ptl);
     if (!pte) {
             if (cur_progress)
                     *cur_progress += 1; // here

Same comment as above.

Quoted 9 lines
             result = SCAN_NO_PTE_TABLE;
             goto out;
     }

     for (addr = start_addr, _pte = pte; _pte < pte + HPAGE_PMD_NR;
          _pte++, addr += PAGE_SIZE) {
             ...
     }
     ...
Quoted 8 lines

out_unmap:

     if (cur_progress) {
             if (_pte >= pte + HPAGE_PMD_NR)
                     *cur_progress += HPAGE_PMD_NR;   // here
             else
                     *cur_progress += _pte - pte + 1; // here
     }
}

I will vote case 2. In case 1 I don't like the fact that the if (cur_progress) branch will be checked each iteration - and I don't think the compiler can optimize this since the body of the loop is complex, so this check cannot be hoisted out of the loop.

Quoted 52 lines

case 3:
current patch, and add more comments to clearer.

Quoted 32 lines
Quoted 32 lines
mm/khugepaged.c +10 -2
+
   result = find_pmd_or_thp_or_none(mm, start_addr, &pmd);
   if (result != SCAN_SUCCEED)
           goto out;
@@ -1396,6 +1403,12 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
           result = SCAN_SUCCEED;
   }
  out_unmap:
+  if (cur_progress) {
+          if (_pte >= pte + HPAGE_PMD_NR)
+                  *cur_progress += HPAGE_PMD_NR - 1;
+          else
+                  *cur_progress += _pte - pte;
+  }
   pte_unmap_unlock(pte, ptl);
   if (result == SCAN_SUCCEED) {
           result = collapse_huge_page(mm, start_addr, referenced,
@@ -2286,8 +2299,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
   return result;
  }
 
-static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
-          struct file *file, pgoff_t start, struct collapse_control *cc)
+static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm,
+          unsigned long addr, struct file *file, pgoff_t start,
+          unsigned int *cur_progress, struct collapse_control *cc)
  {
   struct folio *folio = NULL;
   struct address_space *mapping = file->f_mapping;
@@ -2376,6 +2390,18 @@ static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned
                   cond_resched_rcu();
           }
Quoted 16 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, unsigned · line 2397

Quoted 6 lines
mm/khugepaged.c +5 -0
   }
+  if (cur_progress) {
+          unsigned long idx = xas_get_index(&xas) - start;
+
+          if (folio == NULL)
+                  *cur_progress += HPAGE_PMD_NR;

I think this whole block needs some comments. Can you explain, why you do a particular increment in each case?

Quoted 7 lines
mm/khugepaged.c +7 -0
+          else if (xa_is_value(folio))
+                  *cur_progress += idx + (1 << xas_get_order(&xas));
+          else if (folio_order(folio) == HPAGE_PMD_ORDER)
+                  *cur_progress += idx + 1;
+          else
+                  *cur_progress += idx + folio_nr_pages(folio);
+  }
Quoted 6 lines

The "idx" represent PTEs number already scanned when exiting xas_for_each().

However, the last valid folio size was not counted in "idx" (except folio == NULL, "idx" equal to HPAGE_PMD_NR), which can be further divided into three cases:

But, the number of PTEs you account in these three cases, are *not* scanned, right? So we can simply drop these 3 cases.

Quoted 8 lines

1. shmem swap entries (xa_is_value), add folio size.
2. the folio is HPAGE_PMD_ORDER, the memory has been collapsed to PMD, so add 1 only.
3. Normal folio, add folio size.

Is the version below more readable?

Quoted 12 lines
     if (cur_progress) {
             *cur_progress += xas.xa_index - start;

             if (folio) {
                     if (xa_is_value(folio))
                             *cur_progress += 1 << xas_get_order(&xas);
                     else if (folio_order(folio) == HPAGE_PMD_ORDER)
                             *cur_progress += 1;
                     else
                             *cur_progress += folio_nr_pages(folio);
             }
     }
Quoted 6 lines

Yep, this is unneeded complexity. This looks really ugly and the benefits of this are not clear. You can simply do

if (cur_progress)

      *cur_progress = xas.xa_index - start;

I agree with Dev here. The extra complexity in hpage_collapse_scan_file() doesn't seem worth it.

Quoted 6 lines

Suggest:

if (cur_progress)

        *cur_progress = max(xas.xa_index - start, 1UL);

Just keeps it simple, and handles the idx=0 case you mentioned as well.

Thank you for your suggestion, but two scenarios are still missing. For a detailed explanation, please refer to the link below.

https://lore.kernel.org/linux-mm/[email protected]/T/#mc3969cb0d52e28ffea2fb96260f0880a5f005848

#17 lore raw

PATCH v5 3/5 mm: add folio_test_lazyfree helper

In reply to Vernon Yang

From: Vernon Yang <[email protected]>

Add folio_test_lazyfree() function to identify lazy-free folios to improve code readability.

---
 include/linux/page-flags.h | 5 +++++
 mm/rmap.c                  | 2 +-
 mm/vmscan.c                | 5 ++---
 3 files changed, 8 insertions(+), 4 deletions(-)
include/linux/page-flags.h +5 -0
index f7a0e4af0c73..415e9f2ef616 100644
@@ -724,6 +724,11 @@ static __always_inline bool folio_test_anon(const struct folio *folio)
 	return ((unsigned long)folio->mapping & FOLIO_MAPPING_ANON) != 0;
 }
 
+static __always_inline bool folio_test_lazyfree(const struct folio *folio)
+{
+	return folio_test_anon(folio) && !folio_test_swapbacked(folio);
+}
+
 static __always_inline bool PageAnonNotKsm(const struct page *page)
 {
 	unsigned long flags = (unsigned long)page_folio(page)->mapping;
mm/rmap.c +1 -1
index 088723f909e4..6b09be06c707 100644
@@ -2049,7 +2049,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		}
 
 		if (!pvmw.pte) {
-			if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
+			if (folio_test_lazyfree(folio)) {
 				if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
 					goto walk_done;
 				/*
mm/vmscan.c +2 -3
index 8643c0fb9162..1f32c74ec738 100644
@@ -944,8 +944,7 @@ static void folio_check_dirty_writeback(struct folio *folio,
 	 * They could be mistakenly treated as file lru. So further anon
 	 * test is needed.
 	 */
-	if (!folio_is_file_lru(folio) ||
-	    (folio_test_anon(folio) && !folio_test_swapbacked(folio))) {
+	if (!folio_is_file_lru(folio) || folio_test_lazyfree(folio)) {
 		*dirty = false;
 		*writeback = false;
 		return;
@@ -1489,7 +1488,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 			}
 		}
 
-		if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
+		if (folio_test_lazyfree(folio)) {
 			/* follow __remove_mapping for reference */
 			if (!folio_ref_freeze(folio, 1))
 				goto keep_locked;
Signature · 1 line
2.51.0
reviewer Reviewed-by on v5 3/5
#18 lore raw

Re: [PATCH mm-new v5 3/5] mm: add folio_test_lazyfree helper

Quoted 8 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Add folio_test_lazyfree() function to identify lazy-free folios to improve code readability.

Thanks, this is very useful.

Quoted 58 lines
---
 include/linux/page-flags.h | 5 +++++
 mm/rmap.c                  | 2 +-
 mm/vmscan.c                | 5 ++---
 3 files changed, 8 insertions(+), 4 deletions(-)
include/linux/page-flags.h +5 -0
index f7a0e4af0c73..415e9f2ef616 100644
@@ -724,6 +724,11 @@ static __always_inline bool folio_test_anon(const struct folio *folio)
 	return ((unsigned long)folio->mapping & FOLIO_MAPPING_ANON) != 0;
 }
 
+static __always_inline bool folio_test_lazyfree(const struct folio *folio)
+{
+	return folio_test_anon(folio) && !folio_test_swapbacked(folio);
+}
+
 static __always_inline bool PageAnonNotKsm(const struct page *page)
 {
 	unsigned long flags = (unsigned long)page_folio(page)->mapping;
mm/rmap.c +1 -1
index 088723f909e4..6b09be06c707 100644
@@ -2049,7 +2049,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		}
 
 		if (!pvmw.pte) {
-			if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
+			if (folio_test_lazyfree(folio)) {
 				if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
 					goto walk_done;
 				/*
mm/vmscan.c +2 -3
index 8643c0fb9162..1f32c74ec738 100644
@@ -944,8 +944,7 @@ static void folio_check_dirty_writeback(struct folio *folio,
 	 * They could be mistakenly treated as file lru. So further anon
 	 * test is needed.
 	 */
-	if (!folio_is_file_lru(folio) ||
-	    (folio_test_anon(folio) && !folio_test_swapbacked(folio))) {
+	if (!folio_is_file_lru(folio) || folio_test_lazyfree(folio)) {
 		*dirty = false;
 		*writeback = false;
 		return;
@@ -1489,7 +1488,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 			}
 		}
 
-		if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) {
+		if (folio_test_lazyfree(folio)) {
 			/* follow __remove_mapping for reference */
 			if (!folio_ref_freeze(folio, 1))
 				goto keep_locked;
Reviewed-by on v5 3/5
#19 lore raw

Re: [PATCH mm-new v5 3/5] mm: add folio_test_lazyfree helper

In reply to Vernon Yang
Quoted 9 lines · Vernon Yang

From: Vernon Yang <[email protected]>

Add folio_test_lazyfree() function to identify lazy-free folios to improve code readability.

LGTM,

#20 lore raw

PATCH v5 4/5 mm: khugepaged: skip lazy-free folios

In reply to Vernon Yang

From: Vernon Yang <[email protected]>

For example, create three task: hot1 -> cold -> hot2. After all three task are created, each allocate memory 128MB. the hot1/hot2 task continuously access 128 MB memory, while the cold task only accesses its memory briefly andthen call madvise(MADV_FREE). However, khugepaged still prioritizes scanning the cold task and only scans the hot2 task after completing the scan of the cold task.

And if we collapse with a lazyfree page, that content will never be none and the deferred shrinker cannot reclaim them.

So if the user has explicitly informed us via MADV_FREE that this memory will be freed, it is appropriate for khugepaged to skip it only, thereby avoiding unnecessary scan and collapse operations to reducing CPU wastage.

Here are the performance test results:
(Throughput bigger is better, other smaller is better)

Testing on x86_64 machine:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.14 sec     |  2.93 sec     | -6.69%  |
| cycles per access   |  4.96         |  2.21         | -55.44% |
| Throughput          |  104.38 M/sec |  111.89 M/sec | +7.19%  |
| dTLB-load-misses    |  284814532    |  69597236     | -75.56% |

Testing on qemu-system-x86_64 -enable-kvm:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.35 sec     |  2.96 sec     | -11.64% |
| cycles per access   |  7.29         |  2.07         | -71.60% |
| Throughput          |  97.67 M/sec  |  110.77 M/sec | +13.41% |
| dTLB-load-misses    |  241600871    |  3216108      | -98.67% |
---
 include/trace/events/huge_memory.h |  1 +
 mm/khugepaged.c                    | 11 +++++++++++
 2 files changed, 12 insertions(+)
include/trace/events/huge_memory.h +1 -0
index 384e29f6bef0..bcdc57eea270 100644
@@ -25,6 +25,7 @@
 	EM( SCAN_PAGE_LRU,		"page_not_in_lru")		\
 	EM( SCAN_PAGE_LOCK,		"page_locked")			\
 	EM( SCAN_PAGE_ANON,		"page_not_anon")		\
+	EM( SCAN_PAGE_LAZYFREE,		"page_lazyfree")		\
 	EM( SCAN_PAGE_COMPOUND,		"page_compound")		\
 	EM( SCAN_ANY_PROCESS,		"no_process_for_page")		\
 	EM( SCAN_VMA_NULL,		"vma_null")			\
mm/khugepaged.c +11 -0
index de95029e3763..be1c09842ea2 100644
@@ -46,6 +46,7 @@ enum scan_result {
 	SCAN_PAGE_LRU,
 	SCAN_PAGE_LOCK,
 	SCAN_PAGE_ANON,
+	SCAN_PAGE_LAZYFREE,
 	SCAN_PAGE_COMPOUND,
 	SCAN_ANY_PROCESS,
 	SCAN_VMA_NULL,
@@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
 		folio = page_folio(page);
 		VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
 
+		if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {
+			result = SCAN_PAGE_LAZYFREE;
+			goto out;
+		}
+
 		/* See hpage_collapse_scan_pmd(). */
 		if (folio_maybe_mapped_shared(folio)) {
 			++shared;
@@ -1330,6 +1336,11 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
 		}
 		folio = page_folio(page);
 
+		if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {
+			result = SCAN_PAGE_LAZYFREE;
+			goto out_unmap;
+		}
+
 		if (!folio_test_anon(folio)) {
 			result = SCAN_PAGE_ANON;
 			goto out_unmap;
Signature · 1 line
2.51.0
reviewer
#21 lore raw

Re: [PATCH mm-new v5 4/5] mm: khugepaged: skip lazy-free folios

Quoted 69 lines · Vernon Yang

From: Vernon Yang <[email protected]>

For example, create three task: hot1 -> cold -> hot2. After all three task are created, each allocate memory 128MB. the hot1/hot2 task continuously access 128 MB memory, while the cold task only accesses its memory briefly andthen call madvise(MADV_FREE). However, khugepaged still prioritizes scanning the cold task and only scans the hot2 task after completing the scan of the cold task.

And if we collapse with a lazyfree page, that content will never be none and the deferred shrinker cannot reclaim them.

So if the user has explicitly informed us via MADV_FREE that this memory will be freed, it is appropriate for khugepaged to skip it only, thereby avoiding unnecessary scan and collapse operations to reducing CPU wastage.

Here are the performance test results:
(Throughput bigger is better, other smaller is better)

Testing on x86_64 machine:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.14 sec     |  2.93 sec     | -6.69%  |
| cycles per access   |  4.96         |  2.21         | -55.44% |
| Throughput          |  104.38 M/sec |  111.89 M/sec | +7.19%  |
| dTLB-load-misses    |  284814532    |  69597236     | -75.56% |

Testing on qemu-system-x86_64 -enable-kvm:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.35 sec     |  2.96 sec     | -11.64% |
| cycles per access   |  7.29         |  2.07         | -71.60% |
| Throughput          |  97.67 M/sec  |  110.77 M/sec | +13.41% |
| dTLB-load-misses    |  241600871    |  3216108      | -98.67% |
---
  include/trace/events/huge_memory.h |  1 +
  mm/khugepaged.c                    | 11 +++++++++++
  2 files changed, 12 insertions(+)
include/trace/events/huge_memory.h +1 -0
index 384e29f6bef0..bcdc57eea270 100644
@@ -25,6 +25,7 @@
  	EM( SCAN_PAGE_LRU,		"page_not_in_lru")		\
  	EM( SCAN_PAGE_LOCK,		"page_locked")			\
  	EM( SCAN_PAGE_ANON,		"page_not_anon")		\
+	EM( SCAN_PAGE_LAZYFREE,		"page_lazyfree")		\
  	EM( SCAN_PAGE_COMPOUND,		"page_compound")		\
  	EM( SCAN_ANY_PROCESS,		"no_process_for_page")		\
  	EM( SCAN_VMA_NULL,		"vma_null")			\
mm/khugepaged.c +1 -0
index de95029e3763..be1c09842ea2 100644
@@ -46,6 +46,7 @@ enum scan_result {
  	SCAN_PAGE_LRU,
  	SCAN_PAGE_LOCK,
  	SCAN_PAGE_ANON,
+	SCAN_PAGE_LAZYFREE,
  	SCAN_PAGE_COMPOUND,
  	SCAN_ANY_PROCESS,
  	SCAN_VMA_NULL,

mm/khugepaged.c static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 587

Quoted 4 lines
mm/khugepaged.c +1 -0
@@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
  		folio = page_folio(page);
  		VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
 
+		if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

I'm wondering if we need "cc->is_khugepaged &&" as well here?

We should allow users to enforce collapse via the madvise_collapse() path even if pages are marked lazyfree, IMHO.

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1339

Quoted 12 lines
mm/khugepaged.c +5 -0
+			result = SCAN_PAGE_LAZYFREE;
+			goto out;
+		}
+
  		/* See hpage_collapse_scan_pmd(). */
  		if (folio_maybe_mapped_shared(folio)) {
  			++shared;
@@ -1330,6 +1336,11 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
  		}
  		folio = page_folio(page);
 
+		if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

Ditto.

Quoted 7 lines
mm/khugepaged.c +4 -0
+			result = SCAN_PAGE_LAZYFREE;
+			goto out_unmap;
+		}
+
  		if (!folio_test_anon(folio)) {
  			result = SCAN_PAGE_ANON;
  			goto out_unmap;
#22 lore raw

Re: [PATCH mm-new v5 4/5] mm: khugepaged: skip lazy-free folios

Quoted 74 lines · Lance Yang
Quoted 72 lines · Vernon Yang

From: Vernon Yang <[email protected]>

For example, create three task: hot1 -> cold -> hot2. After all three task are created, each allocate memory 128MB. the hot1/hot2 task continuously access 128 MB memory, while the cold task only accesses its memory briefly andthen call madvise(MADV_FREE). However, khugepaged still prioritizes scanning the cold task and only scans the hot2 task after completing the scan of the cold task.

And if we collapse with a lazyfree page, that content will never be none and the deferred shrinker cannot reclaim them.

So if the user has explicitly informed us via MADV_FREE that this memory will be freed, it is appropriate for khugepaged to skip it only, thereby avoiding unnecessary scan and collapse operations to reducing CPU wastage.

Here are the performance test results:
(Throughput bigger is better, other smaller is better)

Testing on x86_64 machine:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.14 sec     |  2.93 sec     | -6.69%  |
| cycles per access   |  4.96         |  2.21         | -55.44% |
| Throughput          |  104.38 M/sec |  111.89 M/sec | +7.19%  |
| dTLB-load-misses    |  284814532    |  69597236     | -75.56% |

Testing on qemu-system-x86_64 -enable-kvm:

| task hot2           | without patch | with patch    |  delta  |
|---------------------|---------------|---------------|---------|
| total accesses time |  3.35 sec     |  2.96 sec     | -11.64% |
| cycles per access   |  7.29         |  2.07         | -71.60% |
| Throughput          |  97.67 M/sec  |  110.77 M/sec | +13.41% |
| dTLB-load-misses    |  241600871    |  3216108      | -98.67% |
---
  include/trace/events/huge_memory.h |  1 +
  mm/khugepaged.c                    | 11 +++++++++++
  2 files changed, 12 insertions(+)
include/trace/events/huge_memory.h +1 -0
index 384e29f6bef0..bcdc57eea270 100644
@@ -25,6 +25,7 @@
      EM( SCAN_PAGE_LRU,              "page_not_in_lru")              \
      EM( SCAN_PAGE_LOCK,             "page_locked")                  \
      EM( SCAN_PAGE_ANON,             "page_not_anon")                \
+     EM( SCAN_PAGE_LAZYFREE,         "page_lazyfree")                \
      EM( SCAN_PAGE_COMPOUND,         "page_compound")                \
      EM( SCAN_ANY_PROCESS,           "no_process_for_page")          \
      EM( SCAN_VMA_NULL,              "vma_null")                     \
mm/khugepaged.c +1 -0
index de95029e3763..be1c09842ea2 100644
@@ -46,6 +46,7 @@ enum scan_result {
      SCAN_PAGE_LRU,
      SCAN_PAGE_LOCK,
      SCAN_PAGE_ANON,
+     SCAN_PAGE_LAZYFREE,
      SCAN_PAGE_COMPOUND,
      SCAN_ANY_PROCESS,
      SCAN_VMA_NULL,
@@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
              folio = page_folio(page);
              VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
 
Quoted 6 lines

mm/khugepaged.c static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 587

Quoted 1 line
mm/khugepaged.c +1 -0
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

I'm wondering if we need "cc->is_khugepaged &&" as well here?

We should allow users to enforce collapse via the madvise_collapse() path even if pages are marked lazyfree, IMHO.

$ man madvise
MADV_COLLAPSE
        Perform a best-effort synchronous collapse of the native pages
        mapped by the memory range into Transparent Huge Pages (THPs).

The semantics of MADV_COLLAPSE are best-effort and do not imply to enforce collapsing, so we don't need "cc->is_khugepaged" here.

We can imagine that if a user simultaneously uses MADV_FREE and MADV_COLLAPSE, it indicates a misunderstanding of their semantics. As the kernel, we need to safeguard the baseline.

Quoted 23 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1339

Quoted 12 lines
mm/khugepaged.c +5 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out;
+             }
+
              /* See hpage_collapse_scan_pmd(). */
              if (folio_maybe_mapped_shared(folio)) {
                      ++shared;
@@ -1330,6 +1336,11 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
              }
              folio = page_folio(page);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

Ditto.

Quoted 7 lines
mm/khugepaged.c +4 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out_unmap;
+             }
+
              if (!folio_test_anon(folio)) {
                      result = SCAN_PAGE_ANON;
                      goto out_unmap;
reviewer
#23 lore raw

Re: [PATCH mm-new v5 4/5] mm: khugepaged: skip lazy-free folios

Quoted 5 lines · Vernon Yang
Quoted 4 lines · Lance Yang
Quoted 2 lines · Vernon Yang

From: Vernon Yang <[email protected]>

[...]

Quoted 16 lines
Quoted 10 lines

mm/khugepaged.c static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 587

Quoted 5 lines
mm/khugepaged.c +1 -0
@@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
               folio = page_folio(page);
               VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

I'm wondering if we need "cc->is_khugepaged &&" as well here?

We should allow users to enforce collapse via the madvise_collapse() path even if pages are marked lazyfree, IMHO.

$ man madvise
MADV_COLLAPSE
         Perform a best-effort synchronous collapse of the native pages
         mapped by the memory range into Transparent Huge Pages (THPs).
Quoted 6 lines

The semantics of MADV_COLLAPSE are best-effort and do not imply to enforce collapsing, so we don't need "cc->is_khugepaged" here.

We can imagine that if a user simultaneously uses MADV_FREE and MADV_COLLAPSE, it indicates a misunderstanding of their semantics. As the kernel, we need to safeguard the baseline.

No. Afraid I don't think so.

To be clear, what I meant by "enforce":

Yep, MADV_COLLAPSE is best-effort - it can fail. But when users call MADV_COLLAPSE, they're explicitly asking for collapse.

Compared to khugepaged just scanning around, that's already "enforce"
- users are actively requesting it, not passively waiting for.

Note that you're *breaking* userspace. Users would not be able to collapse the range where there are any lazyfree pages anymore, even when they explicitly call MADV_COLLAPSE.

For khugepaged, skipping lazyfree makes sense.

Quoted 24 lines
Quoted 24 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1339

Quoted 13 lines
mm/khugepaged.c +5 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out;
+             }
+
               /* See hpage_collapse_scan_pmd(). */
               if (folio_maybe_mapped_shared(folio)) {
                       ++shared;
@@ -1330,6 +1336,11 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
               }
               folio = page_folio(page);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

Ditto.

Quoted 7 lines
mm/khugepaged.c +4 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out_unmap;
+             }
+
               if (!folio_test_anon(folio)) {
                       result = SCAN_PAGE_ANON;
                       goto out_unmap;
#24 lore raw

Re: [PATCH mm-new v5 4/5] mm: khugepaged: skip lazy-free folios

Quoted 40 lines · Lance Yang
Quoted 5 lines · Vernon Yang
Quoted 4 lines · Lance Yang
Quoted 2 lines · Vernon Yang

From: Vernon Yang <[email protected]>

[...]

Quoted 16 lines
Quoted 10 lines

mm/khugepaged.c static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 587

Quoted 5 lines
mm/khugepaged.c +1 -0
@@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
               folio = page_folio(page);
               VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

I'm wondering if we need "cc->is_khugepaged &&" as well here?

We should allow users to enforce collapse via the madvise_collapse() path even if pages are marked lazyfree, IMHO.

$ man madvise
MADV_COLLAPSE
         Perform a best-effort synchronous collapse of the native pages
         mapped by the memory range into Transparent Huge Pages (THPs).
Quoted 6 lines

The semantics of MADV_COLLAPSE are best-effort and do not imply to enforce collapsing, so we don't need "cc->is_khugepaged" here.

We can imagine that if a user simultaneously uses MADV_FREE and MADV_COLLAPSE, it indicates a misunderstanding of their semantics. As the kernel, we need to safeguard the baseline.

No. Afraid I don't think so.

To be clear, what I meant by "enforce":

Yep, MADV_COLLAPSE is best-effort - it can fail. But when users call MADV_COLLAPSE, they're explicitly asking for collapse.

Quoted 8 lines

Compared to khugepaged just scanning around, that's already "enforce"
- users are actively requesting it, not passively waiting for.

Note that you're *breaking* userspace. Users would not be able to collapse the range where there are any lazyfree pages anymore, even when they explicitly call MADV_COLLAPSE.

For khugepaged, skipping lazyfree makes sense.

I got your meaning, this is equivalent to two questions:

1. Does the semantics of best-effort imply any "enforce" meaning?
2. When madvise(MADV_FREE| MADV_COLLAPSE), do we want to collapse lazyfree folios?

This is a semantic warning, and I'd like to hear others' opinions.

Quoted 25 lines
Quoted 24 lines
Quoted 24 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1339

Quoted 13 lines
mm/khugepaged.c +5 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out;
+             }
+
               /* See hpage_collapse_scan_pmd(). */
               if (folio_maybe_mapped_shared(folio)) {
                       ++shared;
@@ -1330,6 +1336,11 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
               }
               folio = page_folio(page);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

Ditto.

Quoted 7 lines
mm/khugepaged.c +4 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out_unmap;
+             }
+
               if (!folio_test_anon(folio)) {
                       result = SCAN_PAGE_ANON;
                       goto out_unmap;
reviewer
#25 lore raw

Re: [PATCH mm-new v5 4/5] mm: khugepaged: skip lazy-free folios

Quoted 42 lines · Vernon Yang
Quoted 41 lines · Lance Yang
Quoted 4 lines · Vernon Yang
Quoted 3 lines · Lance Yang
Quoted 2 lines · Vernon Yang

From: Vernon Yang <[email protected]>

[...]

Quoted 14 lines
Quoted 9 lines

mm/khugepaged.c static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 587

Quoted 5 lines
mm/khugepaged.c +1 -0
@@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
               folio = page_folio(page);
               VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

I'm wondering if we need "cc->is_khugepaged &&" as well here?

We should allow users to enforce collapse via the madvise_collapse() path even if pages are marked lazyfree, IMHO.

$ man madvise
MADV_COLLAPSE
         Perform a best-effort synchronous collapse of the native pages
         mapped by the memory range into Transparent Huge Pages (THPs).
Quoted 6 lines

The semantics of MADV_COLLAPSE are best-effort and do not imply to enforce collapsing, so we don't need "cc->is_khugepaged" here.

We can imagine that if a user simultaneously uses MADV_FREE and MADV_COLLAPSE, it indicates a misunderstanding of their semantics. As the kernel, we need to safeguard the baseline.

No. Afraid I don't think so.

To be clear, what I meant by "enforce":

Yep, MADV_COLLAPSE is best-effort - it can fail. But when users call MADV_COLLAPSE, they're explicitly asking for collapse.

Compared to khugepaged just scanning around, that's already "enforce"
- users are actively requesting it, not passively waiting for.

Note that you're *breaking* userspace. Users would not be able to collapse the range where there are any lazyfree pages anymore, even when they explicitly call MADV_COLLAPSE.

Quoted 8 lines
Quoted 1 line

For khugepaged, skipping lazyfree makes sense.

I got your meaning, this is equivalent to two questions:

1. Does the semantics of best-effort imply any "enforce" meaning?
2. When madvise(MADV_FREE| MADV_COLLAPSE), do we want to collapse lazyfree folios?

This is a semantic warning, and I'd like to hear others' opinions.

Lance is right. When user does MADV_COLLAPSE, kernel needs to try its best to collapse. It may not be in the best interest of the user to do MADV_FREE then MADV_COLLAPSE, but that is something the user has to fix - kernel does not need to think about it.

Regarding "best-effort", it is best-effort in the sense that, the madvise(MADV_COLLAPSE) is a syscall needed not for correctness, but for optimization purposes. So it is not the end of the world if the syscall fails. But, since the user has decided to do an expensive operation (syscall), kernel needs to try harder to make sure those CPU cycles weren't a waste.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

mm/khugepaged.c static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, · line 1339

Quoted 13 lines
mm/khugepaged.c +5 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out;
+             }
+
               /* See hpage_collapse_scan_pmd(). */
               if (folio_maybe_mapped_shared(folio)) {
                       ++shared;
@@ -1330,6 +1336,11 @@ static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm,
               }
               folio = page_folio(page);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

Ditto.

Quoted 7 lines
mm/khugepaged.c +4 -0
+                     result = SCAN_PAGE_LAZYFREE;
+                     goto out_unmap;
+             }
+
               if (!folio_test_anon(folio)) {
                       result = SCAN_PAGE_ANON;
                       goto out_unmap;
#26 lore raw

Re: [PATCH mm-new v5 4/5] mm: khugepaged: skip lazy-free folios

Quoted 45 lines · Dev Jain
Quoted 42 lines · Vernon Yang
Quoted 41 lines · Lance Yang
Quoted 4 lines · Vernon Yang
Quoted 3 lines · Lance Yang
Quoted 2 lines · Vernon Yang

From: Vernon Yang <[email protected]>

[...]

Quoted 14 lines
Quoted 9 lines

mm/khugepaged.c static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 587

Quoted 5 lines
mm/khugepaged.c +1 -0
@@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
               folio = page_folio(page);
               VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
 
+             if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) {

I'm wondering if we need "cc->is_khugepaged &&" as well here?

We should allow users to enforce collapse via the madvise_collapse() path even if pages are marked lazyfree, IMHO.

$ man madvise
MADV_COLLAPSE
         Perform a best-effort synchronous collapse of the native pages
         mapped by the memory range into Transparent Huge Pages (THPs).
Quoted 6 lines

The semantics of MADV_COLLAPSE are best-effort and do not imply to enforce collapsing, so we don't need "cc->is_khugepaged" here.

We can imagine that if a user simultaneously uses MADV_FREE and MADV_COLLAPSE, it indicates a misunderstanding of their semantics. As the kernel, we need to safeguard the baseline.

No. Afraid I don't think so.

To be clear, what I meant by "enforce":

Yep, MADV_COLLAPSE is best-effort - it can fail. But when users call MADV_COLLAPSE, they're explicitly asking for collapse.

Compared to khugepaged just scanning around, that's already "enforce"
- users are actively requesting it, not passively waiting for.

Note that you're *breaking* userspace. Users would not be able to collapse the range where there are any lazyfree pages anymore, even when they explicitly call MADV_COLLAPSE.

Quoted 9 lines
Quoted 8 lines
Quoted 1 line

For khugepaged, skipping lazyfree makes sense.

I got your meaning, this is equivalent to two questions:

1. Does the semantics of best-effort imply any "enforce" meaning?
2. When madvise(MADV_FREE| MADV_COLLAPSE), do we want to collapse lazyfree folios?

This is a semantic warning, and I'd like to hear others' opinions.

That said, it does feel a bit unfortunate. I was wondering whether we want to give users a hint in this case, e.g. via something like:

pr_warn("Attempt to enforce hugepage collapse on lazyfree memory");

But I'm not sure whether this is actually worth a printk, or if it would just add noise without providing actionable value.

Quoted 7 lines

Regarding "best-effort", it is best-effort in the sense that, the madvise(MADV_COLLAPSE) is a syscall needed not for correctness, but for optimization purposes. So it is not the end of the world if the syscall fails. But, since the user has decided to do an expensive operation (syscall), kernel needs to try harder to make sure those CPU cycles weren't a waste.

Thanks
Barry

#27 lore raw

PATCH v5 5/5 mm: khugepaged: set to next mm direct when mm has MMF_DISABLE_THP_COMPLETELY

In reply to Vernon Yang

From: Vernon Yang <[email protected]>

When an mm with the MMF_DISABLE_THP_COMPLETELY flag is detected during scanning, directly set khugepaged_scan.mm_slot to the next mm_slot, reduce redundant operation.

Without this patch, entering khugepaged_scan_mm_slot() next time, we will set khugepaged_scan.mm_slot to the next mm_slot.

With this patch, we will directly set khugepaged_scan.mm_slot to the next mm_slot.

---
 mm/khugepaged.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/khugepaged.c +1 -1
index be1c09842ea2..1dc19c1b1f97 100644
@@ -2558,7 +2558,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 	 * Release the current mm_slot if this mm is about to die, or
 	 * if we scanned all vmas of this mm.
 	 */
-	if (hpage_collapse_test_exit(mm) || !vma) {
+	if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
 		/*
 		 * Make sure that if mm_users is reaching zero while
 		 * khugepaged runs here, khugepaged_exit will find
Signature · 1 line
2.51.0
reviewer Reviewed-by on v5 5/5
#28 lore raw

Re: [PATCH mm-new v5 5/5] mm: khugepaged: set to next mm direct when mm has MMF_DISABLE_THP_COMPLETELY

Quoted 24 lines · Vernon Yang

From: Vernon Yang <[email protected]>

When an mm with the MMF_DISABLE_THP_COMPLETELY flag is detected during scanning, directly set khugepaged_scan.mm_slot to the next mm_slot, reduce redundant operation.

Without this patch, entering khugepaged_scan_mm_slot() next time, we will set khugepaged_scan.mm_slot to the next mm_slot.

With this patch, we will directly set khugepaged_scan.mm_slot to the next mm_slot.

---
 mm/khugepaged.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/khugepaged.c +0 -0
index be1c09842ea2..1dc19c1b1f97 100644

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result · line 2560

Quoted 3 lines
mm/khugepaged.c +0 -0
@@ -2558,7 +2558,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
 	 * Release the current mm_slot if this mm is about to die, or
 	 * if we scanned all vmas of this mm.
 	 */

Need to update comment: "...or THP got disabled".

Quoted 5 lines
mm/khugepaged.c +1 -1
-	if (hpage_collapse_test_exit(mm) || !vma) {
+	if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
 		/*
 		 * Make sure that if mm_users is reaching zero while
 		 * khugepaged runs here, khugepaged_exit will find
#29 lore raw

Re: [PATCH mm-new v5 5/5] mm: khugepaged: set to next mm direct when mm has MMF_DISABLE_THP_COMPLETELY

Quoted 21 lines · Dev Jain
Quoted 19 lines · Vernon Yang

From: Vernon Yang <[email protected]>

When an mm with the MMF_DISABLE_THP_COMPLETELY flag is detected during scanning, directly set khugepaged_scan.mm_slot to the next mm_slot, reduce redundant operation.

Without this patch, entering khugepaged_scan_mm_slot() next time, we will set khugepaged_scan.mm_slot to the next mm_slot.

With this patch, we will directly set khugepaged_scan.mm_slot to the next mm_slot.

---
 mm/khugepaged.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result · line 2560

Quoted 8 lines
mm/khugepaged.c +0 -0
index be1c09842ea2..1dc19c1b1f97 100644
@@ -2558,7 +2558,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
       * Release the current mm_slot if this mm is about to die, or
       * if we scanned all vmas of this mm.
       */

Need to update comment: "...or THP got disabled".

LGTM, I will update comment in the next version. Thanks!

Quoted 7 lines
Quoted 5 lines
mm/khugepaged.c +1 -1
-     if (hpage_collapse_test_exit(mm) || !vma) {
+     if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
              /*
               * Make sure that if mm_users is reaching zero while
               * khugepaged runs here, khugepaged_exit will find
Reviewed-by
#30 lore raw

Re: [PATCH mm-new v5 5/5] mm: khugepaged: set to next mm direct when mm has MMF_DISABLE_THP_COMPLETELY

In reply to Dev Jain
Quoted 22 lines · Dev Jain
Quoted 19 lines · Vernon Yang

From: Vernon Yang <[email protected]>

When an mm with the MMF_DISABLE_THP_COMPLETELY flag is detected during scanning, directly set khugepaged_scan.mm_slot to the next mm_slot, reduce redundant operation.

Without this patch, entering khugepaged_scan_mm_slot() next time, we will set khugepaged_scan.mm_slot to the next mm_slot.

With this patch, we will directly set khugepaged_scan.mm_slot to the next mm_slot.

---
 mm/khugepaged.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result · line 2560

Quoted 8 lines
mm/khugepaged.c +0 -0
index be1c09842ea2..1dc19c1b1f97 100644
@@ -2558,7 +2558,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
       * Release the current mm_slot if this mm is about to die, or
       * if we scanned all vmas of this mm.
       */

Need to update comment: "...or THP got disabled".

+1
With that,