Re: [PATCH v2 31/31] mm/migrate: use consistent folio locking logic

From: Rik van Riel

Date: Fri Dec 04 2026 - 23:16:21 EST


On Fri, 2026-12-04 at 13:50 +0100, David Hildenbrand (Arm) wrote:
> migrate_folio_unmap() never blocks on the folio lock during direct
> compaction to prevent deadlocks, as the allocating task might hold
> that
> folio lock itself. Further, in MIGRATE_SYNC_LIGHT mode, it does not
> wait
> for the folio lock of folios that are not uptodate, as that would
> effectively wait for I/O.
>
> However, try_split_folio() blocks on the folio lock in any mode
> except
> MIGRATE_ASYNC. Consequently, a task that holds the folio lock of a
> large
> folio while allocating memory could deadlock when direct compaction
> tries
> splitting that very folio:
>
>     folio_lock(A)
>     high-order allocation
>       direct compaction
>         migrate_pages_batch()
>           try_split_folio(A)
>             folio_lock(A)
>
> For example, shmem_swapin_folio() can call shmem_replace_folio() with
> the folio lock of a large swapcache folio held, where it will try
> allocating a folio of the same order. Some conditions make this
> deadlock
> currently impossible: direct compaction skips folios that are not
> smaller than the requested order.
>
> There are no known cases where we could currently deadlock. But it
> should be the right thing to do to make this locking logic consistent
> in
> migration code.
>
> Let's make it consistent by factoring the locking logic of
> migrate_folio_unmap() out into migrate_try_lock_folio() and using it
> in
> try_split_folio() as well.
>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

Reviewed-by: Rik van Riel <riel@xxxxxxxxxxx>

--
All Rights Reversed.