Skip to contentSkip to conversation

Boot-time page size selection for arm64 (core)

RFC PATCH New Eric Curtin 1 participant 44 messages Dec 1

Subsystems (54)
lore
Share & tools
lore
#0 lore raw

v2 cover Boot-time page size selection for arm64 (core)

From: Eric Curtin <[email protected]>

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/[email protected]/
[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

#1 lore raw

RFC PATCH v2 1/43 mm: Add macros ahead of supporting boot-time page size selection

From: Ryan Roberts <[email protected]>

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.

(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
arch/alpha/include/asm/page.h +1 -0
index 67053c52953f..f5468f52b281 100644
@@ -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 */
arch/arc/include/asm/page.h +1 -0
index facc7a03b250..844ade515d4d 100644
@@ -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
 
arch/arm/include/asm/page.h +1 -0
index fa4c1225dde5..fab78b35823f 100644
@@ -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
arch/arm64/include/asm/page-def.h +2 -0
index d402e08442ee..e0ef167519e6 100644
@@ -12,4 +12,6 @@
 
 #include <vdso/page.h>
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_PAGE_DEF_H */
arch/csky/include/asm/page.h +3 -0
index 76774dbce869..cc223a7c25b2 100644
@@ -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 */
arch/hexagon/include/asm/page.h +2 -0
index 6d82572a7f21..b2e38f1132b9 100644
@@ -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
arch/loongarch/include/asm/page.h +2 -0
index b9d61f7c032c..c446799933a0 100644
@@ -107,4 +107,6 @@ extern int __virt_addr_valid(volatile void *kaddr);
 
 #endif /* !__ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_PAGE_H */
arch/m68k/include/asm/page.h +1 -0
index d30f8b2f1592..d4dc0147e47c 100644
@@ -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 */
arch/microblaze/include/asm/page.h +1 -0
index e1e396367ba7..4d3c1cc0073c 100644
@@ -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 */
arch/mips/include/asm/page.h +1 -0
index 50a382a0d8f6..9cdc0b8cb966 100644
@@ -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 */
arch/nios2/include/asm/page.h +2 -0
index 71eb7c1b67d4..27c4c68e8aea 100644
@@ -93,4 +93,6 @@ extern struct page *mem_map;
 
 #endif /* !__ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_NIOS2_PAGE_H */
arch/openrisc/include/asm/page.h +1 -0
index d2cdbf3579bb..ce3a6d921d8d 100644
@@ -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 */
arch/parisc/include/asm/page.h +1 -0
index 3630b36d07da..4dffa850d30a 100644
@@ -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))
arch/powerpc/include/asm/page.h +2 -0
index 281f25e071a3..e5c01c11f26b 100644
@@ -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 */
arch/riscv/include/asm/page.h +1 -0
index b4bbae55e931..2e68929ac15c 100644
@@ -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 */
arch/s390/include/asm/page.h +1 -0
index 56da819a79e6..46f340d25b74 100644
@@ -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)
 
arch/sh/include/asm/page.h +1 -0
index def4205491ec..13e13e900586 100644
@@ -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 */
arch/sparc/include/asm/page.h +3 -0
index 1a00cc0a1893..8f46b7a38ccd 100644
@@ -7,4 +7,7 @@
 #else
 #include <asm/page_32.h>
 #endif
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif
arch/um/include/asm/page.h +2 -0
index e348ff489b89..6eb5a5cdbcf0 100644
@@ -95,4 +95,6 @@ extern unsigned long uml_physmem;
 
 #endif	/* __ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* __UM_PAGE_H */
arch/x86/include/asm/page_types.h +2 -0
index 3e0801a0f782..99145b56714c 100644
@@ -66,4 +66,6 @@ extern void initmem_init(void);
 
 #endif	/* !__ASSEMBLER__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* _ASM_X86_PAGE_DEFS_H */
arch/xtensa/include/asm/page.h +1 -0
index 059493256765..6fb8bfbd0f45 100644
@@ -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 */
include/asm-generic/pgtable-geometry.h new file +86 -0

86 lines not on this page · raw message

init/main.c +4 -1
index f14780345051..3d619be281aa 100644
@@ -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();
 }
 
kernel/module/main.c +3 -1
index 34e0204ea37c..17145dc9c009 100644
@@ -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,
#2 lore raw

RFC PATCH v2 2/43 vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
include/asm-generic/vmlinux.lds.h +19 -19
index 3eb732942d69..2a86599cb620 100644
@@ -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						\
include/linux/linkage.h +2 -2
index 53fe1f48fd28..3673440789c3 100644
@@ -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
include/linux/percpu-defs.h +2 -2
index dbe3267a0a13..9e15c10ca6a5 100644
@@ -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.
#5 lore raw

RFC PATCH v2 5/43 mm: Avoid split pmd ptl if pmd level is run-time folded

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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).

(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(-)
include/linux/mm.h +16 -1
index 040cff64381b..14a35440c806 100644
@@ -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
 
include/linux/mm_types.h +1 -1
index 1ed35d258d2e..ed9277798631 100644
@@ -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
kernel/fork.c +2 -2
index d55a7d21a8fb..5a0db5a89615 100644
@@ -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);
mm/Kconfig +8 -0
index afff1b25b543..f0868970b367 100644
@@ -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
#6 lore raw

