[RFC PATCH v2 3/4] arm64: mm: Create r/o page table region that permits updates
From: Ard Biesheuvel
Date: Thu Aug 27 2026 - 12:50:21 EST
From: Ard Biesheuvel <ardb@xxxxxxxxxx>
Generalize the handling of swapper_pg_dir, and apply the fixmap fallback
for page table updates to all page tables in the .rodata..pgtbl section.
Currently, this holds only swapper_pg_dir, but this will be expanded in
subsequent patches.
Signed-off-by: Ard Biesheuvel <ardb@xxxxxxxxxx>
---
arch/arm64/include/asm/pgtable.h | 6 -----
arch/arm64/kernel/vmlinux.lds.S | 6 +++++
arch/arm64/mm/mmu.c | 26 +++++++++++++++++---
3 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index 799cd9d52c28..de0a07dae83a 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -817,12 +817,6 @@ extern pgd_t reserved_pg_dir[];
void try_set_readonly_ptval(ptval_t *ptvalp, ptval_t ptval);
-static inline bool in_swapper_pgdir(void *addr)
-{
- return ((unsigned long)addr & PAGE_MASK) ==
- ((unsigned long)swapper_pg_dir & PAGE_MASK);
-}
-
static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
{
__put_kernel_nofault(pmdp, &pmd, pmd_t, fault);
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index af1d72020976..2cda0b7c41d4 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -243,9 +243,15 @@ SECTIONS
reserved_pg_dir = .;
. += PAGE_SIZE;
+ __rodata_pgtbl_start = .;
swapper_pg_dir = .;
. += PAGE_SIZE;
+ .pgtbl.ro_after_init : ALIGN(PAGE_SIZE) {
+ *(.pgtbl.ro_after_init)
+ }
+ __rodata_pgtbl_end = .;
+
. = ALIGN(SEGMENT_ALIGN);
__init_begin = .;
__inittext_begin = .;
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index b97e4bf99ca5..dad482d66f17 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -65,17 +65,27 @@ long __section(".mmuoff.data.write") __early_cpu_boot_status;
static DEFINE_MUTEX(fixmap_lock);
+static struct range kimg_ropgtbl_range __ro_after_init;
+
void noinstr try_set_readonly_ptval(ptval_t *ptvalp, ptval_t ptval)
{
static DEFINE_SPINLOCK(lock);
+ bool is_lm = __is_lm_address(ptvalp);
+ u64 pa = is_lm ? __pa(ptvalp) : __pa_symbol(ptvalp);
- BUG_ON(!in_swapper_pgdir(ptvalp));
+ if (!range_contains(&kimg_ropgtbl_range,
+ &DEFINE_RANGE(pa, pa + sizeof(ptval_t))))
+ BUG();
/*
- * Don't bother with the fixmap if swapper_pg_dir is still mapped
+ * Don't bother with the fixmap if .rodata is still mapped
* writable in the kernel mapping.
*/
if (rodata_is_rw) {
+ /* no fault should have occurred for a kimg address */
+ BUG_ON(!is_lm);
+
+ ptvalp = (ptval_t *)__phys_to_kimg(pa);
WRITE_ONCE(*ptvalp, ptval);
dsb(ishst);
isb();
@@ -83,7 +93,7 @@ void noinstr try_set_readonly_ptval(ptval_t *ptvalp, ptval_t ptval)
}
guard(spinlock)(&lock);
- ptvalp = (ptval_t *)set_fixmap_offset(FIX_PTVAL, __pa_symbol(ptvalp));
+ ptvalp = (ptval_t *)set_fixmap_offset(FIX_PTVAL, pa);
WRITE_ONCE(*ptvalp, ptval);
/*
* We need dsb(ishst) here to ensure the page-table-walker sees
@@ -1177,6 +1187,14 @@ static inline void arm64_kfence_map_pool(void) { }
#endif /* CONFIG_KFENCE */
+static void __init record_ropgtbl_phys_range(void)
+{
+ extern const char __rodata_pgtbl_start[], __rodata_pgtbl_end[];
+
+ kimg_ropgtbl_range = DEFINE_RANGE(__pa_symbol(__rodata_pgtbl_start),
+ __pa_symbol(__rodata_pgtbl_end));
+}
+
static void __init map_mem(void)
{
static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
@@ -1189,6 +1207,8 @@ static void __init map_mem(void)
int flags = NO_EXEC_MAPPINGS;
u64 i;
+ record_ropgtbl_phys_range();
+
/*
* Setting hierarchical PXNTable attributes on table entries covering
* the linear region is only possible if it is guaranteed that no table
--
2.55.0.887.g758fc8c411-goog