[PATCH RFC v2 37/54] ARM: mm: use the generic CPA core

From: Mike Rapoport (Microsoft)

Date: Thu Nov 26 2026 - 06:20:32 EST


The availability of the generic CPA core makes ARM's implementation of
set_memory_*() APIs unnecessary and it can be replaced with several hooks
required by CPA core.

This way the core can manage the policy decisions and the architecture only
has to take care of the page table manipulation.

Switch to the generic CPA core and its set_memory() entry points.
For ARM this means that:

* a hole in the range fails the request with a warning instead of getting a
page table
* nothing is written or flushed when nothing changes
* a request on a section, which ioremap() may create in the vmalloc area on
!SMP, still fails with a warning and -EINVAL
* a request on a static I/O mapping, which may use sections in the vmalloc
area, fails with a warning and -EINVAL as well
* a request that fails flushes the entire TLB
* set_memory_rox() is a single request instead of set_memory_ro() followed
by set_memory_x()
* lazily unmapped vmap areas and unused kmaps are purged before a request,
so that they do not keep stale mappings or TLB entries with the old
protection

ARM only changes ranges mapped with base pages: modules and vmalloc, as
checked before, and the KFENCE pool, which arch_kfence_init_pool() splits.

KFENCE keeps set_memory_valid(), which runs with interrupts disabled. It
now walks with apply_to_existing_page_range(), which never allocates,
rather than apply_to_page_range(), which allocates a page table for a hole
with GFP_KERNEL.

Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
---
arch/arm/Kconfig | 1 +
arch/arm/include/asm/set_memory.h | 6 +--
arch/arm/mm/pageattr.c | 96 +++++++++++++++++++++++++--------------
3 files changed, 65 insertions(+), 38 deletions(-)

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 0cc289a7184a..7a7ccc113126 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -77,6 +77,7 @@ config ARM
select GENERIC_IRQ_SHOW
select GENERIC_IRQ_SHOW_LEVEL
select GENERIC_LIB_DEVMEM_IS_ALLOWED
+ select GENERIC_PAGEATTR if MMU
select GENERIC_PCI_IOMAP
select GENERIC_SCHED_CLOCK
select GENERIC_SMP_IDLE_THREAD
diff --git a/arch/arm/include/asm/set_memory.h b/arch/arm/include/asm/set_memory.h
index 0211b9c5b14d..4f1988dad160 100644
--- a/arch/arm/include/asm/set_memory.h
+++ b/arch/arm/include/asm/set_memory.h
@@ -7,10 +7,8 @@
#define _ASMARM_SET_MEMORY_H

#ifdef CONFIG_MMU
-int set_memory_ro(unsigned long addr, int numpages);
-int set_memory_rw(unsigned long addr, int numpages);
-int set_memory_x(unsigned long addr, int numpages);
-int set_memory_nx(unsigned long addr, int numpages);
+#include <asm-generic/set_memory.h>
+
int set_memory_valid(unsigned long addr, int numpages, int enable);
#else
static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; }
diff --git a/arch/arm/mm/pageattr.c b/arch/arm/mm/pageattr.c
index 064ad508c149..2147b58b19cb 100644
--- a/arch/arm/mm/pageattr.c
+++ b/arch/arm/mm/pageattr.c
@@ -4,10 +4,14 @@
*/
#include <linux/mm.h>
#include <linux/module.h>
+#include <linux/pageattr.h>
+#include <linux/vmalloc.h>

#include <asm/tlbflush.h>
#include <asm/set_memory.h>

