[PATCH RFC v2 24/54] x86/mm/pat: detect unchanged mappings by comparing protections

From: Mike Rapoport (Microsoft)

Date: Thu Nov 26 2026 - 06:18:35 EST


CPA decides whether a mapping changes by encoding the new page table entry
and comparing it with the existing one.

It would be suboptimal to encode actual entries in the generic code for
every comparison of the existing, requested and new protections, as every
architecture would need one more out-of-line hook for it.

cpa_leaf by definition has the decoded protection which can be used in
comparisons.

Compare the new protection with the decoded one and encode only the page
table entries that are written, as the last step.

Encoding masks out the bits the CPU does not support and _PAGE_GLOBAL bit
of a non-present entry, comparing protections does not. set_memory_global()
on a CPU without PGE or on a non-present page now rewrites the PTEs with
the same value and flushes the TLB. Its only caller changes the kernel
text, which is always present, once at boot.

Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
---
arch/x86/mm/pat/set_memory.c | 14 +++-----------
1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 0a2d19064f23..f4d3e2d9c3c9 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -1917,26 +1917,18 @@ static int __change_page_attr(struct cpa_data *cpa)
return __cpa_process_fault(cpa, address);

if (leaf.level == PGTABLE_LEVEL_PTE) {
- pte_t old_pte = *leaf.kpte;
pgprot_t new_prot;
unsigned long pfn = leaf.pfn;
- pte_t new_pte;

cpa_inc_pte_install();
new_prot = cpa_new_prot(cpa, &leaf);
-
- /*
- * We need to keep the pfn from the existing PTE,
- * after all we're only going to change its attributes
- * not the memory it points to
- */
- new_pte = cpa_pte(leaf.level, pfn, new_prot);
cpa->pfn = pfn;
/*
* Do we really change anything ?
*/
- if (pte_val(old_pte) != pte_val(new_pte)) {
- set_pte_atomic(leaf.kpte, new_pte);
+ if (pgprot_val(new_prot) != pgprot_val(leaf.prot)) {
+ set_pte_atomic(leaf.kpte,
+ cpa_pte(leaf.level, pfn, new_prot));
cpa->state |= CPA_ST_FLUSHTLB;
}
cpa->numpages = 1;

--
2.53.0