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 49598606A322 for ; Wed, 2 Dec 2026 19:49:56 +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=G6L9NeVi Received: by kanga.kvack.org (Postfix) id 50DEC6B0634; Wed, 2 Dec 2026 14:49:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E5916B0636; Wed, 2 Dec 2026 14:49:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3ADFC6B0639; Wed, 2 Dec 2026 14:49:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 125296B0634 for ; Wed, 2 Dec 2026 14:49:55 -0500 (EST) Received: from smtpin21.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8890740476 for ; Wed, 2 Dec 2026 19:49:54 +0000 (UTC) X-FDA: 85500083508.21.062316B Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf19.hostedemail.com (Postfix) with ESMTP id EEC231A000C for ; Wed, 2 Dec 2026 19:49:52 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=G6L9NeVi; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796240993; b=DnOOYTO3ljk/XMFRf7UGDXHkS3lFBUkQPvMuYKxwJxqIMcBlgiOKqhxihfUmE4B0ienFJ6 ZKXxDxe8OQch8uKJ5Rx+Q0z1vnUxcqcPcBcdInxTjZkhb/qELM8S1YsnbJYs9fc09g2+wQ QgzcOopZkOTsPGi6O93PYw3XvTliR0A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796240993; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dc9AYZuDwsjfpr9kzqOW6Qi+eVzEabiM8Ygb3/D2K+M=; b=wXwCypYEbayLL1d6NLI5FvtM4FHpX/V09YoV181FI2OV8v3wuZBKyCAM7Fpg6wM97ZoYED SETeviHquowHUHg10FODFoUYX1nClpLRQh9wAMTxTeaY4HyPy93FKdHz9ECLDxfHJFvXQL wOklmjFzJfOqkQ+wB8zndAoQhDPaP0Y= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=G6L9NeVi; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id A220260CB7; Wed, 2 Dec 2026 19:49:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8FD11F00893; Wed, 2 Dec 2026 19:49:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796240991; bh=dc9AYZuDwsjfpr9kzqOW6Qi+eVzEabiM8Ygb3/D2K+M=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=G6L9NeVi8Pmy9jSjt8RpfVnMWD3eNhFhSHfT7ZYeMXHw+E6iGBlo+sYYjQpfUIBrv vUkghEimGM+O2DmHOn+0boivdtNK4MhmYfa7p6ydO3KfhmNe8N6rCuVcFe1KDEJ4/c RZX4rwGIMqDP52d4k+Cd1aorsykx03pFzxVwNh0G0vRdZ01xXKb8CuSTTFGOyJTzdP LIJ3OfdA/ypsRdKzqiTQmKSfpSwzdCRARORgFsTR8DXC7u38asoTmCMkk/jViebfoy Bk8zmP0EUSiTQvFPlDg1RT2T6HpR7l+ZqledQXvrptjdGdRnu5a611OBU16JjH8heE /FWNxg+tdKjvw== Date: Wed, 2 Dec 2026 19:49:38 +0000 From: "Lorenzo Stoakes (ARM)" To: "David Hildenbrand (Arm)" Cc: "Liam R. Howlett" , Vlastimil Babka , "Borislav Petkov (AMD)" , Cenk =?utf-8?Q?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 , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2 1/2] mm/rmap: don't defer TLB flushes when unmapping lazyfree folios Message-ID: References: <20261201-file_folio_flush-v2-0-994d587aec8e@kernel.org> <20261201-file_folio_flush-v2-1-994d587aec8e@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261201-file_folio_flush-v2-1-994d587aec8e@kernel.org> X-Stat-Signature: 4idhr1hztf8tzsqjdts8qx58naqn7xr6 X-Rspamd-Queue-Id: EEC231A000C X-Rspam-User: X-Rspamd-Server: rspam04 X-Spam-Status: No, score=-15.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 autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796240992-639686 X-HE-Meta: U2FsdGVkX19PbPfwDDJLhWAbWNNS8DszFyCrdTCt/fydQzQldkCxjVsiKhAhIRQ4rHGRQtUvF9y0krY2VPkAJs7DwxrttAhc4hov1OfVou2TDTZNbANm5MTt+8uMMPJwUsiNJJAV13G4Sqczlpsj7skqhm2LmgM3cyJ+V60Xb1NiLqKY/RrUuqAKhQg4/HNpCM86XqBg/qGIoDyvyIWZuH0TTNgXd02xkpSYe2WGVpAA2GDAoiHGFCjPfo237WLZKqBsNNk3hWDnLu+udHanIP/UM5vPeJtFLNXs9szaqm2M624RZ710Jl6zR8fMxyUqJ6XD7H8Hey+J/eN5LEN++c6NvcoDa+3GMKJ0Pza3r9w/ycIVwOAsVtrTy/5aiECbtd6boOVypFSyqx6BwsVBJIq3yY1h5CF5l34/bU1CvXfVIs6qPMMkgg+6g7/xHYxV6t6D797I+Bw1sPlzPrYEmL+6CFcRpkxhl36R7bOlAumKoWHEykSVFW/znT4p0wor6Vj40WT+RStEZLKTwfMRbv7wh991p4s+KXUb1Jah6G7WRkL81xXgmqB/xeiEuVI3hMDdJ2aF3zOn4aXXSiPik61l68ZjyBGJ57BX5jKGo5cNzTPwoeqcorcEowVGvFS5m5sXbQROe3mJqWHxwCRHe7jCrkJjgQmLWb7wx4O+LV3I79U+3t5c+EGzfR9Iu2kQw0zef4M678P/V+a1PY9D8JIWi9JyNgoKSeFufR/6TiRGRWPLgWSuvmkapHqNk+B4oofMyTGCkmLuIL1wcaGoP1xlBGuMtjsMakICuNZqYa2NtLH/DB/p6HKQ43tigpjdJm1ckPErGGG23cqFOrrx3dkPw/vkiANTIhXmW6kksMpwnFHpPWOHm1vQnG7AAs5O/uKdbPmRUnjP4NqcjzuSb8Ep2h1olDYCtiRzGUpV095lHoL9odqjQ2zg15ttupL7u+/rgL5AbQjSwBlk2wr 5QK0wpFD gcVB7Rl0ul18flenTMV7uoxLxLhWUncA2Nw2kJY0BoRH96zNWDh3qfZsz7OgVTvSAb/TdcdpDZkyA8HQ0VNhp6IKGF8zevDAFWZIpkAZBwtKMu6MemFacFnnU0zYmTM/s8/7f0hQvQVHMO70+KJKO8ECyowchzuYwk52oWH9dgo8faiSjLAxhkYknf9hmDpEKrjFmRdbFHAD304Vu3gmNwgsfvVbayiHXwn0Kle1tdVmzpjbgIEbKcMTqqrDdbD4wNaiQ+mDBvNSYgTM9yyEqUeYdzAx5u56UuIxMx/LfL9n2WaaLwIJbH521IMFe5bvs83SUvK2j01yZI3bzqvZaz28W8cxnL49Ncpx94biv0XnFCN0McQaRm0/GVuwpC5FRkIiGvoxgQwizI5G9pRp91qo/3WhHoZLlEFN272u7l4csJ46BE40fACeY7D6akzbeGD0cS0iYU4qNrJBTsfChbFeTxHj0jES2QZus0KeXlcjxNQCAJV/0hAtv/K7EUKtWddsUK1WxM35ydIBUAb63xohLkfVpdNEdOA/oqAxTd3vGTX5S8MJ42VjnhyEpY3V6UBSylETHSeIPLVUeBivrjWEa9tn/jKqUreMj 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 On Tue, Dec 01, 2026 at 05:25:05PM +0100, David Hildenbrand (Arm) wrote: > 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) Lazyfree seems like yet another silly edge case thing we shouldn't have done (or maybe should have done better :) But anyway, LGTM so: Reviewed-by: Lorenzo Stoakes (ARM) But see below, claude came up with a thing about contpte when restoring PTEs. > --- > 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; Hmm it's like 'should defer' or 'should defer a little bit' :) But I can't see how the 'slight' delay does any harm and as the commit msg says you avoid unnecessary flushes. Claude mentioned something though it says this exit case the 'not restore PTEs' one can make contpte_set_ptes() restore a previously non-cont block as cont, leaving non-cont TLB entries overlapping new cont ones. So it's a question of whether the hw tolerates that or not. > 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 > -- Cheers, Lorenzo