RFC PATCH v2 6/43 mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
include/linux/vm_event_item.h +7 -7
index 2628ccda076a..d1bca28052ef 100644
@@ -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 */
kernel/exit.c +7 -7
index d5e7bc2af804..a8453363d1fd 100644
@@ -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
mm/vmstat.c +7 -7
index c6beac6be2bc..248ab936410c 100644
@@ -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 */
#4 lore raw

RFC PATCH v2 4/43 mm: page_frag: Add _MIN/_MAX variants of PAGE_FRAG_CACHE_MAX_SIZE

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

PAGE_FRAG_CACHE_MAX_SIZE is derived from PAGE_MASK and, with boot-time page size selection, is not a compile-time constant. It is needed both where a constant is required (struct layout, preprocessor conditionals, BUILD_BUG_ON()) and to compute the allocation order at run time.

Make PAGE_FRAG_CACHE_MAX_SIZE, and hence PAGE_FRAG_CACHE_MAX_ORDER, follow the runtime PAGE_MASK, and add PAGE_FRAG_CACHE_MAX_SIZE_MIN and PAGE_FRAG_CACHE_MAX_SIZE_MAX, computed from PAGE_MASK_MIN and PAGE_MASK_MAX. Use the _MAX variant for the struct page_frag_cache layout and the _MIN variant for PAGE_FRAG_CACHE_ORDER_MASK and the encoded_page_create() order check.

No functional change for compile-time page sizes.

(cherry picked from commit ef68b4f691a7bb68097fb285c111ac88caeb107c)
---
 include/linux/mm_types_task.h   | 6 ++++--
 include/linux/page_frag_cache.h | 2 +-
 mm/page_frag_cache.c            | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)
include/linux/mm_types_task.h +4 -2
index 704c3c31d11d..2f760f472e19 100644
@@ -44,7 +44,9 @@ struct page_frag {
 #endif
 };
 
-#define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK_MIN)
+#define PAGE_FRAG_CACHE_MAX_SIZE	__ALIGN_MASK(32768, ~PAGE_MASK)
+#define PAGE_FRAG_CACHE_MAX_SIZE_MIN	__ALIGN_MASK(32768, ~PAGE_MASK_MIN)
+#define PAGE_FRAG_CACHE_MAX_SIZE_MAX	__ALIGN_MASK(32768, ~PAGE_MASK_MAX)
 #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 +57,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_MAX < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32)
+#if (PAGE_SIZE_MAX < PAGE_FRAG_CACHE_MAX_SIZE_MAX) && (BITS_PER_LONG <= 32)
 	__u16 offset;
 	__u16 pagecnt_bias;
 #else
include/linux/page_frag_cache.h +1 -1
index 49a0b0b51446..5ce206c892f7 100644
@@ -8,7 +8,7 @@
 #include <linux/mm_types_task.h>
 #include <linux/types.h>
 
-#if (PAGE_SIZE_MIN < PAGE_FRAG_CACHE_MAX_SIZE)
+#if (PAGE_SIZE_MIN < PAGE_FRAG_CACHE_MAX_SIZE_MIN)
 /* Use a full byte here to enable assembler optimization as the shift
  * operation is usually expecting a byte.
  */
mm/page_frag_cache.c +1 -1
index d4419caa0c06..7f1eca0b9955 100644
@@ -23,7 +23,7 @@
 static unsigned long encoded_page_create(struct page *page, unsigned int order,
 					 bool pfmemalloc)
 {
-	BUILD_BUG_ON(order_base_2(PAGE_FRAG_CACHE_MAX_SIZE >> PAGE_SHIFT_MIN) >
+	BUILD_BUG_ON(order_base_2(PAGE_FRAG_CACHE_MAX_SIZE_MIN >> PAGE_SHIFT_MIN) >
 		     PAGE_FRAG_CACHE_ORDER_MASK);
 	BUILD_BUG_ON(PAGE_FRAG_CACHE_PFMEMALLOC_BIT >= PAGE_SIZE_MIN);
 
#7 lore raw

RFC PATCH v2 7/43 fork: Permit boot-time THREAD_SIZE determination

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
include/asm-generic/vmlinux.lds.h +5 -1
index 2a86599cb620..4df6c8fd38cb 100644
@@ -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							\
include/linux/sched.h +2 -2
index e4f6323d7855..2b649bbd668a 100644
@@ -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)
include/linux/thread_info.h +9 -1
index 307b8390fc67..73d0a77a3c2c 100644
@@ -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)
init/main.c +1 -1
index 3d619be281aa..2a44990521eb 100644
@@ -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)
 {
 }
kernel/fork.c +17 -36

82 lines not on this page · raw message

mm/kasan/report.c +1 -2
index e804b1e1f886..7b170efd224b 100644
@@ -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,
#8 lore raw

RFC PATCH v2 8/43 cgroup: Use DEFINE_GLOBAL_PAGE_SIZE_VAR for the kernfs_ops tables

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(cherry picked from commit 565a75cff5df277dedade0680ed7a3810a4d7d44)
---
 kernel/cgroup/cgroup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/cgroup/cgroup.c +4 -4
