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