From nobody Fri Dec 19 08:05:38 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A3057130E58 for ; Thu, 15 Feb 2024 10:33:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707993194; cv=none; b=OKTpyrZpZlOAO99qF2GRCEAzP7VNeiF6knbstS0deaD50Pc+rCWbDwLH5CuNq6t9GZDOSnbcJxi2Sbm/LkISdYDdQSzwpGN1kHcXtPcuLocaUtunUfzf8tMYAmJ43oYo0eVe3R7wcfkWY6ZgHFC1xFroVjv/B++Xf5eD4FRrCUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707993194; c=relaxed/simple; bh=4dt5LCeatOElsPUkFZ0pGfvF4+jB8UBy2mT55eRRhoI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WqH799jRnjXZcrsRXPrcyUOnJ1ZNo3nRt5KMdZdbceEV4Cvm3byEHy3BhJ5zk5t9G6xMuiGIAcddtXivCkrggSZ01dUJtcLICLhb1iTXsMfHBbQdRENtizFHSICDMt6JTnF3PxN4vutGCK3BtolfK6nYKeKi6f/ZF7PksPv0i3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 20A7A15A1; Thu, 15 Feb 2024 02:33:52 -0800 (PST) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 17B8B3F7B4; Thu, 15 Feb 2024 02:33:07 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , James Morse , Andrey Ryabinin , Andrew Morton , Matthew Wilcox , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, x86@kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 15/18] mm: Add pte_batch_hint() to reduce scanning in folio_pte_batch() Date: Thu, 15 Feb 2024 10:32:02 +0000 Message-Id: <20240215103205.2607016-16-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240215103205.2607016-1-ryan.roberts@arm.com> References: <20240215103205.2607016-1-ryan.roberts@arm.com> 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" Some architectures (e.g. arm64) can tell from looking at a pte, if some follow-on ptes also map contiguous physical memory with the same pgprot. (for arm64, these are contpte mappings). Take advantage of this knowledge to optimize folio_pte_batch() so that it can skip these ptes when scanning to create a batch. By default, if an arch does not opt-in, folio_pte_batch() returns a compile-time 1, so the changes are optimized out and the behaviour is as before. arm64 will opt-in to providing this hint in the next patch, which will greatly reduce the cost of ptep_get() when scanning a range of contptes. Acked-by: David Hildenbrand Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- include/linux/pgtable.h | 21 +++++++++++++++++++++ mm/memory.c | 19 ++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index bc005d84f764..a36cf4e124b0 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -212,6 +212,27 @@ static inline int pmd_dirty(pmd_t pmd) #define arch_flush_lazy_mmu_mode() do {} while (0) #endif =20 +#ifndef pte_batch_hint +/** + * pte_batch_hint - Number of pages that can be added to batch without sca= nning. + * @ptep: Page table pointer for the entry. + * @pte: Page table entry. + * + * Some architectures know that a set of contiguous ptes all map the same + * contiguous memory with the same permissions. In this case, it can provi= de a + * hint to aid pte batching without the core code needing to scan every pt= e. + * + * An architecture implementation may ignore the PTE accessed state. Furth= er, + * the dirty state must apply atomically to all the PTEs described by the = hint. + * + * May be overridden by the architecture, else pte_batch_hint is always 1. + */ +static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte) +{ + return 1; +} +#endif + #ifndef pte_advance_pfn static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) { diff --git a/mm/memory.c b/mm/memory.c index 3b8e56eb08a3..4dd8e35b593a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -988,16 +988,20 @@ static inline int folio_pte_batch(struct folio *folio= , unsigned long addr, { unsigned long folio_end_pfn =3D folio_pfn(folio) + folio_nr_pages(folio); const pte_t *end_ptep =3D start_ptep + max_nr; - pte_t expected_pte =3D __pte_batch_clear_ignored(pte_next_pfn(pte), flags= ); - pte_t *ptep =3D start_ptep + 1; + pte_t expected_pte, *ptep; bool writable; + int nr; =20 if (any_writable) *any_writable =3D false; =20 VM_WARN_ON_FOLIO(!pte_present(pte), folio); =20 - while (ptep !=3D end_ptep) { + nr =3D pte_batch_hint(start_ptep, pte); + expected_pte =3D __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flag= s); + ptep =3D start_ptep + nr; + + while (ptep < end_ptep) { pte =3D ptep_get(ptep); if (any_writable) writable =3D !!pte_write(pte); @@ -1011,17 +1015,18 @@ static inline int folio_pte_batch(struct folio *fol= io, unsigned long addr, * corner cases the next PFN might fall into a different * folio. */ - if (pte_pfn(pte) =3D=3D folio_end_pfn) + if (pte_pfn(pte) >=3D folio_end_pfn) break; =20 if (any_writable) *any_writable |=3D writable; =20 - expected_pte =3D pte_next_pfn(expected_pte); - ptep++; + nr =3D pte_batch_hint(ptep, pte); + expected_pte =3D pte_advance_pfn(expected_pte, nr); + ptep +=3D nr; } =20 - return ptep - start_ptep; + return min(ptep - start_ptep, max_nr); } =20 /* --=20 2.25.1