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=1795039061; b=WbpHUR0eagmpVdk+wQBJUIj+e1Lq6Hj1ij2HNICPLRJ/flHqxO0a/2sUXnCRZFwGeayc V9uLG5YMGQYuEqsUyHxOFvqLoLNTRLyPr7q8wEHKqDjXM6SwAWHLSev6DItVXRqQBz6Bl sBjK8khN7nWZWFYpli74hxPBzLN6eFsBlNn1jcZ7apXNUArbmzzt5rDdfsZ/rd+bw6KbZ Wq3EqXS3I2qmj+Fs1Q+TP18qs0PxQyn8BBShtepenmjEmoPGdQY6/N7xhiFmCgUzvfzTi yg95Z+4FNoZOsDs0Me1XHCrL6gxCdo/zFfFeOkMjdCDUD6NN4/nQo+75jTyS9np8Hpw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795039061; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=U7S5B8HGMTaKWHFTB71NNj1U//xOlTnzsh/aJ8L1Ruw=; b=eo8wks6SsCGLfykh+ZjcKi9w50XlxFXdZ/tpaXN+ibGhoIJi6z4YXxfBA8eZFmMsDCOr kvUoWBMim8pKVX57z0kHsbBXti1/KXl62qPX2z6ZXh3cl4EChYK0sCcEVNk6DEL3Gr95R evv8K7r+cGWb494Rq9Kw4w5cKvyP4IpI2WUvJKE28kgmFLNycs4LyFqdyAqaSueZ2rbuX SkWIEyPU0YROSeH0PW+KkcBGg/xOkvTuc6AZFh0qbYewrxCrOxwtH+UKJfLn+ZTOMNYQJ nP8s0zGLCaBRTA6xQkvYQ8i0Rqv/MpCJnZZjY5OCO6QQTRJnnhvKgnlzKl/XTaR0lhQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=1NtwKHhl; 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 862F9606A332; Wed, 18 Nov 2026 21:57:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=1NtwKHhl Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3AF71F000C4; Wed, 18 Nov 2026 21:57:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795039061; bh=U7S5B8HGMTaKWHFTB71NNj1U//xOlTnzsh/aJ8L1Ruw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=1NtwKHhlzSurJW9/mvfDUO0wpPtQ7+Vt+d8Ee/Q6ZWoszXkSrLVC9pCUjoWz+VEAi 0MpBH4Q/OOCwwcHg97zfFlsIP9mguyNZVkjXbPuHRudBmT8Y7zokFkHh4AUn1SyKw9 KFBjmU7UcEA8jB9NVEzK9W/2ea/3Rrb+4AZ2JUPQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , "Gregory Price (Meta)" , Muhammad Usama Anjum , Zi Yan , Baolin Wang , "David Hildenbrand (Arm)" , Sasha Levin Subject: [PATCH 6.6 1923/3318] mm/huge_memory: fix truncate racing with PMD zapping Date: Wed, 18 Nov 2026 15:42:25 +0100 Message-ID: <20261118150419.702063079@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150313.527275416@linuxfoundation.org> References: <20261118150313.527275416@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Chris Mason [ Upstream commit e6a07d29b0a60f3a4cfe77fe34eeb4b3fb38d6c4 ] zap_huge_pmd_folio() removes the rmap before calling folio_mark_dirty(). If this was the last mapping, truncation can skip unmapping the folio and proceed without waiting for the PMD lock, racing with dirtying. ifs_set_range_dirty() can oops if MADV_DONTNEED races with ftruncate() on XFS, when we get the folio set dirty after it was found to not be mapped anymore. Move folio_mark_dirty() and folio_mark_accessed() before rmap removal, matching the PTE zap path. This keeps the folio mapped while updating its state, so truncation must wait for the page table lock. Fix generated by an LLM and sent by Chris Mason in private. Fixes: fe6cf984939d ("mm/huge_memory: transfer the pmd dirty bit to the folio on zap") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: simplify comment and description ] Reviewed-by: Gregory Price (Meta) Tested-by: Muhammad Usama Anjum Link: https://patch.msgid.link/20261031-fixes_core-v3-13-d6437171ecc3@kernel.org Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Signed-off-by: David Hildenbrand (Arm) [ sashal: Adapted zap_huge_pmd_folio() changes to zap_huge_pmd() using page_remove_rmap(). ] Signed-off-by: Sasha Levin --- mm/huge_memory.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4aa032e96fa60..c9b40d69ff1fd 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1717,8 +1717,6 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, if (pmd_present(orig_pmd)) { page = pmd_page(orig_pmd); - page_remove_rmap(page, vma, true); - VM_BUG_ON_PAGE(page_mapcount(page) < 0, page); VM_BUG_ON_PAGE(!PageHead(page), page); } else if (thp_migration_supported()) { swp_entry_t entry; @@ -1742,6 +1740,12 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, folio_mark_dirty(page_folio(page)); } + /* Remove the rmap after transferring the dirty bit to the folio. */ + if (pmd_present(orig_pmd)) { + page_remove_rmap(page, vma, true); + VM_BUG_ON_PAGE(page_mapcount(page) < 0, page); + } + spin_unlock(ptl); if (flush_needed) tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE); -- 2.53.0