[RFC PATCH 0/2] mm: add try_lock_next_vma() and use it in the BPF task_vma iterator

From: Chuanhua Han

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


From: Chuanhua Han <hanchuanhua@xxxxxxxxxx>

This series adds try_lock_next_vma(), a variant of lock_next_vma()
that never falls back to mmap_lock and skips a VMA it cannot lock, and
converts the BPF task_vma iterator to it.

In the discussion of the RFC "mm/mglru: walk page tables under per-VMA
locks" [1], Suren suggested a lock_next_vma() variant that does not fall
back to mmap_lock [2], and Lorenzo suggested modelling it on the BPF
task_vma iterator and naming it try_lock_next_vma() [3]. This replaces
the vma_start_read_rcu() helper from that RFC. Suren asked for these two
patches on their own [4], to see whether they fit his planned cleanup of
the VMA locking APIs. I'm happy to rework or drop them in favour of that
work.

try_lock_next_vma() takes the caller's vma_iterator and an end address,
is called under rcu_read_lock() and never drops it, so the iterator
stays valid across calls. It locks the VMA the iterator found with
vma_start_read() instead of looking it up again from the root. Since
vma_start_read() leaves RCU on failure, it takes a nested
rcu_read_lock() around it. This relies on commit 44926897b6c2
("mm/vma: drop mm asynchronously in vma_start_read()"), which is in
mm-hotfixes-unstable, so this series is based on mm-unstable with that
commit applied.

Once locked, the VMA is checked again. If it was detached, or no longer
covers the lowest address of interest, that range is looked up again.
If only its end moved, the iterator is moved to the new end. So only a
VMA that cannot be locked, or one mapped into a range already searched,
is missed. The BPF iterator already skips a VMA it cannot lock, so it
keeps that behaviour.

Patch 2 has the numbers. In short, a debugfs microbenchmark of only the
lookup and locking, over one-page VMAs (the worst case) on an
i7-14700, goes from 68 to 39 ns per VMA at 5023 VMAs (-43%), and
-38% to -45% across all sizes, with turbo on or off. Under KVM, a BPF
program that walks all VMAs of a task with bpf_for_each(task_vma) gets
about 37% faster at 5037 VMAs (504 -> 316 us per pass).

Testing:
- Performance: the microbenchmark in patch 2 was run on bare metal
(i7-14700), and the end-to-end BPF benchmark under KVM on x86_64.
Both only cover the uncontended case.
- Functional, on x86_64 under KVM: the BPF selftests iters/task_vma,
bpf_iter/task_vma, task_vma_dead_task and find_vma pass with
PROVE_LOCKING and PROVE_RCU, with no splats.
- Stress, on x86_64 under KVM: a BPF program walks all VMAs of a task
in a loop while 2 or 4 threads split and merge random VMAs with
mprotect(). No splats, also with PROVE_LOCKING and PROVE_RCU. The
iterator resumes at the end of the VMA it returned last, so if that
VMA is merged with a neighbour in between, the next VMA returned
overlaps it or even starts before it. This happens without the series
too, at a similar rate: with 2000 VMAs and 2 writers, without
lockdep, about 1 in 10000 passes with the series and 2 in 10000
without it.

I used an LLM (Claude Opus 5.5) as an assistant throughout this
series. I have reviewed all of it, double checked the results, and take
full responsibility for it.

[1] https://lore.kernel.org/linux-mm/20261201131135.2914944-1-hanchuanhua@xxxxxxxxxx/
[2] https://lore.kernel.org/linux-mm/CAJuCfpFP_Zno2pN9GkUGJFKpjbe1=YRF8Qk5tWLck3uadgKFWA@xxxxxxxxxxxxxx/
[3] https://lore.kernel.org/linux-mm/aw_fpcuaRgHvMhpW@gremlin/
[4] https://lore.kernel.org/linux-mm/CAJuCfpEWYUfVOYKGzvRJoEsb9ny1SczEg7zkgFRMwHw=cEc26w@xxxxxxxxxxxxxx/

Chuanhua Han (2):
mm/mmap_lock: add try_lock_next_vma()
bpf: use try_lock_next_vma() in the task_vma iterator

include/linux/mmap_lock.h | 4 +++
kernel/bpf/task_iter.c | 44 +++------------------------
mm/mmap_lock.c | 62 +++++++++++++++++++++++++++++++++++++++
3 files changed, 70 insertions(+), 40 deletions(-)


base-commit: 7d1a16abe4cc5dccd21e3d6262e44433a5885d27
prerequisite-patch-id: 5dff458c923da4b2ce990cf4e1f009e7043d88bf
--
2.34.1