[RFC PATCH 1/2] mm/mmap_lock: add try_lock_next_vma()

From: Chuanhua Han

Date: Fri Dec 04 2026 - 22:40:27 EST


From: Chuanhua Han <hanchuanhua@xxxxxxxxxx>

lock_next_vma() iterates VMAs under RCU and read-locks each, falling
back to mmap_lock when a VMA cannot be locked or the tree may have
changed under the iterator, so that no VMA is missed. Some callers
cannot take mmap_lock, or would rather skip a VMA than wait for it:
the BPF task_vma iterator can run in contexts that cannot sleep, and
open-codes such a loop on top of lock_vma_under_rcu().

Add try_lock_next_vma(), a variant of lock_next_vma() that never takes
mmap_lock. It continues a VMA iterator up to an end address, and
skips a VMA whose lock cannot be taken. A VMA being modified
concurrently may therefore be missed.

It read-locks the VMA found by the iterator directly with
vma_start_read(), instead of looking it up again from the root of the
tree, as lock_vma_under_rcu() does. vma_start_read() leaves the RCU
read side critical section on failure, so take a nested one around it,
and the caller's stays held. If the VMA has been reused by another mm,
vma_start_read() drops that mm with mmdrop_async() since
commit 44926897b6c2 ("mm/vma: drop mm asynchronously in
vma_start_read()"), so its failure path no longer runs __mmdrop() and
takes no lock such as pgd_lock. try_lock_next_vma() can therefore be
called in the caller's RCU read side critical section, in which the
iterator stays valid across calls.

The VMA locked may have changed between the lookup and the lock. Once
locked, it is still in the tree, so if it still covers the lowest
address of interest in the range it was found in, nothing can be
mapped between that address and its start, and it is returned, with
the iterator moved to its end if that has moved. Otherwise, for
example if it was split and another VMA now maps its start, that
range is looked up again, as when the VMA was detached. Only a VMA
whose lock cannot be taken, or mapped into a range already searched,
is missed.

The next patch converts the BPF task_vma iterator, which makes its
lookup and locking about 40% faster.

Suggested-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>
Suggested-by: Lorenzo Stoakes <ljs@xxxxxxxxxx>
Signed-off-by: Chuanhua Han <hanchuanhua@xxxxxxxxxx>
---
include/linux/mmap_lock.h | 4 +++
mm/mmap_lock.c | 62 +++++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+)

diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
index bada7a03ddf4..792139aa4294 100644
--- a/include/linux/mmap_lock.h
+++ b/include/linux/mmap_lock.h
@@ -556,6 +556,10 @@ static inline void vma_rcu_iter_init(struct vma_rcu_iter *rvmi,
*/
struct vm_area_struct *lock_next_vma(struct vma_rcu_iter *rvmi,
unsigned long from_addr);
+struct vm_area_struct *try_lock_next_vma(struct mm_struct *mm,
+ struct vma_iterator *vmi,
+ unsigned long from_addr,
+ unsigned long end);

static inline void vma_assert_can_modify(struct vm_area_struct *vma)
{
diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c
index c7261f980cef..7d542f5d6d3f 100644
--- a/mm/mmap_lock.c
+++ b/mm/mmap_lock.c
@@ -522,6 +522,68 @@ struct vm_area_struct *lock_next_vma(struct vma_rcu_iter *rvmi,
return vma;
}

+/**
+ * try_lock_next_vma() - Try to read-lock the next VMA, without mmap_lock.
+ * @mm: the mm_struct of the address space to search
+ * @vmi: VMA iterator on @mm, at or before @from_addr, used under RCU
+ * @from_addr: the lowest address of interest
+ * @end: the end of the range searched (exclusive)
+ *
+ * Unlike lock_next_vma(), never take mmap_lock: a VMA whose lock cannot be
+ * taken is skipped, and the search continues after it. So a VMA being
+ * modified, or mapped into a gap already searched, concurrently may be
+ * missed.
+ *
+ * Context: RCU read side critical section, which stays held. Does not sleep.
+ * Return: a read-locked VMA overlapping [@from_addr, @end), with @vmi at its
+ * end, or NULL.
+ */
+struct vm_area_struct *try_lock_next_vma(struct mm_struct *mm,
+ struct vma_iterator *vmi,
+ unsigned long from_addr,
+ unsigned long end)
+{
+ struct vm_area_struct *vma, *locked;
+ unsigned long found_from;
+
+ RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu read lock held");
+
+ while ((vma = vma_find(vmi, end))) {
+ /* the lowest address of interest where it was found */
+ found_from = max(from_addr, vma_iter_addr(vmi));
+
+ /* vma_start_read() leaves RCU on failure: keep the caller's */
+ rcu_read_lock();
+ locked = vma_start_read(mm, vma);
+ if (IS_ERR_OR_NULL(locked)) {
+ /* detached: look up what replaced it */
+ if (PTR_ERR(locked) == -EAGAIN)
+ vma_iter_set(vmi, found_from);
+ continue;
+ }
+ rcu_read_unlock();
+
+ /*
+ * Stable now, and still in the tree, but it may have changed since
+ * it was found. If it no longer covers @found_from, another VMA
+ * may be there now: look it up again.
+ */
+ if (unlikely(locked->vm_start > found_from ||
+ locked->vm_end <= found_from)) {
+ vma_end_read(locked);
+ vma_iter_set(vmi, found_from);
+ continue;
+ }
+
+ /* it may have shrunk or grown at its end: resync @vmi */
+ if (unlikely(vma_iter_end(vmi) != locked->vm_end))
+ vma_iter_set(vmi, locked->vm_end);
+ return locked;
+ }
+
+ return NULL;
+}
+
/*
* Called with mmap write lock held after mm->mm_lock_seq just overflowed (and
* therefore zero), with all VMA write locks now released.
--
2.34.1