[PATCH RFC v2 23/54] x86/mm/pat: allocate the page tables for a split by level

From: Mike Rapoport (Microsoft)

Date: Thu Nov 26 2026 - 06:19:00 EST


split_large_page() allocates the page table for a split with
pte_alloc_one_kernel() regardless of the level it splits. A PMD table that
replaces a PUD is allocated and announced to paravirt as a PTE table.

While in practice this only matters for the accounting of page table pages,
having proper allocation helpers not only makes that accounting right but
will allow architectures to override page table allocation methods when
core CPA logic moves to the generic code.

Add helpers to allocate and free page tables with the appropriate function:
pte_alloc_one_kernel() for a table that replaces PMD and pmd_alloc_one()
for a table that replaces PUD.

Use paravirt_alloc_pmd() to notify PV guests about the new page table that
replaces a PUD.

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

diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index cb503e41340b..0a2d19064f23 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -1215,7 +1215,10 @@ __split_large_page(struct cpa_data *cpa, struct cpa_leaf *leaf, pte_t *pbase)
if (level != PGTABLE_LEVEL_PMD && level != PGTABLE_LEVEL_PUD)
return -EINVAL;

- paravirt_alloc_pte(&init_mm, page_to_pfn(base));
+ if (level == PGTABLE_LEVEL_PMD)
+ paravirt_alloc_pte(&init_mm, page_to_pfn(base));
+ else
+ paravirt_alloc_pmd(&init_mm, page_to_pfn(base));

pfninc = pgtable_level_size(level - 1) >> PAGE_SHIFT;
for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
@@ -1257,18 +1260,34 @@ __split_large_page(struct cpa_data *cpa, struct cpa_leaf *leaf, pte_t *pbase)
return 0;
}

+static void *cpa_alloc_pgtable(struct cpa_leaf *leaf)
+{
+ if (leaf->level == PGTABLE_LEVEL_PMD)
+ return pte_alloc_one_kernel(&init_mm);
+
+ return pmd_alloc_one(&init_mm, leaf->addr);
+}
+
+static void cpa_free_pgtable(struct cpa_leaf *leaf, void *table)
+{
+ if (leaf->level == PGTABLE_LEVEL_PMD)
+ pte_free_kernel(&init_mm, table);
+ else
+ pmd_free(&init_mm, table);
+}
+
static int split_large_page(struct cpa_data *cpa, struct cpa_leaf *leaf)
{
unsigned long address = __cpa_addr(cpa, cpa->curpage);
pte_t *kpte = leaf->kpte;
struct cpa_leaf tmp;
- pte_t *pte;
+ void *table;
int ret = 0;

spin_unlock(&cpa_lock);
- pte = pte_alloc_one_kernel(&init_mm);
+ table = cpa_alloc_pgtable(leaf);
spin_lock(&cpa_lock);
- if (!pte)
+ if (!table)
return -ENOMEM;

/*
@@ -1279,14 +1298,14 @@ static int split_large_page(struct cpa_data *cpa, struct cpa_leaf *leaf)
goto out;
*leaf = tmp;

- ret = __split_large_page(cpa, leaf, pte);
+ ret = __split_large_page(cpa, leaf, table);
if (ret)
goto out;

return 0;

out:
- pte_free_kernel(&init_mm, pte);
+ cpa_free_pgtable(leaf, table);
return ret;
}


--
2.53.0