From patchwork Thu Dec 19 16:44:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Brodsky X-Patchwork-Id: 13915293 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 89247E77184 for ; Thu, 19 Dec 2024 16:46:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 217C66B0099; Thu, 19 Dec 2024 11:46:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A1736B009A; Thu, 19 Dec 2024 11:46:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 01ADC6B009B; Thu, 19 Dec 2024 11:46:38 -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 D43966B0099 for ; Thu, 19 Dec 2024 11:46:38 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 81D82121791 for ; Thu, 19 Dec 2024 16:46:38 +0000 (UTC) X-FDA: 82912286898.02.2BDA896 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id D288020022 for ; Thu, 19 Dec 2024 16:46:21 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of kevin.brodsky@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=kevin.brodsky@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734626774; 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=KpXAr1GRKOAEyXRCUFZMJrnbzxsTSidU7pgyiULk9+Q=; b=uG3ZY5qCQWPndwq01SLCpwz1ut5ZUGVfzms2sbY8lzhcDN4qMjoWqa+Yyt3+ZbCycoGOJl g2Z4eaeK0Ccit/oBaqawRYAudlEcN9h+7j5munmrbPalsiV1g7vBEDulfB5KjQdrbB7OtO I4iVGZDo7jmqScoHWUF9AWsHiOHelWc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734626774; a=rsa-sha256; cv=none; b=jACXo05EXpYaLa5gjs2odCSM2ba1IJiMU7JjGZCNjjGrS95/eVZ9dWhToHAGNEMtW3JOu9 dlzpttz0WtEw30wCCB81QwOimHSbFz4r9Nuz5Hw4tiezar67N7CR5H2mISb2RvIpn72qYw NjLC3L2CntFue3v0Mh0kWurgf5tf/nw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of kevin.brodsky@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=kevin.brodsky@arm.com; dmarc=pass (policy=none) header.from=arm.com 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 5AAB71D13; Thu, 19 Dec 2024 08:47:04 -0800 (PST) Received: from e123572-lin.arm.com (e123572-lin.cambridge.arm.com [10.1.194.54]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5BE483F58B; Thu, 19 Dec 2024 08:46:32 -0800 (PST) From: Kevin Brodsky To: linux-mm@kvack.org Cc: Kevin Brodsky , Andrew Morton , Catalin Marinas , Dave Hansen , Linus Walleij , Andy Lutomirski , Peter Zijlstra , "Mike Rapoport (IBM)" , Ryan Roberts , Thomas Gleixner , Will Deacon , Matthew Wilcox , linux-alpha@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-um@lists.infradead.org, loongarch@lists.linux.dev, x86@kernel.org Subject: [PATCH 06/10] asm-generic: pgalloc: Provide generic p4d_{alloc_one,free} Date: Thu, 19 Dec 2024 16:44:21 +0000 Message-ID: <20241219164425.2277022-7-kevin.brodsky@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241219164425.2277022-1-kevin.brodsky@arm.com> References: <20241219164425.2277022-1-kevin.brodsky@arm.com> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: D288020022 X-Stat-Signature: bhk3hzrjyxh3f8wysprqkx5euogoxdpq X-Rspam-User: X-HE-Tag: 1734626781-617460 X-HE-Meta: U2FsdGVkX1/Z6Zb1tuvmxqd2ygbXjOIjJwWL9n3EXYKroM55sKrdS9vg3rtWfuWPz40B/POUuJPzMMhOmXniqhQEWdYLA+gT1ycMswGaS8y4F3hIn6cP5nnFPh8hq1cCVojmgXyLWn4nNt0qxnqBCGsVhuecgmdlRZNngqXyeePpAQL6FqVl9GwBRskakp1x92SJTqN9qEqXmzqasU7U62c5upHxwU8GZRZYbLa8QOf3frsmEGBdTLS2+iXnnChn55+Ewk/+kQheboeay2EQwLEgi8VJ0tPosh/SLKXCnuM3BfCtR7A6f9Bzn/g8wzaBw6CGwHCi2z/V8eEecAVsJLjo8+IqzpWHTCC51J/1WKwhl8qrGq4H87/rHb2PFdst2VYjVatbUWnOqUmZPgFFBG3xJYeOKYzTw1R3YWC9/Wj3dHaDM+dlukxxodofgYecwQ+1VX7I4RqLpFc1ukIb4xsG4ZSMuQgYcEcxCrVrpBrvc2b3LoUAiJXm/+c7FwaykN2T4WgPb1QqEN5cjZqC/CFSOAkiio4/AUj26jJo7NPhdnhrZdzK/8D5sh3zT1OIdOqNZAgx4w3AZdPVrNtK+Vi6BDHmhk3HX0QIh+Kf1d0HqjH+jCAw8QlHQ/wwaDDdN+eu1HBBfPHqi+7cK+QMros6j/SfK8m1WRmlD9cdn7EJQ4FjDJOdals9w/HmNe1jA+UvpNuEYd3OkFB+RP929gYBv4hQhWzIoV4eXjo7d1+F9hAzlbDBX/jqo6MPskPzuEhRl6KTIOrOqBGwQKmOIuUyYmr7Wm+7V1Tq03f0u0zDtkf+V8iSDnqsIdmiFBP6oeD/kWzxUGw14Nj5zYFNUILCYg55japhe2H9LIKrN3VyUzSkoHnyTdEcWcwb66ZkKrYzwMgsHK1KhIjN72V22+Ab8ek2S+m1gKjBZoDZ630dFD+9XEE4/3IDTREpT+0ueDOzoQdDNwTZ0GXGEwc vyY/uQCY D6eFPtkB/y+WyHMltw5IktenwfyBnetdQuY1tPhxOk7UmzNkNRZJo3zQ+caeiQs0NFhd/9AhebrC2iTTSc9oaNpTmkCMWB0Izma6/jtkGZxvIvKPPylW8oEeNhjM1A8g8NWa7nmg37UhfLw69z/aGKqvZufBtavuHKfKirXzK6Vex0k0NWEZxJyGA1YV/w1yCdo/ZhuX0twDLnvGR3CO2gZsV4bxcYVbt78vLOpWnefdXRHAk1aFzPlhHI60lkReh8T8ZhnL/fe6FUjNtdHW2u62NcbSoRNToG4ZT7fOW6puaR1RiSrtz+qVYB5ob/ufIK3su6DIl/iRqsmY= 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: Four architectures currently implement 5-level pgtables: arm64, riscv, x86 and s390. The first three have essentially the same implementation for p4d_alloc_one() and p4d_free(), so we've got an opportunity to reduce duplication like at the lower levels. Provide a generic version of p4d_alloc_one() and p4d_free(), and make use of it on those architectures. Their implementation is the same as at PUD level, except that p4d_free() performs a runtime check by calling mm_p4d_folded(). 5-level pgtables depend on a runtime-detected hardware feature on all supported architectures, so we might as well include this check in the generic implementation. No runtime check is required in p4d_alloc_one() as the top-level p4d_alloc() already does the required check. Signed-off-by: Kevin Brodsky --- arch/arm64/include/asm/pgalloc.h | 17 ------------ arch/riscv/include/asm/pgalloc.h | 23 ---------------- arch/x86/include/asm/pgalloc.h | 18 ------------- include/asm-generic/pgalloc.h | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 58 deletions(-) diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h index e75422864d1b..2965f5a7e39e 100644 --- a/arch/arm64/include/asm/pgalloc.h +++ b/arch/arm64/include/asm/pgalloc.h @@ -85,23 +85,6 @@ static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp) __pgd_populate(pgdp, __pa(p4dp), pgdval); } -static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) -{ - gfp_t gfp = GFP_PGTABLE_USER; - - if (mm == &init_mm) - gfp = GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); -} - -static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) -{ - if (!pgtable_l5_enabled()) - return; - BUG_ON((unsigned long)p4d & (PAGE_SIZE-1)); - free_page((unsigned long)p4d); -} - #define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d) #else static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot) diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h index 8ad0bbe838a2..551d614d3369 100644 --- a/arch/riscv/include/asm/pgalloc.h +++ b/arch/riscv/include/asm/pgalloc.h @@ -105,29 +105,6 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, } } -#define p4d_alloc_one p4d_alloc_one -static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) -{ - gfp_t gfp = GFP_PGTABLE_USER; - - if (mm == &init_mm) - gfp = GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); -} - -static inline void __p4d_free(struct mm_struct *mm, p4d_t *p4d) -{ - BUG_ON((unsigned long)p4d & (PAGE_SIZE-1)); - free_page((unsigned long)p4d); -} - -#define p4d_free p4d_free -static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) -{ - if (pgtable_l5_enabled) - __p4d_free(mm, p4d); -} - static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) { diff --git a/arch/x86/include/asm/pgalloc.h b/arch/x86/include/asm/pgalloc.h index dcd836b59beb..dd4841231bb9 100644 --- a/arch/x86/include/asm/pgalloc.h +++ b/arch/x86/include/asm/pgalloc.h @@ -147,24 +147,6 @@ static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4 set_pgd_safe(pgd, __pgd(_PAGE_TABLE | __pa(p4d))); } -static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) -{ - gfp_t gfp = GFP_KERNEL_ACCOUNT; - - if (mm == &init_mm) - gfp &= ~__GFP_ACCOUNT; - return (p4d_t *)get_zeroed_page(gfp); -} - -static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) -{ - if (!pgtable_l5_enabled()) - return; - - BUG_ON((unsigned long)p4d & (PAGE_SIZE-1)); - free_page((unsigned long)p4d); -} - extern void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d); static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, diff --git a/include/asm-generic/pgalloc.h b/include/asm-generic/pgalloc.h index 7c48f5fbf8aa..59131629ac9c 100644 --- a/include/asm-generic/pgalloc.h +++ b/include/asm-generic/pgalloc.h @@ -215,6 +215,51 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud) #endif /* CONFIG_PGTABLE_LEVELS > 3 */ +#if CONFIG_PGTABLE_LEVELS > 4 + +static inline p4d_t *__p4d_alloc_one_noprof(struct mm_struct *mm, unsigned long addr) +{ + gfp_t gfp = GFP_PGTABLE_USER; + struct ptdesc *ptdesc; + + if (mm == &init_mm) + gfp = GFP_PGTABLE_KERNEL; + gfp &= ~__GFP_HIGHMEM; + + ptdesc = pagetable_alloc_noprof(gfp, 0); + if (!ptdesc) + return NULL; + + return ptdesc_address(ptdesc); +} +#define __p4d_alloc_one(...) alloc_hooks(__p4d_alloc_one_noprof(__VA_ARGS__)) + +#ifndef __HAVE_ARCH_P4D_ALLOC_ONE +static inline p4d_t *p4d_alloc_one_noprof(struct mm_struct *mm, unsigned long addr) +{ + return __p4d_alloc_one_noprof(mm, addr); +} +#define p4d_alloc_one(...) alloc_hooks(p4d_alloc_one_noprof(__VA_ARGS__)) +#endif + +static inline void __p4d_free(struct mm_struct *mm, p4d_t *p4d) +{ + struct ptdesc *ptdesc = virt_to_ptdesc(p4d); + + BUG_ON((unsigned long)p4d & (PAGE_SIZE-1)); + pagetable_free(ptdesc); +} + +#ifndef __HAVE_ARCH_P4D_FREE +static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) +{ + if (!mm_p4d_folded(mm)) + __p4d_free(mm, p4d); +} +#endif + +#endif /* CONFIG_PGTABLE_LEVELS > 4 */ + #ifndef __HAVE_ARCH_PGD_FREE static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd) {