CI: build: fix parse toolchain step failing for git strict rules
[openwrt/openwrt.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 @@ -578,4 +578,12 @@ pte_install_uffd_wp_if_needed(struct vm_
62 #endif
63 }
64
65 +static inline bool vma_has_recency(struct vm_area_struct *vma)
66 +{
67 + if (vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))
68 + return false;
69 +
70 + return true;
71 +}
72 +
73 #endif
74 --- a/mm/memory.c
75 +++ b/mm/memory.c
76 @@ -1435,8 +1435,7 @@ again:
77 force_flush = 1;
78 set_page_dirty(page);
79 }
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);
84 }
85 rss[mm_counter(page)]--;
86 @@ -5170,8 +5169,8 @@ static inline void mm_account_fault(stru
87 #ifdef CONFIG_LRU_GEN
88 static void lru_gen_enter_fault(struct vm_area_struct *vma)
89 {
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);
94 }
95
96 static void lru_gen_exit_fault(void)
97 --- a/mm/rmap.c
98 +++ b/mm/rmap.c
99 @@ -823,25 +823,14 @@ static bool folio_referenced_one(struct
100 }
101
102 if (pvmw.pte) {
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);
107 referenced++;
108 }
109
110 if (ptep_clear_flush_young_notify(vma, address,
111 - pvmw.pte)) {
112 - /*
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.
119 - */
120 - if (likely(!(vma->vm_flags & VM_SEQ_READ)))
121 - referenced++;
122 - }
123 + pvmw.pte))
124 + referenced++;
125 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
126 if (pmdp_clear_flush_young_notify(vma, address,
127 pvmw.pmd))
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;
131
132 - if (!mm_match_cgroup(vma->vm_mm, memcg))
133 + /*
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().
138 + */
139 + if (!vma_has_recency(vma))
140 + return true;
141 +
142 + /*
143 + * If we are reclaiming on behalf of a cgroup, skip counting on behalf
144 + * of references from different cgroups.
145 + */
146 + if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
147 return true;
148
149 return false;
150 @@ -906,6 +908,7 @@ int folio_referenced(struct folio *folio
151 .arg = (void *)&pra,
152 .anon_lock = folio_lock_anon_vma_read,
153 .try_lock = true,
154 + .invalid_vma = invalid_folio_referenced_vma,
155 };
156
157 *vm_flags = 0;
158 @@ -921,15 +924,6 @@ int folio_referenced(struct folio *folio
159 return 1;
160 }
161
162 - /*
163 - * If we are reclaiming on behalf of a cgroup, skip
164 - * counting on behalf of references from different
165 - * cgroups
166 - */
167 - if (memcg) {
168 - rwc.invalid_vma = invalid_folio_referenced_vma;
169 - }
170 -
171 rmap_walk(folio, &rwc);
172 *vm_flags = pra.vm_flags;
173
174 --- a/mm/vmscan.c
175 +++ b/mm/vmscan.c
176 @@ -3766,7 +3766,10 @@ static int should_skip_vma(unsigned long
177 if (is_vm_hugetlb_page(vma))
178 return true;
179
180 - if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL | VM_SEQ_READ | VM_RAND_READ))
181 + if (!vma_has_recency(vma))
182 + return true;
183 +
184 + if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL))
185 return true;
186
187 if (vma == get_gate_vma(vma->vm_mm))