Re: [PATCH v8 07/30] x86: mm: add PMD swap-exclusive helpers
From: Zi Yan
Date: Mon Oct 05 2026 - 16:15:08 EST
On Fri Oct 2, 2026 at 5:52 AM EDT, Usama Arif wrote:
> Prepare for keeping a PMD-mapped anonymous THP mapped by a PMD across the
> swap round-trip, which makes PG_anon_exclusive have to survive in a swap
> PMD and not just in a swap PTE.
>
> x86-64 encodes a swap PMD exactly like a swap PTE, so the new helpers reuse
> _PAGE_SWP_EXCLUSIVE, bit 3, which the swap-entry layout already reserves
> for PG_anon_exclusive. 32-bit x86 aliases that bit to _PAGE_PSE and does
> not select ARCH_HAS_PMD_SOFTLEAVES; assert that, because an exclusive swap
> PMD would otherwise be pmd_present().
>
> Cc: Thomas Gleixner <tglx@xxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> Cc: Borislav Petkov <bp@xxxxxxxxx>
> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
> Cc: x86@xxxxxxxxxx
> Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
> Reviewed-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
> Reviewed-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> arch/x86/include/asm/pgtable.h | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
>
> diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
> index d551120a7c889..c35dfa6ceda05 100644
> --- a/arch/x86/include/asm/pgtable.h
> +++ b/arch/x86/include/asm/pgtable.h
> @@ -1525,6 +1525,26 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)
> return pte_clear_flags(pte, _PAGE_SWP_EXCLUSIVE);
> }
>
> +#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
> +/* 32-bit aliases this to _PAGE_PSE, making an exclusive swap PMD present. */
Is it better to say? Since the above comment sounds like 32-bit
specific, but the actual constraint is about not being _PAGE_PSE.
_PAGE_SWP_EXCLUSIVE cannot be _PAGE_PSE, otherwise making an exclusive
swap PMD present.
> +static_assert(_PAGE_SWP_EXCLUSIVE != _PAGE_PSE);
> +
> +static inline pmd_t pmd_swp_mkexclusive(pmd_t pmd)
> +{
> + return pmd_set_flags(pmd, _PAGE_SWP_EXCLUSIVE);
> +}
> +
> +static inline bool pmd_swp_exclusive(pmd_t pmd)
> +{
> + return pmd_flags(pmd) & _PAGE_SWP_EXCLUSIVE;
> +}
> +
> +static inline pmd_t pmd_swp_clear_exclusive(pmd_t pmd)
> +{
> + return pmd_clear_flags(pmd, _PAGE_SWP_EXCLUSIVE);
> +}
> +#endif
> +
> #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
> static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
> {
Otherwise, LGTM.
Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>]
--
Best Regards,
Yan, Zi