index b3657e72db99..0421e88179b7 100644
@@ -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)
 {
#9 lore raw

RFC PATCH v2 9/43 bpf: Do not embed page-aligned members in struct bpf_ringbuf

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

struct bpf_ringbuf places consumer_pos, producer_pos and data at offsets aligned with __aligned(PAGE_SIZE). With boot-time page size selection the page size is not known at compile time, so this layout cannot be expressed.

Make consumer_pos, producer_pos, pending_pos, overwrite_pos and data pointers instead, and keep only the non-mmap()able metadata in the struct. bpf_ringbuf_area_alloc() points them into the pages that follow the struct when the area is vmap()ed, and all users dereference the pointers. RINGBUF_PGOFF becomes the page-aligned size of the struct.

In kernel/bpf/core.c, always expose __PAGE_SIZE_MIN and __PAGE_SIZE_MAX to BTF, but expose __PAGE_SIZE only when PAGE_SIZE_MIN equals PAGE_SIZE_MAX, since otherwise it is not a constant. bpf_prog_alloc_no_stats() now rounds up with PAGE_SIZE directly.

(cherry picked from commit dacca9a55f8e2b32aeccf6bf90497e5890f46de0)
---
 kernel/bpf/core.c    | 11 ++++++--
 kernel/bpf/ringbuf.c | 67 ++++++++++++++++++++++++--------------------
 2 files changed, 46 insertions(+), 32 deletions(-)
kernel/bpf/core.c +9 -2
index 6742f5be8658..dc3d62671a9d 100644
@@ -97,9 +97,16 @@ void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, uns
 	return NULL;
 }
 
-/* tell bpf programs that include vmlinux.h kernel's PAGE_SIZE */
+/*
+ * tell bpf programs that include vmlinux.h kernel's PAGE_SIZE. __PAGE_SIZE is
+ * only known for compile-time PAGE_SIZE builds, the limits are always known.
+ */
 enum page_size_enum {
+	__PAGE_SIZE_MIN = PAGE_SIZE_MIN,
+	__PAGE_SIZE_MAX = PAGE_SIZE_MAX,
+#if PAGE_SIZE_MIN == PAGE_SIZE_MAX
 	__PAGE_SIZE = PAGE_SIZE
+#endif
 };
 
 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)
@@ -108,7 +115,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
 	struct bpf_prog_aux *aux;
 	struct bpf_prog *fp;
 
-	size = round_up(size, __PAGE_SIZE);
+	size = round_up(size, PAGE_SIZE);
 	fp = __vmalloc(size, gfp_flags);
 	if (fp == NULL)
 		return NULL;
kernel/bpf/ringbuf.c +37 -30

176 lines not on this page · raw message

#10 lore raw

RFC PATCH v2 10/43 PM: hibernate: Do not size swap structures to exactly PAGE_SIZE

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
kernel/power/power.h +2 -1
index 75b63843886e..e7bbf94fe00d 100644
@@ -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);
kernel/power/snapshot.c +7 -3
index d5dba0e50b2e..af49d749aa66 100644
@@ -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) {
kernel/power/swap.c +106 -23

255 lines not on this page · raw message

#11 lore raw

RFC PATCH v2 11/43 perf/core: Handle a boot-time PAGE_SIZE in format attrs and mlock

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

With boot-time page size selection PAGE_SIZE is not a compile-time constant.

In the PMU_FORMAT_ATTR() show function, compare sizeof(_format) against PAGE_SIZE_MIN in BUILD_BUG_ON(). The format string does not depend on the page size, so checking it fits the smallest page is sufficient.

Define sysctl_perf_event_mlock, whose default is derived from PAGE_SIZE, with DEFINE_GLOBAL_PAGE_SIZE_VAR_ATTR() so that it is initialised at boot for boot-time page size builds.

No functional change for compile-time page sizes.

(cherry picked from commit 6e83c7d00396aa8d86ae2a50b6f7ce156c01d7cc)
---
 include/linux/perf_event.h | 2 +-
 kernel/events/core.c       | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
include/linux/perf_event.h +1 -1
index b52348eca6ef..e09498c9379e 100644
@@ -2098,7 +2098,7 @@ _name##_show(struct device *dev,					\
 			       struct device_attribute *attr,		\
 			       char *page)				\
 {									\
-	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);			\
+	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE_MIN);			\
 	return sprintf(page, _format "\n");				\
 }									\
 
kernel/events/core.c +2 -1
index 4ab3919ac78e..41c372980e0c 100644
@@ -496,7 +496,8 @@ static __always_inline bool is_guest_mediated_pmu_loaded(void)
 int sysctl_perf_event_paranoid __read_mostly = 2;
 
 /* Minimum for 512 kiB + 1 user control page. 'free' kiB per user. */
