[PATCH 06/15] mm/hugetlb: fix mremap() racing with page migration

From: David Hildenbrand (Arm)

Date: Wed Oct 28 2026 - 17:13:49 EST


Migration of anon hugetlb folios walks the anon_vma, so the file's
i_mmap_rwsem does not serialize it against move_hugetlb_page_tables().

There are various issues with that. Most importantly, move_huge_pte()
releases both page table locks before the TLB is flushed. Migration can
then find the destination entry and finish migration, reusing the old
folio while the TLB still has entries cached.

Hold the anon_vma write lock across the move and the final TLB flush.
This also prevents concurrent rmap walks from missing entries moved
during rollback.

When we must allocate a destination page table, flush the TLB before
dropping the anon_vma lock, then reacquire it after allocation.

Dropping the lock only works around lockdep: hugetlb folios are not
reclaimed or migrated by allocation compaction, but their anon_vmas
share the lock class used by ordinary anonymous memory. Having a
separate lock class for hugetlb's anon_vma (like already done for
i_mmap_rwsem via hugetlbfs_i_mmap_rwsem_key) requires more thought.

There is a concern that this can block page migration of anonymous
folios quite a while ... which is not really different to what happens
to page-cache folios today. If ever a problem, we can temporarily
release the lock.

So let's keep it simple for now.

It is a shame that hugetlb has to implement everything core-mm implements
in a different way that just screams for problems.

Fixes: 550a7d60bd5e ("mm, hugepages: add mremap() support for hugepage backed vma")
Cc: stable@xxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/hugetlb.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 55f7d692ed0d..2fdd72897db8 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5219,6 +5219,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
{
struct hstate *h = hstate_vma(vma);
struct address_space *mapping = vma->vm_file->f_mapping;
+ struct anon_vma *anon_vma = vma->anon_vma;
unsigned long sz = huge_page_size(h);
struct mm_struct *mm = vma->vm_mm;
unsigned long old_end = old_addr + len;
@@ -5242,6 +5243,8 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
/* Prevent race with file truncation */
hugetlb_vma_lock_write(vma);
i_mmap_lock_write(mapping);
+ if (anon_vma)
+ anon_vma_lock_write(anon_vma);
for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
const unsigned long offset_to_last_entry =
(old_addr | last_addr_mask) - old_addr;
@@ -5261,7 +5264,20 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
continue;
}

- dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
+ dst_pte = hugetlb_walk(new_vma, new_addr, sz);
+ if (!dst_pte) {
+ /*
+ * We can allocate memory under anon_vma lock with hugetlb,
+ * but lockdep does not know about that. So for now we simply
+ * flush the TLB and temporarily drop the lock.
+ */
+ tlb_flush_mmu_tlbonly(&tlb);
+ if (anon_vma)
+ anon_vma_unlock_write(anon_vma);
+ dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
+ if (anon_vma)
+ anon_vma_lock_write(anon_vma);
+ }
if (!dst_pte)
break;

@@ -5269,7 +5285,10 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
tlb_remove_huge_tlb_entry(h, &tlb, src_pte, old_addr);
}

+ /* Flush pending moves before allowing migration to walk the entries. */
tlb_flush_mmu_tlbonly(&tlb);
+ if (anon_vma)
+ anon_vma_unlock_write(anon_vma);
huge_pmd_unshare_flush(&tlb, vma);

mmu_notifier_invalidate_range_end(&range);

--
2.43.0