[PATCH v2 1/3] mm/filemap: move the mmap hit check of filemap_map_pages() into helpers
From: Qiliang Yuan
Date: Mon Nov 09 2026 - 05:47:23 EST
filemap_map_pages() lowers mmap_miss for a folio it maps with PTEs at
the faulting address, unless the fault is a retry, the folio was
recently evicted, or the mapping skips the matching mmap_miss increment
in do_sync_mmap_readahead().
The PMD path of filemap_map_pages() returns before that loop, and the
next patch needs the same check there.
Move the check into filemap_mmap_hit() and the decrement into
filemap_count_mmap_hit().
Signed-off-by: Qiliang Yuan <qiliang.yuan@xxxxxxxxx>
---
mm/filemap.c | 57 +++++++++++++++++++++++++++++++++++----------------------
1 file changed, 35 insertions(+), 22 deletions(-)
diff --git a/mm/filemap.c b/mm/filemap.c
index b04be64a5069f..c38f0e4cf6123 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3914,6 +3914,39 @@ static vm_fault_t filemap_map_order0_folio(struct vm_fault *vmf,
return ret;
}
+/*
+ * Whether mapping @folio at the faulting address counts as an mmap hit,
+ * which lowers mmap_miss in do_sync_mmap_readahead().
+ */
+static bool filemap_mmap_hit(struct vm_fault *vmf, struct folio *folio)
+{
+ /* Skip the retry of a fault, which maps the folio its miss read */
+ if (vmf->flags & FAULT_FLAG_TRIED)
+ return false;
+ /*
+ * If there are too many folios that are recently evicted in a file,
+ * they will probably continue to be evicted. In such situation,
+ * read-ahead is only a waste of IO. Don't decrease mmap_miss in this
+ * scenario to make sure we can stop read-ahead.
+ */
+ if (folio_test_workingset(folio))
+ return false;
+ /*
+ * VM_SEQ_READ and VM_EXEC mappings skip the mmap_miss increment in
+ * do_sync_mmap_readahead(), so skip the decrement as well to keep
+ * the counter symmetric.
+ */
+ return !(vmf->vma->vm_flags & (VM_SEQ_READ | VM_EXEC));
+}
+
+static void filemap_count_mmap_hit(struct file *file)
+{
+ unsigned short mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
+
+ if (mmap_miss)
+ WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss - 1);
+}
+
vm_fault_t filemap_map_pages(struct vm_fault *vmf,
pgoff_t start_pgoff, pgoff_t end_pgoff)
{
@@ -3986,28 +4019,8 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
}
ret |= map_ret;
- /*
- * If there are too many folios that are recently evicted
- * in a file, they will probably continue to be evicted.
- * In such situation, read-ahead is only a waste of IO.
- * Don't decrease mmap_miss in this scenario to make sure
- * we can stop read-ahead.
- *
- * VM_SEQ_READ and VM_EXEC mappings skip the mmap_miss
- * increment in do_sync_mmap_readahead(), so skip the
- * decrement here as well to keep the counter symmetric.
- */
- if ((map_ret & VM_FAULT_NOPAGE) &&
- !(vmf->flags & FAULT_FLAG_TRIED) &&
- !folio_test_workingset(folio) &&
- !(vma->vm_flags & (VM_SEQ_READ | VM_EXEC))) {
- unsigned short mmap_miss;
-
- mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
- if (mmap_miss)
- WRITE_ONCE(file->f_ra.mmap_miss,
- mmap_miss - 1);
- }
+ if ((map_ret & VM_FAULT_NOPAGE) && filemap_mmap_hit(vmf, folio))
+ filemap_count_mmap_hit(file);
folio_unlock(folio);
} while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);
--
2.43.0