6154bbe352899fdaca0dbe273af5687319245534
[openwrt/staging/dedeckeh.git] / target / linux / generic / backport-5.15 / 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 @@ -333,4 +333,13 @@ static __always_inline void del_page_fro
62 update_lru_size(lruvec, page_lru(page), page_zonenum(page),
63 -thp_nr_pages(page));
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 @@ -41,6 +41,7 @@
78
79 #include <linux/kernel_stat.h>
80 #include <linux/mm.h>
81 +#include <linux/mm_inline.h>
82 #include <linux/sched/mm.h>
83 #include <linux/sched/coredump.h>
84 #include <linux/sched/numa_balancing.h>
85 @@ -1353,8 +1354,7 @@ again:
86 force_flush = 1;
87 set_page_dirty(page);
88 }
89 - if (pte_young(ptent) &&
90 - likely(!(vma->vm_flags & VM_SEQ_READ)))
91 + if (pte_young(ptent) && likely(vma_has_recency(vma)))
92 mark_page_accessed(page);
93 }
94 rss[mm_counter(page)]--;
95 @@ -4781,8 +4781,8 @@ static inline void mm_account_fault(stru
96 #ifdef CONFIG_LRU_GEN
97 static void lru_gen_enter_fault(struct vm_area_struct *vma)
98 {
99 - /* the LRU algorithm doesn't apply to sequential or random reads */
100 - current->in_lru_fault = !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ));
101 + /* the LRU algorithm only applies to accesses with recency */
102 + current->in_lru_fault = vma_has_recency(vma);
103 }
104
105 static void lru_gen_exit_fault(void)
106 --- a/mm/rmap.c
107 +++ b/mm/rmap.c
108 @@ -794,25 +794,14 @@ static bool page_referenced_one(struct p
109 }
110
111 if (pvmw.pte) {
112 - if (lru_gen_enabled() && pte_young(*pvmw.pte) &&
113 - !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))) {
114 + if (lru_gen_enabled() && pte_young(*pvmw.pte)) {
115 lru_gen_look_around(&pvmw);
116 referenced++;
117 }
118
119 if (ptep_clear_flush_young_notify(vma, address,
120 - pvmw.pte)) {
121 - /*
122 - * Don't treat a reference through
123 - * a sequentially read mapping as such.
124 - * If the page has been used in another mapping,
125 - * we will catch it; if this other mapping is
126 - * already gone, the unmap path will have set
127 - * PG_referenced or activated the page.
128 - */
129 - if (likely(!(vma->vm_flags & VM_SEQ_READ)))
130 - referenced++;
131 - }
132 + pvmw.pte))
133 + referenced++;
134 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
135 if (pmdp_clear_flush_young_notify(vma, address,
136 pvmw.pmd))
137 @@ -846,7 +835,20 @@ static bool invalid_page_referenced_vma(
138 struct page_referenced_arg *pra = arg;
139 struct mem_cgroup *memcg = pra->memcg;
140
141 - if (!mm_match_cgroup(vma->vm_mm, memcg))
142 + /*
143 + * Ignore references from this mapping if it has no recency. If the
144 + * page has been used in another mapping, we will catch it; if this
145 + * other mapping is already gone, the unmap path will have set the
146 + * referenced flag or activated the page in zap_pte_range().
147 + */
148 + if (!vma_has_recency(vma))
149 + return true;
150 +
151 + /*
152 + * If we are reclaiming on behalf of a cgroup, skip counting on behalf
153 + * of references from different cgroups.
154 + */
155 + if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
156 return true;
157
158 return false;
159 @@ -876,6 +878,7 @@ int page_referenced(struct page *page,
160 .rmap_one = page_referenced_one,
161 .arg = (void *)&pra,
162 .anon_lock = page_lock_anon_vma_read,
163 + .invalid_vma = invalid_page_referenced_vma,
164 };
165
166 *vm_flags = 0;
167 @@ -891,15 +894,6 @@ int page_referenced(struct page *page,
168 return 1;
169 }
170
171 - /*
172 - * If we are reclaiming on behalf of a cgroup, skip
173 - * counting on behalf of references from different
174 - * cgroups
175 - */
176 - if (memcg) {
177 - rwc.invalid_vma = invalid_page_referenced_vma;
178 - }
179 -
180 rmap_walk(page, &rwc);
181 *vm_flags = pra.vm_flags;
182
183 --- a/mm/vmscan.c
184 +++ b/mm/vmscan.c
185 @@ -3486,7 +3486,10 @@ static int should_skip_vma(unsigned long
186 if (is_vm_hugetlb_page(vma))
187 return true;
188
189 - if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL | VM_SEQ_READ | VM_RAND_READ))
190 + if (!vma_has_recency(vma))
191 + return true;
192 +
193 + if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL))
194 return true;
195
196 if (vma == get_gate_vma(vma->vm_mm))