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

From: bot+bpf-ci

Date: Fri Dec 04 2026 - 23:15:18 EST


> 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