From patchwork Sat Dec 14 09:02:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13908427 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 588BBE7716A for ; Sat, 14 Dec 2024 09:03:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1DEB6B0089; Sat, 14 Dec 2024 04:03:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DA6816B008A; Sat, 14 Dec 2024 04:03:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFA686B008C; Sat, 14 Dec 2024 04:03:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9EB0C6B0089 for ; Sat, 14 Dec 2024 04:03:58 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 53116C1C5C for ; Sat, 14 Dec 2024 09:03:58 +0000 (UTC) X-FDA: 82892976600.12.CC0E380 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf19.hostedemail.com (Postfix) with ESMTP id 58CA91A0011 for ; Sat, 14 Dec 2024 09:03:28 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=lfE0godM; spf=pass (imf19.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.175 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=1734167018; 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=ogKjScRTvWjbxx3zCDWukawvbFfMJH57eI/cAcIUhW0=; b=SgjAbuuuVbqI3MAtKiNuFYBf5ZCEN9jGHeXTRv1vaKwilRbO++Ndx6+cpbCPk6OpZ+Oe6q rVClZC1NERz4MinCA8ZcN3RAI8R3qvWuNc35HTiAXwAPTsQmueUBp0dd527l5573To/qyz r0OkWZyw7Ufi3gDuZI/Of9Fsw4eQ1rw= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=lfE0godM; spf=pass (imf19.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.175 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=1734167018; a=rsa-sha256; cv=none; b=wwQCGgHs5RN5CKre07xxsMWJYAsJWBsMeIjJ599CKRU3U7HMKRV1fUQ393XtEp3BI0WPq1 ERgrENCNFIsQWqd9sQIxPiPc5EvTCKPzo3kANw4KBy5VEyomzmN4SWIfSdIZxhtpKQrf/f bWACtvNiFBQXf+dgP4H5rsWqK1K2lXg= Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-725ed193c9eso2117034b3a.1 for ; Sat, 14 Dec 2024 01:03:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734167035; x=1734771835; 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=ogKjScRTvWjbxx3zCDWukawvbFfMJH57eI/cAcIUhW0=; b=lfE0godMtaU0M0CsHVO1pKL/g7Me2tjqoHoe/miX4UECY39B8qDPv1WzThOJBRrsPj KQxbEcpThZAV1Evd5voL6K2bbTT/ThzB/vC11GRfjzqYVax9zJTBlXk6HnwXrkSGyLtv Q4vJ2lhtgAc2BAqubApqrFI/3w4C/CoTboyIFxaHtfjJvDyjtThSbKJIdKj8lmVW40gk bazwCnRT551pwV/btiWe2vf0pEyu3eegWPW6uy4qX+t2jq0eM1yp+Cnizsb3BS2mmT3a iP/P/Sq65+f+ExkJJyHvdgEcf7H1Ut4JdjA7IS4btGcyl4scx5kUi/4c9/1XFeQuT3KZ P8tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734167035; x=1734771835; 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=ogKjScRTvWjbxx3zCDWukawvbFfMJH57eI/cAcIUhW0=; b=ZeSKVC09A79QlDqBgBIeyOp+gXDk1rtR5OPh+7+Ep1ym+FEdi1D5HF+0OrN6yq9R0k 0Kc6pmDIsa7pTauhINr3fXWw+r7BqaFJwwULvo62JZm5+hdvrwQMMQhzB1RA3atZHnOt QgupD4fmM7Jn0UImn++VY1x/DRnm2zkld8DyaNkw/byPDOp05DG8AtBQbvWU+BcYqXZA oAxyP++tH5+NlZAjlhxqUd0t5q0Y95A4t+pS0+DssJkbNYoVDbdgf6PdKM5DaF6Mix6q eKzrdPb/Fd6BJ4eNnFxywqNN3iW5JLdm0Qaq+LbUrpj/fJCUJgPQAPJeRNgetBanZPo5 w/7Q== X-Gm-Message-State: AOJu0YzsPCtjdjhM6i6AJil63eCapESoUyp+X9P+z8H1sw89NmGt8HV8 n6M+N16QmJp4/CBbmLMxd2F007fw1OfDsaWVwqNdz62qd8TBa8thcz1wyMGS4mg= X-Gm-Gg: ASbGnct6Ig2QrN1UJhX97vVSxC9AKRCcRQvak51wvVbtvMIGN4uyX2T0msDQxvJ+ELh jYuPPkLDCE1QTq++FENXu2KPdl9nOAwTWNJ/UJcqaRznfpR7bpEHYIJoOH2p5h6GXnVaKpvjHE+ 61lu3wqun6G7m+TUeZCNQ8R0WCYhPp1Ol1YQjhlbv/Xtdweg8ONx/ZuXKEXkx+4efXCtwp8FcHa LXPPDQ1hKdTm3MGLBpL3XnrdPjADPt5R9paBiZkNhMJL/nl90ltdVOk4M4NjmDpolS7UTWTRF/W 2TFOkcXKkL0V1Dby0i784s/TNYjtFw== X-Google-Smtp-Source: AGHT+IH8cEofDdPO5AW+ExE/DB+fdraWijYPkixsUFC1j8JWg1c9+2I6Y5RryDFAZbl9xgKYU+lFhw== X-Received: by 2002:a05:6a20:9f05:b0:1d9:6c9c:75ea with SMTP id adf61e73a8af0-1e1dfbfbe78mr8233213637.5.1734167035315; Sat, 14 Dec 2024 01:03:55 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([2409:8a28:f4f:a9a4:5d46:4ef0:daa9:1db8]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-801d5a90ba0sm905731a12.16.2024.12.14.01.03.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Dec 2024 01:03:54 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, 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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng Subject: [PATCH 02/12] mm: pgtable: introduce generic p4d_alloc_one() and p4d_free() Date: Sat, 14 Dec 2024 17:02:48 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Stat-Signature: y4af1oxf8ffx6o1p34mmk7pfwwrt4oj8 X-Rspamd-Queue-Id: 58CA91A0011 X-Rspam-User: X-HE-Tag: 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ldpyx1ElyDi1GMkbOUwM181apHYU65GyKtDTsN2FGPDk6VfzCui4WnhT/1rsBFZ0oe/uQkG+9qFifwF5/Ynk7qHTg0du4T98E0Xw3gZxftNWqVYFRLXBUjlvH0mVI4P0W1T3mn3Q9KdegSsY2L4NP71hcnUrw1bHcdbIG5aSbG66grC0vuk9vreur8m9eENqxlQMuuZH4yb3zX0qpppc6xn9cLcuccWEq/D4szJvO6RM8D9UuUZGhhb1w1KpvF+oovLCbMD4Fakl4s/y6ui4iyNKSK3zVp0oIwGupeDiQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Several architectures (arm64, riscv, x86) define p4d_alloc_one() as a wrapper for get_zeroed_page() and p4d_free() as a wrapper for free_page(). For these architectures, provide a generic implementation in asm-generic/pgalloc.h and convert them to use it. And like other levels of page tables, add statistics for P4D level page table. For s390, it also defines p4d_alloc_one() and p4d_free(), but it uses its own logic, so skip it. Signed-off-by: Qi Zheng --- arch/arm64/include/asm/pgalloc.h | 15 ++++----- arch/riscv/include/asm/pgalloc.h | 25 ++++++--------- arch/x86/include/asm/pgalloc.h | 16 ++++------ arch/x86/mm/pgtable.c | 3 ++ include/asm-generic/pgalloc.h | 55 ++++++++++++++++++++++++++++++++ include/linux/mm.h | 16 ++++++++++ 6 files changed, 98 insertions(+), 32 deletions(-) diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h index e75422864d1bd..679c530549327 100644 --- a/arch/arm64/include/asm/pgalloc.h +++ b/arch/arm64/include/asm/pgalloc.h @@ -15,6 +15,8 @@ #define __HAVE_ARCH_PGD_FREE #define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t)) @@ -87,19 +89,16 @@ static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp) static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) { - gfp_t gfp = GFP_PGTABLE_USER; + if (!pgtable_l5_enabled()) + return NULL; - if (mm == &init_mm) - gfp = GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); + return __p4d_alloc_one(mm, addr); } 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); + if (pgtable_l5_enabled()) + __p4d_free(mm, p4d); } #define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d) diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h index f52264304f772..bb6e1c5f1fb19 100644 --- a/arch/riscv/include/asm/pgalloc.h +++ b/arch/riscv/include/asm/pgalloc.h @@ -14,6 +14,8 @@ #ifdef CONFIG_MMU #define __HAVE_ARCH_PUD_ALLOC_ONE #define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include static inline void riscv_tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt) @@ -118,21 +120,10 @@ 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) { - if (pgtable_l5_enabled) { - gfp_t gfp = GFP_PGTABLE_USER; - - if (mm == &init_mm) - gfp = GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); - } + if (!pgtable_l5_enabled) + return NULL; - return NULL; -} - -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); + return __p4d_alloc_one(mm, addr); } #define p4d_free p4d_free @@ -145,8 +136,12 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) { - if (pgtable_l5_enabled) + if (pgtable_l5_enabled) { + struct ptdesc *ptdesc = virt_to_ptdesc(p4d); + + pagetable_p4d_dtor(ptdesc); riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); + } } #endif /* __PAGETABLE_PMD_FOLDED */ diff --git a/arch/x86/include/asm/pgalloc.h b/arch/x86/include/asm/pgalloc.h index dcd836b59bebd..d9bc6cae77c9e 100644 --- a/arch/x86/include/asm/pgalloc.h +++ b/arch/x86/include/asm/pgalloc.h @@ -8,6 +8,8 @@ #define __HAVE_ARCH_PTE_ALLOC_ONE #define __HAVE_ARCH_PGD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include static inline int __paravirt_pgd_alloc(struct mm_struct *mm) { return 0; } @@ -149,20 +151,16 @@ static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4 static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) { - gfp_t gfp = GFP_KERNEL_ACCOUNT; + if (!pgtable_l5_enabled()) + return NULL; - if (mm == &init_mm) - gfp &= ~__GFP_ACCOUNT; - return (p4d_t *)get_zeroed_page(gfp); + return __p4d_alloc_one(mm, addr); } 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); + if (pgtable_l5_enabled()) + return __p4d_free(mm, p4d); } extern void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d); diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 69a357b15974a..3d6e84da45b24 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -94,6 +94,9 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud) #if CONFIG_PGTABLE_LEVELS > 4 void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d) { + struct ptdesc *ptdesc = virt_to_ptdesc(p4d); + + pagetable_p4d_dtor(ptdesc); paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT); paravirt_tlb_remove_table(tlb, virt_to_page(p4d)); } diff --git a/include/asm-generic/pgalloc.h b/include/asm-generic/pgalloc.h index 7c48f5fbf8aa7..dbf61819b3581 100644 --- a/include/asm-generic/pgalloc.h +++ b/include/asm-generic/pgalloc.h @@ -215,6 +215,61 @@ 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; + + pagetable_p4d_ctor(ptdesc); + return ptdesc_address(ptdesc); +} +#define __p4d_alloc_one(...) alloc_hooks(__p4d_alloc_one_noprof(__VA_ARGS__)) + +#ifndef __HAVE_ARCH_P4D_ALLOC_ONE +/** + * p4d_alloc_one - allocate memory for a P4D-level page table + * @mm: the mm_struct of the current context + * + * Allocate memory for a page table using %GFP_PGTABLE_USER for user context + * and %GFP_PGTABLE_KERNEL for kernel context. + * + * Return: pointer to the allocated memory or %NULL on error + */ +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_p4d_dtor(ptdesc); + pagetable_free(ptdesc); +} + +#ifndef __HAVE_ARCH_P4D_FREE +static inline void p4d_free(struct mm_struct *mm, pud_t *p4d) +{ + __p4d_free(mm, p4d); +} +#endif + +#endif + #ifndef __HAVE_ARCH_PGD_FREE static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd) { diff --git a/include/linux/mm.h b/include/linux/mm.h index 5e73e53c34e9e..807a12ed8ec96 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3237,6 +3237,22 @@ static inline void pagetable_pud_dtor(struct ptdesc *ptdesc) lruvec_stat_sub_folio(folio, NR_PAGETABLE); } +static inline void pagetable_p4d_ctor(struct ptdesc *ptdesc) +{ + struct folio *folio = ptdesc_folio(ptdesc); + + __folio_set_pgtable(folio); + lruvec_stat_add_folio(folio, NR_PAGETABLE); +} + +static inline void pagetable_p4d_dtor(struct ptdesc *ptdesc) +{ + struct folio *folio = ptdesc_folio(ptdesc); + + __folio_clear_pgtable(folio); + lruvec_stat_sub_folio(folio, NR_PAGETABLE); +} + extern void __init pagecache_init(void); extern void free_initmem(void);