[PATCH] fix
From: Lorenzo Stoakes (ARM)
Date: Wed Jul 29 2026 - 13:33:08 EST
---
mm/hugetlb.c | 103 +++++++++++++++++++++++++++++++++++++++------------
1 file changed, 79 insertions(+), 24 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index e93c4d2456aa..88ff2c4ad483 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5077,6 +5077,79 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
spin_unlock(dst_ptl);
}
+/* Returns last address processed */
+static unsigned long prealloc_or_move_page_tables(struct vm_area_struct *vma,
+ struct vm_area_struct *new_vma, unsigned long old_addr,
+ unsigned long new_addr, unsigned long len, unsigned long sz,
+ unsigned long last_addr_mask, struct mmu_gather *tlb,
+ bool is_prealloc)
+{
+ unsigned long old_end = old_addr + len;
+ struct hstate *h = hstate_vma(vma);
+ struct mm_struct *mm = vma->vm_mm;
+ pte_t *src_pte, *dst_pte;
+
+ hugetlb_vma_assert_locked(vma);
+
+ for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
+ src_pte = hugetlb_walk(vma, old_addr, sz);
+ if (!src_pte) {
+ old_addr |= last_addr_mask;
+ new_addr |= last_addr_mask;
+ continue;
+ }
+ if (huge_pte_none(huge_ptep_get(mm, old_addr, src_pte)))
+ continue;
+
+ if (is_prealloc) {
+ if (!huge_pte_alloc(mm, new_vma, new_addr, sz))
+ break;
+ continue;
+ }
+
+ if (huge_pmd_unshare(tlb, vma, old_addr, src_pte)) {
+ old_addr |= last_addr_mask;
+ new_addr |= last_addr_mask;
+ continue;
+ }
+
+ dst_pte = hugetlb_walk(new_vma, new_addr, sz);
+ if (!dst_pte)
+ break;
+
+ move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte, sz);
+ tlb_remove_huge_tlb_entry(h, tlb, src_pte, old_addr);
+ }
+
+ return old_addr;
+}
+
+/* Returns true if preallocation succeeded across range, otherwise false. */
+static bool prealloc_hugetlb_page_tables(struct vm_area_struct *vma,
+ struct vm_area_struct *new_vma, unsigned long old_addr,
+ unsigned long new_addr, unsigned long len, unsigned long sz,
+ unsigned long last_addr_mask)
+{
+ unsigned long old_end = old_addr + len;
+ unsigned long addr_end;
+
+ addr_end = prealloc_or_move_page_tables(vma, new_vma, old_addr, new_addr,
+ len, sz, last_addr_mask, NULL, /*is_prealloc=*/true);
+
+ return addr_end >= old_end;
+}
+
+/* Returns last processed address. */
+static unsigned long __move_hugetlb_page_tables(struct vm_area_struct *vma,
+ struct vm_area_struct *new_vma, unsigned long old_addr,
+ unsigned long new_addr, unsigned long len, unsigned long sz,
+ unsigned long last_addr_mask, struct mmu_gather *tlb)
+{
+ i_mmap_assert_write_locked(vma->vm_file->f_mapping);
+ return prealloc_or_move_page_tables(vma, new_vma, old_addr, new_addr,
+ len, sz, last_addr_mask, tlb, /*is_prealloc=*/false);
+}
+
int move_hugetlb_page_tables(struct vm_area_struct *vma,
struct vm_area_struct *new_vma,
unsigned long old_addr, unsigned long new_addr,
@@ -5088,9 +5161,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
struct mm_struct *mm = vma->vm_mm;
unsigned long old_end = old_addr + len;
unsigned long last_addr_mask;
- pte_t *src_pte, *dst_pte;
struct mmu_notifier_range range;
struct mmu_gather tlb;
+ bool preallocated;
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, old_addr,
old_end);
@@ -5106,30 +5179,12 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
last_addr_mask = hugetlb_mask_last_page(h);
/* Prevent race with file truncation */
hugetlb_vma_lock_write(vma);
+ preallocated = prealloc_hugetlb_page_tables(vma, new_vma, old_addr,
+ new_addr, len, sz, last_addr_mask);
i_mmap_lock_write(mapping);
- for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
- src_pte = hugetlb_walk(vma, old_addr, sz);
- if (!src_pte) {
- old_addr |= last_addr_mask;
- new_addr |= last_addr_mask;
- continue;
- }
- if (huge_pte_none(huge_ptep_get(mm, old_addr, src_pte)))
- continue;
-
- if (huge_pmd_unshare(&tlb, vma, old_addr, src_pte)) {
- old_addr |= last_addr_mask;
- new_addr |= last_addr_mask;
- continue;
- }
-
- dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
- if (!dst_pte)
- break;
-
- move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte, sz);
- tlb_remove_huge_tlb_entry(h, &tlb, src_pte, old_addr);
- }
+ if (preallocated)
+ old_addr = __move_hugetlb_page_tables(vma, new_vma, old_addr,
+ new_addr, len, sz, last_addr_mask, &tlb);
tlb_flush_mmu_tlbonly(&tlb);
huge_pmd_unshare_flush(&tlb, vma);
--
2.55.0
--
Cheers,
David