+#include "mm.h"
+
struct page_change_data {
pgprot_t set_mask;
pgprot_t clear_mask;
@@ -44,58 +48,82 @@ static int __change_memory_common(unsigned long start, unsigned long size,
data.set_mask = set_mask;
data.clear_mask = clear_mask;

- ret = apply_to_page_range(&init_mm, start, size, change_page_range,
- &data);
+ ret = apply_to_existing_page_range(&init_mm, start, size,
+ change_page_range, &data);

flush_tlb_kernel_range(start, start + size);
return ret;
}

-static int change_memory_common(unsigned long addr, int numpages,
- pgprot_t set_mask, pgprot_t clear_mask)
+/* Only page mappings are changed: a section has a different format. */
+void arch_cpa_decode_leaf(struct cpa_leaf *leaf)
{
- unsigned long start = addr & PAGE_MASK;
- unsigned long end = PAGE_ALIGN(addr) + numpages * PAGE_SIZE;
- unsigned long size = end - start;
-
- WARN_ON_ONCE(start != addr);
-
- if (!size)
- return 0;
+ pte_t pte = ptep_get(leaf->kpte);

- if (!range_in_range(start, size, MODULES_VADDR, MODULES_END) &&
- !range_in_range(start, size, VMALLOC_START, VMALLOC_END))
- return -EINVAL;
+ if (leaf->level != PGTABLE_LEVEL_PTE) {
+ leaf->pfn = 0;
+ leaf->prot = __pgprot(0);
+ return;
+ }

- return __change_memory_common(start, size, set_mask, clear_mask);
+ leaf->pfn = pte_pfn(pte);
+ leaf->prot = __pgprot(pte_val(pte) & ~(PHYS_MASK & (s32)PAGE_MASK));
}

-int set_memory_ro(unsigned long addr, int numpages)
+void arch_cpa_modify_leaf(struct cpa_leaf *leaf, pgprot_t prot)
{
- return change_memory_common(addr, numpages,
- __pgprot(L_PTE_RDONLY),
- __pgprot(0));
+ if (leaf->level == PGTABLE_LEVEL_PTE)
+ set_pte_ext(leaf->kpte, pfn_pte(leaf->pfn, prot), 0);
}

-int set_memory_rw(unsigned long addr, int numpages)
+void arch_cpa_flush(struct cpa_data *cpa)
{
- return change_memory_common(addr, numpages,
- __pgprot(0),
- __pgprot(L_PTE_RDONLY));
-}
+ if (cpa->state & CPA_ST_FLUSH_ALL) {
+ flush_tlb_all();
+ return;
+ }

-int set_memory_nx(unsigned long addr, int numpages)
-{
- return change_memory_common(addr, numpages,
- __pgprot(L_PTE_XN),
- __pgprot(0));
+ cpa_flush_tlb_kernel_range(cpa);
}

-int set_memory_x(unsigned long addr, int numpages)
+static const struct cpa_op_prot cpa_ops[CPA_OP_NR] = {
+ [CPA_OP_RO] = { .set = __pgprot(L_PTE_RDONLY) },
+ [CPA_OP_RW] = { .clr = __pgprot(L_PTE_RDONLY) },
+ [CPA_OP_X] = { .clr = __pgprot(L_PTE_XN) },
+ [CPA_OP_NX] = { .set = __pgprot(L_PTE_XN) },
+ [CPA_OP_ROX] = { .set = __pgprot(L_PTE_RDONLY),
+ .clr = __pgprot(L_PTE_XN) },
+};
+
+int arch_cpa_prepare(struct cpa_data *cpa, enum cpa_op op)
{
- return change_memory_common(addr, numpages,
- __pgprot(0),
- __pgprot(L_PTE_XN));
+ unsigned long end = PAGE_ALIGN(cpa->addr) + cpa->numpages * PAGE_SIZE;
+ unsigned long start = cpa->addr & PAGE_MASK;
+ unsigned long size = end - start;
+ bool in_modules, in_vmalloc;
+ struct vm_struct *area;
+
+ if (!size)
+ return 0;
+
+ /*
+ * The sections of the linear map are copied to every page table, so
+ * only the page mappings of the modules and vmalloc areas can change.
+ * ioremap() and the static I/O mappings may use sections there.
+ */
+ in_modules = range_in_range(start, size, MODULES_VADDR, MODULES_END);
+ in_vmalloc = range_in_range(start, size, VMALLOC_START, VMALLOC_END);
+ if (!in_modules && !in_vmalloc)
+ return -EINVAL;
+
+ area = find_vm_area((void *)start);
+ if (WARN_ON_ONCE(area && (area->flags & (VM_ARM_SECTION_MAPPING |
+ VM_ARM_STATIC_MAPPING))))
+ return -EINVAL;
+
+ cpa_init_op(cpa, &cpa_ops[op]);
+
+ return 0;
}

int set_memory_valid(unsigned long addr, int numpages, int enable)

--
2.53.0