[PATCH 08/12] mm: khugepaged: add a helper to get the minimal collapse order
From: Baolin Wang
Date: Thu Sep 17 2026 - 23:59:25 EST
shmem mTHP collapse will need to support order-1 collapse in the future,
so add a helper to retrieve the minimal collapse order as a preparation.
Also rename KHUGEPAGED_MIN_MTHP_ORDER to KHUGEPAGED_MIN_ANON_MTHP_ORDER
for better readability, and add a new COLLAPSE_MIN_FILE_MTHP_ORDER for
the shmem minimal collapse order.
Signed-off-by: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
---
mm/collapse.h | 3 ++-
mm/khugepaged.c | 10 +++++++++-
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/mm/collapse.h b/mm/collapse.h
index e52969965262..d2fff6b5e1ec 100644
--- a/mm/collapse.h
+++ b/mm/collapse.h
@@ -8,7 +8,8 @@
#include <linux/types.h>
#define COLLAPSE_MAX_PTES_LIMIT (HPAGE_PMD_NR - 1)
-#define COLLAPSE_MIN_MTHP_ORDER 2
+#define COLLAPSE_MIN_ANON_MTHP_ORDER 2
+#define COLLAPSE_MIN_FILE_MTHP_ORDER 1
enum scan_result {
SCAN_FAIL,
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 6c648ede24c4..64605a72ac2b 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -302,6 +302,14 @@ static bool pte_none_or_zero(pte_t pte)
return pte_present(pte) && is_zero_pfn(pte_pfn(pte));
}
+static unsigned int collapse_min_mthp_order(struct file *file)
+{
+ if (file)
+ return COLLAPSE_MIN_FILE_MTHP_ORDER;
+
+ return COLLAPSE_MIN_ANON_MTHP_ORDER;
+}
+
/**
* collapse_max_ptes_none - Calculate maximum allowed empty PTEs or PTEs mapping
* the shared zeropage for the given collapse operation.
@@ -1485,7 +1493,7 @@ static enum scan_result mthp_collapse(struct mm_struct *mm, unsigned long addres
* any smaller order enabled. When at the smallest order
* we must always move to the next offset.
*/
- if (order > COLLAPSE_MIN_MTHP_ORDER &&
+ if (order > collapse_min_mthp_order(NULL) &&
(cc->scan_orders & GENMASK(order - 1, 0))) {
order--;
continue;
--
2.47.3