From mboxrd@z Thu Jan 1 00:00:00 1970 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: 7bit 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 --- 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; } +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 = 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 = 0; i < count; ++i) { + folio = 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 = 0; + count = folio_batch_count(fbatch); + if (!count) + return; + /* One TLB flush for the batch */ + try_to_unmap_flush_dirty(); + for (i = 0; i < count; ++i) { + folio = 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_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_reclaimed = 0, nr_demoted = 0; @@ -1273,6 +1325,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, struct swap_iocb *plug = NULL; folio_batch_init(&free_folios); + folio_batch_init(&flush_folios); memset(stat, 0, sizeof(*stat)); cond_resched(); do_demote_pass = can_demote(pgdat->node_id, sc, memcg); @@ -1568,15 +1621,19 @@ static unsigned int shrink_folio_list(struct list_head *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; } if (!folio_free(folio, &free_folios, sc, stat, &nr_reclaimed)) @@ -1601,6 +1658,12 @@ static unsigned int 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; + } + 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 */ -- 2.43.7