[PATCH v2 22/35] mm/hugetlb: serialize private faults with truncation

From: David Hildenbrand (Arm)

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


When we fault in a folio in a MAP_PRIVATE hugetlb mapping at an index
without a page cache folio, hugetlb_no_page() checks that the index is
not beyond EOF, allocates a new anonymous folio and maps it. Truncation
first updates i_size under the invalidate lock, and then unmaps the
truncated range from all VMAs.

For page cache folios, we recently started rechecking EOF under
filemap_invalidate_lock_shared() to handle exactly that.

However, nothing serializes the allocation of anonymous folios with
truncation: if truncation unmaps the range after we checked EOF, but
before we mapped the new folio, the folio stays mapped beyond EOF.

Consequently, accessing the truncated range does not raise SIGBUS, and
the process keeps the folio mapped until it unmaps it.

Reproduced by artificially delaying hugetlb_no_page() before allocating
the folio, while another thread truncates the file: the page beyond EOF
stays mapped.

Let's fix it by also rechecking EOF under the invalidate lock for
anonymous folios, and holding it until we mapped the folio, such that
truncation will unmap it.

Like faults in shared mappings, faults in private mappings now briefly take
the invalidate lock in read mode, and might have to wait for a concurrent
truncation. Which seems to be the right thing to do.

This has low urgency, as it requires racing a page fault in a private
mapping with truncation. No need to CC stable.

Fixes: fa27759af4a6 ("hugetlb: clean up code checking for fault/truncation races")
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/hugetlb.c | 25 +++++++++++++++----------
mm/rmap.c | 2 +-
2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 1555a326efe4c..105cc2840f988 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6002,6 +6002,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE;
u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
bool folio_locked = true, new_anon_folio = false;
+ bool invalidate_locked = false;
struct vm_area_struct *vma = vmf->vma;
struct mm_struct *mm = vma->vm_mm;
struct hstate *h = hstate_vma(vma);
@@ -6061,15 +6062,19 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
ret = __vmf_anon_prepare(vmf);
if (unlikely(ret))
goto out;
- } else {
- /* Recheck EOF before consuming an inode reservation. */
- filemap_invalidate_lock_shared(mapping);
- if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false)) {
- filemap_invalidate_unlock_shared(mapping);
- goto out;
- }
+ ret = VM_FAULT_SIGBUS;
}

+ /*
+ * Recheck EOF and keep truncation out until the folio is in the
+ * page cache, or until we mapped the new anonymous folio:
+ * truncation unmaps or removes it afterwards.
+ */
+ filemap_invalidate_lock_shared(mapping);
+ invalidate_locked = true;
+ if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false))
+ goto out;
+
folio = alloc_hugetlb_folio(vma, vmf->address, false);
if (IS_ERR(folio)) {
/*
@@ -6088,8 +6093,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
ret = vmf_error(PTR_ERR(folio));
else
ret = 0;
- if (pagecache_folio)
- filemap_invalidate_unlock_shared(mapping);
goto out;
}
folio_zero_user(folio, vmf->real_address);
@@ -6110,10 +6113,10 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
folio);
folio_put(folio);
ret = VM_FAULT_SIGBUS;
- filemap_invalidate_unlock_shared(mapping);
goto out;
}
filemap_invalidate_unlock_shared(mapping);
+ invalidate_locked = false;
folio_set_hugetlb_migratable(folio);
} else {
new_anon_folio = true;
@@ -6212,6 +6215,8 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
*/
spin_unlock(vmf->ptl);
out:
+ if (invalidate_locked)
+ filemap_invalidate_unlock_shared(mapping);
hugetlb_vma_unlock_read(vma);

/*
diff --git a/mm/rmap.c b/mm/rmap.c
index f35b671d26514..4193793158bc1 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -48,7 +48,7 @@
* hugetlbfs PageHuge() take locks in this order:
* hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
* vma_lock (hugetlb specific lock for pmd_sharing)
- * mapping->invalidate_lock (when allocating page cache folios)
+ * mapping->invalidate_lock (when allocating folios)
* folio_lock
* mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
*/

--
2.56.1