Skip to contentSkip to conversation

Boot-time page size selection for arm64

RFC PATCH In review Ryan Roberts 34 participants 196 messages Oct 14, 2024 – Dec 6, 2024 Page 2 of 2

Subsystems (93)
lore
Share & tools
lore
author reviewer
#120 lore raw

RFC PATCH 49/57 arm64: Convert BUILD_BUG_ON to VM_BUG_ON

In reply to Ryan Roberts

There are some build bug checks that will no longer compile for boot-time page size because the values they are testing are no longer compile-time constants. Resolve these by converting them to VM_BUG_ON, which will perform a runtime check.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/mm/init.c | 6 +++---
 arch/arm64/mm/mmu.c  | 4 ++--
 arch/arm64/mm/pgd.c  | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
arch/arm64/mm/init.c +3 -3
index 42eb246949072..4d24034418b39 100644
@@ -388,15 +388,15 @@ void __init mem_init(void)
 	 * detected at build time already.
 	 */
 #ifdef CONFIG_COMPAT
-	BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
+	VM_BUG_ON(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 +2 -2
index b4cd3b6a73c22..ad7fd3fda705a 100644
@@ -639,8 +639,8 @@ static void __init map_mem(pgd_t *pgdp)
 	 * 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);
+	VM_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end) &&
+		  pgd_index(_PAGE_OFFSET(VA_BITS_MIN)) != PTRS_PER_PGD - 1);
 
 	early_kfence_pool = arm64_kfence_alloc_pool();
 
arch/arm64/mm/pgd.c +1 -1
index 0c501cabc2384..4b106510358b1 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(PGD_SIZE < 64);
+	VM_BUG_ON(PGD_SIZE < 64);
 #endif
 
 	/*
Signature · 1 line
2.43.0
author reviewer
#121 lore raw

RFC PATCH 50/57 arm64: Remove PAGE_SZ asm-offset

In reply to Ryan Roberts

PAGE_SZ is not used anywhere and for boot-time builds, where it is not a compile-time constant, we don't know the value anyway. So let's remove it.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/kernel/asm-offsets.c | 2 --
 1 file changed, 2 deletions(-)
arch/arm64/kernel/asm-offsets.c +0 -2
index 27de1dddb0abe..f32b8d7f00b2a 100644
@@ -111,8 +111,6 @@ int main(void)
   BLANK();
   DEFINE(VM_EXEC,	       	VM_EXEC);
   BLANK();
-  DEFINE(PAGE_SZ,	       	PAGE_SIZE);
-  BLANK();
   DEFINE(DMA_TO_DEVICE,		DMA_TO_DEVICE);
   DEFINE(DMA_FROM_DEVICE,	DMA_FROM_DEVICE);
   BLANK();
Signature · 1 line
2.43.0
author reviewer
#122 lore raw

RFC PATCH 51/57 arm64: Introduce cpu features for page sizes

In reply to Ryan Roberts

Introduce one boot cpu feature per page size, ARM64_USE_PAGE_SIZE_*K. These will enable use of the alternatives framework to patch code based on the page size selected at boot.

Additionally, this provides a neat way to figure out what page size is in use during boot from the kernel log.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/kernel/cpufeature.c | 25 +++++++++++++++++++++++++
 arch/arm64/tools/cpucaps       |  3 +++
 2 files changed, 28 insertions(+)
arch/arm64/kernel/cpufeature.c +25 -0
index 7705c9c0e7142..e5618423bb99d 100644
@@ -1831,6 +1831,13 @@ static bool has_nv1(const struct arm64_cpu_capabilities *entry, int scope)
 		  is_midr_in_range_list(read_cpuid_id(), 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;
@@ -2881,6 +2888,24 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		.matches = has_nv1,
 		ARM64_CPUID_FIELDS_NEG(ID_AA64MMFR4_EL1, E2H0, NI_NV1)
 	},
+	{
+		.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 ac3429d892b9a..5cb75675303c6 100644
@@ -71,6 +71,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
Signature · 1 line
2.43.0
author reviewer
#123 lore raw

RFC PATCH 52/57 arm64: Remove PAGE_SIZE from assembly code

In reply to Ryan Roberts

Remove usage of PAGE_SHIFT, PAGE_SIZE and PAGE_MASK macros from assembly code since these are no longer compile-time constants when boot-time page size is in use.

For the most part, they are replaced with run-time lookups based on the value of TG0. This is done outside of loops so while there is a cost of a few extra instructions, performance should not be impacted.

However, invalid_host_el2_vect requires that the page shift be an immediate since it has no registers to spare. So for this, let's use alternatives patching. This code is guarranteed not to run until after patching is complete.

__pi_copy_page has no registers to spare to hold the page size, and we want to avoid having to reload it on every iteration of the loop. Since I couldn't provably conclude that the function is not called prior to alternatives patching, I opted to make a copy of the function for each page size and branch to the right one at the start.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 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      | 10 ++++++++-
 arch/arm64/lib/clear_page.S         |  7 ++++--
 arch/arm64/lib/copy_page.S          | 33 +++++++++++++++++++++--------
 arch/arm64/lib/mte.S                | 27 +++++++++++++++++------
 7 files changed, 85 insertions(+), 26 deletions(-)
arch/arm64/include/asm/assembler.h +15 -3
index 77c2d707adb1a..6424fd6be1cbe 100644
@@ -495,9 +495,11 @@ alternative_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]
@@ -508,7 +510,7 @@ alternative_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
 
@@ -911,4 +913,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 0e1d9c3c6a933..375b2fcf82e84 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 413f899e4ac63..bc4f37fba6c74 100644
@@ -46,6 +46,10 @@ SYM_CODE_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 */
@@ -54,7 +58,7 @@ SYM_CODE_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:
@@ -62,8 +66,8 @@ SYM_CODE_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 civac, sy, x19, x1, x15, x20
 	b	.Lnext
 .Ltest_indirection:
arch/arm64/kvm/hyp/nvhe/host.S +9 -1
index 3d610fc51f4d3..2b0d583fcf1af 100644
@@ -193,7 +193,15 @@ SYM_FUNC_END(__host_hvc)
 	 */
 	add	sp, sp, x0			// sp' = sp + x0
 	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
-	tbz	x0, #PAGE_SHIFT, .L__hyp_sp_overflow\@
+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
 	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/clear_page.S +5 -2
index ebde40e7fa2b2..b6f2cb8d704cc 100644
@@ -15,6 +15,9 @@
  *	x0 - dest
  */
 SYM_FUNC_START(__pi_clear_page)
+	get_page_size x3
+	sub	x3, x3, #1	/* (PAGE_SIZE - 1) in x3 */
+
 	mrs	x1, dczid_el0
 	tbnz	x1, #4, 2f	/* Branch if DC ZVA is prohibited */
 	and	w1, w1, #0xf
@@ -23,7 +26,7 @@ SYM_FUNC_START(__pi_clear_page)
 
 1:	dc	zva, x0
 	add	x0, x0, x1
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, x3
 	b.ne	1b
 	ret
 
@@ -32,7 +35,7 @@ SYM_FUNC_START(__pi_clear_page)
 	stnp	xzr, xzr, [x0, #32]
 	stnp	xzr, xzr, [x0, #48]
 	add	x0, x0, #64
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, x3
 	b.ne	2b
 	ret
 SYM_FUNC_END(__pi_clear_page)
arch/arm64/lib/copy_page.S +24 -9
index 6a56d7cf309da..6c19b03ab4d69 100644
@@ -10,14 +10,7 @@
 #include <asm/cpufeature.h>
 #include <asm/alternative.h>
 
-/*
- * Copy a page from src to dest (both are page aligned)
- *
- * Parameters:
- *	x0 - dest
- *	x1 - src
- */
-SYM_FUNC_START(__pi_copy_page)
+	.macro	copy_page_body, page_size
 	ldp	x2, x3, [x1]
 	ldp	x4, x5, [x1, #16]
 	ldp	x6, x7, [x1, #32]
@@ -30,7 +23,7 @@ SYM_FUNC_START(__pi_copy_page)
 	add	x0, x0, #256
 	add	x1, x1, #128
 1:
-	tst	x0, #(PAGE_SIZE - 1)
+	tst	x0, #(\page_size - 1)
 
 	stnp	x2, x3, [x0, #-256]
 	ldp	x2, x3, [x1]
@@ -62,7 +55,29 @@ SYM_FUNC_START(__pi_copy_page)
 	stnp	x12, x13, [x0, #80 - 256]
 	stnp	x14, x15, [x0, #96 - 256]
 	stnp	x16, x17, [x0, #112 - 256]
+	.endm
 
+/*
+ * Copy a page from src to dest (both are page aligned)
+ *
+ * Parameters:
+ *	x0 - dest
+ *	x1 - src
+ */
+SYM_FUNC_START(__pi_copy_page)
+	get_tg0	x2
+.Lsz_64k:
+	cmp	x2, #TCR_TG0_64K
+	b.ne	.Lsz_16k
+	copy_page_body SZ_64K
+	ret
+.Lsz_16k:
+	cmp	x2, #TCR_TG0_16K
+	b.ne	.Lsz_4k
+	copy_page_body SZ_16K
+	ret
+.Lsz_4k:
+	copy_page_body SZ_4K
 	ret
 SYM_FUNC_END(__pi_copy_page)
 SYM_FUNC_ALIAS(copy_page, __pi_copy_page)
arch/arm64/lib/mte.S +21 -6
index 5018ac03b6bf3..b4f6f5be0ec79 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
Signature · 1 line
2.43.0
author reviewer
#124 lore raw

RFC PATCH 53/57 arm64: Runtime-fold pmd level

In reply to Ryan Roberts

For a given VA size, the number of levels of lookup depends on the page size. With boot-time page size selection, we therefore don't know how many levels of lookup we require until boot time. So we need to runtime-fold some levels of lookup.

We already have code to runtime-fold p4d and pud levels; that exists for LPA2 fallback paths and can be repurposed for our needs. But pmd level also needs to support runtime folding; for example, 16K/36-bit and 64K/42-bit configs require only 2 levels.

So let's add the required code. However, note that until we actually add the boot-time page size config, pgtable_l3_enabled() simply returns the compile-time determined answer.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/pgalloc.h |  16 +++-
 arch/arm64/include/asm/pgtable.h | 123 +++++++++++++++++++++++--------
 arch/arm64/include/asm/tlb.h     |   3 +
 arch/arm64/kernel/cpufeature.c   |   4 +-
 arch/arm64/kvm/mmu.c             |   9 +--
 arch/arm64/mm/fixmap.c           |   2 +-
 arch/arm64/mm/hugetlbpage.c      |  16 ++--
 arch/arm64/mm/init.c             |   2 +-
 arch/arm64/mm/mmu.c              |   2 +-
 arch/arm64/mm/ptdump.c           |   3 +-
 10 files changed, 126 insertions(+), 54 deletions(-)
arch/arm64/include/asm/pgalloc.h +15 -1
index 8ff5f2a2579e4..51cc2f32931d2 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,18 @@ 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_pmd_dtor(ptdesc);
+	pagetable_free(ptdesc);
+}
 #else
 static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
 {
arch/arm64/include/asm/pgtable.h +92 -31

171 lines not on this page · raw message

arch/arm64/include/asm/tlb.h +3 -0
index a947c6e784ed2..527630f0803c6 100644
@@ -92,6 +92,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;
+
 	pagetable_pmd_dtor(ptdesc);
 	tlb_remove_ptdesc(tlb, ptdesc);
 }
arch/arm64/kernel/cpufeature.c +3 -1
index e5618423bb99d..663cc76569a27 100644
@@ -1923,8 +1923,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);
 
arch/arm64/kvm/mmu.c +3 -6
index 248a2d7ad6dbb..146ecdaaaf647 100644
@@ -1370,12 +1370,11 @@ static int get_vma_page_shift(struct vm_area_struct *vma, unsigned long hva)
 
 	pa = (vma->vm_pgoff << PAGE_SHIFT) + (hva - vma->vm_start);
 
-#ifndef __PAGETABLE_PMD_FOLDED
-	if ((hva & (PUD_SIZE - 1)) == (pa & (PUD_SIZE - 1)) &&
+	if (pgtable_l3_enabled() &&
+	    (hva & (PUD_SIZE - 1)) == (pa & (PUD_SIZE - 1)) &&
 	    ALIGN_DOWN(hva, PUD_SIZE) >= vma->vm_start &&
 	    ALIGN(hva, PUD_SIZE) <= vma->vm_end)
 		return PUD_SHIFT;
-#endif
 
 	if ((hva & (PMD_SIZE - 1)) == (pa & (PMD_SIZE - 1)) &&
 	    ALIGN_DOWN(hva, PMD_SIZE) >= vma->vm_start &&
@@ -1487,12 +1486,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		vma_shift = get_vma_page_shift(vma, hva);
 	}
 
-#ifndef __PAGETABLE_PMD_FOLDED
-	if (vma_shift == PUD_SHIFT) {
+	if (pgtable_l3_enabled() && vma_shift == PUD_SHIFT) {
 		if (!fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
 			vma_shift = PMD_SHIFT;
 	}
-#endif
 	if (vma_shift == CONT_PMD_SHIFT) {
 		vma_shift = PMD_SHIFT;
 	}
arch/arm64/mm/fixmap.c +1 -1
index a0dcf2375ccb4..f2c6678046a96 100644
@@ -87,7 +87,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 +5 -11
index bc98c20655bba..2add0839179e3 100644
@@ -51,10 +51,9 @@ void __init arm64_hugetlb_cma_reserve(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;
 
@@ -100,13 +99,10 @@ static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
 
 	*pgsize = size;
 
-#ifndef __PAGETABLE_PMD_FOLDED
-	if (size == PUD_SIZE) {
+	if (pgtable_l3_enabled() && size == PUD_SIZE) {
 		if (pud_sect_supported())
 			contig_ptes = 1;
-	} else
-#endif
-	if (size == PMD_SIZE) {
+	} else if (size == PMD_SIZE) {
 		contig_ptes = 1;
 	} else if (size == CONT_PMD_SIZE) {
 		*pgsize = PMD_SIZE;
@@ -331,10 +327,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)
+	if (pgtable_l3_enabled() && hp_size == PUD_SIZE)
 		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 +1 -1
index 4d24034418b39..62587104f30d8 100644
@@ -396,7 +396,7 @@ void __init mem_init(void)
 	 * scratch using the virtual address range and page size.
 	 */
 	VM_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
-		  CONFIG_PGTABLE_LEVELS);
+		  ptg_pgtable_levels);
 
 	if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
 		extern int sysctl_overcommit_memory;
arch/arm64/mm/mmu.c +1 -1
index ad7fd3fda705a..b78a341cd9e70 100644
@@ -1046,7 +1046,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 +2 -1
index 6986827e0d645..045a4188afc10 100644
@@ -230,7 +230,8 @@ static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
 
 	/* check if the current level has been folded dynamically */
 	if ((level == 1 && mm_p4d_folded(st->mm)) ||
-	    (level == 2 && mm_pud_folded(st->mm)))
+	    (level == 2 && mm_pud_folded(st->mm)) ||
+	    (level == 3 && mm_pmd_folded(st->mm)))
 		level = 0;
 
 	if (level >= 0)
Signature · 1 line
2.43.0
author reviewer
#125 lore raw

RFC PATCH 54/57 arm64: Support runtime folding in idmap_kpti_install_ng_mappings

In reply to Ryan Roberts

TODO:

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/assembler.h |   5 ++
 arch/arm64/kernel/cpufeature.c     |  21 +++++-
 arch/arm64/mm/proc.S               | 107 ++++++++++++++++++++++-------
 3 files changed, 108 insertions(+), 25 deletions(-)
arch/arm64/include/asm/assembler.h +5 -0
index 6424fd6be1cbe..0cfa7c3efd214 100644
@@ -919,6 +919,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/kernel/cpufeature.c +19 -2
index 663cc76569a27..ee94de556d3f0 100644
@@ -1908,11 +1908,27 @@ static phys_addr_t __init kpti_ng_pgd_alloc(int shift)
 	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;
@@ -1957,7 +1973,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

Signature · 1 line
2.43.0
author reviewer
#126 lore raw

RFC PATCH 55/57 arm64: TRAMP_VALIAS is no longer compile-time constant

In reply to Ryan Roberts

When boot-time page size is in operation, TRAMP_VALIAS is no longer a compile-time constant, because the VA of a fixmap slot depends upon PAGE_SIZE.

Let's handle this by instead exporting the slot index, FIX_ENTRY_TRAMP_BEGIN,to assembly, then do the TRAMP_VALIAS calculation per page size and use alternatives to decide which variant to activate.

Note that for the tramp_map_kernel case, we are one instruction short of space in the vector to have NOPs for all 3 page size variants. So we do if/else for 16K/64K and branch around it for the 4K case. This saves 2 instructions.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/kernel/asm-offsets.c |  2 +-
 arch/arm64/kernel/entry.S       | 50 ++++++++++++++++++++++++++-------
 2 files changed, 41 insertions(+), 11 deletions(-)
arch/arm64/kernel/asm-offsets.c +1 -1
index f32b8d7f00b2a..c45fa3e281884 100644
@@ -172,7 +172,7 @@ int main(void)
   DEFINE(ARM64_FTR_SYSVAL,	offsetof(struct arm64_ftr_reg, sys_val));
   BLANK();
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
-  DEFINE(TRAMP_VALIAS,		TRAMP_VALIAS);
+  DEFINE(FIX_ENTRY_TRAMP_BEGIN,	FIX_ENTRY_TRAMP_BEGIN);
 #endif
 #ifdef CONFIG_ARM_SDE_INTERFACE
   DEFINE(SDEI_EVENT_INTREGS,	offsetof(struct sdei_registered_event, interrupted_regs));
arch/arm64/kernel/entry.S +40 -10

67 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#127 lore raw

Re: [RFC PATCH v1 55/57] arm64: TRAMP_VALIAS is no longer compile-time constant

Hi Ryan,

Quoted 61 lines · Ryan Roberts

When boot-time page size is in operation, TRAMP_VALIAS is no longer a compile-time constant, because the VA of a fixmap slot depends upon PAGE_SIZE.

Let's handle this by instead exporting the slot index, FIX_ENTRY_TRAMP_BEGIN,to assembly, then do the TRAMP_VALIAS calculation per page size and use alternatives to decide which variant to activate.

Note that for the tramp_map_kernel case, we are one instruction short of space in the vector to have NOPs for all 3 page size variants. So we do if/else for 16K/64K and branch around it for the 4K case. This saves 2 instructions.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/kernel/asm-offsets.c |  2 +-
 arch/arm64/kernel/entry.S       | 50 ++++++++++++++++++++++++++-------
 2 files changed, 41 insertions(+), 11 deletions(-)
arch/arm64/kernel/asm-offsets.c +1 -1
index f32b8d7f00b2a..c45fa3e281884 100644
@@ -172,7 +172,7 @@ int main(void)
   DEFINE(ARM64_FTR_SYSVAL,     offsetof(struct arm64_ftr_reg, sys_val));
   BLANK();
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
-  DEFINE(TRAMP_VALIAS,         TRAMP_VALIAS);
+  DEFINE(FIX_ENTRY_TRAMP_BEGIN,        FIX_ENTRY_TRAMP_BEGIN);
 #endif
 #ifdef CONFIG_ARM_SDE_INTERFACE
   DEFINE(SDEI_EVENT_INTREGS,   offsetof(struct sdei_registered_event, interrupted_regs));
arch/arm64/kernel/entry.S +10 -4
index 7ef0e127b149f..ba47dc8672c04 100644
@@ -101,11 +101,27 @@
 .org .Lventry_start\@ + 128    // Did we overflow the ventry slot?
        .endm
 
+#define TRAMP_VALIAS(page_shift)       (FIXADDR_TOP - (FIX_ENTRY_TRAMP_BEGIN << (page_shift)))
+
        .macro  tramp_alias, dst, sym
-       .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\@
+alternative_if ARM64_USE_PAGE_SIZE_4K
+       .set    .Lalias4k\@, TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) + \sym - .entry.tramp.text
+       movz    \dst, :abs_g2_s:.Lalias4k\@
+       movk    \dst, :abs_g1_nc:.Lalias4k\@
+       movk    \dst, :abs_g0_nc:.Lalias4k\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_16K
+       .set    .Lalias16k\@, TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) + \sym - .entry.tramp.text

arch/arm64/kernel/entry.S line 124

Quoted 10 lines
arch/arm64/kernel/entry.S +10 -0
+       movz    \dst, :abs_g2_s:.Lalias16k\@
+       movk    \dst, :abs_g1_nc:.Lalias16k\@
+       movk    \dst, :abs_g0_nc:.Lalias16k\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_64K
+       .set    .Lalias64k\@, TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) + \sym - .entry.tramp.text
+       movz    \dst, :abs_g2_s:.Lalias64k\@
+       movk    \dst, :abs_g1_nc:.Lalias64k\@
+       movk    \dst, :abs_g0_nc:.Lalias64k\@
+alternative_else_nop_endif

Since you're changing these, might as well drop the middle movk as the fixmap is now always in the top 2 GiB of the VA space.

However, wouldn't it be better to reuse the existing callback alternative stuff that Marc added for KVM?

Same applies below, I reckon.

Quoted 43 lines
arch/arm64/kernel/entry.S +20 -7
        .endm
 
        /*
@@ -627,16 +643,30 @@ SYM_CODE_END(ret_to_user)
        bic     \tmp, \tmp, #USER_ASID_FLAG
        msr     ttbr1_el1, \tmp
 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
-alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
+alternative_if_not ARM64_WORKAROUND_QCOM_FALKOR_E1003
+       b       .Lskip_falkor_e1003\@
+alternative_else_nop_endif
        /* ASID already in \tmp[63:48] */
-       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
-       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
-       /* 2MB boundary containing the vectors, so we nobble the walk cache */
-       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
+alternative_if ARM64_USE_PAGE_SIZE_4K
+       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) >> 12)
+       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) >> 12)
+       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) & ~(SZ_2M - 1)) >> 12)
+       b       .Lfinish_falkor_e1003\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_16K
+       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) >> 12)
+       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) >> 12)
+       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) & ~(SZ_2M - 1)) >> 12)
+alternative_else /* ARM64_USE_PAGE_SIZE_64K */
+       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) >> 12)
+       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) >> 12)
+       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) & ~(SZ_2M - 1)) >> 12)
+alternative_endif
+.Lfinish_falkor_e1003\@:
        isb
        tlbi    vae1, \tmp
        dsb     nsh
-alternative_else_nop_endif
+.Lskip_falkor_e1003\@:
 #endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */
        .endm
 
--

2.43.0

author reviewer
#128 lore raw

Re: [RFC PATCH v1 55/57] arm64: TRAMP_VALIAS is no longer compile-time constant

Quoted 78 lines · Ard Biesheuvel

Hi Ryan,

Quoted 61 lines · Ryan Roberts

When boot-time page size is in operation, TRAMP_VALIAS is no longer a compile-time constant, because the VA of a fixmap slot depends upon PAGE_SIZE.

Let's handle this by instead exporting the slot index, FIX_ENTRY_TRAMP_BEGIN,to assembly, then do the TRAMP_VALIAS calculation per page size and use alternatives to decide which variant to activate.

Note that for the tramp_map_kernel case, we are one instruction short of space in the vector to have NOPs for all 3 page size variants. So we do if/else for 16K/64K and branch around it for the 4K case. This saves 2 instructions.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/kernel/asm-offsets.c |  2 +-
 arch/arm64/kernel/entry.S       | 50 ++++++++++++++++++++++++++-------
 2 files changed, 41 insertions(+), 11 deletions(-)
arch/arm64/kernel/asm-offsets.c +1 -1
index f32b8d7f00b2a..c45fa3e281884 100644
@@ -172,7 +172,7 @@ int main(void)
   DEFINE(ARM64_FTR_SYSVAL,     offsetof(struct arm64_ftr_reg, sys_val));
   BLANK();
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
-  DEFINE(TRAMP_VALIAS,         TRAMP_VALIAS);
+  DEFINE(FIX_ENTRY_TRAMP_BEGIN,        FIX_ENTRY_TRAMP_BEGIN);
 #endif
 #ifdef CONFIG_ARM_SDE_INTERFACE
   DEFINE(SDEI_EVENT_INTREGS,   offsetof(struct sdei_registered_event, interrupted_regs));
arch/arm64/kernel/entry.S +10 -4
index 7ef0e127b149f..ba47dc8672c04 100644
@@ -101,11 +101,27 @@
 .org .Lventry_start\@ + 128    // Did we overflow the ventry slot?
        .endm
 
+#define TRAMP_VALIAS(page_shift)       (FIXADDR_TOP - (FIX_ENTRY_TRAMP_BEGIN << (page_shift)))
+
        .macro  tramp_alias, dst, sym
-       .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\@
+alternative_if ARM64_USE_PAGE_SIZE_4K
+       .set    .Lalias4k\@, TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) + \sym - .entry.tramp.text
+       movz    \dst, :abs_g2_s:.Lalias4k\@
+       movk    \dst, :abs_g1_nc:.Lalias4k\@
+       movk    \dst, :abs_g0_nc:.Lalias4k\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_16K
+       .set    .Lalias16k\@, TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) + \sym - .entry.tramp.text

arch/arm64/kernel/entry.S line 124

Quoted 10 lines
arch/arm64/kernel/entry.S +10 -0
+       movz    \dst, :abs_g2_s:.Lalias16k\@
+       movk    \dst, :abs_g1_nc:.Lalias16k\@
+       movk    \dst, :abs_g0_nc:.Lalias16k\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_64K
+       .set    .Lalias64k\@, TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) + \sym - .entry.tramp.text
+       movz    \dst, :abs_g2_s:.Lalias64k\@
+       movk    \dst, :abs_g1_nc:.Lalias64k\@
+       movk    \dst, :abs_g0_nc:.Lalias64k\@
+alternative_else_nop_endif

Since you're changing these, might as well drop the middle movk as the fixmap is now always in the top 2 GiB of the VA space.

Quoted 2 lines

However, wouldn't it be better to reuse the existing callback alternative stuff that Marc added for KVM?

Yes, I agree. Mark suggested the same thing when we were talking the other day too. I'll definitely use the callbacks for next version, but I didn't want to hold up the RFC any further - I'd already spent way too much time polishing.

Quoted 46 lines

Same applies below, I reckon.

Quoted 43 lines
arch/arm64/kernel/entry.S +20 -7
        .endm
 
        /*
@@ -627,16 +643,30 @@ SYM_CODE_END(ret_to_user)
        bic     \tmp, \tmp, #USER_ASID_FLAG
        msr     ttbr1_el1, \tmp
 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
-alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
+alternative_if_not ARM64_WORKAROUND_QCOM_FALKOR_E1003
+       b       .Lskip_falkor_e1003\@
+alternative_else_nop_endif
        /* ASID already in \tmp[63:48] */
-       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
-       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
-       /* 2MB boundary containing the vectors, so we nobble the walk cache */
-       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
+alternative_if ARM64_USE_PAGE_SIZE_4K
+       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) >> 12)
+       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) >> 12)
+       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS(ARM64_PAGE_SHIFT_4K) & ~(SZ_2M - 1)) >> 12)
+       b       .Lfinish_falkor_e1003\@
+alternative_else_nop_endif
+alternative_if ARM64_USE_PAGE_SIZE_16K
+       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) >> 12)
+       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) >> 12)
+       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS(ARM64_PAGE_SHIFT_16K) & ~(SZ_2M - 1)) >> 12)
+alternative_else /* ARM64_USE_PAGE_SIZE_64K */
+       movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) >> 12)
+       movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) >> 12)
+       movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS(ARM64_PAGE_SHIFT_64K) & ~(SZ_2M - 1)) >> 12)
+alternative_endif
+.Lfinish_falkor_e1003\@:
        isb
        tlbi    vae1, \tmp
        dsb     nsh
-alternative_else_nop_endif
+.Lskip_falkor_e1003\@:
 #endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */
        .endm
 
--

2.43.0

author reviewer
#129 lore raw

RFC PATCH 56/57 arm64: Determine THREAD_SIZE at boot-time

In reply to Ryan Roberts

Since THREAD_SIZE depends on PAGE_SIZE when stacks are vmapped, we must defer the decision on THREAD_SIZE until we have selected PAGE_SIZE at boot.

The one wrinkle is entry.S's requirement to have THREAD_SHIFT as an immediate in order to check that the stack has not overflowed without clobbering any registers, early in the exception handler. Solve this by patching alternatives. During early boot, all 3 options are NOPs until the alternative is patched in. So we forgo overflow checking until boot-cpu patching is complete.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/assembler.h |  5 +++
 arch/arm64/include/asm/efi.h       |  2 +-
 arch/arm64/include/asm/memory.h    | 51 +++++++++++++++++++++++++-----
 arch/arm64/kernel/efi.c            |  2 +-
 arch/arm64/kernel/entry.S          | 10 +++++-
 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, 64 insertions(+), 15 deletions(-)
arch/arm64/include/asm/assembler.h +5 -0
index 0cfa7c3efd214..745328e7768b7 100644
@@ -928,4 +928,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 bcd5622aa0968..913f599c14e40 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 +43 -8

77 lines not on this page · raw message

arch/arm64/kernel/efi.c +1 -1
index 712718aed5dd9..ebc44b7e83199 100644
@@ -197,7 +197,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 +9 -1
index ba47dc8672c04..1ab65e406b62e 100644
@@ -62,7 +62,15 @@
 	 */
 	add	sp, sp, x0			// sp' = sp + x0
 	sub	x0, sp, x0			// x0' = sp' - x0 = (sp + x0) - x0 = sp
-	tbnz	x0, #THREAD_SHIFT, 0f
+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
 	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 761b7f5633e15..2530ee5cee548 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 09fcc234c0f77..937900a458a89 100644
@@ -60,11 +60,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"
@@ -292,7 +292,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)
 
 	/*
 	 * Data written with the MMU off but read with the MMU on requires
arch/arm64/kvm/hyp/nvhe/hyp.lds.S +1 -1
index 74c7c21626270..fe1fbfa8f8f05 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)
Signature · 1 line
2.43.0
author reviewer
#130 lore raw

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

In reply to Ryan Roberts

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/Kconfig                         | 26 ++++++++++---
 arch/arm64/include/asm/kvm_hyp.h           | 11 ++++++
 arch/arm64/include/asm/pgtable-geometry.h  | 22 ++++++++++-
 arch/arm64/include/asm/pgtable-hwdef.h     |  6 +--
 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           | 24 ++++++++++++
 drivers/firmware/efi/libstub/arm64.c       |  3 +-
 16 files changed, 187 insertions(+), 17 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c
 create mode 100644 arch/arm64/mm/pgtable-geometry.c
arch/arm64/Kconfig +20 -6
index a2f8ff354ca67..573d308741169 100644
@@ -121,6 +121,7 @@ config ARM64
 	select BUILDTIME_TABLE_SORT
 	select CLONE_BACKWARDS
 	select COMMON_CLK
+	select CONSTRUCTORS if ARM64_BOOT_TIME_PAGE_SIZE
 	select CPU_PM if (SUSPEND || CPU_IDLE)
 	select CPUMASK_OFFSTACK if NR_CPUS > 256
 	select CRC32
@@ -284,18 +285,20 @@ config MMU
 
 config ARM64_CONT_PTE_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 7 if PAGE_SIZE_16KB
 	default 4
 
 config ARM64_CONT_PMD_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 5 if PAGE_SIZE_16KB
 	default 4
 
 config ARCH_MMAP_RND_BITS_MIN
-	default 14 if PAGE_SIZE_64KB
+	default 14 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 16 if PAGE_SIZE_16KB
 	default 18
 
@@ -306,15 +309,15 @@ config ARCH_MMAP_RND_BITS_MAX
 	default 24 if ARM64_VA_BITS=39
 	default 27 if ARM64_VA_BITS=42
 	default 30 if ARM64_VA_BITS=47
-	default 29 if ARM64_VA_BITS=48 && ARM64_64K_PAGES
+	default 29 if ARM64_VA_BITS=48 && (ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE)
 	default 31 if ARM64_VA_BITS=48 && ARM64_16K_PAGES
 	default 33 if ARM64_VA_BITS=48
-	default 14 if ARM64_64K_PAGES
+	default 14 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 16 if ARM64_16K_PAGES
 	default 18
 
 config ARCH_MMAP_RND_COMPAT_BITS_MIN
-	default 7 if ARM64_64K_PAGES
+	default 7 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 9 if ARM64_16K_PAGES
 	default 11
 
@@ -362,6 +365,7 @@ config FIX_EARLYCON_MEM
 
 config PGTABLE_LEVELS
 	int
+	default 4 if ARM64_BOOT_TIME_PAGE_SIZE # Advertise max supported levels
 	default 2 if ARM64_16K_PAGES && ARM64_VA_BITS_36
 	default 2 if ARM64_64K_PAGES && ARM64_VA_BITS_42
 	default 3 if ARM64_64K_PAGES && (ARM64_VA_BITS_48 || ARM64_VA_BITS_52)
@@ -1316,6 +1320,14 @@ config ARM64_64K_PAGES
 	  look-up. AArch32 emulation requires applications compiled
 	  with 64K aligned segments.
 
+config ARM64_BOOT_TIME_PAGE_SIZE
+	bool "Boot-time selection"
+	select HAVE_PAGE_SIZE_64KB # Advertise largest page size to core
+	help
+	  Select desired page size (4KB, 16KB or 64KB) at boot-time via the
+	  kernel command line option "arm64.pagesize=4k", "arm64.pagesize=16k"
+	  or "arm64.pagesize=64k".
+
 endchoice
 
 choice
@@ -1348,6 +1360,7 @@ config ARM64_VA_BITS_48
 config ARM64_VA_BITS_52
 	bool "52-bit"
 	depends on ARM64_PAN || !ARM64_SW_TTBR0_PAN
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	help
 	  Enable 52-bit virtual addressing for userspace when explicitly
 	  requested via a hint to mmap(). The kernel will also use 52-bit
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help
@@ -1663,7 +1677,7 @@ config ARM64_TAGGED_ADDR_ABI
 
 menuconfig COMPAT
 	bool "Kernel support for 32-bit EL0"
-	depends on ARM64_4K_PAGES || EXPERT
+	depends on ARM64_4K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE || EXPERT
 	select HAVE_UID16
 	select OLD_SIGSUSPEND3
 	select COMPAT_OLD_SIGACTION
arch/arm64/include/asm/kvm_hyp.h +11 -0
index c838309e4ec47..9397a14642afa 100644
@@ -145,4 +145,15 @@ extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
 
+#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 62fe125909c08..18a8c8d499ecc 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 +3 -3
index ca8bcbc1fe220..da5404617acbf 100644
@@ -52,7 +52,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_PMD		(ptg_pgtable_levels > 2 ? (1 << (PAGE_SHIFT - 3)) : 1)
 #define MAX_PTRS_PER_PMD	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
 
@@ -63,7 +63,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_PUD		(ptg_pgtable_levels > 3 ? (1 << (PAGE_SHIFT - 3)) : 1)
 #define MAX_PTRS_PER_PUD	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
 
@@ -71,7 +71,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_P4D		(ptg_pgtable_levels > 4 ? (1 << (PAGE_SHIFT - 3)) : 1)
 #define MAX_PTRS_PER_P4D	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
 
arch/arm64/include/asm/pgtable.h +9 -1
index 8ead41da715b0..ad9f75f5cc29a 100644
@@ -755,7 +755,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)
@@ -888,6 +888,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))
@@ -935,6 +937,8 @@ static inline phys_addr_t p4d_page_paddr(p4d_t p4d)
 
 static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr)
 {
+	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);
 }
 
@@ -1014,6 +1018,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);
@@ -1059,6 +1065,8 @@ static inline phys_addr_t pgd_page_paddr(pgd_t pgd)
 
 static inline p4d_t *pgd_to_folded_p4d(pgd_t *pgdp, unsigned long addr)
 {
+	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 a05fdd54014f7..2daf1263ba638 100644
@@ -17,6 +17,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 a168f3337446f..9968320f83bc4 100644
@@ -36,6 +36,17 @@ PROVIDE(__pi___memcpy			= __pi_memcpy);
 PROVIDE(__pi___memmove			= __pi_memmove);
 PROVIDE(__pi___memset			= __pi_memset);
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+PROVIDE(__pi_ptg_page_shift		= ptg_page_shift);
+PROVIDE(__pi_ptg_pmd_shift		= ptg_pmd_shift);
+PROVIDE(__pi_ptg_pud_shift		= ptg_pud_shift);
+PROVIDE(__pi_ptg_p4d_shift		= ptg_p4d_shift);
+PROVIDE(__pi_ptg_pgdir_shift		= ptg_pgdir_shift);
+PROVIDE(__pi_ptg_cont_pte_shift		= ptg_cont_pte_shift);
+PROVIDE(__pi_ptg_cont_pmd_shift		= ptg_cont_pmd_shift);
+PROVIDE(__pi_ptg_pgtable_levels		= ptg_pgtable_levels);
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 PROVIDE(__pi_id_aa64isar1_override	= id_aa64isar1_override);
 PROVIDE(__pi_id_aa64isar2_override	= id_aa64isar2_override);
 PROVIDE(__pi_id_aa64mmfr0_override	= id_aa64mmfr0_override);
arch/arm64/kernel/image.h +4 -0
index 7bc3ba8979019..01502fc3b891b 100644
@@ -47,7 +47,11 @@
 #define __HEAD_FLAG_BE		ARM64_IMAGE_FLAG_LE
 #endif
 
+#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 9bef7638342ef..c835a50b8b768 100644
@@ -2424,6 +2424,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 */
 }
 
 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
arch/arm64/kvm/hyp/nvhe/Makefile +1 -0
index b43426a493df5..a8fcbb84c7996 100644
@@ -27,6 +27,7 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o
 	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o
+hyp-obj-$(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE) += pgtable-geometry.o
 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 hyp-obj-y += $(lib-objs)
 
arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c new file +16 -0
new file mode 100644
index 0000000000000..17f807450a31a
@@ -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 60454256945b8..2ba30d06b35fe 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 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_CORE)	+= ptdump.o
arch/arm64/mm/pgd.c +6 -4
index 4b106510358b1..c052d0dcb0c69 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 +24 -0
new file mode 100644
index 0000000000000..ba50637f1e9d0
@@ -0,0 +1,24 @@
+// 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;
+int ptg_pmd_shift __read_mostly;
+int ptg_pud_shift __read_mostly;
+int ptg_p4d_shift __read_mostly;
+int ptg_pgdir_shift __read_mostly;
+int ptg_cont_pte_shift __read_mostly;
+int ptg_cont_pmd_shift __read_mostly;
+int ptg_pgtable_levels __read_mostly;
drivers/firmware/efi/libstub/arm64.c +2 -1
index e57cd3de0a00f..8db9dba7d5423 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;
Signature · 1 line
2.43.0
reviewer
#131 lore raw

Re: [RFC PATCH v1 57/57] arm64: Enable boot-time page size selection

Quoted 12 lines · Ryan Roberts

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

<snip>

arch/arm64/Kconfig config XEN · line 1610

Quoted 13 lines
arch/arm64/Kconfig +2 -1
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help

So boot-time page size kernel always has the highest MAX_PAGE_ORDER, which means the section size increases for 4KB and 16KB page sizes. Any downside for this?

Is there any plan (not in this patchset) to support boot-time MAX_PAGE_ORDER to keep section size the same?

Best Regards,
Yan, Zi

author reviewer
#132 lore raw

Re: [RFC PATCH v1 57/57] arm64: Enable boot-time page size selection

Quoted 18 lines · Zi Yan
Quoted 12 lines · Ryan Roberts

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

<snip>

Quoted 1 line
Quoted 16 lines

arch/arm64/Kconfig config XEN · line 1610

Quoted 12 lines
arch/arm64/Kconfig +2 -1
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help

So boot-time page size kernel always has the highest MAX_PAGE_ORDER, which means the section size increases for 4KB and 16KB page sizes. Any downside for this?

I guess there is some cost to the buddy when MAX_PAGE_ORDER is larger than it needs to be - I expect you can explain those details much better than I can. I'm just setting it to the worst case for now as it was the easiest solution for the initial series.

Quoted 3 lines

Is there any plan (not in this patchset) to support boot-time MAX_PAGE_ORDER to keep section size the same?

Yes absolutely. I should have documented MAX_PAGE_ORDER in the commit log along with the comments for MMAP_RND_BITS and SECTION_SIZE_BITS - that was an oversight and I'll fix it in the next version. I plan to look at making all 3 values boot-time configurable in future (although I have no idea at this point how involved that will be).

Thanks,
Ryan

Quoted 3 lines

Best Regards,
Yan, Zi

reviewer
#133 lore raw

Re: [RFC PATCH v1 57/57] arm64: Enable boot-time page size selection

Quoted 36 lines · Ryan Roberts
Quoted 18 lines · Zi Yan
Quoted 12 lines · Ryan Roberts

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

<snip>

Quoted 1 line
Quoted 16 lines

arch/arm64/Kconfig config XEN · line 1610

Quoted 12 lines
arch/arm64/Kconfig +2 -1
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help

So boot-time page size kernel always has the highest MAX_PAGE_ORDER, which means the section size increases for 4KB and 16KB page sizes. Any downside for this?

Quoted 4 lines

I guess there is some cost to the buddy when MAX_PAGE_ORDER is larger than it needs to be - I expect you can explain those details much better than I can. I'm just setting it to the worst case for now as it was the easiest solution for the initial series.

From my past experience (around 5.19), the perf impact (using vm-scalability) seems very small due to MAX_PAGE_ORDER increases [1] (I made MAX_PAGE_ORDER a boot time variable and increased it to 20 for my 1GB THP experiments).

Larger MAX_PAGE_ORDER means larger section size and larger mem_block size, so the granularity of memory hotplug also increases. In this case:
1. ARM64 4KB: mem_block size increases from 4MB to 32MB,
2. ARM64 16KB: mem_block size increases from 32MB to 128MB,
3. ARM64 64KB: mem_block size keeps the same, 512MB.

DavidH was concerned about large mem_block size before. He might have some opinion on this.

Quoted 10 lines
Quoted 4 lines

Is there any plan (not in this patchset) to support boot-time MAX_PAGE_ORDER to keep section size the same?

Yes absolutely. I should have documented MAX_PAGE_ORDER in the commit log along with the comments for MMAP_RND_BITS and SECTION_SIZE_BITS - that was an oversight and I'll fix it in the next version. I plan to look at making all 3 values boot-time configurable in future (although I have no idea at this point how involved that will be).

In [1], I tried to make MAX_PAGE_ORDER a boot time variable, but for a different purpose, allocating 1GB THP. I needed some additional changes in my patchset, since I assumed MAX_PAGE_ORDER can go beyond section size, which makes things a little bit complicated. For your case, I assume you are not planning to make MAX_PAGE_ORDER bigger than section size, then I should be able to revive my patchset with fewer changes.

In terms of SECTION_SIZE_BITS, why do you want to make it a boot time variable? Since it decides the minimum memory hotplug size, I assume we should keep it unchanged or as small as possible to make virtual machine memory usage efficient.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Best Regards,
Yan, Zi

author reviewer
#134 lore raw

Re: [RFC PATCH v1 57/57] arm64: Enable boot-time page size selection

Quoted 68 lines · Zi Yan
Quoted 36 lines · Ryan Roberts
Quoted 18 lines · Zi Yan
Quoted 12 lines · Ryan Roberts

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

<snip>

Quoted 1 line
Quoted 16 lines

arch/arm64/Kconfig config XEN · line 1610

Quoted 12 lines
arch/arm64/Kconfig +2 -1
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help

So boot-time page size kernel always has the highest MAX_PAGE_ORDER, which means the section size increases for 4KB and 16KB page sizes. Any downside for this?

Quoted 4 lines

I guess there is some cost to the buddy when MAX_PAGE_ORDER is larger than it needs to be - I expect you can explain those details much better than I can. I'm just setting it to the worst case for now as it was the easiest solution for the initial series.

From my past experience (around 5.19), the perf impact (using vm-scalability) seems very small due to MAX_PAGE_ORDER increases [1] (I made MAX_PAGE_ORDER a boot time variable and increased it to 20 for my 1GB THP experiments).

Larger MAX_PAGE_ORDER means larger section size and larger mem_block size, so the granularity of memory hotplug also increases. In this case:
1. ARM64 4KB: mem_block size increases from 4MB to 32MB,
2. ARM64 16KB: mem_block size increases from 32MB to 128MB,
3. ARM64 64KB: mem_block size keeps the same, 512MB.

DavidH was concerned about large mem_block size before. He might have some opinion on this.

Quoted 10 lines
Quoted 4 lines

Is there any plan (not in this patchset) to support boot-time MAX_PAGE_ORDER to keep section size the same?

Yes absolutely. I should have documented MAX_PAGE_ORDER in the commit log along with the comments for MMAP_RND_BITS and SECTION_SIZE_BITS - that was an oversight and I'll fix it in the next version. I plan to look at making all 3 values boot-time configurable in future (although I have no idea at this point how involved that will be).

Quoted 6 lines

In [1], I tried to make MAX_PAGE_ORDER a boot time variable, but for a different purpose, allocating 1GB THP. I needed some additional changes in my patchset, since I assumed MAX_PAGE_ORDER can go beyond section size, which makes things a little bit complicated. For your case, I assume you are not planning to make MAX_PAGE_ORDER bigger than section size, then I should be able to revive my patchset with fewer changes.

Yes correct; no need to make it bigger than section size. Thanks for the patch, I'll certainly use it as a base when I get there or if you're interested in doing it then even better ;-)

But I don't think this is urgent. For now, boot-time page size is a new Kconfig for arm64. It still supports the compile-time page size options. So having a larger MAX_PAGE_ORDER than strictly necessary doesn't represent a regression, just a limitation of boot-time page size config - something we can optimize later.

Quoted 5 lines

In terms of SECTION_SIZE_BITS, why do you want to make it a boot time variable? Since it decides the minimum memory hotplug size, I assume we should keep it unchanged or as small as possible to make virtual machine memory usage efficient.

When I say "boot-time variable" I just mean something that the arch can configure at boot based on the selected page size. I'm not proposing to allow the user to set it via the command line. That means we need to rid the code of any assumptions that it is compile time constant (e.g. c preprocessor usage of the value, etc). The same goes for MAX_PAGE_ORDER and the MMAP_RND_BITS stuff.

Quoted 7 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/

Best Regards,
Yan, Zi

reviewer
#135 lore raw

Re: [RFC PATCH v1 57/57] arm64: Enable boot-time page size selection

Quoted 76 lines · Ryan Roberts
Quoted 68 lines · Zi Yan
Quoted 36 lines · Ryan Roberts
Quoted 18 lines · Zi Yan
Quoted 12 lines · Ryan Roberts

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

<snip>

Quoted 1 line
Quoted 16 lines

arch/arm64/Kconfig config XEN · line 1610

Quoted 12 lines
arch/arm64/Kconfig +2 -1
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help

So boot-time page size kernel always has the highest MAX_PAGE_ORDER, which means the section size increases for 4KB and 16KB page sizes. Any downside for this?

Quoted 4 lines

I guess there is some cost to the buddy when MAX_PAGE_ORDER is larger than it needs to be - I expect you can explain those details much better than I can. I'm just setting it to the worst case for now as it was the easiest solution for the initial series.

From my past experience (around 5.19), the perf impact (using vm-scalability) seems very small due to MAX_PAGE_ORDER increases [1] (I made MAX_PAGE_ORDER a boot time variable and increased it to 20 for my 1GB THP experiments).

Larger MAX_PAGE_ORDER means larger section size and larger mem_block size, so the granularity of memory hotplug also increases. In this case:
1. ARM64 4KB: mem_block size increases from 4MB to 32MB,
2. ARM64 16KB: mem_block size increases from 32MB to 128MB,
3. ARM64 64KB: mem_block size keeps the same, 512MB.

DavidH was concerned about large mem_block size before. He might have some opinion on this.

Quoted 10 lines
Quoted 4 lines

Is there any plan (not in this patchset) to support boot-time MAX_PAGE_ORDER to keep section size the same?

Yes absolutely. I should have documented MAX_PAGE_ORDER in the commit log along with the comments for MMAP_RND_BITS and SECTION_SIZE_BITS - that was an oversight and I'll fix it in the next version. I plan to look at making all 3 values boot-time configurable in future (although I have no idea at this point how involved that will be).

Quoted 6 lines

In [1], I tried to make MAX_PAGE_ORDER a boot time variable, but for a different purpose, allocating 1GB THP. I needed some additional changes in my patchset, since I assumed MAX_PAGE_ORDER can go beyond section size, which makes things a little bit complicated. For your case, I assume you are not planning to make MAX_PAGE_ORDER bigger than section size, then I should be able to revive my patchset with fewer changes.

Quoted 8 lines

Yes correct; no need to make it bigger than section size. Thanks for the patch, I'll certainly use it as a base when I get there or if you're interested in doing it then even better ;-)

But I don't think this is urgent. For now, boot-time page size is a new Kconfig for arm64. It still supports the compile-time page size options. So having a larger MAX_PAGE_ORDER than strictly necessary doesn't represent a regression, just a limitation of boot-time page size config - something we can optimize later.

Sure. I will revisit my boot time MAX_PAGE_ORDER patchset when this patchset settles. Glad to help. :)

Quoted 7 lines
Quoted 6 lines

In terms of SECTION_SIZE_BITS, why do you want to make it a boot time variable? Since it decides the minimum memory hotplug size, I assume we should keep it unchanged or as small as possible to make virtual machine memory usage efficient.

Quoted 6 lines

When I say "boot-time variable" I just mean something that the arch can configure at boot based on the selected page size. I'm not proposing to allow the user to set it via the command line. That means we need to rid the code of any assumptions that it is compile time constant (e.g. c preprocessor usage of the value, etc). The same goes for MAX_PAGE_ORDER and the MMAP_RND_BITS stuff.

Got it.

Quoted 1 line
Quoted 1 line

[1] https://lore.kernel.org/linux-mm/[email protected]/

Best Regards,
Yan, Zi

#136 lore raw

RE: [RFC PATCH v1 57/57] arm64: Enable boot-time page size selection

In reply to Ryan Roberts

From: Ryan Roberts <[email protected]> Sent: Monday, October 14, 2024 3:59 AM

Quoted 526 lines

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/Kconfig                         | 26 ++++++++++---
 arch/arm64/include/asm/kvm_hyp.h           | 11 ++++++
 arch/arm64/include/asm/pgtable-geometry.h  | 22 ++++++++++-
 arch/arm64/include/asm/pgtable-hwdef.h     |  6 +--
 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           | 24 ++++++++++++
 drivers/firmware/efi/libstub/arm64.c       |  3 +-
 16 files changed, 187 insertions(+), 17 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c
 create mode 100644 arch/arm64/mm/pgtable-geometry.c
arch/arm64/Kconfig +8 -4
index a2f8ff354ca67..573d308741169 100644
@@ -121,6 +121,7 @@ config ARM64
 	select BUILDTIME_TABLE_SORT
 	select CLONE_BACKWARDS
 	select COMMON_CLK
+	select CONSTRUCTORS if ARM64_BOOT_TIME_PAGE_SIZE
 	select CPU_PM if (SUSPEND || CPU_IDLE)
 	select CPUMASK_OFFSTACK if NR_CPUS > 256
 	select CRC32
@@ -284,18 +285,20 @@ config MMU
 
 config ARM64_CONT_PTE_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 7 if PAGE_SIZE_16KB
 	default 4
 
 config ARM64_CONT_PMD_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 5 if PAGE_SIZE_16KB
 	default 4
 
 config ARCH_MMAP_RND_BITS_MIN
-	default 14 if PAGE_SIZE_64KB
+	default 14 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 16 if PAGE_SIZE_16KB
 	default 18
 
@@ -306,15 +309,15 @@ config ARCH_MMAP_RND_BITS_MAX
 	default 24 if ARM64_VA_BITS=39
 	default 27 if ARM64_VA_BITS=42
 	default 30 if ARM64_VA_BITS=47
-	default 29 if ARM64_VA_BITS=48 && ARM64_64K_PAGES
+	default 29 if ARM64_VA_BITS=48 && (ARM64_64K_PAGES ||
 ARM64_BOOT_TIME_PAGE_SIZE)
 	default 31 if ARM64_VA_BITS=48 && ARM64_16K_PAGES
 	default 33 if ARM64_VA_BITS=48
-	default 14 if ARM64_64K_PAGES
+	default 14 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 16 if ARM64_16K_PAGES
 	default 18
 
 config ARCH_MMAP_RND_COMPAT_BITS_MIN
-	default 7 if ARM64_64K_PAGES
+	default 7 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 9 if ARM64_16K_PAGES
 	default 11
@@ -362,6 +365,7 @@ config FIX_EARLYCON_MEM
 
 config PGTABLE_LEVELS
 	int
+	default 4 if ARM64_BOOT_TIME_PAGE_SIZE # Advertise max supported levels
 	default 2 if ARM64_16K_PAGES && ARM64_VA_BITS_36
 	default 2 if ARM64_64K_PAGES && ARM64_VA_BITS_42
 	default 3 if ARM64_64K_PAGES && (ARM64_VA_BITS_48 ||

ARM64_VA_BITS_52)

arch/arm64/Kconfig +12 -2
@@ -1316,6 +1320,14 @@ config ARM64_64K_PAGES
 	  look-up. AArch32 emulation requires applications compiled
 	  with 64K aligned segments.
 
+config ARM64_BOOT_TIME_PAGE_SIZE
+	bool "Boot-time selection"
+	select HAVE_PAGE_SIZE_64KB # Advertise largest page size to core
+	help
+	  Select desired page size (4KB, 16KB or 64KB) at boot-time via the
+	  kernel command line option "arm64.pagesize=4k", "arm64.pagesize=16k"
+	  or "arm64.pagesize=64k".
+
 endchoice
 
 choice
@@ -1348,6 +1360,7 @@ config ARM64_VA_BITS_48
 config ARM64_VA_BITS_52
 	bool "52-bit"
 	depends on ARM64_PAN || !ARM64_SW_TTBR0_PAN
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	help
 	  Enable 52-bit virtual addressing for userspace when explicitly
 	  requested via a hint to mmap(). The kernel will also use 52-bit
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help
@@ -1663,7 +1677,7 @@ config ARM64_TAGGED_ADDR_ABI
 
 menuconfig COMPAT
 	bool "Kernel support for 32-bit EL0"
-	depends on ARM64_4K_PAGES || EXPERT
+	depends on ARM64_4K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE || EXPERT
 	select HAVE_UID16
 	select OLD_SIGSUSPEND3
 	select COMPAT_OLD_SIGACTION
arch/arm64/include/asm/kvm_hyp.h +11 -0
index c838309e4ec47..9397a14642afa 100644
@@ -145,4 +145,15 @@ extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
 
+#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 62fe125909c08..18a8c8d499ecc 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 +3 -3
index ca8bcbc1fe220..da5404617acbf 100644
@@ -52,7 +52,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_PMD		(ptg_pgtable_levels > 2 ? (1 << (PAGE_SHIFT -
 3)) : 1)
 #define MAX_PTRS_PER_PMD	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
@@ -63,7 +63,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_PUD		(ptg_pgtable_levels > 3 ? (1 << (PAGE_SHIFT -
 3)) : 1)
 #define MAX_PTRS_PER_PUD	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
@@ -71,7 +71,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_P4D		(ptg_pgtable_levels > 4 ? (1 << (PAGE_SHIFT -
 3)) : 1)
 #define MAX_PTRS_PER_P4D	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
arch/arm64/include/asm/pgtable.h +9 -1
index 8ead41da715b0..ad9f75f5cc29a 100644
@@ -755,7 +755,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)
@@ -888,6 +888,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))
@@ -935,6 +937,8 @@ static inline phys_addr_t p4d_page_paddr(p4d_t p4d)
 
 static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr)
 {
+	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);
 }
 
@@ -1014,6 +1018,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);
@@ -1059,6 +1065,8 @@ static inline phys_addr_t pgd_page_paddr(pgd_t pgd)
 
 static inline p4d_t *pgd_to_folded_p4d(pgd_t *pgdp, unsigned long addr)
 {
+	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 a05fdd54014f7..2daf1263ba638 100644
@@ -17,6 +17,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 a168f3337446f..9968320f83bc4 100644
@@ -36,6 +36,17 @@ PROVIDE(__pi___memcpy			=
 __pi_memcpy);
 PROVIDE(__pi___memmove			= __pi_memmove);
 PROVIDE(__pi___memset			= __pi_memset);
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+PROVIDE(__pi_ptg_page_shift		= ptg_page_shift);
+PROVIDE(__pi_ptg_pmd_shift		= ptg_pmd_shift);
+PROVIDE(__pi_ptg_pud_shift		= ptg_pud_shift);
+PROVIDE(__pi_ptg_p4d_shift		= ptg_p4d_shift);
+PROVIDE(__pi_ptg_pgdir_shift		= ptg_pgdir_shift);
+PROVIDE(__pi_ptg_cont_pte_shift		= ptg_cont_pte_shift);
+PROVIDE(__pi_ptg_cont_pmd_shift		= ptg_cont_pmd_shift);
+PROVIDE(__pi_ptg_pgtable_levels		= ptg_pgtable_levels);
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 PROVIDE(__pi_id_aa64isar1_override	= id_aa64isar1_override);
 PROVIDE(__pi_id_aa64isar2_override	= id_aa64isar2_override);

PROVIDE(__pi_id_aa64mmfr0_override = id_aa64mmfr0_override);

arch/arm64/kernel/image.h +4 -0
index 7bc3ba8979019..01502fc3b891b 100644
@@ -47,7 +47,11 @@
 #define __HEAD_FLAG_BE		ARM64_IMAGE_FLAG_LE
 #endif
 
+#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 +43 -0
index deb8cd50b0b0c..22b3c70e04f9c 100644
@@ -221,6 +221,49 @@ static void __init map_fdt(u64 fdt, int page_shift)
 	dsb(ishst);
 }
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+static void __init ptg_init(int page_shift)
+{
+	ptg_pgtable_levels =
+		__ARM64_HW_PGTABLE_LEVELS(page_shift,
 CONFIG_ARM64_VA_BITS);
+
+	ptg_pgdir_shift = __ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift,
+				4 - ptg_pgtable_levels);
+
+	ptg_p4d_shift = ptg_pgtable_levels >= 5 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 0) :
+				ptg_pgdir_shift;
+
+	ptg_pud_shift = ptg_pgtable_levels >= 4 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 1) :
+				ptg_pgdir_shift;
+
+	ptg_pmd_shift = ptg_pgtable_levels >= 3 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 2) :
+				ptg_pgdir_shift;
+
+	ptg_page_shift = page_shift;
+
+	switch (page_shift) {
+	case ARM64_PAGE_SHIFT_64K:
+		ptg_cont_pte_shift = ptg_page_shift + 5;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 5;
+		break;
+	case ARM64_PAGE_SHIFT_16K:
+		ptg_cont_pte_shift = ptg_page_shift + 7;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 5;
+		break;
+	default: /* ARM64_PAGE_SHIFT_4K */
+		ptg_cont_pte_shift = ptg_page_shift + 4;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 4;
+	}
+}
+#else /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+static inline void ptg_init(int page_shift)
+{
+}
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 asmlinkage void __init early_map_kernel(u64 boot_status, void *fdt)
 {
arch/arm64/kernel/pi/map_kernel.c +2 -0
 	static char const chosen_str[] __initconst = "/chosen";
@@ -247,6 +290,8 @@ asmlinkage void __init early_map_kernel(u64 boot_status, void
 *fdt)
 	if (!page_shift)
 		page_shift = early_page_shift;
 
+	ptg_init(page_shift);
+
 	if (va_bits > 48) {
 		u64 page_size = early_page_size(page_shift);
arch/arm64/kvm/arm.c +10 -0
index 9bef7638342ef..c835a50b8b768 100644
@@ -2424,6 +2424,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 */
 }
 

static int __init kvm_hyp_init_protection(u32 hyp_va_bits)

arch/arm64/kvm/hyp/nvhe/Makefile +0 -0
index b43426a493df5..a8fcbb84c7996 100644
@@ -27,6 +27,7 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-

init.o host.o

arch/arm64/kvm/hyp/nvhe/Makefile +1 -0
 	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o
+hyp-obj-$(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE) += pgtable-geometry.o
 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 hyp-obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c new file +16 -0
new file mode 100644
index 0000000000000..17f807450a31a
@@ -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 60454256945b8..2ba30d06b35fe 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 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_CORE) += ptdump.o

arch/arm64/mm/pgd.c +6 -4
index 4b106510358b1..c052d0dcb0c69 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- renamed from arch/arm64/mm/pgtable-geometry.c +0 -0

geometry.c
new file mode 100644
index 0000000000000..ba50637f1e9d0

arch/arm64/mm/pgtable-geometry.c new file +8 -0
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 ARM Ltd.
+ */
+
+#include <linux/cache.h>
+#include <asm/pgtable-geometry.h>
+

arch/arm64/mm/pgtable-geometry.c line 18

Quoted 10 lines
arch/arm64/mm/pgtable-geometry.c new file +10 -0
+/*
+ * 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;
+int ptg_pmd_shift __read_mostly;

I found that ptg_page_shift and ptg_pmd_shift need EXPORT_SYMBOL_GPL for cases where code compiled as a module is using PAGE_SIZE/PAGE_SHIFT or PMD_SIZE/PMD_SHIFT. Some of the others below might also need EXPORT_SYMBOL_GPL.

Michael

Quoted 25 lines
arch/arm64/mm/pgtable-geometry.c new file +6 -0
+int ptg_pud_shift __read_mostly;
+int ptg_p4d_shift __read_mostly;
+int ptg_pgdir_shift __read_mostly;
+int ptg_cont_pte_shift __read_mostly;
+int ptg_cont_pmd_shift __read_mostly;
+int ptg_pgtable_levels __read_mostly;
drivers/firmware/efi/libstub/arm64.c +2 -1
index e57cd3de0a00f..8db9dba7d5423 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;
--
2.43.0

author reviewer
#137 lore raw

Re: [RFC PATCH v1 57/57] arm64: Enable boot-time page size selection

Quoted 535 lines · Michael Kelley

From: Ryan Roberts <[email protected]> Sent: Monday, October 14, 2024 3:59 AM

Quoted 534 lines

Introduce a new Kconfig, ARM64_BOOT_TIME_PAGE_SIZE, which can be selected instead of a page size. When selected, the resulting kernel's page size can be configured at boot via the command line.

For now, boot-time page size kernels are limited to 48-bit VA, since more work is required to support LPA2. Additionally MMAP_RND_BITS and SECTION_SIZE_BITS are configured for the worst case (64K pages). Future work could be implemented to be able to configure these at boot time for optimial page size-specific values.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 arch/arm64/Kconfig                         | 26 ++++++++++---
 arch/arm64/include/asm/kvm_hyp.h           | 11 ++++++
 arch/arm64/include/asm/pgtable-geometry.h  | 22 ++++++++++-
 arch/arm64/include/asm/pgtable-hwdef.h     |  6 +--
 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           | 24 ++++++++++++
 drivers/firmware/efi/libstub/arm64.c       |  3 +-
 16 files changed, 187 insertions(+), 17 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c
 create mode 100644 arch/arm64/mm/pgtable-geometry.c
arch/arm64/Kconfig +8 -4
index a2f8ff354ca67..573d308741169 100644
@@ -121,6 +121,7 @@ config ARM64
 	select BUILDTIME_TABLE_SORT
 	select CLONE_BACKWARDS
 	select COMMON_CLK
+	select CONSTRUCTORS if ARM64_BOOT_TIME_PAGE_SIZE
 	select CPU_PM if (SUSPEND || CPU_IDLE)
 	select CPUMASK_OFFSTACK if NR_CPUS > 256
 	select CRC32
@@ -284,18 +285,20 @@ config MMU
 
 config ARM64_CONT_PTE_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 7 if PAGE_SIZE_16KB
 	default 4
 
 config ARM64_CONT_PMD_SHIFT
 	int
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	default 5 if PAGE_SIZE_64KB
 	default 5 if PAGE_SIZE_16KB
 	default 4
 
 config ARCH_MMAP_RND_BITS_MIN
-	default 14 if PAGE_SIZE_64KB
+	default 14 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 16 if PAGE_SIZE_16KB
 	default 18
 
@@ -306,15 +309,15 @@ config ARCH_MMAP_RND_BITS_MAX
 	default 24 if ARM64_VA_BITS=39
 	default 27 if ARM64_VA_BITS=42
 	default 30 if ARM64_VA_BITS=47
-	default 29 if ARM64_VA_BITS=48 && ARM64_64K_PAGES
+	default 29 if ARM64_VA_BITS=48 && (ARM64_64K_PAGES ||
 ARM64_BOOT_TIME_PAGE_SIZE)
 	default 31 if ARM64_VA_BITS=48 && ARM64_16K_PAGES
 	default 33 if ARM64_VA_BITS=48
-	default 14 if ARM64_64K_PAGES
+	default 14 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 16 if ARM64_16K_PAGES
 	default 18
 
 config ARCH_MMAP_RND_COMPAT_BITS_MIN
-	default 7 if ARM64_64K_PAGES
+	default 7 if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default 9 if ARM64_16K_PAGES
 	default 11
@@ -362,6 +365,7 @@ config FIX_EARLYCON_MEM
 
 config PGTABLE_LEVELS
 	int
+	default 4 if ARM64_BOOT_TIME_PAGE_SIZE # Advertise max supported levels
 	default 2 if ARM64_16K_PAGES && ARM64_VA_BITS_36
 	default 2 if ARM64_64K_PAGES && ARM64_VA_BITS_42
 	default 3 if ARM64_64K_PAGES && (ARM64_VA_BITS_48 ||

ARM64_VA_BITS_52)

arch/arm64/Kconfig +12 -2
@@ -1316,6 +1320,14 @@ config ARM64_64K_PAGES
 	  look-up. AArch32 emulation requires applications compiled
 	  with 64K aligned segments.
 
+config ARM64_BOOT_TIME_PAGE_SIZE
+	bool "Boot-time selection"
+	select HAVE_PAGE_SIZE_64KB # Advertise largest page size to core
+	help
+	  Select desired page size (4KB, 16KB or 64KB) at boot-time via the
+	  kernel command line option "arm64.pagesize=4k", "arm64.pagesize=16k"
+	  or "arm64.pagesize=64k".
+
 endchoice
 
 choice
@@ -1348,6 +1360,7 @@ config ARM64_VA_BITS_48
 config ARM64_VA_BITS_52
 	bool "52-bit"
 	depends on ARM64_PAN || !ARM64_SW_TTBR0_PAN
+	depends on !ARM64_BOOT_TIME_PAGE_SIZE
 	help
 	  Enable 52-bit virtual addressing for userspace when explicitly
 	  requested via a hint to mmap(). The kernel will also use 52-bit
@@ -1588,9 +1601,10 @@ config XEN
 # 4K  |       27          |      12      |       15             |         10              |
 # 16K |       27          |      14      |       13             |         11              |
 # 64K |       29          |      16      |       13             |         13              |
+# BOOT|       29          |    16 (max)  |       13             |         13              |
 config ARCH_FORCE_MAX_ORDER
 	int
-	default "13" if ARM64_64K_PAGES
+	default "13" if ARM64_64K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE
 	default "11" if ARM64_16K_PAGES
 	default "10"
 	help
@@ -1663,7 +1677,7 @@ config ARM64_TAGGED_ADDR_ABI
 
 menuconfig COMPAT
 	bool "Kernel support for 32-bit EL0"
-	depends on ARM64_4K_PAGES || EXPERT
+	depends on ARM64_4K_PAGES || ARM64_BOOT_TIME_PAGE_SIZE || EXPERT
 	select HAVE_UID16
 	select OLD_SIGSUSPEND3
 	select COMPAT_OLD_SIGACTION
arch/arm64/include/asm/kvm_hyp.h +11 -0
index c838309e4ec47..9397a14642afa 100644
@@ -145,4 +145,15 @@ extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
 
+#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 62fe125909c08..18a8c8d499ecc 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 +3 -3
index ca8bcbc1fe220..da5404617acbf 100644
@@ -52,7 +52,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_PMD		(ptg_pgtable_levels > 2 ? (1 << (PAGE_SHIFT -
 3)) : 1)
 #define MAX_PTRS_PER_PMD	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
@@ -63,7 +63,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_PUD		(ptg_pgtable_levels > 3 ? (1 << (PAGE_SHIFT -
 3)) : 1)
 #define MAX_PTRS_PER_PUD	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
@@ -71,7 +71,7 @@
 #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 << (PAGE_SHIFT - 3))
+#define PTRS_PER_P4D		(ptg_pgtable_levels > 4 ? (1 << (PAGE_SHIFT -
 3)) : 1)
 #define MAX_PTRS_PER_P4D	(1 << (PAGE_SHIFT_MAX - 3))
 #endif
arch/arm64/include/asm/pgtable.h +9 -1
index 8ead41da715b0..ad9f75f5cc29a 100644
@@ -755,7 +755,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)
@@ -888,6 +888,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))
@@ -935,6 +937,8 @@ static inline phys_addr_t p4d_page_paddr(p4d_t p4d)
 
 static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr)
 {
+	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);
 }
 
@@ -1014,6 +1018,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);
@@ -1059,6 +1065,8 @@ static inline phys_addr_t pgd_page_paddr(pgd_t pgd)
 
 static inline p4d_t *pgd_to_folded_p4d(pgd_t *pgdp, unsigned long addr)
 {
+	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 a05fdd54014f7..2daf1263ba638 100644
@@ -17,6 +17,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 a168f3337446f..9968320f83bc4 100644
@@ -36,6 +36,17 @@ PROVIDE(__pi___memcpy			=
 __pi_memcpy);
 PROVIDE(__pi___memmove			= __pi_memmove);
 PROVIDE(__pi___memset			= __pi_memset);
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+PROVIDE(__pi_ptg_page_shift		= ptg_page_shift);
+PROVIDE(__pi_ptg_pmd_shift		= ptg_pmd_shift);
+PROVIDE(__pi_ptg_pud_shift		= ptg_pud_shift);
+PROVIDE(__pi_ptg_p4d_shift		= ptg_p4d_shift);
+PROVIDE(__pi_ptg_pgdir_shift		= ptg_pgdir_shift);
+PROVIDE(__pi_ptg_cont_pte_shift		= ptg_cont_pte_shift);
+PROVIDE(__pi_ptg_cont_pmd_shift		= ptg_cont_pmd_shift);
+PROVIDE(__pi_ptg_pgtable_levels		= ptg_pgtable_levels);
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 PROVIDE(__pi_id_aa64isar1_override	= id_aa64isar1_override);
 PROVIDE(__pi_id_aa64isar2_override	= id_aa64isar2_override);

PROVIDE(__pi_id_aa64mmfr0_override = id_aa64mmfr0_override);

arch/arm64/kernel/image.h +4 -0
index 7bc3ba8979019..01502fc3b891b 100644
@@ -47,7 +47,11 @@
 #define __HEAD_FLAG_BE		ARM64_IMAGE_FLAG_LE
 #endif
 
+#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 +43 -0
index deb8cd50b0b0c..22b3c70e04f9c 100644
@@ -221,6 +221,49 @@ static void __init map_fdt(u64 fdt, int page_shift)
 	dsb(ishst);
 }
 
+#ifdef CONFIG_ARM64_BOOT_TIME_PAGE_SIZE
+static void __init ptg_init(int page_shift)
+{
+	ptg_pgtable_levels =
+		__ARM64_HW_PGTABLE_LEVELS(page_shift,
 CONFIG_ARM64_VA_BITS);
+
+	ptg_pgdir_shift = __ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift,
+				4 - ptg_pgtable_levels);
+
+	ptg_p4d_shift = ptg_pgtable_levels >= 5 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 0) :
+				ptg_pgdir_shift;
+
+	ptg_pud_shift = ptg_pgtable_levels >= 4 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 1) :
+				ptg_pgdir_shift;
+
+	ptg_pmd_shift = ptg_pgtable_levels >= 3 ?
+				__ARM64_HW_PGTABLE_LEVEL_SHIFT(page_shift, 2) :
+				ptg_pgdir_shift;
+
+	ptg_page_shift = page_shift;
+
+	switch (page_shift) {
+	case ARM64_PAGE_SHIFT_64K:
+		ptg_cont_pte_shift = ptg_page_shift + 5;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 5;
+		break;
+	case ARM64_PAGE_SHIFT_16K:
+		ptg_cont_pte_shift = ptg_page_shift + 7;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 5;
+		break;
+	default: /* ARM64_PAGE_SHIFT_4K */
+		ptg_cont_pte_shift = ptg_page_shift + 4;
+		ptg_cont_pmd_shift = ptg_pmd_shift + 4;
+	}
+}
+#else /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+static inline void ptg_init(int page_shift)
+{
+}
+#endif /* CONFIG_ARM64_BOOT_TIME_PAGE_SIZE */
+
 asmlinkage void __init early_map_kernel(u64 boot_status, void *fdt)
 {
arch/arm64/kernel/pi/map_kernel.c +2 -0
 	static char const chosen_str[] __initconst = "/chosen";
@@ -247,6 +290,8 @@ asmlinkage void __init early_map_kernel(u64 boot_status, void
 *fdt)
 	if (!page_shift)
 		page_shift = early_page_shift;
 
+	ptg_init(page_shift);
+
 	if (va_bits > 48) {
 		u64 page_size = early_page_size(page_shift);
arch/arm64/kvm/arm.c +10 -0
index 9bef7638342ef..c835a50b8b768 100644
@@ -2424,6 +2424,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 */
 }
 

static int __init kvm_hyp_init_protection(u32 hyp_va_bits)

arch/arm64/kvm/hyp/nvhe/Makefile +0 -0
index b43426a493df5..a8fcbb84c7996 100644
@@ -27,6 +27,7 @@ hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-

init.o host.o

arch/arm64/kvm/hyp/nvhe/Makefile +1 -0
 	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o
+hyp-obj-$(CONFIG_ARM64_BOOT_TIME_PAGE_SIZE) += pgtable-geometry.o
 hyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
 hyp-obj-y += $(lib-objs)
arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c new file +16 -0
new file mode 100644
index 0000000000000..17f807450a31a
@@ -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 60454256945b8..2ba30d06b35fe 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 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_CORE) += ptdump.o

