[PATCH 19/21] mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing

From: David Hildenbrand (Arm)

Date: Wed Nov 25 2026 - 17:38:35 EST


huge_pmd_share() currently only shares the page tables of another VMA if
it can read-lock the mmap lock of the corresponding mm. That refuses
sharing whenever that mm is write-locked or if the mmap lock is
contended.

mprotect() holds the VMA write lock while changing the flags and the
page table entries, so let's read-lock only that VMA instead. As we hold
i_mmap_rwsem, that VMA remains part of its mm and cannot go away.

Add vma_trylock_read(), a variant of vma_start_read(), that does not
require being called with the RCU read lock held, and will also not drop
it itself.

Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
include/linux/mmap_lock.h | 1 +
mm/hugetlb.c | 4 +--
mm/mmap_lock.c | 78 +++++++++++++++++++++++++++++++----------------
3 files changed, 55 insertions(+), 28 deletions(-)

diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
index bada7a03ddf4b..f42444069e76c 100644
--- a/include/linux/mmap_lock.h
+++ b/include/linux/mmap_lock.h
@@ -278,6 +278,7 @@ static inline bool vma_start_read_locked(struct vm_area_struct *vma)

struct vm_area_struct *vma_start_read_unlocked(struct mm_struct *mm,
unsigned long address);
+bool vma_trylock_read(struct vm_area_struct *vma);

static inline void vma_end_read(struct vm_area_struct *vma)
{
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 5db97f6d15595..62ed8d9aeeadd 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -7287,7 +7287,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
* Stabilize the svma flags, as mprotect() changes flags
* before adjusting page tables.
*/
- if (!mmap_read_trylock(svma->vm_mm))
+ if (!vma_trylock_read(svma))
continue;

saddr = page_table_shareable(svma, vma, addr, idx);
@@ -7297,7 +7297,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
if (spte)
ptdesc_pmd_pts_inc(virt_to_ptdesc(spte));
}
- mmap_read_unlock(svma->vm_mm);
+ vma_end_read(svma);
if (spte)
break;
}
diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c
index c7261f980cef1..68c16281e52a5 100644
--- a/mm/mmap_lock.c
+++ b/mm/mmap_lock.c
@@ -212,26 +212,17 @@ static bool vma_seqcount_is_write_locked(unsigned int vma_seq, unsigned int mm_s
}

/*
- * Try to read-lock a vma. The function is allowed to occasionally yield false
- * locked result to avoid performance overhead, in which case we fall back to
- * using mmap_lock. The function should never yield false unlocked result.
- * False locked result is possible if vma gets reused and attached to a
- * different mm before we lock it.
- * Returns the vma on success, NULL on failure to lock and EAGAIN if vma got
- * detached.
- *
- * IMPORTANT: RCU lock must be held upon entering the function, but upon error
- * IT IS RELEASED. The caller must handle this correctly.
+ * Like vma_start_read(), but without RCU handling: the caller must make sure
+ * that the vma cannot be freed, for example by holding the RCU lock.
*/
-static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
- struct vm_area_struct *vma)
+static struct vm_area_struct *__vma_start_read(struct mm_struct *mm,
+ struct vm_area_struct *vma)
{
unsigned int vma_seq;
unsigned int mm_seq;
struct mm_struct *other_mm;
int oldcnt;

- RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
/*
* Check before locking. A race might cause false locked result.
* We can use READ_ONCE() for the mm_lock_seq here, and don't need
@@ -241,10 +232,8 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
*/
vma_seq = READ_ONCE(vma->vm_lock_seq);
mm_seq = READ_ONCE(mm->mm_lock_seq.sequence);
- if (vma_seqcount_is_write_locked(vma_seq, mm_seq)) {
- vma = NULL;
- goto err;
- }
+ if (vma_seqcount_is_write_locked(vma_seq, mm_seq))
+ return NULL;

/*
* If VM_REFCNT_EXCLUDE_READERS_FLAG is set,
@@ -257,8 +246,7 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
if (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt,
VM_REFCNT_LIMIT))) {
/* return EAGAIN if vma got detached from under us */
- vma = oldcnt ? NULL : ERR_PTR(-EAGAIN);
- goto err;
+ return oldcnt ? NULL : ERR_PTR(-EAGAIN);
}

__vma_lockdep_acquire_read(vma);
@@ -288,14 +276,9 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
mm_seq = raw_read_seqcount(&mm->mm_lock_seq);
if (unlikely(vma_seqcount_is_write_locked(vma_seq, mm_seq))) {
vma_refcount_put(vma);
- vma = NULL;
- goto err;
+ return NULL;
}

- return vma;
-err:
- rcu_read_unlock();
-
return vma;
err_unstable:
/*
@@ -309,11 +292,54 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
mmgrab(other_mm);
vma_refcount_put(vma);
mmdrop_async(other_mm);
- rcu_read_unlock();

return NULL;
}

+/**
+ * vma_start_read() - Try to read-lock a vma looked up under RCU.
+ * @mm: The mm the vma is expected to belong to.
+ * @vma: The vma to read-lock.
+ *
+ * The function is allowed to occasionally yield false locked result to avoid
+ * performance overhead, in which case we fall back to using mmap_lock. The
+ * function should never yield false unlocked result. False locked result is
+ * possible if vma gets reused and attached to a different mm before we lock it.
+ *
+ * Context: RCU lock must be held upon entering the function, but upon error
+ * IT IS RELEASED. The caller must handle this correctly.
+ *
+ * Return: The vma on success, NULL on failure to lock and ERR_PTR(-EAGAIN) if
+ * the vma got detached.
+ */
+static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
+ struct vm_area_struct *vma)
+{
+ RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
+
+ vma = __vma_start_read(mm, vma);
+ if (IS_ERR_OR_NULL(vma))
+ rcu_read_unlock();
+ return vma;
+}
+
+/**
+ * vma_trylock_read() - Try to read-lock a VMA that belongs to its mm.
+ * @vma: The VMA to read-lock.
+ *
+ * Unlike vma_start_read(), this function does not deal with VMAs that might
+ * get freed and reused in another mm concurrently.
+ *
+ * Context: The caller must make sure that @vma remains part of @vma->vm_mm
+ * and cannot go away.
+ *
+ * Return: true if @vma is read-locked, false otherwise.
+ */
+bool vma_trylock_read(struct vm_area_struct *vma)
+{
+ return !IS_ERR_OR_NULL(__vma_start_read(vma->vm_mm, vma));
+}
+
/*
* Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be
* stable and not isolated. If the VMA is not found or is being modified the

--
2.56.1