From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from outbound.qs.icloud.com (qs-2006g-snip4-4.eps.apple.com [57.103.85.195]) (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 64C3A2D97BB for ; Mon, 20 Jul 2026 05:08:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.85.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784524101; cv=none; b=q2rjWX/S9V+EvZCq3chJfczN2Qui24dmAQ7lGuodOb0dSgl1HbDlE8slhK0kCTQN0J/5VaFt7Soyg6huonU8LbvrOXWDVELg0YCLdAmdsVRKd3QrF6ojAyIfHUcASUSc48t2p9E90RYneS07IrlUM/z/gvEAs5pW0UxmPF7uflw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784524101; c=relaxed/simple; bh=1wbXcA3EwOgkLmlnWmmIkzMkHOobeEr8JUOUrutpQDg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ftu3qKjwBkWn0XPTOoxEDV/UvDPZnhN7icNefhoeNXwaxoag9JClbmqEkZHV7Ua5SiKueP+BXokHLkWZcoF++FuMGkpf70gEJhyql/wCF13/Nu3UGeaJl5BaOKK1JWGqWOWwD5Ic9O5GY3FAhHzAtgUXhQdfAM4UgIqjVX5JM4k= 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=HdTIZPLJ; arc=none smtp.client-ip=57.103.85.195 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="HdTIZPLJ" Received: from outbound.qs.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-2d-60-percent-7 (Postfix) with ESMTPS id 89436180013A; Mon, 20 Jul 2026 05:08:16 +0000 (UTC) X-ICL-RepId: 019f7dec-c3da-751e-9eb6-011f49e09339 X-ICL-Out-Info: HUtFAUMEWwJACUgBTUQeDx5WFlZNRAJCTQhMHVwGXRxCCkEdXgBLVxQEAlodRw5AHVYWWAhOK1sTVRdGCRkIXR0ZHldQXgheH0wcHQ5YBhICWkUBXRcDVxxWRVwYQwldBVccHRxERVsTVRdGCRkIXR0ZCEcfCjADQg5WA0MHRQAtGRxXUF4IXh9MHB0OWAYSHVAcDlEFWwBGCU8BXRoJUwRaEB4ZWwkfFlUNQAUaHQddDVVXDw5fAREJHAMJAQlyGVoUXBhTRVEfVEYTGU4bV01QG18CQg8= Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1784524099; x=1787116099; bh=oxorjIprI4mpAO6TiEZUTmw1D9wu+4GV0+L2FBilTss=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=HdTIZPLJfpgSLxqnWXwavTqKBbqneLmo5rYK9L4QUqrDsCOwqDFpGMKBM8By1mBAlgHm2uGgChRFQI6Cecf+5sd+K+HSMq+cEi76FuLZmJqOwHTAY3VZLVjykJl40gkgDsdwyrcnJxWLLk2OblSUWJC+dt1mdkxD8nZZ7kfza5rPAqRQAxWenSspL/d0Mwm+SZNOfvD9CPHjV1BerWWegs0B+UXWGzy7AzoRACohH850NPYFeBZlyj/8bO15C2WUWpZKDFlbNW0hFvqhHYOiWkMPwyj+unnY2IcLaA4Pi63Dibq7juxpKVrKZwC8mGI0eF2vb3twRnz2pCHjQNtHRw== Received: from [21.6.122.162] (unknown [17.57.155.37]) by p00-icloudmta-asmtp-us-east-2d-60-percent-7 (Postfix) with ESMTPSA id 04C041800142; Mon, 20 Jul 2026 05:08:09 +0000 (UTC) From: Zhang Peng Date: Mon, 20 Jul 2026 13:07:41 +0800 Subject: [PATCH v5 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: <20260720-batch-tlb-flush-v5-4-db943a0d0d6b@icloud.com> References: <20260720-batch-tlb-flush-v5-0-db943a0d0d6b@icloud.com> In-Reply-To: <20260720-batch-tlb-flush-v5-0-db943a0d0d6b@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 , Qi Zheng , "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=1784524064; l=3675; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=1wbXcA3EwOgkLmlnWmmIkzMkHOobeEr8JUOUrutpQDg=; b=A0Ybc/7n8H9VBZRr6lMHF+lordRq5fKgCvcJzGPaTIFmdjV7Kt2djrb+R+w34345x0tJId+O5 eDmYa8PgD6BClRepn7xUiQO66idzQllacf+nVS/AVw6vzDFHQqcY6EG X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNzIwMDA1NCBTYWx0ZWRfXwY1/FyH0WiBA k74iNQLescyWi2rEr4XWd/WubxdgqWEAtqPeXf9mLvJ1yT+6zVSHltWjIKLTa7iZSlcC14ZVoOY 0REYlT5ADNfuDHM9YeoHw08cSyWKT4FCSZvtdnb3cClwYjle5EquaQPSNYPvxvPlCK/ie1F0RGW 486IL/k682lPvsZlnMa60/FqUoLCK8ZLRAN0Ne8ZHpjGooo0EUtYKhHAkCXiWSuJQnZtds/M+QY JkURkqkl6zea6miskrgrFQC6vGjt2rc9gYbsDeUJZWJYq2O3cjNNTZGsKAgvkUx5sPRq7vGd7Ta KgA06dJZpNwiGxjSK4O X-Proofpoint-GUID: GG3EJULa8lsx5m573CbJU4TMSUiB9zCl X-Proofpoint-ORIG-GUID: GG3EJULa8lsx5m573CbJU4TMSUiB9zCl 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 | 65 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 3e18948e90d1..bb479ead1ee0 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1220,6 +1220,38 @@ static bool folio_try_pageout(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; + const 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; + return false; + } + return true; +} + /* * Reclaimed folios are counted in the return value. */ @@ -1494,36 +1526,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