From nobody Mon Apr 13 07:12:25 2026 Received: from outbound.st.icloud.com (p-east2-cluster5-host11-snip4-10.eps.apple.com [57.103.79.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9DD3227BB9 for ; Fri, 10 Apr 2026 12:44:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.79.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825087; cv=none; b=EdYgpWlYtM3QAwrAxRzIT2gG4syqYj3narhdIShR+UA05pYwoXiSbEBs3whZBxvQKIZjQ/B5ok2GNrHSXNe9ARsIGilEhcNt9vZ/EIuKtEC4YPWtnQsiSCLGqfeaxfQl5xLeOxBF2eNSoaoRRU3NdQWnSCmc2rmNGF/hdAoIPcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775825087; c=relaxed/simple; bh=aPPkFbpAyCahB7a9WkbN0clOTcDNuxZoSKkxVQFPl4c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=E5lS+1Lj5DMCc6L9fezyrgvVj8efp/vAmY5yIJ4KTvZ9/VqZN/HH4wgz2EsN1HFb0e7IVkcjP8qvhJG4WQNUuJ08CYhYXVad5K/xaa5VWSUsF3ypsZsWYGSefgVnbmzUvuK2raN4MbESgRByVCs63slm2iTRNiY+f9p73q8pK0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=fdstTdwU; arc=none smtp.client-ip=57.103.79.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="fdstTdwU" Received: from outbound.st.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPS id AB8DD18001BC; Fri, 10 Apr 2026 12:44:41 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1775825084; x=1778417084; bh=qmoarnu2fjz9QcThGpQzSg55OCBVK8r4IXQdeVfdm94=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=fdstTdwUGP6oeoUNcImHxlaPOWSy2m8Md9l2vIg8ASn4eb+f5+4MIvRK0L47tyzltg/3qVmQLIQJ5aJtCFbp7DpzJjal3qR8sZFjs6zVkkVgkK22JsCUGJnQzCWTQNQufOoynnrV4w8qtglgSGm6/u4nqb2dTFGYEnAhqjQ4IxbK6PeAq2bkoFpNmJ1SlehiAD1WQrgOM5fxI2gILCsJ6WJLlu0DhanWnBzQJKGjCGNv6ZmgWCtZmp49PNVzYRWnrv3mOEh3bVmUMDWnlLNLGB2DBzmNtOx14ALlas/wRpKBXTOjD/NOY75NF5vrDQ18XuPRu4dfK+WQsqiSBaHCUg== Received: from [21.6.122.162] (unknown [17.42.251.67]) by p00-icloudmta-asmtp-us-east-1a-100-percent-0 (Postfix) with ESMTPSA id 4B5071800371; Fri, 10 Apr 2026 12:44:35 +0000 (UTC) From: Zhang Peng Date: Fri, 10 Apr 2026 20:44:03 +0800 Subject: [PATCH v3 5/5] mm/vmscan: flush TLB for every 31 folios evictions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260410-batch-tlb-flush-v3-5-ff0b9d3a351a@icloud.com> References: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> In-Reply-To: <20260410-batch-tlb-flush-v3-0-ff0b9d3a351a@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775825044; l=4434; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=IgHhHeddVtpb8e5HB29Ud1Cb4p7uaarC/SsI4SxrvPk=; b=coi4Y6VtXZfeKDBO8TUliE567FShmsMAgOmtlebee0aTx5oghQA/KKOtOzsvTdKWkc7SH58p7 OndkCNYXkKtDYcJQ+XPzEE9N0COL+RlZ/TGpE6NhFmcdZpq3T5L/0AT X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Authority-Info-Out: v=2.4 cv=GOwF0+NK c=1 sm=1 tr=0 ts=69d8f0bb cx=c_apl:c_pps:t_out a=YrL12D//S6tul8v/L+6tKg==:117 a=YrL12D//S6tul8v/L+6tKg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=x7bEGLp0ZPQA:10 a=YE32fvk_ji8A:10 a=VkNPw1HP01LnGYTKEx00:22 a=GvQkQWPkAAAA:8 a=6QAaJVoE38gftqD1B9gA:9 a=QEXdDO2ut3YA:10 a=J82S1U87d15UFHHUFZS8:22 a=R6HnoDE5lksTgPyyjrIf:22 X-Proofpoint-ORIG-GUID: Z71cGpqHke44Ul61rUpscVntrnPPnNZ8 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDEwMDExOCBTYWx0ZWRfX05YzfSkSvbtj 0IAzfoVHAhGIrDpFkaPrXZKkKPeZKagS4oobIsTib6TkHTyo3U3E6OuPzoTBiummaKDMO4upczb lG9RTsPgHrNJnBaS/CuQV/rmz4OEG9UzIIvMvfU7jLjsgdratuR0N2YzbErmDCIvm5lsnanTWP3 sJNvI2+6+K1VymDlxmFThvxC5AstkN0nG0znciL4g/401HHKVIJLQuqcX0Jf4cVBEtN0X1yjIOU Jl1nCCGFnzamaIeMSObv107r6HkOR59raSlliQ7ksFRCwmtjTJYgZI377EiLHO27CwaJYTZjqqJ SjdG3M4JV6PIVMqUGJjZlpnnZHo30iLJSJHqRH92QkSvH/jEAl4ofC/+ORnS9w= X-Proofpoint-GUID: Z71cGpqHke44Ul61rUpscVntrnPPnNZ8 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-10_03,2026-04-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 clxscore=1015 spamscore=0 suspectscore=0 lowpriorityscore=0 malwarescore=0 bulkscore=0 adultscore=0 mlxscore=0 mlxlogscore=505 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2604100118 From: Zhang Peng Currently we flush TLB for every dirty folio, which is a bottleneck for systems with many cores as this causes heavy IPI usage. So instead, batch the folios, and flush once for every 31 folios (one folio_batch). These folios will be held in a folio_batch releasing their lock, then when folio_batch is full, do following steps: - For each folio: lock - check still evictable (writeback, mapped, dma_pinned) - If no longer evictable, put back to LRU - Flush TLB once for the batch - Pageout the folios Suggested-by: Kairui Song Signed-off-by: Zhang Peng --- mm/vmscan.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--= ---- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 63cc88c875e8..8d18e1c5b53d 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1217,6 +1217,55 @@ static void pageout_one(struct folio *folio, struct = list_head *ret_folios, folio_test_unevictable(folio), folio); } =20 +static void pageout_batch(struct folio_batch *fbatch, + struct list_head *ret_folios, + struct folio_batch *free_folios, + struct scan_control *sc, struct reclaim_stat *stat, + struct swap_iocb **plug, struct list_head *folio_list) +{ + int i, count =3D folio_batch_count(fbatch); + struct folio *folio; + + /* + * Reuse fbatch in-place: reinit only clears the count, the + * underlying folios array is still accessible via saved count. + * Filter and re-add valid folios back into the same batch. + */ + folio_batch_reinit(fbatch); + for (i =3D 0; i < count; ++i) { + folio =3D fbatch->folios[i]; + if (!folio_trylock(folio)) { + list_add(&folio->lru, ret_folios); + continue; + } + + VM_WARN_ON_FOLIO(folio_test_lru(folio), folio); + + if (folio_test_writeback(folio) || folio_mapped(folio) || + folio_maybe_dma_pinned(folio)) { + folio_unlock(folio); + list_add(&folio->lru, ret_folios); + continue; + } + + folio_batch_add(fbatch, folio); + } + + i =3D 0; + count =3D folio_batch_count(fbatch); + if (!count) + return; + /* One TLB flush for the batch */ + try_to_unmap_flush_dirty(); + for (i =3D 0; i < count; ++i) { + folio =3D fbatch->folios[i]; + pageout_one(folio, ret_folios, free_folios, sc, stat, plug, + folio_list); + } + /* Clear the batch for the caller's next use */ + folio_batch_reinit(fbatch); +} + static bool folio_try_unmap(struct folio *folio, struct reclaim_stat *stat, unsigned int nr_pages) { @@ -1264,6 +1313,8 @@ static void shrink_folio_list(struct list_head *folio= _list, struct mem_cgroup *memcg) { struct folio_batch free_folios; + struct folio_batch flush_folios; + LIST_HEAD(ret_folios); LIST_HEAD(demote_folios); unsigned int nr_demoted =3D 0; @@ -1272,6 +1323,8 @@ static void shrink_folio_list(struct list_head *folio= _list, struct swap_iocb *plug =3D NULL; =20 folio_batch_init(&free_folios); + folio_batch_init(&flush_folios); + memset(stat, 0, sizeof(*stat)); cond_resched(); do_demote_pass =3D can_demote(pgdat->node_id, sc, memcg); @@ -1565,15 +1618,19 @@ static void shrink_folio_list(struct list_head *fol= io_list, goto keep_locked; if (!sc->may_writepage) goto keep_locked; - /* - * Folio is dirty. Flush the TLB if a writable entry - * potentially exists to avoid CPU writes after I/O - * starts and then write it out here. + * Unlock while batching: holding the lock until the + * batch fills would stall swap faults that find this + * folio via swap cache lookup. pageout_batch() will + * relock each folio and recheck its state before + * writing it out. */ - try_to_unmap_flush_dirty(); - pageout_one(folio, &ret_folios, &free_folios, sc, stat, - &plug, folio_list); + folio_unlock(folio); + if (!folio_batch_add(&flush_folios, folio)) + pageout_batch(&flush_folios, + &ret_folios, &free_folios, + sc, stat, &plug, + folio_list); goto next; } =20 @@ -1603,6 +1660,10 @@ static void shrink_folio_list(struct list_head *foli= o_list, next: continue; } + if (folio_batch_count(&flush_folios)) { + pageout_batch(&flush_folios, &ret_folios, &free_folios, sc, + stat, &plug, folio_list); + } /* 'folio_list' is always empty here */ =20 /* Migrate folios selected for demotion */ --=20 2.43.7