From patchwork Thu Jun 22 14:42:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13289262 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 0E1A6C0015E for ; Thu, 22 Jun 2023 14:43:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B6098D0012; Thu, 22 Jun 2023 10:43:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53FAF8D000C; Thu, 22 Jun 2023 10:43:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2CD048D0012; Thu, 22 Jun 2023 10:43:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 138D78D000C for ; Thu, 22 Jun 2023 10:43:11 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DAC51AFD7D for ; Thu, 22 Jun 2023 14:43:10 +0000 (UTC) X-FDA: 80930651340.16.1C1CB5C Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf07.hostedemail.com (Postfix) with ESMTP id D7F6940025 for ; Thu, 22 Jun 2023 14:43:08 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf07.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=1687444989; 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=3VXUsl66xsyGZxd0YSBpF82rlW1iasetGIFfl5S2d8w=; b=Z4tKw1g82LUB2WSDQYkDLqFzVsPdwJ0fezsNsXKPyTwAdvO/bnJ7P8BRgPf3XBBarMjSaA Engk3AsQpkaF/mCuKuc82pvFg3COtl5HD79rTOqAZ+Unodev70OkJikzJMU7jdkrX/ADvK 8Rp0PRPtU5tof8iQTP7zvjpV2GIWPvA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf07.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=1687444989; a=rsa-sha256; cv=none; b=Kr+d+zNgRbTR8eJcAYVOo2/EKn5dthvLdc/jnhmEvCpA1UMVwogze1JRZBTsBwLftpQSj3 deie2xxOAkVuX99uz72UxABuryKOmxCL2wA/8KpBrpryUURWQur+crgGVLBgB4w1LxKsqM 3bR2YzOgPe3t+DNL5JE6krzdU9E5888= 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 A45B71042; Thu, 22 Jun 2023 07:43:51 -0700 (PDT) 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 207E63F663; Thu, 22 Jun 2023 07:43:05 -0700 (PDT) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 14/14] arm64/mm: Implement ptep_set_wrprotects() to optimize fork() Date: Thu, 22 Jun 2023 15:42:09 +0100 Message-Id: <20230622144210.2623299-15-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230622144210.2623299-1-ryan.roberts@arm.com> References: <20230622144210.2623299-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 1h7fygr8qbsf9qsywzafk5ymedd7fzb7 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: D7F6940025 X-HE-Tag: 1687444988-105489 X-HE-Meta: U2FsdGVkX18nK/4anJd9DfoV8UrSMtHMrG2bs5Xc12HwaIBXifVi7RB4eJSjxdciA20cBOKPjJqLAZESthr1CR7FAxCUM6crFpdAUxjDXPI2v7hnlSZ33ip3/lXKdO40bxaSi7dZh+RRznMbnTOdvt8uvKdcqaokz37B5tfeVPE6BmNbr2+RS7rpj2jPk+PBeGHL+zTm9mrZ8Aq6u6jeZQEWM5t9g4Bn0RVWGkCKHmTaEKHVxryPncebzEn1QrGslRGybo6MgJA9N5+Ws98+6PqCVdc5mNT5H8pWoLPpCUJc1DE7lFHrqpCVEoscyCvXaqApCnLveUJUTswTD34oMyMZUtvXx5dloBhLIKqVHADKWWL5PqmsLM547DIr3qoOG6ntVsItm5Vkjm2OKXQ8EDaPNAEmwYOCEa//iCINeD3X8nIVBipYjQl5A13IdPFZlwuNks1x+tW2GDdWoM0+uVBcIEYX9bhtJ6YmH/8zM5RiwkmxFgTpd7HMCg5MJT1tRTEW5ExJRCE9tx+xUhv5p5zuxuxpiD0AX0dNiRxcD6Ay98Vw8c+pM4tIo6Edn9E3vxB9bnYBfUlXIFEVM5CtIkPmOz/nK+9lPeCayxCzoazoFYCnakivfGA+JKRA/0U1G7Ul1tqbpye6rw3zt4wvkZScDz0TtDRO6I7jRIjf57g6WfeBI8F4ydPRcd40uP26e+KHSnZuQCcuD0Szyr0mAPPH9ebDYosX7jQrqDxnVHCd1ZJsK8gr3nDF+fj9Xg4tSVRXejRIKKqNhJrMI3/EnZKy+mZ624OKGU+IJsnA+wP7ln2UPfMbatqf5S5gffyPPvo/XH0F5IDIjl8xgLmaqqdKWVE5/1XzvdKPZCIDPX/diGT5kvIqyP+ez6vP7j6vRIbG2DoyLBF5cp/GBLC987bbI+RYQHDDzptWF8qG+ZU16+j+fe7xz/RT6q+7Xdaxgtq+LeULykXBE8RHK51 GckIqT9v 87bOpVWX028AnBzJjwvdXUrLvmKR0lwbk4bntb8RuJCPAAdPUqCI709i09J/pwfSbl08R7FMBSNA1kv6muqYMaoh8QM69H+ae5O6zrJL5VJVn49kwtkc6ebtXCQg+tLFfIJMTZNp9XrdmGc2Jzd/7w+cAeGIdbW6SPXVYLZPGc68NO9kTYybLQT7RFUo10YrjmWkOmZWYF2euE59ZOX9lm/emFWeYyagpK3JbRZh0sD+21WflE4L46jFDs8635u6pT0XKpLvWjTd5K5v8ZGq0kS/+sg== 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: With the core-mm changes in place to batch-copy ptes during fork, we can take advantage of this in arm64 to greatly reduce the number of tlbis we have to issue, and recover the lost fork performance incured when adding support for transparent contiguous ptes. If we are write-protecting a whole contig range, we can apply the write-protection to the whole range and know that it won't change whether the range should have the contiguous bit set or not. For ranges smaller than the contig range, we will still have to unfold, apply the write-protection, then fold if the change now means the range is foldable. Performance tested with the following test written for the will-it-scale framework: ------- char *testcase_description = "fork and exit"; void testcase(unsigned long long *iterations, unsigned long nr) { int pid; char *mem; mem = malloc(SZ_128M); assert(mem); memset(mem, 1, SZ_128M); while (1) { pid = fork(); assert(pid >= 0); if (!pid) exit(0); waitpid(pid, NULL, 0); (*iterations)++; } } ------- I see huge performance regression when PTE_CONT support was added, then the regression is mostly fixed with the addition of this change. The following shows regression relative to before PTE_CONT was enabled (bigger negative value is bigger regression): | cpus | before opt | after opt | |-------:|-------------:|------------:| | 1 | -10.4% | -5.2% | | 8 | -15.4% | -3.5% | | 16 | -38.7% | -3.7% | | 24 | -57.0% | -4.4% | | 32 | -65.8% | -5.4% | Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 48 ++++++++++++++++++++++---------- arch/arm64/mm/contpte.c | 41 +++++++++++++++++++++++++++ arch/arm64/mm/hugetlbpage.c | 2 +- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 5963da651da7..479a9e5ab756 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -963,21 +963,25 @@ 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 + * __ptep_set_wrprotects - 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_wrprotects(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + unsigned int nr) { pte_t old_pte, pte; - - pte = __ptep_get(ptep); - do { - old_pte = pte; - pte = pte_wrprotect(pte); - pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep), - pte_val(old_pte), pte_val(pte)); - } while (pte_val(pte) != pte_val(old_pte)); + unsigned int i; + + for (i = 0; i < nr; i++, address += PAGE_SIZE, ptep++) { + pte = __ptep_get(ptep); + do { + old_pte = pte; + pte = pte_wrprotect(pte); + pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep), + pte_val(old_pte), pte_val(pte)); + } while (pte_val(pte) != pte_val(old_pte)); + } } #ifdef CONFIG_TRANSPARENT_HUGEPAGE @@ -985,7 +989,7 @@ static inline void __ptep_set_wrprotect(struct mm_struct *mm, static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long address, pmd_t *pmdp) { - __ptep_set_wrprotect(mm, address, (pte_t *)pmdp); + __ptep_set_wrprotects(mm, address, (pte_t *)pmdp, 1); } #define pmdp_establish pmdp_establish @@ -1134,6 +1138,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_set_wrprotects(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); @@ -1298,13 +1304,25 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma, return contpte_ptep_clear_flush_young(vma, addr, ptep); } +#define ptep_set_wrprotects ptep_set_wrprotects +static inline void ptep_set_wrprotects(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + if (!contpte_is_enabled(mm)) + __ptep_set_wrprotects(mm, addr, ptep, nr); + else if (nr == 1) { + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + __ptep_set_wrprotects(mm, addr, ptep, 1); + contpte_try_fold(mm, addr, ptep, __ptep_get(ptep)); + } else + contpte_set_wrprotects(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); - contpte_try_fold(mm, addr, ptep, __ptep_get(ptep)); + ptep_set_wrprotects(mm, addr, ptep, 1); } #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 0b585d1c4c94..4f666697547d 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -353,6 +353,47 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, return young; } +void contpte_set_wrprotects(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + unsigned long next; + unsigned long end = addr + (nr << PAGE_SHIFT); + + do { + next = pte_cont_addr_end(addr, end); + nr = (next - addr) >> PAGE_SHIFT; + + /* + * 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. + */ + if (nr != CONT_PTES) + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + + __ptep_set_wrprotects(mm, addr, ptep, nr); + + addr = next; + ptep += nr; + + /* + * If applying to a partial contig range, the change could have + * made the range foldable. Use the last pte in the range we + * just set for comparison, since contpte_try_fold() only + * triggers when acting on the last pte in the contig range. + */ + if (nr != CONT_PTES) + contpte_try_fold(mm, addr - PAGE_SIZE, ptep - 1, + __ptep_get(ptep - 1)); + + } while (addr != end); +} + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 82b2036dbe2f..ecf7bfa761c3 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -511,7 +511,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pte_t pte; if (!pte_cont(__ptep_get(ptep))) { - __ptep_set_wrprotect(mm, addr, ptep); + __ptep_set_wrprotects(mm, addr, ptep, 1); return; }