arch/arm64/mm/pgd.c +6 -4
index 4b106510358b1..c052d0dcb0c69 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- renamed from arch/arm64/mm/pgtable-geometry.c +0 -0

geometry.c
new file mode 100644
index 0000000000000..ba50637f1e9d0

arch/arm64/mm/pgtable-geometry.c new file +16 -0
@@ -0,0 +1,24 @@
+// 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.
+ */
+
Quoted 8 lines

arch/arm64/mm/pgtable-geometry.c line 18

Quoted 2 lines
arch/arm64/mm/pgtable-geometry.c new file +2 -0
+int ptg_page_shift __read_mostly;
+int ptg_pmd_shift __read_mostly;

I found that ptg_page_shift and ptg_pmd_shift need EXPORT_SYMBOL_GPL for cases where code compiled as a module is using PAGE_SIZE/PAGE_SHIFT or PMD_SIZE/PMD_SHIFT. Some of the others below might also need EXPORT_SYMBOL_GPL.

Ahh good spot - thanks! I'll fix this in the next version.

I wonder if these should really be EXPORT_SYMBOL() and not limited to GPL? I guess having access to PAGE_SIZE is a pretty fundamental thing? Anybody know the policy here?

Thanks,
Ryan

Quoted 29 lines

Michael

Quoted 25 lines
arch/arm64/mm/pgtable-geometry.c new file +6 -0
+int ptg_pud_shift __read_mostly;
+int ptg_p4d_shift __read_mostly;
+int ptg_pgdir_shift __read_mostly;
+int ptg_cont_pte_shift __read_mostly;
+int ptg_cont_pmd_shift __read_mostly;
+int ptg_pgtable_levels __read_mostly;
drivers/firmware/efi/libstub/arm64.c +2 -1
index e57cd3de0a00f..8db9dba7d5423 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;
--
2.43.0

#138 lore raw

Re: [RFC PATCH v1 01/57] mm: Add macros ahead of supporting boot-time page size selection

In reply to Ryan Roberts

Hello Ryan,

Quoted 9 lines · Ryan Roberts

arm64 can support multiple base page sizes. Instead of selecting a page size at compile time, as is done today, we will make it possible to select the desired page size on the command line.

In this case PAGE_SHIFT and it's derivatives, PAGE_SIZE and PAGE_MASK (as well as a number of other macros related to or derived from PAGE_SHIFT, but I'm not worrying about those yet), are no longer compile-time constants. So the code base needs to cope with that.

Quoted 8 lines

As a first step, introduce MIN and MAX variants of these macros, which express the range of possible page sizes. These are always compile-time constants and can be used in many places where PAGE_[SHIFT|SIZE|MASK] were previously used where a compile-time constant is required. (Subsequent patches will do that conversion work). When the arch/build doesn't support boot-time page size selection, the MIN and MAX variants are equal and everything resolves as it did previously.

MIN and MAX appear to construct a boundary, but it may be not enough. Please see the following comment inline.

Quoted 35 lines

Additionally, introduce DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() which wrap global variable defintions so that for boot-time page size selection builds, the variable being wrapped is initialized at boot-time, instead of compile-time. This is done by defining a function to do the assignment, which has the "constructor" attribute. Constructor is preferred over initcall, because when compiling a module, the module is limited to a single initcall but constructors are unlimited. For built-in code, constructors are now called earlier to guarrantee that the variables are initialized by the time they are used. Any arch that wants to enable boot-time page size selection will need to select CONFIG_CONSTRUCTORS.

These new macros need to be available anywhere PAGE_SHIFT and friends are available. Those are defined via asm/page.h (although some arches have a sub-include that defines them). Unfortunately there is no reliable asm-generic header we can easily piggy-back on, so let's define a new one, pgtable-geometry.h, which we include near where each arch defines PAGE_SHIFT. Ugh.

-------

Most of the problems that need to be solved over the next few patches fall into these broad categories, which are all solved with the help of these new macros:

1. Assignment of values derived from PAGE_SIZE in global variables

For boot-time page size builds, we must defer the initialization of these variables until boot-time, when the page size is known. See DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() as described above.

2. Define static storage in units related to PAGE_SIZE

This static storage will be defined according to PAGE_SIZE_MAX.

Quoted 8 lines

3. Define size of struct so that it is related to PAGE_SIZE

The struct often contains an array that is sized to fill the page. In this case, use a flexible array with dynamic allocation. In other cases, the struct fits exactly over a page, which is a header (e.g. swap file header). In this case, remove the padding, and manually determine the struct pointer within the page.

About two years ago, I tried to do similar thing in your series, but ran into problem at this point, or maybe not exactly as the point you list here. I consider this as the most challenged part.

The scenario is

struct X {
	a[size_a];
	b[size_b];
	c;
};

Where size_a = f(PAGE_SHIFT), size_b=g(PAGE_SHIFT). One of f() and g() is proportional to PAGE_SHIFT, the other is inversely proportional.

How can you fix the reference of X.a and X.b?

Thanks,

Pingfan

Quoted 399 lines

4. BUILD_BUG_ON() with values derived from PAGE_SIZE

In most cases, we can change these to compare againt the appropriate limit (either MIN or MAX). In other cases, we must change these to run-time BUG_ON().

5. Ensure page alignment of static data structures

Align instead to PAGE_SIZE_MAX.

6. #ifdeffery based on PAGE_SIZE

Often these can be changed to c code constructs. e.g. a macro that returns a different value depending on page size can be changed to use the ternary operator and the compiler will dead code strip it for the compile-time constant case and runtime evaluate it for the non-const case. Or #if/#else/#endif within a function can be converted to c if/else blocks, which are also dead code stripped for the const case. Sometimes we can change the c-preprocessor logic to use the appropriate MIN/MAX limit.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 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 | 71 ++++++++++++++++++++++++++
 init/main.c                            |  5 +-
 23 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 include/asm-generic/pgtable-geometry.h
arch/alpha/include/asm/page.h +1 -0
index 70419e6be1a35..d0096fb5521b8 100644
@@ -88,5 +88,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 def0dfb95b436..8d56549db7a33 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 62af9f7f9e963..417aa8533c718 100644
@@ -191,5 +191,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 792e9fe881dcf..d69971cf49cd2 100644
@@ -15,4 +15,6 @@
 #define PAGE_SIZE		(_AC(1, UL) << PAGE_SHIFT)
 #define PAGE_MASK		(~(PAGE_SIZE-1))
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_PAGE_DEF_H */
arch/csky/include/asm/page.h +3 -0
index 0ca6c408c07f2..95173d57adc8b 100644
@@ -92,4 +92,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #include <asm-generic/getorder.h>
 
 #endif /* !__ASSEMBLY__ */
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_CSKY_PAGE_H */
arch/hexagon/include/asm/page.h +2 -0
index 8a6af57274c2d..ba7ad5231695f 100644
@@ -139,4 +139,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 e85df33f11c77..9862e8fb047a6 100644
@@ -123,4 +123,6 @@ extern int __virt_addr_valid(volatile void *kaddr);
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_PAGE_H */
arch/m68k/include/asm/page.h +1 -0
index 8cfb84b499751..4df4681b02194 100644
@@ -60,5 +60,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 8810f4f1c3b02..abc23c3d743bd 100644
@@ -142,5 +142,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 4609cb0326cf3..3d91021538f02 100644
@@ -227,5 +227,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 0722f88e63cc7..2e5f93beb42b7 100644
@@ -97,4 +97,6 @@ extern struct page *mem_map;
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_NIOS2_PAGE_H */
arch/openrisc/include/asm/page.h +1 -0
index 1d5913f67c312..a0da2a9842241 100644
@@ -88,5 +88,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 4bea2e95798f0..2a75496237c09 100644
@@ -173,6 +173,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 83d0a4fc5f755..4601c115b6485 100644
@@ -300,4 +300,6 @@ static inline unsigned long kaslr_offset(void)
 #include <asm-generic/memory_model.h>
 #endif /* __ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_POWERPC_PAGE_H */
arch/riscv/include/asm/page.h +1 -0
index 7ede2111c5917..e5af7579e45bf 100644
@@ -204,5 +204,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 16e4caa931f1f..42157e7690a77 100644
@@ -275,6 +275,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 f780b467e75d7..09533d46ef033 100644
@@ -162,5 +162,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 5e44cdf2a8f2b..4327fe2bfa010 100644
@@ -9,4 +9,7 @@
 #else
 #include <asm/page_32.h>
 #endif
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif
arch/um/include/asm/page.h +2 -0
index 9ef9a8aedfa66..f26011808f514 100644
@@ -119,4 +119,6 @@ extern unsigned long uml_physmem;
 #define __HAVE_ARCH_GATE_AREA 1
 #endif
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* __UM_PAGE_H */
arch/x86/include/asm/page_types.h +2 -0
index 52f1b4ff0cc16..6d2381342047f 100644
@@ -71,4 +71,6 @@ extern void initmem_init(void);
 
 #endif	/* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* _ASM_X86_PAGE_DEFS_H */
arch/xtensa/include/asm/page.h +1 -0
index 4db56ef052d22..86952cb32af23 100644
@@ -200,4 +200,5 @@ static inline unsigned long ___pa(unsigned long va)
 #endif /* __ASSEMBLY__ */
 
 #include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
 #endif /* _XTENSA_PAGE_H */
include/asm-generic/pgtable-geometry.h new file +71 -0
new file mode 100644
index 0000000000000..358e729a6ac37
@@ -0,0 +1,71 @@
+/* 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 pure initcall is registered and run during boot to
+ * initialize the variable.
+ *
+ * @type: Unqualified type. Do not include "const"; implied by macro variant.
+ * @name: Variable name.
+ * @...:  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 int __init __attribute__((constructor)) __##name##_init(void)	\
+	{								\
+		name = (type)__VA_ARGS__;				\
+		return 0;						\
+	}
+
+#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
+
+#endif /* ASM_GENERIC_PGTABLE_GEOMETRY_H */
init/main.c +4 -1
index 206acdde51f5a..ba1515eb20b9d 100644
@@ -899,6 +899,8 @@ static void __init early_numa_node_init(void)
 #endif
 }
 
+static __init void do_ctors(void);
+
 asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
 void start_kernel(void)
 {
@@ -910,6 +912,8 @@ void start_kernel(void)
 	debug_objects_early_init();
 	init_vmlinux_build_id();
 
+	do_ctors();
+
 	cgroup_init_early();
 
 	local_irq_disable();
@@ -1360,7 +1364,6 @@ static void __init do_basic_setup(void)
 	cpuset_init_smp();
 	driver_init();
 	init_irq_proc();
-	do_ctors();
 	do_initcalls();
 }
 
Signature · 1 line
2.43.0
author reviewer
#139 lore raw

Re: [RFC PATCH v1 01/57] mm: Add macros ahead of supporting boot-time page size selection

Quoted 79 lines · Pingfan Liu

Hello Ryan,

Quoted 9 lines · Ryan Roberts

arm64 can support multiple base page sizes. Instead of selecting a page size at compile time, as is done today, we will make it possible to select the desired page size on the command line.

In this case PAGE_SHIFT and it's derivatives, PAGE_SIZE and PAGE_MASK (as well as a number of other macros related to or derived from PAGE_SHIFT, but I'm not worrying about those yet), are no longer compile-time constants. So the code base needs to cope with that.

Quoted 8 lines

As a first step, introduce MIN and MAX variants of these macros, which express the range of possible page sizes. These are always compile-time constants and can be used in many places where PAGE_[SHIFT|SIZE|MASK] were previously used where a compile-time constant is required. (Subsequent patches will do that conversion work). When the arch/build doesn't support boot-time page size selection, the MIN and MAX variants are equal and everything resolves as it did previously.

MIN and MAX appear to construct a boundary, but it may be not enough. Please see the following comment inline.

Quoted 35 lines

Additionally, introduce DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() which wrap global variable defintions so that for boot-time page size selection builds, the variable being wrapped is initialized at boot-time, instead of compile-time. This is done by defining a function to do the assignment, which has the "constructor" attribute. Constructor is preferred over initcall, because when compiling a module, the module is limited to a single initcall but constructors are unlimited. For built-in code, constructors are now called earlier to guarrantee that the variables are initialized by the time they are used. Any arch that wants to enable boot-time page size selection will need to select CONFIG_CONSTRUCTORS.

These new macros need to be available anywhere PAGE_SHIFT and friends are available. Those are defined via asm/page.h (although some arches have a sub-include that defines them). Unfortunately there is no reliable asm-generic header we can easily piggy-back on, so let's define a new one, pgtable-geometry.h, which we include near where each arch defines PAGE_SHIFT. Ugh.

-------

Most of the problems that need to be solved over the next few patches fall into these broad categories, which are all solved with the help of these new macros:

1. Assignment of values derived from PAGE_SIZE in global variables

For boot-time page size builds, we must defer the initialization of these variables until boot-time, when the page size is known. See DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() as described above.

2. Define static storage in units related to PAGE_SIZE

This static storage will be defined according to PAGE_SIZE_MAX.

Quoted 8 lines

3. Define size of struct so that it is related to PAGE_SIZE

The struct often contains an array that is sized to fill the page. In this case, use a flexible array with dynamic allocation. In other cases, the struct fits exactly over a page, which is a header (e.g. swap file header). In this case, remove the padding, and manually determine the struct pointer within the page.

About two years ago, I tried to do similar thing in your series, but ran into problem at this point, or maybe not exactly as the point you list here. I consider this as the most challenged part.

The scenario is

struct X {
	a[size_a];
	b[size_b];
	c;
};
Quoted 4 lines

Where size_a = f(PAGE_SHIFT), size_b=g(PAGE_SHIFT). One of f() and g() is proportional to PAGE_SHIFT, the other is inversely proportional.

How can you fix the reference of X.a and X.b?

If you need to allocate static memory, then in this scenario, assuming f() is proportional and g() is inversely-proportional, then I guess you need size_a=f(PAGE_SIZE_MAX) and size_b=g(PAGE_SIZE_MIN). Or if you can allocate the memory dynamically, then make a and b pointers to dynamically allocated buffers.

Is there a specific place in the source where this pattern is used today? It might be easier to discuss in the context of the code if so.

Thanks,
Ryan

Quoted 406 lines

Thanks,

Pingfan

Quoted 399 lines

4. BUILD_BUG_ON() with values derived from PAGE_SIZE

In most cases, we can change these to compare againt the appropriate limit (either MIN or MAX). In other cases, we must change these to run-time BUG_ON().

5. Ensure page alignment of static data structures

Align instead to PAGE_SIZE_MAX.

6. #ifdeffery based on PAGE_SIZE

Often these can be changed to c code constructs. e.g. a macro that returns a different value depending on page size can be changed to use the ternary operator and the compiler will dead code strip it for the compile-time constant case and runtime evaluate it for the non-const case. Or #if/#else/#endif within a function can be converted to c if/else blocks, which are also dead code stripped for the const case. Sometimes we can change the c-preprocessor logic to use the appropriate MIN/MAX limit.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 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 | 71 ++++++++++++++++++++++++++
 init/main.c                            |  5 +-
 23 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 include/asm-generic/pgtable-geometry.h
arch/alpha/include/asm/page.h +1 -0
index 70419e6be1a35..d0096fb5521b8 100644
@@ -88,5 +88,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 def0dfb95b436..8d56549db7a33 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 62af9f7f9e963..417aa8533c718 100644
@@ -191,5 +191,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 792e9fe881dcf..d69971cf49cd2 100644
@@ -15,4 +15,6 @@
 #define PAGE_SIZE		(_AC(1, UL) << PAGE_SHIFT)
 #define PAGE_MASK		(~(PAGE_SIZE-1))
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_PAGE_DEF_H */
arch/csky/include/asm/page.h +3 -0
index 0ca6c408c07f2..95173d57adc8b 100644
@@ -92,4 +92,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #include <asm-generic/getorder.h>
 
 #endif /* !__ASSEMBLY__ */
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_CSKY_PAGE_H */
arch/hexagon/include/asm/page.h +2 -0
index 8a6af57274c2d..ba7ad5231695f 100644
@@ -139,4 +139,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 e85df33f11c77..9862e8fb047a6 100644
@@ -123,4 +123,6 @@ extern int __virt_addr_valid(volatile void *kaddr);
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_PAGE_H */
arch/m68k/include/asm/page.h +1 -0
index 8cfb84b499751..4df4681b02194 100644
@@ -60,5 +60,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 8810f4f1c3b02..abc23c3d743bd 100644
@@ -142,5 +142,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 4609cb0326cf3..3d91021538f02 100644
@@ -227,5 +227,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 0722f88e63cc7..2e5f93beb42b7 100644
@@ -97,4 +97,6 @@ extern struct page *mem_map;
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_NIOS2_PAGE_H */
arch/openrisc/include/asm/page.h +1 -0
index 1d5913f67c312..a0da2a9842241 100644
@@ -88,5 +88,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 4bea2e95798f0..2a75496237c09 100644
@@ -173,6 +173,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 83d0a4fc5f755..4601c115b6485 100644
@@ -300,4 +300,6 @@ static inline unsigned long kaslr_offset(void)
 #include <asm-generic/memory_model.h>
 #endif /* __ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_POWERPC_PAGE_H */
arch/riscv/include/asm/page.h +1 -0
index 7ede2111c5917..e5af7579e45bf 100644
@@ -204,5 +204,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 16e4caa931f1f..42157e7690a77 100644
@@ -275,6 +275,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 f780b467e75d7..09533d46ef033 100644
@@ -162,5 +162,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 5e44cdf2a8f2b..4327fe2bfa010 100644
@@ -9,4 +9,7 @@
 #else
 #include <asm/page_32.h>
 #endif
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif
arch/um/include/asm/page.h +2 -0
index 9ef9a8aedfa66..f26011808f514 100644
@@ -119,4 +119,6 @@ extern unsigned long uml_physmem;
 #define __HAVE_ARCH_GATE_AREA 1
 #endif
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* __UM_PAGE_H */
arch/x86/include/asm/page_types.h +2 -0
index 52f1b4ff0cc16..6d2381342047f 100644
@@ -71,4 +71,6 @@ extern void initmem_init(void);
 
 #endif	/* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* _ASM_X86_PAGE_DEFS_H */
arch/xtensa/include/asm/page.h +1 -0
index 4db56ef052d22..86952cb32af23 100644
@@ -200,4 +200,5 @@ static inline unsigned long ___pa(unsigned long va)
 #endif /* __ASSEMBLY__ */
 
 #include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
 #endif /* _XTENSA_PAGE_H */
include/asm-generic/pgtable-geometry.h new file +71 -0
new file mode 100644
index 0000000000000..358e729a6ac37
@@ -0,0 +1,71 @@
+/* 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 pure initcall is registered and run during boot to
+ * initialize the variable.
+ *
+ * @type: Unqualified type. Do not include "const"; implied by macro variant.
+ * @name: Variable name.
+ * @...:  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 int __init __attribute__((constructor)) __##name##_init(void)	\
+	{								\
+		name = (type)__VA_ARGS__;				\
+		return 0;						\
+	}
+
+#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
+
+#endif /* ASM_GENERIC_PGTABLE_GEOMETRY_H */
init/main.c +4 -1
index 206acdde51f5a..ba1515eb20b9d 100644
@@ -899,6 +899,8 @@ static void __init early_numa_node_init(void)
 #endif
 }
 
+static __init void do_ctors(void);
+
 asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
 void start_kernel(void)
 {
@@ -910,6 +912,8 @@ void start_kernel(void)
 	debug_objects_early_init();
 	init_vmlinux_build_id();
 
+	do_ctors();
+
 	cgroup_init_early();
 
 	local_irq_disable();
@@ -1360,7 +1364,6 @@ static void __init do_basic_setup(void)
 	cpuset_init_smp();
 	driver_init();
 	init_irq_proc();
-	do_ctors();
 	do_initcalls();
 }
 
Signature · 1 line
2.43.0
#140 lore raw

Re: [RFC PATCH v1 01/57] mm: Add macros ahead of supporting boot-time page size selection

Quoted 81 lines · Ryan Roberts
Quoted 79 lines · Pingfan Liu

Hello Ryan,

Quoted 9 lines · Ryan Roberts

arm64 can support multiple base page sizes. Instead of selecting a page size at compile time, as is done today, we will make it possible to select the desired page size on the command line.

In this case PAGE_SHIFT and it's derivatives, PAGE_SIZE and PAGE_MASK (as well as a number of other macros related to or derived from PAGE_SHIFT, but I'm not worrying about those yet), are no longer compile-time constants. So the code base needs to cope with that.

Quoted 8 lines

As a first step, introduce MIN and MAX variants of these macros, which express the range of possible page sizes. These are always compile-time constants and can be used in many places where PAGE_[SHIFT|SIZE|MASK] were previously used where a compile-time constant is required. (Subsequent patches will do that conversion work). When the arch/build doesn't support boot-time page size selection, the MIN and MAX variants are equal and everything resolves as it did previously.

MIN and MAX appear to construct a boundary, but it may be not enough. Please see the following comment inline.

Quoted 35 lines

Additionally, introduce DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() which wrap global variable defintions so that for boot-time page size selection builds, the variable being wrapped is initialized at boot-time, instead of compile-time. This is done by defining a function to do the assignment, which has the "constructor" attribute. Constructor is preferred over initcall, because when compiling a module, the module is limited to a single initcall but constructors are unlimited. For built-in code, constructors are now called earlier to guarrantee that the variables are initialized by the time they are used. Any arch that wants to enable boot-time page size selection will need to select CONFIG_CONSTRUCTORS.

These new macros need to be available anywhere PAGE_SHIFT and friends are available. Those are defined via asm/page.h (although some arches have a sub-include that defines them). Unfortunately there is no reliable asm-generic header we can easily piggy-back on, so let's define a new one, pgtable-geometry.h, which we include near where each arch defines PAGE_SHIFT. Ugh.

-------

Most of the problems that need to be solved over the next few patches fall into these broad categories, which are all solved with the help of these new macros:

1. Assignment of values derived from PAGE_SIZE in global variables

For boot-time page size builds, we must defer the initialization of these variables until boot-time, when the page size is known. See DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() as described above.

2. Define static storage in units related to PAGE_SIZE

This static storage will be defined according to PAGE_SIZE_MAX.

Quoted 8 lines

3. Define size of struct so that it is related to PAGE_SIZE

The struct often contains an array that is sized to fill the page. In this case, use a flexible array with dynamic allocation. In other cases, the struct fits exactly over a page, which is a header (e.g. swap file header). In this case, remove the padding, and manually determine the struct pointer within the page.

About two years ago, I tried to do similar thing in your series, but ran into problem at this point, or maybe not exactly as the point you list here. I consider this as the most challenged part.

The scenario is

struct X {
      a[size_a];
      b[size_b];
      c;
};
Quoted 8 lines
Quoted 4 lines

Where size_a = f(PAGE_SHIFT), size_b=g(PAGE_SHIFT). One of f() and g() is proportional to PAGE_SHIFT, the other is inversely proportional.

How can you fix the reference of X.a and X.b?

If you need to allocate static memory, then in this scenario, assuming f() is proportional and g() is inversely-proportional, then I guess you need size_a=f(PAGE_SIZE_MAX) and size_b=g(PAGE_SIZE_MIN). Or if you can allocate the

My point is that such stuff can not be handled by scripts automatically and needs manual intervention.

Quoted 2 lines

memory dynamically, then make a and b pointers to dynamically allocated buffers.

This seems a better way out.

Quoted 3 lines

Is there a specific place in the source where this pattern is used today? It might be easier to discuss in the context of the code if so.

No such code at hand. Just throw out the potential issue and be curious about it which frustrates me.
I hope people can reach an agreement on it and turn this useful series into reality.

Thanks,

Pingfan

author reviewer
#141 lore raw

Re: [RFC PATCH v1 01/57] mm: Add macros ahead of supporting boot-time page size selection

Quoted 85 lines · Pingfan Liu
Quoted 84 lines · Ryan Roberts
Quoted 82 lines · Pingfan Liu

Hello Ryan,

Quoted 9 lines · Ryan Roberts

arm64 can support multiple base page sizes. Instead of selecting a page size at compile time, as is done today, we will make it possible to select the desired page size on the command line.

In this case PAGE_SHIFT and it's derivatives, PAGE_SIZE and PAGE_MASK (as well as a number of other macros related to or derived from PAGE_SHIFT, but I'm not worrying about those yet), are no longer compile-time constants. So the code base needs to cope with that.

Quoted 8 lines

As a first step, introduce MIN and MAX variants of these macros, which express the range of possible page sizes. These are always compile-time constants and can be used in many places where PAGE_[SHIFT|SIZE|MASK] were previously used where a compile-time constant is required. (Subsequent patches will do that conversion work). When the arch/build doesn't support boot-time page size selection, the MIN and MAX variants are equal and everything resolves as it did previously.

MIN and MAX appear to construct a boundary, but it may be not enough. Please see the following comment inline.

Quoted 35 lines

Additionally, introduce DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() which wrap global variable defintions so that for boot-time page size selection builds, the variable being wrapped is initialized at boot-time, instead of compile-time. This is done by defining a function to do the assignment, which has the "constructor" attribute. Constructor is preferred over initcall, because when compiling a module, the module is limited to a single initcall but constructors are unlimited. For built-in code, constructors are now called earlier to guarrantee that the variables are initialized by the time they are used. Any arch that wants to enable boot-time page size selection will need to select CONFIG_CONSTRUCTORS.

These new macros need to be available anywhere PAGE_SHIFT and friends are available. Those are defined via asm/page.h (although some arches have a sub-include that defines them). Unfortunately there is no reliable asm-generic header we can easily piggy-back on, so let's define a new one, pgtable-geometry.h, which we include near where each arch defines PAGE_SHIFT. Ugh.

-------

Most of the problems that need to be solved over the next few patches fall into these broad categories, which are all solved with the help of these new macros:

1. Assignment of values derived from PAGE_SIZE in global variables

For boot-time page size builds, we must defer the initialization of these variables until boot-time, when the page size is known. See DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() as described above.

2. Define static storage in units related to PAGE_SIZE

This static storage will be defined according to PAGE_SIZE_MAX.

Quoted 8 lines

3. Define size of struct so that it is related to PAGE_SIZE

The struct often contains an array that is sized to fill the page. In this case, use a flexible array with dynamic allocation. In other cases, the struct fits exactly over a page, which is a header (e.g. swap file header). In this case, remove the padding, and manually determine the struct pointer within the page.

About two years ago, I tried to do similar thing in your series, but ran into problem at this point, or maybe not exactly as the point you list here. I consider this as the most challenged part.

The scenario is

struct X {
      a[size_a];
      b[size_b];
      c;
};

Where size_a = f(PAGE_SHIFT), size_b=g(PAGE_SHIFT). One of f() and g() is proportional to PAGE_SHIFT, the other is inversely proportional.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

How can you fix the reference of X.a and X.b?

If you need to allocate static memory, then in this scenario, assuming f() is proportional and g() is inversely-proportional, then I guess you need size_a=f(PAGE_SIZE_MAX) and size_b=g(PAGE_SIZE_MIN). Or if you can allocate the

My point is that such stuff can not be handled by scripts automatically and needs manual intervention.

Yes agreed. I spent some time thinking about how much of this could be automated (i.e. with Cochinelle or otherwise), but concluded that it's very difficult. As a result, all of the patches in this series are manually created.

Quoted 6 lines
Quoted 3 lines

memory dynamically, then make a and b pointers to dynamically allocated buffers.

This seems a better way out.

Quoted 8 lines
Quoted 3 lines

Is there a specific place in the source where this pattern is used today? It might be easier to discuss in the context of the code if so.

No such code at hand. Just throw out the potential issue and be curious about it which frustrates me.
I hope people can reach an agreement on it and turn this useful series into reality.

Yes, hope so!

Quoted 5 lines

Thanks,

Pingfan

author reviewer
#142 lore raw

Re: [RFC PATCH v1 01/57] mm: Add macros ahead of supporting boot-time page size selection

In reply to Ryan Roberts

+ Albert Ou, Alexander Gordeev, Brian Cain, Guo Ren, Heiko Carstens, Michael Ellerman, Michal Simek, Palmer Dabbelt, Paul Walmsley, Vasily Gorbik, Vineet Gupta.

This was a rather tricky series to get the recipients correct for and my script did not realize that "supporter" was a pseudonym for "maintainer" so you were missed off the original post. Appologies!

More context in cover letter:
https://lore.kernel.org/all/[email protected]/

Quoted 456 lines · Ryan Roberts

arm64 can support multiple base page sizes. Instead of selecting a page size at compile time, as is done today, we will make it possible to select the desired page size on the command line.

In this case PAGE_SHIFT and it's derivatives, PAGE_SIZE and PAGE_MASK (as well as a number of other macros related to or derived from PAGE_SHIFT, but I'm not worrying about those yet), are no longer compile-time constants. So the code base needs to cope with that.

As a first step, introduce MIN and MAX variants of these macros, which express the range of possible page sizes. These are always compile-time constants and can be used in many places where PAGE_[SHIFT|SIZE|MASK] were previously used where a compile-time constant is required. (Subsequent patches will do that conversion work). When the arch/build doesn't support boot-time page size selection, the MIN and MAX variants are equal and everything resolves as it did previously.

Additionally, introduce DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() which wrap global variable defintions so that for boot-time page size selection builds, the variable being wrapped is initialized at boot-time, instead of compile-time. This is done by defining a function to do the assignment, which has the "constructor" attribute. Constructor is preferred over initcall, because when compiling a module, the module is limited to a single initcall but constructors are unlimited. For built-in code, constructors are now called earlier to guarrantee that the variables are initialized by the time they are used. Any arch that wants to enable boot-time page size selection will need to select CONFIG_CONSTRUCTORS.

These new macros need to be available anywhere PAGE_SHIFT and friends are available. Those are defined via asm/page.h (although some arches have a sub-include that defines them). Unfortunately there is no reliable asm-generic header we can easily piggy-back on, so let's define a new one, pgtable-geometry.h, which we include near where each arch defines PAGE_SHIFT. Ugh.

-------

Most of the problems that need to be solved over the next few patches fall into these broad categories, which are all solved with the help of these new macros:

1. Assignment of values derived from PAGE_SIZE in global variables

For boot-time page size builds, we must defer the initialization of these variables until boot-time, when the page size is known. See DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() as described above.

2. Define static storage in units related to PAGE_SIZE

This static storage will be defined according to PAGE_SIZE_MAX.

3. Define size of struct so that it is related to PAGE_SIZE

The struct often contains an array that is sized to fill the page. In this case, use a flexible array with dynamic allocation. In other cases, the struct fits exactly over a page, which is a header (e.g. swap file header). In this case, remove the padding, and manually determine the struct pointer within the page.

4. BUILD_BUG_ON() with values derived from PAGE_SIZE

In most cases, we can change these to compare againt the appropriate limit (either MIN or MAX). In other cases, we must change these to run-time BUG_ON().

5. Ensure page alignment of static data structures

Align instead to PAGE_SIZE_MAX.

6. #ifdeffery based on PAGE_SIZE

Often these can be changed to c code constructs. e.g. a macro that returns a different value depending on page size can be changed to use the ternary operator and the compiler will dead code strip it for the compile-time constant case and runtime evaluate it for the non-const case. Or #if/#else/#endif within a function can be converted to c if/else blocks, which are also dead code stripped for the const case. Sometimes we can change the c-preprocessor logic to use the appropriate MIN/MAX limit.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 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 | 71 ++++++++++++++++++++++++++
 init/main.c                            |  5 +-
 23 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 include/asm-generic/pgtable-geometry.h
arch/alpha/include/asm/page.h +1 -0
index 70419e6be1a35..d0096fb5521b8 100644
@@ -88,5 +88,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 def0dfb95b436..8d56549db7a33 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 62af9f7f9e963..417aa8533c718 100644
@@ -191,5 +191,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 792e9fe881dcf..d69971cf49cd2 100644
@@ -15,4 +15,6 @@
 #define PAGE_SIZE		(_AC(1, UL) << PAGE_SHIFT)
 #define PAGE_MASK		(~(PAGE_SIZE-1))
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_PAGE_DEF_H */
arch/csky/include/asm/page.h +3 -0
index 0ca6c408c07f2..95173d57adc8b 100644
@@ -92,4 +92,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #include <asm-generic/getorder.h>
 
 #endif /* !__ASSEMBLY__ */
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_CSKY_PAGE_H */
arch/hexagon/include/asm/page.h +2 -0
index 8a6af57274c2d..ba7ad5231695f 100644
@@ -139,4 +139,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 e85df33f11c77..9862e8fb047a6 100644
@@ -123,4 +123,6 @@ extern int __virt_addr_valid(volatile void *kaddr);
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_PAGE_H */
arch/m68k/include/asm/page.h +1 -0
index 8cfb84b499751..4df4681b02194 100644
@@ -60,5 +60,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 8810f4f1c3b02..abc23c3d743bd 100644
@@ -142,5 +142,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 4609cb0326cf3..3d91021538f02 100644
@@ -227,5 +227,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 0722f88e63cc7..2e5f93beb42b7 100644
@@ -97,4 +97,6 @@ extern struct page *mem_map;
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_NIOS2_PAGE_H */
arch/openrisc/include/asm/page.h +1 -0
index 1d5913f67c312..a0da2a9842241 100644
@@ -88,5 +88,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 4bea2e95798f0..2a75496237c09 100644
@@ -173,6 +173,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 83d0a4fc5f755..4601c115b6485 100644
@@ -300,4 +300,6 @@ static inline unsigned long kaslr_offset(void)
 #include <asm-generic/memory_model.h>
 #endif /* __ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_POWERPC_PAGE_H */
arch/riscv/include/asm/page.h +1 -0
index 7ede2111c5917..e5af7579e45bf 100644
@@ -204,5 +204,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 16e4caa931f1f..42157e7690a77 100644
@@ -275,6 +275,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 f780b467e75d7..09533d46ef033 100644
@@ -162,5 +162,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 5e44cdf2a8f2b..4327fe2bfa010 100644
@@ -9,4 +9,7 @@
 #else
 #include <asm/page_32.h>
 #endif
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif
arch/um/include/asm/page.h +2 -0
index 9ef9a8aedfa66..f26011808f514 100644
@@ -119,4 +119,6 @@ extern unsigned long uml_physmem;
 #define __HAVE_ARCH_GATE_AREA 1
 #endif
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* __UM_PAGE_H */
arch/x86/include/asm/page_types.h +2 -0
index 52f1b4ff0cc16..6d2381342047f 100644
@@ -71,4 +71,6 @@ extern void initmem_init(void);
 
 #endif	/* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* _ASM_X86_PAGE_DEFS_H */
arch/xtensa/include/asm/page.h +1 -0
index 4db56ef052d22..86952cb32af23 100644
@@ -200,4 +200,5 @@ static inline unsigned long ___pa(unsigned long va)
 #endif /* __ASSEMBLY__ */
 
 #include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
 #endif /* _XTENSA_PAGE_H */
include/asm-generic/pgtable-geometry.h new file +71 -0
new file mode 100644
index 0000000000000..358e729a6ac37
@@ -0,0 +1,71 @@
+/* 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 pure initcall is registered and run during boot to
+ * initialize the variable.
+ *
+ * @type: Unqualified type. Do not include "const"; implied by macro variant.
+ * @name: Variable name.
+ * @...:  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 int __init __attribute__((constructor)) __##name##_init(void)	\
+	{								\
+		name = (type)__VA_ARGS__;				\
+		return 0;						\
+	}
+
+#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
+
+#endif /* ASM_GENERIC_PGTABLE_GEOMETRY_H */
init/main.c +4 -1
index 206acdde51f5a..ba1515eb20b9d 100644
@@ -899,6 +899,8 @@ static void __init early_numa_node_init(void)
 #endif
 }
 
+static __init void do_ctors(void);
+
 asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
 void start_kernel(void)
 {
@@ -910,6 +912,8 @@ void start_kernel(void)
 	debug_objects_early_init();
 	init_vmlinux_build_id();
 
+	do_ctors();
+
 	cgroup_init_early();
 
 	local_irq_disable();
@@ -1360,7 +1364,6 @@ static void __init do_basic_setup(void)
 	cpuset_init_smp();
 	driver_init();
 	init_irq_proc();
-	do_ctors();
 	do_initcalls();
 }
 
author reviewer
#143 lore raw

Re: [RFC PATCH v1 01/57] mm: Add macros ahead of supporting boot-time page size selection

In reply to Ryan Roberts

Hi all (especially mm people!),

Quoted 29 lines · Ryan Roberts

arm64 can support multiple base page sizes. Instead of selecting a page size at compile time, as is done today, we will make it possible to select the desired page size on the command line.

In this case PAGE_SHIFT and it's derivatives, PAGE_SIZE and PAGE_MASK (as well as a number of other macros related to or derived from PAGE_SHIFT, but I'm not worrying about those yet), are no longer compile-time constants. So the code base needs to cope with that.

As a first step, introduce MIN and MAX variants of these macros, which express the range of possible page sizes. These are always compile-time constants and can be used in many places where PAGE_[SHIFT|SIZE|MASK] were previously used where a compile-time constant is required. (Subsequent patches will do that conversion work). When the arch/build doesn't support boot-time page size selection, the MIN and MAX variants are equal and everything resolves as it did previously.

Additionally, introduce DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() which wrap global variable defintions so that for boot-time page size selection builds, the variable being wrapped is initialized at boot-time, instead of compile-time. This is done by defining a function to do the assignment, which has the "constructor" attribute. Constructor is preferred over initcall, because when compiling a module, the module is limited to a single initcall but constructors are unlimited. For built-in code, constructors are now called earlier to guarrantee that the variables are initialized by the time they are used. Any arch that wants to enable boot-time page size selection will need to select CONFIG_CONSTRUCTORS.

Quoted 6 lines

These new macros need to be available anywhere PAGE_SHIFT and friends are available. Those are defined via asm/page.h (although some arches have a sub-include that defines them). Unfortunately there is no reliable asm-generic header we can easily piggy-back on, so let's define a new one, pgtable-geometry.h, which we include near where each arch defines PAGE_SHIFT. Ugh.

I haven't had any feedback on this particular patch yet. It would be great to get this one into v6.13, since once this is in place, the changes in other subsystems can go via their respective trees without any dependency issues. Although time is getting tight.

If anyone has any feedback for this patch it would be great to hear it now. Then I'll re-post on it's own in a couple of days time.

Thanks,
Ryan

Quoted 421 lines

-------

Most of the problems that need to be solved over the next few patches fall into these broad categories, which are all solved with the help of these new macros:

1. Assignment of values derived from PAGE_SIZE in global variables

For boot-time page size builds, we must defer the initialization of these variables until boot-time, when the page size is known. See DEFINE_GLOBAL_PAGE_SIZE_VAR[_CONST]() as described above.

2. Define static storage in units related to PAGE_SIZE

This static storage will be defined according to PAGE_SIZE_MAX.

3. Define size of struct so that it is related to PAGE_SIZE

The struct often contains an array that is sized to fill the page. In this case, use a flexible array with dynamic allocation. In other cases, the struct fits exactly over a page, which is a header (e.g. swap file header). In this case, remove the padding, and manually determine the struct pointer within the page.

4. BUILD_BUG_ON() with values derived from PAGE_SIZE

In most cases, we can change these to compare againt the appropriate limit (either MIN or MAX). In other cases, we must change these to run-time BUG_ON().

5. Ensure page alignment of static data structures

Align instead to PAGE_SIZE_MAX.

6. #ifdeffery based on PAGE_SIZE

Often these can be changed to c code constructs. e.g. a macro that returns a different value depending on page size can be changed to use the ternary operator and the compiler will dead code strip it for the compile-time constant case and runtime evaluate it for the non-const case. Or #if/#else/#endif within a function can be converted to c if/else blocks, which are also dead code stripped for the const case. Sometimes we can change the c-preprocessor logic to use the appropriate MIN/MAX limit.

---

***NOTE***
Any confused maintainers may want to read the cover note here for context:
https://lore.kernel.org/all/[email protected]/

 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 | 71 ++++++++++++++++++++++++++
 init/main.c                            |  5 +-
 23 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 include/asm-generic/pgtable-geometry.h
arch/alpha/include/asm/page.h +1 -0
index 70419e6be1a35..d0096fb5521b8 100644
@@ -88,5 +88,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 def0dfb95b436..8d56549db7a33 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 62af9f7f9e963..417aa8533c718 100644
@@ -191,5 +191,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 792e9fe881dcf..d69971cf49cd2 100644
@@ -15,4 +15,6 @@
 #define PAGE_SIZE		(_AC(1, UL) << PAGE_SHIFT)
 #define PAGE_MASK		(~(PAGE_SIZE-1))
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_PAGE_DEF_H */
arch/csky/include/asm/page.h +3 -0
index 0ca6c408c07f2..95173d57adc8b 100644
@@ -92,4 +92,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #include <asm-generic/getorder.h>
 
 #endif /* !__ASSEMBLY__ */
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* __ASM_CSKY_PAGE_H */
arch/hexagon/include/asm/page.h +2 -0
index 8a6af57274c2d..ba7ad5231695f 100644
@@ -139,4 +139,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 e85df33f11c77..9862e8fb047a6 100644
@@ -123,4 +123,6 @@ extern int __virt_addr_valid(volatile void *kaddr);
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_PAGE_H */
arch/m68k/include/asm/page.h +1 -0
index 8cfb84b499751..4df4681b02194 100644
@@ -60,5 +60,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 8810f4f1c3b02..abc23c3d743bd 100644
@@ -142,5 +142,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 4609cb0326cf3..3d91021538f02 100644
@@ -227,5 +227,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 0722f88e63cc7..2e5f93beb42b7 100644
@@ -97,4 +97,6 @@ extern struct page *mem_map;
 
 #endif /* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_NIOS2_PAGE_H */
arch/openrisc/include/asm/page.h +1 -0
index 1d5913f67c312..a0da2a9842241 100644
@@ -88,5 +88,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 4bea2e95798f0..2a75496237c09 100644
@@ -173,6 +173,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 83d0a4fc5f755..4601c115b6485 100644
@@ -300,4 +300,6 @@ static inline unsigned long kaslr_offset(void)
 #include <asm-generic/memory_model.h>
 #endif /* __ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif /* _ASM_POWERPC_PAGE_H */
arch/riscv/include/asm/page.h +1 -0
index 7ede2111c5917..e5af7579e45bf 100644
@@ -204,5 +204,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 16e4caa931f1f..42157e7690a77 100644
@@ -275,6 +275,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 f780b467e75d7..09533d46ef033 100644
@@ -162,5 +162,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 5e44cdf2a8f2b..4327fe2bfa010 100644
@@ -9,4 +9,7 @@
 #else
 #include <asm/page_32.h>
 #endif
+
+#include <asm-generic/pgtable-geometry.h>
+
 #endif
arch/um/include/asm/page.h +2 -0
index 9ef9a8aedfa66..f26011808f514 100644
@@ -119,4 +119,6 @@ extern unsigned long uml_physmem;
 #define __HAVE_ARCH_GATE_AREA 1
 #endif
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* __UM_PAGE_H */
arch/x86/include/asm/page_types.h +2 -0
index 52f1b4ff0cc16..6d2381342047f 100644
@@ -71,4 +71,6 @@ extern void initmem_init(void);
 
 #endif	/* !__ASSEMBLY__ */
 
+#include <asm-generic/pgtable-geometry.h>
+
 #endif	/* _ASM_X86_PAGE_DEFS_H */
arch/xtensa/include/asm/page.h +1 -0
index 4db56ef052d22..86952cb32af23 100644
@@ -200,4 +200,5 @@ static inline unsigned long ___pa(unsigned long va)
 #endif /* __ASSEMBLY__ */
 
 #include <asm-generic/memory_model.h>
+#include <asm-generic/pgtable-geometry.h>
 #endif /* _XTENSA_PAGE_H */
include/asm-generic/pgtable-geometry.h new file +71 -0
new file mode 100644
index 0000000000000..358e729a6ac37
@@ -0,0 +1,71 @@
+/* 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 pure initcall is registered and run during boot to
+ * initialize the variable.
+ *
+ * @type: Unqualified type. Do not include "const"; implied by macro variant.
+ * @name: Variable name.
+ * @...:  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 int __init __attribute__((constructor)) __##name##_init(void)	\
+	{								\
+		name = (type)__VA_ARGS__;				\
+		return 0;						\
+	}
+
+#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
+
+#endif /* ASM_GENERIC_PGTABLE_GEOMETRY_H */
init/main.c +4 -1
index 206acdde51f5a..ba1515eb20b9d 100644
@@ -899,6 +899,8 @@ static void __init early_numa_node_init(void)
 #endif
 }
 
+static __init void do_ctors(void);
+
 asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
 void start_kernel(void)
 {
@@ -910,6 +912,8 @@ void start_kernel(void)
 	debug_objects_early_init();
 	init_vmlinux_build_id();
 
+	do_ctors();
+
 	cgroup_init_early();
 
 	local_irq_disable();
@@ -1360,7 +1364,6 @@ static void __init do_basic_setup(void)
 	cpuset_init_smp();
 	driver_init();
 	init_irq_proc();
-	do_ctors();
 	do_initcalls();
 }
 
#144 lore raw
In reply to Ryan Roberts
Quoted 66 lines · Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

Quoted 5 lines

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

FWIW, this paragraph was not entirely clear to me until I looked at patch 57 to see that the compile time page size selection had been retained, and could continue to be used as-is. It was somewhat implicit, but not IMHO explicit enough, not a big deal though.

Great work, thanks for doing that! This makes me wonder if we could leverage any of that to have a single kernel supporting both LPAE and !LPAE on ARM 32-bit, but that still seems like somewhat more difficult, largely due to the difference in the page table descriptor format (long vs. short).

Signature · 1 line
Florian
author reviewer
#145 lore raw
Quoted 73 lines · Florian Fainelli
Quoted 66 lines · Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

Quoted 5 lines

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Quoted 4 lines

FWIW, this paragraph was not entirely clear to me until I looked at patch 57 to see that the compile time page size selection had been retained, and could continue to be used as-is. It was somewhat implicit, but not IMHO explicit enough, not a big deal though.

I intended to make that bit clear with the above sentance "arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.", but appreciate there is a lot going on here.

Quoted 5 lines

Great work, thanks for doing that! This makes me wonder if we could leverage any of that to have a single kernel supporting both LPAE and !LPAE on ARM 32-bit, but that still seems like somewhat more difficult, largely due to the difference in the page table descriptor format (long vs. short).

We will eventually have the exact same problem with FEAT_D128 on arm64. This introduces page tables with 128 bit PTEs. Ideally we would like to support both in a single image, although, we have much more thinking to do on that. But my current view is that this series solves a bunch of problems that makes it easier (PTRS_PER_Pxx and Pxx_SHIFT all become boot-time values, for example, so we can easily represent the different geometries).

Yes, we still need to solve the PTE size difference (in our case 64-bit vs 128-bit). I have a couple of proposals for how to do that; the "gold-plated" approach would be to create and use a handle type to represent a PTE/PxD slot in a table. Then increments/decrements would be enforced via explicit helpers that know the size, and direct dereferencing would be impossible. When accessing via helpers we would pass around pte_t/pxd_t values that are the larger size, then narrow then when writing back.

Anshuman has a series [1] that starts to move in that direction.

If you have any other ideas, it would be good to talk!

[1]
https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Ryan

#146 lore raw
In reply to Ryan Roberts

From: Ryan Roberts <[email protected]> Sent: Monday, October 14, 2024 3:55 AM

Quoted 46 lines

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

Quoted 5 lines

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

[snip]

This is pretty cool. :-) FWIW, I've built a kernel with this patch set, and have it running in a RHEL 8.7 guest on Hyper-V in the Azure public cloud. Ran with 4K, 16K, and 64K page sizes, and the basic smoke tests work.

The Hyper-V specific code in the Linux kernel needed a few tweaks to deal with PAGE_SIZE and friends no longer being constant, but it's nothing significant. Getting the kernel built in the first place was a little harder because my .config file is fairly generic with a lot of device drivers and file system code that aren't really needed for Hyper-V guests. I had to weed out the ones that won't build. My RHEL 8.7 install uses LVM, so I hacked the 'dm' code to make it compile and run.

As this work moves forward, I can supply the necessary patches for the Hyper-V support. Let me know if you want to include them in the main patch set.

I've added a couple of Microsoft's Linux people to this email's addressee list so they are aware of what's going on.

Michael Kelley

author reviewer
#147 lore raw
Quoted 53 lines · Michael Kelley

From: Ryan Roberts <[email protected]> Sent: Monday, October 14, 2024 3:55 AM

Quoted 46 lines

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

Quoted 5 lines

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Quoted 5 lines

[snip]

This is pretty cool. :-) FWIW, I've built a kernel with this patch set, and have it running in a RHEL 8.7 guest on Hyper-V in the Azure public cloud. Ran with 4K, 16K, and 64K page sizes, and the basic smoke tests work.

That's great to hear - thanks for taking the time to test!

Quoted 7 lines

The Hyper-V specific code in the Linux kernel needed a few tweaks to
deal with PAGE_SIZE and friends no longer being constant, but it's nothing
significant. Getting the kernel built in the first place was a little harder
because my .config file is fairly generic with a lot of device drivers and file
system code that aren't really needed for Hyper-V guests. I had to
weed out the ones that won't build. My RHEL 8.7 install uses LVM, so I> hacked the 'dm' code to make it compile and run.

Yeah, getting all this sorted is going to be the long tail. I feel I've had enough positive response to this RFC that I should probably just get on and start that work to get a real feel for how much of it there is going to be.

Quoted 4 lines

As this work moves forward, I can supply the necessary patches for the Hyper-V support. Let me know if you want to include them in the main patch set.

Great! If you are happy to forward them to me, I'll include them in future versions of the series (or more likely, serieses).

Thanks,
Ryan

Quoted 5 lines

I've added a couple of Microsoft's Linux people to this email's addressee list so they are aware of what's going on.

Michael Kelley

#148 lore raw
In reply to Ryan Roberts
Quoted 26 lines

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times, so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|-------------|---------|---------|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |
Quoted 11 lines

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last words). Values are times, so smaller is better. All relative to base-4k:

... and here I am, worrying about much smaller degradation in these micro-benchmark ;) You're right, these are pure micro-benchmarks, and while 12% does sound like "much", even stupid compiler code movement can result in such changes in the fork() micro benchmark.

So I think this is just fine, and actually "surprisingly" small. And, there is even a way to statically compile a page size and not worry about that at all.

As discussed ahead of times, I consider this change very valuable. In RHEL, the biggest issue is actually the test matrix, that cannot really be reduced significantly ... but it will make shipping/packaging easier.

CCing Don, who did the separate 64k RHEL flavor kernel.

Signature · 3 lines
Cheers,

David / dhildenb
author reviewer
#149 lore raw
Quoted 43 lines · David Hildenbrand
Quoted 26 lines

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times, so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|-------------|---------|---------|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |
Quoted 11 lines

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last words). Values are times, so smaller is better. All relative to base-4k:

... and here I am, worrying about much smaller degradation in these micro- benchmark ;) You're right, these are pure micro-benchmarks, and while 12% does sound like "much", even stupid compiler code movement can result in such changes in the fork() micro benchmark.

Quoted 9 lines

So I think this is just fine, and actually "surprisingly" small. And, there is even a way to statically compile a page size and not worry about that at all.

As discussed ahead of times, I consider this change very valuable. In RHEL, the biggest issue is actually the test matrix, that cannot really be reduced significantly ... but it will make shipping/packaging easier.

CCing Don, who did the separate 64k RHEL flavor kernel.

Thanks, David! I'm planning to investigate and see if I can improve even on that 12%. I have a couple of ideas. But like you say, I don't think this should be a blocker to moving forwards.

#150 lore raw
In reply to Ryan Roberts
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

Petr T

author reviewer
#151 lore raw
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

Thanks,
Ryan

Quoted 2 lines

Petr T

#152 lore raw
Quoted 28 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

Quoted 6 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

You're right, performance testing is my goal.

Heh, the openSUSE master config is cranked up to max. ;-) That would be a lot of work, and we don't need all those options for running our test suite. Let me disable the conflicting options instead.

For reference, here's a long (yet incomplete) list of kernel options that conflict with this v1 patch series:

# already handled by Michael
CONFIG_HYPERV

# sorry, Windows
CONFIG_CIFS
CONFIG_NTFS3_FS

# no, not even with ntfs-3g
CONFIG_FUSE_FS

# bye-bye ZSWAP
CONFIG_ZBUD
CONFIG_Z3FOLD
CONFIG_ZSMALLOC  # ah, also bye-bye ZRAM
# who needs redundancy?
CONFIG_DM_RAID
CONFIG_MD_RAID1
CONFIG_MD_RAID456
CONFIG_MD_RAID10
# who needs security?
CONFIG_SECURITY_SELINUX
# or integrity?
CONFIG_IMA
CONFIG_DM_INTEGRITY
# or even crypto (this disables A LOT of stuff)...
CONFIG_CRYPTO_MANAGER2
# meh...
CONFIG_ARM_SMMU_V3_SVA
CONFIG_ACPI_NFIT
CONFIG_DEV_DAX_PMEM
CONFIG_NVDIMM
CONFIG_MTD_SWAP
CONFIG_MLXBF_PMC
CONFIG_THUNDERX2_PMU
CONFIG_LKDTM
CONFIG_VMWARE_VMCI
CONFIG_HT16K33
CONFIG_FB_TFT_HX8340BN
CONFIG_FB_TFT_ILI9341
CONFIG_DVB_FIREDTV
CONFIG_DVB_PT3
CONFIG_VIDEO_ET8EK8
CONFIG_VIDEO_IVTV
CONFIG_VIDEO_SAA7164
CONFIG_DRM_AMDGPU
CONFIG_DRM_POWERVR
CONFIG_DRM_QXL
CONFIG_DRM_RADEON
CONFIG_DRM_VMWGFX
CONFIG_FIREWIRE_OHCI
CONFIG_SND_SEQ_MIDI
CONFIG_SND_DARLA20
CONFIG_SND_GINA20
CONFIG_SND_LAYLA20
CONFIG_SND_DARLA24
CONFIG_SND_DARLA24
CONFIG_SND_GINA24
CONFIG_SND_MONA
CONFIG_SND_MIA
CONFIG_SND_ECHO3G
CONFIG_SND_INDIGO
CONFIG_SND_INDIGOIO
CONFIG_SND_INDIGODJ
CONFIG_SND_INDIGOIOX
CONFIG_SND_INDIGODJX
CONFIG_SND_BCM63XX_I2S_WHISTLER
CONFIG_SND_SOC_SOF
CONFIG_SND_SOC_SPRD
CONFIG_SND_SOC_STM32_SAI
CONFIG_SND_SOC_STM32_I2S
CONFIG_SND_SOC_STM32_SPDIFRX
CONFIG_SND_SOC_STM32_DFSDM
CONFIG_SND_SOC_TEGRA
CONFIG_SND_SOC_CROS_EC_CODEC
CONFIG_SND_SOC_RT5514_SPI
CONFIG_SND_USB_UA101
CONFIG_USB_F_PHONET
CONFIG_USB_F_TCM
CONFIG_SPI_LOOPBACK_TEST
CONFIG_W1
CONFIG_RDS
CONFIG_TIPC
CONFIG_TCP_SIGPOOL
CONFIG_OPENVSWITCH
CONFIG_NIU
CONFIG_QED_SRIOV
CONFIG_SFC
CONFIG_SFC_FALCON
CONFIG_SFC_SIENA
CONFIG_TSNEP
CONFIG_LIBERTAS
CONFIG_LOOPBACK_TARGET
CONFIG_SUNRPC_XPRT_RDMA
CONFIG_INFINIBAND_HNS
CONFIG_INFINIBAND_IPOIB
CONFIG_INFINIBAND_EFA
CONFIG_INFINIBAND_MTHCA
CONFIG_MLX4_CORE
CONFIG_MLX4_INFINIBAND
CONFIG_MLX5_CORE
CONFIG_MLX5_INFINIBAND
CONFIG_MLX5_VDPA_NET
CONFIG_MLX5_VFIO_PCI
CONFIG_ISCSI_TCP
CONFIG_SCSI_CXGB3_ISCSI
CONFIG_SCSI_CXGB4_ISCSI
CONFIG_SCSI_DC395x
CONFIG_SCSI_DMX3191D
CONFIG_SCSI_FDOMAIN
CONFIG_SCSI_MVUMI
CONFIG_SCSI_STEX
CONFIG_SCSI_SYM53C8XX_2
CONFIG_CDROM_PKTCDVD
CONFIG_AFS_FS
CONFIG_BCACHE
CONFIG_BCACHEFS_FS
CONFIG_CEPH_FS
CONFIG_DLM
CONFIG_BLK_DEV_NULL_BLK
CONFIG_BLK_DEV_DRBD
CONFIG_BLK_DEV_RBD
CONFIG_OCFS2_FS
CONFIG_CRAMFS
CONFIG_EROFS_FS
CONFIG_ECRYPT_FS
CONFIG_F2FS_FS
CONFIG_ZISOFS
CONFIG_NFS_V3_ACL
# would be nice to have...
CONFIG_NFSD_V4
CONFIG_SUNRPC_BACKCHANNEL   # required by CONFIG_NFS_V4_1
CONFIG_MMC
CONFIG_NVME_CORE
CONFIG_NVMEM                # required by CONFIG_USB4
CONFIG_USB_UAS
CONFIG_BLK_DEV_DM
# ...but this is kind of really necessary
CONFIG_BTRFS_FS

After disabling all the above and exporting ptg_page_shift, the tumbleweed kernel builds. TBH I expected more broken things. Great success! ;-)

I'll see if I can do something about btrfs. Then I can try to boot the kernel...

Petr T

#153 lore raw
Quoted 38 lines · On Fri, 18 Oct 2024 14:56:00 +0200 Petr Tesarik <[email protected]> wrote:
Quoted 28 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

Quoted 6 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

Quoted 8 lines

You're right, performance testing is my goal.

Heh, the openSUSE master config is cranked up to max. ;-) That would be a lot of work, and we don't need all those options for running our test suite. Let me disable the conflicting options instead.
[...]
I'll see if I can do something about btrfs. Then I can try to boot the kernel...

FWIW the kernel builds and _boots_ after applying this patch:

 fs/btrfs/compression.h  |    2 +-
 fs/btrfs/defrag.c       |    2 +-
 fs/btrfs/extent_io.h    |    2 +-
 fs/btrfs/scrub.c        |    2 +-
 include/linux/raid/pq.h |    4 ++--
 lib/raid6/algos.c       |    2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)
fs/btrfs/compression.h +1 -1
@@ -33,7 +33,7 @@ struct btrfs_bio;
 /* Maximum length of compressed data stored on disk */
 #define BTRFS_MAX_COMPRESSED		(SZ_128K)
 #define BTRFS_MAX_COMPRESSED_PAGES	(BTRFS_MAX_COMPRESSED / PAGE_SIZE)
-static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
+static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE_MAX) == 0);
 
 /* Maximum size of data before compression */
 #define BTRFS_MAX_UNCOMPRESSED		(SZ_128K)
fs/btrfs/defrag.c +1 -1
@@ -1144,7 +1144,7 @@ next:
 }
 
 #define CLUSTER_SIZE	(SZ_256K)
-static_assert(PAGE_ALIGNED(CLUSTER_SIZE));
+static_assert(IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE_MAX));
 
 /*
  * Defrag one contiguous target range.
fs/btrfs/extent_io.h +1 -1
@@ -89,7 +89,7 @@ enum {
 int __init extent_buffer_init_cachep(void);
 void __cold extent_buffer_free_cachep(void);
 
-#define INLINE_EXTENT_BUFFER_PAGES     (BTRFS_MAX_METADATA_BLOCKSIZE / PAGE_SIZE)
+#define INLINE_EXTENT_BUFFER_PAGES     (BTRFS_MAX_METADATA_BLOCKSIZE / PAGE_SIZE_MIN)
 struct extent_buffer {
 	u64 start;
 	u32 len;
fs/btrfs/scrub.c +1 -1
@@ -100,7 +100,7 @@ enum scrub_stripe_flags {
 	SCRUB_STRIPE_FLAG_NO_REPORT,
 };
 
-#define SCRUB_STRIPE_PAGES		(BTRFS_STRIPE_LEN / PAGE_SIZE)
+#define SCRUB_STRIPE_PAGES		(BTRFS_STRIPE_LEN / PAGE_SIZE_MIN)
 
 /*
  * Represent one contiguous range with a length of BTRFS_STRIPE_LEN.
include/linux/raid/pq.h +2 -2
@@ -12,7 +12,7 @@
 
 #include <linux/blkdev.h>
 
-extern const char raid6_empty_zero_page[PAGE_SIZE];
+extern const char raid6_empty_zero_page[PAGE_SIZE_MAX];
 
 #else /* ! __KERNEL__ */
 /* Used for testing in user space */
@@ -39,7 +39,7 @@ typedef uint64_t u64;
 #ifndef PAGE_SHIFT
 # define PAGE_SHIFT 12
 #endif
-extern const char raid6_empty_zero_page[PAGE_SIZE];
+extern const char raid6_empty_zero_page[PAGE_SIZE_MAX];
 
 #define __init
 #define __exit
lib/raid6/algos.c +1 -1
@@ -19,7 +19,7 @@
 #include <linux/module.h>
 #include <linux/gfp.h>
 /* In .bss so it's zeroed */
-const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
+const char raid6_empty_zero_page[PAGE_SIZE_MAX] __attribute__((aligned(256)));
 EXPORT_SYMBOL(raid6_empty_zero_page);
 #endif
 

Petr T

author reviewer
#154 lore raw
Quoted 43 lines · Petr Tesarik
Quoted 40 lines · On Fri, 18 Oct 2024 14:56:00 +0200 Petr Tesarik <[email protected]> wrote:
Quoted 28 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

Quoted 6 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

You're right, performance testing is my goal.

Quoted 8 lines
Quoted 6 lines

Heh, the openSUSE master config is cranked up to max. ;-) That would be a lot of work, and we don't need all those options for running our test suite. Let me disable the conflicting options instead.
[...]
I'll see if I can do something about btrfs. Then I can try to boot the kernel...

FWIW the kernel builds and _boots_ after applying this patch:

Amazing - thanks for doing this!

Quoted 34 lines
 fs/btrfs/compression.h  |    2 +-
 fs/btrfs/defrag.c       |    2 +-
 fs/btrfs/extent_io.h    |    2 +-
 fs/btrfs/scrub.c        |    2 +-
 include/linux/raid/pq.h |    4 ++--
 lib/raid6/algos.c       |    2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)
fs/btrfs/compression.h +1 -1
@@ -33,7 +33,7 @@ struct btrfs_bio;
 /* Maximum length of compressed data stored on disk */
 #define BTRFS_MAX_COMPRESSED		(SZ_128K)
 #define BTRFS_MAX_COMPRESSED_PAGES	(BTRFS_MAX_COMPRESSED / PAGE_SIZE)
-static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
+static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE_MAX) == 0);
 
 /* Maximum size of data before compression */
 #define BTRFS_MAX_UNCOMPRESSED		(SZ_128K)
fs/btrfs/defrag.c +1 -1
@@ -1144,7 +1144,7 @@ next:
 }
 
 #define CLUSTER_SIZE	(SZ_256K)
-static_assert(PAGE_ALIGNED(CLUSTER_SIZE));
+static_assert(IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE_MAX));
 
 /*
  * Defrag one contiguous target range.

fs/btrfs/extent_io.h enum { · line 92

Quoted 5 lines
fs/btrfs/extent_io.h +1 -1
@@ -89,7 +89,7 @@ enum {
 int __init extent_buffer_init_cachep(void);
 void __cold extent_buffer_free_cachep(void);
 
-#define INLINE_EXTENT_BUFFER_PAGES     (BTRFS_MAX_METADATA_BLOCKSIZE / PAGE_SIZE)
+#define INLINE_EXTENT_BUFFER_PAGES     (BTRFS_MAX_METADATA_BLOCKSIZE / PAGE_SIZE_MIN)

While this works, I'm not sure if you would want to have 2 separate macros; 1 for worst-case static allocation, and 1 for dynamic allocation and iterating. I could imagine if you allocate PAGE_SIZE_MAX pages into the worst case number of slots that would increase memory. I'm not familiar with the code so don't know if this is a problem in practice. Certainly what you have done is much simpler if acceptable.

fs/btrfs/scrub.c enum scrub_stripe_flags { · line 103

Quoted 11 lines
fs/btrfs/extent_io.h +0 -0
 struct extent_buffer {
 	u64 start;
 	u32 len;
fs/btrfs/scrub.c +1 -1
@@ -100,7 +100,7 @@ enum scrub_stripe_flags {
 	SCRUB_STRIPE_FLAG_NO_REPORT,
 };
 
-#define SCRUB_STRIPE_PAGES		(BTRFS_STRIPE_LEN / PAGE_SIZE)
+#define SCRUB_STRIPE_PAGES		(BTRFS_STRIPE_LEN / PAGE_SIZE_MIN)

Same comment.

Thanks,
Ryan

Quoted 36 lines
fs/btrfs/scrub.c +0 -0
 /*
  * Represent one contiguous range with a length of BTRFS_STRIPE_LEN.
include/linux/raid/pq.h +2 -2
@@ -12,7 +12,7 @@
 
 #include <linux/blkdev.h>
 
-extern const char raid6_empty_zero_page[PAGE_SIZE];
+extern const char raid6_empty_zero_page[PAGE_SIZE_MAX];
 
 #else /* ! __KERNEL__ */
 /* Used for testing in user space */
@@ -39,7 +39,7 @@ typedef uint64_t u64;
 #ifndef PAGE_SHIFT
 # define PAGE_SHIFT 12
 #endif
-extern const char raid6_empty_zero_page[PAGE_SIZE];
+extern const char raid6_empty_zero_page[PAGE_SIZE_MAX];
 
 #define __init
 #define __exit
lib/raid6/algos.c +1 -1
@@ -19,7 +19,7 @@
 #include <linux/module.h>
 #include <linux/gfp.h>
 /* In .bss so it's zeroed */
-const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
+const char raid6_empty_zero_page[PAGE_SIZE_MAX] __attribute__((aligned(256)));
 EXPORT_SYMBOL(raid6_empty_zero_page);
 #endif
 

Petr T

#155 lore raw
In reply to Ryan Roberts
Quoted 20 lines · Ryan Roberts
Quoted 19 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

   u8 reserved2[PAGE_SIZE - 68];
      ^~~~~~~~~
Quoted 5 lines
Quoted 1 line

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

It would be nice if you could provide the defconfig you are using; I also ran into build issues when using the arch/arm64/configs/defconfig.

Thank you,
Thomas

Quoted 13 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

Thanks,
Ryan

Quoted 1 line

Petr T

author reviewer
#156 lore raw
Quoted 23 lines · Thomas Tai
Quoted 21 lines · Ryan Roberts
Quoted 20 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

   - patch 1:       Add macros required for converting non-arch code to support boot-time page size selection
   - patches 2-36:  Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

   CC      arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope
   u8 reserved2[PAGE_SIZE - 68];
      ^~~~~~~~~

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

It would be nice if you could provide the defconfig you are using; I also ran into build issues when using the arch/arm64/configs/defconfig.

git clean -xdfq
make defconfig

# Set CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --disable CONFIG_ARM64_4K_PAGES ./scripts/config --disable CONFIG_ARM64_16K_PAGES ./scripts/config --disable CONFIG_ARM64_64K_PAGES ./scripts/config --disable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --enable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE

# Set ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_36
./scripts/config --disable ARM64_VA_BITS_39
./scripts/config --disable ARM64_VA_BITS_42
./scripts/config --disable ARM64_VA_BITS_47
./scripts/config --disable ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_52
./scripts/config --enable ARM64_VA_BITS_48

# Optional: filesystems known to compile with boot-time page size ./scripts/config --enable CONFIG_SQUASHFS_LZ4 ./scripts/config --enable CONFIG_SQUASHFS_LZO ./scripts/config --enable CONFIG_SQUASHFS_XZ ./scripts/config --enable CONFIG_SQUASHFS_ZSTD ./scripts/config --enable CONFIG_XFS_FS

# Optional: trace stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_FTRACE ./scripts/config --enable CONFIG_FUNCTION_TRACER ./scripts/config --enable CONFIG_KPROBES ./scripts/config --enable CONFIG_HIST_TRIGGERS ./scripts/config --enable CONFIG_FTRACE_SYSCALLS

# Optional: misc mm stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_PTDUMP_DEBUGFS ./scripts/config --enable CONFIG_READ_ONLY_THP_FOR_FS ./scripts/config --enable CONFIG_USERFAULTFD

# Optional: mm debug stuff known compile with boot-time page size ./scripts/config --enable CONFIG_DEBUG_VM ./scripts/config --enable CONFIG_DEBUG_VM_MAPLE_TREE ./scripts/config --enable CONFIG_DEBUG_VM_RB ./scripts/config --enable CONFIG_DEBUG_VM_PGFLAGS ./scripts/config --enable CONFIG_DEBUG_VM_PGTABLE ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK_ENFORCED

make olddefconfig
make -s -jnproc Image

So I'm explicitly only building and booting the kernel image, not the modules. The kernel image contains all the drivers needed to get a VM up and running under QEMU/KVM.

Thanks,
Ryan

Quoted 17 lines


Thank you,
Thomas

Quoted 13 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

Thanks,
Ryan

Quoted 1 line

Petr T

#157 lore raw
Quoted 80 lines · On Thu, 24 Oct 2024 11:48:55 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 23 lines · Thomas Tai
Quoted 21 lines · Ryan Roberts
Quoted 20 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

   - patch 1:       Add macros required for converting non-arch code to support boot-time page size selection
   - patches 2-36:  Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

   CC      arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope
   u8 reserved2[PAGE_SIZE - 68];
      ^~~~~~~~~

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

It would be nice if you could provide the defconfig you are using; I also ran into build issues when using the arch/arm64/configs/defconfig.

git clean -xdfq
make defconfig

# Set CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --disable CONFIG_ARM64_4K_PAGES ./scripts/config --disable CONFIG_ARM64_16K_PAGES ./scripts/config --disable CONFIG_ARM64_64K_PAGES ./scripts/config --disable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --enable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE

# Set ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_36
./scripts/config --disable ARM64_VA_BITS_39
./scripts/config --disable ARM64_VA_BITS_42
./scripts/config --disable ARM64_VA_BITS_47
./scripts/config --disable ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_52
./scripts/config --enable ARM64_VA_BITS_48

# Optional: filesystems known to compile with boot-time page size ./scripts/config --enable CONFIG_SQUASHFS_LZ4 ./scripts/config --enable CONFIG_SQUASHFS_LZO ./scripts/config --enable CONFIG_SQUASHFS_XZ ./scripts/config --enable CONFIG_SQUASHFS_ZSTD ./scripts/config --enable CONFIG_XFS_FS

# Optional: trace stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_FTRACE ./scripts/config --enable CONFIG_FUNCTION_TRACER ./scripts/config --enable CONFIG_KPROBES ./scripts/config --enable CONFIG_HIST_TRIGGERS ./scripts/config --enable CONFIG_FTRACE_SYSCALLS

# Optional: misc mm stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_PTDUMP_DEBUGFS ./scripts/config --enable CONFIG_READ_ONLY_THP_FOR_FS ./scripts/config --enable CONFIG_USERFAULTFD

# Optional: mm debug stuff known compile with boot-time page size ./scripts/config --enable CONFIG_DEBUG_VM ./scripts/config --enable CONFIG_DEBUG_VM_MAPLE_TREE ./scripts/config --enable CONFIG_DEBUG_VM_RB ./scripts/config --enable CONFIG_DEBUG_VM_PGFLAGS ./scripts/config --enable CONFIG_DEBUG_VM_PGTABLE ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK_ENFORCED

Quoted 6 lines

make olddefconfig
make -s -jnproc Image

So I'm explicitly only building and booting the kernel image, not the modules. The kernel image contains all the drivers needed to get a VM up and running under QEMU/KVM.

FWIW with the attached patch I was also able to boot the kernel on Ampere Altra bare metal and using modules.

Petr T

arch/arm64/mm/pgtable-geometry.c +6 -0
index ba50637f1e9d..4eb074b99654 100644
@@ -15,8 +15,14 @@
  */
 
 int ptg_page_shift __read_mostly;
+EXPORT_SYMBOL_GPL(ptg_page_shift);
+
 int ptg_pmd_shift __read_mostly;
+EXPORT_SYMBOL_GPL(ptg_pmd_shift);
+
 int ptg_pud_shift __read_mostly;
+EXPORT_SYMBOL_GPL(ptg_pud_shift);
+
 int ptg_p4d_shift __read_mostly;
 int ptg_pgdir_shift __read_mostly;
 int ptg_cont_pte_shift __read_mostly;
author reviewer
#158 lore raw
Quoted 86 lines · Petr Tesarik
Quoted 83 lines · On Thu, 24 Oct 2024 11:48:55 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 23 lines · Thomas Tai
Quoted 21 lines · Ryan Roberts
Quoted 20 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

   - patch 1:       Add macros required for converting non-arch code to support boot-time page size selection
   - patches 2-36:  Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

   CC      arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope
   u8 reserved2[PAGE_SIZE - 68];
      ^~~~~~~~~

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

It would be nice if you could provide the defconfig you are using; I also ran into build issues when using the arch/arm64/configs/defconfig.

git clean -xdfq
make defconfig

# Set CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --disable CONFIG_ARM64_4K_PAGES ./scripts/config --disable CONFIG_ARM64_16K_PAGES ./scripts/config --disable CONFIG_ARM64_64K_PAGES ./scripts/config --disable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --enable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE

# Set ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_36
./scripts/config --disable ARM64_VA_BITS_39
./scripts/config --disable ARM64_VA_BITS_42
./scripts/config --disable ARM64_VA_BITS_47
./scripts/config --disable ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_52
./scripts/config --enable ARM64_VA_BITS_48

# Optional: filesystems known to compile with boot-time page size ./scripts/config --enable CONFIG_SQUASHFS_LZ4 ./scripts/config --enable CONFIG_SQUASHFS_LZO ./scripts/config --enable CONFIG_SQUASHFS_XZ ./scripts/config --enable CONFIG_SQUASHFS_ZSTD ./scripts/config --enable CONFIG_XFS_FS

# Optional: trace stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_FTRACE ./scripts/config --enable CONFIG_FUNCTION_TRACER ./scripts/config --enable CONFIG_KPROBES ./scripts/config --enable CONFIG_HIST_TRIGGERS ./scripts/config --enable CONFIG_FTRACE_SYSCALLS

# Optional: misc mm stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_PTDUMP_DEBUGFS ./scripts/config --enable CONFIG_READ_ONLY_THP_FOR_FS ./scripts/config --enable CONFIG_USERFAULTFD

# Optional: mm debug stuff known compile with boot-time page size ./scripts/config --enable CONFIG_DEBUG_VM ./scripts/config --enable CONFIG_DEBUG_VM_MAPLE_TREE ./scripts/config --enable CONFIG_DEBUG_VM_RB ./scripts/config --enable CONFIG_DEBUG_VM_PGFLAGS ./scripts/config --enable CONFIG_DEBUG_VM_PGTABLE ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK_ENFORCED

make olddefconfig
make -s -jnproc Image

Quoted 6 lines
Quoted 3 lines

So I'm explicitly only building and booting the kernel image, not the modules. The kernel image contains all the drivers needed to get a VM up and running under QEMU/KVM.

FWIW with the attached patch I was also able to boot the kernel on Ampere Altra bare metal and using modules.

Nice!

Thanks for the below. That was already reported and I have a fix in my branch at [1]. That also includes the btrfs patch you sent and the hyper-v patches, as well as other fixups from review.

[1]
https://gitlab.arm.com/linux-arm/linux-rr/-/tree/features/boot-time-page-size-v2-wip

Thanks,
Ryan

Quoted 22 lines

Petr T

arch/arm64/mm/pgtable-geometry.c +6 -0
index ba50637f1e9d..4eb074b99654 100644
@@ -15,8 +15,14 @@
  */
 
 int ptg_page_shift __read_mostly;
+EXPORT_SYMBOL_GPL(ptg_page_shift);
+
 int ptg_pmd_shift __read_mostly;
+EXPORT_SYMBOL_GPL(ptg_pmd_shift);
+
 int ptg_pud_shift __read_mostly;
+EXPORT_SYMBOL_GPL(ptg_pud_shift);
+
 int ptg_p4d_shift __read_mostly;
 int ptg_pgdir_shift __read_mostly;
 int ptg_cont_pte_shift __read_mostly;
#159 lore raw
In reply to Ryan Roberts
Quoted 86 lines · Ryan Roberts

External email: Use caution opening links or attachments

Quoted 23 lines · Thomas Tai
Quoted 21 lines · Ryan Roberts
Quoted 20 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

    u8 reserved2[PAGE_SIZE - 68];
       ^~~~~~~~~
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

It would be nice if you could provide the defconfig you are using; I also ran into build issues when using the arch/arm64/configs/defconfig.

git clean -xdfq
make defconfig

# Set CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --disable CONFIG_ARM64_4K_PAGES ./scripts/config --disable CONFIG_ARM64_16K_PAGES ./scripts/config --disable CONFIG_ARM64_64K_PAGES ./scripts/config --disable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE ./scripts/config --enable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE

# Set ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_36
./scripts/config --disable ARM64_VA_BITS_39
./scripts/config --disable ARM64_VA_BITS_42
./scripts/config --disable ARM64_VA_BITS_47
./scripts/config --disable ARM64_VA_BITS_48
./scripts/config --disable ARM64_VA_BITS_52
./scripts/config --enable ARM64_VA_BITS_48

# Optional: filesystems known to compile with boot-time page size ./scripts/config --enable CONFIG_SQUASHFS_LZ4 ./scripts/config --enable CONFIG_SQUASHFS_LZO ./scripts/config --enable CONFIG_SQUASHFS_XZ ./scripts/config --enable CONFIG_SQUASHFS_ZSTD ./scripts/config --enable CONFIG_XFS_FS

# Optional: trace stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_FTRACE ./scripts/config --enable CONFIG_FUNCTION_TRACER ./scripts/config --enable CONFIG_KPROBES ./scripts/config --enable CONFIG_HIST_TRIGGERS ./scripts/config --enable CONFIG_FTRACE_SYSCALLS

# Optional: misc mm stuff known to compile with boot-time page size ./scripts/config --enable CONFIG_PTDUMP_DEBUGFS ./scripts/config --enable CONFIG_READ_ONLY_THP_FOR_FS ./scripts/config --enable CONFIG_USERFAULTFD

# Optional: mm debug stuff known compile with boot-time page size ./scripts/config --enable CONFIG_DEBUG_VM ./scripts/config --enable CONFIG_DEBUG_VM_MAPLE_TREE ./scripts/config --enable CONFIG_DEBUG_VM_RB ./scripts/config --enable CONFIG_DEBUG_VM_PGFLAGS ./scripts/config --enable CONFIG_DEBUG_VM_PGTABLE ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK ./scripts/config --enable CONFIG_PAGE_TABLE_CHECK_ENFORCED

make olddefconfig
make -s -jnproc Image

Quoted 7 lines

So I'm explicitly only building and booting the kernel image, not the modules. The kernel image contains all the drivers needed to get a VM up and running under QEMU/KVM.

Thanks,
Ryan

Thank you for this patch set.
I was able to boot with minimal configs on Tegra234 board. Will enable more configs and discuss.

Thank you,
Sumit Gupta

Quoted 19 lines
Quoted 17 lines

Thank you,
Thomas

Quoted 13 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

Thanks,
Ryan

Quoted 1 line

Petr T

#160 lore raw
In reply to Ryan Roberts

Hi Ryan,

Quoted 2 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:

[...]
I understand that Suse might be able to help with wider performance testing

Sorry for the delay (vacation, other tasks). Anyway, let me share some results with you.

First, I have looked only at 4k pages (constant v. selected at boot time) so far.

Second, the impact of the patch series is much smaller than I expected. Most macro-benchmarks (dbench, io-bench) did not see any significant slowdown. There appears to be a performance hit of approx. 1-2%, but that's within noise, and I can't dedicate my time to running extensive tests to find the distribution peak and compare. In short, I suspect a slight performance hit, but I cannot quantify it.

Third, a few micro-benchmarks saw a significant regression.

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

The dup() syscall was up to 5% slower (depends on underlying filesystem type).

VMA unmap was slower for some sizes, but the pattern seemed random, sometimes giving even better performance with variable page size, so this micro-benchmark may be too unstable to draw any conclusions.

Stay tuned
Petr T

author reviewer
#161 lore raw

Hi Petr,

Quoted 10 lines · Petr Tesarik

Hi Ryan,

Quoted 2 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:

[...]
I understand that Suse might be able to help with wider performance testing

Sorry for the delay (vacation, other tasks). Anyway, let me share some results with you.

Not at all; thanks for coming back with these results!

Quoted 11 lines

First, I have looked only at 4k pages (constant v. selected at boot time) so far.

Second, the impact of the patch series is much smaller than I expected. Most macro-benchmarks (dbench, io-bench) did not see any significant slowdown. There appears to be a performance hit of approx. 1-2%, but that's within noise, and I can't dedicate my time to running extensive tests to find the distribution peak and compare. In short, I suspect a slight performance hit, but I cannot quantify it.

Quoted 6 lines

Third, a few micro-benchmarks saw a significant regression.

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

OK, ouch. I think there are some things we can try to optimize the implementation further. But I'll wait for your analysis before digging myself.

You probably also saw the conversation with Catalin about the cost vs benefit of this series. Performance regressions will all need to be considered in the cost column, of course. So understanding the root cause and trying to reduce the regression as much as possible will increase chances of getting it accepted upstream.

Thanks,
Ryan

Quoted 10 lines

The dup() syscall was up to 5% slower (depends on underlying filesystem type).

VMA unmap was slower for some sizes, but the pattern seemed random, sometimes giving even better performance with variable page size, so this micro-benchmark may be too unstable to draw any conclusions.

Stay tuned
Petr T

#162 lore raw
Quoted 10 lines · On Mon, 11 Nov 2024 12:25:35 +0000 Ryan Roberts <[email protected]> wrote:

Hi Petr,

Quoted 4 lines · Petr Tesarik

Hi Ryan,

On Thu, 17 Oct 2024 13:32:43 +0100
Ryan Roberts <[email protected]> wrote:

[...]

Quoted 2 lines

Third, a few micro-benchmarks saw a significant regression.

Quoted 7 lines
Quoted 4 lines

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

OK, ouch. I think there are some things we can try to optimize the implementation further. But I'll wait for your analysis before digging myself.

This turned out to be a false positive. The way this microbenchmark was invoked did not get enough samples, so it was mostly dependent on whether caches were hot or cold, and the timing on this specific system with the specific sequence of bencnmarks in the suite happens to favour my baseline kernel.

After increasing the batch count, I'm getting pretty much the same performance for 6.11 vanilla and patched kernels:

prc thr usecs/call samples errors cnt/samp

getenv (baseline)         1   1      0.14975           99        0   100000 
getenv (patched)          1   1      0.14981           92        0   100000 
Quoted 5 lines

You probably also saw the conversation with Catalin about the cost vs benefit of this series. Performance regressions will all need to be considered in the cost column, of course. So understanding the root cause and trying to reduce the regression as much as possible will increase chances of getting it accepted upstream.

Yes. Now that the biggest number is off the table, I'm going to:

- look into the dup() slowdown
- verify whether VMA split/merge operations are indeed slower

Petr T

author reviewer
#163 lore raw
Quoted 27 lines · Petr Tesarik
Quoted 10 lines · On Mon, 11 Nov 2024 12:25:35 +0000 Ryan Roberts <[email protected]> wrote:

Hi Petr,

Quoted 4 lines · Petr Tesarik

Hi Ryan,

On Thu, 17 Oct 2024 13:32:43 +0100
Ryan Roberts <[email protected]> wrote:

[...]

Quoted 2 lines

Third, a few micro-benchmarks saw a significant regression.

Quoted 7 lines
Quoted 4 lines

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

OK, ouch. I think there are some things we can try to optimize the implementation further. But I'll wait for your analysis before digging myself.

This turned out to be a false positive. The way this microbenchmark was invoked did not get enough samples, so it was mostly dependent on whether caches were hot or cold, and the timing on this specific system with the specific sequence of bencnmarks in the suite happens to favour my baseline kernel.

Quoted 6 lines

After increasing the batch count, I'm getting pretty much the same performance for 6.11 vanilla and patched kernels:

prc thr usecs/call samples errors cnt/samp

getenv (baseline)         1   1      0.14975           99        0   100000 
getenv (patched)          1   1      0.14981           92        0   100000 

Oh that's good news! Does this account for all 3 of the above tests (getenv, getenvT2 and system())?

Quoted 13 lines
Quoted 6 lines

You probably also saw the conversation with Catalin about the cost vs benefit of this series. Performance regressions will all need to be considered in the cost column, of course. So understanding the root cause and trying to reduce the regression as much as possible will increase chances of getting it accepted upstream.

Yes. Now that the biggest number is off the table, I'm going to:

- look into the dup() slowdown
- verify whether VMA split/merge operations are indeed slower

Petr T

#164 lore raw
Quoted 31 lines · On Tue, 12 Nov 2024 10:19:34 +0000 Ryan Roberts <[email protected]> wrote:
Quoted 30 lines · Petr Tesarik
Quoted 10 lines · On Mon, 11 Nov 2024 12:25:35 +0000 Ryan Roberts <[email protected]> wrote:

Hi Petr,

Quoted 4 lines · Petr Tesarik

Hi Ryan,

On Thu, 17 Oct 2024 13:32:43 +0100
Ryan Roberts <[email protected]> wrote:

[...]

Quoted 2 lines

Third, a few micro-benchmarks saw a significant regression.

Quoted 7 lines
Quoted 4 lines

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

OK, ouch. I think there are some things we can try to optimize the implementation further. But I'll wait for your analysis before digging myself.

This turned out to be a false positive. The way this microbenchmark was invoked did not get enough samples, so it was mostly dependent on whether caches were hot or cold, and the timing on this specific system with the specific sequence of bencnmarks in the suite happens to favour my baseline kernel.

After increasing the batch count, I'm getting pretty much the same performance for 6.11 vanilla and patched kernels:

Quoted 6 lines
Quoted 3 lines

prc thr usecs/call samples errors cnt/samp

getenv (baseline)         1   1      0.14975           99        0   100000 
getenv (patched)          1   1      0.14981           92        0   100000   

Oh that's good news! Does this account for all 3 of the above tests (getenv, getenvT2 and system())?

It does for getenvT2 (a variant of the test with 2 threads), but not for system. Thanks for asking, I forgot about that one.

I'm getting substantial difference there (+29% on average over 100 runs):

                        prc thr   usecs/call      samples   errors cnt/samp  command
system (baseline)         1   1   6937.18016          102        0      100     A=$$
system (patched)          1   1   8959.48032          102        0      100     A=$$

So, yeah, this should in fact be my priority #1.

The "system" benchmark measures the duration of system("A=$$"), which involves starting the system shell (in my case bash-4.4.23), so this is not really a microbenchmark. I hope perf can help match the difference to a kernel API.

Petr T

#165 lore raw
Quoted 44 lines · On Tue, 12 Nov 2024 11:50:39 +0100 Petr Tesarik <[email protected]> wrote:
Quoted 31 lines · On Tue, 12 Nov 2024 10:19:34 +0000 Ryan Roberts <[email protected]> wrote:
Quoted 30 lines · Petr Tesarik
Quoted 10 lines · On Mon, 11 Nov 2024 12:25:35 +0000 Ryan Roberts <[email protected]> wrote:

Hi Petr,

Quoted 4 lines · Petr Tesarik

Hi Ryan,

On Thu, 17 Oct 2024 13:32:43 +0100
Ryan Roberts <[email protected]> wrote:

[...]

Quoted 2 lines

Third, a few micro-benchmarks saw a significant regression.

Quoted 7 lines
Quoted 4 lines

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

OK, ouch. I think there are some things we can try to optimize the implementation further. But I'll wait for your analysis before digging myself.

This turned out to be a false positive. The way this microbenchmark was invoked did not get enough samples, so it was mostly dependent on whether caches were hot or cold, and the timing on this specific system with the specific sequence of bencnmarks in the suite happens to favour my baseline kernel.

After increasing the batch count, I'm getting pretty much the same performance for 6.11 vanilla and patched kernels:

Quoted 6 lines
Quoted 3 lines

prc thr usecs/call samples errors cnt/samp

getenv (baseline)         1   1      0.14975           99        0   100000 
getenv (patched)          1   1      0.14981           92        0   100000     

Oh that's good news! Does this account for all 3 of the above tests (getenv, getenvT2 and system())?

It does for getenvT2 (a variant of the test with 2 threads), but not for system. Thanks for asking, I forgot about that one.

Quoted 7 lines

I'm getting substantial difference there (+29% on average over 100 runs):

                        prc thr   usecs/call      samples   errors cnt/samp  command
system (baseline)         1   1   6937.18016          102        0      100     A=$$
system (patched)          1   1   8959.48032          102        0      100     A=$$

So, yeah, this should in fact be my priority #1.

Further testing reveals the workload is bimodal, that is to say the distribution of results has two peaks. The first peak around 3.2 ms covers 30% runs, the second peak around 15.7 ms covers 11%. Two per cent are faster than the fast peak, 5% are slower than slow peak, the rest is distributed almost evenly between them.

100 samples were not sufficient to see this distribution, and it was mere bad luck that only the patched kernel originally reported bad results. I can now see bad results even with the unpatched kernel.

In short, I don't think there is a difference in system() performance.

I will still have a look at dup() and VMA performance, but so far it all looks good to me. Good job! ;-)

I will also try running a more complete set of benchmarks during next week. That's SUSE Hack Week, and I want to make a PoC for the MM changes I proposed at LPC24, so I won't need this Ampere system for interactive use.

Petr T

author reviewer
#166 lore raw
Quoted 53 lines · Petr Tesarik
Quoted 50 lines · On Tue, 12 Nov 2024 11:50:39 +0100 Petr Tesarik <[email protected]> wrote:
Quoted 31 lines · On Tue, 12 Nov 2024 10:19:34 +0000 Ryan Roberts <[email protected]> wrote:
Quoted 30 lines · Petr Tesarik
Quoted 10 lines · On Mon, 11 Nov 2024 12:25:35 +0000 Ryan Roberts <[email protected]> wrote:

Hi Petr,

Quoted 4 lines · Petr Tesarik

Hi Ryan,

On Thu, 17 Oct 2024 13:32:43 +0100
Ryan Roberts <[email protected]> wrote:

[...]

Quoted 2 lines

Third, a few micro-benchmarks saw a significant regression.

Quoted 7 lines
Quoted 4 lines

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

OK, ouch. I think there are some things we can try to optimize the implementation further. But I'll wait for your analysis before digging myself.

This turned out to be a false positive. The way this microbenchmark was invoked did not get enough samples, so it was mostly dependent on whether caches were hot or cold, and the timing on this specific system with the specific sequence of bencnmarks in the suite happens to favour my baseline kernel.

After increasing the batch count, I'm getting pretty much the same performance for 6.11 vanilla and patched kernels:

Quoted 6 lines
Quoted 3 lines

prc thr usecs/call samples errors cnt/samp

getenv (baseline)         1   1      0.14975           99        0   100000 
getenv (patched)          1   1      0.14981           92        0   100000     

Oh that's good news! Does this account for all 3 of the above tests (getenv, getenvT2 and system())?

It does for getenvT2 (a variant of the test with 2 threads), but not for system. Thanks for asking, I forgot about that one.

I'm getting substantial difference there (+29% on average over 100 runs):

                        prc thr   usecs/call      samples   errors cnt/samp  command
system (baseline)         1   1   6937.18016          102        0      100     A=$$
system (patched)          1   1   8959.48032          102        0      100     A=$$
Quoted 7 lines
Quoted 1 line

So, yeah, this should in fact be my priority #1.

Further testing reveals the workload is bimodal, that is to say the distribution of results has two peaks. The first peak around 3.2 ms covers 30% runs, the second peak around 15.7 ms covers 11%. Two per cent are faster than the fast peak, 5% are slower than slow peak, the rest is distributed almost evenly between them.

FWIW, One source of bimodality I've seen on Ampere systems with 2 NUMA nodes is placement of the kernel image vs placement of the running thread. If they are remote from eachother, you'll see a slowdown. I've hacked this source away in the past by effectively using only a single NUMA node (with the help of 'maxcpus' and 'mem' kernel cmdline options).

Quoted 9 lines

100 samples were not sufficient to see this distribution, and it was mere bad luck that only the patched kernel originally reported bad results. I can now see bad results even with the unpatched kernel.

In short, I don't think there is a difference in system() performance.

I will still have a look at dup() and VMA performance, but so far it all looks good to me. Good job! ;-)

Thanks for digging into all this!

Quoted 7 lines

I will also try running a more complete set of benchmarks during next week. That's SUSE Hack Week, and I want to make a PoC for the MM changes I proposed at LPC24, so I won't need this Ampere system for interactive use.

Petr T

#167 lore raw
Quoted 62 lines · On Wed, 13 Nov 2024 12:56:24 +0000 Ryan Roberts <[email protected]> wrote:
Quoted 53 lines · Petr Tesarik
Quoted 50 lines · On Tue, 12 Nov 2024 11:50:39 +0100 Petr Tesarik <[email protected]> wrote:
Quoted 31 lines · On Tue, 12 Nov 2024 10:19:34 +0000 Ryan Roberts <[email protected]> wrote:
Quoted 30 lines · Petr Tesarik
Quoted 10 lines · On Mon, 11 Nov 2024 12:25:35 +0000 Ryan Roberts <[email protected]> wrote:

Hi Petr,

Quoted 4 lines · Petr Tesarik

Hi Ryan,

On Thu, 17 Oct 2024 13:32:43 +0100
Ryan Roberts <[email protected]> wrote:

[...]

Quoted 2 lines

Third, a few micro-benchmarks saw a significant regression.

Quoted 7 lines
Quoted 4 lines

Most notably, getenv and getenvT2 tests from libMicro were 18% and 20% slower with variable page size. I don't know why, but I'm looking into it. The system() library call was also about 18% slower, but that might be related.

OK, ouch. I think there are some things we can try to optimize the implementation further. But I'll wait for your analysis before digging myself.

This turned out to be a false positive. The way this microbenchmark was invoked did not get enough samples, so it was mostly dependent on whether caches were hot or cold, and the timing on this specific system with the specific sequence of bencnmarks in the suite happens to favour my baseline kernel.

After increasing the batch count, I'm getting pretty much the same performance for 6.11 vanilla and patched kernels:

Quoted 6 lines
Quoted 3 lines

prc thr usecs/call samples errors cnt/samp

getenv (baseline)         1   1      0.14975           99        0   100000 
getenv (patched)          1   1      0.14981           92        0   100000       

Oh that's good news! Does this account for all 3 of the above tests (getenv, getenvT2 and system())?

It does for getenvT2 (a variant of the test with 2 threads), but not for system. Thanks for asking, I forgot about that one.

I'm getting substantial difference there (+29% on average over 100 runs):

                        prc thr   usecs/call      samples   errors cnt/samp  command
system (baseline)         1   1   6937.18016          102        0      100     A=$$
system (patched)          1   1   8959.48032          102        0      100     A=$$
Quoted 7 lines
Quoted 1 line

So, yeah, this should in fact be my priority #1.

Further testing reveals the workload is bimodal, that is to say the distribution of results has two peaks. The first peak around 3.2 ms covers 30% runs, the second peak around 15.7 ms covers 11%. Two per cent are faster than the fast peak, 5% are slower than slow peak, the rest is distributed almost evenly between them.

Quoted 5 lines

FWIW, One source of bimodality I've seen on Ampere systems with 2 NUMA nodes is placement of the kernel image vs placement of the running thread. If they are remote from eachother, you'll see a slowdown. I've hacked this source away in the past by effectively using only a single NUMA node (with the help of 'maxcpus' and 'mem' kernel cmdline options).

This system has only one NUMA node. But your comment leads in the right direction. CPU placement does play a role here.

I can consistently get the fast results if I pin the benchmark process to a single CPU core, or more generally to a CPU set which shares the L2 cache (as found on eMAG). But the scheduler only considers LLC, which (with CONFIG_SCHED_CLUSTER=y) follows the complex affinity of the SLC.

Long story short, without explicit affinity, the scheduler may place a forked child onto a CPU with a cold L2 cache, which harms short-lived processes (like the ones created by this benchmark).

Now it all makes sense and it is totally unrelated to dynamic page size selection. :-)

Petr T

#168 lore raw
In reply to Ryan Roberts

Hi Ryan,

Quoted 28 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

Quoted 6 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

This project was de-prioritised for some time, but I have just returned to it, and one of our test systems uses a Mellanox 5 NIC, which did not build.

If you still have time to work on your patch series, please, can you look into enabling MLX5_CORE_EN?

Oh, and have you rebased the series to 6.12 yet?

Petr T

#169 lore raw

From: Petr Tesarik <[email protected]> Sent: Thursday, December 5, 2024 9:20 AM

Quoted 41 lines

Hi Ryan,

Quoted 28 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

Quoted 6 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

Quoted 8 lines

This project was de-prioritised for some time, but I have just returned to it, and one of our test systems uses a Mellanox 5 NIC, which did not build.

If you still have time to work on your patch series, please, can you look into enabling MLX5_CORE_EN?

Oh, and have you rebased the series to 6.12 yet?

FWIW, here's what I hacked together to compile and run the mlx5 driver in a Hyper-V VM. This was against a 6.11 kernel code base.

Michael

