Re: [PATCH v2 08/15] mm/hugetlb: serialize page-cache allocations with truncation
From: Oscar Salvador (SUSE)
Date: Thu Nov 19 2026 - 04:10:10 EST
On Mon, Nov 16, 2026 at 07:29:29PM +0100, David Hildenbrand (Arm) wrote:
> Truncation can remove an inode reservation while a fault is still
> allocating its folio. UFFDIO_COPY and memfd pinning can also consume
> reservations beyond the new i_size. This can underflow resv_huge_pages
> and leave folios beyond EOF.
>
> The main issue is that truncation takes the fault mutex for folios in
> the page-cache, but it has no clue about faults that are still being
> processed concurrently.
>
> Matthew Wilcox raised mapping->invalidate_lock in a different upstream
> hugetlb locking discussion:
>
> "although I wonder if we still need the fault mutex array, given that we
> now have mapping->invalidate_lock?"
>
> While it likely cannot remove the mutex array entirely (concurrent page
> faults have to sync ... ), we can at least use it to fix this problem.
>
> Hold mapping->invalidate_lock shared across the size check, allocation
> and page cache insertion or reservation rollback. Truncation takes it
> exclusively around i_size_write() only for now: it waits for concurrent
> allocations to finish updating the page-cache or rolling back reservations
> before changing the size. Subsequent allocations see the updated i_size.
>
> Let truncation release the lock for now before unmapping and removing
> folios to preserve the existing lock ordering. Cached-folio faults and
> private allocations do not acquire the new lock.
>
> Use it to synchronize with truncation, keeping the fault mutexes to
> serialize allocations for the same index.
>
> Locking order is now:
>
> fault mutex -> hugetlb VMA lock (read) -> invalidate lock (read) ->
> folio lock -> PTE lock
>
> And on truncation:
>
> i_rwsem (write) -> invalidate lock (write)
>
> Add hugetlb_pgoff_valid() to not repeat the same size check multiple
> times. Memfd needs a bit of care, as it traditionally allowed partial
> folios to be pinned: the size check in memfd_pin_folios() allows for
> that.
>
> Fixes: 1c9e8def43a3 ("userfaultfd: hugetlbfs: add UFFDIO_COPY support for shared mappings")
> Fixes: 188a39725ad7 ("hugetlbfs: revert use i_mmap_rwsem to address page fault/truncate race")
> Fixes: 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios")
> Cc: stable@xxxxxxxxxx
> Reported-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
> Link: https://lore.kernel.org/all/ajMfdQf-Fspwmstr@xxxxxxxxxxxxxxxxxxxx/
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Acked-by: Oscar Salvador (SUSE) <osalvador@xxxxxxxxxx>
Just one thing:
mm/rmap.c has a description of the lock ordering for hugetlb:
"
* hugetlbfs PageHuge() take locks in this order:
* hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
* vma_lock (hugetlb specific lock for pmd_sharing)
* mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
* folio_lock
"
We should update it to reflect the updated locking order, right?
--
Oscar Salvador
SUSE Labs