From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
To: linux-mm@kvack.org
Cc: "Adrian Barnaś" <abarnas@google.com>,
"Alexander Gordeev" <agordeev@linux.ibm.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Andy Lutomirski" <luto@kernel.org>,
"Arnd Bergmann" <arnd@arndb.de>, "Borislav Petkov" <bp@alien8.de>,
"Brendan Jackman" <brendan.jackman@linux.dev>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
"David Hildenbrand" <david@kernel.org>,
"Gerald Schaefer" <gerald.schaefer@linux.ibm.com>,
"Heiko Carstens" <hca@linux.ibm.com>,
"Huacai Chen" <chenhuacai@kernel.org>,
"Ingo Molnar" <mingo@kernel.org>,
"Kevin Brodsky" <kevin.brodsky@arm.com>,
"Liam R. Howlett" <liam@infradead.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Madhavan Srinivasan" <maddy@linux.ibm.com>,
"Michal Hocko" <mhocko@suse.com>,
"Mike Rapoport" <rppt@kernel.org>,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Paul Walmsley" <pjw@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Russell King" <linux@armlinux.org.uk>,
"Suren Baghdasaryan" <surenb@google.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Vasily Gorbik" <gor@linux.ibm.com>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Will Deacon" <will@kernel.org>,
linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org,
linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org,
loongarch@lists.linux.dev, x86@kernel.org
Subject: [PATCH RFC v2 50/54] arm64: use the generic CPA core
Date: Thu, 26 Nov 2026 13:15:03 +0200 [thread overview]
Message-ID: <20261126-generic-set-memory-v0-1-v2-50-723f1e33c3d8@kernel.org> (raw)
In-Reply-To: <20261126-generic-set-memory-v0-1-v2-0-723f1e33c3d8@kernel.org>
arm64 implementation of set_memory_*() APIs has a few shortcomings:
* the page table walk that changes kernel page protections is preceded by
another walk that splits the mappings at both ends of a request down to
base pages, whether the request needs that or not
* the linear alias of a vmalloc area is changed and flushed page by page
* set_memory_*() refuse anything but a single VM_ALLOC area, a check that
dates from when a live mapping could not be split at all and makes arm64
the odd one out among the architectures
The availability of the generic CPA core makes arm64'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 arm64 this means that:
* set_memory_*() accept any kernel mapping; a request that needs a split
arm64 cannot do fails with a warning and -EINVAL
* a contiguous block of entries is kept or changed like a large page; its
entries are rewritten one by one, so the block has mixed permissions for
a few stores, the same window every change has before its TLB flush
* the linear alias follows read-only and read-write changes when
rodata_full is set, as before, for any mapping of RAM
* a request and its aliases run in one lazy MMU section
* requests are serialized by the core; the old walk ran without a lock
* KFENCE and debug_pagealloc change the linear map with interrupts disabled
and keep a lockless set_memory_valid()
* the linear alias of data and bss, which may use block mappings, is mapped
and unmapped for hibernation by a request of its own
The mappings at both ends of a request are still split to base pages before
the walk, so the core does not split yet.
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
arch/arm64/Kconfig | 1 +
arch/arm64/include/asm/pageattr.h | 18 ++
arch/arm64/include/asm/set_memory.h | 2 -
arch/arm64/mm/mmu.c | 21 +-
arch/arm64/mm/pageattr.c | 387 +++++++++++++++++++-----------------
5 files changed, 246 insertions(+), 183 deletions(-)
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 9b08b2457a55..846fae788b3c 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -138,6 +138,7 @@ config ARM64
select GENERIC_IRQ_SHOW
select GENERIC_IRQ_SHOW_LEVEL
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/arm64/include/asm/pageattr.h b/arch/arm64/include/asm/pageattr.h
new file mode 100644
index 000000000000..9634cb523298
--- /dev/null
+++ b/arch/arm64/include/asm/pageattr.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_PAGEATTR_H
+#define __ASM_PAGEATTR_H
+
+#define cpa_is_direct_map(addr) __is_lm_address(__tag_reset(addr))
+
+int arch_cpa_process_alias(struct cpa_data *cpa);
+#define arch_cpa_process_alias arch_cpa_process_alias
+
+void arch_cpa_enter(void);
+#define arch_cpa_enter arch_cpa_enter
+
+void arch_cpa_leave(void);
+#define arch_cpa_leave arch_cpa_leave
+
+int arm64_change_page_attr(struct cpa_data *cpa);
+
+#endif /* __ASM_PAGEATTR_H */
diff --git a/arch/arm64/include/asm/set_memory.h b/arch/arm64/include/asm/set_memory.h
index 0091ba12200e..4c4ca9cd50c8 100644
--- a/arch/arm64/include/asm/set_memory.h
+++ b/arch/arm64/include/asm/set_memory.h
@@ -11,8 +11,6 @@ bool can_set_direct_map(void);
int set_memory_valid(unsigned long addr, int numpages, int enable);
-int set_direct_map_invalid_noflush(struct page *page, unsigned int numpages);
-int set_direct_map_default_noflush(struct page *page, unsigned int numpages);
bool kernel_page_present(struct page *page);
int set_memory_encrypted(unsigned long addr, int numpages);
diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index 0f96efea9057..c8f3961b4555 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -13,6 +13,7 @@
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/kexec.h>
+#include <linux/pageattr.h>
#include <linux/libfdt.h>
#include <linux/mman.h>
#include <linux/nodemask.h>
@@ -1078,9 +1079,23 @@ static void __init __map_memblock(phys_addr_t start, phys_addr_t end,
static void mark_linear_data_alias_valid(bool valid)
{
- set_memory_valid((unsigned long)lm_alias(__init_end),
- (unsigned long)(__bss_stop - __init_end) / PAGE_SIZE,
- valid);
+ pgprot_t valid_prot = __pgprot(PTE_PRESENT_VALID_KERNEL);
+ pgprot_t invalid_prot = __pgprot(PTE_PRESENT_INVALID);
+ struct cpa_data cpa = {
+ .addr = (unsigned long)lm_alias(__init_end),
+ .numpages = (__bss_stop - __init_end) / PAGE_SIZE,
+ .mask_set = valid ? valid_prot : invalid_prot,
+ .mask_clr = valid ? invalid_prot : valid_prot,
+ .flags = CPA_REQ_NO_CHECK_ALIAS,
+ };
+ int ret;
+
+ ret = split_kernel_leaf_mapping(cpa.addr,
+ cpa.addr + cpa.numpages * PAGE_SIZE);
+ if (WARN_ON_ONCE(ret))
+ return;
+
+ arm64_change_page_attr(&cpa);
}
static int arm64_hibernate_pm_notify(struct notifier_block *nb,
diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
index de7bf95003ea..14a791b8e645 100644
--- a/arch/arm64/mm/pageattr.c
+++ b/arch/arm64/mm/pageattr.c
@@ -8,7 +8,7 @@
#include <linux/mem_encrypt.h>
#include <linux/sched.h>
#include <linux/vmalloc.h>
-#include <linux/pagewalk.h>
+#include <linux/pageattr.h>
#include <asm/cacheflush.h>
#include <asm/pgtable-prot.h>
@@ -16,75 +16,94 @@
#include <asm/tlbflush.h>
#include <asm/kfence.h>
-struct page_change_data {
- pgprot_t set_mask;
- pgprot_t clear_mask;
-};
-
-static ptval_t set_pageattr_masks(ptval_t val, struct mm_walk *walk)
+void arch_cpa_decode_leaf(struct cpa_leaf *leaf)
{
- struct page_change_data *masks = walk->private;
+ enum pgtable_level level = leaf->level;
+ pte_t pte = __ptep_get(leaf->kpte);
+ pteval_t prot, ns_shared;
+ unsigned int nr;
+
+ if (pte_cont(pte)) {
+ nr = level == PGTABLE_LEVEL_PTE ? CONT_PTES : CONT_PMDS;
+ leaf->kpte = PTR_ALIGN_DOWN(leaf->kpte, nr * sizeof(pte_t));
+ leaf->size *= nr;
+ leaf->addr &= ~(leaf->size - 1);
+ pte = __ptep_get(leaf->kpte);
+ }
/*
- * Some users clear and set bits which alias each other (e.g. PTE_NG and
- * PTE_PRESENT_INVALID). It is therefore important that we always clear
- * first then set.
+ * The NS shared bit of a Realm is an address bit that selects the
+ * unprotected alias of the same memory.
*/
- val &= ~(pgprot_val(masks->clear_mask));
- val |= (pgprot_val(masks->set_mask));
+ ns_shared = pte_val(pte) & PROT_NS_SHARED;
+ pte = __pte(pte_val(pte) & ~ns_shared);
+
+ switch (level) {
+ case PGTABLE_LEVEL_PTE:
+ leaf->pfn = pte_pfn(pte);
+ break;
+ case PGTABLE_LEVEL_PMD:
+ leaf->pfn = pmd_pfn(pte_pmd(pte));
+ break;
+ default:
+ leaf->pfn = pud_pfn(pte_pud(pte));
+ break;
+ }
- return val;
+ prot = pgprot_val(pte_pgprot(pte)) | ns_shared;
+ leaf->prot = __pgprot((prot & ~PTE_TYPE_MASK) | (prot & PTE_VALID));
}
-static int pageattr_pud_entry(pud_t *pud, unsigned long addr,
- unsigned long next, struct mm_walk *walk)
+void arch_cpa_modify_leaf(struct cpa_leaf *leaf, pgprot_t prot)
{
- pud_t val = pudp_get(pud);
-
- if (pud_leaf(val)) {
- if (WARN_ON_ONCE((next - addr) != PUD_SIZE))
- return -EINVAL;
- val = __pud(set_pageattr_masks(pud_val(val), walk));
- set_pud(pud, val);
- walk->action = ACTION_CONTINUE;
- }
+ unsigned int nr = leaf->size / pgtable_level_size(leaf->level);
+ enum pgtable_level level = leaf->level;
+ pteval_t val = pgprot_val(prot);
+ unsigned long pfn = leaf->pfn;
+ pte_t *kpte = leaf->kpte;
+ unsigned long pfninc;
+ unsigned int i;
- return 0;
-}
+ pfninc = pgtable_level_size(level) >> PAGE_SHIFT;
-static int pageattr_pmd_entry(pmd_t *pmd, unsigned long addr,
- unsigned long next, struct mm_walk *walk)
-{
- pmd_t val = pmdp_get(pmd);
-
- if (pmd_leaf(val)) {
- if (WARN_ON_ONCE((next - addr) != PMD_SIZE))
- return -EINVAL;
- val = __pmd(set_pageattr_masks(pmd_val(val), walk));
- set_pmd(pmd, val);
- walk->action = ACTION_CONTINUE;
- }
+ if (level == PGTABLE_LEVEL_PTE)
+ val |= PTE_TYPE_PAGE & ~PTE_VALID;
+ prot = __pgprot(val);
- return 0;
+ /*
+ * The entries of a contiguous block are rewritten one by one, so the
+ * block has mixed permissions until the last store, as any mapping has
+ * until the TLB flush of the request.
+ */
+ for (i = 0; i < nr; i++, pfn += pfninc) {
+ switch (level) {
+ case PGTABLE_LEVEL_PTE:
+ __set_pte(kpte + i, pfn_pte(pfn, prot));
+ break;
+ case PGTABLE_LEVEL_PMD:
+ set_pmd((pmd_t *)kpte + i, pfn_pmd(pfn, prot));
+ break;
+ default:
+ set_pud((pud_t *)kpte + i, pfn_pud(pfn, prot));
+ break;
+ }
+ }
}
-static int pageattr_pte_entry(pte_t *pte, unsigned long addr,
- unsigned long next, struct mm_walk *walk)
+void arch_cpa_flush(struct cpa_data *cpa)
{
- pte_t val = __ptep_get(pte);
-
- val = __pte(set_pageattr_masks(pte_val(val), walk));
- __set_pte(pte, val);
+ /*
+ * If the memory is being switched from present-invalid to valid without
+ * changing any other bits then a TLBI isn't required as a non-valid
+ * entry cannot be cached in the TLB.
+ */
+ if (pgprot_val(cpa->mask_set) == PTE_PRESENT_VALID_KERNEL &&
+ pgprot_val(cpa->mask_clr) == PTE_PRESENT_INVALID)
+ return;
- return 0;
+ cpa_flush_tlb_kernel_range(cpa);
}
-static const struct mm_walk_ops pageattr_ops = {
- .pud_entry = pageattr_pud_entry,
- .pmd_entry = pageattr_pmd_entry,
- .pte_entry = pageattr_pte_entry,
-};
-
bool rodata_full __ro_after_init = true;
bool can_set_direct_map(void)
@@ -102,177 +121,189 @@ bool can_set_direct_map(void)
arm64_kfence_can_set_direct_map() || is_realm_world();
}
-static int update_range_prot(unsigned long start, unsigned long size,
- pgprot_t set_mask, pgprot_t clear_mask)
+/*
+ * If we are manipulating read-only permissions, apply the same
+ * change to the linear mapping of the pages that back this VM area.
+ */
+int arch_cpa_process_alias(struct cpa_data *cpa)
{
- struct page_change_data data;
+ pteval_t prot = pgprot_val(cpa->mask_set) | pgprot_val(cpa->mask_clr);
+ unsigned long vaddr = cpa_addr(cpa, cpa->curpage);
+ struct cpa_data alias_cpa;
+ unsigned long alias;
int ret;
- data.set_mask = set_mask;
- data.clear_mask = clear_mask;
+ if (!rodata_full || !(prot & PTE_RDONLY))
+ return 0;
- ret = split_kernel_leaf_mapping(start, start + size);
+ /* Memory that is not RAM, like ioremap(), has no linear map alias. */
+ if (cpa_is_direct_map(vaddr) || !pfn_valid(cpa->pfn))
+ return 0;
+
+ alias = (unsigned long)page_address(pfn_to_page(cpa->pfn));
+ ret = split_kernel_leaf_mapping(alias,
+ alias + cpa->numpages * PAGE_SIZE);
if (WARN_ON_ONCE(ret))
return ret;
- lazy_mmu_mode_enable();
+ alias_cpa = *cpa;
+ alias_cpa.addr = alias;
+ pgprot_val(alias_cpa.mask_set) &= PTE_RDONLY | PTE_WRITE;
+ pgprot_val(alias_cpa.mask_clr) &= PTE_RDONLY | PTE_WRITE;
- /*
- * The caller must ensure that the range we are operating on does not
- * partially overlap a block mapping, or a cont mapping. Any such case
- * must be eliminated by splitting the mapping.
- */
- ret = walk_kernel_page_table_range_lockless(start, start + size,
- &pageattr_ops, NULL, &data);
- lazy_mmu_mode_disable();
+ ret = cpa_change_alias(cpa, &alias_cpa);
+ if (alias_cpa.state & CPA_ST_FLUSHTLB)
+ arch_cpa_flush(&alias_cpa);
return ret;
}
-static int __change_memory_common(unsigned long start, unsigned long size,
- pgprot_t set_mask, pgprot_t clear_mask)
-{
- int ret;
-
- ret = update_range_prot(start, size, set_mask, clear_mask);
-
- /*
- * If the memory is being switched from present-invalid to valid without
- * changing any other bits then a TLBI isn't required as a non-valid
- * entry cannot be cached in the TLB.
- */
- if (pgprot_val(set_mask) != PTE_PRESENT_VALID_KERNEL ||
- pgprot_val(clear_mask) != PTE_PRESENT_INVALID)
- flush_tlb_kernel_range(start, start + size);
- return ret;
-}
+static const struct cpa_op_prot cpa_ops[CPA_OP_NR] = {
+ [CPA_OP_RO] = { .set = __pgprot(PTE_RDONLY),
+ .clr = __pgprot(PTE_WRITE) },
+ [CPA_OP_RW] = { .set = __pgprot(PTE_WRITE),
+ .clr = __pgprot(PTE_RDONLY) },
+ [CPA_OP_X] = { .set = __pgprot(PTE_GP),
+ .clr = __pgprot(PTE_PXN) },
+ [CPA_OP_NX] = { .set = __pgprot(PTE_PXN),
+ .clr = __pgprot(PTE_GP) },
+ [CPA_OP_ROX] = { .set = __pgprot(PTE_RDONLY | PTE_GP),
+ .clr = __pgprot(PTE_WRITE | PTE_PXN) },
+ [CPA_OP_DIRECT_MAP_INVALID] = {
+ .set = __pgprot(PTE_PRESENT_INVALID),
+ .clr = __pgprot(PTE_VALID | PTE_NG),
+ },
+ [CPA_OP_DIRECT_MAP_DEFAULT] = {
+ .set = __pgprot(PTE_VALID | PTE_NG | PTE_WRITE),
+ .clr = __pgprot(PTE_PRESENT_INVALID | PTE_RDONLY),
+ },
+};
-static int change_memory_common(unsigned long addr, int numpages,
- pgprot_t set_mask, pgprot_t clear_mask)
+int arch_cpa_prepare(struct cpa_data *cpa, enum cpa_op op)
{
- unsigned long start = addr;
- unsigned long size = PAGE_SIZE * numpages;
- unsigned long end = start + size;
- struct vm_struct *area;
+ unsigned long start = PAGE_ALIGN_DOWN(cpa->addr);
+ unsigned long end = start + cpa->numpages * PAGE_SIZE;
int ret;
- if (!PAGE_ALIGNED(addr)) {
- start &= PAGE_MASK;
- end = start + size;
- WARN_ON_ONCE(1);
- }
-
- /*
- * Kernel VA mappings are always live, and splitting live section
- * mappings into page mappings may cause TLB conflicts. This means
- * we have to ensure that changing the permission bits of the range
- * we are operating on does not result in such splitting.
- *
- * Let's restrict ourselves to mappings created by vmalloc (or vmap).
- * Disallow VM_ALLOW_HUGE_VMAP mappings to guarantee that only page
- * mappings are updated and splitting is never needed.
- *
- * So check whether the [addr, addr + size) interval is entirely
- * covered by precisely one VM area that has the VM_ALLOC flag set.
- */
- area = find_vm_area((void *)addr);
- if (!area ||
- ((unsigned long)kasan_reset_tag((void *)end) >
- (unsigned long)kasan_reset_tag(area->addr) + area->size) ||
- ((area->flags & (VM_ALLOC | VM_ALLOW_HUGE_VMAP)) != VM_ALLOC))
- return -EINVAL;
-
- if (!numpages)
+ /* Nothing in the linear map is invalid without can_set_direct_map() */
+ if ((op == CPA_OP_DIRECT_MAP_INVALID ||
+ op == CPA_OP_DIRECT_MAP_DEFAULT) && !can_set_direct_map())
return 0;
- /*
- * If we are manipulating read-only permissions, apply the same
- * change to the linear mapping of the pages that back this VM area.
- */
- if (rodata_full && (pgprot_val(set_mask) == PTE_RDONLY ||
- pgprot_val(clear_mask) == PTE_RDONLY)) {
- unsigned long idx = ((unsigned long)kasan_reset_tag((void *)start) -
- (unsigned long)kasan_reset_tag(area->addr))
- >> PAGE_SHIFT;
- for (; numpages; idx++, numpages--) {
- ret = __change_memory_common((u64)page_address(area->pages[idx]),
- PAGE_SIZE, set_mask, clear_mask);
- if (ret)
- return ret;
- }
- }
+ ret = split_kernel_leaf_mapping(start, end);
+ if (WARN_ON_ONCE(ret))
+ return ret;
+
+ cpa_init_op(cpa, &cpa_ops[op]);
/*
- * Get rid of potentially aliasing lazily unmapped vm areas that may
- * have permissions set that deviate from the ones we are setting here.
+ * A valid kernel entry has nG only with nG mappings and GP only with
+ * BTI, see PTE_MAYBE_NG and PTE_MAYBE_GP.
*/
- vm_unmap_aliases();
+ if (!arm64_use_ng_mappings &&
+ (pgprot_val(cpa->mask_set) & PTE_VALID))
+ pgprot_val(cpa->mask_set) &= ~PTE_NG;
+ if (!system_supports_bti_kernel())
+ pgprot_val(cpa->mask_set) &= ~PTE_GP;
- return __change_memory_common(start, size, set_mask, clear_mask);
+ return 0;
}
-int set_memory_ro(unsigned long addr, int numpages)
+void arch_cpa_enter(void)
{
- return change_memory_common(addr, numpages,
- __pgprot(PTE_RDONLY),
- __pgprot(PTE_WRITE));
+ lazy_mmu_mode_enable();
}
-int set_memory_rw(unsigned long addr, int numpages)
+void arch_cpa_leave(void)
{
- return change_memory_common(addr, numpages,
- __pgprot(PTE_WRITE),
- __pgprot(PTE_RDONLY));
+ lazy_mmu_mode_disable();
}
-int set_memory_nx(unsigned long addr, int numpages)
+static int set_valid_pte(pte_t *ptep, unsigned long addr, void *data)
{
- return change_memory_common(addr, numpages,
- __pgprot(PTE_PXN),
- __pgprot(PTE_MAYBE_GP));
-}
+ struct cpa_op_prot *prot = data;
+ pte_t pte = __ptep_get(ptep);
-int set_memory_x(unsigned long addr, int numpages)
-{
- return change_memory_common(addr, numpages,
- __pgprot(PTE_MAYBE_GP),
- __pgprot(PTE_PXN));
+ /*
+ * Some users clear and set bits which alias each other (e.g. PTE_NG and
+ * PTE_PRESENT_INVALID). It is therefore important that we always clear
+ * first then set.
+ */
+ pte = clear_pte_bit(pte, prot->clr);
+ pte = set_pte_bit(pte, prot->set);
+ __set_pte(ptep, pte);
+
+ return 0;
}
+/*
+ * KFENCE and debug_pagealloc change the linear map with interrupts
+ * disabled. Their pages are mapped with PTEs that nothing splits or
+ * collapses.
+ */
int set_memory_valid(unsigned long addr, int numpages, int enable)
{
- if (enable)
- return __change_memory_common(addr, PAGE_SIZE * numpages,
- __pgprot(PTE_PRESENT_VALID_KERNEL),
- __pgprot(PTE_PRESENT_INVALID));
- else
- return __change_memory_common(addr, PAGE_SIZE * numpages,
- __pgprot(PTE_PRESENT_INVALID),
- __pgprot(PTE_PRESENT_VALID_KERNEL));
+ unsigned long size = PAGE_SIZE * numpages;
+ struct cpa_op_prot prot;
+ int ret;
+
+ if (enable) {
+ prot.set = __pgprot(PTE_PRESENT_VALID_KERNEL);
+ prot.clr = __pgprot(PTE_PRESENT_INVALID);
+ } else {
+ prot.set = __pgprot(PTE_PRESENT_INVALID);
+ prot.clr = __pgprot(PTE_PRESENT_VALID_KERNEL);
+ }
+
+ ret = apply_to_existing_page_range(&init_mm, addr, size, set_valid_pte,
+ &prot);
+
+ /*
+ * If the memory is being switched from present-invalid to valid without
+ * changing any other bits then a TLBI isn't required as a non-valid
+ * entry cannot be cached in the TLB.
+ */
+ if (!enable)
+ flush_tlb_kernel_range(addr, addr + size);
+
+ return ret;
}
-int set_direct_map_invalid_noflush(struct page *page, unsigned int numpages)
+/*
+ * Unlike set_memory_*(), the arm64 users of the core don't purge the
+ * lazily freed vmap aliases first.
+ */
+int arm64_change_page_attr(struct cpa_data *cpa)
{
- pgprot_t clear_mask = __pgprot(PTE_PRESENT_VALID_KERNEL);
- pgprot_t set_mask = __pgprot(PTE_PRESENT_INVALID);
+ int ret;
- if (!can_set_direct_map())
- return 0;
+ arch_cpa_enter();
+ ret = change_page_attr_noflush(cpa);
+ arch_cpa_leave();
+
+ if (cpa->state & CPA_ST_FLUSHTLB)
+ arch_cpa_flush(cpa);
- return update_range_prot((unsigned long)page_address(page),
- PAGE_SIZE * numpages, set_mask, clear_mask);
+ return ret;
}
-int set_direct_map_default_noflush(struct page *page, unsigned int numpages)
+static int __change_memory_common(unsigned long start, unsigned long size,
+ pgprot_t set_mask, pgprot_t clear_mask)
{
- pgprot_t set_mask = __pgprot(PTE_PRESENT_VALID_KERNEL | PTE_WRITE);
- pgprot_t clear_mask = __pgprot(PTE_PRESENT_INVALID | PTE_RDONLY);
+ struct cpa_data cpa = {
+ .addr = start,
+ .numpages = size >> PAGE_SHIFT,
+ .mask_set = set_mask,
+ .mask_clr = clear_mask,
+ .flags = CPA_REQ_NO_CHECK_ALIAS,
+ };
+ int ret;
- if (!can_set_direct_map())
- return 0;
+ ret = split_kernel_leaf_mapping(start, start + size);
+ if (WARN_ON_ONCE(ret))
+ return ret;
- return update_range_prot((unsigned long)page_address(page),
- PAGE_SIZE * numpages, set_mask, clear_mask);
+ return arm64_change_page_attr(&cpa);
}
int __set_memory_enc_dec(unsigned long addr, int numpages, bool encrypt)
--
2.53.0
next prev parent reply other threads:[~2026-11-26 11:22 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 11:14 [PATCH RFC v2 00/54] arch, mm: create a generic set_memory/change_page_attr core Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 01/54] riscv: do not use P4D entries for the linear mapping Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 02/54] riscv: test for Svnapot in pte_napot() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 03/54] s390/boot: parse boolean parameters like the kernel Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 04/54] s390/boot: stop parsing the command line at "--" Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 05/54] s390/boot: treat dashes and underscores alike in parameter names Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 06/54] x86/mm/pat: don't count kernel image splits as direct map splits Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 07/54] x86/mm/pat: drop KERN_WARNING from the WARN() in __cpa_process_fault() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 08/54] x86/mm/pat: fail a split at an unexpected level with -EINVAL Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 09/54] x86/mm/pat: separate CPA request flags from internal state Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 10/54] x86/mm/pat: stop using CPA for debug_pagealloc Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 11/54] x86/mm/pat: drop the init_mm read lock from change_page_attr_set_clr() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 12/54] x86/mm/pat: introduce change_page_attr_pages() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 13/54] x86/mm/pat: make the direct map counters atomic Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 14/54] x86/mm/pat: take pgd_lock only around the pgd_list walks Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 15/54] x86/mm/pat: look up the effective NX/RW bits in verify_rwx() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 16/54] x86/mm/pat: look up a large page again only after dropping cpa_lock Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 17/54] x86/mm/pat: drop the check of existing mappings against static protections Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 18/54] x86/mm/pat: consolidate encoding of page table entries Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 19/54] x86/mm/pat: use enum pgtable_level in CPA Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 20/54] x86/mm/pat: index the CPA statistics by page table level Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 21/54] x86/mm/pat: introduce struct cpa_leaf Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 22/54] x86/mm/pat: consolidate calculation of the new protection Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 23/54] x86/mm/pat: allocate the page tables for a split by level Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 24/54] x86/mm/pat: detect unchanged mappings by comparing protections Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 25/54] x86/mm/pat: add cpa_is_direct_map() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 26/54] x86/mm/pat: implement set_memory_*() with a table of operations Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 27/54] x86/mm/pat: prepare the direct map counters for the generic code Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 28/54] x86/mm/pat: separate the generic part of CPA from the x86 hooks Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 29/54] x86/mm/pat, mm: pull CPA core to mm/ Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 30/54] mm/pageattr: introduce arch_cpa_can_split() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 31/54] mm/pageattr: introduce cpa_flush_tlb_kernel_range() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 32/54] asm-generic: add an empty pageattr.h Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 33/54] LoongArch: use pfn_is_nosave() to exclude firmware ranges from hibernation Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 34/54] LoongArch: stop pretending LoongArch supports set_direct_map APIs Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 35/54] LoongArch: drop the huge page handling from set_memory() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 36/54] LoongArch: use the generic CPA core Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 37/54] ARM: mm: " Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 38/54] mm/pageattr: let a leaf cover a contiguous block of entries Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 39/54] powerpc: use the generic CPA core Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 40/54] riscv: replace set_memory_rw_nx() with set_memory_nx() and set_memory_rw() Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 41/54] riscv: use the generic CPA core Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 42/54] riscv: count the mappings of the linear map Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 43/54] mm/pageattr: let an architecture allocate the page tables for a split Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 44/54] mm/pageattr: derive direct map counter updates from the level sizes Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 45/54] s390/boot: map the identity map with 4K pages for debug_pagealloc Mike Rapoport (Microsoft)
2026-11-26 11:14 ` [PATCH RFC v2 46/54] s390: remove set_memory_rwnx() Mike Rapoport (Microsoft)
2026-11-26 11:15 ` [PATCH RFC v2 47/54] s390: use the generic CPA core Mike Rapoport (Microsoft)
2026-11-26 11:15 ` [PATCH RFC v2 48/54] mm/pageattr: let an architecture bracket the set_memory() entry points Mike Rapoport (Microsoft)
2026-11-26 11:15 ` [PATCH RFC v2 49/54] arm64: mm: allocate page tables in the callers of split_p{mu}d() Mike Rapoport (Microsoft)
2026-11-26 11:15 ` Mike Rapoport (Microsoft) [this message]
2026-11-26 11:15 ` [PATCH RFC v2 51/54] arm64: let the generic CPA core split large mappings Mike Rapoport (Microsoft)
2026-11-26 11:15 ` [PATCH RFC v2 52/54] arm64: count the mappings of the linear map Mike Rapoport (Microsoft)
2026-11-26 11:15 ` [PATCH RFC v2 53/54] arch, mm: enable the generic CPA core with ARCH_HAS_SET_MEMORY Mike Rapoport (Microsoft)
2026-11-26 11:15 ` [PATCH RFC v2 54/54] mm/pageattr: document CPA core and the architecture hooks Mike Rapoport (Microsoft)
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=20261126-generic-set-memory-v0-1-v2-50-723f1e33c3d8@kernel.org \
--to=rppt@kernel.org \
--cc=abarnas@google.com \
--cc=agordeev@linux.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=bp@alien8.de \
--cc=brendan.jackman@linux.dev \
--cc=catalin.marinas@arm.com \
--cc=chenhuacai@kernel.org \
--cc=dave.hansen@linux.intel.com \
--cc=david@kernel.org \
--cc=gerald.schaefer@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=kevin.brodsky@arm.com \
--cc=liam@infradead.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-s390@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=ljs@kernel.org \
--cc=loongarch@lists.linux.dev \
--cc=luto@kernel.org \
--cc=maddy@linux.ibm.com \
--cc=mhocko@suse.com \
--cc=mingo@kernel.org \
--cc=palmer@dabbelt.com \
--cc=peterz@infradead.org \
--cc=pjw@kernel.org \
--cc=surenb@google.com \
--cc=tglx@kernel.org \
--cc=vbabka@kernel.org \
--cc=will@kernel.org \
--cc=x86@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
all inboxes | Powered by JetHome®