-static int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024);
+static DEFINE_GLOBAL_PAGE_SIZE_VAR_ATTR(int, sysctl_perf_event_mlock, __read_mostly,
+					512 + (PAGE_SIZE / 1024));
 
 /*
  * max perf event sample rate
#12 lore raw

RFC PATCH v2 12/43 KVM: Check struct kvm_run against PAGE_SIZE_MIN

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(cherry picked from commit 76729ec02c8773a6f44897c7629f594ab451312c)
---
 virt/kvm/kvm_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/kvm_main.c +1 -1
index 83e727bf6452..794f41a2d052 100644
@@ -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;
#3 lore raw

RFC PATCH v2 3/43 mm: page_frag: Use PAGE_SIZE_MIN/MAX limits in page_frag_cache

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
include/linux/mm_types_task.h +2 -2
index 11bf319d78ec..704c3c31d11d 100644
@@ -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
include/linux/page_frag_cache.h +1 -1
index 41a91df82631..49a0b0b51446 100644
@@ -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.
  */
mm/page_frag_cache.c +9 -8
index 8914437ddc51..d4419caa0c06 100644
@@ -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;
#16 lore raw

RFC PATCH v2 16/43 arm64: Introduce macros required for boot-time page size selection

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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
arch/arm64/include/asm/page-def.h +8 -1
index e0ef167519e6..dd9e164e1978 100644
@@ -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 */
arch/arm64/include/asm/pgtable-geometry.h new file +28 -0
new file mode 100644
index 000000000000..62fe125909c0
@@ -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 */
arch/arm64/include/asm/pgtable-hwdef.h +10 -6
index 72f31800c703..60e34018d5fa 100644
@@ -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))
#21 lore raw

RFC PATCH v2 21/43 arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/kvm_arm.h +7 -16
index 3e1c9e31028e..497d7202f978 100644
@@ -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)))
arch/arm64/include/asm/kvm_pgtable.h +1 -5
index 6d6ed8596847..24ff90459a8c 100644
@@ -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()
 
arch/arm64/include/asm/memory.h +2 -5
index a2b7a33966ff..a6df911206ff 100644
@@ -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
arch/arm64/include/asm/processor.h +5 -5
index 481f1999f7c1..9ca6fd0f409b 100644
@@ -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) ? \
arch/arm64/include/asm/sparsemem.h +2 -9
index 84783efdc9d1..4148f2440831 100644
@@ -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
arch/arm64/include/asm/sysreg.h +36 -18

71 lines not on this page · raw message

arch/arm64/kernel/cpufeature.c +27 -16

74 lines not on this page · raw message

arch/arm64/mm/fixmap.c +1 -1
index c8a0d008cf2a..bd48a21a98f9 100644
@@ -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))
arch/arm64/mm/init.c +8 -12
index 4e7682dfe366..70393e4d2d82 100644
@@ -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)
 {
arch/arm64/mm/kasan_init.c +4 -4
index 45fbdce684c8..638882f90ad5 100644
@@ -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);
arch/arm64/mm/mmu.c +1 -1
index 1567f76607d9..8cb8234cc24a 100644
@@ -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
drivers/irqchip/irq-gic-v3-its.c +1 -1
index c952c069f937..928cba93d8b2 100644
@@ -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) {
#22 lore raw

RFC PATCH v2 22/43 arm64: Align image sections to PAGE_SIZE_MAX

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/memory.h +2 -2
index a6df911206ff..05a8efc6810d 100644
@@ -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__
 
arch/arm64/kernel/vdso-wrap.S +2 -2
index c4b1990bf2be..79fa77628199 100644
@@ -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
arch/arm64/kernel/vdso.c +2 -2
index 50596dab23f9..8459ac3288cf 100644
@@ -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|
arch/arm64/kernel/vdso32-wrap.S +2 -2
index e72ac7bc4c04..1c6069d6c457 100644
@@ -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
arch/arm64/kernel/vmlinux.lds.S +23 -23

154 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/hyp.lds.S +1 -1
index 89222425359a..69b8fdbc977e 100644
@@ -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
#26 lore raw

RFC PATCH v2 26/43 arm64: Convert KVM and hugetlb size switches to if/else

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(cherry picked from commit 1cd569027d7d41ba43eedde8d483618ac38e2426)
---
 arch/arm64/kvm/mmu.c        | 29 +++++++++------------
 arch/arm64/mm/hugetlbpage.c | 50 ++++++++++++-------------------------
 2 files changed, 28 insertions(+), 51 deletions(-)
arch/arm64/kvm/mmu.c +12 -17
index 86aa0b6fa8b5..c3a5ee486ae8 100644
@@ -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;
arch/arm64/mm/hugetlbpage.c +16 -34

92 lines not on this page · raw message

#25 lore raw

RFC PATCH v2 25/43 arm64: Size and align the zero page and FDT ptes for PAGE_SIZE_MAX

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/kernel/pi/map_kernel.c +1 -1
index fa3549701927..16f12fa8c890 100644
@@ -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;
include/linux/pgtable.h +1 -1
index 66d369870a74..e3544dd7871d 100644
@@ -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)
mm/mm_init.c +1 -1
index d99af80ec752..0abbe36f5667 100644
@@ -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;
#29 lore raw

RFC PATCH v2 29/43 arm64: Look up the page size at runtime in assembly code

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/assembler.h +15 -3
index fc9659c64848..9ee11672d192 100644
@@ -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 */
arch/arm64/kernel/hibernate-asm.S +4 -2
index 2baefe7a82d3..556a7ecd3575 100644
@@ -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
arch/arm64/kernel/relocate_kernel.S +7 -3
index 064db5856bba..3ffb5682ee83 100644
@@ -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
arch/arm64/kvm/hyp/nvhe/host.S +12 -0
index 50581822ec74..6c12a9a90e86 100644
@@ -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
 
