From patchwork Wed Jan 8 06:57:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13930194 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 F06A6E77188 for ; Wed, 8 Jan 2025 07:00:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F9A46B0096; Wed, 8 Jan 2025 02:00:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A9AC6B0098; Wed, 8 Jan 2025 02:00:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6229B6B0099; Wed, 8 Jan 2025 02:00:05 -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 4200E6B0096 for ; Wed, 8 Jan 2025 02:00:05 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EED1FA0F6F for ; Wed, 8 Jan 2025 07:00:04 +0000 (UTC) X-FDA: 82983385128.10.C88706E Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf07.hostedemail.com (Postfix) with ESMTP id 15CFF40012 for ; Wed, 8 Jan 2025 07:00:02 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=iBUk+ftZ; spf=pass (imf07.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736319603; 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:dkim-signature; bh=Hi4+N7L/R07Fq6g8WRpMxop4cD3t01a1BHl7awbkQjI=; b=3Mvfo+AyOpau8tTPkrP7bPy0fbff2lT0y6p0xWInTzq69wb2FeaXj758uTnH8XUzLlGiFI ndsrqSrT0SytPEYPq2VS/5M+j3IWNnWT5TfjUQSn0ai425nwX9wcYpZfSLpc/wtAiXBPf+ A9jtYNlR3sGZI1/Ag2svY8vGXUiSRiU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736319603; a=rsa-sha256; cv=none; b=1F2450S+fcEAwFLP1ZZ6KagR07pAFDSPrwpAPvSBimAbb3NIvt/V3vFjbMFV9LOraeslsw g5zsKoWLQVa8ShqR+z/4wn/qiDuPyuI9UOBKUevqo7FLdnI2jLWY96gh0KKlUZIy775yoN uXa6CohvCbkg2EiYL0P3Pj4BbbY8iec= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=iBUk+ftZ; spf=pass (imf07.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2f4409fc8fdso829796a91.1 for ; Tue, 07 Jan 2025 23:00:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1736319601; x=1736924401; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Hi4+N7L/R07Fq6g8WRpMxop4cD3t01a1BHl7awbkQjI=; b=iBUk+ftZtfIeAxrbLt/7+8AkLxPKExeXQK9GEOcwXFQVJJHxy75uD13OGfOvZMPfP8 hSUciaJFmIOBqHUqLbW9dNYlzgrnSqikWZInoZdfBuQSWtjTi9L0CLCKI/mY5wWnVZio wxMHw+z+8Ti8O/kB/CIFZuJTWV9iGXT+zJGyVGPzG99riVfrX1ZXoc3MskGM8/5tWhDK 1cKZe2BxqKA490cPFv6R0OdSLXJqunZbQZ9GsCfs+XbajQ09yZ6j531af076r+36J+Il xhNahoP7po8iCJATFFcuq2m34u8GJ2v134vs5QyHhDGkjucpYNVzVjPA8ppw7WfBplC3 fTVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736319601; x=1736924401; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hi4+N7L/R07Fq6g8WRpMxop4cD3t01a1BHl7awbkQjI=; b=NTm2IN+iDqqoNWjKEDAT+XOjobPue0lJpB6gYbe4aSXBYHgJ0kwxJdn6tEhiPlUDlD N3+21xJ0OccEvYOS3OYjbL1ACrWVesc7XkR6jNbzKorWK2jCS3o6p8fYetFSAoCiVepG sIkKjJqCOkcxYaueQK0EbdaoIAMWi7w4JdHBKobTlDw+/6RgqrM1+ID2KgrkTStueVmo SD6CrYjY7C8Ff9AphqM8VZpVppXAj99Sq/RwUvTFCT12R5n6rotQEcBcb5ZEhJCPILG+ b4VRo/dmJAfe3zLRAJxABB2JZNdUO9miBXFlntVVC2WtFz3SnFUmCXZT65yOj9DnH6f1 hxpA== X-Gm-Message-State: AOJu0YxqsZpCTsOTjF0br6MxS8mGQ4fIhYbCrBM5bhaiPpzHoCCtQBsg Z5tIpML+L8kpfvQcWlmrlIjppbMRsgcUwhcskLQH/B1m5lqBqf+yha6ioqNHFE8= X-Gm-Gg: ASbGnctBAPUGbbZJ+4vUtrFRh8okQAmOAO148T1HyCqhCsUwlbRw5nODZJpP15pSHti IRU1kwV1pNRZpUSWj+JHTVUIxglofAkFOsgWnW7CZLaeIq8xeSiWd+iwm0QMxjrUiItyFlc5l1c ZuSud6rZC/lM5aXv3uyqJcYjpCPijdtKGa0xAmL/9fZ5TCDahAbgWXKimwVvUyeUzIkPM46lPgR 24conwV35IVeENuyH0cK/p+bYoE/SpHWK/YWyLu9dk6mYS47SfXLl/i5teSOktlAJkSwdkJq8dp WnD6nxnuOghaebXbMJMVEhIMHBo= X-Google-Smtp-Source: AGHT+IGjcc9iZhJOiA9Wc8tHUVktzF7/LrG3apZd5+oOKb6c7hgnTezIflh22cvX8oiOIwiosByWAA== X-Received: by 2002:a17:90b:4d09:b0:2ef:2980:4411 with SMTP id 98e67ed59e1d1-2f53cbd9b2fmr9342892a91.9.1736319601534; Tue, 07 Jan 2025 23:00:01 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.244]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dca023a3sm320067275ad.250.2025.01.07.22.59.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 23:00:01 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, agordeev@linux.ibm.com, kevin.brodsky@arm.com, alex@ghiti.fr, andreas@gaisler.com, palmer@dabbelt.com, tglx@linutronix.de, david@redhat.com, jannh@google.com, hughd@google.com, yuzhao@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, lorenzo.stoakes@oracle.com, akpm@linux-foundation.org, rientjes@google.com, vishal.moola@gmail.com, arnd@arndb.de, will@kernel.org, aneesh.kumar@kernel.org, npiggin@gmail.com, dave.hansen@linux.intel.com, rppt@kernel.org, ryan.roberts@arm.com Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-arch@vger.kernel.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-sh@vger.kernel.org, linux-um@lists.infradead.org, Qi Zheng Subject: [PATCH v5 03/17] asm-generic: pgalloc: Provide generic p4d_{alloc_one,free} Date: Wed, 8 Jan 2025 14:57:19 +0800 Message-Id: <26d69c74a29183ecc335b9b407040d8e4cd70c6a.1736317725.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 15CFF40012 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: bzbxqoeyaw7f5r3wn3iatqwm8xi4d6yb X-HE-Tag: 1736319602-684854 X-HE-Meta: U2FsdGVkX1+yrt3GRJkW2mbTi4xAtG4GJSUDJHZ578zTdDlbcNe2Ygbdy7oeP3zMEwNFUm1c4HSX+0aY8FALD8uDEB0EQIImHEYi2OTJ55oDViZUe/eZ4fZUiXZmHm9O45jeRhfudl7xHyAnowMh9KDCdpxfjkLJrY92bywFLd4U6EfZmpwvoRSVggKSvXT/u1FPlHRj+HalvYDWtRqLhr+v/sdpA0YPNyeolFejTKevV+kv/zsYigfQ9UeT20CNFdLM2xiItSKxz3vP4oalUwXwj3eL5GbQtaucojXtKL8DEwdmaMOtp4i8WqCvcQqko5Rb3OYiMVH91/LF4t87oxnlRzFOQuWXsOJaLk2redEzEY8EgDCxs/ps0i7fOHihlucevFa7zMJsRpf718+nM5q5tJ51cpqCA/1Glb7Yt0XpnGnvtR0qV/H3KGWY6ge77FDJazrUrea/y0wwLGlDeBT6PB5KM5mJQvcTWOy2RBTVRXW0+KQKaZmKKjpr7y6PQQnhkkzjOOwTM0oakzFjvtrOthGBDiQI/OEuLmN7XDfaUroShnTVsrJmoNVncG1Hj7zRdAQ29mLgGlXBIH0FtoWg1AQcUxTbI62r9T2tDCIR9Vt7KM0thE3iuu/TjBOBIsewheW/IO+/f1ZdiFcEulV59NYlznqGOAP0k9/HcSgFSWPM3kZehV4+lMt4NQqgb1G/RP7HVXuzyapkJvGNl9QV/1Ok+/NN3qcq3PDkIqzZxXwBU7DpoiQwZkWIcDFOiucwIlTkN5bPXToB7Tc404eJ4ewnUPJx8GSyS0PCX5KIH6s1UsyWbRB7IIzDaNnJr0J+NjUixV/V5A70UX2ZPHeHuUcODfd8msmfJRXKQ/i+Hm8C5YpDa8e7zD98OgIBW/3Oa3TtsncE7S0Jy7FDTJblnYuumu2/NaJbylSKHArDw1TzdWuAU8rOwGrPngi95nWwaz3Kat2cjrBfGHP WndZvTLn 4qNUUJO/Y2JCZ9PccBNsD4KrMjXqLEqREtx6ASKAmtT9sgr7oAsw9Td3SjxOQsg8hs1KijRKZs25BmhLrJXL1kOhWXgKDtPEIU1Oju0J6ZcdY1yyb4mNnRtYwPvOy/TZQ9F2zgmHhL2L3hivghbc0N8/utj9gFdQ0JdZkke5A86XwAvC9CdTanbvDWh7glqZtndVB+u7/PlSyrDWLEXpSzMwb/aR6Igb+fdSxuFGgvlQnrLNdcJriH30fDsQN21X+L1BdH555Xq3XeP1yBXP2eC/bphnqPHbvbfmtUff+6eHMEQ/2tIT2tkcJW2F/Ckb2C9sPKWSLYHRUawylcSBf8YGx0bThJCEFB5b+f+ypgB/OBWrkIc49EjS6DphqSRFGFl+MBcGx3OBceOjijQudwWB/RwuVyagDUwJb7nAnxI2Whps5q/YIUBxzP+BegXMN3H7bCnoSuSXOSbKWx0mrNv0scaeRNhmowB3YDQFdoTxzdI9fMpAUZs6csoemTSPuqOTPFfPo8gqgQUYFy8tPDQCEGIjj793K6I2KV6u9fFWJtjiVaZQWnzHkDGdDlqoEqWFqN4x9LxnbJI0= 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: From: Kevin Brodsky 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 Acked-by: Dave Hansen Signed-off-by: Qi Zheng Acked-by: Arnd Bergmann # asm-generic --- 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 e75422864d1bd..2965f5a7e39e3 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 8ad0bbe838a24..551d614d3369c 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 dcd836b59bebd..dd4841231bb9f 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 7c48f5fbf8aa7..59131629ac9cc 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) {