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) --- mm/migrate.c | 71 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 4743bd76e93f4..4bd10c6170d42 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1255,6 +1255,37 @@ static void migrate_folio_done(struct folio *src, folio_put(src); } +static bool migrate_try_lock_folio(struct folio *folio, enum migrate_mode mode) +{ + if (folio_trylock(folio)) + return true; + if (mode == MIGRATE_ASYNC) + return false; + + /* + * It's not safe for direct compaction to call lock_page. For example, + * during page readahead pages are added locked to the LRU. Later, when + * the IO completes the pages are marked uptodate and unlocked. However, + * the queueing could be merging multiple pages for one bio (e.g. + * mpage_readahead). If an allocation happens for the second or third + * page, the process can end up locking the same page twice and + * deadlocking. Rather than trying to be clever about what pages can be + * locked, avoid the use of lock_page for direct compaction altogether. + */ + if (current->flags & PF_MEMALLOC) + return false; + + /* + * In "light" mode, we can wait for transient locks (eg inserting a page + * into the page table), but it's not worth waiting for I/O. + */ + if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(folio)) + return false; + + folio_lock(folio); + return true; +} + /* 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) @@ -1265,36 +1296,8 @@ static int migrate_folio_unmap(struct folio *src, struct folio *dst, bool locked = false; bool dst_locked = false; - if (!folio_trylock(src)) { - if (mode == MIGRATE_ASYNC) - goto out; - - /* - * It's not safe for direct compaction to call lock_page. - * For example, during page readahead pages are added locked - * to the LRU. Later, when the IO completes the pages are - * marked uptodate and unlocked. However, the queueing - * could be merging multiple pages for one bio (e.g. - * mpage_readahead). If an allocation happens for the - * second or third page, the process can end up locking - * the same page twice and deadlocking. Rather than - * trying to be clever about what pages can be locked, - * avoid the use of lock_page for direct compaction - * altogether. - */ - if (current->flags & PF_MEMALLOC) - goto out; - - /* - * In "light" mode, we can wait for transient locks (eg - * inserting a page into the page table), but it's not - * worth waiting for I/O. - */ - if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(src)) - goto out; - - folio_lock(src); - } + if (!migrate_try_lock_folio(src, mode)) + goto out; locked = true; if (folio_test_mlocked(src)) old_folio_state |= FOLIO_WAS_MLOCKED; @@ -1630,12 +1633,8 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_f { int rc; - if (mode == MIGRATE_ASYNC) { - if (!folio_trylock(folio)) - return -EAGAIN; - } else { - folio_lock(folio); - } + if (!migrate_try_lock_folio(folio, mode)) + return -EAGAIN; rc = split_folio_to_list(folio, split_folios); folio_unlock(folio); if (!rc) -- 2.56.1