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 BE40E606A322 for ; Wed, 2 Dec 2026 20:52: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=T1uBG3ZX Received: by kanga.kvack.org (Postfix) id 4D7DA6B0648; Wed, 2 Dec 2026 15:52:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AF116B0649; Wed, 2 Dec 2026 15:52:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C9596B064A; Wed, 2 Dec 2026 15:52:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A4BD66B0648 for ; Wed, 2 Dec 2026 15:52:39 -0500 (EST) Received: from smtpin05.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 658B11C3C66 for ; Wed, 2 Dec 2026 20:52:38 +0000 (UTC) X-FDA: 85500241596.05.3397FFB Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf25.hostedemail.com (Postfix) with ESMTP id B4B5EA000A for ; Wed, 2 Dec 2026 20:52:36 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=T1uBG3ZX; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796244756; b=XWaFOsj1WZ7Hce65BWIdTqM6BsDL23c6QwbUic4hjKSYPro7UOHEaqEUBViEdMz8/pVSnW TO6ylsXvxqW07TQToy0UIwA2Vf96oaZlJH0u12VGlHJVQICxCiDDgP22AJG3Dotlx8sbR0 /zyIOqRG6NcuHpWDws0LeTswn5K2crk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796244756; 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=LFqRXBBh6j+vhgTKHfAi2X3sMrS1gYVUTs3qVOutEy8=; b=YsPO/Px38NMQwDzoWX06k6XlFYboh+AUWkcRraQ1Lpt3Bxgp7J7zNzLm7WlRA5UkmMJ3/i CODorNQR1aYtrxQQeMwggpX2WVhjiqDu99elLE9WhkutfHcYT4eXcs/chYhIKaKpAzdHkP QwFBKlLL+phYYUoW88jyUJYmoQ7JOWw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=T1uBG3ZX; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id D65F240710; Wed, 2 Dec 2026 20:52:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B89B81F00899; Wed, 2 Dec 2026 20:52:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796244755; bh=LFqRXBBh6j+vhgTKHfAi2X3sMrS1gYVUTs3qVOutEy8=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=T1uBG3ZX2WjP8tvAoG9cunKms9KaIfaJZKTd/w09EeEmhUUhwlrxic79JwVC19nYo tbup6r9M1Pa2I64zrGlbhwfqri8N6UIpyMr/hsJs5y9lyRWIQCvLBXxop8qlrogp2Z eV3PszHX5PZvG2NFrPoIYFNi0Hv20DUd9nwl9oGmtju7WsRLaTtH2fbW44TaW2IGHe wKT/6mnQ+yylQ0vuLLX5QD12D3qGxzsHBvoN90IxVY68+vAXnoS8zNCL9BDFMNXhOU E2NUYqIZZfqv+D4/7pyx/b2rknrdvgR6u/yAIGjd4Kd1x2lK2SCVP+bSA8uW3UqTek CHMSK6lpFhQXQ== Date: Wed, 2 Dec 2026 20:52:23 +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> <5169e528-41fc-4c15-b34a-4ea037f47909@kernel.org> <5f0bc679-13e5-419a-8617-4a9b278ae0f1@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5f0bc679-13e5-419a-8617-4a9b278ae0f1@kernel.org> X-Stat-Signature: qx4cjk3oas6ekumwebiiwb6wjuq55gj7 X-Rspamd-Queue-Id: B4B5EA000A 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: 1796244756-787248 X-HE-Meta: U2FsdGVkX19c/+9lG9+Yldw9UbFpWFavGEU9xtkt9042lUZm7DCFs5OuMXl76sPg9+PJC4n/awMipSSOqZBsMIySy52+xCbSzCpk6WwRs90WM48dejweIaVC7LBT84Gc+0xJXtll1LTkOYePRsQFM3eg63RuwzWcubpp0hWwit73FJTQZvqgXA9RqzhqdmoqjDpYnYQVqC9pPN+ppQTxK7yonjNAyt3g7pRVdFHx764bMbv06lDAbj5tC2wmJaeNImqJSud5HC/jYyLkE7eg+espXv1i5fWvKjUP6i7ae19VTx3ODGCw5akhbckzBY4TWHprYHPITQ6jWfHxZt/q3bcO82bhXOXkM+nTQeLCt36j0MPhT3DQoTGmkBcl2QPmjGUSbi+7EngslTAbmC6AlJH54TBsNh59eDoETb9gmEtg2XiUkOp8se0YAW5oFab8TttuhzP28/AZXnu2X4o43g3yZlbhDuIgWNd87NlG8Luf/xBN3yDZYclZLaRFElIbGycIwtEpCyjxDfwShAtHt3vNZRbvNcKH1vpX2W/xz6uMrJiviLUwN4Wq4ZXV4+YbnOz8BqfqunCfgT8NqecUpOfeMu9g614i7i8pja5ZL5o+Clxl0d3eJPwp+wnPOy3BI0Jn/ZzadDOmeQqJOC1iENxcy3u4E3bqrvIVGTBfgp/aw8rF/n+BAyv+28wTSjmpjpZvUn59C+XSz+PohIcxmBoTs5nIFz8KM40KhSYVP77tmjdLwQB9mqaVq13GEn81RqdvcB2SZENAwON58XC8rCb/I+5IFcve8Wh/0T1l+mVBiUOUT/yA0KHy976gDOzvvPiSSyrv8JoLl5Rnrp8fP07rmOcuglgRuGONQByXfTCs/1T31V7zJrCP/x4ROAaVwenUlmDiYaQRR5nk47RH4fVswj9UNkE0uxeRM45y7liv5lOaCkMAuM7J+7f3kOHv1mW+913taF+FU0khfeu +j9S4DhR v73TxbV7EVrWHuXVw2wsJwJJkBaFHUUVLq+aU/jFBqLQph3NYGbTil6o5VDoSCIBtOxknNAvBi2YFW4L8/WOnFVTgX+t1U4cya+ZY5NFAO1yRtskIv5QevHZyDIo9Tl+ImoGwPxizM484vM6sgrjhwztYbj8uzTzlHWQa+0Jq2ZILFpkqIab0SkIsZiC8FVH2NEOY5PJasO/D6jNlvTdsicX3BF+EMYVWeuW4ETqKJEYcDn8Dy1cO1WrQIFzGOdF/etTI8xQpEQlCw6pAzMsfxUDdpXq0RZMck5a6yWJLtNTYEk+wLsPjb4S1H77dO67yEgliyblzQZB62PmixSYOapfCVQyrkcZMNMlmqWjzIKE5fNY+vjMdATDmjtk+ewVw/ENWyeeqXY3+jtAXCmpu7Gj5izjrMqJEVJRXfAv9ZSobR1PzNcfU+BhCBSd57EH2g62BQqW+CBb87UXjV0cI7fCZaGzM9Ui6Ozcbb7axzPsIkQN+1sPhB0+NBieLrMTqkgkVA0SQYVfy5RMqQat7rVkvAVbC2MU0kzRd6hp/U8KBSwQA8ndU+lDKIPq58IOWbOIXX7a7ckMqNuL/vjAHn4RkDNENd1EkdH9F 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 Wed, Dec 02, 2026 at 09:40:25PM +0100, David Hildenbrand (Arm) wrote: > On 12/2/26 21:36, David Hildenbrand (Arm) wrote: > > 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. > > Ah, now I get it, moving the flush after the set_pte_at restoration. In general, > arch code must properly handle the TLB when shattering/merging cont ptes, that's > not core-mm's responsibility. (we simply restore what was set earlier; if any of > that shattered a cont-pte, the arch code must perform the flushes itself) Ack. Yeah struck me that the expectation would be, if the original PTE entries are restored, that the TLB would remain valid, and any other change that might happen is indeed arch's responsibility. > > -- > Cheers, > > David -- Cheers, Lorenzo