From nobody Thu Apr 2 22:28:54 2026 Received: from outbound.ci.icloud.com (ci-2006j-snip4-8.eps.apple.com [57.103.90.229]) (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 527323B960F for ; Thu, 26 Mar 2026 08:36:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.90.229 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514207; cv=none; b=DPDKtkrXtF6fIsSaqCZmWIHRc9Z3c/3OzGt3Vgzmu3hY1BLX+ftRFyjg0mHWAduuZyRDZhctrx8naiXszWHq9ng1Sek2r+TZK/iHOkNRXT/ch/DvgBEAmRYv7yolBah1JYXb+k1eG8aOkVOmxOJyrDfzYdxW7X9fPveScWGXOAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514207; c=relaxed/simple; bh=tqh3vdlmNsT48tUHWGrESaIe4SZ64Y1OWUn3hKrdDcw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mDYGCoBU8+NmkscAXTm2dup4raG5PROZKqpLre3jsySJeAvAI+dp6gd4RBZgN50F7xcONdM0G+7K+gNLg1EhQa6JbD4ZLOFoEkQHHz2ENTZcm8tnoCVpwnSB4ix2Hhirj3CJBsBUaCNZoqwHoB+vDaWZ6umcru9B3tQjsq6l5pw= 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=xv0llZso; arc=none smtp.client-ip=57.103.90.229 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="xv0llZso" Received: from outbound.ci.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPS id 0B238180010E; Thu, 26 Mar 2026 08:36:38 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1774514201; x=1777106201; bh=xnHQdlgcOL/4fk6OrbKqPiZnFKcP7FFawnir2OWjlBA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=xv0llZso92roc6dajC0joL0IrCGYUWOmiaNYuBcCOkIvKjSgC4dRNIHAbkKorw2uVYG2agAW900AuFdKL06LXGTdMJRvpUn/CfNr2wuNVyxFvmKjclEg+F+K2NY/OndS62yUhg2E3q6mPTGs3Cr/ZHCffahLm1eq9HmerHgG+uznXg2jM31OW5pi2Y/PixWMnUTk+E3eP8cFcGf2TJpqz5jT/rCCnEFe+NKyjd2a/mykd9kHwT+DLsp4j7FGl8G9fgT96S60Vt/90xYKms/XpLLfY04yPzdNvw5HPu36x0afTMQgugERhhKzpq6GzkR0Y23Ia4TduBuxP4oVxheQGA== Received: from [21.6.122.162] (unknown [17.57.156.36]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPSA id 4B565180020C; Thu, 26 Mar 2026 08:36:33 +0000 (UTC) From: Zhang Peng Date: Thu, 26 Mar 2026 16:36:17 +0800 Subject: [PATCH v2 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: <20260326-batch-tlb-flush-v2-1-403e523325c4@icloud.com> References: <20260326-batch-tlb-flush-v2-0-403e523325c4@icloud.com> In-Reply-To: <20260326-batch-tlb-flush-v2-0-403e523325c4@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=1774514187; l=4784; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=L4RSnGOlWbx1ob39I2cW9qTJxKxYSjJYIPZI0IE6F+E=; b=2UV1ap/2XGBFOjlcc27PyZc1fCg3+tnRXSu6BT6jv0K3cIvJ26xjERomAh6jkgpWLeAH1rLK0 ySUc5HwZkDZDb6cRQcK74iUfnSbKqi3c2pFBSt+jf4wYfmgY2Kn6VGi X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-GUID: rKqbL-ccvg4837RxPW9bvsrkbaBUYOvm X-Authority-Info-Out: v=2.4 cv=SONPlevH c=1 sm=1 tr=0 ts=69c4f018 cx=c_apl:c_pps:t_out a=2G65uMN5HjSv0sBfM2Yj2w==:117 a=2G65uMN5HjSv0sBfM2Yj2w==:17 a=IkcTkHD0fZMA:10 a=Yq5XynenixoA: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-Spam-Details-Enc: AW1haW4tMjYwMzI2MDA2MiBTYWx0ZWRfX9KnOKacCtdnj kuHp4ES2ft9H0ny5hcWJTUqgzRxXQ4Hcud1TTC2qe5eenz0nK/vy4xtjtZUNxNoAsnK9Lybsj9y UVexacLPeeuzFNen0DU+ZUMLnMzNpLXVk8yRejr3FDOnq2WfLdn7SRy1Te0GqNeFFbb3xa1krum 0Xh3AZKgc24e6Zbo5QjRkvanSOWHWbiUx5h0cd/DV1L4abYbRmDZ3gw00rJSQBL9h4iue/XdglK k4o5OHSpk6hrYgJcU1hSfj2LZi9YW1WlYRlmNm6A8NASMbAQ3L5wdVrj2rXrBhFI5IeCqDPwFOe jamXbCYEUyefgQh6bMpWatOtP7+jpVe1joFoAkx32ePhe2DivtW24ih4RgRzCM= X-Proofpoint-ORIG-GUID: rKqbL-ccvg4837RxPW9bvsrkbaBUYOvm 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-03-26_02,2026-03-24_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxscore=0 bulkscore=0 mlxlogscore=999 lowpriorityscore=0 adultscore=0 clxscore=1015 suspectscore=0 spamscore=0 malwarescore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2603260062 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 Thu Apr 2 22:28:54 2026 Received: from outbound.ci.icloud.com (ci-2006h-snip4-3.eps.apple.com [57.103.90.204]) (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 896D53BC681 for ; Thu, 26 Mar 2026 08:36:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.90.204 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514212; cv=none; b=gUJmlYUkwo3wMPeXrAcwLKn54cNqT3vJYFhIP4MkJzZ+pqrzeAxa31whWWadWL6oRSIpTvEQ7O2tvVMfFMySJmX5BpUtiThYEbhSwHNX4XFiKLzHjVWMHsPfw5i89hlmJKqvOLULCcqY0gBqYbC3zBlMACfgxn22EKOM3QnYcm0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514212; c=relaxed/simple; bh=o0V0zHRuokKFdrTmqqx9vAI1MOcEwIIWKgzyYz0MNDI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=axYxBAIfIRrOC1tOhg4ylZtguE/NkLpJWVOUTVXALlPOhWOglISjBbG5IMiiJA5nvBpWA18ExTK6I7Tzmw09PwKoKDPnUklN/16Sr6fqQ3GiONKXIM75hd0B3wYNagyjdv1DjiynUr72bSIFkeea+3jgcgkvw1qPPYnt11sx4vg= 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=y5fckf4a; arc=none smtp.client-ip=57.103.90.204 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="y5fckf4a" Received: from outbound.ci.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPS id 277DB1800216; Thu, 26 Mar 2026 08:36:45 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1774514209; x=1777106209; bh=W+PRHeM3s2DI2yQOSrDPnTDvHR27Fnjgpu54ajqjWKM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=y5fckf4aHvY/4d/k3tDLEvwtHArMQ3QvflAZqnGyufERLyAoK0PiQTpsvukg5kdqrz0j8okrB+tex28EDyL+udWlPGM/ov3fG4BDF+XiYWwxf+3UKqcz3R+zDe3BeIP4iqrM7V9PYGoS5uZKNtZZ6wkl4yxvY80eRwp2YTRKkjwveYCkv4yDuS3X7Q+z+YTH3pCIEhhKvCWD7XgJow0O2XvJiEloXr5wF4gk93PrvA8jN8k2pjI3cJ+Ook2Nwf9Tqu7Eb5Im+HIUIv/qwJSy9zwiNx0EGmHah4Q0quA6XXa+coBRXYbyVtk5FHwhLsboRJj/qrvqW2xuNiPopli71Q== Received: from [21.6.122.162] (unknown [17.57.156.36]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPSA id E62D81800103; Thu, 26 Mar 2026 08:36:38 +0000 (UTC) From: Zhang Peng Date: Thu, 26 Mar 2026 16:36:18 +0800 Subject: [PATCH v2 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: <20260326-batch-tlb-flush-v2-2-403e523325c4@icloud.com> References: <20260326-batch-tlb-flush-v2-0-403e523325c4@icloud.com> In-Reply-To: <20260326-batch-tlb-flush-v2-0-403e523325c4@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=1774514187; l=2167; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=MwBPaFWPFTZ2m6kdtZRGEvS5uyMpPolk6vIxczHd7KA=; b=QTpI4KhMzfCr7GoxijlS887QDstZgQYD2aylFzXTg1x16Sz+J6AVX6Kio9gWjhzhmPJWOihxV Be08cy4jIvVAjl7Pq55we5fm/FOdC6J27OAWoVx6NKDbz6HMHN7q+Sm X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-GUID: Bkfp8Xp4cWnVHNChC_PiyCLqKKrQZk6r X-Authority-Info-Out: v=2.4 cv=acpsXBot c=1 sm=1 tr=0 ts=69c4f020 cx=c_apl:c_pps:t_out a=2G65uMN5HjSv0sBfM2Yj2w==:117 a=2G65uMN5HjSv0sBfM2Yj2w==:17 a=IkcTkHD0fZMA:10 a=Yq5XynenixoA: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-ORIG-GUID: Bkfp8Xp4cWnVHNChC_PiyCLqKKrQZk6r X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzI2MDA2MiBTYWx0ZWRfX0eQXWbj8yDBm nNm+6008kYFQdsgm+vSpXzEAMOfBLAUxgTxbjuTp3nFrLYrBvfqVPa2usPCCwBFVrEpjKwbI64D Vv8ftur/dqMW4Qw6U4XU5f4D9zgJC2Eveg/EOnR4QlHWHSl4YOq7MPY81i6er7qn3lW8mCfbxDJ vybhu9hKzPEFNI6GjYG34cC9SAlcFAZqfAI514EAQ19LHhkCQpn/2I6Jtj2xpWvoB/hnpEJbwpc mF0mTfIrWmp2CU7O3/j3puvx58XQUjT+SDa4KasECfQmSbf/iipKc1Am1Wh578fgeUv6hMmZifD DEzYkMAux4E0yopD5hCzDMLlE/nLG+WNmqo0pCGPTVLQdDXNskZ2aaU+qgdVn8= 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-03-26_02,2026-03-24_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 phishscore=0 spamscore=0 clxscore=1015 mlxscore=0 malwarescore=0 suspectscore=0 adultscore=0 lowpriorityscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2603260062 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 Thu Apr 2 22:28:54 2026 Received: from outbound.ci.icloud.com (ci-2005f-snip4-11.eps.apple.com [57.103.89.191]) (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 7E9413B6C1E for ; Thu, 26 Mar 2026 08:36:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.89.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514218; cv=none; b=sl+LshfqaA5EtYZFm2Eu9PJCHEGnWhvQbyGxqEQqfEaQWyCci/DG4+a+agVCU7+EiXxDm7r+rXpELHqz7jRReuGQYRcjnUKOWXsL/d8cO2l4QwJ3XvJmH73slJhs1HKvJ7xe12q6PO5V5uImyFOY7vvu8JOaaT30ARSL/NHod2A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514218; c=relaxed/simple; bh=JVwnSvPhyLSvxZuHUf6Ajbvprw7lz7E3S+t8UdpoShY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bHhHnSNUJJ+tXBCV5vMLC2Ab6IjzXHkqBqJxld/oK+VOfLKGtiS8lVw77zDoN9AqhbPrOy/27lMYtN4anxr48uyqgSwJu/WvnHYLxjYJ7dWEr7wBcYr0oFisGmRGDi1J3JCLJYf01la7YMvLnAiAdFDucANRdm1DgYqtGpwzcgw= 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=InriqrVR; arc=none smtp.client-ip=57.103.89.191 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="InriqrVR" Received: from outbound.ci.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPS id 2F1CA180021B; Thu, 26 Mar 2026 08:36:52 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1774514215; x=1777106215; bh=YLXk0ExvSFKrM+8uRAGb/whtOA3pAADUVtDTnKG795I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=InriqrVRXN+ao2dHfSY9otBp0V80Z8N9f2S/e+6JXttHjS/ywV2nytGKkJNkjSyLt49XEHLo42VcZyg6yasJjCWaHjnt/b4zLRjcSqRPC8jfvhZ/cxn3IkXtZz5aQhE1WoRLF71HY/w5ax+niIxOWXA34mKYOB8iqDOSDHeAVdDzRTD5hdPjTcr0+eKdzmvyc5H3yk+aPIPGW0vsDc6FdUtGoNVRKODbafjBvITfdXUy+6NLA1Sgk/6Kq9BFig1VD9u7pPPKz4HPgyBdp9B+357Gf/qFo7+t+8kz81neBU26aAVKIpngM9v3P7KIlEqhSBbSDteYIaPcrvWqpzudig== Received: from [21.6.122.162] (unknown [17.57.156.36]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPSA id 03D39180020E; Thu, 26 Mar 2026 08:36:45 +0000 (UTC) From: Zhang Peng Date: Thu, 26 Mar 2026 16:36:19 +0800 Subject: [PATCH v2 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: <20260326-batch-tlb-flush-v2-3-403e523325c4@icloud.com> References: <20260326-batch-tlb-flush-v2-0-403e523325c4@icloud.com> In-Reply-To: <20260326-batch-tlb-flush-v2-0-403e523325c4@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=1774514187; l=10277; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=tGKdWFwPZnHwuagsV9hFB2br3VeaFYCW4yO9KxlFTSI=; b=BfHYyYtMeMcvuuxKlM+EB/3NcQwptDx1pljihw1sC/Hgur/RIhDv8u61BAz1gGYS/n7l4PAoO So0OPSRtttqDubi9h5dOc/0f3T4anU7PCn2zP49TkNfQYJHT3C4xoo0 X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-GUID: _FmElwohPil0BxWOL18C3vy9v1uORJtF X-Authority-Info-Out: v=2.4 cv=PPsCOPqC c=1 sm=1 tr=0 ts=69c4f026 cx=c_apl:c_pps:t_out a=2G65uMN5HjSv0sBfM2Yj2w==:117 a=2G65uMN5HjSv0sBfM2Yj2w==:17 a=IkcTkHD0fZMA:10 a=Yq5XynenixoA: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-ORIG-GUID: _FmElwohPil0BxWOL18C3vy9v1uORJtF X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzI2MDA2MiBTYWx0ZWRfXybuUeK+i1g/t wvRROGQBuy0Dkheqa3i6WJn9rDsJvNxzLG2VsttkNE0V0ShkM3KoZtC2JDYnEe9LyaBO37DO6Qv MhinLd6wSB+QGhydV2e1oU6vN9zYNTwi+JwFiqjpIgCsiCkg3HYtyMxWp2QJPJtbAtPaAheo5vp u4gmNtCVt16jsdhRCbj0rSZyHyB3w5fvClxqqABJsjaBtQPf3WGsc3Q7BcDIxwU4cn0LCiMhF6G mGKHQ/fr37I3eD3zwx8Fk1zhudNX/wkMvYV0Is2ESs20g8dwNW/rt64+kSNS5mj3G5FpamEmqbB GfifFCn9ZRlDIwD6dpOSwTnn/LRcO3I6AzqhMOU2mhrNUY2lSNX9byEqVfcQhc= 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-03-26_02,2026-03-24_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxscore=0 bulkscore=0 malwarescore=0 spamscore=0 lowpriorityscore=0 mlxlogscore=999 suspectscore=0 clxscore=1015 phishscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2603260062 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 Thu Apr 2 22:28:54 2026 Received: from outbound.ci.icloud.com (ci-2005i-snip4-11.eps.apple.com [57.103.89.221]) (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 572063B9D9A for ; Thu, 26 Mar 2026 08:37:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.89.221 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514222; cv=none; b=LX+wXZMZvwU+VbvPnyEEcilNobpvgKOLxCymFSRnD+Mbd4ojLlok+w63jiXIuZ8qD2/dGkAPCf7GFHN5Uz/B30WH6x6ACv+XXKD19T4IeG9GdsAmfrBvcCy3GOGUbwLq2B8sSRwrEt0EXF7ZcRrOXPmAQ5p9xnXqlIOSuij+EuA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514222; c=relaxed/simple; bh=zWB/b0UX77NUsZLNabvQJnXU6XcpAog6MaO8o4rjLHI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Q1EIpAumdRy8DBxYAJQUQW7yOI++0VehXoJHN2llKwD/JAtXSvhno7eV1x2pt26PAS1EEmkYWwLovvDpJzpvg2Ar37gZSnCiqWQWbnwnwef7ZUNDtW5aIbUNut/844i2KWRnxOx3eP/8CkxELqX92Ib1UXmLlBuj+A63k0bz2Pw= 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=dZjRay/z; arc=none smtp.client-ip=57.103.89.221 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="dZjRay/z" Received: from outbound.ci.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPS id 7ACBC18000B9; Thu, 26 Mar 2026 08:36:58 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1774514220; x=1777106220; bh=yWdo7Hdt7imdUj5xnZfV2DW+WMLcx/gdexYpPpAmv2g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=dZjRay/zYVP9ZfZIq9IR9Ouux+kdLB73T3/H7PS/CxTNqUZpUrXi2SJNNnbn5U/PcZ1C8j08jwCg8iQFemd2oAbQ78zTz9GfDgiY2OChRSLTJfJeur4FSuBTaxW+xXtjeI8bw9LLzEZVhMdCDHDfCJbk2OnX1wpNlMW2S6WVwJvrlfbg7GgkOVu8Hlug2noLK+feQazD6gHFohZIt1Ou3Y/35L2skHRf7hw7kzY7ciKWWt/tdLzxGLKwt0P1Hxopw3iu1mQ4MQE09pdkWynQx6cGpTfAAQr45nWhObaoMiQiIDhySHqi1cGqIuFo+FlTt25eCyc75lH2R8uvagMjOg== Received: from [21.6.122.162] (unknown [17.57.156.36]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPSA id 178281800139; Thu, 26 Mar 2026 08:36:51 +0000 (UTC) From: Zhang Peng Date: Thu, 26 Mar 2026 16:36:20 +0800 Subject: [PATCH v2 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: <20260326-batch-tlb-flush-v2-4-403e523325c4@icloud.com> References: <20260326-batch-tlb-flush-v2-0-403e523325c4@icloud.com> In-Reply-To: <20260326-batch-tlb-flush-v2-0-403e523325c4@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=1774514187; l=3583; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=Y4Ryu6QrIkBwzzotoKqlopBsSJ0saBN29iSqKN2EdmU=; b=Rj1I5FoCkrSh+ZfYn1ymUCAMwpL5FG1T5gr8wOnSdqruFLqr2zEJWvpWrW3tL6QnQT2lz6tyz ginbigcuZd0D5jDrdPi2cxV2hytVs6CDTY0uqXhgOlvIlIgPYMvP8+Q X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Authority-Info-Out: v=2.4 cv=QO1lhwLL c=1 sm=1 tr=0 ts=69c4f02b cx=c_apl:c_pps:t_out a=2G65uMN5HjSv0sBfM2Yj2w==:117 a=2G65uMN5HjSv0sBfM2Yj2w==:17 a=IkcTkHD0fZMA:10 a=Yq5XynenixoA: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-ORIG-GUID: guicJJqKTU_NJdVTnchv-CoEO9VUaA9U X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzI2MDA2MiBTYWx0ZWRfXzB/tM/gEjFLc 1GlU7Mm6WDQqeTuKfWjZ5xHDGnnXO00F9FV4U5DQz4pRsng1Llcw9BTN+HSE5/UGWJPHK+/elEv 259tQ8ZGQPdCiSE+yR9E4Lfgtt1OpI7sdQxeWeo4se7AGDx4QwMRXPLTSU0Ze5JSBQ/Yf2qzHss sZHV6QGF0iyxI2XOnY6Mq+W5BcbKghQp9MTtcbSRhAWKChgoZ08SWmecIx9FMsv+OLfRwv9dRgv wqTQIulXfpTDkVEvPV/YPhb2VoJrAjfGhoDbiGRhfJupEkz6Hh1V8B81vxZwW/iu895CAoN/S0q nIO1Nhoi9NkTaYVWmY+zFTAJwgnWZOE1Cotp4nx2m9500L+idmdN7p90gDnWBo= X-Proofpoint-GUID: guicJJqKTU_NJdVTnchv-CoEO9VUaA9U 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-03-26_02,2026-03-24_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 spamscore=0 bulkscore=0 adultscore=0 malwarescore=0 clxscore=1015 lowpriorityscore=0 mlxlogscore=983 phishscore=0 mlxscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2603260062 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 Thu Apr 2 22:28:54 2026 Received: from outbound.ci.icloud.com (ci-2005g-snip4-11.eps.apple.com [57.103.89.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 6D2A42D97B8 for ; Thu, 26 Mar 2026 08:37:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.89.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514231; cv=none; b=EkAEpOFxJ9RZv6MEDZeO1l8n7v7e478gGkVtTgN5p0KlM24KKhO8x4eD27E4dUXfy0tzcL8IYrrHLmi3FyIGnECkFIFfRbcU9n20GjPnupq2LNUVRIFgGARmdp6vONjvfulbScVRaevCf2BV92lNv1L9vMr1J/oAawfOgGpQMbA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514231; c=relaxed/simple; bh=3/PkhF8c4Sqs7NCBfkg1dvBVntK1BkvSvbzojj6Ku40=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KUEG30+0whVdLVzt5dsw4n+jO9qdM/MKbOAJJGIOe0Del2eVQgvzFwiP7dBhLWa2gEp4ilXX0sjyZYagjYVg6kM9npR9QklO2EzX/dbXFo+IhdqV7k08og945iGSsuI8ik4vWEXDZsCPx3VS6ZXk8YYRifSHlYboRBlQqfWg+pk= 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=Jz94qwq9; arc=none smtp.client-ip=57.103.89.201 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="Jz94qwq9" Received: from outbound.ci.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPS id D9FC51800105; Thu, 26 Mar 2026 08:37:05 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1774514229; x=1777106229; bh=2sE/Uo/jRYYdN0UWH7fbR2fadJhnGzJnFLpQ50tGmpY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=Jz94qwq9j0IHKu/N8vYm3ygq1sXR3Ajy0TlZzgzLOUAWHyR3teI5gaGC4wY4yY5KlXbGe9Ae89KuvUWsVRX8Ag0Ua+L/WullFjQZ4Nf4AVmjmN7RhDc0V/Fo7CgBEe+yQD1GjayM6ZQfSrMl0I6O0sIAjEQziDnmjNWaA40qQ8XSV+G/p7f9kYSR5dAzBa/zGf+Q8rLovd+G8RiwqJDOQBJZCgA1mDruLa9kLKyBYfc8joSmqrkWde3uXUSEg1KXI7z9YxtLplgKM3HBKGVGUyUjm1iR4zXmO3o9bLmySRBk3HWL/7LqQnbvsWzjKxcjJNnWezAAwEpVhSjm2m7MtQ== Received: from [21.6.122.162] (unknown [17.57.156.36]) by p00-icloudmta-asmtp-us-central-1k-100-percent-11 (Postfix) with ESMTPSA id 989CA1800101; Thu, 26 Mar 2026 08:36:58 +0000 (UTC) From: Zhang Peng Date: Thu, 26 Mar 2026 16:36:21 +0800 Subject: [PATCH v2 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: <20260326-batch-tlb-flush-v2-5-403e523325c4@icloud.com> References: <20260326-batch-tlb-flush-v2-0-403e523325c4@icloud.com> In-Reply-To: <20260326-batch-tlb-flush-v2-0-403e523325c4@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=1774514187; l=4560; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=ktO5LArnI4nbf2Br6U60uWT52jrGYKbMW/1pY1vTAV4=; b=5HDL+iwXk5ZIHMD2sHDZce2sXUi1l4PSUe0l6Z5PfKPVYwRpMTMNrzp29VSjzU6qUn0ziUt7l ZVtcZRrpVWEAiuoZccoBfMD6hFBPZjSQoM334EkFcOWbefUOQX5gF14 X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-ORIG-GUID: _StyBDbCn9ITzjn54h0Q8v7dkMDMWNON X-Authority-Info-Out: v=2.4 cv=eNgeTXp1 c=1 sm=1 tr=0 ts=69c4f033 cx=c_apl:c_pps:t_out a=2G65uMN5HjSv0sBfM2Yj2w==:117 a=2G65uMN5HjSv0sBfM2Yj2w==:17 a=IkcTkHD0fZMA:10 a=Yq5XynenixoA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=w1dcpSADNQsu10hy-bYA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMzI2MDA2MiBTYWx0ZWRfX74mX4e3E0Qs+ AnnwNDs5h3WWcSYEiAHYUKvuiXNNIc6kqJgw8JzJBm0DjJWKP4ECo76VV7f4iigCpLlQIpRyaFn 7YDDHFr91e82VwTdIMW7/SX0iu/jWiAQiRXpDO0SnjQrkTAJAc5OjRARrOa0rJlYvAsO6tBnIKY nY79MDfovOsc3K8VcoxQiaS5D9HTVEmzS6eA1TRDTx/SO7p+gy6nr+9jJN2I2uM49P2p9vLbjy0 bM1lqgPKKr0WPcRityaKZBufgraOtRFyCY1irxd8YwTSsqbTuIxDMlqE0IlQlnF8kG63ZuwHXlw s7rjjxI8mZCnuStdnR0mjOGJ0ukyw9c4a8wxC7JTyMoXa5wfR/eA/voZGFq8yc= X-Proofpoint-GUID: _StyBDbCn9ITzjn54h0Q8v7dkMDMWNON 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-03-26_02,2026-03-24_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxlogscore=716 spamscore=0 bulkscore=0 phishscore=0 malwarescore=0 clxscore=1015 adultscore=0 lowpriorityscore=0 mlxscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2603260062 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, lru, mapped, dma_pinned) - If no longer evictable, put back to LRU - Flush TLB once for the batch - Pageout the folios Note we can't hold a frozen folio in folio_batch for long as it will cause filemap/swapcache lookup to livelock. Fortunately pageout usually won't take too long; sync IO is fast, and non-sync IO will be issued with the folio marked writeback. Suggested-by: Kairui Song Signed-off-by: Zhang Peng --- mm/vmscan.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 63cc88c875e8..27de8034f582 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1217,6 +1217,47 @@ 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; + + 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; + } + + if (folio_test_writeback(folio) || folio_test_lru(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); + } + folio_batch_reinit(fbatch); +} + static bool folio_try_unmap(struct folio *folio, struct reclaim_stat *stat, unsigned int nr_pages) { @@ -1264,6 +1305,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 +1315,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 +1610,21 @@ 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. + * For anon, we should only see swap cache (anon) and + * the list pinning the page. For file page, the filemap + * and the list pins it. The folio is unlocked while + * held in the batch, so pageout_batch() relocks each + * folio and rechecks its state. If the folio is under + * writeback, on LRU, mapped, or DMA-pinned, it will + * not be written out and is put back to LRU list. */ - 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 +1654,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