[RFC PATCH v2 40/43] arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX
Eric Curtin
ericcurtin17 at gmail.com
Mon Nov 30 17:52:12 PST 2026
From: Eric Curtin <eric.curtin at docker.com>
With boot-time page size selection, PAGE_SIZE and the page table
geometry derived from it are not compile-time constants, so they cannot
size static objects or appear in preprocessor conditionals.
Size the static page tables used by KASAN early init (tmp_pg_dir, the
early pte table and pud[2]) with the MAX_PTRS_PER_*() limits and align
them to PAGE_SIZE_MAX. Size the pKVM stacktrace per-CPU buffer from the
new NVHE_STACK_SIZE_MAX and NVHE_STACKTRACE_SIZE_MAX, which are based on
PAGE_SHIFT_MAX; the old NVHE_STACKTRACE_SIZE has no remaining user and
is replaced. The BUILD_BUG_ON()s in kasan_early_init() that depend on
the page size become BUILD_BUG_ON_FIXED_PAGE_SIZE(), which stays a
build-time check when the page size is fixed and is a run-time check
otherwise.
Also build arm64 with -Werror=undef when boot-time page size selection
is enabled. A page size that is a variable and is used in #if silently
evaluates to 0, so turn such uses into build errors.
No functional change for compile-time page sizes.
Signed-off-by: Eric Curtin <eric.curtin at docker.com>
(cherry picked from commit 262118da104c7ff1a670fc0f51e7e37237cdaa75)
---
arch/arm64/Makefile | 5 +++++
arch/arm64/include/asm/memory.h | 4 +++-
arch/arm64/include/asm/stacktrace/nvhe.h | 2 +-
arch/arm64/kvm/hyp/nvhe/stacktrace.c | 3 ++-
arch/arm64/mm/kasan_init.c | 17 +++++++++++------
5 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index ed28f94be428..d1be87f08fbd 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -57,6 +57,11 @@ endif
KBUILD_CFLAGS += $(call cc-option,-mabi=lp64)
KBUILD_AFLAGS += $(call cc-option,-mabi=lp64)
+# PAGE_SIZE and friends are variables; in #if they would silently evaluate to 0
+ifeq ($(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE),y)
+KBUILD_CFLAGS += -Werror=undef
+endif
+
# Avoid generating .eh_frame* sections.
ifneq ($(CONFIG_UNWIND_TABLES),y)
KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables
diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index 716f346bd618..476ee4addfc2 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -167,6 +167,7 @@
#define NVHE_STACK_SHIFT PAGE_SHIFT
#define NVHE_STACK_SIZE (UL(1) << NVHE_STACK_SHIFT)
+#define NVHE_STACK_SIZE_MAX (UL(1) << PAGE_SHIFT_MAX)
/*
* With the minimum frame size of [x29, x30], exactly half the combined
@@ -174,7 +175,8 @@
* save the unwinded stacktrace; plus an additional entry to delimit the
* end.
*/
-#define NVHE_STACKTRACE_SIZE ((OVERFLOW_STACK_SIZE + NVHE_STACK_SIZE) / 2 + sizeof(long))
+#define NVHE_STACKTRACE_SIZE_MAX \
+ ((OVERFLOW_STACK_SIZE + NVHE_STACK_SIZE_MAX) / 2 + sizeof(long))
/*
* Alignment of kernel segments (e.g. .text, .data).
diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/asm/stacktrace/nvhe.h
index a631a577cbe5..5aa035707481 100644
--- a/arch/arm64/include/asm/stacktrace/nvhe.h
+++ b/arch/arm64/include/asm/stacktrace/nvhe.h
@@ -41,7 +41,7 @@ DECLARE_KVM_NVHE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overf
DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_stacktrace_info, kvm_stacktrace_info);
#ifdef CONFIG_PKVM_STACKTRACE
-DECLARE_KVM_NVHE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)],
+DECLARE_KVM_NVHE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE_MAX/sizeof(long)],
pkvm_stacktrace);
#endif
diff --git a/arch/arm64/kvm/hyp/nvhe/stacktrace.c b/arch/arm64/kvm/hyp/nvhe/stacktrace.c
index 11fadbebbf1d..dff23678e48c 100644
--- a/arch/arm64/kvm/hyp/nvhe/stacktrace.c
+++ b/arch/arm64/kvm/hyp/nvhe/stacktrace.c
@@ -36,7 +36,8 @@ static void hyp_prepare_backtrace(unsigned long fp, unsigned long pc)
}
#ifdef CONFIG_PKVM_STACKTRACE
-DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], pkvm_stacktrace);
+DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE_MAX/sizeof(long)],
+ pkvm_stacktrace);
static struct stack_info stackinfo_get_overflow(void)
{
diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c
index 638882f90ad5..1390884c71bf 100644
--- a/arch/arm64/mm/kasan_init.c
+++ b/arch/arm64/mm/kasan_init.c
@@ -23,7 +23,8 @@
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
-static pgd_t tmp_pg_dir[PTRS_PER_PTE] __initdata __aligned(PAGE_SIZE);
+static pgd_t tmp_pg_dir[MAX_PTRS_PER_PTE] __initdata
+ __aligned(PAGE_SIZE_MAX);
/*
* The p*d_populate functions call virt_to_phys implicitly so they can't be used
@@ -200,9 +201,12 @@ asmlinkage void __init kasan_early_init(void)
{
BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
- BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), SHADOW_ALIGN));
- BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), SHADOW_ALIGN));
- BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, SHADOW_ALIGN));
+ BUILD_BUG_ON_FIXED_PAGE_SIZE(
+ !IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), SHADOW_ALIGN));
+ BUILD_BUG_ON_FIXED_PAGE_SIZE(
+ !IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), SHADOW_ALIGN));
+ BUILD_BUG_ON_FIXED_PAGE_SIZE(
+ !IS_ALIGNED(KASAN_SHADOW_END, SHADOW_ALIGN));
if (!root_level_aligned(KASAN_SHADOW_START)) {
/*
@@ -214,7 +218,7 @@ asmlinkage void __init kasan_early_init(void)
* shadow pud_t[]/p4d_t[], which could end up getting corrupted
* when the linear region is mapped.
*/
- static pte_t tbl[PTRS_PER_PTE] __bss_pgtbl;
+ static pte_t tbl[MAX_PTRS_PER_PTE] __bss_pgtbl;
pgd_t *pgdp = pgd_offset_k(KASAN_SHADOW_START);
set_pgd(pgdp, __pgd(__pa_symbol(tbl) | PGD_TYPE_TABLE));
@@ -299,7 +303,8 @@ static void __init clear_shadow(u64 start, u64 end)
static void __init kasan_init_shadow(void)
{
- static pud_t pud[2][PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE);
+ static pud_t pud[2][MAX_PTRS_PER_PUD] __initdata
+ __aligned(PAGE_SIZE_MAX);
u64 kimg_shadow_start, kimg_shadow_end;
u64 mod_shadow_start;
u64 vmalloc_shadow_end;
More information about the linux-arm-kernel
mailing list