[PATCH v2 0/2] mm: fixes for stale TLB entries due to delayed/batched TLB flushes
From: David Hildenbrand (Arm)
Date: Tue Dec 01 2026 - 11:25:31 EST
These are the two remaining fixes from v1, whereby we fix two stale TLB
issues with TTU_BATCH_FLUSH during reclaim.
Patch #1: Don't defer TLB flushes when unmapping lazyfree folios. Handles
mapping a new anon folio after reclaim discarded a lazyfree folio
(do_anonymous_page(), migrate_vma_insert_page(), UFFDIO_COPY,
UFFDIO_MOVE).
Patch #2: Unlock unmapped folios only after the deferred TLB flush. Handles
refaulting a new page-cache folio, CoW of a folio that is still
in the page-cache, and swapping in a new folio (do_swap_page(),
unuse_pte(), UFFDIO_CONTINUE). Also handles invalidation
replacing a folio that reclaim kept, and write() modifying a new
page-cache folio.
There is one remaining problem that I describe in the cover letter of
v1 in detail.
Testing
-------
I threw a bunch of selftests at it and had AI generate some stress
tests, in particular for the benchmarks below. I also ran stress-ng to
force reclaim.
I managed to reproduce both issues, as described in the patch descriptions.
Stale reads out of 1024000 checked pages in 2000 iterations:
base series
lazyfree, MGLRU 16242 0
lazyfree, classic LRU 16274 0
swapcache, MGLRU 24076 0
swapcache, classic LRU 23028 0
Performance Impact
------------------
I ran some benchmarks on v1 in an x86-64 VM with limited CPUs, RAM and
zram enabled. Running inside a VM might make TLB flushes more expensive,
so the results below might be a bit exaggerated. AI results below.
Classic LRU, 4 threads randomly touching a working set twice the size of
their memcg for 30 seconds. Pages touched per second:
base series
anon-rw 681k 1055k
anon-ro 1013k 1027k
file-ro 417k 419k
file-rw 173k 183k
lazyfree 1512k 333k
Swapping out dirty anon folios ("anon-rw") is about 55% faster, because
patch #2 now also batches TLB flushes of stale writable TLB entries.
That is what Zhang Peng's series [1] tried to achieve, and his work also
inspired how patch #2 locks folios again, but the used mechanism is
different.
Reclaiming clean anon folios ("anon-ro"), clean page-cache folios
("file-ro") and dirty page-cache folios ("file-rw") is not noticeably
affected.
Reclaiming lazyfree folios ("lazyfree") is about 4.5x slower, because
we stop batching TLB flushes when discarding them entirely, which seems
to be the right thing to do for now. We can look into optimizing this
separately later.
Kairui tested v1 as well on x86-64 under memory pressure (40 GiB in one
process with swap, 4 processes concurrently, and 4 memcg-limited cgroups
reclaiming concurrently) [2]: TLB shootdown IPIs dropped by 90% and wall
time by 30%. Reclaiming clean page-cache folios seemed unchanged, and on
arm64 nothing changed either.
[1] https://lore.kernel.org/linux-mm/20261031-batch-tlb-flush-v6-0-190539839eec@xxxxxxxxxxx/
[2] https://lore.kernel.org/linux-mm/CAMgjq7DWWCF_iB+GmTciuXr=m=uk7mMmidvY=8yg6MH98X4oPg@xxxxxxxxxxxxxx/
---
Changes in v2:
- #1 and #2 are upstream now
- Rebase on top of mm-core/for-next
- "mm/rmap: don't defer TLB flushes when unmapping lazyfree folios"
- Avoid line split
- "mm/vmscan: unlock unmapped folios only after the deferred TLB flush"
- Label the three phases in the comments
- Finish reclaiming a batch before collecting more folios, even if
reclaim added folios to folio_list (e.g., splitting a shmem folio)
- Activate folios that went under writeback while unlocked
- Update the description of (c) and drop the outdated migration
example from the patch description now that that stuff is fixed
upstream
- Collect tags
- Link to v1: https://patch.msgid.link/20261118-file_folio_flush-v1-0-e9e739ed14e0@xxxxxxxxxx
---
David Hildenbrand (Arm) (2):
mm/rmap: don't defer TLB flushes when unmapping lazyfree folios
mm/vmscan: unlock unmapped folios only after the deferred TLB flush
mm/internal.h | 5 ++
mm/rmap.c | 19 ++++++-
mm/shmem.c | 2 +-
mm/swap.h | 5 +-
mm/swapfile.c | 7 ++-
mm/vmscan.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
6 files changed, 197 insertions(+), 16 deletions(-)
---
base-commit: 40e0fcedf9c39c3a4a0100323c101506b4206394
change-id: 20261114-file_folio_flush-7d1749fab2d8
Best regards,
--
David Hildenbrand (Arm) <david@xxxxxxxxxx>