From patchwork Mon Dec 18 10:51:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496580 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 79567C46CCD for ; Mon, 18 Dec 2023 10:52:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 18A726B00B1; Mon, 18 Dec 2023 05:52:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 114216B00B2; Mon, 18 Dec 2023 05:52:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECF8F6B00B3; Mon, 18 Dec 2023 05:52:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id DE97F6B00B1 for ; Mon, 18 Dec 2023 05:52:18 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B72BF140E5B for ; Mon, 18 Dec 2023 10:52:18 +0000 (UTC) X-FDA: 81579624756.02.791C86C Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf22.hostedemail.com (Postfix) with ESMTP id 1BFAAC001C for ; Mon, 18 Dec 2023 10:52:16 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf22.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=1702896737; 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=Pl80kIjTjpL3O0EXepdffRGD/m7SMioiLS6gBNRB6iM=; b=XT2GH8DtNvlcbMmB2dWIxZ29pJ+zqe3QRkwtFhpgsvKokC5wF9yy8SvkLp20KfPdw3qGPh ORhWdBk+2iYL2F+/p062VpBuD5C5oMSQBYp28MxQ46ow3OSSG7vQuhB8kVJnzCJ3eIesFZ P363haAZv0tLaQvTAWh7EXxRxYDzd+Q= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf22.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=1702896737; a=rsa-sha256; cv=none; b=G//ksPJK2+XzZ9MwICt4MybbVGjXPe4aVFCEcSj7d2k8cFxasxU4tiTUTqohCn9U12/zp3 qflNW9QLdX+vegvVUFqX5spxufb8sH+fW+D6iXQN/OdCT6eVCjnQr5C9Ww1kpyncXwZmPM /upduyjVnQJ4aXsXsJ9899Q9xuheohU= 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 D081E1424; Mon, 18 Dec 2023 02:53:00 -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 00DE33F738; Mon, 18 Dec 2023 02:52: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 , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 16/16] arm64/mm: Implement clear_ptes() to optimize exit, munmap, dontneed Date: Mon, 18 Dec 2023 10:51:00 +0000 Message-Id: <20231218105100.172635-17-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1BFAAC001C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: twzmqgur5s9b7rj9i44oacn31q7s38qf X-HE-Tag: 1702896736-719275 X-HE-Meta: U2FsdGVkX1/4XhelwtPo8fgBB5RTkPnhN2QuuLR/KtIiPpzggQ5tHGg05KLjljZawl+mL/pOo462F+bOYaoRNmgG/U/dOCQ1iksmdI+97GDKvz+9C6eXGYfz29JqoWBhJoeRB5LKBYD1ZjdvdZNcAdHiO3Or0kIwt6loQPQTVUkxLRyD73LGGKPObPd3Sb7gduvqWqwnXek9Z4d4QjT6EIoYKOkGEmzbLPBDg+n53ubn2gsvtctVoLBwDbSA1Nq5VeoNXsFBOZi3Xe9yYUJF19+kuv4DjDS7297Ag30SlgPp8QXpJj+gB1ks8f54wCm7ePMoq2Tf1Et0RcHzdGGHYBCMy753RtoKw7yRWk+2icaiGDFmiC/B5xHMrt6zO/yPL6xPs4Aw5fKXdt7vbHzaHlSYHrwsPYIeK2skDASKbqnsYS8WT6RPnHfbKsp2KH17XdWHbLEDmDwFkbWbptJQMKaSkjV+MBFXqiQ94GIcGoZa7i8JBrvk2ZtAu6ekjE9IZkOT68egptds/siprqsN/DNu10T6GgTVzeJsrAyX6eFa9ulb31m8Uok+S55aNZKZ43AameymLFTiBjN6TmZCJpVDprZPOE7bAjsfjh6dwFazl4K1ya2jh7AMkyU5vJJvjJbqbynRbwBnuKL5NswxCBc1KJC92P0mvsEvQAprHY009Yx2GC4eIFMOts4cQspyHBc2wxmn+oi+HJtzh/w9RuqAf6lSCVBwg+pTiuhD9nJcLx/U/yzbqg9Nxd/fzcqIlJ5M9SPVF3kgoPs/MkDmNJ5VdEhSqX+crVHEFJwq05mY7coc433ASKljIWcqD7x2RKkQG70fKczLP4Uk83inkgeAreOFEvbdHn/zJWSj2KSyOq9TKz1wD+DjkaKEK0vSzO5duFCP2oom3tRuIxQQBVDRWz4zPuwTaq4I9BLMgQn/PQqH/hTQ3CjzCpMNbDQZZeeCughOb30WAbnGzym HC+qwmvD ltbVrDNh4ziu/cvCYOCIqLXCp6jyImNFkCBqlF38Ov5BWA9fJlUtCK4KwjhTDUsyx1X4kMm5BE92XbaU3Qr+7829lEzdTuxTZDy979U2qmHNV/iHXl9JO6glZnWHOq6seNIy3Yja4g6WCQSlCGM7YcntqwT7oIpFvq0uMCEBAhTe4utLgrMA+oug8gNfxiVhFfetHmA3exsItT5XjapeyjsplMyfXoM6Flp4i5Wex5fqL3MOII9jKOKBvwRIjNzwxxYImv2qKgofEVcp86NIlvqYFmNlVdTgoEdrt/X+9zZU8U0o= 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-clear ptes during zap_pte_range(), we can take advantage of this in arm64 to greatly reduce the number of tlbis we have to issue, and recover the lost performance in exit, munmap and madvise(DONTNEED) incured when adding support for transparent contiguous ptes. If we are clearing a whole contpte range, we can elide first unfolding that range and save the tlbis. We just clear the whole range. The following microbenchmark results demonstate the effect of this change on madvise(DONTNEED) performance for large pte-mapped folios. madvise(dontneed) is called for each page of a 1G populated mapping and the total time is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | dontneed | Apple M2 VM | Ampere Altra | | order-9 |-------------------|-------------------| | (pte-map) | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 7.9% | 0.0% | 0.0% | | before-change | -1.3% | 7.0% | 13.0% | 0.0% | | after-change | -9.9% | 0.9% | 14.1% | 0.0% | The memory is initially all contpte-mapped and has to be unfolded (which requires tlbi for the whole block) when the first page is touched (since the test is madvise-ing 1 page at a time). Ampere Altra has high cost for tlbi; this is why cost increases there. The following microbenchmark results demonstate the recovery (and overall improvement) of munmap performance for large pte-mapped folios. munmap is called for a 1G populated mapping and the function runtime is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | munmap | Apple M2 VM | Ampere Altra | | order-9 |-------------------|-------------------| | (pte-map) | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 6.4% | 0.0% | 0.1% | | before-change | 43.3% | 1.9% | 375.2% | 0.0% | | after-change | -6.0% | 1.4% | -0.6% | 0.2% | Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 42 +++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index d4805f73b9db..f5bf059291c3 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -953,6 +953,29 @@ static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, return pte; } +static inline pte_t __clear_ptes(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + unsigned int nr, int full) +{ + pte_t orig_pte = __ptep_get_and_clear(mm, address, ptep); + unsigned int i; + pte_t pte; + + for (i = 1; i < nr; i++) { + address += PAGE_SIZE; + ptep++; + pte = __ptep_get_and_clear(mm, address, ptep); + + if (pte_dirty(pte)) + orig_pte = pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte = pte_mkyoung(orig_pte); + } + + return orig_pte; +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, @@ -1151,6 +1174,8 @@ extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte); extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep); extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte, unsigned int nr); +extern pte_t contpte_clear_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, int full); 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, @@ -1279,6 +1304,22 @@ static inline void pte_clear(struct mm_struct *mm, __pte_clear(mm, addr, ptep); } +#define clear_ptes clear_ptes +static inline pte_t clear_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + pte_t pte; + + if (nr == 1) { + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + pte = __ptep_get_and_clear(mm, addr, ptep); + } else + pte = contpte_clear_ptes(mm, addr, ptep, nr, full); + + return pte; +} + #define __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) @@ -1366,6 +1407,7 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, #define set_pte __set_pte #define set_ptes __set_ptes #define pte_clear __pte_clear +#define clear_ptes __clear_ptes #define __HAVE_ARCH_PTEP_GET_AND_CLEAR #define ptep_get_and_clear __ptep_get_and_clear #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 72e672024785..6f2a15ac5163 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -293,6 +293,51 @@ void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, } EXPORT_SYMBOL(contpte_set_ptes); +pte_t contpte_clear_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + /* + * If we cover a partial contpte block at the beginning or end of the + * batch, unfold if currently folded. This makes it safe to clear some + * of the entries while keeping others. contpte blocks in the middle of + * the range, which are fully covered don't need to be unfolded because + * we will clear the full block. + */ + + unsigned int i; + pte_t pte; + pte_t tail; + + if (!mm_is_user(mm)) + return __clear_ptes(mm, addr, ptep, nr, full); + + 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)); + + pte = __ptep_get_and_clear(mm, addr, ptep); + + for (i = 1; i < nr; i++) { + addr += PAGE_SIZE; + ptep++; + + tail = __ptep_get_and_clear(mm, addr, ptep); + + if (pte_dirty(tail)) + pte = pte_mkdirty(pte); + + if (pte_young(tail)) + pte = pte_mkyoung(pte); + } + + return pte; +} +EXPORT_SYMBOL(contpte_clear_ptes); + int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) {