From: Chris Mason With MGLRU, reclaim that races with compaction for a folio can deadlock on the lruvec->lru_lock that it already holds. isolate_folio() runs under the lru_lock. It takes a reference, and drops it again if another isolator has already cleared PG_lru. That folio_put() must not be the last one, since the free path takes the lru_lock if PG_lru is set. But compaction can release the lru_lock while the folio is still on the list with PG_lru clear. If the folio is truncated or unmapped meanwhile, the two isolators hold the only references: isolate_migratepages_block() evict_folios() ============================ ============== folio_get_nontail_page() folio_test_clear_lru() goto isolate_abort lruvec_unlock_irqrestore() lruvec_lock_irq() isolate_folio() folio_try_get() folio_test_clear_lru() fails folio_set_lru() folio_put() folio_put() __page_cache_release() folio_lruvec_lock_irqsave() The fix used here is to leave a folio alone if PG_lru is already clear, before a reference is taken, as isolate_lru_folios() does. An isolator that clears PG_lru after that test has to take the lru_lock before it drops its own reference. Fixes: ac35a4902374 ("mm: multi-gen LRU: minimal implementation") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) [Kairui: Improved the comment] Signed-off-by: Kairui Song Signed-off-by: Barry Song (Xiaomi) --- mm/vmscan.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mm/vmscan.c b/mm/vmscan.c index 60c416819c57..5fb3055a4302 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4802,6 +4802,15 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca { bool success; + /* + * If PG_lru is set, the folio_put() below can't be the last one and + * free the folio: whoever clears the flag keeps its reference until + * it sets the flag again or takes the folio off the list, and it has + * to take the lru_lock we hold before it can do either. + */ + if (!folio_test_lru(folio)) + return false; + /* raced with release_pages() */ if (!folio_try_get(folio)) return false; -- 2.39.3 (Apple Git-146)