From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795963116; b=KTT8Z7cB7+tU3RCfhH1aYxOpWe0/dY6H64VOJv+Uw9yyn4yPA+Fv95pPdCJegso8RGjm bF/r1Tmwekh8MKvCxdoPhub+uuKUVEpVVLdJ49Mu15iDBMcY9t0nPDgMXzVBNLnp/mO77 NU1q1tyk6PKvTTFIZxSqakv630J0L7vY/hzlyT9Fnz2JeOmHDvy+oIMUO5GNn6PWIwqWq hxPjWMUzfybDk1QLvl2S3IkInNmzSjMBtTGQToJUQ2aQU514EqO0KMKdvAPI5so0J002a ZJNnJd/K7SOb8hBXPu9Wyw8inAlX30pknjyKbHdgx7h0bJ1ElapiQRCT9tii4dcQ8wg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795963116; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-Id:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=qrazpSDmUqZmBzI85MTbGEO3hQxkAHF1sikeDvegTiM=; b=P7b/4nnyE4FtJFUgCvOrgA+ZTH3Y5UPLb53mEM/HzlkvChchD2Qq/d7KOCrHNhdiZl+H IjB9CnM5P5tHNWDA4gBEPKJ8zX6+ee/gxgp5sazIZu+XPaw9lN7HZaHQQqGV6+POqpBGO llrR4n7a1HJRqzMB6npwiaKMO9KgYKzkJpJQWPJZSVvaSTEu88eb25M2Xqo0wujoXRT0+ G6LytW+hjZp7cz2i7+c9JbaGysjYk/WzNep3XnxwHSjPC3Woh0qsCTMAgctWhZbvDTcfg V+sJp6x3cXVnV/RT0jHzin34aECSrQbRzd5jrskgX6HMgofu3LfSh9t0SBwnvD/kagQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AYxGBmND; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5F7B606A31C; Sun, 29 Nov 2026 14:38:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AYxGBmND Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2CB91F000C1; Sun, 29 Nov 2026 14:38:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795963116; bh=qrazpSDmUqZmBzI85MTbGEO3hQxkAHF1sikeDvegTiM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=AYxGBmNDwVO5HC78Sqm0JCsFZCozatgsFArbYA8mSS51RF5qqphYSUvMk7cDQDU+e Ftrw9xCWN7MP5pZJjQc6HsqWQWHwjXQENZksBL8TqqLAm2TAX4kyJLx89Riijaykga lVYRIV6s7iSFnANW2AvAk88Oex2CZb3FH0pzCkrVEq4VtoZKW2bSafjhyDc6K3tTZV h40nsBMk31E0hTezOCJwHsz8bnJ4J3UDwSliG41M/+ooB+E53DIXIazrtl0du8Wknb ++ergDkY/fYo7K9/3Sa5IUHOR+VAIT5ACM6r4pTsWknvtEr9JZlG3IIh4pmcD1o/LG G5pJKNYfSC1Vw== From: "Barry Song (Xiaomi)" 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 Subject: [PATCH 2/3] mm/mglru: test PG_lru before taking a folio reference Date: Sun, 29 Nov 2026 22:37:58 +0800 Message-Id: <20261129143759.96221-3-baohua@kernel.org> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20261129143759.96221-1-baohua@kernel.org> References: <20261129143759.96221-1-baohua@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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)