[RFC PATCH 4/5] mm/mglru: hand off VMA locks without dropping the PTE lock

From: Chuanhua Han

Date: Tue Dec 01 2026 - 08:13:47 EST


Under mmap_lock, walk_pte_range() moved on to the next VMA in the same
PTE table while holding the PTE lock. Since the previous patch, it
drops the PTE lock at each such handoff, looks the next VMA up from
the root of the VMA tree, locks it, and then maps, locks and
revalidates the table again. Userspace allocators often map many
small adjacent VMAs, for example Scudo [1], the default allocator on
Android, so one PTE table can span many VMAs, and the walk pays this
for each of them.

In the worst case, 512 one-page VMAs in one PTE table, the walk does
little but hand off. On a Pixel 6, aging this layout takes about 40%
less time with this patch, about 220 ns less per VMA boundary.

The handoff does not need to drop the PTE lock:

- The current VMA stays locked until the next one is, and the next
one lies in the same PTE table, so munmap() cannot free the table,
as at the other levels.

- khugepaged collapses an anonymous PMD range under the write lock of
the VMA that covers it, and collapse_pte_mapped_thp(),
retract_page_tables() and MADV_DONTNEED with CONFIG_PT_RECLAIM take
the PTE lock to clear the PMD entry, so none of them can free the
table while the walk holds the current VMA lock and the PTE lock.

So keep the PTE lock, continue a VMA iterator from the current VMA
within the RCU read side critical section of pte_offset_map(), and
read-lock the next walkable VMA with vma_start_read_rcu() before
releasing the current one. A VMA that should_skip_vma() rejects is
unlocked right away: the table is protected by the lock of the
current VMA, not by the skipped one.

The lock is only tried, never waited for. A writer such as mprotect()
holds the VMA write lock and then takes the PTE lock in
change_pte_range(). If the walk waited for the VMA lock while holding
the PTE lock, the two would deadlock. A VMA that cannot be locked is
skipped, as at the other levels.

If the VMA was recycled for another mm, that mm must be dropped
outside the PTE lock and RCU. Fall back to the previous path in that
case: drop the PTE lock, mmdrop(), hand off, and map the table again.

[1] https://llvm.org/docs/ScudoHardenedAllocator.html

Signed-off-by: Chuanhua Han <hanchuanhua@xxxxxxxxxx>
---
mm/vmscan.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 72 insertions(+), 5 deletions(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9235006442b6..34ddddab9935 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3558,6 +3558,58 @@ static bool get_next_vma(unsigned long mask, unsigned long size,
return true;
}

+/*
+ * PTE level variant of get_next_vma() that keeps the PTE lock, so the table
+ * needs no remap. The PTE lock pins the tables, so skipped VMAs are not kept
+ * locked, and the VMA lock is only tried, so it cannot deadlock against a writer.
+ *
+ * @vmi must be at *vm_end, and is only valid within the RCU read side
+ * critical section of pte_offset_map(). On success, it is left at the end
+ * of the VMA handed off to, where the next call continues.
+ *
+ * Return false if nothing is left, or if a recycled VMA left *drop_mm, which
+ * must be mmdrop()ed after dropping the PTE lock and leaving RCU.
+ */
+static bool get_next_vma_rcu(struct mm_walk *args, struct vma_iterator *vmi,
+ unsigned long *vm_start, unsigned long *vm_end,
+ struct mm_struct **drop_mm)
+{
+ struct lru_gen_mm_walk *walk = args->private;
+ unsigned long start = *vm_end;
+ unsigned long end = (*vm_start | ~PMD_MASK) + 1;
+ struct vm_area_struct *vma;
+
+ VM_WARN_ON_ONCE(walk->locked_vma != args->vma);
+
+ /*
+ * Nothing is left in this PTE table, or this is the last table in the
+ * address space and end wrapped to 0, which means no limit below. No
+ * user memory lies in that table.
+ */
+ if (!end || start >= end)
+ return false;
+
+ for (;;) {
+ vma = walk_trylock_next_vma_rcu(args->mm, vmi, start, end, drop_mm);
+ if (!vma)
+ return false;
+
+ if (!should_skip_vma(vma, walk))
+ break;
+
+ vma_end_read(vma);
+ }
+
+ vma_end_read(walk->locked_vma);
+ walk->locked_vma = vma;
+ args->vma = vma;
+
+ *vm_start = max(start, vma->vm_start);
+ *vm_end = min(end - 1, vma->vm_end - 1) + 1;
+
+ return true;
+}
+
static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, unsigned long addr,
struct pglist_data *pgdat)
{
@@ -3674,6 +3726,8 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
unsigned int nr;
pmd_t pmdval;
+ struct mm_struct *drop_mm = NULL;
+ struct vma_iterator vmi;

remap:
pte = pte_offset_map_rw_nolock(args->mm, pmd, start & PMD_MASK, &pmdval, &ptl);
@@ -3690,7 +3744,11 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
return false;
}

+ /* only valid in the RCU section of pte_offset_map(): reset on remap */
+ vma_iter_init(&vmi, args->mm, end);
+
lazy_mmu_mode_enable();
+restart:
for (i = pte_index(start), addr = start; addr != end; i += nr, addr += nr * PAGE_SIZE) {
unsigned long pfn;
struct folio *folio;
@@ -3738,16 +3796,25 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
walk_update_folio(walk, args->vma, walk->lruvec, last, dirty);
last = NULL;

+ if (i < PTRS_PER_PTE &&
+ get_next_vma_rcu(args, &vmi, &start, &end, &drop_mm))
+ goto restart;
+
lazy_mmu_mode_disable();
pte_unmap_unlock(pte, ptl);

/*
- * Hand the VMA lock off to the next VMA in this PTE table. The table
- * may be retracted while the PTE lock is not held: map it again.
+ * The handoff above stopped at a recycled VMA. Drop its mm now that
+ * neither the PTE lock nor RCU is held, then hand off and map the
+ * table again, which may have been retracted meanwhile.
*/
- if (i < PTRS_PER_PTE &&
- get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end))
- goto remap;
+ if (unlikely(drop_mm)) {
+ mmdrop(drop_mm);
+ drop_mm = NULL;
+
+ if (get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end))
+ goto remap;
+ }

return suitable_to_scan(total, young);
}
--
2.34.1