From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 990F0606A31C for ; Sun, 29 Nov 2026 14:38:40 +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 kanga.kvack.org (Postfix) id BD8EF6B05B6; Sun, 29 Nov 2026 09:38:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BAFEB6B05B7; Sun, 29 Nov 2026 09:38:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC5FC6B05B8; Sun, 29 Nov 2026 09:38:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 89B8A6B05B6 for ; Sun, 29 Nov 2026 09:38:39 -0500 (EST) Received: from smtpin22.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1710580836 for ; Sun, 29 Nov 2026 14:38:39 +0000 (UTC) X-FDA: 85488412758.22.3576001 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf25.hostedemail.com (Postfix) with ESMTP id 7E8D3A0010 for ; Sun, 29 Nov 2026 14:38:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=AYxGBmND; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of baohua@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=baohua@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795963117; b=Y+ZOiAbXfIeCR5t5XBjldL5yf9m6HzJVLzn2LlYkM1DDp9JheV8DvvYQdHdtfb74QEKNLq 0MbirEYtd4JVJYxMZ/g6kdnLSeRVROnkYQO5UA1t8zFj3AjuITpdR5Hrr4iVdoG7pXLyMG dzrMYIC9N7fSbRyhFxrtUPuZPCmTyiA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795963117; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qrazpSDmUqZmBzI85MTbGEO3hQxkAHF1sikeDvegTiM=; b=xXDu7qihrN5ATge18/gNMR1tcOPps1tVwRAX3lLRA3urEQo/95z6lhAQQVAq/FTSZj2FDe KNx9JtXZf7fsPHHCe7H2KvkT2ZBF7p+M4WN24JxQ93jOfoXedAxw6bqBKeIWTjXX0LmQ1x sMTbN3RX5kP8cx2kd29j3JTAKhk/B58= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=AYxGBmND; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of baohua@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=baohua@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 06B2B60239; Sun, 29 Nov 2026 14:38:37 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 7E8D3A0010 X-Rspam-User: X-Stat-Signature: pi3rcps6hd96dyb8s7kpdigiu6c77hpz X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1795963117-467973 X-HE-Meta: U2FsdGVkX1/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TlvGcufsGOKT9Dv7jM7/5hocC/+TFBcPT5X+2AwGiqMn9R4iGLTdV/o+BdCiFht/1f2jXTxylGzGLFkJmYMIldDjb6+y+d7typ+LBz24BQYV8nlW4nGmNRd9W9M1JaI1Oj6hO/GSFtMkIvYH+BJ7oByn+uggs4NezUX4FNqe19X3sZFyPN43hZYpaGwGHWp9KU/JdGrEMLu3F4enOW04ZdF6ND8KrnkVNHzlUyJz/PXJzUNfLYOtzT94RJm7Qoh8tbBVulyCmoTowN5/Af3C25L2gmuDabB2gua41eKE+sGB3Douqb/5H5u29IH5tUUnfo5KmvL9LAZp+BXOhCLOe3HYsqaKJ1lwkuB6Bv9Zs6xLBlh08wJ4qLmdjd84uC/gd+cS+twXPm6/uP/+6vRZwt1jotZ8+/ld+Xe1widId+jm+ELIwlg== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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)