From nobody Sun Nov 24 11:49:52 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 454B318FDBA; Wed, 6 Nov 2024 02:10:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730859006; cv=none; b=tmVzAWxPWyul9nkoiEZdCU8FkjFJGwWYxtY6wFeM095W41QqqwYILqX3s1NAnBVa40c5DbFgJYx+SZJULVjIrDJBpwDQAEkRJ3XqEzlO3jnqzS7eq3n7dmPpqUIPJXuHoaJUu84ROBWzVuLXiUssxR7qcnCCnpSFsp14ock4RY0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730859006; c=relaxed/simple; bh=DEnQJQNDMiiOXMpyHq+sUGk43RCAzJypHtU77gDkvqM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=fzzFU/HP/s3iO+Vr8Xa6bCNim1JvtHLx48hByCIuYZmVnO8SNzdoagZw08QwFw/0BWx/ZGrlxyi1ZCypcxYBPas9SL9yxJntpA/tl3PrwXzHyLOPkWF4kO9SeZ/+63dLFdlWR8AQQ+zMbfTsUCWQUz7u/dBVQZrfJgwK+8bFKHg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gPaAJK79; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gPaAJK79" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76CB9C4CEE3; Wed, 6 Nov 2024 02:10:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730859005; bh=DEnQJQNDMiiOXMpyHq+sUGk43RCAzJypHtU77gDkvqM=; h=From:To:Cc:Subject:Date:From; b=gPaAJK79uwm2/Hium77ERZ0p9nU6YUVdQdaeQ5h8itPAoSiPepqmEmAsNnvPJCMcC EpPl1GrSFdow63vFb772cbplNGbqyRAoyQoYQ+n3XQDjJ3QAj/FFKPjnqJhnjkKRSC Rd+8Ro0g6/Xr6XQ2GRdCKvrpbp1zTtpJwXN7DHOmQQWjKBTPJzMd29Gp/B7tPLOCKP yX2dd8u0+aJzbCN5CRKQ/5wt2iD4BbNRxkHTujAiyqHAYDBjXY4qH/CsAnByc6xsbK Xo+SUYEFSwr2+vNLMSrfaS9QeigLvnzfmQklRa6Wjr+kQts8N5u4vSDpUdWETYxSNW 3Qw9NIa+Ui+AQ== From: Sasha Levin To: stable@vger.kernel.org, yuzhao@google.com Cc: James Houghton , David Stevens , Axel Rasmussen , David Matlack , David Rientjes , Oliver Upton , Paolo Bonzini , Sean Christopherson , Wei Xu , kernel test robot , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: FAILED: Patch "mm: multi-gen LRU: use {ptep,pmdp}_clear_young_notify()" failed to apply to v6.6-stable tree Date: Tue, 5 Nov 2024 21:10:00 -0500 Message-ID: <20241106021001.176509-1-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Hint: ignore X-stable: review Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The patch below does not apply to the v6.6-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . Thanks, Sasha Reported-by: David Stevens ------------------ original commit in Linus's tree ------------------ From 1d4832becdc2cdb2cffe2a6050c9d9fd8ff1c58c Mon Sep 17 00:00:00 2001 From: Yu Zhao Date: Sat, 19 Oct 2024 01:29:39 +0000 Subject: [PATCH] mm: multi-gen LRU: use {ptep,pmdp}_clear_young_notify() When the MM_WALK capability is enabled, memory that is mostly accessed by a VM appears younger than it really is, therefore this memory will be less likely to be evicted. Therefore, the presence of a running VM can significantly increase swap-outs for non-VM memory, regressing the performance for the rest of the system. Fix this regression by always calling {ptep,pmdp}_clear_young_notify() whenever we clear the young bits on PMDs/PTEs. [jthoughton@google.com: fix link-time error] Link: https://lkml.kernel.org/r/20241019012940.3656292-3-jthoughton@google.= com Fixes: bd74fdaea146 ("mm: multi-gen LRU: support page table walks") Signed-off-by: Yu Zhao Signed-off-by: James Houghton Reported-by: David Stevens Cc: Axel Rasmussen Cc: David Matlack Cc: David Rientjes Cc: Oliver Upton Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Wei Xu Cc: Cc: kernel test robot Signed-off-by: Andrew Morton --- include/linux/mmzone.h | 5 ++- mm/rmap.c | 9 ++--- mm/vmscan.c | 88 +++++++++++++++++++++++------------------- 3 files changed, 55 insertions(+), 47 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 9342e5692dab6..5b1c984daf454 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -555,7 +555,7 @@ struct lru_gen_memcg { =20 void lru_gen_init_pgdat(struct pglist_data *pgdat); void lru_gen_init_lruvec(struct lruvec *lruvec); -void lru_gen_look_around(struct page_vma_mapped_walk *pvmw); +bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw); =20 void lru_gen_init_memcg(struct mem_cgroup *memcg); void lru_gen_exit_memcg(struct mem_cgroup *memcg); @@ -574,8 +574,9 @@ static inline void lru_gen_init_lruvec(struct lruvec *l= ruvec) { } =20 -static inline void lru_gen_look_around(struct page_vma_mapped_walk *pvmw) +static inline bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) { + return false; } =20 static inline void lru_gen_init_memcg(struct mem_cgroup *memcg) diff --git a/mm/rmap.c b/mm/rmap.c index a8797d1b3d496..73d5998677d40 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -885,13 +885,10 @@ static bool folio_referenced_one(struct folio *folio, return false; } =20 - if (pvmw.pte) { - if (lru_gen_enabled() && - pte_young(ptep_get(pvmw.pte))) { - lru_gen_look_around(&pvmw); + if (lru_gen_enabled() && pvmw.pte) { + if (lru_gen_look_around(&pvmw)) referenced++; - } - + } else if (pvmw.pte) { if (ptep_clear_flush_young_notify(vma, address, pvmw.pte)) referenced++; diff --git a/mm/vmscan.c b/mm/vmscan.c index 4f1d33e4b3601..ddaaff67642e1 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -56,6 +56,7 @@ #include #include #include +#include =20 #include #include @@ -3294,7 +3295,8 @@ static bool get_next_vma(unsigned long mask, unsigned= long size, struct mm_walk return false; } =20 -static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, un= signed long addr) +static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, un= signed long addr, + struct pglist_data *pgdat) { unsigned long pfn =3D pte_pfn(pte); =20 @@ -3306,13 +3308,20 @@ static unsigned long get_pte_pfn(pte_t pte, struct = vm_area_struct *vma, unsigned if (WARN_ON_ONCE(pte_devmap(pte) || pte_special(pte))) return -1; =20 + if (!pte_young(pte) && !mm_has_notifiers(vma->vm_mm)) + return -1; + if (WARN_ON_ONCE(!pfn_valid(pfn))) return -1; =20 + if (pfn < pgdat->node_start_pfn || pfn >=3D pgdat_end_pfn(pgdat)) + return -1; + return pfn; } =20 -static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, un= signed long addr) +static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, un= signed long addr, + struct pglist_data *pgdat) { unsigned long pfn =3D pmd_pfn(pmd); =20 @@ -3324,9 +3333,15 @@ static unsigned long get_pmd_pfn(pmd_t pmd, struct v= m_area_struct *vma, unsigned if (WARN_ON_ONCE(pmd_devmap(pmd))) return -1; =20 + if (!pmd_young(pmd) && !mm_has_notifiers(vma->vm_mm)) + return -1; + if (WARN_ON_ONCE(!pfn_valid(pfn))) return -1; =20 + if (pfn < pgdat->node_start_pfn || pfn >=3D pgdat_end_pfn(pgdat)) + return -1; + return pfn; } =20 @@ -3335,10 +3350,6 @@ static struct folio *get_pfn_folio(unsigned long pfn= , struct mem_cgroup *memcg, { struct folio *folio; =20 - /* try to avoid unnecessary memory loads */ - if (pfn < pgdat->node_start_pfn || pfn >=3D pgdat_end_pfn(pgdat)) - return NULL; - folio =3D pfn_folio(pfn); if (folio_nid(folio) !=3D pgdat->node_id) return NULL; @@ -3394,20 +3405,16 @@ static bool walk_pte_range(pmd_t *pmd, unsigned lon= g start, unsigned long end, total++; walk->mm_stats[MM_LEAF_TOTAL]++; =20 - pfn =3D get_pte_pfn(ptent, args->vma, addr); + pfn =3D get_pte_pfn(ptent, args->vma, addr, pgdat); if (pfn =3D=3D -1) continue; =20 - if (!pte_young(ptent)) { - continue; - } - folio =3D get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); if (!folio) continue; =20 - if (!ptep_test_and_clear_young(args->vma, addr, pte + i)) - VM_WARN_ON_ONCE(true); + if (!ptep_clear_young_notify(args->vma, addr, pte + i)) + continue; =20 young++; walk->mm_stats[MM_LEAF_YOUNG]++; @@ -3473,21 +3480,25 @@ static void walk_pmd_range_locked(pud_t *pud, unsig= ned long addr, struct vm_area /* don't round down the first address */ addr =3D i ? (*first & PMD_MASK) + i * PMD_SIZE : *first; =20 - pfn =3D get_pmd_pfn(pmd[i], vma, addr); - if (pfn =3D=3D -1) + if (!pmd_present(pmd[i])) goto next; =20 if (!pmd_trans_huge(pmd[i])) { - if (!walk->force_scan && should_clear_pmd_young()) + if (!walk->force_scan && should_clear_pmd_young() && + !mm_has_notifiers(args->mm)) pmdp_test_and_clear_young(vma, addr, pmd + i); goto next; } =20 + pfn =3D get_pmd_pfn(pmd[i], vma, addr, pgdat); + if (pfn =3D=3D -1) + goto next; + folio =3D get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); if (!folio) goto next; =20 - if (!pmdp_test_and_clear_young(vma, addr, pmd + i)) + if (!pmdp_clear_young_notify(vma, addr, pmd + i)) goto next; =20 walk->mm_stats[MM_LEAF_YOUNG]++; @@ -3545,24 +3556,18 @@ static void walk_pmd_range(pud_t *pud, unsigned lon= g start, unsigned long end, } =20 if (pmd_trans_huge(val)) { - unsigned long pfn =3D pmd_pfn(val); struct pglist_data *pgdat =3D lruvec_pgdat(walk->lruvec); + unsigned long pfn =3D get_pmd_pfn(val, vma, addr, pgdat); =20 walk->mm_stats[MM_LEAF_TOTAL]++; =20 - if (!pmd_young(val)) { - continue; - } - - /* try to avoid unnecessary memory loads */ - if (pfn < pgdat->node_start_pfn || pfn >=3D pgdat_end_pfn(pgdat)) - continue; - - walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first); + if (pfn !=3D -1) + walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first); continue; } =20 - if (!walk->force_scan && should_clear_pmd_young()) { + if (!walk->force_scan && should_clear_pmd_young() && + !mm_has_notifiers(args->mm)) { if (!pmd_young(val)) continue; =20 @@ -4036,13 +4041,13 @@ static void lru_gen_age_node(struct pglist_data *pg= dat, struct scan_control *sc) * the PTE table to the Bloom filter. This forms a feedback loop between t= he * eviction and the aging. */ -void lru_gen_look_around(struct page_vma_mapped_walk *pvmw) +bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) { int i; unsigned long start; unsigned long end; struct lru_gen_mm_walk *walk; - int young =3D 0; + int young =3D 1; pte_t *pte =3D pvmw->pte; unsigned long addr =3D pvmw->address; struct vm_area_struct *vma =3D pvmw->vma; @@ -4058,12 +4063,15 @@ void lru_gen_look_around(struct page_vma_mapped_wal= k *pvmw) lockdep_assert_held(pvmw->ptl); VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio); =20 + if (!ptep_clear_young_notify(vma, addr, pte)) + return false; + if (spin_is_contended(pvmw->ptl)) - return; + return true; =20 /* exclude special VMAs containing anon pages from COW */ if (vma->vm_flags & VM_SPECIAL) - return; + return true; =20 /* avoid taking the LRU lock under the PTL when possible */ walk =3D current->reclaim_state ? current->reclaim_state->mm_walk : NULL; @@ -4071,6 +4079,9 @@ void lru_gen_look_around(struct page_vma_mapped_walk = *pvmw) start =3D max(addr & PMD_MASK, vma->vm_start); end =3D min(addr | ~PMD_MASK, vma->vm_end - 1) + 1; =20 + if (end - start =3D=3D PAGE_SIZE) + return true; + if (end - start > MIN_LRU_BATCH * PAGE_SIZE) { if (addr - start < MIN_LRU_BATCH * PAGE_SIZE / 2) end =3D start + MIN_LRU_BATCH * PAGE_SIZE; @@ -4084,7 +4095,7 @@ void lru_gen_look_around(struct page_vma_mapped_walk = *pvmw) =20 /* folio_update_gen() requires stable folio_memcg() */ if (!mem_cgroup_trylock_pages(memcg)) - return; + return true; =20 arch_enter_lazy_mmu_mode(); =20 @@ -4094,19 +4105,16 @@ void lru_gen_look_around(struct page_vma_mapped_wal= k *pvmw) unsigned long pfn; pte_t ptent =3D ptep_get(pte + i); =20 - pfn =3D get_pte_pfn(ptent, vma, addr); + pfn =3D get_pte_pfn(ptent, vma, addr, pgdat); if (pfn =3D=3D -1) continue; =20 - if (!pte_young(ptent)) - continue; - folio =3D get_pfn_folio(pfn, memcg, pgdat, can_swap); if (!folio) continue; =20 - if (!ptep_test_and_clear_young(vma, addr, pte + i)) - VM_WARN_ON_ONCE(true); + if (!ptep_clear_young_notify(vma, addr, pte + i)) + continue; =20 young++; =20 @@ -4136,6 +4144,8 @@ void lru_gen_look_around(struct page_vma_mapped_walk = *pvmw) /* feedback from rmap walkers to page table walkers */ if (mm_state && suitable_to_scan(i, young)) update_bloom_filter(mm_state, max_seq, pvmw->pmd); + + return true; } =20 /*************************************************************************= ***** --=20 2.43.0