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=1795022631; b=hcjfjbjKsPUJllX/AFjIOSW5YX2k3YgzPXQFKsvxP5dngnNok/9osEHtjltSHhPEiQ6g HPn5ca1qBa8GlJKYyITlcm1HsPr83fbjizgRlQo5kGxYQ/fnB2Mt75LuMebY1jaTqA3DM /ufPv+um7kObUPg3Nmga303/vuaoJn9pwz96gvPAw/H1pvBMGgFskfpO/+JSjPeIk1Mor BxYIz+Mg6SlF+XGu2PuoTge79rI2epMIrbB/3TcftXm3p23Xjx2puksMMkFX0TczChNZ6 iVesiIQtdK/xqPP3tTjr+BZ84x5ZIhaPXhUVpafjefak7/9id3+MDt7JdKJDeWX+E6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795022631; 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=I2tm20EeU2eL2nOxAdc3fv+PfJUSs86mJCFz1oA4Z4o=; b=IqZzSaIilKCSV5wBdNASml7IOwt+aOx1p14i4PbEMS3Mfw/pB7TE2PxEaKw666drzsx2 A3E89j1sjCaVOeSxo/Ub4Gtn8ldLDyssAfvsjTcLHIRj0lEmE/iHrbRSFJJN7kz80D43Q g/EKnqRkLPik8uVwiLSred3L2e0842IkxbPTMDJAuVvPJkfbK0F/6dexc6ik0EFyJjO5y WGiN3g5BznTJx54JCf5G5b0ktIrPosGM7iNGUTQ+cuIzt18KCIvS+UeOM/73fNY6gutp/ vmkMFBR0Q/klz+JKqo6acdRQ26nUcqclmsd3d+sqQUzGypjPTEz5JGmIJfqQyXKuppQ== 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=ZqaeDp27; 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 C408C606A339; Wed, 18 Nov 2026 17:23:51 +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=ZqaeDp27 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28FB71F000C6; Wed, 18 Nov 2026 17:23:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795022631; bh=I2tm20EeU2eL2nOxAdc3fv+PfJUSs86mJCFz1oA4Z4o=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ZqaeDp27wihV8W5RpghBohPoA6dZDUe8XSwC/zh0sI+FtAGw6Ff8u033cPqVjIjPU K6cFGp/5on1M9ixVfWM/xOzkVmak02PhSCkFUsaBdmp0ZzeUv9VGUAla6bUSJZKPSb hAcfx70LZikItnFN95hrwAehWW++qe6jO7NkGpq8= 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.18 0081/2151] mm/huge_memory: fix truncate racing with PMD zapping Date: Wed, 18 Nov 2026 15:30:16 +0100 Message-ID: <20261118150124.725265595@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150121.603863898@linuxfoundation.org> References: <20261118150121.603863898@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.18-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 the existing inline logic in zap_huge_pmd() ] 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 24f5887eb48ee..eba9280ddb594 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2327,8 +2327,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; @@ -2360,6 +2358,15 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, folio_mark_accessed(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