From patchwork Fri Feb 2 08:07:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13542355 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E6BDC47258 for ; Fri, 2 Feb 2024 08:09:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1CB66B00A9; Fri, 2 Feb 2024 03:09:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A592A6B00AB; Fri, 2 Feb 2024 03:09:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 685366B00AC; Fri, 2 Feb 2024 03:09:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 55B836B00A9 for ; Fri, 2 Feb 2024 03:09:35 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3258E801D1 for ; Fri, 2 Feb 2024 08:09:35 +0000 (UTC) X-FDA: 81746139510.01.664E4CC Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf12.hostedemail.com (Postfix) with ESMTP id 7FBF040010 for ; Fri, 2 Feb 2024 08:09:33 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf12.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706861373; a=rsa-sha256; cv=none; b=FYCyWCU1WsfbJd4gdRefcwJSRAg3n4luJoeGtvBzgELmaAJjrwOlz2QyBxp+c9C2ktmvOA y4nxUpSWu65rdQz1ILLN7nlkrbFDBWdUlS0Uujvo0MMIX6lq9HO9f/9pPBcKFEtcjQaRT6 d/TyuPpBTY+80f42EQgWedqF6ewH0y8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf12.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706861373; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=c/4EEJu2gQomaE2sg7y+ahoqZodMpQ+lo82yJLdFja4=; b=gn9tifKuQ7GF3vYSbAP69CMBCUCxO9eiX9HYAJAFSyqejARHgMJ+sd5k8+GeQgZJKZvJbB oV38PYxzVINcbrzq8xc/9PW/msnLl/lHt15ODx9wGd1W0S2ALepPhAFJ4bB7DrPYOQOVpr RGRkmTik7dsiIxn0rK0eK0+ubX7UhxE= 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 1EEE91BCB; Fri, 2 Feb 2024 00:10:15 -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 29FC73F8A4; Fri, 2 Feb 2024 00:09:29 -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 , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , 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 v5 20/25] arm64/mm: Implement new wrprotect_ptes() batch API Date: Fri, 2 Feb 2024 08:07:51 +0000 Message-Id: <20240202080756.1453939-21-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240202080756.1453939-1-ryan.roberts@arm.com> References: <20240202080756.1453939-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 7FBF040010 X-Stat-Signature: 5c1wasi9jk4bob378tbokn6g5xtdbyph X-Rspam-User: X-HE-Tag: 1706861373-616869 X-HE-Meta: U2FsdGVkX19EfbY3UU1Zrh22rvgVfG1QpqH0vl+gTiYp3tyeyJhAE10XHw1LT3OH9aqDbul5Gs+HzEQ4SErqvmfHpKJOuHCRVbTVPPvw5CqPK/AUNUePv4KTZG8PP+dC+MvlEOnQpFrGdADyiATszI5vq6NqVxWYbwkzen1X1vUeiMr6Z54mdLZ60rUmCHlAwedr4KZgMbba0G4fNJFN24WhJTwtxFCQl3itLE9bt3NfgUc3pVfQcwTPJ9I/p5F5AZDCXDDoqav/yw9TG6NLw5MLcdQ9gysmBMRlesf88XNPgghO2IuKe7G3EQxffwiBWMET1ton81ZY+tx8BQinJMWI5JXfBN4rHNyInoOrVC+9Gmt8HuLMaTZomATnBpmacjBJYEQc/pzPgoi/xeC+qWRsrUugIqVQnkfn7+VJnU9hA4YHT8acdeP3wcWTvg/eRhA/UhFKU1JSUSzr0KLiQ5g0NWuDjf4qI9/8/R9U9X5esS/cNzM8IPneT815LjAGJcUQj0RPKMxbAFqicq853g0fGffxnm8dd+dDtlFTG5a3kvuGBJiDR0cChCqJRnrf9yNiQsnTF09rXj4TPQd8Ngt0QbFOYekyDcKksy8Kw0zebmoQ/Wm1AH7WrZrhZj6tswRiFAyzQy+6JiDXHQhBGtf2BfFTZy7dq9XqFC8LRAna8Xxe0vmQshCUIcNxW1F20JpN4wvVYUU/gIIoyyzwyAWtuVsnwKoPz26uZenISCSSHiPUYodRkzh37UY7Pa8cjfVmcL1MHh/x+Sn6WCeV1fXIfaGH4yIGTVtwdo6LSh+7jzLOz1h4307lTmeIiKE5Updiy//2QOvebO8OD0NukLoH+XXBfAySPcaG4iAP15/VvsRzdQTU8mzZ+P2qIuOoAcl0cGgHtLQjTj8B0lPVv15fdg9pwx1UAxuKgwLMSGlZKvDh/tpXJYdfW5FJFd8n1uGSzDmZQnJAOJMHBQZ hdFaf8bJ tjk1VVtB8vOiYOtJnRtF1qfFxXGdHc7zQ9UBIsdHHxGUeBR0AD6IlAd26e0QhNNUHiskT7rB++JpNSxkmo4xq0aeYJJKGbXqc22X6d/J3MdclzhAc9IBjICMN5fgmTDu/crIHvZ6y6tlS5tjLcUGhkITs2ACWdjfzuJcZjn6Jpd/tk+omx42zdK0y8p92IltvBRCTtiUZ/fZxiTuI9iePyUGRpI+87duhmCS5vDQbyFGNLlvup/RrjfEeEh3VYAkxsO5qMFCOWR2NX63eKZ5iWEqylfpZmea1adiLq8ysZMOYY/M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Optimize the contpte implementation to fix some of the fork performance regression introduced by the initial contpte commit. Subsequent patches will solve it entirely. During fork(), any private memory in the parent must be write-protected. Previously this was done 1 PTE at a time. But the core-mm supports batched wrprotect via the new wrprotect_ptes() API. So let's implement that API and for fully covered contpte mappings, we no longer need to unfold the contpte. This has 2 benefits: - reduced unfolding, reduces the number of tlbis that must be issued. - The memory remains contpte-mapped ("folded") in the parent, so it continues to benefit from the more efficient use of the TLB after the fork. The optimization to wrprotect a whole contpte block without unfolding is possible thanks to the tightening of the Arm ARM in respect to the definition and behaviour when 'Misprogramming the Contiguous bit'. See section D21194 at https://developer.arm.com/documentation/102105/latest/ Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 61 ++++++++++++++++++++++++++------ arch/arm64/mm/contpte.c | 35 ++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 34892a95403d..c07f0d563733 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -978,16 +978,12 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ -/* - * __ptep_set_wrprotect - mark read-only while trasferring potential hardware - * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit. - */ -static inline void __ptep_set_wrprotect(struct mm_struct *mm, - unsigned long address, pte_t *ptep) +static inline void ___ptep_set_wrprotect(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + pte_t pte) { - pte_t old_pte, pte; + pte_t old_pte; - pte = __ptep_get(ptep); do { old_pte = pte; pte = pte_wrprotect(pte); @@ -996,6 +992,25 @@ static inline void __ptep_set_wrprotect(struct mm_struct *mm, } while (pte_val(pte) != pte_val(old_pte)); } +/* + * __ptep_set_wrprotect - mark read-only while trasferring potential hardware + * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit. + */ +static inline void __ptep_set_wrprotect(struct mm_struct *mm, + unsigned long address, pte_t *ptep) +{ + ___ptep_set_wrprotect(mm, address, ptep, __ptep_get(ptep)); +} + +static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address, + pte_t *ptep, unsigned int nr) +{ + unsigned int i; + + for (i = 0; i < nr; i++, address += PAGE_SIZE, ptep++) + __ptep_set_wrprotect(mm, address, ptep); +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_PMDP_SET_WRPROTECT static inline void pmdp_set_wrprotect(struct mm_struct *mm, @@ -1156,6 +1171,8 @@ 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 void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr); extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty); @@ -1269,12 +1286,35 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma, return contpte_ptep_clear_flush_young(vma, addr, ptep); } +#define wrprotect_ptes wrprotect_ptes +static inline void wrprotect_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + if (likely(nr == 1)) { + /* + * Optimization: wrprotect_ptes() can only be called for present + * ptes so we only need to check contig bit as condition for + * unfold, and we can remove the contig bit from the pte we read + * to avoid re-reading. This speeds up fork() which is sensitive + * for order-0 folios. Equivalent to contpte_try_unfold(). + */ + pte_t orig_pte = __ptep_get(ptep); + + if (unlikely(pte_cont(orig_pte))) { + __contpte_try_unfold(mm, addr, ptep, orig_pte); + orig_pte = pte_mknoncont(orig_pte); + } + ___ptep_set_wrprotect(mm, addr, ptep, orig_pte); + } else { + contpte_wrprotect_ptes(mm, addr, ptep, nr); + } +} + #define __HAVE_ARCH_PTEP_SET_WRPROTECT static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { - contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); - __ptep_set_wrprotect(mm, addr, ptep); + wrprotect_ptes(mm, addr, ptep, 1); } #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS @@ -1306,6 +1346,7 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, #define ptep_clear_flush_young __ptep_clear_flush_young #define __HAVE_ARCH_PTEP_SET_WRPROTECT #define ptep_set_wrprotect __ptep_set_wrprotect +#define wrprotect_ptes __wrprotect_ptes #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS #define ptep_set_access_flags __ptep_set_access_flags diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index bfb50e6b44c7..c85e64baf03b 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -23,6 +23,23 @@ static inline pte_t *contpte_align_down(pte_t *ptep) return (pte_t *)(ALIGN_DOWN((unsigned long)ptep >> 3, CONT_PTES) << 3); } +static void contpte_try_unfold_partial(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + /* + * Unfold any partially covered contpte block at the beginning and end + * of the range. + */ + + if (ptep != contpte_align_down(ptep) || nr < CONT_PTES) + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + + if (ptep + nr != contpte_align_down(ptep + nr)) + contpte_try_unfold(mm, addr + PAGE_SIZE * (nr - 1), + ptep + nr - 1, + __ptep_get(ptep + nr - 1)); +} + static void contpte_convert(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) { @@ -236,6 +253,24 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, } EXPORT_SYMBOL(contpte_ptep_clear_flush_young); +void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + /* + * If wrprotecting an entire contig range, we can avoid unfolding. Just + * set wrprotect and wait for the later mmu_gather flush to invalidate + * the tlb. Until the flush, the page may or may not be wrprotected. + * After the flush, it is guarranteed wrprotected. If its a partial + * range though, we must unfold, because we can't have a case where + * CONT_PTE is set but wrprotect applies to a subset of the PTEs; this + * would cause it to continue to be unpredictable after the flush. + */ + + contpte_try_unfold_partial(mm, addr, ptep, nr); + __wrprotect_ptes(mm, addr, ptep, nr); +} +EXPORT_SYMBOL(contpte_wrprotect_ptes); + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty)