[PATCH] mm: filemap: avoid unnecessary major faults in filemap_fault()

From: Peng Zhang
Date: Thu Nov 23 2023 - 21:31:22 EST


From: ZhangPeng <zhangpeng362@xxxxxxxxxx>

The major fault occurred when using mlockall(MCL_CURRENT | MCL_FUTURE)
in application, which leading to an unexpected performance issue[1].

This caused by temporarily cleared pte during a read/modify/write update
of the pte, eg, do_numa_page()/change_pte_range().

For the data segment of the user-mode program, the global variable area
is a private mapping. After the pagecache is loaded, the private anonymous
page is generated after the COW is triggered. Mlockall can lock COW pages
(anonymous pages), but the original file pages cannot be locked and may
be reclaimed. If the global variable (private anon page) is accessed when
vmf->pte is zeroed in numa fault, a file page fault will be triggered.

At this time, the original private file page may have been reclaimed.
If the page cache is not available at this time, a major fault will be
triggered and the file will be read, causing additional overhead.

Fix this by rechecking the pte by holding ptl in filemap_fault() before
triggering a major fault.

We tested the performance of file private mapping page fault
(page_fault2.c of will-it-scale [2]) and file shared mapping page fault
(page_fault3.c of will-it-scale). The difference in performance (in
operations per second) before and after patch applied is about 0.7% on a
x86 physical machine.

[1] https://lore.kernel.org/linux-mm/9e62fd9a-bee0-52bf-50a7-498fa17434ee@xxxxxxxxxx/
[2] https://github.com/antonblanchard/will-it-scale/tree/master

Suggested-by: "Huang, Ying" <ying.huang@xxxxxxxxx>
Signed-off-by: ZhangPeng <zhangpeng362@xxxxxxxxxx>
Signed-off-by: Kefeng Wang <wangkefeng.wang@xxxxxxxxxx>
Reviewed-by: Yin Fengwei <fengwei.yin@xxxxxxxxx>
---
RFC->v1: - Update commit message and add RB from Yin Fengwei
- Add error handling when ptep == NULL per Huang, Ying and
Matthew Wilcox

mm/filemap.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/mm/filemap.c b/mm/filemap.c
index 71f00539ac00..f3dcabdbc810 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3226,6 +3226,20 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
mapping_locked = true;
}
} else {
+ pte_t *ptep = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
+ vmf->address, &vmf->ptl);
+ if (unlikely(!ptep))
+ return VM_FAULT_NOPAGE;
+ /*
+ * Recheck pte with ptl locked as the pte can be cleared
+ * temporarily during a read/modify/write update.
+ */
+ if (unlikely(!pte_none(ptep_get(ptep))))
+ ret = VM_FAULT_NOPAGE;
+ pte_unmap_unlock(ptep, vmf->ptl);
+ if (unlikely(ret))
+ return ret;
+
/* No page in the page cache at all */
count_vm_event(PGMAJFAULT);
count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
--
2.25.1