1 From 6c7f552a48b49a8612786a28a2239fbc24fac289 Mon Sep 17 00:00:00 2001
2 From: Yu Zhao <yuzhao@google.com>
3 Date: Fri, 30 Dec 2022 14:52:51 -0700
4 Subject: [PATCH 19/29] mm: add vma_has_recency()
6 Add vma_has_recency() to indicate whether a VMA may exhibit temporal
7 locality that the LRU algorithm relies on.
9 This function returns false for VMAs marked by VM_SEQ_READ or
10 VM_RAND_READ. While the former flag indicates linear access, i.e., a
11 special case of spatial locality, both flags indicate a lack of temporal
12 locality, i.e., the reuse of an area within a relatively small duration.
14 "Recency" is chosen over "locality" to avoid confusion between temporal
15 and spatial localities.
17 Before this patch, the active/inactive LRU only ignored the accessed bit
18 from VMAs marked by VM_SEQ_READ. After this patch, the active/inactive
19 LRU and MGLRU share the same logic: they both ignore the accessed bit if
20 vma_has_recency() returns false.
22 For the active/inactive LRU, the following fio test showed a [6, 8]%
23 increase in IOPS when randomly accessing mapped files under memory
26 kb=$(awk '/MemTotal/ { print $2 }' /proc/meminfo)
27 kb=$((kb - 8*1024*1024))
29 modprobe brd rd_nr=1 rd_size=$kb
30 dd if=/dev/zero of=/dev/ram0 bs=1M
36 fio --name=test --directory=/mnt/ --ioengine=mmap --numjobs=8 \
37 --size=8G --rw=randrw --time_based --runtime=10m \
40 The discussion that led to this patch is here [1]. Additional test
41 results are available in that thread.
43 [1] https://lore.kernel.org/r/Y31s%2FK8T85jh05wH@google.com/
45 Link: https://lkml.kernel.org/r/20221230215252.2628425-1-yuzhao@google.com
46 Signed-off-by: Yu Zhao <yuzhao@google.com>
47 Cc: Alexander Viro <viro@zeniv.linux.org.uk>
48 Cc: Andrea Righi <andrea.righi@canonical.com>
49 Cc: Johannes Weiner <hannes@cmpxchg.org>
50 Cc: Michael Larabel <Michael@MichaelLarabel.com>
51 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
53 include/linux/mm_inline.h | 9 +++++++++
54 mm/memory.c | 8 ++++----
55 mm/rmap.c | 42 +++++++++++++++++----------------------
57 4 files changed, 35 insertions(+), 29 deletions(-)
59 --- a/include/linux/mm_inline.h
60 +++ b/include/linux/mm_inline.h
61 @@ -578,4 +578,12 @@ pte_install_uffd_wp_if_needed(struct vm_
65 +static inline bool vma_has_recency(struct vm_area_struct *vma)
67 + if (vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))
76 @@ -1435,8 +1435,7 @@ again:
80 - if (pte_young(ptent) &&
81 - likely(!(vma->vm_flags & VM_SEQ_READ)))
82 + if (pte_young(ptent) && likely(vma_has_recency(vma)))
83 mark_page_accessed(page);
85 rss[mm_counter(page)]--;
86 @@ -5170,8 +5169,8 @@ static inline void mm_account_fault(stru
88 static void lru_gen_enter_fault(struct vm_area_struct *vma)
90 - /* the LRU algorithm doesn't apply to sequential or random reads */
91 - current->in_lru_fault = !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ));
92 + /* the LRU algorithm only applies to accesses with recency */
93 + current->in_lru_fault = vma_has_recency(vma);
96 static void lru_gen_exit_fault(void)
99 @@ -823,25 +823,14 @@ static bool folio_referenced_one(struct
103 - if (lru_gen_enabled() && pte_young(*pvmw.pte) &&
104 - !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))) {
105 + if (lru_gen_enabled() && pte_young(*pvmw.pte)) {
106 lru_gen_look_around(&pvmw);
110 if (ptep_clear_flush_young_notify(vma, address,
113 - * Don't treat a reference through
114 - * a sequentially read mapping as such.
115 - * If the folio has been used in another mapping,
116 - * we will catch it; if this other mapping is
117 - * already gone, the unmap path will have set
118 - * the referenced flag or activated the folio.
120 - if (likely(!(vma->vm_flags & VM_SEQ_READ)))
125 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
126 if (pmdp_clear_flush_young_notify(vma, address,
128 @@ -875,7 +864,20 @@ static bool invalid_folio_referenced_vma
129 struct folio_referenced_arg *pra = arg;
130 struct mem_cgroup *memcg = pra->memcg;
132 - if (!mm_match_cgroup(vma->vm_mm, memcg))
134 + * Ignore references from this mapping if it has no recency. If the
135 + * page has been used in another mapping, we will catch it; if this
136 + * other mapping is already gone, the unmap path will have set the
137 + * referenced flag or activated the page in zap_pte_range().
139 + if (!vma_has_recency(vma))
143 + * If we are reclaiming on behalf of a cgroup, skip counting on behalf
144 + * of references from different cgroups.
146 + if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
150 @@ -906,6 +908,7 @@ int folio_referenced(struct folio *folio
152 .anon_lock = folio_lock_anon_vma_read,
154 + .invalid_vma = invalid_folio_referenced_vma,
158 @@ -921,15 +924,6 @@ int folio_referenced(struct folio *folio
163 - * If we are reclaiming on behalf of a cgroup, skip
164 - * counting on behalf of references from different
168 - rwc.invalid_vma = invalid_folio_referenced_vma;
171 rmap_walk(folio, &rwc);
172 *vm_flags = pra.vm_flags;
176 @@ -3766,7 +3766,10 @@ static int should_skip_vma(unsigned long
177 if (is_vm_hugetlb_page(vma))
180 - if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL | VM_SEQ_READ | VM_RAND_READ))
181 + if (!vma_has_recency(vma))
184 + if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL))
187 if (vma == get_gate_vma(vma->vm_mm))