Commit 28eae84e authored by David Hildenbrand's avatar David Hildenbrand Committed by Kefeng Wang
Browse files

mm/memory: further separate anon and pagecache folio handling in zap_present_pte()

mainline inclusion
from mainline-v6.9-rc1
commit d11838ed63ee842fc9ef335b9f3aee3aa26f2ab5
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I9CHB4
CVE: NA

-------------------------------------------------

We don't need up-to-date accessed-dirty information for anon folios and
can simply work with the ptent we already have.  Also, we know the RSS
counter we want to update.

We can safely move arch_check_zapped_pte() + tlb_remove_tlb_entry() +
zap_install_uffd_wp_if_needed() after updating the folio and RSS.

While at it, only call zap_install_uffd_wp_if_needed() if there is even
any chance that pte_install_uffd_wp_if_needed() would do *something*.
That is, just don't bother if uffd-wp does not apply.

Link: https://lkml.kernel.org/r/20240214204435.167852-4-david@redhat.com


Signed-off-by: default avatarDavid Hildenbrand <david@redhat.com>
Reviewed-by: default avatarRyan Roberts <ryan.roberts@arm.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@suse.com>
Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yin Fengwei <fengwei.yin@intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
(cherry picked from commit d11838ed63ee842fc9ef335b9f3aee3aa26f2ab5)
Signed-off-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>

 Conflicts:
	mm/memory.c
parent ce919583
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -1555,12 +1555,9 @@ static inline void zap_present_pte(struct mmu_gather *tlb,
	folio = page_folio(page);
	if (unlikely(!should_zap_folio(details, folio)))
		return;
	ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm);
	arch_check_zapped_pte(vma, ptent);
	tlb_remove_tlb_entry(tlb, pte, addr);
	zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent);

	if (!folio_test_anon(folio)) {
		ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm);
		if (pte_dirty(ptent)) {
			folio_mark_dirty(folio);
			if (tlb_delay_rmap(tlb)) {
@@ -1570,9 +1567,19 @@ static inline void zap_present_pte(struct mmu_gather *tlb,
		}
		if (pte_young(ptent) && likely(vma_has_recency(vma)))
			folio_mark_accessed(folio);
	}
		rss[mm_counter(folio)]--;
		add_reliable_page_counter(page, mm, -1);
	} else {
		/* We don't need up-to-date accessed/dirty bits. */
		ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm);
		rss[MM_ANONPAGES]--;
	}

	arch_check_zapped_pte(vma, ptent);
	tlb_remove_tlb_entry(tlb, pte, addr);
	if (unlikely(userfaultfd_pte_wp(vma, ptent)))
		zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent);

	if (!delay_rmap) {
		folio_remove_rmap_pte(folio, page, vma);
		if (unlikely(page_mapcount(page) < 0))