Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796325223; b=EQxuHKd5liuR5ihQwJBK4NlhTpsQsqSkzygoXIWmIUFMnHdKMqbfYajGbNEBl7ABattW i9OenDo577AAaeTePcjlZ5jUoVc8QXQ6FFl4WeuhW8zGsFOKtduA4h9iqIk5DAlQ06+kV 6ha7AVnvhalY1eKQsKitJ0klrDKiTXZff590p1ouImZXUMpwqoz6B2h97JlcR2u4vOx/A 2dZT/4EUuN+wV1NmoyyyerMiEQZNu9tkIM21KllmD0EEhWQqRdWmszbKvvZrnBOYNYPGP zmmEUUTaqtNomC4gdPc1Vh3iv+IKwpV+TRh2+T90XZet3iEp+EEZL/ogBdYAPbPpJ3w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325223; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=wFLGTirHBKL/lGa655ZM6AAq6CPMhgQsTWbDUZkV5lE=; b=a71+fUvXZBq2tHyKh8UT9byUvvRYeFxL664kzS9Mm4LsD22D5ztxsn3iTCUpBbd2LJhE 7ISYAbxq0ojRe6S7Tvje5sjw7KQcCO7xpYiLc2NcYo5kn48LJzAbyajQQ1BpINjI5EfVl kV6wqnf05GBE+oDbuBlDrgkXIpTUeMnpS/oMi1Z/bSLCMX3QGTfI7G4wZgcuSSyN1Th/m 9PJk13g3ymrmKhg/rQlwok83QxmJeSrqLDKjdLpvI2cky3z+UPBxEToeA1byD2clCE2eY 9WrRcF5CRuOIccJ/ihi/xeW1RnEXDTMUmVS4f+BvfGAhc4NiQhBBY6eXW1OEDTgKSPg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=tmdOUThS; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E895606A322 for ; Thu, 3 Dec 2026 19:13:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=tmdOUThS Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7B6A1F000C2; Thu, 3 Dec 2026 19:13:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796325223; bh=wFLGTirHBKL/lGa655ZM6AAq6CPMhgQsTWbDUZkV5lE=; h=Date:To:From:In-Reply-To:References:Subject; b=tmdOUThSFIDte47drHvxQkuIDjVzxG+wZ8W+3fvFMt2uFua8TAWDe7X82rNu2R4tp jQpJI+1eJ3dWiZd19JgyffD554Vi+/XgOQhQp2gNKaDsq4jHWrqPjIt8SFuIAzwENu +MzE2MQb4k601aKvZRR6O3DtC2/AZiF9tGsNGJ7A= Date: Thu, 03 Dec 2026 11:13:42 -0800 To: mm-commits@vger.kernel.org,mason@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261203-hugetlb_fixes_2-v2-16-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-16-9bbeabb555d2@kernel.org> Subject: + hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch added to mm-unstable branch Message-Id: <20261203191342.D7B6A1F000C2@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: hugetlbfs: fix deadlock between hole punching and memory failure has been added to the -mm mm-unstable branch. Its filename is hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Chris Mason Subject: hugetlbfs: fix deadlock between hole punching and memory failure Date: Thu, 03 Dec 2026 12:37:39 +0100 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). [david@kernel.org: adjust comment and description] Link: https://patch.msgid.link/20261203-hugetlb_fixes_2-v2-16-9bbeabb555d2@kernel.org Fixes: 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch") Signed-off-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Cc: Muchun Song Cc: Oscar Salvador Cc: Hongfu Li Cc: Jinmeng Zhou Cc: Mina Almasry Cc: Davidlohr Bueso Cc: Naoya Horiguchi Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Michal Hocko Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Vivek Kasireddy Cc: Mel Gorman Cc: Hugh Dickins Cc: Baolin Wang Cc: Dave Airlie Cc: Jason Gunthorpe Cc: Andy Whitcroft Cc: Roman Gushchin Cc: Barry Song Cc: Andrea Arcangeli Cc: Lorenzo Stoakes Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Pedro Falcato Cc: Jann Horn Cc: Guillaume Morin Cc: Yang Shi Cc: James Houghton Cc: Rik van Riel Cc: Harry Yoo Cc: Lance Yang Cc: Axel Rasmussen Cc: Peter Xu Cc: Sasha Levin Cc: Kiryl Shutsemau Cc: Christian Brauner Cc: Steven Rostedt Cc: Hongbo Li Cc: Gregory Price Cc: Zi Yan Cc: Joshua Hahn Cc: Rakie Kim Cc: Ying Huang Cc: Matthew Brost Cc: Byungchul Park Cc: Alistair Popple Cc: Andi Kleen Cc: David Rientjes Cc: Joern Engel Cc: Aneesh Kumar K.V Cc: Gerd Hoffmann Cc: Johannes Weiner Cc: Jiaqi Yan Cc: fs/hugetlbfs/inode.c | 6 ++---- mm/hugetlb.c | 3 +-- mm/rmap.c | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) --- a/fs/hugetlbfs/inode.c~hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure +++ a/fs/hugetlbfs/inode.c @@ -689,8 +689,6 @@ static long hugetlbfs_punch_hole(struct 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, @@ -698,10 +696,12 @@ static long hugetlbfs_punch_hole(struct /* 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. */ @@ -709,8 +709,6 @@ static long hugetlbfs_punch_hole(struct 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); --- a/mm/hugetlb.c~hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure +++ a/mm/hugetlb.c @@ -1878,8 +1878,7 @@ void init_new_hugetlb_folio(struct 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) { --- a/mm/rmap.c~hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure +++ a/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 _ Patches currently in -mm which might be from mason@kernel.org are mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch