From nobody Thu Apr 2 23:54:45 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