From patchwork Wed Nov 15 16:30:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13457068 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 BE3D5C072A2 for ; Wed, 15 Nov 2023 16:31:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 955286B039C; Wed, 15 Nov 2023 11:31:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9067D6B039D; Wed, 15 Nov 2023 11:31:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CBB36B039F; Wed, 15 Nov 2023 11:31:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 618496B039D for ; Wed, 15 Nov 2023 11:31:18 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2009A1602A2 for ; Wed, 15 Nov 2023 16:31:18 +0000 (UTC) X-FDA: 81460728636.30.17CBEDF Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf16.hostedemail.com (Postfix) with ESMTP id 40A9D180022 for ; Wed, 15 Nov 2023 16:31:16 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf16.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=1700065876; 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=bsYZKHoCNNIKvSKeL5iLaAvAxp8A686Xb5jxLzFpFHw=; b=T7mcqWBFINrQhzDTsbvdJiC5YzUO9B405w4YfdiIl9TtMNW8tKYR4972nI4BNY/MbpXFjX FIoa9f69j2qAUjHE7rA5ZRCheV7cE2lEuie7eqGwraXZ1CZ10RaS4+MCMAQm5b6gPX6bAe evrw/vLyubkjcj+/9ajPhD9Zww8fkNg= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf16.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=1700065876; a=rsa-sha256; cv=none; b=BmA52j7H7/44xfjytbPP0I+s9TYmqePTGdkGXMhIbcD/kjpKBsU2nPCJ19kamYprw0CHw3 VOOqK1WR+h4fESt1PvPiKG+F5G4gnDmj3MdZhFVS+xivclSF7RA2qsFvoRbKiTqXahDo20 UAsj8Edd72MGbnsIf476kA1phsN0F9w= 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 3633F15DB; Wed, 15 Nov 2023 08:32:01 -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 7CAC83F641; Wed, 15 Nov 2023 08:31:12 -0800 (PST) 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 , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 13/14] arm64/mm: Implement ptep_set_wrprotects() to optimize fork() Date: Wed, 15 Nov 2023 16:30:17 +0000 Message-Id: <20231115163018.1303287-14-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231115163018.1303287-1-ryan.roberts@arm.com> References: <20231115163018.1303287-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 40A9D180022 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: fjqfhqxwtrmw4ytkqpkxi4nfgioy4prb X-HE-Tag: 1700065876-992011 X-HE-Meta: U2FsdGVkX1+6uhYpe3NVn1qFjAsmH2aFc7Fzc1bZbwAGhF4FJyYHZe5febyzigWhBUTU7DHct8EMXa2G7tnX+QMD+eY9Pe68qmTMZopCcfRSk0eHQ4HoW98pACZjRdTmZDClI6LnXHAKASRlaU/sQ5P0alOG+4ZKZi751pmATWOkesVRgoAXAPax5DZzPL4bkVt6A6ajW/u0Q5pznbluRwa0lkPDtyKQC4akx04cXCxSl79s0JS+cJbh+Aiz56L0/U7W7fLhD8KGF6IDg6TwqhUn1lKSawoFyysjXNORauHQDT11KpBi253v7gfhkzIcfCkB4bFLJD3wjKMpMiLAy4CX6F2R952jjGrUNWgX9AZjyC/EFr30dHeTtaePMGY+Vnl+qydJ3/MRIdDpgX5TBztScKh6bmhQ4uuqqZw2uhghFN7EOFUqzDPo9bhb8fCu4PZ5hiuLUOVXqru8qFw0AlFOya+5nrcym1l66ngQecvlS6oq8q0smnGRW9wPS0qqXblH6Mrauu35dFmNANepAuaO7NyBbWNUnvfHYovLCPNZsJDMMDRChjvVJgHK9n/ZWoorbz6pBlrOgZoYSZleP3hRdFGIpnAK0AWec/Z+VVFRSSCcM1cp/IciUpSehqh8ZYAbfyaSywyKcBBdSBCDPE3p2/9gdb3BiYL7twv1R0x6knPTYy0prI7ZS7TZ0tXDKcVuavoUBL3Zach/nXJ2jdCSGzJ/3JSNxyZZKKIdsSkEc6X3woBhiZl6YqMqEqmpqF5i4sDjg+zgJ/RtMyoPc4modNB/KL7Ko21rOQ0QNEjX2w2GSu6rIZzfIPCNPNDDmr1RONIZN82OxDCVDFzQZARY4vjtBWR+ToXjiXZaisy740H7ApjUTwEXzrVd330BGhXJ4QMjpcSSJadN4HlhO82MYcR0KSpyfIwMxgjI1Ip0ZHdriM7YB1KX1pfoUebkt85b7O8qk3jnRnq8ilJ RX8D2FNc aFfVnJ0rciU9gIrBwBB3fdwBrUvWfy/c5Yk557dZnJiNFaX9ncf/YUiqRHE3wWi9JZ1Ni3Ce/uXenCz+TDJdlzdbXKeeWxrk8/980+Uhi1+a9NUmqjC5RIDBcFxMn4VCecS0phlTJ++/X8Q10liIr2FW7dwDAgybeuvqGL4x70/2bDQ7lXdhkr+1b+K9kyblPEL66MArVu7Jkj3OvR+iDtWotvHNnCeixsMrfwwqZJFys6WAq2I6PlAfgm4PE45Sa8ZPavvONVWPyJ8rVCsntbZm3ug== 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: 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. This optimization 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/ 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 | 30 ++++++++++++++++++++--- arch/arm64/mm/contpte.c | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 15bc9cf1eef4..9bd2f57a9e11 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -984,6 +984,16 @@ static inline void __ptep_set_wrprotect(struct mm_struct *mm, } while (pte_val(pte) != pte_val(old_pte)); } +static inline void __ptep_set_wrprotects(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, @@ -1139,6 +1149,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); @@ -1290,13 +1302,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 667bcf7c3260..426be9cd4dea 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -302,6 +302,48 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, } EXPORT_SYMBOL(contpte_ptep_clear_flush_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); +} +EXPORT_SYMBOL(contpte_set_wrprotects); + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty)