From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (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 787F137A842 for ; Sun, 26 Jul 2026 22:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.69 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785104610; cv=none; b=aWwj2rndBUpow9zC4vjCJx+RwGrKIEnZHGl1wNK/bkF+xHNxKQSXuKq/k6xKpW3Fa9gRGJEUFpt7NbOsnVvUdedgRTZJT2g8PFJfTfS2zJ13SMxMbUoiwem3kd9b5UK7WzopDrjcW1BRhmZtN8jkkG+U04HaNSzSKhqmlpdsRJI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785104610; c=relaxed/simple; bh=02OhE4yVnfct3d3+FmuPnf+eRRqWBfDUKnStaSwgnTs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rXrBfOt802rb3cVM/7YaxiGhdHbZmW/DhphBCRLprL/RLnJs/wJ/f//VYVzeQogujk/E3jN0a26xWDwrvqgIOMiU/j/g1tSc2drM7Q5HM1KUHuCcYESTdqVciIuw9ruu7BvaGBSd9AIms++hYQ8iIo+hp0TitCoD6SBuREfcN3U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ej+jyXmC; arc=none smtp.client-ip=209.85.128.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ej+jyXmC" Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4957287363bso13101325e9.0 for ; Sun, 26 Jul 2026 15:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785104607; x=1785709407; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=6BiXc+2inU2lDJU0ldFQE+FFEl2R2e3zkQ3EIvc1jU4=; b=ej+jyXmCDASluuCKYu+dqfRfE6p0HPpqVNEtOWo1Nx4K3Fl0vEA/z/h5j0KfB7ooZ+ z3axKYJFrHJB+ia/BbMZyk2mJ+dVeJBJ7wedq8dI+xsywVOC5NcrpCORgU1olJo6SXHk QD5k0tGKXIF1Wrvt/8iYajPuueqD9a3UCihDfTQZ+1xBVOBs6m6DzNNGCEkO+jHEfOG2 VdnD6UPwmNtTa++ozt6y2hKwUTVVQoOSEXrdb3/rDpxAZHzWaXC61OXV5tLC66Ughnpa VU9K0I49gYxka7PeU9opYIBvRAzz0NqQjnRmiIOqCWyLRiSsYk+OtXsIgB7tUUr3TxFf piuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785104607; x=1785709407; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6BiXc+2inU2lDJU0ldFQE+FFEl2R2e3zkQ3EIvc1jU4=; b=kTyKDwUIP2CxtdGC5PbEQX/bkAuXELFeU2oJt2RNyS1DQKt2x+iHyNhroZgrd0dk3i fieAlP+IsSWpWhaZ/k1AjWfcuCzwZQeweHVHNngq46Vxw5Q7OrYYF/Xjo7EX+8bUC9uX GRIrEmlwenwj5rmyUe2KgdqMiy91OqkjynnBrITyra0MNIhR8dYTgjJBPthE0xRNurPT +vPL6xHkYLW5xeKQtZjVrO5WiFBr9Dkw3f/heLqP9+emeJicjmB2dpXYhiRpOgjCkDSI dAcUa27rznVO937Cj/iK7QZ0xT8k8DSfsUsw/ZokCc9d+94/8ctYstBKCI/be6HT1Shr V7cw== X-Forwarded-Encrypted: i=1; AHgh+RrMVlkxpI60eG2YeJxyPF7MWbywCCvjvO7YbTFxJO0kDyhthaeGaZvs/tCJX5z7Alo0BaPx2Q0D7HQXY0s=@vger.kernel.org X-Gm-Message-State: AOJu0YzuDsFZRi1EF6M4zxrqez8XybeU4e9ZgfMGQQblacmXZmzOKpJf u0YJXqxhKqmbs/9Ce1iYScC0A6IpElqIbLxQLsNuKfi91MFsp+D0qeS+99yehOt9YlEo7X56gl0 GBe8rHhi1MRp/IA== X-Received: from wmog3.prod.google.com ([2002:a05:600c:3103:b0:495:56f8:8afd]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:35d5:b0:490:e5c1:b8bf with SMTP id 5b1f17b1804b1-496b56f385dmr81438395e9.13.1785104606288; Sun, 26 Jul 2026 15:23:26 -0700 (PDT) Date: Sun, 26 Jul 2026 22:22:37 +0000 In-Reply-To: <20260726-page_alloc-unmapped-v3-0-6f5729aa9832@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260726-page_alloc-unmapped-v3-0-6f5729aa9832@google.com> X-Mailer: b4 0.16-dev Message-ID: <20260726-page_alloc-unmapped-v3-4-6f5729aa9832@google.com> Subject: [PATCH v3 04/26] x86/mm: split out preallocate_sub_pgd() From: Brendan Jackman To: Borislav Petkov , Dave Hansen , Peter Zijlstra , Andrew Morton , David Hildenbrand , Vlastimil Babka , Mike Rapoport , Wei Xu , Johannes Weiner , Zi Yan , Lorenzo Stoakes Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, rppt@kernel.org, Sumit Garg , Will Deacon , rientjes@google.com, "Kalyazin, Nikita" , patrick.roy@linux.dev, "Itazuri, Takahiro" , Andy Lutomirski , David Kaplan , Thomas Gleixner , Yosry Ahmed , Patrick Bellasi , Reiji Watanabe , Sean Christopherson , Brendan Jackman Content-Type: text/plain; charset="utf-8" This code will be needed elsewhere in a following patch. Split out the trivial code move for easy review. As a side effect, change the logging slightly: instead of directly reporting the level of the failure in panic(), show a generic panic message, will be preceded by a separate warn that reports the level of the failure. This is a simple way to have this helper suit the needs of its new user as well as the existing one. Other than logging, no functional change intended. Signed-off-by: Brendan Jackman --- arch/x86/include/asm/pgalloc.h | 3 +++ arch/x86/mm/init_64.c | 44 +++++++----------------------------------- arch/x86/mm/pgtable.c | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/arch/x86/include/asm/pgalloc.h b/arch/x86/include/asm/pgalloc.h index c88691b15f3c6..2aba6cfabf495 100644 --- a/arch/x86/include/asm/pgalloc.h +++ b/arch/x86/include/asm/pgalloc.h @@ -2,6 +2,7 @@ #ifndef _ASM_X86_PGALLOC_H #define _ASM_X86_PGALLOC_H +#include #include #include /* for struct page */ #include @@ -128,6 +129,8 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, ___pud_free_tlb(tlb, pud); } +extern int preallocate_sub_pgd(struct mm_struct *mm, unsigned long addr); + #if CONFIG_PGTABLE_LEVELS > 4 static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d) { diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index ab4c5a02326f7..ac6688c70872e 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1293,46 +1293,16 @@ static struct kcore_list kcore_vsyscall; static void __init preallocate_vmalloc_pages(void) { unsigned long addr; - const char *lvl; for (addr = VMALLOC_START; addr <= VMEMORY_END; addr = ALIGN(addr + 1, PGDIR_SIZE)) { - pgd_t *pgd = pgd_offset_k(addr); - p4d_t *p4d; - pud_t *pud; - - lvl = "p4d"; - p4d = p4d_alloc(&init_mm, pgd, addr); - if (!p4d) - goto failed; - - if (pgtable_l5_enabled()) - continue; - - /* - * The goal here is to allocate all possibly required - * hardware page tables pointed to by the top hardware - * level. - * - * On 4-level systems, the P4D layer is folded away and - * the above code does no preallocation. Below, go down - * to the pud _software_ level to ensure the second - * hardware level is allocated on 4-level systems too. - */ - lvl = "pud"; - pud = pud_alloc(&init_mm, p4d, addr); - if (!pud) - goto failed; + if (preallocate_sub_pgd(&init_mm, addr)) { + /* + * The pages have to be there now or they will be + * missing in process page-tables later. + */ + panic("Failed to pre-allocate pagetables for vmalloc area\n"); + } } - - return; - -failed: - - /* - * The pages have to be there now or they will be missing in - * process page-tables later. - */ - panic("Failed to pre-allocate %s pages for vmalloc area\n", lvl); } void __init arch_mm_preinit(void) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index f32facdb30354..fdd3709509946 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -833,3 +833,41 @@ void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) /* See note in arch_check_zapped_pte() */ VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pud_shstk(pud)); } + +#if CONFIG_PGTABLE_LEVELS > 3 +/* + * Allocate all possibly required hardware page tables pointed to ths + * top hardware level. In other words, allocate a p4d on 5-level or a + * pud on 4-level. + */ +int preallocate_sub_pgd(struct mm_struct *mm, unsigned long addr) +{ + const char *lvl; + p4d_t *p4d; + pud_t *pud; + + lvl = "p4d"; + p4d = p4d_alloc(mm, pgd_offset_pgd(mm->pgd, addr), addr); + if (!p4d) + goto failed; + + if (pgtable_l5_enabled()) + return 0; + + /* + * On 4-level systems, the P4D layer is folded away and + * the above code does no preallocation. Below, go down + * to the pud _software_ level to ensure the second + * hardware level is allocated on 4-level systems too. + */ + lvl = "pud"; + pud = pud_alloc(mm, p4d, addr); + if (!pud) + goto failed; + return 0; + +failed: + pr_warn_ratelimited("Failed to preallocate %s\n", lvl); + return -ENOMEM; +} +#endif -- 2.54.0