From: Chris Mason 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: Signed-off-by: Chris Mason [ david: adjust comment and description ] Signed-off-by: David Hildenbrand (Arm) --- fs/hugetlbfs/inode.c | 6 ++---- mm/hugetlb.c | 3 +-- mm/rmap.c | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 4a92f31c88e1a..2e8672ae2fd52 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -684,8 +684,6 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) return -EPERM; } - i_mmap_lock_write(mapping); - /* If range starts before first full page, zero partial page. */ if (offset < hole_start) hugetlbfs_zero_partial_page(h, mapping, @@ -693,10 +691,12 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) /* Unmap users of full pages in the hole. */ if (hole_end > hole_start) { + i_mmap_lock_write(mapping); if (mapping_mapped(mapping)) hugetlb_vmdelete_list(mapping, hole_start >> PAGE_SHIFT, hole_end >> PAGE_SHIFT, 0); + i_mmap_unlock_write(mapping); } /* If range extends beyond last full page, zero partial page. */ @@ -704,8 +704,6 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) hugetlbfs_zero_partial_page(h, mapping, hole_end, offset + len); - i_mmap_unlock_write(mapping); - /* Remove full pages from the file. */ if (hole_end > hole_start) remove_inode_hugepages(inode, hole_start, hole_end); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 0f3db932266d7..befa2670cb03f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1871,8 +1871,7 @@ void init_new_hugetlb_folio(struct folio *folio) * Find and lock address space (mapping) in write mode. * * Upon entry, the folio is locked which means that folio_mapping() is - * stable. Due to locking order, we can only trylock_write. If we can - * not get the lock, simply return NULL to caller. + * stable. If we cannot get the lock immediately, we simply return NULL. */ struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio) { diff --git a/mm/rmap.c b/mm/rmap.c index fe354fa6cc43a..f35b671d26514 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -49,8 +49,8 @@ * 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->i_mmap_rwsem (also used for hugetlb pmd sharing) - * folio_lock + * folio_lock + * mapping->i_mmap_rwsem (also used for hugetlb pmd sharing) */ #include -- 2.56.1