Re: [PATCH 2/2] mm/migrate: allocate destination folios before locking a batch
From: Zi Yan
Date: Mon Nov 23 2026 - 12:24:53 EST
On Sun Nov 22, 2026 at 7:42 AM EST, David Hildenbrand (Arm) wrote:
> Migration locks folios without holding a reference on their inode.
> migrate_pages_batch() keeps a whole batch locked while allocating the
> destinations of the next folios. If such an allocation ends up in direct
> reclaim with __GFP_FS, reclaim can evict an inode through the dcache
> shrinker, and wait in truncate_inode_pages_final() for a folio that we
> hold locked:
>
> migrate_pages_batch()
> migrate_folio_unmap(A)
> folio_trylock(A)
> migrate_folio_unmap(B)
> get_new_folio()
> try_to_free_pages()
> super_cache_scan()
> prune_dcache_sb()
> iput_final()
> evict()
> truncate_inode_pages_final()
> folio_lock(A)
>
> This requires that nothing else holds a reference on the inode, and can
> be triggered most easily with FUSE, which evicts inodes on the last
> reference.
>
> Consequently, allocations under the folio lock are dangerous if we
> cannot prevent that scenario through an inode reference; and we want to
> avoid grabbing inode references, because we might then drop the last
> one, and would have to evict the inode from migration context, ideally
> without holding any folio locks.
>
> Allocating in a NOFS scope would be one option, but it would have rather
> bad side effects, such as direct compaction not migrating file folios
> and the OOM killer not being invoked.
>
> So let's fix it by allocating all destination folios of a batch before
> locking any source folio. Now, we no longer allocate destination folios
> while holding folio locks, which also avoids holding these locks during
> direct reclaim or compaction for these allocations: an improvement on
> its own.
>
> We keep the destination folio of a folio that we have to retry, instead
> of freeing it and allocating a new one. If we would have to allocate
> while already holding folio locks, which can happen when retrying a
> failed split for longterm pinning, we leave the folio to the synchronous
> fallback in migrate_pages_sync() instead, which migrates folios one by
> one.
>
> Note that nfs_migrate_folio() still writes back the folio while holding
> its lock in MIGRATE_SYNC mode, which allocates memory. As NFS only
> evicts stale or unlinked inodes on the last reference, this is unlikely
> to deadlock; this must be sorted out in NFS code independently.
>
> This has medium urgency.
>
> Fixes: 5dfab109d519 ("migrate_pages: batch _unmap and _move")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> mm/migrate.c | 134 ++++++++++++++++++++++++++++++++++++++++++-----------------
> 1 file changed, 97 insertions(+), 37 deletions(-)
>
<snip>
> + /* Remember the source folio until we unmap it. */
> + dst->private = folio;
Clever to use ->private, but I wonder if we want a new field name, like
we added migrate_info, to clarify its use.
In addition, it will be great to keep migration info for each pair of
src and dst in an array of structs to avoid the ->private being used for
different things at different migration phases, but allocating such an
arrary will cause headaches. So this patch is probably the best we can
get.
Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
--
Best Regards,
Yan, Zi