From mboxrd@z Thu Jan 1 00:00:00 1970 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 255CAC4829A for ; Tue, 13 Feb 2024 18:05:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B1518D0012; Tue, 13 Feb 2024 13:05:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 960A18D0001; Tue, 13 Feb 2024 13:05:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DBC48D0012; Tue, 13 Feb 2024 13:05:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5F37E8D0001 for ; Tue, 13 Feb 2024 13:05:18 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2ED7F140C6D for ; Tue, 13 Feb 2024 18:05:18 +0000 (UTC) X-FDA: 81787557516.18.42F839D Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf07.hostedemail.com (Postfix) with ESMTP id 03CF940024 for ; Tue, 13 Feb 2024 18:05:15 +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=1707847516; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sNhtElb3cqkeCuV3jhCOHAs/n0W8erWB6htmp+SGak4=; b=V0azzk1vgyGA6UKB/HvojME8rdeIEx5WgeFO7QRO10falXREgOhJESoupACfzIjZ//Njqy 4yd8RUDzM2qdSsKhiyP+AztGzkEHpUlYm3d2zpbCj8xK6IHk7DSzeFcSRanlOUFKQBB6IS jvU9LGNdbOGXYNklSQWEPddAtH8AKjM= 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=1707847516; a=rsa-sha256; cv=none; b=MjIXIvytC8PIBCMWpR/Nw8IuYcvwNUwtstQZRjuuMGgp6IAaB+jTgVztEy5AxqCrTo8GtW f1FefmOdKT4KZqddMKaSdKZj057en9pIIPqYJtWpyR8jFFl0u354AOKC/PQZ0DueTo4WLr PYAdqTyppdco2l+4l0aaMdtWNU+CdGY= 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 3C4011FB; Tue, 13 Feb 2024 10:05:56 -0800 (PST) Received: from [10.1.36.184] (XHFQ2J9959.cambridge.arm.com [10.1.36.184]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 08BDE3F766; Tue, 13 Feb 2024 10:05:10 -0800 (PST) Message-ID: <5b4ab6d9-1078-4834-91c4-8715c27b861f@arm.com> Date: Tue, 13 Feb 2024 18:05:09 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 25/25] arm64/mm: Automatically fold contpte mappings Content-Language: en-GB To: Mark Rutland Cc: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , James Morse , Andrey Ryabinin , Andrew Morton , Matthew Wilcox , 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" , linux-arm-kernel@lists.infradead.org, x86@kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240202080756.1453939-1-ryan.roberts@arm.com> <20240202080756.1453939-26-ryan.roberts@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: X-Stat-Signature: wf4au49wphgsjyimm3acy4huwxta58q5 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 03CF940024 X-HE-Tag: 1707847515-328058 X-HE-Meta: U2FsdGVkX1/eVhXD6N2aOTyrelMKLTWhh7tKDnJOB0pvF/RbkjMcvoRKcEecLSMZquI1NKtOtFYokVhMhBiGuGJlZkT5GxfweGsXsm59GpvmHPv4NnpZtSo5yZLzoejn8YNQ9fsXggUMEc5Ro8xKhkRhyJMR5EllCHQPVojeRRob+Vg+oQgvNWttlVSq/x+Jmtd0CMC9MbYTTHvgASPmnlQbMQUVb1ZTkCkFFkduWXx/LgMzPoHva6qbqdXfJAGpLwAcnUM69jGk4f3M+J8SOdaR/tFSX+jk+6sZIph14DrkI+wMbnwDwDsRSA7e82taRt0m195QOjk8NYP+Vj594I6oYccZ5H4d+uJt+0xK5O7gJZlWfCJjgig+QoIkQ+7zTOoBZRTTkGHk3KSqd1PdGfPYtd7PkHjI1R+Qiqez7eeg0kuCSkjJvVKFFZJ6Hx8LZULMLHWurRfikWbxiUzCQhql5tpaRB90D1CkOEMqVhlGBXUV2hXB72pfA0PwBMa3osW1zv1kJ/+PjivQ81nsp/l0mz8AsfkjU3wMqY6aGlJGyZBlQX5SCWVf8a1Mrfraia0Jc/Rr0bDuHo6W2vw7jkTYVRosbP2PW8Kd7+rC4xvjlBnT+3og8Aj4k77frDn1HNFfiVxNBKkjsLg8KJouyQjne4UaNFXIOfI7uXtzd3KKuGBFW3sKXX/TjDIiCO59OHThrAoQBrLsWbT0D1nKDXYR5Ua3k99ZorG/Hihp2N7SyNuXvt9phgNPPrAULTckLyMzsdbFBwy3dVrEp1uxLhNkHxXHExlt2BH45qcIFZ9NAfg4RwzpIe/ej/SyIWzQASblvfB8gJYcu0ppVogFVDnq0JqHqDpx1DMl/1CPbaYFFBmSqRGQJ5uuw4fl6F/8DBn7NWCnHmg7/fuJZwknqZbUkxM96zDusOzy3i9YR7vRdPFhuZuO8TeLzAnfD+PPSdVdK1xRYokvMWGAQAW y8MD4LCS vXMzboVBfZ/in1kYWlgqw3eAtafN1zIL6qU4CJdZWNPqVpSLT9up598oCyDQMMmaMSBUteZ18TnlFT4bD4V2POafDRcQNzfLAiOKkfvcMsJKwJwR5av7W/vJp06k8ziZpcVrzpBotZek70sYQcj7Lg3nhYL3BulSC2+ApZThL1m+nllsqE4RRWUj9cVynpQvwhmryV07gPwDyBvlKKLf0EdI+l+mRmWkLjWt6y9wRgmOVuzjxrEoSiSBGDhsJnzyL6m/BBSWpSQBNfZBYv0Su68FhPQ== 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: On 13/02/2024 17:44, Mark Rutland wrote: > On Fri, Feb 02, 2024 at 08:07:56AM +0000, Ryan Roberts wrote: >> There are situations where a change to a single PTE could cause the >> contpte block in which it resides to become foldable (i.e. could be >> repainted with the contiguous bit). Such situations arise, for example, >> when user space temporarily changes protections, via mprotect, for >> individual pages, such can be the case for certain garbage collectors. >> >> We would like to detect when such a PTE change occurs. However this can >> be expensive due to the amount of checking required. Therefore only >> perform the checks when an indiviual PTE is modified via mprotect >> (ptep_modify_prot_commit() -> set_pte_at() -> set_ptes(nr=1)) and only >> when we are setting the final PTE in a contpte-aligned block. >> >> Signed-off-by: Ryan Roberts >> --- >> arch/arm64/include/asm/pgtable.h | 26 +++++++++++++ >> arch/arm64/mm/contpte.c | 64 ++++++++++++++++++++++++++++++++ >> 2 files changed, 90 insertions(+) >> >> diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h >> index cdc310880a3b..d3357fe4eb89 100644 >> --- a/arch/arm64/include/asm/pgtable.h >> +++ b/arch/arm64/include/asm/pgtable.h >> @@ -1192,6 +1192,8 @@ void vmemmap_update_pte(unsigned long addr, pte_t *ptep, pte_t pte); >> * where it is possible and makes sense to do so. The PTE_CONT bit is considered >> * a private implementation detail of the public ptep API (see below). >> */ >> +extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, >> + pte_t *ptep, pte_t pte); >> extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr, >> pte_t *ptep, pte_t pte); >> extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte); >> @@ -1213,6 +1215,29 @@ extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, >> unsigned long addr, pte_t *ptep, >> pte_t entry, int dirty); >> >> +static __always_inline void contpte_try_fold(struct mm_struct *mm, >> + unsigned long addr, pte_t *ptep, pte_t pte) >> +{ >> + /* >> + * Only bother trying if both the virtual and physical addresses are >> + * aligned and correspond to the last entry in a contig range. The core >> + * code mostly modifies ranges from low to high, so this is the likely >> + * the last modification in the contig range, so a good time to fold. >> + * We can't fold special mappings, because there is no associated folio. >> + */ >> + >> + const unsigned long contmask = CONT_PTES - 1; >> + bool valign = ((addr >> PAGE_SHIFT) & contmask) == contmask; >> + >> + if (unlikely(valign)) { >> + bool palign = (pte_pfn(pte) & contmask) == contmask; >> + >> + if (unlikely(palign && >> + pte_valid(pte) && !pte_cont(pte) && !pte_special(pte))) >> + __contpte_try_fold(mm, addr, ptep, pte); >> + } >> +} >> + >> static __always_inline void contpte_try_unfold(struct mm_struct *mm, >> unsigned long addr, pte_t *ptep, pte_t pte) >> { >> @@ -1287,6 +1312,7 @@ static __always_inline void set_ptes(struct mm_struct *mm, unsigned long addr, >> if (likely(nr == 1)) { >> contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); >> __set_ptes(mm, addr, ptep, pte, 1); >> + contpte_try_fold(mm, addr, ptep, pte); >> } else { >> contpte_set_ptes(mm, addr, ptep, pte, nr); >> } >> diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c >> index 80346108450b..2c7dafd0552a 100644 >> --- a/arch/arm64/mm/contpte.c >> +++ b/arch/arm64/mm/contpte.c >> @@ -67,6 +67,70 @@ static void contpte_convert(struct mm_struct *mm, unsigned long addr, >> __set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES); >> } >> >> +void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, >> + pte_t *ptep, pte_t pte) >> +{ >> + /* >> + * We have already checked that the virtual and pysical addresses are >> + * correctly aligned for a contpte mapping in contpte_try_fold() so the >> + * remaining checks are to ensure that the contpte range is fully >> + * covered by a single folio, and ensure that all the ptes are valid >> + * with contiguous PFNs and matching prots. We ignore the state of the >> + * access and dirty bits for the purpose of deciding if its a contiguous >> + * range; the folding process will generate a single contpte entry which >> + * has a single access and dirty bit. Those 2 bits are the logical OR of >> + * their respective bits in the constituent pte entries. In order to >> + * ensure the contpte range is covered by a single folio, we must >> + * recover the folio from the pfn, but special mappings don't have a >> + * folio backing them. Fortunately contpte_try_fold() already checked >> + * that the pte is not special - we never try to fold special mappings. >> + * Note we can't use vm_normal_page() for this since we don't have the >> + * vma. >> + */ >> + >> + unsigned long folio_saddr, folio_eaddr; >> + unsigned long cont_saddr, cont_eaddr; >> + pte_t expected_pte, subpte; >> + struct folio *folio; >> + struct page *page; >> + unsigned long pfn; >> + pte_t *orig_ptep; >> + pgprot_t prot; >> + >> + int i; >> + >> + if (!mm_is_user(mm)) >> + return; >> + >> + page = pte_page(pte); >> + folio = page_folio(page); >> + folio_saddr = addr - (page - &folio->page) * PAGE_SIZE; >> + folio_eaddr = folio_saddr + folio_nr_pages(folio) * PAGE_SIZE; >> + cont_saddr = ALIGN_DOWN(addr, CONT_PTE_SIZE); >> + cont_eaddr = cont_saddr + CONT_PTE_SIZE; > > I assume that the 's' in *_sddar is for "start", and the 'e' in *_eaddr is for > "end". Could we use "start" and "end" directly, e.g. folio_start, folio_end? ACK; will fix. > >> + >> + if (folio_saddr > cont_saddr || folio_eaddr < cont_eaddr) >> + return; >> + >> + pfn = pte_pfn(pte) - ((addr - cont_saddr) >> PAGE_SHIFT); > > IIUC this should be the same as: > > pfn = ALIGN_DOWN(pte_pfn(pte), NR_CONT_PTES); > > ... which would align with the way we generate 'cont_saddr' above. ACK; will fix. > > Otherwise, this looks good to me. Great thanks! I'll get these changes done and rebase onto mm-unstable once David's zap batching series is in, retest and re-post (hopefully in the next couple of days!) > > Mark. > >> + prot = pte_pgprot(pte_mkold(pte_mkclean(pte))); >> + expected_pte = pfn_pte(pfn, prot); >> + orig_ptep = ptep; >> + ptep = contpte_align_down(ptep); >> + >> + for (i = 0; i < CONT_PTES; i++) { >> + subpte = pte_mkold(pte_mkclean(__ptep_get(ptep))); >> + if (!pte_same(subpte, expected_pte)) >> + return; >> + expected_pte = pte_advance_pfn(expected_pte, 1); >> + ptep++; >> + } >> + >> + pte = pte_mkcont(pte); >> + contpte_convert(mm, addr, orig_ptep, pte); >> +} >> +EXPORT_SYMBOL(__contpte_try_fold); >> + >> void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr, >> pte_t *ptep, pte_t pte) >> { >> -- >> 2.25.1 >>