Re: [PATCH RFC v3 1/4] x86/mm: Use proper page table helpers for huge page generation

From: Jonathan Cameron

Date: Fri Mar 06 2026 - 04:31:09 EST


On Sat, 28 Feb 2026 15:09:03 +0800
Yin Tirui <yintirui@xxxxxxxxxx> wrote:

> Historically, several core x86 mm subsystems (vmemmap, vmalloc, and CPA)
> have abused `pfn_pte()` to generate PMD and PUD entries by passing
> pgprot values containing the _PAGE_PSE flag, and then casting the
> resulting pte_t to a pmd_t or pud_t.
>
> This violates strict type safety and prevents us from enforcing the rule
> that `pfn_pte()` should strictly generate pte without huge page attributes.
>
> Fix these abuses by explicitly using the correct level-specific helpers
> (`pfn_pmd()` and `pfn_pud()`) and their corresponding setters
> (`set_pmd()`, `set_pud()`).
>
> For the CPA (Change Page Attribute) code, which uses `pte_t` as a generic
> container for page table entries across all levels in
> __should_split_large_page(), pack the correctly generated PMD/PUD values
> into the pte_t container.
>
> This cleanup prepares the ground for making `pfn_pte()` strictly filter
> out huge page attributes.
>
> Signed-off-by: Yin Tirui <yintirui@xxxxxxxxxx>
Hi. A tiny drive by review comment below.

> ---
> arch/x86/mm/init_64.c | 6 +++---
> arch/x86/mm/pat/set_memory.c | 6 +++++-
> arch/x86/mm/pgtable.c | 4 ++--
> 3 files changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c
> index df2261fa4f98..d65f3d05c66f 100644
> --- a/arch/x86/mm/init_64.c
> +++ b/arch/x86/mm/init_64.c
> @@ -1518,11 +1518,11 @@ static int __meminitdata node_start;
> void __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node,
> unsigned long addr, unsigned long next)
> {
> - pte_t entry;
> + pmd_t entry;
>
> - entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
> + entry = pfn_pmd(__pa(p) >> PAGE_SHIFT,
> PAGE_KERNEL_LARGE);

Whilst you are here, can we make that a one liner.
entry = pfn_pmd(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL_LARGE);

Could even do

pmd_t entry = pfn_pmd(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL_LARGE);
but that's more of a question of taste.


> - set_pmd(pmd, __pmd(pte_val(entry)));
> + set_pmd(pmd, entry);
>
> /* check to see if we have contiguous blocks */