Re: [PATCH v2 16/35] hugetlbfs: fix deadlock between hole punching and memory failure

From: Jose A. Perez de Azpillaga

Date: Fri Dec 04 2026 - 06:05:17 EST


On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote:
> From: Chris Mason <mason@xxxxxxxxxx>
>
> Punching a hole that isn't aligned to the huge page size can deadlock
> against memory_failure() on a mapped folio at the edge of the hole.
>
> hugetlbfs_punch_hole() locks and zeroes the partial folios with
> i_mmap_rwsem held for write, following the hugetlbfs locking order
> documented in mm/rmap.c: i_mmap_rwsem before the folio lock.
>
> However, try_memory_failure_hugetlb() locks the folio first, and then
> collects the processes to kill under i_mmap_rwsem:
>
> hugetlbfs_punch_hole() try_memory_failure_hugetlb()
> ====================== ============================
> i_mmap_lock_write()
> folio_lock()
> hugetlbfs_zero_partial_page() hwpoison_user_mappings()
> filemap_lock_hugetlb_folio() collect_procs_file()
> waits for the folio lock i_mmap_lock_read()
> waits for the writer
>
> In fact, the documentation is outdated: hugetlbfs_punch_hole() is the
> *only* one that still takes i_mmap_rwsem before the folio lock. All
> other scenarios always take the folio lock before the i_mmap_rwsem.
>
> Only memory failure can deadlock with the hole punch:
>
> * truncation and hole punching of full folios are serialized against it
> through the inode lock
> * unmap_ref_private() holds the lock of an anon folio, not of a page
> cache folio that the hole punch zeroes
> * migration only tries to take the i_mmap_rwsem.
>
> There is no need to zero the partial folios under i_mmap_rwsem. The lock
> only protects the VMA tree and PMD table sharing, nothing about zeroing;
> the inode lock and the folio lock are sufficient.
>
> Let's fix it by only holding i_mmap_rwsem while unmapping the full
> folios of the hole.
>
> Update the documented locking order in mm/rmap.c and the function
> comment above hugetlb_folio_mapping_lock_write().
> hugetlb_folio_mapping_lock_write() could now take the lock
> unconditionally, but we leave that for a separate change.
>
> The order of the folio lock and i_mmap_rwsem now matches the one for
> other folios, which is nice.
>
> This has low urgency, as the race shouldn't really happen all that often
> in practice (never seen).
>
> Fixes: 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
> Cc: <stable@xxxxxxxxxxxxxxx>
> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
> [ david: adjust comment and description ]
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

LGTM:

Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>

--
cheers,
jose a. p-a