[PATCH RFC v2 22/54] x86/mm/pat: consolidate calculation of the new protection

From: Mike Rapoport (Microsoft)

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


The large page path and the PTE path of CPA both apply the request masks to
the current protection and then run static_protections() and verify_rwx()
on the result. Each path spells the arguments differently.

The generic core will need this calculation as a hook.

Move it to cpa_req_prot() and cpa_new_prot() and use them in both PTE and
PxD paths.

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

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 304b7ad26c45..cb503e41340b 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -1085,6 +1085,38 @@ static pte_t cpa_pte(enum pgtable_level level, unsigned long pfn, pgprot_t prot)
return pfn_pte(pfn, prot);
}

+static pgprot_t cpa_req_prot(const struct cpa_data *cpa,
+ const struct cpa_leaf *leaf)
+{
+ return __pgprot((pgprot_val(leaf->prot) & ~pgprot_val(cpa->mask_clr)) |
+ pgprot_val(cpa->mask_set));
+}
+
+static pgprot_t cpa_new_prot(struct cpa_data *cpa, struct cpa_leaf *leaf)
+{
+ pgprot_t prot = cpa_req_prot(cpa, leaf);
+ enum pgtable_level level = leaf->level;
+ unsigned long size = leaf->size;
+ unsigned long addr = leaf->addr;
+ pgprot_t old_prot = leaf->prot;
+ unsigned long pfn = leaf->pfn;
+ int warnlvl = CPA_PROTECT;
+ unsigned long lpsize = 0;
+ unsigned long numpages;
+
+ numpages = size >> PAGE_SHIFT;
+
+ if (level != PGTABLE_LEVEL_PTE) {
+ warnlvl = CPA_DETECT;
+ lpsize = size;
+ }
+
+ /* A PTE hands in lpsize = 0 to enforce the protection mechanism */
+ prot = static_protections(prot, addr, pfn, numpages, lpsize, warnlvl);
+
+ return verify_rwx(cpa, old_prot, prot, addr, pfn, numpages);
+}
+
static int should_split_large_page(struct cpa_data *cpa, struct cpa_leaf *leaf)
{
unsigned long address = __cpa_addr(cpa, cpa->curpage);
@@ -1114,9 +1146,7 @@ static int should_split_large_page(struct cpa_data *cpa, struct cpa_leaf *leaf)
if (numpages < cpa->numpages)
cpa->numpages = numpages;

- req_prot = old_prot;
- pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
- pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
+ req_prot = cpa_req_prot(cpa, leaf);

/*
* old_pfn points to the large page base pfn. So we need to add the
@@ -1148,11 +1178,7 @@ static int should_split_large_page(struct cpa_data *cpa, struct cpa_leaf *leaf)
* Check whether the requested pgprot is conflicting with a static
* protection requirement in the large page.
*/
- new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages,
- psize, CPA_DETECT);
-
- new_prot = verify_rwx(cpa, old_prot, new_prot, lpaddr, old_pfn,
- numpages);
+ new_prot = cpa_new_prot(cpa, leaf);

/*
* If there is a conflict, split the large page.
@@ -1873,20 +1899,12 @@ static int __change_page_attr(struct cpa_data *cpa)

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

- pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
- pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
-
cpa_inc_pte_install();
- /* Hand in lpsize = 0 to enforce the protection mechanism */
- new_prot = static_protections(new_prot, address, pfn, 1, 0,
- CPA_PROTECT);
-
- new_prot = verify_rwx(cpa, old_prot, new_prot, address, pfn, 1);
+ new_prot = cpa_new_prot(cpa, &leaf);

/*
* We need to keep the pfn from the existing PTE,

--
2.53.0