Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Curtin <ericcurtin17@gmail.com>
To: linux-arm-kernel@lists.infradead.org
Cc: Ryan Roberts <ryan.roberts@arm.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	Joey Gouly <joey.gouly@arm.com>,
	Steffen Eiden <seiden@linux.ibm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Zenghui Yu <yuzenghui@huawei.com>,
	linux-efi@vger.kernel.org, kvmarm@lists.linux.dev
Subject: [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time
Date: Tue,  1 Dec 2026 01:52:05 +0000	[thread overview]
Message-ID: <20261201015240.1017259-34-ericcurtin17@gmail.com> (raw)
In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com>

From: Ryan Roberts <ryan.roberts@arm.com>

With vmapped stacks THREAD_SIZE depends on PAGE_SIZE, because stacks are
allocated at page granularity, so the decision cannot be made until the
page size has been selected at boot.

Define THREAD_SHIFT in terms of the runtime PAGE_SHIFT, and add
THREAD_SHIFT_MIN/MAX, THREAD_SIZE_MIN/MAX and THREAD_ALIGN_MAX for code
that needs constants. Also add per page size THREAD_SHIFT_4K/16K/64K for
assembly. The linker script and EFI_KIMG_ALIGN use THREAD_ALIGN_MAX,
IRQ_STACK_SIZE uses THREAD_SIZE_MIN and the EFI runtime stack assertion
checks THREAD_SIZE_MIN.

entry.S needs THREAD_SHIFT as an immediate to check for stack overflow
before any register has been clobbered. With
CONFIG_ARM64_BOOT_TIME_PAGE_SIZE it uses one alternative per page size,
all of which are NOPs until alternatives are patched, so overflow
checking is skipped until the boot CPU has been patched. head.S obtains
the stack size with the new get_task_size macro.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit e8bcfb01d50df36bc8cb0083dae7633fd208b5cd)
---
 arch/arm64/include/asm/assembler.h |  5 ++++
 arch/arm64/include/asm/efi.h       |  2 +-
 arch/arm64/include/asm/memory.h    | 40 ++++++++++++++++++++++++------
 arch/arm64/kernel/efi.c            |  2 +-
 arch/arm64/kernel/entry.S          | 14 ++++++++++-
 arch/arm64/kernel/head.S           |  3 ++-
 arch/arm64/kernel/vmlinux.lds.S    |  4 +--
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S  |  2 +-
 8 files changed, 57 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
index e55c4094a2a5..eac670152123 100644
--- a/arch/arm64/include/asm/assembler.h
+++ b/arch/arm64/include/asm/assembler.h
@@ -902,4 +902,9 @@ alternative_cb_end
 	get_tg0 \val
 	value_for_page_size \val, \val, (~(SZ_4K-1)), (~(SZ_16K-1)), (~(SZ_64K-1))
 	.endm
+
+	.macro	get_task_size, val
+	get_tg0 \val
+	value_for_page_size \val, \val, (1 << THREAD_SHIFT_4K), (1 << THREAD_SHIFT_16K), (1 << THREAD_SHIFT_64K)
+	.endm
 #endif	/* __ASM_ASSEMBLER_H */
diff --git a/arch/arm64/include/asm/efi.h b/arch/arm64/include/asm/efi.h
index e8a9783235cb..3ac0d8fb0677 100644
--- a/arch/arm64/include/asm/efi.h
+++ b/arch/arm64/include/asm/efi.h
@@ -68,7 +68,7 @@ void arch_efi_call_virt_teardown(void);
  * kernel need greater alignment than we require the segments to be padded to.
  */
 #define EFI_KIMG_ALIGN	\
-	(SEGMENT_ALIGN > THREAD_ALIGN ? SEGMENT_ALIGN : THREAD_ALIGN)
+	(SEGMENT_ALIGN > THREAD_ALIGN_MAX ? SEGMENT_ALIGN : THREAD_ALIGN_MAX)
 
 /*
  * On arm64, we have to ensure that the initrd ends up in the linear region,
diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index 05a8efc6810d..716f346bd618 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -112,23 +112,46 @@
 
 #define DIRECT_MAP_PHYSMEM_END	__pa(PAGE_END - 1)
 
-#define MIN_THREAD_SHIFT	(14 + KASAN_THREAD_SHIFT)
+#define IDEAL_THREAD_SHIFT	(14 + KASAN_THREAD_SHIFT)
 
 /*
  * VMAP'd stacks are allocated at page granularity, so we must ensure that such
  * stacks are a multiple of page size.
  */
