[PATCH 02/45] mm: Add {pmd,pud}_huge_lock helper

Oscar Salvador posted 45 patches 1 year, 7 months ago
[PATCH 02/45] mm: Add {pmd,pud}_huge_lock helper
Posted by Oscar Salvador 1 year, 7 months ago
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@suse.de>
---
 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;
+}
+
 #endif
-- 
2.26.2
Re: [PATCH 02/45] mm: Add {pmd,pud}_huge_lock helper
Posted by Peter Xu 1 year, 7 months ago
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@suse.de>
> ---
>  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