* [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
* [RFC PATCH 1/2] mm/mmap_lock: add try_lock_next_vma()
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 ` 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
1 sibling, 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>
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@google.com>
Suggested-by: Lorenzo Stoakes <ljs@kernel.org>
Signed-off-by: Chuanhua Han <hanchuanhua@xiaomi.com>
---
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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [RFC PATCH 2/2] bpf: use try_lock_next_vma() in the task_vma iterator
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:39 ` Chuanhua Han
2026-12-05 4:11 ` sashiko-bot
1 sibling, 1 reply; 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>
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@kernel.org>
Signed-off-by: Chuanhua Han <hanchuanhua@xiaomi.com>
---
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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RFC PATCH 1/2] mm/mmap_lock: add try_lock_next_vma()
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
1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-05 3:56 UTC (permalink / raw)
To: Chuanhua Han, 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
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm: try_lock_next_vma() runs synchronous mmdrop() under RCU
read lock
--
commit fd43adadc737249dfd917dd44af112219f9309df
Author: Chuanhua Han <hanchuanhua@xiaomi.com>
mm/mmap_lock: add try_lock_next_vma()
This commit adds try_lock_next_vma() to iterate and read-lock VMAs under
RCU without falling back to mmap_lock, skipping any VMA whose lock cannot
be taken.
> diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c
> index 073c5125b4247..8933735d7bad9 100644
> --- a/mm/mmap_lock.c
> +++ b/mm/mmap_lock.c
> @@ -523,6 +523,68 @@ struct vm_area_struct *lock_next_vma(struct vma_rcu_iter *rvmi,
> return vma;
> }
[ ... ]
> +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);
[Severity: High]
Can vma_start_read() still run synchronous mmdrop() while the caller's
RCU read lock is held?
The commit message relies on commit 44926897b6c2 ("mm/vma: drop mm
asynchronously in vma_start_read()"), which is not in this tree.
vma_start_read() still drops other_mm synchronously:
mm/mmap_lock.c:vma_start_read() {
...
err_unstable:
...
rcu_read_unlock();
mmgrab(other_mm);
vma_refcount_put(vma);
mmdrop(other_mm);
...
}
Because try_lock_next_vma() takes a nested rcu_read_lock(), that single
rcu_read_unlock() leaves the caller's RCU read lock held across
mmdrop(other_mm). If the VMA was reused by another mm whose last
reference drops here, __mmdrop() runs synchronously under RCU.
In non-sleepable contexts such as the BPF task_vma iterator, __mmdrop()
-> mm_free_pgd() -> pgd_free() -> pgd_dtor() on x86 takes plain
spin_lock(&pgd_lock), deadlocking on the same CPU if entered from softirq
or a tracepoint while pgd_lock is held (and sleeping on CONFIG_PREEMPT_RT).
cleanup_lazy_tlbs() also issues synchronous cross-CPU IPIs under RCU.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205033951.1811578-1-hanchuanhua802@gmail.com?part=1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC PATCH 2/2] bpf: use try_lock_next_vma() in the task_vma iterator
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
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-05 4:11 UTC (permalink / raw)
To: Chuanhua Han, 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
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: try_lock_next_vma() runs synchronous __mmdrop() inside RCU
read lock
- [Low] bpf: 32-bit truncation of next_addr in
bpf_iter_task_vma_find_next()
--
commit 775b83d9ac0b9c4a11b6a270f1f445469d3c8aa0
Author: Chuanhua Han <hanchuanhua@xiaomi.com>
bpf: use try_lock_next_vma() in the task_vma iterator
This commit replaces the open-coded RCU maple tree walk and
lock_vma_under_rcu() retry loop in bpf_iter_task_vma_find_next() with a
call to try_lock_next_vma().
> diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
> index 2ee7b3b3c10be..a12c125eef9d1 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,
[ ... ]
> +/* 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();
[Severity: High]
Can calling try_lock_next_vma() under rcu_read_lock() run synchronous
__mmdrop() inside an RCU read-side critical section?
Commit "mm/mmap_lock: add try_lock_next_vma()" takes a nested
rcu_read_lock() around vma_start_read(), assuming commit 44926897b6c2
("mm/vma: drop mm asynchronously in vma_start_read()") is present. In
this tree, vma_start_read() still calls synchronous mmdrop():
mm/mmap_lock.c:vma_start_read() {
...
/* __mmdrop() is a heavy operation, do it after dropping RCU lock. */
rcu_read_unlock();
mmgrab(other_mm);
vma_refcount_put(vma);
mmdrop(other_mm);
...
}
When a SLAB_TYPESAFE_BY_RCU VMA is reused by another mm and that mm drops
its last mm_count reference here, vma_start_read()'s single
rcu_read_unlock() leaves the outer rcu_read_lock() held across __mmdrop().
Because bpf_iter_task_vma_new() only rejects irqs_disabled(), a softirq
BPF program on x86 can deadlock on pgd_lock:
pgd_alloc()
spin_lock(&pgd_lock)
<softirq>
bpf_iter_task_vma_find_next()
rcu_read_lock()
try_lock_next_vma() -> vma_start_read()
mmdrop() -> __mmdrop() -> pgd_dtor()
spin_lock(&pgd_lock) <-- deadlock
Under CONFIG_MMU_LAZY_TLB_SHOOTDOWN, __mmdrop() -> cleanup_lazy_tlbs() ->
on_each_cpu_mask(..., wait=1) also hits WARN_ON_ONCE(!in_task()) in
smp_call_function_many_cond(). Applying commit 44926897b6c2 ("mm/vma:
drop mm asynchronously in vma_start_read()") as a prerequisite would keep
__mmdrop() out of this path.
[ ... ]
> - if (unlikely(vma->vm_end <= data->next_addr)) {
> - data->next_addr += PAGE_SIZE;
> - vma_end_read(vma);
> - goto retry;
> - }
> -
> return vma;
> }
[Severity: Low]
Does removing this check introduce a 32-bit wrap-around regression when
data->next_addr exceeds ULONG_MAX?
bpf_iter_task_vma_new() stores the caller-supplied u64 addr directly into
data->next_addr, which is implicitly truncated to unsigned long in
VMA_ITERATOR() and try_lock_next_vma(). On 32-bit kernels, passing an
addr >= (1ULL << 32) now wraps to a low address and returns VMAs below
addr instead of NULL, whereas the removed 64-bit vma->vm_end <=
data->next_addr check previously prevented returning them.
Guarding data->next_addr >= ULONG_MAX (or rejecting out-of-range addr in
bpf_iter_task_vma_new()) would avoid the truncation.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205033951.1811578-1-hanchuanhua802@gmail.com?part=2
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC PATCH 1/2] mm/mmap_lock: add try_lock_next_vma()
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
1 sibling, 0 replies; 6+ messages in thread
From: bot+bpf-ci @ 2026-12-05 4:15 UTC (permalink / raw)
To: hanchuanhua802, surenb, ljs, liam, akpm, yonghong.song, ast,
daniel, andrii, eddyz87, memxor
Cc: hanchuanhua, vbabka, shakeel.butt, pfalcato, david, jannh,
martin.lau, song, jolsa, emil, ihor.solodrai, baohua, linux-mm,
bpf, linux-kernel, ast, andrii, daniel, martin.lau, eddyz87,
yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3302 bytes --]
> mm/mmap_lock: add try_lock_next_vma()
TL;DR: The changelog relies on commit 44926897b6c2 (mmdrop_async in
vma_start_read()), which the tree may lack. If so, __mmdrop() could run inside
the caller's RCU section, taking pgd_lock on x86; no worse than today.
> mm/mmap_lock: add try_lock_next_vma()
>
> 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 changelog says that vma_start_read() drops the other mm with
mmdrop_async() since commit 44926897b6c2, so that its failure path no
longer runs __mmdrop() or takes pgd_lock.
Is that commit in the tree this series applies to? It is not an ancestor of
this patch or of the end of the series, and the failure path of
vma_start_read() still reads:
/* __mmdrop() is a heavy operation, do it after dropping RCU lock. */
rcu_read_unlock();
mmgrab(other_mm);
vma_refcount_put(vma);
mmdrop(other_mm);
If the VMA found by vma_find() was freed and reused by another mm, can this
call chain still run on x86?
try_lock_next_vma() -> vma_start_read() -> mmdrop(other_mm) -> __mmdrop()
-> mm_free_pgd() -> pgd_free() -> pgd_dtor() -> spin_lock(&pgd_lock)
With the nested rcu_read_lock() in try_lock_next_vma(), that __mmdrop()
would then run inside the caller's RCU read side critical section, which
the comment above was written to avoid.
This doesn't look worse than what bpf_iter_task_vma_find_next() does today
through lock_vma_under_rcu(), but the changelog describes behavior the base
tree does not have, and no later patch in the series adds it.
Should the cover letter or changelog state that the series depends on
44926897b6c2, or should the series wait until that commit is in the target
tree?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47475600323
^ 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