arch/arm64/lib/copy_page.S +37 -22

78 lines not on this page · raw message

arch/arm64/lib/mte.S +21 -6
index 5018ac03b6bf..b4f6f5be0ec7 100644
@@ -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
#30 lore raw

RFC PATCH v2 30/43 arm64: Support runtime folding of the pmd level

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/pgalloc.h +14 -1
index 1b4509d3382c..c79c7f43234b 100644
@@ -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)
 {
arch/arm64/include/asm/pgtable.h +86 -29

164 lines not on this page · raw message

arch/arm64/include/asm/tlb.h +3 -0
index 751bd57bc3ba..3197ed0702b4 100644
@@ -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
arch/arm64/kvm/mmu.c +2 -4
index c3a5ee486ae8..b15d59ec0bee 100644
@@ -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)) ||
arch/arm64/mm/fixmap.c +1 -1
index 46e08631d08e..0d1c1798e52c 100644
@@ -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
arch/arm64/mm/hugetlbpage.c +3 -6
index 447e832ad512..efe0f90e041e 100644
@@ -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)
arch/arm64/mm/init.c +3 -2
index f3229c921d9c..e6c15b06dc46 100644
@@ -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;
arch/arm64/mm/mmu.c +4 -2
index 9bc4c0b4b4d5..731633c64557 100644
@@ -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))
arch/arm64/mm/ptdump.c +4 -2
index 9d9bcb6738f4..6f9ddaa5da7b 100644
@@ -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)
#33 lore raw

RFC PATCH v2 33/43 arm64: Determine THREAD_SIZE at boot time

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/assembler.h +5 -0
index e55c4094a2a5..eac670152123 100644
@@ -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 */
arch/arm64/include/asm/efi.h +1 -1
index e8a9783235cb..3ac0d8fb0677 100644
@@ -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,
arch/arm64/include/asm/memory.h +32 -8
index 05a8efc6810d..716f346bd618 100644
@@ -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
 
arch/arm64/kernel/efi.c +1 -1
index 6f34aef3d04e..70967fe828ef 100644
@@ -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)
 {
arch/arm64/kernel/entry.S +13 -1
index 6c03468ae0dd..4f13e48cb497 100644
@@ -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
arch/arm64/kernel/head.S +2 -1
index 3641e638b31e..6bccecb1be91 100644
@@ -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]
arch/arm64/kernel/vmlinux.lds.S +2 -2
index 31e398bdc983..89a60e07dc52 100644
@@ -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
 
arch/arm64/kvm/hyp/nvhe/hyp.lds.S +1 -1
index 69b8fdbc977e..fc0f543ba76d 100644
@@ -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)
#34 lore raw

RFC PATCH v2 34/43 mm/damon/lru_sort: Initialise the stub access pattern at boot

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(cherry picked from commit fed214a06106c320f2e46950b5b6672966952196)
---
 mm/damon/lru_sort.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/damon/lru_sort.c +2 -2
index 64e086985eb5..37215e9b956c 100644
@@ -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;
#36 lore raw

RFC PATCH v2 36/43 arm64: Convert remaining page-size switches to if/else

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(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(-)
arch/arm64/include/asm/hugetlb.h +6 -9
index d038ff14d16c..b8487e8c9cf8 100644
@@ -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);
 	}
 }
arch/arm64/include/asm/linkage.h +1 -1
index d1f7a16729d2..2eb3c0f933e3 100644
@@ -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
arch/arm64/include/asm/pgtable.h +8 -16
index c625fe7f8452..7cdff90a3b7e 100644
@@ -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);
 	}
 
arch/arm64/kvm/hyp/nvhe/mm.c +14 -16
index 83b74c5223c3..ede688d8a5f1 100644
@@ -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)
arch/arm64/mm/fault.c +4 -8
index b22736853fcd..0880f6456b49 100644
@@ -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);
 		}
#37 lore raw

RFC PATCH v2 37/43 vdso: Lay out the data pages in VDSO_PAGE_SIZE units

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(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(-)
include/vdso/datapage.h +5 -5
index 09897f76ae07..d999f10032b6 100644
@@ -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					\
include/vdso/page.h +4 -0
index bc47186c07fc..b79e1c8c8363 100644
@@ -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.
lib/vdso/datastore.c +19 -15
index 1426bf4e0c12..74ad8d71f6c8 100644
@@ -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);
lib/vdso/gettimeofday.c +2 -2
index a324b2f2ec6c..e3ea7df41fe4 100644
@@ -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
#35 lore raw

RFC PATCH v2 35/43 mm/zsmalloc: Size static limits for the worst-case page size

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(cherry picked from commit dca1629b5374efd825c6a5bc6d4f210dae3b861f)
---
 mm/zsmalloc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
mm/zsmalloc.c +13 -5
index 7ef80e0da626..09d131da22ac 100644
@@ -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);
 }
#39 lore raw

