[PATCH 21/60] mm/khugepaged: restrict collapse to common memory
From: Gregory Price via B4 Relay
Date: Fri Nov 20 2026 - 13:19:18 EST
From: "Gregory Price (Meta)" <gourry@xxxxxxxxxx>
Collapse is an opportunistic MM service and should operate only on
common memory. The anonymous scan and isolation paths already filter
zone device for the same reason.
Replace the zone_device check with page_is_common_memory().
collapse_scan_file() gains a folio_is_common_memory() check. Page-cache
folios are never ZONE_DEVICE, so there it only filters memory outside
N_MEMORY_COMMON.
collapse_file() rechecks once each folio is locked, since a folio can be
added or migrated after collapse_scan_file() looked at it.
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/khugepaged.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index a930d99f89da..b1c6bb95a936 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -628,7 +628,7 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
goto out;
}
page = vm_normal_page(vma, addr, pteval);
- if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
+ if (unlikely(!page) || unlikely(!page_is_common_memory(page))) {
result = SCAN_PAGE_NULL;
goto out;
}
@@ -1673,7 +1673,7 @@ static enum scan_result collapse_scan_anon(struct vm_area_struct *vma,
}
page = vm_normal_page(vma, addr, pteval);
- if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
+ if (unlikely(!page) || unlikely(!page_is_common_memory(page))) {
result = SCAN_PAGE_NULL;
goto out_unmap;
}
@@ -2397,6 +2397,12 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
goto out_unlock;
}
+ /* A folio may have been added or migrated since the scan. */
+ if (unlikely(!folio_is_common_memory(folio))) {
+ result = SCAN_PAGE_NULL;
+ goto out_unlock;
+ }
+
if (!is_shmem && (folio_test_dirty(folio) ||
folio_test_writeback(folio))) {
/*
@@ -2737,6 +2743,13 @@ static enum scan_result collapse_scan_file(struct mm_struct *mm,
continue;
}
+ if (unlikely(!folio_is_common_memory(folio))) {
+ result = SCAN_PAGE_NULL;
+ failed_pfn = folio_pfn(folio);
+ folio_put(folio);
+ break;
+ }
+
if (is_pmd_order(folio_order(folio))) {
result = SCAN_PTE_MAPPED_HUGEPAGE;
failed_pfn = folio_pfn(folio);
--
2.56.1