Re: [PATCH v3] riscv: mm: Fix split linear map page table allocation
From: Muchun Song
Date: Tue Nov 24 2026 - 03:07:59 EST
On Tue, Nov 24, 2026 at 3:44 PM <li.hanting@xxxxxxxxxx> wrote:
>
> 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" [1]
> 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 page-table allocation and
> population helpers:
>
> - the PTE level switches to pte_alloc_one_kernel(&init_mm), exactly
> like the x86 fix, and installs the new table into the parent entry
> with pmd_populate_kernel();
>
> - 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, and install the new tables with
> pud_populate() and p4d_populate().
>
> No change to the resulting linear mappings 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 <li.hanting@xxxxxxxxxx>
It's annoying that you didn't cc my linux.dev mail, because I used this email
in v2.
The changes looks good to me.
Reviewed-by: Muchun Song (ByteDance) <muchun.song@xxxxxxxxx>
>
> ---
> Changes since v2 [2]:
>
> - Install the new page tables with pmd_populate_kernel(),
> pud_populate() and p4d_populate() instead of open-coding the
> virtual-address-to-PFN conversion, as suggested by Muchun Song.
>
> - Reword the intended-behaviour statement: the accounting, PG_table
> state and hot-remove destructor behavior are intentional functional
> changes, so "no functional change" was misleading; it now states
> that no change to the resulting linear mappings is intended. Also
> suggested by Muchun Song.
>
> - Shorten the subject and drop the greeting from the commit message.
>
> [1] https://lore.kernel.org/r/20261008073021.2512665-3-songmuchun@xxxxxxxxxxxxx
> [2] https://lore.kernel.org/r/<202611230851.6AN8pFDD065900@xxxxxxxxxxxxxxxxxx>
>
> Hello,
>
> Thank you for the review, Muchun. All four suggestions are
> incorporated in this version.
>
> One detail worth spelling out about p4d_populate(): unlike the
> previous open-coded set_p4d(), which falls back to writing through
> the pud alias when 4-level paging is disabled, p4d_populate() is a
> no-op in that case. The difference is not reachable here: with
> 3-level paging, best_map_size() caps the linear mapping at PMD_SIZE,
> so a leaf p4d entry can only exist when 4-level paging is enabled,
> where p4d_populate() matches the open-coded write exactly.
>
> The discussion about deferring this change until free-path work
> lands is now settled by Muchun's series [1] being queued for
> mainline: this patch fixes the allocation side of the same lifetime
> mismatch.
>
> (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.)
>
> Comments welcome.
>
> arch/riscv/mm/pageattr.c | 38 +++++++++++++++++---------------------
> 1 file changed, 17 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 <linux/mm.h>
> #include <linux/pagewalk.h>
> #include <linux/pgtable.h>
> #include <linux/vmalloc.h>
> +#include <asm/pgalloc.h>
> #include <asm/tlbflush.h>
> #include <asm/bitops.h>
> #include <asm/set_memory.h>
> @@ -109,23 +111,21 @@
> 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));
> + pmd_populate_kernel(&init_mm, pmdp, ptep_new);
> }
> } while (pmdp++, vaddr = next, vaddr != end);
>
> @@ -149,24 +149,22 @@
> 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));
> + pud_populate(&init_mm, pudp, pmdp_new);
> }
>
> ret = __split_linear_mapping_pmd(pudp, vaddr, next);
> @@ -198,23 +196,21 @@
> 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 +220,7 @@
> */
> smp_wmb();
>
> - set_p4d(p4dp, pfn_p4d(page_to_pfn(pud_page), PAGE_TABLE));
> + p4d_populate(&init_mm, p4dp, pudp_new);
> }
>
> ret = __split_linear_mapping_pud(p4dp, vaddr, next);