RFC PATCH v2 39/43 crash: Size the vmcoreinfo page array for the smallest page size

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(cherry picked from commit 08a51916386a159aeb15a6e51605740a895b7bb5)
---
 include/linux/vmcore_info.h | 1 +
 kernel/crash_core.c         | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/vmcore_info.h +1 -0
index e71518caacdf..f5d49e71ac7b 100644
@@ -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) +	\
kernel/crash_core.c +1 -1
index c7eb72748fee..ca7252fda848 100644
@@ -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;
#40 lore raw

RFC PATCH v2 40/43 arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(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(-)
arch/arm64/Makefile +5 -0
index ed28f94be428..d1be87f08fbd 100644
@@ -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
arch/arm64/include/asm/memory.h +3 -1
index 716f346bd618..476ee4addfc2 100644
@@ -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).
arch/arm64/include/asm/stacktrace/nvhe.h +1 -1
index a631a577cbe5..5aa035707481 100644
@@ -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
 
arch/arm64/kvm/hyp/nvhe/stacktrace.c +2 -1
index 11fadbebbf1d..dff23678e48c 100644
@@ -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)
 {
arch/arm64/mm/kasan_init.c +11 -6
index 638882f90ad5..1390884c71bf 100644
@@ -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;
#41 lore raw

RFC PATCH v2 41/43 exit: Use THREAD_SIZE_MAX for lowest_to_date

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(cherry picked from commit e4b475ad2b5c70b4fa666356ca309ed3ec0528ed)
---
 kernel/exit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/exit.c +1 -1
index a8453363d1fd..4a7d792f69ed 100644
@@ -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);
#38 lore raw

RFC PATCH v2 38/43 mm: Use fixed sizes for shmem str_dup and swap cache PFN bits

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(cherry picked from commit 54043747407a2dc56dcd60bc2b1f29ce193ee804)
---
 mm/shmem.c | 4 ++--
 mm/swap.h  | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
mm/shmem.c +2 -2
index a5713109c843..757b0906caae 100644
@@ -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);
 
mm/swap.h +4 -3
index d5bf21f517dc..6dab19c1fac9 100644
@@ -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) */
#42 lore raw

RFC PATCH v2 42/43 tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(cherry picked from commit f68b4569af533a62d3da83ffb3141256868d1c70)
---
 kernel/trace/trace_uprobe.c   | 2 +-
 kernel/trace/undefsyms_base.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
kernel/trace/trace_uprobe.c +1 -1
index fb7991543db7..d7ae99294bd0 100644
@@ -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;
kernel/trace/undefsyms_base.c +2 -1
index 37310baf706b..01b4f1913d61 100644
@@ -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));
#13 lore raw

RFC PATCH v2 13/43 crash: Check crash_notes size against PAGE_SIZE_MIN

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(cherry picked from commit 6f90d8bafde4f6460bc1e449ffd28bd649a85e4a)
---
 kernel/crash_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/crash_core.c +1 -1
index d0bd2d0cf899..c7eb72748fee 100644
@@ -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) {
#43 lore raw

RFC PATCH v2 43/43 arm64: Enable boot-time page size selection

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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
Documentation/admin-guide/kernel-parameters.txt +11 -0
index dce9b8f3ae98..fce8dbfad329 100644
@@ -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,
Documentation/arch/arm64/booting.rst +3 -1
index dae5ff9fae26..2fe72b58b088 100644
@@ -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
Documentation/arch/arm64/memory.rst +21 -0
index 678fbb418c3a..8207096bc7e7 100644
@@ -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
arch/arm64/Kconfig +49 -5

120 lines not on this page · raw message

arch/arm64/include/asm/kvm_hyp.h +11 -0
index b0e42be3d9ee..1394dc2de129 100644
@@ -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__ */
arch/arm64/include/asm/pgtable-geometry.h +20 -2
index 62fe125909c0..18a8c8d499ec 100644
@@ -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 */
arch/arm64/include/asm/pgtable-hwdef.h +6 -3
index 02373c4ea474..21e9c8e76651 100644
@@ -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
 
arch/arm64/include/asm/pgtable.h +9 -1
index 7cdff90a3b7e..7fd4e6356297 100644
@@ -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);
 }
 
arch/arm64/include/asm/sparsemem.h +4 -0
index 4148f2440831..1e196360ae70 100644
@@ -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
arch/arm64/kernel/image-vars.h +11 -0
index 9c9215847d26..1774f75d0cb7 100644
@@ -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);
arch/arm64/kernel/image.h +4 -0
index 5c10ca773363..0cfa5bc5e90f 100644
@@ -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
 
arch/arm64/kernel/pi/map_kernel.c +45 -0

57 lines not on this page · raw message

arch/arm64/kvm/arm.c +10 -0
index a148e80d0aef..37ccceda65ea 100644
@@ -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();
arch/arm64/kvm/hyp/nvhe/Makefile +1 -0
index 56a7e2c05f73..af36e8ef2006 100644
@@ -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)
arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c new file +16 -0
new file mode 100644
index 000000000000..17f807450a31
@@ -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;
arch/arm64/mm/Makefile +1 -0
index c26489cf96cd..160ef2da4629 100644
@@ -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
arch/arm64/mm/pgd.c +6 -4
index 5d40ce1eab3f..072e1b821b2a 100644
@@ -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;
 }
 
