[PATCH] ideas
From: Lorenzo Stoakes (ARM)
Date: Wed Aug 05 2026 - 06:35:13 EST
---
mm/huge_memory.c | 32 +++++++++++++++++++++++++++++---
1 file changed, 29 insertions(+), 3 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index abc65d608c23..5fa01364f089 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -111,6 +111,34 @@ static bool vma_is_special_huge(const struct vm_area_struct *vma)
return vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT);
}
+static bool should_obey_thp_file_tunables(const struct vm_area_struct *vma,
+ bool forced_collapse)
+{
+ if (forced_collapse)
+ return false;
+ VM_WARN_ON_ONCE(vma_is_anonymous(vma));
+ /* Huge PFN mappings allocate no folios so the policy doesn't apply. */
+ if (vma_test(vma, VMA_PFNMAP_BIT) && vma->vm_ops->huge_fault)
+ return false;
+ return true;
+}
+
+static bool can_thp_collapse_file(const struct vm_area_struct *vma,
+ vm_flags_t vm_flags, bool forced_collapse)
+{
+ /* Override THP tunables? */
+ if (!should_obey_thp_file_tunables(vma, forced_collapse))
+ return true;
+ /* THP=always? */
+ if (hugepage_global_always())
+ return true;
+ /* THP=madvise? */
+ if (!hugepage_global_enabled())
+ return false;
+ /* Has VMA had madvise(..., MADV_HUGEPAGE) applied to it? */
+ return vm_flags & VM_HUGEPAGE;
+}
+
unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
vm_flags_t vm_flags,
enum tva_type type,
@@ -188,9 +216,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
* Enforce THP collapse requirements as necessary. Anonymous vmas
* were already handled in thp_vma_allowable_orders().
*/
- if (!forced_collapse &&
- (!hugepage_global_enabled() || (!(vm_flags & VM_HUGEPAGE) &&
- !hugepage_global_always())))
+ if (!can_thp_collapse_file(vma, vm_flags, forced_collapse))
return 0;
/*
--
2.55.0