From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796007121; b=eZh0J+TeY12vTGLEOOJS35QE21Cf4AQ7BWTIZyv2mykYn3q9J2+Vlucd6RqMkSKVctI6 X4zAnWjUQUkhcGb0oMwJozZ3rqYvRCmoqJHBEHY8OVHecPnhxsQEcAwBOKvruGAs1EoIW X2LozdKyYOWkA/lxRflqjOlJtc9iCKYy5oCBy7Oazk0nQdYdCCUvRlKVRi9osZJVvSGPq tmYL4geeL+QeiOLeex6o0CG7YAtH5EB8AZf0bMue+GgmwwkieMKzL9QShWgX3fQc4+Mwp N1YnPTMRvBD7pgsn+nUTaQKrgdeEqyqRPQg5dYnvIpixPLJ0/8wSe3c0wh/qr52w0XA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796007121; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=uiQspacstmdwSwAsk4Qcsq1z/oL8VWRiHHcNeHl1CM4=; b=LQH4n+RYH5pcu6mAad6K5KxZZZeoxPyk2ZrebDw4JByaY6MWnajCrg9FMOsJ0kOMruMz NjsdVOl8WdlYwzGClBZbRyHRXeL/i0h1m1n0B8vDJlgK97GExPocU2sJMN+1/MAW5ZQgi +wbnWXgUgTsNxFPZImRPbAeZKvuNCkVe+N+bjDZ6NIzGAVP8IjPQMuWv0g0eIK6eWnxfY kxj12fQ0yktbcWBU34IVfHUm1Et/nUuxIWNu7X0ybq76YM7/fHI3z386ib6XXkUqoKz/v BTYy0IQPyXSBPvyxyNSomqAVGDZZ7Vs6N6ihRN9F8aJjbVP/L8caCYqg5ebuLiFDFBA== 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=zvl6AS7Q; 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 57F87606A31C for ; Mon, 30 Nov 2026 02:52:01 +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=zvl6AS7Q Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4EDC51F00897; Mon, 30 Nov 2026 02:52:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796007120; bh=uiQspacstmdwSwAsk4Qcsq1z/oL8VWRiHHcNeHl1CM4=; h=Date:To:From:In-Reply-To:References:Subject; b=zvl6AS7QdlOnJFjXm4fO0viE5oq4jE9ifpgZIMAA/Cc6BMU/TkQlxbIZInx6c6RgE bS5wj/dRiOT5y42IVYmj67csgGtY1tPXt5doUYAY3XpTvr7AaJBwQgZzzzw8HYNM+c EK0afyT+lo48rxsKUXC2fCykrWPmiOep8vW2ooBY= Date: Sun, 29 Nov 2026 18:51:59 -0800 To: mm-commits@vger.kernel.org,mason@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261125-hugetlb_fixes_2-v1-6-db27fd2f71a8@kernel.org> References: <20261125-hugetlb_fixes_2-v1-6-db27fd2f71a8@kernel.org> Subject: + hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch added to mm-new branch Message-Id: <20261130025200.4EDC51F00897@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-new 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-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next 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: Wed, 25 Nov 2026 23:33:04 +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/20261125-hugetlb_fixes_2-v1-6-db27fd2f71a8@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: Jinmeng Zhou Cc: Hongfu Li Cc: Andrea Arcangeli Cc: Mike Kravetz Cc: Naoya Horiguchi Cc: Hugh Dickins Cc: Baolin Wang Cc: Dave Airlie Cc: Vivek Kasireddy Cc: Gerd Hoffmann Cc: Mike Rapoport Cc: Mel Gorman Cc: Dave McCracken Cc: Andy Whitcroft Cc: Adam Litke Cc: Lorenzo Stoakes Cc: Rik van Riel Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Harry Yoo Cc: Jann Horn Cc: Lance Yang Cc: James Houghton Cc: Yang Shi Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Pedro Falcato 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: Wanpeng Li Cc: Andi Kleen Cc: Kenneth W Chen Cc: Michal Hocko Cc: David Rientjes Cc: Joern Engel Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Aneesh Kumar K.V Cc: KAMEZAWA Hiroyuki Cc: Kiryl Shutsemau Cc: Sasha Levin Cc: Guillaume Morin Cc: Wanpeng Li Cc: Jason Gunthorpe Cc: luojiajun Cc: Josef Bacik Cc: Daehyeon Ko <4ncienth@gmail.com> Cc: Ackerley Tng Cc: "Oscar Salvador (SUSE)" 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 @@ -684,8 +684,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, @@ -693,10 +691,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. */ @@ -704,8 +704,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 @@ -1871,8 +1871,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 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 kfence-use-a-nofault-store-to-zero-the-guard-page.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 mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch