Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Curtin <ericcurtin17@gmail.com>
To: linux-arm-kernel@lists.infradead.org
Cc: Ryan Roberts <ryan.roberts@arm.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>
Subject: [RFC PATCH v2 31/43] arm64: Support runtime folding in idmap_kpti_install_ng_mappings
Date: Tue,  1 Dec 2026 01:52:03 +0000	[thread overview]
Message-ID: <20261201015240.1017259-32-ericcurtin17@gmail.com> (raw)
In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com>

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

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

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

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

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


  parent reply	other threads:[~2026-12-01  1:54 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  1:51 [RFC PATCH v2 00/43] Boot-time page size selection for arm64 (core) Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 01/43] mm: Add macros ahead of supporting boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 02/43] vmlinux.lds: Align page-aligned sections to PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 03/43] mm: page_frag: Use PAGE_SIZE_MIN/MAX limits in page_frag_cache Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 05/43] mm: Avoid split pmd ptl if pmd level is run-time folded Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 06/43] mm/vmstat: Use THREAD_SIZE_MAX for the KSTACK_* counters Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 08/43] cgroup: Use DEFINE_GLOBAL_PAGE_SIZE_VAR for the kernfs_ops tables Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 10/43] PM: hibernate: Do not size swap structures to exactly PAGE_SIZE Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 12/43] KVM: Check struct kvm_run against PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 13/43] crash: Check crash_notes size " Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 14/43] arm64: Make INIT_DIR_SIZE and INIT_IDMAP_DIR_SIZE usable from C Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 15/43] arm64: pi: Pass the early pgtable allocation limit to map_range() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 16/43] arm64: Introduce macros required for boot-time page size selection Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 17/43] arm64: Parameterise early pgtable size macros by page shift Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 18/43] arm64: Add arm64.pagesize= command line override Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 19/43] arm64: pi: Select the page size at runtime during early boot Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 20/43] arm64: pi: Clear the FDT ptes on every remap in map_fdt() Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 21/43] arm64: Test PAGE_SIZE instead of CONFIG_ARM64_*K_PAGES Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 22/43] arm64: Align image sections to PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 23/43] arm64: Map the trampoline rodata at its aligned offset Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 24/43] arm64: fixmap: Size slots and page tables for PAGE_SIZE_MIN Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 25/43] arm64: Size and align the zero page and FDT ptes for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 26/43] arm64: Convert KVM and hugetlb size switches to if/else Eric Curtin
2026-12-01  1:51 ` [RFC PATCH v2 27/43] arm64: mm: Make page size dependent BUILD_BUG_ON() checks runtime Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 29/43] arm64: Look up the page size at runtime in assembly code Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 30/43] arm64: Support runtime folding of the pmd level Eric Curtin
2026-12-01  1:52 ` Eric Curtin [this message]
2026-12-01  1:52 ` [RFC PATCH v2 32/43] arm64: Patch TRAMP_VALIAS into instructions at boot Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 33/43] arm64: Determine THREAD_SIZE at boot time Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 34/43] mm/damon/lru_sort: Initialise the stub access pattern at boot Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 35/43] mm/zsmalloc: Size static limits for the worst-case page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 36/43] arm64: Convert remaining page-size switches to if/else Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 37/43] vdso: Lay out the data pages in VDSO_PAGE_SIZE units Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 38/43] mm: Use fixed sizes for shmem str_dup and swap cache PFN bits Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 39/43] crash: Size the vmcoreinfo page array for the smallest page size Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 40/43] arm64: Size kasan and pKVM stacktrace arrays for PAGE_SIZE_MAX Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 41/43] exit: Use THREAD_SIZE_MAX for lowest_to_date Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 42/43] tracing: Use PAGE_SIZE_MIN/MAX in the uprobe check and undefsyms page Eric Curtin
2026-12-01  1:52 ` [RFC PATCH v2 43/43] arm64: Enable boot-time page size selection Eric Curtin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201015240.1017259-32-ericcurtin17@gmail.com \
    --to=ericcurtin17@gmail.com \
    --cc=catalin.marinas@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=mark.rutland@arm.com \
    --cc=ryan.roberts@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox