[PATCH RFC v2 16/54] x86/mm/pat: look up a large page again only after dropping cpa_lock

From: Mike Rapoport (Microsoft)

Date: Thu Nov 26 2026 - 06:17:11 EST


should_split_large_page() and __split_large_page() both look up the address
again to check whether another CPU split the large page after the previous
lookup.

In should_split_large_page() this cannot happen, because cpa_lock is held
from the lookup in __change_page_attr() to the check.

split_large_page() drops cpa_lock to allocate a page table, so the check is
needed there.

Drop the lookup from should_split_large_page() and pass it the level that
__change_page_attr() found.

Move the lookup from __split_large_page() to split_large_page(), right
after it retakes cpa_lock, and pass the level down.

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

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 8d481e9edf75..967d691ef1c0 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -973,24 +973,15 @@ static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot)
}

static int should_split_large_page(pte_t *kpte, unsigned long address,
- struct cpa_data *cpa)
+ enum pg_level level, struct cpa_data *cpa)
{
unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn;
pgprot_t old_prot, new_prot, req_prot, chk_prot;
- pte_t new_pte, *tmp;
- enum pg_level level;
+ pte_t new_pte;

if (cpa->flags & CPA_REQ_SPLIT)
return 1;

- /*
- * Check for races, another CPU might have split this page
- * up already:
- */
- tmp = _lookup_address_cpa(cpa, address, &level);
- if (tmp != kpte)
- return 1;
-
switch (level) {
case PG_LEVEL_2M:
old_prot = pmd_pgprot(*(pmd_t *)kpte);
@@ -1159,21 +1150,12 @@ static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn,

static int
__split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
- pte_t *pbase)
+ unsigned int level, pte_t *pbase)
{
unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1;
struct page *base = virt_to_page(pbase);
- unsigned int i, level;
pgprot_t ref_prot;
- pte_t *tmp;
-
- /*
- * Check for races, another CPU might have split this page
- * up for us already:
- */
- tmp = _lookup_address_cpa(cpa, address, &level);
- if (tmp != kpte)
- return 1;
+ unsigned int i;

paravirt_alloc_pte(&init_mm, page_to_pfn(base));

@@ -1257,8 +1239,9 @@ __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
unsigned long address)
{
+ unsigned int level;
pte_t *pte;
- int ret;
+ int ret = 0;

spin_unlock(&cpa_lock);
pte = pte_alloc_one_kernel(&init_mm);
@@ -1266,13 +1249,22 @@ static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
if (!pte)
return -ENOMEM;

- ret = __split_large_page(cpa, kpte, address, pte);
+ /*
+ * Check for races, another CPU might have split this page
+ * up for us already:
+ */
+ if (_lookup_address_cpa(cpa, address, &level) != kpte)
+ goto out;
+
+ ret = __split_large_page(cpa, kpte, address, level, pte);
if (ret)
- pte_free_kernel(&init_mm, pte);
- if (ret < 0)
- return ret;
+ goto out;

return 0;
+
+out:
+ pte_free_kernel(&init_mm, pte);
+ return ret;
}

static int collapse_pmd_page(pmd_t *pmd, unsigned long addr,
@@ -1928,7 +1920,7 @@ static int __change_page_attr(struct cpa_data *cpa)
* Check, whether we can keep the large page intact
* and just change the pte:
*/
- do_split = should_split_large_page(kpte, address, cpa);
+ do_split = should_split_large_page(kpte, address, level, cpa);
/*
* When the range fits into the existing large page,
* return. cp->numpages and cpa->tlbflush have been updated in

--
2.53.0