From nobody Tue Apr 7 20:07:29 2026 Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) (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 47ABA3921FD for ; Fri, 27 Feb 2026 09:45:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772185505; cv=none; b=TAXcPzVVIxNfBXvZDPsYeUhX6niFuA0WgalFSrb/JZpihHJkTHt72C/3RUniTWn6OY8zxSHQUKRUQB2N+aFR6z4f7Ox/y14xynFEcUrCNWgI88wzoAMAEolWHKx/WL4VU4qhGBalST94G0wD90YVkKPBApxtvitUYXOxM+roN8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772185505; c=relaxed/simple; bh=9C3L9DVQJBeMLf07XC4ArfLWt/SOixfKFMj4jFauWlg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XPA56+vQCpA9R5EuBhjJJuyOtoeBPsbHarjIXfG5x2tFbIaMLiMcRmR0Xsn7cCnXoDR4JTbtmc9bV/QvWmJS8R5oD8Pzi8mlspRs2oT+oBWSWIU2LH5HyGVFA/gIQMhCrYHG/zWjA8vrupblUkw/CpDsBZfGLbmG2D66Dm22ZlA= 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=AzIBTTiA; arc=none smtp.client-ip=115.124.30.124 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="AzIBTTiA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1772185500; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; bh=eqA2PZyQDXj1Bd/fcvaoW0kfwGHOGzOHK70Gr5h6lbo=; b=AzIBTTiAMsWFS8Y9Q7Nx+Ptde2yuGeJ7ZpxS7Id4tCzeDTe71bLLMiLluzjKAbnRWH6TUirhlJbrmcb30++85E3x4ySihT2IAk4SYBOUqRUjm6GcUJo1IOc7V7Z15MWxKbSoEqe+i+tBkIRHMja6boTSNlGIJS+TFqmv5gYChls= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WztzmPK_1772185497 cluster:ay36) by smtp.aliyun-inc.com; Fri, 27 Feb 2026 17:44:58 +0800 From: Baolin Wang To: akpm@linux-foundation.org, david@kernel.org Cc: catalin.marinas@arm.com, will@kernel.org, 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, dev.jain@arm.com, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, hannes@cmpxchg.org, zhengqi.arch@bytedance.com, shakeel.butt@linux.dev, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 5/6] mm: support batched checking of the young flag for MGLRU Date: Fri, 27 Feb 2026 17:44:39 +0800 Message-ID: X-Mailer: git-send-email 2.47.3 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Use the batched helper test_and_clear_young_ptes_notify() to check and clear the young flag to improve the performance during large folio reclamation wh= en MGLRU is enabled. Meanwhile, we can also support batched checking the young and dirty flag when MGLRU walks the mm's pagetable to update the folios' generation counter. Since MGLRU also checks the PTE dirty bit, use folio_pte_batch_fla= gs() with FPB_MERGE_YOUNG_DIRTY set to detect batches of PTEs for a large folio. Then we can remove the ptep_test_and_clear_young_notify() since it has no users now. Note that we also update the 'young' counter and 'mm_stats[MM_LEAF_YOUNG]' = counter with the batched count in the lru_gen_look_around() and walk_pte_range(). H= owever, the batched operations may inflate these two counters, because in a large f= olio not all PTEs may have been accessed. (Additionally, tracking how many PTEs have= been accessed within a large folio is not very meaningful, since the mm core act= ually tracks access/dirty on a per-folio basis, not per page). The impact analysi= s is as follows: 1. The 'mm_stats[MM_LEAF_YOUNG]' counter has no functional impact and is ma= inly for debugging. 2. The 'young' counter is used to decide whether to place the current PMD e= ntry into the bloom filters by suitable_to_scan() (so that next time we can check whether= it has been accessed again), which may set the hash bit in the bloom filters for a PMD = entry that hasn=E2=80=99t seen much access. However, bloom filters inherently allow so= me error, so this effect appears negligible. Reviewed-by: Rik van Riel Signed-off-by: Baolin Wang --- include/linux/mmzone.h | 5 +++-- mm/internal.h | 6 ------ mm/rmap.c | 28 +++++++++++++-------------- mm/vmscan.c | 44 +++++++++++++++++++++++++++++++----------- 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 3e2f9c953ad4..66ad80b83baa 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -630,7 +630,7 @@ struct lru_gen_memcg { =20 void lru_gen_init_pgdat(struct pglist_data *pgdat); void lru_gen_init_lruvec(struct lruvec *lruvec); -bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw); +bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int n= r); =20 void lru_gen_init_memcg(struct mem_cgroup *memcg); void lru_gen_exit_memcg(struct mem_cgroup *memcg); @@ -652,7 +652,8 @@ static inline void lru_gen_init_lruvec(struct lruvec *l= ruvec) { } =20 -static inline bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) +static inline bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, + unsigned int nr) { return false; } diff --git a/mm/internal.h b/mm/internal.h index a5f0a264ad56..a1b3967afe41 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1843,10 +1843,4 @@ static inline int pmdp_test_and_clear_young_notify(s= truct vm_area_struct *vma, =20 #endif /* CONFIG_MMU_NOTIFIER */ =20 -static inline int ptep_test_and_clear_young_notify(struct vm_area_struct *= vma, - unsigned long addr, pte_t *ptep) -{ - return test_and_clear_young_ptes_notify(vma, addr, ptep, 1); -} - #endif /* __MM_INTERNAL_H */ diff --git a/mm/rmap.c b/mm/rmap.c index 11cc6171344f..beb423f3e8ec 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -958,25 +958,21 @@ static bool folio_referenced_one(struct folio *folio, return false; } =20 + if (pvmw.pte && folio_test_large(folio)) { + const unsigned long end_addr =3D pmd_addr_end(address, vma->vm_end); + const 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 (lru_gen_enabled() && pvmw.pte) { - if (lru_gen_look_around(&pvmw)) + if (lru_gen_look_around(&pvmw, nr)) 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 (clear_flush_young_ptes_notify(vma, address, 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)) @@ -995,6 +991,10 @@ static bool folio_referenced_one(struct folio *folio, page_vma_mapped_walk_done(&pvmw); break; } + + /* Skip the batched PTEs */ + pvmw.pte +=3D nr - 1; + pvmw.address +=3D (nr - 1) * PAGE_SIZE; } =20 if (referenced) diff --git a/mm/vmscan.c b/mm/vmscan.c index 0a5622420987..7457b3c06fa3 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3474,6 +3474,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long = start, unsigned long end, struct pglist_data *pgdat =3D lruvec_pgdat(walk->lruvec); DEFINE_MAX_SEQ(walk->lruvec); int gen =3D lru_gen_from_seq(max_seq); + unsigned int nr; pmd_t pmdval; =20 pte =3D pte_offset_map_rw_nolock(args->mm, pmd, start & PMD_MASK, &pmdval= , &ptl); @@ -3492,11 +3493,13 @@ static bool walk_pte_range(pmd_t *pmd, unsigned lon= g start, unsigned long end, =20 lazy_mmu_mode_enable(); restart: - for (i =3D pte_index(start), addr =3D start; addr !=3D end; i++, addr += =3D PAGE_SIZE) { + for (i =3D pte_index(start), addr =3D start; addr !=3D end; i +=3D nr, ad= dr +=3D nr * PAGE_SIZE) { unsigned long pfn; struct folio *folio; - pte_t ptent =3D ptep_get(pte + i); + pte_t *cur_pte =3D pte + i; + pte_t ptent =3D ptep_get(cur_pte); =20 + nr =3D 1; total++; walk->mm_stats[MM_LEAF_TOTAL]++; =20 @@ -3508,7 +3511,16 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long= start, unsigned long end, if (!folio) continue; =20 - if (!ptep_test_and_clear_young_notify(args->vma, addr, pte + i)) + if (folio_test_large(folio)) { + const unsigned int max_nr =3D (end - addr) >> PAGE_SHIFT; + + nr =3D folio_pte_batch_flags(folio, NULL, cur_pte, &ptent, + max_nr, FPB_MERGE_YOUNG_DIRTY); + total +=3D nr - 1; + walk->mm_stats[MM_LEAF_TOTAL] +=3D nr - 1; + } + + if (!test_and_clear_young_ptes_notify(args->vma, addr, cur_pte, nr)) continue; =20 if (last !=3D folio) { @@ -3521,8 +3533,8 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long = start, unsigned long end, if (pte_dirty(ptent)) dirty =3D true; =20 - young++; - walk->mm_stats[MM_LEAF_YOUNG]++; + young +=3D nr; + walk->mm_stats[MM_LEAF_YOUNG] +=3D nr; } =20 walk_update_folio(walk, last, gen, dirty); @@ -4166,7 +4178,7 @@ static void lru_gen_age_node(struct pglist_data *pgda= t, struct scan_control *sc) * the PTE table to the Bloom filter. This forms a feedback loop between t= he * eviction and the aging. */ -bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) +bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int n= r) { int i; bool dirty; @@ -4184,12 +4196,13 @@ bool lru_gen_look_around(struct page_vma_mapped_wal= k *pvmw) struct lruvec *lruvec; struct lru_gen_mm_state *mm_state; unsigned long max_seq; + pte_t *cur_pte; int gen; =20 lockdep_assert_held(pvmw->ptl); VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio); =20 - if (!ptep_test_and_clear_young_notify(vma, addr, pte)) + if (!test_and_clear_young_ptes_notify(vma, addr, pte, nr)) return false; =20 if (spin_is_contended(pvmw->ptl)) @@ -4229,10 +4242,12 @@ bool lru_gen_look_around(struct page_vma_mapped_wal= k *pvmw) =20 pte -=3D (addr - start) / PAGE_SIZE; =20 - for (i =3D 0, addr =3D start; addr !=3D end; i++, addr +=3D PAGE_SIZE) { + for (i =3D 0, addr =3D start, cur_pte =3D pte; addr !=3D end; + i +=3D nr, cur_pte +=3D nr, addr +=3D nr * PAGE_SIZE) { unsigned long pfn; - pte_t ptent =3D ptep_get(pte + i); + pte_t ptent =3D ptep_get(cur_pte); =20 + nr =3D 1; pfn =3D get_pte_pfn(ptent, vma, addr, pgdat); if (pfn =3D=3D -1) continue; @@ -4241,7 +4256,14 @@ bool lru_gen_look_around(struct page_vma_mapped_walk= *pvmw) if (!folio) continue; =20 - if (!ptep_test_and_clear_young_notify(vma, addr, pte + i)) + if (folio_test_large(folio)) { + const unsigned int max_nr =3D (end - addr) >> PAGE_SHIFT; + + nr =3D folio_pte_batch_flags(folio, NULL, cur_pte, &ptent, + max_nr, FPB_MERGE_YOUNG_DIRTY); + } + + if (!test_and_clear_young_ptes_notify(vma, addr, cur_pte, nr)) continue; =20 if (last !=3D folio) { @@ -4254,7 +4276,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk = *pvmw) if (pte_dirty(ptent)) dirty =3D true; =20 - young++; + young +=3D nr; } =20 walk_update_folio(walk, last, gen, dirty); --=20 2.47.3