eb0181301c545c8ad48df3b8c2394a52f2a3209e
[openwrt/staging/ldir.git] / target / linux / generic / backport-6.1 / 020-v6.3-19-mm-add-vma_has_recency.patch
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()
5
6 Add vma_has_recency() to indicate whether a VMA may exhibit temporal
7 locality that the LRU algorithm relies on.
8
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.
13
14 "Recency" is chosen over "locality" to avoid confusion between temporal
15 and spatial localities.
16
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.
21
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
24 pressure.
25
26 kb=$(awk '/MemTotal/ { print $2 }' /proc/meminfo)
27 kb=$((kb - 8*1024*1024))
28
29 modprobe brd rd_nr=1 rd_size=$kb
30 dd if=/dev/zero of=/dev/ram0 bs=1M
31
32 mkfs.ext4 /dev/ram0
33 mount /dev/ram0 /mnt/
34 swapoff -a
35
36 fio --name=test --directory=/mnt/ --ioengine=mmap --numjobs=8 \
37 --size=8G --rw=randrw --time_based --runtime=10m \
38 --group_reporting
39
40 The discussion that led to this patch is here [1]. Additional test
41 results are available in that thread.
42
43 [1] https://lore.kernel.org/r/Y31s%2FK8T85jh05wH@google.com/
44
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>
52 ---
53 include/linux/mm_inline.h | 9 +++++++++
54 mm/memory.c | 8 ++++----
55 mm/rmap.c | 42 +++++++++++++++++----------------------
56 mm/vmscan.c | 5 ++++-
57 4 files changed, 35 insertions(+), 29 deletions(-)
58
59 --- a/include/linux/mm_inline.h
60 +++ b/include/linux/mm_inline.h
61 @@ -606,5 +606,13 @@ static __always_inline void del_page_fro
62 make_pte_marker(PTE_MARKER_UFFD_WP));
63 #endif
64 }
65 +
66 +static inline bool vma_has_recency(struct vm_area_struct *vma)
67 +{
68 + if (vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))
69 + return false;
70 +
71 + return true;
72 +}
73
74 #endif
75 --- a/mm/memory.c
76 +++ b/mm/memory.c
77 @@ -1353,8 +1354,7 @@ again:
78 force_flush = 1;
79 set_page_dirty(page);
80 }
81 - if (pte_young(ptent) &&
82 - likely(!(vma->vm_flags & VM_SEQ_READ)))
83 + if (pte_young(ptent) && likely(vma_has_recency(vma)))
84 mark_page_accessed(page);
85 }
86 rss[mm_counter(page)]--;
87 @@ -4795,8 +4795,8 @@ static inline void mm_account_fault(stru
88 #ifdef CONFIG_LRU_GEN
89 static void lru_gen_enter_fault(struct vm_area_struct *vma)
90 {
91 - /* the LRU algorithm doesn't apply to sequential or random reads */
92 - current->in_lru_fault = !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ));
93 + /* the LRU algorithm only applies to accesses with recency */
94 + current->in_lru_fault = vma_has_recency(vma);
95 }
96
97 static void lru_gen_exit_fault(void)
98 --- a/mm/rmap.c
99 +++ b/mm/rmap.c
100 @@ -794,25 +794,14 @@ static bool page_referenced_one(struct p
101 }
102
103 if (pvmw.pte) {
104 - if (lru_gen_enabled() && pte_young(*pvmw.pte) &&
105 - !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))) {
106 + if (lru_gen_enabled() && pte_young(*pvmw.pte)) {
107 lru_gen_look_around(&pvmw);
108 referenced++;
109 }
110
111 if (ptep_clear_flush_young_notify(vma, address,
112 - pvmw.pte)) {
113 - /*
114 - * Don't treat a reference through
115 - * a sequentially read mapping as such.
116 - * If the folio has been used in another mapping,
117 - * we will catch it; if this other mapping is
118 - * already gone, the unmap path will have set
119 - * the referenced flag or activated the folio.
120 - */
121 - if (likely(!(vma->vm_flags & VM_SEQ_READ)))
122 - referenced++;
123 - }
124 + pvmw.pte))
125 + referenced++;
126 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
127 if (pmdp_clear_flush_young_notify(vma, address,
128 pvmw.pmd))
129 @@ -846,7 +835,20 @@ static bool invalid_page_referenced_vma(
130 struct page_referenced_arg *pra = arg;
131 struct mem_cgroup *memcg = pra->memcg;
132
133 - if (!mm_match_cgroup(vma->vm_mm, memcg))
134 + /*
135 + * Ignore references from this mapping if it has no recency. If the
136 + * page has been used in another mapping, we will catch it; if this
137 + * other mapping is already gone, the unmap path will have set the
138 + * referenced flag or activated the page in zap_pte_range().
139 + */
140 + if (!vma_has_recency(vma))
141 + return true;
142 +
143 + /*
144 + * If we are reclaiming on behalf of a cgroup, skip counting on behalf
145 + * of references from different cgroups.
146 + */
147 + if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
148 return true;
149
150 return false;
151 @@ -876,6 +878,7 @@ int page_referenced(struct page *page,
152 .arg = (void *)&pra,
153 .anon_lock = folio_lock_anon_vma_read,
154 .try_lock = true,
155 + .invalid_vma = invalid_folio_referenced_vma,
156 };
157
158 *vm_flags = 0;
159 @@ -891,15 +894,6 @@ int page_referenced(struct page *page,
160 return 1;
161 }
162
163 - /*
164 - * If we are reclaiming on behalf of a cgroup, skip
165 - * counting on behalf of references from different
166 - * cgroups
167 - */
168 - if (memcg) {
169 - rwc.invalid_vma = invalid_folio_referenced_vma;
170 - }
171 -
172 rmap_walk(folio, &rwc);
173 *vm_flags = pra.vm_flags;
174
175 --- a/mm/vmscan.c
176 +++ b/mm/vmscan.c
177 @@ -3486,7 +3486,10 @@ static int should_skip_vma(unsigned long
178 if (is_vm_hugetlb_page(vma))
179 return true;
180
181 - if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL | VM_SEQ_READ | VM_RAND_READ))
182 + if (!vma_has_recency(vma))
183 + return true;
184 +
185 + if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL))
186 return true;
187
188 if (vma == get_gate_vma(vma->vm_mm))