-#if (MIN_THREAD_SHIFT < PAGE_SHIFT)
-#define THREAD_SHIFT		PAGE_SHIFT
+#define THREAD_SHIFT							\
+	(IDEAL_THREAD_SHIFT < PAGE_SHIFT ? PAGE_SHIFT : IDEAL_THREAD_SHIFT)
+#if (IDEAL_THREAD_SHIFT < PAGE_SHIFT_MIN)
+#define THREAD_SHIFT_MIN	PAGE_SHIFT_MIN
 #else
-#define THREAD_SHIFT		MIN_THREAD_SHIFT
+#define THREAD_SHIFT_MIN	IDEAL_THREAD_SHIFT
 #endif
-
-#if THREAD_SHIFT >= PAGE_SHIFT
-#define THREAD_SIZE_ORDER	(THREAD_SHIFT - PAGE_SHIFT)
+#if (IDEAL_THREAD_SHIFT < PAGE_SHIFT_MAX)
+#define THREAD_SHIFT_MAX	PAGE_SHIFT_MAX
+#else
+#define THREAD_SHIFT_MAX	IDEAL_THREAD_SHIFT
 #endif
+#if (IDEAL_THREAD_SHIFT < ARM64_PAGE_SHIFT_4K)
+#define THREAD_SHIFT_4K		ARM64_PAGE_SHIFT_4K
+#else
+#define THREAD_SHIFT_4K		IDEAL_THREAD_SHIFT
+#endif
+#if (IDEAL_THREAD_SHIFT < ARM64_PAGE_SHIFT_16K)
+#define THREAD_SHIFT_16K	ARM64_PAGE_SHIFT_16K
+#else
+#define THREAD_SHIFT_16K	IDEAL_THREAD_SHIFT
+#endif
+#if (IDEAL_THREAD_SHIFT < ARM64_PAGE_SHIFT_64K)
+#define THREAD_SHIFT_64K	ARM64_PAGE_SHIFT_64K
+#else
+#define THREAD_SHIFT_64K	IDEAL_THREAD_SHIFT
+#endif
+
+#define THREAD_SIZE_ORDER						\
+	(PAGE_SHIFT < THREAD_SHIFT ? THREAD_SHIFT - PAGE_SHIFT : 0)
 
 #define THREAD_SIZE		(UL(1) << THREAD_SHIFT)
+#define THREAD_SIZE_MIN		(UL(1) << THREAD_SHIFT_MIN)
+#define THREAD_SIZE_MAX		(UL(1) << THREAD_SHIFT_MAX)
 
 /*
  * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
@@ -136,8 +159,9 @@
  * assembly.
  */
 #define THREAD_ALIGN		(2 * THREAD_SIZE)
+#define THREAD_ALIGN_MAX	(2 * THREAD_SIZE_MAX)
 
-#define IRQ_STACK_SIZE		THREAD_SIZE
+#define IRQ_STACK_SIZE		THREAD_SIZE_MIN
 
 #define OVERFLOW_STACK_SIZE	SZ_4K
 
diff --git a/arch/arm64/kernel/efi.c b/arch/arm64/kernel/efi.c
index 6f34aef3d04e..70967fe828ef 100644
--- a/arch/arm64/kernel/efi.c
+++ b/arch/arm64/kernel/efi.c
@@ -246,7 +246,7 @@ bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
 }
 
 /* EFI requires 8 KiB of stack space for runtime services */
