From nobody Tue Dec 16 16:35:16 2025 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (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 651792165EA for ; Thu, 11 Dec 2025 08:17:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765441038; cv=none; b=P2eb3esIvqFkJq4GP3tXzguPvCS2DDc4KxobDOz/V0qcYJhUqN9uoKZFV23k/+6ZO3jc7rv4lW+rdc1NZgoRq0dV90NVPQwv27Upxam/YbFi/g35+uOsqoQJEMyJaBSOaAzdgfdndKswUznkj/vL+sQ6oxcL2DcEjtkfIx8shKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765441038; c=relaxed/simple; bh=HhZ/mm7RhM5IXzy10VA6jzwSOJRfN9g+3VLUyg6Zps4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s1p9brxr5fLj0HX7bPtwx7/UU/p8u6UJhvobAxKyWTyLfguLLkTN+2zNR0Pvlf0soB4gNe3Ss8QJR3GjRaH4swxyvXPXdej/gd0j4JqWv0Wm1tx4527oEvgq1f0TkQJQ/4Gfh2Jv85Ut/QlFJhjDEIIzj046F3N2jTlFQt3TgVY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=cbOqi1Bm; arc=none smtp.client-ip=115.124.30.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="cbOqi1Bm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1765441032; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=/ea6etQCpReM0Dd5R3qfwP463Goog5MVH76oxL1v2kk=; b=cbOqi1BmJlR3HeCWfRata3dM8PlfIC8sss/Oz9J9NrF8666xT8cH8zu5mEMSH6/k0NMTZWL1dNHWUX0yD2GZIzaFSVcjmZWMsmENwumzxS03lgFOnicbPHW8IwpLArzqc3tNVi7/b0UtHgMtB21urPYtnlLaimHfUOeWfv0GNhk= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WuZnGbv_1765441030 cluster:ay36) by smtp.aliyun-inc.com; Thu, 11 Dec 2025 16:17:11 +0800 From: Baolin Wang To: akpm@linux-foundation.org, david@kernel.org, catalin.marinas@arm.com, will@kernel.org Cc: lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, Liam.Howlett@oracle.com, vbabka@suse.cz, rppt@kernel.org, surenb@google.com, mhocko@suse.com, riel@surriel.com, harry.yoo@oracle.com, jannh@google.com, willy@infradead.org, baohua@kernel.org, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] arm64: mm: support batch clearing of the young flag for large folios Date: Thu, 11 Dec 2025 16:16:54 +0800 Message-ID: X-Mailer: git-send-email 2.43.7 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, contpte_ptep_test_and_clear_young() and contpte_ptep_clear_flush= _young() only clear the young flag and flush TLBs for PTEs within the contiguous ran= ge. To support batch PTE operations for other sized large folios in the followi= ng patches, adding a new parameter to specify the number of PTEs. While we are at it, rename the functions to maintain consistency with other contpte_*() functions. Signed-off-by: Baolin Wang --- arch/arm64/include/asm/pgtable.h | 12 ++++----- arch/arm64/mm/contpte.c | 44 ++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgta= ble.h index 0944e296dd4a..e03034683156 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1679,10 +1679,10 @@ extern void contpte_clear_full_ptes(struct mm_struc= t *mm, unsigned long addr, extern pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned int nr, int full); -extern int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep); -extern int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep); +extern int contpte_test_and_clear_young_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, unsigned int nr); +extern int contpte_clear_flush_young_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, unsigned int nr); extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long add= r, pte_t *ptep, unsigned int nr); extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, @@ -1854,7 +1854,7 @@ static inline int ptep_test_and_clear_young(struct vm= _area_struct *vma, if (likely(!pte_valid_cont(orig_pte))) return __ptep_test_and_clear_young(vma, addr, ptep); =20 - return contpte_ptep_test_and_clear_young(vma, addr, ptep); + return contpte_test_and_clear_young_ptes(vma, addr, ptep, CONT_PTES); } =20 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH @@ -1866,7 +1866,7 @@ static inline int ptep_clear_flush_young(struct vm_ar= ea_struct *vma, if (likely(!pte_valid_cont(orig_pte))) return __ptep_clear_flush_young(vma, addr, ptep); =20 - return contpte_ptep_clear_flush_young(vma, addr, ptep); + return contpte_clear_flush_young_ptes(vma, addr, ptep, CONT_PTES); } =20 #define wrprotect_ptes wrprotect_ptes diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index c0557945939c..19b122441be3 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -488,8 +488,9 @@ pte_t contpte_get_and_clear_full_ptes(struct mm_struct = *mm, } EXPORT_SYMBOL_GPL(contpte_get_and_clear_full_ptes); =20 -int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep) +int contpte_test_and_clear_young_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + unsigned int nr) { /* * ptep_clear_flush_young() technically requires us to clear the access @@ -500,39 +501,56 @@ int contpte_ptep_test_and_clear_young(struct vm_area_= struct *vma, * having to unfold. */ =20 + unsigned long start =3D addr; + unsigned long end =3D start + nr * PAGE_SIZE; int young =3D 0; int i; =20 - ptep =3D contpte_align_down(ptep); - addr =3D ALIGN_DOWN(addr, CONT_PTE_SIZE); + if (pte_cont(__ptep_get(ptep + nr - 1))) + end =3D ALIGN(end, CONT_PTE_SIZE); =20 - for (i =3D 0; i < CONT_PTES; i++, ptep++, addr +=3D PAGE_SIZE) - young |=3D __ptep_test_and_clear_young(vma, addr, ptep); + if (pte_cont(__ptep_get(ptep))) { + start =3D ALIGN_DOWN(start, CONT_PTE_SIZE); + ptep =3D contpte_align_down(ptep); + } + + nr =3D (end - start) / PAGE_SIZE; + for (i =3D 0; i < nr; i++, ptep++, start +=3D PAGE_SIZE) + young |=3D __ptep_test_and_clear_young(vma, start, ptep); =20 return young; } -EXPORT_SYMBOL_GPL(contpte_ptep_test_and_clear_young); +EXPORT_SYMBOL_GPL(contpte_test_and_clear_young_ptes); =20 -int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep) +int contpte_clear_flush_young_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + unsigned int nr) { int young; =20 - young =3D contpte_ptep_test_and_clear_young(vma, addr, ptep); + young =3D contpte_test_and_clear_young_ptes(vma, addr, ptep, nr); =20 if (young) { + unsigned long start =3D addr; + unsigned long end =3D start + nr * PAGE_SIZE; + + if (pte_cont(__ptep_get(ptep + nr - 1))) + end =3D ALIGN(end, CONT_PTE_SIZE); + + if (pte_cont(__ptep_get(ptep))) + start =3D ALIGN_DOWN(start, CONT_PTE_SIZE); + /* * See comment in __ptep_clear_flush_young(); same rationale for * eliding the trailing DSB applies here. */ - addr =3D ALIGN_DOWN(addr, CONT_PTE_SIZE); - __flush_tlb_range_nosync(vma->vm_mm, addr, addr + CONT_PTE_SIZE, + __flush_tlb_range_nosync(vma->vm_mm, start, end, PAGE_SIZE, true, 3); } =20 return young; } -EXPORT_SYMBOL_GPL(contpte_ptep_clear_flush_young); +EXPORT_SYMBOL_GPL(contpte_clear_flush_young_ptes); =20 void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned int nr) --=20 2.47.3 From nobody Tue Dec 16 16:35:16 2025 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (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 78E72283FF0 for ; Thu, 11 Dec 2025 08:17:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765441039; cv=none; b=p8RisB85x/+hpcJnXRMvgeVuJKVT1CO3hQOdRPiudyrItk15BKbItN2f0fiirjTSweDfq9DLPL4sPDmgqWNJAfEqCTX/vkhGBFsS9krrCznf9hJ9J1p3mtW9sjeVYm6wAn5s5kd6q+Rubfk9YeUUY1vp2rm8CoJilJyYS1QFhK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765441039; c=relaxed/simple; bh=yrbn61xz8OWgjKVq/7bifPZ9TC8ChqzVfippITqpjzg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H+5S0mBE3JrjWo+cY4BuaQhaZa9WN3tZ2inPfeRNQN8jKoOAekoUUFYMnR5lTvz2LaweByex6rL48+7ACHLVOjjzYEB9QT7Lq+I0Q2/fslliFyOEICMurHWtnttHfJtble3nO2aZmS/xq7ZiO5nP7t/9J+ti6mXT+dDiitXAIVc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=EIKE3qcm; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="EIKE3qcm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1765441034; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=EjLnRLW+ZFEC1wSW1kP4/WvU/VRHJEHcFxTUH2LMdBg=; b=EIKE3qcmni0pggP9ZImhZAtyAZcFJs4mniMZ6ihQHmb97kgVNJciNQId05kbeOPcon+GAWraEnKKqu+2cJw9E9qIhOjkiHkbd/96sIR4DaQIpRBVLGHqirh7jWGLDK9upXhygktCqaDB0lzFgTtrjEJ4pdV4nRweu2/kk5Su0M8= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WuZl4H3_1765441031 cluster:ay36) by smtp.aliyun-inc.com; Thu, 11 Dec 2025 16:17:12 +0800 From: Baolin Wang To: akpm@linux-foundation.org, david@kernel.org, catalin.marinas@arm.com, will@kernel.org Cc: lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, Liam.Howlett@oracle.com, vbabka@suse.cz, rppt@kernel.org, surenb@google.com, mhocko@suse.com, riel@surriel.com, harry.yoo@oracle.com, jannh@google.com, willy@infradead.org, baohua@kernel.org, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] mm: rmap: support batched checks of the references for large folios Date: Thu, 11 Dec 2025 16:16:55 +0800 Message-ID: <545dba5e899634bc6c8ca782417d16fef3bd049f.1765439381.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.43.7 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, folio_referenced_one() always checks the young flag for each PTE sequentially, which is inefficient for large folios. This inefficiency is especially noticeable when reclaiming clean file-backed large folios, where folio_referenced() is observed as a significant performance hotspot. Moreover, on Arm architecture, which supports contiguous PTEs, there is alr= eady an optimization to clear the young flags for PTEs within a contiguous range. However, this is not sufficient. We can extend this to perform batched oper= ations for the entire large folio (which might exceed the contiguous range: CONT_P= TE_SIZE). Introduce a new API: clear_flush_young_ptes() to facilitate batched checking of the young flags and flushing TLB entries, thereby improving performance during large folio reclamation. Performance testing: Allocate 10G clean file-backed folios by mmap() in a memory cgroup, and try= to reclaim 8G file-backed folios via the memory.reclaim interface. I can obser= ve 33% performance improvement on my Arm64 32-core server (and 10%+ improvement on my X86 machine). Meanwhile, the hotspot folio_check_references() dropped from approximately 35% to around 5%. W/o patchset: real 0m1.518s user 0m0.000s sys 0m1.518s W/ patchset: real 0m1.018s user 0m0.000s sys 0m1.018s Signed-off-by: Baolin Wang --- arch/arm64/include/asm/pgtable.h | 11 +++++++++++ include/linux/mmu_notifier.h | 9 +++++---- include/linux/pgtable.h | 19 +++++++++++++++++++ mm/rmap.c | 22 ++++++++++++++++++++-- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgta= ble.h index e03034683156..a865bd8c46a3 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1869,6 +1869,17 @@ static inline int ptep_clear_flush_young(struct vm_a= rea_struct *vma, return contpte_clear_flush_young_ptes(vma, addr, ptep, CONT_PTES); } =20 +#define clear_flush_young_ptes clear_flush_young_ptes +static inline int clear_flush_young_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + unsigned int nr) +{ + if (likely(nr =3D=3D 1)) + return __ptep_clear_flush_young(vma, addr, ptep); + + return contpte_clear_flush_young_ptes(vma, addr, ptep, nr); +} + #define wrprotect_ptes wrprotect_ptes static __always_inline void wrprotect_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned int nr) diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h index d1094c2d5fb6..be594b274729 100644 --- a/include/linux/mmu_notifier.h +++ b/include/linux/mmu_notifier.h @@ -515,16 +515,17 @@ static inline void mmu_notifier_range_init_owner( range->owner =3D owner; } =20 -#define ptep_clear_flush_young_notify(__vma, __address, __ptep) \ +#define ptep_clear_flush_young_notify(__vma, __address, __ptep, __nr) \ ({ \ int __young; \ struct vm_area_struct *___vma =3D __vma; \ unsigned long ___address =3D __address; \ - __young =3D ptep_clear_flush_young(___vma, ___address, __ptep); \ + unsigned int ___nr =3D __nr; \ + __young =3D clear_flush_young_ptes(___vma, ___address, __ptep, ___nr); \ __young |=3D mmu_notifier_clear_flush_young(___vma->vm_mm, \ ___address, \ ___address + \ - PAGE_SIZE); \ + nr * PAGE_SIZE); \ __young; \ }) =20 @@ -650,7 +651,7 @@ static inline void mmu_notifier_subscriptions_destroy(s= truct mm_struct *mm) =20 #define mmu_notifier_range_update_to_read_only(r) false =20 -#define ptep_clear_flush_young_notify ptep_clear_flush_young +#define ptep_clear_flush_young_notify clear_flush_young_ptes #define pmdp_clear_flush_young_notify pmdp_clear_flush_young #define ptep_clear_young_notify ptep_test_and_clear_young #define pmdp_clear_young_notify pmdp_test_and_clear_young diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index b13b6f42be3c..c7d0fd228cb7 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -947,6 +947,25 @@ static inline void wrprotect_ptes(struct mm_struct *mm= , unsigned long addr, } #endif =20 +#ifndef clear_flush_young_ptes +static inline int clear_flush_young_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + unsigned int nr) +{ + int young =3D 0; + + for (;;) { + young |=3D ptep_clear_flush_young(vma, addr, ptep); + if (--nr =3D=3D 0) + break; + ptep++; + addr +=3D PAGE_SIZE; + } + + return young; +} +#endif + /* * On some architectures hardware does not set page access bit when access= ing * memory page, it is responsibility of software setting this bit. It brin= gs diff --git a/mm/rmap.c b/mm/rmap.c index d6799afe1114..ec232165c47d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -827,9 +827,11 @@ static bool folio_referenced_one(struct folio *folio, struct folio_referenced_arg *pra =3D arg; DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); int ptes =3D 0, referenced =3D 0; + unsigned int nr; =20 while (page_vma_mapped_walk(&pvmw)) { address =3D pvmw.address; + nr =3D 1; =20 if (vma->vm_flags & VM_LOCKED) { ptes++; @@ -874,9 +876,21 @@ static bool folio_referenced_one(struct folio *folio, if (lru_gen_look_around(&pvmw)) referenced++; } else if (pvmw.pte) { + if (folio_test_large(folio)) { + unsigned long end_addr =3D pmd_addr_end(address, vma->vm_end); + unsigned int max_nr =3D (end_addr - address) >> PAGE_SHIFT; + pte_t pteval =3D ptep_get(pvmw.pte); + + nr =3D folio_pte_batch(folio, pvmw.pte, pteval, max_nr); + } + + ptes +=3D nr; if (ptep_clear_flush_young_notify(vma, address, - pvmw.pte)) + pvmw.pte, nr)) referenced++; + /* Skip the batched PTEs */ + pvmw.pte +=3D nr - 1; + pvmw.address +=3D (nr - 1) * PAGE_SIZE; } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { if (pmdp_clear_flush_young_notify(vma, address, pvmw.pmd)) @@ -886,7 +900,11 @@ static bool folio_referenced_one(struct folio *folio, WARN_ON_ONCE(1); } =20 - pra->mapcount--; + pra->mapcount -=3D nr; + if (ptes =3D=3D pvmw.nr_pages) { + page_vma_mapped_walk_done(&pvmw); + break; + } } =20 if (referenced) --=20 2.47.3 From nobody Tue Dec 16 16:35:16 2025 Received: from out30-113.freemail.mail.aliyun.com (out30-113.freemail.mail.aliyun.com [115.124.30.113]) (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 69B6B298CC4 for ; Thu, 11 Dec 2025 08:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.113 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765441040; cv=none; b=i+CJiWMBsR/lPI353kv8PNvfsjl2EjkFR7V/2ytHK1k2zPHuYN3EFw3oeiJJ0ZFQW4RpRGQ0hYfKzW7C+IvWvQai/bK069TC88DEutcb/qLk6NGwuVPq6IHj40Jm4mbAMWv3lTb+s3D9/T3dfvNXJJiYUqo4MnK9u/iN4KmsnH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765441040; c=relaxed/simple; bh=UFZxc/ZRMGIPUXnfqq32PnB9V1MiPcBtIySry0YhS8s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jqMk+/yPEXSPLRYcgRzJKfx20b7eJWMEfOGUPLC/vDqEaSaYvOODNqylS2HipAwOAxyAavuZKHeQ4KptosKPNTQGS1YGBhou4hrooxL4qLLEvHRsouh4r1YOZabF1TkIAAK0dSp8epZt8y4+2xGN1vwQJoHquIcAtVpk0GXS8Us= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=gJo4993S; arc=none smtp.client-ip=115.124.30.113 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="gJo4993S" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1765441035; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=GTUtfG6uYByUGiREez0T2P/sllJfoIOg91OqMSv2QCo=; b=gJo4993SZkNx01mWQ6MeC3Fw+IYxByVyhZDRR9vJ6k1L9JJzXVA5emuoqZiUBlPCSn3gJfEj+C2dWyT+gJ8tpjboG+vt9A3XfbnMjOdShZYU891+Ib5M9FMIJkqwO4Gs7mupJBRGkPRy/xacdXNsuS4Ul8WvftrsBFKoUEN/w18= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WuZjI5S_1765441033 cluster:ay36) by smtp.aliyun-inc.com; Thu, 11 Dec 2025 16:17:14 +0800 From: Baolin Wang To: akpm@linux-foundation.org, david@kernel.org, catalin.marinas@arm.com, will@kernel.org Cc: lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, Liam.Howlett@oracle.com, vbabka@suse.cz, rppt@kernel.org, surenb@google.com, mhocko@suse.com, riel@surriel.com, harry.yoo@oracle.com, jannh@google.com, willy@infradead.org, baohua@kernel.org, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] mm: rmap: support batched unmapping for file large folios Date: Thu, 11 Dec 2025 16:16:56 +0800 Message-ID: X-Mailer: git-send-email 2.43.7 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Similar to folio_referenced_one(), we can apply batched unmapping for file large folios to optimize the performance of file folios reclamation. Performance testing: Allocate 10G clean file-backed folios by mmap() in a memory cgroup, and try= to reclaim 8G file-backed folios via the memory.reclaim interface. I can obser= ve 75% performance improvement on my Arm64 32-core server. W/o patch: real 0m1.018s user 0m0.000s sys 0m1.018s W/ patch: real 0m0.249s user 0m0.000s sys 0m0.249s Signed-off-by: Baolin Wang Acked-by: Barry Song --- mm/rmap.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index ec232165c47d..4c9d5777c8da 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1855,9 +1855,10 @@ static inline unsigned int folio_unmap_pte_batch(str= uct folio *folio, end_addr =3D pmd_addr_end(addr, vma->vm_end); max_nr =3D (end_addr - addr) >> PAGE_SHIFT; =20 - /* We only support lazyfree batching for now ... */ - if (!folio_test_anon(folio) || folio_test_swapbacked(folio)) + /* We only support lazyfree or file folios batching for now ... */ + if (folio_test_anon(folio) && folio_test_swapbacked(folio)) return 1; + if (pte_unused(pte)) return 1; =20 @@ -2223,7 +2224,7 @@ static bool try_to_unmap_one(struct folio *folio, str= uct vm_area_struct *vma, * * See Documentation/mm/mmu_notifier.rst */ - dec_mm_counter(mm, mm_counter_file(folio)); + add_mm_counter(mm, mm_counter_file(folio), -nr_pages); } discard: if (unlikely(folio_test_hugetlb(folio))) { --=20 2.47.3