drivers/net/ethernet/mellanox/mlx5/core/pagealloc.c +2 -1
index d894a88fa9f2..d0b381df074c 100644
@@ -66,9 +66,10 @@ struct fw_page {
 
 enum {
 	MLX5_MAX_RECLAIM_TIME_MILI	= 5000,
-	MLX5_NUM_4K_IN_PAGE		= PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
 };
 
+#define MLX5_NUM_4K_IN_PAGE	((int)(PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE))
+
 static u32 get_function(u16 func_id, bool ec_function)
 {
 	return (u32)func_id | (ec_function << 16);
include/linux/mlx5/device.h +4 -2
index ba875a619b97..2d39ba77b591 100644
@@ -255,12 +255,14 @@ enum {
 					  MLX5_NON_FP_BFREGS_PER_UAR,
 	MLX5_MAX_BFREGS			= MLX5_MAX_UARS *
 					  MLX5_NON_FP_BFREGS_PER_UAR,
-	MLX5_UARS_IN_PAGE		= PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
-	MLX5_NON_FP_BFREGS_IN_PAGE	= MLX5_NON_FP_BFREGS_PER_UAR * MLX5_UARS_IN_PAGE,
 	MLX5_MIN_DYN_BFREGS		= 512,
 	MLX5_MAX_DYN_BFREGS		= 1024,
 };
 
+
+#define	MLX5_UARS_IN_PAGE		((int)(PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE))
+#define	MLX5_NON_FP_BFREGS_IN_PAGE	((int)(MLX5_NON_FP_BFREGS_PER_UAR * MLX5_UARS_IN_PAGE))
+
 enum {
 	MLX5_MKEY_MASK_LEN		= 1ull << 0,
 	MLX5_MKEY_MASK_PAGE_SIZE	= 1ull << 1,
#170 lore raw
Quoted 45 lines · On Thu, 5 Dec 2024 18:52:35 +0000 Michael Kelley <[email protected]> wrote:

From: Petr Tesarik <[email protected]> Sent: Thursday, December 5, 2024 9:20 AM

Quoted 44 lines

Hi Ryan,

Quoted 28 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

Quoted 6 lines

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

This project was de-prioritised for some time, but I have just returned to it, and one of our test systems uses a Mellanox 5 NIC, which did not build.

Quoted 8 lines
Quoted 5 lines

If you still have time to work on your patch series, please, can you look into enabling MLX5_CORE_EN?

Oh, and have you rebased the series to 6.12 yet?

FWIW, here's what I hacked together to compile and run the mlx5 driver in a Hyper-V VM. This was against a 6.11 kernel code base.

Wow! Thank you, Michael. I'll give it a try.

Petr T

author reviewer
#171 lore raw
Quoted 41 lines · Petr Tesarik
Quoted 38 lines · On Thu, 5 Dec 2024 18:52:35 +0000 Michael Kelley <[email protected]> wrote:

From: Petr Tesarik <[email protected]> Sent: Thursday, December 5, 2024 9:20 AM

Quoted 37 lines

Hi Ryan,

Quoted 31 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified ‘reserved2’ at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

This project was de-prioritised for some time, but I have just returned to it, and one of our test systems uses a Mellanox 5 NIC, which did not build.

No problem - I appreciate all the time you have spent on it so far!

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

If you still have time to work on your patch series, please, can you look into enabling MLX5_CORE_EN?

I've also had other things that have been taking up my time. I'm planning to get back to this series properly after Christmas and convert all the remaining module code. I'm hoping that Michael's patch will solve your problem for now?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Oh, and have you rebased the series to 6.12 yet?

Afraid the latest I have at the moment is based on v6.12-rc3. It also includes all the changes from the review feedback:

https://gitlab.arm.com/linux-arm/linux-rr/-/tree/features/boot-time-page-size-v2-wip

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

FWIW, here's what I hacked together to compile and run the mlx5 driver in a Hyper-V VM. This was against a 6.11 kernel code base.

Wow! Thank you, Michael. I'll give it a try.

Yes, thanks, Michael - I'll take a look at this and integrate into my tree after Christmas.

Thanks,
Ryan

Quoted 2 lines

Petr T

#172 lore raw

From: Ryan Roberts <[email protected]> Sent: Friday, December 6, 2024 2:26 AM

Quoted 70 lines · Petr Tesarik
Quoted 41 lines
Quoted 38 lines · On Thu, 5 Dec 2024 18:52:35 +0000 Michael Kelley <[email protected]> wrote:

From: Petr Tesarik <[email protected]> Sent: Thursday, December 5, 2024 9:20 AM

Quoted 37 lines

Hi Ryan,

Quoted 31 lines · On Thu, 17 Oct 2024 13:32:43 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Petr Tesarik
Quoted 7 lines · On Mon, 14 Oct 2024 11:55:11 +0100 Ryan Roberts <[email protected]> wrote:

[...]
The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code

I have just tried to recompile the openSUSE kernel with these patches applied, and I'm running into this:

Quoted 7 lines

CC arch/arm64/hyperv/hv_core.o
In file included from ../arch/arm64/hyperv/hv_core.c:14:0:
../include/linux/hyperv.h:158:5: error: variably modified 'reserved2' at file scope

  u8 reserved2[PAGE_SIZE - 68];
     ^~~~~~~~~

It looks like one more place which needs a patch, right?

As mentioned in the cover letter, so far I've only converted enough to get the defconfig *image* building (i.e. no modules). If you are compiling a different config or compiling the modules for defconfig, you will likely run into these types of issues.

That said, I do have some patches to fix Hyper-V, which Michael Kelley was kind enough to send me.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

I understand that Suse might be able to help with wider performance testing - if that's the reason you are trying to compile, you could send me your config and I'll start working on fixing up other drivers?

This project was de-prioritised for some time, but I have just returned to it, and one of our test systems uses a Mellanox 5 NIC, which did not build.

No problem - I appreciate all the time you have spent on it so far!

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

If you still have time to work on your patch series, please, can you look into enabling MLX5_CORE_EN?

I've also had other things that have been taking up my time. I'm planning to get back to this series properly after Christmas and convert all the remaining module code. I'm hoping that Michael's patch will solve your problem for now?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Oh, and have you rebased the series to 6.12 yet?

Afraid the latest I have at the moment is based on v6.12-rc3. It also includes all the changes from the review feedback:

https://gitlab.arm.com/linux-arm/linux-rr/-/tree/features/boot-time-page-size-v2-wip

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

FWIW, here's what I hacked together to compile and run the mlx5 driver in a Hyper-V VM. This was against a 6.11 kernel code base.

Wow! Thank you, Michael. I'll give it a try.

Yes, thanks, Michael - I'll take a look at this and integrate into my tree after Christmas.

To be clear, you probably do *not* want to integrate my changes into your tree. They are a hack, and I'm not sure what approach the mlx driver folks will want to take. But they made the Mellanox CX-5 NIC functional for my purposes in testing variable page sizes.

Michael

#173 lore raw
In reply to Ryan Roberts
Quoted 10 lines · Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

Quoted 7 lines

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

This looks really promising. Building and maintaining separate kernels is costly. Being able to build one kernel for three protential page sizes would not only cut down on the overhead of producing kernel packages and images, but also eases benchmarking and testing different page sizes without the need to build and install multiple kernels.

I'm also impressed that the patches are less intrusive than I would have expected. I'm looking forward to seeing this project move forward.

Thanks,
Shaggy

Quoted 441 lines

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to support
boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page size
selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times, so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|-------------|---------|---------|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
| config      |    mean |   stdev |   stdev |   stdev |
|-------------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

  arch/alpha/include/asm/page.h                 |   1 +
  arch/arc/include/asm/page.h                   |   1 +
  arch/arm/include/asm/page.h                   |   1 +
  arch/arm64/Kconfig                            |  26 ++-
  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/kernel-pgtable.h       | 150 +++++++++----
  arch/arm64/include/asm/kvm_arm.h              |  21 +-
  arch/arm64/include/asm/kvm_hyp.h              |  11 +
  arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
  arch/arm64/include/asm/memory.h               |  62 ++++--
  arch/arm64/include/asm/page-def.h             |   3 +-
  arch/arm64/include/asm/pgalloc.h              |  16 +-
  arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
  arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
  arch/arm64/include/asm/pgtable-prot.h         |   2 +-
  arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
  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/sysreg.h               |  54 +++--
  arch/arm64/include/asm/tlb.h                  |   3 +
  arch/arm64/kernel/asm-offsets.c               |   4 +-
  arch/arm64/kernel/cpufeature.c                |  93 ++++++--
  arch/arm64/kernel/efi.c                       |   2 +-
  arch/arm64/kernel/entry.S                     |  60 +++++-
  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         |  68 +++++-
  arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
  arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
  arch/arm64/kernel/pi/pi.h                     |  63 +++++-
  arch/arm64/kernel/relocate_kernel.S           |  10 +-
  arch/arm64/kernel/vdso-wrap.S                 |   4 +-
  arch/arm64/kernel/vdso.c                      |   7 +-
  arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
  arch/arm64/kernel/vdso32-wrap.S               |   4 +-
  arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
  arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
  arch/arm64/kvm/arm.c                          |  10 +
  arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
  arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
  arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
  arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
  arch/arm64/kvm/mmu.c                          |  39 ++--
  arch/arm64/lib/clear_page.S                   |   7 +-
  arch/arm64/lib/copy_page.S                    |  33 ++-
  arch/arm64/lib/mte.S                          |  27 ++-
  arch/arm64/mm/Makefile                        |   1 +
  arch/arm64/mm/fixmap.c                        |  38 ++--
  arch/arm64/mm/hugetlbpage.c                   |  40 +---
  arch/arm64/mm/init.c                          |  26 +--
  arch/arm64/mm/kasan_init.c                    |   8 +-
  arch/arm64/mm/mmu.c                           |  53 +++--
  arch/arm64/mm/pgd.c                           |  12 +-
  arch/arm64/mm/pgtable-geometry.c              |  24 +++
  arch/arm64/mm/proc.S                          | 128 ++++++++---
  arch/arm64/mm/ptdump.c                        |   3 +-
  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 +
  crypto/lskcipher.c                            |   4 +-
  drivers/ata/sata_sil24.c                      |  46 ++--
  drivers/base/node.c                           |   6 +-
  drivers/base/topology.c                       |  32 +--
  drivers/block/virtio_blk.c                    |   2 +-
  drivers/char/random.c                         |   4 +-
  drivers/edac/edac_mc.h                        |  13 +-
  drivers/firmware/efi/libstub/arm64.c          |   3 +-
  drivers/irqchip/irq-gic-v3-its.c              |   2 +-
  drivers/mtd/mtdswap.c                         |   4 +-
  drivers/net/ethernet/freescale/fec.h          |   3 +-
  drivers/net/ethernet/freescale/fec_main.c     |   5 +-
  .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
  drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
  drivers/net/ethernet/intel/igb/igb.h          |  25 +--
  drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
  drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
  drivers/net/ethernet/marvell/mvneta.c         |   9 +-
  drivers/net/ethernet/marvell/sky2.h           |   2 +-
  drivers/tee/optee/call.c                      |   7 +-
  drivers/tee/optee/smc_abi.c                   |   2 +-
  drivers/virtio/virtio_balloon.c               |  10 +-
  drivers/xen/balloon.c                         |  11 +-
  drivers/xen/biomerge.c                        |  12 +-
  drivers/xen/privcmd.c                         |   2 +-
  drivers/xen/xenbus/xenbus_client.c            |   5 +-
  drivers/xen/xlate_mmu.c                       |   6 +-
  fs/binfmt_elf.c                               |  11 +-
  fs/buffer.c                                   |   2 +-
  fs/coredump.c                                 |   8 +-
  fs/ext4/ext4.h                                |  36 ++--
  fs/ext4/move_extent.c                         |   2 +-
  fs/ext4/readpage.c                            |   2 +-
  fs/fat/dir.c                                  |   4 +-
  fs/fat/fatent.c                               |   4 +-
  fs/nfs/nfs42proc.c                            |   2 +-
  fs/nfs/nfs42xattr.c                           |   2 +-
  fs/nfs/nfs4proc.c                             |   2 +-
  include/asm-generic/pgtable-geometry.h        |  71 +++++++
  include/asm-generic/vmlinux.lds.h             |  38 ++--
  include/linux/buffer_head.h                   |   1 +
  include/linux/cpumask.h                       |   5 +
  include/linux/linkage.h                       |   4 +-
  include/linux/mm.h                            |  17 +-
  include/linux/mm_types.h                      |  15 +-
  include/linux/mm_types_task.h                 |   2 +-
  include/linux/mmzone.h                        |   3 +-
  include/linux/netlink.h                       |   6 +-
  include/linux/percpu-defs.h                   |   4 +-
  include/linux/perf_event.h                    |   2 +-
  include/linux/sched.h                         |   4 +-
  include/linux/slab.h                          |   7 +-
  include/linux/stackdepot.h                    |   6 +-
  include/linux/sunrpc/svc.h                    |   8 +-
  include/linux/sunrpc/svc_rdma.h               |   4 +-
  include/linux/sunrpc/svcsock.h                |   2 +-
  include/linux/swap.h                          |  17 +-
  include/linux/swapops.h                       |   6 +-
  include/linux/thread_info.h                   |  10 +-
  include/xen/page.h                            |   2 +
  init/main.c                                   |   7 +-
  kernel/bpf/core.c                             |   9 +-
  kernel/bpf/ringbuf.c                          |  54 ++---
  kernel/cgroup/cgroup.c                        |   8 +-
  kernel/crash_core.c                           |   2 +-
  kernel/events/core.c                          |   2 +-
  kernel/fork.c                                 |  71 +++----
  kernel/power/power.h                          |   2 +-
  kernel/power/snapshot.c                       |   2 +-
  kernel/power/swap.c                           | 129 +++++++++--
  kernel/trace/fgraph.c                         |   2 +-
  kernel/trace/trace.c                          |   2 +-
  lib/stackdepot.c                              |   6 +-
  mm/kasan/report.c                             |   3 +-
  mm/memcontrol.c                               |  11 +-
  mm/memory.c                                   |   4 +-
  mm/mmap.c                                     |   2 +-
  mm/page-writeback.c                           |   2 +-
  mm/page_alloc.c                               |  31 +--
  mm/slub.c                                     |   2 +-
  mm/sparse.c                                   |   2 +-
  mm/swapfile.c                                 |   2 +-
  mm/vmalloc.c                                  |   7 +-
  net/9p/trans_virtio.c                         |   4 +-
  net/core/hotdata.c                            |   4 +-
  net/core/skbuff.c                             |   4 +-
  net/core/sysctl_net_core.c                    |   2 +-
  net/sunrpc/cache.c                            |   3 +-
  net/unix/af_unix.c                            |   2 +-
  sound/soc/soc-utils.c                         |   4 +-
  virt/kvm/kvm_main.c                           |   2 +-
  172 files changed, 2185 insertions(+), 951 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

--
2.43.0

author reviewer
#174 lore raw
Quoted 19 lines · Dave Kleikamp
Quoted 10 lines · Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

Quoted 7 lines

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Quoted 8 lines

This looks really promising. Building and maintaining separate kernels is costly. Being able to build one kernel for three protential page sizes would not only cut down on the overhead of producing kernel packages and images, but also eases benchmarking and testing different page sizes without the need to build and install multiple kernels.

I'm also impressed that the patches are less intrusive than I would have expected. I'm looking forward to seeing this project move forward.

Thanks for the feedback! I'm sure any review/test capacity that Oracle has would be greatly appreciated :)

Thanks,
Ryan

Quoted 445 lines

Thanks,
Shaggy

Quoted 441 lines

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

The series is arranged as follows:

   - patch 1:       Add macros required for converting non-arch code to support boot-time page size selection
   - patches 2-36:  Remove PAGE_SIZE compile-time constant assumption from all non-arch code
   - patches 37-38: Some arm64 tidy ups
   - patch 39:       Add macros required for converting arm64 code to support boot-time page size selection
   - patches 40-56: arm64 changes to support boot-time page size selection
   - patch 57:       Add arm64 Kconfig option to enable boot-time page size selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times, so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|-------------|---------|---------|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
| config      |    mean |   stdev |   stdev |   stdev |
|-------------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
   mm: Add macros ahead of supporting boot-time page size selection
   vmlinux: Align to PAGE_SIZE_MAX
   mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
   mm: Avoid split pmd ptl if pmd level is run-time folded
   mm: Remove PAGE_SIZE compile-time constant assumption
   fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
   fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
   fork: Permit boot-time THREAD_SIZE determination
   cgroup: Remove PAGE_SIZE compile-time constant assumption
   bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
   stackdepot: Remove PAGE_SIZE compile-time constant assumption
   perf: Remove PAGE_SIZE compile-time constant assumption
   kvm: Remove PAGE_SIZE compile-time constant assumption
   trace: Remove PAGE_SIZE compile-time constant assumption
   crash: Remove PAGE_SIZE compile-time constant assumption
   crypto: Remove PAGE_SIZE compile-time constant assumption
   sunrpc: Remove PAGE_SIZE compile-time constant assumption
   sound: Remove PAGE_SIZE compile-time constant assumption
   net: Remove PAGE_SIZE compile-time constant assumption
   net: fec: Remove PAGE_SIZE compile-time constant assumption
   net: marvell: Remove PAGE_SIZE compile-time constant assumption
   net: hns3: Remove PAGE_SIZE compile-time constant assumption
   net: e1000: Remove PAGE_SIZE compile-time constant assumption
   net: igbvf: Remove PAGE_SIZE compile-time constant assumption
   net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
   edac: Remove PAGE_SIZE compile-time constant assumption
   optee: Remove PAGE_SIZE compile-time constant assumption
   random: Remove PAGE_SIZE compile-time constant assumption
   sata_sil24: Remove PAGE_SIZE compile-time constant assumption
   virtio: Remove PAGE_SIZE compile-time constant assumption
   xen: Remove PAGE_SIZE compile-time constant assumption
   arm64: Fix macros to work in C code in addition to the linker script
   arm64: Track early pgtable allocation limit
   arm64: Introduce macros required for boot-time page selection
   arm64: Refactor early pgtable size calculation macros
   arm64: Pass desired page size on command line
   arm64: Divorce early init from PAGE_SIZE
   arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
   arm64: Align sections to PAGE_SIZE_MAX
   arm64: Rework trampoline rodata mapping
   arm64: Generalize fixmap for boot-time page size
   arm64: Statically allocate and align for worst-case page size
   arm64: Convert switch to if for non-const comparison values
   arm64: Convert BUILD_BUG_ON to VM_BUG_ON
   arm64: Remove PAGE_SZ asm-offset
   arm64: Introduce cpu features for page sizes
   arm64: Remove PAGE_SIZE from assembly code
   arm64: Runtime-fold pmd level
   arm64: Support runtime folding in idmap_kpti_install_ng_mappings
   arm64: TRAMP_VALIAS is no longer compile-time constant
   arm64: Determine THREAD_SIZE at boot-time
   arm64: Enable boot-time page size selection

  arch/alpha/include/asm/page.h                 |   1 + arch/arc/include/asm/page.h                   |   1 + arch/arm/include/asm/page.h                   |   1 + arch/arm64/Kconfig                            |  26 ++- 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/kernel-pgtable.h       | 150 +++++++++---- arch/arm64/include/asm/kvm_arm.h              |  21 +- arch/arm64/include/asm/kvm_hyp.h              |  11 + arch/arm64/include/asm/kvm_pgtable.h          |   6 +- arch/arm64/include/asm/memory.h               |  62 ++++-- arch/arm64/include/asm/page-def.h             |   3 +- arch/arm64/include/asm/pgalloc.h              |  16 +- arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++ arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++- arch/arm64/include/asm/pgtable-prot.h         |   2 +- arch/arm64/include/asm/pgtable.h              | 133 +++++++++--- 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/sysreg.h               |  54 +++-- arch/arm64/include/asm/tlb.h                  |   3 + arch/arm64/kernel/asm-offsets.c               |   4 +- arch/arm64/kernel/cpufeature.c                |  93 ++++++-- arch/arm64/kernel/efi.c                       |   2 +- arch/arm64/kernel/entry.S                     |  60 +++++- 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         |  68 +++++- arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++---- arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++-- arch/arm64/kernel/pi/pi.h                     |  63 +++++- arch/arm64/kernel/relocate_kernel.S           |  10 +- arch/arm64/kernel/vdso-wrap.S                 |   4 +- arch/arm64/kernel/vdso.c                      |   7 +- arch/arm64/kernel/vdso/vdso.lds.S             |   4 +- arch/arm64/kernel/vdso32-wrap.S               |   4 +- arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +- arch/arm64/kernel/vmlinux.lds.S               |  48 +++-- arch/arm64/kvm/arm.c                          |  10 + arch/arm64/kvm/hyp/nvhe/Makefile              |   1 + arch/arm64/kvm/hyp/nvhe/host.S                |  10 +- arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +- arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++ arch/arm64/kvm/mmu.c                          |  39 ++-- arch/arm64/lib/clear_page.S                   |   7 +- arch/arm64/lib/copy_page.S                    |  33 ++- arch/arm64/lib/mte.S                          |  27 ++- arch/arm64/mm/Makefile                        |   1 + arch/arm64/mm/fixmap.c                        |  38 ++-- arch/arm64/mm/hugetlbpage.c                   |  40 +--- arch/arm64/mm/init.c                          |  26 +-- arch/arm64/mm/kasan_init.c                    |   8 +- arch/arm64/mm/mmu.c                           |  53 +++-- arch/arm64/mm/pgd.c                           |  12 +- arch/arm64/mm/pgtable-geometry.c              |  24 +++ arch/arm64/mm/proc.S                          | 128 ++++++++--- arch/arm64/mm/ptdump.c                        |   3 +- 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 + crypto/lskcipher.c                            |   4 +- drivers/ata/sata_sil24.c                      |  46 ++-- drivers/base/node.c                           |   6 +- drivers/base/topology.c                       |  32 +-- drivers/block/virtio_blk.c                    |   2 +- drivers/char/random.c                         |   4 +- drivers/edac/edac_mc.h                        |  13 +- drivers/firmware/efi/libstub/arm64.c          |   3 +- drivers/irqchip/irq-gic-v3-its.c              |   2 +- drivers/mtd/mtdswap.c                         |   4 +- drivers/net/ethernet/freescale/fec.h          |   3 +- drivers/net/ethernet/freescale/fec_main.c     |   5 +- .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +- drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +- drivers/net/ethernet/intel/igb/igb.h          |  25 +-- drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------ drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +- drivers/net/ethernet/marvell/mvneta.c         |   9 +- drivers/net/ethernet/marvell/sky2.h           |   2 +- drivers/tee/optee/call.c                      |   7 +- drivers/tee/optee/smc_abi.c                   |   2 +- drivers/virtio/virtio_balloon.c               |  10 +- drivers/xen/balloon.c                         |  11 +- drivers/xen/biomerge.c                        |  12 +- drivers/xen/privcmd.c                         |   2 +- drivers/xen/xenbus/xenbus_client.c            |   5 +- drivers/xen/xlate_mmu.c                       |   6 +- fs/binfmt_elf.c                               |  11 +- fs/buffer.c                                   |   2 +- fs/coredump.c                                 |   8 +- fs/ext4/ext4.h                                |  36 ++-- fs/ext4/move_extent.c                         |   2 +- fs/ext4/readpage.c                            |   2 +- fs/fat/dir.c                                  |   4 +- fs/fat/fatent.c                               |   4 +- fs/nfs/nfs42proc.c                            |   2 +- fs/nfs/nfs42xattr.c                           |   2 +- fs/nfs/nfs4proc.c                             |   2 +- include/asm-generic/pgtable-geometry.h        |  71 +++++++ include/asm-generic/vmlinux.lds.h             |  38 ++-- include/linux/buffer_head.h                   |   1 + include/linux/cpumask.h                       |   5 + include/linux/linkage.h                       |   4 +- include/linux/mm.h                            |  17 +- include/linux/mm_types.h                      |  15 +- include/linux/mm_types_task.h                 |   2 +- include/linux/mmzone.h                        |   3 +- include/linux/netlink.h                       |   6 +- include/linux/percpu-defs.h                   |   4 +- include/linux/perf_event.h                    |   2 +- include/linux/sched.h                         |   4 +- include/linux/slab.h                          |   7 +- include/linux/stackdepot.h                    |   6 +- include/linux/sunrpc/svc.h                    |   8 +- include/linux/sunrpc/svc_rdma.h               |   4 +- include/linux/sunrpc/svcsock.h                |   2 +- include/linux/swap.h                          |  17 +- include/linux/swapops.h                       |   6 +- include/linux/thread_info.h                   |  10 +- include/xen/page.h                            |   2 + init/main.c                                   |   7 +- kernel/bpf/core.c                             |   9 +- kernel/bpf/ringbuf.c                          |  54 ++--- kernel/cgroup/cgroup.c                        |   8 +- kernel/crash_core.c                           |   2 +- kernel/events/core.c                          |   2 +- kernel/fork.c                                 |  71 +++---- kernel/power/power.h                          |   2 +- kernel/power/snapshot.c                       |   2 +- kernel/power/swap.c                           | 129 +++++++++-- kernel/trace/fgraph.c                         |   2 +- kernel/trace/trace.c                          |   2 +- lib/stackdepot.c                              |   6 +- mm/kasan/report.c                             |   3 +- mm/memcontrol.c                               |  11 +- mm/memory.c                                   |   4 +- mm/mmap.c                                     |   2 +- mm/page-writeback.c                           |   2 +- mm/page_alloc.c                               |  31 +-- mm/slub.c                                     |   2 +- mm/sparse.c                                   |   2 +- mm/swapfile.c                                 |   2 +- mm/vmalloc.c                                  |   7 +- net/9p/trans_virtio.c                         |   4 +- net/core/hotdata.c                            |   4 +- net/core/skbuff.c                             |   4 +- net/core/sysctl_net_core.c                    |   2 +- net/sunrpc/cache.c                            |   3 +- net/unix/af_unix.c                            |   2 +- sound/soc/soc-utils.c                         |   4 +- virt/kvm/kvm_main.c                           |   2 +-

  172 files changed, 2185 insertions(+), 951 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

Signature · 1 line
2.43.0
#175 lore raw
In reply to Ryan Roberts
Quoted 450 lines · Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to support
boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page size
selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times, so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|-------------|---------|---------|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
| config      |    mean |   stdev |   stdev |   stdev |
|-------------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

  arch/alpha/include/asm/page.h                 |   1 +
  arch/arc/include/asm/page.h                   |   1 +
  arch/arm/include/asm/page.h                   |   1 +
  arch/arm64/Kconfig                            |  26 ++-
  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/kernel-pgtable.h       | 150 +++++++++----
  arch/arm64/include/asm/kvm_arm.h              |  21 +-
  arch/arm64/include/asm/kvm_hyp.h              |  11 +
  arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
  arch/arm64/include/asm/memory.h               |  62 ++++--
  arch/arm64/include/asm/page-def.h             |   3 +-
  arch/arm64/include/asm/pgalloc.h              |  16 +-
  arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
  arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
  arch/arm64/include/asm/pgtable-prot.h         |   2 +-
  arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
  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/sysreg.h               |  54 +++--
  arch/arm64/include/asm/tlb.h                  |   3 +
  arch/arm64/kernel/asm-offsets.c               |   4 +-
  arch/arm64/kernel/cpufeature.c                |  93 ++++++--
  arch/arm64/kernel/efi.c                       |   2 +-
  arch/arm64/kernel/entry.S                     |  60 +++++-
  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         |  68 +++++-
  arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
  arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
  arch/arm64/kernel/pi/pi.h                     |  63 +++++-
  arch/arm64/kernel/relocate_kernel.S           |  10 +-
  arch/arm64/kernel/vdso-wrap.S                 |   4 +-
  arch/arm64/kernel/vdso.c                      |   7 +-
  arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
  arch/arm64/kernel/vdso32-wrap.S               |   4 +-
  arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
  arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
  arch/arm64/kvm/arm.c                          |  10 +
  arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
  arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
  arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
  arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
  arch/arm64/kvm/mmu.c                          |  39 ++--
  arch/arm64/lib/clear_page.S                   |   7 +-
  arch/arm64/lib/copy_page.S                    |  33 ++-
  arch/arm64/lib/mte.S                          |  27 ++-
  arch/arm64/mm/Makefile                        |   1 +
  arch/arm64/mm/fixmap.c                        |  38 ++--
  arch/arm64/mm/hugetlbpage.c                   |  40 +---
  arch/arm64/mm/init.c                          |  26 +--
  arch/arm64/mm/kasan_init.c                    |   8 +-
  arch/arm64/mm/mmu.c                           |  53 +++--
  arch/arm64/mm/pgd.c                           |  12 +-
  arch/arm64/mm/pgtable-geometry.c              |  24 +++
  arch/arm64/mm/proc.S                          | 128 ++++++++---
  arch/arm64/mm/ptdump.c                        |   3 +-
  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 +
  crypto/lskcipher.c                            |   4 +-
  drivers/ata/sata_sil24.c                      |  46 ++--
  drivers/base/node.c                           |   6 +-
  drivers/base/topology.c                       |  32 +--
  drivers/block/virtio_blk.c                    |   2 +-
  drivers/char/random.c                         |   4 +-
  drivers/edac/edac_mc.h                        |  13 +-
  drivers/firmware/efi/libstub/arm64.c          |   3 +-
  drivers/irqchip/irq-gic-v3-its.c              |   2 +-
  drivers/mtd/mtdswap.c                         |   4 +-
  drivers/net/ethernet/freescale/fec.h          |   3 +-
  drivers/net/ethernet/freescale/fec_main.c     |   5 +-
  .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
  drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
  drivers/net/ethernet/intel/igb/igb.h          |  25 +--
  drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
  drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
  drivers/net/ethernet/marvell/mvneta.c         |   9 +-
  drivers/net/ethernet/marvell/sky2.h           |   2 +-
  drivers/tee/optee/call.c                      |   7 +-
  drivers/tee/optee/smc_abi.c                   |   2 +-
  drivers/virtio/virtio_balloon.c               |  10 +-
  drivers/xen/balloon.c                         |  11 +-
  drivers/xen/biomerge.c                        |  12 +-
  drivers/xen/privcmd.c                         |   2 +-
  drivers/xen/xenbus/xenbus_client.c            |   5 +-
  drivers/xen/xlate_mmu.c                       |   6 +-
  fs/binfmt_elf.c                               |  11 +-
  fs/buffer.c                                   |   2 +-
  fs/coredump.c                                 |   8 +-
  fs/ext4/ext4.h                                |  36 ++--
  fs/ext4/move_extent.c                         |   2 +-
  fs/ext4/readpage.c                            |   2 +-
  fs/fat/dir.c                                  |   4 +-
  fs/fat/fatent.c                               |   4 +-
  fs/nfs/nfs42proc.c                            |   2 +-
  fs/nfs/nfs42xattr.c                           |   2 +-
  fs/nfs/nfs4proc.c                             |   2 +-
  include/asm-generic/pgtable-geometry.h        |  71 +++++++
  include/asm-generic/vmlinux.lds.h             |  38 ++--
  include/linux/buffer_head.h                   |   1 +
  include/linux/cpumask.h                       |   5 +
  include/linux/linkage.h                       |   4 +-
  include/linux/mm.h                            |  17 +-
  include/linux/mm_types.h                      |  15 +-
  include/linux/mm_types_task.h                 |   2 +-
  include/linux/mmzone.h                        |   3 +-
  include/linux/netlink.h                       |   6 +-
  include/linux/percpu-defs.h                   |   4 +-
  include/linux/perf_event.h                    |   2 +-
  include/linux/sched.h                         |   4 +-
  include/linux/slab.h                          |   7 +-
  include/linux/stackdepot.h                    |   6 +-
  include/linux/sunrpc/svc.h                    |   8 +-
  include/linux/sunrpc/svc_rdma.h               |   4 +-
  include/linux/sunrpc/svcsock.h                |   2 +-
  include/linux/swap.h                          |  17 +-
  include/linux/swapops.h                       |   6 +-
  include/linux/thread_info.h                   |  10 +-
  include/xen/page.h                            |   2 +
  init/main.c                                   |   7 +-
  kernel/bpf/core.c                             |   9 +-
  kernel/bpf/ringbuf.c                          |  54 ++---
  kernel/cgroup/cgroup.c                        |   8 +-
  kernel/crash_core.c                           |   2 +-
  kernel/events/core.c                          |   2 +-
  kernel/fork.c                                 |  71 +++----
  kernel/power/power.h                          |   2 +-
  kernel/power/snapshot.c                       |   2 +-
  kernel/power/swap.c                           | 129 +++++++++--
  kernel/trace/fgraph.c                         |   2 +-
  kernel/trace/trace.c                          |   2 +-
  lib/stackdepot.c                              |   6 +-
  mm/kasan/report.c                             |   3 +-
  mm/memcontrol.c                               |  11 +-
  mm/memory.c                                   |   4 +-
  mm/mmap.c                                     |   2 +-
  mm/page-writeback.c                           |   2 +-
  mm/page_alloc.c                               |  31 +--
  mm/slub.c                                     |   2 +-
  mm/sparse.c                                   |   2 +-
  mm/swapfile.c                                 |   2 +-
  mm/vmalloc.c                                  |   7 +-
  net/9p/trans_virtio.c                         |   4 +-
  net/core/hotdata.c                            |   4 +-
  net/core/skbuff.c                             |   4 +-
  net/core/sysctl_net_core.c                    |   2 +-
  net/sunrpc/cache.c                            |   3 +-
  net/unix/af_unix.c                            |   2 +-
  sound/soc/soc-utils.c                         |   4 +-
  virt/kvm/kvm_main.c                           |   2 +-
  172 files changed, 2185 insertions(+), 951 deletions(-)
Quoted 9 lines

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

--
2.43.0

Hi Ryan,

First off, this is excellent work!  Your cover page was very detailed and made the patch set easier to understand.

Some questions/comments:

Once a kernel is booted with a certain page size, could there be issues if it is booted later with a different page size?  How about if this is done frequently?

A random example of this: Lets say a retailer, doctors office or a similar OLTP environment prefers a small page size during the day for performance reasons.  Then in the off-hours prefer a large page size for DSS type workloads like running reports or batch jobs.

I'm thinking how this might be used for cost savings.  The best approach would be to have multiple systems/VMs/cloud instances for the different workload types.  However, and end user might only have one system type and change the page size regularly as in that example.

Also, the performance impact does look very minimal.  It will be interesting to see if there are any effects on the larger industry standard benchmarks like TPC and SPEC.

Thanks,

Joe

#176 lore raw
Quoted 466 lines · Joseph Salisbury
Quoted 450 lines · Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to support
boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page size
selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times, so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|-------------|---------|---------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|-------------|---------|---------|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
| config      |    mean |   stdev |   stdev |   stdev |
|-------------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

   arch/alpha/include/asm/page.h                 |   1 +
   arch/arc/include/asm/page.h                   |   1 +
   arch/arm/include/asm/page.h                   |   1 +
   arch/arm64/Kconfig                            |  26 ++-
   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/kernel-pgtable.h       | 150 +++++++++----
   arch/arm64/include/asm/kvm_arm.h              |  21 +-
   arch/arm64/include/asm/kvm_hyp.h              |  11 +
   arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
   arch/arm64/include/asm/memory.h               |  62 ++++--
   arch/arm64/include/asm/page-def.h             |   3 +-
   arch/arm64/include/asm/pgalloc.h              |  16 +-
   arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
   arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
   arch/arm64/include/asm/pgtable-prot.h         |   2 +-
   arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
   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/sysreg.h               |  54 +++--
   arch/arm64/include/asm/tlb.h                  |   3 +
   arch/arm64/kernel/asm-offsets.c               |   4 +-
   arch/arm64/kernel/cpufeature.c                |  93 ++++++--
   arch/arm64/kernel/efi.c                       |   2 +-
   arch/arm64/kernel/entry.S                     |  60 +++++-
   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         |  68 +++++-
   arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
   arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
   arch/arm64/kernel/pi/pi.h                     |  63 +++++-
   arch/arm64/kernel/relocate_kernel.S           |  10 +-
   arch/arm64/kernel/vdso-wrap.S                 |   4 +-
   arch/arm64/kernel/vdso.c                      |   7 +-
   arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
   arch/arm64/kernel/vdso32-wrap.S               |   4 +-
   arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
   arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
   arch/arm64/kvm/arm.c                          |  10 +
   arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
   arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
   arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
   arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
   arch/arm64/kvm/mmu.c                          |  39 ++--
   arch/arm64/lib/clear_page.S                   |   7 +-
   arch/arm64/lib/copy_page.S                    |  33 ++-
   arch/arm64/lib/mte.S                          |  27 ++-
   arch/arm64/mm/Makefile                        |   1 +
   arch/arm64/mm/fixmap.c                        |  38 ++--
   arch/arm64/mm/hugetlbpage.c                   |  40 +---
   arch/arm64/mm/init.c                          |  26 +--
   arch/arm64/mm/kasan_init.c                    |   8 +-
   arch/arm64/mm/mmu.c                           |  53 +++--
   arch/arm64/mm/pgd.c                           |  12 +-
   arch/arm64/mm/pgtable-geometry.c              |  24 +++
   arch/arm64/mm/proc.S                          | 128 ++++++++---
   arch/arm64/mm/ptdump.c                        |   3 +-
   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 +
   crypto/lskcipher.c                            |   4 +-
   drivers/ata/sata_sil24.c                      |  46 ++--
   drivers/base/node.c                           |   6 +-
   drivers/base/topology.c                       |  32 +--
   drivers/block/virtio_blk.c                    |   2 +-
   drivers/char/random.c                         |   4 +-
   drivers/edac/edac_mc.h                        |  13 +-
   drivers/firmware/efi/libstub/arm64.c          |   3 +-
   drivers/irqchip/irq-gic-v3-its.c              |   2 +-
   drivers/mtd/mtdswap.c                         |   4 +-
   drivers/net/ethernet/freescale/fec.h          |   3 +-
   drivers/net/ethernet/freescale/fec_main.c     |   5 +-
   .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
   drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
   drivers/net/ethernet/intel/igb/igb.h          |  25 +--
   drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
   drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
   drivers/net/ethernet/marvell/mvneta.c         |   9 +-
   drivers/net/ethernet/marvell/sky2.h           |   2 +-
   drivers/tee/optee/call.c                      |   7 +-
   drivers/tee/optee/smc_abi.c                   |   2 +-
   drivers/virtio/virtio_balloon.c               |  10 +-
   drivers/xen/balloon.c                         |  11 +-
   drivers/xen/biomerge.c                        |  12 +-
   drivers/xen/privcmd.c                         |   2 +-
   drivers/xen/xenbus/xenbus_client.c            |   5 +-
   drivers/xen/xlate_mmu.c                       |   6 +-
   fs/binfmt_elf.c                               |  11 +-
   fs/buffer.c                                   |   2 +-
   fs/coredump.c                                 |   8 +-
   fs/ext4/ext4.h                                |  36 ++--
   fs/ext4/move_extent.c                         |   2 +-
   fs/ext4/readpage.c                            |   2 +-
   fs/fat/dir.c                                  |   4 +-
   fs/fat/fatent.c                               |   4 +-
   fs/nfs/nfs42proc.c                            |   2 +-
   fs/nfs/nfs42xattr.c                           |   2 +-
   fs/nfs/nfs4proc.c                             |   2 +-
   include/asm-generic/pgtable-geometry.h        |  71 +++++++
   include/asm-generic/vmlinux.lds.h             |  38 ++--
   include/linux/buffer_head.h                   |   1 +
   include/linux/cpumask.h                       |   5 +
   include/linux/linkage.h                       |   4 +-
   include/linux/mm.h                            |  17 +-
   include/linux/mm_types.h                      |  15 +-
   include/linux/mm_types_task.h                 |   2 +-
   include/linux/mmzone.h                        |   3 +-
   include/linux/netlink.h                       |   6 +-
   include/linux/percpu-defs.h                   |   4 +-
   include/linux/perf_event.h                    |   2 +-
   include/linux/sched.h                         |   4 +-
   include/linux/slab.h                          |   7 +-
   include/linux/stackdepot.h                    |   6 +-
   include/linux/sunrpc/svc.h                    |   8 +-
   include/linux/sunrpc/svc_rdma.h               |   4 +-
   include/linux/sunrpc/svcsock.h                |   2 +-
   include/linux/swap.h                          |  17 +-
   include/linux/swapops.h                       |   6 +-
   include/linux/thread_info.h                   |  10 +-
   include/xen/page.h                            |   2 +
   init/main.c                                   |   7 +-
   kernel/bpf/core.c                             |   9 +-
   kernel/bpf/ringbuf.c                          |  54 ++---
   kernel/cgroup/cgroup.c                        |   8 +-
   kernel/crash_core.c                           |   2 +-
   kernel/events/core.c                          |   2 +-
   kernel/fork.c                                 |  71 +++----
   kernel/power/power.h                          |   2 +-
   kernel/power/snapshot.c                       |   2 +-
   kernel/power/swap.c                           | 129 +++++++++--
   kernel/trace/fgraph.c                         |   2 +-
   kernel/trace/trace.c                          |   2 +-
   lib/stackdepot.c                              |   6 +-
   mm/kasan/report.c                             |   3 +-
   mm/memcontrol.c                               |  11 +-
   mm/memory.c                                   |   4 +-
   mm/mmap.c                                     |   2 +-
   mm/page-writeback.c                           |   2 +-
   mm/page_alloc.c                               |  31 +--
   mm/slub.c                                     |   2 +-
   mm/sparse.c                                   |   2 +-
   mm/swapfile.c                                 |   2 +-
   mm/vmalloc.c                                  |   7 +-
   net/9p/trans_virtio.c                         |   4 +-
   net/core/hotdata.c                            |   4 +-
   net/core/skbuff.c                             |   4 +-
   net/core/sysctl_net_core.c                    |   2 +-
   net/sunrpc/cache.c                            |   3 +-
   net/unix/af_unix.c                            |   2 +-
   sound/soc/soc-utils.c                         |   4 +-
   virt/kvm/kvm_main.c                           |   2 +-
   172 files changed, 2185 insertions(+), 951 deletions(-)
Quoted 9 lines

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

--
2.43.0

Hi Ryan,

Quoted 8 lines

First off, this is excellent work!  Your cover page was very detailed and made the patch set easier to understand.

Some questions/comments:

Once a kernel is booted with a certain page size, could there be issues if it is booted later with a different page size?  How about if this is done frequently?

I think that is the reason why you are only given the option in RHEL to select the kernel (4K vs. 64K) to use at install time.

Software can easily use a different data format for persistance based on the base page size. I would suspect DBs might be the usual suspects.

One example is swap space I think, where the base page size used when formatting the device is used, and it cannot be used with a different page size unless reformatting it.

So ... one has to be a bit careful ...

Signature · 3 lines
Cheers,

David / dhildenb
#177 lore raw

Re: [External] : Re: [RFC PATCH v1 00/57] Boot-time page size selection for arm64

Quoted 479 lines · David Hildenbrand
Quoted 469 lines · Joseph Salisbury
Quoted 452 lines · Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

The series is arranged as follows:

    - patch 1:       Add macros required for converting non-arch code to support boot-time page size selection
    - patches 2-36:  Remove PAGE_SIZE compile-time constant assumption from all non-arch code
    - patches 37-38: Some arm64 tidy ups
    - patch 39:       Add macros required for converting arm64 code to support boot-time page size selection
    - patches 40-56: arm64 changes to support boot-time page size selection
    - patch 57:       Add arm64 Kconfig option to enable boot-time page size selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data | data |    data |
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|-------------|---------|---------|---------|---------|---------|---------| 
| base-4k     |   20561 |       0 |    0.0% |   14314 | 0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 | 311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 | 1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 | 1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 | 204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 | 820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 | 973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times, so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user | real |    real |
| config      |    mean |   stdev |    mean |   stdev | mean |   stdev |
|-------------|---------|---------|---------|---------|---------|---------| 
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% | 0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% | -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% | -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|-------------|---------|---------|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
| config      |    mean |   stdev |   stdev |   stdev |
|-------------|---------|---------|---------|---------|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
    mm: Add macros ahead of supporting boot-time page size selection
    vmlinux: Align to PAGE_SIZE_MAX
    mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
    mm: Avoid split pmd ptl if pmd level is run-time folded
    mm: Remove PAGE_SIZE compile-time constant assumption
    fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
    fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
    fork: Permit boot-time THREAD_SIZE determination
    cgroup: Remove PAGE_SIZE compile-time constant assumption
    bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
    stackdepot: Remove PAGE_SIZE compile-time constant assumption
    perf: Remove PAGE_SIZE compile-time constant assumption
    kvm: Remove PAGE_SIZE compile-time constant assumption
    trace: Remove PAGE_SIZE compile-time constant assumption
    crash: Remove PAGE_SIZE compile-time constant assumption
    crypto: Remove PAGE_SIZE compile-time constant assumption
    sunrpc: Remove PAGE_SIZE compile-time constant assumption
    sound: Remove PAGE_SIZE compile-time constant assumption
    net: Remove PAGE_SIZE compile-time constant assumption
    net: fec: Remove PAGE_SIZE compile-time constant assumption
    net: marvell: Remove PAGE_SIZE compile-time constant assumption
    net: hns3: Remove PAGE_SIZE compile-time constant assumption
    net: e1000: Remove PAGE_SIZE compile-time constant assumption
    net: igbvf: Remove PAGE_SIZE compile-time constant assumption
    net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
    edac: Remove PAGE_SIZE compile-time constant assumption
    optee: Remove PAGE_SIZE compile-time constant assumption
    random: Remove PAGE_SIZE compile-time constant assumption
    sata_sil24: Remove PAGE_SIZE compile-time constant assumption
    virtio: Remove PAGE_SIZE compile-time constant assumption
    xen: Remove PAGE_SIZE compile-time constant assumption
    arm64: Fix macros to work in C code in addition to the linker script
    arm64: Track early pgtable allocation limit
    arm64: Introduce macros required for boot-time page selection
    arm64: Refactor early pgtable size calculation macros
    arm64: Pass desired page size on command line
    arm64: Divorce early init from PAGE_SIZE
    arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
    arm64: Align sections to PAGE_SIZE_MAX
    arm64: Rework trampoline rodata mapping
    arm64: Generalize fixmap for boot-time page size
    arm64: Statically allocate and align for worst-case page size
    arm64: Convert switch to if for non-const comparison values
    arm64: Convert BUILD_BUG_ON to VM_BUG_ON
    arm64: Remove PAGE_SZ asm-offset
    arm64: Introduce cpu features for page sizes
    arm64: Remove PAGE_SIZE from assembly code
    arm64: Runtime-fold pmd level
    arm64: Support runtime folding in idmap_kpti_install_ng_mappings
    arm64: TRAMP_VALIAS is no longer compile-time constant
    arm64: Determine THREAD_SIZE at boot-time
    arm64: Enable boot-time page size selection

   arch/alpha/include/asm/page.h                 |   1 + arch/arc/include/asm/page.h                   |   1 + arch/arm/include/asm/page.h                   |   1 + arch/arm64/Kconfig                            |  26 ++- 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/kernel-pgtable.h       | 150 +++++++++---- arch/arm64/include/asm/kvm_arm.h              |  21 +- arch/arm64/include/asm/kvm_hyp.h              |  11 + arch/arm64/include/asm/kvm_pgtable.h          |   6 +- arch/arm64/include/asm/memory.h               |  62 ++++-- arch/arm64/include/asm/page-def.h             |   3 +- arch/arm64/include/asm/pgalloc.h              |  16 +- arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++ arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++- arch/arm64/include/asm/pgtable-prot.h         |   2 +- arch/arm64/include/asm/pgtable.h              | 133 +++++++++--- 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/sysreg.h               |  54 +++-- arch/arm64/include/asm/tlb.h                  |   3 + arch/arm64/kernel/asm-offsets.c               |   4 +- arch/arm64/kernel/cpufeature.c                |  93 ++++++-- arch/arm64/kernel/efi.c                       |   2 +- arch/arm64/kernel/entry.S                     |  60 +++++- 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         |  68 +++++- arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++---- arch/arm64/kernel/pi/map_range.c              | 201
++++++++++++++++--
   arch/arm64/kernel/pi/pi.h                     |  63 +++++- arch/arm64/kernel/relocate_kernel.S           |  10 +- arch/arm64/kernel/vdso-wrap.S                 |   4 +- arch/arm64/kernel/vdso.c                      |   7 +- arch/arm64/kernel/vdso/vdso.lds.S             |   4 +- arch/arm64/kernel/vdso32-wrap.S               |   4 +- arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +- arch/arm64/kernel/vmlinux.lds.S               |  48 +++-- arch/arm64/kvm/arm.c                          |  10 + arch/arm64/kvm/hyp/nvhe/Makefile              |   1 + arch/arm64/kvm/hyp/nvhe/host.S                |  10 +- arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +- arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++ arch/arm64/kvm/mmu.c                          |  39 ++-- arch/arm64/lib/clear_page.S                   |   7 +- arch/arm64/lib/copy_page.S                    |  33 ++- arch/arm64/lib/mte.S                          |  27 ++- arch/arm64/mm/Makefile                        |   1 + arch/arm64/mm/fixmap.c                        |  38 ++-- arch/arm64/mm/hugetlbpage.c                   |  40 +--- arch/arm64/mm/init.c                          |  26 +-- arch/arm64/mm/kasan_init.c                    |   8 +- arch/arm64/mm/mmu.c                           |  53 +++-- arch/arm64/mm/pgd.c                           |  12 +- arch/arm64/mm/pgtable-geometry.c              |  24 +++ arch/arm64/mm/proc.S                          | 128 ++++++++--- arch/arm64/mm/ptdump.c                        |   3 +- 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 + crypto/lskcipher.c                            |   4 +- drivers/ata/sata_sil24.c                      |  46 ++-- drivers/base/node.c                           |   6 +- drivers/base/topology.c                       |  32 +-- drivers/block/virtio_blk.c                    |   2 +- drivers/char/random.c                         |   4 +- drivers/edac/edac_mc.h                        |  13 +- drivers/firmware/efi/libstub/arm64.c          |   3 +- drivers/irqchip/irq-gic-v3-its.c              |   2 +- drivers/mtd/mtdswap.c                         |   4 +- drivers/net/ethernet/freescale/fec.h          |   3 +- drivers/net/ethernet/freescale/fec_main.c     |   5 +- .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +- drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +- drivers/net/ethernet/intel/igb/igb.h          |  25 +-- drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------ drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +- drivers/net/ethernet/marvell/mvneta.c         |   9 +- drivers/net/ethernet/marvell/sky2.h           |   2 +- drivers/tee/optee/call.c                      |   7 +- drivers/tee/optee/smc_abi.c                   |   2 +- drivers/virtio/virtio_balloon.c               |  10 +- drivers/xen/balloon.c                         |  11 +- drivers/xen/biomerge.c                        |  12 +- drivers/xen/privcmd.c                         |   2 +- drivers/xen/xenbus/xenbus_client.c            |   5 +- drivers/xen/xlate_mmu.c                       |   6 +- fs/binfmt_elf.c                               |  11 +- fs/buffer.c                                   |   2 +- fs/coredump.c                                 |   8 +- fs/ext4/ext4.h                                |  36 ++-- fs/ext4/move_extent.c                         |   2 +- fs/ext4/readpage.c                            |   2 +- fs/fat/dir.c                                  |   4 +- fs/fat/fatent.c                               |   4 +- fs/nfs/nfs42proc.c                            |   2 +- fs/nfs/nfs42xattr.c                           |   2 +- fs/nfs/nfs4proc.c                             |   2 +- include/asm-generic/pgtable-geometry.h        |  71 +++++++ include/asm-generic/vmlinux.lds.h             |  38 ++-- include/linux/buffer_head.h                   |   1 + include/linux/cpumask.h                       |   5 + include/linux/linkage.h                       |   4 +- include/linux/mm.h                            |  17 +- include/linux/mm_types.h                      |  15 +- include/linux/mm_types_task.h                 |   2 +- include/linux/mmzone.h                        |   3 +- include/linux/netlink.h                       |   6 +- include/linux/percpu-defs.h                   |   4 +- include/linux/perf_event.h                    |   2 +- include/linux/sched.h                         |   4 +- include/linux/slab.h                          |   7 +- include/linux/stackdepot.h                    |   6 +- include/linux/sunrpc/svc.h                    |   8 +- include/linux/sunrpc/svc_rdma.h               |   4 +- include/linux/sunrpc/svcsock.h                |   2 +- include/linux/swap.h                          |  17 +- include/linux/swapops.h                       |   6 +- include/linux/thread_info.h                   |  10 +- include/xen/page.h                            |   2 + init/main.c                                   |   7 +- kernel/bpf/core.c                             |   9 +- kernel/bpf/ringbuf.c                          |  54 ++--- kernel/cgroup/cgroup.c                        |   8 +- kernel/crash_core.c                           |   2 +- kernel/events/core.c                          |   2 +- kernel/fork.c                                 |  71 +++---- kernel/power/power.h                          |   2 +- kernel/power/snapshot.c                       |   2 +- kernel/power/swap.c                           | 129 +++++++++-- kernel/trace/fgraph.c                         |   2 +- kernel/trace/trace.c                          |   2 +- lib/stackdepot.c                              |   6 +- mm/kasan/report.c                             |   3 +- mm/memcontrol.c                               |  11 +- mm/memory.c                                   |   4 +- mm/mmap.c                                     |   2 +- mm/page-writeback.c                           |   2 +- mm/page_alloc.c                               |  31 +-- mm/slub.c                                     |   2 +- mm/sparse.c                                   |   2 +- mm/swapfile.c                                 |   2 +- mm/vmalloc.c                                  |   7 +- net/9p/trans_virtio.c                         |   4 +- net/core/hotdata.c                            |   4 +- net/core/skbuff.c                             |   4 +- net/core/sysctl_net_core.c                    |   2 +- net/sunrpc/cache.c                            |   3 +- net/unix/af_unix.c                            |   2 +- sound/soc/soc-utils.c                         |   4 +- virt/kvm/kvm_main.c                           |   2 +-

Quoted 10 lines
   172 files changed, 2185 insertions(+), 951 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

Signature · 1 line
2.43.0

Hi Ryan,

Quoted 8 lines

First off, this is excellent work!  Your cover page was very detailed and made the patch set easier to understand.

Some questions/comments:

Once a kernel is booted with a certain page size, could there be issues if it is booted later with a different page size?  How about if this is done frequently?

Quoted 8 lines

I think that is the reason why you are only given the option in RHEL to select the kernel (4K vs. 64K) to use at install time.

Software can easily use a different data format for persistance based on the base page size. I would suspect DBs might be the usual suspects.

One example is swap space I think, where the base page size used when formatting the device is used, and it cannot be used with a different page size unless reformatting it.

So ... one has to be a bit careful ...

Yes, that is what I was thinking.  Once a userspace process does an I/O and if it is based on PAGE_SIZE things can go south.  I think this is not an issue with THP, so maybe it's possible with boot-time page selection?

#178 lore raw

Re: [External] : Re: [RFC PATCH v1 00/57] Boot-time page size selection for arm64

Quoted 22 lines
Quoted 22 lines
Quoted 11 lines

Hi Ryan,

First off, this is excellent work!  Your cover page was very detailed and made the patch set easier to understand.

Some questions/comments:

Once a kernel is booted with a certain page size, could there be issues if it is booted later with a different page size?  How about if this is done frequently?

I think that is the reason why you are only given the option in RHEL to select the kernel (4K vs. 64K) to use at install time.

Software can easily use a different data format for persistance based on the base page size. I would suspect DBs might be the usual suspects.

One example is swap space I think, where the base page size used when formatting the device is used, and it cannot be used with a different page size unless reformatting it.

Quoted 5 lines
Quoted 2 lines

So ... one has to be a bit careful ...

Yes, that is what I was thinking.  Once a userspace process does an I/O and if it is based on PAGE_SIZE things can go south.  I think this is not an issue with THP, so maybe it's possible with boot-time page selection?

THP is a different beast and has different semantics: the base page size doesn't change: the result of getpagesize() is unmodified ("transparent").

One would have to emulate for a given user space process a different page size ... and Ryan can likely tell some stories about that.

Not that I consider it reasonable to have dynamic page sizes in the kernel and then try emulating a different one for all user space.

Signature · 3 lines
Cheers,

David / dhildenb
#179 lore raw

Re: [External] : Re: [RFC PATCH v1 00/57] Boot-time page size selection for arm64

Quoted 28 lines · David Hildenbrand
Quoted 22 lines
Quoted 22 lines
Quoted 11 lines

Hi Ryan,

First off, this is excellent work!  Your cover page was very detailed and made the patch set easier to understand.

Some questions/comments:

Once a kernel is booted with a certain page size, could there be issues if it is booted later with a different page size?  How about if this is done frequently?

I think that is the reason why you are only given the option in RHEL to select the kernel (4K vs. 64K) to use at install time.

Software can easily use a different data format for persistance based on the base page size. I would suspect DBs might be the usual suspects.

One example is swap space I think, where the base page size used when formatting the device is used, and it cannot be used with a different page size unless reformatting it.

Quoted 5 lines
Quoted 2 lines

So ... one has to be a bit careful ...

Yes, that is what I was thinking.  Once a userspace process does an I/O and if it is based on PAGE_SIZE things can go south.  I think this is not an issue with THP, so maybe it's possible with boot-time page selection?

Quoted 5 lines

THP is a different beast and has different semantics: the base page size doesn't change: the result of getpagesize() is unmodified ("transparent").

One would have to emulate for a given user space process a different page size ... and Ryan can likely tell some stories about that.

Not that I consider it reasonable to have dynamic page sizes in the kernel and then try emulating a different one for all user space.

This is probably  a case of ensuring proper documentation from the distro or application vendor.

Or maybe some type of "Safety gate" could be implemented outside of the kernel. Some check for the prior use of different page sizes, in the cases where it could cause problems.

author reviewer
#180 lore raw

Re: [External] : Re: [RFC PATCH v1 00/57] Boot-time page size selection for arm64

Quoted 9 lines · Joseph Salisbury
Quoted 5 lines · David Hildenbrand
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Hi Ryan,

First off, this is excellent work!  Your cover page was very detailed and made the patch set easier to understand.

Thanks!

Quoted 32 lines
Quoted 23 lines
Quoted 17 lines
Quoted 17 lines
Quoted 6 lines

Some questions/comments:

Once a kernel is booted with a certain page size, could there be issues if it is booted later with a different page size?  How about if this is done frequently?

I think that is the reason why you are only given the option in RHEL to select the kernel (4K vs. 64K) to use at install time.

Software can easily use a different data format for persistance based on the base page size. I would suspect DBs might be the usual suspects.

One example is swap space I think, where the base page size used when formatting the device is used, and it cannot be used with a different page size unless reformatting it.

Quoted 5 lines
Quoted 2 lines

So ... one has to be a bit careful ...

Yes, that is what I was thinking.  Once a userspace process does an I/O and if it is based on PAGE_SIZE things can go south.  I think this is not an issue with THP, so maybe it's possible with boot-time page selection?

Quoted 8 lines

THP is a different beast and has different semantics: the base page size doesn't change: the result of getpagesize() is unmodified ("transparent").

One would have to emulate for a given user space process a different page size ... and Ryan can likely tell some stories about that.

Not that I consider it reasonable to have dynamic page sizes in the kernel and then try emulating a different one for all user space.

Quoted 6 lines

This is probably  a case of ensuring proper documentation from the distro or application vendor.

Or maybe some type of "Safety gate" could be implemented outside of the kernel. Some check for the prior use of different page sizes, in the cases where it could cause problems.

I agree there are likely to be problems in some corner cases if switching page size between boots, if persisted data makes assumptions about the page size. I would argue that any problems that are observed should really be considered bugs in the user space SW though.

But I don't think this is really any different from today; With Ubuntu, for example, you can install both 4K and 64K kernels concurrently, then choose which one to boot via Grub. So the issue exists there already. This proposed boot-time page size selection series, doesn't make that any worse, it just simplifies the distribution model, given the reality that distros are now having to support multiple page sizes.

Thanks,
Ryan

#181 lore raw
In reply to Ryan Roberts
Quoted 83 lines · EDT Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
| 
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| 
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
| 
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

* How would we handle an invalid selection at boot? Can we program in a fallback when the "wrong" mode is selected for a chip or something similar?

Thanks again and best regards!

(P.S.: Please add the asahi@ mailing list to the CC for future iterations of this patch set and tag both Hector and myself in as well. Thanks!)

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
author reviewer
#182 lore raw
Quoted 479 lines · Neal Gompa
Quoted 83 lines · EDT Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
| 
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
| 
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
| 
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Quoted 4 lines
Quoted 1 line

Thanks again and best regards!

(P.S.: Please add the asahi@ mailing list to the CC for future iterations of this patch set and tag both Hector and myself in as well. Thanks!)

Will do!

Quoted 2 lines
#183 lore raw
Quoted 502 lines · Ryan Roberts
Quoted 479 lines · Neal Gompa
Quoted 83 lines · EDT Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Like in downstream kernels like Fedora/RHEL/etc. I would expect the default would be 4k, but you could override with 16k, 64k, etc. with a karg.

Quoted 12 lines
Quoted 5 lines
Quoted 2 lines

Thanks again and best regards!

(P.S.: Please add the asahi@ mailing list to the CC for future iterations of this patch set and tag both Hector and myself in as well. Thanks!)

Will do!

Quoted 2 lines
author reviewer
#184 lore raw
Quoted 511 lines · Eric Curtin
Quoted 502 lines · Ryan Roberts
Quoted 479 lines · Neal Gompa
Quoted 83 lines · EDT Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Quoted 4 lines

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Ahh, yes, that would certainly work. I'll work it into the next version.

Quoted 18 lines

Like in downstream kernels like Fedora/RHEL/etc. I would expect the default would be 4k, but you could override with 16k, 64k, etc. with a karg.

Quoted 12 lines
Quoted 5 lines
Quoted 2 lines

Thanks again and best regards!

(P.S.: Please add the asahi@ mailing list to the CC for future iterations of this patch set and tag both Hector and myself in as well. Thanks!)

Will do!

Quoted 2 lines
#185 lore raw
Quoted 513 lines · Ryan Roberts
Quoted 511 lines · Eric Curtin
Quoted 509 lines · Ryan Roberts
Quoted 479 lines · Neal Gompa
Quoted 83 lines · EDT Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Quoted 7 lines
Quoted 4 lines

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Ahh, yes, that would certainly work. I'll work it into the next version.

Could we maybe extend to have some kind of way to include a table of SoC IDs that certain modes are disabled (e.g. 64k on Apple Silicon) and preferred modes when no arg is set (16k for Apple Silicon)? That way it'd work something like this:

1. Table identification of 4/16/64 depending on identified SoC
2. Unidentified ones follow build-time default
3. karg forces a mode regardless

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
author reviewer
#186 lore raw
Quoted 519 lines · Neal Gompa
Quoted 518 lines · Ryan Roberts
Quoted 515 lines · Eric Curtin
Quoted 502 lines · Ryan Roberts
Quoted 479 lines · Neal Gompa
Quoted 83 lines · EDT Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Quoted 5 lines
Quoted 2 lines

Ahh, yes, that would certainly work. I'll work it into the next version.

Could we maybe extend to have some kind of way to include a table of SoC IDs that certain modes are disabled (e.g. 64k on Apple Silicon)

64k is already disabled on Apple Silicon because mmfr0 reports that 64k is not supported.

Quoted 1 line

and preferred modes when no arg is set (16k for Apple Silicon)? That

And it's not obvious that we should hard-code a page size preference to a SoC ID. If the CPU can support multiple page sizes, it should be up to the SW stack to decide, not the SoC.

I'm guessing your desire is to have a single kernel build that will boot 16k by default on Apple Silicon and 4k by default on other systems, all without needing to modify the command line? Personally I think it's cleaner to just require setting the page size on the command line in these cases.

Quoted 3 lines

way it'd work something like this:

1. Table identification of 4/16/64 depending on identified SoC

So I'd prefer not to have this

Quoted 2 lines

2. Unidentified ones follow build-time default
3. karg forces a mode regardless

But keep these 2.

Quoted 2 lines
#187 lore raw
Quoted 541 lines · Ryan Roberts
Quoted 519 lines · Neal Gompa
Quoted 518 lines · Ryan Roberts
Quoted 515 lines · Eric Curtin
Quoted 502 lines · Ryan Roberts
Quoted 479 lines · Neal Gompa
Quoted 83 lines · EDT Ryan Roberts

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Quoted 5 lines
Quoted 2 lines

Ahh, yes, that would certainly work. I'll work it into the next version.

Could we maybe extend to have some kind of way to include a table of SoC IDs that certain modes are disabled (e.g. 64k on Apple Silicon)

64k is already disabled on Apple Silicon because mmfr0 reports that 64k is not supported.

Quoted 1 line

and preferred modes when no arg is set (16k for Apple Silicon)? That

And it's not obvious that we should hard-code a page size preference to a SoC ID. If the CPU can support multiple page sizes, it should be up to the SW stack to decide, not the SoC.

I'm guessing your desire is to have a single kernel build that will boot 16k by default on Apple Silicon and 4k by default on other systems, all without needing to modify the command line? Personally I think it's cleaner to just require setting the page size on the command line in these cases.

Quoted 9 lines
Quoted 3 lines

way it'd work something like this:

1. Table identification of 4/16/64 depending on identified SoC

So I'd prefer not to have this

Quoted 2 lines

2. Unidentified ones follow build-time default
3. karg forces a mode regardless

But keep these 2.

I think it makes sense to have it, because it's not just Apple Silicon where such a preference/requirement may be necessary. Apple Silicon technically works at 4k, but is completely broken at 4k because Linux cannot do 16k IOMMU with 4k everything else, so being able to at least prefer 16k out of the box is important. And SoCs like the NVIDIA Grace Hopper platform prefer 64k over other options (though I am unaware of a gross incompatibility that effectively requires it like Apple Silicon has).

When we're trying to get to "single generic image that works everywhere", stuff like this matters and I would really like you to consider it from the lens of "we want things to work as automagic as they do on x86".

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
#188 lore raw

Neal Gompa 於 2024/10/22 下午5:33 寫道:

Quoted 544 lines · Ryan Roberts
Quoted 543 lines · Neal Gompa
Quoted 519 lines · Ryan Roberts
Quoted 518 lines · Eric Curtin
Quoted 515 lines · Ryan Roberts
Quoted 502 lines · Neal Gompa
Quoted 479 lines · EDT Ryan Roberts
Quoted 83 lines

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Quoted 5 lines
Quoted 2 lines

Ahh, yes, that would certainly work. I'll work it into the next version.

Could we maybe extend to have some kind of way to include a table of SoC IDs that certain modes are disabled (e.g. 64k on Apple Silicon)

64k is already disabled on Apple Silicon because mmfr0 reports that 64k is not supported.

Quoted 1 line

and preferred modes when no arg is set (16k for Apple Silicon)? That

And it's not obvious that we should hard-code a page size preference to a SoC ID. If the CPU can support multiple page sizes, it should be up to the SW stack to decide, not the SoC.

I'm guessing your desire is to have a single kernel build that will boot 16k by default on Apple Silicon and 4k by default on other systems, all without needing to modify the command line? Personally I think it's cleaner to just require setting the page size on the command line in these cases.

Quoted 2 lines

way it'd work something like this:

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

1. Table identification of 4/16/64 depending on identified SoC

So I'd prefer not to have this

Quoted 2 lines

2. Unidentified ones follow build-time default
3. karg forces a mode regardless

But keep these 2.

Since we are talking about Apple Silicon and page size, I would like to add that on the Apple Silicon SoCs I am working on, the situation is like this:

Apple A7 (s5l8960x), A8 (T7000), A8X (T7001): CPU MMU support 4K and 64K page sizes.

Apple A9 (s8000/s8003), A9X (s8001), A10 (t8010), A10X (t8011), A11 (t8015):
CPU MMU Support 16K and 64K page sizes.

However, all of them have 4K page DART IOMMUs.

Quoted 9 lines

I think it makes sense to have it, because it's not just Apple Silicon where such a preference/requirement may be necessary. Apple Silicon technically works at 4k, but is completely broken at 4k because Linux cannot do 16k IOMMU with 4k everything else, so being able to at least prefer 16k out of the box is important. And SoCs like the NVIDIA Grace Hopper platform prefer 64k over other options (though I am unaware of a gross incompatibility that effectively requires it like Apple Silicon has).

Quoted 4 lines

When we're trying to get to "single generic image that works everywhere", stuff like this matters and I would really like you to consider it from the lens of "we want things to work as automagic as they do on x86".

For me, in order to get to this level of automagic, there do need to be a table of which SoC should use which page size table.

Quoted 2 lines

Nick Chan

author reviewer
#189 lore raw
Quoted 576 lines · Nick Chan

Neal Gompa 於 2024/10/22 下午5:33 寫道:

Quoted 544 lines · Ryan Roberts
Quoted 543 lines · Neal Gompa
Quoted 519 lines · Ryan Roberts
Quoted 518 lines · Eric Curtin
Quoted 515 lines · Ryan Roberts
Quoted 502 lines · Neal Gompa
Quoted 479 lines · EDT Ryan Roberts
Quoted 83 lines

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Quoted 5 lines
Quoted 2 lines

Ahh, yes, that would certainly work. I'll work it into the next version.

Could we maybe extend to have some kind of way to include a table of SoC IDs that certain modes are disabled (e.g. 64k on Apple Silicon)

64k is already disabled on Apple Silicon because mmfr0 reports that 64k is not supported.

Quoted 1 line

and preferred modes when no arg is set (16k for Apple Silicon)? That

And it's not obvious that we should hard-code a page size preference to a SoC ID. If the CPU can support multiple page sizes, it should be up to the SW stack to decide, not the SoC.

I'm guessing your desire is to have a single kernel build that will boot 16k by default on Apple Silicon and 4k by default on other systems, all without needing to modify the command line? Personally I think it's cleaner to just require setting the page size on the command line in these cases.

Quoted 2 lines

way it'd work something like this:

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

1. Table identification of 4/16/64 depending on identified SoC

So I'd prefer not to have this

Quoted 2 lines

2. Unidentified ones follow build-time default
3. karg forces a mode regardless

But keep these 2.

Since we are talking about Apple Silicon and page size, I would like to add that on the Apple Silicon SoCs I am working on, the situation is like this:

Apple A7 (s5l8960x), A8 (T7000), A8X (T7001): CPU MMU support 4K and 64K page sizes.

Apple A9 (s8000/s8003), A9X (s8001), A10 (t8010), A10X (t8011), A11 (t8015):
CPU MMU Support 16K and 64K page sizes.

However, all of them have 4K page DART IOMMUs.

Quoted 9 lines

I think it makes sense to have it, because it's not just Apple Silicon where such a preference/requirement may be necessary. Apple Silicon technically works at 4k, but is completely broken at 4k because Linux cannot do 16k IOMMU with 4k everything else, so being able to at least prefer 16k out of the box is important. And SoCs like the NVIDIA Grace Hopper platform prefer 64k over other options (though I am unaware of a gross incompatibility that effectively requires it like Apple Silicon has).

Quoted 6 lines
Quoted 4 lines

When we're trying to get to "single generic image that works everywhere", stuff like this matters and I would really like you to consider it from the lens of "we want things to work as automagic as they do on x86".

For me, in order to get to this level of automagic, there do need to be a table of which SoC should use which page size table.

OK, but it's not clear to me that this table needs to be in the kernel. Could it not be something in user space (e.g. during installation) that configures the kernel command line?

Regardless, the hard work here is getting the boot-time page size selection mechanism in place. Once that's there, follow up patches can add the desired policy. I'd rather leave it out for now to avoid anything slowing down the core work.

Thanks,
Ryan

Quoted 6 lines
Quoted 3 lines

Nick Chan

#190 lore raw
Quoted 582 lines · Ryan Roberts
Quoted 580 lines · Nick Chan

Neal Gompa 於 2024/10/22 下午5:33 寫道:

Quoted 544 lines · Ryan Roberts
Quoted 543 lines · Neal Gompa
Quoted 519 lines · Ryan Roberts
Quoted 518 lines · Eric Curtin
Quoted 515 lines · Ryan Roberts
Quoted 502 lines · Neal Gompa
Quoted 479 lines · EDT Ryan Roberts
Quoted 83 lines

Hi All,

Patch bomb incoming... This covers many subsystems, so I've included a core set of people on the full series and additionally included maintainers on relevant patches. I haven't included those maintainers on this cover letter since the numbers were far too big for it to work. But I've included a link to this cover letter on each patch, so they can hopefully find their way here. For follow up submissions I'll break it up by subsystem, but for now thought it was important to show the full picture.

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Why is having an image-per-page size problematic?
=================================================

Many traditional distros are now supporting both 4K and 64K. And this means managing 2 kernel packages, along with drivers for each. For some, it means multiple installer flavours and multiple ISOs. All of this adds up to a less-than-ideal level of complexity. Additionally, Android now supports 4K and 16K kernels. I'm told having to explicitly manage their KABI for each kernel is painful, and the extra flash space required for both kernel images and the duplicated modules has been problematic. Boot-time page size selection solves all of this.

Additionally, in starting to think about the longer term deployment story for D128 page tables, which Arm architecture now supports, a lot of the same problems need to be solved, so this work sets us up nicely for that.

So what's the down side?
========================

Well nothing's free; Various static allocations in the kernel image must be sized for the worst case (largest supported page size), so image size is in line with size of 64K compile-time image. So if you're interested in 4K or 16K, there is a slight increase to the image size. But I expect that problem goes away if you're compressing the image - its just some extra zeros. At boot-time, I expect we could free the unused static storage once we know the page size - although that would be a follow up enhancement.

And then there is performance. Since PAGE_SIZE and friends are no longer compile-time constants, we must look up their values and do arithmetic at runtime instead of compile-time. My early perf testing suggests this is inperceptible for real-world workloads, and only has small impact on microbenchmarks - more on this below.

Approach
========

The basic idea is to rid the source of any assumptions that PAGE_SIZE and friends are compile-time constant, but in a way that allows the compiler to perform the same optimizations as was previously being done if they do turn out to be compile-time constant. Where constants are required, we use limits; PAGE_SIZE_MIN and PAGE_SIZE_MAX. See commit log in patch 1 for full description of all the classes of problems to solve.

By default PAGE_SIZE_MIN=PAGE_SIZE_MAX=PAGE_SIZE. But an arch may opt-in to boot-time page size selection by defining PAGE_SIZE_MIN & PAGE_SIZE_MAX. arm64 does this if the user selects the CONFIG_ARM64_BOOT_TIME_PAGE_SIZE Kconfig, which is an alternative to selecting a compile-time page size.

When boot-time page size is active, the arch pgtable geometry macro definitions resolve to something that can be configured at boot. The arm64 implementation in this series mainly uses global, __ro_after_init variables. I've tried using alternatives patching, but that performs worse than loading from memory; I think due to code size bloat.

Status
======

When CONFIG_ARM64_BOOT_TIME_PAGE_SIZE is selected, I've only implemented enough to compile the kernel image itself with defconfig (and a few other bits and pieces). This is enough to build a kernel that can boot under QEMU or FVP. I'll happily do the rest of the work to enable all the extra drivers, but wanted to get feedback on the shape of this effort first. If anyone wants to do any testing, and has a must-have config, let me know and I'll prioritize enabling it first.

Quoted 8 lines

The series is arranged as follows:

- patch 1: Add macros required for converting non-arch code to support
boot-time page size selection
- patches 2-36: Remove PAGE_SIZE compile-time constant assumption from
all non-arch code
- patches 37-38: Some arm64 tidy ups
- patch 39: Add macros required for converting arm64 code to

support

Quoted 3 lines

boot-time page size selection
- patches 40-56: arm64 changes to support boot-time page size selection
- patch 57: Add arm64 Kconfig option to enable boot-time page

size

Quoted 376 lines

selection

Ideally, I'd like to get the basics merged (something like this series), then incrementally improve it over a handful of kernel releases until we can demonstrate that we have feature parity with the compile-time build and no performance blockers. Once at that point, ideally the compile-time build options would be removed and the code could be cleaned up further.

One of the bigger peices that I'd propose to add as a follow up, is to make va-size boot-time selectable too. That will greatly simplify LPA2 fallback handling.

Assuming people are ammenable to the rough shape, how would I go about getting the non-arch changes merged? Since they cover many subsystems, will each piece need to go independently to each relevant maintainer or could it all be merged together through the arm64 tree?

Image Size
==========

The below shows the size of a defconfig (+ xfs, squashfs, ftrace, kprobes) kernel image on disk for base (before any changes applied), compile (with changes, configured for compile-time page size) and boot (with changes, configured for boot-time page size).

You can see the that compile-16k and 64k configs are actually slightly smaller than the baselines; that's due to optimizing some buffer sizes which didn't need to depend on page size during the series. The boot-time image is ~1% bigger than the 64k compile-time image. I believe there is scope to improve this to make it
equal to compile-64k if required:

| config      | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|
|
| base-4k     |   54895 |       0 |    0.0% |
| base-16k    |   55161 |     266 |    0.5% |
| base-64k    |   56775 |    1880 |    3.4% |
| compile-4k  |   54895 |       0 |    0.0% |
| compile-16k |   55097 |     202 |    0.4% |
| compile-64k |   56391 |    1496 |    2.7% |
| boot-4K     |   57045 |    2150 |    3.9% |

And below shows the size of the image in memory at run-time, separated for text and data costs. The boot image has ~1% text cost; most likely due to the fact that PAGE_SIZE and friends are not compile-time constants so need instructions to load the values and do arithmetic. I believe we could eventually get the data cost to match the cost for the compile image for the chosen page size by freeing
the ends of the static buffers not needed for the selected page size:

|             |    text |    text |    text |    data |    data |    data |
|
| config      | size/KB | diff/KB |  diff/% | size/KB | diff/KB |  diff/% |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |   20561 |       0 |    0.0% |   14314 |       0 |    0.0% |
| base-16k    |   20439 |    -122 |   -0.6% |   14625 |     311 |    2.2% |
| base-64k    |   20435 |    -126 |   -0.6% |   15673 |    1359 |    9.5% |
| compile-4k  |   20565 |       4 |    0.0% |   14315 |       1 |    0.0% |
| compile-16k |   20443 |    -118 |   -0.6% |   14517 |     204 |    1.4% |
| compile-64k |   20439 |    -122 |   -0.6% |   15134 |     820 |    5.7% |
| boot-4K     |   20811 |     250 |    1.2% |   15287 |     973 |    6.8% |

Functional Testing
==================

I've build-tested defconfig for all arches supported by tuxmake (which is most) without issue.

I've boot-tested arm64 with CONFIG_ARM64_BOOT_TIME_PAGE_SIZE for all page sizes and a few va-sizes, and additionally have run all the mm-selftests, with no regressions observed vs the equivalent compile-time page size build (although the mm-selftests have a few existing failures when run against 16K and 64K kernels - those should really be investigated and fixed independently).

Test coverage is lacking for many of the drivers that I've touched, but in many cases, I'm hoping the changes are simple enough that review might suffice?

Performance Testing
===================

I've run some limited performance benchmarks:

First, a real-world benchmark that causes a lot of page table manipulation (and therefore we would expect to see regression here if we are going to see it anywhere); kernel compilation. It barely registers a change. Values are times,
so smaller is better. All relative to base-4k:

|             |    kern |    kern |    user |    user |    real |    real |
|
| config      |    mean |   stdev |    mean |   stdev |    mean |   stdev |
|
|-------------|---------|---------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.1% |    0.0% |    0.3% |    0.0% |    0.3% |
| compile-4k  |   -0.2% |    1.1% |   -0.2% |    0.3% |   -0.1% |    0.3% |
| boot-4k     |    0.1% |    1.0% |   -0.3% |    0.2% |   -0.2% |    0.2% |

The Speedometer JavaScript benchmark also shows no change. Values are runs per
min, so bigger is better. All relative to base-4k:

| config      |    mean |   stdev |
|
|-------------|---------|---------|
|
| base-4k     |    0.0% |    0.8% |
| compile-4k  |    0.4% |    0.8% |
| boot-4k     |    0.0% |    0.9% |

Finally, I've run some microbenchmarks known to stress page table manipulations (originally from David Hildenbrand). The fork test maps/allocs 1G of anon memory, then measures the cost of fork(). The munmap test maps/allocs 1G of anon memory then measures the cost of munmap()ing it. The fork test is known to be extremely sensitive to any changes that cause instructions to be aligned differently in cachelines. When using this test for other changes, I've seen double digit regressions for the slightest thing, so 12% regression on this test is actually fairly good. This likely represents the extreme worst case for regressions that will be observed across other microbenchmarks (famous last
words). Values are times, so smaller is better. All relative to base-4k:

|             |    fork |    fork |  munmap |  munmap |
|
| config      |    mean |   stdev |   stdev |   stdev |
|
|-------------|---------|---------|---------|---------|
|
| base-4k     |    0.0% |    1.3% |    0.0% |    0.3% |
| compile-4k  |    0.1% |    1.3% |   -0.9% |    0.1% |
| boot-4k     |   12.8% |    1.2% |    3.8% |    1.0% |

NOTE: The series applies on top of v6.11.

Thanks,
Ryan

Ryan Roberts (57):
mm: Add macros ahead of supporting boot-time page size selection
vmlinux: Align to PAGE_SIZE_MAX
mm/memcontrol: Fix seq_buf size to save memory when PAGE_SIZE is large mm/page_alloc: Make page_frag_cache boot-time page size compatible
mm: Avoid split pmd ptl if pmd level is run-time folded
mm: Remove PAGE_SIZE compile-time constant assumption
fs: Introduce MAX_BUF_PER_PAGE_SIZE_MAX for array sizing
fs: Remove PAGE_SIZE compile-time constant assumption fs/nfs: Remove PAGE_SIZE compile-time constant assumption fs/ext4: Remove PAGE_SIZE compile-time constant assumption
fork: Permit boot-time THREAD_SIZE determination
cgroup: Remove PAGE_SIZE compile-time constant assumption
bpf: Remove PAGE_SIZE compile-time constant assumption pm/hibernate: Remove PAGE_SIZE compile-time constant assumption
stackdepot: Remove PAGE_SIZE compile-time constant assumption
perf: Remove PAGE_SIZE compile-time constant assumption
kvm: Remove PAGE_SIZE compile-time constant assumption
trace: Remove PAGE_SIZE compile-time constant assumption
crash: Remove PAGE_SIZE compile-time constant assumption
crypto: Remove PAGE_SIZE compile-time constant assumption
sunrpc: Remove PAGE_SIZE compile-time constant assumption
sound: Remove PAGE_SIZE compile-time constant assumption
net: Remove PAGE_SIZE compile-time constant assumption
net: fec: Remove PAGE_SIZE compile-time constant assumption
net: marvell: Remove PAGE_SIZE compile-time constant assumption
net: hns3: Remove PAGE_SIZE compile-time constant assumption
net: e1000: Remove PAGE_SIZE compile-time constant assumption
net: igbvf: Remove PAGE_SIZE compile-time constant assumption
net: igb: Remove PAGE_SIZE compile-time constant assumption drivers/base: Remove PAGE_SIZE compile-time constant assumption
edac: Remove PAGE_SIZE compile-time constant assumption
optee: Remove PAGE_SIZE compile-time constant assumption
random: Remove PAGE_SIZE compile-time constant assumption
sata_sil24: Remove PAGE_SIZE compile-time constant assumption
virtio: Remove PAGE_SIZE compile-time constant assumption
xen: Remove PAGE_SIZE compile-time constant assumption
arm64: Fix macros to work in C code in addition to the linker script
arm64: Track early pgtable allocation limit
arm64: Introduce macros required for boot-time page selection
arm64: Refactor early pgtable size calculation macros
arm64: Pass desired page size on command line
arm64: Divorce early init from PAGE_SIZE
arm64: Clean up simple cases of CONFIG_ARM64_*K_PAGES
arm64: Align sections to PAGE_SIZE_MAX
arm64: Rework trampoline rodata mapping
arm64: Generalize fixmap for boot-time page size
arm64: Statically allocate and align for worst-case page size
arm64: Convert switch to if for non-const comparison values
arm64: Convert BUILD_BUG_ON to VM_BUG_ON
arm64: Remove PAGE_SZ asm-offset
arm64: Introduce cpu features for page sizes
arm64: Remove PAGE_SIZE from assembly code
arm64: Runtime-fold pmd level
arm64: Support runtime folding in idmap_kpti_install_ng_mappings
arm64: TRAMP_VALIAS is no longer compile-time constant
arm64: Determine THREAD_SIZE at boot-time
arm64: Enable boot-time page size selection

 arch/alpha/include/asm/page.h                 |   1 +
 arch/arc/include/asm/page.h                   |   1 +
 arch/arm/include/asm/page.h                   |   1 +
 arch/arm64/Kconfig                            |  26 ++-
 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/kernel-pgtable.h       | 150 +++++++++----
 arch/arm64/include/asm/kvm_arm.h              |  21 +-
 arch/arm64/include/asm/kvm_hyp.h              |  11 +
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/memory.h               |  62 ++++--
 arch/arm64/include/asm/page-def.h             |   3 +-
 arch/arm64/include/asm/pgalloc.h              |  16 +-
 arch/arm64/include/asm/pgtable-geometry.h     |  46 ++++
 arch/arm64/include/asm/pgtable-hwdef.h        |  28 ++-
 arch/arm64/include/asm/pgtable-prot.h         |   2 +-
 arch/arm64/include/asm/pgtable.h              | 133 +++++++++---
 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/sysreg.h               |  54 +++--
 arch/arm64/include/asm/tlb.h                  |   3 +
 arch/arm64/kernel/asm-offsets.c               |   4 +-
 arch/arm64/kernel/cpufeature.c                |  93 ++++++--
 arch/arm64/kernel/efi.c                       |   2 +-
 arch/arm64/kernel/entry.S                     |  60 +++++-
 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         |  68 +++++-
 arch/arm64/kernel/pi/map_kernel.c             | 165 ++++++++++----
 arch/arm64/kernel/pi/map_range.c              | 201 ++++++++++++++++--
 arch/arm64/kernel/pi/pi.h                     |  63 +++++-
 arch/arm64/kernel/relocate_kernel.S           |  10 +-
 arch/arm64/kernel/vdso-wrap.S                 |   4 +-
 arch/arm64/kernel/vdso.c                      |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S             |   4 +-
 arch/arm64/kernel/vdso32-wrap.S               |   4 +-
 arch/arm64/kernel/vdso32/vdso.lds.S           |   4 +-
 arch/arm64/kernel/vmlinux.lds.S               |  48 +++--
 arch/arm64/kvm/arm.c                          |  10 +
 arch/arm64/kvm/hyp/nvhe/Makefile              |   1 +
 arch/arm64/kvm/hyp/nvhe/host.S                |  10 +-
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   4 +-
 arch/arm64/kvm/hyp/nvhe/pgtable-geometry.c    |  16 ++
 arch/arm64/kvm/mmu.c                          |  39 ++--
 arch/arm64/lib/clear_page.S                   |   7 +-
 arch/arm64/lib/copy_page.S                    |  33 ++-
 arch/arm64/lib/mte.S                          |  27 ++-
 arch/arm64/mm/Makefile                        |   1 +
 arch/arm64/mm/fixmap.c                        |  38 ++--
 arch/arm64/mm/hugetlbpage.c                   |  40 +---
 arch/arm64/mm/init.c                          |  26 +--
 arch/arm64/mm/kasan_init.c                    |   8 +-
 arch/arm64/mm/mmu.c                           |  53 +++--
 arch/arm64/mm/pgd.c                           |  12 +-
 arch/arm64/mm/pgtable-geometry.c              |  24 +++
 arch/arm64/mm/proc.S                          | 128 ++++++++---
 arch/arm64/mm/ptdump.c                        |   3 +-
 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 +
 crypto/lskcipher.c                            |   4 +-
 drivers/ata/sata_sil24.c                      |  46 ++--
 drivers/base/node.c                           |   6 +-
 drivers/base/topology.c                       |  32 +--
 drivers/block/virtio_blk.c                    |   2 +-
 drivers/char/random.c                         |   4 +-
 drivers/edac/edac_mc.h                        |  13 +-
 drivers/firmware/efi/libstub/arm64.c          |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |   2 +-
 drivers/mtd/mtdswap.c                         |   4 +-
 drivers/net/ethernet/freescale/fec.h          |   3 +-
 drivers/net/ethernet/freescale/fec_main.c     |   5 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.h   |   4 +-
 drivers/net/ethernet/intel/e1000/e1000_main.c |   6 +-
 drivers/net/ethernet/intel/igb/igb.h          |  25 +--
 drivers/net/ethernet/intel/igb/igb_main.c     | 149 +++++++------
 drivers/net/ethernet/intel/igbvf/netdev.c     |   6 +-
 drivers/net/ethernet/marvell/mvneta.c         |   9 +-
 drivers/net/ethernet/marvell/sky2.h           |   2 +-
 drivers/tee/optee/call.c                      |   7 +-
 drivers/tee/optee/smc_abi.c                   |   2 +-
 drivers/virtio/virtio_balloon.c               |  10 +-
 drivers/xen/balloon.c                         |  11 +-
 drivers/xen/biomerge.c                        |  12 +-
 drivers/xen/privcmd.c                         |   2 +-
 drivers/xen/xenbus/xenbus_client.c            |   5 +-
 drivers/xen/xlate_mmu.c                       |   6 +-
 fs/binfmt_elf.c                               |  11 +-
 fs/buffer.c                                   |   2 +-
 fs/coredump.c                                 |   8 +-
 fs/ext4/ext4.h                                |  36 ++--
 fs/ext4/move_extent.c                         |   2 +-
 fs/ext4/readpage.c                            |   2 +-
 fs/fat/dir.c                                  |   4 +-
 fs/fat/fatent.c                               |   4 +-
 fs/nfs/nfs42proc.c                            |   2 +-
 fs/nfs/nfs42xattr.c                           |   2 +-
 fs/nfs/nfs4proc.c                             |   2 +-
 include/asm-generic/pgtable-geometry.h        |  71 +++++++
 include/asm-generic/vmlinux.lds.h             |  38 ++--
 include/linux/buffer_head.h                   |   1 +
 include/linux/cpumask.h                       |   5 +
 include/linux/linkage.h                       |   4 +-
 include/linux/mm.h                            |  17 +-
 include/linux/mm_types.h                      |  15 +-
 include/linux/mm_types_task.h                 |   2 +-
 include/linux/mmzone.h                        |   3 +-
 include/linux/netlink.h                       |   6 +-
 include/linux/percpu-defs.h                   |   4 +-
 include/linux/perf_event.h                    |   2 +-
 include/linux/sched.h                         |   4 +-
 include/linux/slab.h                          |   7 +-
 include/linux/stackdepot.h                    |   6 +-
 include/linux/sunrpc/svc.h                    |   8 +-
 include/linux/sunrpc/svc_rdma.h               |   4 +-
 include/linux/sunrpc/svcsock.h                |   2 +-
 include/linux/swap.h                          |  17 +-
 include/linux/swapops.h                       |   6 +-
 include/linux/thread_info.h                   |  10 +-
 include/xen/page.h                            |   2 +
 init/main.c                                   |   7 +-
 kernel/bpf/core.c                             |   9 +-
 kernel/bpf/ringbuf.c                          |  54 ++---
 kernel/cgroup/cgroup.c                        |   8 +-
 kernel/crash_core.c                           |   2 +-
 kernel/events/core.c                          |   2 +-
 kernel/fork.c                                 |  71 +++----
 kernel/power/power.h                          |   2 +-
 kernel/power/snapshot.c                       |   2 +-
 kernel/power/swap.c                           | 129 +++++++++--
 kernel/trace/fgraph.c                         |   2 +-
 kernel/trace/trace.c                          |   2 +-
 lib/stackdepot.c                              |   6 +-
 mm/kasan/report.c                             |   3 +-
 mm/memcontrol.c                               |  11 +-
 mm/memory.c                                   |   4 +-
 mm/mmap.c                                     |   2 +-
 mm/page-writeback.c                           |   2 +-
 mm/page_alloc.c                               |  31 +--
 mm/slub.c                                     |   2 +-
 mm/sparse.c                                   |   2 +-
 mm/swapfile.c                                 |   2 +-
 mm/vmalloc.c                                  |   7 +-
 net/9p/trans_virtio.c                         |   4 +-
 net/core/hotdata.c                            |   4 +-
 net/core/skbuff.c                             |   4 +-
 net/core/sysctl_net_core.c                    |   2 +-
 net/sunrpc/cache.c                            |   3 +-
 net/unix/af_unix.c                            |   2 +-
 sound/soc/soc-utils.c                         |   4 +-
 virt/kvm/kvm_main.c                           |   2 +-
 172 files changed, 2185 insertions(+), 951 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
Quoted 2 lines

--
2.43.0

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

Quoted 6 lines

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Quoted 5 lines
Quoted 2 lines

Ahh, yes, that would certainly work. I'll work it into the next version.

Could we maybe extend to have some kind of way to include a table of SoC IDs that certain modes are disabled (e.g. 64k on Apple Silicon)

64k is already disabled on Apple Silicon because mmfr0 reports that 64k is not supported.

Quoted 1 line

and preferred modes when no arg is set (16k for Apple Silicon)? That

And it's not obvious that we should hard-code a page size preference to a SoC ID. If the CPU can support multiple page sizes, it should be up to the SW stack to decide, not the SoC.

I'm guessing your desire is to have a single kernel build that will boot 16k by default on Apple Silicon and 4k by default on other systems, all without needing to modify the command line? Personally I think it's cleaner to just require setting the page size on the command line in these cases.

Quoted 2 lines

way it'd work something like this:

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

1. Table identification of 4/16/64 depending on identified SoC

So I'd prefer not to have this

Quoted 2 lines

2. Unidentified ones follow build-time default
3. karg forces a mode regardless

But keep these 2.

Since we are talking about Apple Silicon and page size, I would like to add that on the Apple Silicon SoCs I am working on, the situation is like this:

Apple A7 (s5l8960x), A8 (T7000), A8X (T7001): CPU MMU support 4K and 64K page sizes.

Apple A9 (s8000/s8003), A9X (s8001), A10 (t8010), A10X (t8011), A11 (t8015):
CPU MMU Support 16K and 64K page sizes.

However, all of them have 4K page DART IOMMUs.

Quoted 13 lines

I think it makes sense to have it, because it's not just Apple Silicon where such a preference/requirement may be necessary. Apple Silicon technically works at 4k, but is completely broken at 4k because Linux cannot do 16k IOMMU with 4k everything else, so being able to at least prefer 16k out of the box is important. And SoCs like the NVIDIA Grace Hopper platform prefer 64k over other options (though I am unaware of a gross incompatibility that effectively requires it like Apple Silicon has).

When we're trying to get to "single generic image that works everywhere", stuff like this matters and I would really like you to consider it from the lens of "we want things to work as automagic as they do on x86".

Quoted 7 lines
Quoted 2 lines

For me, in order to get to this level of automagic, there do need to be a table of which SoC should use which page size table.

OK, but it's not clear to me that this table needs to be in the kernel. Could it not be something in user space (e.g. during installation) that configures the kernel command line?

This is not compatible with using things like ISOs with UEFI+ACPI enabled desktop/server systems. We need to be able to safely, automatically, and correctly boot up and support hardware. The only place to do that early enough is in the kernel. But this can wait until the core stuff is in.

Quoted 5 lines

Regardless, the hard work here is getting the boot-time page size selection mechanism in place. Once that's there, follow up patches can add the desired policy. I'd rather leave it out for now to avoid anything slowing down the core work.

Sure, this can be done afterward.

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
author reviewer
#191 lore raw

On 22/10/2024 18:30, Neal Gompa wrote:

[...]

Quoted 101 lines
Quoted 93 lines
Quoted 93 lines
Quoted 60 lines
Quoted 60 lines
Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 25 lines
Quoted 10 lines

This is a generally very exciting patch set! I'm looking forward to seeing it land so I can take advantage of it for Fedora ARM and Fedora Asahi Remix.

That said, I have a couple of questions:

* Going forward, how would we handle drivers/modules that require a particular page size? For example, the Apple Silicon IOMMU driver code requires the kernel to operate in 16k page size mode, and it would need to be disabled in other page sizes.

I think these drivers would want to check PAGE_SIZE at probe time and fail if an unsupported page size is in use. Do you see any issue with that?

Quoted 2 lines

* How would we handle an invalid selection at boot?

What do you mean by invalid here? The current policy validates that the requested page size is supported by the HW by checking mmfr0. If no page size is passed on the command line, or the passed value is not supported by the HW, then the we default to the largest page size supported by the HW (so for Apple Silicon that would be 16k since the HW doesn't support 64k). Although I think it may be better to change that policy to use the smallest page size in this case; 4k is the safer bet for compat and will waste much less memory than 64k.

Quoted 7 lines
Quoted 2 lines

Can we program in a
fallback when the "wrong" mode is selected for a chip or something similar?

Do you mean effectively add a machanism to force 16k if the detected HW is Apple Silicon? The trouble is that we need to select the page size, very early in boot, before start_kernel() is called, so we really only have generic arch code and the command line with which to make the decision.

Yes... I think a build-time CONFIG for default page size, which can be overridden by a karg makes sense... Even on platforms like Apple Silicon you may want to test very specific things in 4k by overriding with a karg.

Quoted 5 lines
Quoted 2 lines

Ahh, yes, that would certainly work. I'll work it into the next version.

Could we maybe extend to have some kind of way to include a table of SoC IDs that certain modes are disabled (e.g. 64k on Apple Silicon)

64k is already disabled on Apple Silicon because mmfr0 reports that 64k is not supported.

Quoted 1 line

and preferred modes when no arg is set (16k for Apple Silicon)? That

And it's not obvious that we should hard-code a page size preference to a SoC ID. If the CPU can support multiple page sizes, it should be up to the SW stack to decide, not the SoC.

I'm guessing your desire is to have a single kernel build that will boot 16k by default on Apple Silicon and 4k by default on other systems, all without needing to modify the command line? Personally I think it's cleaner to just require setting the page size on the command line in these cases.

Quoted 2 lines

way it'd work something like this:

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

1. Table identification of 4/16/64 depending on identified SoC

So I'd prefer not to have this

Quoted 2 lines

2. Unidentified ones follow build-time default
3. karg forces a mode regardless

But keep these 2.

Since we are talking about Apple Silicon and page size, I would like to add that on the Apple Silicon SoCs I am working on, the situation is like this:

Apple A7 (s5l8960x), A8 (T7000), A8X (T7001): CPU MMU support 4K and 64K page sizes.

Apple A9 (s8000/s8003), A9X (s8001), A10 (t8010), A10X (t8011), A11 (t8015):
CPU MMU Support 16K and 64K page sizes.

However, all of them have 4K page DART IOMMUs.

Quoted 13 lines

I think it makes sense to have it, because it's not just Apple Silicon where such a preference/requirement may be necessary. Apple Silicon technically works at 4k, but is completely broken at 4k because Linux cannot do 16k IOMMU with 4k everything else, so being able to at least prefer 16k out of the box is important. And SoCs like the NVIDIA Grace Hopper platform prefer 64k over other options (though I am unaware of a gross incompatibility that effectively requires it like Apple Silicon has).

When we're trying to get to "single generic image that works everywhere", stuff like this matters and I would really like you to consider it from the lens of "we want things to work as automagic as they do on x86".

Quoted 7 lines
Quoted 2 lines

For me, in order to get to this level of automagic, there do need to be a table of which SoC should use which page size table.

OK, but it's not clear to me that this table needs to be in the kernel. Could it not be something in user space (e.g. during installation) that configures the kernel command line?

Quoted 5 lines

This is not compatible with using things like ISOs with UEFI+ACPI enabled desktop/server systems. We need to be able to safely, automatically, and correctly boot up and support hardware. The only place to do that early enough is in the kernel. But this can wait until the core stuff is in.

OK got it.

Quoted 8 lines
Quoted 6 lines

Regardless, the hard work here is getting the boot-time page size selection mechanism in place. Once that's there, follow up patches can add the desired policy. I'd rather leave it out for now to avoid anything slowing down the core work.

Sure, this can be done afterward.

Thanks! I understand the problem a bit better now. I'm sure we can find a solution once we have landed the core mechanism.

Thanks,
Ryan

maintainer
#192 lore raw
In reply to Ryan Roberts

Hi Ryan,

Quoted 7 lines · Ryan Roberts

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

That's great work, something I wasn't expecting to even build, let alone run ;). I only looked briefly through the patches, there's probably room for optimisation of micro-benchmarks like fork(), maybe using something like runtime constants. The advantage for deployment and easy testing of different configurations is pretty clear (distros mainly, not sure how relevant it is for Android if apps can't move beyond 4K pages).

However, as a maintainer, my main concern is having to chase build failures in obscure drivers that have not been tested/developed on arm64. If people primarily test on x86, they wouldn't notice that PAGE_SIZE/PAGE_SHIFT are no longer constants. Not looking forward to trying to sort out allmodconfig builds every kernel release, especially if they turn up in subsystems I have no clue about (like most stuff outside arch/arm64).

So, first of all, I'd like to understand the overall maintainability impact better. I assume you tested mostly defconfig. If you run an allmodconfig build with make -k, how many build failures do you get with this patchset? Similarly for some distro configs.

Do we have any better way to detect this other than actual compilation on arm64? Can we hack something around COMPILE_TEST like redefine PAGE_SIZE (for modules only) to a variable so that we have a better chance of detecting build failures when modules are only tested on other architectures?

At the moment, I'm not entirely convinced of the benefits vs. long term maintainability. Even if we don't end up merging the dynamic PAGE_SIZE support, parts of this series are needed for supporting 128-bit ptes on arm64, hopefully dynamically as well.

Thanks.

Signature · 1 line
Catalin
author reviewer
#193 lore raw
Quoted 11 lines · Catalin Marinas

Hi Ryan,

Quoted 7 lines · Ryan Roberts

This RFC series implements support for boot-time page size selection within the arm64 kernel. arm64 supports 3 base page sizes (4K, 16K, 64K), but to date, page size has been selected at compile-time, meaning the size is baked into a given kernel image. As use of larger-than-4K page sizes become more prevalent this starts to present a problem for distributions. Boot-time page size selection enables the creation of a single kernel image, which can be told which page size to use on the kernel command line.

Quoted 2 lines

That's great work, something I wasn't expecting to even build, let alone run ;).

Cheers!

Quoted 3 lines

I only looked briefly through the patches, there's probably room for optimisation of micro-benchmarks like fork(), maybe using something like runtime constants.

Yes I suspect there is room for some optimization. Although note I already tried using alternatives patching but for the fork() microbenchmark this performed worse than the approach I ended up taking of just loading a global variable. I think this was likely due to code layout changes due to all the extra branches/nops - fork has been very sensitive to code layout changes in the past.

Quoted 11 lines

The advantage for deployment and easy testing of
different configurations is pretty clear (distros mainly, not sure how relevant it is for Android if apps can't move beyond 4K pages).

However, as a maintainer, my main concern is having to chase build failures in obscure drivers that have not been tested/developed on arm64. If people primarily test on x86, they wouldn't notice that PAGE_SIZE/PAGE_SHIFT are no longer constants. Not looking forward to trying to sort out allmodconfig builds every kernel release, especially if they turn up in subsystems I have no clue about (like most stuff outside arch/arm64).

Yes, I understand that concern.

Quoted 5 lines

So, first of all, I'd like to understand the overall maintainability impact better. I assume you tested mostly defconfig. If you run an allmodconfig build with make -k, how many build failures do you get with this patchset? Similarly for some distro configs.

I've roughly done:

    make alldefconfig &&
        ./scripts/config --enable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE &&
	make -s -j`nproc` -k &> allmodconfig.log

Then parsed the log for issues. Unfortunately the errors are very chatty and it is difficult to perfectly extract stats.

If I search for r'(\S+\.[ch]):.*error:', that is optimistic because PAGE_SIZE being non-const gets the ultimate blame for most things, but I'm interested in the call sites. Number of affected files using this approach: 111.

If I just blindly search for all files, r'(\S+\.[ch]):', that is pessimistic because when the issue is in a header, the full include chain is spat out. Number of affected files using this approach: 1807.

If I just search for C files, r'(\S+\.[c]):', (all issues in headers terminate in a C file) that is also pessimistic because the same single header issue is reported for every C file it is included in. Number of affected files using this
approach: 1369.

In the end, I decided to go for r'(\S+\.[ch]):.*(error|note):', which is any files described as having an error or being the callsite of the thing with the error. I think this is likely most accurate from eyeballing the log:

|            |     C&H files | percentage of |
| directory  |      w/ error | all C&H files |
|------------|---------------|---------------|
| arch/arm64 |             7 |          1.3% |
| drivers    |           127 |          0.4% |
| fs         |            25 |          1.1% |
| include    |            27 |          0.4% |
| init       |             1 |          8.3% |
| kernel     |             7 |          1.3% |
| lib        |             1 |          0.2% |
| mm         |             6 |          3.2% |
| net        |             7 |          0.4% |
| security   |             2 |          0.8% |
| sound      |            21 |          0.8% |
|------------|---------------|---------------|
| TOTAL      |           231 |          0.4% |
|------------|---------------|---------------|

I'm not sure how best to evaluate if this is a large or small number though! For comparison, the RFC modified 172 files.

Quoted 6 lines

Do we have any better way to detect this other than actual compilation on arm64? Can we hack something around COMPILE_TEST like redefine PAGE_SIZE (for modules only) to a variable so that we have a better chance of detecting build failures when modules are only tested on other architectures?

I can certainly look into this. But if the concern is that drivers are not being compiled against arm64, what is the likelyhood of them being compiled against COMPILE_TEST?

Quoted 5 lines

At the moment, I'm not entirely convinced of the benefits vs. long term maintainability. Even if we don't end up merging the dynamic PAGE_SIZE support, parts of this series are needed for supporting 128-bit ptes on arm64, hopefully dynamically as well.

Agreed.

Thanks,
Ryan

Quoted 3 lines

Thanks.

maintainer
#194 lore raw
Quoted 11 lines · Ryan Roberts
Quoted 4 lines · Catalin Marinas

So, first of all, I'd like to understand the overall maintainability impact better. I assume you tested mostly defconfig. If you run an allmodconfig build with make -k, how many build failures do you get with this patchset? Similarly for some distro configs.

I've roughly done:

    make alldefconfig &&
        ./scripts/config --enable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE &&
	make -s -j`nproc` -k &> allmodconfig.log

Is it alldefconfig or allmodconfig? The former has a lot less symbols enabled than even defconfig (fairly close to allnoconfig actually):

$ make defconfig
$ grep -v "^#\|^$" .config | wc -l
4449
$ make alldefconfig
$ grep -v "^#\|^$" .config | wc -l
713
$ make allmodconfig
$ grep -v "^#\|^$" .config | wc -l
14401
Quoted 3 lines

In the end, I decided to go for r'(\S+\.[ch]):.*(error|note):', which is any files described as having an error or being the callsite of the thing with the error. I think this is likely most accurate from eyeballing the log:

I think that's good enough to give us a rough idea.

Quoted 9 lines
|            |     C&H files | percentage of |
| directory  |      w/ error | all C&H files |
|------------|---------------|---------------|
| arch/arm64 |             7 |          1.3% |
| drivers    |           127 |          0.4% |
| fs         |            25 |          1.1% |
| include    |            27 |          0.4% |
| init       |             1 |          8.3% |
| kernel     |             7 |          1.3% |
Quoted 8 lines
| lib        |             1 |          0.2% |
| mm         |             6 |          3.2% |
| net        |             7 |          0.4% |
| security   |             2 |          0.8% |
| sound      |            21 |          0.8% |
|------------|---------------|---------------|
| TOTAL      |           231 |          0.4% |
|------------|---------------|---------------|

This doesn't look that bad _if_ you actually built most modules. But if it was alldefconfig, you likely missed the majority of modules.

Quoted 9 lines
Quoted 5 lines

Do we have any better way to detect this other than actual compilation on arm64? Can we hack something around COMPILE_TEST like redefine PAGE_SIZE (for modules only) to a variable so that we have a better chance of detecting build failures when modules are only tested on other architectures?

I can certainly look into this. But if the concern is that drivers are not being compiled against arm64, what is the likelyhood of them being compiled against COMPILE_TEST?

Hopefully some CIs out there catching them. Well, if we are to fix them anyway, we might as well eventually force a non-const PAGE_SIZE generically even if it returns a constant.

I'm building allmod now with something like below (and some hacks in arch and core code to use STATIC_PAGE_* as I did not apply your patches). alldefconfig passes with my hacks but, as you can see, the non-const PAGE_SIZE kicks in only if MODULE is defined. So, not an accurate test, just to get a feel of the modules problem.

----------8<---------------------------

arch/arm64/include/asm/page-def.h +13 -1
index 792e9fe881dc..71a761f86b15 100644
@@ -12,7 +12,19 @@
 
 /* PAGE_SHIFT determines the page size */
 #define PAGE_SHIFT		CONFIG_PAGE_SHIFT
-#define PAGE_SIZE		(_AC(1, UL) << PAGE_SHIFT)
+#define STATIC_PAGE_SIZE	(_AC(1, UL) << PAGE_SHIFT)
+#define STATIC_PAGE_MASK	(~(STATIC_PAGE_SIZE-1))
+
+#if !defined(MODULE) || defined(__ASSEMBLY__)
+#define PAGE_SIZE		STATIC_PAGE_SIZE
+#else
+static inline unsigned long __runtime_page_size(void)
+{
+	return 1UL << PAGE_SHIFT;
+}
+#define PAGE_SIZE		(__runtime_page_size())
+#endif
+
 #define PAGE_MASK		(~(PAGE_SIZE-1))
 
 #endif /* __ASM_PAGE_DEF_H */

----------8<---------------------------

Signature · 1 line
Catalin
author reviewer
#195 lore raw
Quoted 7 lines · Catalin Marinas
Quoted 6 lines · Ryan Roberts
Quoted 4 lines · Catalin Marinas

So, first of all, I'd like to understand the overall maintainability impact better. I assume you tested mostly defconfig. If you run an allmodconfig build with make -k, how many build failures do you get with this patchset? Similarly for some distro configs.

Quoted 8 lines
Quoted 5 lines

I've roughly done:

    make alldefconfig &&
        ./scripts/config --enable CONFIG_ARM64_BOOT_TIME_PAGE_SIZE &&
	make -s -j`nproc` -k &> allmodconfig.log

Is it alldefconfig or allmodconfig? The former has a lot less symbols enabled than even defconfig (fairly close to allnoconfig actually):

Eek, that was a typo when I wrote the email... I built allmodconfig - the big one.

Quoted 37 lines
$ make defconfig
$ grep -v "^#\|^$" .config | wc -l
4449
$ make alldefconfig
$ grep -v "^#\|^$" .config | wc -l
713
$ make allmodconfig
$ grep -v "^#\|^$" .config | wc -l
14401
Quoted 3 lines

In the end, I decided to go for r'(\S+\.[ch]):.*(error|note):', which is any files described as having an error or being the callsite of the thing with the error. I think this is likely most accurate from eyeballing the log:

I think that's good enough to give us a rough idea.

Quoted 9 lines
|            |     C&H files | percentage of |
| directory  |      w/ error | all C&H files |
|------------|---------------|---------------|
| arch/arm64 |             7 |          1.3% |
| drivers    |           127 |          0.4% |
| fs         |            25 |          1.1% |
| include    |            27 |          0.4% |
| init       |             1 |          8.3% |
| kernel     |             7 |          1.3% |
Quoted 8 lines
| lib        |             1 |          0.2% |
| mm         |             6 |          3.2% |
| net        |             7 |          0.4% |
| security   |             2 |          0.8% |
| sound      |            21 |          0.8% |
|------------|---------------|---------------|
| TOTAL      |           231 |          0.4% |
|------------|---------------|---------------|
Quoted 2 lines

This doesn't look that bad _if_ you actually built most modules. But if it was alldefconfig, you likely missed the majority of modules.

I definitely built allmodconfig, so I guess "this doesn't look bad" :)

Quoted 15 lines
Quoted 10 lines
Quoted 6 lines

Do we have any better way to detect this other than actual compilation on arm64? Can we hack something around COMPILE_TEST like redefine PAGE_SIZE (for modules only) to a variable so that we have a better chance of detecting build failures when modules are only tested on other architectures?

I can certainly look into this. But if the concern is that drivers are not being compiled against arm64, what is the likelyhood of them being compiled against COMPILE_TEST?

Hopefully some CIs out there catching them. Well, if we are to fix them anyway, we might as well eventually force a non-const PAGE_SIZE generically even if it returns a constant.

Quoted 5 lines

I'm building allmod now with something like below (and some hacks in arch and core code to use STATIC_PAGE_* as I did not apply your patches). alldefconfig passes with my hacks but, as you can see, the non-const PAGE_SIZE kicks in only if MODULE is defined. So, not an accurate test, just to get a feel of the modules problem.

Nice. I guess that's pretty much the change we would add for x86 with COMPILE_TEST.

Quoted 29 lines

----------8<---------------------------

arch/arm64/include/asm/page-def.h +13 -2
index 792e9fe881dc..71a761f86b15 100644
@@ -12,7 +12,19 @@
 
 /* PAGE_SHIFT determines the page size */
 #define PAGE_SHIFT		CONFIG_PAGE_SHIFT
-#define PAGE_SIZE		(_AC(1, UL) << PAGE_SHIFT)
+#define STATIC_PAGE_SIZE	(_AC(1, UL) << PAGE_SHIFT)
+#define STATIC_PAGE_MASK	(~(STATIC_PAGE_SIZE-1))
+
+#if !defined(MODULE) || defined(__ASSEMBLY__)
+#define PAGE_SIZE		STATIC_PAGE_SIZE
+#else
+static inline unsigned long __runtime_page_size(void)
+{
+	return 1UL << PAGE_SHIFT;
+}
+#define PAGE_SIZE		(__runtime_page_size())
+#endif
+
 #define PAGE_MASK		(~(PAGE_SIZE-1))
 
 #endif /* __ASM_PAGE_DEF_H */
----------8<---------------------------