Re: [RFC PATCH 01/11] mm: khugepaged: add khugepaged_max_ptes_none check in collapse_file()

From: Baolin Wang
Date: Wed Aug 20 2025 - 21:11:19 EST




On 2025/8/20 18:13, Dev Jain wrote:

On 20/08/25 2:37 pm, Baolin Wang wrote:
Similar to the anonymous folios collapse, we should also check the 'khugepaged_max_ptes_none'
when trying to collapse shmem/file folios.

Signed-off-by: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
---
  mm/khugepaged.c | 7 +++++++
  1 file changed, 7 insertions(+)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 5a3386043f39..5d4493b77f3c 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2125,6 +2125,13 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
                      }
                  }
                  nr_none++;
+
+                if (cc->is_khugepaged && nr_none > khugepaged_max_ptes_none) {
+                    result = SCAN_EXCEED_NONE_PTE;
+                    count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
+                    goto xa_locked;
+                }
+
                  index++;
                  continue;
              }

Isn't this already being checked in collapse_scan_file(), in the block
if (cc->is_khugepaged && present < HPAGE_PMD_NR - khugepaged_max_ptes_none)?

Yes, as I said in the commit message, this follows the same behavior as for anonymous folios, by checking the folio’s present state again before isolating it, since the folio's present state could change after the check in collapse_scan_file().

In addition, and importantly, this prepares for the mTHP collapse check, see patch 4.