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=1796298108; b=yONTNhVzNzjfJu55AoEqQcMKWAvBeBf+tDmSxdYx28oaJbvfCqNLhKXHHTAk3keGkMM6 mpajm+XheFvJH9xfAVkI/gyBhIwQfXmFiOiYQjWxOKJaovEx2IIyCCA3EsLVVyGHvON3g f1tOwqh0a+Eol/2blLmDXg0B/umuRzPkRJ8Og+9CdoCuHjbrVjPN9A70xKIkQ8CxE/v64 Ie87bW3ZuIjdMufWwwaWFmCizYMm50M+p058Bu3zqW+FM/GJblTB/D3DJG+Wo9VBEI/dB +3CALMrbubx64LL87/da2nQUCWhJkhMjMebeA8Y6Kn3IZPeTcF0judY8fC8oXO8YupQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298108; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=d6EPRkR4rMGlVupW/jlEbrBVNeMfKmPD1DfyLYwAMaI=; b=ZPMBQAboB4eXBQ1XdWBQ8Blu7iuplCuYUcDGwH/GaVyOtX+HGTWEAz3wpI/j+vSph9BY vSjxTodm7i1IgBtQvSoXogRTQaLPpVSytsz/6vVpnLaIx7g6sz3HyzEH4H/xDRQm1FcRF U7ABv54h41aU8fYGSHtVXHQx1okATAGUIGYuDS8/j9Y3w9a2gPTCrgdKZN/uN70XnF2zn pgHy0hfYkEKOTloTVXGxDnEaUoLyJoPZZ16eZjVbS0C1x1afgkd4SpImNnPvPEo6I4sxE 0saUSWINLXIeOB/Kp9KeRWp9wUHNNTK1nLvA+cnUYzN5LLjzkCMOif5BXdgUw/HYEVw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aSDp7KPb; 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 C1DED606A322; Thu, 3 Dec 2026 11:41:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aSDp7KPb Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B3611F00893; Thu, 3 Dec 2026 11:41:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298108; bh=d6EPRkR4rMGlVupW/jlEbrBVNeMfKmPD1DfyLYwAMaI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=aSDp7KPbcD/mcRyl38i5imKxIVItwOPE34atiji6io48Iep+mEqaRWoXftIkk9vXU NNU8OzDK7c4PBowQKwLAvecd3TwjYeWGvJuTJZmBx0y03RqS20ni3wPhf6RgQDdWP+ vROzhfyr4f0tYVpL4lVRXFTSYxRug0mU/ISLicncmjB1AQtqjh2QGDlV2bY8qOzaf7 zY0SW0xnS/8Rma7WzuLhwfAqOhFpuhyFbfXCB7CM3VhkPzfsQarrBlhYASx58D+25u oFYqac2SO+QKUYDmTZixqVqaJ9x5Rc2g6QqiIitUrkLEo/Wh2u/rSoxmGL+2Rj3EKa N+0GSLecizAfA== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:39 +0100 Subject: [PATCH v2 16/35] hugetlbfs: fix deadlock between hole punching and memory failure X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-16-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5106; i=david@kernel.org; h=from:subject:message-id; bh=pqt8gklumECIsP3Zwuv/H2jN9lYLa/gUH84NSE2TUws=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ6rVbJauFygLd/sevsBx5qK2JUtvN+fO22W8ctqf6 V2M26ZzdJSyMIhxMMiKKbIEBd83ePlyXW21Gx8zzBxWJpAhDFycAjARjjMMf2VOt91LjvL5YbyW dauL68Simjr2TT9lUrm2ubkePxnUtYORoZ9H1Khzx5IqufproonzQphvKK85ayXDbqyTm9XW9zO JAwA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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 dd0cc0384a181..4290e88f23c5b 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -689,8 +689,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, @@ -698,10 +696,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. */ @@ -709,8 +709,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 0d242ebfb07d2..72d912f488fdc 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1878,8 +1878,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