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 3907C606A32F for ; Wed, 2 Dec 2026 20:38:02 +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=mJyy2BQY Received: by kanga.kvack.org (Postfix) id 57A646B05CF; Wed, 2 Dec 2026 15:38:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 52AA06B05D8; Wed, 2 Dec 2026 15:38:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F2F76B0643; Wed, 2 Dec 2026 15:38:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id EF89C6B05CF for ; Wed, 2 Dec 2026 15:38:00 -0500 (EST) Received: from smtpin12.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 39480A04EC for ; Wed, 2 Dec 2026 20:38:00 +0000 (UTC) X-FDA: 85500204720.12.048F491 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf04.hostedemail.com (Postfix) with ESMTP id 7FBCF40009 for ; Wed, 2 Dec 2026 20:37:58 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=mJyy2BQY; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.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=1796243878; b=qLy49eywk+yOsmcXL4/0e/Mgn8Wq0YfC04uMcpuKdVUlLqHdycVcVMSUnz8KUJbK+BeEGP qjKHQ97I/pTgt+GKAuM/93v7FW65KcT/KNclpojJzNcgJROtyajTxx4OKNpVOb/MEH86Mc pq4tlt3A6qbpevejVNRvfsqArCKW7SM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796243878; 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=EMdI9DnC87y0jdBfz3PFL+IHAT0T5V1auDEJjtKoS5w=; b=323zitTlo86MF7PLy0h+DT62Ar8rCTsvkmnsfYRGdYqQrQbaEVU3ssH+Q5+Gh2bSUUyNjj AQKq76m52H5kPrdnMTCy78+YT5fExRD3KBXdPdLjrJEijD5fCAn9F+fhn+pdgHR6SOYqgS muV8sjCV2ZNFP4LPNa6xHKmu5zV4uzI= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=mJyy2BQY; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.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 E2B0A6120B; Wed, 2 Dec 2026 20:37:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 307291F00896; Wed, 2 Dec 2026 20:37:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796243877; bh=EMdI9DnC87y0jdBfz3PFL+IHAT0T5V1auDEJjtKoS5w=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=mJyy2BQYvnR4PrkZvVtqWajq+ii76lM8+tmwTD3tD1Ly0CddF5iYG6PRMcry0hI9Y Gu16DSjUlCCH6MRapn9zajMvIPTqOXBYinTAvBRyAt9c1b0vSHRiH6gIdQc1tz/gQV QzBQhMm55Ub96awIcsq5/V3YZraEjjpjE18fbk4yLp2RYf8vT3kQ0WDqm0N8boCiy/ 5lclD83nx6H9smkQ+UJjqYg0KjEyapdZPO7jlMq6GCdhKNAjz2PvJ8YFnoDCPpp5Ih cdEHczdUDoDFBAUb3kvBO0Qe1/kLaSUcHnIeCt7DHE/fu9XD9sO4OR7kIpW+BX2DL7 JoiiT07Dkm5Qw== Date: Wed, 2 Dec 2026 20:37:44 +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 2/2] mm/vmscan: unlock unmapped folios only after the deferred TLB flush Message-ID: References: <20261201-file_folio_flush-v2-0-994d587aec8e@kernel.org> <20261201-file_folio_flush-v2-2-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-2-994d587aec8e@kernel.org> X-Stat-Signature: qbmke8ku3fc5b4oiw4jr6jj8ny65dw5u X-Rspamd-Queue-Id: 7FBCF40009 X-Rspamd-Server: rspam10 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-Rspam-User: X-HE-Tag: 1796243878-370844 X-HE-Meta: U2FsdGVkX19K1JOPVyokmtzO7i4LtpQN6OeOF0DOXssEifdWOKHD6dWFarJ4nx138T23A75uNq7ku1fAAhzOifzSaF+P+m591j+3OR6MjGAI6iSoAna76gFd5oJ+VyLLuBkNFagxIAUSLv180ARVTvO090YMx+Kr/Srtf9ni0ZoehbJ774jSs11Wk2Iatj06eBHhitaQxOJOMH8u6Jg9qAlXtUMukqdQmBr/ZKRjrr1iV/fTfEl81ZqWAdAhm3KX1x6q3gHyOEclegjfhKeb+XyvAHLiNF+/97jrN/E+2MBtIYJ9reii8tHFBHc9pJpiOnfqrrTR2FK8NUNmHOHmWFaT2bqa6KJz9Io7aZwOak1UuHy580KIpyn07qb2JfOZEsovKOg9dOk7VcjVkEKSp8TRIyQFtA6T+uaw6/7w24MJupsAQMGTCKzrvamAp7K10XMzwH8eE0GBe3fL0yWwFoA6wNdNOtW4Nd03v8pnLPDjtP8DYCQLKGxOMRz+Hu0U/y3u2EJEfJwvMUtSqW67Kf2k8gRrAmYRrdbzOBPvUImradooWuQ75R5vu3vhbW4Q0bqh9nDdAbDQkTwsjtSJcO0aNvEoUrc7NfH/xX5sK6oOk9um3rnwZg8Yhvve4VWfN+yztK+Z/qcjh4aK8hPn3L7zEmgAODun4gSEJ0ceG8daHCaI34Zi8mLN5Ck5BgeFTVkpiWyyzEqwSusncr5eNx9agvx+ARRWJmBbEMsdt5XgvqMzna86Nyr6uyOlnH0frSSAm+ONwUTRDuE0TaLivjPtU6XGz7QCUkpEw0CtZ7KcUQi4/lfoeDFa8em5K/0tna7mrUDNWL8MS1/sNagGcxMPEyxt4iYBmySYAGDzShMqE17L2ay3V9xrPXBDRDAHfn9VKXl2L0Wel3fbpaEdjoYmKEJPSw8tCetpwEIRW+yeBi1YmGwGEuFVOeslwAicdIgzu+jPsRlc+axOMTl dOzFwccx JTqbaN/whuA9hP89Ef84AjB6TLvqhenV4R1GF8wJc32KkS2zd52KtuKX3BKoAC6ZyI4wADNMszrd4OEdmei4JuEQ8reihIva+n7hzLYHyV09DBcgSCjkd0L3d979f8ZEwfUbByvdx0Ao9nvoy21N3Ays9yvwyNt/y3f+sUPI032aCArkCveLyXqAWGA5o3eRE4z9sgxCBACiF5UGUVoDaBTtUIWk7ekFT1HAHcKIbfuFWtmFmIdEC5bDMU2IcsYXRg9Wq6TCiq6tE+GG1Ae6YOf8HuwZAVn5nuX0uOhTBS0htTITBPyLCaA4nUGAfoDrXnT1FyVtUYFYTJKqOedOZ6pWMLAqIL7cIwsOxPs+dzR/Hl5HGqBiyMinCVm3hhJoxiP9eqCl5b4FW2qGfpF31FpOgMMss5bTQl/2nRqJy0j2kNsOqib8/omMZ9/OQ+lWtt0C2YtL9kK/l89iEOSa4vfEuuBbvDcQg9mZgFkgI4/gxP38w7JYnGZM0LmQeqeKbkZC/qPNmqwdogbxN97xXIGvUNSSRB3r2nFsKYskDiQvHlUB1G7ewDfCeZpWxpNUj2EpVIx+UMkaSFdXHhNN0K/9xAOuzGvCvIEGAB6XRmYhnH7pHwxPaDjlBvFvUOEjj8sEIdXnQkioWdc7B17ZMHoGp+M3/T5dFGx6O7MA+YxPW8uA= 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:06PM +0100, David Hildenbrand (Arm) wrote: > try_to_unmap() with TTU_BATCH_FLUSH zaps the PTEs, transfers the PTE > dirty bit to the folio and defers the TLB flush. Until the TLB is > flushed, other CPUs might still access the folio through stale TLB > entries. > > However, reclaim unlocks folios it keeps and removes folios it frees > from the page-cache / swap-cache before performing the deferred TLB > flush. > > Stale writable TLB entries are currently handled by flushing them right > after try_to_unmap() if the folio is dirty, so writes through stale TLB > entries cannot get lost, but they are also not batched. > > Stale read-only TLB entries, on the other hand, are problematic: > > (1) A page fault can map a new page-cache folio, or swap in a new folio > once reclaim removed the old one from the swap-cache (e.g., a clean > swap-cache folio, or a dirty folio mapped by clean PTEs after > synchronous swap-out to zram or zswap). Modifications of the new > folio (e.g., through write() or page table mappings) are then not > visible to CPUs that still read the old folio. > > (2) A CoW fault in a MAP_PRIVATE mapping can copy a page-cache folio > that reclaim kept. Writes go to the new anon folio, while other CPUs > still read the old folio. > > (3) Invalidation (e.g., invalidate_inode_pages2()) can remove a folio > that reclaim kept from the page-cache without flushing the TLB, as > the folio is no longer mapped. A page fault can then map a new > folio while other CPUs still read the old folio. > > This can be reproduced easily by swapping out an anon memory range with > MADV_PAGEOUT, reading it back in, and swapping it out again > (MADV_PAGEOUT) while one thread keeps reading the range and another > thread writes to pages as soon as reclaim removed the folios from the > swap-cache. The first thread will occasionally read the old content. > > Fix it by reclaiming folios in batches of up to 64 folios: > > (a) Lock each folio, check whether we want to reclaim it, and unmap it. > Keep the folio locked if any deferred TLB flush is pending after > unmapping it, otherwise unlock it right away. > > (b) Flush the TLB once, and unlock all folios that are still locked. > > (c) Lock the folios again, one at a time, to try reclaiming them. > Activate folios that are still or again mapped, or that are under > writeback by now (marking them for immediate reclaim, like we do > in (a)), and skip folios that are poisoned by now, or that somebody > else holds locked. > > As others might map or unmap folios while they are unlocked, we might > now activate or reclaim some folios that we would previously have kept > or activated, which is fine. > > Keeping folios locked until the TLB flush makes sure that nobody can > remove them from the page-cache / swap-cache, copy them or start > writeback. Page faults will either wait on the folio lock, or find the > old folio and map it again, which is fine. > > Unlocking folios before trying to reclaim them makes sure that we never > hold more than one folio lock while performing I/O or waiting on the > filesystem. > > The idea of unlocking folios and locking them again using a trylock > before trying to reclaim them was inspired by [1] from Zhang Peng. > > As nobody can start writeback before the TLB flush, we no longer have to > flush stale writable TLB entries right after try_to_unmap(), so remove > the try_to_unmap_flush_dirty() call and batch these flushes as well. > This effectively enables batching TLB flushes of stale writable TLB > entries, which is what [1] tried to achieve. > > We must not wait for a folio lock, a folio flag or I/O while keeping > other folios locked, as that could deadlock. So flush the TLB and unlock > the batch early before waiting for writeback (legacy memcg), and before > allocating swap space would perform synchronous discard I/O > (folio_alloc_swap() now returns -EBUSY if allocation failed but retrying > with synchronous discard might help). > > Allocating swap space can otherwise only sleep when allocating memory > for the swap table, which cannot enter direct reclaim or compaction, > as reclaim runs with PF_MEMALLOC. > > We'll limit batching to 64 folios for now; the limit mostly matters for, > e.g., MADV_PAGEOUT and DAMON that can pass more, or when reclaim splits > large folios. > > Note that khugepaged and batched (async) folio migration already keep up > to HPAGE_PMD_NR (e.g., 512 with 4 KiB pages) small folios locked after > unmapping them with a deferred TLB flush. Both also call MMU notifiers > while keeping other folios locked, just like we do now. > > Keep code churn to a minimum. Some follow-up cleanups and optimizations > make sense, in particular: > > (1) Cleanly separate the stages of shrink_folio_list() (see the TODO). > > (2) Avoid unlocking and locking folios again that do not require a TLB > flush. But only if it's worth it and doesn't make the code too > ugly. > > (3) Remove the remaining try_to_unmap_flush() calls before freeing > folios; we only free folios that we flushed already or that we > never unmapped. > > (4) Remove the now-unused try_to_unmap_flush_dirty() infrastructure. > > Fixes: 72b252aed506 ("mm: send one IPI per CPU to TLB flush all entries after unmapping pages") > Link: https://lore.kernel.org/linux-mm/20261031-batch-tlb-flush-v6-0-190539839eec@tencent.com/ [1] > Cc: stable@vger.kernel.org > Acked-by: Zi Yan > Reviewed-by: Kairui Song > Signed-off-by: David Hildenbrand (Arm) Hmm this seems like a really heavyweight and significant change to be backporting, especially so far. It's like you're implementing a whole new mechanism here, but also backporting it. I wonder a bit how the net of these backported patches are going to interact honestly. But maybe I worry too much:) Maybe I'm just a bit frustrated at the rate of change going on ATM and how completely unmanageable it is to keep up with it all. I fear waking up to an mm with about 20 new mechanisms I wasn't aware of + now I have to figure them out/support them/etc :))) > --- > mm/internal.h | 5 ++ > mm/rmap.c | 5 ++ > mm/shmem.c | 2 +- > mm/swap.h | 5 +- > mm/swapfile.c | 7 ++- > mm/vmscan.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- > 6 files changed, 185 insertions(+), 14 deletions(-) > > diff --git a/mm/internal.h b/mm/internal.h > index 96cb55d7816b1..06c895a4078f1 100644 > --- a/mm/internal.h > +++ b/mm/internal.h > @@ -1246,6 +1246,7 @@ extern struct workqueue_struct *mm_percpu_wq; > #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH > void try_to_unmap_flush(void); > void try_to_unmap_flush_dirty(void); > +bool try_to_unmap_flush_pending(void); > void flush_tlb_batched_pending(struct mm_struct *mm); > #else > static inline void try_to_unmap_flush(void) > @@ -1254,6 +1255,10 @@ static inline void try_to_unmap_flush(void) > static inline void try_to_unmap_flush_dirty(void) > { > } > +static inline bool try_to_unmap_flush_pending(void) > +{ > + return false; > +} > static inline void flush_tlb_batched_pending(struct mm_struct *mm) > { > } > diff --git a/mm/rmap.c b/mm/rmap.c > index 7459722d22d9f..f7f08649f27e3 100644 > --- a/mm/rmap.c > +++ b/mm/rmap.c > @@ -731,6 +731,11 @@ void try_to_unmap_flush_dirty(void) > try_to_unmap_flush(); > } > > +bool try_to_unmap_flush_pending(void) > +{ > + return current->tlb_ubc.flush_required; > +} > + > static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, > unsigned long start, unsigned long end) > { > diff --git a/mm/shmem.c b/mm/shmem.c > index a5713109c843c..fe749cfd529a8 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -1954,7 +1954,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, > folio_mark_uptodate(folio); > } > > - ret = folio_alloc_swap(folio); > + ret = folio_alloc_swap(folio, /* may_sync_discard= */ true); > if (!ret) { > bool first_swapped = shmem_recalc_inode(inode, 0, nr_pages); > int error; > diff --git a/mm/swap.h b/mm/swap.h > index d5bf21f517dce..b77c8a071a0a8 100644 > --- a/mm/swap.h > +++ b/mm/swap.h > @@ -243,7 +243,7 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp); > * > * folio_put_swap(): does the opposite thing of folio_dup_swap(). > */ > -int folio_alloc_swap(struct folio *folio); > +int folio_alloc_swap(struct folio *folio, bool may_sync_discard); > int folio_dup_swap(struct folio *folio, struct page *page); > void folio_put_swap(struct folio *folio, struct page *page); > > @@ -363,7 +363,8 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry) > return NULL; > } > > -static inline int folio_alloc_swap(struct folio *folio) > +static inline int folio_alloc_swap(struct folio *folio, > + bool may_sync_discard) > { > return -EINVAL; > } > diff --git a/mm/swapfile.c b/mm/swapfile.c > index c2af13eb15b33..03b9484b88fc9 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1737,16 +1737,19 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, > /** > * folio_alloc_swap - allocate swap space for a folio > * @folio: folio we want to move to swap > + * @may_sync_discard: whether we may wait for synchronous discard I/O > * > * Allocate swap space for the folio and add the folio to the > * swap cache. > * > * Context: Caller needs to hold the folio lock. > * Return: %0 on success, %-E2BIG if splitting the folio might allow swapout, > + * %-EBUSY if allocation failed but retrying with @may_sync_discard might > + * succeed, > * %-ENOSPC if no global swap space is available, or %-ENOMEM if splitting > * would not help. > */ > -int folio_alloc_swap(struct folio *folio) > +int folio_alloc_swap(struct folio *folio, bool may_sync_discard) > { > unsigned int order = folio_order(folio); > unsigned int size = 1 << order; > @@ -1779,6 +1782,8 @@ int folio_alloc_swap(struct folio *folio) > local_unlock(&percpu_swap_cluster.lock); > > if (!order && unlikely(!folio_test_swapcache(folio))) { > + if (!may_sync_discard) > + return -EBUSY; > if (swap_sync_discard()) > goto again; > } > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 053f8e437bdd8..fb3a801ecbfbf 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -1155,6 +1155,105 @@ static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask) > return false; > } > > +/* > + * Maximum number of staged folios that shrink_folio_list() collects before > + * it flushes the TLB. > + */ > +#define MAX_STAGED_FOLIOS 64 > + > +struct staged_folios { > + /* Locked folios, waiting for the TLB flush. */ > + struct list_head unflushed; > + /* Unlocked folios, after the TLB flush (if required). */ > + struct list_head flushed; > + /* Number of folios in the current batch. */ > + unsigned int nr_folios; > + /* Whether we are reclaiming the folios of the current batch. */ > + bool reclaiming; > +}; > + > +static bool staged_folios_empty(struct staged_folios *staged) > +{ > + return list_empty(&staged->unflushed) && > + list_empty(&staged->flushed); > +} > + > +static void staged_folios_add(struct staged_folios *staged, > + struct folio *folio, bool unmapped) > +{ > + if (unmapped && try_to_unmap_flush_pending()) { > + /* Keep the folio locked until we flushed the TLB. */ > + list_add_tail(&folio->lru, &staged->unflushed); > + } else { > + folio_unlock(folio); > + list_add_tail(&folio->lru, &staged->flushed); > + } > + staged->nr_folios++; > +} > + > +static void staged_folios_flush(struct staged_folios *staged) > +{ > + struct folio *folio; > + > + try_to_unmap_flush(); > + list_for_each_entry(folio, &staged->unflushed, lru) > + folio_unlock(folio); > + list_splice_tail_init(&staged->unflushed, &staged->flushed); > +} > + > +static struct folio *staged_folios_next(struct staged_folios *staged, > + struct list_head *folio_list) > +{ > + struct folio *folio; > + > + if (staged_folios_empty(staged)) > + return NULL; > + /* > + * (a) Collect more folios until we have enough or there are no more. > + * Once we started reclaiming the folios of a batch, finish that first, > + * even if reclaim (e.g., splitting a folio) added folios to folio_list. > + */ > + if (!staged->reclaiming && staged->nr_folios < MAX_STAGED_FOLIOS && > + !list_empty(folio_list)) > + return NULL; > + > + /* > + * (b) Flush the TLB and unlock the folios that waited for the flush. > + * Nothing to do if we already did that for this batch. > + */ > + staged_folios_flush(staged); > + staged->reclaiming = true; > + > + /* (c) Hand out the next folio to reclaim. */ > + folio = list_first_entry(&staged->flushed, struct folio, lru); > + list_del(&folio->lru); > + /* We handed out all folios: start collecting again (a). */ > + if (list_empty(&staged->flushed)) { > + staged->nr_folios = 0; > + staged->reclaiming = false; > + } > + return folio; > +} > + > +static int staged_folios_alloc_swap(struct staged_folios *staged, > + struct folio *folio) > +{ > + bool may_sync_discard = list_empty(&staged->unflushed); > + int ret; > + > + ret = folio_alloc_swap(folio, may_sync_discard); > + if (ret != -EBUSY) > + return ret; > + > + /* > + * Don't wait for synchronous discard I/O while keeping other folios > + * locked: there is potential for deadlocks and long delays. > + */ > + staged_folios_flush(staged); > + may_sync_discard = true; > + return folio_alloc_swap(folio, may_sync_discard); > +} > + I genuinely do not think I've ever seen a Cc: stable patch add this much before, it's really crazy. Can we find some way of breaking this up or something? :) > /* > * shrink_folio_list() returns the number of reclaimed pages > */ > @@ -1166,6 +1265,10 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > struct folio_batch free_folios; > LIST_HEAD(ret_folios); > LIST_HEAD(demote_folios); > + struct staged_folios staged = { > + .unflushed = LIST_HEAD_INIT(staged.unflushed), > + .flushed = LIST_HEAD_INIT(staged.flushed), > + }; > unsigned int nr_reclaimed = 0, nr_demoted = 0; > unsigned int pgactivate = 0; > bool do_demote_pass; > @@ -1177,15 +1280,43 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > do_demote_pass = can_demote(pgdat->node_id, sc, memcg); > > retry: > - while (!list_empty(folio_list)) { > + /* > + * We reclaim folios in batches, in three phases: > + * > + * (a) Collect: we take folios from folio_list to pre-process them > + * (lock, check, unmap). Then, they move to the staged(unflushed) > + * list. If no TLB flush is pending for a folio, it moves to the > + * staged(flushed) list directly. > + * > + * (b) Flush: once we collected a batch of staged folios, or there are > + * no more folios to pre-process, we perform a single TLB flush and > + * move the staged(unflushed) folios to the staged(flushed) list > + * after unlocking them. > + * > + * (c) Reclaim: we take folios from the staged(flushed) list to actually > + * reclaim them, or to activate them if they are still mapped. Once > + * we took all folios of the batch, we start collecting again (a). > + * > + * Once all lists are empty, we are done. > + * > + * TODO: the stages of this algorithm should be cleanly separated. I mean this TODO is I guess you intended this patch to be reworked right? :) > + */ > + while (!list_empty(folio_list) || !staged_folios_empty(&staged)) { > struct address_space *mapping; > struct folio *folio; > enum folio_references references = FOLIOREF_RECLAIM; > bool dirty, writeback; > + bool unmapped = false; > unsigned int nr_pages; > > cond_resched(); > > + folio = staged_folios_next(&staged, folio_list); > + if (folio) { > + nr_pages = folio_nr_pages(folio); > + goto reclaim_staged; > + } > + > folio = lru_to_folio(folio_list); > list_del(&folio->lru); > > @@ -1324,6 +1455,11 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > /* Case 3 above */ > } else { > folio_unlock(folio); > + /* > + * Don't wait for writeback while keeping > + * staged folios locked. > + */ > + staged_folios_flush(&staged); > folio_wait_writeback(folio); > /* then go back and try same folio again */ > list_add_tail(&folio->lru, folio_list); > @@ -1382,7 +1518,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > split_folio_to_list(folio, folio_list)) > goto activate_locked; > } > - ret = folio_alloc_swap(folio); > + ret = staged_folios_alloc_swap(&staged, folio); > if (ret) { > int __maybe_unused order = folio_order(folio); > > @@ -1401,7 +1537,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > } > #endif > count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK); > - if (folio_alloc_swap(folio)) > + if (staged_folios_alloc_swap(&staged, folio)) > goto activate_locked_split; > } > /* > @@ -1455,21 +1591,40 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, > flags |= TTU_SYNC; > > try_to_unmap(folio, flags); > - /* > - * Flush deferred writable TLB entries before we could > - * drop the folio lock and others could start writeback. > - */ > - if (folio_test_dirty(folio)) > - try_to_unmap_flush_dirty(); > + unmapped = true; This seems like you're throwing in an optimisation for fun here too, which isn't really great. But claude raised the fact that PTE markers rely on this too (e.g. guard regions can be applied to file-backed mappings, but also UFFDIO_POISON) and you'll get writers writing through stale TLB entries this way, which seems like a really bad (TM) thing. It's gross to have to 'just know' to think about these things here but I think this should be reconsidered? > if (folio_mapped(folio)) { > stat->nr_unmap_fail += nr_pages; > if (!was_swapbacked && > folio_test_swapbacked(folio)) > stat->nr_lazyfree_fail += nr_pages; > - goto activate_locked; > } > } > > + staged_folios_add(&staged, folio, unmapped); > + continue; > + > +reclaim_staged: > + if (!folio_trylock(folio)) > + goto keep; > + > + /* > + * We failed to unmap the folio, or it was mapped again while it > + * was unlocked. > + */ > + if (folio_mapped(folio)) > + goto activate_locked; > + /* > + * While the folio was unlocked, writeback might have started, or > + * it might have been poisoned. > + */ > + if (folio_test_writeback(folio)) { > + folio_set_reclaim(folio); > + stat->nr_writeback += nr_pages; > + goto activate_locked; > + } > + if (folio_contain_hwpoisoned_page(folio)) > + goto keep_locked; > + > /* > * Folio is unmapped now so it cannot be newly pinned anymore. > * No point in trying to reclaim folio if it is pinned. > > -- > 2.56.1 > I think the rest of it needs to be chopped up to be really reviewable, or at least it's late here so :) -- Cheers, Lorenzo