arch/arm64/mm/pgtable-geometry.c new file +32 -0
new file mode 100644
index 000000000000..645b722ced55
@@ -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);
drivers/firmware/efi/libstub/arm64.c +2 -1
index d35c6dd76601..b50674fcf40d 100644
@@ -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;
#23 lore raw

RFC PATCH v2 23/43 arm64: Map the trampoline rodata at its aligned offset

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/fixmap.h +11 -5
index 170c3502d723..5ec75d6daf36 100644
@@ -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,
 
arch/arm64/include/asm/sections.h +1 -0
index 51b0d594239e..f61cd8d9d4b1 100644
@@ -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)
arch/arm64/kernel/vmlinux.lds.S +3 -1
index eb253341711c..31e398bdc983 100644
@@ -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
arch/arm64/mm/mmu.c +16 -10
index 8cb8234cc24a..96fc6d843d34 100644
@@ -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)
 {
#24 lore raw

RFC PATCH v2 24/43 arm64: fixmap: Size slots and page tables for PAGE_SIZE_MIN

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/fixmap.h +8 -4
index 5ec75d6daf36..fd3597e920bc 100644
@@ -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)
 
arch/arm64/mm/fixmap.c +23 -11
index bd48a21a98f9..46e08631d08e 100644
@@ -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);
 	}
#27 lore raw

RFC PATCH v2 27/43 arm64: mm: Make page size dependent BUILD_BUG_ON() checks runtime

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/mm/init.c +3 -3
index 70393e4d2d82..f3229c921d9c 100644
@@ -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;
arch/arm64/mm/mmu.c +4 -2
index 96fc6d843d34..9bc4c0b4b4d5 100644
@@ -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();
 
arch/arm64/mm/pgd.c +1 -1
index bf5110b91e2f..5d40ce1eab3f 100644
@@ -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
 
 	/*
#32 lore raw

RFC PATCH v2 32/43 arm64: Patch TRAMP_VALIAS into instructions at boot

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/kernel/asm-offsets.c +1 -1
index 9d603317eeb9..c7490746d760 100644
@@ -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
arch/arm64/kernel/cpu_errata.c +53 -0

59 lines not on this page · raw message

arch/arm64/kernel/entry.S +34 -0
index 8d17f61d1b5b..6c03468ae0dd 100644
@@ -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
 
arch/arm64/mm/mmu.c +50 -0

62 lines not on this page · raw message

#31 lore raw

RFC PATCH v2 31/43 arm64: Support runtime folding in idmap_kpti_install_ng_mappings

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/assembler.h +5 -0
index 9ee11672d192..e55c4094a2a5 100644
@@ -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))
arch/arm64/mm/mmu.c +19 -2
index 731633c64557..e1f06f622f32 100644
@@ -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) {
arch/arm64/mm/proc.S +84 -23

211 lines not on this page · raw message

#14 lore raw

RFC PATCH v2 14/43 arm64: Make INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE usable from C

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(cherry picked from commit 39baea4f61f9134940df5053a53befa0a894270f)
---
 arch/arm64/include/asm/kernel-pgtable.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kernel-pgtable.h +5 -2
index 229ee7976f69..91f3b6691c9f 100644
@@ -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)
#15 lore raw

RFC PATCH v2 15/43 arm64: pi: Pass the early pgtable allocation limit to map_range()

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/smp.h +1 -0
index 10ea4f543069..ffd8206b557e 100644
@@ -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__
 
arch/arm64/kernel/head.S +2 -1
index d6ba3c0af811..2bdae5d0c5ef 100644
@@ -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
 
 	/*
arch/arm64/kernel/image-vars.h +3 -0
index 19d602e45523..9c9215847d26 100644
@@ -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
 
arch/arm64/kernel/pi/map_kernel.c +21 -18
index f9338257adb7..88850c98597e 100644
@@ -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);
arch/arm64/kernel/pi/map_range.c +39 -6

85 lines not on this page · raw message

arch/arm64/kernel/pi/pi.h +5 -4
index 5dfd8484d200..ce07cdc3445d 100644
@@ -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);
arch/arm64/mm/mmu.c +8 -4
index 19459a4a870a..4127db9fb645 100644
@@ -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);
 	}
 }
#18 lore raw

RFC PATCH v2 18/43 arm64: Add arm64.pagesize= command line override

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(+)
arch/arm64/include/asm/cpufeature.h +11 -0
index 7d7b8eb7f72f..405291491866 100644
@@ -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);
arch/arm64/kernel/pi/idreg-override.c +13 -0
index 2f132b44ef27..f2641106e164 100644
@@ -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)
#20 lore raw

RFC PATCH v2 20/43 arm64: pi: Clear the FDT ptes on every remap in map_fdt()

In reply to Eric Curtin

From: Eric Curtin <[email protected]>

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.

(cherry picked from commit cacdf8d80bbab6eaafc3bcbc5689636ac521cb0a)
---
 arch/arm64/kernel/pi/map_kernel.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm64/kernel/pi/map_kernel.c +2 -3
index 357ccb562746..fa3549701927 100644
@@ -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
#17 lore raw

RFC PATCH v2 17/43 arm64: Parameterise early pgtable size macros by page shift

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/kernel-pgtable.h +107 -43
index 91f3b6691c9f..3f24f2d0829a 100644
@@ -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 */
arch/arm64/include/asm/pgtable-hwdef.h +7 -2
index 60e34018d5fa..02373c4ea474 100644
@@ -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))
arch/arm64/kernel/pi/map_kernel.c +3 -3
index 88850c98597e..47d5b29cf99c 100644
@@ -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();
arch/arm64/kernel/pi/map_range.c +4 -4
index 20289f9683b8..4ca0aaf12e17 100644
@@ -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;
 }
