Re: [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush

From: Zi Yan

Date: Sun Nov 22 2026 - 23:08:28 EST


On 18 Nov 2026, at 4:20, 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.

Who would have thought about it? I also wonder why we never get a report
for this issue.

>
> 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, and skip folios
> that are under writeback or 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.

This solution makes sense to me.

>
> 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; migration even allocates new
> folios, possibly entering direct reclaim, before flushing the TLB and
> unlocking them. 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.
>
> [1] https://lore.kernel.org/linux-mm/20261031-batch-tlb-flush-v6-0-190539839eec@xxxxxxxxxxx/
>
> Fixes: 72b252aed506 ("mm: send one IPI per CPU to TLB flush all entries after unmapping pages")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> mm/internal.h | 5 ++
> mm/rmap.c | 5 ++
> mm/shmem.c | 2 +-
> mm/swap.h | 5 +-
> mm/swapfile.c | 7 ++-
> mm/vmscan.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
> 6 files changed, 165 insertions(+), 14 deletions(-)

<snip>

> +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;
> + /* Collect more folios until we have enough or there are no more. */
> + if (staged->nr_folios < MAX_STAGED_FOLIOS && !list_empty(folio_list))

One thing to note here is that !list_empty(folio_list) can happen
in the middle of phase (c), when pageout() splits a large shmem folio,
and the code switches back to phase (a) and folios on ->flushed
will stay unlocked longer. This is discovered by AI and I verified it.

To make the code strictly follows the comment, you will want to
add ->draining and set it to true at phase (c) and back to false
at phase (a) and check it instead of !list_empty(folio_list).

Or add “most of time/usually” to the comment below.

> + return NULL;
> +

At this point, the code moves from phase (a) to phase (b).

> + /* Flush the TLB and unlock the folios that waited for the flush. */
> + staged_folios_flush(staged);
> +

Now we move from phase (b) to phase (c).

> + folio = list_first_entry(&staged->flushed, struct folio, lru);
> + list_del(&folio->lru);
> + /* We handed out all folios: start collecting again. */
> + if (list_empty(&staged->flushed))
> + staged->nr_folios = 0;
The batch is done, we move back to phase (a).

> + 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);
> +}
> +
> /*
> * shrink_folio_list() returns the number of reclaimed pages
> */
> @@ -1166,6 +1251,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 +1266,41 @@ 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 take folios from folio_list to pre-process them (lock, check,
> + * unmap). Then, they move to the staged(unflushed) list, from where
> + * they move to the staged(flushed) list after a batched TLB flush
> + * and after unlocking them. If no TLB flush is pending for a folio,
> + * it moves to the staged(flushed) list directly.
> + *
> + * We then take folios from the staged(flushed) list to actually
> + * reclaim them, or to activate them if they are still mapped.
> + *
> + * So we pre-process folios from folio_list until we collected a batch
> + * of staged folios, or until there are no more folios to
> + * pre-process. Then, we perform a single TLB flush and reclaim all
> + * folios of that batch, before we pre-process more folios.
> + *
> + * Once all lists are empty, we are done.

I wonder if you want to label the above three phases/steps and
add comments in staged_folios_next() to clarify how phase transition
happens.

Otherwise, LGTM.

Acked-by: Zi Yan <ziy@xxxxxxxxxx>

Best Regards,
Yan, Zi