[PATCH mm-new v2 2/3] mm/khugepaged: use VM_WARN_ON_FOLIO instead of VM_BUG_ON_FOLIO for non-anon folios
From: Lance Yang
Date: Mon Oct 06 2025 - 10:46:11 EST
From: Lance Yang <lance.yang@xxxxxxxxx>
As Zi pointed out, we should avoid crashing the kernel for conditions
that can be handled gracefully. Encountering a non-anonymous folio in an
anonymous VMA is a bug, but a warning is sufficient.
This patch changes the VM_BUG_ON_FOLIO(!folio_test_anon(folio)) to a
VM_WARN_ON_FOLIO() in both __collapse_huge_page_isolate() and
hpage_collapse_scan_pmd(), and then aborts the scan with SCAN_PAGE_ANON.
Making more of the scanning logic common between hpage_collapse_scan_pmd()
and __collapse_huge_page_isolate(), as suggested by Dev.
Suggested-by: Dev Jain <dev.jain@xxxxxxx>
Suggested-by: Zi Yan <ziy@xxxxxxxxxx>
Signed-off-by: Lance Yang <lance.yang@xxxxxxxxx>
---
mm/khugepaged.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 808523f92c7b..87a8df90b3a6 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -573,7 +573,11 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
}
folio = page_folio(page);
- VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio);
+ if (!folio_test_anon(folio)) {
+ VM_WARN_ON_FOLIO(true, folio);
+ result = SCAN_PAGE_ANON;
+ goto out;
+ }
/* See hpage_collapse_scan_pmd(). */
if (folio_maybe_mapped_shared(folio)) {
@@ -1335,6 +1339,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
folio = page_folio(page);
if (!folio_test_anon(folio)) {
+ VM_WARN_ON_FOLIO(true, folio);
result = SCAN_PAGE_ANON;
goto out_unmap;
}
--
2.49.0