BPF List
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] mm: add try_lock_next_vma() and use it in the BPF task_vma iterator
@ 2026-12-05  3:39 Chuanhua Han
  2026-12-05  3:39 ` [RFC PATCH 1/2] mm/mmap_lock: add try_lock_next_vma() Chuanhua Han
  2026-12-05  3:39 ` [RFC PATCH 2/2] bpf: use try_lock_next_vma() in the task_vma iterator Chuanhua Han
  0 siblings, 2 replies; 6+ messages in thread
From: Chuanhua Han @ 2026-12-05  3:39 UTC (permalink / raw)
  To: Suren Baghdasaryan, Lorenzo Stoakes, Liam R. Howlett,
	Andrew Morton, Yonghong Song, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi
  Cc: Chuanhua Han, Vlastimil Babka, Shakeel Butt, Pedro Falcato,
	David Hildenbrand, Jann Horn, Martin KaFai Lau, Song Liu,
	Jiri Olsa, Emil Tsalapatis, Ihor Solodrai, Barry Song, linux-mm,
	bpf, linux-kernel

From: Chuanhua Han <hanchuanhua@xiaomi.com>

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@xiaomi.com/
[2] https://lore.kernel.org/linux-mm/CAJuCfpFP_Zno2pN9GkUGJFKpjbe1=YRF8Qk5tWLck3uadgKFWA@mail.gmail.com/
[3] https://lore.kernel.org/linux-mm/aw_fpcuaRgHvMhpW@gremlin/
[4] https://lore.kernel.org/linux-mm/CAJuCfpEWYUfVOYKGzvRJoEsb9ny1SczEg7zkgFRMwHw=cEc26w@mail.gmail.com/

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-05  4:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  3:39 [RFC PATCH 0/2] mm: add try_lock_next_vma() and use it in the BPF task_vma iterator Chuanhua Han
2026-12-05  3:39 ` [RFC PATCH 1/2] mm/mmap_lock: add try_lock_next_vma() Chuanhua Han
2026-12-05  3:56   ` sashiko-bot
2026-12-05  4:15   ` bot+bpf-ci
2026-12-05  3:39 ` [RFC PATCH 2/2] bpf: use try_lock_next_vma() in the task_vma iterator Chuanhua Han
2026-12-05  4:11   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox