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=1793217108; b=Ks6JR2GxyTh5zpTrrJiUc3DTLug7xfwP7vdvamGhju/RJ2fu/8RfEwn5BhfXfWkuXsUt 3nIA3HGwh9LCHDI/+bhBW5PHO797FfywXEPXiLzhWxqe2/qdU+mZph8SVJIsPkSnLVllM brmRb8Mv54zpbmStWeTDhRsPlT5YF1wp9fOa6/th+es3RPsSjxNyLBy+w0Y2HGPIymIli wjZGUkGiONIjC7/R6NUo8QM+dzPuQlsgyFj8BAKxrm4irjKnFg6rX3AUe823VUQKHCn8+ gkEQviVjWpsCzro6Ikd293NaSA26RadCeWEBsZxag6kgjlQ2CmQ2Nu1KG1fk6/sPKIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793217108; 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=M/WqyTcrckJ2Vv1FjKVDyGCnmuQqEp27lazby6zbzAQ=; b=hhw4wXG+bwIkeEBjrtg/6t5UJHlyA16fDvx86oL2mq2kcwNTDGbCSJXdL9KaIDA8vVMm 1tVAHxpKeC83g/Io5DgWkxcmKaR3JjqjFBfkwT3xJwJnLwqXDOhUn+eE0yvPJEQuhY3LI ZZor9F6RVf58XYi7FeCjSgsJ1FWu8nHR3MeXCcxyAshqkQ3SwDXtmV5g1hMIYHXc90DeY 0E6w47QwEKP4jgU7Q/2+vY5O8TTEVRO52yuKKyY/XGM9VIZgQ9m3FEIsk4TJRWg51ikWt sulwt4t058hh6aqJ7/OXWWNvp4VY3GRPGdsTLNRT7W4YYv7Lc7k4lHGiYDmehhOV+dA== 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=nNWG5rLj; 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 58F75606A31A; Wed, 28 Oct 2026 19:51:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=nNWG5rLj Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87B241F01825; Wed, 28 Oct 2026 19:51:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1793217108; bh=M/WqyTcrckJ2Vv1FjKVDyGCnmuQqEp27lazby6zbzAQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=nNWG5rLjnO8RljD7VWqa3ogn4A73bg0qdklET1epr5sAUrbfkO8PXXBhaMgrPQDxh sktAGtKHtnVHeBunRMBG9zf+SkaaD6as0AGMm4ABfgP6CDT3k1o8+TBIhVYBwyLROg Nd0m1uAIl4OqZp4jCsHW1H7jzxVyJz4nBhOTBHd4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Usama Arif , "David Hildenbrand (Arm)" , Kiryl Shutsemau , Hugh Dickins , Lance Yang , Zi Yan , "Lorenzo Stoakes (ARM)" , Baolin Wang , Barry Song , Dev Jain , Johannes Weiner , "Liam R. Howlett" , Nhat Pham , Rik van Riel , Ryan Roberts , Shakeel Butt , Andrew Morton , Ting-Han Hou , Sasha Levin Subject: [PATCH 6.1 1784/1887] mm/huge_memory: transfer the pmd dirty bit to the folio on zap Date: Wed, 28 Oct 2026 18:04:40 +0100 Message-ID: <20261028170540.047851648@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261028170459.200466217@linuxfoundation.org> References: <20261028170459.200466217@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.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Usama Arif [ Upstream commit fe6cf984939d8e12cb33a99673c8d026c5135e68 ] zap_huge_pmd_folio() propagates the pmd young bit to the folio for the file case, but not the dirty bit. The pte path does propagate it, in zap_present_folio_ptes() and so does the pmd split path, in __split_huge_pmd_locked(). For most file mappings the omission is harmless, because writing to a shared file mapping goes through page_mkwrite(), which dirties the folio. tmpfs is different: it has no page_mkwrite(), and vma_wants_writenotify() is false for it, so a *read* fault on a MAP_SHARED tmpfs mapping installs a writable pmd via do_read_fault(). do_read_fault() does not call fault_dirty_shared_page(), so subsequent stores through that mapping set only the hardware dirty bit in the pmd and never call folio_mark_dirty(). A shmem folio allocated by a fault is marked uptodate but not dirty (see the clear: block in shmem_get_folio_gfp()), so PG_dirty is never set at all. Unmapping such a folio - munmap(), or exit_mmap() when the process dies - then loses the only record that it was written, because zap_huge_pmd() drops the pmd without transferring the dirty bit. Reclaim afterwards sees a clean shmem folio: the whole swap-out block in shrink_folio_list() is inside "if (folio_test_dirty(folio))", so pageout() is skipped and the folio falls into __remove_mapping(). There, folio_is_file_lru() is false for a swapbacked folio, so no shadow entry is created and __filemap_remove_folio(folio, NULL) simply empties the i_pages slot. The data is freed without ever being written to swap, and the next fault on that index returns a freshly zeroed folio. This is silent data loss for any process that keeps state in a MAP_SHARED tmpfs segment across an unmap - for example a cache handed from one process generation to the next through /dev/shm. It requires the folio to be PMD-mapped, so it only shows up once shmem THP is enabled (which is what we did in Meta fleet and started noticing crashes); with THP off the pte path transfers the dirty bit correctly. It also only becomes visible when swap is enabled, because with no swap device shmem folios (which are on the anon LRU) are not scanned by reclaim at all, so the clean folio is never dropped. Reproduced on x86_64 with a tmpfs mounted huge=within_size: read-fault a 2MB-backed region, write a known pattern through the resulting mapping, munmap, force reclaim of the cgroup, then re-map and read back. Without this patch the region reads back as zeros and vmstat shows zswpout 0 - the data was discarded rather than swapped. With this patch the region reads back correctly and the pages are swapped out as expected. With huge=never, or when the first touch is a write, the test passes either way. Link: https://lore.kernel.org/20260819101222.3732660-1-usama.arif@linux.dev Fixes: b5072380eb61 ("thp: support file pages in zap_huge_pmd()") Signed-off-by: Usama Arif Acked-by: David Hildenbrand (Arm) Reviewed-by: Kiryl Shutsemau Acked-by: Hugh Dickins Tested-by: Lance Yang Reviewed-by: Zi Yan Reviewed-by: Lorenzo Stoakes (ARM) Reviewed-by: Baolin Wang Cc: Barry Song Cc: Dev Jain Cc: Johannes Weiner Cc: Liam R. Howlett Cc: Nhat Pham Cc: Rik van Riel Cc: Ryan Roberts Cc: Shakeel Butt Cc: Signed-off-by: Andrew Morton [ Ting-Han: zap_huge_pmd_folio() does not exist in this tree; the file branch is open-coded in zap_huge_pmd(), so transfer the dirty bit there, as the 6.6.y backport does ] Assisted-by: LLM Signed-off-by: Ting-Han Hou Signed-off-by: Sasha Levin --- mm/huge_memory.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 339f2986f2c6a..5807f276fe228 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1701,6 +1701,9 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, if (arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR); + + if (pmd_present(orig_pmd) && pmd_dirty(orig_pmd)) + folio_mark_dirty(page_folio(page)); } spin_unlock(ptl); -- 2.53.0