[PATCH v2 21/35] hugetlbfs: don't skip private VMAs when unmapping on truncation

From: David Hildenbrand (Arm)

Date: Thu Dec 03 2026 - 06:43:10 EST


When truncating a file or punching a hole, hugetlb_vmdelete_list() unmaps
the range from all VMAs that map the file. It skips VMAs whose VMA lock it
cannot trylock, which is fine for shared VMAs: remove_inode_hugepages()
unmaps their page cache folios later.

However, private VMAs that own their reservations have a VMA lock as
well, so, for example, a concurrent page fault or MADV_DONTNEED in such a
VMA makes us skip that VMA, and nobody unmaps its anonymous folios or, on
truncation, its uffd-wp markers later.

Consequently, they stay mapped beyond EOF after a truncation, or inside a
punched hole.

We only need the VMA lock here for PMD table sharing, which synchronizes
on the shareable lock. We already hold the i_mmap lock in write mode.

Let's fix it like commit da48afefa78d ("hugetlbfs: fix unmapping folios
from private VMAs") did for hugetlb_unmap_file_folio(): only take the VMA
lock for VMAs with a shareable lock, and always unmap other VMAs only
under i_mmap lock.

This has low urgency, as it requires racing with e.g., a page
fault in the private VMA.

Fixes: bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
fs/hugetlbfs/inode.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index db2af960ee7dc..5f44236785b5a 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -482,10 +482,16 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,
*/
mapping_rmap_tree_foreach(vma, mapping, start,
end ? end - 1 : ULONG_MAX) {
+ const bool need_vma_lock = __vma_shareable_lock(vma);
unsigned long v_start;
unsigned long v_end;

- if (!hugetlb_vma_trylock_write(vma))
+ /*
+ * We only need additional VMA locking here for PMD table
+ * sharing, which synchronizes on the shareable lock. Other VMAs
+ * never share PMD tables, so we can unmap them without it.
+ */
+ if (need_vma_lock && !hugetlb_vma_trylock_write(vma))
continue;

v_start = vma_offset_start(vma, start);
@@ -493,12 +499,8 @@ hugetlb_vmdelete_list(struct address_space *mapping, pgoff_t start,

unmap_hugepage_range(vma, v_start, v_end, NULL, zap_flags);

- /*
- * Note that vma lock only exists for shared/non-private
- * vmas. Therefore, lock is not held when calling
- * unmap_hugepage_range for private vmas.
- */
- hugetlb_vma_unlock_write(vma);
+ if (need_vma_lock)
+ hugetlb_vma_unlock_write(vma);
}
}


--
2.56.1