[PATCH RFC v2 36/54] LoongArch: use the generic CPA core
From: Mike Rapoport (Microsoft)
Date: Thu Nov 26 2026 - 06:20:24 EST
The availability of the generic CPA core makes LoongArch's implementation
of set_memory_*() APIs unnecessary.
The page table walk in arch/loongarch/mm/pageattr.c can be replaced with
several simple 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 LoongArch this means that:
* an empty entry in the range is reported with a warning and -EFAULT
* nothing is written or flushed when nothing changes
* 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()
* requests are serialized by the core, rather than by the init_mm write
lock
* lazily unmapped vmap areas are purged before a request, so that they do
not keep stale TLB entries with the old protection
Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
---
arch/loongarch/Kconfig | 1 +
arch/loongarch/include/asm/set_memory.h | 8 +--
arch/loongarch/mm/pageattr.c | 102 ++++++++------------------------
3 files changed, 28 insertions(+), 83 deletions(-)
diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index bad699e2847f..455a72b8669d 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -107,6 +107,7 @@ config LOONGARCH
select GENERIC_LIB_LSHRDI3
select GENERIC_LIB_UCMPDI2
select GENERIC_LIB_DEVMEM_IS_ALLOWED
+ select GENERIC_PAGEATTR
select GENERIC_PCI_IOMAP
select GENERIC_SCHED_CLOCK
select GENERIC_SMP_IDLE_THREAD
diff --git a/arch/loongarch/include/asm/set_memory.h b/arch/loongarch/include/asm/set_memory.h
index 64c7f942e8ec..5496a3ee27da 100644
--- a/arch/loongarch/include/asm/set_memory.h
+++ b/arch/loongarch/include/asm/set_memory.h
@@ -6,12 +6,6 @@
#ifndef _ASM_LOONGARCH_SET_MEMORY_H
#define _ASM_LOONGARCH_SET_MEMORY_H
-/*
- * Functions to change memory attributes.
- */
-int set_memory_x(unsigned long addr, int numpages);
-int set_memory_nx(unsigned long addr, int numpages);
-int set_memory_ro(unsigned long addr, int numpages);
-int set_memory_rw(unsigned long addr, int numpages);
+#include <asm-generic/set_memory.h>
#endif /* _ASM_LOONGARCH_SET_MEMORY_H */
diff --git a/arch/loongarch/mm/pageattr.c b/arch/loongarch/mm/pageattr.c
index 11cea93c7415..a90380b387d3 100644
--- a/arch/loongarch/mm/pageattr.c
+++ b/arch/loongarch/mm/pageattr.c
@@ -3,100 +3,50 @@
* Copyright (C) 2024 Loongson Technology Corporation Limited
*/
-#include <linux/pagewalk.h>
+#include <linux/pageattr.h>
#include <linux/pgtable.h>
#include <asm/set_memory.h>
#include <asm/tlbflush.h>
-struct pageattr_masks {
- pgprot_t set_mask;
- pgprot_t clear_mask;
-};
-
-static unsigned long set_pageattr_masks(unsigned long val, struct mm_walk *walk)
-{
- unsigned long new_val = val;
- struct pageattr_masks *masks = walk->private;
-
- new_val &= ~(pgprot_val(masks->clear_mask));
- new_val |= (pgprot_val(masks->set_mask));
-
- return new_val;
-}
-
-static int pageattr_pte_entry(pte_t *pte, unsigned long addr,
- unsigned long next, struct mm_walk *walk)
+void arch_cpa_decode_leaf(struct cpa_leaf *leaf)
{
- pte_t val = ptep_get(pte);
-
- val = __pte(set_pageattr_masks(pte_val(val), walk));
- set_pte(pte, val);
+ pte_t pte = ptep_get(leaf->kpte);
- return 0;
+ leaf->pfn = pte_pfn(pte);
+ leaf->prot = pte_pgprot(pte);
}
-static int pageattr_pte_hole(unsigned long addr, unsigned long next,
- int depth, struct mm_walk *walk)
+void arch_cpa_modify_leaf(struct cpa_leaf *leaf, pgprot_t prot)
{
- return 0;
+ set_pte(leaf->kpte, pfn_pte(leaf->pfn, prot));
}
-static const struct mm_walk_ops pageattr_ops = {
- .pte_entry = pageattr_pte_entry,
- .pte_hole = pageattr_pte_hole,
- .walk_lock = PGWALK_RDLOCK,
-};
-
-static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask, pgprot_t clear_mask)
+void arch_cpa_flush(struct cpa_data *cpa)
{
- int ret;
- unsigned long start = addr;
- unsigned long end = start + PAGE_SIZE * numpages;
- struct pageattr_masks masks = {
- .set_mask = set_mask,
- .clear_mask = clear_mask
- };
-
- if (!numpages)
- return 0;
-
- mmap_write_lock(&init_mm);
- ret = walk_kernel_page_table_range(start, end, &pageattr_ops, NULL, &masks);
- mmap_write_unlock(&init_mm);
-
- flush_tlb_kernel_range(start, end);
+ if (cpa->state & CPA_ST_FLUSH_ALL) {
+ flush_tlb_all();
+ return;
+ }
- return ret;
+ cpa_flush_tlb_kernel_range(cpa);
}
-int set_memory_x(unsigned long addr, int numpages)
-{
- if (addr < vm_map_base)
- return 0;
-
- return __set_memory(addr, numpages, __pgprot(0), __pgprot(_PAGE_NO_EXEC));
-}
-
-int set_memory_nx(unsigned long addr, int numpages)
-{
- if (addr < vm_map_base)
- return 0;
-
- return __set_memory(addr, numpages, __pgprot(_PAGE_NO_EXEC), __pgprot(0));
-}
+static const struct cpa_op_prot cpa_ops[CPA_OP_NR] = {
+ [CPA_OP_RO] = { .clr = __pgprot(_PAGE_WRITE | _PAGE_DIRTY) },
+ [CPA_OP_RW] = { .set = __pgprot(_PAGE_WRITE | _PAGE_DIRTY) },
+ [CPA_OP_X] = { .clr = __pgprot(_PAGE_NO_EXEC) },
+ [CPA_OP_NX] = { .set = __pgprot(_PAGE_NO_EXEC) },
+ [CPA_OP_ROX] = { .clr = __pgprot(_PAGE_WRITE | _PAGE_DIRTY |
+ _PAGE_NO_EXEC) },
+};
-int set_memory_ro(unsigned long addr, int numpages)
+int arch_cpa_prepare(struct cpa_data *cpa, enum cpa_op op)
{
- if (addr < vm_map_base)
+ /* The direct mapping windows below vm_map_base have no page tables */
+ if (cpa->addr < vm_map_base)
return 0;
- return __set_memory(addr, numpages, __pgprot(0), __pgprot(_PAGE_WRITE | _PAGE_DIRTY));
-}
+ cpa_init_op(cpa, &cpa_ops[op]);
-int set_memory_rw(unsigned long addr, int numpages)
-{
- if (addr < vm_map_base)
- return 0;
-
- return __set_memory(addr, numpages, __pgprot(_PAGE_WRITE | _PAGE_DIRTY), __pgprot(0));
+ return 0;
}
--
2.53.0