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 D92FBC02194 for ; Thu, 6 Feb 2025 11:53:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB9C36B0082; Thu, 6 Feb 2025 06:53:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D696B6B0083; Thu, 6 Feb 2025 06:53:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C30F06B0085; Thu, 6 Feb 2025 06:53:38 -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 A4B766B0082 for ; Thu, 6 Feb 2025 06:53:38 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2793F8115C for ; Thu, 6 Feb 2025 11:53:24 +0000 (UTC) X-FDA: 83089359528.08.C125ED9 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf10.hostedemail.com (Postfix) with ESMTP id 65D66C005C for ; Thu, 6 Feb 2025 11:53:22 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf10.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738842802; 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=dCSIGE2wMg1P8xDRWTGSRIaw1+BvNT3ut4B4Mu1BE8Q=; b=npLU+kf7nEsTOjNcos5LX0v9dy6AH1fCjYWlAAyLyUrpQ4cA8YZqDe0MUSHJ8N+nFy3UF9 2ozY81+oOf3UEGEuSiOyeofPAymEo6XZp6gGazv6UgzxUQQUdq+9NuWTE6ozK0ZRd3Ade1 Ov+Zr6JbuvKvsq95Fv38y4+TUiCaB/M= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf10.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738842802; a=rsa-sha256; cv=none; b=iGQgRVxUF4j/Zqq6LPHLFPdjS8H7BTSRfveYoM7k66Qh/5+Tv+no8MCS1EjY/OVxS434JQ kuza4FSxJIg3rTgNsXkIiBwwWuMCJ7ZTl8CnLSUDClGBnqsKtlZInv9PpDfy8ZspRZ/DYt fItXKjQKf9g8Om4bbKZ5Zjt48aNllaU= 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 1A47312FC; Thu, 6 Feb 2025 03:48:44 -0800 (PST) Received: from [10.163.34.115] (unknown [10.163.34.115]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0405E3F5A1; Thu, 6 Feb 2025 03:48:14 -0800 (PST) Message-ID: Date: Thu, 6 Feb 2025 17:18:12 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 06/16] arm64/mm: Refactor __set_ptes() and __ptep_get_and_clear() To: Ryan Roberts , Catalin Marinas , Will Deacon , Muchun Song , Pasha Tatashin , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Mark Rutland , Ard Biesheuvel , Dev Jain , Alexandre Ghiti , Steve Capper , Kevin Brodsky Cc: linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20250205151003.88959-1-ryan.roberts@arm.com> <20250205151003.88959-7-ryan.roberts@arm.com> Content-Language: en-US From: Anshuman Khandual In-Reply-To: <20250205151003.88959-7-ryan.roberts@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 65D66C005C X-Stat-Signature: nznhiumsbe9cng8jpz3aqi97gkxo8szd X-Rspam-User: X-HE-Tag: 1738842802-492657 X-HE-Meta: U2FsdGVkX1+m8RrY3eFuxtxlUVYabq+hKWQPnopdgGpoAnoeJ569OVRbnefLL9gl/qrPc0cadQ7nG1wqxjWEciDnlASVt6pFbLZwAADue9aS0qX/8r1dMjaJFaVDieUgIw91ORNCfu9vsDG1ne/Wi/T7Nkf+vtuq7yyxM67EZ9zlFJ5Y0LEHBdO8buNMJ8SEouxGLyfe9rS1TCuffneeu3n9FLHtC5DPKl3AV8zL/k1gJO12K46iAgXBqvcTALWj9fylRBOF1o7MwW+Cazk5pVLFTrbRMF515Ys2k3QSigNfb+GVkCLBcJy2Ncmv1qg2VlNUyCgPenKBxP2e/AuxwyInbAybX/Sr0bm1RRDtMPzsAb4FwKO8NP2WRT50T+RF5xB6D47msXoe5LNGcWLacRXXLvyr2oiakVAHIV0ZTE3IHK7a4QiJ45blWpe2G/Pu+N1psEJKLosVYNC4zuaRsFVNA9PVgicmLmpz6FZihqLTqbECC7X9X1Lapi/CC3TPfxbWH3s18dHGJDShKwudSpw3F0oK3aCN2TWl1nga0lFPbqQCNPLf9dCx5tCPBfwspWIAMxHfaT+DVHmX2X1O0+6zigsfaJnKFkrqg3OduoJyL0AuOGAz2z36txtpECDMoptWwHqHjXgUI53HtAAGsK02eqg9m/mFTa1nrLaA9tJY4s+oN6KsIGzOF9k5GxaLDTsGVDsno6ZyJegJkJs/AmUR9PMRGHN8cICyZBeBDiz9hqU2NB25pBK5PYfA/Km8I3gBzN21Xq4nbxPKwF2PXJUcu19YUrqJOFPpT+fgItaKYqtMaTwxlK8BWjS53TsWCDtNpREcYtCDNk9/7f17Ut4c+mJiIS7Sfv/U9F6+PKXNkvJb5alMCzctmCB9cpWNseT+VS1B11REEvCup5uils+wKF1qkDpAvcRhxsPl5FV/JOjXsr4V9Jzvm1xd7Ndb4+zU7J3UXE2G//il9Pe AS+Jw+SV TYilQ8E335pq4Av87O15uAGRwN8KrZsQgsam2vYscPSrGoTNtplNyN3qk0P2wjSVuVQ3vShEWvsUPZe5v8XlZQY6k1smeN4D/COXppBOR9XMELzC5lCRA40WTyqAAYpe2huTFp3JJBTSzOAKuds8/BOYZpw7uhmVJB0LCIBUKFXrwFsWspcheRdbAMh4OhnJOK0tTXKpg6q7W4iBSOTZTmjpShGY6+MrnHWAYRyeMnX5/obfCpZQj903MxA== 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 2/5/25 20:39, Ryan Roberts wrote: > Refactor __set_ptes(), set_pmd_at() and set_pud_at() so that they are > all a thin wrapper around a generic ___set_ptes(), which takes pgsize s/generic/common - as generic might be misleading for being generic MM. > parameter. This cleans up the code to remove the confusing > __set_pte_at() (which was only ever used for pmd/pud) and will allow us > to perform future barrier optimizations in a single place. Additionally, > it will permit the huge_pte API to efficiently batch-set pgtable entries > and take advantage of the future barrier optimizations. Makes sense. > > ___set_ptes() calls the correct page_table_check_*_set() function based > on the pgsize. This means that huge_ptes be able to get proper coverage > regardless of their size, once it's plumbed into huge_pte. Currently the > huge_pte API always uses the pte API which assumes an entry only covers > a single page. Right > > While we are at it, refactor __ptep_get_and_clear() and > pmdp_huge_get_and_clear() to use a common ___ptep_get_and_clear() which > also takes a pgsize parameter. This will provide the huge_pte API the > means to clear ptes corresponding with the way they were set. __ptep_get_and_clear() refactoring does not seem to be related to the previous __set_ptes(). Should they be separated out into two different patches instead - for better clarity and review ? Both these clean ups have enough change and can stand own their own. > > Signed-off-by: Ryan Roberts > --- > arch/arm64/include/asm/pgtable.h | 108 +++++++++++++++++++------------ > 1 file changed, 67 insertions(+), 41 deletions(-) > > diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h > index 0b2a2ad1b9e8..3b55d9a15f05 100644 > --- a/arch/arm64/include/asm/pgtable.h > +++ b/arch/arm64/include/asm/pgtable.h > @@ -420,23 +420,6 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) > return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte)); > } > > -static inline void __set_ptes(struct mm_struct *mm, > - unsigned long __always_unused addr, > - pte_t *ptep, pte_t pte, unsigned int nr) > -{ > - page_table_check_ptes_set(mm, ptep, pte, nr); > - __sync_cache_and_tags(pte, nr); > - > - for (;;) { > - __check_safe_pte_update(mm, ptep, pte); > - __set_pte(ptep, pte); > - if (--nr == 0) > - break; > - ptep++; > - pte = pte_advance_pfn(pte, 1); > - } > -} > - > /* > * Hugetlb definitions. > */ > @@ -641,30 +624,59 @@ static inline pgprot_t pud_pgprot(pud_t pud) > return __pgprot(pud_val(pfn_pud(pfn, __pgprot(0))) ^ pud_val(pud)); > } > > -static inline void __set_pte_at(struct mm_struct *mm, > - unsigned long __always_unused addr, > - pte_t *ptep, pte_t pte, unsigned int nr) > +static inline void ___set_ptes(struct mm_struct *mm, pte_t *ptep, pte_t pte, > + unsigned int nr, unsigned long pgsize) So address got dropped and page size got added as an argument. s/___set_ptes/___set_pxds ? to be more generic for all levels. > { > - __sync_cache_and_tags(pte, nr); > - __check_safe_pte_update(mm, ptep, pte); > - __set_pte(ptep, pte); > + unsigned long stride = pgsize >> PAGE_SHIFT; > + > + switch (pgsize) { > + case PAGE_SIZE: > + page_table_check_ptes_set(mm, ptep, pte, nr); > + break; > + case PMD_SIZE: > + page_table_check_pmds_set(mm, (pmd_t *)ptep, pte_pmd(pte), nr); > + break; > + case PUD_SIZE: > + page_table_check_puds_set(mm, (pud_t *)ptep, pte_pud(pte), nr); > + break; This is where the new page table check APIs get used for batch testing. > + default: > + VM_WARN_ON(1); > + } > + > + __sync_cache_and_tags(pte, nr * stride); > + > + for (;;) { > + __check_safe_pte_update(mm, ptep, pte); > + __set_pte(ptep, pte); > + if (--nr == 0) > + break; > + ptep++; > + pte = pte_advance_pfn(pte, stride); > + } > } LGTM > > -static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr, > - pmd_t *pmdp, pmd_t pmd) > +static inline void __set_ptes(struct mm_struct *mm, > + unsigned long __always_unused addr, > + pte_t *ptep, pte_t pte, unsigned int nr) > { > - page_table_check_pmd_set(mm, pmdp, pmd); > - return __set_pte_at(mm, addr, (pte_t *)pmdp, pmd_pte(pmd), > - PMD_SIZE >> PAGE_SHIFT); > + ___set_ptes(mm, ptep, pte, nr, PAGE_SIZE); > } > > -static inline void set_pud_at(struct mm_struct *mm, unsigned long addr, > - pud_t *pudp, pud_t pud) > +static inline void __set_pmds(struct mm_struct *mm, > + unsigned long __always_unused addr, > + pmd_t *pmdp, pmd_t pmd, unsigned int nr) > +{ > + ___set_ptes(mm, (pte_t *)pmdp, pmd_pte(pmd), nr, PMD_SIZE); > +} > +#define set_pmd_at(mm, addr, pmdp, pmd) __set_pmds(mm, addr, pmdp, pmd, 1) > + > +static inline void __set_puds(struct mm_struct *mm, > + unsigned long __always_unused addr, > + pud_t *pudp, pud_t pud, unsigned int nr) > { > - page_table_check_pud_set(mm, pudp, pud); > - return __set_pte_at(mm, addr, (pte_t *)pudp, pud_pte(pud), > - PUD_SIZE >> PAGE_SHIFT); > + ___set_ptes(mm, (pte_t *)pudp, pud_pte(pud), nr, PUD_SIZE); > } > +#define set_pud_at(mm, addr, pudp, pud) __set_puds(mm, addr, pudp, pud, 1) LGTM > > #define __p4d_to_phys(p4d) __pte_to_phys(p4d_pte(p4d)) > #define __phys_to_p4d_val(phys) __phys_to_pte_val(phys) > @@ -1276,16 +1288,34 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma, > } > #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */ > > -static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, > - unsigned long address, pte_t *ptep) > +static inline pte_t ___ptep_get_and_clear(struct mm_struct *mm, pte_t *ptep, > + unsigned long pgsize) So address got dropped and page size got added as an argument. > { > pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0)); > > - page_table_check_pte_clear(mm, pte); > + switch (pgsize) { > + case PAGE_SIZE: > + page_table_check_pte_clear(mm, pte); > + break; > + case PMD_SIZE: > + page_table_check_pmd_clear(mm, pte_pmd(pte)); > + break; > + case PUD_SIZE: > + page_table_check_pud_clear(mm, pte_pud(pte)); > + break; > + default: > + VM_WARN_ON(1); > + } > > return pte; > } > > +static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, > + unsigned long address, pte_t *ptep) > +{ > + return ___ptep_get_and_clear(mm, ptep, PAGE_SIZE); > +} > + > static inline void __clear_full_ptes(struct mm_struct *mm, unsigned long addr, > pte_t *ptep, unsigned int nr, int full) > { > @@ -1322,11 +1352,7 @@ static inline pte_t __get_and_clear_full_ptes(struct mm_struct *mm, > static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, > unsigned long address, pmd_t *pmdp) > { > - pmd_t pmd = __pmd(xchg_relaxed(&pmd_val(*pmdp), 0)); > - > - page_table_check_pmd_clear(mm, pmd); > - > - return pmd; > + return pte_pmd(___ptep_get_and_clear(mm, (pte_t *)pmdp, PMD_SIZE)); > } > #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ > Although currently there is no pudp_huge_get_and_clear() helper on arm64 reworked ___ptep_get_and_clear() will be able to support that as well if and when required as it now supports PUD_SIZE page size.