[RFC PATCH v3 2/8] mm: Add {pmd,pud}_huge_lock helper
From: Oscar Salvador
Date: Mon May 25 2026 - 12:56:37 EST
HugeTLB and THP use the same lock for pud and pmd,
so 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 | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index a171070e15f0..93637890cbeb 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -667,4 +667,36 @@ static inline size_t num_pages_contiguous(struct page **pages, size_t nr_pages)
return i;
}
+static inline spinlock_t *pmd_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
+{
+ spinlock_t *ptl;
+
+ if (pmd_present(*pmd) || !pmd_none(*pmd)) {
+ ptl = pmd_lock(vma->vm_mm, pmd);
+ if (pmd_present(*pmd) && pmd_leaf(*pmd))
+ return ptl;
+ else if (!pmd_present(*pmd) && !pmd_none(*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;
+
+ if (pud_present(*pud) || !pud_none(*pud)) {
+ ptl = pud_lock(vma->vm_mm, pud);
+ if (pud_present(*pud) && pud_leaf(*pud))
+ return ptl;
+ else if (!pud_present(*pud) && !pud_none(*pud))
+ return ptl;
+ spin_unlock(ptl);
+ }
+
+ return NULL;
+}
+
#endif
--
2.53.0