Re: [PATCH v8 03/30] loongarch: mm: add PMD swap-exclusive helpers
From: Huacai Chen
Date: Fri Oct 02 2026 - 10:22:02 EST
Hi, Usama,
On Fri, Oct 2, 2026 at 5:55 PM Usama Arif <usama.arif@xxxxxxxxx> 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.
>
> A LoongArch swap PMD is the swap PTE value plus _PAGE_HUGE, and
> _PAGE_SWP_EXCLUSIVE sits outside both the type and the offset field, so the
> PMD helpers can use the same bit.
>
> Cc: Huacai Chen <chenhuacai@xxxxxxxxxx>
> Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
> Reviewed-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> arch/loongarch/include/asm/pgtable.h | 19 +++++++++++++++++++
> 1 file changed, 19 insertions(+)
>
> diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
> index cf29a4c8ac593..87fecc3a51001 100644
> --- a/arch/loongarch/include/asm/pgtable.h
> +++ b/arch/loongarch/include/asm/pgtable.h
> @@ -351,6 +351,25 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)
> return pte;
> }
>
> +#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
> +static inline pmd_t pmd_swp_mkexclusive(pmd_t pmd)
> +{
> + pmd_val(pmd) |= _PAGE_SWP_EXCLUSIVE;
> + return pmd;
> +}
> +
> +static inline bool pmd_swp_exclusive(pmd_t pmd)
> +{
> + return pmd_val(pmd) & _PAGE_SWP_EXCLUSIVE;
> +}
> +
> +static inline pmd_t pmd_swp_clear_exclusive(pmd_t pmd)
> +{
> + pmd_val(pmd) &= ~_PAGE_SWP_EXCLUSIVE;
> + return pmd;
> +}
I think the order should be pmd_swp_exclusive, pmd_swp_mkexclusive and
pmd_swp_clear_exclusive. That looks more natural.
The riscv version is already like this, but others are not.
Huacai
> +#endif
> +
> #define pte_none(pte) (!(pte_val(pte) & ~_PAGE_GLOBAL))
> #define pte_present(pte) (pte_val(pte) & (_PAGE_PRESENT | _PAGE_PROTNONE))
> #define pte_no_exec(pte) (pte_val(pte) & _PAGE_NO_EXEC)
> --
> 2.53.0-Meta
>