[RFC PATCH 06/10] mm/migrate: keep folios with private data off the batch copy

From: Daniel Byrne

Date: Tue Dec 01 2026 - 09:20:05 EST


The batch path copies a folio after it is unmapped and before the move
phase. Buffer heads are written through without a folio reference, and
__buffer_migrate_folio() keeps such writers out only in the move phase,
after a batched copy has already run. The destination can then miss a
write. Other users of folio->private have migrate_folio() callbacks with
checks of their own, and a copy made ahead of them bypasses those too.

Refuse the batch copy for a folio that has private data attached, and
for one whose mapping migrates through a callback other than
migrate_folio(), filemap_migrate_folio() and the two buffer head ones.
Such a folio takes the per-folio copy in the move phase, as before the
batch path.

This is a fix to "mm/migrate: add batch-copy path in
migrate_pages_batch" and is meant to be folded into it.

Signed-off-by: Daniel Byrne <daniel1.byrne@xxxxxxxxx>
---
A fix to patch 2. For folding into v7.

mm/migrate.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/mm/migrate.c b/mm/migrate.c
index 79970170a53a..e584c3776618 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1722,10 +1722,34 @@ static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio,

static bool folio_can_batch_copy(struct folio *folio)
{
+ struct address_space *mapping;
+
/* movable_ops pages have a separate migration path */
if (unlikely(page_has_movable_ops(&folio->page)))
return false;

+ /*
+ * Buffer heads are written through without a folio reference, and
+ * __buffer_migrate_folio() keeps such writers out only in the move
+ * phase, after a batched copy would already have run. Other users
+ * of folio->private have migrate_folio() callbacks with checks of
+ * their own that a copy made ahead of them would bypass as well.
+ */
+ if (folio_has_attached_private(folio))
+ return false;
+
+ mapping = folio_mapping(folio);
+ if (mapping) {
+ const struct address_space_operations *a_ops = mapping->a_ops;
+
+ if (a_ops->migrate_folio &&
+ a_ops->migrate_folio != migrate_folio &&
+ a_ops->migrate_folio != filemap_migrate_folio &&
+ a_ops->migrate_folio != buffer_migrate_folio &&
+ a_ops->migrate_folio != buffer_migrate_folio_norefs)
+ return false;
+ }
+
/*
* Only batch when refcount matches the expected refcount.
* Otherwise a GUP holder could modify src between the
--
2.43.0