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=1795028668; b=y29LgBxA9LShEcRvCcpKgKG2aRxAn4vO5gBPiBnr5WTOdwxFu7o6O6yyQK58KMs+xPp5 cw9RWXTkabK8yfHM/Wyit+ACmABSm5j/uGrcJGOlAESCpvA7U8YjEC04jg5OCNaNZoz1R g8zBlUzeEyjmwbgdTX59zLceLmxuCSWcS7DD/O/Xx0s/WPg92rNFROKN9ccxiXFzDHVF1 Zgijz7w70G15pTDPI9ici4NBeEIz39vBIczIH45exkuUc+tk5VdTTYFr4s51Np9pr64Ku ZVkYUmnnJGdKGQsJ4Kk2Whxbbc9ikxmo30WeRV9JM+Ogdeb3xWNyEDZKUxm7S1Hh+Fg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795028668; 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=6HAKdrMhCN03vki/HjH1ziUgpJPmQhpvShe17kay24U=; b=AWSmxf3rtEwWoDg+PRPIX2mq4zleILltFeGBzoaBHvlFiZF7qIlhNN+OM8L/gKnNhP9q f3eUUUggcD1XyeNRkBXpFjSqsvnUMjT85nD4AbtnZlxSlwiDa9C+FEP/sAYSKBoHF87fR u7hvAYKJY21NxfxFxp29tcIe3QydDdNYFeAmchX31PJFM5OXCmYAayMZxVVa/ioKPYZ5u yfhrrY9e5C4nPHMJVitZV9OZnjjxSXrmtHmr4i01EWC3VNqk7jBLIEAiwyWCaGMkrx1/n v/W/5ajaTA5w3tNa3AxgcGIUAt4RXJ8kCXeaWsAQo4JzchqMU9C6UMtDdCUrEG9Jh9A== 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=rabuz539; 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 36B9D606A334; Wed, 18 Nov 2026 19:04:28 +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=rabuz539 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92DA51F000C4; Wed, 18 Nov 2026 19:04:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795028668; bh=6HAKdrMhCN03vki/HjH1ziUgpJPmQhpvShe17kay24U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=rabuz539itA+P5YmyRq1EcqwtYe2BsQjO5vy81NOO5wYJlpLw26TuW6NGgoz0FHsr pUzKGtlWndlMuD6xLKG2W8qPCsBd6cIL2iyxjy6KeitbntqKorU6HCXwqG+Je7o+SY hPAzmUpZyOibsoDiEp28VoFSkGpK362Tz1s6sBxE= 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.12 0078/1756] mm/huge_memory: fix truncate racing with PMD zapping Date: Wed, 18 Nov 2026 15:36:45 +0100 Message-ID: <20261118150011.396017024@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150007.957976754@linuxfoundation.org> References: <20261118150007.957976754@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-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 to inline zap_huge_pmd() logic with flush_needed replacing the is_present || is_device_private check ] Signed-off-by: Sasha Levin --- mm/huge_memory.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ce4d17871ff43..6cd88cd4b33be 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2128,8 +2128,6 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, struct page *page = pmd_page(orig_pmd); folio = page_folio(page); - folio_remove_rmap_pmd(folio, page, vma); - WARN_ON_ONCE(folio_mapcount(folio) < 0); VM_BUG_ON_PAGE(!PageHead(page), page); } else if (thp_migration_supported()) { swp_entry_t entry; @@ -2154,6 +2152,15 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, folio_mark_dirty(folio); } + /* + * Only present folios are rmappable. Remove the rmap after + * transferring young+dirty to the folio. + */ + if (flush_needed) { + folio_remove_rmap_pmd(folio, &folio->page, vma); + WARN_ON_ONCE(folio_mapcount(folio) < 0); + } + spin_unlock(ptl); if (flush_needed) tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); -- 2.53.0