From mboxrd@z Thu Jan 1 00:00:00 1970 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: 7bit 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 --- 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; } +static bool folio_try_unmap(struct folio *folio, struct reclaim_stat *stat, + unsigned int nr_pages) +{ + enum ttu_flags flags = TTU_BATCH_FLUSH; + bool was_swapbacked; + + was_swapbacked = folio_test_swapbacked(folio); + if (folio_test_pmd_mappable(folio)) + flags |= 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 |= TTU_SYNC; + + try_to_unmap(folio, flags); + if (folio_mapped(folio)) { + stat->nr_unmap_fail += nr_pages; + if (!was_swapbacked && + folio_test_swapbacked(folio)) + stat->nr_lazyfree_fail += 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_head *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 = TTU_BATCH_FLUSH; - bool was_swapbacked = folio_test_swapbacked(folio); - - if (folio_test_pmd_mappable(folio)) - flags |= 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 |= TTU_SYNC; - - try_to_unmap(folio, flags); - if (folio_mapped(folio)) { - stat->nr_unmap_fail += nr_pages; - if (!was_swapbacked && - folio_test_swapbacked(folio)) - stat->nr_lazyfree_fail += nr_pages; - goto activate_locked; - } - } + if (folio_mapped(folio) && + !folio_try_unmap(folio, stat, nr_pages)) + goto activate_locked; /* * Folio is unmapped now so it cannot be newly pinned anymore. -- 2.43.7