* [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection
[not found] <20261201015240.1017259-1-ericcurtin17@gmail.com>
@ 2026-12-01 1:51 ` Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
` (2 subsequent siblings)
3 siblings, 0 replies; 4+ messages in thread
From: Eric Curtin @ 2026-12-01 1:51 UTC (permalink / raw)
To: linux-arm-kernel
Cc: Ryan Roberts, Catalin Marinas, Will Deacon, linux-kernel,
linux-arch
From: Ryan Roberts <ryan.roberts@arm.com>
arm64 can support several base page sizes. To let the page size be
chosen on the kernel command line instead of at build time, PAGE_SHIFT,
PAGE_SIZE and PAGE_MASK can no longer be compile-time constants, and
code that needs a constant has to be converted.
Introduce PAGE_{SHIFT,SIZE,MASK}_{MIN,MAX}, which bound the range of
possible page sizes and are always compile-time constants. An arch must
define all of them or none. When it defines none, they are set equal to
PAGE_SHIFT, PAGE_SIZE and PAGE_MASK, so existing behaviour is unchanged.
Introduce DEFINE_GLOBAL_PAGE_SIZE_VAR(), _CONST() and _ATTR() to wrap
definitions of globals whose initialiser depends on PAGE_SIZE. With a
fixed page size they expand to a plain definition. With a boot-time page
size the variable is assigned by a function marked with the constructor
attribute. Constructors are used rather than initcalls because a module
is limited to one initcall but may have any number of constructors.
To make this work, call do_ctors() early in start_kernel() instead of
from do_basic_setup(), and run module constructors before module
parameters are parsed so that the parameters can override the defaults.
An arch enabling boot-time page size must select CONFIG_CONSTRUCTORS.
Also add BUILD_BUG_ON_FIXED_PAGE_SIZE(), which is BUILD_BUG_ON() when
PAGE_SIZE_MIN equals PAGE_SIZE_MAX and VM_BUG_ON() otherwise.
The macros live in a new asm-generic/pgtable-geometry.h, included from
each arch's page header next to PAGE_SHIFT, as there is no common header
to hook into.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 2e4f93099407da931c07e4bd7aec9ff5696207fa)
---
arch/alpha/include/asm/page.h | 1 +
arch/arc/include/asm/page.h | 1 +
arch/arm/include/asm/page.h | 1 +
arch/arm64/include/asm/page-def.h | 2 +
arch/csky/include/asm/page.h | 3 +
arch/hexagon/include/asm/page.h | 2 +
arch/loongarch/include/asm/page.h | 2 +
arch/m68k/include/asm/page.h | 1 +
arch/microblaze/include/asm/page.h | 1 +
arch/mips/include/asm/page.h | 1 +
arch/nios2/include/asm/page.h | 2 +
arch/openrisc/include/asm/page.h | 1 +
arch/parisc/include/asm/page.h | 1 +
arch/powerpc/include/asm/page.h | 2 +
arch/riscv/include/asm/page.h | 1 +
arch/s390/include/asm/page.h | 1 +
arch/sh/include/asm/page.h | 1 +
arch/sparc/include/asm/page.h | 3 +
arch/um/include/asm/page.h | 2 +
arch/x86/include/asm/page_types.h | 2 +
arch/xtensa/include/asm/page.h | 1 +
include/asm-generic/pgtable-geometry.h | 86 ++++++++++++++++++++++++++
init/main.c | 5 +-
kernel/module/main.c | 4 +-
24 files changed, 125 insertions(+), 2 deletions(-)
create mode 100644 include/asm-generic/pgtable-geometry.h
diff --git a/arch/alpha/include/asm/page.h b/arch/alpha/include/asm/page.h
index 67053c52953f..f5468f52b281 100644
--- a/arch/alpha/include/asm/page.h
+++ b/arch/alpha/include/asm/page.h
@@ -96,5 +96,6 @@ typedef struct page *pgtable_t;
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* _ALPHA_PAGE_H */
diff --git a/arch/arc/include/asm/page.h b/arch/arc/include/asm/page.h
index facc7a03b250..844ade515d4d 100644
--- a/arch/arc/include/asm/page.h
+++ b/arch/arc/include/asm/page.h
@@ -6,6 +6,7 @@
#define __ASM_ARC_PAGE_H
#include <uapi/asm/page.h>
+#include <asm-generic/pgtable-geometry.h>
#ifdef CONFIG_ARC_HAS_PAE40
diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h
index fa4c1225dde5..fab78b35823f 100644
--- a/arch/arm/include/asm/page.h
+++ b/arch/arm/include/asm/page.h
@@ -188,5 +188,6 @@ extern int pfn_valid(unsigned long);
#include <asm-generic/getorder.h>
#include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
#endif
diff --git a/arch/arm64/include/asm/page-def.h b/arch/arm64/include/asm/page-def.h
index d402e08442ee..e0ef167519e6 100644
--- a/arch/arm64/include/asm/page-def.h
+++ b/arch/arm64/include/asm/page-def.h
@@ -12,4 +12,6 @@
#include <vdso/page.h>
+#include <asm-generic/pgtable-geometry.h>
+
#endif /* __ASM_PAGE_DEF_H */
diff --git a/arch/csky/include/asm/page.h b/arch/csky/include/asm/page.h
index 76774dbce869..cc223a7c25b2 100644
--- a/arch/csky/include/asm/page.h
+++ b/arch/csky/include/asm/page.h
@@ -85,4 +85,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
#include <asm-generic/getorder.h>
#endif /* !__ASSEMBLER__ */
+
+#include <asm-generic/pgtable-geometry.h>
+
#endif /* __ASM_CSKY_PAGE_H */
diff --git a/arch/hexagon/include/asm/page.h b/arch/hexagon/include/asm/page.h
index 6d82572a7f21..b2e38f1132b9 100644
--- a/arch/hexagon/include/asm/page.h
+++ b/arch/hexagon/include/asm/page.h
@@ -130,4 +130,6 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
#endif /* ifdef __ASSEMBLY__ */
#endif /* ifdef __KERNEL__ */
+#include <asm-generic/pgtable-geometry.h>
+
#endif
diff --git a/arch/loongarch/include/asm/page.h b/arch/loongarch/include/asm/page.h
index b9d61f7c032c..c446799933a0 100644
--- a/arch/loongarch/include/asm/page.h
+++ b/arch/loongarch/include/asm/page.h
@@ -107,4 +107,6 @@ extern int __virt_addr_valid(volatile void *kaddr);
#endif /* !__ASSEMBLER__ */
+#include <asm-generic/pgtable-geometry.h>
+
#endif /* _ASM_PAGE_H */
diff --git a/arch/m68k/include/asm/page.h b/arch/m68k/include/asm/page.h
index d30f8b2f1592..d4dc0147e47c 100644
--- a/arch/m68k/include/asm/page.h
+++ b/arch/m68k/include/asm/page.h
@@ -58,5 +58,6 @@ extern unsigned long _ramend;
#include <asm-generic/getorder.h>
#include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* _M68K_PAGE_H */
diff --git a/arch/microblaze/include/asm/page.h b/arch/microblaze/include/asm/page.h
index e1e396367ba7..4d3c1cc0073c 100644
--- a/arch/microblaze/include/asm/page.h
+++ b/arch/microblaze/include/asm/page.h
@@ -137,5 +137,6 @@ static inline const void *pfn_to_virt(unsigned long pfn)
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* _ASM_MICROBLAZE_PAGE_H */
diff --git a/arch/mips/include/asm/page.h b/arch/mips/include/asm/page.h
index 50a382a0d8f6..9cdc0b8cb966 100644
--- a/arch/mips/include/asm/page.h
+++ b/arch/mips/include/asm/page.h
@@ -223,5 +223,6 @@ static inline unsigned long kaslr_offset(void)
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* _ASM_PAGE_H */
diff --git a/arch/nios2/include/asm/page.h b/arch/nios2/include/asm/page.h
index 71eb7c1b67d4..27c4c68e8aea 100644
--- a/arch/nios2/include/asm/page.h
+++ b/arch/nios2/include/asm/page.h
@@ -93,4 +93,6 @@ extern struct page *mem_map;
#endif /* !__ASSEMBLER__ */
+#include <asm-generic/pgtable-geometry.h>
+
#endif /* _ASM_NIOS2_PAGE_H */
diff --git a/arch/openrisc/include/asm/page.h b/arch/openrisc/include/asm/page.h
index d2cdbf3579bb..ce3a6d921d8d 100644
--- a/arch/openrisc/include/asm/page.h
+++ b/arch/openrisc/include/asm/page.h
@@ -76,5 +76,6 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* __ASM_OPENRISC_PAGE_H */
diff --git a/arch/parisc/include/asm/page.h b/arch/parisc/include/asm/page.h
index 3630b36d07da..4dffa850d30a 100644
--- a/arch/parisc/include/asm/page.h
+++ b/arch/parisc/include/asm/page.h
@@ -169,6 +169,7 @@ extern int npmem_ranges;
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#include <asm/pdc.h>
#define PAGE0 ((struct zeropage *)absolute_pointer(__PAGE_OFFSET))
diff --git a/arch/powerpc/include/asm/page.h b/arch/powerpc/include/asm/page.h
index 281f25e071a3..e5c01c11f26b 100644
--- a/arch/powerpc/include/asm/page.h
+++ b/arch/powerpc/include/asm/page.h
@@ -293,4 +293,6 @@ static inline unsigned long kaslr_offset(void)
#include <asm-generic/memory_model.h>
#endif /* __ASSEMBLER__ */
+#include <asm-generic/pgtable-geometry.h>
+
#endif /* _ASM_POWERPC_PAGE_H */
diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
index b4bbae55e931..2e68929ac15c 100644
--- a/arch/riscv/include/asm/page.h
+++ b/arch/riscv/include/asm/page.h
@@ -176,5 +176,6 @@ static __always_inline void *pfn_to_kaddr(unsigned long pfn)
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* _ASM_RISCV_PAGE_H */
diff --git a/arch/s390/include/asm/page.h b/arch/s390/include/asm/page.h
index 56da819a79e6..46f340d25b74 100644
--- a/arch/s390/include/asm/page.h
+++ b/arch/s390/include/asm/page.h
@@ -283,6 +283,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#define AMODE31_SIZE (3 * PAGE_SIZE)
diff --git a/arch/sh/include/asm/page.h b/arch/sh/include/asm/page.h
index def4205491ec..13e13e900586 100644
--- a/arch/sh/include/asm/page.h
+++ b/arch/sh/include/asm/page.h
@@ -159,5 +159,6 @@ typedef struct page *pgtable_t;
#include <asm-generic/memory_model.h>
#include <asm-generic/getorder.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* __ASM_SH_PAGE_H */
diff --git a/arch/sparc/include/asm/page.h b/arch/sparc/include/asm/page.h
index 1a00cc0a1893..8f46b7a38ccd 100644
--- a/arch/sparc/include/asm/page.h
+++ b/arch/sparc/include/asm/page.h
@@ -7,4 +7,7 @@
#else
#include <asm/page_32.h>
#endif
+
+#include <asm-generic/pgtable-geometry.h>
+
#endif
diff --git a/arch/um/include/asm/page.h b/arch/um/include/asm/page.h
index e348ff489b89..6eb5a5cdbcf0 100644
--- a/arch/um/include/asm/page.h
+++ b/arch/um/include/asm/page.h
@@ -95,4 +95,6 @@ extern unsigned long uml_physmem;
#endif /* __ASSEMBLER__ */
+#include <asm-generic/pgtable-geometry.h>
+
#endif /* __UM_PAGE_H */
diff --git a/arch/x86/include/asm/page_types.h b/arch/x86/include/asm/page_types.h
index 3e0801a0f782..99145b56714c 100644
--- a/arch/x86/include/asm/page_types.h
+++ b/arch/x86/include/asm/page_types.h
@@ -66,4 +66,6 @@ extern void initmem_init(void);
#endif /* !__ASSEMBLER__ */
+#include <asm-generic/pgtable-geometry.h>
+
#endif /* _ASM_X86_PAGE_DEFS_H */
diff --git a/arch/xtensa/include/asm/page.h b/arch/xtensa/include/asm/page.h
index 059493256765..6fb8bfbd0f45 100644
--- a/arch/xtensa/include/asm/page.h
+++ b/arch/xtensa/include/asm/page.h
@@ -174,4 +174,5 @@ static inline unsigned long ___pa(unsigned long va)
#endif /* __ASSEMBLER__ */
#include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
#endif /* _XTENSA_PAGE_H */
diff --git a/include/asm-generic/pgtable-geometry.h b/include/asm-generic/pgtable-geometry.h
new file mode 100644
index 000000000000..89609cab425f
--- /dev/null
+++ b/include/asm-generic/pgtable-geometry.h
@@ -0,0 +1,86 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_GENERIC_PGTABLE_GEOMETRY_H
+#define ASM_GENERIC_PGTABLE_GEOMETRY_H
+
+#if defined(PAGE_SHIFT_MAX) && defined(PAGE_SIZE_MAX) && defined(PAGE_MASK_MAX) && \
+ defined(PAGE_SHIFT_MIN) && defined(PAGE_SIZE_MIN) && defined(PAGE_MASK_MIN)
+/* Arch supports boot-time page size selection. */
+#elif defined(PAGE_SHIFT_MAX) || defined(PAGE_SIZE_MAX) || defined(PAGE_MASK_MAX) || \
+ defined(PAGE_SHIFT_MIN) || defined(PAGE_SIZE_MIN) || defined(PAGE_MASK_MIN)
+#error Arch must define all or none of the boot-time page size macros
+#else
+/* Arch does not support boot-time page size selection. */
+#define PAGE_SHIFT_MIN PAGE_SHIFT
+#define PAGE_SIZE_MIN PAGE_SIZE
+#define PAGE_MASK_MIN PAGE_MASK
+#define PAGE_SHIFT_MAX PAGE_SHIFT
+#define PAGE_SIZE_MAX PAGE_SIZE
+#define PAGE_MASK_MAX PAGE_MASK
+#endif
+
+/*
+ * Define a global variable (scalar or struct), whose value is derived from
+ * PAGE_SIZE and friends. When PAGE_SIZE is a compile-time constant, the global
+ * variable is simply defined with the static value. When PAGE_SIZE is
+ * determined at boot-time, a constructor is registered and run before any
+ * initcalls or module params are parsed to initialize the variable.
+ *
+ * @type: Unqualified type. Do not include "const"; implied by macro variant.
+ * @name: Variable name.
+ * @attrib: Variable attributes (DEFINE_GLOBAL_PAGE_SIZE_VAR_ATTR only).
+ * @...: Initialization value. May be scalar or initializer.
+ *
+ * "static" is declared by placing "static" before the macro.
+ *
+ * Example:
+ *
+ * struct my_struct {
+ * int a;
+ * char b;
+ * };
+ *
+ * static DEFINE_GLOBAL_PAGE_SIZE_VAR(struct my_struct, my_variable, {
+ * .a = 10,
+ * .b = 'e',
+ * });
+ */
+#if PAGE_SIZE_MIN != PAGE_SIZE_MAX
+#define __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, attrib, ...) \
+ type name attrib; \
+ static void __init __attribute__((constructor)) __##name##_init(void) \
+ { \
+ name = (type)__VA_ARGS__; \
+ }
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, ...) \
+ __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, , __VA_ARGS__)
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR_CONST(type, name, ...) \
+ __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, __ro_after_init, __VA_ARGS__)
+#else /* PAGE_SIZE_MIN == PAGE_SIZE_MAX */
+#define __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, attrib, ...) \
+ type name attrib = __VA_ARGS__; \
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, ...) \
+ __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, , __VA_ARGS__)
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR_CONST(type, name, ...) \
+ __DEFINE_GLOBAL_PAGE_SIZE_VAR(const type, name, , __VA_ARGS__)
+#endif
+
+#define DEFINE_GLOBAL_PAGE_SIZE_VAR_ATTR(type, name, attrib, ...) \
+ __DEFINE_GLOBAL_PAGE_SIZE_VAR(type, name, attrib, __VA_ARGS__)
+
+#ifndef __ASSEMBLY__
+/*
+ * Compile-time check when the page size is fixed. With boot-time page size
+ * the condition is only known at boot, so check it at run-time instead.
+ */
+#if PAGE_SIZE_MIN == PAGE_SIZE_MAX
+#define BUILD_BUG_ON_FIXED_PAGE_SIZE(cond) BUILD_BUG_ON(cond)
+#else
+#define BUILD_BUG_ON_FIXED_PAGE_SIZE(cond) VM_BUG_ON(cond)
+#endif
+#endif /* __ASSEMBLY__ */
+
+#endif /* ASM_GENERIC_PGTABLE_GEOMETRY_H */
diff --git a/init/main.c b/init/main.c
index f14780345051..3d619be281aa 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1053,6 +1053,8 @@ static void __init print_kernel_cmdline(const char *cmdline)
pr_notice("%s%s\n", KERNEL_CMDLINE_PREFIX, cmdline);
}
+static __init void do_ctors(void);
+
asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
void start_kernel(void)
{
@@ -1064,6 +1066,8 @@ void start_kernel(void)
debug_objects_early_init();
init_vmlinux_build_id();
+ do_ctors();
+
cgroup_init_early();
local_irq_disable();
@@ -1526,7 +1530,6 @@ static void __init do_basic_setup(void)
ksysfs_init();
driver_init();
init_irq_proc();
- do_ctors();
do_initcalls();
}
diff --git a/kernel/module/main.c b/kernel/module/main.c
index 34e0204ea37c..17145dc9c009 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -3127,7 +3127,6 @@ static noinline int do_init_module(struct module *mod)
freeinit->init_data = mod->mem[MOD_INIT_DATA].base;
freeinit->init_rodata = mod->mem[MOD_INIT_RODATA].base;
- do_mod_ctors(mod);
/* Start the module */
if (mod->init != NULL)
ret = do_one_initcall(mod->init);
@@ -3575,6 +3574,9 @@ static int load_module(struct load_info *info, const char __user *uargs,
mod->async_probe_requested = async_probe;
+ /* Constructors may set defaults that module params must override. */
+ do_mod_ctors(mod);
+
/* Module is ready to execute: parsing args may do that. */
after_dashes = parse_args(mod->name, args, mod->kp, mod->num_kp,
-32768, 32767, mod,
^ permalink raw reply related [flat|nested] 4+ messages in thread* [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX
[not found] <20261201015240.1017259-1-ericcurtin17@gmail.com>
2026-12-01 1:51 ` [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection Eric Curtin
@ 2026-12-01 1:51 ` Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
2026-12-01 1:52 ` [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level Eric Curtin
3 siblings, 0 replies; 4+ messages in thread
From: Eric Curtin @ 2026-12-01 1:51 UTC (permalink / raw)
To: linux-arm-kernel
Cc: Ryan Roberts, Arnd Bergmann, Dennis Zhou, Tejun Heo,
Christoph Lameter, linux-arch, linux-kernel, linux-mm
From: Ryan Roberts <ryan.roberts@arm.com>
Data that must be page aligned is laid out at build time, so it cannot
be aligned to a PAGE_SIZE that is only known at boot.
Use PAGE_SIZE_MAX for the alignment in vmlinux.lds.h (init text, rodata,
nosave, BTF, bss, percpu and the other page-aligned sections), in
__page_aligned_data and __page_aligned_bss, and in
DECLARE/DEFINE_PER_CPU_PAGE_ALIGNED(). This keeps the data aligned for
whichever page size is selected at boot.
PAGE_SIZE_MAX equals PAGE_SIZE when the page size is fixed, so there is
no change for compile-time page sizes.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit bb504904abc5ead0ce74f9e9c7d868f4f13c3815)
---
include/asm-generic/vmlinux.lds.h | 38 +++++++++++++++----------------
include/linux/linkage.h | 4 ++--
include/linux/percpu-defs.h | 4 ++--
3 files changed, 23 insertions(+), 23 deletions(-)
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 3eb732942d69..2a86599cb620 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -13,7 +13,7 @@
* . = START;
* __init_begin = .;
* HEAD_TEXT_SECTION
- * INIT_TEXT_SECTION(PAGE_SIZE)
+ * INIT_TEXT_SECTION(PAGE_SIZE_MAX)
* INIT_DATA_SECTION(...)
* PERCPU_SECTION(CACHELINE_SIZE)
* __init_end = .;
@@ -23,7 +23,7 @@
* _etext = .;
*
* _sdata = .;
- * RO_DATA(PAGE_SIZE)
+ * RO_DATA(PAGE_SIZE_MAX)
* RW_DATA(...)
* _edata = .;
*
@@ -397,10 +397,10 @@
* Data section helpers
*/
#define NOSAVE_DATA \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
__nosave_begin = .; \
*(.data..nosave) \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
__nosave_end = .;
#define CACHE_HOT_DATA(align) \
@@ -603,7 +603,7 @@
*(.text.unknown .text.unknown.*) \
TEXT_SPLIT \
TEXT_UNLIKELY \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
TEXT_HOT \
*(TEXT_MAIN .text.fixup) \
NOINSTR_TEXT \
@@ -677,7 +677,7 @@
*/
#ifdef CONFIG_DEBUG_INFO_BTF_INLINE
#define BTF_INLINE \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
.BTF.inline : AT(ADDR(.BTF.inline) - LOAD_OFFSET) { \
BOUNDED_SECTION_BY(.BTF.inline, _BTF_inline) \
}
@@ -693,7 +693,7 @@
#define BTF_LINK_SIZE (64 - 4 + 32 + 4)
#endif
#define BTF_INLINE_LINK \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
.BTF.inline.link : AT(ADDR(.BTF.inline.link) - LOAD_OFFSET) { \
__start_BTF_inline_link = .; \
BYTE(0) \
@@ -706,13 +706,13 @@
#ifdef CONFIG_DEBUG_INFO_BTF
#define BTF \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \
BOUNDED_SECTION_BY(.BTF, _BTF) \
} \
BTF_INLINE \
BTF_INLINE_LINK \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) { \
*(.BTF_ids) \
}
@@ -807,9 +807,9 @@
. = ALIGN(bss_align); \
.bss : AT(ADDR(.bss) - LOAD_OFFSET) { \
BSS_FIRST_SECTIONS \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
*(.bss..page_aligned) \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
*(.dynbss) \
*(BSS_MAIN) \
*(COMMON) \
@@ -1046,9 +1046,9 @@
*/
#ifdef CONFIG_AMD_MEM_ENCRYPT
#define PERCPU_DECRYPTED_SECTION \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
*(.data..percpu..decrypted) \
- . = ALIGN(PAGE_SIZE);
+ . = ALIGN(PAGE_SIZE_MAX);
#else
#define PERCPU_DECRYPTED_SECTION
#endif
@@ -1132,7 +1132,7 @@
*/
#define PERCPU_INPUT(cacheline) \
__per_cpu_start = .; \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
*(.data..percpu..page_aligned) \
. = ALIGN(cacheline); \
__per_cpu_hot_start = .; \
@@ -1156,7 +1156,7 @@
* sharing between subsections for different purposes.
*/
#define PERCPU_SECTION(cacheline) \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
.data..percpu : AT(ADDR(.data..percpu) - LOAD_OFFSET) { \
PERCPU_INPUT(cacheline) \
}
@@ -1173,15 +1173,15 @@
* All sections are combined in a single .data section.
* The sections following CONSTRUCTORS are arranged so their
* typical alignment matches.
- * A cacheline is typical/always less than a PAGE_SIZE so
+ * A cacheline is typical/always less than a PAGE_SIZE_MAX so
* the sections that has this restriction (or similar)
- * is located before the ones requiring PAGE_SIZE alignment.
- * NOSAVE_DATA starts and ends with a PAGE_SIZE alignment which
+ * is located before the ones requiring PAGE_SIZE_MAX alignment.
+ * NOSAVE_DATA starts and ends with a PAGE_SIZE_MAX alignment which
* matches the requirement of PAGE_ALIGNED_DATA.
*
* use 0 as page_align if page_aligned data is not used */
#define RW_DATA(cacheline, pagealigned, inittask) \
- . = ALIGN(PAGE_SIZE); \
+ . = ALIGN(PAGE_SIZE_MAX); \
.data : AT(ADDR(.data) - LOAD_OFFSET) { \
INIT_TASK_DATA(inittask) \
NOSAVE_DATA \
diff --git a/include/linux/linkage.h b/include/linux/linkage.h
index 53fe1f48fd28..3673440789c3 100644
--- a/include/linux/linkage.h
+++ b/include/linux/linkage.h
@@ -36,8 +36,8 @@
__stringify(name))
#endif
-#define __page_aligned_data __section(".data..page_aligned") __aligned(PAGE_SIZE)
-#define __page_aligned_bss __section(".bss..page_aligned") __aligned(PAGE_SIZE)
+#define __page_aligned_data __section(".data..page_aligned") __aligned(PAGE_SIZE_MAX)
+#define __page_aligned_bss __section(".bss..page_aligned") __aligned(PAGE_SIZE_MAX)
#ifndef __bss_pgtbl
#define __bss_pgtbl __page_aligned_bss
diff --git a/include/linux/percpu-defs.h b/include/linux/percpu-defs.h
index dbe3267a0a13..9e15c10ca6a5 100644
--- a/include/linux/percpu-defs.h
+++ b/include/linux/percpu-defs.h
@@ -153,11 +153,11 @@
*/
#define DECLARE_PER_CPU_PAGE_ALIGNED(type, name) \
DECLARE_PER_CPU_SECTION(type, name, "..page_aligned") \
- __aligned(PAGE_SIZE)
+ __aligned(PAGE_SIZE_MAX)
#define DEFINE_PER_CPU_PAGE_ALIGNED(type, name) \
DEFINE_PER_CPU_SECTION(type, name, "..page_aligned") \
- __aligned(PAGE_SIZE)
+ __aligned(PAGE_SIZE_MAX)
/*
* Declaration/definition used for per-CPU variables that must be read mostly.
^ permalink raw reply related [flat|nested] 4+ messages in thread* [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination
[not found] <20261201015240.1017259-1-ericcurtin17@gmail.com>
2026-12-01 1:51 ` [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
@ 2026-12-01 1:51 ` Eric Curtin
2026-12-01 1:52 ` [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level Eric Curtin
3 siblings, 0 replies; 4+ messages in thread
From: Eric Curtin @ 2026-12-01 1:51 UTC (permalink / raw)
To: linux-arm-kernel
Cc: Ryan Roberts, Vlastimil Babka, Arnd Bergmann, Ingo Molnar,
Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak, David Hildenbrand, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Michal Hocko, Kees Cook,
Andrey Ryabinin, Alexander Potapenko, Andrey Konovalov,
Dmitry Vyukov, Vincenzo Frascino, Andrew Morton, linux-arch,
linux-kernel, linux-mm, kasan-dev
From: Ryan Roberts <ryan.roberts@arm.com>
THREAD_SIZE is the size of a kernel thread stack. It has been a
compile-time constant, but with vmap stacks it must be a multiple of
PAGE_SIZE, so with boot-time page size selection it can no longer be
fixed at build time.
Defining THREAD_SIZE for the largest supported page size would waste
memory with smaller pages. For example, arm64 needs 16K stacks, but 64K
pages with a vmap stack require 64K; paying that with 4K or 16K pages
would waste 48K per kernel thread.
So allow THREAD_SIZE to be a runtime value. Add THREAD_SIZE_MAX and
THREAD_ALIGN_MAX, defaulting to THREAD_SIZE and
max(THREAD_ALIGN, THREAD_SIZE_MAX), and use THREAD_SIZE_MAX to size
init_stack, thread_union and the init stack in the linker script.
In fork.c, replace the compile-time split between the page allocator
and the kmem_cache based stack allocator with run-time checks of
THREAD_SIZE >= PAGE_SIZE, and make thread_stack_cache_init() return
early when stacks are page based. The weak stub in init/main.c is now
conditional on CONFIG_VMAP_STACK. KASAN's init_task_stack_addr() uses
THREAD_SIZE rather than sizeof() of the stack array.
No functional change when THREAD_SIZE is a compile-time constant.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 13e7b56091910f3533cee539b7c1ef857f61fe4d)
---
include/asm-generic/vmlinux.lds.h | 6 +++-
include/linux/sched.h | 4 +--
include/linux/thread_info.h | 10 +++++-
init/main.c | 2 +-
kernel/fork.c | 53 ++++++++++---------------------
mm/kasan/report.c | 3 +-
6 files changed, 35 insertions(+), 43 deletions(-)
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 2a86599cb620..4df6c8fd38cb 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -56,6 +56,10 @@
#define LOAD_OFFSET 0
#endif
+#ifndef THREAD_SIZE_MAX
+#define THREAD_SIZE_MAX THREAD_SIZE
+#endif
+
/*
* Only some architectures want to have the .notes segment visible in
* a separate PT_NOTE ELF Program Header. When this happens, it needs
@@ -428,7 +432,7 @@
init_thread_union = .; \
init_stack = .; \
KEEP(*(.data..init_thread_info)) \
- . = __start_init_stack + THREAD_SIZE; \
+ . = __start_init_stack + THREAD_SIZE_MAX; \
__end_init_stack = .;
#define JUMP_TABLE_DATA \
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d7855..2b649bbd668a 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1998,14 +1998,14 @@ union thread_union {
#ifndef CONFIG_THREAD_INFO_IN_TASK
struct thread_info thread_info;
#endif
- unsigned long stack[THREAD_SIZE/sizeof(long)];
+ unsigned long stack[THREAD_SIZE_MAX/sizeof(long)];
};
#ifndef CONFIG_THREAD_INFO_IN_TASK
extern struct thread_info init_thread_info;
#endif
-extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
+extern unsigned long init_stack[THREAD_SIZE_MAX / sizeof(unsigned long)];
#ifdef CONFIG_THREAD_INFO_IN_TASK
# define task_thread_info(task) (&(task)->thread_info)
diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index 307b8390fc67..73d0a77a3c2c 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -89,7 +89,15 @@ static inline long set_restart_fn(struct restart_block *restart,
}
#ifndef THREAD_ALIGN
-#define THREAD_ALIGN THREAD_SIZE
+#define THREAD_ALIGN THREAD_SIZE
+#endif
+
+#ifndef THREAD_SIZE_MAX
+#define THREAD_SIZE_MAX THREAD_SIZE
+#endif
+
+#ifndef THREAD_ALIGN_MAX
+#define THREAD_ALIGN_MAX max(THREAD_ALIGN, THREAD_SIZE_MAX)
#endif
#define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_SKIP_KASAN)
diff --git a/init/main.c b/init/main.c
index 3d619be281aa..2a44990521eb 100644
--- a/init/main.c
+++ b/init/main.c
@@ -849,7 +849,7 @@ void __init __weak smp_prepare_boot_cpu(void)
{
}
-# if THREAD_SIZE >= PAGE_SIZE
+#ifdef CONFIG_VMAP_STACK
void __init __weak thread_stack_cache_init(void)
{
}
diff --git a/kernel/fork.c b/kernel/fork.c
index 5a0db5a89615..87c102ea8d62 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -393,45 +393,15 @@ static void free_thread_stack(struct task_struct *tsk)
* Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
* kmemcache based allocator.
*/
-#if THREAD_SIZE >= PAGE_SIZE
-
-static void thread_stack_free_rcu(struct rcu_head *rh)
-{
- __free_pages(virt_to_page(rh), THREAD_SIZE_ORDER);
-}
-
-static void thread_stack_delayed_free(struct task_struct *tsk)
-{
- struct rcu_head *rh = tsk->stack;
-
- call_rcu(rh, thread_stack_free_rcu);
-}
-
-static int alloc_thread_stack_node(struct task_struct *tsk, int node)
-{
- struct page *page = alloc_pages_node(node, THREADINFO_GFP,
- THREAD_SIZE_ORDER);
-
- if (likely(page)) {
- tsk->stack = kasan_reset_tag(page_address(page));
- return 0;
- }
- return -ENOMEM;
-}
-
-static void free_thread_stack(struct task_struct *tsk)
-{
- thread_stack_delayed_free(tsk);
- tsk->stack = NULL;
-}
-
-#else /* !(THREAD_SIZE >= PAGE_SIZE) */
static struct kmem_cache *thread_stack_cache;
static void thread_stack_free_rcu(struct rcu_head *rh)
{
- kmem_cache_free(thread_stack_cache, rh);
+ if (THREAD_SIZE >= PAGE_SIZE)
+ __free_pages(virt_to_page(rh), THREAD_SIZE_ORDER);
+ else
+ kmem_cache_free(thread_stack_cache, rh);
}
static void thread_stack_delayed_free(struct task_struct *tsk)
@@ -444,7 +414,16 @@ static void thread_stack_delayed_free(struct task_struct *tsk)
static int alloc_thread_stack_node(struct task_struct *tsk, int node)
{
unsigned long *stack;
- stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
+ struct page *page;
+
+ if (THREAD_SIZE >= PAGE_SIZE) {
+ page = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER);
+ stack = likely(page) ? page_address(page) : NULL;
+ } else {
+ stack = kmem_cache_alloc_node(thread_stack_cache,
+ THREADINFO_GFP, node);
+ }
+
stack = kasan_reset_tag(stack);
tsk->stack = stack;
return stack ? 0 : -ENOMEM;
@@ -458,13 +437,15 @@ static void free_thread_stack(struct task_struct *tsk)
void thread_stack_cache_init(void)
{
+ if (THREAD_SIZE >= PAGE_SIZE)
+ return;
+
thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
THREAD_SIZE, THREAD_SIZE, 0, 0,
THREAD_SIZE, NULL);
BUG_ON(thread_stack_cache == NULL);
}
-#endif /* THREAD_SIZE >= PAGE_SIZE */
#endif /* CONFIG_VMAP_STACK */
/* SLAB cache for signal_struct structures (tsk->signal) */
diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index e804b1e1f886..7b170efd224b 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -366,8 +366,7 @@ static inline bool kernel_or_module_addr(const void *addr)
static inline bool init_task_stack_addr(const void *addr)
{
return addr >= (void *)&init_thread_union.stack &&
- (addr <= (void *)&init_thread_union.stack +
- sizeof(init_thread_union.stack));
+ (addr <= (void *)&init_thread_union.stack + THREAD_SIZE);
}
static void print_address_description(void *addr, u8 tag,
^ permalink raw reply related [flat|nested] 4+ messages in thread* [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level
[not found] <20261201015240.1017259-1-ericcurtin17@gmail.com>
` (2 preceding siblings ...)
2026-12-01 1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
@ 2026-12-01 1:52 ` Eric Curtin
3 siblings, 0 replies; 4+ messages in thread
From: Eric Curtin @ 2026-12-01 1:52 UTC (permalink / raw)
To: linux-arm-kernel
Cc: Ryan Roberts, Catalin Marinas, Will Deacon, Mark Rutland,
Aneesh Kumar K.V, Andrew Morton, Nick Piggin, Peter Zijlstra,
Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, linux-arch, linux-mm, kvmarm
From: Ryan Roberts <ryan.roberts@arm.com>
For a given VA size the number of translation levels depends on the
page size, so with boot-time page size selection it is not known until
boot how many levels are needed. 16K pages with 36-bit VAs and 64K pages
with 42-bit VAs, for example, need only two levels. The p4d and pud
levels can already be folded at runtime for the LPA2 fallback paths, and
the pmd level now needs the same treatment.
Add pgtable_l3_enabled() and use it, as the p4d and pud equivalents are
used, to make the pud_* helpers, set_pmd(), pmd_offset() and friends,
pmd_set_fixmap(), pmd_free(), __pmd_free_tlb() and mm_pmd_folded()
behave as folded when the pmd level is absent. The same check replaces
__PAGETABLE_PMD_FOLDED and CONFIG_PGTABLE_LEVELS tests in the KVM
stage-2 VMA size code, the hugetlb size helpers, the fixmap, the KPTI
level count, free_empty_pmd_table() and the ptdump level fixup. The
levels check in arch_mm_preinit() uses ptg_pgtable_levels.
pgtable_l3_enabled() is only the compile-time answer for now, so there
is no functional change.
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Eric Curtin <eric.curtin@docker.com>
(cherry picked from commit 203ac9561f66a4947887e902541b403935c4fa1f)
---
arch/arm64/include/asm/pgalloc.h | 15 +++-
arch/arm64/include/asm/pgtable.h | 115 +++++++++++++++++++++++--------
arch/arm64/include/asm/tlb.h | 3 +
arch/arm64/kvm/mmu.c | 6 +-
arch/arm64/mm/fixmap.c | 2 +-
arch/arm64/mm/hugetlbpage.c | 9 +--
arch/arm64/mm/init.c | 5 +-
arch/arm64/mm/mmu.c | 6 +-
arch/arm64/mm/ptdump.c | 6 +-
9 files changed, 120 insertions(+), 47 deletions(-)
diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h
index 1b4509d3382c..c79c7f43234b 100644
--- a/arch/arm64/include/asm/pgalloc.h
+++ b/arch/arm64/include/asm/pgalloc.h
@@ -15,6 +15,7 @@
#define __HAVE_ARCH_PGD_FREE
#define __HAVE_ARCH_PUD_FREE
+#define __HAVE_ARCH_PMD_FREE
#include <asm-generic/pgalloc.h>
#define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
@@ -23,7 +24,8 @@
static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
{
- set_pud(pudp, __pud(__phys_to_pud_val(pmdp) | prot));
+ if (pgtable_l3_enabled())
+ set_pud(pudp, __pud(__phys_to_pud_val(pmdp) | prot));
}
static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)
@@ -33,6 +35,17 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)
pudval |= (mm == &init_mm) ? PUD_TABLE_UXN : PUD_TABLE_PXN;
__pud_populate(pudp, __pa(pmdp), pudval);
}
+
+static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
+{
+ struct ptdesc *ptdesc = virt_to_ptdesc(pmd);
+
+ if (!pgtable_l3_enabled())
+ return;
+
+ BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
+ pagetable_dtor_free(ptdesc);
+}
#else
static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
{
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index a474c9fb485a..c625fe7f8452 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -760,12 +760,14 @@ static inline int pmd_trans_huge(pmd_t pmd)
}
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
-#if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS < 3
-static inline bool pud_table(pud_t pud) { return true; }
-#else
-#define pud_table(pud) ((pud_val(pud) & PUD_TYPE_MASK) == \
- PUD_TYPE_TABLE)
-#endif
+static inline bool pgtable_l3_enabled(void);
+
+static inline bool pud_table(pud_t pud)
+{
+ if (PAGE_SIZE == SZ_64K || !pgtable_l3_enabled())
+ return true;
+ return (pud_val(pud) & PUD_TYPE_MASK) == PUD_TYPE_TABLE;
+}
extern pgd_t swapper_pg_dir[];
extern pgd_t idmap_pg_dir[];
@@ -782,12 +784,10 @@ static inline bool in_swapper_pgdir(void *addr)
static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
{
-#ifdef __PAGETABLE_PMD_FOLDED
- if (in_swapper_pgdir(pmdp)) {
+ if (!pgtable_l3_enabled() && in_swapper_pgdir(pmdp)) {
set_swapper_pgd((pgd_t *)pmdp, __pgd(pmd_val(pmd)));
return;
}
-#endif /* __PAGETABLE_PMD_FOLDED */
WRITE_ONCE(*pmdp, pmd);
@@ -824,22 +824,29 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
#if CONFIG_PGTABLE_LEVELS > 2
-#define pud_none(pud) (!pud_val(pud))
-#define pud_bad(pud) ((pud_val(pud) & PUD_TYPE_MASK) != \
- PUD_TYPE_TABLE)
-#define pud_present(pud) pte_present(pud_pte(pud))
-#ifndef __PAGETABLE_PMD_FOLDED
+static __always_inline bool pgtable_l3_enabled(void)
+{
+ return true;
+}
+
+static inline bool mm_pmd_folded(const struct mm_struct *mm)
+{
+ return !pgtable_l3_enabled();
+}
+#define mm_pmd_folded mm_pmd_folded
+
+#define pud_none(pud) (pgtable_l3_enabled() && !pud_val(pud))
+#define pud_bad(pud) (pgtable_l3_enabled() && !pud_table(pud))
+#define pud_present(pud) (!pgtable_l3_enabled() || pte_present(pud_pte(pud)))
#define pud_leaf pud_leaf
static inline bool pud_leaf(pud_t pud)
{
- return pud_present(pud) && !pud_table(pud);
+ return pgtable_l3_enabled() && pte_present(pud_pte(pud)) &&
+ !pud_table(pud);
}
-#else
-#define pud_leaf(pud) false
-#endif
-#define pud_valid(pud) pte_valid(pud_pte(pud))
-#define pud_user(pud) pte_user(pud_pte(pud))
-#define pud_user_exec(pud) pte_user_exec(pud_pte(pud))
+#define pud_valid(pud) (pgtable_l3_enabled() && pte_valid(pud_pte(pud)))
+#define pud_user(pud) (pgtable_l3_enabled() && pte_user(pud_pte(pud)))
+#define pud_user_exec(pud) (pgtable_l3_enabled() && pte_user_exec(pud_pte(pud)))
static inline bool pgtable_l4_enabled(void);
@@ -858,7 +865,8 @@ static inline void set_pud(pud_t *pudp, pud_t pud)
static inline void pud_clear(pud_t *pudp)
{
- set_pud(pudp, __pud(0));
+ if (pgtable_l3_enabled())
+ set_pud(pudp, __pud(0));
}
static inline phys_addr_t pud_page_paddr(pud_t pud)
@@ -866,25 +874,74 @@ static inline phys_addr_t pud_page_paddr(pud_t pud)
return __pud_to_phys(pud);
}
+#define pmd_index(addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
+
+static inline pmd_t *pud_to_folded_pmd(pud_t *pudp, unsigned long addr)
+{
+ return (pmd_t *)pudp;
+}
+
static inline pmd_t *pud_pgtable(pud_t pud)
{
return (pmd_t *)__va(pud_page_paddr(pud));
}
-/* Find an entry in the second-level page table. */
-#define pmd_offset_phys(dir, addr) (pud_page_paddr(READ_ONCE(*(dir))) + pmd_index(addr) * sizeof(pmd_t))
+static inline phys_addr_t pmd_offset_phys(pud_t *pudp, unsigned long addr)
+{
+ BUG_ON(!pgtable_l3_enabled());
-#define pmd_set_fixmap(addr) ((pmd_t *)set_fixmap_offset(FIX_PMD, addr))
-#define pmd_set_fixmap_offset(pud, addr) pmd_set_fixmap(pmd_offset_phys(pud, addr))
-#define pmd_clear_fixmap() clear_fixmap(FIX_PMD)
+ return pud_page_paddr(READ_ONCE(*pudp)) + pmd_index(addr) * sizeof(pmd_t);
+}
-#define pud_page(pud) phys_to_page(__pud_to_phys(pud))
+static inline pmd_t *pmd_offset_lockless(pud_t *pudp, pud_t pud,
+ unsigned long addr)
+{
+ if (!pgtable_l3_enabled())
+ return pud_to_folded_pmd(pudp, addr);
+ return (pmd_t *)__va(pud_page_paddr(pud)) + pmd_index(addr);
+}
+#define pmd_offset_lockless pmd_offset_lockless
+
+static inline pmd_t *pmd_offset(pud_t *pudp, unsigned long addr)
+{
+ return pmd_offset_lockless(pudp, READ_ONCE(*pudp), addr);
+}
+#define pmd_offset pmd_offset
+
+static inline pmd_t *pmd_set_fixmap(unsigned long addr)
+{
+ if (!pgtable_l3_enabled())
+ return NULL;
+ return (pmd_t *)set_fixmap_offset(FIX_PMD, addr);
+}
+
+static inline pmd_t *pmd_set_fixmap_offset(pud_t *pudp, unsigned long addr)
+{
+ if (!pgtable_l3_enabled())
+ return pud_to_folded_pmd(pudp, addr);
+ return pmd_set_fixmap(pmd_offset_phys(pudp, addr));
+}
+
+static inline void pmd_clear_fixmap(void)
+{
+ if (pgtable_l3_enabled())
+ clear_fixmap(FIX_PMD);
+}
/* use ONLY for statically allocated translation tables */
-#define pmd_offset_kimg(dir,addr) ((pmd_t *)__phys_to_kimg(pmd_offset_phys((dir), (addr))))
+static inline pmd_t *pmd_offset_kimg(pud_t *pudp, u64 addr)
+{
+ if (!pgtable_l3_enabled())
+ return pud_to_folded_pmd(pudp, addr);
+ return (pmd_t *)__phys_to_kimg(pmd_offset_phys(pudp, addr));
+}
+
+#define pud_page(pud) phys_to_page(__pud_to_phys(pud))
#else
+static inline bool pgtable_l3_enabled(void) { return false; }
+
#define pud_valid(pud) false
#define pud_page_paddr(pud) ({ BUILD_BUG(); 0; })
#define pud_user_exec(pud) pud_user(pud) /* Always 0 with folding */
diff --git a/arch/arm64/include/asm/tlb.h b/arch/arm64/include/asm/tlb.h
index 751bd57bc3ba..3197ed0702b4 100644
--- a/arch/arm64/include/asm/tlb.h
+++ b/arch/arm64/include/asm/tlb.h
@@ -87,6 +87,9 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
{
struct ptdesc *ptdesc = virt_to_ptdesc(pmdp);
+ if (!pgtable_l3_enabled())
+ return;
+
tlb_remove_ptdesc(tlb, ptdesc);
}
#endif
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index c3a5ee486ae8..b15d59ec0bee 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1929,13 +1929,11 @@ static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd,
vma_shift = get_vma_page_shift(vma);
}
-#ifndef __PAGETABLE_PMD_FOLDED
- if (vma_shift == PUD_SHIFT) {
+ if (pgtable_l3_enabled() && vma_shift == PUD_SHIFT) {
if (!fault_supports_stage2_huge_mapping(s2fd, PUD_SIZE))
vma_shift = PMD_SHIFT;
}
-#endif
- if (vma_shift == CONT_PMD_SHIFT) {
+ if (vma_shift == CONT_PMD_SHIFT)
vma_shift = PMD_SHIFT;
if ((vma_shift == PMD_SHIFT &&
!fault_supports_stage2_huge_mapping(s2fd, PMD_SIZE)) ||
diff --git a/arch/arm64/mm/fixmap.c b/arch/arm64/mm/fixmap.c
index 46e08631d08e..0d1c1798e52c 100644
--- a/arch/arm64/mm/fixmap.c
+++ b/arch/arm64/mm/fixmap.c
@@ -89,7 +89,7 @@ static void __init early_fixmap_init_pud(p4d_t *p4dp, unsigned long addr,
p4d_t p4d = READ_ONCE(*p4dp);
pud_t *pudp;
- if (CONFIG_PGTABLE_LEVELS > 3 && !p4d_none(p4d) &&
+ if (ptg_pgtable_levels > 3 && !p4d_none(p4d) &&
p4d_page_paddr(p4d) != __pa_symbol(bm_pud)) {
/*
* We only end up here if the kernel mapping and the fixmap
diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c
index 447e832ad512..efe0f90e041e 100644
--- a/arch/arm64/mm/hugetlbpage.c
+++ b/arch/arm64/mm/hugetlbpage.c
@@ -47,10 +47,9 @@ unsigned int arch_hugetlb_cma_order(void)
static bool __hugetlb_valid_size(unsigned long size)
{
-#ifndef __PAGETABLE_PMD_FOLDED
- if (size == PUD_SIZE)
+ if (pgtable_l3_enabled() && size == PUD_SIZE)
return pud_sect_supported();
-#endif
+
if (size == CONT_PMD_SIZE || size == PMD_SIZE || size == CONT_PTE_SIZE)
return true;
@@ -314,10 +313,8 @@ unsigned long hugetlb_mask_last_page(struct hstate *h)
{
unsigned long hp_size = huge_page_size(h);
-#ifndef __PAGETABLE_PMD_FOLDED
- if (hp_size == PUD_SIZE && pud_sect_supported())
+ if (pgtable_l3_enabled() && hp_size == PUD_SIZE && pud_sect_supported())
return PGDIR_SIZE - PUD_SIZE;
-#endif
if (hp_size == CONT_PMD_SIZE)
return PUD_SIZE - CONT_PMD_SIZE;
if (hp_size == PMD_SIZE)
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index f3229c921d9c..e6c15b06dc46 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -362,8 +362,9 @@ void __init arch_mm_preinit(void)
* Selected page table levels should match when derived from
* scratch using the virtual address range and page size.
*/
- VM_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
- CONFIG_PGTABLE_LEVELS);
+ BUILD_BUG_ON_FIXED_PAGE_SIZE(
+ ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
+ ptg_pgtable_levels);
if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
extern int sysctl_overcommit_memory;
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 9bc4c0b4b4d5..731633c64557 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1322,8 +1322,10 @@ static int __init __kpti_install_ng_mappings(void *__unused)
if (levels == 5 && !pgtable_l5_enabled())
levels = 4;
- else if (levels == 4 && !pgtable_l4_enabled())
+ if (levels == 4 && !pgtable_l4_enabled())
levels = 3;
+ if (levels == 3 && !pgtable_l3_enabled())
+ levels = 2;
remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
@@ -1726,7 +1728,7 @@ static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
free_empty_pte_table(pmdp, addr, next, floor, ceiling);
} while (addr = next, addr < end);
- if (CONFIG_PGTABLE_LEVELS <= 2)
+ if (!pgtable_l3_enabled())
return;
if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
diff --git a/arch/arm64/mm/ptdump.c b/arch/arm64/mm/ptdump.c
index 9d9bcb6738f4..6f9ddaa5da7b 100644
--- a/arch/arm64/mm/ptdump.c
+++ b/arch/arm64/mm/ptdump.c
@@ -197,8 +197,10 @@ void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
ptval_t prot = 0;
/* check if the current level has been folded dynamically */
- if (st->mm && ((level == 1 && mm_p4d_folded(st->mm)) ||
- (level == 2 && mm_pud_folded(st->mm))))
+ if (st->mm &&
+ ((level == 1 && mm_p4d_folded(st->mm)) ||
+ (level == 2 && mm_pud_folded(st->mm)) ||
+ (level == 3 && mm_pmd_folded(st->mm))))
level = 0;
if (level >= 0)
^ permalink raw reply related [flat|nested] 4+ messages in thread