From nobody Mon Apr 13 05:44:23 2026 Received: from outbound.st.icloud.com (p-east2-cluster5-host6-snip4-6.eps.apple.com [57.103.79.99]) (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 4FDC68F4A for ; Fri, 10 Apr 2026 12:44:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.79.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825064; cv=none; b=Oai/DuOuiFOrpWqPuwENpKlrLf9hX+q7z5OtAIyuPtOQ0ekCV5xLWOoBxI1K5/wqgKeR7OtUQ0abk22oXj5LD/NQnAC9oRjLW1nVPItlPBgl9KWeJQ5sjoYgQ+Xowq5QWp/h9Xl/SUDPQsqSTTtk8B7+8oU2psjCR1PA5dlI2UU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825064; c=relaxed/simple; bh=tqh3vdlmNsT48tUHWGrESaIe4SZ64Y1OWUn3hKrdDcw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QD9PWVU4vDeyHnQ2/EdNmPczREwHqzCBTfLWpPfTxbpGGrvdxuVFLGmY+2rHIGhbG+z24iYgstLUe4oLoJPn1ftIJeIVLDm92gh7/U9whKPGLnmfakFXd+NXxyKh6KlPtNvnLDMroW20J02QZNRHJtlBd8tzucWch5lBATbAwxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=Bwbh/WpM; arc=none smtp.client-ip=57.103.79.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="Bwbh/WpM" Received: from outbound.st.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPS id 05F991800370; Fri, 10 Apr 2026 12:44:18 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1775825062; x=1778417062; bh=xnHQdlgcOL/4fk6OrbKqPiZnFKcP7FFawnir2OWjlBA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=Bwbh/WpMXu14l1eAf+6SpysHVgxbbXiWS9bo9Z//gBroQLPS8aTS62h/sarDCcBrJne3AGys38BrvIgbydxiOb7aAfGrnMc3Pt1f44J5pU+PYsfOv9uGD9v9NLwnddhnlCn9S6P1briLTnPwqSM+YrzZwuYztyHI0mqax+JW9Lbjie0AmGQim6Ue+ukaFBOaNF9LY3sfavIsohQ+cpgsvSKsDhjtt/LpXyAnap+qbwEY7t46UfGocrbYKVOzayk74ET/Am9gHS1+Lb2k0GlSMDuAqfOdeYIsPSVJjPOLjtEBkl/SwXwE7OUa/VMpOlYtxRhhOGaXa1HBFvIl5VtqCg== Received: from [21.6.122.162] (unknown [17.42.251.67]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPSA id 1BC7618002D9; Fri, 10 Apr 2026 12:44:11 +0000 (UTC) From: Zhang Peng Date: Fri, 10 Apr 2026 20:43:59 +0800 Subject: [PATCH v3 1/5] mm/vmscan: track reclaimed pages in reclaim_stat 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: <20260410-batch-tlb-flush-v3-1-ff0b9d3a351a@icloud.com> References: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> In-Reply-To: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775825044; l=4784; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=L4RSnGOlWbx1ob39I2cW9qTJxKxYSjJYIPZI0IE6F+E=; b=gQ9yLWGUxW517zEk1IQvsIrvKwskhHYXuieEWSNucHDUrXNZoYMUsoqxvdu9QG4YJ8utPTMaH nxzrqn2oSXJBbe8SlTaqo/SI2qppqjltR2eijdLF2P5/LITQ5LcTV3i X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-ORIG-GUID: vaOzalWNA5dld2tRGp3kg27SWnVGxl2v X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDEwMDExNyBTYWx0ZWRfX2XHa8stvVtpF BPoMCkjTdv2QLkEvhwXsB0DDRj3wFo0+/JQa3rO+zlJtjFQMji893q2bvF08omGsoPYMs/f6Jzq py0b8c7zAyU9Pu+5xlAWH16EHH8uleRxDlYZpErHCitwpuRajSJG6sPpx5cfSVF+OM2pyp3ff81 GSOrdhgts6jvbyiHMNWapM8JJELSjGjEBFfyP/AcRj2N6Jkl+SedppnH8mOPfivW+I+Y15kNRFs l0eW7MB6l4hIUOY0HKcr9R0CqEL4iRj+LrB7maYqt4dXJ7ehaSEKDI+tLY+oPeKOGP4FUlv9dqK vydVwDlqgSSfIref8+Ah2xpOyPcP82NiOgRM7gd2y12DfTLswxZTDODfI3I83c= X-Authority-Info-Out: v=2.4 cv=V+ZwEOni c=1 sm=1 tr=0 ts=69d8f0a4 cx=c_apl:c_pps:t_out a=YrL12D//S6tul8v/L+6tKg==:117 a=YrL12D//S6tul8v/L+6tKg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=_Dbb_MnoW-ofl-KRMSEA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-GUID: vaOzalWNA5dld2tRGp3kg27SWnVGxl2v X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-10_03,2026-04-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 spamscore=0 mlxscore=0 malwarescore=0 phishscore=0 adultscore=0 lowpriorityscore=0 clxscore=1015 suspectscore=0 bulkscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2604100117 From: Zhang Peng shrink_folio_list() returns nr_reclaimed while all other statistics are reported via reclaim_stat. Add nr_reclaimed to reclaim_stat and change the function to void for a consistent interface. No functional change. Suggested-by: Kairui Song Signed-off-by: Zhang Peng --- include/linux/vmstat.h | 1 + mm/vmscan.c | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h index 3c9c266cf782..f088c5641d99 100644 --- a/include/linux/vmstat.h +++ b/include/linux/vmstat.h @@ -26,6 +26,7 @@ struct reclaim_stat { unsigned nr_unmap_fail; unsigned nr_lazyfree_fail; unsigned nr_demoted; + unsigned nr_reclaimed; }; =20 /* Stat data for system wide items */ diff --git a/mm/vmscan.c b/mm/vmscan.c index 5ee64cf81378..f3f03a44042e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1053,9 +1053,9 @@ static bool may_enter_fs(struct folio *folio, gfp_t g= fp_mask) } =20 /* - * shrink_folio_list() returns the number of reclaimed pages + * Reclaimed folios are counted in stat->nr_reclaimed. */ -static unsigned int shrink_folio_list(struct list_head *folio_list, +static void shrink_folio_list(struct list_head *folio_list, struct pglist_data *pgdat, struct scan_control *sc, struct reclaim_stat *stat, bool ignore_references, struct mem_cgroup *memcg) @@ -1063,7 +1063,7 @@ static unsigned int shrink_folio_list(struct list_hea= d *folio_list, struct folio_batch free_folios; LIST_HEAD(ret_folios); LIST_HEAD(demote_folios); - unsigned int nr_reclaimed =3D 0, nr_demoted =3D 0; + unsigned int nr_demoted =3D 0; unsigned int pgactivate =3D 0; bool do_demote_pass; struct swap_iocb *plug =3D NULL; @@ -1477,7 +1477,7 @@ static unsigned int shrink_folio_list(struct list_hea= d *folio_list, * increment nr_reclaimed here (and * leave it off the LRU). */ - nr_reclaimed +=3D nr_pages; + stat->nr_reclaimed +=3D nr_pages; continue; } } @@ -1507,7 +1507,7 @@ static unsigned int shrink_folio_list(struct list_hea= d *folio_list, * Folio may get swapped out as a whole, need to account * all pages in it. */ - nr_reclaimed +=3D nr_pages; + stat->nr_reclaimed +=3D nr_pages; =20 folio_unqueue_deferred_split(folio); if (folio_batch_add(&free_folios, folio) =3D=3D 0) { @@ -1549,7 +1549,7 @@ static unsigned int shrink_folio_list(struct list_hea= d *folio_list, =20 /* Migrate folios selected for demotion */ nr_demoted =3D demote_folio_list(&demote_folios, pgdat, memcg); - nr_reclaimed +=3D nr_demoted; + stat->nr_reclaimed +=3D nr_demoted; stat->nr_demoted +=3D nr_demoted; /* Folios that could not be demoted are still in @demote_folios */ if (!list_empty(&demote_folios)) { @@ -1589,7 +1589,6 @@ static unsigned int shrink_folio_list(struct list_hea= d *folio_list, =20 if (plug) swap_write_unplug(plug); - return nr_reclaimed; } =20 unsigned int reclaim_clean_pages_from_list(struct zone *zone, @@ -1623,8 +1622,9 @@ unsigned int reclaim_clean_pages_from_list(struct zon= e *zone, * change in the future. */ noreclaim_flag =3D memalloc_noreclaim_save(); - nr_reclaimed =3D shrink_folio_list(&clean_folios, zone->zone_pgdat, &sc, + shrink_folio_list(&clean_folios, zone->zone_pgdat, &sc, &stat, true, NULL); + nr_reclaimed =3D stat.nr_reclaimed; memalloc_noreclaim_restore(noreclaim_flag); =20 list_splice(&clean_folios, folio_list); @@ -1992,8 +1992,9 @@ static unsigned long shrink_inactive_list(unsigned lo= ng nr_to_scan, if (nr_taken =3D=3D 0) return 0; =20 - nr_reclaimed =3D shrink_folio_list(&folio_list, pgdat, sc, &stat, false, + shrink_folio_list(&folio_list, pgdat, sc, &stat, false, lruvec_memcg(lruvec)); + nr_reclaimed =3D stat.nr_reclaimed; =20 move_folios_to_lru(&folio_list); =20 @@ -2168,7 +2169,8 @@ static unsigned int reclaim_folio_list(struct list_he= ad *folio_list, .no_demotion =3D 1, }; =20 - nr_reclaimed =3D shrink_folio_list(folio_list, pgdat, &sc, &stat, true, N= ULL); + shrink_folio_list(folio_list, pgdat, &sc, &stat, true, NULL); + nr_reclaimed =3D stat.nr_reclaimed; while (!list_empty(folio_list)) { folio =3D lru_to_folio(folio_list); list_del(&folio->lru); @@ -4862,7 +4864,8 @@ static int evict_folios(unsigned long nr_to_scan, str= uct lruvec *lruvec, if (list_empty(&list)) return scanned; retry: - reclaimed =3D shrink_folio_list(&list, pgdat, sc, &stat, false, memcg); + shrink_folio_list(&list, pgdat, sc, &stat, false, memcg); + reclaimed =3D stat.nr_reclaimed; sc->nr.unqueued_dirty +=3D stat.nr_unqueued_dirty; sc->nr_reclaimed +=3D reclaimed; trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, --=20 2.43.7 From nobody Mon Apr 13 05:44:23 2026 Received: from outbound.st.icloud.com (p-east2-cluster5-host7-snip4-10.eps.apple.com [57.103.79.33]) (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 0B4202236F0 for ; Fri, 10 Apr 2026 12:44:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.79.33 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825069; cv=none; b=Vn/eLCQG5xYvvxUnOxrrh58g5Qyf/nvTsQBpY2Wm1rlY7wTwnOqymHpQMYVnVfYU7vtwnYZ7bQE3+y27Dp9z/aXRS/whTbXkjSFBCfMcNwjxUThEc3sjHSiIpgpC+nBiYY98EHIzPVX/o5TTByjsZ3zyblZs5yDvwRTPSQ+DBsM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825069; c=relaxed/simple; bh=o0V0zHRuokKFdrTmqqx9vAI1MOcEwIIWKgzyYz0MNDI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eJkuYHYMN2r3z6l49mP2F0bEl8PW+nW7Wn76GcDwMYqN8OW9Z9VfIRYHqBLjIgba9nuCpK2ZLKGdLwEafoFfPQxuqg1WrmrVosC0Fj+jDRmrZL7ukaGrchqXWWzZWXD44lkPuzq2F2YmjtxJ1uAXHdHGjjwBRPD4UHZFz9hEq/c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=NggYHnO4; arc=none smtp.client-ip=57.103.79.33 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="NggYHnO4" Received: from outbound.st.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPS id D5BA7180035C; Fri, 10 Apr 2026 12:44:23 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1775825067; x=1778417067; bh=W+PRHeM3s2DI2yQOSrDPnTDvHR27Fnjgpu54ajqjWKM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=NggYHnO4sCFiWf2yFeD6rksIwDpSw5yxCxi26nNqpz/6DmfEeQq/s5yQcbpafCjC3u86NGruNIuxFFxBEfUNrqff969cVpMFeNg9kffWOjSeA2WVFu6R8LCRCc+ktl7hBGCS9QDJZ2+Nt+i+dDtwFwKl2DsS3t384wx51BwUZxlXXb2FqUgTohxH1MztHwR7mUYqWjC+rv8yc7ytntwh2uopoly7RtFbyb/dWdn7hJZbSLq7O6pNmB6SHfHIpX4tIdUUdDS4BzBD1rQGXfjB2LilghNGYo/JpZE4yuACanaONuMVhzLqiR2SmNtWsOUCWrUw2KKOPlclStY7gQpg/Q== Received: from [21.6.122.162] (unknown [17.42.251.67]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPSA id D257B180036C; Fri, 10 Apr 2026 12:44:18 +0000 (UTC) From: Zhang Peng Date: Fri, 10 Apr 2026 20:44:00 +0800 Subject: [PATCH v3 2/5] mm/vmscan: extract folio activation into folio_active_bounce() 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: <20260410-batch-tlb-flush-v3-2-ff0b9d3a351a@icloud.com> References: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> In-Reply-To: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775825044; l=2167; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=MwBPaFWPFTZ2m6kdtZRGEvS5uyMpPolk6vIxczHd7KA=; b=XuRAinfERZ2Os9psc4BxOrW2+d/eB2893MURTJHmBehjAGSD2yk28pf6dvlnr4MR8QpkWRk0C GkBfS5+/tOUDxEzBpWDrzBUO114U9QSuMrOTttn1auf5zpTB3m/Ornh X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-GUID: 7VQ5o_7CxGS7jTlLb2DDTJXiqvZ-pwy7 X-Proofpoint-ORIG-GUID: 7VQ5o_7CxGS7jTlLb2DDTJXiqvZ-pwy7 X-Authority-Info-Out: v=2.4 cv=edowvrEH c=1 sm=1 tr=0 ts=69d8f0a9 cx=c_apl:c_pps:t_out a=YrL12D//S6tul8v/L+6tKg==:117 a=YrL12D//S6tul8v/L+6tKg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=tUYT8qNuPuMAWAmJffYA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDEwMDExOSBTYWx0ZWRfX4/cDOcCkKePu 3FbeuyWGTLWr3SZ7vWpug4GM8Fri06MlOfPZODbyqIWdMcBlEiIGf4a18bKniQ62gHVcvmH4WOF XUry12O/5NkMvgTyC3m+lgbxWUMwtGYo6eybPYHiawvEhREp/WWgEgMBifHtY4dFmzEziIm5/Fq r5Al2wsZ6ahvImQcmgTuxj98XI0qe+27gJq/hxcQDdHBxSuNbxc54k5j/ZrhDQw7NPIHBXGJxvT wDxlaE8pykTgfcnx1jBRqDi/Ul9zpZzxHSB5HjdgRlyZfYtusprajOnPPXd1+ANt6WAP0LVxis/ jAGmtSYsy9TtW5nWurst4XbEI/cx2909BdT4wpl/cPLEpTKg1SShGzvMEuSbvA= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-10_03,2026-04-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 lowpriorityscore=0 adultscore=0 mlxscore=0 phishscore=0 clxscore=1015 malwarescore=0 spamscore=0 bulkscore=0 mlxlogscore=999 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2604100119 From: Zhang Peng The activate_locked label in shrink_folio_list() handles swap cache reclaim, marks the folio active, and updates activation statistics. Extract this block into folio_active_bounce() to make shrink_folio_list() easier to follow. No functional change Suggested-by: Kairui Song Signed-off-by: Zhang Peng --- mm/vmscan.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index f3f03a44042e..0860a48d5bf3 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1052,6 +1052,24 @@ static bool may_enter_fs(struct folio *folio, gfp_t = gfp_mask) return !data_race(folio_swap_flags(folio) & SWP_FS_OPS); } =20 +/* Mark folio as active and prepare to bounce back to head of LRU */ +static void folio_active_bounce(struct folio *folio, struct reclaim_stat *= stat, + unsigned int nr_pages) +{ + /* Not a candidate for swapping, so reclaim swap space. */ + if (folio_test_swapcache(folio) && + (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio))) + folio_free_swap(folio); + VM_BUG_ON_FOLIO(folio_test_active(folio), folio); + if (!folio_test_mlocked(folio)) { + int type =3D folio_is_file_lru(folio); + + folio_set_active(folio); + stat->nr_activate[type] +=3D nr_pages; + count_memcg_folio_events(folio, PGACTIVATE, nr_pages); + } +} + /* * Reclaimed folios are counted in stat->nr_reclaimed. */ @@ -1527,17 +1545,7 @@ static void shrink_folio_list(struct list_head *foli= o_list, nr_pages =3D 1; } activate_locked: - /* Not a candidate for swapping, so reclaim swap space. */ - if (folio_test_swapcache(folio) && - (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio))) - folio_free_swap(folio); - VM_BUG_ON_FOLIO(folio_test_active(folio), folio); - if (!folio_test_mlocked(folio)) { - int type =3D folio_is_file_lru(folio); - folio_set_active(folio); - stat->nr_activate[type] +=3D nr_pages; - count_memcg_folio_events(folio, PGACTIVATE, nr_pages); - } + folio_active_bounce(folio, stat, nr_pages); keep_locked: folio_unlock(folio); keep: --=20 2.43.7 From nobody Mon Apr 13 05:44:23 2026 Received: from outbound.st.icloud.com (p-east2-cluster5-host11-snip4-10.eps.apple.com [57.103.79.73]) (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 306551F4181 for ; Fri, 10 Apr 2026 12:44:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.79.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825074; cv=none; b=GQIQLoBbHxYmvq18AvToTvhG/mEuCnMZ1pgKsMMj6dyIIdFW0Sm4353YH6Qjbd0NnvPgG2i4yEjl51GzHRS1I/C0T5Vd4U31Ie4j1Fg78qnHvrr6vH/3TNJrAFd3sOuH7Vk8vE03k3ulqoKVNSZ2r2g8ZYFlSXdo+vnn0wQV1kw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825074; c=relaxed/simple; bh=JVwnSvPhyLSvxZuHUf6Ajbvprw7lz7E3S+t8UdpoShY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LQLPCME/pUFzvKJzA5gtBakTCGrbI/VIjPS6+0VdchC98UYJCpl0EqZsrmYZwz1ll7bDgx765AIx4CJiPOBKxth/IRVEvuZFnQ+e1HiFb+pwuo0a2dGPJs/AwB7KSadIjCmind1cI3DVxoMJKmFuaBaOyrvt9YM6XcNGLgS48N0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=KuW3bPup; arc=none smtp.client-ip=57.103.79.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="KuW3bPup" Received: from outbound.st.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPS id 8DAC018002C5; Fri, 10 Apr 2026 12:44:29 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1775825072; x=1778417072; bh=YLXk0ExvSFKrM+8uRAGb/whtOA3pAADUVtDTnKG795I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=KuW3bPupyQTxzro44jBfKAI9gLAoBhmcUHmmd6KD3N0VUFUGUAncGSIblDX8ELLgjxc4DpLtPZDy4b2funZ8CEkBR0Tv3S2rxRSM/UDbnuRnfPXb9qPggbua0mJ0VHuKcXOtrVmXR7iTRUCEnQGOO+UAd8BrWuFp29inSGbzY72Ds+B7bnpF41L5kP+nqmw60W4xJ5q9i00515zH9cFr4UtzWsbfzMJK4Si27I6T+MI71H81VFcmg4WuYH1XKWXjoJvd4JgKFl/hqNf9MblxqgzmvSVRPsWHnt/KAEN/uk7Xa/fo5g7TeUzaeNI14jQHMkjNsn4bw9vzgCcx2puk5A== Received: from [21.6.122.162] (unknown [17.42.251.67]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPSA id ABC7E18002DF; Fri, 10 Apr 2026 12:44:23 +0000 (UTC) From: Zhang Peng Date: Fri, 10 Apr 2026 20:44:01 +0800 Subject: [PATCH v3 3/5] mm/vmscan: extract folio_free() and pageout_one() 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: <20260410-batch-tlb-flush-v3-3-ff0b9d3a351a@icloud.com> References: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> In-Reply-To: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775825044; l=10277; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=tGKdWFwPZnHwuagsV9hFB2br3VeaFYCW4yO9KxlFTSI=; b=OFyHTTcEW4pb+CKG65b/skBmnsB++XD3E5sKI+f9WBVszMRb8k9PvzDjJIRPq1637T9bRYxWo CYRIk34R7/kCBu3gorVp0g2GZ7iJufkIx2Ovshlp1eSbYmEnGcemhTK X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDEwMDExOSBTYWx0ZWRfX/37wF9CgZq3d w2RnsEXJLfaMe3LE2HbcDDr7gFacJ60oUJ8PpLKfOs+oBdjcisrSfpFXotJw1O+GhIgIlT68JB4 qKMHbcD+NO1DYS1CozxnH8Q9IQMcho9NUfAamVutZmbYvDRZmr/iznILp+bALmouUNNpONJkFnP oJxnx/9348oqkSgnLsqeSuywrj7j8HiTEn06oXPcLQXJ5/U1w97Yfm9q0EmLTBj3VjCXrINCg2K ZJKxdp6uDJTIG5Ir7U7G0qPzxpsgQ0aEHEmD52PqZJ0V2ogvnE5ugqEaw7N40OXUZ+avZm1BrQP c44es404TSl4j67u1yygF3lsxkmpVi0YAaZBb/AavJF1M9ap7dCBmnOX1BJflU= X-Proofpoint-ORIG-GUID: nfbvZmfla3ovqC2eWVzP1jBpxIzQDv25 X-Authority-Info-Out: v=2.4 cv=SIpPlevH c=1 sm=1 tr=0 ts=69d8f0ae cx=c_apl:c_pps:t_out a=YrL12D//S6tul8v/L+6tKg==:117 a=YrL12D//S6tul8v/L+6tKg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=6kEm6JJsUdwq9LL28-YA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-GUID: nfbvZmfla3ovqC2eWVzP1jBpxIzQDv25 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-10_03,2026-04-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 clxscore=1015 lowpriorityscore=0 bulkscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2604100119 From: Zhang Peng shrink_folio_list() contains two large self-contained sections: the pageout() dispatch state machine and the folio-freeing path (buffer release, lazyfree, __remove_mapping, folio_batch). Extract them into pageout_one() and folio_free() respectively to reduce the size of shrink_folio_list() and make each step independently readable. No functional change Suggested-by: Kairui Song Signed-off-by: Zhang Peng --- mm/vmscan.c | 270 ++++++++++++++++++++++++++++++++++----------------------= ---- 1 file changed, 155 insertions(+), 115 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 0860a48d5bf3..c8ff742ed891 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1070,6 +1070,153 @@ static void folio_active_bounce(struct folio *folio= , struct reclaim_stat *stat, } } =20 +static bool folio_free(struct folio *folio, struct folio_batch *free_folio= s, + struct scan_control *sc, struct reclaim_stat *stat) +{ + unsigned int nr_pages =3D folio_nr_pages(folio); + struct address_space *mapping =3D folio_mapping(folio); + + /* + * If the folio has buffers, try to free the buffer + * mappings associated with this folio. If we succeed + * we try to free the folio as well. + * + * We do this even if the folio is dirty. + * filemap_release_folio() does not perform I/O, but it + * is possible for a folio to have the dirty flag set, + * but it is actually clean (all its buffers are clean). + * This happens if the buffers were written out directly, + * with submit_bh(). ext3 will do this, as well as + * the blockdev mapping. filemap_release_folio() will + * discover that cleanness and will drop the buffers + * and mark the folio clean - it can be freed. + * + * Rarely, folios can have buffers and no ->mapping. + * These are the folios which were not successfully + * invalidated in truncate_cleanup_folio(). We try to + * drop those buffers here and if that worked, and the + * folio is no longer mapped into process address space + * (refcount =3D=3D 1) it can be freed. Otherwise, leave + * the folio on the LRU so it is swappable. + */ + if (folio_needs_release(folio)) { + if (!filemap_release_folio(folio, sc->gfp_mask)) { + folio_active_bounce(folio, stat, nr_pages); + return false; + } + + if (!mapping && folio_ref_count(folio) =3D=3D 1) { + folio_unlock(folio); + if (folio_put_testzero(folio)) + goto free_it; + else { + /* + * rare race with speculative reference. + * the speculative reference will free + * this folio shortly, so we may + * increment nr_reclaimed here (and + * leave it off the LRU). + */ + stat->nr_reclaimed +=3D nr_pages; + return true; + } + } + } + + if (folio_test_lazyfree(folio)) { + /* follow __remove_mapping for reference */ + if (!folio_ref_freeze(folio, 1)) + return false; + /* + * The folio has only one reference left, which is + * from the isolation. After the caller puts the + * folio back on the lru and drops the reference, the + * folio will be freed anyway. It doesn't matter + * which lru it goes on. So we don't bother checking + * the dirty flag here. + */ + count_vm_events(PGLAZYFREED, nr_pages); + count_memcg_folio_events(folio, PGLAZYFREED, nr_pages); + } else if (!mapping || !__remove_mapping(mapping, folio, true, + sc->target_mem_cgroup)) + return false; + + folio_unlock(folio); +free_it: + /* + * Folio may get swapped out as a whole, need to account + * all pages in it. + */ + stat->nr_reclaimed +=3D nr_pages; + + folio_unqueue_deferred_split(folio); + if (folio_batch_add(free_folios, folio) =3D=3D 0) { + mem_cgroup_uncharge_folios(free_folios); + try_to_unmap_flush(); + free_unref_folios(free_folios); + } + return true; +} + +static void pageout_one(struct folio *folio, struct list_head *ret_folios, + struct folio_batch *free_folios, + struct scan_control *sc, struct reclaim_stat *stat, + struct swap_iocb **plug, struct list_head *folio_list) +{ + struct address_space *mapping =3D folio_mapping(folio); + unsigned int nr_pages =3D folio_nr_pages(folio); + + switch (pageout(folio, mapping, plug, folio_list)) { + case PAGE_ACTIVATE: + /* + * If shmem folio is split when writeback to swap, + * the tail pages will make their own pass through + * this function and be accounted then. + */ + if (nr_pages > 1 && !folio_test_large(folio)) { + sc->nr_scanned -=3D (nr_pages - 1); + nr_pages =3D 1; + } + folio_active_bounce(folio, stat, nr_pages); + fallthrough; + case PAGE_KEEP: + goto locked_keepit; + case PAGE_SUCCESS: + if (nr_pages > 1 && !folio_test_large(folio)) { + sc->nr_scanned -=3D (nr_pages - 1); + nr_pages =3D 1; + } + stat->nr_pageout +=3D nr_pages; + + if (folio_test_writeback(folio)) + goto keepit; + if (folio_test_dirty(folio)) + goto keepit; + + /* + * A synchronous write - probably a ramdisk. Go + * ahead and try to reclaim the folio. + */ + if (!folio_trylock(folio)) + goto keepit; + if (folio_test_dirty(folio) || + folio_test_writeback(folio)) + goto locked_keepit; + mapping =3D folio_mapping(folio); + fallthrough; + case PAGE_CLEAN: + ; /* try to free the folio below */ + } + if (folio_free(folio, free_folios, sc, stat)) + return; +locked_keepit: + folio_unlock(folio); +keepit: + list_add(&folio->lru, ret_folios); + VM_BUG_ON_FOLIO(folio_test_lru(folio) || + folio_test_unevictable(folio), folio); +} + /* * Reclaimed folios are counted in stat->nr_reclaimed. */ @@ -1415,125 +1562,16 @@ static void shrink_folio_list(struct list_head *fo= lio_list, * starts and then write it out here. */ try_to_unmap_flush_dirty(); - switch (pageout(folio, mapping, &plug, folio_list)) { - case PAGE_KEEP: - goto keep_locked; - case PAGE_ACTIVATE: - /* - * If shmem folio is split when writeback to swap, - * the tail pages will make their own pass through - * this function and be accounted then. - */ - if (nr_pages > 1 && !folio_test_large(folio)) { - sc->nr_scanned -=3D (nr_pages - 1); - nr_pages =3D 1; - } - goto activate_locked; - case PAGE_SUCCESS: - if (nr_pages > 1 && !folio_test_large(folio)) { - sc->nr_scanned -=3D (nr_pages - 1); - nr_pages =3D 1; - } - stat->nr_pageout +=3D nr_pages; - - if (folio_test_writeback(folio)) - goto keep; - if (folio_test_dirty(folio)) - goto keep; - - /* - * A synchronous write - probably a ramdisk. Go - * ahead and try to reclaim the folio. - */ - if (!folio_trylock(folio)) - goto keep; - if (folio_test_dirty(folio) || - folio_test_writeback(folio)) - goto keep_locked; - mapping =3D folio_mapping(folio); - fallthrough; - case PAGE_CLEAN: - ; /* try to free the folio below */ - } - } - - /* - * If the folio has buffers, try to free the buffer - * mappings associated with this folio. If we succeed - * we try to free the folio as well. - * - * We do this even if the folio is dirty. - * filemap_release_folio() does not perform I/O, but it - * is possible for a folio to have the dirty flag set, - * but it is actually clean (all its buffers are clean). - * This happens if the buffers were written out directly, - * with submit_bh(). ext3 will do this, as well as - * the blockdev mapping. filemap_release_folio() will - * discover that cleanness and will drop the buffers - * and mark the folio clean - it can be freed. - * - * Rarely, folios can have buffers and no ->mapping. - * These are the folios which were not successfully - * invalidated in truncate_cleanup_folio(). We try to - * drop those buffers here and if that worked, and the - * folio is no longer mapped into process address space - * (refcount =3D=3D 1) it can be freed. Otherwise, leave - * the folio on the LRU so it is swappable. - */ - if (folio_needs_release(folio)) { - if (!filemap_release_folio(folio, sc->gfp_mask)) - goto activate_locked; - if (!mapping && folio_ref_count(folio) =3D=3D 1) { - folio_unlock(folio); - if (folio_put_testzero(folio)) - goto free_it; - else { - /* - * rare race with speculative reference. - * the speculative reference will free - * this folio shortly, so we may - * increment nr_reclaimed here (and - * leave it off the LRU). - */ - stat->nr_reclaimed +=3D nr_pages; - continue; - } - } + pageout_one(folio, &ret_folios, &free_folios, sc, stat, + &plug, folio_list); + goto next; } =20 - if (folio_test_lazyfree(folio)) { - /* follow __remove_mapping for reference */ - if (!folio_ref_freeze(folio, 1)) - goto keep_locked; - /* - * The folio has only one reference left, which is - * from the isolation. After the caller puts the - * folio back on the lru and drops the reference, the - * folio will be freed anyway. It doesn't matter - * which lru it goes on. So we don't bother checking - * the dirty flag here. - */ - count_vm_events(PGLAZYFREED, nr_pages); - count_memcg_folio_events(folio, PGLAZYFREED, nr_pages); - } else if (!mapping || !__remove_mapping(mapping, folio, true, - sc->target_mem_cgroup)) + if (!folio_free(folio, &free_folios, sc, stat)) goto keep_locked; =20 - folio_unlock(folio); -free_it: - /* - * Folio may get swapped out as a whole, need to account - * all pages in it. - */ - stat->nr_reclaimed +=3D nr_pages; - - folio_unqueue_deferred_split(folio); - if (folio_batch_add(&free_folios, folio) =3D=3D 0) { - mem_cgroup_uncharge_folios(&free_folios); - try_to_unmap_flush(); - free_unref_folios(&free_folios); - } - continue; + else + continue; =20 activate_locked_split: /* @@ -1552,6 +1590,8 @@ static void shrink_folio_list(struct list_head *folio= _list, list_add(&folio->lru, &ret_folios); VM_BUG_ON_FOLIO(folio_test_lru(folio) || folio_test_unevictable(folio), folio); +next: + continue; } /* 'folio_list' is always empty here */ =20 --=20 2.43.7 From nobody Mon Apr 13 05:44:23 2026 Received: from outbound.st.icloud.com (p-east2-cluster5-host12-snip4-6.eps.apple.com [57.103.79.79]) (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 073CA214204 for ; Fri, 10 Apr 2026 12:44:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.79.79 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825080; cv=none; b=mnsqzJhssodam2+i4ATa03NO92Dkf2Cxw0yKzIRt5K9WCfTdm/xIPn+lKXKj+Osf07IEnIjw/WBCorknq6Q1OQYG70cLCYmKMxCrKvu0H4Gm0cp3NQRLRzZzomwvkOwb3p0JqeubvacbJpo6YKWL2TGyb1nM35BoCHRQpFC3gOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825080; c=relaxed/simple; bh=zWB/b0UX77NUsZLNabvQJnXU6XcpAog6MaO8o4rjLHI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kGRxzTHz8WWLc+jHyvinE8ZWd18Ii5lawyn7bfkCN4B+j0eFhzY1EfCOwNZ6M6BBcHCKGH+Qa5R+ODuLWMTxpqCe18xUvNhHlEYDKK5mgknikQ7CKPQbKQbvwTUge6CPyqozkSqX14zZm1grJwVRM9+pCt1aUlWHUFIXhDp+XT0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=lAQGjq/g; arc=none smtp.client-ip=57.103.79.79 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="lAQGjq/g" Received: from outbound.st.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPS id 42F5418001A7; Fri, 10 Apr 2026 12:44:35 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1775825078; x=1778417078; bh=yWdo7Hdt7imdUj5xnZfV2DW+WMLcx/gdexYpPpAmv2g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=lAQGjq/g7UcTBPINXozb+x/3UkwpLgoir36EfHLkOZFF53LMl6GX+SD8KE5ANiyuk6yokiqNt6IP9eSVaRjuExjPuFXOnt+EvaZAAoesfmWxZ3KsxSGUFJQctRHjiW2+oeaDtUesAcfERGFMl5i+flQXawiME5rcHyEv0e94NXo7wDJZaMs/+jJbMmviQGhI7BUj7l9TEZIGfEwU7Rc8boIPFCWSLKvjhmVWNqwY8gdEPxxoMun/gh6Tiv/1Sl1KXMPPo7aE3OrFizcJlBxrxCBSLUkNRseAUDuzFfcDRY1p3pf/eFFgUFinL3VAexPfCXSp5F+DRLFK70IO/riz2w== Received: from [21.6.122.162] (unknown [17.42.251.67]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPSA id 9938118002C9; Fri, 10 Apr 2026 12:44:29 +0000 (UTC) From: Zhang Peng Date: Fri, 10 Apr 2026 20:44:02 +0800 Subject: [PATCH v3 4/5] mm/vmscan: extract folio unmap logic into folio_try_unmap() 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: <20260410-batch-tlb-flush-v3-4-ff0b9d3a351a@icloud.com> References: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> In-Reply-To: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775825044; l=3583; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=Y4Ryu6QrIkBwzzotoKqlopBsSJ0saBN29iSqKN2EdmU=; b=874860+xk4cJPDuTM+XHgE1UfY0vAlWSFQ3ZmtH1qL9yYregIaV76AyXpDLsziUFjGhheoS7F Zy8uaGXlcHMD4FgOU7bm2KY/SMLYbXX+UGM1mLfxQgHcycZhLjINmmL X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-ORIG-GUID: GLk-jVPy0vNigIB7TtleosjzPtlVoQ81 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDEwMDExNyBTYWx0ZWRfX9qO4tI6tj5qs nx99lE9JTIIKEJKnhJrB9tw8ePCLgV5asT1acUP2SPzD5AQxl9QlTRcL9BzUk7eJzRc+NpY7xHM /oiULPiY23Ctngm5QjYkFksqqxpFXIQkSAKG6wS+I6yh/OZx3ekGSsMuEmvbC34edd7VkZ3381a GoymUqi1JSUSyn6l/W5M2DyicOdcSn6FdgwOvJhgjmGTPk7umdg3HHm1sWA88yHxvfcXkjPkFDF 1uxh8Vh0lzQZjLkHgIDc9cQ1jSh/WKTKlQiDftWEDLWJgqQHr+svgNgHFo5lRh0+YVSUCFZ2vAm WQAq1qgvhbqS7MAcHkZMEKrczabq1ndCMG7++f5MovWyQVJ9evORZjeUtHSmlk= X-Authority-Info-Out: v=2.4 cv=V+ZwEOni c=1 sm=1 tr=0 ts=69d8f0b4 cx=c_apl:c_pps:t_out a=YrL12D//S6tul8v/L+6tKg==:117 a=YrL12D//S6tul8v/L+6tKg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=Eai1HigTdSjqxVI1HoIA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-GUID: GLk-jVPy0vNigIB7TtleosjzPtlVoQ81 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-10_03,2026-04-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=995 spamscore=0 mlxscore=0 malwarescore=0 phishscore=0 adultscore=0 lowpriorityscore=0 clxscore=1015 suspectscore=0 bulkscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2604100117 From: Zhang Peng shrink_folio_list() contains a self-contained block that sets up TTU flags and calls try_to_unmap(), accounting for failures via reclaim_stat. Extract it into folio_try_unmap() to reduce the size of shrink_folio_list() and make the unmap step independently readable. No functional change. Suggested-by: Kairui Song Signed-off-by: Zhang Peng --- mm/vmscan.c | 70 +++++++++++++++++++++++++++++++++++----------------------= ---- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index c8ff742ed891..63cc88c875e8 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1217,6 +1217,44 @@ static void pageout_one(struct folio *folio, struct = list_head *ret_folios, folio_test_unevictable(folio), folio); } =20 +static bool folio_try_unmap(struct folio *folio, struct reclaim_stat *stat, + unsigned int nr_pages) +{ + enum ttu_flags flags =3D TTU_BATCH_FLUSH; + bool was_swapbacked; + + if (!folio_mapped(folio)) + return true; + + was_swapbacked =3D folio_test_swapbacked(folio); + if (folio_test_pmd_mappable(folio)) + flags |=3D TTU_SPLIT_HUGE_PMD; + /* + * Without TTU_SYNC, try_to_unmap will only begin to + * hold PTL from the first present PTE within a large + * folio. Some initial PTEs might be skipped due to + * races with parallel PTE writes in which PTEs can be + * cleared temporarily before being written new present + * values. This will lead to a large folio is still + * mapped while some subpages have been partially + * unmapped after try_to_unmap; TTU_SYNC helps + * try_to_unmap acquire PTL from the first PTE, + * eliminating the influence of temporary PTE values. + */ + if (folio_test_large(folio)) + flags |=3D TTU_SYNC; + + try_to_unmap(folio, flags); + if (folio_mapped(folio)) { + stat->nr_unmap_fail +=3D nr_pages; + if (!was_swapbacked && + folio_test_swapbacked(folio)) + stat->nr_lazyfree_fail +=3D nr_pages; + return false; + } + return true; +} + /* * Reclaimed folios are counted in stat->nr_reclaimed. */ @@ -1491,36 +1529,8 @@ static void shrink_folio_list(struct list_head *foli= o_list, * The folio is mapped into the page tables of one or more * processes. Try to unmap it here. */ - if (folio_mapped(folio)) { - enum ttu_flags flags =3D TTU_BATCH_FLUSH; - bool was_swapbacked =3D folio_test_swapbacked(folio); - - if (folio_test_pmd_mappable(folio)) - flags |=3D TTU_SPLIT_HUGE_PMD; - /* - * Without TTU_SYNC, try_to_unmap will only begin to - * hold PTL from the first present PTE within a large - * folio. Some initial PTEs might be skipped due to - * races with parallel PTE writes in which PTEs can be - * cleared temporarily before being written new present - * values. This will lead to a large folio is still - * mapped while some subpages have been partially - * unmapped after try_to_unmap; TTU_SYNC helps - * try_to_unmap acquire PTL from the first PTE, - * eliminating the influence of temporary PTE values. - */ - if (folio_test_large(folio)) - flags |=3D TTU_SYNC; - - try_to_unmap(folio, flags); - if (folio_mapped(folio)) { - stat->nr_unmap_fail +=3D nr_pages; - if (!was_swapbacked && - folio_test_swapbacked(folio)) - stat->nr_lazyfree_fail +=3D nr_pages; - goto activate_locked; - } - } + if (!folio_try_unmap(folio, stat, nr_pages)) + goto activate_locked; =20 /* * Folio is unmapped now so it cannot be newly pinned anymore. --=20 2.43.7 From nobody Mon Apr 13 05:44:23 2026 Received: from outbound.st.icloud.com (p-east2-cluster5-host11-snip4-10.eps.apple.com [57.103.79.73]) (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 A9DD3227BB9 for ; Fri, 10 Apr 2026 12:44:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.79.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825087; cv=none; b=EdYgpWlYtM3QAwrAxRzIT2gG4syqYj3narhdIShR+UA05pYwoXiSbEBs3whZBxvQKIZjQ/B5ok2GNrHSXNe9ARsIGilEhcNt9vZ/EIuKtEC4YPWtnQsiSCLGqfeaxfQl5xLeOxBF2eNSoaoRRU3NdQWnSCmc2rmNGF/hdAoIPcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825087; c=relaxed/simple; bh=aPPkFbpAyCahB7a9WkbN0clOTcDNuxZoSKkxVQFPl4c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=E5lS+1Lj5DMCc6L9fezyrgvVj8efp/vAmY5yIJ4KTvZ9/VqZN/HH4wgz2EsN1HFb0e7IVkcjP8qvhJG4WQNUuJ08CYhYXVad5K/xaa5VWSUsF3ypsZsWYGSefgVnbmzUvuK2raN4MbESgRByVCs63slm2iTRNiY+f9p73q8pK0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=fdstTdwU; arc=none smtp.client-ip=57.103.79.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="fdstTdwU" Received: from outbound.st.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPS id AB8DD18001BC; Fri, 10 Apr 2026 12:44:41 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1775825084; x=1778417084; bh=qmoarnu2fjz9QcThGpQzSg55OCBVK8r4IXQdeVfdm94=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=fdstTdwUGP6oeoUNcImHxlaPOWSy2m8Md9l2vIg8ASn4eb+f5+4MIvRK0L47tyzltg/3qVmQLIQJ5aJtCFbp7DpzJjal3qR8sZFjs6zVkkVgkK22JsCUGJnQzCWTQNQufOoynnrV4w8qtglgSGm6/u4nqb2dTFGYEnAhqjQ4IxbK6PeAq2bkoFpNmJ1SlehiAD1WQrgOM5fxI2gILCsJ6WJLlu0DhanWnBzQJKGjCGNv6ZmgWCtZmp49PNVzYRWnrv3mOEh3bVmUMDWnlLNLGB2DBzmNtOx14ALlas/wRpKBXTOjD/NOY75NF5vrDQ18XuPRu4dfK+WQsqiSBaHCUg== Received: from [21.6.122.162] (unknown [17.42.251.67]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPSA id 4B5071800371; Fri, 10 Apr 2026 12:44:35 +0000 (UTC) From: Zhang Peng Date: Fri, 10 Apr 2026 20:44:03 +0800 Subject: [PATCH v3 5/5] mm/vmscan: flush TLB for every 31 folios evictions 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: <20260410-batch-tlb-flush-v3-5-ff0b9d3a351a@icloud.com> References: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> In-Reply-To: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775825044; l=4434; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=IgHhHeddVtpb8e5HB29Ud1Cb4p7uaarC/SsI4SxrvPk=; b=coi4Y6VtXZfeKDBO8TUliE567FShmsMAgOmtlebee0aTx5oghQA/KKOtOzsvTdKWkc7SH58p7 OndkCNYXkKtDYcJQ+XPzEE9N0COL+RlZ/TGpE6NhFmcdZpq3T5L/0AT X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Authority-Info-Out: v=2.4 cv=GOwF0+NK c=1 sm=1 tr=0 ts=69d8f0bb cx=c_apl:c_pps:t_out a=YrL12D//S6tul8v/L+6tKg==:117 a=YrL12D//S6tul8v/L+6tKg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=6QAaJVoE38gftqD1B9gA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-ORIG-GUID: Z71cGpqHke44Ul61rUpscVntrnPPnNZ8 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDEwMDExOCBTYWx0ZWRfX05YzfSkSvbtj 0IAzfoVHAhGIrDpFkaPrXZKkKPeZKagS4oobIsTib6TkHTyo3U3E6OuPzoTBiummaKDMO4upczb lG9RTsPgHrNJnBaS/CuQV/rmz4OEG9UzIIvMvfU7jLjsgdratuR0N2YzbErmDCIvm5lsnanTWP3 sJNvI2+6+K1VymDlxmFThvxC5AstkN0nG0znciL4g/401HHKVIJLQuqcX0Jf4cVBEtN0X1yjIOU Jl1nCCGFnzamaIeMSObv107r6HkOR59raSlliQ7ksFRCwmtjTJYgZI377EiLHO27CwaJYTZjqqJ SjdG3M4JV6PIVMqUGJjZlpnnZHo30iLJSJHqRH92QkSvH/jEAl4ofC/+ORnS9w= X-Proofpoint-GUID: Z71cGpqHke44Ul61rUpscVntrnPPnNZ8 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-10_03,2026-04-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 clxscore=1015 spamscore=0 suspectscore=0 lowpriorityscore=0 malwarescore=0 bulkscore=0 adultscore=0 mlxscore=0 mlxlogscore=505 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2604100118 From: Zhang Peng Currently we flush TLB for every dirty folio, which is a bottleneck for systems with many cores as this causes heavy IPI usage. So instead, batch the folios, and flush once for every 31 folios (one folio_batch). These folios will be held in a folio_batch releasing their lock, then when folio_batch is full, do following steps: - For each folio: lock - check still evictable (writeback, mapped, dma_pinned) - If no longer evictable, put back to LRU - Flush TLB once for the batch - Pageout the folios Suggested-by: Kairui Song Signed-off-by: Zhang Peng --- mm/vmscan.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--= ---- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 63cc88c875e8..8d18e1c5b53d 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1217,6 +1217,55 @@ static void pageout_one(struct folio *folio, struct = list_head *ret_folios, folio_test_unevictable(folio), folio); } =20 +static void pageout_batch(struct folio_batch *fbatch, + struct list_head *ret_folios, + struct folio_batch *free_folios, + struct scan_control *sc, struct reclaim_stat *stat, + struct swap_iocb **plug, struct list_head *folio_list) +{ + int i, count =3D folio_batch_count(fbatch); + struct folio *folio; + + /* + * Reuse fbatch in-place: reinit only clears the count, the + * underlying folios array is still accessible via saved count. + * Filter and re-add valid folios back into the same batch. + */ + folio_batch_reinit(fbatch); + for (i =3D 0; i < count; ++i) { + folio =3D fbatch->folios[i]; + if (!folio_trylock(folio)) { + list_add(&folio->lru, ret_folios); + continue; + } + + VM_WARN_ON_FOLIO(folio_test_lru(folio), folio); + + if (folio_test_writeback(folio) || folio_mapped(folio) || + folio_maybe_dma_pinned(folio)) { + folio_unlock(folio); + list_add(&folio->lru, ret_folios); + continue; + } + + folio_batch_add(fbatch, folio); + } + + i =3D 0; + count =3D folio_batch_count(fbatch); + if (!count) + return; + /* One TLB flush for the batch */ + try_to_unmap_flush_dirty(); + for (i =3D 0; i < count; ++i) { + folio =3D fbatch->folios[i]; + pageout_one(folio, ret_folios, free_folios, sc, stat, plug, + folio_list); + } + /* Clear the batch for the caller's next use */ + folio_batch_reinit(fbatch); +} + static bool folio_try_unmap(struct folio *folio, struct reclaim_stat *stat, unsigned int nr_pages) { @@ -1264,6 +1313,8 @@ static void shrink_folio_list(struct list_head *folio= _list, struct mem_cgroup *memcg) { struct folio_batch free_folios; + struct folio_batch flush_folios; + LIST_HEAD(ret_folios); LIST_HEAD(demote_folios); unsigned int nr_demoted =3D 0; @@ -1272,6 +1323,8 @@ static void shrink_folio_list(struct list_head *folio= _list, struct swap_iocb *plug =3D NULL; =20 folio_batch_init(&free_folios); + folio_batch_init(&flush_folios); + memset(stat, 0, sizeof(*stat)); cond_resched(); do_demote_pass =3D can_demote(pgdat->node_id, sc, memcg); @@ -1565,15 +1618,19 @@ static void shrink_folio_list(struct list_head *fol= io_list, goto keep_locked; if (!sc->may_writepage) goto keep_locked; - /* - * Folio is dirty. Flush the TLB if a writable entry - * potentially exists to avoid CPU writes after I/O - * starts and then write it out here. + * Unlock while batching: holding the lock until the + * batch fills would stall swap faults that find this + * folio via swap cache lookup. pageout_batch() will + * relock each folio and recheck its state before + * writing it out. */ - try_to_unmap_flush_dirty(); - pageout_one(folio, &ret_folios, &free_folios, sc, stat, - &plug, folio_list); + folio_unlock(folio); + if (!folio_batch_add(&flush_folios, folio)) + pageout_batch(&flush_folios, + &ret_folios, &free_folios, + sc, stat, &plug, + folio_list); goto next; } =20 @@ -1603,6 +1660,10 @@ static void shrink_folio_list(struct list_head *foli= o_list, next: continue; } + if (folio_batch_count(&flush_folios)) { + pageout_batch(&flush_folios, &ret_folios, &free_folios, sc, + stat, &plug, folio_list); + } /* 'folio_list' is always empty here */ =20 /* Migrate folios selected for demotion */ --=20 2.43.7