1 From 70d216c71ff5c5b17dd1da6294f97b91fb6aba7a 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 10/19] UPSTREAM: 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 Change-Id: I291dcb795197659e40e46539cd32b857677c34ad
47 Signed-off-by: Yu Zhao <yuzhao@google.com>
48 Cc: Alexander Viro <viro@zeniv.linux.org.uk>
49 Cc: Andrea Righi <andrea.righi@canonical.com>
50 Cc: Johannes Weiner <hannes@cmpxchg.org>
51 Cc: Michael Larabel <Michael@MichaelLarabel.com>
52 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
53 (cherry picked from commit 8788f6781486769d9598dcaedc3fe0eb12fc3e59)
55 Signed-off-by: T.J. Mercier <tjmercier@google.com>
57 include/linux/mm_inline.h | 8 ++++++++
58 mm/memory.c | 7 +++----
59 mm/rmap.c | 42 +++++++++++++++++----------------------
61 4 files changed, 33 insertions(+), 29 deletions(-)
63 --- a/include/linux/mm_inline.h
64 +++ b/include/linux/mm_inline.h
65 @@ -595,4 +595,12 @@ pte_install_uffd_wp_if_needed(struct vm_
69 +static inline bool vma_has_recency(struct vm_area_struct *vma)
71 + if (vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))
80 @@ -1445,8 +1445,7 @@ again:
84 - if (pte_young(ptent) &&
85 - likely(!(vma->vm_flags & VM_SEQ_READ)))
86 + if (pte_young(ptent) && likely(vma_has_recency(vma)))
87 mark_page_accessed(page);
89 rss[mm_counter(page)]--;
90 @@ -5199,8 +5198,8 @@ static inline void mm_account_fault(stru
92 static void lru_gen_enter_fault(struct vm_area_struct *vma)
94 - /* the LRU algorithm doesn't apply to sequential or random reads */
95 - current->in_lru_fault = !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ));
96 + /* the LRU algorithm only applies to accesses with recency */
97 + current->in_lru_fault = vma_has_recency(vma);
100 static void lru_gen_exit_fault(void)
103 @@ -823,25 +823,14 @@ static bool folio_referenced_one(struct
107 - if (lru_gen_enabled() && pte_young(*pvmw.pte) &&
108 - !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))) {
109 + if (lru_gen_enabled() && pte_young(*pvmw.pte)) {
110 lru_gen_look_around(&pvmw);
114 if (ptep_clear_flush_young_notify(vma, address,
117 - * Don't treat a reference through
118 - * a sequentially read mapping as such.
119 - * If the folio has been used in another mapping,
120 - * we will catch it; if this other mapping is
121 - * already gone, the unmap path will have set
122 - * the referenced flag or activated the folio.
124 - if (likely(!(vma->vm_flags & VM_SEQ_READ)))
129 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
130 if (pmdp_clear_flush_young_notify(vma, address,
132 @@ -875,7 +864,20 @@ static bool invalid_folio_referenced_vma
133 struct folio_referenced_arg *pra = arg;
134 struct mem_cgroup *memcg = pra->memcg;
136 - if (!mm_match_cgroup(vma->vm_mm, memcg))
138 + * Ignore references from this mapping if it has no recency. If the
139 + * folio has been used in another mapping, we will catch it; if this
140 + * other mapping is already gone, the unmap path will have set the
141 + * referenced flag or activated the folio in zap_pte_range().
143 + if (!vma_has_recency(vma))
147 + * If we are reclaiming on behalf of a cgroup, skip counting on behalf
148 + * of references from different cgroups.
150 + if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
154 @@ -906,6 +908,7 @@ int folio_referenced(struct folio *folio
156 .anon_lock = folio_lock_anon_vma_read,
158 + .invalid_vma = invalid_folio_referenced_vma,
162 @@ -921,15 +924,6 @@ int folio_referenced(struct folio *folio
167 - * If we are reclaiming on behalf of a cgroup, skip
168 - * counting on behalf of references from different
172 - rwc.invalid_vma = invalid_folio_referenced_vma;
175 rmap_walk(folio, &rwc);
176 *vm_flags = pra.vm_flags;
180 @@ -3778,7 +3778,10 @@ static int should_skip_vma(unsigned long
181 if (is_vm_hugetlb_page(vma))
184 - if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL | VM_SEQ_READ | VM_RAND_READ))
185 + if (!vma_has_recency(vma))
188 + if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL))
191 if (vma == get_gate_vma(vma->vm_mm))