From nobody Thu Dec 18 20:17:52 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 520281A23BC for ; Wed, 18 Dec 2024 13:05:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734527125; cv=none; b=Oy9xddlArLeqjlwknmt6Dyw5PIQx2nCHT//YlknclmGTsWvqd9HNyfSuaEIyGuAFdBbATBIoLboowx04ezKo4aKZto3L6ctEZZEULYvUceSeUMCuMnsw0mlp/kLztmCazS/CABf5vuedEAc2SpwDyNCbY7Jm+lGui44rqrbyl+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734527125; c=relaxed/simple; bh=Ti9Lo8T5GTRAmE8N1daT8ak1jSzvn9zg2U/AuMVKQNI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cVvDDzYPwOy0v8mdac9zBQQ+bf5+uZVbalP2CwqOUouRcyX6jkVXoEzfPiWQ0j7+2Knu0CsWQyHa0+fYKlFoZanT/6N6mmsZE/RAu2ZnX6IRwJDgawt86fash56ydXnrbp65/f+48eYiIfHa5k1zKs26pMVY7+RVjWAJSS/MRHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=aHSMNh9s; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="aHSMNh9s" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2165cb60719so52140415ad.0 for ; Wed, 18 Dec 2024 05:05:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734527122; x=1735131922; darn=vger.kernel.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=V1U3ICCbfOFNMbev/k3EdvOWA+vXPbG14S+K01SHhE0=; b=aHSMNh9soVzRp9U6ldwWI17HLY5/EFtdGAojIdw06/i0FgLXko50kAsyRWorhIecxw oR4M4U0fbjJBwur/zTPsO+cJeF6zqcFfM5/nYP0/5iBVjIDCUpoMQbSk1MrHJ8wYwGhF EplqxdaznfI3KsYlf1EV6lWJZUxrUW8x7i9KKIgzGr819MapSTQtRk7rxboQWFKg0qZ7 1yCLskHIwjy/fhdo/Dv60l/SsZIlSYHZndN2pXylERvGlPv9DBE4cXi1j8f8UaHmyVPr cUroOCu3MiouRELCyfWsChBNjaUjJtUf47UfNX7+4Fy8HD0gOs5Sd5mtxQdFTqNBAhw+ XC6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734527123; x=1735131923; 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=V1U3ICCbfOFNMbev/k3EdvOWA+vXPbG14S+K01SHhE0=; b=m/jcpbtoG5tVWazeH39lSYgGzNGSnrRBRa/XCRHjmFGGR7jrpy4QX+bE1ChN6msPzD HRbB1y9GzNikVDX00O/KbCQYqI2gmzAfHg8fvdjOK0P6OS+DwWzC/PuKYG0LGKR2AI9H j3ahSv39daGRX5ks4o0sRCzyqhymjCEV6xop35eRM+8PoP4XoUOQ/9EH8U7cIl6nc0Wx +gHpQW25m4Ajqn3xhLXMUdqc0PL4KbgZkBxZ9vYhONcVnEa5HgBkCYHKxvTDLA9JaHAx 8tAZYZEI3fLlq4+deTzS2uq6MrHoiY/N2ZYOgpIqucTj408FLjLkD0QkskvDxCyNVzKH ABaA== X-Forwarded-Encrypted: i=1; AJvYcCVg3HRzILYqzGx8FnxFNwKPeLfRnP9ZjEccIgqpmArABqns+lrEZqzhqmWocRkwGH9mYztiMs+0QgTccCA=@vger.kernel.org X-Gm-Message-State: AOJu0YxShhjI2RW9MB47dok1GgE0hm38EuQ90gHtqjTaXveZmSnW2t/x N9Sskzt9cVojN3Qu3xjdPgAnlEb+TYxCkjpH4YbFhTqC5QjsbHMZSensVyKjSho= X-Gm-Gg: ASbGncvUTVjkHV0n4UTA/0pNHEQmqIscaqrpYZ66m0TtizUr7aV9bbG9zKYWrhTxBdK zXYimN7rphg9vWMoOXLyGthJkOYnxq4p42YkRGyGb+EK9UYFiTezpzSGuSZ3c+dMF7BrAzfEZ0l hWCLv7N/JA3dtPiOnk4//qk8aL4LHXI2CTplgpp3WqH57EIJ1Ub7T4wHyBk8ye18eV74cE+XJGv uv1bVpM2tKBs+5tIfr4Y9qc5YLuOlThwQo0jSyoL9ywcKRI0jYyhUHQyb8yG0+3ck2Zh7IUujoI Y6vfOW6vApdQ+LdiLV+qZQ== X-Google-Smtp-Source: AGHT+IHf2m598v0e4o8VjiXswnpFdHX9IrqnwvlyprzzHZEN7MlhmaMV6JPA0vL7pLwTvqcblsUT+g== X-Received: by 2002:a17:902:fac5:b0:216:6769:9ed7 with SMTP id d9443c01a7336-218d724960fmr35428695ad.40.1734527122502; Wed, 18 Dec 2024 05:05:22 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-218a1db755dsm75751825ad.42.2024.12.18.05.05.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2024 05:05:22 -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, vishal.moola@gmail.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng Subject: [PATCH v2 02/15] mm: pgtable: introduce generic p4d_alloc_one() and p4d_free() Date: Wed, 18 Dec 2024 21:04:38 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Originally-by: Peter Zijlstra (Intel) --- 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/pgal= loc.h index e75422864d1bd..679c530549327 100644 --- a/arch/arm64/include/asm/pgalloc.h +++ b/arch/arm64/include/asm/pgalloc.h @@ -15,6 +15,8 @@ =20 #define __HAVE_ARCH_PGD_FREE #define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include =20 #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t)) @@ -87,19 +89,16 @@ static inline void pgd_populate(struct mm_struct *mm, p= gd_t *pgdp, p4d_t *p4dp) =20 static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long add= r) { - gfp_t gfp =3D GFP_PGTABLE_USER; + if (!pgtable_l5_enabled()) + return NULL; =20 - if (mm =3D=3D &init_mm) - gfp =3D GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); + return __p4d_alloc_one(mm, addr); } =20 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); } =20 #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/pgal= loc.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 =20 static inline void riscv_tlb_remove_ptdesc(struct mmu_gather *tlb, void *p= t) @@ -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 add= r) { - if (pgtable_l5_enabled) { - gfp_t gfp =3D GFP_PGTABLE_USER; - - if (mm =3D=3D &init_mm) - gfp =3D GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); - } + if (!pgtable_l5_enabled) + return NULL; =20 - 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); } =20 #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 =3D virt_to_ptdesc(p4d); + + pagetable_p4d_dtor(ptdesc); riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); + } } #endif /* __PAGETABLE_PMD_FOLDED */ =20 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 @@ =20 #define __HAVE_ARCH_PTE_ALLOC_ONE #define __HAVE_ARCH_PGD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include =20 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 =20 static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long add= r) { - gfp_t gfp =3D GFP_KERNEL_ACCOUNT; + if (!pgtable_l5_enabled()) + return NULL; =20 - if (mm =3D=3D &init_mm) - gfp &=3D ~__GFP_ACCOUNT; - return (p4d_t *)get_zeroed_page(gfp); + return __p4d_alloc_one(mm, addr); } =20 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); } =20 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 =3D 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) =20 #endif /* CONFIG_PGTABLE_LEVELS > 3 */ =20 +#if CONFIG_PGTABLE_LEVELS > 4 + +static inline p4d_t *__p4d_alloc_one_noprof(struct mm_struct *mm, unsigned= long addr) +{ + gfp_t gfp =3D GFP_PGTABLE_USER; + struct ptdesc *ptdesc; + + if (mm =3D=3D &init_mm) + gfp =3D GFP_PGTABLE_KERNEL; + gfp &=3D ~__GFP_HIGHMEM; + + ptdesc =3D 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 conte= xt + * 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 l= ong 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 =3D 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 9d58f89e9643e..7168f46ad4770 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3269,6 +3269,22 @@ static inline void pagetable_pud_dtor(struct ptdesc = *ptdesc) lruvec_stat_sub_folio(folio, NR_PAGETABLE); } =20 +static inline void pagetable_p4d_ctor(struct ptdesc *ptdesc) +{ + struct folio *folio =3D 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 =3D 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); =20 --=20 2.20.1