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=1796243829; b=nm+ZW07y+9ohZLearh6Q7CHkrvwRX5oj3nuLIKZ6ai6jhqA3oYmcnsuhdr6KnwiKndh3 OnHro796qs18nn3mJNTSpCpgehQErAbvPZ+afrk2zsT/6xkB+SehwRehO2bdp7biB2xAF dCZLqtPpBa2nEGzByrpZmGsz4Fpo4QHCSCJQxpAhOpk5hHJbwNIXJCHl10KaivR3aF/p2 ZNTKgCIOYsmOgQZvKT+CBOHNhc73n9boYHPwNgF66biy6vm2vpoAqeyyXdw0NZcy98o2W qi3AiVlzjVVg+hLepxuThTCgW8XeYJuqRi/Bfbet3inw+6spLWbA0VoK1IicbsedPZw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796243829; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Autocrypt:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=8JX2E08+Gx116c+rD8lZvuKq8jiH6a6H0oTYImBEukI=; b=3crAR5ZB74nYteuTcHPsPmVhx1t1h3A3Pc/tL8/YSIxamvxp0zzJJ4pmBePn6s4LVzRA K+imcPh0SETe1xahSSEj+eMtu3FdnnzRbvD4cdt/1PXXLWyuDFM2TYHUKnAXQcGvy9MJd OXuRNLI7LzYLtG63GdS6BUFbc1Ov2oERU2KvFr7vbOQk03a6q2EgA1m9BtMXoEw5VFiqw uyh7nFRff3bhbHTbPG8nIbfPkqYR5OJ/3NP9jGkPbgSTE64f2QK7p3cbpz76VJ3f1ExNC YdXImYoFlapHitBKI0lNyvc2J5QGTzzqxQhuZPQwHDaLYAzjdQ1XP7Gx/bp3vPoStgw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Knl7ipyJ; 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 A13E1606A336; Wed, 2 Dec 2026 20:37:09 +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=Knl7ipyJ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E9841F00893; Wed, 2 Dec 2026 20:36:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796243829; bh=8JX2E08+Gx116c+rD8lZvuKq8jiH6a6H0oTYImBEukI=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=Knl7ipyJFoIBZFCCgW+AClXl1p74//H51RYwvHdWRVtFCQYZD7zK++XWi2S8m23TC Wbk6NzViEzxYeoX2M4sStu7ctxBUlMiavxlnizaIr71/xuJDsBIbFkwcwo07rFBpOr oL+l52fxOU8cnzwzhkrq86Icf85HGmj1LKCy3bKl1aXVXoPIQJNM2JaU8UFQgSXY4W kwqV1YYfRbfZ603MgYpCbqcxKvswsW4ml2k9xnSMHWo3O2hGIydI61VoRh5C0xfDWJ Kwy0z8TsomqurFvsqclaq87ZBRfeVOernyUuEDChs0kI07zm0Vs+oLoic7Bcd1/ZC8 vsAXvrWudLkCg== Message-ID: <5169e528-41fc-4c15-b34a-4ea037f47909@kernel.org> Date: Wed, 2 Dec 2026 21:36:56 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 1/2] mm/rmap: don't defer TLB flushes when unmapping lazyfree folios To: "Lorenzo Stoakes (ARM)" Cc: "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 , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20261201-file_folio_flush-v2-0-994d587aec8e@kernel.org> <20261201-file_folio_flush-v2-1-994d587aec8e@kernel.org> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoM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1ilGXde2ZzzGYl64glmccD8e87OnEgKnH3FbnJnT4iJchtSvx yJNi1+t0+qDti4m88+/9IuPqCKb6Stl+s2dnLtJNrjXBGJtsQG/sRpqsJz5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/2/26 20:49, Lorenzo Stoakes (ARM) wrote: > 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) Thanks. > > 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. That's arch code problems, and would be pre-existing. I'll ping some people to understand if something in arch code might need fixing. -- Cheers, David