Re: [RFC PATCH 0/5] mm/mglru: walk page tables under per-VMA locks

From: chuanhua han

Date: Fri Dec 04 2026 - 11:55:58 EST


Hi Lorenzo, Suren,

On Fri, Dec 4, 2026 at 7:22 PM Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx> wrote:
>
> On Fri, Dec 04, 2026 at 06:43:33PM +0800, Barry Song wrote:
> > On Fri, Dec 4, 2026 at 6:39 PM Zicheng Wang <wangzicheng@xxxxxxxxx> wrote:
> > >
> > > Hi Suren, Chuanhua, Lorenzo,
> > >
> > > We hit the same stalls (140-250ms mmap/munmap/mprotect waits on
> > > SurfaceFlinger and Launcher) and have a measurement that may be
> > > relevant to the try_lock_next_vma() direction.
> > >
> > > In 3 dogfood user traces we picked for this problem, each with janky
> > > frames over 500ms, a direct reclaim task doing the aging walk holds
> > > mmap_lock for 140-250ms. Over 99% of that hold is spent runnable,
> > > waiting to be scheduled; the aging walk itself runs for less than 1ms.
> > > Each trace has thousands of direct reclaim events inside the jank window.
> > >
> > > A finer lock narrows who waits, but not how long. The walker runs with
> > > preemption enabled while holding the lock, so a round can be preempted
> > > mid-way and the lock is then held for the whole scheduling delay.
> > >
> > > Since the aging walk itself is already fast (<1ms), could we simply
> > > disable preemption during the walk's lock-hold window instead of
> > > changing the lock? We are testing a ~60-line prototype (no new lock or API)
> > > and will send the patch and data separately—mainly wondering whether
> > > you would prefer bounding that non-preemptible window at PUD or PMD
> > > granularity.
> >
> > Hi Zicheng,
> >
> > Disabling preemption for 1 ms is unlikely to fly upstream, as
> > that's simply too long. Now imagine 200 threads doing direct
> > reclaim, each in a different memcg. That could result in a
> > cumulative preemption-disabled time of 200 ms.
>
> Yup :) In general we don't disable preemption unless we have to, and in general
> I think when you look to a solution like this it's typically indicative that the
> abstraction level of the proposed fix is wrong, i.e. you're asking the wrong
> question :)
>
> IOW this suggests that holding the VMA lock is the right solution.
>
> Since (as I gather) Suren is looking to provide try_lock_next_vma() and update
> the BPF code to reflect this (Suren - let me know if this is accurate), I
> suggest you wait for this work.

Suren, are you already working on this? I'd rather not duplicate it.
If you are, I'll wait and build on yours, and I'm happy to test it :)

If you haven't started yet, I have a version I've been testing. It is
modelled on the BPF task_vma iterator, takes the caller's vma_iterator
and an end address, and never falls back to mmap_lock. It is called
under rcu_read_lock() and never drops it, so the iterator stays valid
across calls. The VMA the iterator found is locked directly with
vma_start_read() instead of being looked up again from the root, and a
VMA that cannot be locked is skipped.

Once locked, the VMA is checked again. If it was detached, or no
longer covers the lowest address of interest (e.g. it was split), 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.

vma_start_read() still leaves RCU on failure, so it takes a nested
rcu_read_lock() around it. Since Lorenzo's mmdrop_async() change, the
failure path only queues the mmdrop, so nothing heavy runs inside the
caller's RCU section. If vma_start_read() later stops dropping RCU on
failure, the nested rcu_read_lock() is no longer needed.

Converting bpf_iter_task_vma_find_next() to it removes the open-coded
retry loop. A debugfs microbenchmark of just the lookup and locking,
locking every VMA of a process in turn (one-page VMAs, so the worst
case), on an i7-14700, median of 5 runs:

VMAs before after
523 31 us 17 us
5023 343 us 197 us
50023 3740 us 2272 us

That is 68 -> 39 ns per VMA at 5023 VMAs.

I could send just those two patches (mm + bpf), with no MGLRU changes.

>
> Another point to be cautious of here is that a pure-VMA lock walk of VMAs is
> very fiddly - you control nothing about what the _other_ VMAs are doing
> i.e. have no stability on that, so you may observe unexpected things in a
> traversal.
>
> Suren has written a lot about this into the VMA lock work around /proc/$pid/maps
> (in both comments and commit msgs and what is asserted in self tests) so I
> suggest having a look at that code in the meantime to assess what would need to
> be taken into account in this approach.

Understood. I'll go through the /proc/$pid/maps work and its selftests
first. Before posting anything for MGLRU again, I'll write down which
concurrent VMA changes the walk can see and how it handles each one.

--
Thanks,
Chuanhua