[PATCH v7] mm/migrate: don't batch the TLB flush when there is nothing to batch

From: Zhiteng Qiu

Date: Sat Dec 05 2026 - 05:31:51 EST


NUMA balancing migrates one folio at a time, but since commit
7e12beb8ca2a ("migrate_pages: batch flushing TLB") it still unmaps with
TTU_BATCH_FLUSH like any other MIGRATE_ASYNC migration. When the batch
holds one small folio mapped only once, there is nothing to batch, and
the batched unmap can leave mm->tlb_flush_batched pending, so the NUMA
scanner's next change_pte_range() can call flush_tlb_mm() before
changing the PTE table. With migrations running concurrently, that
state keeps being refilled, and on this workload the scanner often
flushes the whole mm as it walks the PTE tables.

This slows scanning and keeps mm->tlb_flush_pending raised longer. On
x86, a migration that unmaps a PROT_NONE PTE while mm->tlb_flush_pending
is set gets true from pte_accessible() and needs a flush too. Without
INVLPGB, the resulting arch_tlbbatch_flush() is a full flush. The two
feed each other. In one run, 33% of the migrations overlapped a scan
pass, although passes covered only 9% of the time.

The bare-metal benchmark compared the baseline with v5 on a two-socket
Hygon 7447V (384 logical CPUs, 12 NUMA nodes). Both kernels used the
same v7.4-rc4-based mm-unstable tree and the same two Hygon die and LLC
topology fixes. The benchmark combined pointer chasing and allocations
across 384 threads on a 120 GiB anonymous working set backed by 4 KiB
pages. For each seed, both kernels ran for 180 seconds with NUMA
balancing on and 180 seconds with it off. The throughput and interrupt
counts below compare the baseline with v5. The bare-metal benchmark
has not been rerun with v7.

Throughput (million operations/s) TLB IRQs/migrated page
balancing on balancing off balancing on
seed base v5 base v5 base v5
1013 17.41 27.88 26.85 26.98 207.5 29.8
1019 18.46 29.12 26.60 27.08 199.7 26.1
1021 25.57 28.33 27.08 26.85 181.0 29.1

With balancing on, throughput increased by 60.1%, 57.7% and 10.8%
across the three seeds. With balancing off, the changes were +0.5%,
+1.8% and -0.8%. The last two columns divide the system-wide increase
in TLB interrupts (summed over CPUs in /proc/interrupts) by the increase
in numa_pages_migrated in /proc/vmstat over the same run.

Let migrate_pages_batch() choose the unmap flags for asynchronous
migration. If the current task already has a pending batched unmap
flush, keep TTU_BATCH_FLUSH for the tail so it can merge with that flush.
The pending state belongs to the task and may cover another mm. Rmap
sets current->tlb_ubc.flush_required after arch_tlbbatch_add_pending(),
and try_to_unmap_flush() clears it after completing the batch. When
nothing is pending and only one small folio remains, omit TTU_BATCH_FLUSH
if folio_maybe_mapped_shared() is false. In that case,
try_to_migrate_one() uses ptep_clear_flush() for a present PTE.
Large folios keep their existing behavior. For PMD-mapped THPs,
set_pmd_migration_entry() flushes regardless of TTU_BATCH_FLUSH.

Link: https://lore.kernel.org/r/20261122171706.9376E1F000C4@xxxxxxxxxxxxxxx
Signed-off-by: Zhiteng Qiu <qiuzhiteng@xxxxxxxx>
---
Changes in v7:
- Fix the build failure reported by linux-mm CI for v6. With batched
unmap flushing enabled, the CI parent (dd7acb657528) declares
try_to_unmap_flush_pending() in mm/internal.h. With it disabled,
the header provides a static inline stub that returns false.
The static inline definition v6 added outside that conditional
conflicted with the existing declaration in the CI build.
mm-unstable at 2d2d9f22c9c7 has neither declaration nor stub.
https://github.com/linux-mm/linux-mm/actions/runs/47491537730/job/145585174003
- Read current->tlb_ubc.flush_required in folio_migrate_ttu_flags()
under CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH. This keeps the
pending check in mm/migrate.c and leaves mm/internal.h unchanged
on both bases.
- Built x86-64 bzImage and modules locally on both bases with batching
enabled. The build on the CI parent included both MM selftest config
fragments used by the job. On each base, also compiled eight objects,
including mm/migrate.o and mm/rmap.o, under UML with migration enabled
and batched unmap flushing disabled.
- The uninstrumented kernel on each base passed the p13a_single
single-page migration smoke test. Traced both cases below in a
two-node, 8-vCPU KVM guest on each v7 kernel and reran the same v5
image as a control. In both cases, the workload's first async batch
had global trace seq=2 on the CI parent and seq=1 on mm-unstable
and v5.
* Writeback: each kernel ran 200 attempts. In the first async
two-page batch, the first folio returned -EBUSY with no flush
pending. Both v7 kernels flushed the second page's PTE directly
and left no flush pending. v5 deferred that flush. The second
page reached node 1 with its contents intact. move_pages()
retried the first page synchronously, so the final per-page status
showed node 1 for both pages and the test printed W_DONE valid=0.
The verdict uses the trace of the initial async batch.
* Eight-page batch: at least one of the first seven pages queued a
flush. Both v7 kernels and v5 deferred the eighth page's PTE
flush and cleared the pending state at batch end. All eight
pages reached node 1 with their contents intact.
- checkpatch.pl reported 0 errors and 0 warnings on the code diff.
With --strict it also reported one alignment CHECK for the two-tab
continuation indent requested by David.

