[RFC PATCH 0/5] mm/mglru: walk page tables under per-VMA locks
From: Chuanhua Han
Date: Tue Dec 01 2026 - 08:13:03 EST
The MGLRU aging walk holds mmap_lock for read while it walks the page
tables of an mm. A writer to that mm, such as mmap() or mprotect(),
waits for the walk, and page faults that fall back to mmap_lock queue
behind the writer. When mmap_read_trylock() fails, the walk skips the
whole mm. The admin guide already warns that the page table walk "can
theoretically worsen lock contention (mmap_lock)".
This series walks the page tables under per-VMA read locks instead. A
writer then waits only while the walk holds the lock of the VMA it
modifies, and a VMA that cannot be locked is skipped on its own rather
than with the rest of the mm.
On Android, these waits make the foreground app drop frames. kswapd
often ages the mm of the foreground app, because the walk visits only
the mms whose tasks have run since the last walk, and the app keeps
running and mapping memory while it scrolls. On a Pixel 6 running a
daily-use script, the main thread or render thread of the foreground
app waited 9-30 ms for the walk in frames that missed their deadline.
With this series, the walk takes no mmap_lock, and in a
microbenchmark, mprotect() drops from 41.2 ms to 0.14 ms on average.
How the walk works
==================
Before this series, walk_mm() holds mmap_lock from the start of a
round until a PUD boundary where need_resched() is true or the batch
is full, or until the end of the mm. Within a PTE, PMD or PUD table,
get_next_vma() moves to the next VMA without walking down from the
PGD again. A writer waits until the round ends.
With this series, each round finds the next VMA under RCU, skips it if
its flags rule it out, tries to read-lock it, and walks it with
walk_page_range_vma(). No lock is held between rounds. Within a table,
the walk hands the VMA lock off to the next VMA in the same table: it
locks the next one, then unlocks the current one:
walk |---- VMA A ----|---- VMA B ----|---- VMA C ----|
A locked [===============]
B locked [===================]
C locked [==================]
writer to C ^ waits for C
writer to A ^ no wait
Design
======
- Page table lifetime. As long as a locked VMA maps part of a PUD or
PMD table, free_pgtables() keeps that table, so the handoff locks
the next VMA before releasing the current one, and looks for it
only within the current table. Patch 3 shows the race with munmap()
this prevents. PTE tables can be freed without a VMA write lock, so
patch 3 maps them again and revalidates them after a handoff, and
patch 4 keeps the PTE lock across it instead.
- mm lifetime. exit_mmap() frees the VMA tree without waiting for RCU,
so the walk pins mm_users with mmget_not_zero() and drops it with
mmput_async(). It stops before the next PTE table once it holds the
last reference. See question 4.
Alternatives considered
=======================
1. Turning off the page table walk, LRU_GEN_MM_WALK in
/sys/kernel/mm/lru_gen/enabled. Aging then finds young PTEs only
through the rmap, which commit bd74fdaea146 ("mm: multi-gen LRU:
support page table walks") found more costly.
2. Yielding mmap_lock when it is contended. A writer would still wait
until the walk reaches its next check, the walk would compete with
writers for mmap_lock again, and an mm whose trylock fails would
still be skipped whole.
3. Falling back to mmap_lock for a VMA that cannot be locked, as
/proc/PID/maps does. Aging does not need to scan every VMA in every
walk, and it would make reclaim wait for writers again, which this
series avoids.
Series layout
=============
1-2. Preparation, no functional change. Patch 1 adds
vma_start_read_rcu(), which, unlike vma_start_read(), stays in
the RCU read side critical section and does not mmdrop() when it
fails. Patch 2 splits out the VMA flag checks that can be done
before the VMA is locked.
3. Walk each mm under per-VMA locks instead of mmap_lock.
4. Keep the PTE lock across a handoff at the PTE level. This is an
optimization: patches 1-3 are complete without it.
5. Update the MGLRU design doc and admin guide.
Questions for reviewers
=======================
1. Skipping VMAs that cannot be locked. A VMA that is modified often
may be skipped by several consecutive walks, while the base kernel
skips the whole mm only when mmap_read_trylock() fails. The walk
could retry the skipped VMAs once at its end. Is skipping acceptable
for aging?
2. Patch 4 searches the VMA tree and tries to lock the next VMA while
holding the PTE lock. Is its gain worth the extra locking
complexity, or should the series keep the handoff of patch 3?
3. vma_start_read_rcu() returns the mm of a recycled VMA through
@drop_mm, for the caller to mmdrop() outside RCU. Is an out
parameter acceptable?
4. Pinning mm_users. Commit d14514c66cb9 ("mm/vmscan: prevent MGLRU
reclaim from pinning address space") stopped the walk from pinning
mm_users, so that reclaim would not delay the freeing of an exiting
process's memory. This series pins it again for the whole walk of
each mm, across the cond_resched() between rounds, and stops before
the next PTE table once the process exits. Until that walk ends, the
OOM reaper and process_mrelease() still see the elevated mm_users
and skip the mm. Two alternatives avoid the pin:
a. Take mmap_read_trylock() around each lookup of the next VMA,
and drop it once the VMA is locked.
b. Keep one VMA locked from the end of one round to the start of
the next, so that only the first lookup in each mm needs
mmap_read_trylock() or a pin.
Testing
=======
Unless noted otherwise, all tests ran on a Pixel 6 (Google Tensor,
arm64, 39-bit VA, 8 CPUs, 8 GB of memory, 90 Hz display) with an
Android mainline kernel based on v7.2, comparing it with and without
this series backported.
- Daily-use script: 30 apps launched cold and scrolled, the camera
every 5 apps, and 2 games, about 10 minutes per run. Lock waits
were traced with the mmap_lock tracepoints in 18 runs on the base
kernel.
The app must finish each frame within 16.6 ms of its start, longer
than the 11.1 ms refresh period at 90 Hz because frames are
pipelined. A frame finished later is shown late, and the user sees
a stutter. In 5 of the 18 runs, 6 frames, all during scrolling,
were late mainly because the main thread or render thread of the
foreground app waited for the walk, for at least half of the time by
which the frame was late:
thread lock wait missed by missed by walk held
(ms) (ms) without wait (ms)
main read 14.0 13.3 on time 14.8
main read 20.7 12.0 on time 32.6
main read 21.5 14.2 on time 34.3
render write 29.9 35.3 5.4 ms 36.5
render write 8.9 10.6 1.7 ms 26.9
main write 17.1 34.0 16.9 ms 33.9
The render threads waited in mmap() while drawing. Each main thread
that waited for the read lock queued behind a writer in the same
process. These frames normally took 5-11 ms, so a wait first used
up the slack, which is why a wait can be longer than the miss.
Preemption makes the waits longer: on the loaded phone, kswapd spent
about as much time waiting for a CPU as running, and it keeps
holding mmap_lock while it waits. In one case it held mmap_lock for
39.1 ms, 22.9 ms of it runnable but not running.
With this series, the walk takes no mmap_lock, so none of these
waits remain, and a preempted walk holds the lock of one VMA only.
- Lock contention microbenchmark, with the CPU governor at
performance. The benchmark maps two adjacent 512 MB anonymous VMAs
that fill one PUD-aligned 1 GB range, and a separate 10 MB VMA, C.
In each of 100 iterations, it reads every page of the three VMAs,
then one thread ages the memcg, and 1 ms later another thread calls
mprotect() on C:
base patched
mprotect() avg (ms) 41.2 0.14
mprotect() max (ms) 50.8 0.18
On the base kernel, the walk drops mmap_lock only at a PUD
boundary, after it has walked both 512 MB VMAs. The aging time is
about the same on both kernels.
- Aging cost, with the CPU governor at performance. Each case maps a
fixed VMA layout in its own memcg, reads all of its pages, and times
one write of "+ memcg 0 max_seq" to /sys/kernel/debug/lru_gen.
Each kernel was booted 3 times, alternating, with 4 sets of 30
agings per case on each boot.
Some sets ran slower, on both kernels and also with the memory
controller frequency pinned.
mean of 12 sets base patched
Typical layouts:
5000 VMAs, mixed, app-like 16.9 ms 18.2 ms
one 512 MB VMA 18.7 ms 20.4 ms
Worst case, one PTE walked per VMA boundary:
512 x 4 KB VMAs, one PTE table 137 us 151 us
512 x 4 KB VMAs, one per PMD 249 us 297 us
For the typical layouts, the change is within the run-to-run
variation, which the slow sets make large: the 90% confidence
intervals of the difference include zero. The single 512 MB VMA has
no VMA boundary, so the page table walk itself is not measurably
slower.
The worst cases consist of single-page VMAs, so the walk does
little but hand off, and they bound the cost of the per-VMA lock:
about 25 ns per VMA boundary at the PTE level and 90 ns at the PMD
level. The PTE level costs less because its handoff continues the
VMA iterator, instead of searching the tree from the root for each
boundary as the base kernel does. On x86_64 (i7-14700, v7.4-rc3),
the PTE level shows no measurable cost, the PMD level about 70 ns,
and the PUD level, which is folded on the Pixel 6, about 80 ns. With
5000 app-like VMAs, this would add at most 0.5 ms to a 16.9 ms walk.
Chuanhua Han (5):
mm/mmap_lock: add vma_start_read_rcu() helper
mm/mglru: split the VMA flag checks out of should_skip_vma()
mm/mglru: walk page tables under per-VMA locks
mm/mglru: hand off VMA locks without dropping the PTE lock
mm/mglru: update the docs for the per-VMA lock walk
Documentation/admin-guide/mm/multigen_lru.rst | 3 +-
Documentation/mm/multigen_lru.rst | 22 +-
include/linux/mmap_lock.h | 3 +
include/linux/mmzone.h | 3 +
mm/mmap_lock.c | 75 +++-
mm/vmscan.c | 373 +++++++++++++++---
6 files changed, 405 insertions(+), 74 deletions(-)
base-commit: 7d1a16abe4cc5dccd21e3d6262e44433a5885d27
--
2.34.1