arch/arm64/kernel/vmlinux.lds.S +2 -2
index c95243a8b24e..ca3016f284d5 100644
@@ -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 */
 
arch/arm64/mm/mmu.c +6 -7
index 4127db9fb645..1567f76607d9 100644
@@ -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);
 	}
#19 lore raw

RFC PATCH v2 19/43 arm64: pi: Select the page size at runtime during early boot

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

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.

(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(-)
arch/arm64/include/asm/assembler.h +49 -1
index 020670814bda..fc9659c64848 100644
@@ -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 */
arch/arm64/include/asm/cpufeature.h +23 -10
index 405291491866..35239ea01e77 100644
@@ -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__ */
arch/arm64/include/asm/pgtable-prot.h +1 -1
index 590ef4328f12..da792605ac57 100644
@@ -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
arch/arm64/kernel/head.S +31 -9
index 2bdae5d0c5ef..3641e638b31e 100644
@@ -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
arch/arm64/kernel/pi/idreg-override.c +28 -9
index f2641106e164..c58f34b24ea7 100644
@@ -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;
 }
 
arch/arm64/kernel/pi/map_kernel.c +76 -29

186 lines not on this page · raw message

arch/arm64/kernel/pi/map_range.c +157 -26

248 lines not on this page · raw message

arch/arm64/kernel/pi/pi.h +60 -1

71 lines not on this page · raw message

arch/arm64/mm/proc.S +9 -15
index 62e790a2a779..afc9dc20084e 100644
@@ -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
 
#28 lore raw

RFC PATCH v2 28/43 arm64: cpufeature: Add capabilities for the page size in use

In reply to Eric Curtin

From: Ryan Roberts <[email protected]>

Add one boot CPU capability per page size, ARM64_USE_PAGE_SIZE_4K, ARM64_USE_PAGE_SIZE_16K and ARM64_USE_PAGE_SIZE_64K. Exactly one of them is set, according to PAGE_SIZE, by the new use_page_size() match function. This lets the alternatives framework patch code, such as assembly that needs the page shift as an immediate, according to the page size selected at boot.

The capability descriptions also make the page size in use visible in the kernel log during boot.

(cherry picked from commit fc11b748a3ad8b591f1ae32c8b6f9f607865d06c)
---
 arch/arm64/kernel/cpufeature.c | 25 +++++++++++++++++++++++++
 arch/arm64/tools/cpucaps       |  3 +++
 2 files changed, 28 insertions(+)
arch/arm64/kernel/cpufeature.c +25 -0
index 0090ec390cf8..553d30eb1e1a 100644
@@ -1780,6 +1780,13 @@ static bool has_nv1(const struct arm64_cpu_capabilities *entry, int scope)
 		  is_midr_in_range_list(nv1_ni_list)));
 }
 
+static bool use_page_size(const struct arm64_cpu_capabilities *entry, int scope)
+{
+	return (entry->capability == ARM64_USE_PAGE_SIZE_4K && PAGE_SIZE == SZ_4K) ||
+	       (entry->capability == ARM64_USE_PAGE_SIZE_16K && PAGE_SIZE == SZ_16K) ||
+	       (entry->capability == ARM64_USE_PAGE_SIZE_64K && PAGE_SIZE == SZ_64K);
+}
+
 static bool has_lpa2_at_stage1(u64 mmfr0)
 {
 	unsigned int tgran;
@@ -3112,6 +3119,24 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		ARM64_CPUID_FIELDS(ID_AA64ISAR3_EL1, LSUI, IMP)
 	},
 #endif
+	{
+		.desc = "4K page size",
+		.capability = ARM64_USE_PAGE_SIZE_4K,
+		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
+		.matches = use_page_size,
+	},
+	{
+		.desc = "16K page size",
+		.capability = ARM64_USE_PAGE_SIZE_16K,
+		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
+		.matches = use_page_size,
+	},
+	{
+		.desc = "64K page size",
+		.capability = ARM64_USE_PAGE_SIZE_64K,
+		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
+		.matches = use_page_size,
+	},
 	{},
 };
 
arch/arm64/tools/cpucaps +3 -0
index a0a0ee33fd09..a3a9f297ae75 100644
@@ -93,6 +93,9 @@ SPECTRE_BHB
 SSBS
 SVE
 UNMAP_KERNEL_AT_EL0
+USE_PAGE_SIZE_4K
+USE_PAGE_SIZE_16K
+USE_PAGE_SIZE_64K
 WORKAROUND_834220
 WORKAROUND_843419
 WORKAROUND_845719