[RFC PATCH 2/2] bpf: use try_lock_next_vma() in the task_vma iterator

From: Chuanhua Han

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


From: Chuanhua Han <hanchuanhua@xxxxxxxxxx>

Commit bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to
per-VMA locks") made bpf_iter_task_vma_find_next() open-code a
lock_next_vma() without the mmap_lock fallback, since the iterator can
be called from contexts that cannot sleep. It finds the next VMA under
RCU, leaves RCU, and locks it with lock_vma_under_rcu(), which looks it
up again from the root of the VMA tree. Since the VMA may have changed
in between, it retries on failure, advancing by PAGE_SIZE when the
range it saw is stale, to guarantee progress.

try_lock_next_vma() does the same without the second lookup. It locks
the VMA the iterator found, and when that VMA has changed, it looks up
the same address again with the same iterator, which only moves
forward, so there is no stale range to advance past. Use it, and update
the comment that says lock_vma_under_rcu() checks vma->vm_mm.

As before, the iterator returns the next VMA it can lock that ends
after the previous one, and skips a VMA it cannot lock.

A microbenchmark measures the lookup and locking cost of the
iterator. A debugfs file, not part of this series, runs a copy of the
old and the new bpf_iter_task_vma_find_next() alternately on the mm of
the calling process, locking and unlocking every VMA in turn as the
iterator does, and reports the median time of a full pass. The process
maps single-page anonymous VMAs, alternately RW and RO so that they do
not merge, which is the worst case for the lookup. On an i7-14700,
bound to one P-core with the performance governor, the median of five
runs in one boot, with the range of the five in brackets:

VMAs before after change
50023 3740 us [3729-3749] 2272 us [2246-2284] -39%
5023 343 us [342-344] 197 us [197-198] -43%
523 31 us [31-31] 17 us [17-17] -45%

Per VMA, that is 74 -> 45 ns, 68 -> 39 ns and 60 -> 33 ns. With turbo
disabled, the change is the same, -38% to -45%. Making every second
VMA PROT_NONE gives the same results, since the iterator locks every
VMA either way.

Suggested-by: Lorenzo Stoakes <ljs@xxxxxxxxxx>
Signed-off-by: Chuanhua Han <hanchuanhua@xxxxxxxxxx>
---
kernel/bpf/task_iter.c | 44 ++++--------------------------------------
1 file changed, 4 insertions(+), 40 deletions(-)

diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
index 2ee7b3b3c10b..a12c125eef9d 100644
--- a/kernel/bpf/task_iter.c
+++ b/kernel/bpf/task_iter.c
@@ -941,53 +941,17 @@ __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
return err;
}

-/*
- * Find and lock the next VMA at or after data->next_addr.
- *
- * lock_vma_under_rcu() is a point lookup (mas_walk): it finds the VMA
- * containing a given address but cannot iterate. An RCU-protected
- * maple tree walk with vma_next() (mas_find) is needed first to locate
- * the next VMA's vm_start across any gap.
- *
- * Between the RCU walk and the lock, the VMA may be removed, shrunk,
- * or write-locked. On failure, advance past it using vm_end from the
- * RCU walk. SLAB_TYPESAFE_BY_RCU can make vm_end stale, so fall back
- * to PAGE_SIZE advancement to guarantee forward progress.
- */
+/* Find and lock the next VMA at or after data->next_addr. */
static struct vm_area_struct *
bpf_iter_task_vma_find_next(struct bpf_iter_task_vma_kern_data *data)
{
+ VMA_ITERATOR(vmi, data->mm, data->next_addr);
struct vm_area_struct *vma;
- struct vma_iterator vmi;
- unsigned long start, end;

-retry:
rcu_read_lock();
- vma_iter_init(&vmi, data->mm, data->next_addr);
- vma = vma_next(&vmi);
- if (!vma) {
- rcu_read_unlock();
- return NULL;
- }
- start = vma->vm_start;
- end = vma->vm_end;
+ vma = try_lock_next_vma(data->mm, &vmi, data->next_addr, ULONG_MAX);
rcu_read_unlock();

- vma = lock_vma_under_rcu(data->mm, start);
- if (!vma) {
- if (end <= data->next_addr)
- data->next_addr += PAGE_SIZE;
- else
- data->next_addr = end;
- goto retry;
- }
-
- if (unlikely(vma->vm_end <= data->next_addr)) {
- data->next_addr += PAGE_SIZE;
- vma_end_read(vma);
- goto retry;
- }
-
return vma;
}

@@ -1020,7 +984,7 @@ __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_v
/*
* The verifier only trusts vm_mm and vm_file (see
* BTF_TYPE_SAFE_TRUSTED_OR_NULL in verifier.c). Take a reference
- * on vm_file; vm_mm is already correct because lock_vma_under_rcu()
+ * on vm_file; vm_mm is already correct because try_lock_next_vma()
* verifies vma->vm_mm == mm. All other pointers are untrusted by
* the verifier and left as-is.
*/
--
2.34.1