From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta0.migadu.com (out-224.mta0.migadu.com [91.218.175.224]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6BFD28505E for ; Fri, 21 Aug 2026 02:16:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.224 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787278592; cv=none; b=megTvLa/fuVONdviYDTPXMRcPsUEORAk7k4Jo7PionqPSPfLJQzA1lUsjz7eOp2mNfYCdEvl+Pu6JcjWLvAtSkYkyvDaWZJ2pzMk9olsI7NqB9qo8UDS/KmihD4MXe9+9J/3qeG0yd7hKYBec0D5Jq+kRWXTTISu0TPJWHWGtzo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787278592; c=relaxed/simple; bh=cQfTML8jAwykpKy6eZpPxbBffY9ZWjQHa+xVmDcrmew=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=g4r7YkPZQRR8MtY1kkkin8ESdHP8VMtIdlNDIDRkuA0CuJJpnOdUxbaLkPbLDCXQ52JL3N7kZ7BqWc4z7lkcifUM7NXssIYhDP4Ar1h0KSEITclzV/FB+Rt+5Ho2j6ZNOGz5XzAEVo0JTBNKkBXVVCRn+nE2M7vO7Uh9mjb1ej8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Wq8oJGAk; arc=none smtp.client-ip=91.218.175.224 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Wq8oJGAk" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=cQfTML8jAwykpKy6eZpPxbBffY9ZWjQHa+xVmDcrmew=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787278587; v=1; x=1787883387; b=Wq8oJGAkepwUhI9Og+QnNlfn63D+3A4EDQ3ua4Sr+Dn9HkM3xa6pKEAY2koWxPRUcnAIKHUK D6rw7hnJmdg/508lDpEccAzQnVyy8lMtzc4NKqHBWYUmnmuv+Hn+6+ZHvdgBYF9BTBnHqrtF2xP Hfz7SAtH0j275FFo003t7erQ= X-Envelope-To: linux-kernel@vger.kernel.org Received: from mi-ThinkCentre-M760t.mioffice.cn (14.29.108.92) by smtp.migadu.com with ESMTPS id 2ae2dc645df94b93; Fri, 21 Aug 2026 02:16:27 +0000 X-Mizu-Trace-ID: 2ae2dc645df94b93 X-Migadu-Flow: FLOW_OUT From: Ridong Chen To: Andrew Morton , Johannes Weiner Cc: Kairui Song , Qi Zheng , Shakeel Butt , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , David Hildenbrand , Michal Hocko , Lorenzo Stoakes , Steven Barrett , "Jan Alexander Steffens (heftig)" , Suleiman Souhlal , Yu Zhao , Oleksandr Natalenko , linux-mm@kvack.org (open list:MEMORY MANAGEMENT - MGLRU (MULTI-GEN LRU)), linux-kernel@vger.kernel.org, Ridong Chen , Ridong Chen Subject: [PATCH v2] mm/mglru: preserve inactive placement when enabling MGLRU Date: Fri, 21 Aug 2026 10:16:06 +0800 Message-Id: <20260821021606.877330-1-ridong.chen@linux.dev> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ridong Chen When the LRU is switched to MGLRU (echo y > /sys/kernel/mm/lru_gen/ enabled), fill_evictable() re-inserts every folio via lru_gen_add_folio(..., false). With reclaiming hardcoded to false, an inactive anonymous folio (no PG_active, not in the swapcache) takes the "gen = MIN_NR_GENS" branch in lru_gen_folio_seq() and is seeded at seq = max_seq - 1, which lru_gen_is_active() treats as active. Its inactive placement is lost and NR_INACTIVE_ANON is folded into NR_ACTIVE_ANON. Pass reclaiming=!active so a folio from an inactive list is seeded into an older generation. Folios from the active list carry PG_active and hit the first branch either way, so they are unchanged. reclaiming also selects the insertion end in lru_gen_add_folio(): list_add_tail() for inactive folios, list_add() for active ones. Both the legacy LRU and a MGLRU generation keep the hottest folios at the head and the coldest at the tail, and reclaim takes from the tail. To preserve that order the folio must be taken from the end matching the insertion end, so take inactive folios from the head and active folios from the tail; otherwise hot/cold would be reversed within the generation. Tested on x86_64, next-20260812, 2G VM + 1G swap, ~1.5G anon pushed onto the inactive list before enabling MGLRU: Active(anon) Inactive(anon) before switch (legacy) 2952 1548792 kB after `echo y`, unpatched 1552052 0 kB after `echo y`, patched 15144 1536636 kB Inactive file folios stay inactive either way (NR_INACTIVE_FILE is preserved). Fixes: 354ed5974429 ("mm: multi-gen LRU: kill switch") Suggested-by: Barry Song Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Ridong Chen Acked-by: Barry Song --- mm/vmscan.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 94fc4f25e99f..c17ac77b08a4 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -5311,7 +5311,17 @@ static bool fill_evictable(struct lruvec *lruvec) while (!list_empty(head)) { bool success; - struct folio *folio = lru_to_folio(head); + struct folio *folio; + + /* + * lru_gen_add_folio() uses list_add_tail() rather + * than list_add() when reclaiming is true. Match + * its ordering to avoid cold/hot inversion. + */ + if (active) + folio = lru_to_folio(head); + else + folio = list_first_entry(head, struct folio, lru); VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio) != active, folio); @@ -5319,7 +5329,11 @@ static bool fill_evictable(struct lruvec *lruvec) VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio); lruvec_del_folio(lruvec, folio); - success = lru_gen_add_folio(lruvec, folio, false); + /* + * Borrow reclaiming=true to place inactive folios in + * the older gens. + */ + success = lru_gen_add_folio(lruvec, folio, !active); VM_WARN_ON_ONCE(!success); if (!--remaining) -- 2.34.1