Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core)
@ 2026-12-01  1:51 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
                   ` (38 more replies)
  0 siblings, 39 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Eric Curtin, Ryan Roberts, Andrew Morton, Anshuman Khandual,
	Ard Biesheuvel, Catalin Marinas, David Hildenbrand, Greg Marsden,
	Ivan Ivanov, Kalesh Singh, Marc Zyngier, Mark Rutland,
	Matthias Brugger, Miroslav Benes, Will Deacon, Vlastimil Babka,
	linux-kernel, linux-mm, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Guo Ren, linux-riscv, bpf, linux-csky

From: Eric Curtin <eric.curtin@docker.com>

This is a continuation of Ryan Roberts's boot-time page size selection
series [1]. I have rebased it onto v7.4-rc5, folded in the feedback from
the v1 thread and Ryan's v2 work-in-progress branches [2], and split it
up as Ryan proposed. This posting is the core: the PAGE_SIZE_MIN/MAX
infrastructure, the mm and kernel changes it needs and the arm64
implementation. The conversions of individual subsystems and drivers
(fs, net, sound, drm, block, ...) follow as separate series to the
relevant maintainers; they only depend on patch 1. Ryan, I hope you are
happy for me to carry this forward; shout if you would rather post it
yourself.

arm64 supports 4K, 16K and 64K base pages, but the size is fixed at
compile time, so distributions that want several sizes ship several
kernels. With CONFIG_ARM64_BOOT_TIME_PAGE_SIZE=y the size is chosen at
boot with arm64.pagesize=4k|16k|64k (default set by
ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT), and PAGE_SIZE, PAGE_SHIFT, PAGE_MASK
and the pgtable geometry macros become __ro_after_init variables. Where
a constant is unavoidable (static arrays, struct layouts, BUILD_BUG_ON(),
case labels) code uses PAGE_SIZE_MIN/PAGE_SIZE_MAX. For fixed page size
configurations these collapse to the old constants and the generated
code is meant to be unchanged.

Patches 1-13 are generic: the macros (patch 1), section alignment,
page_frag, the split pmd lock, THREAD_SIZE and a few core users. Patches
14-33 and 40 are the arm64 plumbing, 34-39, 41 and 42 are further core
conversions found while building, and patch 43 adds the Kconfig option
and documentation last, so every patch builds with fixed page sizes.

Changes since v1:
 - Rebased onto v7.4-rc5; picked up Ryan's v2-1-wip fixes and the review
   comments from the v1 thread.
 - Split into this core series plus per-subsystem series.
 - Fixed bugs found in an independent review of the whole series (KVM
   stage-2 mapping size on demotion, map_fdt() state, hibernate image
   buffers, module constructor ordering vs module_param, others).
 - Added -Werror=undef when the option is enabled. #if PAGE_SIZE ...
   otherwise silently evaluates the runtime variable as 0.
 - Added arm64.pagesize= documentation and a Kconfig default size.
 - Dropped patches that are already upstream.

Testing:
 - arm64 defconfig with the option enabled builds and boots to userspace
   with 4K, 16K and 64K, under QEMU/KVM on an NVIDIA GB10 (DGX Spark),
   and with 4K and 16K under QEMU/HVF on an Apple M1. A small userspace
   test (fork/COW, mremap, mincore, memfd, THP, tmpfs and ext4-on-loop
   I/O) passes at each size; the kernel log is checked for warnings.
 - allmodconfig with the option enabled (KASAN off) builds cleanly with
   all the per-subsystem conversions applied.
 - defconfig builds with fixed 4K, 16K and 64K pages are unchanged, and
   this series alone builds at all three.
 - Not done: mm selftests, LTP, KASAN or LPA2 runs, and no new
   performance numbers; Ryan's v1 numbers [1] still stand as a guide. Most
   driver conversions are compile-tested only.

Known limitations: 48-bit VA only, MMAP_RND_BITS, SECTION_SIZE_BITS and
ARCH_FORCE_MAX_ORDER are sized for the worst case (64K) and the image
carries the static buffers for the largest page size.

The full tree, including the subsystem conversions, is at:
  https://github.com/ericcurtin/linux/tree/boot-time-page-size-v2

Tool use: the rebase conflict resolution, most of the build-fix
conversions in the subsystem series, the review pass and the commit
message drafting were done with an LLM coding assistant (opencode with
Claude Sonnet), driven and tested as described above. Ryan's original
patches keep his authorship and sign-off; I have not hand-reviewed every
driver change, so please read them with that in mind.

[1] https://lore.kernel.org/all/20241014105514.3206191-1-ryan.roberts@arm.com/
[2] https://gitlab.arm.com/linux-arm/linux-rr, features/boot-time-page-size-v2-1-wip

Eric Curtin (9):
  mm: page_frag: Add _MIN/_MAX variants of PAGE_FRAG_CACHE_MAX_SIZE
  arm64: pi: Clear the FDT ptes on every remap in map_fdt()
  arm64: Convert remaining page-size switches to if/else
  vdso: Lay out the data pages in VDSO_PAGE_SIZE units
  mm: Use fixed sizes for shmem str_dup and swap cache PFN bits
  crash: Size the vmcoreinfo page array for the smallest page size
  arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX
  exit: Use THREAD_SIZE_MAX for lowest_to_date
  tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page

Ryan Roberts (34):
  mm: Add macros ahead of supporting boot-time page size selection
  vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX
  mm: page_frag: Use PAGE_SIZE_MIN/MAX limits in page_frag_cache
  mm: Avoid split pmd ptl if pmd level is run-time folded
  mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters
  fork: Permit boot-time THREAD_SIZE determination
  cgroup: Use DEFINE_GLOBAL_PAGE_SIZE_VAR for the kernfs_ops tables
  bpf: Do not embed page-aligned members in struct bpf_ringbuf
  PM: hibernate: Do not size swap structures to exactly PAGE_SIZE
  perf/core: Handle a boot-time PAGE_SIZE in format attrs and mlock
  KVM: Check struct kvm_run against PAGE_SIZE_MIN
  crash: Check crash_notes size against PAGE_SIZE_MIN
  arm64: Make INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE usable from C
  arm64: pi: Pass the early pgtable allocation limit to map_range()
  arm64: Introduce macros required for boot-time page size selection
  arm64: Parameterise early pgtable size macros by page shift
  arm64: Add arm64.pagesize= command line override
  arm64: pi: Select the page size at runtime during early boot
  arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES
  arm64: Align image sections to PAGE_SIZE_MAX
  arm64: Map the trampoline rodata at its aligned offset
  arm64: fixmap: Size slots and page tables for PAGE_SIZE_MIN
  arm64: Size and align the zero page and FDT ptes for PAGE_SIZE_MAX
  arm64: Convert KVM and hugetlb size switches to if/else
  arm64: mm: Make page size dependent BUILD_BUG_ON() checks runtime
  arm64: cpufeature: Add capabilities for the page size in use
  arm64: Look up the page size at runtime in assembly code
  arm64: Support runtime folding of the pmd level
  arm64: Support runtime folding in idmap_kpti_install_ng_mappings
  arm64: Patch TRAMP_VALIAS into instructions at boot
  arm64: Determine THREAD_SIZE at boot time
  mm/damon/lru_sort: Initialise the stub access pattern at boot
  mm/zsmalloc: Size static limits for the worst-case page size
  arm64: Enable boot-time page size selection

 .../admin-guide/kernel-parameters.txt         |  11 +
 Documentation/arch/arm64/booting.rst          |   4 +-
 Documentation/arch/arm64/memory.rst           |  21 ++
 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  54 ++++-
 arch/arm64/Makefile                           |   5 +
 arch/arm64/include/asm/assembler.h            |  78 ++++++-
 arch/arm64/include/asm/cpufeature.h           |  44 +++-
 arch/arm64/include/asm/efi.h                  |   2 +-
 arch/arm64/include/asm/fixmap.h               |  28 ++-
 arch/arm64/include/asm/hugetlb.h              |  15 +-
 arch/arm64/include/asm/kernel-pgtable.h       | 153 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  23 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/linkage.h              |   2 +-
 arch/arm64/include/asm/memory.h               |  55 +++--
 arch/arm64/include/asm/page-def.h             |   9 +
 arch/arm64/include/asm/pgalloc.h              |  15 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  34 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 147 ++++++++----
 arch/arm64/include/asm/processor.h            |  10 +-
 arch/arm64/include/asm/sections.h             |   1 +
 arch/arm64/include/asm/smp.h                  |   1 +
 arch/arm64/include/asm/sparsemem.h            |  15 +-
 arch/arm64/include/asm/stacktrace/nvhe.h      |   2 +-
 arch/arm64/include/asm/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   2 +-
 arch/arm64/kernel/cpu_errata.c                |  53 +++++
 arch/arm64/kernel/cpufeature.c                |  68 ++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  48 +++-
 arch/arm64/kernel/head.S                      |  46 +++-
 arch/arm64/kernel/hibernate-asm.S             |   6 +-
 arch/arm64/kernel/image-vars.h                |  14 ++
 arch/arm64/kernel/image.h                     |   4 +
 arch/arm64/kernel/pi/idreg-override.c         |  50 +++-
 arch/arm64/kernel/pi/map_kernel.c             | 186 +++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 214 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  68 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  56 ++---
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  12 +
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/mm.c                  |  30 ++-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/hyp/nvhe/stacktrace.c          |   3 +-
 arch/arm64/kvm/mmu.c                          |  31 +--
 arch/arm64/lib/copy_page.S                    |  59 +++--
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fault.c                         |  12 +-
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  55 ++---
 arch/arm64/mm/init.c                          |  27 +--
 arch/arm64/mm/kasan_init.c                    |  25 +-
 arch/arm64/mm/mmu.c                           | 126 +++++++++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  32 +++
 arch/arm64/mm/proc.S                          | 131 +++++++----
 arch/arm64/mm/ptdump.c                        |   6 +-
 arch/arm64/tools/cpucaps                      |   3 +
 arch/csky/include/asm/page.h                  |   3 +
 arch/hexagon/include/asm/page.h               |   2 +
 arch/loongarch/include/asm/page.h             |   2 +
 arch/m68k/include/asm/page.h                  |   1 +
 arch/microblaze/include/asm/page.h            |   1 +
 arch/mips/include/asm/page.h                  |   1 +
 arch/nios2/include/asm/page.h                 |   2 +
 arch/openrisc/include/asm/page.h              |   1 +
 arch/parisc/include/asm/page.h                |   1 +
 arch/powerpc/include/asm/page.h               |   2 +
 arch/riscv/include/asm/page.h                 |   1 +
 arch/s390/include/asm/page.h                  |   1 +
 arch/sh/include/asm/page.h                    |   1 +
 arch/sparc/include/asm/page.h                 |   3 +
 arch/um/include/asm/page.h                    |   2 +
 arch/x86/include/asm/page_types.h             |   2 +
 arch/xtensa/include/asm/page.h                |   1 +
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 include/asm-generic/pgtable-geometry.h        |  86 +++++++
 include/asm-generic/vmlinux.lds.h             |  44 ++--
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |   2 +-
 include/linux/mm_types_task.h                 |   4 +-
 include/linux/page_frag_cache.h               |   2 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/pgtable.h                       |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/thread_info.h                   |  10 +-
 include/linux/vm_event_item.h                 |  14 +-
 include/linux/vmcore_info.h                   |   1 +
 include/vdso/datapage.h                       |  10 +-
 include/vdso/page.h                           |   4 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |  11 +-
 kernel/bpf/ringbuf.c                          |  67 +++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   4 +-
 kernel/events/core.c                          |   3 +-
 kernel/exit.c                                 |  16 +-
 kernel/fork.c                                 |  57 ++---
 kernel/module/main.c                          |   4 +-
 kernel/power/power.h                          |   3 +-
 kernel/power/snapshot.c                       |  10 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/trace_uprobe.c                   |   2 +-
 kernel/trace/undefsyms_base.c                 |   3 +-
 lib/vdso/datastore.c                          |  34 +--
 lib/vdso/gettimeofday.c                       |   4 +-
 mm/Kconfig                                    |   8 +
 mm/damon/lru_sort.c                           |   4 +-
 mm/kasan/report.c                             |   3 +-
 mm/mm_init.c                                  |   2 +-
 mm/page_frag_cache.c                          |  17 +-
 mm/shmem.c                                    |   4 +-
 mm/swap.h                                     |   7 +-
 mm/vmstat.c                                   |  14 +-
 mm/zsmalloc.c                                 |  18 +-
 virt/kvm/kvm_main.c                           |   2 +-
 133 files changed, 2244 insertions(+), 793 deletions(-)
 create mode 100644 arch/arm64/include/asm/pgtable-geometry.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c
 create mode 100644 arch/arm64/mm/pgtable-geometry.c
 create mode 100644 include/asm-generic/pgtable-geometry.h


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244


^ permalink raw reply	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
                   ` (37 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Catalin Marinas, Will Deacon, linux-kernel,
	linux-arch

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

arm64 can support several base page sizes. To let the page size be
chosen on the kernel command line instead of at build time, PAGE_SHIFT,
PAGE_SIZE and PAGE_MASK can no longer be compile-time constants, and
code that needs a constant has to be converted.

Introduce PAGE_{SHIFT,SIZE,MASK}_{MIN,MAX}, which bound the range of
possible page sizes and are always compile-time constants. An arch must
define all of them or none. When it defines none, they are set equal to
PAGE_SHIFT, PAGE_SIZE and PAGE_MASK, so existing behaviour is unchanged.

Introduce DEFINE_GLOBAL_PAGE_SIZE_VAR(), _CONST() and _ATTR() to wrap
definitions of globals whose initialiser depends on PAGE_SIZE. With a
fixed page size they expand to a plain definition. With a boot-time page
size the variable is assigned by a function marked with the constructor
attribute. Constructors are used rather than initcalls because a module
is limited to one initcall but may have any number of constructors.

To make this work, call do_ctors() early in start_kernel() instead of
from do_basic_setup(), and run module constructors before module
parameters are parsed so that the parameters can override the defaults.
An arch enabling boot-time page size must select CONFIG_CONSTRUCTORS.

Also add BUILD_BUG_ON_FIXED_PAGE_SIZE(), which is BUILD_BUG_ON() when
PAGE_SIZE_MIN equals PAGE_SIZE_MAX and VM_BUG_ON() otherwise.

The macros live in a new asm-generic/pgtable-geometry.h, included from
each arch's page header next to PAGE_SHIFT, as there is no common header
to hook into.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 2e4f93099407da931c07e4bd7aec9ff5696207fa)
---
 arch/alpha/include/asm/page.h          |  1 +
 arch/arc/include/asm/page.h            |  1 +
 arch/arm/include/asm/page.h            |  1 +
 arch/arm64/include/asm/page-def.h      |  2 +
 arch/csky/include/asm/page.h           |  3 +
 arch/hexagon/include/asm/page.h        |  2 +
 arch/loongarch/include/asm/page.h      |  2 +
 arch/m68k/include/asm/page.h           |  1 +
 arch/microblaze/include/asm/page.h     |  1 +
 arch/mips/include/asm/page.h           |  1 +
 arch/nios2/include/asm/page.h          |  2 +
 arch/openrisc/include/asm/page.h       |  1 +
 arch/parisc/include/asm/page.h         |  1 +
 arch/powerpc/include/asm/page.h        |  2 +
 arch/riscv/include/asm/page.h          |  1 +
 arch/s390/include/asm/page.h           |  1 +
 arch/sh/include/asm/page.h             |  1 +
 arch/sparc/include/asm/page.h          |  3 +
 arch/um/include/asm/page.h             |  2 +
 arch/x86/include/asm/page_types.h      |  2 +
 arch/xtensa/include/asm/page.h         |  1 +
 include/asm-generic/pgtable-geometry.h | 86 ++++++++++++++++++++++++++
 init/main.c                            |  5 +-
 kernel/module/main.c                   |  4 +-
 24 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100644 include/asm-generic/pgtable-geometry.h

diff --git a/arch/alpha/include/asm/page.h b/arch/alpha/include/asm/page.h
index 67053c52953f..f5468f52b281 100644
--- a/arch/alpha/include/asm/page.h
+++ b/arch/alpha/include/asm/page.h
@@ -96,5 +96,6 @@ typedef struct page *pgtable_t;
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif /* _ALPHA_PAGE_H */
diff --git a/arch/arc/include/asm/page.h b/arch/arc/include/asm/page.h
index facc7a03b250..844ade515d4d 100644
--- a/arch/arc/include/asm/page.h
+++ b/arch/arc/include/asm/page.h
@@ -6,6 +6,7 @@
 #define __ASM_ARC_PAGE_H
 
 #include <uapi/asm/page.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #ifdef CONFIG_ARC_HAS_PAE40
 
diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h
index fa4c1225dde5..fab78b35823f 100644
--- a/arch/arm/include/asm/page.h
+++ b/arch/arm/include/asm/page.h
@@ -188,5 +188,6 @@ extern int pfn_valid(unsigned long);
 
 #include <asm-generic/getorder.h>
 #include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif
diff --git a/arch/arm64/include/asm/page-def.h b/arch/arm64/include/asm/page-def.h
index d402e08442ee..e0ef167519e6 100644
--- a/arch/arm64/include/asm/page-def.h
+++ b/arch/arm64/include/asm/page-def.h
@@ -12,4 +12,6 @@
 
 #include <vdso/page.h>
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_PAGE_DEF_H */
diff --git a/arch/csky/include/asm/page.h b/arch/csky/include/asm/page.h
index 76774dbce869..cc223a7c25b2 100644
--- a/arch/csky/include/asm/page.h
+++ b/arch/csky/include/asm/page.h
@@ -85,4 +85,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #include <asm-generic/getorder.h>
 
 #endif /* !__ASSEMBLER__ */
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_CSKY_PAGE_H */
diff --git a/arch/hexagon/include/asm/page.h b/arch/hexagon/include/asm/page.h
index 6d82572a7f21..b2e38f1132b9 100644
--- a/arch/hexagon/include/asm/page.h
+++ b/arch/hexagon/include/asm/page.h
@@ -130,4 +130,6 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #endif /* ifdef __ASSEMBLY__ */
 #endif /* ifdef __KERNEL__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif
diff --git a/arch/loongarch/include/asm/page.h b/arch/loongarch/include/asm/page.h
index b9d61f7c032c..c446799933a0 100644
--- a/arch/loongarch/include/asm/page.h
+++ b/arch/loongarch/include/asm/page.h
@@ -107,4 +107,6 @@ extern int __virt_addr_valid(volatile void *kaddr);
 
 #endif /* !__ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_PAGE_H */
diff --git a/arch/m68k/include/asm/page.h b/arch/m68k/include/asm/page.h
index d30f8b2f1592..d4dc0147e47c 100644
--- a/arch/m68k/include/asm/page.h
+++ b/arch/m68k/include/asm/page.h
@@ -58,5 +58,6 @@ extern unsigned long _ramend;
 
 #include <asm-generic/getorder.h>
 #include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif /* _M68K_PAGE_H */
diff --git a/arch/microblaze/include/asm/page.h b/arch/microblaze/include/asm/page.h
index e1e396367ba7..4d3c1cc0073c 100644
--- a/arch/microblaze/include/asm/page.h
+++ b/arch/microblaze/include/asm/page.h
@@ -137,5 +137,6 @@ static inline const void *pfn_to_virt(unsigned long pfn)
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif /* _ASM_MICROBLAZE_PAGE_H */
diff --git a/arch/mips/include/asm/page.h b/arch/mips/include/asm/page.h
index 50a382a0d8f6..9cdc0b8cb966 100644
--- a/arch/mips/include/asm/page.h
+++ b/arch/mips/include/asm/page.h
@@ -223,5 +223,6 @@ static inline unsigned long kaslr_offset(void)
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif /* _ASM_PAGE_H */
diff --git a/arch/nios2/include/asm/page.h b/arch/nios2/include/asm/page.h
index 71eb7c1b67d4..27c4c68e8aea 100644
--- a/arch/nios2/include/asm/page.h
+++ b/arch/nios2/include/asm/page.h
@@ -93,4 +93,6 @@ extern struct page *mem_map;
 
 #endif /* !__ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_NIOS2_PAGE_H */
diff --git a/arch/openrisc/include/asm/page.h b/arch/openrisc/include/asm/page.h
index d2cdbf3579bb..ce3a6d921d8d 100644
--- a/arch/openrisc/include/asm/page.h
+++ b/arch/openrisc/include/asm/page.h
@@ -76,5 +76,6 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif /* __ASM_OPENRISC_PAGE_H */
diff --git a/arch/parisc/include/asm/page.h b/arch/parisc/include/asm/page.h
index 3630b36d07da..4dffa850d30a 100644
--- a/arch/parisc/include/asm/page.h
+++ b/arch/parisc/include/asm/page.h
@@ -169,6 +169,7 @@ extern int npmem_ranges;
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 #include <asm/pdc.h>
 
 #define PAGE0   ((struct zeropage *)absolute_pointer(__PAGE_OFFSET))
diff --git a/arch/powerpc/include/asm/page.h b/arch/powerpc/include/asm/page.h
index 281f25e071a3..e5c01c11f26b 100644
--- a/arch/powerpc/include/asm/page.h
+++ b/arch/powerpc/include/asm/page.h
@@ -293,4 +293,6 @@ static inline unsigned long kaslr_offset(void)
 #include <asm-generic/memory_model.h>
 #endif /* __ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_POWERPC_PAGE_H */
diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
index b4bbae55e931..2e68929ac15c 100644
--- a/arch/riscv/include/asm/page.h
+++ b/arch/riscv/include/asm/page.h
@@ -176,5 +176,6 @@ static __always_inline void *pfn_to_kaddr(unsigned long pfn)
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif /* _ASM_RISCV_PAGE_H */
diff --git a/arch/s390/include/asm/page.h b/arch/s390/include/asm/page.h
index 56da819a79e6..46f340d25b74 100644
--- a/arch/s390/include/asm/page.h
+++ b/arch/s390/include/asm/page.h
@@ -283,6 +283,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #define AMODE31_SIZE		(3 * PAGE_SIZE)
 
diff --git a/arch/sh/include/asm/page.h b/arch/sh/include/asm/page.h
index def4205491ec..13e13e900586 100644
--- a/arch/sh/include/asm/page.h
+++ b/arch/sh/include/asm/page.h
@@ -159,5 +159,6 @@ typedef struct page *pgtable_t;
 
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
 
 #endif /* __ASM_SH_PAGE_H */
diff --git a/arch/sparc/include/asm/page.h b/arch/sparc/include/asm/page.h
index 1a00cc0a1893..8f46b7a38ccd 100644
--- a/arch/sparc/include/asm/page.h
+++ b/arch/sparc/include/asm/page.h
@@ -7,4 +7,7 @@
 #else
 #include <asm/page_32.h>
 #endif
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif
diff --git a/arch/um/include/asm/page.h b/arch/um/include/asm/page.h
index e348ff489b89..6eb5a5cdbcf0 100644
--- a/arch/um/include/asm/page.h
+++ b/arch/um/include/asm/page.h
@@ -95,4 +95,6 @@ extern unsigned long uml_physmem;
 
 #endif	/* __ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* __UM_PAGE_H */
diff --git a/arch/x86/include/asm/page_types.h b/arch/x86/include/asm/page_types.h
index 3e0801a0f782..99145b56714c 100644
--- a/arch/x86/include/asm/page_types.h
+++ b/arch/x86/include/asm/page_types.h
@@ -66,4 +66,6 @@ extern void initmem_init(void);
 
 #endif	/* !__ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* _ASM_X86_PAGE_DEFS_H */
diff --git a/arch/xtensa/include/asm/page.h b/arch/xtensa/include/asm/page.h
index 059493256765..6fb8bfbd0f45 100644
--- a/arch/xtensa/include/asm/page.h
+++ b/arch/xtensa/include/asm/page.h
@@ -174,4 +174,5 @@ static inline unsigned long ___pa(unsigned long va)
 #endif /* __ASSEMBLER__ */
 
 #include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
 #endif /* _XTENSA_PAGE_H */
diff --git a/include/asm-generic/pgtable-geometry.h b/include/asm-generic/pgtable-geometry.h
new file mode 100644
index 000000000000..89609cab425f
--- /dev/null
+++ b/include/asm-generic/pgtable-geometry.h
@@ -0,0 +1,86 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_GENERIC_PGTABLE_GEOMETRY_H
+#define ASM_GENERIC_PGTABLE_GEOMETRY_H
+
+#if   defined(PAGE_SHIFT_MAX) && defined(PAGE_SIZE_MAX) && defined(PAGE_MASK_MAX) && \
+      defined(PAGE_SHIFT_MIN) && defined(PAGE_SIZE_MIN) && defined(PAGE_MASK_MIN)
+/* Arch supports boot-time page size selection. */
+#elif defined(PAGE_SHIFT_MAX) || defined(PAGE_SIZE_MAX) || defined(PAGE_MASK_MAX) || \
+      defined(PAGE_SHIFT_MIN) || defined(PAGE_SIZE_MIN) || defined(PAGE_MASK_MIN)
+#error Arch must define all or none of the boot-time page size macros
+#else
+/* Arch does not support boot-time page size selection. */
+#define PAGE_SHIFT_MIN	PAGE_SHIFT
+#define PAGE_SIZE_MIN	PAGE_SIZE
+#define PAGE_MASK_MIN	PAGE_MASK
+#define PAGE_SHIFT_MAX	PAGE_SHIFT
+#define PAGE_SIZE_MAX	PAGE_SIZE
+#define PAGE_MASK_MAX	PAGE_MASK
+#endif
+
+/*
+ * Define a global variable (scalar or struct), whose value is derived from
+ * PAGE_SIZE and friends. When PAGE_SIZE is a compile-time constant, the global
+ * variable is simply defined with the static value. When PAGE_SIZE is
+ * determined at boot-time, a constructor is registered and run before any
+ * initcalls or module params are parsed to initialize the variable.
+ *
+ * @type: Unqualified type. Do not include "const"; implied by macro variant.
+ * @name: Variable name.
+ * @attrib: Variable attributes (DEFINE_GLOBAL_PAGE_SIZE_VAR_ATTR only).
+ * @...:  Initialization value. May be scalar or initializer.
+ *
+ * "static" is declared by placing "static" before the macro.
+ *
+ * Example:
+ *
+ * struct my_struct {
+ *         int a;
+ *         char b;
+ * };
+ *
+ * static DEFINE_GLOBAL_PAGE_SIZE_VAR(struct my_struct, my_variable, {
+ *         .a = 10,
+ *         .b = 'e',
+ * });
+ */
+#if PAGE_SIZE_MIN != PAGE_SIZE_MAX
+#define __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, attrib, ...)		\
+	type name attrib;						\
+	static void __init __attribute__((constructor)) __##name##_init(void)	\
+	{								\
+		name = (type)__VA_ARGS__;				\
+	}
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, ...)			\
+	__DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, , __VA_ARGS__)
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR_CONST(type, name, ...)		\
+	__DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, __ro_after_init, __VA_ARGS__)
+#else /* PAGE_SIZE_MIN == PAGE_SIZE_MAX */
+#define __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, attrib, ...)		\
+	type name attrib = __VA_ARGS__;					\
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, ...)			\
+	__DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, , __VA_ARGS__)
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR_CONST(type, name, ...)		\
+	__DEFINE_GLOBAL_PAGE_SIZE_VAR(const type, name, , __VA_ARGS__)
+#endif
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR_ATTR(type, name, attrib, ...)	\
+	__DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, attrib, __VA_ARGS__)
+
+#ifndef __ASSEMBLY__
+/*
+ * Compile-time check when the page size is fixed. With boot-time page size
+ * the condition is only known at boot, so check it at run-time instead.
+ */
+#if PAGE_SIZE_MIN == PAGE_SIZE_MAX
+#define BUILD_BUG_ON_FIXED_PAGE_SIZE(cond)	BUILD_BUG_ON(cond)
+#else
+#define BUILD_BUG_ON_FIXED_PAGE_SIZE(cond)	VM_BUG_ON(cond)
+#endif
+#endif /* __ASSEMBLY__ */
+
+#endif /* ASM_GENERIC_PGTABLE_GEOMETRY_H */
diff --git a/init/main.c b/init/main.c
index f14780345051..3d619be281aa 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1053,6 +1053,8 @@ static void __init print_kernel_cmdline(const char *cmdline)
 		pr_notice("%s%s\n", KERNEL_CMDLINE_PREFIX, cmdline);
 }
 
+static __init void do_ctors(void);
+
 asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
 void start_kernel(void)
 {
@@ -1064,6 +1066,8 @@ void start_kernel(void)
 	debug_objects_early_init();
 	init_vmlinux_build_id();
 
+	do_ctors();
+
 	cgroup_init_early();
 
 	local_irq_disable();
@@ -1526,7 +1530,6 @@ static void __init do_basic_setup(void)
 	ksysfs_init();
 	driver_init();
 	init_irq_proc();
-	do_ctors();
 	do_initcalls();
 }
 
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 34e0204ea37c..17145dc9c009 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -3127,7 +3127,6 @@ static noinline int do_init_module(struct module *mod)
 	freeinit->init_data = mod->mem[MOD_INIT_DATA].base;
 	freeinit->init_rodata = mod->mem[MOD_INIT_RODATA].base;
 
-	do_mod_ctors(mod);
 	/* Start the module */
 	if (mod->init != NULL)
 		ret = do_one_initcall(mod->init);
@@ -3575,6 +3574,9 @@ static int load_module(struct load_info *info, const char __user *uargs,
 
 	mod->async_probe_requested = async_probe;
 
+	/* Constructors may set defaults that module params must override. */
+	do_mod_ctors(mod);
+
 	/* Module is ready to execute: parsing args may do that. */
 	after_dashes = parse_args(mod->name, args, mod->kp, mod->num_kp,
 				  -32768, 32767, mod,


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX
  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 ` 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
                   ` (36 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Arnd Bergmann, Dennis Zhou, Tejun Heo,
	Christoph Lameter, linux-arch, linux-kernel, linux-mm

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

Data that must be page aligned is laid out at build time, so it cannot
be aligned to a PAGE_SIZE that is only known at boot.

Use PAGE_SIZE_MAX for the alignment in vmlinux.lds.h (init text, rodata,
nosave, BTF, bss, percpu and the other page-aligned sections), in
__page_aligned_data and __page_aligned_bss, and in
DECLARE/DEFINE_PER_CPU_PAGE_ALIGNED(). This keeps the data aligned for
whichever page size is selected at boot.

PAGE_SIZE_MAX equals PAGE_SIZE when the page size is fixed, so there is
no change for compile-time page sizes.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit bb504904abc5ead0ce74f9e9c7d868f4f13c3815)
---
 include/asm-generic/vmlinux.lds.h | 38 +++++++++++++++----------------
 include/linux/linkage.h           |  4 ++--
 include/linux/percpu-defs.h       |  4 ++--
 3 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 3eb732942d69..2a86599cb620 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -13,7 +13,7 @@
  *	. = START;
  *	__init_begin = .;
  *	HEAD_TEXT_SECTION
- *	INIT_TEXT_SECTION(PAGE_SIZE)
+ *	INIT_TEXT_SECTION(PAGE_SIZE_MAX)
  *	INIT_DATA_SECTION(...)
  *	PERCPU_SECTION(CACHELINE_SIZE)
  *	__init_end = .;
@@ -23,7 +23,7 @@
  *	_etext = .;
  *
  *      _sdata = .;
- *	RO_DATA(PAGE_SIZE)
+ *	RO_DATA(PAGE_SIZE_MAX)
  *	RW_DATA(...)
  *	_edata = .;
  *
@@ -397,10 +397,10 @@
  * Data section helpers
  */
 #define NOSAVE_DATA							\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	__nosave_begin = .;						\
 	*(.data..nosave)						\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	__nosave_end = .;
 
 #define CACHE_HOT_DATA(align)						\
@@ -603,7 +603,7 @@
 		*(.text.unknown .text.unknown.*)			\
 		TEXT_SPLIT						\
 		TEXT_UNLIKELY						\
-		. = ALIGN(PAGE_SIZE);					\
+		. = ALIGN(PAGE_SIZE_MAX);				\
 		TEXT_HOT						\
 		*(TEXT_MAIN .text.fixup)				\
 		NOINSTR_TEXT						\
@@ -677,7 +677,7 @@
  */
 #ifdef CONFIG_DEBUG_INFO_BTF_INLINE
 #define BTF_INLINE							\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	.BTF.inline : AT(ADDR(.BTF.inline) - LOAD_OFFSET) {		\
 		BOUNDED_SECTION_BY(.BTF.inline, _BTF_inline)		\
 	}
@@ -693,7 +693,7 @@
 #define BTF_LINK_SIZE	(64 - 4 + 32 + 4)
 #endif
 #define BTF_INLINE_LINK						\
-	. = ALIGN(PAGE_SIZE);					\
+	. = ALIGN(PAGE_SIZE_MAX);				\
 	.BTF.inline.link : AT(ADDR(.BTF.inline.link) - LOAD_OFFSET) { \
 		__start_BTF_inline_link = .;				\
 		BYTE(0)						\
@@ -706,13 +706,13 @@
 
 #ifdef CONFIG_DEBUG_INFO_BTF
 #define BTF								\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) {				\
 		BOUNDED_SECTION_BY(.BTF, _BTF)				\
 	}								\
 	BTF_INLINE							\
 	BTF_INLINE_LINK						\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {			\
 		*(.BTF_ids)						\
 	}
@@ -807,9 +807,9 @@
 	. = ALIGN(bss_align);						\
 	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {				\
 		BSS_FIRST_SECTIONS					\
-		. = ALIGN(PAGE_SIZE);					\
+		. = ALIGN(PAGE_SIZE_MAX);				\
 		*(.bss..page_aligned)					\
-		. = ALIGN(PAGE_SIZE);					\
+		. = ALIGN(PAGE_SIZE_MAX);				\
 		*(.dynbss)						\
 		*(BSS_MAIN)						\
 		*(COMMON)						\
@@ -1046,9 +1046,9 @@
  */
 #ifdef CONFIG_AMD_MEM_ENCRYPT
 #define PERCPU_DECRYPTED_SECTION					\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	*(.data..percpu..decrypted)					\
-	. = ALIGN(PAGE_SIZE);
+	. = ALIGN(PAGE_SIZE_MAX);
 #else
 #define PERCPU_DECRYPTED_SECTION
 #endif
@@ -1132,7 +1132,7 @@
  */
 #define PERCPU_INPUT(cacheline)						\
 	__per_cpu_start = .;						\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	*(.data..percpu..page_aligned)					\
 	. = ALIGN(cacheline);						\
 	__per_cpu_hot_start = .;					\
@@ -1156,7 +1156,7 @@
  * sharing between subsections for different purposes.
  */
 #define PERCPU_SECTION(cacheline)					\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	.data..percpu	: AT(ADDR(.data..percpu) - LOAD_OFFSET) {	\
 		PERCPU_INPUT(cacheline)					\
 	}
@@ -1173,15 +1173,15 @@
  * All sections are combined in a single .data section.
  * The sections following CONSTRUCTORS are arranged so their
  * typical alignment matches.
- * A cacheline is typical/always less than a PAGE_SIZE so
+ * A cacheline is typical/always less than a PAGE_SIZE_MAX so
  * the sections that has this restriction (or similar)
- * is located before the ones requiring PAGE_SIZE alignment.
- * NOSAVE_DATA starts and ends with a PAGE_SIZE alignment which
+ * is located before the ones requiring PAGE_SIZE_MAX alignment.
+ * NOSAVE_DATA starts and ends with a PAGE_SIZE_MAX alignment which
  * matches the requirement of PAGE_ALIGNED_DATA.
  *
  * use 0 as page_align if page_aligned data is not used */
 #define RW_DATA(cacheline, pagealigned, inittask)			\
-	. = ALIGN(PAGE_SIZE);						\
+	. = ALIGN(PAGE_SIZE_MAX);					\
 	.data : AT(ADDR(.data) - LOAD_OFFSET) {				\
 		INIT_TASK_DATA(inittask)				\
 		NOSAVE_DATA						\
diff --git a/include/linux/linkage.h b/include/linux/linkage.h
index 53fe1f48fd28..3673440789c3 100644
--- a/include/linux/linkage.h
+++ b/include/linux/linkage.h
@@ -36,8 +36,8 @@
 		  __stringify(name))
 #endif
 
-#define __page_aligned_data	__section(".data..page_aligned") __aligned(PAGE_SIZE)
-#define __page_aligned_bss	__section(".bss..page_aligned") __aligned(PAGE_SIZE)
+#define __page_aligned_data	__section(".data..page_aligned") __aligned(PAGE_SIZE_MAX)
+#define __page_aligned_bss	__section(".bss..page_aligned") __aligned(PAGE_SIZE_MAX)
 
 #ifndef __bss_pgtbl
 #define __bss_pgtbl		__page_aligned_bss
diff --git a/include/linux/percpu-defs.h b/include/linux/percpu-defs.h
index dbe3267a0a13..9e15c10ca6a5 100644
--- a/include/linux/percpu-defs.h
+++ b/include/linux/percpu-defs.h
@@ -153,11 +153,11 @@
  */
 #define DECLARE_PER_CPU_PAGE_ALIGNED(type, name)			\
 	DECLARE_PER_CPU_SECTION(type, name, "..page_aligned")		\
-	__aligned(PAGE_SIZE)
+	__aligned(PAGE_SIZE_MAX)
 
 #define DEFINE_PER_CPU_PAGE_ALIGNED(type, name)				\
 	DEFINE_PER_CPU_SECTION(type, name, "..page_aligned")		\
-	__aligned(PAGE_SIZE)
+	__aligned(PAGE_SIZE_MAX)
 
 /*
  * Declaration/definition used for per-CPU variables that must be read mostly.


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 03/43] mm: page_frag: Use PAGE_SIZE_MIN/MAX limits in page_frag_cache
  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 ` 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
                   ` (35 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Vlastimil Babka, David Hildenbrand, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Michal Hocko, Johannes Weiner,
	Suren Baghdasaryan, Brendan Jackman, Zi Yan, Andrew Morton,
	linux-mm

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

With boot-time page size selection, PAGE_SIZE and PAGE_MASK are not
compile-time constants, but struct page_frag_cache, the order mask and
the BUILD_BUG_ON() checks in encoded_page_create() need constants.

Define PAGE_FRAG_CACHE_MAX_SIZE with PAGE_MASK_MIN. Choose the layout of
struct page_frag_cache using PAGE_SIZE_MAX, and choose
PAGE_FRAG_CACHE_ORDER_MASK using PAGE_SIZE_MIN. Make the
BUILD_BUG_ON() checks in encoded_page_create() use PAGE_SHIFT_MIN and
PAGE_SIZE_MIN.

In __page_frag_cache_refill(), turn the #if on PAGE_SIZE into a C if
statement so that the large-order attempt is decided at run time and is
dead-code eliminated when the page size is fixed.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit c15bbe772338e7c74cd1eaa8d1034a0c9ad0c0f9)
---
 include/linux/mm_types_task.h   |  4 ++--
 include/linux/page_frag_cache.h |  2 +-
 mm/page_frag_cache.c            | 17 +++++++++--------
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h
index 11bf319d78ec..704c3c31d11d 100644
--- a/include/linux/mm_types_task.h
+++ b/include/linux/mm_types_task.h
@@ -44,7 +44,7 @@ struct page_frag {
 #endif
 };
 
-#define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK)
+#define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK_MIN)
 #define PAGE_FRAG_CACHE_MAX_ORDER	get_order(PAGE_FRAG_CACHE_MAX_SIZE)
 struct page_frag_cache {
 	/* encoded_page consists of the virtual address, pfmemalloc bit and
@@ -55,7 +55,7 @@ struct page_frag_cache {
 	/* we maintain a pagecount bias, so that we dont dirty cache line
 	 * containing page->_refcount every time we allocate a fragment.
 	 */
-#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32)
+#if (PAGE_SIZE_MAX < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32)
 	__u16 offset;
 	__u16 pagecnt_bias;
 #else
diff --git a/include/linux/page_frag_cache.h b/include/linux/page_frag_cache.h
index 41a91df82631..49a0b0b51446 100644
--- a/include/linux/page_frag_cache.h
+++ b/include/linux/page_frag_cache.h
@@ -8,7 +8,7 @@
 #include <linux/mm_types_task.h>
 #include <linux/types.h>
 
-#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
+#if (PAGE_SIZE_MIN < PAGE_FRAG_CACHE_MAX_SIZE)
 /* Use a full byte here to enable assembler optimization as the shift
  * operation is usually expecting a byte.
  */
diff --git a/mm/page_frag_cache.c b/mm/page_frag_cache.c
index 8914437ddc51..d4419caa0c06 100644
--- a/mm/page_frag_cache.c
+++ b/mm/page_frag_cache.c
@@ -23,8 +23,9 @@
 static unsigned long encoded_page_create(struct page *page, unsigned int order,
 					 bool pfmemalloc)
 {
-	BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_FRAG_CACHE_ORDER_MASK);
-	BUILD_BUG_ON(PAGE_FRAG_CACHE_PFMEMALLOC_BIT >= PAGE_SIZE);
+	BUILD_BUG_ON(order_base_2(PAGE_FRAG_CACHE_MAX_SIZE >> PAGE_SHIFT_MIN) >
+		     PAGE_FRAG_CACHE_ORDER_MASK);
+	BUILD_BUG_ON(PAGE_FRAG_CACHE_PFMEMALLOC_BIT >= PAGE_SIZE_MIN);
 
 	return (unsigned long)page_address(page) |
 		(order & PAGE_FRAG_CACHE_ORDER_MASK) |
@@ -53,12 +54,12 @@ static struct page *__page_frag_cache_refill(struct page_frag_cache *nc,
 	struct page *page = NULL;
 	gfp_t gfp = gfp_mask;
 
-#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
-	gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) |  __GFP_COMP |
-		   __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC;
-	page = __alloc_pages(gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER,
-			     numa_mem_id(), NULL, ALLOC_DEFAULT);
-#endif
+	if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) {
+		gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) |  __GFP_COMP |
+			   __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC;
+		page = __alloc_pages(gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER,
+				     numa_mem_id(), NULL, ALLOC_DEFAULT);
+	}
 	if (unlikely(!page)) {
 		page = __alloc_pages(gfp, 0, numa_mem_id(), NULL, ALLOC_DEFAULT);
 		order = 0;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 05/43] mm: Avoid split pmd ptl if pmd level is run-time folded
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (2 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 06/43] mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters Eric Curtin
                   ` (34 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Michal Hocko, Ingo Molnar, Peter Zijlstra,
	Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
	Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
	Kees Cook, Andrew Morton, linux-mm, linux-kernel

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

If there are only two levels of translation, the first level (pgd) may
not be an entire page and so may not have a ptdesc backing it (this can
be the case on arm64, depending on VA size and page size). Even if it
does have a ptdesc, pagetable_pmd_ctor() is not called for it, since it
is a pgd and not a pmd table, so the per-ptdesc ptl fields are not
initialised.

So far this has been fine: the arch knows at compile time whether the
pmd level is folded and, if so, does not select
CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK. With boot-time page size selection
the number of levels is not known at compile time, so the choice has to
be made at boot.

When CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK is selected, make
pmd_lockptr() return mm->page_table_lock if mm_pmd_folded() is true, and
have pmd_huge_pte() use mm->pmd_huge_pte in that case. Add
CONFIG_ARCH_RUNTIME_PMD_FOLD for arches that fold at run time, and
CONFIG_MM_PMD_HUGE_PTE to describe when mm_struct needs the pmd_huge_pte
member (THP without split pmd ptlocks, or with run-time pmd folding).

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 78715f47a6259f419370cf8089bd79361516be26)
---
 include/linux/mm.h       | 17 ++++++++++++++++-
 include/linux/mm_types.h |  2 +-
 kernel/fork.c            |  4 ++--
 mm/Kconfig               |  8 ++++++++
 4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff64381b..14a35440c806 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -4208,6 +4208,8 @@ static inline struct ptdesc *pmd_ptdesc(pmd_t *pmd)
 
 static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
 {
+	if (mm_pmd_folded(mm))
+		return &mm->page_table_lock;
 	return ptlock_ptr(pmd_ptdesc(pmd));
 }
 
@@ -4219,7 +4221,20 @@ static inline bool pmd_ptlock_init(struct ptdesc *ptdesc)
 	return ptlock_init(ptdesc);
 }
 
-#define pmd_huge_pte(mm, pmd) (pmd_ptdesc(pmd)->pmd_huge_pte)
+static inline pgtable_t *__pmd_huge_pte(struct mm_struct *mm, pmd_t *pmd)
+{
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+#ifdef CONFIG_ARCH_RUNTIME_PMD_FOLD
+	if (mm_pmd_folded(mm))
+		return &mm->pmd_huge_pte;
+#endif
+	return &pmd_ptdesc(pmd)->pmd_huge_pte;
+#else
+	return NULL;
+#endif
+}
+
+#define pmd_huge_pte(mm, pmd) (*__pmd_huge_pte(mm, pmd))
 
 #else
 
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 1ed35d258d2e..ed9277798631 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -1382,7 +1382,7 @@ struct mm_struct {
 #ifdef CONFIG_MMU_NOTIFIER
 		struct mmu_notifier_subscriptions *notifier_subscriptions;
 #endif
-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
+#ifdef CONFIG_MM_PMD_HUGE_PTE
 		pgtable_t pmd_huge_pte; /* protected by page_table_lock */
 #endif
 #ifdef CONFIG_NUMA_BALANCING
diff --git a/kernel/fork.c b/kernel/fork.c
index d55a7d21a8fb..5a0db5a89615 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -647,7 +647,7 @@ static void check_mm(struct mm_struct *mm)
 		pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n",
 				mm_pgtables_bytes(mm));
 
-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
+#ifdef CONFIG_MM_PMD_HUGE_PTE
 	VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
 #endif
 }
@@ -1110,7 +1110,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
 	RCU_INIT_POINTER(mm->exe_file, NULL);
 	mmu_notifier_subscriptions_init(mm);
 	init_tlb_flush_pending(mm);
-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
+#ifdef CONFIG_MM_PMD_HUGE_PTE
 	mm->pmd_huge_pte = NULL;
 #endif
 	mm_init_uprobes_state(mm);
diff --git a/mm/Kconfig b/mm/Kconfig
index afff1b25b543..f0868970b367 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -611,10 +611,18 @@ config SPLIT_PTE_PTLOCKS
 config ARCH_ENABLE_SPLIT_PMD_PTLOCK
 	bool
 
+config ARCH_RUNTIME_PMD_FOLD
+	bool
+
 config SPLIT_PMD_PTLOCKS
 	def_bool y
 	depends on SPLIT_PTE_PTLOCKS && ARCH_ENABLE_SPLIT_PMD_PTLOCK
 
+config MM_PMD_HUGE_PTE
+	def_bool y
+	depends on TRANSPARENT_HUGEPAGE
+	depends on !SPLIT_PMD_PTLOCKS || ARCH_RUNTIME_PMD_FOLD
+
 #
 # support for memory balloon
 config BALLOON


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 06/43] mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (3 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
                   ` (33 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Michal Hocko, Andrew Morton, linux-mm,
	linux-kernel

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

THREAD_SIZE depends on PAGE_SIZE when boot-time page size selection is
used, so it is not a compile-time constant and cannot be used in C
preprocessor conditionals.

Test THREAD_SIZE_MAX instead in the #if directives that decide which
KSTACK_nK vm event items exist, in vm_event_item.h, vmstat_text[] in
mm/vmstat.c, and kstack_histogram() in kernel/exit.c. With a smaller
runtime THREAD_SIZE, the counters for sizes above it are still defined
but simply never incremented.

No functional change for compile-time page sizes.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit bf0856284bb664002b756e3785d35aa050d766c9)
---
 include/linux/vm_event_item.h | 14 +++++++-------
 kernel/exit.c                 | 14 +++++++-------
 mm/vmstat.c                   | 14 +++++++-------
 3 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
index 2628ccda076a..d1bca28052ef 100644
--- a/include/linux/vm_event_item.h
+++ b/include/linux/vm_event_item.h
@@ -153,25 +153,25 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
 #endif
 #ifdef CONFIG_DEBUG_STACK_USAGE
 		KSTACK_1K,
-#if THREAD_SIZE > 1024
+#if THREAD_SIZE_MAX > 1024
 		KSTACK_2K,
 #endif
-#if THREAD_SIZE > 2048
+#if THREAD_SIZE_MAX > 2048
 		KSTACK_4K,
 #endif
-#if THREAD_SIZE > 4096
+#if THREAD_SIZE_MAX > 4096
 		KSTACK_8K,
 #endif
-#if THREAD_SIZE > 8192
+#if THREAD_SIZE_MAX > 8192
 		KSTACK_16K,
 #endif
-#if THREAD_SIZE > 16384
+#if THREAD_SIZE_MAX > 16384
 		KSTACK_32K,
 #endif
-#if THREAD_SIZE > 32768
+#if THREAD_SIZE_MAX > 32768
 		KSTACK_64K,
 #endif
-#if THREAD_SIZE > 65536
+#if THREAD_SIZE_MAX > 65536
 		KSTACK_REST,
 #endif
 #endif /* CONFIG_DEBUG_STACK_USAGE */
diff --git a/kernel/exit.c b/kernel/exit.c
index d5e7bc2af804..a8453363d1fd 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -813,31 +813,31 @@ static inline void kstack_histogram(unsigned long used_stack)
 #ifdef CONFIG_VM_EVENT_COUNTERS
 	if (used_stack <= 1024)
 		count_vm_event(KSTACK_1K);
-#if THREAD_SIZE > 1024
+#if THREAD_SIZE_MAX > 1024
 	else if (used_stack <= 2048)
 		count_vm_event(KSTACK_2K);
 #endif
-#if THREAD_SIZE > 2048
+#if THREAD_SIZE_MAX > 2048
 	else if (used_stack <= 4096)
 		count_vm_event(KSTACK_4K);
 #endif
-#if THREAD_SIZE > 4096
+#if THREAD_SIZE_MAX > 4096
 	else if (used_stack <= 8192)
 		count_vm_event(KSTACK_8K);
 #endif
-#if THREAD_SIZE > 8192
+#if THREAD_SIZE_MAX > 8192
 	else if (used_stack <= 16384)
 		count_vm_event(KSTACK_16K);
 #endif
-#if THREAD_SIZE > 16384
+#if THREAD_SIZE_MAX > 16384
 	else if (used_stack <= 32768)
 		count_vm_event(KSTACK_32K);
 #endif
-#if THREAD_SIZE > 32768
+#if THREAD_SIZE_MAX > 32768
 	else if (used_stack <= 65536)
 		count_vm_event(KSTACK_64K);
 #endif
-#if THREAD_SIZE > 65536
+#if THREAD_SIZE_MAX > 65536
 	else
 		count_vm_event(KSTACK_REST);
 #endif
diff --git a/mm/vmstat.c b/mm/vmstat.c
index c6beac6be2bc..248ab936410c 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1481,25 +1481,25 @@ const char * const vmstat_text[] = {
 #endif
 #ifdef CONFIG_DEBUG_STACK_USAGE
 	[I(KSTACK_1K)]				= "kstack_1k",
-#if THREAD_SIZE > 1024
+#if THREAD_SIZE_MAX > 1024
 	[I(KSTACK_2K)]				= "kstack_2k",
 #endif
-#if THREAD_SIZE > 2048
+#if THREAD_SIZE_MAX > 2048
 	[I(KSTACK_4K)]				= "kstack_4k",
 #endif
-#if THREAD_SIZE > 4096
+#if THREAD_SIZE_MAX > 4096
 	[I(KSTACK_8K)]				= "kstack_8k",
 #endif
-#if THREAD_SIZE > 8192
+#if THREAD_SIZE_MAX > 8192
 	[I(KSTACK_16K)]				= "kstack_16k",
 #endif
-#if THREAD_SIZE > 16384
+#if THREAD_SIZE_MAX > 16384
 	[I(KSTACK_32K)]				= "kstack_32k",
 #endif
-#if THREAD_SIZE > 32768
+#if THREAD_SIZE_MAX > 32768
 	[I(KSTACK_64K)]				= "kstack_64k",
 #endif
-#if THREAD_SIZE > 65536
+#if THREAD_SIZE_MAX > 65536
 	[I(KSTACK_REST)]			= "kstack_rest",
 #endif
 #endif /* CONFIG_DEBUG_STACK_USAGE */


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (4 preceding siblings ...)
  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 ` 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
                   ` (32 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Vlastimil Babka, Arnd Bergmann, Ingo Molnar,
	Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
	K Prateek Nayak, David Hildenbrand, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Michal Hocko, Kees Cook,
	Andrey Ryabinin, Alexander Potapenko, Andrey Konovalov,
	Dmitry Vyukov, Vincenzo Frascino, Andrew Morton, linux-arch,
	linux-kernel, linux-mm, kasan-dev

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

THREAD_SIZE is the size of a kernel thread stack. It has been a
compile-time constant, but with vmap stacks it must be a multiple of
PAGE_SIZE, so with boot-time page size selection it can no longer be
fixed at build time.

Defining THREAD_SIZE for the largest supported page size would waste
memory with smaller pages. For example, arm64 needs 16K stacks, but 64K
pages with a vmap stack require 64K; paying that with 4K or 16K pages
would waste 48K per kernel thread.

So allow THREAD_SIZE to be a runtime value. Add THREAD_SIZE_MAX and
THREAD_ALIGN_MAX, defaulting to THREAD_SIZE and
max(THREAD_ALIGN, THREAD_SIZE_MAX), and use THREAD_SIZE_MAX to size
init_stack, thread_union and the init stack in the linker script.

In fork.c, replace the compile-time split between the page allocator
and the kmem_cache based stack allocator with run-time checks of
THREAD_SIZE >= PAGE_SIZE, and make thread_stack_cache_init() return
early when stacks are page based. The weak stub in init/main.c is now
conditional on CONFIG_VMAP_STACK. KASAN's init_task_stack_addr() uses
THREAD_SIZE rather than sizeof() of the stack array.

No functional change when THREAD_SIZE is a compile-time constant.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 13e7b56091910f3533cee539b7c1ef857f61fe4d)
---
 include/asm-generic/vmlinux.lds.h |  6 +++-
 include/linux/sched.h             |  4 +--
 include/linux/thread_info.h       | 10 +++++-
 init/main.c                       |  2 +-
 kernel/fork.c                     | 53 ++++++++++---------------------
 mm/kasan/report.c                 |  3 +-
 6 files changed, 35 insertions(+), 43 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 2a86599cb620..4df6c8fd38cb 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -56,6 +56,10 @@
 #define LOAD_OFFSET 0
 #endif
 
+#ifndef THREAD_SIZE_MAX
+#define THREAD_SIZE_MAX		THREAD_SIZE
+#endif
+
 /*
  * Only some architectures want to have the .notes segment visible in
  * a separate PT_NOTE ELF Program Header. When this happens, it needs
@@ -428,7 +432,7 @@
 	init_thread_union = .;						\
 	init_stack = .;							\
 	KEEP(*(.data..init_thread_info))				\
-	. = __start_init_stack + THREAD_SIZE;				\
+	. = __start_init_stack + THREAD_SIZE_MAX;			\
 	__end_init_stack = .;
 
 #define JUMP_TABLE_DATA							\
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d7855..2b649bbd668a 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1998,14 +1998,14 @@ union thread_union {
 #ifndef CONFIG_THREAD_INFO_IN_TASK
 	struct thread_info thread_info;
 #endif
-	unsigned long stack[THREAD_SIZE/sizeof(long)];
+	unsigned long stack[THREAD_SIZE_MAX/sizeof(long)];
 };
 
 #ifndef CONFIG_THREAD_INFO_IN_TASK
 extern struct thread_info init_thread_info;
 #endif
 
-extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
+extern unsigned long init_stack[THREAD_SIZE_MAX / sizeof(unsigned long)];
 
 #ifdef CONFIG_THREAD_INFO_IN_TASK
 # define task_thread_info(task)	(&(task)->thread_info)
diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index 307b8390fc67..73d0a77a3c2c 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -89,7 +89,15 @@ static inline long set_restart_fn(struct restart_block *restart,
 }
 
 #ifndef THREAD_ALIGN
-#define THREAD_ALIGN	THREAD_SIZE
+#define THREAD_ALIGN		THREAD_SIZE
+#endif
+
+#ifndef THREAD_SIZE_MAX
+#define THREAD_SIZE_MAX		THREAD_SIZE
+#endif
+
+#ifndef THREAD_ALIGN_MAX
+#define THREAD_ALIGN_MAX	max(THREAD_ALIGN, THREAD_SIZE_MAX)
 #endif
 
 #define THREADINFO_GFP		(GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_SKIP_KASAN)
diff --git a/init/main.c b/init/main.c
index 3d619be281aa..2a44990521eb 100644
--- a/init/main.c
+++ b/init/main.c
@@ -849,7 +849,7 @@ void __init __weak smp_prepare_boot_cpu(void)
 {
 }
 
-# if THREAD_SIZE >= PAGE_SIZE
+#ifdef CONFIG_VMAP_STACK
 void __init __weak thread_stack_cache_init(void)
 {
 }
diff --git a/kernel/fork.c b/kernel/fork.c
index 5a0db5a89615..87c102ea8d62 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -393,45 +393,15 @@ static void free_thread_stack(struct task_struct *tsk)
  * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
  * kmemcache based allocator.
  */
-#if THREAD_SIZE >= PAGE_SIZE
-
-static void thread_stack_free_rcu(struct rcu_head *rh)
-{
-	__free_pages(virt_to_page(rh), THREAD_SIZE_ORDER);
-}
-
-static void thread_stack_delayed_free(struct task_struct *tsk)
-{
-	struct rcu_head *rh = tsk->stack;
-
-	call_rcu(rh, thread_stack_free_rcu);
-}
-
-static int alloc_thread_stack_node(struct task_struct *tsk, int node)
-{
-	struct page *page = alloc_pages_node(node, THREADINFO_GFP,
-					     THREAD_SIZE_ORDER);
-
-	if (likely(page)) {
-		tsk->stack = kasan_reset_tag(page_address(page));
-		return 0;
-	}
-	return -ENOMEM;
-}
-
-static void free_thread_stack(struct task_struct *tsk)
-{
-	thread_stack_delayed_free(tsk);
-	tsk->stack = NULL;
-}
-
-#else /* !(THREAD_SIZE >= PAGE_SIZE) */
 
 static struct kmem_cache *thread_stack_cache;
 
 static void thread_stack_free_rcu(struct rcu_head *rh)
 {
-	kmem_cache_free(thread_stack_cache, rh);
+	if (THREAD_SIZE >= PAGE_SIZE)
+		__free_pages(virt_to_page(rh), THREAD_SIZE_ORDER);
+	else
+		kmem_cache_free(thread_stack_cache, rh);
 }
 
 static void thread_stack_delayed_free(struct task_struct *tsk)
@@ -444,7 +414,16 @@ static void thread_stack_delayed_free(struct task_struct *tsk)
 static int alloc_thread_stack_node(struct task_struct *tsk, int node)
 {
 	unsigned long *stack;
-	stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
+	struct page *page;
+
+	if (THREAD_SIZE >= PAGE_SIZE) {
+		page = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER);
+		stack = likely(page) ? page_address(page) : NULL;
+	} else {
+		stack = kmem_cache_alloc_node(thread_stack_cache,
+					      THREADINFO_GFP, node);
+	}
+
 	stack = kasan_reset_tag(stack);
 	tsk->stack = stack;
 	return stack ? 0 : -ENOMEM;
@@ -458,13 +437,15 @@ static void free_thread_stack(struct task_struct *tsk)
 
 void thread_stack_cache_init(void)
 {
+	if (THREAD_SIZE >= PAGE_SIZE)
+		return;
+
 	thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
 					THREAD_SIZE, THREAD_SIZE, 0, 0,
 					THREAD_SIZE, NULL);
 	BUG_ON(thread_stack_cache == NULL);
 }
 
-#endif /* THREAD_SIZE >= PAGE_SIZE */
 #endif /* CONFIG_VMAP_STACK */
 
 /* SLAB cache for signal_struct structures (tsk->signal) */
diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index e804b1e1f886..7b170efd224b 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -366,8 +366,7 @@ static inline bool kernel_or_module_addr(const void *addr)
 static inline bool init_task_stack_addr(const void *addr)
 {
 	return addr >= (void *)&init_thread_union.stack &&
-		(addr <= (void *)&init_thread_union.stack +
-			sizeof(init_thread_union.stack));
+		(addr <= (void *)&init_thread_union.stack + THREAD_SIZE);
 }
 
 static void print_address_description(void *addr, u8 tag,


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 08/43] cgroup: Use DEFINE_GLOBAL_PAGE_SIZE_VAR for the kernfs_ops tables
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (5 preceding siblings ...)
  2026-12-01  1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
@ 2026-12-01  1:51 ` 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
                   ` (31 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Tejun Heo, Johannes Weiner, Michal Koutný,
	cgroups

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

cgroup_kf_single_ops and cgroup_kf_ops initialise .atomic_write_len with
PAGE_SIZE. With boot-time page size selection that is not a compile-time
constant, so it cannot be used in a static initialiser.

Define both tables with DEFINE_GLOBAL_PAGE_SIZE_VAR() so that, for
boot-time page size builds, they are filled in at boot once the page
size is known.

No functional change for compile-time page sizes.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 565a75cff5df277dedade0680ed7a3810a4d7d44)
---
 kernel/cgroup/cgroup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index b3657e72db99..0421e88179b7 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -4495,16 +4495,16 @@ static int cgroup_seqfile_show(struct seq_file *m, void *arg)
 	return 0;
 }
 
-static struct kernfs_ops cgroup_kf_single_ops = {
+static DEFINE_GLOBAL_PAGE_SIZE_VAR(struct kernfs_ops, cgroup_kf_single_ops, {
 	.atomic_write_len	= PAGE_SIZE,
 	.open			= cgroup_file_open,
 	.release		= cgroup_file_release,
 	.write			= cgroup_file_write,
 	.poll			= cgroup_file_poll,
 	.seq_show		= cgroup_seqfile_show,
-};
+});
 
-static struct kernfs_ops cgroup_kf_ops = {
+static DEFINE_GLOBAL_PAGE_SIZE_VAR(struct kernfs_ops, cgroup_kf_ops, {
 	.atomic_write_len	= PAGE_SIZE,
 	.open			= cgroup_file_open,
 	.release		= cgroup_file_release,
@@ -4514,7 +4514,7 @@ static struct kernfs_ops cgroup_kf_ops = {
 	.seq_next		= cgroup_seqfile_next,
 	.seq_stop		= cgroup_seqfile_stop,
 	.seq_show		= cgroup_seqfile_show,
-};
+});
 
 static void cgroup_file_notify_timer(struct timer_list *timer)
 {


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 10/43] PM: hibernate: Do not size swap structures to exactly PAGE_SIZE
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (6 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 12/43] KVM: Check struct kvm_run against PAGE_SIZE_MIN Eric Curtin
                   ` (30 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Rafael J. Wysocki, Len Brown, Pavel Machek,
	linux-pm

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

struct linked_page, struct swap_map_page, struct swsusp_header and
struct swsusp_info are sized to exactly PAGE_SIZE, and struct cmp_data
and struct dec_data embed buffers sized from PAGE_SIZE. With boot-time
page size selection PAGE_SIZE is not a compile-time constant, so none of
this can be expressed in the type definitions.

Remove the padding and superimpose the structures on a page at run time:

- linked_page.data becomes a flexible array.
- swap_map_page.entries is sized with PAGE_SIZE_MAX, and the next_swap
  link lives in entries[NEXT_SWAP_INDEX], the last slot of the page.
- swsusp_header loses its reserved[] member and is placed at the end of
  the page held in swsusp_header_pg.
- swsusp_info loses __aligned(PAGE_SIZE); init_header() clears a full
  page instead.
- cmp_data and dec_data hold pointers to unc and cmp buffers, with new
  alloc/free helpers for each.

Because an image is only valid for the page size it was written with,
record page_shift in swsusp_info and make check_header() reject an image
whose page size does not match.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit e1d1db39b074b85e3d043190f3c59d67ffcf1136)
---
 kernel/power/power.h    |   3 +-
 kernel/power/snapshot.c |  10 +++-
 kernel/power/swap.c     | 129 +++++++++++++++++++++++++++++++++-------
 3 files changed, 115 insertions(+), 27 deletions(-)

diff --git a/kernel/power/power.h b/kernel/power/power.h
index 75b63843886e..e7bbf94fe00d 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -16,7 +16,8 @@ struct swsusp_info {
 	unsigned long		image_pages;
 	unsigned long		pages;
 	unsigned long		size;
-} __aligned(PAGE_SIZE);
+	unsigned int		page_shift;
+};
 
 #if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
 extern int pm_sleep_fs_sync(void);
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index d5dba0e50b2e..af49d749aa66 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -155,7 +155,7 @@ struct pbe *restore_pblist;
 
 struct linked_page {
 	struct linked_page *next;
-	char data[LINKED_PAGE_DATA_SIZE];
+	char data[];
 } __packed;
 
 /*
@@ -2179,12 +2179,13 @@ unsigned long snapshot_get_image_size(void)
 
 static int init_header(struct swsusp_info *info)
 {
-	memset(info, 0, sizeof(struct swsusp_info));
+	memset(info, 0, PAGE_SIZE);
 	info->num_physpages = get_num_physpages();
 	info->image_pages = nr_copy_pages;
 	info->pages = snapshot_get_image_size();
 	info->size = info->pages;
 	info->size <<= PAGE_SHIFT;
+	info->page_shift = PAGE_SHIFT;
 	return init_header_complete(info);
 }
 
@@ -2320,7 +2321,10 @@ static int check_header(struct swsusp_info *info)
 {
 	const char *reason;
 
-	reason = check_image_kernel(info);
+	if (info->page_shift != PAGE_SHIFT)
+		reason = "page size";
+	else
+		reason = check_image_kernel(info);
 	if (!reason && info->num_physpages != get_num_physpages())
 		reason = "memory size";
 	if (reason) {
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index c78f1593600b..4ae766593081 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -59,6 +59,7 @@ static bool clean_pages_on_decompress;
  * During resume we pick up all swap_map_page structures into a list.
  */
 #define MAP_PAGE_ENTRIES	(PAGE_SIZE / sizeof(sector_t) - 1)
+#define NEXT_SWAP_INDEX		MAP_PAGE_ENTRIES
 
 /*
  * Number of free pages that are not high.
@@ -78,8 +79,11 @@ static inline unsigned long reqd_free_pages(void)
 }
 
 struct swap_map_page {
-	sector_t entries[MAP_PAGE_ENTRIES];
-	sector_t next_swap;
+	/*
+	 * Only the first PAGE_SIZE bytes (PAGE_SIZE / sizeof(sector_t) entries)
+	 * are used. The last one, [NEXT_SWAP_INDEX], is `.next_swap`.
+	 */
+	sector_t entries[PAGE_SIZE_MAX / sizeof(sector_t)];
 };
 
 struct swap_map_page_list {
@@ -101,8 +105,6 @@ struct swap_map_handle {
 };
 
 struct swsusp_header {
-	char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
-	              sizeof(u32) - sizeof(u32)];
 	u32	hw_sig;
 	u32	crc32;
 	sector_t image;
@@ -111,6 +113,7 @@ struct swsusp_header {
 	char	sig[10];
 } __packed;
 
+static char *swsusp_header_pg;
 static struct swsusp_header *swsusp_header;
 
 /*
@@ -303,7 +306,7 @@ static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
 {
 	int error;
 
-	hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header);
+	hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header_pg);
 	if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
 	    !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
 		memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
@@ -317,7 +320,7 @@ static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
 		if (flags & SF_CRC32_MODE)
 			swsusp_header->crc32 = handle->crc32;
 		error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
-				      swsusp_resume_block, swsusp_header);
+				      swsusp_resume_block, swsusp_header_pg);
 	} else {
 		pr_err("Swap header not found!\n");
 		error = -ENODEV;
@@ -441,7 +444,7 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
 		if (!offset)
 			return -ENOSPC;
 
-		handle->cur->next_swap = offset;
+		handle->cur->entries[NEXT_SWAP_INDEX] = offset;
 		error = write_page(handle->cur, handle->cur_swap, hb);
 		if (error)
 			return error;
@@ -653,8 +656,8 @@ struct cmp_data {
 	wait_queue_head_t done;                   /* compression done */
 	size_t unc_len;                           /* uncompressed length */
 	size_t cmp_len;                           /* compressed length */
-	unsigned char unc[UNC_SIZE];              /* uncompressed buffer */
-	unsigned char cmp[CMP_SIZE];              /* compressed buffer */
+	unsigned char *unc;                       /* uncompressed buffer */
+	unsigned char *cmp;                       /* compressed buffer */
 };
 
 /* Indicates the image size after compression */
@@ -691,6 +694,45 @@ static int compress_threadfn(void *data)
 	return 0;
 }
 
+static void free_cmp_data(struct cmp_data *data, unsigned nr_threads)
+{
+	int i;
+
+	if (!data)
+		return;
+
+	for (i = 0; i < nr_threads; i++) {
+		vfree(data[i].unc);
+		vfree(data[i].cmp);
+	}
+
+	vfree(data);
+}
+
+static struct cmp_data *alloc_cmp_data(unsigned nr_threads)
+{
+	struct cmp_data *data = NULL;
+	int i = -1;
+
+	data = vcalloc(nr_threads, sizeof(*data));
+	if (!data)
+		goto fail;
+
+	for (i = 0; i < nr_threads; i++) {
+		data[i].unc = vzalloc(UNC_SIZE);
+		if (!data[i].unc)
+			goto fail;
+		data[i].cmp = vzalloc(CMP_SIZE);
+		if (!data[i].cmp)
+			goto fail;
+	}
+
+	return data;
+fail:
+	free_cmp_data(data, nr_threads);
+	return NULL;
+}
+
 static int save_compressed_image(struct swap_map_handle *handle,
 				 struct snapshot_handle *snapshot,
 				 unsigned int nr_to_write)
@@ -726,7 +768,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
 		goto out_clean;
 	}
 
-	data = vcalloc(nr_threads, sizeof(*data));
+	data = alloc_cmp_data(nr_threads);
 	if (!data) {
 		pr_err("Failed to allocate %s data\n", hib_comp_algo);
 		ret = -ENOMEM;
@@ -913,7 +955,7 @@ static int save_compressed_image(struct swap_map_handle *handle,
 			if (!IS_ERR_OR_NULL(data[thr].cc))
 				crypto_free_acomp(data[thr].cc);
 		}
-		vfree(data);
+		free_cmp_data(data, nr_threads);
 	}
 	if (page)
 		free_page((unsigned long)page);
@@ -1042,7 +1084,7 @@ static int get_swap_reader(struct swap_map_handle *handle,
 			release_swap_reader(handle);
 			return error;
 		}
-		offset = tmp->map->next_swap;
+		offset = tmp->map->entries[NEXT_SWAP_INDEX];
 	}
 	handle->k = 0;
 	handle->cur = handle->maps->map;
@@ -1154,8 +1196,8 @@ struct dec_data {
 	wait_queue_head_t done;                   /* decompression done */
 	size_t unc_len;                           /* uncompressed length */
 	size_t cmp_len;                           /* compressed length */
-	unsigned char unc[UNC_SIZE];              /* uncompressed buffer */
-	unsigned char cmp[CMP_SIZE];              /* compressed buffer */
+	unsigned char *unc;                       /* uncompressed buffer */
+	unsigned char *cmp;                       /* compressed buffer */
 };
 
 static int decompress_threadfn(void *data)
@@ -1192,6 +1234,45 @@ static int decompress_threadfn(void *data)
 	return 0;
 }
 
+static void free_dec_data(struct dec_data *data, unsigned nr_threads)
+{
+	int i;
+
+	if (!data)
+		return;
+
+	for (i = 0; i < nr_threads; i++) {
+		vfree(data[i].unc);
+		vfree(data[i].cmp);
+	}
+
+	vfree(data);
+}
+
+static struct dec_data *alloc_dec_data(unsigned nr_threads)
+{
+	struct dec_data *data = NULL;
+	int i = -1;
+
+	data = vcalloc(nr_threads, sizeof(*data));
+	if (!data)
+		goto fail;
+
+	for (i = 0; i < nr_threads; i++) {
+		data[i].unc = vzalloc(UNC_SIZE);
+		if (!data[i].unc)
+			goto fail;
+		data[i].cmp = vzalloc(CMP_SIZE);
+		if (!data[i].cmp)
+			goto fail;
+	}
+
+	return data;
+fail:
+	free_dec_data(data, nr_threads);
+	return NULL;
+}
+
 static int load_compressed_image(struct swap_map_handle *handle,
 				 struct snapshot_handle *snapshot,
 				 unsigned int nr_to_read)
@@ -1228,7 +1309,7 @@ static int load_compressed_image(struct swap_map_handle *handle,
 		goto out_clean;
 	}
 
-	data = vcalloc(nr_threads, sizeof(*data));
+	data = alloc_dec_data(nr_threads);
 	if (!data) {
 		pr_err("Failed to allocate %s data\n", hib_comp_algo);
 		ret = -ENOMEM;
@@ -1513,7 +1594,7 @@ static int load_compressed_image(struct swap_map_handle *handle,
 			if (!IS_ERR_OR_NULL(data[thr].cc))
 				crypto_free_acomp(data[thr].cc);
 		}
-		vfree(data);
+		free_dec_data(data, nr_threads);
 	}
 	vfree(page);
 
@@ -1574,9 +1655,9 @@ int swsusp_check(bool exclusive)
 	hib_resume_bdev_file = bdev_file_open_by_dev(swsusp_resume_device,
 				BLK_OPEN_READ, holder, NULL);
 	if (!IS_ERR(hib_resume_bdev_file)) {
-		clear_page(swsusp_header);
+		clear_page(swsusp_header_pg);
 		error = hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block,
-					swsusp_header);
+					swsusp_header_pg);
 		if (error)
 			goto put;
 
@@ -1586,7 +1667,7 @@ int swsusp_check(bool exclusive)
 			/* Reset swap signature now */
 			error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
 						swsusp_resume_block,
-						swsusp_header);
+						swsusp_header_pg);
 		} else {
 			error = -EINVAL;
 		}
@@ -1635,12 +1716,12 @@ int swsusp_unmark(void)
 {
 	int error;
 
-	hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header);
+	hib_submit_io_sync(REQ_OP_READ, swsusp_resume_block, swsusp_header_pg);
 	if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
 		memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
 		error = hib_submit_io_sync(REQ_OP_WRITE | REQ_SYNC,
 					swsusp_resume_block,
-					swsusp_header);
+					swsusp_header_pg);
 	} else {
 		pr_err("Cannot find swsusp signature!\n");
 		error = -ENODEV;
@@ -1695,9 +1776,11 @@ static int __init swsusp_header_init(void)
 	if (error)
 		return -ENOMEM;
 
-	swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
-	if (!swsusp_header)
+	swsusp_header_pg = (char *)__get_free_page(GFP_KERNEL);
+	if (!swsusp_header_pg)
 		panic("Could not allocate memory for swsusp_header\n");
+	swsusp_header = (struct swsusp_header *)(swsusp_header_pg +
+				PAGE_SIZE - sizeof(struct swsusp_header));
 	return 0;
 }
 


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 12/43] KVM: Check struct kvm_run against PAGE_SIZE_MIN
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (7 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 13/43] crash: Check crash_notes size " Eric Curtin
                   ` (29 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Sean Christopherson, Paolo Bonzini, kvm

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

With boot-time page size selection PAGE_SIZE is not a compile-time
constant, so the BUILD_BUG_ON() in kvm_vm_ioctl_create_vcpu() that
checks sizeof(struct kvm_run) against PAGE_SIZE cannot be evaluated.

Check against PAGE_SIZE_MIN instead. If kvm_run fits in the smallest
possible page it fits in whichever page size is chosen at boot.

No functional change for compile-time page sizes.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reviewed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 76729ec02c8773a6f44897c7629f594ab451312c)
---
 virt/kvm/kvm_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 83e727bf6452..794f41a2d052 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4294,7 +4294,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
 
 	vcpu->vcpu_idx = -1;
 
-	BUILD_BUG_ON(sizeof(struct kvm_run) > PAGE_SIZE);
+	BUILD_BUG_ON(sizeof(struct kvm_run) > PAGE_SIZE_MIN);
 	page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!page) {
 		r = -ENOMEM;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 13/43] crash: Check crash_notes size against PAGE_SIZE_MIN
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (8 preceding siblings ...)
  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 ` 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
                   ` (28 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Baoquan He, Andrew Morton, Baoquan He,
	Mike Rapoport, Pasha Tatashin, Pratyush Yadav, kexec

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

With boot-time page size selection PAGE_SIZE is not a compile-time
constant, so it cannot be used in BUILD_BUG_ON().

In crash_notes_memory_init(), check the size of the per-CPU crash notes
buffer against PAGE_SIZE_MIN. If it fits in the smallest possible page
it fits in whichever page size is chosen at boot.

No functional change for compile-time page sizes.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Acked-by: Baoquan He <bhe@redhat.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 6f90d8bafde4f6460bc1e449ffd28bd649a85e4a)
---
 kernel/crash_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index d0bd2d0cf899..c7eb72748fee 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -576,7 +576,7 @@ static int __init crash_notes_memory_init(void)
 	 * Break compile if size is bigger than PAGE_SIZE since crash_notes
 	 * definitely will be in 2 pages with that.
 	 */
-	BUILD_BUG_ON(size > PAGE_SIZE);
+	BUILD_BUG_ON(size > PAGE_SIZE_MIN);
 
 	crash_notes = __alloc_percpu(size, align);
 	if (!crash_notes) {


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 14/43] arm64: Make INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE usable from C
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (9 preceding siblings ...)
  2026-12-01  1:51 ` [RFC PATCH v2 13/43] crash: Check crash_notes size " Eric Curtin
@ 2026-12-01  1:51 ` 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
                   ` (27 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE use the symbols _end and
kimage_limit as the end of the kernel image. In the linker script these
resolve to integers holding the link-time virtual address. In C they
resolve to the address of the symbol in the image as loaded, which can
differ from the address it was linked at, so combining them with
KIMAGE_VADDR gives the wrong image size.

Add KIMAGE_VADDR_END and KIMAGE_VADDR_LIMIT, which compute the link-time
virtual address of _end and kimage_limit as _AT(u64, sym) - _AT(u64,
_text) + KIMAGE_VADDR. This is valid both in the linker script and in C.
Use them in INIT_DIR_SIZE and INIT_IDMAP_DIR_PAGES.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 39baea4f61f9134940df5053a53befa0a894270f)
---
 arch/arm64/include/asm/kernel-pgtable.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/include/asm/kernel-pgtable.h b/arch/arm64/include/asm/kernel-pgtable.h
index 229ee7976f69..91f3b6691c9f 100644
--- a/arch/arm64/include/asm/kernel-pgtable.h
+++ b/arch/arm64/include/asm/kernel-pgtable.h
@@ -34,6 +34,9 @@
 #define IDMAP_LEVELS		ARM64_HW_PGTABLE_LEVELS(IDMAP_VA_BITS)
 #define IDMAP_ROOT_LEVEL	(4 - IDMAP_LEVELS)
 
+#define KIMAGE_VADDR_END	(_AT(u64, _end) - _AT(u64, _text) + KIMAGE_VADDR)
+#define KIMAGE_VADDR_LIMIT	(_AT(u64, kimage_limit) - _AT(u64, _text) + KIMAGE_VADDR)
+
 /*
  * A relocatable kernel may execute from an address that differs from the one at
  * which it was linked. In the worst case, its runtime placement may intersect
@@ -55,10 +58,10 @@
 	+ EARLY_LEVEL(3, (lvls), (vstart), (vend), add) /* each entry needs a next level page table */	\
 	+ EARLY_LEVEL(2, (lvls), (vstart), (vend), add)	/* each entry needs a next level page table */	\
 	+ EARLY_LEVEL(1, (lvls), (vstart), (vend), add))/* each entry needs a next level page table */
-#define INIT_DIR_SIZE (PAGE_SIZE * (EARLY_PAGES(SWAPPER_PGTABLE_LEVELS, KIMAGE_VADDR, _end, EXTRA_PAGE) \
+#define INIT_DIR_SIZE (PAGE_SIZE * (EARLY_PAGES(SWAPPER_PGTABLE_LEVELS, KIMAGE_VADDR, KIMAGE_VADDR_END, EXTRA_PAGE) \
 				    + EARLY_SEGMENT_EXTRA_PAGES))
 
-#define INIT_IDMAP_DIR_PAGES	(EARLY_PAGES(INIT_IDMAP_PGTABLE_LEVELS, KIMAGE_VADDR, kimage_limit, 1))
+#define INIT_IDMAP_DIR_PAGES	(EARLY_PAGES(INIT_IDMAP_PGTABLE_LEVELS, KIMAGE_VADDR, KIMAGE_VADDR_LIMIT, 1))
 #define INIT_IDMAP_DIR_SIZE	((INIT_IDMAP_DIR_PAGES + EARLY_IDMAP_EXTRA_PAGES) * PAGE_SIZE)
 
 #define INIT_IDMAP_FDT_PAGES	(EARLY_PAGES(INIT_IDMAP_PGTABLE_LEVELS, 0UL, UL(MAX_FDT_SIZE), 1) - 1)


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 15/43] arm64: pi: Pass the early pgtable allocation limit to map_range()
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (10 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 16/43] arm64: Introduce macros required for boot-time page size selection Eric Curtin
                   ` (26 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

The early page tables (init_idmap_pg_dir, init_pg_dir, the FDT ptes and
so on) are allocated from statically sized blocks in the kernel image.
The block sizes are calculated for the worst case, which with boot-time
page size selection has to cover every supported page size, but nothing
detects an overrun if that calculation is wrong.

Pass the end of the allocation block to map_range(), create_init_idmap()
and map_segment() as a new limit argument. When map_range() is about to
allocate a table that would run past the limit, it records the new
CPU_STUCK_REASON_NO_PGTABLE_MEM reason in __early_cpu_boot_status,
makes the update visible to the boot CPU and parks the CPU. Callers
that only remap existing entries pass a limit of 0, as they do not
allocate. __early_cpu_boot_status and the mmuoff data bounds are
exported to the PI code to support this.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 4fb3483f68c4a14585c018b1bf3d76a1154a96b0)
---
 arch/arm64/include/asm/smp.h      |  1 +
 arch/arm64/kernel/head.S          |  3 ++-
 arch/arm64/kernel/image-vars.h    |  3 +++
 arch/arm64/kernel/pi/map_kernel.c | 39 ++++++++++++++-------------
 arch/arm64/kernel/pi/map_range.c  | 45 ++++++++++++++++++++++++++-----
 arch/arm64/kernel/pi/pi.h         |  9 ++++---
 arch/arm64/mm/mmu.c               | 12 ++++++---
 7 files changed, 79 insertions(+), 33 deletions(-)

diff --git a/arch/arm64/include/asm/smp.h b/arch/arm64/include/asm/smp.h
index 10ea4f543069..ffd8206b557e 100644
--- a/arch/arm64/include/asm/smp.h
+++ b/arch/arm64/include/asm/smp.h
@@ -22,6 +22,7 @@
 
 #define CPU_STUCK_REASON_52_BIT_VA	(UL(1) << CPU_STUCK_REASON_SHIFT)
 #define CPU_STUCK_REASON_NO_GRAN	(UL(2) << CPU_STUCK_REASON_SHIFT)
+#define CPU_STUCK_REASON_NO_PGTABLE_MEM	(UL(3) << CPU_STUCK_REASON_SHIFT)
 
 #ifndef __ASSEMBLER__
 
diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index d6ba3c0af811..2bdae5d0c5ef 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -90,7 +90,8 @@ SYM_CODE_START(primary_entry)
 	mov	sp, x1
 	mov	x29, xzr
 	adrp	x0, __pi_init_idmap_pg_dir
-	mov	x1, xzr
+	adrp	x1, __pi_init_idmap_pg_end
+	mov	x2, xzr
 	bl	__pi_create_init_idmap
 
 	/*
diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h
index 19d602e45523..9c9215847d26 100644
--- a/arch/arm64/kernel/image-vars.h
+++ b/arch/arm64/kernel/image-vars.h
@@ -59,6 +59,7 @@ PI_EXPORT_SYM(id_aa64zfr0_override);
 PI_EXPORT_SYM(arm64_sw_feature_override);
 PI_EXPORT_SYM(arm64_use_ng_mappings);
 PI_EXPORT_SYM(_ctype);
+PI_EXPORT_SYM(__early_cpu_boot_status);
 
 PI_EXPORT_SYM(swapper_pg_dir);
 
@@ -71,6 +72,8 @@ PI_EXPORT_SYM(__inittext_end);
 PI_EXPORT_SYM(__initdata_begin);
 PI_EXPORT_SYM(__initdata_end);
 PI_EXPORT_SYM(_data);
+PI_EXPORT_SYM(__mmuoff_data_start);
+PI_EXPORT_SYM(__mmuoff_data_end);
 
 #ifdef CONFIG_KVM
 
diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c
index f9338257adb7..88850c98597e 100644
--- a/arch/arm64/kernel/pi/map_kernel.c
+++ b/arch/arm64/kernel/pi/map_kernel.c
@@ -20,11 +20,11 @@ extern const u8 __eh_frame_start[], __eh_frame_end[];
 
 extern void idmap_cpu_replace_ttbr1(phys_addr_t pgdir);
 
-static void __init map_segment(pgd_t *pg_dir, phys_addr_t *pgd, u64 va_offset,
-			       void *start, void *end, pgprot_t prot,
-			       bool may_use_cont, int root_level)
+static void __init map_segment(pgd_t *pg_dir, phys_addr_t *pgd, u64 limit,
+			       u64 va_offset, void *start, void *end,
+			       pgprot_t prot, bool may_use_cont, int root_level)
 {
-	map_range(pgd, ((u64)start + va_offset) & ~PAGE_OFFSET,
+	map_range(pgd, limit, ((u64)start + va_offset) & ~PAGE_OFFSET,
 		  ((u64)end + va_offset) & ~PAGE_OFFSET, (u64)start,
 		  prot, root_level, (pte_t *)pg_dir, may_use_cont, 0);
 }
@@ -32,7 +32,7 @@ static void __init map_segment(pgd_t *pg_dir, phys_addr_t *pgd, u64 va_offset,
 static void __init unmap_segment(pgd_t *pg_dir, u64 va_offset, void *start,
 				 void *end, int root_level)
 {
-	map_segment(pg_dir, NULL, va_offset, start, end, __pgprot(0),
+	map_segment(pg_dir, NULL, 0, va_offset, start, end, __pgprot(0),
 		    false, root_level);
 }
 
@@ -41,6 +41,7 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level)
 	bool enable_scs = IS_ENABLED(CONFIG_UNWIND_PATCH_PAC_INTO_SCS);
 	bool twopass = IS_ENABLED(CONFIG_RELOCATABLE);
 	phys_addr_t pgdp = (phys_addr_t)init_pg_dir + PAGE_SIZE;
+	u64 limit = (u64)init_pg_end;
 	pgprot_t text_prot = PAGE_KERNEL_ROX;
 	pgprot_t data_prot = PAGE_KERNEL;
 	pgprot_t prot;
@@ -82,18 +83,18 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level)
 	 * [_stext, _text) isn't executed after boot and contains some
 	 * non-executable, unpredictable data, so map it non-executable.
 	 */
-	map_segment(init_pg_dir, &pgdp, va_offset, _text, _stext, data_prot,
-		    false, root_level);
-	map_segment(init_pg_dir, &pgdp, va_offset, _stext, _etext, prot,
+	map_segment(init_pg_dir, &pgdp, limit, va_offset, _text, _stext,
+		    data_prot, false, root_level);
+	map_segment(init_pg_dir, &pgdp, limit, va_offset, _stext, _etext, prot,
 		    !twopass, root_level);
-	map_segment(init_pg_dir, &pgdp, va_offset, __start_rodata,
+	map_segment(init_pg_dir, &pgdp, limit, va_offset, __start_rodata,
 		    __inittext_begin, data_prot, false, root_level);
-	map_segment(init_pg_dir, &pgdp, va_offset, __inittext_begin,
+	map_segment(init_pg_dir, &pgdp, limit, va_offset, __inittext_begin,
 		    __inittext_end, prot, false, root_level);
-	map_segment(init_pg_dir, &pgdp, va_offset, __initdata_begin,
+	map_segment(init_pg_dir, &pgdp, limit, va_offset, __initdata_begin,
 		    __initdata_end, data_prot, false, root_level);
-	map_segment(init_pg_dir, &pgdp, va_offset, _data, _end, data_prot,
-		    true, root_level);
+	map_segment(init_pg_dir, &pgdp, limit, va_offset, _data, _end,
+		    data_prot, true, root_level);
 	dsb(ishst);
 
 	idmap_cpu_replace_ttbr1((phys_addr_t)init_pg_dir);
@@ -126,9 +127,9 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level)
 		 * Remap these segments with different permissions
 		 * No new page table allocations should be needed
 		 */
-		map_segment(init_pg_dir, NULL, va_offset, _stext, _etext,
+		map_segment(init_pg_dir, NULL, 0, va_offset, _stext, _etext,
 			    text_prot, true, root_level);
-		map_segment(init_pg_dir, NULL, va_offset, __inittext_begin,
+		map_segment(init_pg_dir, NULL, 0, va_offset, __inittext_begin,
 			    __inittext_end, text_prot, false, root_level);
 	}
 
@@ -176,7 +177,7 @@ static void __init remap_idmap_for_lpa2(void)
 	 * LPA2 compatible fashion, and update the initial ID map while running
 	 * from that.
 	 */
-	create_init_idmap(init_pg_dir, mask);
+	create_init_idmap(init_pg_dir, init_pg_end, mask);
 	dsb(ishst);
 	set_ttbr0_for_lpa2((phys_addr_t)init_pg_dir);
 
@@ -187,7 +188,7 @@ static void __init remap_idmap_for_lpa2(void)
 	memset(init_idmap_pg_dir, 0,
 	       (char *)init_idmap_pg_end - (char *)init_idmap_pg_dir);
 
-	create_init_idmap(init_idmap_pg_dir, mask);
+	create_init_idmap(init_idmap_pg_dir, init_idmap_pg_end, mask);
 	dsb(ishst);
 
 	/* switch back to the updated initial ID map */
@@ -200,6 +201,7 @@ static void __init remap_idmap_for_lpa2(void)
 static void *__init map_fdt(phys_addr_t fdt)
 {
 	static u8 ptes[INIT_IDMAP_FDT_SIZE] __initdata __aligned(PAGE_SIZE);
+	u64 limit = (u64)&ptes[INIT_IDMAP_FDT_SIZE];
 	phys_addr_t efdt = fdt + MAX_FDT_SIZE;
 	phys_addr_t ptep = (phys_addr_t)ptes; /* We're idmapped when called */
 
@@ -207,7 +209,8 @@ static void *__init map_fdt(phys_addr_t fdt)
 	 * Map up to MAX_FDT_SIZE bytes, but avoid overlap with
 	 * the kernel image.
 	 */
-	map_range(&ptep, fdt, (u64)_text > fdt ? min((u64)_text, efdt) : efdt,
+	map_range(&ptep, limit, fdt,
+		  (u64)_text > fdt ? min((u64)_text, efdt) : efdt,
 		  fdt, PAGE_KERNEL, IDMAP_ROOT_LEVEL,
 		  (pte_t *)init_idmap_pg_dir, false, 0);
 	dsb(ishst);
diff --git a/arch/arm64/kernel/pi/map_range.c b/arch/arm64/kernel/pi/map_range.c
index 761b14893f74..20289f9683b8 100644
--- a/arch/arm64/kernel/pi/map_range.c
+++ b/arch/arm64/kernel/pi/map_range.c
@@ -11,11 +11,36 @@
 
 #include "pi.h"
 
+static void __init mmuoff_data_clean(void)
+{
+	bool cache_ena = !!(read_sysreg(sctlr_el1) & SCTLR_ELx_C);
+
+	if (cache_ena)
+		dcache_clean_poc((unsigned long)__mmuoff_data_start,
+				 (unsigned long)__mmuoff_data_end);
+	else
+		dcache_inval_poc((unsigned long)__mmuoff_data_start,
+				 (unsigned long)__mmuoff_data_end);
+}
+
+static void __init __noreturn report_cpu_stuck(long val)
+{
+	val |= CPU_STUCK_IN_KERNEL;
+	WRITE_ONCE(__early_cpu_boot_status, val);
+
+	/* Ensure the visibility of the status update */
+	dsb(ishst);
+	mmuoff_data_clean();
+
+	cpu_park_loop();
+}
+
 /**
  * map_range - Map a contiguous range of physical pages into virtual memory
  *
  * @pte:		Address of physical pointer to array of pages to
  *			allocate page tables from
+ * @limit:		Physical address of end of page allocation array
  * @start:		Virtual address of the start of the range
  * @end:		Virtual address of the end of the range (exclusive)
  * @pa:			Physical address of the start of the range
@@ -26,7 +51,8 @@
  * @va_offset:		Offset between a physical page and its current mapping
  * 			in the VA space
  */
-void __init map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa,
+void __init map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
+		      phys_addr_t pa,
 		      pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
 		      u64 va_offset)
 {
@@ -57,11 +83,17 @@ void __init map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa,
 			 * table mapping if necessary and recurse.
 			 */
 			if (pte_none(*tbl)) {
+				u64 size = PTRS_PER_PTE * sizeof(pte_t);
+
+				if (*pte + size > limit)
+					report_cpu_stuck(
+						CPU_STUCK_REASON_NO_PGTABLE_MEM);
+
 				*tbl = __pte(__phys_to_pte_val(*pte) |
 					     PMD_TYPE_TABLE | PMD_TABLE_UXN);
-				*pte += PTRS_PER_PTE * sizeof(pte_t);
+				*pte += size;
 			}
-			map_range(pte, start, next, pa, prot, level + 1,
+			map_range(pte, limit, start, next, pa, prot, level + 1,
 				  (pte_t *)(__pte_to_phys(*tbl) + va_offset),
 				  may_use_cont, va_offset);
 		} else {
@@ -88,7 +120,8 @@ void __init map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa,
 	}
 }
 
-asmlinkage phys_addr_t __init create_init_idmap(pgd_t *pg_dir, ptval_t clrmask)
+asmlinkage phys_addr_t __init create_init_idmap(pgd_t *pg_dir, pgd_t *pg_end,
+						 ptval_t clrmask)
 {
 	phys_addr_t ptep = (phys_addr_t)pg_dir + PAGE_SIZE; /* MMU is off */
 	pgprot_t text_prot = PAGE_KERNEL_ROX;
@@ -98,10 +131,10 @@ asmlinkage phys_addr_t __init create_init_idmap(pgd_t *pg_dir, ptval_t clrmask)
 	pgprot_val(data_prot) &= ~clrmask;
 
 	/* MMU is off; pointer casts to phys_addr_t are safe */
-	map_range(&ptep, (u64)_stext, (u64)__initdata_begin,
+	map_range(&ptep, (u64)pg_end, (u64)_stext, (u64)__initdata_begin,
 		  (phys_addr_t)_stext, text_prot, IDMAP_ROOT_LEVEL,
 		  (pte_t *)pg_dir, false, 0);
-	map_range(&ptep, (u64)__initdata_begin, (u64)_end,
+	map_range(&ptep, (u64)pg_end, (u64)__initdata_begin, (u64)_end,
 		  (phys_addr_t)__initdata_begin, data_prot, IDMAP_ROOT_LEVEL,
 		  (pte_t *)pg_dir, false, 0);
 
diff --git a/arch/arm64/kernel/pi/pi.h b/arch/arm64/kernel/pi/pi.h
index 5dfd8484d200..ce07cdc3445d 100644
--- a/arch/arm64/kernel/pi/pi.h
+++ b/arch/arm64/kernel/pi/pi.h
@@ -29,10 +29,11 @@ u64 kaslr_early_init(void *fdt, int chosen);
 void relocate_kernel(u64 offset);
 int scs_patch(const u8 eh_frame[], int size, bool skip_dry_run);
 
-void map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa,
-	       pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
-	       u64 va_offset);
+void map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
+	       phys_addr_t pa, pgprot_t prot, int level, pte_t *tbl,
+	       bool may_use_cont, u64 va_offset);
 
 asmlinkage void early_map_kernel(u64 boot_status, phys_addr_t fdt);
 
-asmlinkage phys_addr_t create_init_idmap(pgd_t *pgd, ptval_t clrmask);
+asmlinkage phys_addr_t create_init_idmap(pgd_t *pgd, pgd_t *pg_end,
+					 ptval_t clrmask);
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 19459a4a870a..4127db9fb645 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1437,7 +1437,8 @@ static void __init declare_kernel_vmas(void)
 	declare_vma(&vmlinux_seg[4], _data, _end, 0);
 }
 
-void __pi_map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa,
+void __pi_map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
+		    phys_addr_t pa,
 		    pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
 		    u64 va_offset);
 
@@ -1449,8 +1450,9 @@ static void __init create_idmap(void)
 	phys_addr_t start = __pa_symbol(__idmap_text_start);
 	phys_addr_t end   = __pa_symbol(__idmap_text_end);
 	phys_addr_t ptep  = __pa_symbol(idmap_ptes);
+	u64 limit = __pa_symbol(&idmap_ptes[IDMAP_LEVELS - 1][0]);
 
-	__pi_map_range(&ptep, start, end, start, PAGE_KERNEL_ROX,
+	__pi_map_range(&ptep, limit, start, end, start, PAGE_KERNEL_ROX,
 		       IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false,
 		       __phys_to_virt(ptep) - ptep);
 
@@ -1465,8 +1467,10 @@ static void __init create_idmap(void)
 		 * doesn't support bbml3.
 		 */
 		ptep = __pa_symbol(kpti_bbml3_ptes);
-		__pi_map_range(&ptep, pa, pa + sizeof(u32), pa, PAGE_KERNEL,
-			       IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false,
+		limit = __pa_symbol(&kpti_bbml3_ptes[IDMAP_LEVELS - 1][0]);
+		__pi_map_range(&ptep, limit, pa, pa + sizeof(u32), pa,
+			       PAGE_KERNEL, IDMAP_ROOT_LEVEL,
+			       (pte_t *)idmap_pg_dir, false,
 			       __phys_to_virt(ptep) - ptep);
 	}
 }


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 16/43] arm64: Introduce macros required for boot-time page size selection
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (11 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 17/43] arm64: Parameterise early pgtable size macros by page shift Eric Curtin
                   ` (25 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland,
	linux-kernel

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

Supporting a page size that is chosen at boot time needs the page and
page table geometry to be expressed in terms of values that can later
become variables, along with compile-time limits for code that needs a
constant.

Add asm/pgtable-geometry.h, which defines PAGE_SHIFT_MIN/MAX,
PAGE_SIZE_MIN/MAX and PAGE_MASK_MIN/MAX, together with ptg_*
definitions for the page, PMD, PUD, P4D and PGD shifts, the contiguous
PTE/PMD shifts and the number of page table levels. PAGE_SHIFT,
PMD_SHIFT, PUD_SHIFT, P4D_SHIFT, PGDIR_SHIFT, CONT_PTE_SHIFT and
CONT_PMD_SHIFT are now defined through them. Add MAX_PTRS_PER_PTE/PMD/
PUD/P4D for code that needs the largest possible table size as a
constant.

For now every ptg_* value is derived from the compile-time page size
configuration and both limits equal CONFIG_PAGE_SHIFT, so there is no
functional change. Later patches turn them into runtime values.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 9c901bc468652489bb381b43d47fa122b9faa392)
---
 arch/arm64/include/asm/page-def.h         |  9 +++++++-
 arch/arm64/include/asm/pgtable-geometry.h | 28 +++++++++++++++++++++++
 arch/arm64/include/asm/pgtable-hwdef.h    | 16 ++++++++-----
 3 files changed, 46 insertions(+), 7 deletions(-)
 create mode 100644 arch/arm64/include/asm/pgtable-geometry.h

diff --git a/arch/arm64/include/asm/page-def.h b/arch/arm64/include/asm/page-def.h
index e0ef167519e6..dd9e164e1978 100644
--- a/arch/arm64/include/asm/page-def.h
+++ b/arch/arm64/include/asm/page-def.h
@@ -9,9 +9,16 @@
 #define __ASM_PAGE_DEF_H
 
 #include <linux/const.h>
+#include <asm/pgtable-geometry.h>
 
 #include <vdso/page.h>
 
-#include <asm-generic/pgtable-geometry.h>
+/* PAGE_SHIFT determines the page size */
+#undef PAGE_SHIFT
+#undef PAGE_SIZE
+#undef PAGE_MASK
+#define PAGE_SHIFT		ptg_page_shift
+#define PAGE_SIZE		(_AC(1, UL) << PAGE_SHIFT)
+#define PAGE_MASK		(~(PAGE_SIZE-1))
 
 #endif /* __ASM_PAGE_DEF_H */
diff --git a/arch/arm64/include/asm/pgtable-geometry.h b/arch/arm64/include/asm/pgtable-geometry.h
new file mode 100644
index 000000000000..62fe125909c0
--- /dev/null
+++ b/arch/arm64/include/asm/pgtable-geometry.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef ASM_PGTABLE_GEOMETRY_H
+#define ASM_PGTABLE_GEOMETRY_H
+
+#define ARM64_PAGE_SHIFT_4K	12
+#define ARM64_PAGE_SHIFT_16K	14
+#define ARM64_PAGE_SHIFT_64K	16
+
+#define PAGE_SHIFT_MIN		CONFIG_PAGE_SHIFT
+#define PAGE_SIZE_MIN		(_AC(1, UL) << PAGE_SHIFT_MIN)
+#define PAGE_MASK_MIN		(~(PAGE_SIZE_MIN-1))
+
+#define PAGE_SHIFT_MAX		CONFIG_PAGE_SHIFT
+#define PAGE_SIZE_MAX		(_AC(1, UL) << PAGE_SHIFT_MAX)
+#define PAGE_MASK_MAX		(~(PAGE_SIZE_MAX-1))
+
+#include <asm-generic/pgtable-geometry.h>
+
+#define ptg_page_shift		CONFIG_PAGE_SHIFT
+#define ptg_pmd_shift		ARM64_HW_PGTABLE_LEVEL_SHIFT(2)
+#define ptg_pud_shift		ARM64_HW_PGTABLE_LEVEL_SHIFT(1)
+#define ptg_p4d_shift		ARM64_HW_PGTABLE_LEVEL_SHIFT(0)
+#define ptg_pgdir_shift		ARM64_HW_PGTABLE_LEVEL_SHIFT(4 - CONFIG_PGTABLE_LEVELS)
+#define ptg_cont_pte_shift	(CONFIG_ARM64_CONT_PTE_SHIFT + PAGE_SHIFT)
+#define ptg_cont_pmd_shift	(CONFIG_ARM64_CONT_PMD_SHIFT + PMD_SHIFT)
+#define ptg_pgtable_levels	CONFIG_PGTABLE_LEVELS
+
+#endif /* ASM_PGTABLE_GEOMETRY_H */
diff --git a/arch/arm64/include/asm/pgtable-hwdef.h b/arch/arm64/include/asm/pgtable-hwdef.h
index 72f31800c703..60e34018d5fa 100644
--- a/arch/arm64/include/asm/pgtable-hwdef.h
+++ b/arch/arm64/include/asm/pgtable-hwdef.h
@@ -47,39 +47,43 @@
 #define ARM64_HW_PGTABLE_LEVEL_SHIFT(n)	(PTDESC_TABLE_SHIFT * (4 - (n)) + PTDESC_ORDER)
 
 #define PTRS_PER_PTE		(1 << PTDESC_TABLE_SHIFT)
+#define MAX_PTRS_PER_PTE	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
 
 /*
  * PMD_SHIFT determines the size a level 2 page table entry can map.
  */
 #if CONFIG_PGTABLE_LEVELS > 2
-#define PMD_SHIFT		ARM64_HW_PGTABLE_LEVEL_SHIFT(2)
+#define PMD_SHIFT		ptg_pmd_shift
 #define PMD_SIZE		(_AC(1, UL) << PMD_SHIFT)
 #define PMD_MASK		(~(PMD_SIZE-1))
 #define PTRS_PER_PMD		(1 << PTDESC_TABLE_SHIFT)
+#define MAX_PTRS_PER_PMD	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
 #endif
 
 /*
  * PUD_SHIFT determines the size a level 1 page table entry can map.
  */
 #if CONFIG_PGTABLE_LEVELS > 3
-#define PUD_SHIFT		ARM64_HW_PGTABLE_LEVEL_SHIFT(1)
+#define PUD_SHIFT		ptg_pud_shift
 #define PUD_SIZE		(_AC(1, UL) << PUD_SHIFT)
 #define PUD_MASK		(~(PUD_SIZE-1))
 #define PTRS_PER_PUD		(1 << PTDESC_TABLE_SHIFT)
+#define MAX_PTRS_PER_PUD	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
 #endif
 
 #if CONFIG_PGTABLE_LEVELS > 4
-#define P4D_SHIFT		ARM64_HW_PGTABLE_LEVEL_SHIFT(0)
+#define P4D_SHIFT		ptg_p4d_shift
 #define P4D_SIZE		(_AC(1, UL) << P4D_SHIFT)
 #define P4D_MASK		(~(P4D_SIZE-1))
 #define PTRS_PER_P4D		(1 << PTDESC_TABLE_SHIFT)
+#define MAX_PTRS_PER_P4D	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
 #endif
 
 /*
  * PGDIR_SHIFT determines the size a top-level page table entry can map
  * (depending on the configuration, this level can be -1, 0, 1 or 2).
  */
-#define PGDIR_SHIFT		ARM64_HW_PGTABLE_LEVEL_SHIFT(4 - CONFIG_PGTABLE_LEVELS)
+#define PGDIR_SHIFT		ptg_pgdir_shift
 #define PGDIR_SIZE		(_AC(1, UL) << PGDIR_SHIFT)
 #define PGDIR_MASK		(~(PGDIR_SIZE-1))
 #define PTRS_PER_PGD		(1 << (VA_BITS - PGDIR_SHIFT))
@@ -87,12 +91,12 @@
 /*
  * Contiguous page definitions.
  */
-#define CONT_PTE_SHIFT		(CONFIG_ARM64_CONT_PTE_SHIFT + PAGE_SHIFT)
+#define CONT_PTE_SHIFT		ptg_cont_pte_shift
 #define CONT_PTES		(1 << (CONT_PTE_SHIFT - PAGE_SHIFT))
 #define CONT_PTE_SIZE		(CONT_PTES * PAGE_SIZE)
 #define CONT_PTE_MASK		(~(CONT_PTE_SIZE - 1))
 
-#define CONT_PMD_SHIFT		(CONFIG_ARM64_CONT_PMD_SHIFT + PMD_SHIFT)
+#define CONT_PMD_SHIFT		ptg_cont_pmd_shift
 #define CONT_PMDS		(1 << (CONT_PMD_SHIFT - PMD_SHIFT))
 #define CONT_PMD_SIZE		(CONT_PMDS * PMD_SIZE)
 #define CONT_PMD_MASK		(~(CONT_PMD_SIZE - 1))


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 17/43] arm64: Parameterise early pgtable size macros by page shift
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (12 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 18/43] arm64: Add arm64.pagesize= command line override Eric Curtin
                   ` (24 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

The early idmaps and the init/swapper page tables are built in static
storage that is sized at compile time from the page size. With boot-time
page size selection that storage has to be big enough for the worst
case, whichever page size ends up being chosen.

Make the macros in kernel-pgtable.h that determine the storage
requirement (SWAPPER_BLOCK_SHIFT, INIT_DIR_SIZE, INIT_IDMAP_DIR_SIZE,
INIT_IDMAP_FDT_SIZE, IDMAP_LEVELS, IDMAP_ROOT_LEVEL and friends) take a
page_shift argument, backed by new __ARM64_HW_PGTABLE_LEVELS() and
__ARM64_HW_PGTABLE_LEVEL_SHIFT() helpers. Evaluate them for each of the
4K, 16K and 64K page sizes within [PAGE_SHIFT_MIN, PAGE_SHIFT_MAX] and
take the maximum, to give IDMAP_LEVELS_MAX, INIT_DIR_SIZE_MAX,
INIT_IDMAP_DIR_SIZE_MAX and INIT_IDMAP_FDT_SIZE_MAX. The linker script,
map_fdt() and the idmap_ptes/kpti_bbml3_ptes arrays use the _MAX values.

With a compile-time page size only one size is in range, so the _MAX
values equal the previous ones and there is no functional change.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 86770c1aec367c208caca554f0be7f3a2f9ab17e)
---
 arch/arm64/include/asm/kernel-pgtable.h | 150 +++++++++++++++++-------
 arch/arm64/include/asm/pgtable-hwdef.h  |   9 +-
 arch/arm64/kernel/pi/map_kernel.c       |   6 +-
 arch/arm64/kernel/pi/map_range.c        |   8 +-
 arch/arm64/kernel/vmlinux.lds.S         |   4 +-
 arch/arm64/mm/mmu.c                     |  13 +-
 6 files changed, 129 insertions(+), 61 deletions(-)

diff --git a/arch/arm64/include/asm/kernel-pgtable.h b/arch/arm64/include/asm/kernel-pgtable.h
index 91f3b6691c9f..3f24f2d0829a 100644
--- a/arch/arm64/include/asm/kernel-pgtable.h
+++ b/arch/arm64/include/asm/kernel-pgtable.h
@@ -12,27 +12,38 @@
 #include <asm/pgtable-hwdef.h>
 #include <asm/sparsemem.h>
 
+#define PGTABLE_LEVELS(page_shift, va_bits)				\
+	__ARM64_HW_PGTABLE_LEVELS(page_shift, va_bits)
+#define PGTABLE_LEVEL_SHIFT(page_shift, n)				\
+	__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, n)
+#define PGTABLE_LEVEL_SIZE(page_shift, n)				\
+	(UL(1) << PGTABLE_LEVEL_SHIFT(page_shift, n))
+
 /*
  * The physical and virtual addresses of the start of the kernel image are
  * equal modulo 2 MiB (per the arm64 booting.txt requirements). Hence we can
  * use section mapping with 4K (section size = 2M) but not with 16K (section
  * size = 32M) or 64K (section size = 512M).
  */
-#if defined(PMD_SIZE) && PMD_SIZE <= MIN_KIMG_ALIGN
-#define SWAPPER_BLOCK_SHIFT	PMD_SHIFT
-#define SWAPPER_SKIP_LEVEL	1
-#else
-#define SWAPPER_BLOCK_SHIFT	PAGE_SHIFT
-#define SWAPPER_SKIP_LEVEL	0
-#endif
-#define SWAPPER_BLOCK_SIZE	(UL(1) << SWAPPER_BLOCK_SHIFT)
-
-#define SWAPPER_PGTABLE_LEVELS		(CONFIG_PGTABLE_LEVELS - SWAPPER_SKIP_LEVEL)
-#define INIT_IDMAP_PGTABLE_LEVELS	(IDMAP_LEVELS - SWAPPER_SKIP_LEVEL)
-
-#define IDMAP_VA_BITS		48
-#define IDMAP_LEVELS		ARM64_HW_PGTABLE_LEVELS(IDMAP_VA_BITS)
-#define IDMAP_ROOT_LEVEL	(4 - IDMAP_LEVELS)
+#define SWAPPER_BLOCK_SHIFT(page_shift)					\
+	((PGTABLE_LEVEL_SIZE(page_shift, 2) <= MIN_KIMG_ALIGN) ?	\
+		PGTABLE_LEVEL_SHIFT(page_shift, 2) : (page_shift))
+
+#define SWAPPER_SKIP_LEVEL(page_shift)					\
+	((PGTABLE_LEVEL_SIZE(page_shift, 2) <= MIN_KIMG_ALIGN) ? 1 : 0)
+
+#define SWAPPER_BLOCK_SIZE(page_shift)					\
+	(UL(1) << SWAPPER_BLOCK_SHIFT(page_shift))
+
+#define SWAPPER_PGTABLE_LEVELS(page_shift)				\
+	(PGTABLE_LEVELS(page_shift, VA_BITS) - SWAPPER_SKIP_LEVEL(page_shift))
+
+#define INIT_IDMAP_PGTABLE_LEVELS(page_shift)				\
+	(IDMAP_LEVELS(page_shift) - SWAPPER_SKIP_LEVEL(page_shift))
+
+#define IDMAP_VA_BITS			48
+#define IDMAP_LEVELS(page_shift)	PGTABLE_LEVELS(page_shift, IDMAP_VA_BITS)
+#define IDMAP_ROOT_LEVEL(page_shift)	(4 - IDMAP_LEVELS(page_shift))
 
 #define KIMAGE_VADDR_END	(_AT(u64, _end) - _AT(u64, _text) + KIMAGE_VADDR)
 #define KIMAGE_VADDR_LIMIT	(_AT(u64, kimage_limit) - _AT(u64, _text) + KIMAGE_VADDR)
@@ -43,52 +54,105 @@
  * with two adjacent PGDIR entries, which means that an additional page table
  * may be needed at each subordinate level.
  */
-#define EXTRA_PAGE	__is_defined(CONFIG_RELOCATABLE)
+#define EXTRA_PAGE			__is_defined(CONFIG_RELOCATABLE)
 
-#define SPAN_NR_ENTRIES(vstart, vend, shift) \
+#define SPAN_NR_ENTRIES(vstart, vend, shift)				\
 	((((vend) - 1) >> (shift)) - ((vstart) >> (shift)) + 1)
 
-#define EARLY_ENTRIES(lvl, vstart, vend) \
-	SPAN_NR_ENTRIES(vstart, vend, SWAPPER_BLOCK_SHIFT + lvl * PTDESC_TABLE_SHIFT)
-
-#define EARLY_LEVEL(lvl, lvls, vstart, vend, add) \
-	((lvls) > (lvl) ? EARLY_ENTRIES(lvl, vstart, vend) + (add) : 0)
+#define EARLY_ENTRIES(page_shift, lvl, vstart, vend)			\
+	SPAN_NR_ENTRIES(vstart, vend,					\
+		SWAPPER_BLOCK_SHIFT(page_shift) +			\
+		(lvl) * ((page_shift) - PTDESC_ORDER))
 
-#define EARLY_PAGES(lvls, vstart, vend, add) (1 	/* PGDIR page */				\
-	+ EARLY_LEVEL(3, (lvls), (vstart), (vend), add) /* each entry needs a next level page table */	\
-	+ EARLY_LEVEL(2, (lvls), (vstart), (vend), add)	/* each entry needs a next level page table */	\
-	+ EARLY_LEVEL(1, (lvls), (vstart), (vend), add))/* each entry needs a next level page table */
-#define INIT_DIR_SIZE (PAGE_SIZE * (EARLY_PAGES(SWAPPER_PGTABLE_LEVELS, KIMAGE_VADDR, KIMAGE_VADDR_END, EXTRA_PAGE) \
-				    + EARLY_SEGMENT_EXTRA_PAGES))
+#define EARLY_LEVEL(page_shift, lvl, lvls, vstart, vend, add)		\
+	((lvls) > (lvl) ?						\
+		EARLY_ENTRIES(page_shift, lvl, vstart, vend) + (add) : 0)
 
-#define INIT_IDMAP_DIR_PAGES	(EARLY_PAGES(INIT_IDMAP_PGTABLE_LEVELS, KIMAGE_VADDR, KIMAGE_VADDR_LIMIT, 1))
-#define INIT_IDMAP_DIR_SIZE	((INIT_IDMAP_DIR_PAGES + EARLY_IDMAP_EXTRA_PAGES) * PAGE_SIZE)
-
-#define INIT_IDMAP_FDT_PAGES	(EARLY_PAGES(INIT_IDMAP_PGTABLE_LEVELS, 0UL, UL(MAX_FDT_SIZE), 1) - 1)
-#define INIT_IDMAP_FDT_SIZE	((INIT_IDMAP_FDT_PAGES + EARLY_IDMAP_EXTRA_FDT_PAGES) * PAGE_SIZE)
+#define EARLY_PAGES(page_shift, lvls, vstart, vend, add) (1 /* PGDIR */	\
+	+ EARLY_LEVEL((page_shift), 3, (lvls), (vstart), (vend), add)	\
+	+ EARLY_LEVEL((page_shift), 2, (lvls), (vstart), (vend), add)	\
+	+ EARLY_LEVEL((page_shift), 1, (lvls), (vstart), (vend), add))
 
 /* The number of segments in the kernel image (text, rodata, inittext, initdata, data+bss) */
-#define KERNEL_SEGMENT_COUNT	5
+#define KERNEL_SEGMENT_COUNT		5
 
-#if SWAPPER_BLOCK_SIZE > SEGMENT_ALIGN
 /*
  * KERNEL_SEGMENT_COUNT counts the permanent kernel VMAs. The early mapping
  * has one additional split, [_text, _stext). Reserve one more page for the
  * SWAPPER_BLOCK_SIZE-unaligned boundaries.
  */
-#define EARLY_SEGMENT_EXTRA_PAGES (KERNEL_SEGMENT_COUNT + 2)
+#define EARLY_SEGMENT_EXTRA_PAGES(page_shift)				\
+	((SWAPPER_BLOCK_SIZE(page_shift) > SEGMENT_ALIGN) ?		\
+		(KERNEL_SEGMENT_COUNT + 2) : 0)
+
 /*
  * The initial ID map consists of the kernel image, mapped as two separate
  * segments, and may appear misaligned wrt the swapper block size. This means
  * we need 3 additional pages. The DT could straddle a swapper block boundary,
  * so it may need 2.
  */
-#define EARLY_IDMAP_EXTRA_PAGES		3
-#define EARLY_IDMAP_EXTRA_FDT_PAGES	2
-#else
-#define EARLY_SEGMENT_EXTRA_PAGES	0
-#define EARLY_IDMAP_EXTRA_PAGES		0
-#define EARLY_IDMAP_EXTRA_FDT_PAGES	0
-#endif
+#define EARLY_IDMAP_EXTRA_PAGES(page_shift)				\
+	((SWAPPER_BLOCK_SIZE(page_shift) > SEGMENT_ALIGN) ? 3 : 0)
+
+#define EARLY_IDMAP_EXTRA_FDT_PAGES(page_shift)				\
+	((SWAPPER_BLOCK_SIZE(page_shift) > SEGMENT_ALIGN) ? 2 : 0)
+
+#define INIT_DIR_PAGES(page_shift)					\
+	(EARLY_PAGES((page_shift), SWAPPER_PGTABLE_LEVELS(page_shift),	\
+		KIMAGE_VADDR, KIMAGE_VADDR_END, EXTRA_PAGE))
+
+#define INIT_DIR_SIZE(page_shift)					\
+	((INIT_DIR_PAGES(page_shift) +					\
+	EARLY_SEGMENT_EXTRA_PAGES(page_shift)) * (UL(1) << (page_shift)))
+
+#define INIT_IDMAP_DIR_PAGES(page_shift)				\
+	(EARLY_PAGES((page_shift),					\
+		INIT_IDMAP_PGTABLE_LEVELS(page_shift),			\
+		KIMAGE_VADDR, KIMAGE_VADDR_LIMIT, 1))
+
+#define INIT_IDMAP_DIR_SIZE(page_shift)					\
+	((INIT_IDMAP_DIR_PAGES(page_shift) +				\
+	EARLY_IDMAP_EXTRA_PAGES(page_shift)) * (UL(1) << (page_shift)))
+
+#define INIT_IDMAP_FDT_PAGES(page_shift)				\
+	(EARLY_PAGES((page_shift),					\
+		INIT_IDMAP_PGTABLE_LEVELS(page_shift),			\
+		UL(0), UL(MAX_FDT_SIZE), 1) - 1)
+
+#define INIT_IDMAP_FDT_SIZE(page_shift)					\
+	((INIT_IDMAP_FDT_PAGES(page_shift) +				\
+	EARLY_IDMAP_EXTRA_FDT_PAGES(page_shift)) * (UL(1) << (page_shift)))
+
+#define VAL_IF_HAVE_PGSZ(val, page_shift)				\
+	((page_shift) >= PAGE_SHIFT_MIN &&				\
+	 (page_shift) <= PAGE_SHIFT_MAX ? (val) : 0)
+
+#define MAX_IF_HAVE_PGSZ(val4k, val16k, val64k)				\
+	MAX(VAL_IF_HAVE_PGSZ((val4k), ARM64_PAGE_SHIFT_4K), MAX(	\
+	    VAL_IF_HAVE_PGSZ((val16k), ARM64_PAGE_SHIFT_16K),		\
+	    VAL_IF_HAVE_PGSZ((val64k), ARM64_PAGE_SHIFT_64K)))
+
+#define IDMAP_LEVELS_MAX						\
+	MAX_IF_HAVE_PGSZ(IDMAP_LEVELS(ARM64_PAGE_SHIFT_4K),		\
+			 IDMAP_LEVELS(ARM64_PAGE_SHIFT_16K),		\
+			 IDMAP_LEVELS(ARM64_PAGE_SHIFT_64K))
+
+#define __INIT_DIR_SIZE_MAX						\
+	MAX_IF_HAVE_PGSZ(INIT_DIR_SIZE(ARM64_PAGE_SHIFT_4K),		\
+			 INIT_DIR_SIZE(ARM64_PAGE_SHIFT_16K),		\
+			 INIT_DIR_SIZE(ARM64_PAGE_SHIFT_64K))
+
+#define INIT_DIR_SIZE_MAX						\
+	MAX(__INIT_DIR_SIZE_MAX, INIT_IDMAP_DIR_SIZE_MAX)
+
+#define INIT_IDMAP_DIR_SIZE_MAX						\
+	MAX_IF_HAVE_PGSZ(INIT_IDMAP_DIR_SIZE(ARM64_PAGE_SHIFT_4K),	\
+			 INIT_IDMAP_DIR_SIZE(ARM64_PAGE_SHIFT_16K),	\
+			 INIT_IDMAP_DIR_SIZE(ARM64_PAGE_SHIFT_64K))
+
+#define INIT_IDMAP_FDT_SIZE_MAX						\
+	MAX_IF_HAVE_PGSZ(INIT_IDMAP_FDT_SIZE(ARM64_PAGE_SHIFT_4K),	\
+			 INIT_IDMAP_FDT_SIZE(ARM64_PAGE_SHIFT_16K),	\
+			 INIT_IDMAP_FDT_SIZE(ARM64_PAGE_SHIFT_64K))
 
 #endif	/* __ASM_KERNEL_PGTABLE_H */
diff --git a/arch/arm64/include/asm/pgtable-hwdef.h b/arch/arm64/include/asm/pgtable-hwdef.h
index 60e34018d5fa..02373c4ea474 100644
--- a/arch/arm64/include/asm/pgtable-hwdef.h
+++ b/arch/arm64/include/asm/pgtable-hwdef.h
@@ -28,8 +28,10 @@
  *
  * which gets simplified as :
  */
+#define __ARM64_HW_PGTABLE_LEVELS(page_shift, va_bits) \
+	(((va_bits) - PTDESC_ORDER - 1) / ((page_shift) - PTDESC_ORDER))
 #define ARM64_HW_PGTABLE_LEVELS(va_bits) \
-	(((va_bits) - PTDESC_ORDER - 1) / PTDESC_TABLE_SHIFT)
+	__ARM64_HW_PGTABLE_LEVELS(PAGE_SHIFT, va_bits)
 
 /*
  * Size mapped by an entry at level n ( -1 <= n <= 3)
@@ -44,7 +46,10 @@
  * Rearranging it a bit we get :
  *   (4 - n) * PTDESC_TABLE_SHIFT + PTDESC_ORDER
  */
-#define ARM64_HW_PGTABLE_LEVEL_SHIFT(n)	(PTDESC_TABLE_SHIFT * (4 - (n)) + PTDESC_ORDER)
+#define __ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, n) \
+	(((page_shift) - PTDESC_ORDER) * (4 - (n)) + PTDESC_ORDER)
+#define ARM64_HW_PGTABLE_LEVEL_SHIFT(n) \
+	__ARM64_HW_PGTABLE_LEVEL_SHIFT(PAGE_SHIFT, n)
 
 #define PTRS_PER_PTE		(1 << PTDESC_TABLE_SHIFT)
 #define MAX_PTRS_PER_PTE	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c
index 88850c98597e..47d5b29cf99c 100644
--- a/arch/arm64/kernel/pi/map_kernel.c
+++ b/arch/arm64/kernel/pi/map_kernel.c
@@ -200,8 +200,8 @@ static void __init remap_idmap_for_lpa2(void)
 
 static void *__init map_fdt(phys_addr_t fdt)
 {
-	static u8 ptes[INIT_IDMAP_FDT_SIZE] __initdata __aligned(PAGE_SIZE);
-	u64 limit = (u64)&ptes[INIT_IDMAP_FDT_SIZE];
+	static u8 ptes[INIT_IDMAP_FDT_SIZE_MAX] __initdata __aligned(PAGE_SIZE);
+	u64 limit = (u64)&ptes[INIT_IDMAP_FDT_SIZE_MAX];
 	phys_addr_t efdt = fdt + MAX_FDT_SIZE;
 	phys_addr_t ptep = (phys_addr_t)ptes; /* We're idmapped when called */
 
@@ -211,7 +211,7 @@ static void *__init map_fdt(phys_addr_t fdt)
 	 */
 	map_range(&ptep, limit, fdt,
 		  (u64)_text > fdt ? min((u64)_text, efdt) : efdt,
-		  fdt, PAGE_KERNEL, IDMAP_ROOT_LEVEL,
+		  fdt, PAGE_KERNEL, IDMAP_ROOT_LEVEL(PAGE_SHIFT),
 		  (pte_t *)init_idmap_pg_dir, false, 0);
 	dsb(ishst);
 	isb();
diff --git a/arch/arm64/kernel/pi/map_range.c b/arch/arm64/kernel/pi/map_range.c
index 20289f9683b8..4ca0aaf12e17 100644
--- a/arch/arm64/kernel/pi/map_range.c
+++ b/arch/arm64/kernel/pi/map_range.c
@@ -132,11 +132,11 @@ asmlinkage phys_addr_t __init create_init_idmap(pgd_t *pg_dir, pgd_t *pg_end,
 
 	/* MMU is off; pointer casts to phys_addr_t are safe */
 	map_range(&ptep, (u64)pg_end, (u64)_stext, (u64)__initdata_begin,
-		  (phys_addr_t)_stext, text_prot, IDMAP_ROOT_LEVEL,
-		  (pte_t *)pg_dir, false, 0);
+		  (phys_addr_t)_stext, text_prot,
+		  IDMAP_ROOT_LEVEL(PAGE_SHIFT), (pte_t *)pg_dir, false, 0);
 	map_range(&ptep, (u64)pg_end, (u64)__initdata_begin, (u64)_end,
-		  (phys_addr_t)__initdata_begin, data_prot, IDMAP_ROOT_LEVEL,
-		  (pte_t *)pg_dir, false, 0);
+		  (phys_addr_t)__initdata_begin, data_prot,
+		  IDMAP_ROOT_LEVEL(PAGE_SHIFT), (pte_t *)pg_dir, false, 0);
 
 	return ptep;
 }
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index c95243a8b24e..ca3016f284d5 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -280,7 +280,7 @@ SECTIONS
 	__initdata_begin = .;
 
 	__pi_init_idmap_pg_dir = .;
-	. += INIT_IDMAP_DIR_SIZE;
+	. += INIT_IDMAP_DIR_SIZE_MAX;
 	__pi_init_idmap_pg_end = .;
 
 	.init.data : {
@@ -368,7 +368,7 @@ SECTIONS
 
 	. = ALIGN(PAGE_SIZE);
 	__pi_init_pg_dir = .;
-	. += INIT_DIR_SIZE;
+	. += INIT_DIR_SIZE_MAX;
 	__pi_init_pg_end = .;
 	/* end of zero-init region */
 
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 4127db9fb645..1567f76607d9 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1442,19 +1442,19 @@ void __pi_map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
 		    pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
 		    u64 va_offset);
 
-static u8 idmap_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init,
-	  kpti_bbml3_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init;
+static u8 idmap_ptes[IDMAP_LEVELS_MAX - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init,
+	  kpti_bbml3_ptes[IDMAP_LEVELS_MAX - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init;
 
 static void __init create_idmap(void)
 {
 	phys_addr_t start = __pa_symbol(__idmap_text_start);
 	phys_addr_t end   = __pa_symbol(__idmap_text_end);
 	phys_addr_t ptep  = __pa_symbol(idmap_ptes);
-	u64 limit = __pa_symbol(&idmap_ptes[IDMAP_LEVELS - 1][0]);
+	u64 limit = __pa_symbol(&idmap_ptes[IDMAP_LEVELS_MAX - 1][0]);
 
 	__pi_map_range(&ptep, limit, start, end, start, PAGE_KERNEL_ROX,
-		       IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false,
-		       __phys_to_virt(ptep) - ptep);
+		       IDMAP_ROOT_LEVEL(PAGE_SHIFT), (pte_t *)idmap_pg_dir,
+		       false, __phys_to_virt(ptep) - ptep);
 
 	if (linear_map_requires_bbml3 ||
 	    (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0) && !arm64_use_ng_mappings)) {
@@ -1467,9 +1467,8 @@ static void __init create_idmap(void)
 		 * doesn't support bbml3.
 		 */
 		ptep = __pa_symbol(kpti_bbml3_ptes);
-		limit = __pa_symbol(&kpti_bbml3_ptes[IDMAP_LEVELS - 1][0]);
 		__pi_map_range(&ptep, limit, pa, pa + sizeof(u32), pa,
-			       PAGE_KERNEL, IDMAP_ROOT_LEVEL,
+			       PAGE_KERNEL, IDMAP_ROOT_LEVEL(PAGE_SHIFT),
 			       (pte_t *)idmap_pg_dir, false,
 			       __phys_to_virt(ptep) - ptep);
 	}


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 18/43] arm64: Add arm64.pagesize= command line override
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (13 preceding siblings ...)
  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 ` 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
                   ` (23 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

Boot-time page size selection needs a way for the user to request a
page size. Accept "arm64.pagesize=4k", "arm64.pagesize=16k" or
"arm64.pagesize=64k", which set a new pageshift field in the
software feature override register. The page shift is stored in a 4 bit
field in compressed form, as (shift - 10) / 2, and
arm64_pageshift_cmdline() converts it back.

The override is only honoured when early_page_shift_supported() accepts
the requested size, that is when the hardware supports it and it lies
within the compile-time [PAGE_SIZE_MIN, PAGE_SIZE_MAX] range. As that
range is a single size for a compile-time page size build, the option
is ignored there.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit ca43672932d6d458c531e2b750d1e63da248fd35)
---
 arch/arm64/include/asm/cpufeature.h   | 11 +++++++++++
 arch/arm64/kernel/pi/idreg-override.c | 13 +++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h
index 7d7b8eb7f72f..405291491866 100644
--- a/arch/arm64/include/asm/cpufeature.h
+++ b/arch/arm64/include/asm/cpufeature.h
@@ -18,6 +18,7 @@
 #define ARM64_SW_FEATURE_OVERRIDE_NOKASLR	0
 #define ARM64_SW_FEATURE_OVERRIDE_HVHE		4
 #define ARM64_SW_FEATURE_OVERRIDE_RODATA_OFF	8
+#define ARM64_SW_FEATURE_OVERRIDE_PAGESHIFT	12
 
 #ifndef __ASSEMBLER__
 
@@ -1050,6 +1051,16 @@ static inline bool arm64_test_sw_feature_override(int feat)
 					    &arm64_sw_feature_override);
 }
 
+static inline int arm64_pageshift_cmdline(void)
+{
+	int val;
+
+	val = arm64_apply_feature_override(0,
+					   ARM64_SW_FEATURE_OVERRIDE_PAGESHIFT,
+					   4, &arm64_sw_feature_override);
+	return val ? val * 2 + 10 : 0;
+}
+
 static inline bool kaslr_disabled_cmdline(void)
 {
 	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_NOKASLR);
diff --git a/arch/arm64/kernel/pi/idreg-override.c b/arch/arm64/kernel/pi/idreg-override.c
index 2f132b44ef27..f2641106e164 100644
--- a/arch/arm64/kernel/pi/idreg-override.c
+++ b/arch/arm64/kernel/pi/idreg-override.c
@@ -216,6 +216,15 @@ static bool __init hvhe_filter(u64 val)
 						     ID_AA64MMFR1_EL1_VH_SHIFT));
 }
 
+static bool __init pageshift_filter(u64 val)
+{
+	/* pageshift is stored compressed in 4 bit field. */
+	if (val)
+		val = val * 2 + 10;
+
+	return early_page_shift_supported(val);
+}
+
 static const struct ftr_set_desc sw_features __prel64_initconst = {
 	.name		= "arm64_sw",
 	.override	= &arm64_sw_feature_override,
@@ -223,6 +232,7 @@ static const struct ftr_set_desc sw_features __prel64_initconst = {
 		FIELD("nokaslr", ARM64_SW_FEATURE_OVERRIDE_NOKASLR, NULL),
 		FIELD("hvhe", ARM64_SW_FEATURE_OVERRIDE_HVHE, hvhe_filter),
 		FIELD("rodataoff", ARM64_SW_FEATURE_OVERRIDE_RODATA_OFF, NULL),
+		FIELD("pageshift", ARM64_SW_FEATURE_OVERRIDE_PAGESHIFT, pageshift_filter),
 		{}
 	},
 };
@@ -265,6 +275,9 @@ static const struct {
 	{ "arm64.nolva",		"id_aa64mmfr2.varange=0" },
 	{ "arm64.no32bit_el0",		"id_aa64pfr0.el0=1" },
 	{ "arm64.nompam",		"id_aa64pfr0.mpam=0 id_aa64pfr1.mpam_frac=0" },
+	{ "arm64.pagesize=4k",		"arm64_sw.pageshift=1" },
+	{ "arm64.pagesize=16k",		"arm64_sw.pageshift=2" },
+	{ "arm64.pagesize=64k",		"arm64_sw.pageshift=3" },
 };
 
 static int __init parse_hexdigit(const char *p, u64 *v)


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 19/43] arm64: pi: Select the page size at runtime during early boot
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (14 preceding siblings ...)
  2026-12-01  1:51 ` [RFC PATCH v2 18/43] arm64: Add arm64.pagesize= command line override Eric Curtin
@ 2026-12-01  1:51 ` 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
                   ` (22 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

Make all the early code between the entry point and start_kernel()
independent of the compile-time page size, so that the page size can be
chosen at boot. For now the compile-time size is still the only one that
is allowed, so behaviour does not change, but the range of allowed sizes
is widened by later patches.

probe_init_idmap_page_shift() picks the initial page size used for
init_idmap_pg_dir: the largest size supported by the hardware that is
also within [PAGE_SHIFT_MIN, PAGE_SHIFT_MAX]. Once the MMU is on, the
command line is read from the device tree and the requested size is
filtered by the same range. If none was requested, the configured
default and then the initial size are used. If the final size or LPA2
differs from the initial setup, remap_idmap() repaints
init_idmap_pg_dir and the FDT mapping for the new page size.

To get there, compile-time decisions become runtime ones. map_range()
and create_init_idmap() take the page size from tcr_el1.TG0 (or an
explicit page_shift), and the output address encoding for 52-bit PA is
chosen accordingly. The TG0, TG1 and DS fields are stashed in
tcr_boot_fields so that __cpu_setup can program tcr_el1 on the primary
and secondary CPUs. __enable_mmu, __cpu_secondary_check52bitva,
offset_ttbr1 and cpu_has_lva()/cpu_has_lpa2() now check the installed
page size rather than the configuration, and the LPA2 detection in the
override code covers both the 4K and 16K granules. These checks are not
in hot paths, so the runtime approach is used even for compile-time
page size builds.

The static page table storage is still sized for the compile-time page
size.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 4b79790f74f2492dd8c876af3e9dd935c02195bf)
---
 arch/arm64/include/asm/assembler.h    |  50 ++++++-
 arch/arm64/include/asm/cpufeature.h   |  33 +++--
 arch/arm64/include/asm/pgtable-prot.h |   2 +-
 arch/arm64/kernel/head.S              |  40 ++++--
 arch/arm64/kernel/pi/idreg-override.c |  37 ++++--
 arch/arm64/kernel/pi/map_kernel.c     | 105 +++++++++++----
 arch/arm64/kernel/pi/map_range.c      | 183 ++++++++++++++++++++++----
 arch/arm64/kernel/pi/pi.h             |  61 ++++++++-
 arch/arm64/mm/proc.S                  |  24 ++--
 9 files changed, 434 insertions(+), 101 deletions(-)

diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
index 020670814bda..fc9659c64848 100644
--- a/arch/arm64/include/asm/assembler.h
+++ b/arch/arm64/include/asm/assembler.h
@@ -564,6 +564,14 @@ alternative_else_nop_endif
 	mrs	\rd, sp_el0
 	.endm
 
+/*
+ * Retrieve and return tcr_el1.tg0 in \tg0.
+ */
+	.macro	get_tg0, tg0
+	mrs	\tg0, tcr_el1
+	and	\tg0, \tg0, #TCR_TG0_MASK
+	.endm
+
 /*
  * If the kernel is built for 52-bit virtual addressing but the hardware only
  * supports 48 bits, we cannot program the pgdir address into TTBR1 directly,
@@ -580,12 +588,16 @@ alternative_else_nop_endif
  * 	ttbr: Value of ttbr to set, modified.
  */
 	.macro	offset_ttbr1, ttbr, tmp
-#if defined(CONFIG_ARM64_VA_BITS_52) && !defined(CONFIG_ARM64_LPA2)
+#if defined(CONFIG_ARM64_VA_BITS_52)
+	get_tg0	\tmp
+	cmp	\tmp, #TCR_TG0_64K
+	b.ne	.Ldone\@
 	mrs	\tmp, tcr_el1
 	and	\tmp, \tmp, #TCR_EL1_T1SZ_MASK
 	cmp	\tmp, #TCR_T1SZ(VA_BITS_MIN)
 	orr	\tmp, \ttbr, #TTBR1_BADDR_4852_OFFSET
 	csel	\ttbr, \tmp, \ttbr, eq
+.Ldone\@:
 #endif
 	.endm
 
@@ -837,4 +849,40 @@ alternative_cb ARM64_ALWAYS_SYSTEM, spectre_bhb_patch_clearbhb
 alternative_cb_end
 #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
 	.endm
+
+/*
+ * Given \tg0, populates \val with one of the 3 passed in values, corresponding
+ * to the page size advertised by \tg0.
+ */
+	.macro	value_for_page_size, val, tg0, val4k, val16k, val64k
+.Lsz_64k\@:
+	cmp	\tg0, #TCR_TG0_64K
+	b.ne	.Lsz_16k\@
+	mov	\val, #\val64k
+	b	.Ldone\@
+.Lsz_16k\@:
+	cmp	\tg0, #TCR_TG0_16K
+	b.ne	.Lsz_4k\@
+	mov	\val, #\val16k
+	b	.Ldone\@
+.Lsz_4k\@:
+	mov	\val, #\val4k
+.Ldone\@:
+	.endm
+
+	.macro	tgran_shift, val, tg0
+	value_for_page_size \val, \tg0, ID_AA64MMFR0_EL1_TGRAN4_SHIFT, ID_AA64MMFR0_EL1_TGRAN16_SHIFT, ID_AA64MMFR0_EL1_TGRAN64_SHIFT
+	.endm
+
+	.macro	tgran_min, val, tg0
+	value_for_page_size \val, \tg0, ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MIN, ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MIN, ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MIN
+	.endm
+
+	.macro	tgran_max, val, tg0
+	value_for_page_size \val, \tg0, ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MAX, ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MAX, ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MAX
+	.endm
+
+	.macro	tgran_lpa2, val, tg0
+	value_for_page_size \val, \tg0, ID_AA64MMFR0_EL1_TGRAN4_52_BIT, ID_AA64MMFR0_EL1_TGRAN16_52_BIT, -1
+	.endm
 #endif	/* __ASM_ASSEMBLER_H */
diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h
index 405291491866..35239ea01e77 100644
--- a/arch/arm64/include/asm/cpufeature.h
+++ b/arch/arm64/include/asm/cpufeature.h
@@ -26,6 +26,7 @@
 #include <linux/jump_label.h>
 #include <linux/kernel.h>
 #include <linux/cpumask.h>
+#include <linux/sizes.h>
 
 /*
  * CPU feature register tracking
@@ -1101,10 +1102,13 @@ static inline bool cpu_has_pac(void)
 					    &id_aa64isar2_override);
 }
 
-static inline bool cpu_has_lva(void)
+static inline bool cpu_has_lva(u64 page_size)
 {
 	u64 mmfr2;
 
+	if (page_size != SZ_64K)
+		return false;
+
 	mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
 	mmfr2 &= ~id_aa64mmfr2_override.mask;
 	mmfr2 |= id_aa64mmfr2_override.val;
@@ -1112,22 +1116,31 @@ static inline bool cpu_has_lva(void)
 						    ID_AA64MMFR2_EL1_VARange_SHIFT);
 }
 
-static inline bool cpu_has_lpa2(void)
+static inline bool cpu_has_lpa2(u64 page_size)
 {
-#ifdef CONFIG_ARM64_LPA2
 	u64 mmfr0;
 	int feat;
+	int shift;
+	int minval;
+
+	switch (page_size) {
+	case SZ_4K:
+		shift = ID_AA64MMFR0_EL1_TGRAN4_SHIFT;
+		minval = ID_AA64MMFR0_EL1_TGRAN4_52_BIT;
+		break;
+	case SZ_16K:
+		shift = ID_AA64MMFR0_EL1_TGRAN16_SHIFT;
+		minval = ID_AA64MMFR0_EL1_TGRAN16_52_BIT;
+		break;
+	default:
+		return false;
+	}
 
 	mmfr0 = read_sysreg(id_aa64mmfr0_el1);
 	mmfr0 &= ~id_aa64mmfr0_override.mask;
 	mmfr0 |= id_aa64mmfr0_override.val;
-	feat = cpuid_feature_extract_signed_field(mmfr0,
-						  ID_AA64MMFR0_EL1_TGRAN_SHIFT);
-
-	return feat >= ID_AA64MMFR0_EL1_TGRAN_LPA2;
-#else
-	return false;
-#endif
+	feat = cpuid_feature_extract_signed_field(mmfr0, shift);
+	return feat >= minval;
 }
 
 #endif /* __ASSEMBLER__ */
diff --git a/arch/arm64/include/asm/pgtable-prot.h b/arch/arm64/include/asm/pgtable-prot.h
index 590ef4328f12..da792605ac57 100644
--- a/arch/arm64/include/asm/pgtable-prot.h
+++ b/arch/arm64/include/asm/pgtable-prot.h
@@ -78,7 +78,7 @@ extern unsigned long prot_ns_shared;
 #define PTE_MAYBE_NG		(arm64_use_ng_mappings ? PTE_NG : 0)
 #define PMD_MAYBE_NG		(arm64_use_ng_mappings ? PMD_SECT_NG : 0)
 
-#ifndef CONFIG_ARM64_LPA2
+#ifndef CONFIG_ARM64_VA_BITS_52
 #define lpa2_is_enabled()	false
 #define PTE_MAYBE_SHARED	PTE_SHARED
 #define PMD_MAYBE_SHARED	PMD_SECT_S
diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index 2bdae5d0c5ef..3641e638b31e 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -89,6 +89,8 @@ SYM_CODE_START(primary_entry)
 	adrp	x1, early_init_stack
 	mov	sp, x1
 	mov	x29, xzr
+	bl	__pi_probe_init_idmap_page_shift
+	mov	x3, x0
 	adrp	x0, __pi_init_idmap_pg_dir
 	adrp	x1, __pi_init_idmap_pg_end
 	mov	x2, xzr
@@ -457,11 +459,16 @@ SYM_FUNC_END(set_cpu_boot_mode_flag)
  */
 	.section ".idmap.text","a"
 SYM_FUNC_START(__enable_mmu)
+	get_tg0 x3
+	tgran_shift x4, x3
+	tgran_min x5, x3
+	tgran_max x6, x3
 	mrs	x3, ID_AA64MMFR0_EL1
-	ubfx	x3, x3, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4
-	cmp     x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN
+	lsr	x3, x3, x4
+	ubfx	x3, x3, #0, 4
+	cmp     x3, x5
 	b.lt    __no_granule_support
-	cmp     x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX
+	cmp     x3, x6
 	b.gt    __no_granule_support
 	phys_to_ttbr x2, x2
 	msr	ttbr0_el1, x2			// load TTBR0
@@ -474,17 +481,32 @@ SYM_FUNC_END(__enable_mmu)
 
 #ifdef CONFIG_ARM64_VA_BITS_52
 SYM_FUNC_START(__cpu_secondary_check52bitva)
-#ifndef CONFIG_ARM64_LPA2
+	/*
+	 * tcr_el1 is not yet loaded (there is a chicken-and-egg problem) so we
+	 * can't figure out LPA2 vs LVA from that. But this is only called for
+	 * secondary cpus so tcr_boot_fields has already been populated by the
+	 * primary cpu. So grab that and rely on the DS bit to tell us if we are
+	 * LPA2 or LVA.
+	 */
+	adr_l	x1, __pi_tcr_boot_fields
+	ldr	x0, [x1]
+	and	x1, x0, #TCR_DS
+	cbnz	x1, .Llpa2
+.Llva:
 	mrs_s	x0, SYS_ID_AA64MMFR2_EL1
 	and	x0, x0, ID_AA64MMFR2_EL1_VARange_MASK
 	cbnz	x0, 2f
-#else
+	b	.Lfail
+.Llpa2:
+	and	x0, x0, #TCR_TG0_MASK
+	tgran_shift x1, x0
+	tgran_lpa2 x2, x0
 	mrs	x0, id_aa64mmfr0_el1
-	sbfx	x0, x0, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4
-	cmp	x0, #ID_AA64MMFR0_EL1_TGRAN_LPA2
+	lsr	x0, x0, x1
+	sbfx	x0, x0, #0, 4
+	cmp	x0, x2
 	b.ge	2f
-#endif
-
+.Lfail:
 	update_early_cpu_boot_status \
 		CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_52_BIT_VA, x0, x1
 1:	wfe
diff --git a/arch/arm64/kernel/pi/idreg-override.c b/arch/arm64/kernel/pi/idreg-override.c
index f2641106e164..c58f34b24ea7 100644
--- a/arch/arm64/kernel/pi/idreg-override.c
+++ b/arch/arm64/kernel/pi/idreg-override.c
@@ -72,19 +72,38 @@ static const struct ftr_set_desc mmfr1 __prel64_initconst = {
 
 static bool __init mmfr2_varange_filter(u64 val)
 {
-	int __maybe_unused feat;
+	u64 mmfr0;
+	int feat;
+	int shift;
+	int minval;
+	bool lpa2_removed = false;
 
 	if (val)
 		return false;
 
-#ifdef CONFIG_ARM64_LPA2
-	feat = cpuid_feature_extract_signed_field(read_sysreg(id_aa64mmfr0_el1),
-						  ID_AA64MMFR0_EL1_TGRAN_SHIFT);
-	if (feat >= ID_AA64MMFR0_EL1_TGRAN_LPA2) {
-		id_aa64mmfr0_override.val |=
-			(ID_AA64MMFR0_EL1_TGRAN_LPA2 - 1) << ID_AA64MMFR0_EL1_TGRAN_SHIFT;
-		id_aa64mmfr0_override.mask |= 0xfU << ID_AA64MMFR0_EL1_TGRAN_SHIFT;
+	mmfr0 = read_sysreg(id_aa64mmfr0_el1);
+
+	/* Remove LPA2 support for 4K granule. */
+	shift = ID_AA64MMFR0_EL1_TGRAN4_SHIFT;
+	minval = ID_AA64MMFR0_EL1_TGRAN4_52_BIT;
+	feat = cpuid_feature_extract_signed_field(mmfr0, shift);
+	if (feat >= minval) {
+		id_aa64mmfr0_override.val |= (minval - 1) << shift;
+		id_aa64mmfr0_override.mask |= 0xfU << shift;
+		lpa2_removed = true;
+	}
 
+	/* Remove LPA2 support for 16K granule. */
+	shift = ID_AA64MMFR0_EL1_TGRAN16_SHIFT;
+	minval = ID_AA64MMFR0_EL1_TGRAN16_52_BIT;
+	feat = cpuid_feature_extract_signed_field(mmfr0, shift);
+	if (feat >= minval) {
+		id_aa64mmfr0_override.val |= (minval - 1) << shift;
+		id_aa64mmfr0_override.mask |= 0xfU << shift;
+		lpa2_removed = true;
+	}
+
+	if (lpa2_removed) {
 		/*
 		 * Override PARange to 48 bits - the override will just be
 		 * ignored if the actual PARange is smaller, but this is
@@ -94,7 +113,7 @@ static bool __init mmfr2_varange_filter(u64 val)
 			ID_AA64MMFR0_EL1_PARANGE_48 << ID_AA64MMFR0_EL1_PARANGE_SHIFT;
 		id_aa64mmfr0_override.mask |= 0xfU << ID_AA64MMFR0_EL1_PARANGE_SHIFT;
 	}
-#endif
+
 	return true;
 }
 
diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c
index 47d5b29cf99c..357ccb562746 100644
--- a/arch/arm64/kernel/pi/map_kernel.c
+++ b/arch/arm64/kernel/pi/map_kernel.c
@@ -139,16 +139,26 @@ static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level)
 	idmap_cpu_replace_ttbr1((phys_addr_t)swapper_pg_dir);
 }
 
-static void noinline __section(".idmap.text") set_ttbr0_for_lpa2(phys_addr_t ttbr)
+static void noinline __section(".idmap.text") set_ttbr0(phys_addr_t ttbr,
+							bool use_lpa2,
+							int page_shift)
 {
 	u64 sctlr = read_sysreg(sctlr_el1);
-	u64 tcr = read_sysreg(tcr_el1) | TCR_EL1_DS;
-	u64 mmfr0 = read_sysreg(id_aa64mmfr0_el1);
-	u64 parange = cpuid_feature_extract_unsigned_field(mmfr0,
-							   ID_AA64MMFR0_EL1_PARANGE_SHIFT);
+	u64 tcr = read_sysreg(tcr_el1);
+	u64 boot_fields = early_page_shift_to_tcr_tgx(page_shift);
 
-	tcr &= ~TCR_EL1_IPS_MASK;
-	tcr |= parange << TCR_EL1_IPS_SHIFT;
+	if (use_lpa2) {
+		u64 mmfr0 = read_sysreg(id_aa64mmfr0_el1);
+		u64 parange = cpuid_feature_extract_unsigned_field(mmfr0,
+				ID_AA64MMFR0_EL1_PARANGE_SHIFT);
+
+		boot_fields |= TCR_EL1_DS;
+		tcr &= ~TCR_EL1_IPS_MASK;
+		tcr |= parange << TCR_EL1_IPS_SHIFT;
+	}
+
+	tcr &= ~(TCR_EL1_TG0_MASK | TCR_EL1_TG1_MASK | TCR_EL1_DS);
+	tcr |= boot_fields;
 
 	asm("	msr	sctlr_el1, %0		;"
 	    "	isb				;"
@@ -161,57 +171,66 @@ static void noinline __section(".idmap.text") set_ttbr0_for_lpa2(phys_addr_t ttb
 	    "	msr     sctlr_el1, %3		;"
 	    "	isb				;"
 	    ::	"r"(sctlr & ~SCTLR_ELx_M), "r"(ttbr), "r"(tcr), "r"(sctlr));
+
+	/* Stash this for __cpu_setup to configure secondary cpus' tcr_el1. */
+	set_tcr_boot_fields(boot_fields);
 }
 
-static void __init remap_idmap_for_lpa2(void)
+static void __init remap_idmap(bool use_lpa2, int page_shift)
 {
 	/* clear the bits that change meaning once LPA2 is turned on */
-	ptval_t mask = PTE_SHARED;
+	ptval_t mask = use_lpa2 ? PTE_SHARED : 0;
 
 	/*
-	 * We have to clear bits [9:8] in all block or page descriptors in the
-	 * initial ID map, as otherwise they will be (mis)interpreted as
+	 * For LPA2, We have to clear bits [9:8] in all block or page descriptors
+	 * in the initial ID map, as otherwise they will be (mis)interpreted as
 	 * physical address bits once we flick the LPA2 switch (TCR.DS). Since
 	 * we cannot manipulate live descriptors in that way without creating
 	 * potential TLB conflicts, let's create another temporary ID map in a
 	 * LPA2 compatible fashion, and update the initial ID map while running
 	 * from that.
 	 */
-	create_init_idmap(init_pg_dir, init_pg_end, mask);
+	create_init_idmap(init_pg_dir, init_pg_end, mask, page_shift);
 	dsb(ishst);
-	set_ttbr0_for_lpa2((phys_addr_t)init_pg_dir);
+	set_ttbr0((phys_addr_t)init_pg_dir, use_lpa2, page_shift);
 
 	/*
-	 * Recreate the initial ID map with the same granularity as before.
-	 * Don't bother with the FDT, we no longer need it after this.
+	 * Recreate the initial ID map with new page size and, if LPA2 is in
+	 * use, bits [9:8] cleared.
 	 */
 	memset(init_idmap_pg_dir, 0,
 	       (char *)init_idmap_pg_end - (char *)init_idmap_pg_dir);
 
-	create_init_idmap(init_idmap_pg_dir, init_idmap_pg_end, mask);
+	create_init_idmap(init_idmap_pg_dir, init_idmap_pg_end, mask, page_shift);
 	dsb(ishst);
 
 	/* switch back to the updated initial ID map */
-	set_ttbr0_for_lpa2((phys_addr_t)init_idmap_pg_dir);
+	set_ttbr0((phys_addr_t)init_idmap_pg_dir, use_lpa2, page_shift);
 
 	/* wipe the temporary ID map from memory */
 	memset(init_pg_dir, 0, (char *)init_pg_end - (char *)init_pg_dir);
 }
 
-static void *__init map_fdt(phys_addr_t fdt)
+static void *__init map_fdt(phys_addr_t fdt, int page_shift)
 {
 	static u8 ptes[INIT_IDMAP_FDT_SIZE_MAX] __initdata __aligned(PAGE_SIZE);
+	static bool first_time __initdata = true;
 	u64 limit = (u64)&ptes[INIT_IDMAP_FDT_SIZE_MAX];
 	phys_addr_t efdt = fdt + MAX_FDT_SIZE;
 	phys_addr_t ptep = (phys_addr_t)ptes; /* We're idmapped when called */
 
+	if (!first_time) {
+		memset(ptes, 0, sizeof(ptes));
+		first_time = false;
+	}
+
 	/*
 	 * Map up to MAX_FDT_SIZE bytes, but avoid overlap with
 	 * the kernel image.
 	 */
 	map_range(&ptep, limit, fdt,
 		  (u64)_text > fdt ? min((u64)_text, efdt) : efdt,
-		  fdt, PAGE_KERNEL, IDMAP_ROOT_LEVEL(PAGE_SHIFT),
+		  fdt, PAGE_KERNEL, IDMAP_ROOT_LEVEL(page_shift),
 		  (pte_t *)init_idmap_pg_dir, false, 0);
 	dsb(ishst);
 	isb();
@@ -245,12 +264,15 @@ static bool __init ng_mappings_allowed(void)
 asmlinkage void __init early_map_kernel(u64 boot_status, phys_addr_t fdt)
 {
 	static char const chosen_str[] __initconst = "/chosen";
+	u64 early_page_shift = early_tcr_tg0_to_page_shift();
 	u64 va_base, pa_base = (u64)&_text;
 	u64 kaslr_offset = pa_base % MIN_KIMG_ALIGN;
-	int root_level = 4 - CONFIG_PGTABLE_LEVELS;
 	int va_bits = VA_BITS;
+	bool use_lpa2 = false;
+	int root_level;
+	int page_shift;
 	int chosen;
-	void *fdt_mapped = map_fdt(fdt);
+	void *fdt_mapped = map_fdt(fdt, early_page_shift);
 
 	/* Clear BSS and the initial page tables */
 	memset(__bss_start, 0, (char *)init_pg_end - (char *)__bss_start);
@@ -259,16 +281,43 @@ asmlinkage void __init early_map_kernel(u64 boot_status, phys_addr_t fdt)
 	chosen = fdt_path_offset(fdt_mapped, chosen_str);
 	init_feature_override(boot_status, fdt_mapped, chosen);
 
-	if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && !cpu_has_lva()) {
-		va_bits = VA_BITS_MIN;
-	} else if (IS_ENABLED(CONFIG_ARM64_LPA2) && !cpu_has_lpa2()) {
-		va_bits = VA_BITS_MIN;
-		root_level++;
+	/*
+	 * Get page_shift from cmdline, falling back to the configured default
+	 * and then to early_page_shift.
+	 */
+	page_shift = arm64_pageshift_cmdline();
+	if (!page_shift && IS_ENABLED(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE) &&
+	    early_page_shift_supported(CONFIG_ARM64_BOOT_TIME_PAGE_SHIFT_DEFAULT))
+		page_shift = CONFIG_ARM64_BOOT_TIME_PAGE_SHIFT_DEFAULT;
+	if (!page_shift)
+		page_shift = early_page_shift;
+
+	if (va_bits > 48) {
+		u64 page_size = early_page_size(page_shift);
+
+		if (page_size == SZ_64K) {
+			if (!cpu_has_lva(page_size))
+				va_bits = VA_BITS_MIN;
+		} else {
+			use_lpa2 = cpu_has_lpa2(page_size);
+			if (!use_lpa2)
+				va_bits = VA_BITS_MIN;
+		}
 	}
 
 	if (va_bits > VA_BITS_MIN)
 		sysreg_clear_set(tcr_el1, TCR_EL1_T1SZ_MASK, TCR_T1SZ(va_bits));
 
+	/*
+	 * This will update tg0/tg1 in tcr for the final page size. After this,
+	 * PAGE_SIZE and friends can be used safely. kaslr_early_init(), below,
+	 * is the first such user.
+	 */
+	if (use_lpa2 || page_shift != early_page_shift) {
+		remap_idmap(use_lpa2, page_shift);
+		map_fdt(fdt, page_shift);
+	}
+
 	/*
 	 * The virtual KASLR displacement modulo 2MiB is decided by the
 	 * physical placement of the image, as otherwise, we might not be able
@@ -285,9 +334,7 @@ asmlinkage void __init early_map_kernel(u64 boot_status, phys_addr_t fdt)
 		kaslr_offset |= kaslr_seed & ~(MIN_KIMG_ALIGN - 1);
 	}
 
-	if (IS_ENABLED(CONFIG_ARM64_LPA2) && va_bits > VA_BITS_MIN)
-		remap_idmap_for_lpa2();
-
 	va_base = KIMAGE_VADDR + kaslr_offset;
+	root_level = 4 - PGTABLE_LEVELS(page_shift, va_bits);
 	map_kernel(kaslr_offset, va_base - pa_base, root_level);
 }
diff --git a/arch/arm64/kernel/pi/map_range.c b/arch/arm64/kernel/pi/map_range.c
index 4ca0aaf12e17..0dc99cdd38d2 100644
--- a/arch/arm64/kernel/pi/map_range.c
+++ b/arch/arm64/kernel/pi/map_range.c
@@ -11,6 +11,34 @@
 
 #include "pi.h"
 
+static inline u64 __init pte_get_oa(pte_t pte, int page_shift, bool oa52bit)
+{
+	pteval_t pv = pte_val(pte);
+
+#ifdef CONFIG_ARM64_PA_BITS_52
+	if (oa52bit) {
+		if (early_page_size(page_shift) == SZ_64K)
+			return (pv & GENMASK(47, 16)) |
+			       ((pv & GENMASK(15, 12)) << 36);
+		return (pv & GENMASK(49, 12)) | ((pv & GENMASK(9, 8)) << 42);
+	}
+#endif
+	return pv & GENMASK(47, 12);
+}
+
+static inline u64 __init pte_prep_oa(u64 oa, int page_shift, bool oa52bit)
+{
+#ifdef CONFIG_ARM64_PA_BITS_52
+	if (oa52bit) {
+		if (early_page_size(page_shift) == SZ_64K)
+			return (oa & GENMASK(47, 16)) |
+			       ((oa >> 36) & GENMASK(15, 12));
+		return (oa & GENMASK(49, 12)) | ((oa >> 42) & GENMASK(9, 8));
+	}
+#endif
+	return oa;
+}
+
 static void __init mmuoff_data_clean(void)
 {
 	bool cache_ena = !!(read_sysreg(sctlr_el1) & SCTLR_ELx_C);
@@ -35,8 +63,19 @@ static void __init __noreturn report_cpu_stuck(long val)
 	cpu_park_loop();
 }
 
+u64 __section(".mmuoff.data.read") tcr_boot_fields;
+
+void __init set_tcr_boot_fields(u64 val)
+{
+	WRITE_ONCE(tcr_boot_fields, val);
+
+	/* Ensure the visibility of the new value */
+	dsb(ishst);
+	mmuoff_data_clean();
+}
+
 /**
- * map_range - Map a contiguous range of physical pages into virtual memory
+ * __map_range - Map a contiguous range of physical pages into virtual memory
  *
  * @pte:		Address of physical pointer to array of pages to
  *			allocate page tables from
@@ -50,22 +89,28 @@ static void __init __noreturn report_cpu_stuck(long val)
  * @may_use_cont:	Whether the use of the contiguous attribute is allowed
  * @va_offset:		Offset between a physical page and its current mapping
  * 			in the VA space
+ * @page_shift:		Page size (as a shift) to create page table for
+ * @oa52bit:		Whether to store output addresses in 52-bit format
  */
-void __init map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
-		      phys_addr_t pa,
-		      pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
-		      u64 va_offset)
+static void __init __map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
+			       phys_addr_t pa, pgprot_t prot, int level,
+			       pte_t *tbl, bool may_use_cont, u64 va_offset,
+			       int page_shift, bool oa52bit)
 {
-	u64 cmask = (level == 3) ? CONT_PTE_SIZE - 1 : U64_MAX;
+	const u64 page_size = early_page_size(page_shift);
+	const u64 page_mask = early_page_mask(page_shift);
+	const u64 cont_pte_size = early_cont_pte_size(page_shift);
+	const u64 ptrs_per_pte = early_ptrs_per_pte(page_shift);
+	u64 cmask = (level == 3) ? cont_pte_size - 1 : U64_MAX;
 	ptval_t protval = pgprot_val(prot) & ~PTE_TYPE_MASK;
-	int lshift = (3 - level) * PTDESC_TABLE_SHIFT;
-	u64 lmask = (PAGE_SIZE << lshift) - 1;
+	int lshift = (3 - level) * (page_shift - PTDESC_ORDER);
+	u64 lmask = (page_size << lshift) - 1;
 
-	start	&= PAGE_MASK;
-	pa	&= PAGE_MASK;
+	start	&= page_mask;
+	pa	&= page_mask;
 
 	/* Advance tbl to the entry that covers start */
-	tbl += (start >> (lshift + PAGE_SHIFT)) % PTRS_PER_PTE;
+	tbl += (start >> (lshift + page_shift)) % ptrs_per_pte;
 
 	/*
 	 * Set the right block/page bits for this level unless we are
@@ -75,7 +120,7 @@ void __init map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
 		protval |= (level == 2) ? PMD_TYPE_SECT : PTE_TYPE_PAGE;
 
 	while (start < end) {
-		u64 next = min((start | lmask) + 1, PAGE_ALIGN(end));
+		u64 next = min((start | lmask) + 1, ALIGN(end, page_size));
 
 		if (level < 2 || (level == 2 && (start | next | pa) & lmask)) {
 			/*
@@ -83,19 +128,19 @@ void __init map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
 			 * table mapping if necessary and recurse.
 			 */
 			if (pte_none(*tbl)) {
-				u64 size = PTRS_PER_PTE * sizeof(pte_t);
+				u64 size = ptrs_per_pte * sizeof(pte_t);
 
 				if (*pte + size > limit)
 					report_cpu_stuck(
 						CPU_STUCK_REASON_NO_PGTABLE_MEM);
 
-				*tbl = __pte(__phys_to_pte_val(*pte) |
+				*tbl = __pte(pte_prep_oa(*pte, page_shift, oa52bit) |
 					     PMD_TYPE_TABLE | PMD_TABLE_UXN);
 				*pte += size;
 			}
-			map_range(pte, limit, start, next, pa, prot, level + 1,
-				  (pte_t *)(__pte_to_phys(*tbl) + va_offset),
-				  may_use_cont, va_offset);
+			__map_range(pte, limit, start, next, pa, prot, level + 1,
+				    (pte_t *)(pte_get_oa(*tbl, page_shift, oa52bit) + va_offset),
+				    may_use_cont, va_offset, page_shift, oa52bit);
 		} else {
 			/*
 			 * Start a contiguous range if start and pa are
@@ -112,7 +157,8 @@ void __init map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
 				protval &= ~PTE_CONT;
 
 			/* Put down a block or page mapping */
-			*tbl = __pte(__phys_to_pte_val(pa) | protval);
+			*tbl = __pte(pte_prep_oa(pa, page_shift, oa52bit) |
+				     protval);
 		}
 		pa += next - start;
 		start = next;
@@ -120,10 +166,93 @@ void __init map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
 	}
 }
 
+/**
+ * map_range - Map a contiguous range of physical pages into virtual memory
+ *
+ * As per __map_range(), except it uses the page_shift and oa52bit of the
+ * currently tcr-installed granule size instead of passing explicitly.
+ */
+void __init map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
+		      phys_addr_t pa, pgprot_t prot, int level, pte_t *tbl,
+		      bool may_use_cont, u64 va_offset)
+{
+	int page_shift = early_tcr_tg0_to_page_shift();
+	bool oa52bit = false;
+
+#ifdef CONFIG_ARM64_PA_BITS_52
+	/*
+	 * We can safely assume 52bit for 64K pages because if it turns out to
+	 * be 48bit, its still safe to treat [51:48] as address bits because
+	 * they are 0.
+	 */
+	if (early_page_size(page_shift) == SZ_64K)
+		oa52bit = true;
+	/*
+	 * For 4K and 16K, on the other hand, those bits are used for something
+	 * else when LPA2 is not explicitly enabled. Deliberately not using
+	 * read_tcr() since it is marked pure, and at this point, the tcr is not
+	 * yet stable.
+	 */
+	else if (read_sysreg(tcr_el1) & TCR_DS)
+		oa52bit = true;
+#endif
+
+	__map_range(pte, limit, start, end, pa, prot, level, tbl, may_use_cont,
+		    va_offset, page_shift, oa52bit);
+}
+
+/* Whether @page_shift is allowed by the compilation config and the HW. */
+bool __init early_page_shift_supported(int page_shift)
+{
+	u64 mmfr0 = read_sysreg_s(SYS_ID_AA64MMFR0_EL1);
+	u32 tgran64 = SYS_FIELD_GET(ID_AA64MMFR0_EL1, TGRAN64, mmfr0);
+	u32 tgran16 = SYS_FIELD_GET(ID_AA64MMFR0_EL1, TGRAN16, mmfr0);
+	u32 tgran4 = SYS_FIELD_GET(ID_AA64MMFR0_EL1, TGRAN4, mmfr0);
+
+	if (page_shift < PAGE_SHIFT_MIN || page_shift > PAGE_SHIFT_MAX)
+		return false;
+
+	if (page_shift == ARM64_PAGE_SHIFT_64K)
+		return tgran64 >= ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MIN &&
+		       tgran64 <= ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MAX;
+
+	if (page_shift == ARM64_PAGE_SHIFT_16K)
+		return tgran16 >= ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MIN &&
+		       tgran16 <= ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MAX;
+
+	return page_shift == ARM64_PAGE_SHIFT_4K &&
+	       tgran4 >= ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MIN &&
+	       tgran4 <= ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MAX;
+}
+
+asmlinkage u64 __init probe_init_idmap_page_shift(void)
+{
+	u64 page_shift;
+
+	/*
+	 * Select the largest page size supported by the HW, which is also
+	 * allowed by the compilation config.
+	 */
+	if (early_page_shift_supported(ARM64_PAGE_SHIFT_64K))
+		page_shift = ARM64_PAGE_SHIFT_64K;
+	else if (early_page_shift_supported(ARM64_PAGE_SHIFT_16K))
+		page_shift = ARM64_PAGE_SHIFT_16K;
+	else if (early_page_shift_supported(ARM64_PAGE_SHIFT_4K))
+		page_shift = ARM64_PAGE_SHIFT_4K;
+	else
+		report_cpu_stuck(CPU_STUCK_REASON_NO_GRAN);
+
+	/* Stash this for __cpu_setup to configure primary cpu's tcr_el1. */
+	set_tcr_boot_fields(early_page_shift_to_tcr_tgx(page_shift));
+
+	return page_shift;
+}
+
 asmlinkage phys_addr_t __init create_init_idmap(pgd_t *pg_dir, pgd_t *pg_end,
-						 ptval_t clrmask)
+						 ptval_t clrmask, int page_shift)
 {
-	phys_addr_t ptep = (phys_addr_t)pg_dir + PAGE_SIZE; /* MMU is off */
+	const u64 page_size = early_page_size(page_shift);
+	phys_addr_t ptep = (phys_addr_t)pg_dir + page_size; /* MMU is off */
 	pgprot_t text_prot = PAGE_KERNEL_ROX;
 	pgprot_t data_prot = PAGE_KERNEL;
 
@@ -131,12 +260,14 @@ asmlinkage phys_addr_t __init create_init_idmap(pgd_t *pg_dir, pgd_t *pg_end,
 	pgprot_val(data_prot) &= ~clrmask;
 
 	/* MMU is off; pointer casts to phys_addr_t are safe */
-	map_range(&ptep, (u64)pg_end, (u64)_stext, (u64)__initdata_begin,
-		  (phys_addr_t)_stext, text_prot,
-		  IDMAP_ROOT_LEVEL(PAGE_SHIFT), (pte_t *)pg_dir, false, 0);
-	map_range(&ptep, (u64)pg_end, (u64)__initdata_begin, (u64)_end,
-		  (phys_addr_t)__initdata_begin, data_prot,
-		  IDMAP_ROOT_LEVEL(PAGE_SHIFT), (pte_t *)pg_dir, false, 0);
+	__map_range(&ptep, (u64)pg_end, (u64)_stext, (u64)__initdata_begin,
+		    (phys_addr_t)_stext, text_prot,
+		    IDMAP_ROOT_LEVEL(page_shift), (pte_t *)pg_dir, false, 0,
+		    page_shift, false);
+	__map_range(&ptep, (u64)pg_end, (u64)__initdata_begin, (u64)_end,
+		    (phys_addr_t)__initdata_begin, data_prot,
+		    IDMAP_ROOT_LEVEL(page_shift), (pte_t *)pg_dir, false, 0,
+		    page_shift, false);
 
 	return ptep;
 }
diff --git a/arch/arm64/kernel/pi/pi.h b/arch/arm64/kernel/pi/pi.h
index ce07cdc3445d..a92ba38a989d 100644
--- a/arch/arm64/kernel/pi/pi.h
+++ b/arch/arm64/kernel/pi/pi.h
@@ -24,6 +24,62 @@ extern bool dynamic_scs_is_enabled;
 extern pgd_t init_idmap_pg_dir[], init_idmap_pg_end[];
 extern pgd_t init_pg_dir[], init_pg_end[];
 
+static inline u64 early_page_size(int page_shift)
+{
+	return 1UL << page_shift;
+}
+
+static inline u64 early_page_mask(int page_shift)
+{
+	return ~(early_page_size(page_shift) - 1);
+}
+
+static inline u64 early_cont_pte_size(int page_shift)
+{
+	switch (page_shift) {
+	case 16: /* 64K */
+	case 14: /* 16K */
+		return SZ_2M;
+	default: /* 12 4K */
+		return SZ_64K;
+	}
+}
+
+static inline u64 early_ptrs_per_pte(int page_shift)
+{
+	return 1UL << (page_shift - 3);
+}
+
+static inline int early_tcr_tg0_to_page_shift(void)
+{
+	/*
+	 * Deliberately not using read_tcr() since it is marked pure, and at
+	 * this point, the tcr is not yet stable.
+	 */
+	u64 tg0 = read_sysreg(tcr_el1) & TCR_TG0_MASK;
+
+	switch (tg0) {
+	case TCR_TG0_64K:
+		return 16;
+	case TCR_TG0_16K:
+		return 14;
+	default: /* TCR_TG0_4K */
+		return 12;
+	}
+}
+
+static inline u64 early_page_shift_to_tcr_tgx(int page_shift)
+{
+	switch (early_page_size(page_shift)) {
+	case SZ_64K:
+		return TCR_TG0_64K | TCR_TG1_64K;
+	case SZ_16K:
+		return TCR_TG0_16K | TCR_TG1_16K;
+	default:
+		return TCR_TG0_4K | TCR_TG1_4K;
+	}
+}
+
 void init_feature_override(u64 boot_status, const void *fdt, int chosen);
 u64 kaslr_early_init(void *fdt, int chosen);
 void relocate_kernel(u64 offset);
@@ -35,5 +91,8 @@ void map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
 
 asmlinkage void early_map_kernel(u64 boot_status, phys_addr_t fdt);
 
+void set_tcr_boot_fields(u64 val);
+bool early_page_shift_supported(int page_shift);
+asmlinkage u64 probe_init_idmap_page_shift(void);
 asmlinkage phys_addr_t create_init_idmap(pgd_t *pgd, pgd_t *pg_end,
-					 ptval_t clrmask);
+					 ptval_t clrmask, int page_shift);
diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S
index 62e790a2a779..afc9dc20084e 100644
--- a/arch/arm64/mm/proc.S
+++ b/arch/arm64/mm/proc.S
@@ -22,17 +22,6 @@
 #include <asm/smp.h>
 #include <asm/sysreg.h>
 
-#ifdef CONFIG_ARM64_64K_PAGES
-#define TCR_TG_FLAGS	((TCR_EL1_TG0_64K << TCR_EL1_TG0_SHIFT) |\
-			 (TCR_EL1_TG1_64K << TCR_EL1_TG1_SHIFT))
-#elif defined(CONFIG_ARM64_16K_PAGES)
-#define TCR_TG_FLAGS	((TCR_EL1_TG0_16K << TCR_EL1_TG0_SHIFT) |\
-			 (TCR_EL1_TG1_16K << TCR_EL1_TG1_SHIFT))
-#else /* CONFIG_ARM64_4K_PAGES */
-#define TCR_TG_FLAGS	((TCR_EL1_TG0_4K << TCR_EL1_TG0_SHIFT) |\
-			 (TCR_EL1_TG1_4K << TCR_EL1_TG1_SHIFT))
-#endif
-
 #ifdef CONFIG_RANDOMIZE_BASE
 #define TCR_KASLR_FLAGS	TCR_EL1_NFD1
 #else
@@ -535,19 +524,24 @@ SYM_FUNC_START(__cpu_setup)
 	tcr2	.req	x15
 	mov_q	mair, MAIR_EL1_SET
 	mov_q	tcr, TCR_T0SZ(IDMAP_VA_BITS) | TCR_T1SZ(VA_BITS_MIN) | TCR_CACHE_FLAGS | \
-		     TCR_SHARED | TCR_TG_FLAGS | TCR_KASLR_FLAGS | TCR_EL1_AS | \
+		     TCR_SHARED | TCR_KASLR_FLAGS | TCR_EL1_AS | \
 		     TCR_EL1_TBI0 | TCR_EL1_A1 | TCR_KASAN_SW_FLAGS | TCR_MTE_FLAGS
 	mov	tcr2, xzr
 
+	/*
+	 * Insert the boot-time determined fields (TG0, TG1 and DS), which are
+	 * cached in the tcr_boot_fields variable.
+	 */
+	adr_l	x2, __pi_tcr_boot_fields
+	ldr	x3, [x2]
+	orr	tcr, tcr, x3
+
 	tcr_clear_errata_bits tcr, x9, x5
 
 #ifdef CONFIG_ARM64_VA_BITS_52
 	mov		x9, #64 - VA_BITS
 alternative_if ARM64_HAS_VA52
 	tcr_set_t1sz	tcr, x9
-#ifdef CONFIG_ARM64_LPA2
-	orr		tcr, tcr, #TCR_EL1_DS
-#endif
 alternative_else_nop_endif
 #endif
 


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 20/43] arm64: pi: Clear the FDT ptes on every remap in map_fdt()
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (15 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 21/43] arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES Eric Curtin
                   ` (21 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Eric Curtin, Catalin Marinas, Will Deacon, Mark Rutland

From: Eric Curtin <eric.curtin@docker.com>

map_fdt() is called a second time once the final page size is known, and
the static ptes[] array must be zeroed before the FDT is mapped again
because the new page size gives it a different layout. The first_time
flag is initialised to true and was only cleared inside the
"if (!first_time)" block, so it was never cleared and the memset() never
ran.

Clear first_time unconditionally after the check, so that the first call
leaves the array alone and every later call zeroes it before building
the new mapping.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit cacdf8d80bbab6eaafc3bcbc5689636ac521cb0a)
---
 arch/arm64/kernel/pi/map_kernel.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c
index 357ccb562746..fa3549701927 100644
--- a/arch/arm64/kernel/pi/map_kernel.c
+++ b/arch/arm64/kernel/pi/map_kernel.c
@@ -219,10 +219,9 @@ static void *__init map_fdt(phys_addr_t fdt, int page_shift)
 	phys_addr_t efdt = fdt + MAX_FDT_SIZE;
 	phys_addr_t ptep = (phys_addr_t)ptes; /* We're idmapped when called */
 
-	if (!first_time) {
+	if (!first_time)
 		memset(ptes, 0, sizeof(ptes));
-		first_time = false;
-	}
+	first_time = false;
 
 	/*
 	 * Map up to MAX_FDT_SIZE bytes, but avoid overlap with


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 21/43] arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (16 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 22/43] arm64: Align image sections to PAGE_SIZE_MAX Eric Curtin
                   ` (20 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
	Steffen Eiden, Suzuki K Poulose, Zenghui Yu, Catalin Marinas,
	Will Deacon, Mark Rutland, Muchun Song, Mike Rapoport,
	David Hildenbrand, Andrey Ryabinin, Alexander Potapenko,
	Andrey Konovalov, Dmitry Vyukov, Vincenzo Frascino,
	Thomas Gleixner, Radu Rendec, kvmarm, linux-mm, kasan-dev,
	linux-kernel

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

With boot-time page size selection the page size is no longer known at
compile time, so macros and code that are conditional on the
CONFIG_ARM64_4K_PAGES, CONFIG_ARM64_16K_PAGES and CONFIG_ARM64_64K_PAGES
options cannot keep making that decision in the preprocessor.

Convert the simple cases to compare PAGE_SIZE with SZ_4K/SZ_16K/SZ_64K,
mostly with the ternary operator. This still folds to a compile-time
constant when the page size is fixed at build time, but also works once
PAGE_SIZE is a runtime value. The affected definitions are
VTCR_EL2_TGRAN and VTCR_EL2_TGRAN_SL0_BASE, KVM_PGTABLE_MIN_BLOCK_LEVEL,
IOREMAP_MAX_ORDER, TASK_SIZE_32, SECTION_SIZE_BITS, the
ID_AA64MMFR0_EL1_TGRAN_* helpers, ARM64_MEMSTART_SHIFT,
ARM64_MEMSTART_ALIGN and SHADOW_ALIGN. The remaining
IS_ENABLED(CONFIG_ARM64_*K_PAGES) tests in fixmap.c, kasan_init.c,
mmu.c and irq-gic-v3-its.c become PAGE_SIZE comparisons as well.

The ARM64_HAS_VA52 capability now uses a new has_va52() match function
which selects the ID register field to check (VARange for 64K, TGRAN16
or TGRAN4 for LPA2) from PAGE_SIZE, so the unused cases are removed by
the compiler for a compile-time page size. has_lpa2() is now always
built and returns false for 64K pages. As a result the single
"52-bit Virtual Addressing" description replaces the separate LVA and
LPA2 descriptions.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 1be465f97e3039e3ecd46594b3441b70b0ae9bfb)
---
 arch/arm64/include/asm/kvm_arm.h     | 23 ++++--------
 arch/arm64/include/asm/kvm_pgtable.h |  6 +---
 arch/arm64/include/asm/memory.h      |  7 ++--
 arch/arm64/include/asm/processor.h   | 10 +++---
 arch/arm64/include/asm/sparsemem.h   | 11 ++----
 arch/arm64/include/asm/sysreg.h      | 54 ++++++++++++++++++----------
 arch/arm64/kernel/cpufeature.c       | 43 +++++++++++++---------
 arch/arm64/mm/fixmap.c               |  2 +-
 arch/arm64/mm/init.c                 | 20 +++++------
 arch/arm64/mm/kasan_init.c           |  8 ++---
 arch/arm64/mm/mmu.c                  |  2 +-
 drivers/irqchip/irq-gic-v3-its.c     |  2 +-
 12 files changed, 95 insertions(+), 93 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h
index 3e1c9e31028e..497d7202f978 100644
--- a/arch/arm64/include/asm/kvm_arm.h
+++ b/arch/arm64/include/asm/kvm_arm.h
@@ -85,22 +85,13 @@
  * Entry_Level = 4 - Number_of_levels.
  *
  */
-#ifdef CONFIG_ARM64_64K_PAGES
+#define VTCR_EL2_TGRAN							\
+	(PAGE_SIZE == SZ_64K ?						\
+		VTCR_EL2_TG0_64K :					\
+		(PAGE_SIZE == SZ_16K ? VTCR_EL2_TG0_16K : VTCR_EL2_TG0_4K))
 
-#define VTCR_EL2_TGRAN			64K
-#define VTCR_EL2_TGRAN_SL0_BASE		3UL
-
-#elif defined(CONFIG_ARM64_16K_PAGES)
-
-#define VTCR_EL2_TGRAN			16K
-#define VTCR_EL2_TGRAN_SL0_BASE		3UL
-
-#else	/* 4K */
-
-#define VTCR_EL2_TGRAN			4K
-#define VTCR_EL2_TGRAN_SL0_BASE		2UL
-
-#endif
+#define VTCR_EL2_TGRAN_SL0_BASE						\
+	(PAGE_SIZE == SZ_64K ? 3UL : (PAGE_SIZE == SZ_16K ? 3UL : 2UL))
 
 #define VTCR_EL2_LVLS_TO_SL0(levels)	\
 	FIELD_PREP(VTCR_EL2_SL0, (VTCR_EL2_TGRAN_SL0_BASE - (4 - (levels))))
@@ -112,7 +103,7 @@
 #define VTCR_EL2_FLAGS	(SYS_FIELD_PREP_ENUM(VTCR_EL2, SH0, INNER)	    | \
 			 SYS_FIELD_PREP_ENUM(VTCR_EL2, ORGN0, WBWA)	    | \
 			 SYS_FIELD_PREP_ENUM(VTCR_EL2, IRGN0, WBWA)	    | \
-			 SYS_FIELD_PREP_ENUM(VTCR_EL2, TG0, VTCR_EL2_TGRAN) | \
+			 FIELD_PREP(VTCR_EL2_TG0_MASK, VTCR_EL2_TGRAN)	    | \
 			 VTCR_EL2_RES1)
 
 #define VTCR_EL2_IPA(vtcr)		(64 - FIELD_GET(VTCR_EL2_T0SZ, (vtcr)))
diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h
index 6d6ed8596847..24ff90459a8c 100644
--- a/arch/arm64/include/asm/kvm_pgtable.h
+++ b/arch/arm64/include/asm/kvm_pgtable.h
@@ -28,11 +28,7 @@ struct kvm_s2_mmu;
  *  - 16K (level 2):	32MB
  *  - 64K (level 2):	512MB
  */
-#ifdef CONFIG_ARM64_4K_PAGES
-#define KVM_PGTABLE_MIN_BLOCK_LEVEL	1
-#else
-#define KVM_PGTABLE_MIN_BLOCK_LEVEL	2
-#endif
+#define KVM_PGTABLE_MIN_BLOCK_LEVEL	(PAGE_SIZE == SZ_4K ? 1 : 2)
 
 #define kvm_lpa2_is_enabled()		system_supports_lpa2()
 
diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index a2b7a33966ff..a6df911206ff 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -194,11 +194,8 @@
 #define MT_S2_FWB_DEVICE_nGnRE	1
 #define MT_S2_FWB_AS_S1		7
 
-#ifdef CONFIG_ARM64_4K_PAGES
-#define IOREMAP_MAX_ORDER	(PUD_SHIFT)
-#else
-#define IOREMAP_MAX_ORDER	(PMD_SHIFT)
-#endif
+#define IOREMAP_MAX_ORDER						\
+	(PAGE_SIZE == SZ_4K ? PUD_SHIFT : PMD_SHIFT)
 
 /*
  *  Open-coded (swapper_pg_dir - reserved_pg_dir) as this cannot be calculated
diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h
index 481f1999f7c1..9ca6fd0f409b 100644
--- a/arch/arm64/include/asm/processor.h
+++ b/arch/arm64/include/asm/processor.h
@@ -57,15 +57,15 @@
 #define TASK_SIZE_MAX		(UL(1) << VA_BITS)
 
 #ifdef CONFIG_COMPAT
-#if defined(CONFIG_ARM64_64K_PAGES) && defined(CONFIG_KUSER_HELPERS)
+#if defined(CONFIG_KUSER_HELPERS)
 /*
- * With CONFIG_ARM64_64K_PAGES enabled, the last page is occupied
- * by the compat vectors page.
+ * With 64K pages in use, the last page is occupied by the compat vectors page.
  */
-#define TASK_SIZE_32		UL(0x100000000)
+#define TASK_SIZE_32							\
+	(PAGE_SIZE == SZ_64K ? UL(0x100000000) : (UL(0x100000000) - PAGE_SIZE))
 #else
 #define TASK_SIZE_32		(UL(0x100000000) - PAGE_SIZE)
-#endif /* CONFIG_ARM64_64K_PAGES */
+#endif /* CONFIG_KUSER_HELPERS */
 #define TASK_SIZE		(test_thread_flag(TIF_32BIT) ? \
 				TASK_SIZE_32 : TASK_SIZE_64)
 #define TASK_SIZE_OF(tsk)	(test_tsk_thread_flag(tsk, TIF_32BIT) ? \
diff --git a/arch/arm64/include/asm/sparsemem.h b/arch/arm64/include/asm/sparsemem.h
index 84783efdc9d1..4148f2440831 100644
--- a/arch/arm64/include/asm/sparsemem.h
+++ b/arch/arm64/include/asm/sparsemem.h
@@ -14,19 +14,12 @@
  * Section size must be at least 512MB for 64K base
  * page size config. Otherwise it will be less than
  * MAX_PAGE_ORDER and the build process will fail.
- */
-#ifdef CONFIG_ARM64_64K_PAGES
-#define SECTION_SIZE_BITS 29
-
-#else
-
-/*
+ *
  * Section size must be at least 128MB for 4K base
  * page size config. Otherwise PMD based huge page
  * entries could not be created for vmemmap mappings.
  * 16K follows 4K for simplicity.
  */
-#define SECTION_SIZE_BITS 27
-#endif /* CONFIG_ARM64_64K_PAGES */
+#define SECTION_SIZE_BITS	(PAGE_SIZE == SZ_64K ? 29 : 27)
 
 #endif
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index 7cba1216efd2..c301e943ecb1 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -10,11 +10,13 @@
 #define __ASM_SYSREG_H
 
 #include <linux/bits.h>
+#include <linux/sizes.h>
 #include <linux/stringify.h>
 #include <linux/kasan-tags.h>
 #include <linux/kconfig.h>
 
 #include <asm/gpr-num.h>
+#include <asm/page-def.h>
 
 #if ARM64_S390_COMMON == 1
 
@@ -887,24 +889,40 @@
 #define ID_AA64MMFR0_EL1_PARANGE_MAX	ID_AA64MMFR0_EL1_PARANGE_48
 #endif
 
-#if defined(CONFIG_ARM64_4K_PAGES)
-#define ID_AA64MMFR0_EL1_TGRAN_SHIFT		ID_AA64MMFR0_EL1_TGRAN4_SHIFT
-#define ID_AA64MMFR0_EL1_TGRAN_LPA2		ID_AA64MMFR0_EL1_TGRAN4_52_BIT
-#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN	ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MIN
-#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX	ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MAX
-#define ID_AA64MMFR0_EL1_TGRAN_2_SHIFT		ID_AA64MMFR0_EL1_TGRAN4_2_SHIFT
-#elif defined(CONFIG_ARM64_16K_PAGES)
-#define ID_AA64MMFR0_EL1_TGRAN_SHIFT		ID_AA64MMFR0_EL1_TGRAN16_SHIFT
-#define ID_AA64MMFR0_EL1_TGRAN_LPA2		ID_AA64MMFR0_EL1_TGRAN16_52_BIT
-#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN	ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MIN
-#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX	ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MAX
-#define ID_AA64MMFR0_EL1_TGRAN_2_SHIFT		ID_AA64MMFR0_EL1_TGRAN16_2_SHIFT
-#elif defined(CONFIG_ARM64_64K_PAGES)
-#define ID_AA64MMFR0_EL1_TGRAN_SHIFT		ID_AA64MMFR0_EL1_TGRAN64_SHIFT
-#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN	ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MIN
-#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX	ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MAX
-#define ID_AA64MMFR0_EL1_TGRAN_2_SHIFT		ID_AA64MMFR0_EL1_TGRAN64_2_SHIFT
-#endif
+#define ID_AA64MMFR0_EL1_TGRAN_SHIFT					\
+	(PAGE_SIZE == SZ_4K ?						\
+		ID_AA64MMFR0_EL1_TGRAN4_SHIFT :				\
+		(PAGE_SIZE == SZ_16K ?					\
+			ID_AA64MMFR0_EL1_TGRAN16_SHIFT :		\
+			ID_AA64MMFR0_EL1_TGRAN64_SHIFT))
+
+#define ID_AA64MMFR0_EL1_TGRAN_LPA2					\
+	(PAGE_SIZE == SZ_4K ?						\
+		ID_AA64MMFR0_EL1_TGRAN4_52_BIT :			\
+		(PAGE_SIZE == SZ_16K ?					\
+			ID_AA64MMFR0_EL1_TGRAN16_52_BIT :		\
+			-1))
+
+#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN				\
+	(PAGE_SIZE == SZ_4K ?						\
+		ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MIN :			\
+		(PAGE_SIZE == SZ_16K ?					\
+			ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MIN :	\
+			ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MIN))
+
+#define ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX				\
+	(PAGE_SIZE == SZ_4K ?						\
+		ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MAX :			\
+		(PAGE_SIZE == SZ_16K ?					\
+			ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MAX :	\
+			ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MAX))
+
+#define ID_AA64MMFR0_EL1_TGRAN_2_SHIFT					\
+	(PAGE_SIZE == SZ_4K ?						\
+		ID_AA64MMFR0_EL1_TGRAN4_2_SHIFT :			\
+		(PAGE_SIZE == SZ_16K ?					\
+			ID_AA64MMFR0_EL1_TGRAN16_2_SHIFT :		\
+			ID_AA64MMFR0_EL1_TGRAN64_2_SHIFT))
 
 #define CPACR_EL1_FPEN_EL1EN	(BIT(20)) /* enable EL1 access */
 #define CPACR_EL1_FPEN_EL0EN	(BIT(21)) /* enable EL0 access, if EL1EN set */
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index 866385b490eb..0090ec390cf8 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -1780,11 +1780,13 @@ static bool has_nv1(const struct arm64_cpu_capabilities *entry, int scope)
 		  is_midr_in_range_list(nv1_ni_list)));
 }
 
-#if defined(ID_AA64MMFR0_EL1_TGRAN_LPA2)
 static bool has_lpa2_at_stage1(u64 mmfr0)
 {
 	unsigned int tgran;
 
+	if (PAGE_SIZE == SZ_64K)
+		return false;
+
 	tgran = cpuid_feature_extract_unsigned_field(mmfr0,
 					ID_AA64MMFR0_EL1_TGRAN_SHIFT);
 	return tgran == ID_AA64MMFR0_EL1_TGRAN_LPA2;
@@ -1794,6 +1796,9 @@ static bool has_lpa2_at_stage2(u64 mmfr0)
 {
 	unsigned int tgran;
 
+	if (PAGE_SIZE == SZ_64K)
+		return false;
+
 	tgran = cpuid_feature_extract_unsigned_field(mmfr0,
 					ID_AA64MMFR0_EL1_TGRAN_2_SHIFT);
 	return tgran == ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_LPA2;
@@ -1806,10 +1811,26 @@ static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope)
 	mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
 	return has_lpa2_at_stage1(mmfr0) && has_lpa2_at_stage2(mmfr0);
 }
-#else
-static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope)
+
+#ifdef CONFIG_ARM64_VA_BITS_52
+static bool has_va52(const struct arm64_cpu_capabilities *entry, int scope)
 {
-	return false;
+	const struct arm64_cpu_capabilities entry_64k = {
+		ARM64_CPUID_FIELDS(ID_AA64MMFR2_EL1, VARange, 52)
+	};
+	const struct arm64_cpu_capabilities entry_16k = {
+		ARM64_CPUID_FIELDS(ID_AA64MMFR0_EL1, TGRAN16, 52_BIT)
+	};
+	const struct arm64_cpu_capabilities entry_4k = {
+		ARM64_CPUID_FIELDS(ID_AA64MMFR0_EL1, TGRAN4, 52_BIT)
+	};
+
+	if (PAGE_SIZE == SZ_64K)
+		return has_cpuid_feature(&entry_64k, scope);
+	else if (PAGE_SIZE == SZ_16K)
+		return has_cpuid_feature(&entry_16k, scope);
+	else
+		return has_cpuid_feature(&entry_4k, scope);
 }
 #endif
 
@@ -2978,20 +2999,10 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 	},
 #ifdef CONFIG_ARM64_VA_BITS_52
 	{
+		.desc = "52-bit Virtual Addressing",
 		.capability = ARM64_HAS_VA52,
 		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
-		.matches = has_cpuid_feature,
-#ifdef CONFIG_ARM64_64K_PAGES
-		.desc = "52-bit Virtual Addressing (LVA)",
-		ARM64_CPUID_FIELDS(ID_AA64MMFR2_EL1, VARange, 52)
-#else
-		.desc = "52-bit Virtual Addressing (LPA2)",
-#ifdef CONFIG_ARM64_4K_PAGES
-		ARM64_CPUID_FIELDS(ID_AA64MMFR0_EL1, TGRAN4, 52_BIT)
-#else
-		ARM64_CPUID_FIELDS(ID_AA64MMFR0_EL1, TGRAN16, 52_BIT)
-#endif
-#endif
+		.matches = has_va52,
 	},
 #endif
 	{
diff --git a/arch/arm64/mm/fixmap.c b/arch/arm64/mm/fixmap.c
index c8a0d008cf2a..bd48a21a98f9 100644
--- a/arch/arm64/mm/fixmap.c
+++ b/arch/arm64/mm/fixmap.c
@@ -84,7 +84,7 @@ static void __init early_fixmap_init_pud(p4d_t *p4dp, unsigned long addr,
 		 * share the top level pgd entry, which should only happen on
 		 * 16k/4 levels configurations.
 		 */
-		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
+		BUG_ON(PAGE_SIZE != SZ_16K);
 	}
 
 	if (p4d_none(p4d))
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index 4e7682dfe366..70393e4d2d82 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -77,13 +77,10 @@ phys_addr_t __ro_after_init arm64_dma_phys_limit;
  * (64k granule), or a multiple that can be mapped using contiguous bits
  * in the page tables: 32 * PMD_SIZE (16k granule)
  */
-#if defined(CONFIG_ARM64_4K_PAGES)
-#define ARM64_MEMSTART_SHIFT		PUD_SHIFT
-#elif defined(CONFIG_ARM64_16K_PAGES)
-#define ARM64_MEMSTART_SHIFT		CONT_PMD_SHIFT
-#else
-#define ARM64_MEMSTART_SHIFT		PMD_SHIFT
-#endif
+#define ARM64_MEMSTART_SHIFT						\
+	(PAGE_SIZE == SZ_4K ?						\
+		PUD_SHIFT :						\
+		(PAGE_SIZE == SZ_16K ? CONT_PMD_SHIFT : PMD_SHIFT))
 
 /*
  * sparsemem vmemmap imposes an additional requirement on the alignment of
@@ -91,11 +88,10 @@ phys_addr_t __ro_after_init arm64_dma_phys_limit;
  * has a direct correspondence, and needs to appear sufficiently aligned
  * in the virtual address space.
  */
-#if ARM64_MEMSTART_SHIFT < SECTION_SIZE_BITS
-#define ARM64_MEMSTART_ALIGN	(1UL << SECTION_SIZE_BITS)
-#else
-#define ARM64_MEMSTART_ALIGN	(1UL << ARM64_MEMSTART_SHIFT)
-#endif
+#define ARM64_MEMSTART_ALIGN						\
+	(ARM64_MEMSTART_SHIFT < SECTION_SIZE_BITS ?			\
+		(1UL << SECTION_SIZE_BITS) :				\
+		(1UL << ARM64_MEMSTART_SHIFT))
 
 static void __init arch_reserve_crashkernel(void)
 {
diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c
index 45fbdce684c8..638882f90ad5 100644
--- a/arch/arm64/mm/kasan_init.c
+++ b/arch/arm64/mm/kasan_init.c
@@ -178,10 +178,10 @@ static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
 	} while (pgdp++, addr = next, addr != end);
 }
 
-#if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS > 4
+#if CONFIG_PGTABLE_LEVELS > 4
 #define SHADOW_ALIGN	P4D_SIZE
 #else
-#define SHADOW_ALIGN	PUD_SIZE
+#define SHADOW_ALIGN	(PAGE_SIZE == SZ_64K ? P4D_SIZE : PUD_SIZE)
 #endif
 
 /*
@@ -243,8 +243,8 @@ static int __init root_level_idx(u64 addr)
 	 * not implemented. This means we need to index the table as usual,
 	 * instead of masking off bits based on vabits_actual.
 	 */
-	u64 vabits = IS_ENABLED(CONFIG_ARM64_64K_PAGES) ? VA_BITS
-							: vabits_actual;
+	u64 vabits = PAGE_SIZE == SZ_64K ? VA_BITS
+					 : vabits_actual;
 	int shift = (ARM64_HW_PGTABLE_LEVELS(vabits) - 1) * PTDESC_TABLE_SHIFT;
 
 	return (addr & ~_PAGE_OFFSET(vabits)) >> (shift + PAGE_SHIFT);
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 1567f76607d9..8cb8234cc24a 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1846,7 +1846,7 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
 	/* [start, end] should be within one section */
 	WARN_ON_ONCE(end - start > PAGES_PER_SECTION * sizeof(struct page));
 
-	if (!IS_ENABLED(CONFIG_ARM64_4K_PAGES) ||
+	if (PAGE_SIZE != SZ_4K ||
 	    (end - start < PAGES_PER_SECTION * sizeof(struct page)))
 		return vmemmap_populate_basepages(start, end, node, altmap);
 	else
diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index c952c069f937..928cba93d8b2 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -2410,7 +2410,7 @@ static int its_setup_baser(struct its_node *its, struct its_baser *baser,
 	baser_phys = virt_to_phys(base);
 
 	/* Check if the physical address of the memory is above 48bits */
-	if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
+	if (PAGE_SIZE == SZ_64K && (baser_phys >> 48)) {
 
 		/* 52bit PA is supported only when PageSize=64K */
 		if (psz != SZ_64K) {


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 22/43] arm64: Align image sections to PAGE_SIZE_MAX
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (17 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 23/43] arm64: Map the trampoline rodata at its aligned offset Eric Curtin
                   ` (19 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland,
	Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, kvmarm

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

With boot-time page size selection the sections that are mapped with
page granularity, such as the hyp sections, the vDSO images, the page
table directories and the data and BSS regions, must be aligned for the
largest page size that may be selected. PAGE_SIZE is not a constant in
the linker script or assembly, so use PAGE_SIZE_MAX instead.

Switch ALIGN(), RO_DATA(), RW_DATA(), BSS_SECTION(), the reserved,
swapper and trampoline directory offsets, the hyp.lds.S percpu section
and the vDSO .balign directives from PAGE_SIZE to PAGE_SIZE_MAX. The
vDSO data mapping length in vdso.c is changed to match. The
trampoline text, which has to fit the smallest page, keeps a limit
based on PAGE_SIZE_MIN in the "HYP init code too big" and "Entry
trampoline text too big" assertions, and the alignment that followed the
trampoline text is moved to after __entry_tramp_text_end.

For a compile-time page size PAGE_SIZE_MAX equals PAGE_SIZE and the
image is unchanged. For a boot-time page size build the image is
comparable in size to one built for 64K pages.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit f2ff15f09dae013f6c02715972759154ae9a000c)
---
 arch/arm64/include/asm/memory.h   |  4 +--
 arch/arm64/kernel/vdso-wrap.S     |  4 +--
 arch/arm64/kernel/vdso.c          |  4 +--
 arch/arm64/kernel/vdso32-wrap.S   |  4 +--
 arch/arm64/kernel/vmlinux.lds.S   | 46 +++++++++++++++----------------
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S |  2 +-
 6 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index a6df911206ff..05a8efc6810d 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -201,13 +201,13 @@
  *  Open-coded (swapper_pg_dir - reserved_pg_dir) as this cannot be calculated
  *  until link time.
  */
-#define RESERVED_SWAPPER_OFFSET	(PAGE_SIZE)
+#define RESERVED_SWAPPER_OFFSET	(PAGE_SIZE_MAX)
 
 /*
  *  Open-coded (swapper_pg_dir - tramp_pg_dir) as this cannot be calculated
  *  until link time.
  */
-#define TRAMP_SWAPPER_OFFSET	(2 * PAGE_SIZE)
+#define TRAMP_SWAPPER_OFFSET	(2 * PAGE_SIZE_MAX)
 
 #ifndef __ASSEMBLER__
 
diff --git a/arch/arm64/kernel/vdso-wrap.S b/arch/arm64/kernel/vdso-wrap.S
index c4b1990bf2be..79fa77628199 100644
--- a/arch/arm64/kernel/vdso-wrap.S
+++ b/arch/arm64/kernel/vdso-wrap.S
@@ -13,10 +13,10 @@
 
 	.globl vdso_start, vdso_end
 	.section .rodata
-	.balign PAGE_SIZE
+	.balign PAGE_SIZE_MAX
 vdso_start:
 	.incbin "arch/arm64/kernel/vdso/vdso.so"
-	.balign PAGE_SIZE
+	.balign PAGE_SIZE_MAX
 vdso_end:
 
 	.previous
diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c
index 50596dab23f9..8459ac3288cf 100644
--- a/arch/arm64/kernel/vdso.c
+++ b/arch/arm64/kernel/vdso.c
@@ -108,7 +108,7 @@ static int __setup_additional_pages(enum vdso_abi abi,
 
 	vdso_text_len = vdso_info[abi].vdso_pages << PAGE_SHIFT;
 	/* Be sure to map the data page */
-	vdso_mapping_len = vdso_text_len + VDSO_NR_PAGES * PAGE_SIZE;
+	vdso_mapping_len = vdso_text_len + VDSO_NR_PAGES * PAGE_SIZE_MAX;
 
 	vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0);
 	if (IS_ERR_VALUE(vdso_base)) {
@@ -123,7 +123,7 @@ static int __setup_additional_pages(enum vdso_abi abi,
 	if (system_supports_bti_kernel())
 		gp_flags = VM_ARM64_BTI;
 
-	vdso_base += VDSO_NR_PAGES * PAGE_SIZE;
+	vdso_base += VDSO_NR_PAGES * PAGE_SIZE_MAX;
 	mm->context.vdso = (void *)vdso_base;
 	ret = _install_special_mapping(mm, vdso_base, vdso_text_len,
 				       VM_READ|VM_EXEC|gp_flags|
diff --git a/arch/arm64/kernel/vdso32-wrap.S b/arch/arm64/kernel/vdso32-wrap.S
index e72ac7bc4c04..1c6069d6c457 100644
--- a/arch/arm64/kernel/vdso32-wrap.S
+++ b/arch/arm64/kernel/vdso32-wrap.S
@@ -10,10 +10,10 @@
 
 	.globl vdso32_start, vdso32_end
 	.section .rodata
-	.balign PAGE_SIZE
+	.balign PAGE_SIZE_MAX
 vdso32_start:
 	.incbin "arch/arm64/kernel/vdso32/vdso.so"
-	.balign PAGE_SIZE
+	.balign PAGE_SIZE_MAX
 vdso32_end:
 
 	.previous
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index ca3016f284d5..eb253341711c 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -25,26 +25,26 @@
 
 #define HYPERVISOR_RODATA_SECTIONS				\
 	HYP_SECTION_NAME(.rodata) : {				\
-		. = ALIGN(PAGE_SIZE);				\
+		. = ALIGN(PAGE_SIZE_MAX);			\
 		__hyp_rodata_start = .;				\
 		*(HYP_SECTION_NAME(.data..ro_after_init))	\
 		*(HYP_SECTION_NAME(.rodata))			\
 		HYPERVISOR_EVENT_IDS				\
-		. = ALIGN(PAGE_SIZE);				\
+		. = ALIGN(PAGE_SIZE_MAX);			\
 		__hyp_rodata_end = .;				\
 	}
 
 #define HYPERVISOR_DATA_SECTION					\
 	HYP_SECTION_NAME(.data) : {				\
-		. = ALIGN(PAGE_SIZE);				\
+		. = ALIGN(PAGE_SIZE_MAX);			\
 		__hyp_data_start = .;				\
 		*(HYP_SECTION_NAME(.data))			\
-		. = ALIGN(PAGE_SIZE);				\
+		. = ALIGN(PAGE_SIZE_MAX);			\
 		__hyp_data_end = .;				\
 	}
 
 #define HYPERVISOR_PERCPU_SECTION				\
-	. = ALIGN(PAGE_SIZE);					\
+	. = ALIGN(PAGE_SIZE_MAX);				\
 	HYP_SECTION_NAME(.data..percpu) : {			\
 		*(HYP_SECTION_NAME(.data..percpu))		\
 	}
@@ -59,7 +59,7 @@
 #define BSS_FIRST_SECTIONS					\
 	__hyp_bss_start = .;					\
 	*(HYP_SECTION_NAME(.bss))				\
-	. = ALIGN(PAGE_SIZE);					\
+	. = ALIGN(PAGE_SIZE_MAX);				\
 	__hyp_bss_end = .;
 
 /*
@@ -68,7 +68,7 @@
  * between them, which can in some cases cause the linker to misalign them. To
  * work around the issue, force a page alignment for __bss_start.
  */
-#define SBSS_ALIGN			PAGE_SIZE
+#define SBSS_ALIGN			PAGE_SIZE_MAX
 #else /* CONFIG_KVM */
 #define HYPERVISOR_EXTABLE
 #define HYPERVISOR_RODATA_SECTIONS
@@ -96,14 +96,14 @@ ENTRY(_text)
 jiffies = jiffies_64;
 
 #define HYPERVISOR_TEXT					\
-	. = ALIGN(PAGE_SIZE);				\
+	. = ALIGN(PAGE_SIZE_MAX);			\
 	__hyp_idmap_text_start = .;			\
 	*(.hyp.idmap.text)				\
 	__hyp_idmap_text_end = .;			\
 	__hyp_text_start = .;				\
 	*(.hyp.text)					\
 	HYPERVISOR_EXTABLE				\
-	. = ALIGN(PAGE_SIZE);				\
+	. = ALIGN(PAGE_SIZE_MAX);			\
 	__hyp_text_end = .;
 
 #define IDMAP_TEXT					\
@@ -134,11 +134,11 @@ jiffies = jiffies_64;
 
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
 #define TRAMP_TEXT					\
-	. = ALIGN(PAGE_SIZE);				\
+	. = ALIGN(PAGE_SIZE_MAX);			\
 	__entry_tramp_text_start = .;			\
 	*(.entry.tramp.text)				\
-	. = ALIGN(PAGE_SIZE);				\
 	__entry_tramp_text_end = .;			\
+	. = ALIGN(PAGE_SIZE_MAX);			\
 	*(.entry.tramp.rodata)
 #else
 #define TRAMP_TEXT
@@ -210,7 +210,7 @@ SECTIONS
 	_etext = .;			/* End of text section */
 
 	/* everything from this point to __init_begin will be marked RO NX */
-	RO_DATA(PAGE_SIZE)
+	RO_DATA(PAGE_SIZE_MAX)
 
 	HYPERVISOR_RODATA_SECTIONS
 
@@ -237,22 +237,22 @@ SECTIONS
 		HIBERNATE_TEXT
 		KEXEC_TEXT
 		IDMAP_TEXT
-		. = ALIGN(PAGE_SIZE);
+		. = ALIGN(PAGE_SIZE_MAX);
 	}
 
 	idmap_pg_dir = .;
-	. += PAGE_SIZE;
+	. += PAGE_SIZE_MAX;
 
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
 	tramp_pg_dir = .;
-	. += PAGE_SIZE;
+	. += PAGE_SIZE_MAX;
 #endif
 
 	reserved_pg_dir = .;
-	. += PAGE_SIZE;
+	. += PAGE_SIZE_MAX;
 
 	swapper_pg_dir = .;
-	. += PAGE_SIZE;
+	. += PAGE_SIZE_MAX;
 
 	. = ALIGN(SEGMENT_ALIGN);
 	__init_begin = .;
@@ -323,7 +323,7 @@ SECTIONS
 
 	_data = .;
 	_sdata = .;
-	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN)
+	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE_MAX, THREAD_ALIGN)
 
 	HYPERVISOR_DATA_SECTION
 
@@ -357,16 +357,16 @@ SECTIONS
 	_edata = .;
 
 	/* start of zero-init region */
-	BSS_SECTION(SBSS_ALIGN, 0, PAGE_SIZE)
+	BSS_SECTION(SBSS_ALIGN, 0, PAGE_SIZE_MAX)
 	__pi___bss_start = __bss_start;
 
 	/* page table BSS starts here - preceding data/BSS is omitted from the linear map */
-	.pgtbl : ALIGN(PAGE_SIZE) {
+	.pgtbl : ALIGN(PAGE_SIZE_MAX) {
 		*(.bss..pgtbl)
 	}
 	ASSERT(ADDR(.pgtbl) == __bss_stop, ".pgtbl must follow BSS")
 
-	. = ALIGN(PAGE_SIZE);
+	. = ALIGN(PAGE_SIZE_MAX);
 	__pi_init_pg_dir = .;
 	. += INIT_DIR_SIZE_MAX;
 	__pi_init_pg_end = .;
@@ -405,7 +405,7 @@ SECTIONS
  * former is page-aligned, but the latter may not be with 16K or 64K pages, so
  * it should also not cross a page boundary.
  */
-ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE,
+ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE_MIN,
 	"HYP init code too big")
 ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
 	"ID map text too big or misaligned")
@@ -416,7 +416,7 @@ ASSERT(__hibernate_exit_text_start == swsusp_arch_suspend_exit,
        "Hibernate exit text does not start with swsusp_arch_suspend_exit")
 #endif
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
-ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) <= 3*PAGE_SIZE,
+ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) <= 3 * PAGE_SIZE_MIN,
 	"Entry trampoline text too big")
 #endif
 #ifdef CONFIG_KVM
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp.lds.S b/arch/arm64/kvm/hyp/nvhe/hyp.lds.S
index 89222425359a..69b8fdbc977e 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp.lds.S
+++ b/arch/arm64/kvm/hyp/nvhe/hyp.lds.S
@@ -26,7 +26,7 @@ SECTIONS {
 	 * .hyp..data..percpu needs to be page aligned to maintain the same
 	 * alignment for when linking into vmlinux.
 	 */
-	. = ALIGN(PAGE_SIZE);
+	. = ALIGN(PAGE_SIZE_MAX);
 	BEGIN_HYP_SECTION(.data..percpu)
 		PERCPU_INPUT(L1_CACHE_BYTES)
 	END_HYP_SECTION


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 23/43] arm64: Map the trampoline rodata at its aligned offset
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (18 preceding siblings ...)
  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 ` 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
                   ` (18 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

The trampoline rodata is now aligned to the next PAGE_SIZE_MAX boundary
after the trampoline text. map_entry_trampoline() still assumed that it
sits in the page immediately following the text, which only holds while
PAGE_SIZE equals PAGE_SIZE_MAX. With a smaller boot-time page size the
rodata would be mapped in the wrong fixmap slot.

Reserve enough fixmap slots to cover the text, the data page and the
alignment padding in the worst case (PAGE_SIZE equal to PAGE_SIZE_MIN),
replacing the FIX_ENTRY_TRAMP_TEXT1..4 slots with FIX_ENTRY_TRAMP_BEGIN
and FIX_ENTRY_TRAMP_END. Export __entry_tramp_rodata_start/end from the
linker script, and have map_entry_trampoline() compute the slot of the
rodata from the physical distance between the end of the text and the
start of the rodata. idmap_ptes and kpti_bbml3_ptes are sized and
aligned for PAGE_SIZE_MAX at the same time.

No functional change for compile-time page sizes.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 073b8743136dc4a2b2fbe13673437eb0b065134d)
---
 arch/arm64/include/asm/fixmap.h   | 16 +++++++++++-----
 arch/arm64/include/asm/sections.h |  1 +
 arch/arm64/kernel/vmlinux.lds.S   |  4 +++-
 arch/arm64/mm/mmu.c               | 26 ++++++++++++++++----------
 4 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/arch/arm64/include/asm/fixmap.h b/arch/arm64/include/asm/fixmap.h
index 170c3502d723..5ec75d6daf36 100644
--- a/arch/arm64/include/asm/fixmap.h
+++ b/arch/arm64/include/asm/fixmap.h
@@ -65,13 +65,19 @@ enum fixed_addresses {
 #endif /* CONFIG_ACPI_APEI_GHES */
 
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
+#define TRAMP_TEXT_SIZE	(PAGE_SIZE_MIN * 3)
 #ifdef CONFIG_RELOCATABLE
-	FIX_ENTRY_TRAMP_TEXT4,	/* one extra slot for the data page */
+#define TRAMP_DATA_SIZE	PAGE_SIZE_MIN
+#define TRAMP_PAD_SIZE	(PAGE_SIZE_MAX - PAGE_SIZE_MIN)
+#else
+#define TRAMP_DATA_SIZE	0
+#define TRAMP_PAD_SIZE	0
 #endif
-	FIX_ENTRY_TRAMP_TEXT3,
-	FIX_ENTRY_TRAMP_TEXT2,
-	FIX_ENTRY_TRAMP_TEXT1,
-#define TRAMP_VALIAS		(__fix_to_virt(FIX_ENTRY_TRAMP_TEXT1))
+#define TRAMP_SIZE	(TRAMP_TEXT_SIZE + TRAMP_DATA_SIZE + TRAMP_PAD_SIZE)
+	FIX_ENTRY_TRAMP_END,
+	FIX_ENTRY_TRAMP_BEGIN = FIX_ENTRY_TRAMP_END +
+				DIV_ROUND_UP(TRAMP_SIZE, PAGE_SIZE_MIN) - 1,
+#define TRAMP_VALIAS		(__fix_to_virt(FIX_ENTRY_TRAMP_BEGIN))
 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
 	__end_of_permanent_fixed_addresses,
 
diff --git a/arch/arm64/include/asm/sections.h b/arch/arm64/include/asm/sections.h
index 51b0d594239e..f61cd8d9d4b1 100644
--- a/arch/arm64/include/asm/sections.h
+++ b/arch/arm64/include/asm/sections.h
@@ -22,6 +22,7 @@ extern char __exittext_begin[], __exittext_end[];
 extern char __irqentry_text_start[], __irqentry_text_end[];
 extern char __mmuoff_data_start[], __mmuoff_data_end[];
 extern char __entry_tramp_text_start[], __entry_tramp_text_end[];
+extern char __entry_tramp_rodata_start[], __entry_tramp_rodata_end[];
 extern char __relocate_new_kernel_start[], __relocate_new_kernel_end[];
 
 static inline size_t entry_tramp_text_size(void)
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index eb253341711c..31e398bdc983 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -139,7 +139,9 @@ jiffies = jiffies_64;
 	*(.entry.tramp.text)				\
 	__entry_tramp_text_end = .;			\
 	. = ALIGN(PAGE_SIZE_MAX);			\
-	*(.entry.tramp.rodata)
+	__entry_tramp_rodata_start = .;			\
+	*(.entry.tramp.rodata)				\
+	__entry_tramp_rodata_end = .;
 #else
 #define TRAMP_TEXT
 #endif
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 8cb8234cc24a..96fc6d843d34 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1398,25 +1398,31 @@ static int __init map_entry_trampoline(void)
 		return 0;
 
 	pgprot_t prot = kernel_exec_prot();
-	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
+	phys_addr_t pa_text = __pa_symbol(__entry_tramp_text_start);
+	phys_addr_t pa_data = __pa_symbol(__entry_tramp_rodata_start);
+	int slot = FIX_ENTRY_TRAMP_BEGIN;
 
 	/* The trampoline is always mapped and can therefore be global */
 	pgprot_val(prot) &= ~PTE_NG;
 
 	/* Map only the text into the trampoline page table */
 	memset(tramp_pg_dir, 0, PGD_SIZE);
-	early_create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
+	early_create_pgd_mapping(tramp_pg_dir, pa_text, TRAMP_VALIAS,
 				 entry_tramp_text_size(), prot,
 				 pgd_pgtable_alloc_init_mm, NO_BLOCK_MAPPINGS);
 
 	/* Map both the text and data into the kernel page table */
-	for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
-		__set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
-			     pa_start + i * PAGE_SIZE, prot);
+	for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++) {
+		__set_fixmap(slot, pa_text, prot);
+		pa_text += PAGE_SIZE;
+		slot--;
+	}
 
-	if (IS_ENABLED(CONFIG_RELOCATABLE))
-		__set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
-			     pa_start + i * PAGE_SIZE, PAGE_KERNEL_RO);
+	if (IS_ENABLED(CONFIG_RELOCATABLE)) {
+		slot -= (pa_data - pa_text) / PAGE_SIZE;
+		VM_BUG_ON(slot < FIX_ENTRY_TRAMP_END);
+		__set_fixmap(slot, pa_data, PAGE_KERNEL_RO);
+	}
 
 	return 0;
 }
@@ -1442,8 +1448,8 @@ void __pi_map_range(phys_addr_t *pte, u64 limit, u64 start, u64 end,
 		    pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
 		    u64 va_offset);
 
-static u8 idmap_ptes[IDMAP_LEVELS_MAX - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init,
-	  kpti_bbml3_ptes[IDMAP_LEVELS_MAX - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init;
+static u8 idmap_ptes[IDMAP_LEVELS_MAX - 1][PAGE_SIZE_MAX] __aligned(PAGE_SIZE_MAX) __ro_after_init,
+	  kpti_bbml3_ptes[IDMAP_LEVELS_MAX - 1][PAGE_SIZE_MAX] __aligned(PAGE_SIZE_MAX) __ro_after_init;
 
 static void __init create_idmap(void)
 {


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 24/43] arm64: fixmap: Size slots and page tables for PAGE_SIZE_MIN
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (19 preceding siblings ...)
  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 ` 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
                   ` (17 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

The fixmap slots are enumerators, so their number must be a compile-time
constant, but some of them were computed from PAGE_SIZE. With a
boot-time page size the number of slots needed is largest when PAGE_SIZE
is PAGE_SIZE_MIN, so use that for FIX_FDT and NR_FIX_BTMAPS.

The statically allocated fixmap page tables are sized in the same way.
bm_pte becomes a flat array of FIXMAP_PTE_SIZE_MAX bytes worth of
entries, where FIXMAP_PTE_SIZE_MAX is the maximum of the PTE table space
needed for each supported page size, and fixmap_pte() and
early_fixmap_init_pte() index into it. bm_pmd and bm_pud use
MAX_PTRS_PER_PMD and MAX_PTRS_PER_PUD. The check that the fixmap does
not grow into the PCI I/O region is made with PAGE_SHIFT_MAX, and a
static assertion verifies that a single PMD table covers the fixmap for
each page size.

For a compile-time page size the resulting sizes are unchanged.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit c6b26dd9b60ee6a5813d78688a4704e849d90048)
---
 arch/arm64/include/asm/fixmap.h | 12 ++++++++----
 arch/arm64/mm/fixmap.c          | 34 ++++++++++++++++++++++-----------
 2 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/include/asm/fixmap.h b/arch/arm64/include/asm/fixmap.h
index 5ec75d6daf36..fd3597e920bc 100644
--- a/arch/arm64/include/asm/fixmap.h
+++ b/arch/arm64/include/asm/fixmap.h
@@ -43,7 +43,7 @@ enum fixed_addresses {
 	 * whether it crosses any page boundary.
 	 */
 	FIX_FDT_END,
-	FIX_FDT = FIX_FDT_END + DIV_ROUND_UP(MAX_FDT_SIZE, PAGE_SIZE) + 1,
+	FIX_FDT = FIX_FDT_END + DIV_ROUND_UP(MAX_FDT_SIZE, PAGE_SIZE_MIN) + 1,
 
 	FIX_EARLYCON_MEM_BASE,
 	FIX_TEXT_POKE0,
@@ -89,7 +89,7 @@ enum fixed_addresses {
 	 * offset within a page. early_ioremap() maps the page-aligned
 	 * physical range, so the initial offset can consume an extra page.
 	 */
-#define NR_FIX_BTMAPS		((SZ_256K / PAGE_SIZE) + 1)
+#define NR_FIX_BTMAPS		((SZ_256K / PAGE_SIZE_MIN) + 1)
 #define FIX_BTMAPS_SLOTS	7
 #define TOTAL_FIX_BTMAPS	(NR_FIX_BTMAPS * FIX_BTMAPS_SLOTS)
 
@@ -111,8 +111,12 @@ enum fixed_addresses {
 
 #define FIXADDR_SIZE		(__end_of_permanent_fixed_addresses << PAGE_SHIFT)
 #define FIXADDR_START		(FIXADDR_TOP - FIXADDR_SIZE)
-#define FIXADDR_TOT_SIZE	(__end_of_fixed_addresses << PAGE_SHIFT)
-#define FIXADDR_TOT_START	(FIXADDR_TOP - FIXADDR_TOT_SIZE)
+#define __FIXADDR_TOT_SIZE(page_shift)					\
+	(__end_of_fixed_addresses << (page_shift))
+#define __FIXADDR_TOT_START(page_shift)					\
+	(FIXADDR_TOP - __FIXADDR_TOT_SIZE(page_shift))
+#define FIXADDR_TOT_SIZE	__FIXADDR_TOT_SIZE(PAGE_SHIFT)
+#define FIXADDR_TOT_START	__FIXADDR_TOT_START(PAGE_SHIFT)
 
 #define FIXMAP_PAGE_IO     __pgprot(PROT_DEVICE_nGnRE)
 
diff --git a/arch/arm64/mm/fixmap.c b/arch/arm64/mm/fixmap.c
index bd48a21a98f9..46e08631d08e 100644
--- a/arch/arm64/mm/fixmap.c
+++ b/arch/arm64/mm/fixmap.c
@@ -17,27 +17,39 @@
 #include <asm/tlbflush.h>
 
 /* ensure that the fixmap region does not grow down into the PCI I/O region */
-static_assert(FIXADDR_TOT_START > PCI_IO_END);
+static_assert(__FIXADDR_TOT_START(PAGE_SHIFT_MAX) > PCI_IO_END);
 
-#define NR_BM_PTE_TABLES \
-	SPAN_NR_ENTRIES(FIXADDR_TOT_START, FIXADDR_TOP, PMD_SHIFT)
-#define NR_BM_PMD_TABLES \
-	SPAN_NR_ENTRIES(FIXADDR_TOT_START, FIXADDR_TOP, PUD_SHIFT)
+#define FIXMAP_LEVEL(page_shift, lvl, vstart, vend)			\
+	SPAN_NR_ENTRIES(vstart, vend, PGTABLE_LEVEL_SHIFT(page_shift, lvl))
 
-static_assert(NR_BM_PMD_TABLES == 1);
+#define FIXMAP_PAGES(page_shift, level)					\
+	FIXMAP_LEVEL(page_shift, level,					\
+		     __FIXADDR_TOT_START(page_shift), FIXADDR_TOP)
+
+#define FIXMAP_SIZE(page_shift, level)					\
+	(FIXMAP_PAGES(page_shift, level) * (UL(1) << (page_shift)))
+
+#define FIXMAP_PTE_SIZE_MAX						\
+	MAX_IF_HAVE_PGSZ(FIXMAP_SIZE(ARM64_PAGE_SHIFT_4K, 2),		\
+			 FIXMAP_SIZE(ARM64_PAGE_SHIFT_16K, 2),		\
+			 FIXMAP_SIZE(ARM64_PAGE_SHIFT_64K, 2))
+
+static_assert(FIXMAP_PAGES(ARM64_PAGE_SHIFT_4K, 1) == 1);
+static_assert(FIXMAP_PAGES(ARM64_PAGE_SHIFT_16K, 1) == 1);
+static_assert(FIXMAP_PAGES(ARM64_PAGE_SHIFT_64K, 1) == 1);
 
 #define __BM_TABLE_IDX(addr, shift) \
 	(((addr) >> (shift)) - (FIXADDR_TOT_START >> (shift)))
 
 #define BM_PTE_TABLE_IDX(addr)	__BM_TABLE_IDX(addr, PMD_SHIFT)
 
-static pte_t bm_pte[NR_BM_PTE_TABLES][PTRS_PER_PTE] __bss_pgtbl;
-static pmd_t bm_pmd[PTRS_PER_PMD] __bss_pgtbl __maybe_unused;
-static pud_t bm_pud[PTRS_PER_PUD] __bss_pgtbl __maybe_unused;
+static pte_t bm_pte[FIXMAP_PTE_SIZE_MAX / sizeof(pte_t)] __bss_pgtbl;
+static pmd_t bm_pmd[MAX_PTRS_PER_PMD] __bss_pgtbl __maybe_unused;
+static pud_t bm_pud[MAX_PTRS_PER_PUD] __bss_pgtbl __maybe_unused;
 
 static inline pte_t *fixmap_pte(unsigned long addr)
 {
-	return &bm_pte[BM_PTE_TABLE_IDX(addr)][pte_index(addr)];
+	return &bm_pte[BM_PTE_TABLE_IDX(addr) * PTRS_PER_PTE + pte_index(addr)];
 }
 
 static void __init early_fixmap_init_pte(pmd_t *pmdp, unsigned long addr)
@@ -46,7 +58,7 @@ static void __init early_fixmap_init_pte(pmd_t *pmdp, unsigned long addr)
 	pte_t *ptep;
 
 	if (pmd_none(pmd)) {
-		ptep = bm_pte[BM_PTE_TABLE_IDX(addr)];
+		ptep = &bm_pte[BM_PTE_TABLE_IDX(addr) * PTRS_PER_PTE];
 		__pmd_populate(pmdp, __pa_symbol(ptep),
 			       PMD_TYPE_TABLE | PMD_TABLE_AF);
 	}


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 25/43] arm64: Size and align the zero page and FDT ptes for PAGE_SIZE_MAX
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (20 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:51 ` [RFC PATCH v2 26/43] arm64: Convert KVM and hugetlb size switches to if/else Eric Curtin
                   ` (16 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland,
	David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Michal Hocko, Andrew Morton, linux-mm

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

The zero page and the page table buffer used to map the FDT during early
boot have to be big enough and aligned for whichever page size is
selected at boot, so declare them for PAGE_SIZE_MAX instead of
PAGE_SIZE. This affects empty_zero_page and its declaration in
linux/pgtable.h, and the ptes array in map_fdt().

For a compile-time page size PAGE_SIZE_MAX equals PAGE_SIZE and there is
no functional change. For a boot-time page size, unused memory could be
freed once the page size is known, but that is not done here.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 5394b3c8ab37b41a0e4943c2b0f8220fc63a07a6)
---
 arch/arm64/kernel/pi/map_kernel.c | 2 +-
 include/linux/pgtable.h           | 2 +-
 mm/mm_init.c                      | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c
index fa3549701927..16f12fa8c890 100644
--- a/arch/arm64/kernel/pi/map_kernel.c
+++ b/arch/arm64/kernel/pi/map_kernel.c
@@ -213,7 +213,7 @@ static void __init remap_idmap(bool use_lpa2, int page_shift)
 
 static void *__init map_fdt(phys_addr_t fdt, int page_shift)
 {
-	static u8 ptes[INIT_IDMAP_FDT_SIZE_MAX] __initdata __aligned(PAGE_SIZE);
+	static u8 ptes[INIT_IDMAP_FDT_SIZE_MAX] __initdata __aligned(PAGE_SIZE_MAX);
 	static bool first_time __initdata = true;
 	u64 limit = (u64)&ptes[INIT_IDMAP_FDT_SIZE_MAX];
 	phys_addr_t efdt = fdt + MAX_FDT_SIZE;
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 66d369870a74..e3544dd7871d 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -2075,7 +2075,7 @@ static inline unsigned long zero_pfn(unsigned long addr)
 	return zero_page_pfn;
 }
 
-extern const uint8_t empty_zero_page[PAGE_SIZE];
+extern const uint8_t empty_zero_page[PAGE_SIZE_MAX];
 extern struct page *__zero_page;
 
 static inline struct page *_zero_page(unsigned long addr)
diff --git a/mm/mm_init.c b/mm/mm_init.c
index d99af80ec752..0abbe36f5667 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -62,7 +62,7 @@ unsigned long zero_page_pfn __ro_after_init;
 EXPORT_SYMBOL(zero_page_pfn);
 
 #ifndef __HAVE_COLOR_ZERO_PAGE
-const uint8_t empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
+const uint8_t empty_zero_page[PAGE_SIZE_MAX] __aligned(PAGE_SIZE_MAX);
 EXPORT_SYMBOL(empty_zero_page);
 
 struct page *__zero_page __ro_after_init;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 26/43] arm64: Convert KVM and hugetlb size switches to if/else
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (21 preceding siblings ...)
  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 ` 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
                   ` (15 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
	Steffen Eiden, Suzuki K Poulose, Zenghui Yu, Catalin Marinas,
	Will Deacon, Mark Rutland, kvmarm

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

With boot-time page size selection PMD_SHIFT, PUD_SHIFT, CONT_PTE_SHIFT
and the corresponding _SIZE macros are no longer compile-time constants,
so they cannot be used as case labels in a switch statement.

Convert the switches that use them to if/else chains: the vma_shift
handling in kvm_s2_resolve_vma_size(), and __hugetlb_valid_size(),
num_contig_ptes(), hugetlb_mask_last_page() and arch_make_huge_pte() in
hugetlbpage.c. The decisions made are intended to stay the same as with
the switch statements.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 1cd569027d7d41ba43eedde8d483618ac38e2426)
---
 arch/arm64/kvm/mmu.c        | 29 +++++++++------------
 arch/arm64/mm/hugetlbpage.c | 50 ++++++++++++-------------------------
 2 files changed, 28 insertions(+), 51 deletions(-)

diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 86aa0b6fa8b5..c3a5ee486ae8 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1929,29 +1929,24 @@ static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd,
 		vma_shift = get_vma_page_shift(vma);
 	}
 
-	switch (vma_shift) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SHIFT:
-		if (fault_supports_stage2_huge_mapping(s2fd, PUD_SIZE))
-			break;
-		fallthrough;
+	if (vma_shift == PUD_SHIFT) {
+		if (!fault_supports_stage2_huge_mapping(s2fd, PUD_SIZE))
+			vma_shift = PMD_SHIFT;
+	}
 #endif
-	case CONT_PMD_SHIFT:
+	if (vma_shift == CONT_PMD_SHIFT) {
 		vma_shift = PMD_SHIFT;
-		fallthrough;
-	case PMD_SHIFT:
-		if (fault_supports_stage2_huge_mapping(s2fd, PMD_SIZE))
-			break;
-		fallthrough;
-	case CONT_PTE_SHIFT:
+	if ((vma_shift == PMD_SHIFT &&
+	     !fault_supports_stage2_huge_mapping(s2fd, PMD_SIZE)) ||
+	    vma_shift == CONT_PTE_SHIFT) {
 		vma_shift = PAGE_SHIFT;
 		s2vi->max_map_size = PAGE_SIZE;
-		fallthrough;
-	case PAGE_SHIFT:
-		break;
-	default:
-		WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
 	}
+	if (vma_shift != PUD_SHIFT &&
+	    vma_shift != PMD_SHIFT &&
+	    vma_shift != PAGE_SHIFT)
+		WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
 
 	if (s2fd->nested) {
 		unsigned long max_map_size;
diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c
index ff1f53e435e1..447e832ad512 100644
--- a/arch/arm64/mm/hugetlbpage.c
+++ b/arch/arm64/mm/hugetlbpage.c
@@ -47,16 +47,12 @@ unsigned int arch_hugetlb_cma_order(void)
 
 static bool __hugetlb_valid_size(unsigned long size)
 {
-	switch (size) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
+	if (size == PUD_SIZE)
 		return pud_sect_supported();
 #endif
-	case CONT_PMD_SIZE:
-	case PMD_SIZE:
-	case CONT_PTE_SIZE:
+	if (size == CONT_PMD_SIZE || size == PMD_SIZE || size == CONT_PTE_SIZE)
 		return true;
-	}
 
 	return false;
 }
@@ -100,16 +96,13 @@ static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
 
 	*pgsize = size;
 
-	switch (size) {
-	case CONT_PMD_SIZE:
+	if (size == CONT_PMD_SIZE) {
 		*pgsize = PMD_SIZE;
 		contig_ptes = CONT_PMDS;
-		break;
-	case CONT_PTE_SIZE:
+	} else if (size == CONT_PTE_SIZE) {
 		*pgsize = PAGE_SIZE;
 		contig_ptes = CONT_PTES;
-		break;
-	default:
+	} else {
 		WARN_ON(!__hugetlb_valid_size(size));
 	}
 
@@ -321,22 +314,16 @@ unsigned long hugetlb_mask_last_page(struct hstate *h)
 {
 	unsigned long hp_size = huge_page_size(h);
 
-	switch (hp_size) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
-		if (pud_sect_supported())
-			return PGDIR_SIZE - PUD_SIZE;
-		break;
+	if (hp_size == PUD_SIZE && pud_sect_supported())
+		return PGDIR_SIZE - PUD_SIZE;
 #endif
-	case CONT_PMD_SIZE:
+	if (hp_size == CONT_PMD_SIZE)
 		return PUD_SIZE - CONT_PMD_SIZE;
-	case PMD_SIZE:
+	if (hp_size == PMD_SIZE)
 		return PUD_SIZE - PMD_SIZE;
-	case CONT_PTE_SIZE:
+	if (hp_size == CONT_PTE_SIZE)
 		return PMD_SIZE - CONT_PTE_SIZE;
-	default:
-		break;
-	}
 
 	return 0UL;
 }
@@ -345,22 +332,17 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)
 {
 	size_t pagesize = 1UL << shift;
 
-	switch (pagesize) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
-		if (pud_sect_supported())
-			return pud_pte(pud_mkhuge(pte_pud(entry)));
-		break;
+	if (pagesize == PUD_SIZE && pud_sect_supported())
+		return pud_pte(pud_mkhuge(pte_pud(entry)));
 #endif
-	case CONT_PMD_SIZE:
+	if (pagesize == CONT_PMD_SIZE)
 		return pmd_pte(pmd_mkhuge(pmd_mkcont(pte_pmd(entry))));
-	case PMD_SIZE:
+	if (pagesize == PMD_SIZE)
 		return pmd_pte(pmd_mkhuge(pte_pmd(entry)));
-	case CONT_PTE_SIZE:
+	if (pagesize == CONT_PTE_SIZE)
 		return pte_mkcont(entry);
-	default:
-		break;
-	}
+
 	pr_warn("%s: unrecognized huge page size 0x%lx\n",
 		__func__, pagesize);
 	return entry;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 27/43] arm64: mm: Make page size dependent BUILD_BUG_ON() checks runtime
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (22 preceding siblings ...)
  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 ` 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
                   ` (14 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:51 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

Some BUILD_BUG_ON() checks test values that depend on the page size,
which are not compile-time constants with boot-time page size selection,
so they no longer compile.

Convert the check that the page table levels derived from
CONFIG_ARM64_VA_BITS match CONFIG_PGTABLE_LEVELS in arch_mm_preinit() to
VM_BUG_ON(). Convert the TASK_SIZE_32 check in arch_mm_preinit(), the
pgd_index() check in map_mem() and the PGD_SIZE alignment check in
pgtable_cache_init() to BUILD_BUG_ON_FIXED_PAGE_SIZE(), which stays a
build-time check for a fixed page size and becomes a VM_BUG_ON() for a
boot-time one.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 945351892e8d81903147f4adab6a29e7e5f24115)
---
 arch/arm64/mm/init.c | 6 +++---
 arch/arm64/mm/mmu.c  | 6 ++++--
 arch/arm64/mm/pgd.c  | 2 +-
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index 70393e4d2d82..f3229c921d9c 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -355,15 +355,15 @@ void __init arch_mm_preinit(void)
 	 * detected at build time already.
 	 */
 #ifdef CONFIG_COMPAT
-	BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
+	BUILD_BUG_ON_FIXED_PAGE_SIZE(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
 #endif
 
 	/*
 	 * Selected page table levels should match when derived from
 	 * scratch using the virtual address range and page size.
 	 */
-	BUILD_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
-		     CONFIG_PGTABLE_LEVELS);
+	VM_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
+		  CONFIG_PGTABLE_LEVELS);
 
 	if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
 		extern int sysctl_overcommit_memory;
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 96fc6d843d34..9bc4c0b4b4d5 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1198,8 +1198,10 @@ static void __init map_mem(void)
 	 * entire reduced VA space is covered by a single pgd_t which will have
 	 * been populated without the PXNTable attribute by the time we get here.)
 	 */
-	BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end) &&
-		     pgd_index(_PAGE_OFFSET(VA_BITS_MIN)) != PTRS_PER_PGD - 1);
+	BUILD_BUG_ON_FIXED_PAGE_SIZE(pgd_index(direct_map_end - 1) ==
+				     pgd_index(direct_map_end) &&
+				     pgd_index(_PAGE_OFFSET(VA_BITS_MIN)) !=
+				     PTRS_PER_PGD - 1);
 
 	arm64_kfence_map_pool();
 
diff --git a/arch/arm64/mm/pgd.c b/arch/arm64/mm/pgd.c
index bf5110b91e2f..5d40ce1eab3f 100644
--- a/arch/arm64/mm/pgd.c
+++ b/arch/arm64/mm/pgd.c
@@ -56,7 +56,7 @@ void __init pgtable_cache_init(void)
 	 * With 52-bit physical addresses, the architecture requires the
 	 * top-level table to be aligned to at least 64 bytes.
 	 */
-	BUILD_BUG_ON(!IS_ALIGNED(PGD_SIZE, 64));
+	BUILD_BUG_ON_FIXED_PAGE_SIZE(!IS_ALIGNED(PGD_SIZE, 64));
 #endif
 
 	/*


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 29/43] arm64: Look up the page size at runtime in assembly code
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (23 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:52 ` [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level Eric Curtin
                   ` (13 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland,
	Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, kvmarm

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

PAGE_SHIFT, PAGE_SIZE and PAGE_MASK are not compile-time constants with
boot-time page size selection, so they can no longer be used in
assembly.

Add get_page_size and get_page_mask macros which derive the value from
TCR_EL1.TG0, and use them in copy_page, the MTE page tag helpers in
mte.S, swsusp_arch_suspend_exit and arm64_relocate_new_kernel. The
lookup is done outside the loops, so the cost is a few extra
instructions per call. The copy_page macro takes an additional temporary
register to hold the page mask.

Two cases need different handling:

- __pi_copy_page has no spare register for the page size and should not
  reload it on every iteration. Its loop body is now a macro that is
  instantiated once for each page size, and the function branches to the
  right copy at entry based on TG0.

- invalid_host_el2_vect has no spare register and needs the page shift
  as an immediate, so with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE it selects
  the shift with alternatives based on ARM64_USE_PAGE_SIZE_*K. That code
  cannot run before alternatives have been applied.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 6e16e3bbf859c1da94ba9a3247514132994a5195)
---
 arch/arm64/include/asm/assembler.h  | 18 +++++++--
 arch/arm64/kernel/hibernate-asm.S   |  6 ++-
 arch/arm64/kernel/relocate_kernel.S | 10 +++--
 arch/arm64/kvm/hyp/nvhe/host.S      | 12 ++++++
 arch/arm64/lib/copy_page.S          | 59 ++++++++++++++++++-----------
 arch/arm64/lib/mte.S                | 27 ++++++++++---
 6 files changed, 96 insertions(+), 36 deletions(-)

diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
index fc9659c64848..9ee11672d192 100644
--- a/arch/arm64/include/asm/assembler.h
+++ b/arch/arm64/include/asm/assembler.h
@@ -491,9 +491,11 @@ alternative_else_nop_endif
 .Lskip_\@:
 	.endm
 /*
- * copy_page - copy src to dest using temp registers t1-t8
+ * copy_page - copy src to dest using temp registers t1-t9
  */
-	.macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req
+	.macro copy_page dest:req src:req t1:req t2:req t3:req t4:req t5:req t6:req t7:req t8:req t9:req
+	get_page_size \t9
+	sub	\t9, \t9, #1			// (PAGE_SIZE - 1) in \t9
 9998:	ldp	\t1, \t2, [\src]
 	ldp	\t3, \t4, [\src, #16]
 	ldp	\t5, \t6, [\src, #32]
@@ -504,7 +506,7 @@ alternative_else_nop_endif
 	stnp	\t5, \t6, [\dest, #32]
 	stnp	\t7, \t8, [\dest, #48]
 	add	\dest, \dest, #64
-	tst	\src, #(PAGE_SIZE - 1)
+	tst	\src, \t9
 	b.ne	9998b
 	.endm
 
@@ -885,4 +887,14 @@ alternative_cb_end
 	.macro	tgran_lpa2, val, tg0
 	value_for_page_size \val, \tg0, ID_AA64MMFR0_EL1_TGRAN4_52_BIT, ID_AA64MMFR0_EL1_TGRAN16_52_BIT, -1
 	.endm
+
+	.macro	get_page_size, val
+	get_tg0 \val
+	value_for_page_size \val, \val, SZ_4K, SZ_16K, SZ_64K
+	.endm
+
+	.macro	get_page_mask, val
+	get_tg0 \val
+	value_for_page_size \val, \val, (~(SZ_4K-1)), (~(SZ_16K-1)), (~(SZ_64K-1))
+	.endm
 #endif	/* __ASM_ASSEMBLER_H */
diff --git a/arch/arm64/kernel/hibernate-asm.S b/arch/arm64/kernel/hibernate-asm.S
index 2baefe7a82d3..556a7ecd3575 100644
--- a/arch/arm64/kernel/hibernate-asm.S
+++ b/arch/arm64/kernel/hibernate-asm.S
@@ -57,6 +57,8 @@ SYM_CODE_START(swsusp_arch_suspend_exit)
 	mov	x24, x4
 	mov	x25, x5
 
+	get_page_size x12
+
 	/* walk the restore_pblist and use copy_page() to over-write memory */
 	mov	x19, x3
 
@@ -64,9 +66,9 @@ SYM_CODE_START(swsusp_arch_suspend_exit)
 	mov	x0, x10
 	ldr	x1, [x19, #HIBERN_PBE_ADDR]
 
-	copy_page	x0, x1, x2, x3, x4, x5, x6, x7, x8, x9
+	copy_page	x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x11
 
-	add	x1, x10, #PAGE_SIZE
+	add	x1, x10, x12
 	/* Clean the copied page to PoU - based on caches_clean_inval_pou() */
 	raw_dcache_line_size x2, x3
 	sub	x3, x2, #1
diff --git a/arch/arm64/kernel/relocate_kernel.S b/arch/arm64/kernel/relocate_kernel.S
index 064db5856bba..3ffb5682ee83 100644
--- a/arch/arm64/kernel/relocate_kernel.S
+++ b/arch/arm64/kernel/relocate_kernel.S
@@ -49,6 +49,10 @@ SYM_TYPED_FUNC_START(arm64_relocate_new_kernel)
 	ldr	x27, [x0, #KIMAGE_ARCH_EL2_VECTORS]
 	ldr	x26, [x0, #KIMAGE_ARCH_DTB_MEM]
 
+	/* Grab page size values. */
+	get_page_size x10			/* x10 = PAGE_SIZE */
+	get_page_mask x11			/* x11 = PAGE_MASK */
+
 	/* Setup the list loop variables. */
 	ldr	x18, [x0, #KIMAGE_ARCH_ZERO_PAGE] /* x18 = zero page for BBM */
 	ldr	x17, [x0, #KIMAGE_ARCH_TTBR1]	/* x17 = linear map copy */
@@ -57,7 +61,7 @@ SYM_TYPED_FUNC_START(arm64_relocate_new_kernel)
 	raw_dcache_line_size x15, x1		/* x15 = dcache line size */
 	break_before_make_ttbr_switch	x18, x17, x1, x2 /* set linear map */
 .Lloop:
-	and	x12, x16, PAGE_MASK		/* x12 = addr */
+	and	x12, x16, x11			/* x12 = addr */
 	sub	x12, x12, x22			/* Convert x12 to virt */
 	/* Test the entry flags. */
 .Ltest_source:
@@ -65,8 +69,8 @@ SYM_TYPED_FUNC_START(arm64_relocate_new_kernel)
 
 	/* Invalidate dest page to PoC. */
 	mov	x19, x13
-	copy_page x13, x12, x1, x2, x3, x4, x5, x6, x7, x8
-	add	x1, x19, #PAGE_SIZE
+	copy_page x13, x12, x1, x2, x3, x4, x5, x6, x7, x8, x9
+	add	x1, x19, x10
 	dcache_by_myline_op_nosync civac, x19, x1, x15, x20
 	dsb	sy
 	b	.Lnext
diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S
index 50581822ec74..6c12a9a90e86 100644
--- a/arch/arm64/kvm/hyp/nvhe/host.S
+++ b/arch/arm64/kvm/hyp/nvhe/host.S
@@ -192,7 +192,19 @@ SYM_FUNC_END(__host_hvc)
 	 */
 	add	sp, sp, x0			// sp' = sp + x0
 	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+alternative_if ARM64_USE_PAGE_SIZE_4K
+	tbz	x0, #ARM64_PAGE_SHIFT_4K, .L__hyp_sp_overflow\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_16K
+	tbz	x0, #ARM64_PAGE_SHIFT_16K, .L__hyp_sp_overflow\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_64K
+	tbz	x0, #ARM64_PAGE_SHIFT_64K, .L__hyp_sp_overflow\@
+alternative_else_nop_endif
+#else
 	tbz	x0, #NVHE_STACK_SHIFT, .L__hyp_sp_overflow\@
+#endif
 	sub	x0, sp, x0			// x0'' = sp' - x0' = (sp + x0) - sp = x0
 	sub	sp, sp, x0			// sp'' = sp' - x0 = (sp + x0) - x0 = sp
 
diff --git a/arch/arm64/lib/copy_page.S b/arch/arm64/lib/copy_page.S
index e6374e7e5511..5b4fc2bb983e 100644
--- a/arch/arm64/lib/copy_page.S
+++ b/arch/arm64/lib/copy_page.S
@@ -10,27 +10,7 @@
 #include <asm/cpufeature.h>
 #include <asm/alternative.h>
 
-/*
- * Copy a page from src to dest (both are page aligned)
- *
- * Parameters:
- *	x0 - dest
- *	x1 - src
- */
-SYM_FUNC_START(__pi_copy_page)
-#ifdef CONFIG_AS_HAS_MOPS
-	.arch_extension mops
-alternative_if_not ARM64_HAS_MOPS
-	b	.Lno_mops
-alternative_else_nop_endif
-
-	mov	x2, #PAGE_SIZE
-	cpypwn	[x0]!, [x1]!, x2!
-	cpymwn	[x0]!, [x1]!, x2!
-	cpyewn	[x0]!, [x1]!, x2!
-	ret
-.Lno_mops:
-#endif
+	.macro	copy_page_body, page_size
 	ldp	x2, x3, [x1]
 	ldp	x4, x5, [x1, #16]
 	ldp	x6, x7, [x1, #32]
@@ -43,7 +23,7 @@ alternative_else_nop_endif
 	add	x0, x0, #256
 	add	x1, x1, #128
 1:
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, #(\page_size - 1)
 
 	stnp	x2, x3, [x0, #-256]
 	ldp	x2, x3, [x1]
@@ -75,7 +55,42 @@ alternative_else_nop_endif
 	stnp	x12, x13, [x0, #80 - 256]
 	stnp	x14, x15, [x0, #96 - 256]
 	stnp	x16, x17, [x0, #112 - 256]
+	.endm
+
+/*
+ * Copy a page from src to dest (both are page aligned)
+ *
+ * Parameters:
+ *	x0 - dest
+ *	x1 - src
+ */
+SYM_FUNC_START(__pi_copy_page)
+#ifdef CONFIG_AS_HAS_MOPS
+	.arch_extension mops
+alternative_if_not ARM64_HAS_MOPS
+	b	.Lno_mops
+alternative_else_nop_endif
 
+	get_page_size x2
+	cpypwn	[x0]!, [x1]!, x2!
+	cpymwn	[x0]!, [x1]!, x2!
+	cpyewn	[x0]!, [x1]!, x2!
+	ret
+.Lno_mops:
+#endif
+	get_tg0	x2
+.Lsz_64k:
+	cmp	x2, #TCR_TG0_64K
+	b.ne	.Lsz_16k
+	copy_page_body SZ_64K
+	ret
+.Lsz_16k:
+	cmp	x2, #TCR_TG0_16K
+	b.ne	.Lsz_4k
+	copy_page_body SZ_16K
+	ret
+.Lsz_4k:
+	copy_page_body SZ_4K
 	ret
 SYM_FUNC_END(__pi_copy_page)
 SYM_FUNC_ALIAS(copy_page, __pi_copy_page)
diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S
index 5018ac03b6bf..b4f6f5be0ec7 100644
--- a/arch/arm64/lib/mte.S
+++ b/arch/arm64/lib/mte.S
@@ -28,10 +28,13 @@
  *   x0 - address of the page to be cleared
  */
 SYM_FUNC_START(mte_clear_page_tags)
+	get_page_size x3
+	sub	x3, x3, #1		// (PAGE_SIZE - 1) in x3
+
 	multitag_transfer_size x1, x2
 1:	stgm	xzr, [x0]
 	add	x0, x0, x1
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, x3
 	b.ne	1b
 	ret
 SYM_FUNC_END(mte_clear_page_tags)
@@ -43,6 +46,9 @@ SYM_FUNC_END(mte_clear_page_tags)
  *	x0 - address to the beginning of the page
  */
 SYM_FUNC_START(mte_zero_clear_page_tags)
+	get_page_size x3
+	sub	x3, x3, #1		// (PAGE_SIZE - 1) in x3
+
 	and	x0, x0, #(1 << MTE_TAG_SHIFT) - 1	// clear the tag
 	mrs	x1, dczid_el0
 	tbnz	x1, #4, 2f	// Branch if DC GZVA is prohibited
@@ -52,12 +58,12 @@ SYM_FUNC_START(mte_zero_clear_page_tags)
 
 1:	dc	gzva, x0
 	add	x0, x0, x1
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, x3
 	b.ne	1b
 	ret
 
 2:	stz2g	x0, [x0], #(MTE_GRANULE_SIZE * 2)
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, x3
 	b.ne	2b
 	ret
 SYM_FUNC_END(mte_zero_clear_page_tags)
@@ -68,6 +74,9 @@ SYM_FUNC_END(mte_zero_clear_page_tags)
  *   x1 - address of the source page
  */
 SYM_FUNC_START(mte_copy_page_tags)
+	get_page_size x7
+	sub	x7, x7, #1		// (PAGE_SIZE - 1) in x7
+
 	mov	x2, x0
 	mov	x3, x1
 	multitag_transfer_size x5, x6
@@ -75,7 +84,7 @@ SYM_FUNC_START(mte_copy_page_tags)
 	stgm	x4, [x2]
 	add	x2, x2, x5
 	add	x3, x3, x5
-	tst	x2, #(PAGE_SIZE - 1)
+	tst	x2, x7
 	b.ne	1b
 	ret
 SYM_FUNC_END(mte_copy_page_tags)
@@ -137,6 +146,9 @@ SYM_FUNC_END(mte_copy_tags_to_user)
  *   x1 - tag storage, MTE_PAGE_TAG_STORAGE bytes
  */
 SYM_FUNC_START(mte_save_page_tags)
+	get_page_size x3
+	sub	x3, x3, #1		// (PAGE_SIZE - 1) in x3
+
 	multitag_transfer_size x7, x5
 1:
 	mov	x2, #0
@@ -149,7 +161,7 @@ SYM_FUNC_START(mte_save_page_tags)
 
 	str	x2, [x1], #8
 
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, x3
 	b.ne	1b
 
 	ret
@@ -161,6 +173,9 @@ SYM_FUNC_END(mte_save_page_tags)
  *   x1 - tag storage, MTE_PAGE_TAG_STORAGE bytes
  */
 SYM_FUNC_START(mte_restore_page_tags)
+	get_page_size x3
+	sub	x3, x3, #1		// (PAGE_SIZE - 1) in x3
+
 	multitag_transfer_size x7, x5
 1:
 	ldr	x2, [x1], #8
@@ -170,7 +185,7 @@ SYM_FUNC_START(mte_restore_page_tags)
 	tst	x0, #0xFF
 	b.ne	2b
 
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, x3
 	b.ne	1b
 
 	ret


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (24 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:52 ` [RFC PATCH v2 31/43] arm64: Support runtime folding in idmap_kpti_install_ng_mappings Eric Curtin
                   ` (12 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland,
	Aneesh Kumar K.V, Andrew Morton, Nick Piggin, Peter Zijlstra,
	Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, linux-arch, linux-mm, kvmarm

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

For a given VA size the number of translation levels depends on the
page size, so with boot-time page size selection it is not known until
boot how many levels are needed. 16K pages with 36-bit VAs and 64K pages
with 42-bit VAs, for example, need only two levels. The p4d and pud
levels can already be folded at runtime for the LPA2 fallback paths, and
the pmd level now needs the same treatment.

Add pgtable_l3_enabled() and use it, as the p4d and pud equivalents are
used, to make the pud_* helpers, set_pmd(), pmd_offset() and friends,
pmd_set_fixmap(), pmd_free(), __pmd_free_tlb() and mm_pmd_folded()
behave as folded when the pmd level is absent. The same check replaces
__PAGETABLE_PMD_FOLDED and CONFIG_PGTABLE_LEVELS tests in the KVM
stage-2 VMA size code, the hugetlb size helpers, the fixmap, the KPTI
level count, free_empty_pmd_table() and the ptdump level fixup. The
levels check in arch_mm_preinit() uses ptg_pgtable_levels.

pgtable_l3_enabled() is only the compile-time answer for now, so there
is no functional change.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 203ac9561f66a4947887e902541b403935c4fa1f)
---
 arch/arm64/include/asm/pgalloc.h |  15 +++-
 arch/arm64/include/asm/pgtable.h | 115 +++++++++++++++++++++++--------
 arch/arm64/include/asm/tlb.h     |   3 +
 arch/arm64/kvm/mmu.c             |   6 +-
 arch/arm64/mm/fixmap.c           |   2 +-
 arch/arm64/mm/hugetlbpage.c      |   9 +--
 arch/arm64/mm/init.c             |   5 +-
 arch/arm64/mm/mmu.c              |   6 +-
 arch/arm64/mm/ptdump.c           |   6 +-
 9 files changed, 120 insertions(+), 47 deletions(-)

diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h
index 1b4509d3382c..c79c7f43234b 100644
--- a/arch/arm64/include/asm/pgalloc.h
+++ b/arch/arm64/include/asm/pgalloc.h
@@ -15,6 +15,7 @@
 
 #define __HAVE_ARCH_PGD_FREE
 #define __HAVE_ARCH_PUD_FREE
+#define __HAVE_ARCH_PMD_FREE
 #include <asm-generic/pgalloc.h>
 
 #define PGD_SIZE	(PTRS_PER_PGD * sizeof(pgd_t))
@@ -23,7 +24,8 @@
 
 static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
 {
-	set_pud(pudp, __pud(__phys_to_pud_val(pmdp) | prot));
+	if (pgtable_l3_enabled())
+		set_pud(pudp, __pud(__phys_to_pud_val(pmdp) | prot));
 }
 
 static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)
@@ -33,6 +35,17 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)
 	pudval |= (mm == &init_mm) ? PUD_TABLE_UXN : PUD_TABLE_PXN;
 	__pud_populate(pudp, __pa(pmdp), pudval);
 }
+
+static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
+{
+	struct ptdesc *ptdesc = virt_to_ptdesc(pmd);
+
+	if (!pgtable_l3_enabled())
+		return;
+
+	BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
+	pagetable_dtor_free(ptdesc);
+}
 #else
 static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
 {
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index a474c9fb485a..c625fe7f8452 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -760,12 +760,14 @@ static inline int pmd_trans_huge(pmd_t pmd)
 }
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
-#if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS < 3
-static inline bool pud_table(pud_t pud) { return true; }
-#else
-#define pud_table(pud)		((pud_val(pud) & PUD_TYPE_MASK) == \
-				 PUD_TYPE_TABLE)
-#endif
+static inline bool pgtable_l3_enabled(void);
+
+static inline bool pud_table(pud_t pud)
+{
+	if (PAGE_SIZE == SZ_64K || !pgtable_l3_enabled())
+		return true;
+	return (pud_val(pud) & PUD_TYPE_MASK) == PUD_TYPE_TABLE;
+}
 
 extern pgd_t swapper_pg_dir[];
 extern pgd_t idmap_pg_dir[];
@@ -782,12 +784,10 @@ static inline bool in_swapper_pgdir(void *addr)
 
 static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
 {
-#ifdef __PAGETABLE_PMD_FOLDED
-	if (in_swapper_pgdir(pmdp)) {
+	if (!pgtable_l3_enabled() && in_swapper_pgdir(pmdp)) {
 		set_swapper_pgd((pgd_t *)pmdp, __pgd(pmd_val(pmd)));
 		return;
 	}
-#endif /* __PAGETABLE_PMD_FOLDED */
 
 	WRITE_ONCE(*pmdp, pmd);
 
@@ -824,22 +824,29 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
 
 #if CONFIG_PGTABLE_LEVELS > 2
 
-#define pud_none(pud)		(!pud_val(pud))
-#define pud_bad(pud)		((pud_val(pud) & PUD_TYPE_MASK) != \
-				 PUD_TYPE_TABLE)
-#define pud_present(pud)	pte_present(pud_pte(pud))
-#ifndef __PAGETABLE_PMD_FOLDED
+static __always_inline bool pgtable_l3_enabled(void)
+{
+	return true;
+}
+
+static inline bool mm_pmd_folded(const struct mm_struct *mm)
+{
+	return !pgtable_l3_enabled();
+}
+#define mm_pmd_folded  mm_pmd_folded
+
+#define pud_none(pud)		(pgtable_l3_enabled() && !pud_val(pud))
+#define pud_bad(pud)		(pgtable_l3_enabled() && !pud_table(pud))
+#define pud_present(pud)	(!pgtable_l3_enabled() || pte_present(pud_pte(pud)))
 #define pud_leaf pud_leaf
 static inline bool pud_leaf(pud_t pud)
 {
-	return pud_present(pud) && !pud_table(pud);
+	return pgtable_l3_enabled() && pte_present(pud_pte(pud)) &&
+	       !pud_table(pud);
 }
-#else
-#define pud_leaf(pud)		false
-#endif
-#define pud_valid(pud)		pte_valid(pud_pte(pud))
-#define pud_user(pud)		pte_user(pud_pte(pud))
-#define pud_user_exec(pud)	pte_user_exec(pud_pte(pud))
+#define pud_valid(pud)		(pgtable_l3_enabled() && pte_valid(pud_pte(pud)))
+#define pud_user(pud)		(pgtable_l3_enabled() && pte_user(pud_pte(pud)))
+#define pud_user_exec(pud)	(pgtable_l3_enabled() && pte_user_exec(pud_pte(pud)))
 
 static inline bool pgtable_l4_enabled(void);
 
@@ -858,7 +865,8 @@ static inline void set_pud(pud_t *pudp, pud_t pud)
 
 static inline void pud_clear(pud_t *pudp)
 {
-	set_pud(pudp, __pud(0));
+	if (pgtable_l3_enabled())
+		set_pud(pudp, __pud(0));
 }
 
 static inline phys_addr_t pud_page_paddr(pud_t pud)
@@ -866,25 +874,74 @@ static inline phys_addr_t pud_page_paddr(pud_t pud)
 	return __pud_to_phys(pud);
 }
 
+#define pmd_index(addr)		(((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
+
+static inline pmd_t *pud_to_folded_pmd(pud_t *pudp, unsigned long addr)
+{
+	return (pmd_t *)pudp;
+}
+
 static inline pmd_t *pud_pgtable(pud_t pud)
 {
 	return (pmd_t *)__va(pud_page_paddr(pud));
 }
 
-/* Find an entry in the second-level page table. */
-#define pmd_offset_phys(dir, addr)	(pud_page_paddr(READ_ONCE(*(dir))) + pmd_index(addr) * sizeof(pmd_t))
+static inline phys_addr_t pmd_offset_phys(pud_t *pudp, unsigned long addr)
+{
+	BUG_ON(!pgtable_l3_enabled());
 
-#define pmd_set_fixmap(addr)		((pmd_t *)set_fixmap_offset(FIX_PMD, addr))
-#define pmd_set_fixmap_offset(pud, addr)	pmd_set_fixmap(pmd_offset_phys(pud, addr))
-#define pmd_clear_fixmap()		clear_fixmap(FIX_PMD)
+	return pud_page_paddr(READ_ONCE(*pudp)) + pmd_index(addr) * sizeof(pmd_t);
+}
 
-#define pud_page(pud)			phys_to_page(__pud_to_phys(pud))
+static inline pmd_t *pmd_offset_lockless(pud_t *pudp, pud_t pud,
+					 unsigned long addr)
+{
+	if (!pgtable_l3_enabled())
+		return pud_to_folded_pmd(pudp, addr);
+	return (pmd_t *)__va(pud_page_paddr(pud)) + pmd_index(addr);
+}
+#define pmd_offset_lockless pmd_offset_lockless
+
+static inline pmd_t *pmd_offset(pud_t *pudp, unsigned long addr)
+{
+	return pmd_offset_lockless(pudp, READ_ONCE(*pudp), addr);
+}
+#define pmd_offset pmd_offset
+
+static inline pmd_t *pmd_set_fixmap(unsigned long addr)
+{
+	if (!pgtable_l3_enabled())
+		return NULL;
+	return (pmd_t *)set_fixmap_offset(FIX_PMD, addr);
+}
+
+static inline pmd_t *pmd_set_fixmap_offset(pud_t *pudp, unsigned long addr)
+{
+	if (!pgtable_l3_enabled())
+		return pud_to_folded_pmd(pudp, addr);
+	return pmd_set_fixmap(pmd_offset_phys(pudp, addr));
+}
+
+static inline void pmd_clear_fixmap(void)
+{
+	if (pgtable_l3_enabled())
+		clear_fixmap(FIX_PMD);
+}
 
 /* use ONLY for statically allocated translation tables */
-#define pmd_offset_kimg(dir,addr)	((pmd_t *)__phys_to_kimg(pmd_offset_phys((dir), (addr))))
+static inline pmd_t *pmd_offset_kimg(pud_t *pudp, u64 addr)
+{
+	if (!pgtable_l3_enabled())
+		return pud_to_folded_pmd(pudp, addr);
+	return (pmd_t *)__phys_to_kimg(pmd_offset_phys(pudp, addr));
+}
+
+#define pud_page(pud)			phys_to_page(__pud_to_phys(pud))
 
 #else
 
+static inline bool pgtable_l3_enabled(void) { return false; }
+
 #define pud_valid(pud)		false
 #define pud_page_paddr(pud)	({ BUILD_BUG(); 0; })
 #define pud_user_exec(pud)	pud_user(pud) /* Always 0 with folding */
diff --git a/arch/arm64/include/asm/tlb.h b/arch/arm64/include/asm/tlb.h
index 751bd57bc3ba..3197ed0702b4 100644
--- a/arch/arm64/include/asm/tlb.h
+++ b/arch/arm64/include/asm/tlb.h
@@ -87,6 +87,9 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
 {
 	struct ptdesc *ptdesc = virt_to_ptdesc(pmdp);
 
+	if (!pgtable_l3_enabled())
+		return;
+
 	tlb_remove_ptdesc(tlb, ptdesc);
 }
 #endif
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index c3a5ee486ae8..b15d59ec0bee 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1929,13 +1929,11 @@ static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd,
 		vma_shift = get_vma_page_shift(vma);
 	}
 
-#ifndef __PAGETABLE_PMD_FOLDED
-	if (vma_shift == PUD_SHIFT) {
+	if (pgtable_l3_enabled() && vma_shift == PUD_SHIFT) {
 		if (!fault_supports_stage2_huge_mapping(s2fd, PUD_SIZE))
 			vma_shift = PMD_SHIFT;
 	}
-#endif
-	if (vma_shift == CONT_PMD_SHIFT) {
+	if (vma_shift == CONT_PMD_SHIFT)
 		vma_shift = PMD_SHIFT;
 	if ((vma_shift == PMD_SHIFT &&
 	     !fault_supports_stage2_huge_mapping(s2fd, PMD_SIZE)) ||
diff --git a/arch/arm64/mm/fixmap.c b/arch/arm64/mm/fixmap.c
index 46e08631d08e..0d1c1798e52c 100644
--- a/arch/arm64/mm/fixmap.c
+++ b/arch/arm64/mm/fixmap.c
@@ -89,7 +89,7 @@ static void __init early_fixmap_init_pud(p4d_t *p4dp, unsigned long addr,
 	p4d_t p4d = READ_ONCE(*p4dp);
 	pud_t *pudp;
 
-	if (CONFIG_PGTABLE_LEVELS > 3 && !p4d_none(p4d) &&
+	if (ptg_pgtable_levels > 3 && !p4d_none(p4d) &&
 	    p4d_page_paddr(p4d) != __pa_symbol(bm_pud)) {
 		/*
 		 * We only end up here if the kernel mapping and the fixmap
diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c
index 447e832ad512..efe0f90e041e 100644
--- a/arch/arm64/mm/hugetlbpage.c
+++ b/arch/arm64/mm/hugetlbpage.c
@@ -47,10 +47,9 @@ unsigned int arch_hugetlb_cma_order(void)
 
 static bool __hugetlb_valid_size(unsigned long size)
 {
-#ifndef __PAGETABLE_PMD_FOLDED
-	if (size == PUD_SIZE)
+	if (pgtable_l3_enabled() && size == PUD_SIZE)
 		return pud_sect_supported();
-#endif
+
 	if (size == CONT_PMD_SIZE || size == PMD_SIZE || size == CONT_PTE_SIZE)
 		return true;
 
@@ -314,10 +313,8 @@ unsigned long hugetlb_mask_last_page(struct hstate *h)
 {
 	unsigned long hp_size = huge_page_size(h);
 
-#ifndef __PAGETABLE_PMD_FOLDED
-	if (hp_size == PUD_SIZE && pud_sect_supported())
+	if (pgtable_l3_enabled() && hp_size == PUD_SIZE && pud_sect_supported())
 		return PGDIR_SIZE - PUD_SIZE;
-#endif
 	if (hp_size == CONT_PMD_SIZE)
 		return PUD_SIZE - CONT_PMD_SIZE;
 	if (hp_size == PMD_SIZE)
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index f3229c921d9c..e6c15b06dc46 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -362,8 +362,9 @@ void __init arch_mm_preinit(void)
 	 * Selected page table levels should match when derived from
 	 * scratch using the virtual address range and page size.
 	 */
-	VM_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
-		  CONFIG_PGTABLE_LEVELS);
+	BUILD_BUG_ON_FIXED_PAGE_SIZE(
+		ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
+		ptg_pgtable_levels);
 
 	if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
 		extern int sysctl_overcommit_memory;
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 9bc4c0b4b4d5..731633c64557 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1322,8 +1322,10 @@ static int __init __kpti_install_ng_mappings(void *__unused)
 
 	if (levels == 5 && !pgtable_l5_enabled())
 		levels = 4;
-	else if (levels == 4 && !pgtable_l4_enabled())
+	if (levels == 4 && !pgtable_l4_enabled())
 		levels = 3;
+	if (levels == 3 && !pgtable_l3_enabled())
+		levels = 2;
 
 	remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
 
@@ -1726,7 +1728,7 @@ static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
 		free_empty_pte_table(pmdp, addr, next, floor, ceiling);
 	} while (addr = next, addr < end);
 
-	if (CONFIG_PGTABLE_LEVELS <= 2)
+	if (!pgtable_l3_enabled())
 		return;
 
 	if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c
index 9d9bcb6738f4..6f9ddaa5da7b 100644
--- a/arch/arm64/mm/ptdump.c
+++ b/arch/arm64/mm/ptdump.c
@@ -197,8 +197,10 @@ void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
 	ptval_t prot = 0;
 
 	/* check if the current level has been folded dynamically */
-	if (st->mm && ((level == 1 && mm_p4d_folded(st->mm)) ||
-	    (level == 2 && mm_pud_folded(st->mm))))
+	if (st->mm &&
+	    ((level == 1 && mm_p4d_folded(st->mm)) ||
+	     (level == 2 && mm_pud_folded(st->mm)) ||
+	     (level == 3 && mm_pmd_folded(st->mm))))
 		level = 0;
 
 	if (level >= 0)


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 31/43] arm64: Support runtime folding in idmap_kpti_install_ng_mappings
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (25 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:52 ` [RFC PATCH v2 32/43] arm64: Patch TRAMP_VALIAS into instructions at boot Eric Curtin
                   ` (11 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

idmap_kpti_install_ng_mappings walks the swapper page tables using
PTRS_PER_* and PAGE_SIZE as assembler immediates. These are not
compile-time constants with boot-time page size selection, and some
levels may be folded depending on the page size and VA size in use.

Pass the number of entries at each level to the function in a new
struct install_ng_pgtable_geometry, filled in by
__kpti_install_ng_mappings(), and keep them in registers. The page size
comes from the new get_page_size macro and the page address mask is
derived from the new get_page_shift macro. kpti_mk_tbl_ng and
kpti_map_pgtbl use these registers instead of immediates, and a level
whose entry count is one is skipped by pointing the next level at the
current entry. The register pressure means the callee-saved registers
are now always saved and restored, and the secondary CPU path loads
swapper_ttb and the flag pointer itself.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 02c5bd44e4ab89acc1a534c76c1d7211836dd771)
---
 arch/arm64/include/asm/assembler.h |   5 ++
 arch/arm64/mm/mmu.c                |  21 +++++-
 arch/arm64/mm/proc.S               | 107 ++++++++++++++++++++++-------
 3 files changed, 108 insertions(+), 25 deletions(-)

diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
index 9ee11672d192..e55c4094a2a5 100644
--- a/arch/arm64/include/asm/assembler.h
+++ b/arch/arm64/include/asm/assembler.h
@@ -893,6 +893,11 @@ alternative_cb_end
 	value_for_page_size \val, \val, SZ_4K, SZ_16K, SZ_64K
 	.endm
 
+	.macro	get_page_shift, val
+	get_tg0 \val
+	value_for_page_size \val, \val, ARM64_PAGE_SHIFT_4K, ARM64_PAGE_SHIFT_16K, ARM64_PAGE_SHIFT_64K
+	.endm
+
 	.macro	get_page_mask, val
 	get_tg0 \val
 	value_for_page_size \val, \val, (~(SZ_4K-1)), (~(SZ_16K-1)), (~(SZ_64K-1))
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 731633c64557..e1f06f622f32 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1307,11 +1307,27 @@ static phys_addr_t __init kpti_ng_pgd_alloc(enum pgtable_level pgtable_level)
 	return kpti_ng_temp_alloc;
 }
 
+struct install_ng_pgtable_geometry {
+	unsigned long ptrs_per_pte;
+	unsigned long ptrs_per_pmd;
+	unsigned long ptrs_per_pud;
+	unsigned long ptrs_per_p4d;
+	unsigned long ptrs_per_pgd;
+};
+
 static int __init __kpti_install_ng_mappings(void *__unused)
 {
-	typedef void (kpti_remap_fn)(int, int, phys_addr_t, unsigned long);
+	typedef void (kpti_remap_fn)(int, int, phys_addr_t, unsigned long,
+				     struct install_ng_pgtable_geometry *);
 	extern kpti_remap_fn idmap_kpti_install_ng_mappings;
 	kpti_remap_fn *remap_fn;
+	struct install_ng_pgtable_geometry geometry = {
+		.ptrs_per_pte = PTRS_PER_PTE,
+		.ptrs_per_pmd = PTRS_PER_PMD,
+		.ptrs_per_pud = PTRS_PER_PUD,
+		.ptrs_per_p4d = PTRS_PER_P4D,
+		.ptrs_per_pgd = PTRS_PER_PGD,
+	};
 
 	int cpu = smp_processor_id();
 	int levels = CONFIG_PGTABLE_LEVELS;
@@ -1360,7 +1376,8 @@ static int __init __kpti_install_ng_mappings(void *__unused)
 	}
 
 	cpu_install_idmap();
-	remap_fn(cpu, num_online_cpus(), kpti_ng_temp_pgd_pa, KPTI_NG_TEMP_VA);
+	remap_fn(cpu, num_online_cpus(), kpti_ng_temp_pgd_pa, KPTI_NG_TEMP_VA,
+		 &geometry);
 	cpu_uninstall_idmap();
 
 	if (!cpu) {
diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S
index afc9dc20084e..88bf5f02c5a4 100644
--- a/arch/arm64/mm/proc.S
+++ b/arch/arm64/mm/proc.S
@@ -238,7 +238,7 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1)
 	.pushsection ".idmap.text", "a"
 
 	.macro	pte_to_phys, phys, pte
-	and	\phys, \pte, #PTE_ADDR_LOW
+	and	\phys, \pte, pte_addr_low
 #ifdef CONFIG_ARM64_PA_BITS_52
 	and	\pte, \pte, #PTE_ADDR_HIGH
 	orr	\phys, \phys, \pte, lsl #PTE_ADDR_HIGH_SHIFT
@@ -246,7 +246,8 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1)
 	.endm
 
 	.macro	kpti_mk_tbl_ng, type, num_entries
-	add	end_\type\()p, cur_\type\()p, #\num_entries * 8
+	lsl	scratch, \num_entries, #3
+	add	end_\type\()p, cur_\type\()p, scratch
 .Ldo_\type:
 	ldr	\type, [cur_\type\()p], #8	// Load the entry and advance
 	tbz	\type, #0, .Lnext_\type		// Skip invalid and
@@ -268,14 +269,18 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1)
 	.macro	kpti_map_pgtbl, type, level
 	str	xzr, [temp_pte, #8 * (\level + 2)]	// break before make
 	dsb	nshst
-	add	pte, temp_pte, #PAGE_SIZE * (\level + 2)
+	mov	scratch, #(\level + 2)
+	mul	scratch, scratch, page_size
+	add	pte, temp_pte, scratch
 	lsr	pte, pte, #12
 	tlbi	vaae1, pte
 	dsb	nsh
 	isb
 
 	phys_to_pte pte, cur_\type\()p
-	add	cur_\type\()p, temp_pte, #PAGE_SIZE * (\level + 2)
+	mov	scratch, #(\level + 2)
+	mul	scratch, scratch, page_size
+	add	cur_\type\()p, temp_pte, scratch
 	orr	pte, pte, pte_flags
 	str	pte, [temp_pte, #8 * (\level + 2)]
 	dsb	nshst
@@ -284,7 +289,8 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1)
 
 /*
  * void __kpti_install_ng_mappings(int cpu, int num_secondaries, phys_addr_t temp_pgd,
- *				   unsigned long temp_pte_va)
+ *				   unsigned long temp_pte_va,
+ *				   struct install_ng_pgtable_geometry *geometry)
  *
  * Called exactly once from stop_machine context by each CPU found during boot.
  */
@@ -296,6 +302,8 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings)
 	temp_pgd_phys	.req	x2
 	swapper_ttb	.req	x3
 	flag_ptr	.req	x4
+	geometry	.req	x4
+	scratch		.req	x4
 	cur_pgdp	.req	x5
 	end_pgdp	.req	x6
 	pgd		.req	x7
@@ -309,18 +317,45 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings)
 	valid		.req	x17
 	cur_p4dp	.req	x19
 	end_p4dp	.req	x20
-
-	mov	x5, x3				// preserve temp_pte arg
-	mrs	swapper_ttb, ttbr1_el1
-	adr_l	flag_ptr, idmap_kpti_bbml3_flag
+	page_size	.req	x21
+	ptrs_per_pte	.req	x22
+	ptrs_per_pmd	.req	x23
+	ptrs_per_pud	.req	x24
+	ptrs_per_p4d	.req	x25
+	ptrs_per_pgd	.req	x26
+	pte_addr_low	.req	x27
 
 	cbnz	cpu, __idmap_kpti_secondary
 
-#if CONFIG_PGTABLE_LEVELS > 4
-	stp	x29, x30, [sp, #-32]!
+	/* Preserve callee-saved registers */
+	stp	x19, x20, [sp, #-96]!
+	stp	x21, x22, [sp, #80]
+	stp	x23, x24, [sp, #64]
+	stp	x25, x26, [sp, #48]
+	stp	x27, x28, [sp, #32]
+	stp	x29, x30, [sp, #16]
 	mov	x29, sp
-	stp	x19, x20, [sp, #16]
-#endif
+
+	/* Load pgtable geometry parameters */
+	get_page_size page_size
+	ldr	ptrs_per_pte, [geometry, #0]
+	ldr	ptrs_per_pmd, [geometry, #8]
+	ldr	ptrs_per_pud, [geometry, #16]
+	ldr	ptrs_per_p4d, [geometry, #24]
+	ldr	ptrs_per_pgd, [geometry, #32]
+
+	/* Precalculate pte_addr_low mask */
+	get_page_shift x0
+	mov	pte_addr_low, #50
+	sub	pte_addr_low, pte_addr_low, x0
+	mov	scratch, #1
+	lsl	pte_addr_low, scratch, pte_addr_low
+	sub	pte_addr_low, pte_addr_low, #1
+	lsl	pte_addr_low, pte_addr_low, x0
+
+	mov	temp_pte, x3
+	mrs	swapper_ttb, ttbr1_el1
+	adr_l	flag_ptr, idmap_kpti_bbml3_flag
 
 	/* We're the boot CPU. Wait for the others to catch up */
 	sevl
@@ -335,7 +370,6 @@ SYM_TYPED_FUNC_START(idmap_kpti_install_ng_mappings)
 	msr	ttbr1_el1, temp_pgd_phys
 	isb
 
-	mov	temp_pte, x5
 	mov_q	pte_flags, KPTI_NG_PTE_FLAGS
 
 	/* Everybody is enjoying the idmap, so we can rewrite swapper. */
@@ -365,7 +399,7 @@ alternative_else_nop_endif
 	/* PGD */
 	adrp		cur_pgdp, swapper_pg_dir
 	kpti_map_pgtbl	pgd, -1
-	kpti_mk_tbl_ng	pgd, PTRS_PER_PGD
+	kpti_mk_tbl_ng	pgd, ptrs_per_pgd
 
 	/* Ensure all the updated entries are visible to secondary CPUs */
 	dsb	ishst
@@ -376,21 +410,33 @@ alternative_else_nop_endif
 	isb
 
 	/* Set the flag to zero to indicate that we're all done */
+	adr_l	flag_ptr, idmap_kpti_bbml3_flag
 	str	wzr, [flag_ptr]
-#if CONFIG_PGTABLE_LEVELS > 4
-	ldp	x19, x20, [sp, #16]
-	ldp	x29, x30, [sp], #32
-#endif
+
+	/* Restore callee-saved registers */
+	ldp	x29, x30, [sp, #16]
+	ldp	x27, x28, [sp, #32]
+	ldp	x25, x26, [sp, #48]
+	ldp	x23, x24, [sp, #64]
+	ldp	x21, x22, [sp, #80]
+	ldp	x19, x20, [sp], #96
+
 	ret
 
 .Lderef_pgd:
 	/* P4D */
 	.if		CONFIG_PGTABLE_LEVELS > 4
 	p4d		.req	x30
+	cmp		ptrs_per_p4d, #1
+	b.eq		.Lfold_p4d
 	pte_to_phys	cur_p4dp, pgd
 	kpti_map_pgtbl	p4d, 0
-	kpti_mk_tbl_ng	p4d, PTRS_PER_P4D
+	kpti_mk_tbl_ng	p4d, ptrs_per_p4d
 	b		.Lnext_pgd
+.Lfold_p4d:
+	mov		p4d, pgd		// fold to next level
+	mov		cur_p4dp, end_p4dp	// must be equal to terminate loop
+	b		.Lderef_p4d
 	.else		/* CONFIG_PGTABLE_LEVELS <= 4 */
 	p4d		.req	pgd
 	.set		.Lnext_p4d, .Lnext_pgd
@@ -400,10 +446,16 @@ alternative_else_nop_endif
 	/* PUD */
 	.if		CONFIG_PGTABLE_LEVELS > 3
 	pud		.req	x10
+	cmp		ptrs_per_pud, #1
+	b.eq		.Lfold_pud
 	pte_to_phys	cur_pudp, p4d
 	kpti_map_pgtbl	pud, 1
-	kpti_mk_tbl_ng	pud, PTRS_PER_PUD
+	kpti_mk_tbl_ng	pud, ptrs_per_pud
 	b		.Lnext_p4d
+.Lfold_pud:
+	mov		pud, p4d		// fold to next level
+	mov		cur_pudp, end_pudp	// must be equal to terminate loop
+	b		.Lderef_pud
 	.else		/* CONFIG_PGTABLE_LEVELS <= 3 */
 	pud		.req	pgd
 	.set		.Lnext_pud, .Lnext_pgd
@@ -413,10 +465,16 @@ alternative_else_nop_endif
 	/* PMD */
 	.if		CONFIG_PGTABLE_LEVELS > 2
 	pmd		.req	x13
+	cmp		ptrs_per_pmd, #1
+	b.eq		.Lfold_pmd
 	pte_to_phys	cur_pmdp, pud
 	kpti_map_pgtbl	pmd, 2
-	kpti_mk_tbl_ng	pmd, PTRS_PER_PMD
+	kpti_mk_tbl_ng	pmd, ptrs_per_pmd
 	b		.Lnext_pud
+.Lfold_pmd:
+	mov		pmd, pud		// fold to next level
+	mov		cur_pmdp, end_pmdp	// must be equal to terminate loop
+	b		.Lderef_pmd
 	.else		/* CONFIG_PGTABLE_LEVELS <= 2 */
 	pmd		.req	pgd
 	.set		.Lnext_pmd, .Lnext_pgd
@@ -426,7 +484,7 @@ alternative_else_nop_endif
 	/* PTE */
 	pte_to_phys	cur_ptep, pmd
 	kpti_map_pgtbl	pte, 3
-	kpti_mk_tbl_ng	pte, PTRS_PER_PTE
+	kpti_mk_tbl_ng	pte, ptrs_per_pte
 	b		.Lnext_pmd
 
 	.unreq	cpu
@@ -453,6 +511,9 @@ alternative_else_nop_endif
 
 	/* Secondary CPUs end up here */
 __idmap_kpti_secondary:
+	mrs	swapper_ttb, ttbr1_el1
+	adr_l	flag_ptr, idmap_kpti_bbml3_flag
+
 	/* Uninstall swapper before surgery begins */
 	__idmap_cpu_set_reserved_ttbr1 x16, x17
 	b scondary_cpu_wait


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 32/43] arm64: Patch TRAMP_VALIAS into instructions at boot
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (26 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:52 ` [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time Eric Curtin
                   ` (10 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland

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

TRAMP_VALIAS is the fixmap address of the entry trampoline, and with
boot-time page size selection it depends on PAGE_SIZE and so is not a
compile-time constant. The tramp_alias macro in entry.S and the Falkor
erratum 1003 TLB invalidation in the trampoline both encode it as
immediates in movz/movk instructions.

With CONFIG_ARM64_BOOT_TIME_PAGE_SIZE, wrap these instruction sequences
in alternative_cb and patch in the final value once the page size is
known. tramp_alias initially encodes the offset of the symbol within the
trampoline text; kpti_patch_tramp_alias() recovers that offset, adds
TRAMP_VALIAS and rewrites the two instructions as a movn/movk pair, as
the fixmap lies in the top 2GiB. qcom_erratum_1003_patch_tramp_tlbi()
fills in the three movk instructions that hold the address bits for the
TLBI. TRAMP_VALIAS is no longer exported through asm-offsets in this
configuration.

Builds without boot-time page size are unchanged.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 30e1b94283a4ebe3ef362fd5a7befcc1cfe4ee9a)
---
 arch/arm64/kernel/asm-offsets.c |  2 +-
 arch/arm64/kernel/cpu_errata.c  | 53 +++++++++++++++++++++++++++++++++
 arch/arm64/kernel/entry.S       | 34 +++++++++++++++++++++
 arch/arm64/mm/mmu.c             | 50 +++++++++++++++++++++++++++++++
 4 files changed, 138 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
index 9d603317eeb9..c7490746d760 100644
--- a/arch/arm64/kernel/asm-offsets.c
+++ b/arch/arm64/kernel/asm-offsets.c
@@ -153,7 +153,7 @@ int main(void)
   DEFINE(HIBERN_PBE_NEXT,	offsetof(struct pbe, next));
   DEFINE(ARM64_FTR_SYSVAL,	offsetof(struct arm64_ftr_reg, sys_val));
   BLANK();
-#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
+#if defined(CONFIG_UNMAP_KERNEL_AT_EL0) && !defined(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE)
   DEFINE(TRAMP_VALIAS,		TRAMP_VALIAS);
 #endif
 #ifdef CONFIG_ARM_SDE_INTERFACE
diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c
index 0ded97d7cbec..6a7ae41c7147 100644
--- a/arch/arm64/kernel/cpu_errata.c
+++ b/arch/arm64/kernel/cpu_errata.c
@@ -507,6 +507,59 @@ static const struct arm64_cpu_capabilities qcom_erratum_1003_list[] = {
 	},
 	{},
 };
+
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+/* Only called from assembly, silence sparse */
+void __init qcom_erratum_1003_patch_tramp_tlbi(struct alt_instr *alt,
+					       __le32 *origptr, __le32 *updptr,
+					       int nr_inst);
+
+void __init qcom_erratum_1003_patch_tramp_tlbi(struct alt_instr *alt,
+					       __le32 *origptr, __le32 *updptr,
+					       int nr_inst)
+{
+	u32 insn, rd;
+	u64 val;
+
+	/*
+	 * ASID already in rd[63:48]. Bottom 48 bits, vaddr for TRAMP_VALIAS,
+	 * are different per page size, so are patched in at boot. Need 3 movk
+	 * instructions to do that.
+	 */
+	BUG_ON(nr_inst != 3);
+
+	/* 2MB boundary containing the vectors, so we nobble the walk cache */
+	val = (TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12;
+
+	/* Compute target register */
+	insn = le32_to_cpu(*origptr);
+	rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, insn);
+
+	/* movk rd, #(val & 0xffff) */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)val,
+					 0,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+
+	/* movk rd, #((val >> 16) & 0xffff), lsl #16 */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)(val >> 16),
+					 16,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+
+	/* movk rd, #((val >> 32) & 0xffff), lsl #32 */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)(val >> 32),
+					 32,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+}
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
 #endif
 
 #ifdef CONFIG_ARM64_WORKAROUND_CLEAN_CACHE
diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
index 8d17f61d1b5b..6c03468ae0dd 100644
--- a/arch/arm64/kernel/entry.S
+++ b/arch/arm64/kernel/entry.S
@@ -101,10 +101,25 @@
 	.endm
 
 	.macro	tramp_alias, dst, sym
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+	/*
+	 * TRAMP_VALIAS depends on page size, so let's initially stuff these
+	 * instructions with the offset of the symbol within the tramp text
+	 * section. Then at boot time, kpti_patch_tramp_alias() recovers the
+	 * offset, adds TRAMP_VALIAS and patches the final value back into the
+	 * instructions.
+	 */
+	.set	.Lsymoff\@, \sym - .entry.tramp.text
+alternative_cb ARM64_ALWAYS_BOOT, kpti_patch_tramp_alias
+	movz	\dst, :abs_g0_nc:.Lsymoff\@
+	movk	\dst, :abs_g1_nc:.Lsymoff\@
+alternative_cb_end
+#else
 	.set	.Lalias\@, TRAMP_VALIAS + \sym - .entry.tramp.text
 	movz	\dst, :abs_g2_s:.Lalias\@
 	movk	\dst, :abs_g1_nc:.Lalias\@
 	movk	\dst, :abs_g0_nc:.Lalias\@
+#endif
 	.endm
 
 	/*
@@ -635,6 +650,24 @@ SYM_CODE_END(ret_to_user)
 	bic	\tmp, \tmp, #USER_ASID_FLAG
 	msr	ttbr1_el1, \tmp
 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+alternative_if_not ARM64_WORKAROUND_QCOM_FALKOR_E1003
+	b	.Lskip_falkor_e1003\@
+alternative_else_nop_endif
+	/*
+	 * ASID already in \tmp[63:48]. Bottom 48 bits, vaddr for TRAMP_VALIAS,
+	 * are different per page size, so are patched in at boot.
+	 */
+alternative_cb ARM64_ALWAYS_BOOT, qcom_erratum_1003_patch_tramp_tlbi
+	movk	\tmp, #0
+	movk	\tmp, #0, lsl #16
+	movk	\tmp, #0, lsl #32
+alternative_cb_end
+	isb
+	tlbi	vae1, \tmp
+	dsb	nsh
+.Lskip_falkor_e1003\@:
+#else
 alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
 	/* ASID already in \tmp[63:48] */
 	movk	\tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
@@ -645,6 +678,7 @@ alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
 	tlbi	vae1, \tmp
 	dsb	nsh
 alternative_else_nop_endif
+#endif
 #endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */
 	.endm
 
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index e1f06f622f32..0cc8c8f27e0a 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -34,6 +34,7 @@
 #include <asm/barrier.h>
 #include <asm/cputype.h>
 #include <asm/fixmap.h>
+#include <asm/insn.h>
 #include <asm/kasan.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/sections.h>
@@ -1406,6 +1407,55 @@ void __init kpti_install_ng_mappings(void)
 	stop_machine(__kpti_install_ng_mappings, NULL, cpu_online_mask);
 }
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+/* Only called from assembly, silence sparse */
+void __init kpti_patch_tramp_alias(struct alt_instr *alt, __le32 *origptr,
+				   __le32 *updptr, int nr_inst);
+
+void __init kpti_patch_tramp_alias(struct alt_instr *alt, __le32 *origptr,
+				   __le32 *updptr, int nr_inst)
+{
+	u64 imm, val = 0;
+	u32 insn, rd;
+	int i;
+
+	BUG_ON(nr_inst != 2);
+
+	/* The fixmap is in the top 2GiB, so VA bits [63:32] are all ones */
+	BUILD_BUG_ON(__FIXADDR_TOT_START(PAGE_SHIFT_MAX) < -UL(SZ_2G));
+
+	/* Compute target register */
+	insn = le32_to_cpu(*origptr);
+	rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, insn);
+
+	/* Recover symbol offset; 16 bits per instruction, stored low to high */
+	for (i = 0; i < 2; i++, origptr++) {
+		insn = le32_to_cpu(*origptr);
+		imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn);
+		val |= imm << (i * 16);
+	}
+
+	/* Calculate final address in fixmap */
+	val += TRAMP_VALIAS;
+
+	/* movn rd, #(~(val >> 16) & 0xffff), lsl #16 */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)~(val >> 16),
+					 16,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_INVERSE);
+	*updptr++ = cpu_to_le32(insn);
+
+	/* movk rd, #(val & 0xffff) */
+	insn = aarch64_insn_gen_movewide(rd,
+					 (u16)val,
+					 0,
+					 AARCH64_INSN_VARIANT_64BIT,
+					 AARCH64_INSN_MOVEWIDE_KEEP);
+	*updptr++ = cpu_to_le32(insn);
+}
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 static pgprot_t __init kernel_exec_prot(void)
 {
 	return rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (27 preceding siblings ...)
  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
  2026-12-01  1:52 ` [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern at boot Eric Curtin
                   ` (9 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland,
	Ard Biesheuvel, Ilias Apalodimas, Marc Zyngier, Oliver Upton,
	Fuad Tabba, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
	Zenghui Yu, linux-efi, kvmarm

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)


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern at boot
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (28 preceding siblings ...)
  2026-12-01  1:52 ` [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time Eric Curtin
@ 2026-12-01  1:52 ` 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
                   ` (8 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Ryan Roberts, SJ Park, Andrew Morton, damon, linux-mm

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

damon_lru_sort_stub_pattern sets min_sz_region to PAGE_SIZE in a static
initialiser. PAGE_SIZE is not a constant expression with arm64
boot-time page size selection.

Define the pattern with DEFINE_GLOBAL_PAGE_SIZE_VAR(), which keeps the
plain initialiser for a fixed page size and otherwise fills it in once
the page size is known at boot.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit fed214a06106c320f2e46950b5b6672966952196)
---
 mm/damon/lru_sort.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c
index 64e086985eb5..37215e9b956c 100644
--- a/mm/damon/lru_sort.c
+++ b/mm/damon/lru_sort.c
@@ -170,7 +170,7 @@ DEFINE_DAMON_MODULES_DAMOS_STATS_PARAMS(damon_lru_sort_cold_stat,
 		lru_sort_tried_cold_regions, lru_sorted_cold_regions,
 		cold_quota_exceeds);
 
-static struct damos_access_pattern damon_lru_sort_stub_pattern = {
+static DEFINE_GLOBAL_PAGE_SIZE_VAR(struct damos_access_pattern, damon_lru_sort_stub_pattern, {
 	/* Find regions having PAGE_SIZE or larger size */
 	.min_sz_region = PAGE_SIZE,
 	.max_sz_region = ULONG_MAX,
@@ -180,7 +180,7 @@ static struct damos_access_pattern damon_lru_sort_stub_pattern = {
 	/* no matter its age */
 	.min_age_region = 0,
 	.max_age_region = UINT_MAX,
-};
+});
 
 static struct damon_ctx *ctx;
 static struct damon_target *target;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 35/43] mm/zsmalloc: Size static limits for the worst-case page size
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (29 preceding siblings ...)
  2026-12-01  1:52 ` [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern at boot Eric Curtin
@ 2026-12-01  1:52 ` Eric Curtin
  2026-12-01  1:52 ` [RFC PATCH v2 36/43] arm64: Convert remaining page-size switches to if/else Eric Curtin
                   ` (7 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Minchan Kim, Sergey Senozhatsky, Andrew Morton,
	linux-mm

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

zsmalloc uses PAGE_SIZE and PAGE_SHIFT in constants and in the size of
an array in struct zs_pool, none of which are compile-time constants
with arm64 boot-time page size selection.

Take the worst case where a constant is needed: _PFN_BITS uses
PAGE_SHIFT_MIN, since the smallest page gives the most PFN bits,
ZS_OBJS_PER_PAGE_BITS uses PAGE_SHIFT_MAX, and the 24-bit first object
offset check in set_first_obj_offset() uses PAGE_SIZE_MAX.
ZS_SIZE_CLASSES now depends on the runtime page size, so the size_class
array in struct zs_pool becomes a pointer that zs_create_pool()
allocates with kcalloc() and zs_destroy_pool() frees.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit dca1629b5374efd825c6a5bc6d4f210dae3b861f)
---
 mm/zsmalloc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 7ef80e0da626..09d131da22ac 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -72,13 +72,13 @@
 #else
 /*
  * If this definition of MAX_PHYSMEM_BITS is used, ZS_OBJ_PFN_SHIFT will
- * just be PAGE_SHIFT
+ * just be PAGE_SHIFT_MIN
  */
 #define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG
 #endif
 #endif
 
-#define _PFN_BITS		(MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
+#define _PFN_BITS		(MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT_MIN)
 
 /*
  * Head in allocated object should have OBJ_ALLOCATED_TAG
@@ -137,7 +137,7 @@
  * possible object size: log2(PAGE_SIZE / 32) = PAGE_SHIFT - 5.
  * 32 is the hard floor of ZS_MIN_ALLOC_SIZE.
  */
-#define ZS_OBJS_PER_PAGE_BITS	(PAGE_SHIFT - 5)
+#define ZS_OBJS_PER_PAGE_BITS	(PAGE_SHIFT_MAX - 5)
 
 /*
  * Bits to index any object in the densest possible zspage.  Below this,
@@ -270,7 +270,7 @@ static struct kmem_cache *zspage_cachep;
 struct zs_pool {
 	const char *name;
 
-	struct size_class *size_class[ZS_SIZE_CLASSES];
+	struct size_class **size_class;
 
 	atomic_long_t pages_allocated;
 
@@ -502,7 +502,7 @@ static inline unsigned int get_first_obj_offset(struct zpdesc *zpdesc)
 static inline void set_first_obj_offset(struct zpdesc *zpdesc, unsigned int offset)
 {
 	/* With 24 bits available, we can support offsets into 16 MiB pages. */
-	BUILD_BUG_ON(PAGE_SIZE > SZ_16M);
+	BUILD_BUG_ON(PAGE_SIZE_MAX > SZ_16M);
 	VM_WARN_ON_ONCE(!PageZsmalloc(zpdesc_page(zpdesc)));
 	VM_WARN_ON_ONCE(offset & ~FIRST_OBJ_PAGE_TYPE_MASK);
 	zpdesc->first_obj_offset &= ~FIRST_OBJ_PAGE_TYPE_MASK;
@@ -2151,6 +2151,13 @@ struct zs_pool *zs_create_pool(const char *name)
 	rwlock_init(&pool->lock);
 	atomic_set(&pool->compaction_in_progress, 0);
 
+	pool->size_class = kcalloc(ZS_SIZE_CLASSES,
+					sizeof(*pool->size_class), GFP_KERNEL);
+	if (!pool->size_class) {
+		kfree(pool);
+		return NULL;
+	}
+
 	pool->name = kstrdup(name, GFP_KERNEL);
 	if (!pool->name)
 		goto err;
@@ -2276,6 +2283,7 @@ void zs_destroy_pool(struct zs_pool *pool)
 		kfree(class);
 	}
 
+	kfree(pool->size_class);
 	kfree(pool->name);
 	kfree(pool);
 }


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 36/43] arm64: Convert remaining page-size switches to if/else
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (30 preceding siblings ...)
  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 ` 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
                   ` (6 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Eric Curtin, Catalin Marinas, Will Deacon, Mark Rutland,
	Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, kvmarm

From: Eric Curtin <eric.curtin@docker.com>

With boot-time page size selection PMD_SIZE, PUD_SIZE, PAGE_SIZE and the
CONT_*_SIZE values are not constants, so they cannot be case labels in a
switch statement, and PAGE_SHIFT cannot be used in a preprocessor
conditional.

Convert the remaining switches on those sizes to if/else chains in
__flush_hugetlb_tlb_range(), __set_ptes_anysz(),
__ptep_get_and_clear_anysz() and __ptep_set_access_flags_anysz(). In the
nVHE hypervisor, replace the "#if PAGE_SHIFT < 16" selection of the
fixblock slot with a HAS_FIXBLOCK C expression, so that
create_fixblock() and hyp_fixblock_map()/unmap() choose at run time
between the fixblock and the fixmap. Align __bss_pgtbl to PAGE_SIZE_MAX
so that statically allocated page tables are aligned for any selectable
page size.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit e39ee24baa88970efb2d8cc4f088715f28a6001f)
---
 arch/arm64/include/asm/hugetlb.h | 15 ++++++---------
 arch/arm64/include/asm/linkage.h |  2 +-
 arch/arm64/include/asm/pgtable.h | 24 ++++++++----------------
 arch/arm64/kvm/hyp/nvhe/mm.c     | 30 ++++++++++++++----------------
 arch/arm64/mm/fault.c            | 12 ++++--------
 5 files changed, 33 insertions(+), 50 deletions(-)

diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/hugetlb.h
index d038ff14d16c..b8487e8c9cf8 100644
--- a/arch/arm64/include/asm/hugetlb.h
+++ b/arch/arm64/include/asm/hugetlb.h
@@ -73,20 +73,17 @@ static inline void __flush_hugetlb_tlb_range(struct vm_area_struct *vma,
 					     unsigned long stride,
 					     tlbf_t flags)
 {
-	switch (stride) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
+	if (stride == PUD_SIZE) {
 		__flush_tlb_range(vma, start, end, PUD_SIZE, 1, flags);
-		break;
+		return;
+	}
 #endif
-	case CONT_PMD_SIZE:
-	case PMD_SIZE:
+	if (stride == CONT_PMD_SIZE || stride == PMD_SIZE) {
 		__flush_tlb_range(vma, start, end, PMD_SIZE, 2, flags);
-		break;
-	case CONT_PTE_SIZE:
+	} else if (stride == CONT_PTE_SIZE) {
 		__flush_tlb_range(vma, start, end, PAGE_SIZE, 3, flags);
-		break;
-	default:
+	} else {
 		__flush_tlb_range(vma, start, end, PAGE_SIZE, TLBI_TTL_UNKNOWN, flags);
 	}
 }
diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h
index d1f7a16729d2..2eb3c0f933e3 100644
--- a/arch/arm64/include/asm/linkage.h
+++ b/arch/arm64/include/asm/linkage.h
@@ -45,6 +45,6 @@
 
 #define _THIS_IP_ ({ unsigned long __ip; asm volatile("adr %0, ." : "=r" (__ip)); __ip; })
 
-#define __bss_pgtbl __section(".bss..pgtbl") __aligned(PAGE_SIZE)
+#define __bss_pgtbl __section(".bss..pgtbl") __aligned(PAGE_SIZE_MAX)
 
 #endif
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index c625fe7f8452..7cdff90a3b7e 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -635,21 +635,17 @@ static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,
 {
 	unsigned long stride = pgsize >> PAGE_SHIFT;
 
-	switch (pgsize) {
-	case PAGE_SIZE:
+	if (pgsize == PAGE_SIZE) {
 		page_table_check_ptes_set(mm, addr, ptep, pte, nr);
-		break;
-	case PMD_SIZE:
+	} else if (pgsize == PMD_SIZE) {
 		page_table_check_pmds_set(mm, addr, (pmd_t *)ptep,
 					  pte_pmd(pte), nr);
-		break;
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
+	} else if (pgsize == PUD_SIZE) {
 		page_table_check_puds_set(mm, addr, (pud_t *)ptep,
 					  pte_pud(pte), nr);
-		break;
 #endif
-	default:
+	} else {
 		VM_WARN_ON(1);
 	}
 
@@ -1360,19 +1356,15 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,
 {
 	pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0));
 
-	switch (pgsize) {
-	case PAGE_SIZE:
+	if (pgsize == PAGE_SIZE) {
 		page_table_check_pte_clear(mm, address, pte);
-		break;
-	case PMD_SIZE:
+	} else if (pgsize == PMD_SIZE) {
 		page_table_check_pmd_clear(mm, address, pte_pmd(pte));
-		break;
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
+	} else if (pgsize == PUD_SIZE) {
 		page_table_check_pud_clear(mm, address, pte_pud(pte));
-		break;
 #endif
-	default:
+	} else {
 		VM_WARN_ON(1);
 	}
 
diff --git a/arch/arm64/kvm/hyp/nvhe/mm.c b/arch/arm64/kvm/hyp/nvhe/mm.c
index 83b74c5223c3..ede688d8a5f1 100644
--- a/arch/arm64/kvm/hyp/nvhe/mm.c
+++ b/arch/arm64/kvm/hyp/nvhe/mm.c
@@ -391,15 +391,12 @@ static int create_fixmap_slot(u64 addr, u64 cpu)
 	return kvm_pgtable_walk(&pkvm_pgtable, addr, PAGE_SIZE, &walker);
 }
 
-#if PAGE_SHIFT < 16
-#define HAS_FIXBLOCK
+#define HAS_FIXBLOCK	(PAGE_SHIFT < 16)
 static struct hyp_fixmap_slot hyp_fixblock_slot;
 static DEFINE_HYP_SPINLOCK(hyp_fixblock_lock);
-#endif
 
 static int create_fixblock(void)
 {
-#ifdef HAS_FIXBLOCK
 	struct kvm_pgtable_walker walker = {
 		.cb	= __create_fixmap_slot_cb,
 		.flags	= KVM_PGTABLE_WALK_LEAF,
@@ -409,6 +406,9 @@ static int create_fixblock(void)
 	phys_addr_t phys;
 	int ret, i;
 
+	if (!HAS_FIXBLOCK)
+		return 0;
+
 	/* Find a RAM phys address, PMD aligned */
 	for (i = 0; i < hyp_memblock_nr; i++) {
 		phys = ALIGN(hyp_memory[i].base, PMD_SIZE);
@@ -435,31 +435,29 @@ static int create_fixblock(void)
 	hyp_spin_unlock(&pkvm_pgd_lock);
 
 	return ret;
-#else
-	return 0;
-#endif
 }
 
 void *hyp_fixblock_map(phys_addr_t phys, size_t *size)
 {
-#ifdef HAS_FIXBLOCK
+	if (!HAS_FIXBLOCK) {
+		*size = PAGE_SIZE;
+		return hyp_fixmap_map(phys);
+	}
+
 	*size = PMD_SIZE;
 	hyp_spin_lock(&hyp_fixblock_lock);
 	return fixmap_map_slot(&hyp_fixblock_slot, phys) + offset_in_page(phys);
-#else
-	*size = PAGE_SIZE;
-	return hyp_fixmap_map(phys);
-#endif
 }
 
 void hyp_fixblock_unmap(void)
 {
-#ifdef HAS_FIXBLOCK
+	if (!HAS_FIXBLOCK) {
+		hyp_fixmap_unmap();
+		return;
+	}
+
 	fixmap_clear_slot(&hyp_fixblock_slot);
 	hyp_spin_unlock(&hyp_fixblock_lock);
-#else
-	hyp_fixmap_unmap();
-#endif
 }
 
 int hyp_create_fixmap(void)
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index b22736853fcd..0880f6456b49 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -253,19 +253,15 @@ int __ptep_set_access_flags_anysz(struct vm_area_struct *vma,
 	 * flush_tlb_fix_spurious_fault().
 	 */
 	if (dirty) {
-		switch (pgsize) {
-		case PAGE_SIZE:
+		if (pgsize == PAGE_SIZE) {
 			level = 3;
-			break;
-		case PMD_SIZE:
+		} else if (pgsize == PMD_SIZE) {
 			level = 2;
-			break;
 #ifndef __PAGETABLE_PMD_FOLDED
-		case PUD_SIZE:
+		} else if (pgsize == PUD_SIZE) {
 			level = 1;
-			break;
 #endif
-		default:
+		} else {
 			level = TLBI_TTL_UNKNOWN;
 			WARN_ON(1);
 		}


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 37/43] vdso: Lay out the data pages in VDSO_PAGE_SIZE units
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (31 preceding siblings ...)
  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 ` 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
                   ` (5 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Eric Curtin, Andy Lutomirski, Thomas Gleixner, Vincenzo Frascino,
	linux-kernel

From: Eric Curtin <eric.curtin@docker.com>

The vDSO is built with CONFIG_PAGE_SHIFT, so the data page layout it
uses can differ from the runtime PAGE_SIZE when the page size is chosen
at boot. The kernel side must lay out and map the data pages exactly as
the vDSO code expects.

Add VDSO_PAGE_SHIFT and VDSO_PAGE_SIZE, defined from CONFIG_PAGE_SHIFT,
and use them for the vDSO data layout: the VDSO_ARCH_DATA_* sizes, the
vdso_u_* symbol offsets, vdso_initdata, the vvar mapping length, and the
timens page offsets in gettimeofday.c. Indexes of the vDSO data pages
are converted to PAGE_SIZE page offsets with the new VDSO_PGOFF() macro
when addressing vdso_data_pages, and vvar_fault() shifts the faulting
page offset back to a vDSO data page index. The static assertions on
the size of struct vdso_time_data and struct vdso_rng_data use
PAGE_SIZE_MIN.

For a compile-time page size VDSO_PAGE_SIZE equals PAGE_SIZE and the
layout does not change.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit a870d11aaf0fbcb9e4c7d19f8169832cc676b685)
---
 include/vdso/datapage.h | 10 +++++-----
 include/vdso/page.h     |  4 ++++
 lib/vdso/datastore.c    | 34 +++++++++++++++++++---------------
 lib/vdso/gettimeofday.c |  4 ++--
 4 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/include/vdso/datapage.h b/include/vdso/datapage.h
index 09897f76ae07..d999f10032b6 100644
--- a/include/vdso/datapage.h
+++ b/include/vdso/datapage.h
@@ -164,8 +164,8 @@ extern struct vdso_time_data *vdso_k_time_data;
 extern struct vdso_rng_data *vdso_k_rng_data;
 extern struct vdso_arch_data *vdso_k_arch_data;
 
-#define VDSO_ARCH_DATA_SIZE ALIGN(sizeof(struct vdso_arch_data), PAGE_SIZE)
-#define VDSO_ARCH_DATA_PAGES (VDSO_ARCH_DATA_SIZE >> PAGE_SHIFT)
+#define VDSO_ARCH_DATA_SIZE ALIGN(sizeof(struct vdso_arch_data), VDSO_PAGE_SIZE)
+#define VDSO_ARCH_DATA_PAGES (VDSO_ARCH_DATA_SIZE >> VDSO_PAGE_SHIFT)
 
 enum vdso_pages {
 	VDSO_TIME_PAGE_OFFSET,
@@ -179,19 +179,19 @@ enum vdso_pages {
 #else /* !__ASSEMBLER__ */
 
 #ifdef CONFIG_VDSO_GETRANDOM
-#define __vdso_u_rng_data	PROVIDE(vdso_u_rng_data = vdso_u_data + 2 * PAGE_SIZE);
+#define __vdso_u_rng_data	PROVIDE(vdso_u_rng_data = vdso_u_data + 2 * VDSO_PAGE_SIZE);
 #else
 #define __vdso_u_rng_data
 #endif
 
 #ifdef CONFIG_ARCH_HAS_VDSO_ARCH_DATA
-#define __vdso_u_arch_data	PROVIDE(vdso_u_arch_data = vdso_u_data + 3 * PAGE_SIZE);
+#define __vdso_u_arch_data	PROVIDE(vdso_u_arch_data = vdso_u_data + 3 * VDSO_PAGE_SIZE);
 #else
 #define __vdso_u_arch_data
 #endif
 
 #define VDSO_VVAR_SYMS						\
-	PROVIDE(vdso_u_data = . - __VDSO_PAGES * PAGE_SIZE);	\
+	PROVIDE(vdso_u_data = . - __VDSO_PAGES * VDSO_PAGE_SIZE);\
 	PROVIDE(vdso_u_time_data = vdso_u_data);		\
 	__vdso_u_rng_data					\
 	__vdso_u_arch_data					\
diff --git a/include/vdso/page.h b/include/vdso/page.h
index bc47186c07fc..b79e1c8c8363 100644
--- a/include/vdso/page.h
+++ b/include/vdso/page.h
@@ -14,6 +14,10 @@
 
 #define PAGE_SIZE	(_AC(1,UL) << CONFIG_PAGE_SHIFT)
 
+/* Granule of the vDSO data layout; constant even with boot-time PAGE_SIZE. */
+#define VDSO_PAGE_SHIFT	CONFIG_PAGE_SHIFT
+#define VDSO_PAGE_SIZE	(_AC(1,UL) << VDSO_PAGE_SHIFT)
+
 #if !defined(CONFIG_64BIT)
 /*
  * Applies only to 32-bit architectures.
diff --git a/lib/vdso/datastore.c b/lib/vdso/datastore.c
index 1426bf4e0c12..74ad8d71f6c8 100644
--- a/lib/vdso/datastore.c
+++ b/lib/vdso/datastore.c
@@ -8,32 +8,35 @@
 #include <linux/vdso_datastore.h>
 #include <vdso/datapage.h>
 
-static u8 vdso_initdata[VDSO_NR_PAGES * PAGE_SIZE] __aligned(PAGE_SIZE) __initdata = {};
+/* Convert a vDSO data page index to a PAGE_SIZE page offset. */
+#define VDSO_PGOFF(i)	((unsigned long)(i) << (VDSO_PAGE_SHIFT - PAGE_SHIFT))
+
+static u8 vdso_initdata[VDSO_NR_PAGES * VDSO_PAGE_SIZE] __aligned(VDSO_PAGE_SIZE) __initdata = {};
 
 #ifdef CONFIG_GENERIC_GETTIMEOFDAY
 struct vdso_time_data *vdso_k_time_data __ro_after_init =
-	(void *)&vdso_initdata[VDSO_TIME_PAGE_OFFSET * PAGE_SIZE];
+	(void *)&vdso_initdata[VDSO_TIME_PAGE_OFFSET * VDSO_PAGE_SIZE];
 
-static_assert(sizeof(struct vdso_time_data) <= PAGE_SIZE);
+static_assert(sizeof(struct vdso_time_data) <= PAGE_SIZE_MIN);
 #endif /* CONFIG_GENERIC_GETTIMEOFDAY */
 
 #ifdef CONFIG_VDSO_GETRANDOM
 struct vdso_rng_data *vdso_k_rng_data __ro_after_init =
-	(void *)&vdso_initdata[VDSO_RNG_PAGE_OFFSET * PAGE_SIZE];
+	(void *)&vdso_initdata[VDSO_RNG_PAGE_OFFSET * VDSO_PAGE_SIZE];
 
-static_assert(sizeof(struct vdso_rng_data) <= PAGE_SIZE);
+static_assert(sizeof(struct vdso_rng_data) <= PAGE_SIZE_MIN);
 #endif /* CONFIG_VDSO_GETRANDOM */
 
 #ifdef CONFIG_ARCH_HAS_VDSO_ARCH_DATA
 struct vdso_arch_data *vdso_k_arch_data __ro_after_init =
-	(void *)&vdso_initdata[VDSO_ARCH_PAGES_START * PAGE_SIZE];
+	(void *)&vdso_initdata[VDSO_ARCH_PAGES_START * VDSO_PAGE_SIZE];
 #endif /* CONFIG_ARCH_HAS_VDSO_ARCH_DATA */
 
 static struct page *vdso_data_pages __ro_after_init;
 
 void __init vdso_setup_data_pages(void)
 {
-	unsigned int order = get_order(VDSO_NR_PAGES * PAGE_SIZE);
+	unsigned int order = get_order(VDSO_NR_PAGES * VDSO_PAGE_SIZE);
 
 	/*
 	 * Allocate the data pages dynamically. SPARC does not support mapping
@@ -51,16 +54,16 @@ void __init vdso_setup_data_pages(void)
 	split_page(vdso_data_pages, order);
 
 	/* Move the data already written by other subsystems to the new pages */
-	memcpy(page_address(vdso_data_pages), vdso_initdata, VDSO_NR_PAGES * PAGE_SIZE);
+	memcpy(page_address(vdso_data_pages), vdso_initdata, VDSO_NR_PAGES * VDSO_PAGE_SIZE);
 
 	if (IS_ENABLED(CONFIG_GENERIC_GETTIMEOFDAY))
-		vdso_k_time_data = page_address(vdso_data_pages + VDSO_TIME_PAGE_OFFSET);
+		vdso_k_time_data = page_address(vdso_data_pages + VDSO_PGOFF(VDSO_TIME_PAGE_OFFSET));
 
 	if (IS_ENABLED(CONFIG_VDSO_GETRANDOM))
-		vdso_k_rng_data = page_address(vdso_data_pages + VDSO_RNG_PAGE_OFFSET);
+		vdso_k_rng_data = page_address(vdso_data_pages + VDSO_PGOFF(VDSO_RNG_PAGE_OFFSET));
 
 	if (IS_ENABLED(CONFIG_ARCH_HAS_VDSO_ARCH_DATA))
-		vdso_k_arch_data = page_address(vdso_data_pages + VDSO_ARCH_PAGES_START);
+		vdso_k_arch_data = page_address(vdso_data_pages + VDSO_PGOFF(VDSO_ARCH_PAGES_START));
 }
 
 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
@@ -74,7 +77,8 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
 	page = vdso_data_pages + vmf->pgoff;
 	timens_page = find_timens_vvar_page(vma);
 
-	switch (vmf->pgoff) {
+	/* Only the first page of each vDSO data page holds data. */
+	switch (vmf->pgoff >> (VDSO_PAGE_SHIFT - PAGE_SHIFT)) {
 	case VDSO_TIME_PAGE_OFFSET:
 		if (!IS_ENABLED(CONFIG_GENERIC_GETTIMEOFDAY) || !timens_page)
 			break;
@@ -85,7 +89,7 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
 		unsigned long addr;
 		vm_fault_t err;
 
-		addr = vmf->address + VDSO_TIMENS_PAGE_OFFSET * PAGE_SIZE;
+		addr = vmf->address + VDSO_TIMENS_PAGE_OFFSET * VDSO_PAGE_SIZE;
 		err = vmf_insert_page(vma, addr, page);
 		if (unlikely(err & VM_FAULT_ERROR))
 			return err;
@@ -101,7 +105,7 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
 		 */
 		if (!IS_ENABLED(CONFIG_TIME_NS) || !timens_page)
 			break;
-		page = vdso_data_pages + VDSO_TIME_PAGE_OFFSET;
+		page = vdso_data_pages + VDSO_PGOFF(VDSO_TIME_PAGE_OFFSET);
 		break;
 	case VDSO_RNG_PAGE_OFFSET:
 	case VDSO_ARCH_PAGES_START ... VDSO_ARCH_PAGES_END:
@@ -122,7 +126,7 @@ const struct vm_special_mapping vdso_vvar_mapping = {
 
 struct vm_area_struct *vdso_install_vvar_mapping(struct mm_struct *mm, unsigned long addr)
 {
-	return _install_special_mapping(mm, addr, VDSO_NR_PAGES * PAGE_SIZE,
+	return _install_special_mapping(mm, addr, VDSO_NR_PAGES * VDSO_PAGE_SIZE,
 					VM_READ | VM_MAYREAD | VM_DONTDUMP |
 					VM_MIXEDMAP | VM_SEALED_SYSMAP,
 					&vdso_vvar_mapping);
diff --git a/lib/vdso/gettimeofday.c b/lib/vdso/gettimeofday.c
index a324b2f2ec6c..e3ea7df41fe4 100644
--- a/lib/vdso/gettimeofday.c
+++ b/lib/vdso/gettimeofday.c
@@ -136,13 +136,13 @@ bool vdso_get_timestamp(const struct vdso_time_data *vd, const struct vdso_clock
 static __always_inline
 const struct vdso_time_data *vdso_timens_data(const struct vdso_time_data *vd)
 {
-	return (void *)vd + PAGE_SIZE;
+	return (void *)vd + VDSO_PAGE_SIZE;
 }
 
 static __always_inline
 const struct vdso_clock *vdso_timens_clock(const struct vdso_clock *vc)
 {
-	return (void *)vc + PAGE_SIZE;
+	return (void *)vc + VDSO_PAGE_SIZE;
 }
 
 static __always_inline


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 38/43] mm: Use fixed sizes for shmem str_dup and swap cache PFN bits
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (32 preceding siblings ...)
  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 ` 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
                   ` (4 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Eric Curtin, Hugh Dickins, David Hildenbrand, Baolin Wang,
	Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
	Baoquan He, Barry Song, Youngjun Park, linux-mm

From: Eric Curtin <eric.curtin@docker.com>

str_dup in mm/shmem.c is a static buffer sized with PAGE_SIZE, and
SWAP_CACHE_PFN_BITS in mm/swap.h is computed from PAGE_SHIFT. Neither is
a compile-time constant with arm64 boot-time page size selection.

Size str_dup as SZ_4K, along with the matching length check in
setup_thp_shmem(), which is large enough for the command line option
that it copies. Compute SWAP_CACHE_PFN_BITS with PAGE_SHIFT_MIN, as the
smallest page size needs the most PFN bits.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 54043747407a2dc56dcd60bc2b1f29ce193ee804)
---
 mm/shmem.c | 4 ++--
 mm/swap.h  | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index a5713109c843..757b0906caae 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -5942,7 +5942,7 @@ static int __init setup_transparent_hugepage_tmpfs(char *str)
 }
 __setup("transparent_hugepage_tmpfs=", setup_transparent_hugepage_tmpfs);
 
-static char str_dup[PAGE_SIZE] __initdata;
+static char str_dup[SZ_4K] __initdata;
 static int __init setup_thp_shmem(char *str)
 {
 	char *token, *range, *policy, *subtoken;
@@ -5951,7 +5951,7 @@ static int __init setup_thp_shmem(char *str)
 	int start, end, nr;
 	char *p;
 
-	if (!str || strlen(str) + 1 > PAGE_SIZE)
+	if (!str || strlen(str) + 1 > SZ_4K)
 		goto err;
 	strscpy(str_dup, str);
 
diff --git a/mm/swap.h b/mm/swap.h
index d5bf21f517dc..6dab19c1fac9 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -12,12 +12,13 @@ struct swap_iocb;
 struct swap_memcg_table;
 struct swap_io_ctx;
 
+/* Worst case (most PFN bits) for boot-time page size. */
 #if defined(MAX_POSSIBLE_PHYSMEM_BITS)
-#define SWAP_CACHE_PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
+#define SWAP_CACHE_PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT_MIN)
 #elif defined(MAX_PHYSMEM_BITS)
-#define SWAP_CACHE_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
+#define SWAP_CACHE_PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT_MIN)
 #else
-#define SWAP_CACHE_PFN_BITS (BITS_PER_LONG - PAGE_SHIFT)
+#define SWAP_CACHE_PFN_BITS (BITS_PER_LONG - PAGE_SHIFT_MIN)
 #endif
 
 /* Swap table marker, 0x1 means shadow, 0x2 means PFN (SWP_TB_PFN_MARK) */


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 39/43] crash: Size the vmcoreinfo page array for the smallest page size
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (33 preceding siblings ...)
  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 ` 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
                   ` (3 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Eric Curtin, Andrew Morton, Baoquan He, Mike Rapoport,
	Pasha Tatashin, Pratyush Yadav, linux-kernel, kexec

From: Eric Curtin <eric.curtin@docker.com>

kimage_crash_copy_vmcoreinfo() keeps the vmcoreinfo pages in an on-stack
array whose length is computed from VMCOREINFO_BYTES and PAGE_SIZE, both
of which are runtime values with arm64 boot-time page size selection.

Add VMCOREINFO_BYTES_MAX, defined as PAGE_SIZE_MAX, and size the array
as DIV_ROUND_UP(VMCOREINFO_BYTES_MAX, PAGE_SIZE_MIN), which is the
largest number of pages the buffer can span.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 08a51916386a159aeb15a6e51605740a895b7bb5)
---
 include/linux/vmcore_info.h | 1 +
 kernel/crash_core.c         | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/vmcore_info.h b/include/linux/vmcore_info.h
index e71518caacdf..f5d49e71ac7b 100644
--- a/include/linux/vmcore_info.h
+++ b/include/linux/vmcore_info.h
@@ -21,6 +21,7 @@
 				     CRASH_CORE_NOTE_DESC_BYTES)
 
 #define VMCOREINFO_BYTES	   PAGE_SIZE
+#define VMCOREINFO_BYTES_MAX	   PAGE_SIZE_MAX
 #define VMCOREINFO_NOTE_NAME	   "VMCOREINFO"
 #define VMCOREINFO_NOTE_NAME_BYTES ALIGN(sizeof(VMCOREINFO_NOTE_NAME), 4)
 #define VMCOREINFO_NOTE_SIZE	   ((CRASH_CORE_NOTE_HEAD_BYTES * 2) +	\
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index c7eb72748fee..ca7252fda848 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -43,7 +43,7 @@ note_buf_t __percpu *crash_notes;
 int kimage_crash_copy_vmcoreinfo(struct kimage *image)
 {
 	struct page *vmcoreinfo_base;
-	struct page *vmcoreinfo_pages[DIV_ROUND_UP(VMCOREINFO_BYTES, PAGE_SIZE)];
+	struct page *vmcoreinfo_pages[DIV_ROUND_UP(VMCOREINFO_BYTES_MAX, PAGE_SIZE_MIN)];
 	unsigned int order, nr_pages;
 	int i;
 	void *safecopy;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 40/43] arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (34 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:52 ` [RFC PATCH v2 41/43] exit: Use THREAD_SIZE_MAX for lowest_to_date Eric Curtin
                   ` (2 subsequent siblings)
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Eric Curtin, Catalin Marinas, Will Deacon, Mark Rutland,
	Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Andrey Ryabinin,
	Alexander Potapenko, Andrey Konovalov, Dmitry Vyukov,
	Vincenzo Frascino, kvmarm, kasan-dev

From: Eric Curtin <eric.curtin@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@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;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 41/43] exit: Use THREAD_SIZE_MAX for lowest_to_date
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (35 preceding siblings ...)
  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 ` 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
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel; +Cc: Eric Curtin, linux-kernel

From: Eric Curtin <eric.curtin@docker.com>

With arm64 boot-time page size selection THREAD_SIZE may depend on the
page size and is not a compile-time constant, so it cannot initialise
the static lowest_to_date in check_stack_usage().

Use THREAD_SIZE_MAX, which is an upper bound on the stack size and so a
valid initial value for the lowest free stack seen so far.

No functional change for compile-time page sizes.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit e4b475ad2b5c70b4fa666356ca309ed3ec0528ed)
---
 kernel/exit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/exit.c b/kernel/exit.c
index a8453363d1fd..4a7d792f69ed 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -847,7 +847,7 @@ static inline void kstack_histogram(unsigned long used_stack)
 static void check_stack_usage(void)
 {
 	static DEFINE_SPINLOCK(low_water_lock);
-	static int lowest_to_date = THREAD_SIZE;
+	static int lowest_to_date = THREAD_SIZE_MAX;
 	unsigned long free;
 
 	free = stack_not_used(current);


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 42/43] tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (36 preceding siblings ...)
  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 ` Eric Curtin
  2026-12-01  1:52 ` [RFC PATCH v2 43/43] arm64: Enable boot-time page size selection Eric Curtin
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Eric Curtin, Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	linux-kernel, linux-trace-kernel

From: Eric Curtin <eric.curtin@docker.com>

With arm64 boot-time page size selection PAGE_SIZE is not a compile-time
constant, so it cannot be used in BUILD_BUG_ON() or as an array size or
alignment.

In trace_uprobe.c, MAX_UCB_BUFFER_SIZE is PAGE_SIZE, so the build check
against MAX_PROBE_EVENT_SIZE now uses PAGE_SIZE_MIN directly. In
undefsyms_base.c, size and align the static page[] with PAGE_SIZE_MAX;
the memset() keeps using the run-time PAGE_SIZE.

No functional change for compile-time page sizes.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit f68b4569af533a62d3da83ffb3141256868d1c70)
---
 kernel/trace/trace_uprobe.c   | 2 +-
 kernel/trace/undefsyms_base.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c
index fb7991543db7..d7ae99294bd0 100644
--- a/kernel/trace/trace_uprobe.c
+++ b/kernel/trace/trace_uprobe.c
@@ -1019,7 +1019,7 @@ static struct uprobe_cpu_buffer *prepare_uprobe_buffer(struct trace_uprobe *tu,
 	ucb = uprobe_buffer_get();
 	ucb->dsize = tu->tp.size + dsize;
 
-	BUILD_BUG_ON(MAX_UCB_BUFFER_SIZE < MAX_PROBE_EVENT_SIZE);
+	BUILD_BUG_ON(PAGE_SIZE_MIN < MAX_PROBE_EVENT_SIZE);
 	if (WARN_ON_ONCE(ucb->dsize > MAX_UCB_BUFFER_SIZE)) {
 		ucb->dsize = MAX_UCB_BUFFER_SIZE;
 		dsize = MAX_UCB_BUFFER_SIZE - tu->tp.size;
diff --git a/kernel/trace/undefsyms_base.c b/kernel/trace/undefsyms_base.c
index 37310baf706b..01b4f1913d61 100644
--- a/kernel/trace/undefsyms_base.c
+++ b/kernel/trace/undefsyms_base.c
@@ -14,13 +14,14 @@
 
 void undefsyms_base(void *p, int n);
 
-static char page[PAGE_SIZE] __aligned(PAGE_SIZE);
+static char page[PAGE_SIZE_MAX] __aligned(PAGE_SIZE_MAX);
 
 void undefsyms_base(void *p, int n)
 {
 	char buffer[256] = { 0 };
 	u32 u = 0;
 
+	/* PAGE_SIZE may be a variable, which the hypervisor provides */
 	memset((char * volatile)page, 8, PAGE_SIZE);
 	memset((char * volatile)buffer, 8, sizeof(buffer));
 	memcpy((void * volatile)p, buffer, sizeof(buffer));


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* [RFC PATCH v2 43/43] arm64: Enable boot-time page size selection
  2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
                   ` (37 preceding siblings ...)
  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 ` Eric Curtin
  38 siblings, 0 replies; 40+ messages in thread
From: Eric Curtin @ 2026-12-01  1:52 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Ryan Roberts, Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Catalin Marinas, Will Deacon, Mark Rutland, Marc Zyngier,
	Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Muchun Song, Mike Rapoport,
	David Hildenbrand, Ard Biesheuvel, Ilias Apalodimas, linux-doc,
	kvmarm, linux-mm, linux-kernel, linux-efi

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

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be
selected instead of a fixed page size. When selected, the page size (4K,
16K or 64K) is chosen at boot with the arm64.pagesize= command line
option, falling back to ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT and then to
the largest size the CPU supports.

PAGE_SHIFT, PxD_SHIFT and the associated macros resolve to the ptg_*
variables, which are set by ptg_init() in early_map_kernel() before the
kernel is mapped. PTRS_PER_PMD, PTRS_PER_PUD and PTRS_PER_P4D, and
pgtable_l3/l4/l5_enabled(), are derived from ptg_pgtable_levels, and
folded levels are handled at run time. The variables are also exported
to the EL2 code via kvm_nvhe_sym() and to the position-independent code
via PI_EXPORT_SYM(). Because they are needed by modules, export them
with EXPORT_SYMBOL(), following powerpc and using the non-GPL variant.

Limitations: only 48-bit VA is supported for now, since more work is
required for LPA2. MMAP_RND_BITS, SECTION_SIZE_BITS and
ARCH_FORCE_MAX_ORDER are configured for the worst case (64K pages); they
could be made boot-time values later. The image header reports an
unspecified page size, and the EFI stub no longer checks for 16K or 64K
support. Add documentation for the new option and the header flag.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 38c7974ea05c55b9583b12510f903a3c64968869)
---
 .../admin-guide/kernel-parameters.txt         | 11 ++++
 Documentation/arch/arm64/booting.rst          |  4 +-
 Documentation/arch/arm64/memory.rst           | 21 ++++++++
 arch/arm64/Kconfig                            | 54 +++++++++++++++++--
 arch/arm64/include/asm/kvm_hyp.h              | 11 ++++
 arch/arm64/include/asm/pgtable-geometry.h     | 22 +++++++-
 arch/arm64/include/asm/pgtable-hwdef.h        |  9 ++--
 arch/arm64/include/asm/pgtable.h              | 10 +++-
 arch/arm64/include/asm/sparsemem.h            |  4 ++
 arch/arm64/kernel/image-vars.h                | 11 ++++
 arch/arm64/kernel/image.h                     |  4 ++
 arch/arm64/kernel/pi/map_kernel.c             | 45 ++++++++++++++++
 arch/arm64/kvm/arm.c                          | 10 ++++
 arch/arm64/kvm/hyp/nvhe/Makefile              |  1 +
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    | 16 ++++++
 arch/arm64/mm/Makefile                        |  1 +
 arch/arm64/mm/pgd.c                           | 10 ++--
 arch/arm64/mm/pgtable-geometry.c              | 32 +++++++++++
 drivers/firmware/efi/libstub/arm64.c          |  3 +-
 19 files changed, 262 insertions(+), 17 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c
 create mode 100644 arch/arm64/mm/pgtable-geometry.c

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index dce9b8f3ae98..fce8dbfad329 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -589,6 +589,17 @@ Kernel parameters
 	arm64.nowfxt    [ARM64] Unconditionally disable Wait For Event with
 			Timeout and Wait For Interrupt with Timeout support
 
+	arm64.pagesize=	[ARM64] Select the page size (translation granule)
+			of a kernel built with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE.
+			Format: { 4k | 16k | 64k }
+			If the option is absent, invalid, or names a size that
+			the CPU does not support, the size configured by
+			CONFIG_ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT is used,
+			or the largest size the CPU supports if it does not
+			support that either. Ignored by kernels built for a
+			fixed page size. See
+			Documentation/arch/arm64/memory.rst
+
 	ataflop=	[HW,M68k]
 
 	atkbd.extra=	[HW] Enable extra LEDs and keys on IBM RapidAccess,
diff --git a/Documentation/arch/arm64/booting.rst b/Documentation/arch/arm64/booting.rst
index dae5ff9fae26..2fe72b58b088 100644
--- a/Documentation/arch/arm64/booting.rst
+++ b/Documentation/arch/arm64/booting.rst
@@ -113,7 +113,9 @@ Header notes:
   Bit 0		Kernel endianness.  1 if BE, 0 if LE.
   Bit 1-2	Kernel Page size.
 
-			* 0 - Unspecified.
+			* 0 - Unspecified (always the case for images built
+			  with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE, see
+			  Documentation/arch/arm64/memory.rst).
 			* 1 - 4K
 			* 2 - 16K
 			* 3 - 64K
diff --git a/Documentation/arch/arm64/memory.rst b/Documentation/arch/arm64/memory.rst
index 678fbb418c3a..8207096bc7e7 100644
--- a/Documentation/arch/arm64/memory.rst
+++ b/Documentation/arch/arm64/memory.rst
@@ -33,6 +33,27 @@ ARM64_SPECTRE_V3A is enabled for particular CPUs.
 When using KVM with the Virtualization Host Extensions, no additional
 mappings are created, since the host kernel runs directly in EL2.
 
+Boot-time page size selection
+-----------------------------
+An image built with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE supports all of the 4KB,
+16KB and 64KB page sizes and chooses one at boot with the ``arm64.pagesize=``
+kernel command line option (``4k``, ``16k`` or ``64k``). Kernels built with a
+fixed page size are unaffected. Such an image reports "Unspecified" in the page
+size field of the Image header flags, so a boot loader must not rely on it, and
+it is limited to 48-bit virtual and physical addresses.
+
+The selected granule must be implemented by the CPU (ID_AA64MMFR0_EL1.TGRAN4,
+TGRAN16 and TGRAN64). The kernel checks this itself, and when entered with the
+MMU on (for example from the EFI stub) it switches to the selected granule
+early in boot, so firmware does not need to use the same one. If the option is
+absent, invalid, or names a granule the CPU lacks, the kernel falls back to
+CONFIG_ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT, and then to the largest granule the
+CPU supports. The CPU is parked if it implements none of them.
+
+State that depends on the page size, such as swap, hibernation images and
+kexec handover data, can only be reused by a kernel running with the same
+page size.
+
 52-bit VA support in the kernel
 -------------------------------
 If the ARMv8.2-LVA optional feature is present, and we are running
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 9b08b2457a55..36dab5543e75 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -110,6 +110,7 @@ config ARM64
 	select BUILDTIME_TABLE_SORT
 	select CLONE_BACKWARDS
 	select COMMON_CLK
+	select CONSTRUCTORS if ARM64_BOOT_TIME_PAGE_SIZE
 	select CPU_PM if (SUSPEND || CPU_IDLE)
 	select CPUMASK_OFFSTACK if NR_CPUS > 256
 	select DCACHE_WORD_ACCESS
@@ -280,12 +281,14 @@ config MMU
 
 config ARM64_CONT_PTE_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 7 if PAGE_SIZE_16KB
 	default 4
 
 config ARM64_CONT_PMD_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 5 if PAGE_SIZE_16KB
 	default 4
@@ -302,10 +305,10 @@ config ARCH_MMAP_RND_BITS_MAX
 	default 24 if ARM64_VA_BITS=39
 	default 27 if ARM64_VA_BITS=42
 	default 30 if ARM64_VA_BITS=47
-	default 29 if (ARM64_VA_BITS=48 || ARM64_VA_BITS=52) && ARM64_64K_PAGES
+	default 29 if (ARM64_VA_BITS=48 || ARM64_VA_BITS=52) && PAGE_SIZE_64KB
 	default 31 if (ARM64_VA_BITS=48 || ARM64_VA_BITS=52) && ARM64_16K_PAGES
 	default 33 if (ARM64_VA_BITS=48 || ARM64_VA_BITS=52)
-	default 14 if ARM64_64K_PAGES
+	default 14 if PAGE_SIZE_64KB
 	default 16 if ARM64_16K_PAGES
 	default 18
 
@@ -318,7 +321,7 @@ config ARCH_MMAP_RND_BITS
 	default 28 if (ARM64_VA_BITS=48 || ARM64_VA_BITS=52)
 
 config ARCH_MMAP_RND_COMPAT_BITS_MIN
-	default 7 if ARM64_64K_PAGES
+	default 7 if PAGE_SIZE_64KB
 	default 9 if ARM64_16K_PAGES
 	default 11
 
@@ -366,6 +369,7 @@ config FIX_EARLYCON_MEM
 
 config PGTABLE_LEVELS
 	int
+	default 4 if ARM64_BOOT_TIME_PAGE_SIZE # Advertise max supported levels
 	default 2 if ARM64_16K_PAGES && ARM64_VA_BITS_36
 	default 2 if ARM64_64K_PAGES && ARM64_VA_BITS_42
 	default 3 if ARM64_64K_PAGES && (ARM64_VA_BITS_48 || ARM64_VA_BITS_52)
@@ -1598,8 +1602,46 @@ config ARM64_64K_PAGES
 	  look-up. AArch32 emulation requires applications compiled
 	  with 64K aligned segments.
 
+config ARM64_BOOT_TIME_PAGE_SIZE
+	bool "Boot-time selection"
+	select ARCH_RUNTIME_PMD_FOLD
+	select HAVE_PAGE_SIZE_64KB # Advertise largest page size to core
+	help
+	  Select desired page size (4KB, 16KB or 64KB) at boot-time via the
+	  kernel command line option "arm64.pagesize=4k", "arm64.pagesize=16k"
+	  or "arm64.pagesize=64k".
+
+	  Limitations: only 48-bit virtual and physical addresses are
+	  supported (no LPA/LPA2), and ARCH_WANT_HUGE_PMD_SHARE and
+	  ARCH_WANTS_THP_SWAP are not enabled.
+
 endchoice
 
+choice
+	prompt "Default page size for boot-time selection"
+	depends on ARM64_BOOT_TIME_PAGE_SIZE
+	default ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT_4K
+	help
+	  Page size to use when "arm64.pagesize=" is not given. If the CPU
+	  does not support it, the largest page size it supports is used.
+
+config ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT_4K
+	bool "4KB"
+
+config ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT_16K
+	bool "16KB"
+
+config ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT_64K
+	bool "64KB"
+
+endchoice
+
+config ARM64_BOOT_TIME_PAGE_SHIFT_DEFAULT
+	int
+	default 16 if ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT_64K
+	default 14 if ARM64_BOOT_TIME_PAGE_SIZE_DEFAULT_16K
+	default 12
+
 choice
 	prompt "Virtual address space size"
 	default ARM64_VA_BITS_52
@@ -1629,6 +1671,7 @@ config ARM64_VA_BITS_48
 
 config ARM64_VA_BITS_52
 	bool "52-bit"
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	help
 	  Enable 52-bit virtual addressing for userspace when explicitly
 	  requested via a hint to mmap(). The kernel will also use 52-bit
@@ -1828,9 +1871,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if PAGE_SIZE_64KB
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help
@@ -1889,7 +1933,7 @@ config ARM64_TAGGED_ADDR_ABI
 
 menuconfig COMPAT
 	bool "Kernel support for 32-bit EL0"
-	depends on ARM64_4K_PAGES || EXPERT
+	depends on ARM64_4K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE || EXPERT
 	select HAVE_UID16
 	select OLD_SIGSUSPEND3
 	select COMPAT_OLD_SIGACTION
diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h
index b0e42be3d9ee..1394dc2de129 100644
--- a/arch/arm64/include/asm/kvm_hyp.h
+++ b/arch/arm64/include/asm/kvm_hyp.h
@@ -161,4 +161,15 @@ extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
 extern unsigned long kvm_nvhe_sym(hyp_nr_cpus);
 extern unsigned int kvm_nvhe_sym(hyp_gicv3_nr_lr);
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+extern int kvm_nvhe_sym(ptg_page_shift);
+extern int kvm_nvhe_sym(ptg_pmd_shift);
+extern int kvm_nvhe_sym(ptg_pud_shift);
+extern int kvm_nvhe_sym(ptg_p4d_shift);
+extern int kvm_nvhe_sym(ptg_pgdir_shift);
+extern int kvm_nvhe_sym(ptg_cont_pte_shift);
+extern int kvm_nvhe_sym(ptg_cont_pmd_shift);
+extern int kvm_nvhe_sym(ptg_pgtable_levels);
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 #endif /* __ARM64_KVM_HYP_H__ */
diff --git a/arch/arm64/include/asm/pgtable-geometry.h b/arch/arm64/include/asm/pgtable-geometry.h
index 62fe125909c0..18a8c8d499ec 100644
--- a/arch/arm64/include/asm/pgtable-geometry.h
+++ b/arch/arm64/include/asm/pgtable-geometry.h
@@ -6,16 +6,33 @@
 #define ARM64_PAGE_SHIFT_16K	14
 #define ARM64_PAGE_SHIFT_64K	16
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+#define PAGE_SHIFT_MIN		ARM64_PAGE_SHIFT_4K
+#define PAGE_SHIFT_MAX		ARM64_PAGE_SHIFT_64K
+#else /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
 #define PAGE_SHIFT_MIN		CONFIG_PAGE_SHIFT
+#define PAGE_SHIFT_MAX		CONFIG_PAGE_SHIFT
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 #define PAGE_SIZE_MIN		(_AC(1, UL) << PAGE_SHIFT_MIN)
 #define PAGE_MASK_MIN		(~(PAGE_SIZE_MIN-1))
-
-#define PAGE_SHIFT_MAX		CONFIG_PAGE_SHIFT
 #define PAGE_SIZE_MAX		(_AC(1, UL) << PAGE_SHIFT_MAX)
 #define PAGE_MASK_MAX		(~(PAGE_SIZE_MAX-1))
 
 #include <asm-generic/pgtable-geometry.h>
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+#ifndef __ASSEMBLY__
+extern int ptg_page_shift;
+extern int ptg_pmd_shift;
+extern int ptg_pud_shift;
+extern int ptg_p4d_shift;
+extern int ptg_pgdir_shift;
+extern int ptg_cont_pte_shift;
+extern int ptg_cont_pmd_shift;
+extern int ptg_pgtable_levels;
+#endif /* __ASSEMBLY__ */
+#else /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
 #define ptg_page_shift		CONFIG_PAGE_SHIFT
 #define ptg_pmd_shift		ARM64_HW_PGTABLE_LEVEL_SHIFT(2)
 #define ptg_pud_shift		ARM64_HW_PGTABLE_LEVEL_SHIFT(1)
@@ -24,5 +41,6 @@
 #define ptg_cont_pte_shift	(CONFIG_ARM64_CONT_PTE_SHIFT + PAGE_SHIFT)
 #define ptg_cont_pmd_shift	(CONFIG_ARM64_CONT_PMD_SHIFT + PMD_SHIFT)
 #define ptg_pgtable_levels	CONFIG_PGTABLE_LEVELS
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
 
 #endif /* ASM_PGTABLE_GEOMETRY_H */
diff --git a/arch/arm64/include/asm/pgtable-hwdef.h b/arch/arm64/include/asm/pgtable-hwdef.h
index 02373c4ea474..21e9c8e76651 100644
--- a/arch/arm64/include/asm/pgtable-hwdef.h
+++ b/arch/arm64/include/asm/pgtable-hwdef.h
@@ -61,7 +61,8 @@
 #define PMD_SHIFT		ptg_pmd_shift
 #define PMD_SIZE		(_AC(1, UL) << PMD_SHIFT)
 #define PMD_MASK		(~(PMD_SIZE-1))
-#define PTRS_PER_PMD		(1 << PTDESC_TABLE_SHIFT)
+#define PTRS_PER_PMD		(ptg_pgtable_levels > 2 ? \
+				 (1 << PTDESC_TABLE_SHIFT) : 1)
 #define MAX_PTRS_PER_PMD	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
 #endif
 
@@ -72,7 +73,8 @@
 #define PUD_SHIFT		ptg_pud_shift
 #define PUD_SIZE		(_AC(1, UL) << PUD_SHIFT)
 #define PUD_MASK		(~(PUD_SIZE-1))
-#define PTRS_PER_PUD		(1 << PTDESC_TABLE_SHIFT)
+#define PTRS_PER_PUD		(ptg_pgtable_levels > 3 ? \
+				 (1 << PTDESC_TABLE_SHIFT) : 1)
 #define MAX_PTRS_PER_PUD	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
 #endif
 
@@ -80,7 +82,8 @@
 #define P4D_SHIFT		ptg_p4d_shift
 #define P4D_SIZE		(_AC(1, UL) << P4D_SHIFT)
 #define P4D_MASK		(~(P4D_SIZE-1))
-#define PTRS_PER_P4D		(1 << PTDESC_TABLE_SHIFT)
+#define PTRS_PER_P4D		(ptg_pgtable_levels > 4 ? \
+				 (1 << PTDESC_TABLE_SHIFT) : 1)
 #define MAX_PTRS_PER_P4D	(1 << (PAGE_SHIFT_MAX - PTDESC_ORDER))
 #endif
 
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index 7cdff90a3b7e..7fd4e6356297 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -822,7 +822,7 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
 
 static __always_inline bool pgtable_l3_enabled(void)
 {
-	return true;
+	return ptg_pgtable_levels > 2;
 }
 
 static inline bool mm_pmd_folded(const struct mm_struct *mm)
@@ -955,6 +955,8 @@ static inline bool pgtable_l3_enabled(void) { return false; }
 
 static __always_inline bool pgtable_l4_enabled(void)
 {
+	if (IS_ENABLED(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE))
+		return ptg_pgtable_levels > 3;
 	if (CONFIG_PGTABLE_LEVELS > 4 || !IS_ENABLED(CONFIG_ARM64_LPA2))
 		return true;
 	if (!alternative_has_cap_likely(ARM64_ALWAYS_BOOT))
@@ -1003,6 +1005,8 @@ static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr)
 	/* Ensure that 'p4dp' indexes a page table according to 'addr' */
 	VM_BUG_ON(((addr >> P4D_SHIFT) ^ ((u64)p4dp >> 3)) % PTRS_PER_P4D);
 
+	if (IS_ENABLED(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE))
+		return (pud_t *)p4dp;
 	return (pud_t *)PTR_ALIGN_DOWN(p4dp, PAGE_SIZE) + pud_index(addr);
 }
 
@@ -1082,6 +1086,8 @@ static inline bool pgtable_l4_enabled(void) { return false; }
 
 static __always_inline bool pgtable_l5_enabled(void)
 {
+	if (IS_ENABLED(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE))
+		return ptg_pgtable_levels > 4;
 	if (!alternative_has_cap_likely(ARM64_ALWAYS_BOOT))
 		return vabits_actual == VA_BITS;
 	return alternative_has_cap_unlikely(ARM64_HAS_VA52);
@@ -1128,6 +1134,8 @@ static inline p4d_t *pgd_to_folded_p4d(pgd_t *pgdp, unsigned long addr)
 	/* Ensure that 'pgdp' indexes a page table according to 'addr' */
 	VM_BUG_ON(((addr >> PGDIR_SHIFT) ^ ((u64)pgdp >> 3)) % PTRS_PER_PGD);
 
+	if (IS_ENABLED(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE))
+		return (p4d_t *)pgdp;
 	return (p4d_t *)PTR_ALIGN_DOWN(pgdp, PAGE_SIZE) + p4d_index(addr);
 }
 
diff --git a/arch/arm64/include/asm/sparsemem.h b/arch/arm64/include/asm/sparsemem.h
index 4148f2440831..1e196360ae70 100644
--- a/arch/arm64/include/asm/sparsemem.h
+++ b/arch/arm64/include/asm/sparsemem.h
@@ -20,6 +20,10 @@
  * entries could not be created for vmemmap mappings.
  * 16K follows 4K for simplicity.
  */
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+#define SECTION_SIZE_BITS	29
+#else
 #define SECTION_SIZE_BITS	(PAGE_SIZE == SZ_64K ? 29 : 27)
+#endif
 
 #endif
diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h
index 9c9215847d26..1774f75d0cb7 100644
--- a/arch/arm64/kernel/image-vars.h
+++ b/arch/arm64/kernel/image-vars.h
@@ -46,6 +46,17 @@ PROVIDE(__pi___memcpy			= __pi_memcpy);
 PROVIDE(__pi___memmove			= __pi_memmove);
 PROVIDE(__pi___memset			= __pi_memset);
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+PI_EXPORT_SYM(ptg_page_shift);
+PI_EXPORT_SYM(ptg_pmd_shift);
+PI_EXPORT_SYM(ptg_pud_shift);
+PI_EXPORT_SYM(ptg_p4d_shift);
+PI_EXPORT_SYM(ptg_pgdir_shift);
+PI_EXPORT_SYM(ptg_cont_pte_shift);
+PI_EXPORT_SYM(ptg_cont_pmd_shift);
+PI_EXPORT_SYM(ptg_pgtable_levels);
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 PI_EXPORT_SYM(id_aa64isar1_override);
 PI_EXPORT_SYM(id_aa64isar2_override);
 PI_EXPORT_SYM(id_aa64mmfr0_override);
diff --git a/arch/arm64/kernel/image.h b/arch/arm64/kernel/image.h
index 5c10ca773363..0cfa5bc5e90f 100644
--- a/arch/arm64/kernel/image.h
+++ b/arch/arm64/kernel/image.h
@@ -33,7 +33,11 @@
 
 #define __HEAD_FLAG_BE		ARM64_IMAGE_FLAG_LE
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+#define __HEAD_FLAG_PAGE_SIZE	0
+#else
 #define __HEAD_FLAG_PAGE_SIZE	((PAGE_SHIFT - 10) / 2)
+#endif
 
 #define __HEAD_FLAG_PHYS_BASE	1
 
diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c
index 16f12fa8c890..a74a17fbd0bd 100644
--- a/arch/arm64/kernel/pi/map_kernel.c
+++ b/arch/arm64/kernel/pi/map_kernel.c
@@ -260,6 +260,49 @@ static bool __init ng_mappings_allowed(void)
 	return true;
 }
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+static void __init ptg_init(int page_shift)
+{
+	ptg_pgtable_levels =
+		__ARM64_HW_PGTABLE_LEVELS(page_shift, CONFIG_ARM64_VA_BITS);
+
+	ptg_pgdir_shift = __ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift,
+				4 - ptg_pgtable_levels);
+
+	ptg_p4d_shift = ptg_pgtable_levels >= 5 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 0) :
+				ptg_pgdir_shift;
+
+	ptg_pud_shift = ptg_pgtable_levels >= 4 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 1) :
+				ptg_pgdir_shift;
+
+	ptg_pmd_shift = ptg_pgtable_levels >= 3 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 2) :
+				ptg_pgdir_shift;
+
+	ptg_page_shift = page_shift;
+
+	switch (page_shift) {
+	case ARM64_PAGE_SHIFT_64K:
+		ptg_cont_pte_shift = ptg_page_shift + 5;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 5;
+		break;
+	case ARM64_PAGE_SHIFT_16K:
+		ptg_cont_pte_shift = ptg_page_shift + 7;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 5;
+		break;
+	default: /* ARM64_PAGE_SHIFT_4K */
+		ptg_cont_pte_shift = ptg_page_shift + 4;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 4;
+	}
+}
+#else /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+static inline void ptg_init(int page_shift)
+{
+}
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 asmlinkage void __init early_map_kernel(u64 boot_status, phys_addr_t fdt)
 {
 	static char const chosen_str[] __initconst = "/chosen";
@@ -291,6 +334,8 @@ asmlinkage void __init early_map_kernel(u64 boot_status, phys_addr_t fdt)
 	if (!page_shift)
 		page_shift = early_page_shift;
 
+	ptg_init(page_shift);
+
 	if (va_bits > 48) {
 		u64 page_size = early_page_size(page_shift);
 
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index a148e80d0aef..37ccceda65ea 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -2847,6 +2847,16 @@ static void kvm_hyp_init_symbols(void)
 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+	kvm_nvhe_sym(ptg_page_shift) = ptg_page_shift;
+	kvm_nvhe_sym(ptg_pmd_shift) = ptg_pmd_shift;
+	kvm_nvhe_sym(ptg_pud_shift) = ptg_pud_shift;
+	kvm_nvhe_sym(ptg_p4d_shift) = ptg_p4d_shift;
+	kvm_nvhe_sym(ptg_pgdir_shift) = ptg_pgdir_shift;
+	kvm_nvhe_sym(ptg_cont_pte_shift) = ptg_cont_pte_shift;
+	kvm_nvhe_sym(ptg_cont_pmd_shift) = ptg_cont_pmd_shift;
+	kvm_nvhe_sym(ptg_pgtable_levels) = ptg_pgtable_levels;
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
 
 	kvm_nvhe_sym(spectre_v2_state) = arm64_get_spectre_v2_state();
 	kvm_nvhe_sym(spectre_v4_state) = arm64_get_spectre_v4_state();
diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile
index 56a7e2c05f73..af36e8ef2006 100644
--- a/arch/arm64/kvm/hyp/nvhe/Makefile
+++ b/arch/arm64/kvm/hyp/nvhe/Makefile
@@ -28,6 +28,7 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o
 hyp-obj-y += ../../../kernel/smccc-call.o
+hyp-obj-$(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE) += pgtable-geometry.o
 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 hyp-obj-$(CONFIG_NVHE_EL2_TRACING) += clock.o trace.o events.o
 hyp-obj-y += $(lib-objs)
diff --git a/arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c b/arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c
new file mode 100644
index 000000000000..17f807450a31
--- /dev/null
+++ b/arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 ARM Ltd.
+ */
+
+#include <linux/cache.h>
+#include <asm/pgtable-geometry.h>
+
+int ptg_page_shift __ro_after_init;
+int ptg_pmd_shift __ro_after_init;
+int ptg_pud_shift __ro_after_init;
+int ptg_p4d_shift __ro_after_init;
+int ptg_pgdir_shift __ro_after_init;
+int ptg_cont_pte_shift __ro_after_init;
+int ptg_cont_pmd_shift __ro_after_init;
+int ptg_pgtable_levels __ro_after_init;
diff --git a/arch/arm64/mm/Makefile b/arch/arm64/mm/Makefile
index c26489cf96cd..160ef2da4629 100644
--- a/arch/arm64/mm/Makefile
+++ b/arch/arm64/mm/Makefile
@@ -3,6 +3,7 @@ obj-y				:= dma-mapping.o extable.o fault.o init.o \
 				   cache.o copypage.o flush.o \
 				   ioremap.o mmap.o pgd.o mem_encrypt.o mmu.o \
 				   context.o proc.o pageattr.o fixmap.o
+obj-$(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE) += pgtable-geometry.o
 obj-$(CONFIG_ARM64_CONTPTE)	+= contpte.o
 obj-$(CONFIG_HUGETLB_PAGE)	+= hugetlbpage.o
 obj-$(CONFIG_PTDUMP)		+= ptdump.o
diff --git a/arch/arm64/mm/pgd.c b/arch/arm64/mm/pgd.c
index 5d40ce1eab3f..072e1b821b2a 100644
--- a/arch/arm64/mm/pgd.c
+++ b/arch/arm64/mm/pgd.c
@@ -21,10 +21,12 @@ static bool pgdir_is_page_size(void)
 {
 	if (PGD_SIZE == PAGE_SIZE)
 		return true;
-	if (CONFIG_PGTABLE_LEVELS == 4)
-		return !pgtable_l4_enabled();
-	if (CONFIG_PGTABLE_LEVELS == 5)
-		return !pgtable_l5_enabled();
+	if (!IS_ENABLED(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE)) {
+		if (CONFIG_PGTABLE_LEVELS == 4)
+			return !pgtable_l4_enabled();
+		if (CONFIG_PGTABLE_LEVELS == 5)
+			return !pgtable_l5_enabled();
+	}
 	return false;
 }
 
diff --git a/arch/arm64/mm/pgtable-geometry.c b/arch/arm64/mm/pgtable-geometry.c
new file mode 100644
index 000000000000..645b722ced55
--- /dev/null
+++ b/arch/arm64/mm/pgtable-geometry.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 ARM Ltd.
+ */
+
+#include <linux/cache.h>
+#include <asm/pgtable-geometry.h>
+
+/*
+ * TODO: These should be __ro_after_init, but we need to write to them from the
+ * pi code where they are mapped in the early page table as read-only.
+ * __ro_after_init doesn't become writable until later when the swapper pgtable
+ * is fully set up. We should update the early page table to map __ro_after_init
+ * as read-write.
+ */
+
+int ptg_page_shift __read_mostly;
+EXPORT_SYMBOL(ptg_page_shift);
+int ptg_pmd_shift __read_mostly;
+EXPORT_SYMBOL(ptg_pmd_shift);
+int ptg_pud_shift __read_mostly;
+EXPORT_SYMBOL(ptg_pud_shift);
+int ptg_p4d_shift __read_mostly;
+EXPORT_SYMBOL(ptg_p4d_shift);
+int ptg_pgdir_shift __read_mostly;
+EXPORT_SYMBOL(ptg_pgdir_shift);
+int ptg_cont_pte_shift __read_mostly;
+EXPORT_SYMBOL(ptg_cont_pte_shift);
+int ptg_cont_pmd_shift __read_mostly;
+EXPORT_SYMBOL(ptg_cont_pmd_shift);
+int ptg_pgtable_levels __read_mostly;
+EXPORT_SYMBOL(ptg_pgtable_levels);
diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c
index d35c6dd76601..b50674fcf40d 100644
--- a/drivers/firmware/efi/libstub/arm64.c
+++ b/drivers/firmware/efi/libstub/arm64.c
@@ -68,7 +68,8 @@ efi_status_t check_platform_features(void)
 		efi_novamap = true;
 
 	/* UEFI mandates support for 4 KB granularity, no need to check */
-	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
+	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES) ||
+	    IS_ENABLED(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE))
 		return EFI_SUCCESS;
 
 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_EL1_TGRAN_SHIFT) & 0xf;


^ permalink raw reply related	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2026-12-01  2:04 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern at boot 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox