From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE46D606A696 for ; Tue, 1 Dec 2026 16:25:42 +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=EOLvHs8q Received: by kanga.kvack.org (Postfix) id A4E806B061E; Tue, 1 Dec 2026 11:25:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FE926B061F; Tue, 1 Dec 2026 11:25:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8ED1C6B0620; Tue, 1 Dec 2026 11:25:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 591C86B061E for ; Tue, 1 Dec 2026 11:25:41 -0500 (EST) Received: from smtpin08.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CFEE5C053F for ; Tue, 1 Dec 2026 16:25:40 +0000 (UTC) X-FDA: 85495940040.08.73EFE3C Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf12.hostedemail.com (Postfix) with ESMTP id EF2D340012 for ; Tue, 1 Dec 2026 16:25:38 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=EOLvHs8q; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796142339; b=y9pwVhCWHpI8kCtoq/93hGdxALhoStb+8Cwsc/LeHA0pT0gpqV6CIiwhStJMQUbjDUsLR4 BWh32q7I04wLMnfzvFOJ2DgGChLHb/5FiRVx3PIoxnvos3imvA4I9Mq97BaH3c0c0ptQsX 62eWNXrOBkoLsF9yTMdc/RSY8/K5bmc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796142339; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ux3NuQ+FAqPxxAZiQfDqle9jil41awcPYAk5FFCfpXU=; b=G9asUPRUiShccQG8IjJJ70zjUp7ng4tcmzyLQmMRLBVFI1ZjcuH7ldgiBDZ2j+IGMvuoxV gO8glU2He6elLZV9qo4/AcmQ+UnenlFGzub+PKogY7cPrV88N/nsPnobpDh7H8U+p942uO CzQPrd0Qgt23loVYl8Fb22CId8AvUrU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=EOLvHs8q; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 2780843097; Tue, 1 Dec 2026 16:25:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46D631F00893; Tue, 1 Dec 2026 16:25:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796142337; bh=Ux3NuQ+FAqPxxAZiQfDqle9jil41awcPYAk5FFCfpXU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=EOLvHs8qpdirYd48aUMvHc9tcOLGXu7ohcEnhojDog4oOyASSs2XGV67mPioSLLn2 v+Rn/goLHrRcPiSTBsVQziwsTtz3AXzMJWkmltQkpMG3aepYVGOrH9qIFQC9qAmDf3 8eVYfzfliuupK9l68FNZywYF/lhVQx8A9QNc5w6UYnmBa2WBFZNt/l7oa1DikHL7ZE PxwdlYAktxxPmaJ0HNqAsnLry9f0ctUvskyMAAA+06tpcWRGlt4pnmM7N3fhhlSlgI iISJyTJGy58J/NTCYAB7L9o3i84BcFx0hNcYiiRodVSJ+OKBHucswTjkIbIkuNFF9Y 9H39XR3RYIAOw== From: "David Hildenbrand (Arm)" Date: Tue, 01 Dec 2026 17:25:05 +0100 Subject: [PATCH v2 1/2] mm/rmap: don't defer TLB flushes when unmapping lazyfree folios X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261201-file_folio_flush-v2-1-994d587aec8e@kernel.org> References: <20261201-file_folio_flush-v2-0-994d587aec8e@kernel.org> In-Reply-To: <20261201-file_folio_flush-v2-0-994d587aec8e@kernel.org> To: Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , "Borislav Petkov (AMD)" , =?utf-8?q?Cenk_Y=C4=B1lmaz?= , Zhang Peng , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Andrew Morton , Zi Yan , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Mel Gorman , Sasha Levin , Ingo Molnar , Peter Xu , Hugh Dickins , Axel Rasmussen , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Yuanchu Xie , Wei Xu , Baolin Wang , Thomas Gleixner Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3985; i=david@kernel.org; h=from:subject:message-id; bh=e6zU/Y5PjWEyfO/1mLFQHcb/iuflZNksnVMDHvvQL70=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLL5vry+sPFUiWGXukDm9GcdtTEydb6TYr7tMQnsnHuY3 c/O2e5HRykLgxgHg6yYIktQ8H2Dly/X1Va78THDzGFlAhnCwMUpABPpZWP4X8TUIZsRevj3sp8P NefUKpaIPtipuHLVny/ZNk2emfEvbzEyLK6IORr4eZO/eo3OrNlzDTXXt555+FU9WeRc3x6uuC0 fWQA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A X-Rspamd-Queue-Id: EF2D340012 X-Stat-Signature: xxfp7d3zjtrzzn7nsawyz7nhw439k8n8 X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1796142338-435397 X-HE-Meta: U2FsdGVkX18CGY2f18C7WdSNkpqBN64uVBIOQPZQ1wGJMFHeR6RjrEse3eoAZOvy8744EqxLHLdz9AbLodeI1HBj0SQV7L9t2FWjDE9z45m79GMWtVWBKFBpjfxTB1zpJw1L0xVSzakuPUsUsfdlidRpMBwWF3queLCPiacLozlL3IyzOmcUWAN6EKwj1++wZAqFGcOcGYP5/ms5KbGWig2gTlPNaCBZUyucx5lIjLlQo2L9+jQ5mgkJLE0omPzG98dsHvB/jj9j2Npz9Rb+RkowGwqUCyon1iD2Ooe5rikzFoXyG844OPV1+QAK6rO8SLO5Zszg0KmrI06ugAPDOZqS1G0ymFbbgbM3tNrkI+SDbR0GEmABZr938i4OiCUaPfnmv+NrB8GrFgcpkWKv0kUSFKJsZFXd8zi0UQ0yoLafwJZ/oqAr/jOV6ryPbjDa1mySuFWyHjQqnV5YxvT/sR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aBsSz3f6TajEagxyngo Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 When reclaim discards a lazyfree folio, it clears the PTE but delays the TLB flush. Until flushed, mapping a new folio into the now-none PTE can leave some CPUs still reading from the old folio while others already write to the new folio. This is hard to trigger in practice and would mostly look like a weird use-after-free bug in an allocator that uses MADV_FREE, but it can be reproduced reliably with three threads pinned to different CPUs: (1) Thread A populates the pages, MADV_FREE's them and then MADV_PAGEOUT's them. (2) Thread B reads all pages to keep TLB entries cached and verifies the content of pages published by thread C. (3) Thread C waits until mincore() indicates that a page is no longer present, writes a new value to it, and publishes it to thread B. Thread B can then read the old value after the new folio was mapped and the content updated. One way to handle it would be adding conditional flushes to do_anonymous_page(), migrate_vma_insert_page(), UFFDIO_COPY and UFFDIO_MOVE ... essentially anywhere we install new anonymous folios, which would mean that these places flush quite frequently on concurrent reclaim. To perform targeted flushes, we'd have to keep some PTE markers behind, which is also rather nasty and not an easy fix. So let's not defer the TLB flush for lazyfree folios, which includes folios in VM_DROPPABLE mappings. To avoid flushing if the folio was redirtied or has additional references, perform all non-deferred TLB flushes only once we know that we won't restore the PTEs, right before removing the rmap. The downside is that we no longer batch TLB flushes when discarding lazyfree folios, which harms performance, but is the right thing to do for now. Fixes: 854e9ed09ded ("mm: support madvise(MADV_FREE)") Cc: stable@vger.kernel.org Acked-by: Zi Yan Signed-off-by: David Hildenbrand (Arm) --- mm/rmap.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 038435994b99a..7459722d22d9f 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2218,6 +2218,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; unsigned long nr_pages = 1, end_addr; + bool flush; unsigned long pfn; int ptes = 0; @@ -2243,6 +2244,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, while (page_vma_mapped_walk(&pvmw)) { nr_pages = 1; + flush = false; /* * If the folio is in an mlock()d vma, we must not swap it out. @@ -2329,11 +2331,17 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, * architecture must guarantee that a clear->dirty * transition on a cached TLB entry is written through * and traps if the PTE is unmapped. + * + * If we don't defer the flush, we flush only once we + * know that we won't restore the PTEs below. We never + * defer the flush for lazyfree folios: if we discard + * them, the PTE will be none, and a page fault could + * map a new folio while stale TLB entries still exist. */ - if (should_defer_flush(mm, flags)) + if (!folio_test_lazyfree(folio) && should_defer_flush(mm, flags)) set_tlb_ubc_flush_pending(mm, pteval, address, end_addr); else - flush_tlb_range(vma, address, end_addr); + flush = true; if (pte_dirty(pteval)) folio_mark_dirty(folio); } else { @@ -2392,6 +2400,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, add_mm_counter(mm, mm_counter_file(folio), -nr_pages); } finish_unmap: + if (flush) + flush_tlb_range(vma, address, end_addr); folio_remove_rmap_ptes(folio, page, nr_pages, vma); if (vma_test_any_mask(vma, VMA_LOCKED_MASK)) mlock_drain_local(); -- 2.56.1