Re: [PATCH 02/45] mm: Add {pmd,pud}_huge_lock helper
From: Peter Xu
Date: Thu Jul 04 2024 - 11:03:40 EST
On Thu, Jul 04, 2024 at 06:30:49AM +0200, Oscar Salvador wrote:
> Deep down hugetlb and thp use the same lock for pud and pmd.
> Create two helpers that can be directly used by both of them,
> as they will be used in the generic pagewalkers.
>
> Signed-off-by: Oscar Salvador <osalvador@xxxxxxx>
> ---
> include/linux/mm_inline.h | 27 +++++++++++++++++++++++++++
> 1 file changed, 27 insertions(+)
>
> diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
> index f4fe593c1400..93e3eb86ef4e 100644
> --- a/include/linux/mm_inline.h
> +++ b/include/linux/mm_inline.h
> @@ -9,6 +9,7 @@
> #include <linux/string.h>
> #include <linux/userfaultfd_k.h>
> #include <linux/swapops.h>
> +#include <linux/hugetlb.h>
>
> /**
> * folio_is_file_lru - Should the folio be on a file LRU or anon LRU?
> @@ -590,4 +591,30 @@ static inline bool vma_has_recency(struct vm_area_struct *vma)
> return true;
> }
>
> +static inline spinlock_t *pmd_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
> +{
> + spinlock_t *ptl;
> +
> + if (pmd_leaf(*pmd)) {
> + ptl = pmd_lock(vma->vm_mm, pmd);
> + if (pmd_leaf(*pmd))
> + return ptl;
> + spin_unlock(ptl);
> + }
> + return NULL;
> +}
> +
> +static inline spinlock_t *pud_huge_lock(pud_t *pud, struct vm_area_struct *vma)
> +{
> + spinlock_t *ptl = pud_lock(vma->vm_mm, pud);
> +
> + if (pud_leaf(*pud)) {
> + ptl = pud_lock(vma->vm_mm, pud);
> + if (pud_leaf(*pud))
> + return ptl;
> + spin_unlock(ptl);
> + }
> + return NULL;
> +}
IIRC I left similar comment before somewhere when we're discussing.. but we
may need to consider swap entries too.
I think it might be easier we stick with pxd_trans_huge_lock(), but some
slight modification on top: (1) rename them, perhaps s/trans_//g? (2) need
to also handle swap entry for puds (hugetlb migration entries, right now
pud_trans_huge_lock() didn't consider that).
Thanks,
--
Peter Xu