From: Eric Curtin <ericcurtin17@gmail.com>
To: linux-arm-kernel@lists.infradead.org
Cc: Ryan Roberts <ryan.roberts@arm.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org
Subject: [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection
Date: Tue, 1 Dec 2026 01:51:33 +0000 [thread overview]
Message-ID: <20261201015240.1017259-2-ericcurtin17@gmail.com> (raw)
In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com>
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,
next prev parent reply other threads:[~2026-12-01 1:52 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
2026-12-01 1:51 ` Eric Curtin [this message]
2026-12-01 1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 05/43] mm: Avoid split pmd ptl if pmd level is run-time folded Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 06/43] mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 11/43] perf/core: Handle a boot-time PAGE_SIZE in format attrs and mlock Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 16/43] arm64: Introduce macros required for boot-time page size selection Eric Curtin
2026-12-01 1:51 ` [RFC PATCH v2 21/43] arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES Eric Curtin
2026-12-01 1:52 ` [RFC PATCH v2 37/43] vdso: Lay out the data pages in VDSO_PAGE_SIZE units Eric Curtin
2026-12-01 1:52 ` [RFC PATCH v2 39/43] crash: Size the vmcoreinfo page array for the smallest page size Eric Curtin
2026-12-01 1:52 ` [RFC PATCH v2 41/43] exit: Use THREAD_SIZE_MAX for lowest_to_date Eric Curtin
2026-12-01 1:52 ` [RFC PATCH v2 42/43] tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page Eric Curtin
2026-12-01 1:52 ` [RFC PATCH v2 43/43] arm64: Enable boot-time page size selection Eric Curtin
2026-12-01 10:41 ` [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Lorenzo Stoakes (ARM)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201015240.1017259-2-ericcurtin17@gmail.com \
--to=ericcurtin17@gmail.com \
--cc=catalin.marinas@arm.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ryan.roberts@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox