Re: [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush
From: Kairui Song
Date: Sun Nov 29 2026 - 06:21:15 EST
On Wed, Nov 18, 2026 at 5:22 PM David Hildenbrand (Arm)
<david@xxxxxxxxxx> 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.
Thanks for taking care of this!
> 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.
I'm not sure locking that many folios at once is safe under pressure.
Our first TLB batch flush series internally kept every folio locked,
but we found that unlock them improved performance further for
some workloads so we switched to the posted version from Peng.
But to fix the flush issue we do have to keep them locked though.
There is no deadlock, but seems fragile. e.g. shmem_get_folio_gfp()
takes a plain folio_lock(), so a thread faulting a parked folio waits for
the whole batch while holding mmap_lock, and the batch can be
blocked by any non-trylock part of rmap/splitting. The per-folio hold
time is much longer too. Maybe we can optimize other parts later
instead if there is any actual regression.
> 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.
I'm trying to make the swap table allocation sleepless, so we no longer
need to worry about that :-D Lian and Chris are pushing on the swap
queue series and it should come after that.
> + if (!may_sync_discard)
> + return -EBUSY;
> if (swap_sync_discard())
> goto again;
Hopefully the swap side can get rid of this too. The discard attempt is a
historical thing to avoid OOM on super slow discarding devices, reclaim
throttling (which is being unified for MGLRU) can handle that instead.
> +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;
> +};
Using a list is usually bad for performance IMO, Peng used a folio_batch,
which I'm also a bit concerned about stack usage-wise.. The overhead
could be trivial though, I tested this series and it's working
Ultimately I think we want just one or two unified batches for the whole
reclaim loop: from the very begiing of the call stack,
SWAP_CLUSTER_MAX is 32 which is the typical starting budget
under pressure, close to the folio_batch limit 31 already, and
we have a few of them already too.
> + /* Collect more folios until we have enough or there are no more. */
> + if (staged->nr_folios < MAX_STAGED_FOLIOS && !list_empty(folio_list))
> + return NULL;
> +
> + /* Flush the TLB and unlock the folios that waited for the flush. */
> + staged_folios_flush(staged);
folio_list can get repopulated from shmem (pageout() passes it to
shmem_writeout()), so the loop can leave N already-flushed folios behind and
get NULL from it. Should be fine, but it blurs the two stages.
> + while (!list_empty(folio_list) || !staged_folios_empty(&staged)) {
...
> + folio = staged_folios_next(&staged, folio_list);
> + if (folio) {
> + nr_pages = folio_nr_pages(folio);
> + goto reclaim_staged;
> + }
This looks... really weird; it will be much cleaner with Peng's cleanup I
guess.
> +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_contain_hwpoisoned_page(folio))
> + goto keep_locked;
The writeback part may better at least have a folio_set_reclaim() so they
don't miss a rotation? Better if we can reuse the pre-stage checks by
splitting that part out properly.
I tested this on x86 under pressure (40 GiB in one process with
swap, 4 processes concurrently, and 4 memcg-limited cgroups reclaiming
concurrently): TLB shootdown IPIs dropped by 90%, wall time by 30%.
Clean page cache seems unchanged, and on arm64 nothing changes
either. Really nice result!
So it looks good to me, just a few nits and a lot of cleanup to be done.
Reviewed-by: Kairui Song <kasong@xxxxxxxxxxx>