[RFC PATCH 2/5] mm/mglru: split the VMA flag checks out of should_skip_vma()
From: Chuanhua Han
Date: Tue Dec 01 2026 - 08:13:09 EST
In preparation for walking each mm under per-VMA locks, move the checks
that depend only on the VMA flags into should_skip_vma_flags(), so that
the walk can skip a VMA before trying to lock it, instead of locking and
unlocking VMAs it skips anyway.
Before the VMA is locked, its flags are the only field that is safe to
read. vm_area_cachep is SLAB_TYPESAFE_BY_RCU, so as long as the walk
stays in the RCU read side critical section in which it found the VMA,
the memory stays a vm_area_struct and the flags word can be read, though
the value may be stale. The checks that follow pointers, such as vm_file
and its mapping, need the VMA lock. A stale flags value only makes the
walk skip a VMA it could have walked, or try to lock one it will skip,
and should_skip_vma() checks the VMA again once it is locked.
vma_has_recency() tests both the flags and vm_file, and is also used by
the rmap path, so open code its two halves here instead of splitting the
shared helper.
Change should_skip_vma() to take the VMA and the walk, instead of the
(start, end, mm_walk) arguments of a test_walk callback, so that the
new walk can call it directly. Add test_walk_skip_vma() as the
test_walk callback of the existing walk, which the next patch removes.
No functional change.
Signed-off-by: Chuanhua Han <hanchuanhua@xxxxxxxxxx>
---
mm/vmscan.c | 37 +++++++++++++++++++++++++++----------
1 file changed, 27 insertions(+), 10 deletions(-)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index bc7579d47f9b..5820d6fdb452 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -3361,22 +3361,33 @@ static void reset_batch_size(struct lru_gen_mm_walk *walk)
lruvec_unlock_irq(lruvec);
}
-static int should_skip_vma(unsigned long start, unsigned long end, struct mm_walk *args)
+/* The checks of should_skip_vma() that only need the VMA flags. */
+static inline bool should_skip_vma_flags(const vma_flags_t *flags)
{
- struct address_space *mapping;
- struct vm_area_struct *vma = args->vma;
- struct lru_gen_mm_walk *walk = args->private;
+ if (!vma_flags_test_any_mask(flags, VMA_ACCESS_FLAGS))
+ return true;
- if (!vma_is_accessible(vma))
+ if (vma_flags_is_hugetlb(flags))
return true;
- if (vma_is_hugetlb(vma))
+ /* the flags half of vma_has_recency() */
+ if (vma_flags_test_any(flags, VMA_SEQ_READ_BIT, VMA_RAND_READ_BIT))
return true;
- if (!vma_has_recency(vma))
+ return vma_flags_test(flags, VMA_LOCKED_BIT) ||
+ !vma_flags_is_mm_backed(flags);
+}
+
+static inline bool should_skip_vma(struct vm_area_struct *vma,
+ struct lru_gen_mm_walk *walk)
+{
+ struct address_space *mapping;
+
+ if (should_skip_vma_flags(&vma->flags))
return true;
- if (vma_test(vma, VMA_LOCKED_BIT) || !vma_is_mm_backed(vma))
+ /* the vm_file half of vma_has_recency() */
+ if (vma->vm_file && (vma->vm_file->f_mode & FMODE_NOREUSE))
return true;
if (vma == get_gate_vma(vma->vm_mm))
@@ -3402,6 +3413,12 @@ static int should_skip_vma(unsigned long start, unsigned long end, struct mm_wal
return !mapping->a_ops->read_folio;
}
+static int test_walk_skip_vma(unsigned long start, unsigned long end,
+ struct mm_walk *args)
+{
+ return should_skip_vma(args->vma, args->private);
+}
+
/*
* Some userspace memory allocators map many single-page VMAs. Instead of
* returning back to the PGD table for each of such VMAs, finish an entire PMD
@@ -3421,7 +3438,7 @@ static bool get_next_vma(unsigned long mask, unsigned long size, struct mm_walk
if (end && end <= args->vma->vm_start)
return false;
- if (should_skip_vma(args->vma->vm_start, args->vma->vm_end, args))
+ if (should_skip_vma(args->vma, args->private))
continue;
*vm_start = max(start, args->vma->vm_start);
@@ -3826,7 +3843,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
static void walk_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk)
{
static const struct mm_walk_ops mm_walk_ops = {
- .test_walk = should_skip_vma,
+ .test_walk = test_walk_skip_vma,
.p4d_entry = walk_pud_range,
.walk_lock = PGWALK_RDLOCK,
};
--
2.34.1