Hi, Commit 9e4a3ec3411b ("x86/mm/pat: Allocate split page tables as kernel page tables") recently fixed a latent inconsistency in the x86 CPA code (assigned CVE-2026-97525): __split_large_page() allocated the new PTE page with a bare pagetable_alloc(), bypassing pte_alloc_one_kernel(), so the page table constructor never ran and the resulting page was neither flagged PG_table, nor accounted in NR_PAGETABLE, nor marked as a kernel page table. RISC-V open-codes the same pattern when splitting the linear mapping on behalf of set_memory_*(): __split_linear_mapping_{pmd,pud,p4d}() allocate the new page table pages with bare alloc_page(GFP_KERNEL) - three call sites in arch/riscv/mm/pageattr.c, introduced by commit 311cd2f6e253 ("riscv: Fix set_memory_XX() and set_direct_map_XX() by splitting huge linear mappings"). As on pre-fix x86, the resulting pages live in init_mm without the PG_table flag, without NR_PAGETABLE accounting and without the kernel ptdesc marking. Under CONFIG_MEMORY_HOTPLUG, hot-removing memory tears down the linear mapping of the removed range via arch_remove_memory() -> remove_linear_mapping() -> free_p*d_table(), which releases these page tables through pagetable_dtor() even though the constructor never ran, underflowing NR_PAGETABLE on current mainline. Muchun Song's series "mm: Fix page-table teardown during memory hot-remove" [2] fixes the freeing side by skipping the destructor for pages without the PG_table flag, but the root cause remains: the allocation side still bypasses the page-table constructors, so these tables are unflagged and unaccounted for their entire lifetime. Align the three sites with the standard allocation paths: - the PTE level switches to pte_alloc_one_kernel(&init_mm), exactly like the x86 fix; - the PMD and PUD levels switch to the generic pmd_alloc_one() and pud_alloc_one(), which already handle the init_mm case by selecting GFP_PGTABLE_KERNEL, running the level constructors and marking the ptdesc as a kernel page table. No functional change is intended; the observable difference is that split linear mapping page tables are now properly flagged and accounted, so /proc/vmstat:nr_page_table_pages no longer under-counts. Fixes: 311cd2f6e253 ("riscv: Fix set_memory_XX() and set_direct_map_XX() by splitting huge linear mappings") Signed-off-by: Hanting Li --- Changes since v1 [1]: - Switch the PMD and PUD levels to the generic pmd_alloc_one() / pud_alloc_one() instead of a local helper, as suggested by the Sashiko automated review. The generic versions already handle init_mm correctly, which also resolves the first open question of v1. - Correct the severity discussion. v1 claimed these page tables are never freed; that is wrong under CONFIG_MEMORY_HOTPLUG, where memory hot-removal frees them through remove_linear_mapping() -> free_p*d_table() -> pagetable_dtor(), so the ctor/dtor mismatch can actually underflow NR_PAGETABLE. Also pointed out by the automated review. - Add the Fixes: tag and move the discussion notes below the tear line. [1] https://lore.kernel.org/r/202611091101.6A9B1Nf3015337@mse-db.zte.com.cn [2] https://lore.kernel.org/r/20261008073021.2512665-3-songmuchun@bytedance.com Hello, One discussion point from v1 remains open: Since RISC-V never collapses the split linear mapping back, one could argue for deferring this change until free-path work actually lands. My preference is to take it now: with CONFIG_MEMORY_HOTPLUG the free path already exists today, and taking this now removes a trap for any future work on the linear mapping. (Note: the generic CPA rework series "[PATCH RFC 00/18] arch, mm: create a generic set_memory/change_page_attr core" would eventually replace this code entirely; this patch is intended as a small fix in the meantime. The series author has said he is waiting for such fixes to settle before posting a non-RFC version.) Comments welcome. arch/riscv/mm/pageattr.c | 41 +++++++++++++++++------------------ 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c --- a/arch/riscv/mm/pageattr.c +++ b/arch/riscv/mm/pageattr.c @@ -3,9 +3,11 @@ * Copyright (C) 2019 SiFive */ +#include #include #include #include +#include #include #include #include @@ -109,23 +111,22 @@ static int __split_linear_mapping_pmd(pud_t *pudp, continue; if (pmd_leaf(pmdp_get(pmdp))) { - struct page *pte_page; unsigned long pfn = _pmd_pfn(pmdp_get(pmdp)); pgprot_t prot = __pgprot(pmd_val(pmdp_get(pmdp)) & ~_PAGE_PFN_MASK); pte_t *ptep_new; int i; - pte_page = alloc_page(GFP_KERNEL); - if (!pte_page) + ptep_new = pte_alloc_one_kernel(&init_mm); + if (!ptep_new) return -ENOMEM; - ptep_new = (pte_t *)page_address(pte_page); - for (i = 0; i < PTRS_PER_PTE; ++i, ++ptep_new) - set_pte(ptep_new, pfn_pte(pfn + i, prot)); + for (i = 0; i < PTRS_PER_PTE; ++i) + set_pte(ptep_new + i, pfn_pte(pfn + i, prot)); smp_wmb(); - set_pmd(pmdp, pfn_pmd(page_to_pfn(pte_page), PAGE_TABLE)); + set_pmd(pmdp, pfn_pmd(page_to_pfn(virt_to_page(ptep_new)), + PAGE_TABLE)); } } while (pmdp++, vaddr = next, vaddr != end); @@ -149,24 +150,23 @@ static int __split_linear_mapping_pud(p4d_t *p4dp, continue; if (pud_leaf(pudp_get(pudp))) { - struct page *pmd_page; unsigned long pfn = _pud_pfn(pudp_get(pudp)); pgprot_t prot = __pgprot(pud_val(pudp_get(pudp)) & ~_PAGE_PFN_MASK); pmd_t *pmdp_new; int i; - pmd_page = alloc_page(GFP_KERNEL); - if (!pmd_page) + pmdp_new = pmd_alloc_one(&init_mm, vaddr); + if (!pmdp_new) return -ENOMEM; - pmdp_new = (pmd_t *)page_address(pmd_page); - for (i = 0; i < PTRS_PER_PMD; ++i, ++pmdp_new) - set_pmd(pmdp_new, + for (i = 0; i < PTRS_PER_PMD; ++i) + set_pmd(pmdp_new + i, pfn_pmd(pfn + ((i * PMD_SIZE) >> PAGE_SHIFT), prot)); smp_wmb(); - set_pud(pudp, pfn_pud(page_to_pfn(pmd_page), PAGE_TABLE)); + set_pud(pudp, pfn_pud(page_to_pfn(virt_to_page(pmdp_new)), + PAGE_TABLE)); } ret = __split_linear_mapping_pmd(pudp, vaddr, next); @@ -198,23 +198,21 @@ static int __split_linear_mapping_p4d(pgd_t *pgdp, continue; if (p4d_leaf(p4dp_get(p4dp))) { - struct page *pud_page; unsigned long pfn = _p4d_pfn(p4dp_get(p4dp)); pgprot_t prot = __pgprot(p4d_val(p4dp_get(p4dp)) & ~_PAGE_PFN_MASK); pud_t *pudp_new; int i; - pud_page = alloc_page(GFP_KERNEL); - if (!pud_page) + pudp_new = pud_alloc_one(&init_mm, vaddr); + if (!pudp_new) return -ENOMEM; /* * Fill the pud level with leaf puds that have the same * protections as the leaf p4d. */ - pudp_new = (pud_t *)page_address(pud_page); - for (i = 0; i < PTRS_PER_PUD; ++i, ++pudp_new) - set_pud(pudp_new, + for (i = 0; i < PTRS_PER_PUD; ++i) + set_pud(pudp_new + i, pfn_pud(pfn + ((i * PUD_SIZE) >> PAGE_SHIFT), prot)); /* @@ -224,7 +222,8 @@ static int __split_linear_mapping_p4d(pgd_t *pgdp, */ smp_wmb(); - set_p4d(p4dp, pfn_p4d(page_to_pfn(pud_page), PAGE_TABLE)); + set_p4d(p4dp, pfn_p4d(page_to_pfn(virt_to_page(pudp_new)), + PAGE_TABLE)); } ret = __split_linear_mapping_pud(p4dp, vaddr, next);