From nobody Mon Jun 8 23:56:53 2026 Received: from outbound.pv.icloud.com (pv-2006e-snip4-3.eps.apple.com [57.103.67.66]) (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 EF6A634C808 for ; Mon, 25 May 2026 14:57:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.67.66 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721065; cv=none; b=a2k0W95oezl+ryuqLvRIxI9GCyH6odzYoZbxoCwIjjCS+JdEZuPp0d4TbwvEEcGDSZ/W73QKpsD9GHrzvRCCbIMdkmQkzedPdZHi7Ja3h1djZSXcn6HKbwgmy9d4y546fHRSXto7Dp7RdYKe146YfzfU2u2D0eCE3pr8TMaEI6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721065; c=relaxed/simple; bh=s9EbIFh/LV8iPlyUTN5QDlVlv2e5tCPSHPTZBWN4pmg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hDkbxc4HgOz+aaF9u1y4PdA+od8wIo9I9ApPJMpxs/taj/tWhX8cU5BmWy1tqKPQ8PpQsEDspnf1gISsxQJzTFR4trYJDxtHnzwiJ+O/+3KxOhMCMDN12SS+Q/T4brcSzJgFI4jZCfBL5m8jfc+6QDtXfG1/ZQAK7kL/ZkYqVPg= 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=Nt58n+BQ; arc=none smtp.client-ip=57.103.67.66 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="Nt58n+BQ" Received: from outbound.pv.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPS id 2FBD418000B3; Mon, 25 May 2026 14:57:39 +0000 (UTC) X-ICL-Out-Info: HUtFAUMEWwJACUgBTUQeDx5WFlZNRAJCTQhMHV8FRQNBF0kFWBcOVk1DEUMdUhlfH1cTVhR3AlEcVg1XQ1QEX1BfHA4EVAddBV1WUAJaS0ATBEoDTV8OXh8EF0YZVQRHHl1WQxsZAlEcVg1XQ1QEX1BJDEFQbFoARxdIHV0ZWW9QXRwOBFQHXQVdVlACWktfGV1FD18HWQRADEoGQFUKRhNRVUcBVUZUHEwLW0BBXx9BFEAAWg9SVkZYGlBdBytbE1UXRgkZCF0dB1hHFEcODxlaFFwYUw== Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1779721063; x=1782313063; bh=vYeCbJz1qvnDRDXrZFPnGvD5lPvltNFcb/fwP8QSlLA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=Nt58n+BQ471CG0lbBKFMrOjiRTvJYWpLtMo0f07CvdzLhvgjbyJiLARQ5WCOLwhFgV4kgGae2DbdEqmkoKTjYdr/pDSpWi5fK+WwkaY+1GVqD2kyOKD/36F56VGTflxrZiNJ5Yw6Wrp98LZh0hVwRAuWI0XmLtKvOM47eQLMBEAfZVR1aZknBTuA+96QnVfXhTLJEssbxMNlGomGkJqqRmPrVyJRSNLMO2fTilx3JzS+dg+/PJKZCGexKD9glhavbmi1deGsh0EncUNx9CgYz07lYP7BG6OeOOlYtUa/HKRhJVFtLhiONZY5yvUGe7wBWp3uDYXGNKp2UeewC81lgA== Received: from [21.6.122.162] (unknown [17.56.9.36]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPSA id 8D31D18000D1; Mon, 25 May 2026 14:57:34 +0000 (UTC) From: Zhang Peng Date: Mon, 25 May 2026 22:57:17 +0800 Subject: [PATCH v4 1/5] mm/vmscan: introduce folio_activate_locked() helper 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: <20260525-batch-tlb-flush-v4-1-83789d6abc00@icloud.com> References: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> In-Reply-To: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko , "Liam R. Howlett" , Qi Zheng Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779721049; l=2251; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=s9EbIFh/LV8iPlyUTN5QDlVlv2e5tCPSHPTZBWN4pmg=; b=Q9hFLErSWCqkSz9LhQiyJcXUqZ73JcbBE+VSQfJkIPsQIu6C0SsgjkT7qcNrbu/dskgodbytC fbc3JevNMVEC4fG6KrjSyQZrlDQ+IwOe9pX21+h4qbGSTcHLbfr1n4r X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-GUID: 4R-4Yspfm7SXYI4kHMakN0x1YTQYALGO X-Proofpoint-ORIG-GUID: 4R-4Yspfm7SXYI4kHMakN0x1YTQYALGO X-Authority-Info-Out: v=2.4 cv=Rq3I7SmK c=1 sm=1 tr=0 ts=6a146364 cx=c_apl:c_pps:t_out a=azHRBMxVc17uSn+fyuI/eg==:117 a=azHRBMxVc17uSn+fyuI/eg==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=67y7PdGttH7DD6AgI8EA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTI1MDE1MyBTYWx0ZWRfX9oMhjyYCbRfH SVyIBplsGl3G/r6bWvuKFXx8sq+EpQI13Bpvclv7Wet/3GcViasapcyuIAQ+AEVVIhssSietzx4 xUffNfWIIo9UIjU7wwPW7CJOl9ls7Co1Np67i3XVWQsIjedSzJbPhpltkq1YkdG0JSeJ88aqkRR Jcc7hsVq0QBB+KO8DKMHr1+OVkYXaqP+WBE+h0Exw+/CJmNQwzpyOiAw34zTK5pRD330P7HI8M9 hKdMSTHpcRGCWUwQnPylyo8Uhm+/xXlUcNPwRyYLHyq5trvNb0mxgnBoeIgvI+8dR0Z6pNCkU26 qTWmToWJ4Udmecdb2ucelNnjgffDU3L7+7GbDBfzBXDZCB6sZLrnRiFMUyfWMQ= The activate_locked label in shrink_folio_list() reclaims swap cache when needed, marks the folio active, and updates activation statistics. Extract this block into folio_activate_locked() so it can be reused. No functional change. Signed-off-by: Zhang Peng Suggested-by: Kairui Song --- mm/vmscan.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index ca4533eba701..886d8b4843aa 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1050,6 +1050,28 @@ static bool may_enter_fs(struct folio *folio, gfp_t = gfp_mask) return !data_race(folio_swap_flags(folio) & SWP_FS_OPS); } =20 +/* + * Prepare a locked folio to be kept active rather than reclaimed. + * Reclaims its swap slot if it will not be swapped, then marks it + * active and updates activation statistics. + */ +static void folio_activate_locked(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); + } +} + /* * shrink_folio_list() returns the number of reclaimed pages */ @@ -1525,17 +1547,7 @@ static unsigned int shrink_folio_list(struct list_he= ad *folio_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_activate_locked(folio, stat, nr_pages); keep_locked: folio_unlock(folio); keep: --=20 2.43.7 From nobody Mon Jun 8 23:56:53 2026 Received: from outbound.pv.icloud.com (pv-2002c-snip4-1.eps.apple.com [57.103.64.232]) (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 BA6A0387566 for ; Mon, 25 May 2026 14:57:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.64.232 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721068; cv=none; b=Q8UeVegEtvHPUMf+GGALTBgHG3FF3KMvW0+GHUkS4DE07mQykqGk68DUBA/wJyRft2oDPGdMSE8SDo91+NMpReeoHmbmbhzZZPh04/h9LIdIwuugxBYdBRNzL5Pa29Mz1WmYrY8MAK2TbFYJ3XOM/pb7/q8GRsZqrjAiwHDADi4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721068; c=relaxed/simple; bh=kd9gqAtp/kJZkK6P5AqapN72rGvPeTsc9D8R3n39rXs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VVx43YDhYxhwpp/XVUHU7WIxZtZPzTJDFYBZdeRxRPaB9jqMyRn55hEwmhgx+5w+e7+vUhES+6hZcQeNO/DQVJz66mVGSearRwm9rcM6Ui/CzOX6U8YM0+qP2vzX40DtUXhR7txXfiHqvdQM8Vj60V/fKVLMZ1EK9bRjWUb5KZ4= 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=QQ3lp85W; arc=none smtp.client-ip=57.103.64.232 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="QQ3lp85W" Received: from outbound.pv.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPS id 3080818000BB; Mon, 25 May 2026 14:57:45 +0000 (UTC) X-ICL-Out-Info: HUtFAUMEWwJACUgBTUQeDx5WFlZNRAJCTQhMHV8FRQNBF0kFWBcOVk1DEUMdUhlfH1cTVhR3AlEcVg1XQ1QEX1BfHA4EVAddBV1WUAJaS0ATBEoDTV8OXh8EF0YZVQRHHl1WQxsZAlEcVg1XQ1QEX1BJDEFQbFoARxdIHV0ZWW9QXRwOBFQHXQVdVlACWktfGV1FD18HWQRADEoGQFUKRhNRVUcBVUZUHEwLW0BBXx9CFEAAWg9SVkZYGlBdBytbE1UXRgkZCF0dB1hHFEcODxlaFFwYUw== Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1779721066; x=1782313066; bh=chMMFqiAX+0eAksNcyz+zUnXu+Fqgx3Fcq9dvFyHmY4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=QQ3lp85WY53uuN6kbUZGFmk6IxqDMTgzDK7YAS/YZRsvywM8kpAXoWK9w+mdn3exgZwfA/gXfi0XOtKVYA1MnJU92SoGNUpHI5PslxNYyXU0C8HHfQkB6GKEiC4AOenqpmbrQeF23Z214mg7KES84H58tVFkqTRHIV9k5QlBu+HOWoMopMG2c3cKP4G20BnYJ4xH1OEJPz9cJSWiORerLR/hKsDioahhZv/9KtIMbqTki11WkQKOIEQBDuHFCS7g/yFJczJ5GYlliAD3fo5bOSKJqM1LmOYWrL9cvqlDRkO2lb5BEMbVNtyIxME8KvoAtLolvSffwRvdL4lPN+KyAA== Received: from [21.6.122.162] (unknown [17.56.9.36]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPSA id E887918000DB; Mon, 25 May 2026 14:57:38 +0000 (UTC) From: Zhang Peng Date: Mon, 25 May 2026 22:57:18 +0800 Subject: [PATCH v4 2/5] mm/vmscan: extract folio_free() from shrink_folio_list() 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: <20260525-batch-tlb-flush-v4-2-83789d6abc00@icloud.com> References: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> In-Reply-To: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko , "Liam R. Howlett" , Qi Zheng Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779721049; l=6819; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=kd9gqAtp/kJZkK6P5AqapN72rGvPeTsc9D8R3n39rXs=; b=+LpkzWE9ldHr1mVXISTt16iZ0PMNxzd8bT9XBIfGZ5f89akwrd71EcrdSCXGSF1GCKezxqjhs 7mkpPWUywttCuwrhk2dPQx+IMlmK44qDC34rsHuAqcP23/LwYYldhoo X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Authority-Info-Out: v=2.4 cv=TNVIilla c=1 sm=1 tr=0 ts=6a146369 cx=c_apl:c_pps:t_out a=azHRBMxVc17uSn+fyuI/eg==:117 a=azHRBMxVc17uSn+fyuI/eg==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=Zr1499PEZckTeUg_TBoA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-GUID: u0GiH-pfi0-xcLmt_2s3b48XfcL4rHnc X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTI1MDE1MyBTYWx0ZWRfX12gJl+clwFBM zMk8+uH5IscvOa+J/i415R784OESsJ9JDpm95Cw+4s0PEOcR9EsC0cD1/UzWa3sHzSIdzP8I0RN gBw8G5AaZMTHXJgdFTv7PlVfenkPx+Pv1ELdhU4M4SaM2tTVbZsXsdFXDuc3O537HYnVpKpenW/ nn55SVapPB+Ea2+mary8i/+GcGG9kEQW8iMpIKw1/jSe6xqOmJ0Th75IFKc0N7SJoiujcYrUwi1 /nffsSOuDB+VCap8FJcxrOt44uawRpi8l0pqKRYeL+/MkPtPMcpikqS1e8Slm40YRM9FsKvdXsw aASumbr5qbD/LNg1L3s5y/XRfOEEvaMY8j1TnynJ5UU8i4yjSuBZAk/Hqk1HXk= X-Proofpoint-ORIG-GUID: u0GiH-pfi0-xcLmt_2s3b48XfcL4rHnc shrink_folio_list() contains a self-contained folio-freeing section: buffer release, lazyfree, __remove_mapping, and folio_batch drain. Extract it into folio_free() to reduce the size of shrink_folio_list() and make the freeing step independently readable. No functional change. Signed-off-by: Zhang Peng Suggested-by: Kairui Song --- mm/vmscan.c | 168 +++++++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 92 insertions(+), 76 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 886d8b4843aa..b31f67801836 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1072,6 +1072,95 @@ static void folio_activate_locked(struct folio *foli= o, } } =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_reclaimed) +{ + 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_activate_locked(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). + */ + *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. + */ + *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; +} + /* * shrink_folio_list() returns the number of reclaimed pages */ @@ -1459,83 +1548,10 @@ static unsigned int shrink_folio_list(struct list_h= ead *folio_list, } } =20 - /* - * 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). - */ - nr_reclaimed +=3D nr_pages; - continue; - } - } - } - - 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, &nr_reclaimed)) goto keep_locked; - - folio_unlock(folio); -free_it: - /* - * Folio may get swapped out as a whole, need to account - * all pages in it. - */ - 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: /* --=20 2.43.7 From nobody Mon Jun 8 23:56:53 2026 Received: from outbound.pv.icloud.com (pv-2002k-snip4-7.eps.apple.com [57.103.64.158]) (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 12A59387566 for ; Mon, 25 May 2026 14:57:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.64.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721075; cv=none; b=NkFiN+JhcVIEdegp4LxtLJ5xYnhCIOfz8PwVvvX5QhDUAc7FNSoHtHB/N8nmo/VF8XWiQRXKXMWBsAhNnGXlnSsbqHR5j7b35bShaMPGPxXb7+vZJ5WR/79ar3mXbDw/FTk/cMzkQrq8aB/hN6hKpcoY1TRUKObsBTnvLVObSJY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721075; c=relaxed/simple; bh=HlCDCjaEuUAcWJgatcyXKXkF6++iV8Wx6KA0xfRDRhs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=k//+OQFZKtvxSg8r1wXXYNHB30guBZFh+blEu4fjvF5gNS7/x9vhF4RznWRzOq5jWptvQMt2Rs4eALsL1+nRPBdfDTUV7K0DvZv2tKrvSBZXiZNTPP+9EGwpNNGSi18leDXiy2Wz+uKvaiPbXVxRVOTY+wqKBrnxsKJIWoctVDk= 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=O+Et232F; arc=none smtp.client-ip=57.103.64.158 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="O+Et232F" Received: from outbound.pv.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPS id 0F00A1800123; Mon, 25 May 2026 14:57:48 +0000 (UTC) X-ICL-Out-Info: HUtFAUMEWwJACUgBTUQeDx5WFlZNRAJCTQhMHV8FRQNBF0kFWBcOVk1DEUMdUhlfH1cTVhR3AlEcVg1XQ1QEX1BfHA4EVAddBV1WUAJaS0ATBEoDTV8OXh8EF0YZVQRHHl1WQxsZAlEcVg1XQ1QEX1BJDEFQbFoARxdIHV0ZWW9QXRwOBFQHXQVdVlACWktfGV1FD18HWQRADEoGQFUKRhNRVUcBVUZUHEwLW0BBXx9DFEAAWg9SVkZYGlBdBytbE1UXRgkZCF0dB1hHFEcODxlaFFwYUw== Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1779721073; x=1782313073; bh=TbidOf+0bR1HgAdWvO64+fM8hMyQOeB8vX0q44hvsZ0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=O+Et232FeSvG9SPqZpwqGP5l1oV6L2Wk9GHBxcCRSeoGcrcUc5i7qR9TSDA4ddVwJ6+PFjZb28YqwGtwv4rv1HT49LeXjefXin6r1e780KU2Y4nlCkmvPooQCJjC7yZo2+F4T+EPR5hFCQXuzzBw63qKZ+PUoaYrSrkUPVuElhbfdUOBVl3eaXNeKShoC25Ur4o5Azpuf79lzMibpe9XeHQ/YLTjvli2dxsa7wiJirGEFLsXzVaywRAB6v7oxhVocjhKJZFhdde52xdkfNiXhcrOm6TodQ0ymtW7tTfH0bL1Z9k+y1UUt8pGBrZOrO3Mtz/fqgXCP79p8BybTtuUSA== Received: from [21.6.122.162] (unknown [17.56.9.36]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPSA id 777C41800127; Mon, 25 May 2026 14:57:43 +0000 (UTC) From: Zhang Peng Date: Mon, 25 May 2026 22:57:19 +0800 Subject: [PATCH v4 3/5] mm/vmscan: extract pageout_one() from shrink_folio_list() 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: <20260525-batch-tlb-flush-v4-3-83789d6abc00@icloud.com> References: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> In-Reply-To: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko , "Liam R. Howlett" , Qi Zheng Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779721049; l=4366; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=HlCDCjaEuUAcWJgatcyXKXkF6++iV8Wx6KA0xfRDRhs=; b=Uw+ORlKctNuuGP14pbAYK206TM9iJu+9oCMuARY2+Z7D10MVwgGaeBd1zSXLUxlWxK3A3EB49 o0Sbb0yPGDwAeHKWYcuF+IigkB9NzVfY5shrkIF7why2DVBPWPcPXfP X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-ORIG-GUID: 35a--qBcikftPTJwDqqOP0OrrlzpSt02 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTI1MDE1MyBTYWx0ZWRfX5P0N+GhVwVxW d0vE8rWeJ4yTK1X2Uz5/2tl9cC9YQ8ZUysZHSBD/cHqvv2Y7KZDLjcMSzAnRJChUf+GblQhGzLF 9lmBGRRL6A6IhqPoiz9RKbdyhDIL62RcbYIq/nOXcG0BgCSCzA+yKEeK9HkKLIewgFs2gLjvtga EuL/bdl46yYkYTzR/8xlm31pq6d2dwYO5xJwL/0Sh+hQU8Hm1c8UxlvxG9XlTaJ50FCx2Uz+ks9 J0kHZsbEfeRaF7WKLQlZel83EzWjbB9BNGzzRQnaR6IYwCDz5oXPxCqUsvHrk86CIAJMSP5JtUg esp4sWC5nVThaOoiZxKwFdp//5NxX+OcHW41sV/Ep9Unfl0b8heGY7Rc842SDo= X-Authority-Info-Out: v=2.4 cv=N7Yk1m9B c=1 sm=1 tr=0 ts=6a14636e cx=c_apl:c_pps:t_out a=azHRBMxVc17uSn+fyuI/eg==:117 a=azHRBMxVc17uSn+fyuI/eg==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=LeMCXtsDaY_ey64dPfEA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-GUID: 35a--qBcikftPTJwDqqOP0OrrlzpSt02 shrink_folio_list() contains a self-contained pageout() dispatch state machine. Extract it into pageout_one() to reduce the size of shrink_folio_list() and make the pageout step independently readable. No functional change. Signed-off-by: Zhang Peng Suggested-by: Kairui Song --- mm/vmscan.c | 107 ++++++++++++++++++++++++++++++++++++--------------------= ---- 1 file changed, 65 insertions(+), 42 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index b31f67801836..456d38eb172c 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1161,8 +1161,68 @@ static bool folio_free(struct folio *folio, struct f= olio_batch *free_folios, return true; } =20 +static bool pageout_one(struct folio *folio, + struct folio_batch *free_folios, + struct scan_control *sc, struct reclaim_stat *stat, + struct swap_iocb **plug, struct list_head *folio_list, + unsigned int *nr_reclaimed) +{ + 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_activate_locked(folio, stat, nr_pages); + folio_unlock(folio); + return false; + case PAGE_KEEP: + folio_unlock(folio); + return false; + 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)) + return false; + if (folio_test_dirty(folio)) + return false; + + /* + * A synchronous write - probably a ramdisk. Go + * ahead and try to reclaim the folio. + */ + if (!folio_trylock(folio)) + return false; + if (folio_test_dirty(folio) || + folio_test_writeback(folio)) { + folio_unlock(folio); + return false; + } + mapping =3D folio_mapping(folio); + fallthrough; + case PAGE_CLEAN: + ; /* try to free the folio below */ + } + if (folio_free(folio, free_folios, sc, stat, nr_reclaimed)) + return true; + folio_unlock(folio); + return false; +} + /* - * shrink_folio_list() returns the number of reclaimed pages + * Reclaimed folios are counted in the return value. */ static unsigned int shrink_folio_list(struct list_head *folio_list, struct pglist_data *pgdat, struct scan_control *sc, @@ -1499,53 +1559,16 @@ static unsigned int shrink_folio_list(struct list_h= ead *folio_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. */ 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 (!pageout_one(folio, &free_folios, sc, stat, &plug, + folio_list, &nr_reclaimed)) + goto keep; + continue; } =20 if (!folio_free(folio, &free_folios, sc, stat, &nr_reclaimed)) --=20 2.43.7 From nobody Mon Jun 8 23:56:53 2026 Received: from outbound.pv.icloud.com (pv-2002l-snip4-11.eps.apple.com [57.103.64.171]) (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 2C48238423D for ; Mon, 25 May 2026 14:58:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.64.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721083; cv=none; b=tzNOWa/AXEiimqamJ18yNRWxAljihXAF2kbm4fNbfkWm8LVXugAm6+GHg2W8qhBB8ZDb+eT05se0+/P1KsnAMU28bH9epnkzQGbFMpGShk4x8nbKGJcR9M+qbkKS+tWLVxcE7+AU6Cx0rxrMoMZyfBWa1a+UcwNikAGHfTgaPMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721083; c=relaxed/simple; bh=9uk3LS+tzE9ezvquZlv0cIiXReVHWUTgs5lv55QvXHQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aM+2Oxh9P0OJLhgm9F7YOJdPUlKKTyBMA9M+M5pRwSq8eG3rXTsm1xeNBYZ6klwlJW1BmX+slyAgmgmSnY/bEhjwc66O8GFyOzrdmdlS6fIrYKv5jZCEIEhL/2x3LLsinovkWweduM8XpmweUSThiYpsCVQD0i/b+b0AEIV0Tj0= 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=AcqvTFCi; arc=none smtp.client-ip=57.103.64.171 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="AcqvTFCi" Received: from outbound.pv.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPS id 4B99618000CA; Mon, 25 May 2026 14:57:56 +0000 (UTC) X-ICL-Out-Info: HUtFAUMEWwJACUgBTUQeDx5WFlZNRAJCTQhMHV8FRQNBF0kFWBcOVk1DEUMdUhlfH1cTVhR3AlEcVg1XQ1QEX1BfHA4EVAddBV1WUAJaS0ATBEoDTV8OXh8EF0YZVQRHHl1WQxsZAlEcVg1XQ1QEX1BJDEFQbFoARxdIHV0ZWW9QXRwOBFQHXQVdVlACWktfGV1FD18HWQRADEoGQFUKRhNRVUcBVUZUHEwLW0BBXx9EFEAAWg9SVkZYGlBdBytbE1UXRgkZCF0dB1hHFEcODxlaFFwYUw== Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1779721081; x=1782313081; bh=44yuq/wuzHQyeR1qrNWG3c1aoPjDydahfdtIBLgV6K0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=AcqvTFCi4RwdLMbQVXoXo7fxFim0i48H4zO0ujJqNM2CsOWaoZDMBB6+PPcxWQO8YeAZEaYLDjPk6I7LmW22vsGJ3DebtqgghVkfpBBUWS1oW97rZQduOzJwNJHza9YRpcamrKvxn7PaSRjN8sSKuCCkDeieb98ZlbtgAvhMQpwam6LG7gJUXYxi5c1ZpFayspy7DoLKYom0EuQIKqBMvioM+KlEfx12+lPIbj2vkLCMcY0UUOFi7ccpicZX6kP3FMqpBbxXQ6M6r0SXnozTpt5JyVyPKAyS7KYRi5any914FNIs7d9nqTH73F3YOEAmEAdVh44w/Ky3aJ/M+vbdxw== Received: from [21.6.122.162] (unknown [17.56.9.36]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPSA id 3898218000B1; Mon, 25 May 2026 14:57:49 +0000 (UTC) From: Zhang Peng Date: Mon, 25 May 2026 22:57:20 +0800 Subject: [PATCH v4 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: <20260525-batch-tlb-flush-v4-4-83789d6abc00@icloud.com> References: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> In-Reply-To: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko , "Liam R. Howlett" , Qi Zheng Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779721049; l=3701; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=9uk3LS+tzE9ezvquZlv0cIiXReVHWUTgs5lv55QvXHQ=; b=DAi8J/M4yWtspca16GxLFVZqd0brWJgHWGdZx4/pALgJ4+AiYI7i52SaGQCt0J5C0rdSp+L73 pzrFgyuoE31DTB5si0WXUXiyLF+y3NoS6jUW0qiTHBpkg2pfcgeml1L X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-GUID: wGvB_WRPB1zvlL9m_0EKbp0KOah48qvF X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTI1MDE1MyBTYWx0ZWRfX1SEJVbjACY+i 6D/Yrx5mWnD5vABx0L09n2vG40fqWbla9joKWtzsQsBLnzydYyOjtSBI2w1wf8ioJs1NXwYPV6u ZVm5+fEBfcHBKRwxEy8FfqwMQ8nTWhV0ziGeFkGNLveabiHDPpqHR7pDkqn8/4eAKsXqdt7RXwE 5o1UJ8duXWdHkMckPpp7YT882AqRlhDw/m1zP9Hnl1rc+YH1FjO+xkV00DwMcC7tR3dtp8jPuzE V7I23QXDypflToj4K9i9/itNAxWNfrwaMf1zBRefPMLqt04bdgeRVbBxGSCngrfGPqmrijdcOOd JGFoTrQMpFcTm5B6ITuMN2sUqax8hxz64FQgtuwXe/rTlGKOvTwyGLtBmI7q0I= X-Authority-Info-Out: v=2.4 cv=EavFgfmC c=1 sm=1 tr=0 ts=6a146377 cx=c_apl:c_pps:t_out a=azHRBMxVc17uSn+fyuI/eg==:117 a=azHRBMxVc17uSn+fyuI/eg==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=oO7xqvO4U2nGjC6lxGQA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-ORIG-GUID: wGvB_WRPB1zvlL9m_0EKbp0KOah48qvF 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. folio_try_unmap() is only called when the folio is actually mapped; the !folio_mapped() check stays in the caller, keeping the function's semantics clear: it tries to unmap a mapped folio and returns whether the unmap succeeded. No functional change. Signed-off-by: Zhang Peng Suggested-by: Kairui Song --- mm/vmscan.c | 68 ++++++++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 456d38eb172c..abf3a2878456 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1221,6 +1221,41 @@ static bool pageout_one(struct folio *folio, return false; } =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; + + 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 the return value. */ @@ -1495,36 +1530,9 @@ static unsigned int shrink_folio_list(struct list_he= ad *folio_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_mapped(folio) && + !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 Jun 8 23:56:53 2026 Received: from outbound.pv.icloud.com (pv-2002h-snip4-2.eps.apple.com [57.103.64.203]) (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 EAC6F3F20E4 for ; Mon, 25 May 2026 14:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.64.203 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721086; cv=none; b=O7mb801jnDR5xru+FxQ56b71649vD0LitTyCoxu+3v2xbGVG+vmaCpZw2EQ9M7J6KcI3ZmXHrK8oAdpIi7a9zR2AzR+V3Bx4pCvLXTRqotUWKNmfYIhQxCwT3cObKYUrYtzAwrx4Dt48MtlF5GZ1LJLUP2SxzAiqIejcqTLFHzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779721086; c=relaxed/simple; bh=FWLnZtBsgkDD4unwqq+UsbASxXu9ry+CTqe6VRI1hwc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=t+AMEoTjVL3QFlbS8drR+iZ5PJ1dV5iPQpW+QiZ8e3Bc7xyaGrKV0iSESM+HAiV/4bWLgt8+AIhigKVzeEEuTJ0Ghm7xpBh7VeeeflVGhZm0765I4Ilo5I5DTjptLr+v8I/ibLHWq3HodR2X5sRWjzjenhjqjuFfvNxMrLUuqTE= 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=b0syjqHK; arc=none smtp.client-ip=57.103.64.203 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="b0syjqHK" Received: from outbound.pv.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPS id 1F8711800108; Mon, 25 May 2026 14:57:59 +0000 (UTC) X-ICL-Out-Info: HUtFAUMEWwJACUgBTUQeDx5WFlZNRAJCTQhMHV8FRQNBF0kFWBcOVk1DEUMdUhlfH1cTVhR3AlEcVg1XQ1QEX1BfHA4EVAddBV1WUAJaS0ATBEoDTV8OXh8EF0YZVQRHHl1WQxsZAlEcVg1XQ1QEX1BJDEFQbFoARxdIHV0ZWW9QXRwOBFQHXQVdVlACWktfGV1FD18HWQRADEoGQFUKRhNRVUcBVUZUHEwLW0BBXx9FFEAAWg9SVkZYGlBdBytbE1UXRgkZCF0dB1hHFEcODxlaFFwYUw== Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1779721084; x=1782313084; bh=glSRZB7qYzyzP+WE+xY0AvPw2Fx+WgnlquMKakDxzgY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=b0syjqHKe5m8sEa5mjo9gzrLwyj/1hYew5dL7+TxH4KJPOqfdQP68abcQUsl5RiE24Bxyp5UN2ERcCi+jYl2+Udp4VhRyXtC0TV99vaGs9uvdsKbXnS1nDVgDUbqlKGcnpv3qlpuMLQ4JOMCiWtyvIYSHb2c9ROPj//6yxzIwXrwmVnLhks/1pUasKA53vI+5I7s9iR1Tr7vR6AKxd806jL4omb/nxwhQWkyWJ+svm5S4ejhgtsHiy+Mv2Rh34c/h9P9dm93ojWd/xtHnYsuVOYnDZVfYZFFFMm2NTBpzLMSmL271CYeHTKr3oqnnOAgiBrSQuo01S70JimNHEcTog== Received: from [21.6.122.162] (unknown [17.56.9.36]) by p00-icloudmta-asmtp-us-west-1a-60-percent-11 (Postfix) with ESMTPSA id B38E61800106; Mon, 25 May 2026 14:57:54 +0000 (UTC) From: Zhang Peng Date: Mon, 25 May 2026 22:57:21 +0800 Subject: [PATCH v4 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: <20260525-batch-tlb-flush-v4-5-83789d6abc00@icloud.com> References: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> In-Reply-To: <20260525-batch-tlb-flush-v4-0-83789d6abc00@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko , "Liam R. Howlett" , Qi Zheng Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779721049; l=4660; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=FWLnZtBsgkDD4unwqq+UsbASxXu9ry+CTqe6VRI1hwc=; b=V81iD8yRixX0JkLjM5uHOGjjzBjRYWt0MW58Oo6j+EDjPE3Zh2mkGCpfaDeUWSucJScR09KDm CI20VgSvN1KCySr1FBIKv48L4vo7Y4Url2xwZBuk39zJVGiFBr5t1SG X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Authority-Info-Out: v=2.4 cv=eb8wvrEH c=1 sm=1 tr=0 ts=6a146379 cx=c_apl:c_pps:t_out a=azHRBMxVc17uSn+fyuI/eg==:117 a=azHRBMxVc17uSn+fyuI/eg==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=f5ToH5UDdWaC3ggAQ9UA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-GUID: V00D_MVgD7_hmVZGgeYId9jhY3GqtA3o X-Proofpoint-ORIG-GUID: V00D_MVgD7_hmVZGgeYId9jhY3GqtA3o X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTI1MDE1MyBTYWx0ZWRfX035GMCzTCufn 9g8hdCd4ce9bLyLERu1Rvso25NfOoharizfqG9dhThUN6CHfal6DhYOANtU0CkbJ50egaTfxxrt aHJkDI/kAfHkqdT3y42NxaVzPIYnM7OfP2RjYZ/nEkKgSHeO6Z40TsdTD6Czfelso5YK4zz3XTg rbCHH96ciGktIHV8CMVK0IKUbTjZ9sIMJQgZ2LfzLop9ixQdG5etTMuzjdMNqYzafK1v90PHWiV +Dck+hnIcCGAoHDr6eu0DmndK2K49mWNMJz9Cb6GoL2UYiWf8nsBWA21JnRQejAjW2oB1KfGbbe K25Tj+1mYNruPDiex+aBcT8dmljbzSXyXyXdBIUeax0S6E8oO0HFU+uXaPslUU= 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 Signed-off-by: Zhang Peng Suggested-by: Kairui Song --- mm/vmscan.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index abf3a2878456..c0d22afe67a5 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1221,6 +1221,57 @@ static bool pageout_one(struct folio *folio, return false; } =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, + unsigned int *nr_reclaimed) +{ + 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]; + if (!pageout_one(folio, free_folios, sc, stat, plug, + folio_list, nr_reclaimed)) + list_add(&folio->lru, ret_folios); + } + /* 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) { @@ -1265,6 +1316,7 @@ static unsigned int shrink_folio_list(struct list_hea= d *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_reclaimed =3D 0, nr_demoted =3D 0; @@ -1273,6 +1325,7 @@ static unsigned int shrink_folio_list(struct list_hea= d *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); @@ -1568,15 +1621,19 @@ static unsigned int shrink_folio_list(struct list_h= ead *folio_list, 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(); - if (!pageout_one(folio, &free_folios, sc, stat, &plug, - folio_list, &nr_reclaimed)) - goto keep; - continue; + folio_unlock(folio); + if (!folio_batch_add(&flush_folios, folio)) + pageout_batch(&flush_folios, + &ret_folios, &free_folios, + sc, stat, &plug, + folio_list, &nr_reclaimed); + goto next; } =20 if (!folio_free(folio, &free_folios, sc, stat, &nr_reclaimed)) @@ -1601,6 +1658,12 @@ static unsigned int shrink_folio_list(struct list_he= ad *folio_list, list_add(&folio->lru, &ret_folios); VM_BUG_ON_FOLIO(folio_test_lru(folio) || folio_test_unevictable(folio), folio); +next: + continue; + } + if (folio_batch_count(&flush_folios)) { + pageout_batch(&flush_folios, &ret_folios, &free_folios, sc, + stat, &plug, folio_list, &nr_reclaimed); } /* 'folio_list' is always empty here */ =20 --=20 2.43.7