From patchwork Mon Dec 23 09:40:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13918643 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 B4A83E7718B for ; Mon, 23 Dec 2024 09:43:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BF4A6B0093; Mon, 23 Dec 2024 04:43:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 46F6D6B0095; Mon, 23 Dec 2024 04:43:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 312026B0096; Mon, 23 Dec 2024 04:43:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 12A106B0093 for ; Mon, 23 Dec 2024 04:43:45 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 86FDB80C5E for ; Mon, 23 Dec 2024 09:43:44 +0000 (UTC) X-FDA: 82925736432.29.9BDBDFB Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf02.hostedemail.com (Postfix) with ESMTP id 45C008000E for ; Mon, 23 Dec 2024 09:42:30 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=gMBZ7jP7; spf=pass (imf02.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.177 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=1734946994; 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=nNP61y0Ujn6NSy9418DvAPU0iqWgc7FA5CXmQY+NH74NMVzxpSSgHpu9n8hgjCrmLgmqWR oRVw5h55bGbyP3lcKjfWTxwgIjOUtFrpPlcLvdPmgvRXSksBdBlYu4AOgs7gTsZK6tzGZo 4xED/FdDQp17KL98wnZdG0WnmvOHzQY= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=gMBZ7jP7; spf=pass (imf02.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734946994; a=rsa-sha256; cv=none; b=oVrOGqliiSIo/NM8nQOgD/FepRCDrwnAD9O2EYnlKh2jCxHLQwYRy5szyJW4dZM+JAAKxm L1yrGvc+3rujTXwLaMLg/BiCywMO/kyjhPjhIjwdZFzQSsw/nKFIGoaXLPHDTtGFSxew6t 53mdwRG5ZnYcWBw1tB7eLsYG34eHph0= Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-728f28744c5so3822247b3a.1 for ; Mon, 23 Dec 2024 01:43:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734947021; x=1735551821; 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=gMBZ7jP7iv4hSzqhk2ML5MI5kE/yFXTyXi+9f3Sgy5Jh/shps/NxOO6+vFP/lTItIh eikoXO/IRIkZ+C/iFYVwdIXXwQmsIIPOvrmSP6rtx6ZyjexP9YEtNkd3A2q2CrBMF/Aj wE/BW7m/IsuQczz636XXnKth3qYsDmZZ2Ke4tbC6qNhTZMzmEP6m0ULUVm36ak9gThQb 3U0pW6nrFsqW28cT4Udp6fmIWq7cq3Kkt0Kz7TrRCKL3Ewugo3c3kH2XuwGVBJiHbSd1 apfRHK0gOrApOcImWYfAOzBGICYFTeQaqY2CgtmjhK+U+gK/Mxn5za5cMKznq3YXSwzY Hbkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734947021; x=1735551821; 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=BCSO3qKGZxvds6iNjtg4XzlzZx5D3Lg/ERtETGZl5rTkS36FNV+dxoR8in75Amudek RLUFTsOVt9LIn5LREX3E3gdBHm6LJ2EXWb54aGG6tLLGDIOoaLdbNdCtGQndKYt+ZFGu JIW1JlS78tpYF1dmVu1vpkvDwnnXhT+Zs7/Rju7Vkt51SUiL0peTwkFC+2NxVkznzdE3 9NY/pai8ajgz6SVmVnMqFIC6XbciViX6K2TJtahy54qsh/HIVjnDipJwTdbIZUp8vFSE RJUOIsYQcv6AbTgPY+e49YDbvtwNVho98k31miz4/qimMLyu8+8o1OztPUdG1Gi0Vtzp 1qsA== X-Gm-Message-State: AOJu0Yz9+MKLCDcN7U6gQj3ATJZvLWKNf9r0HEXCtTDFletP3QSOPSF3 UJxZwnRBofv1knLDYbe4CeFBeuWDgdjvb+3lFCLmgUYg70xjoBDQ1ObpNbRjYPA= X-Gm-Gg: ASbGncuFq2BAk4PMP4eeIDHQrxsqv8Yv/elywOcOeTnSRR8aMQUX48kxdV23AoOWaUG JC4leU1wN63oi9eL0LkMR/NHreyIZ+aYYdrVcbUZ/sKZKOtKKPKoBJESeT+Cwr2PsnTTJpJINBL fmxKXekUrVyqvcC6d4yIadgo5Pz/2MNl2H15uDUPAb1ysOFt5xsOCf4zhRDSX90Ck+0oiT5LEtf WfQITeICIOBjOwOSGdJnf9uYZWG1japq3/eHAFyAP4fqQEvjflQT65VopGJSzOGljDsxXeuBS25 MQ9XCeFIMfQkQcmyk0lrFg== X-Google-Smtp-Source: AGHT+IHP/sWNqLjbiRRokya3RRzP3A7lT6GiLvDE2E3HVCuxopdI/+yKYyLcU7/i5H4uYS+gGEKb/A== X-Received: by 2002:a05:6a21:9017:b0:1e5:ddac:1eff with SMTP id adf61e73a8af0-1e5e04a0c7cmr19747203637.20.1734947021430; Mon, 23 Dec 2024 01:43:41 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad8486c6sm7468309b3a.85.2024.12.23.01.43.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Dec 2024 01:43:40 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, agordeev@linux.ibm.com, kevin.brodsky@arm.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 v3 03/17] asm-generic: pgalloc: Provide generic p4d_{alloc_one,free} Date: Mon, 23 Dec 2024 17:40:49 +0800 Message-Id: <4c4bcc1aa565c6252183553aecd5e5cbd1a0f6ea.1734945104.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: 45C008000E X-Rspamd-Server: rspam12 X-Stat-Signature: sbuigff56nfduuuei8its1fp5q84jq6f X-Rspam-User: X-HE-Tag: 1734946950-614877 X-HE-Meta: U2FsdGVkX19OaQ74OPQqlcdAq8221ItwkMyfGFj6l+rc2Fe4j2rD8+BHui89TzIRVyYQf2VnlNDPVW2z4uaV6DA0W70qCRfXOBYJ4TTAjz/HZKoxtU132YCcdMLBjnGtPqVVwhzkMFxnWwNXTY5bOfu3UfUFR/TTZLmnlSCKEOPu5hgLavRo17asyKD9F65FdTyYV2RrYVdb50Zs/Z7SRrz/Q9lGMDqalGwrYqLB/hs4XsYriUZzSC/2C7cIRDQCwaGh5avHcUPR0IUJ1jnSiG7JLcKen3mtwAJWPSaWWRJ51kifGOAYSBq+XtCO/gXzo6i2wKdRORIWuNhnb3ekS+wBRZ5vpzc6H5wU8S9U51nZqbPP4bXr2DepI9DSzHJbKPKF3jkaBahssJSZ2AV9JMgihVoGz608SBy5oiVTNkwBK10dfw0EXRQ6D5l3p1wmYKfpkirYKmr5mgt5VG2632hwcJNls4SUk7nfPkoe12qYvK3EQAwcirs7eDIwXZtK9QcFs503MSSRtm6mkArUveLy4BO2Zl2ZS4dEbPP84j0DRH7V/IiJu3WP3CooM7ABVbsLCd9wa0YeNqKClYmCapEOHUxgyAuXSP4RmgC/I4V4lmtKboVFy6e9mAH0qwbHJ7DDH8aJ1S1qFCZsXM9KC0cRkEGz7tWHZc2ITRBVS+Q1BoJCs9/NwTQC/p7/tAM7L1gJoK9Qc0vdNWcaRlXdrTpzHJga3WSCq4CZIBo6FCOG6WY/mtCGr+D8k2VYr8o0v0WfMo4GWJQZOmGj0ZLLqghim/vbYiSnbADRZpDylLJ2gGoE7vJ1eCjOGOtTvbjpPykwBSIvhXOqjdN+xNwzxLgXRTvdAq1ByE/53tDoe3pH64Uvoz9WpNraLo6UwXWB/5vnHg1qEBg6W66QvBqAeImyKIMxxLuP9MTPI85O1NMr7gJog0jlAgxJ42XU8V7/2QtXI5TEsZitbDMQY+W Oug8tykd QJCykJUOTQjHLnomUNwhdW47/r24cjd8SDHwsf3Vc4HT8P4zbTCzGoFpKRUfM1OlYeKdjW/RQnHx/shvmrVJllDedVEozWFwK23JOjl369V9PXUqjgIl7SwsqTc/TgpIvARFc4BycwPlxr6ITq6C/IYoRHrcUaAbdTu8Pe70x7P5OCDIynMqHa62VDS+xaIdh48LtQ8kZLCObZ8C09Vh3MwPB1mmw+HC/HYNUN8tOYE4Kgra4F9xlIyFNIKIQu8f/tcLn7BoAILt/NWKVUH7Qbft7WVzWkUq/zpu/MSO8/7eR4Wbn2UoZgZ7jlxW5QJ5Z8y/R9we2WGn2j5T25I/8sIHoybw3Gq/1Z+I0egMDHliBv/EVDpHbUfHzRdamZgYv0RxytszzqLopk+/3EdYtetSOEP8Ozk8NuvF1Wiv7DypB+34+PY708bUMcyabvu475eKCCx8sAhNLFF6Xjozwq5f5n8pkvQqA/97yjldKWMB7OGDJLyv1nGockifyYEe6n4t6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000323, 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 --- 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) {