v6 and earlier test results:
https://lore.kernel.org/r/20261205062847.2517357-1-qiuzhiteng@xxxxxxxx

In v6, following Sashiko's review, the batching decision started using
the current task's actual pending state. v5 had predicted it before
attempting the unmap.
https://lore.kernel.org/r/sashiko-outbox-250401@xxxxxxxxxx

For comparison, in the earlier 192-vCPU KVM benchmark with a 60 GiB
working set, v5 throughput with balancing enabled changed by -6.6%,
-0.1% and +5.5% across the same three seeds.

mm/migrate.c | 42 +++++++++++++++++++++++++++++++++++++++---
1 file changed, 39 insertions(+), 3 deletions(-)

diff --git a/mm/migrate.c b/mm/migrate.c
index f59dd50b3f229eb68098ab9b16b032f8fee36eb0..0737a373c850eadbaaa2710a4e77227e6069e1fe 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1227,9 +1227,41 @@ static void migrate_folio_done(struct folio *src,
folio_put(src);
}

+static enum ttu_flags folio_migrate_ttu_flags(struct folio *folio,
+ enum migrate_mode mode,
+ struct list_head *new_folios)
+{
+ if (mode != MIGRATE_ASYNC)
+ return 0;
+
+#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
+ /*
+ * Keep batching the tail if the current task already has a batched
+ * unmap flush to complete, possibly for another mm.
+ */
+ if (current->tlb_ubc.flush_required)
+ return TTU_BATCH_FLUSH;
+#endif
+
+ /* A batch with multiple folios can benefit from a batched flush. */
+ if (!list_is_singular(new_folios))
+ return TTU_BATCH_FLUSH;
+
+ /* Keep the existing flush behavior for large folios. */
+ if (folio_test_large(folio))
+ return TTU_BATCH_FLUSH;
+
+ /* A shared mapping can have more than one PTE to flush. */
+ if (folio_maybe_mapped_shared(folio))
+ return TTU_BATCH_FLUSH;
+
+ return 0;
+}
+
/* Obtain the lock on page, remove all ptes. */
static int migrate_folio_unmap(struct folio *src, struct folio *dst,
- enum migrate_mode mode, struct list_head *ret)
+ enum migrate_mode mode, enum ttu_flags ttu,
+ struct list_head *ret)
{
int rc = -EAGAIN;
int old_folio_state = 0;
@@ -1343,7 +1375,7 @@ static int migrate_folio_unmap(struct folio *src, struct folio *dst,
/* Establish migration ptes */
VM_BUG_ON_FOLIO(folio_test_anon(src) &&
!folio_test_ksm(src) && !anon_vma, src);
- try_to_migrate(src, mode == MIGRATE_ASYNC ? TTU_BATCH_FLUSH : 0);
+ try_to_migrate(src, ttu);
old_folio_state |= FOLIO_WAS_MAPPED;
}

@@ -1856,6 +1888,7 @@ static int migrate_pages_batch(struct list_head *from,
LIST_HEAD(unmap_folios);
LIST_HEAD(dst_folios);
bool nosplit = (reason == MR_NUMA_MISPLACED);
+ enum ttu_flags ttu;

VM_WARN_ON_ONCE(mode != MIGRATE_ASYNC &&
!list_empty(from) && !list_is_singular(from));
@@ -2018,7 +2051,10 @@ static int migrate_pages_batch(struct list_head *from,

cond_resched_tasks_rcu_qs();

- rc = migrate_folio_unmap(folio, dst, mode, ret_folios);
+ ttu = folio_migrate_ttu_flags(folio, mode,
+ &new_folios);
+ rc = migrate_folio_unmap(folio, dst, mode, ttu,
+ ret_folios);
/*
* The rules are:
* 0: folio will be put on unmap_folios list,

base-commit: 2d2d9f22c9c7ae457ba93480ba82ebf57cbf0df8