Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Barry Song (Xiaomi)" <baohua@kernel.org>
To: akpm@linux-foundation.org
Cc: hannes@cmpxchg.org, mhocko@kernel.org, qi.zheng@linux.dev,
	shakeel.butt@linux.dev, kasong@tencent.com,
	axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com,
	baoquan.he@linux.dev, baolin.wang@linux.alibaba.com,
	mason@kernel.org, david@kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, ridong.chen@linux.dev,
	yuzhao@google.com, stable@vger.kernel.org,
	Barry Song <baohua@kernel.org>
Subject: [PATCH 2/3] mm/mglru: test PG_lru before taking a folio reference
Date: Sun, 29 Nov 2026 22:37:58 +0800	[thread overview]
Message-ID: <20261129143759.96221-3-baohua@kernel.org> (raw)
In-Reply-To: <20261129143759.96221-1-baohua@kernel.org>

From: Chris Mason <mason@kernel.org>

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 <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
[Kairui: Improved the comment]
Signed-off-by: Kairui Song <kasong@tencent.com>
Signed-off-by: Barry Song (Xiaomi) <baohua@kernel.org>
---
 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)



  parent reply	other threads:[~2026-11-29 14:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 14:37 [PATCH 0/3] mm/mglru: fix a few reclaim issues Barry Song (Xiaomi)
2026-11-29 14:37 ` [PATCH 1/3] mm/mglru: count all generations as evictable Barry Song (Xiaomi)
2026-11-29 14:37 ` Barry Song (Xiaomi) [this message]
2026-11-29 14:37 ` [PATCH 3/3] mm/mglru: report root reclaim to memory.pressure_level Barry Song (Xiaomi)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129143759.96221-3-baohua@kernel.org \
    --to=baohua@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kasong@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=qi.zheng@linux.dev \
    --cc=ridong.chen@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=weixugc@google.com \
    --cc=yuanchu@google.com \
    --cc=yuzhao@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox