[PATCH 13/21] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails
From: David Hildenbrand (Arm)
Date: Wed Nov 25 2026 - 17:36:59 EST
From: Chris Mason <mason@xxxxxxxxxx>
If we split a VMA that has shared PMD page tables mapped, and allocating
the hugetlb VMA lock for the new VMA fails, a fault in the new VMA can
race with a hole punch and access page tables that were already freed.
__split_vma() calls hugetlb_vm_op_open() for the new VMA, which
allocates its hugetlb VMA lock in hugetlb_vma_lock_alloc(). If that
allocation fails, the new VMA is left without a lock.
A VMA that has shared PMD page tables needs the hugetlb VMA lock, since
the lock synchronizes huge_pmd_unshare() with concurrent page table
walks, including faults.
want_pmd_share() and page_table_shareable() prevent new sharing without
the lock, but a split leaves previously shared page tables in place for
the new VMA, and hugetlb_vm_op_open() can't currently return an error if
the allocation fails.
This can, for example, happen for an OOM victim. Then, nothing prevents
a hole punch from unsharing the page table of the new VMA while a fault
in the new VMA is still using it, and the last other sharer from freeing
it:
fault in the new VMA hole punch, then munmap()
==================== =========================
hugetlb_vma_lock_read()
no lock, does nothing
huge_pte_alloc()
pointer into shared table
folio_zero_user()
hugetlb_vma_trylock_write()
no lock, returns 1
huge_pmd_unshare(new VMA)
pud_clear()
last sharer frees the table
huge_pte_lock()
set_huge_pte_at()
Let's fix it by unsharing the PMD page tables of a new VMA that has no
lock. Also, exit hugetlb_split() early if the original VMA has no lock,
as it cannot have shared PMD page tables then.
This has low urgency, as it requires allocating the hugetlb VMA lock to
fail during a split, which is practically only possible for OOM victims,
and a race with a hole punch.
Fixes: 131a79b474e9 ("hugetlb: fix vma lock handling during split vma and range unmapping")
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
[ david: return early if the original VMA has no lock, adjust comment
and description ]
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
include/linux/hugetlb.h | 6 ++++--
mm/hugetlb.c | 17 ++++++++++++++++-
mm/vma.c | 2 +-
tools/testing/vma/include/stubs.h | 3 ++-
4 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 49dad5ae57512..aea18895e1459 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -274,7 +274,8 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
unsigned long cp_flags);
void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
void fixup_hugetlb_reservations(struct vm_area_struct *vma);
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+ unsigned long addr);
unsigned int arch_hugetlb_cma_order(void);
@@ -457,7 +458,8 @@ static inline void fixup_hugetlb_reservations(struct vm_area_struct *vma)
{
}
-static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {}
+static inline void hugetlb_split(struct vm_area_struct *vma,
+ struct vm_area_struct *new, unsigned long addr) {}
#endif /* !CONFIG_HUGETLB_PAGE */
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 0b0f6e17b8b06..2bba0940a40d5 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4887,7 +4887,8 @@ static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
return 0;
}
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+ unsigned long addr)
{
/*
* PMD sharing is only possible for PUD_SIZE-aligned address ranges
@@ -4898,8 +4899,13 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
* walks (except hardware and gup_fast()).
*/
vma_assert_write_locked(vma);
+ vma_assert_write_locked(new);
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
+ /* Shared PMDs require a hugetlb VMA lock. */
+ if (!__vma_shareable_lock(vma))
+ return;
+
if (addr & ~PUD_MASK) {
unsigned long floor = addr & PUD_MASK;
unsigned long ceil = floor + PUD_SIZE;
@@ -4918,6 +4924,15 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
}
}
+
+ /*
+ * If we failed to allocate the hugetlb VMA lock, unshare everything
+ * that the new VMA covers.
+ */
+ if (!__vma_shareable_lock(new))
+ hugetlb_unshare_pmds(new, ALIGN(new->vm_start, PUD_SIZE),
+ ALIGN_DOWN(new->vm_end, PUD_SIZE),
+ /* take_locks = */ false);
}
static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
diff --git a/mm/vma.c b/mm/vma.c
index c82e9306b11ac..183bfb8ba4703 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -590,7 +590,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
*/
vma_adjust_trans_huge(vma, vma->vm_start, addr, NULL);
if (vma_is_hugetlb(vma))
- hugetlb_split(vma, addr);
+ hugetlb_split(vma, new, addr);
if (new_below) {
vma->vm_start = addr;
diff --git a/tools/testing/vma/include/stubs.h b/tools/testing/vma/include/stubs.h
index 9f5b8e996abf6..6ddecf587d9be 100644
--- a/tools/testing/vma/include/stubs.h
+++ b/tools/testing/vma/include/stubs.h
@@ -466,7 +466,8 @@ static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
{
}
-static inline void hugetlb_split(struct vm_area_struct *, unsigned long) {}
+static inline void hugetlb_split(struct vm_area_struct *,
+ struct vm_area_struct *, unsigned long) {}
static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
{
--
2.56.1