From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FAD925A2C9 for ; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; cv=none; b=X8S8cFodE6Hy6I3cVsYp+NS49lG7qhaq2I4I9MhoFxBy2/W1kdu/7MG0ULYYPJ7X3yhoqFkt5JAPSMHiC0s4px1wHhxWtP+YvFw2Xl4nCNC+MnccuwGO4l3+13y6rohvgNisupdvyZAJCucD/4vd3IdPVHjhFUfyihKEysFwncw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; c=relaxed/simple; bh=I+nZc6FENlgDXRaKA1s8ECHyHfSCusr8kA1PV9U5nyA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kW96RKa2goPS5jdAoGBQag8s9FCaHl/+kRGHnClMIoUzx4fnxjcZKY6zlGbgkYpBoEAlXsNQ7HSLtjVuku21CGHBGfcHXVinB+qwa0toSMO6LWTtQkbXjA3ANgQ8YMibyadkV776jJgAlDa2jn0ejt+aY/8dX0f7bJAS/GsXQ7g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HVU5G1Fj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HVU5G1Fj" Received: by smtp.kernel.org (Postfix) with ESMTPS id 62F3AC2BCB2; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=I+nZc6FENlgDXRaKA1s8ECHyHfSCusr8kA1PV9U5nyA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=HVU5G1FjHQ7WQQXHi28KRd2hjAm2fGMApO3R1907IY3Rvn5NXQpYS3Vtw87RLynM5 GvmXItPEvY7Bl74aVU3d1MxtqnqD7kWAwG2eSpaoejiJMT8/J5ADRNGZySOLg/PPyS Y9dQGUp771B7Pe5aXISCWGIN1EfdxDXxyQcs9+0J0YjFnW8pR4j4c6oJYAikMPyeR2 5FV84d3kP6l0MbwD3nBwLRA4++0278lYdOS+P7+5M+A9Mdr9KmbqQjtUDER3OdDM6g q2Vqkp5M0r7ayBoh0qV7t6lzBB/d82VYrzY6RNrCKvt7BV9ivY3nCduxn6Sw8Zbx39 M4YRMJxHrLyKw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 500FBFDEE31; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:12 +0800 Subject: [PATCH v6 01/14] mm/mglru: consolidate common code for retrieving evictable size Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-1-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=3107; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=PvtqUSXOGCmiDyuBIhr9LBG6XYEzabfX8jVER+0UmH4=; b=30tb+xsrVdGTBZHX4gMVbjbmH8HzJBlBdXFBfVPne3n2lvFLEVbZ1JfdSDM0FQi/QgHP11t6r BKgv56O46jPA0HTAJFW47JI5PjHJ3dwJD10xxaRMGdPB2QEfs1gu+tb X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Merge commonly used code for counting evictable folios in a lruvec. No behavior change. Acked-by: Yuanchu Xie Reviewed-by: Barry Song Reviewed-by: Chen Ridong Reviewed-by: Axel Rasmussen Reviewed-by: Baolin Wang Signed-off-by: Kairui Song --- mm/vmscan.c | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index bd1b1aa12581..6fa828c7c19d 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4084,27 +4084,33 @@ static void set_initial_priority(struct pglist_data= *pgdat, struct scan_control sc->priority =3D clamp(priority, DEF_PRIORITY / 2, DEF_PRIORITY); } =20 -static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *= sc) +static unsigned long lruvec_evictable_size(struct lruvec *lruvec, int swap= piness) { int gen, type, zone; - unsigned long total =3D 0; - int swappiness =3D get_swappiness(lruvec, sc); + unsigned long seq, total =3D 0; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; - struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); DEFINE_MAX_SEQ(lruvec); DEFINE_MIN_SEQ(lruvec); =20 for_each_evictable_type(type, swappiness) { - unsigned long seq; - for (seq =3D min_seq[type]; seq <=3D max_seq; seq++) { gen =3D lru_gen_from_seq(seq); - for (zone =3D 0; zone < MAX_NR_ZONES; zone++) total +=3D max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); } } =20 + return total; +} + +static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *= sc) +{ + unsigned long total; + int swappiness =3D get_swappiness(lruvec, sc); + struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); + + total =3D lruvec_evictable_size(lruvec, swappiness); + /* whether the size is big enough to be helpful */ return mem_cgroup_online(memcg) ? (total >> sc->priority) : total; } @@ -4909,9 +4915,6 @@ static int evict_folios(unsigned long nr_to_scan, str= uct lruvec *lruvec, static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, int swappiness, unsigned long *nr_to_scan) { - int gen, type, zone; - unsigned long size =3D 0; - struct lru_gen_folio *lrugen =3D &lruvec->lrugen; DEFINE_MIN_SEQ(lruvec); =20 *nr_to_scan =3D 0; @@ -4919,18 +4922,7 @@ static bool should_run_aging(struct lruvec *lruvec, = unsigned long max_seq, if (evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS > max_seq) return true; =20 - for_each_evictable_type(type, swappiness) { - unsigned long seq; - - for (seq =3D min_seq[type]; seq <=3D max_seq; seq++) { - gen =3D lru_gen_from_seq(seq); - - for (zone =3D 0; zone < MAX_NR_ZONES; zone++) - size +=3D max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); - } - } - - *nr_to_scan =3D size; + *nr_to_scan =3D lruvec_evictable_size(lruvec, swappiness); /* better to run aging even though eviction is still possible */ return evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS =3D=3D max_se= q; } --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2B69341AB6 for ; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; cv=none; b=tnZB7vP6CYFOKNjE6qic/izd/LbDew6hs7nJIxOsejUSXE4R01mRFLks+AZDcMUpMEul+qWF61wEoZiRQcIZe8XB3JnffaKKZNzZljGtdbrN3f2I2odHL2VHIcZTZjEOJizOzlXbFwNEI+KQ/lASa8hGwpRvAGyZmcmvq1WleQU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; c=relaxed/simple; bh=BLJ7pj/RWnId9OK7LjkDaHvpjcc6H/hZLs4LeAzVOvU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RjrXUH8HIMbfvuSGgPoixq2Vh2HiSVU7Cl0r42/HwhuN5qFhsQtmYoSyaz4Y5hqWp5TfyG8QwLbwL01FgFXwFLuf8DnHT/4Tn/tDbLLZCzhmYf8quFzdBMWw7hyEKPcPutUPjOnQlXccjD551nNFCX2s1I9qpS9GnTDloECutoU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=vOgsLgTy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="vOgsLgTy" Received: by smtp.kernel.org (Postfix) with ESMTPS id 729FFC2BCB4; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=BLJ7pj/RWnId9OK7LjkDaHvpjcc6H/hZLs4LeAzVOvU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=vOgsLgTyfdlgRsui9NlCwCB8G6HlWTA/Ce5tXFUEBB9qHsWfl/sGjgV3zhsN/HcNT 2SlJo+8BbM1TyZmKzlm65VXJDJuxTs5wK9nSYBpQCrOYTgopj2SFcEkGi+/ywNzNWX y5m+5tzJtsJbJpLixWqAzid2m8GYpXwJQ0EvLLlZU826PZDYZpe3XkQqQpnN/w7g1r KXRxucGrHJvrLmrBfdQ+Q91FPNXaftnpUvup8LukFjVwBxt4NnBOdR6SS9FUfTK57l VS1wnX8dx3TnwK5DfAoGO9FnLxY2nOZo7g59ZIaPDWINWCRovh955ManZpt46LAYQL DrhEwfgU+CtKA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 630ECFDEE32; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:13 +0800 Subject: [PATCH v6 02/14] mm/mglru: rename variables related to aging and rotation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-2-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=2994; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=pkefrOisXQDVNekKYKJpaD9Nmjz051ENbT27IFjveYI=; b=gkk5Z3HYypIGbeGnGIXmkrnMrUp0l1JBZ8/g2tU8tOJaF/JLsKy3wLWaipgA6AXs70CsThGgi rp+zKSP77WrDynmVZyyxiI5qWsAWJ2qFdU41PXZUP8Fw5Lg87cySahn X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song The current variable name isn't helpful. Make the variable names more meaningful. Only naming change, no behavior change. Suggested-by: Barry Song Reviewed-by: Baolin Wang Reviewed-by: Chen Ridong Reviewed-by: Barry Song Reviewed-by: Axel Rasmussen Signed-off-by: Kairui Song --- mm/vmscan.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 6fa828c7c19d..4623a5ac6bc7 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4934,7 +4934,7 @@ static bool should_run_aging(struct lruvec *lruvec, u= nsigned long max_seq, */ static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,= int swappiness) { - bool success; + bool need_aging; unsigned long nr_to_scan; struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); DEFINE_MAX_SEQ(lruvec); @@ -4942,7 +4942,7 @@ static long get_nr_to_scan(struct lruvec *lruvec, str= uct scan_control *sc, int s if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) return -1; =20 - success =3D should_run_aging(lruvec, max_seq, swappiness, &nr_to_scan); + need_aging =3D should_run_aging(lruvec, max_seq, swappiness, &nr_to_scan); =20 /* try to scrape all its memory if this memcg was deleted */ if (nr_to_scan && !mem_cgroup_online(memcg)) @@ -4951,7 +4951,7 @@ static long get_nr_to_scan(struct lruvec *lruvec, str= uct scan_control *sc, int s nr_to_scan =3D apply_proportional_protection(memcg, sc, nr_to_scan); =20 /* try to get away with not aging at the default priority */ - if (!success || sc->priority =3D=3D DEF_PRIORITY) + if (!need_aging || sc->priority =3D=3D DEF_PRIORITY) return nr_to_scan >> sc->priority; =20 /* stop scanning this lruvec as it's low on cold folios */ @@ -5040,7 +5040,7 @@ static bool try_to_shrink_lruvec(struct lruvec *lruve= c, struct scan_control *sc) =20 static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) { - bool success; + bool need_rotate; unsigned long scanned =3D sc->nr_scanned; unsigned long reclaimed =3D sc->nr_reclaimed; struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); @@ -5058,7 +5058,7 @@ static int shrink_one(struct lruvec *lruvec, struct s= can_control *sc) memcg_memory_event(memcg, MEMCG_LOW); } =20 - success =3D try_to_shrink_lruvec(lruvec, sc); + need_rotate =3D try_to_shrink_lruvec(lruvec, sc); =20 shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, sc->priority); =20 @@ -5068,10 +5068,10 @@ static int shrink_one(struct lruvec *lruvec, struct= scan_control *sc) =20 flush_reclaim_state(sc); =20 - if (success && mem_cgroup_online(memcg)) + if (need_rotate && mem_cgroup_online(memcg)) return MEMCG_LRU_YOUNG; =20 - if (!success && lruvec_is_sizable(lruvec, sc)) + if (!need_rotate && lruvec_is_sizable(lruvec, sc)) return 0; =20 /* one retry if offlined or too small */ --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C71F135A3BF for ; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; cv=none; b=keCQCm6rswxu/RcCZjJVWbWxzce6UUAibLz/Ji/7JlVVSVkgEcWARDMbu5dxf8RwjGS8ZcV0TxqqZFgPeI8mySkpxx80Ch67JFA2x/h9ml1ktTUFzWh8wwXJS4GfEQJ2nNSE/2pg46Wwop58HyMLXYcxB307o5mWcflPT8gYwUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; c=relaxed/simple; bh=FRgOh8fXu0OnS1jgsvLiDcuxxGzAQqhYewYLvhhv/54=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BHBUgKjKlMJrW/Pl/aYi/hzLDYvCo2CUctarE2vOxtlyWo3tSZeQ3YFCNZLWAOAxlqchNxTIVDxkSyLeaqwgn+E+ba8djqjGOkfLOd6dGrUGlDN+sgzIlBAV+v+wAxnBxlhjP1R/UtGEsVI79JnD9kZ3/yFZ+b9CTcox0mm3SBg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FwnDeVRJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FwnDeVRJ" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7FE86C2BCC4; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=FRgOh8fXu0OnS1jgsvLiDcuxxGzAQqhYewYLvhhv/54=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=FwnDeVRJNpYtbgu1PFvdXj+qes2skaOXBC6AnRtQBmUbVq7cVUpO6IYBEBUTy9jZv OZqKvodRjhqTNCOWybnNbx0Y9qPCgv+aDaITQMKswZTwL+OwFGIFZ3ujH2seYrEX3Q R6ulM/K/OMJpEXond+d/WoAFI4/q4InfU5tPheoZMyjkM/e2rE86vFUueNZN03+772 etY2uL6I1weGW+6mgzh67s/WpG113ubsbXZfyKxygDEjj03Xf8VeW1ApcZoI43a1HB tGptrxLJe//WSDKyZXlpJWIbV5k2jDEEjECeVaMV/I+Rdt1cw8TRZqzYkkd2xMmcGr HkdsqQ4gfvRQw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 740E2FDEE35; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:14 +0800 Subject: [PATCH v6 03/14] mm/mglru: relocate the LRU scan batch limit to callers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-3-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=3315; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=JtRi0BCJNMNigPm1+Pq4gKGtKdNzBL8ESmEzjD65K78=; b=a43u0V3eA+y3mDo5zbYcjIw9REK0ungRg6PBSouYWfBUp2iI/KxK0xUdiWjRta7tTCaFZEec0 p/1/iOENuwRCgwtl9XD3ZnpPBFc39Kl/Uv+ee/DQ2q9DHB23XSXsULn X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Same as active / inactive LRU, MGLRU isolates and scans folios in batches. The batch split is done hidden deep in the helper, which makes the code harder to follow. The helper's arguments are also confusing since callers usually request more folios than the batch size, so the helper almost never processes the full requested amount. Move the batch splitting into the top loop to make it cleaner, there should be no behavior change. Reviewed-by: Axel Rasmussen Reviewed-by: Baolin Wang Reviewed-by: Barry Song Reviewed-by: Chen Ridong Signed-off-by: Kairui Song --- mm/vmscan.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 4623a5ac6bc7..3c5a6ae92440 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4695,10 +4695,10 @@ static int scan_folios(unsigned long nr_to_scan, st= ruct lruvec *lruvec, int scanned =3D 0; int isolated =3D 0; int skipped =3D 0; - int scan_batch =3D min(nr_to_scan, MAX_LRU_BATCH); - int remaining =3D scan_batch; + unsigned long remaining =3D nr_to_scan; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; =20 + VM_WARN_ON_ONCE(nr_to_scan > MAX_LRU_BATCH); VM_WARN_ON_ONCE(!list_empty(list)); =20 if (get_nr_gens(lruvec, type) =3D=3D MIN_NR_GENS) @@ -4751,7 +4751,7 @@ static int scan_folios(unsigned long nr_to_scan, stru= ct lruvec *lruvec, mod_lruvec_state(lruvec, item, isolated); mod_lruvec_state(lruvec, PGREFILL, sorted); mod_lruvec_state(lruvec, PGSCAN_ANON + type, isolated); - trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, scan_batch, + trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, scanned, skipped, isolated, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); if (type =3D=3D LRU_GEN_FILE) @@ -4987,7 +4987,7 @@ static bool should_abort_scan(struct lruvec *lruvec, = struct scan_control *sc) =20 static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_contro= l *sc) { - long nr_to_scan; + long nr_batch, nr_to_scan; unsigned long scanned =3D 0; int swappiness =3D get_swappiness(lruvec, sc); =20 @@ -4998,7 +4998,8 @@ static bool try_to_shrink_lruvec(struct lruvec *lruve= c, struct scan_control *sc) if (nr_to_scan <=3D 0) break; =20 - delta =3D evict_folios(nr_to_scan, lruvec, sc, swappiness); + nr_batch =3D min(nr_to_scan, MAX_LRU_BATCH); + delta =3D evict_folios(nr_batch, lruvec, sc, swappiness); if (!delta) break; =20 @@ -5623,6 +5624,7 @@ static int run_aging(struct lruvec *lruvec, unsigned = long seq, static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct s= can_control *sc, int swappiness, unsigned long nr_to_reclaim) { + int nr_batch; DEFINE_MAX_SEQ(lruvec); =20 if (seq + MIN_NR_GENS > max_seq) @@ -5639,8 +5641,8 @@ static int run_eviction(struct lruvec *lruvec, unsign= ed long seq, struct scan_co if (sc->nr_reclaimed >=3D nr_to_reclaim) return 0; =20 - if (!evict_folios(nr_to_reclaim - sc->nr_reclaimed, lruvec, sc, - swappiness)) + nr_batch =3D min(nr_to_reclaim - sc->nr_reclaimed, MAX_LRU_BATCH); + if (!evict_folios(nr_batch, lruvec, sc, swappiness)) return 0; =20 cond_resched(); --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD42A363C5B for ; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; cv=none; b=O5tkP431sAncdkOUXNkUory6xxi/Gzm3O9iQKw8HJGvF6kJsjst8irWc9Q+qD+19SvsAU7hEuFKVJHszIEd1FzTfQLRkuE4oFTIpN9rb8rt0e5c751GFQbZ1vtForqMCoGTcnzuQmKKTeWfBQZv9SeC5yTCGejwK3BbnJPwW6Ps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; c=relaxed/simple; bh=6IKi2rWc+WPWKqgKFt/2w2199Z1fmv4nfi1USnmh4SE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YVpMXX7rgPUSVYmpXADWM7B/zUUabFPuwxGWIKgUmXyjl506Sb7g3Wh5azfdx9YB/EId9rmtfT4Oo/toLJ82rLcta0rkRiE9/5kG4D5u12VGAU7qicjUR/YOvBSnyZTQjTCXi/EPlpIiiHOrjq4uVShtESjr00TwkTSPnAfECPQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e2YE4xV0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="e2YE4xV0" Received: by smtp.kernel.org (Postfix) with ESMTPS id 90F6EC2BCF4; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=6IKi2rWc+WPWKqgKFt/2w2199Z1fmv4nfi1USnmh4SE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=e2YE4xV0FDAMJyv3E/pN7TLdFatFG+XqB9DZJRUj5iiKmc6rvq5GvqGn3IDJHrTaz btO6mWfiEz9Neeo218KssnK3TpGlxBzJmGN13phg06AicUSf2dlyiepIABqyimOD7S Y8R2s0rYkHu3V3w4yy2e7W4tqg3bn9l36qMPs931u/ik0eVz8BZ34i+ODVmJ67VGYD chOJvG6sRKeNXCStVar6KpU1TpVxUnqWC+RpvHD7ocYB8CXQPj+DN9i/0BNzJPNZ4q 3XzrpGKdQ8LjE1M3ujef7KHV2PfPq0a/7xl+x4Gywik8xOknD67iN6MdPlPIc0Vn/+ 7YdLmCmMs7c2Q== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85956FDEE33; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:15 +0800 Subject: [PATCH v6 04/14] mm/mglru: restructure the reclaim loop Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-4-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=6446; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=zTJS8bEtP6qc/kBSze97l/RtD6AnJQpsOgfF9tsEBIE=; b=LXQmh9e6LJifzIld3GxrjaGzTNXILGR5M0GRW74boJ36+L/gJBtm4Cx1iJ/7PEfIiH5pagxK5 NNoMrvr6Ca4DS7wEjPKX+/Uw0YJFHl3hCRrPVkWU+LKlye+AoV7e5UC X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song The current loop will calculate the scan number on each iteration. The number of folios to scan is based on the LRU length, with some unclear behaviors, eg, the scan number is only shifted by reclaim priority when aging is not needed or when at the default priority, and it couples the number calculation with aging and rotation. Adjust, simplify it, and decouple aging and rotation. Just calculate the scan number for once at the beginning of the reclaim, always respect the reclaim priority, and make the aging and rotation more explicit. This slightly changes how aging and offline memcg reclaim works: Previously, aging was skipped at DEF_PRIORITY even when eviction was no longer possible, so the reclaimer wasted an iteration until the priority escalated. Now aging runs immediately whenever it is needed to make progress; the DEF_PRIORITY skip only applies when eviction is still viable. This may avoid wasted iterations that over-reclaim slab and break reclaim balance in multi-cgroup setups. Similar for offline memcg. Previously, offline memcg wouldn't be aged unless it didn't have any evictable folios. Now, we might age it if it has only 3 generations and the reclaim priority is less than DEF_PRIORITY, which should be fine. On one hand, offline memcg might still hold long-term folios, and in fact, a long-existing offline memcg must be pinned by some long-term folios like shmem. These folios might be used by other memcg, so aging them as ordinary memcg seems correct. Besides, aging enables further reclaim of an offlined memcg, which will certainly happen if we keep shrinking it. And offline memcg might soon be no longer an issue with reparenting. Overall, the memcg LRU rotation, as described in mmzone.h, remains the same. Also apply a minimal batch factor when reclaim is running with higher priority so small memcg won't be over protected. Reviewed-by: Axel Rasmussen Signed-off-by: Kairui Song --- mm/vmscan.c | 70 ++++++++++++++++++++++++++++++++-------------------------= ---- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 3c5a6ae92440..757beb605980 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4913,49 +4913,41 @@ static int evict_folios(unsigned long nr_to_scan, s= truct lruvec *lruvec, } =20 static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, - int swappiness, unsigned long *nr_to_scan) + struct scan_control *sc, int swappiness) { DEFINE_MIN_SEQ(lruvec); =20 - *nr_to_scan =3D 0; /* have to run aging, since eviction is not possible anymore */ if (evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS > max_seq) return true; =20 - *nr_to_scan =3D lruvec_evictable_size(lruvec, swappiness); + /* try to avoid aging, do gentle reclaim at the default priority */ + if (sc->priority =3D=3D DEF_PRIORITY) + return false; + /* better to run aging even though eviction is still possible */ return evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS =3D=3D max_se= q; } =20 -/* - * For future optimizations: - * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg - * reclaim. - */ -static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,= int swappiness) +static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, + struct mem_cgroup *memcg, int swappiness) { - bool need_aging; - unsigned long nr_to_scan; - struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); - DEFINE_MAX_SEQ(lruvec); + unsigned long nr_to_scan, evictable; =20 - if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) - return -1; - - need_aging =3D should_run_aging(lruvec, max_seq, swappiness, &nr_to_scan); + evictable =3D lruvec_evictable_size(lruvec, swappiness); + nr_to_scan =3D evictable; =20 /* try to scrape all its memory if this memcg was deleted */ - if (nr_to_scan && !mem_cgroup_online(memcg)) + if (!mem_cgroup_online(memcg)) return nr_to_scan; =20 nr_to_scan =3D apply_proportional_protection(memcg, sc, nr_to_scan); + nr_to_scan >>=3D sc->priority; =20 - /* try to get away with not aging at the default priority */ - if (!need_aging || sc->priority =3D=3D DEF_PRIORITY) - return nr_to_scan >> sc->priority; + if (!nr_to_scan && sc->priority < DEF_PRIORITY) + nr_to_scan =3D min(evictable, SWAP_CLUSTER_MAX); =20 - /* stop scanning this lruvec as it's low on cold folios */ - return try_to_inc_max_seq(lruvec, max_seq, swappiness, false) ? -1 : 0; + return nr_to_scan; } =20 static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *= sc) @@ -4985,31 +4977,44 @@ static bool should_abort_scan(struct lruvec *lruvec= , struct scan_control *sc) return true; } =20 +/* + * For future optimizations: + * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg + * reclaim. + */ static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_contro= l *sc) { + bool need_rotate =3D false; long nr_batch, nr_to_scan; - unsigned long scanned =3D 0; int swappiness =3D get_swappiness(lruvec, sc); + struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); =20 - while (true) { + nr_to_scan =3D get_nr_to_scan(lruvec, sc, memcg, swappiness); + while (nr_to_scan > 0) { int delta; + DEFINE_MAX_SEQ(lruvec); =20 - nr_to_scan =3D get_nr_to_scan(lruvec, sc, swappiness); - if (nr_to_scan <=3D 0) + if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) { + need_rotate =3D true; break; + } + + if (should_run_aging(lruvec, max_seq, sc, swappiness)) { + if (try_to_inc_max_seq(lruvec, max_seq, swappiness, false)) + need_rotate =3D true; + /* stop scanning as it's low on cold folios */ + break; + } =20 nr_batch =3D min(nr_to_scan, MAX_LRU_BATCH); delta =3D evict_folios(nr_batch, lruvec, sc, swappiness); if (!delta) break; =20 - scanned +=3D delta; - if (scanned >=3D nr_to_scan) - break; - if (should_abort_scan(lruvec, sc)) break; =20 + nr_to_scan -=3D delta; cond_resched(); } =20 @@ -5035,8 +5040,7 @@ static bool try_to_shrink_lruvec(struct lruvec *lruve= c, struct scan_control *sc) reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); } =20 - /* whether this lruvec should be rotated */ - return nr_to_scan < 0; + return need_rotate; } =20 static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8F5737B40B for ; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; cv=none; b=IJsYfx0WXJwyavu+lwK8efKn0CnwWifP30Ibjxe889YWP9niBmHti5Iyxp92nzDRUc3zrozuC56HXbBWRABZRUDnrlza1r4pa0sAPq3kyWzTVXzxM8hMp2ua8Wnpgg9sLVQ4LOYrSVObjuWeLfpsHqMR0JXfNK2LNy31cOL4f38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966211; c=relaxed/simple; bh=fb/z4l46p6XtOPb396GCXAdoZhV6g80kH6BiNXsa0uE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=G40awAYSG3Ui9n9ubxXGQUE2d4bZttHzGu8N/vB+bRSaoRJ6PJrMAdt4Ofsw/N8dxgqcYT/TC/I0k8kHKjXXplWu9IGtn5KbRQB2eqqGuFPhO7jVrhhO0jebkl0W9TjlSb5jUA/LGsy8041D3g25KztKSHDZArUOiZKM8KfbhqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EkyCJPzI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EkyCJPzI" Received: by smtp.kernel.org (Postfix) with ESMTPS id A5887C2BCC6; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=fb/z4l46p6XtOPb396GCXAdoZhV6g80kH6BiNXsa0uE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=EkyCJPzIi1tfOCqgUGzmPuQWmPAq5kKb9R1lZlZS96TRLdsLhxpiaBAmqMGzwGy+F pEyCG4THMs4wdXc/k8YpnQcBguPdsbz/OfAQgHF72q07ip8Q9JZ2R16V+Dm6acEu/U hArNWg7FYHuzO7d3t+0KfRE0T42yhT4qIuQdr33AdnU1n4i8dSI9jRMJtY0sc7N6kT yZUAuruXxNhA4yuxXB3F6/EckCRUGP6ZtridFVtfPHY4kS2lJDZc6R2AXOAvcZiW08 30nglUWn1E0xFqhOAjQhq4dc4FUY/AcEzTgILk6MBXfKSXTZ0CaQ5WVNGcxX2fhm2g gQQyg7i6c+osg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97C1CFDEE31; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:16 +0800 Subject: [PATCH v6 05/14] mm/mglru: scan and count the exact number of folios Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-5-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=7208; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ODZqtE2pvK8J9b/Qn9DADqSwqh8BtYgqOn3xmvuv5Ug=; b=/aNBx65vJgi4oAH2q8DW6+fyqSR0cgDQBJkhovj+OfQOqHjzrcyH2vK14I3MkjtML4NYv0uEB wemkWElke0TBxt1LAOtgwQkHLal1jWOzOM8sVTlEuYI2KQgJfbAChNR X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Make the scan helpers return the exact number of folios being scanned or isolated. Since the reclaim loop now has a natural scan budget that controls the scan progress, returning the scan number and consuming the budget makes the scan more accurate and easier to follow. The number of scanned folios for each iteration is always larger than 0, unless the reclaim must stop for a forced aging, so there is no more need for any special handling when there is no progress made: - `return isolated || !remaining ? scanned : 0` in scan_folios: both the function and the call now just return the exact scan count, combined with the scan budget introduced in the previous commit to avoid livelock or under scan. - `scanned +=3D try_to_inc_min_seq` in evict_folios: adding a bool as a scan count was kind of confusing and no longer needed to, as scan number should never be zero as long as there are still evictable gens. We may encounter a empty old gen that return 0 scan count, to avoid that, do a try_to_inc_min_seq before isolation which have slight to none overhead in most cases. - `evictable_min_seq + MIN_NR_GENS > max_seq` guard in evict_folios: the per-type get_nr_gens =3D=3D MIN_NR_GENS check in scan_folios naturally returns 0 when only two gens remain and breaks the loop. Also change try_to_inc_min_seq to return void, as its return value is no longer used by any caller. Call it before isolate_folios to flush any empty gens left by external folio freeing, and again after isolate_folios when scanning moved or protected folios may have emptied the oldest gen. The scan still stops if only two gens are left, as the scan number will be zero. This matches the previous behavior. This forced gen protection may be removed or softened later to improve reclaim further. Reviewed-by: Axel Rasmussen Reviewed-by: Chen Ridong Reviewed-by: Baolin Wang Signed-off-by: Kairui Song --- mm/vmscan.c | 58 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 757beb605980..f021dd1b84f8 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3878,10 +3878,9 @@ static bool inc_min_seq(struct lruvec *lruvec, int t= ype, int swappiness) return true; } =20 -static bool try_to_inc_min_seq(struct lruvec *lruvec, int swappiness) +static void try_to_inc_min_seq(struct lruvec *lruvec, int swappiness) { int gen, type, zone; - bool success =3D false; bool seq_inc_flag =3D false; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; DEFINE_MIN_SEQ(lruvec); @@ -3907,11 +3906,10 @@ static bool try_to_inc_min_seq(struct lruvec *lruve= c, int swappiness) =20 /* * If min_seq[type] of both anonymous and file is not increased, - * we can directly return false to avoid unnecessary checking - * overhead later. + * return here to avoid unnecessary checking overhead later. */ if (!seq_inc_flag) - return success; + return; =20 /* see the comment on lru_gen_folio */ if (swappiness && swappiness <=3D MAX_SWAPPINESS) { @@ -3929,10 +3927,7 @@ static bool try_to_inc_min_seq(struct lruvec *lruvec= , int swappiness) =20 reset_ctrl_pos(lruvec, type, true); WRITE_ONCE(lrugen->min_seq[type], min_seq[type]); - success =3D true; } - - return success; } =20 static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swap= piness) @@ -4686,7 +4681,7 @@ static bool isolate_folio(struct lruvec *lruvec, stru= ct folio *folio, struct sca =20 static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec, struct scan_control *sc, int type, int tier, - struct list_head *list) + struct list_head *list, int *isolatedp) { int i; int gen; @@ -4756,11 +4751,9 @@ static int scan_folios(unsigned long nr_to_scan, str= uct lruvec *lruvec, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); if (type =3D=3D LRU_GEN_FILE) sc->nr.file_taken +=3D isolated; - /* - * There might not be eligible folios due to reclaim_idx. Check the - * remaining to prevent livelock if it's not making progress. - */ - return isolated || !remaining ? scanned : 0; + + *isolatedp =3D isolated; + return scanned; } =20 static int get_tier_idx(struct lruvec *lruvec, int type) @@ -4804,33 +4797,36 @@ static int get_type_to_scan(struct lruvec *lruvec, = int swappiness) =20 static int isolate_folios(unsigned long nr_to_scan, struct lruvec *lruvec, struct scan_control *sc, int swappiness, - int *type_scanned, struct list_head *list) + struct list_head *list, int *isolated, + int *isolate_type, int *isolate_scanned) { int i; + int total_scanned =3D 0; int type =3D get_type_to_scan(lruvec, swappiness); =20 for_each_evictable_type(i, swappiness) { int scanned; int tier =3D get_tier_idx(lruvec, type); =20 - *type_scanned =3D type; + scanned =3D scan_folios(nr_to_scan, lruvec, sc, + type, tier, list, isolated); =20 - scanned =3D scan_folios(nr_to_scan, lruvec, sc, type, tier, list); - if (scanned) - return scanned; + total_scanned +=3D scanned; + if (*isolated) { + *isolate_type =3D type; + *isolate_scanned =3D scanned; + break; + } =20 type =3D !type; } =20 - return 0; + return total_scanned; } =20 static int evict_folios(unsigned long nr_to_scan, struct lruvec *lruvec, struct scan_control *sc, int swappiness) { - int type; - int scanned; - int reclaimed; LIST_HEAD(list); LIST_HEAD(clean); struct folio *folio; @@ -4838,19 +4834,23 @@ static int evict_folios(unsigned long nr_to_scan, s= truct lruvec *lruvec, enum node_stat_item item; struct reclaim_stat stat; struct lru_gen_mm_walk *walk; + int scanned, reclaimed; + int isolated =3D 0, type, type_scanned; bool skip_retry =3D false; - struct lru_gen_folio *lrugen =3D &lruvec->lrugen; struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); struct pglist_data *pgdat =3D lruvec_pgdat(lruvec); =20 lruvec_lock_irq(lruvec); =20 - scanned =3D isolate_folios(nr_to_scan, lruvec, sc, swappiness, &type, &li= st); + /* In case folio deletion left empty old gens, flush them */ + try_to_inc_min_seq(lruvec, swappiness); =20 - scanned +=3D try_to_inc_min_seq(lruvec, swappiness); + scanned =3D isolate_folios(nr_to_scan, lruvec, sc, swappiness, + &list, &isolated, &type, &type_scanned); =20 - if (evictable_min_seq(lrugen->min_seq, swappiness) + MIN_NR_GENS > lrugen= ->max_seq) - scanned =3D 0; + /* Scanning may have emptied the oldest gen, flush it */ + if (scanned) + try_to_inc_min_seq(lruvec, swappiness); =20 lruvec_unlock_irq(lruvec); =20 @@ -4861,7 +4861,7 @@ static int evict_folios(unsigned long nr_to_scan, str= uct lruvec *lruvec, sc->nr.unqueued_dirty +=3D stat.nr_unqueued_dirty; sc->nr_reclaimed +=3D reclaimed; trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, - scanned, reclaimed, &stat, sc->priority, + type_scanned, reclaimed, &stat, sc->priority, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); =20 list_for_each_entry_safe_reverse(folio, next, &list, lru) { --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5C94384235 for ; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=rKbXQZCbQAfDMmH/9dq+BhmWv3jwfQGZb0PaFaTZL/kb1hypxPD6ClBqlEL4ATTsCytgU0ycuHJWpAaEdeevaTyOKYjM14B3Nl9Im1zQL3G/8LEjiEZZ6qjoUTUMo2LBiRr2t0QEup1+cqGKZdGA2mZk3xptt0b2UfPa0Lyf/9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=TarTl6ss+WmakAmq/MEKfFw+4eMYx5IBwqNdGa1iTjc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Z2U0NV+6CNjjy6cdT/EdsrYP+Z383OhcfDXFNZQX9GAqWMQejsV6ZezrrfPyI8AreluKeB66gNWN4jyV4i/G5MKWprXlKIU6dZTByhhttcpwSPWKn7m0py2QNRzCBgTkWc3OucRsNr6o0/sokKNTNUkFz70tu4auV6JKqq1duCw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EYhq30Bl; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EYhq30Bl" Received: by smtp.kernel.org (Postfix) with ESMTPS id B64A2C4AF10; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=TarTl6ss+WmakAmq/MEKfFw+4eMYx5IBwqNdGa1iTjc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=EYhq30Blaf4wXhaFic3nUgXcsilVKDLb0N8Eo22DJMfiiXsct9heT0zSkf+DmnF05 xtZfnbt3aKwkWzqzb/oi058//w5HE6tlUH3K1bkOuze9Y6lOeyva1TeadoGE2zpbyI kQ1pvUwWri6lrRLBENdnLNqA1l0R1ZFTjX+j7k016oevwlEpFZTF8baa1usY4tZmM/ B6broJ972nljKwA8Bg5dum7mpG/nCY7EizaxofAhb/KVwnKhEp1kLLJcobtPth0+4m qXoP5D/GM9ZsYR4jPlNGEeH/2S8RVpuw1154Q5ENX6EswVQqXx3mXe+XF7BUsq+Rms RmNu87e6wJ/dA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABFB9FDEE38; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:17 +0800 Subject: [PATCH v6 06/14] mm/mglru: use a smaller batch for reclaim Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-6-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=1039; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=prfg9m1Tu1GJeQ+RnyeWNB/GqbsjUBEqoBx+SgrJxbQ=; b=B0GUvgdLw6/QZUwnQrWQy2s99KbNhkSnfjCf1YfCPiMj/KomlU67xWV6/QY/zn21EX/wIU9Ri VkWjytzHo3yAfpYlKd2Rl6bn07KLo3k1PfW76Vw88jHwqKrwlJuD/6I X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song With a fixed number to reclaim calculated at the beginning, making each following step smaller should reduce the lock contention and avoid over-aggressive reclaim of folios, as it will abort earlier when the number of folios to be reclaimed is reached. Reviewed-by: Axel Rasmussen Reviewed-by: Chen Ridong Reviewed-by: Baolin Wang Reviewed-by: Barry Song Signed-off-by: Kairui Song --- mm/vmscan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index f021dd1b84f8..f6ee7ccf4e81 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -5006,7 +5006,7 @@ static bool try_to_shrink_lruvec(struct lruvec *lruve= c, struct scan_control *sc) break; } =20 - nr_batch =3D min(nr_to_scan, MAX_LRU_BATCH); + nr_batch =3D min(nr_to_scan, MIN_LRU_BATCH); delta =3D evict_folios(nr_batch, lruvec, sc, swappiness); if (!delta) break; --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 014C43A785F for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=Pr24GxKI3JPSSngO9++MuNPojBv9kLyhPd81hYcTpEQW0ny0bMoeuauqa/Sq6kK/c31+nV7SU9Z6c08l0Uu9QG7FuRodHEc7Oc8s/V0KsHl7/kflLzNhNzUZ64KnArAA9Dh949Ic0lwc3igzILq0OQYJpcSbGo6olrgogCl9xGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=Kzbku/F4IhWLVRQA4XKEmpWT4XsvM1A8oBihbauYwAM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VixJQd3QniZK2w51gdQZdUxXjYhQ1zKMv17BJCnAQjcTDu0S32esH5p56a7MT7P/MEQVZs92BPK/QLylaexMgpbavYGLqmfpboqGNqCNBGc2dhAoUxugzWM3tTa12Zf+skQssti7Dj8SjkGM1e5e/ubNqmZoQYTUBvtnyhiOTeo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ov2EWquW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ov2EWquW" Received: by smtp.kernel.org (Postfix) with ESMTPS id C6D91C2BCAF; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=Kzbku/F4IhWLVRQA4XKEmpWT4XsvM1A8oBihbauYwAM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Ov2EWquW8bvpBFhrddByuGy14AeV8xBoIgdlN5R6lssrcxmIKtUu8Zd/i3MO3q0wY VRgfpE1bzVSQYB1Hks37zJnIW7fp3jDAC05QmnyOgQNmUeYZ/u9c7FfUvFuhglmT2+ QZKoX4m4LQb24Rzrf6guK8H/VwXPszu9hrQa4ZP8sE9mIxEh/V+BeF5MzQpzBgqBmX jgGkJm6aXaZnh8uf3VXVaVAaWJGcE7euiF6KpmNnBpDbqaQAi9dhgDokO+H1GssULu pO5fhEiPffTZ3yeq5DHslLQH7iB+xHcUcCA6H6jgW5RoDX98WhSTzMiePrSBZ6qB2t 4r8nVAXVXa5PQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBE9CFDEE39; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:18 +0800 Subject: [PATCH v6 07/14] mm/mglru: don't abort scan immediately right after aging Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-7-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=3735; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=cyzkCao3l6tObidLw0rB9YIwMV9MllEksfGhK4Us/14=; b=0MXMmuZsZb1934LVkoZkqTiJqAyaKJhn4iq3J+AU0zWoPtHsgd+IWLlubb/4xiaYV+97NvQP0 1e5BCs8PQcfDY8nxg9cC3PBEtPpWyMDipNHror9tUNf2Qd82b52WQ5A X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Right now, if eviction triggers aging, the reclaimer will abort. This is not the optimal strategy for several reasons. Aborting the reclaim early wastes a reclaim cycle when under pressure, and for concurrent reclaim, if the LRU is under aging, all concurrent reclaimers might fail. And if the age has just finished, new cold folios exposed by the aging are not reclaimed until the next reclaim iteration. What's more, the current aging trigger is quite lenient, having 3 gens with a reclaim priority lower than default will trigger aging, and blocks reclaiming from one memcg. This wastes reclaim retry cycles easily. And in the worst case, if the reclaim is making slower progress and all following attempts fail due to being blocked by aging, it triggers unexpected early OOM. And if a lruvec requires aging, it doesn't mean it's hot. Instead, the lruvec could be idle for quite a while, and hence it might contain lots of cold folios to be reclaimed. While it's helpful to rotate memcg LRU after aging for global reclaim, as global reclaim fairness is coupled with the rotation in shrink_many, memcg fairness is instead handled by cgroup iteration in shrink_node_memcgs. So, for memcg level pressure, this abort is not the key part for keeping the fairness. And in most cases, there is no need to age, and fairness must be achieved by upper-level reclaim control. So instead, just keep the scanning going unless one whole batch of folios failed to be isolated or enough folios have been scanned, which is triggered by evict_folios returning 0. And only abort for global reclaim after one batch, so when there are fewer memcgs, progress is still made, and the fairness mechanism described above still works fine. And in most cases, the one more batch attempt for global reclaim might just be enough to satisfy what the reclaimer needs, hence improving global reclaim performance by reducing reclaim retry cycles. Rotation is still there after the reclaim is done, which still follows the comment in mmzone.h. And fairness still looking good. Reviewed-by: Axel Rasmussen Reviewed-by: Chen Ridong Reviewed-by: Barry Song Signed-off-by: Kairui Song --- mm/vmscan.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index f6ee7ccf4e81..084c6ea8910c 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4984,7 +4984,7 @@ static bool should_abort_scan(struct lruvec *lruvec, = struct scan_control *sc) */ static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_contro= l *sc) { - bool need_rotate =3D false; + bool need_rotate =3D false, should_age =3D false; long nr_batch, nr_to_scan; int swappiness =3D get_swappiness(lruvec, sc); struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); @@ -5002,8 +5002,7 @@ static bool try_to_shrink_lruvec(struct lruvec *lruve= c, struct scan_control *sc) if (should_run_aging(lruvec, max_seq, sc, swappiness)) { if (try_to_inc_max_seq(lruvec, max_seq, swappiness, false)) need_rotate =3D true; - /* stop scanning as it's low on cold folios */ - break; + should_age =3D true; } =20 nr_batch =3D min(nr_to_scan, MIN_LRU_BATCH); @@ -5014,6 +5013,13 @@ static bool try_to_shrink_lruvec(struct lruvec *lruv= ec, struct scan_control *sc) if (should_abort_scan(lruvec, sc)) break; =20 + /* + * Root reclaim needs rotation when low on cold folio for better + * fairness. Cgroup reclaim gets fairness from the iterator. + */ + if (root_reclaim(sc) && should_age) + break; + nr_to_scan -=3D delta; cond_resched(); } --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 065673A7F72 for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=UF3cDq6rXGMRAPd574SyTpvuz+PH558kZ2TY0/5j2fculSHzWOIcHdoJUKDemHD3S+1xzRj+xlqHiDo8uKvqNKfszrylw/LdPCWIYL/82zxWTqw7q2Ogoj6wOnDQoJGhSexAikmFT8x95wnm/Fx4xYbAYUYnV4QRjM3Q1z/Sy0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=UfFnZDwx0HT6FyicEDwiV3SBywyLcuEJDUUBu5liQ2I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sADlQDBq7e5x1Ye1gGlcZlWzkj421/Md1XfMQfO3JdOBIwBFhsdXvECCP0fJNQcxyVPhNYTLWD8ma6Szk+SpD8oSswRQie3OiOIo+8nWFbxzDUFXpqC0lLv8Zb+mgGhY70+LjAtEzLXfFYuoxKXElPwmVGIapv6CLMxPOnU0S70= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZcGSnu1K; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZcGSnu1K" Received: by smtp.kernel.org (Postfix) with ESMTPS id D86A9C2BCF7; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966211; bh=UfFnZDwx0HT6FyicEDwiV3SBywyLcuEJDUUBu5liQ2I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=ZcGSnu1KEwo9flVZ9fCXs052WEMjFRsvT/5B7icAkPUoNVgwFb+fepUH6IHG883yZ dTtY1gpZA20o4cIdbN3KpFWbhwSvFpaAULOfTJvTH4HtBdbk7nxuFC9B5J4kl+oN08 D/jzQ5HfHjo5DWE0/IJMcweAINEyLClRvBEEo1+RtLmFO/X+8/rNYi8ytgDNSV9E0z y+JdvTer1jVeUEGFN4MMG+KxazpMN/DsAbqh/y6fPeePKwu8RFqE/I+GmOYCdF7DMv wMCrp2iEJr+ntkorY+Ar4s/PA2Fvs/+N3fwPn68Ac/ERDi6j08d7P30iQtgGTanTWw /Y2Wo3+2cn/ww== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id CCACDFDEE35; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:19 +0800 Subject: [PATCH v6 08/14] mm/mglru: remove redundant swap constrained check upon isolation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-8-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=1860; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=H32ZZapu03Wi2OjXgn3aDMeoOd5F0TNj/288sMNJO1w=; b=MzmrT40vt0TkDPM/Ul2yLwYIcHAQ2t681GbOMYx98ZYwLoXXD9v+qCfrk0+ZSLglzQH3UlMcI QB7/oot0OF8AigWrOTEEPNWs0NPKEsbBj4z5R2NO/gW/oFCCuZqoZp5 X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Remove the swap-constrained early reject check upon isolation. This check is a micro optimization when swap IO is not allowed, so folios are rejected early. But it is redundant and overly broad since shrink_folio_list() already handles all these cases with proper granularity. Notably, this check wrongly rejected lazyfree folios, and it doesn't cover all rejection cases. shrink_folio_list() uses may_enter_fs(), which distinguishes non-SWP_FS_OPS devices from filesystem-backed swap and does all the checks after folio is locked, so flags like swap cache are stable. This check also covers dirty file folios, which are not a problem now since sort_folio() already bumps dirty file folios to the next generation, but causes trouble for unifying dirty folio writeback handling. And there should be no performance impact from removing it. We may have lost a micro optimization, but unblocked lazyfree reclaim for NOIO contexts, which is not a common case in the first place. Reviewed-by: Axel Rasmussen Reviewed-by: Baolin Wang Reviewed-by: Chen Ridong Reviewed-by: Barry Song Signed-off-by: Kairui Song --- mm/vmscan.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 084c6ea8910c..35e3352a53e3 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4650,12 +4650,6 @@ static bool isolate_folio(struct lruvec *lruvec, str= uct folio *folio, struct sca { bool success; =20 - /* swap constrained */ - if (!(sc->gfp_mask & __GFP_IO) && - (folio_test_dirty(folio) || - (folio_test_anon(folio) && !folio_test_swapcache(folio)))) - return false; - /* raced with release_pages() */ if (!folio_try_get(folio)) return false; --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CFEA3A8732 for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=ZhXai6M+wxVkcrHJwNWF+yoYK9gvJRQ6f6JhCA2SZSe8iKOwFC6qA1e9xzEdwxijZ2GBmECs8mFuVCQhC2Gi40JJP88jc6CPcQjw5SGbEzeWJPEbOs/UlMoC0aB79GOmQGQ4B6Qiocl7iEu1Egrsre+G3gpnNGx9J6yU5dzrVQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=dGjQUlbv1Cn058kOoUOjP+FNqLWNmkWZScyLP4htwAA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tMbKMLULgfItfre478w6N30R728Fqzy35ZQYsyZ58o8UYD5Ob4+uNNc0Ok7DLA5630w8Vbam/RCCrC3sFc90kDTlbjn300V1mIZJCPzVAX9RZ4dF7DWqRKGk02sAXcUv/Tz4IzcRBQVdBuQ1lFV0+85Erxrs8+9RenlIQUU+eRo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eUOO4XIh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="eUOO4XIh" Received: by smtp.kernel.org (Postfix) with ESMTPS id E9896C2BCC4; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966212; bh=dGjQUlbv1Cn058kOoUOjP+FNqLWNmkWZScyLP4htwAA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=eUOO4XIhJW9VvmZVdnTAuOqW2+lBOfkhqHckOacrzons8lqlViUD56KhGAXt4W/sv zHjyjYD36U3liOMlavRvPtKuekNxW4/u5HAVhWzN/dlWL8l5BPxQ1K3UZMTresQQ+N 7XlcETn92AfyCV75hi6rsSzarY5h2OQ8WTXbsDlZZ3F7hJdR6x/LV0R6X3vpzsWuMv xHmL0Bfamom4wMFsGTMmD8McFaVMs40rK328sL11fvJ6Xi8tbvwu84PpIv2Cqo7AV6 NzxSf+goMGvcsDfx6xZI1YzXriG6sFuXG4t9BJmXHwxxozVdmAxsL59VWvBY8QK3s3 xy0OtkeH3nVoA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id DDA93FDEE33; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:20 +0800 Subject: [PATCH v6 09/14] mm/mglru: use the common routine for dirty/writeback reactivation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-9-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=3248; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=7k2CuT+9M2FqGXa3ZySKsRX9m55VQUz1CgD9CNT+STo=; b=e/0nGIlQGEkN6o/HRZO9n+5h03rf9bHKDu79HVK6nCYp4pGnYmPaCq6k/aQjhVK8Aw5NPQkMJ zSzZha2E9UhDciN1F3iY4n+z7KodZliU0miGrxSPt64XWXCf/TcS1U8 X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Currently MGLRU will move the dirty writeback folios to the second oldest gen instead of reactivate them like the classical LRU. This might help to reduce the LRU contention as it skipped the isolation. But as a result we will see these folios at the LRU tail more frequently leading to inefficient reclaim. Besides, the dirty / writeback check after isolation in shrink_folio_list is more accurate and covers more cases. So instead, just drop the special handling for dirty writeback, use the common routine and re-activate it like the classical LRU. This should in theory improve the scan efficiency. These folios will be rotated back to LRU tail once writeback is done so there is no risk of hotness inversion. And now each reclaim loop will have a higher success rate. This also prepares for unifying the writeback and throttling mechanism with classical LRU, we keep these folios far from tail so detecting the tail batch will have a similar pattern with classical LRU. The micro optimization that avoids LRU contention by skipping the isolation is gone, which should be fine. Compared to IO and writeback cost, the isolation overhead is trivial. And using the common routine also keeps the folio's referenced bits (tier bits), which could improve metrics in the long term. Also no more need to clean reclaim bit as the common routine will make use of it. Note the common routine updates a few throttling and writeback counters, which are not used, and never have been for the MGLRU case. We will start making use of these in later commits. Reviewed-by: Axel Rasmussen Reviewed-by: Barry Song Reviewed-by: Baolin Wang Signed-off-by: Kairui Song --- mm/vmscan.c | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 35e3352a53e3..74255efc4ad9 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4578,7 +4578,6 @@ static bool sort_folio(struct lruvec *lruvec, struct = folio *folio, struct scan_c int tier_idx) { bool success; - bool dirty, writeback; int gen =3D folio_lru_gen(folio); int type =3D folio_is_file_lru(folio); int zone =3D folio_zonenum(folio); @@ -4628,21 +4627,6 @@ static bool sort_folio(struct lruvec *lruvec, struct= folio *folio, struct scan_c return true; } =20 - dirty =3D folio_test_dirty(folio); - writeback =3D folio_test_writeback(folio); - if (type =3D=3D LRU_GEN_FILE && dirty) { - sc->nr.file_taken +=3D delta; - if (!writeback) - sc->nr.unqueued_dirty +=3D delta; - } - - /* waiting for writeback */ - if (writeback || (type =3D=3D LRU_GEN_FILE && dirty)) { - gen =3D folio_inc_gen(lruvec, folio, true); - list_move(&folio->lru, &lrugen->folios[gen][type][zone]); - return true; - } - return false; } =20 @@ -4664,9 +4648,6 @@ static bool isolate_folio(struct lruvec *lruvec, stru= ct folio *folio, struct sca if (!folio_test_referenced(folio)) set_mask_bits(&folio->flags.f, LRU_REFS_MASK, 0); =20 - /* for shrink_folio_list() */ - folio_clear_reclaim(folio); - success =3D lru_gen_del_folio(lruvec, folio, true); VM_WARN_ON_ONCE_FOLIO(!success, folio); =20 --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DB4F3A875E for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=k/Xzqn5Yzk1k5F6shdhfT6eEZfav6+Fa1e23OpmsV/TEy8FUHnCrTUIZhi7i7Bn9DqA6twmVgwVQgH7JfIJp0e2tuUY//9QlRyQGlg+5+e/hzZPxNkavFzC02+y1fFmNvz03gSRv0WX/bUajXInpaeIsip9zEjqJjMjHPD9JzQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=otkLcnfNmla34NBzMgC0+6D9v1VkExJNUBcQ2Wftj58=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ajJkBzXscBEX3prz0XLMappB0YxjRbR1rAC5cDHnPyzAebZwyl0jSatG3UpBTTtUj9aGiDcITw88ysBlb14s7y8i/Gi4kDM/lO5i1hmAkI6qbdVQcotiX6lQKyk0Ofc0nH/1lX/15ST8Iaxs72Hd+HoIf+VWPkNmsto02fjLtGM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RGI/6S9m; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RGI/6S9m" Received: by smtp.kernel.org (Postfix) with ESMTPS id 05BF2C2BCC6; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966212; bh=otkLcnfNmla34NBzMgC0+6D9v1VkExJNUBcQ2Wftj58=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=RGI/6S9mSpIxMbkiCagH72OXlQJssfeH1wf+sRGWyR0fxeZixTtjkvy3uCuebcu5W HqxJ6JDaKbMn/xlyv3zI7oZn0HzILtisQsI3rhqgnUyalIJJgfTy3d+HzM0YNbDolM LUzG4EoB6ZEvVddaMZwMxJAgokOhT0ca3fdXQ4KxRDDIJ0ATYQCcNESuanEkqRSizu jOm8yYXVOAGke89AXqcML8CuGzbvjmlWD6mXPddfdHfFdtffMUjyEOTA135AygKW38 QR3t9iQqesA2WfdehWTxoy+9NZIG/M6RNgxbSPyNxGK9ZUzQB+Hne50TiE5GIl+0Og XeO7SUYdN2ROw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF7DCFDEE3B; Thu, 23 Apr 2026 17:43:31 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:21 +0800 Subject: [PATCH v6 10/14] mm/mglru: simplify and improve dirty writeback handling Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-10-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=4411; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=XPufkC5dGXzX8uV7Cj2QhJr/PISwcv22HaMg1KGzKUs=; b=83TwcM7l6VCtXPRMNnDwwLNV5hfrHNISGsC8sWWxi+9iy1qcXg9Q9qRnWrBQNQa7TKdb26nlN fKGr8fkJKhcBakUzQrLDsQHdXCIqeYk2EY2lRLhlLvYdoskYdlVmxAH X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Right now the flusher wakeup mechanism for MGLRU is less responsive and unlikely to trigger compared to classical LRU. The classical LRU wakes the flusher if one batch of folios passed to shrink_folio_list is unevictable due to under writeback. MGLRU instead check and handle this after the whole reclaim loop is done. We previously even saw OOM problems due to passive flusher, which were fixed but still not perfect [1]. We have just unified the dirty folio counting and activation routine, now just move the dirty flush into the loop right after shrink_folio_list. This improves the performance a lot for workloads involving heavy writeback and prepares for throttling too. Test with YCSB workloadb showed a major performance improvement: Before this series: Throughput(ops/sec): 62485.02962831822 AverageLatency(us): 500.9746963330107 pgpgin 159347462 workingset_refault_file 34522071 After this commit: Throughput(ops/sec): 80857.08510208207 AverageLatency(us): 386.653262968934 pgpgin 112233121 workingset_refault_file 19516246 The performance is a lot better with significantly lower refault. We also observed similar or higher performance gain for other real-world workloads. We were concerned that the dirty flush could cause more wear for SSD: that should not be the problem here, since the wakeup condition is when the dirty folios have been pushed to the tail of LRU, which indicates that memory pressure is so high that writeback is blocking the workload already. Reviewed-by: Axel Rasmussen Link: https://lore.kernel.org/linux-mm/20241026115714.1437435-1-jingxiangze= ng.cas@gmail.com/ [1] Reviewed-by: Baolin Wang Signed-off-by: Kairui Song --- mm/vmscan.c | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 74255efc4ad9..d7a72a60c894 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4724,8 +4724,6 @@ static int scan_folios(unsigned long nr_to_scan, stru= ct lruvec *lruvec, trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, scanned, skipped, isolated, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); - if (type =3D=3D LRU_GEN_FILE) - sc->nr.file_taken +=3D isolated; =20 *isolatedp =3D isolated; return scanned; @@ -4833,12 +4831,27 @@ static int evict_folios(unsigned long nr_to_scan, s= truct lruvec *lruvec, return scanned; retry: reclaimed =3D shrink_folio_list(&list, pgdat, sc, &stat, false, memcg); - sc->nr.unqueued_dirty +=3D stat.nr_unqueued_dirty; sc->nr_reclaimed +=3D reclaimed; trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, type_scanned, reclaimed, &stat, sc->priority, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); =20 + /* + * If too many file cache in the coldest generation can't be evicted + * due to being dirty, wake up the flusher. + */ + if (stat.nr_unqueued_dirty =3D=3D isolated) { + wakeup_flusher_threads(WB_REASON_VMSCAN); + + /* + * For cgroupv1 dirty throttling is achieved by waking up + * the kernel flusher here and later waiting on folios + * which are in writeback to finish (see shrink_folio_list()). + */ + if (!writeback_throttling_sane(sc)) + reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); + } + list_for_each_entry_safe_reverse(folio, next, &list, lru) { DEFINE_MIN_SEQ(lruvec); =20 @@ -4999,28 +5012,6 @@ static bool try_to_shrink_lruvec(struct lruvec *lruv= ec, struct scan_control *sc) cond_resched(); } =20 - /* - * If too many file cache in the coldest generation can't be evicted - * due to being dirty, wake up the flusher. - */ - if (sc->nr.unqueued_dirty && sc->nr.unqueued_dirty =3D=3D sc->nr.file_tak= en) { - struct pglist_data *pgdat =3D lruvec_pgdat(lruvec); - - wakeup_flusher_threads(WB_REASON_VMSCAN); - - /* - * For cgroupv1 dirty throttling is achieved by waking up - * the kernel flusher here and later waiting on folios - * which are in writeback to finish (see shrink_folio_list()). - * - * Flusher may not be able to issue writeback quickly - * enough for cgroupv1 writeback throttling to work - * on a large system. - */ - if (!writeback_throttling_sane(sc)) - reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); - } - return need_rotate; } =20 --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39FD13A9017 for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=PWI9pxeggkhN75lOknJdfBUkY9BgTF+k0YRlItLsvQ3UPhvgxzASZw1b9OcfMeduoGGq9OdzEtRudtqleaEaUtSTlA2OxJP1Dnyw6dUEdwE6K24jyo5qfP+Dm2hl15GAZVODIL7albRkvysDak5B2583A1NWyXAwgJatOLfIYaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=Mz130fZwa+XHZtJ3YLUouCVz539YtPTQz+WXvFbHei0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KMxWQoFEU/21CFQxt7Gz0p5zbx9U4TMFYN2vQXXnCslR3rCI2FG0c2wk/e5kOGaG6XhVQtpb2DI81kLggalXrA7MLpYndTR0qXkaQct645ZcuVkOjFYqrG9bt1cSi5RCoK/tcSTzMCIEezfLqScyRSxOMKFne3ZhmHpZcLBmeaI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qraQcFa8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qraQcFa8" Received: by smtp.kernel.org (Postfix) with ESMTPS id 153C8C2BCB2; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966212; bh=Mz130fZwa+XHZtJ3YLUouCVz539YtPTQz+WXvFbHei0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=qraQcFa8vX7Na1JOdwMzOnnNYh7b50AcGrGBN1JvKx2os/HWt/PQHFQqSiuvRsHQ5 oal4lTsXS8KDvjPeeKaRd73bKcPDGfbUgtygoznBrHcy21wkEdKTBqr9MJPBAUlSa8 vlQQArofU9PB7+KBnzt8n4K2DiOgIjywocOIy45PcW3GcGiT2yaNHbEY6h/Go9G3kv 1JO/mUOn+kvBTfZ/hmPUsaHmu4c8ThCAcmnIrHVOQs+0c1ZG0qHP0v0yAqrX2DPhir S06fpvC6rbQge5JFm/H3DTn+rQyf8R30wOLB1b1QzO/p4KXNLdUD7U4xNIKMdfvjR5 9DJ6z4XTeI+sg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0AF89FDEE38; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:22 +0800 Subject: [PATCH v6 11/14] mm/mglru: remove no longer used reclaim argument for folio protection Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-11-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=2628; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ggpH/FNtPk2W1Wsd+xXOs+YPMpOI+Pw1GBWurcJoCJU=; b=vdiX8JIyABAYNQjG/5UYcBYje01VvS0VC1W76VRjdR8wjC32ug0Ukt+ddV7Yb+zh3uiFTTcG3 N3/rubUYBm6Br8mWzz+F/dGn5Tk/NNOVjAkz1ZyI9A8lCmWyLcV78Nj X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Now dirty reclaim folios are handled after isolation, not before, since dirty reactivation must take the folio off LRU first, and that helps to unify the dirty handling logic. So this argument is no longer needed. Just remove it. Reviewed-by: Axel Rasmussen Signed-off-by: Kairui Song --- mm/vmscan.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index d7a72a60c894..f6cb1a4b6a31 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3220,7 +3220,7 @@ static int folio_update_gen(struct folio *folio, int = gen) } =20 /* protect pages accessed multiple times through file descriptors */ -static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, bool = reclaiming) +static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio) { int type =3D folio_is_file_lru(folio); struct lru_gen_folio *lrugen =3D &lruvec->lrugen; @@ -3239,9 +3239,6 @@ static int folio_inc_gen(struct lruvec *lruvec, struc= t folio *folio, bool reclai =20 new_flags =3D old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS); new_flags |=3D (new_gen + 1UL) << LRU_GEN_PGOFF; - /* for folio_end_writeback() */ - if (reclaiming) - new_flags |=3D BIT(PG_reclaim); } while (!try_cmpxchg(&folio->flags.f, &old_flags, new_flags)); =20 lru_gen_update_size(lruvec, folio, old_gen, new_gen); @@ -3855,7 +3852,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int ty= pe, int swappiness) VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) !=3D type, folio); VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) !=3D zone, folio); =20 - new_gen =3D folio_inc_gen(lruvec, folio, false); + new_gen =3D folio_inc_gen(lruvec, folio); list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); =20 /* don't count the workingset being lazily promoted */ @@ -4607,7 +4604,7 @@ static bool sort_folio(struct lruvec *lruvec, struct = folio *folio, struct scan_c =20 /* protected */ if (tier > tier_idx || refs + workingset =3D=3D BIT(LRU_REFS_WIDTH) + 1) { - gen =3D folio_inc_gen(lruvec, folio, false); + gen =3D folio_inc_gen(lruvec, folio); list_move(&folio->lru, &lrugen->folios[gen][type][zone]); =20 /* don't count the workingset being lazily promoted */ @@ -4622,7 +4619,7 @@ static bool sort_folio(struct lruvec *lruvec, struct = folio *folio, struct scan_c =20 /* ineligible */ if (zone > sc->reclaim_idx) { - gen =3D folio_inc_gen(lruvec, folio, false); + gen =3D folio_inc_gen(lruvec, folio); list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); return true; } --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 469CC3A960F for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=sbx+DTImAh0ZTwVEujT1AfEHaupxgcUwoo/ra2/YF/2AV9lruIK1hwWI6mVOz6OWeoHiTdo6lCJo6q1cFRFm0so8eFmz1g9xD0DxPNVGikybR4umcnDLEfS6/5gOK+M8CkCOfgAPdCDM42Qz2LZww/1J6hjOK/NX7JwJQq9JtUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=Pi8alNHf2oX9gUTgyBWhZOdVukmwFFf1pCLzb3ZBy+Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ui55Uc2UQP8DhHYItnyiNpZvyQN6xDr4DVLBHJFtRMAjZW3CxTH5qUGDEaPVa2/gpJc1EF4wMOzVHS5o7W6+4CspUOQwUJvxow5BdZtytS0m0Cky6qPUrTLASq/5agivEutcR73LQhSFPt2dg9A+WUjnJ4pwrLxhnzzmD8fnn1E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=blwlRd5B; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="blwlRd5B" Received: by smtp.kernel.org (Postfix) with ESMTPS id 2644BC2BCB7; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966212; bh=Pi8alNHf2oX9gUTgyBWhZOdVukmwFFf1pCLzb3ZBy+Q=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=blwlRd5ByOUWM7dWrD26xsO59924KoDfg+NdyyzCwW6CLBiqL20pkeIibLeA9w4mw BbrJEhs/3bsOsJwcMOI9wfo5Qm1QrMFsOucJhyFmwA06/489rr+n40IvwssF3lksQi fYG8SWNtZ5sRb3NZ9a3HrdnZUtK/o8kPBIHz28fZJ4RFK8Td8zWugPBUzPRV9oPgk4 3m5rgN7Ygez0H52XBeT+3g7zhvV8PLrJRPOOyRpYevNavemtSET+3e7slJGFN8/dwm yGfjtrn87DSf09pv7V0hxlrisFxCXhhWBlK9C67NAeWW9P0z096sQWR7d/ZuDt1wYG g+bk3C6Pd6Rfw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AE54FDEE39; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:23 +0800 Subject: [PATCH v6 12/14] mm/vmscan: remove sc->file_taken Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-12-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=1018; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=hbjcSghvUpKJ+Cq31Huf7ozCEGn1Zf5iRINfj/MTnU4=; b=9Hs8tFOS36fafoVOzNH2AmgLcP26/rJlYpL3c+JrWMbVYfCT9/cI2Qno4ktn6aBAU5LO1x+jb UGIl2T7iv9UD0BDWwjOYZfuWOcY58EynFCKnUzbsEL02TQ99LaK7DZE X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song No one is using it now, just remove it. Reviewed-by: Axel Rasmussen Reviewed-by: Baolin Wang Reviewed-by: Chen Ridong Signed-off-by: Kairui Song --- mm/vmscan.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index f6cb1a4b6a31..7bec0ae51465 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -173,7 +173,6 @@ struct scan_control { unsigned int congested; unsigned int writeback; unsigned int immediate; - unsigned int file_taken; unsigned int taken; } nr; =20 @@ -2040,8 +2039,6 @@ static unsigned long shrink_inactive_list(unsigned lo= ng nr_to_scan, sc->nr.writeback +=3D stat.nr_writeback; sc->nr.immediate +=3D stat.nr_immediate; sc->nr.taken +=3D nr_taken; - if (file) - sc->nr.file_taken +=3D nr_taken; =20 trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, nr_scanned, nr_reclaimed, &stat, sc->priority, file); --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57D0A3A9620 for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=Z4SiZl047gGlOee/4CkFGi1lfgxx/6e0AumYHWzCW9o3ze7QHMkxsnxg9eYDu5C94X+nrYYMADWOClWIaV0SLotIiMIA+DbNSZSHa3v58GEA7JE0XPneOwVb4/CBiCaXcWj79zRCyjW7cdi/wRrlVdRWs+k7z4uLZbxxa3YJUZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=84Sdzb0xN+DMxvZserLaTWqJv8AqidSImQfw43Dn4S8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QKbMmJ43hsoPnAzc+rXnkJIlxPQ9utoCW8L5OpvDcpKpy22jvS+ifvKauwKPgQmJqSaUa3KZs5ofILibAHnVAaS8Knr33GO76HPOZOUekRZyLqZEu3Fiu5Um6THOlihC6qvOgiWPwVZCEQgodG6MLIerOZzwtD6ifUlAY+AS1lM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IbnrwSSz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IbnrwSSz" Received: by smtp.kernel.org (Postfix) with ESMTPS id 358EFC2BCB8; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966212; bh=84Sdzb0xN+DMxvZserLaTWqJv8AqidSImQfw43Dn4S8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=IbnrwSSzqdzeVjwidFVG0VAA/UBgxVMO98gOI3feeAMSIo9r9LH8lfqhMmE1F4rH4 aKCqS1C/y5POe364fSq6vFicAuu94j9AV2DDXO+A0l/YuJghXiAEZQynW9NI6H5DNr SeyFq1RLmFJLwFoS3URF6vytSzkWrnx6uQz3f9ZVoHt7RQafIxmTZY7OzJvoIfn8Rn 1th9ka8tMArrP16PxR7XZOKjYr5gYsEVenGdtzyCYvGJePzenz5pB7BIspEK7G9Lzo zUYRD0+OI+Fj0CYFdKCBeIvxR47tjlZJLm2yJo5dpVj6W+KDIOMP8Kg06/DoWPw56E hPxh50wTCvyYg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2DC79FDEE35; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:24 +0800 Subject: [PATCH v6 13/14] mm/vmscan: remove sc->unqueued_dirty Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-13-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=1092; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=BcORidMLEmQouegYnHcnzJgwCLFGF5hvusRRBleG7A4=; b=jK5uHZZUVzeN0NAKTODLaABCBqMJKZvZMubU/ltTUylOFyl+ZQ+lSpeo1aKQ9OOUQcBVjTDM8 sqJIiKuJZBgCgIMWLK/Ppy6q83QKXlxGiq9gk73Lt0d8XKZ0W8Q61tk X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song No one is using it now, just remove it. Suggested-by: Axel Rasmussen Reviewed-by: Baolin Wang Reviewed-by: Axel Rasmussen Reviewed-by: Barry Song Reviewed-by: Chen Ridong Signed-off-by: Kairui Song --- mm/vmscan.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 7bec0ae51465..6df5ab625e6a 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -169,7 +169,6 @@ struct scan_control { =20 struct { unsigned int dirty; - unsigned int unqueued_dirty; unsigned int congested; unsigned int writeback; unsigned int immediate; @@ -2035,7 +2034,6 @@ static unsigned long shrink_inactive_list(unsigned lo= ng nr_to_scan, =20 sc->nr.dirty +=3D stat.nr_dirty; sc->nr.congested +=3D stat.nr_congested; - sc->nr.unqueued_dirty +=3D stat.nr_unqueued_dirty; sc->nr.writeback +=3D stat.nr_writeback; sc->nr.immediate +=3D stat.nr_immediate; sc->nr.taken +=3D nr_taken; --=20 2.54.0 From nobody Wed Jun 17 07:36:46 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70D093A962C for ; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; cv=none; b=ggtFESKcYljYbfZIRC0cR5lK07ofKG81HmxXokcuJCj5txUG7XoqZE63qvJyEqPwEDMWfbLngzR/xaJs8qhmhZcj7dYatSnszD30D0M1g/4O1q0xTmq6FC6+PVwGkP5uOirnS/JFcjUWyJimAYPifoPdKhjKNIYe13lqsdm1AZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776966212; c=relaxed/simple; bh=0QjluZuPaKHtRS38Z9q2BcOQuLoRsXERS1LKXhZU1G8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oG+hIopEuWZwSHWzyEgAPpT2SWxyfBElGjR9PY8CfCNFiMHC4fxGILC9ASVtlW5MiqqP/t1UWv6fZBPjjZGdEBesDu40wqTsUmzKkTT8KHkbRL2UDnTv0+Wtcu8wjGlMX51x+hyuDKxVsD1DMi+J4w77qQw7Nxby2vS/W73gbqE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VIVFQjqz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VIVFQjqz" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4BBDFC2BCAF; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776966212; bh=0QjluZuPaKHtRS38Z9q2BcOQuLoRsXERS1LKXhZU1G8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=VIVFQjqz12sDQupe856JzKcx/OfZP2z+gTZzPAoILc3ab8XlO70bTc2FSRyQ/Nu6Z 4BuJZOTFCeQOT4Sykh633EuUatu8xhHFrxDnzNxfJzZuvWRo6XCphas4XInQShGfo0 7Y269MX7gw3D9omwu6AAEXzL6VFnVje2V+u4eSmfOjal4ZmjCLk5D9N0+Ck29WvKAO S2PElj8fulNqpIwu0Yewbw1kQXQamyUtPdzyErZTv69mGlraJukD9RXhnQe20sGoah 6Gc4nlUAMCa/7hgZyZ2l+09w8U2mIMSf1M4oSoaMZ3haj5lPt6uOXFA/NFj3XD7Fit 6AV8gk9jKlAPQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FC2EFDEE3E; Thu, 23 Apr 2026 17:43:32 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 24 Apr 2026 01:43:25 +0800 Subject: [PATCH v6 14/14] mm/vmscan: unify writeback reclaim statistic and throttling Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260424-mglru-reclaim-v6-14-a57622d770c3@tencent.com> References: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> In-Reply-To: <20260424-mglru-reclaim-v6-0-a57622d770c3@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , David Hildenbrand , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Barry Song , David Stevens , Chen Ridong , Leno Hou , Yafang Shao , Yu Zhao , Zicheng Wang , Kalesh Singh , Suren Baghdasaryan , Chris Li , Vernon Yang , linux-kernel@vger.kernel.org, Qi Zheng , Baolin Wang , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1776966208; l=6820; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=HoC8teVsXycmvNADkf3jUmdl9UWSqYgq6rOVfjqvXqo=; b=1tZ/nkNCWY6gDeefkEcLl+P+nLotnO1k29KiZwLW8FYKf3yOOLNFxstX0Ly29vLp7jKwMMeBq /V/AtchiM6+AAOc7bG0my7HZmQ2cs09mkcDbCONMXxBPKvl+zvbr2PW X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Currently MGLRU and non-MGLRU handle the reclaim statistic and writeback handling very differently, especially throttling. Basically MGLRU just ignored the throttling part. Let's just unify this part, use a helper to deduplicate the code so both setups will share the same behavior. Test using following reproducer using bash: echo "Setup a slow device using dm delay" dd if=3D/dev/zero of=3D/var/tmp/backing bs=3D1M count=3D2048 LOOP=3D$(losetup --show -f /var/tmp/backing) mkfs.ext4 -q $LOOP echo "0 $(blockdev --getsz $LOOP) delay $LOOP 0 0 $LOOP 0 1000" | \ dmsetup create slow_dev mkdir -p /mnt/slow && mount /dev/mapper/slow_dev /mnt/slow echo "Start writeback pressure" sync && echo 3 > /proc/sys/vm/drop_caches mkdir /sys/fs/cgroup/test_wb echo 128M > /sys/fs/cgroup/test_wb/memory.max (echo $BASHPID > /sys/fs/cgroup/test_wb/cgroup.procs && \ dd if=3D/dev/zero of=3D/mnt/slow/testfile bs=3D1M count=3D192) echo "Clean up" echo "0 $(blockdev --getsz $LOOP) error" | dmsetup load slow_dev dmsetup resume slow_dev umount -l /mnt/slow && sync dmsetup remove slow_dev Before this commit, `dd` will get OOM killed immediately if MGLRU is enabled. Classic LRU is fine. After this commit, throttling is now effective and no more spin on LRU or premature OOM. Stress test on other workloads also looking good. Global throttling is not here yet, we will fix that separately later. Suggested-by: Chen Ridong Tested-by: Leno Hou Reviewed-by: Axel Rasmussen Reviewed-by: Baolin Wang Signed-off-by: Kairui Song --- mm/vmscan.c | 92 +++++++++++++++++++++++++++++----------------------------= ---- 1 file changed, 43 insertions(+), 49 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 6df5ab625e6a..910420f7754d 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1942,6 +1942,44 @@ static int current_may_throttle(void) return !(current->flags & PF_LOCAL_THROTTLE); } =20 +static void handle_reclaim_writeback(unsigned long nr_taken, + struct pglist_data *pgdat, + struct scan_control *sc, + struct reclaim_stat *stat) +{ + /* + * If dirty folios are scanned that are not queued for IO, it + * implies that flushers are not doing their job. This can + * happen when memory pressure pushes dirty folios to the end of + * the LRU before the dirty limits are breached and the dirty + * data has expired. It can also happen when the proportion of + * dirty folios grows not through writes but through memory + * pressure reclaiming all the clean cache. And in some cases, + * the flushers simply cannot keep up with the allocation + * rate. Nudge the flusher threads in case they are asleep. + */ + if (stat->nr_unqueued_dirty =3D=3D nr_taken) { + wakeup_flusher_threads(WB_REASON_VMSCAN); + /* + * For cgroupv1 dirty throttling is achieved by waking up + * the kernel flusher here and later waiting on folios + * which are in writeback to finish (see shrink_folio_list()). + * + * Flusher may not be able to issue writeback quickly + * enough for cgroupv1 writeback throttling to work + * on a large system. + */ + if (!writeback_throttling_sane(sc)) + reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); + } + + sc->nr.dirty +=3D stat->nr_dirty; + sc->nr.congested +=3D stat->nr_congested; + sc->nr.writeback +=3D stat->nr_writeback; + sc->nr.immediate +=3D stat->nr_immediate; + sc->nr.taken +=3D nr_taken; +} + /* * shrink_inactive_list() is a helper for shrink_node(). It returns the n= umber * of reclaimed pages @@ -2005,39 +2043,7 @@ static unsigned long shrink_inactive_list(unsigned l= ong nr_to_scan, lruvec_lock_irq(lruvec); lru_note_cost_unlock_irq(lruvec, file, stat.nr_pageout, nr_scanned - nr_reclaimed); - - /* - * If dirty folios are scanned that are not queued for IO, it - * implies that flushers are not doing their job. This can - * happen when memory pressure pushes dirty folios to the end of - * the LRU before the dirty limits are breached and the dirty - * data has expired. It can also happen when the proportion of - * dirty folios grows not through writes but through memory - * pressure reclaiming all the clean cache. And in some cases, - * the flushers simply cannot keep up with the allocation - * rate. Nudge the flusher threads in case they are asleep. - */ - if (stat.nr_unqueued_dirty =3D=3D nr_taken) { - wakeup_flusher_threads(WB_REASON_VMSCAN); - /* - * For cgroupv1 dirty throttling is achieved by waking up - * the kernel flusher here and later waiting on folios - * which are in writeback to finish (see shrink_folio_list()). - * - * Flusher may not be able to issue writeback quickly - * enough for cgroupv1 writeback throttling to work - * on a large system. - */ - if (!writeback_throttling_sane(sc)) - reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); - } - - sc->nr.dirty +=3D stat.nr_dirty; - sc->nr.congested +=3D stat.nr_congested; - sc->nr.writeback +=3D stat.nr_writeback; - sc->nr.immediate +=3D stat.nr_immediate; - sc->nr.taken +=3D nr_taken; - + handle_reclaim_writeback(nr_taken, pgdat, sc, &stat); trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, nr_scanned, nr_reclaimed, &stat, sc->priority, file); return nr_reclaimed; @@ -4824,26 +4830,13 @@ static int evict_folios(unsigned long nr_to_scan, s= truct lruvec *lruvec, retry: reclaimed =3D shrink_folio_list(&list, pgdat, sc, &stat, false, memcg); sc->nr_reclaimed +=3D reclaimed; + /* Retry pass is only meant for clean folios without new isolation */ + if (isolated) + handle_reclaim_writeback(isolated, pgdat, sc, &stat); trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, type_scanned, reclaimed, &stat, sc->priority, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); =20 - /* - * If too many file cache in the coldest generation can't be evicted - * due to being dirty, wake up the flusher. - */ - if (stat.nr_unqueued_dirty =3D=3D isolated) { - wakeup_flusher_threads(WB_REASON_VMSCAN); - - /* - * For cgroupv1 dirty throttling is achieved by waking up - * the kernel flusher here and later waiting on folios - * which are in writeback to finish (see shrink_folio_list()). - */ - if (!writeback_throttling_sane(sc)) - reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); - } - list_for_each_entry_safe_reverse(folio, next, &list, lru) { DEFINE_MIN_SEQ(lruvec); =20 @@ -4886,6 +4879,7 @@ static int evict_folios(unsigned long nr_to_scan, str= uct lruvec *lruvec, =20 if (!list_empty(&list)) { skip_retry =3D true; + isolated =3D 0; goto retry; } =20 --=20 2.54.0