-static_assert(THREAD_SIZE >= SZ_8K);
+static_assert(THREAD_SIZE_MIN >= SZ_8K);
 
 static int __init arm64_efi_rt_init(void)
 {
diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
index 6c03468ae0dd..4f13e48cb497 100644
--- a/arch/arm64/kernel/entry.S
+++ b/arch/arm64/kernel/entry.S
@@ -62,7 +62,19 @@
 	 */
 	add	sp, sp, x0			// sp' = sp + x0
 	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
-	tbnz	x0, #THREAD_SHIFT, 0f
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+alternative_if ARM64_USE_PAGE_SIZE_4K
+	tbnz	x0, #THREAD_SHIFT_4K, 0f
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_16K
+	tbnz	x0, #THREAD_SHIFT_16K, 0f
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_64K
+	tbnz	x0, #THREAD_SHIFT_64K, 0f
+alternative_else_nop_endif
+#else
+	tbnz	x0, #THREAD_SHIFT_MAX, 0f	// THREAD_SHIFT in asm
+#endif
 	sub	x0, sp, x0			// x0'' = sp' - x0' = (sp + x0) - sp = x0
 	sub	sp, sp, x0			// sp'' = sp' - x0 = (sp + x0) - x0 = sp
 	b	el\el\ht\()_\regsize\()_\label
diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index 3641e638b31e..6bccecb1be91 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -198,7 +198,8 @@ SYM_CODE_END(preserve_boot_args)
 	msr	sp_el0, \tsk
 
 	ldr	\tmp1, [\tsk, #TSK_STACK]
-	add	sp, \tmp1, #THREAD_SIZE
+	get_task_size \tmp2
+	add	sp, \tmp1, \tmp2
 	sub	sp, sp, #PT_REGS_SIZE
 
 	stp	xzr, xzr, [sp, #S_STACKFRAME]
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index 31e398bdc983..89a60e07dc52 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -81,11 +81,11 @@
 #define RO_EXCEPTION_TABLE_ALIGN	4
 #define RUNTIME_DISCARD_EXIT
 
+#include <asm/memory.h>
 #include <asm-generic/vmlinux.lds.h>
 #include <asm/cache.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/kexec.h>
-#include <asm/memory.h>
 #include <asm/page.h>
 
 #include "image.h"
@@ -325,7 +325,7 @@ SECTIONS
 
 	_data = .;
 	_sdata = .;
-	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE_MAX, THREAD_ALIGN)
+	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE_MAX, THREAD_ALIGN_MAX)
 
 	HYPERVISOR_DATA_SECTION
 
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp.lds.S b/arch/arm64/kvm/hyp/nvhe/hyp.lds.S
index 69b8fdbc977e..fc0f543ba76d 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp.lds.S
+++ b/arch/arm64/kvm/hyp/nvhe/hyp.lds.S
@@ -7,9 +7,9 @@
  */
 
 #include <asm/hyp_image.h>
+#include <asm/memory.h>
 #include <asm-generic/vmlinux.lds.h>
 #include <asm/cache.h>
-#include <asm/memory.h>
 
 SECTIONS {
 	HYP_SECTION(.idmap.text)


  parent reply	other threads:[~2026-12-01  1:55 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 03/43] mm: page_frag: Use PAGE_SIZE_MIN/MAX limits in page_frag_cache Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 05/43] mm: Avoid split pmd ptl if pmd level is run-time folded Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 06/43] mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 08/43] cgroup: Use DEFINE_GLOBAL_PAGE_SIZE_VAR for the kernfs_ops tables Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 10/43] PM: hibernate: Do not size swap structures to exactly PAGE_SIZE Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 12/43] KVM: Check struct kvm_run against PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 13/43] crash: Check crash_notes size " Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 14/43] arm64: Make INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE usable from C Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 15/43] arm64: pi: Pass the early pgtable allocation limit to map_range() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 16/43] arm64: Introduce macros required for boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 17/43] arm64: Parameterise early pgtable size macros by page shift Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 18/43] arm64: Add arm64.pagesize= command line override Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 19/43] arm64: pi: Select the page size at runtime during early boot Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 20/43] arm64: pi: Clear the FDT ptes on every remap in map_fdt() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 21/43] arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 22/43] arm64: Align image sections to PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 23/43] arm64: Map the trampoline rodata at its aligned offset Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 24/43] arm64: fixmap: Size slots and page tables for PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 25/43] arm64: Size and align the zero page and FDT ptes for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 26/43] arm64: Convert KVM and hugetlb size switches to if/else Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 27/43] arm64: mm: Make page size dependent BUILD_BUG_ON() checks runtime Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 29/43] arm64: Look up the page size at runtime in assembly code Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 31/43] arm64: Support runtime folding in idmap_kpti_install_ng_mappings Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 32/43] arm64: Patch TRAMP_VALIAS into instructions at boot Eric Curtin
2026-12-01  1:52 ` Eric Curtin [this message]
2026-12-01  1:52 ` [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern " Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 35/43] mm/zsmalloc: Size static limits for the worst-case page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 36/43] arm64: Convert remaining page-size switches to if/else Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 37/43] vdso: Lay out the data pages in VDSO_PAGE_SIZE units Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 38/43] mm: Use fixed sizes for shmem str_dup and swap cache PFN bits Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 39/43] crash: Size the vmcoreinfo page array for the smallest page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 40/43] arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 41/43] exit: Use THREAD_SIZE_MAX for lowest_to_date Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 42/43] tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 43/43] arm64: Enable boot-time page size selection Eric Curtin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201015240.1017259-34-ericcurtin17@gmail.com \
    --to=ericcurtin17@gmail.com \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=ilias.apalodimas@linaro.org \
    --cc=joey.gouly@arm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=seiden@linux.ibm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    --cc=yuzenghui@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox