856199574a1c43070319b8b989dbf4970c864fce
[openwrt/staging/ldir.git] /
1 From e604c3ccb4dfbdde2467fccef9bb36170a392695 Mon Sep 17 00:00:00 2001
2 From: "T.J. Alumbaugh" <talumbau@google.com>
3 Date: Wed, 18 Jan 2023 00:18:27 +0000
4 Subject: [PATCH 18/19] UPSTREAM: mm: multi-gen LRU: simplify
5 lru_gen_look_around()
6
7 Update the folio generation in place with or without
8 current->reclaim_state->mm_walk. The LRU lock is held for longer, if
9 mm_walk is NULL and the number of folios to update is more than
10 PAGEVEC_SIZE.
11
12 This causes a measurable regression from the LRU lock contention during a
13 microbencmark. But a tiny regression is not worth the complexity.
14
15 Link: https://lkml.kernel.org/r/20230118001827.1040870-8-talumbau@google.com
16 Change-Id: I9ce18b4f4062e6c1c13c98ece9422478eb8e1846
17 Signed-off-by: T.J. Alumbaugh <talumbau@google.com>
18 Cc: Yu Zhao <yuzhao@google.com>
19 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
20 (cherry picked from commit abf086721a2f1e6897c57796f7268df1b194c750)
21 Bug: 274865848
22 Signed-off-by: T.J. Mercier <tjmercier@google.com>
23 ---
24 mm/vmscan.c | 73 +++++++++++++++++------------------------------------
25 1 file changed, 23 insertions(+), 50 deletions(-)
26
27 --- a/mm/vmscan.c
28 +++ b/mm/vmscan.c
29 @@ -4573,13 +4573,12 @@ static void lru_gen_age_node(struct pgli
30 void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
31 {
32 int i;
33 - pte_t *pte;
34 unsigned long start;
35 unsigned long end;
36 - unsigned long addr;
37 struct lru_gen_mm_walk *walk;
38 int young = 0;
39 - unsigned long bitmap[BITS_TO_LONGS(MIN_LRU_BATCH)] = {};
40 + pte_t *pte = pvmw->pte;
41 + unsigned long addr = pvmw->address;
42 struct folio *folio = pfn_folio(pvmw->pfn);
43 struct mem_cgroup *memcg = folio_memcg(folio);
44 struct pglist_data *pgdat = folio_pgdat(folio);
45 @@ -4596,25 +4595,28 @@ void lru_gen_look_around(struct page_vma
46 /* avoid taking the LRU lock under the PTL when possible */
47 walk = current->reclaim_state ? current->reclaim_state->mm_walk : NULL;
48
49 - start = max(pvmw->address & PMD_MASK, pvmw->vma->vm_start);
50 - end = min(pvmw->address | ~PMD_MASK, pvmw->vma->vm_end - 1) + 1;
51 + start = max(addr & PMD_MASK, pvmw->vma->vm_start);
52 + end = min(addr | ~PMD_MASK, pvmw->vma->vm_end - 1) + 1;
53
54 if (end - start > MIN_LRU_BATCH * PAGE_SIZE) {
55 - if (pvmw->address - start < MIN_LRU_BATCH * PAGE_SIZE / 2)
56 + if (addr - start < MIN_LRU_BATCH * PAGE_SIZE / 2)
57 end = start + MIN_LRU_BATCH * PAGE_SIZE;
58 - else if (end - pvmw->address < MIN_LRU_BATCH * PAGE_SIZE / 2)
59 + else if (end - addr < MIN_LRU_BATCH * PAGE_SIZE / 2)
60 start = end - MIN_LRU_BATCH * PAGE_SIZE;
61 else {
62 - start = pvmw->address - MIN_LRU_BATCH * PAGE_SIZE / 2;
63 - end = pvmw->address + MIN_LRU_BATCH * PAGE_SIZE / 2;
64 + start = addr - MIN_LRU_BATCH * PAGE_SIZE / 2;
65 + end = addr + MIN_LRU_BATCH * PAGE_SIZE / 2;
66 }
67 }
68
69 - pte = pvmw->pte - (pvmw->address - start) / PAGE_SIZE;
70 + /* folio_update_gen() requires stable folio_memcg() */
71 + if (!mem_cgroup_trylock_pages(memcg))
72 + return;
73
74 - rcu_read_lock();
75 arch_enter_lazy_mmu_mode();
76
77 + pte -= (addr - start) / PAGE_SIZE;
78 +
79 for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) {
80 unsigned long pfn;
81
82 @@ -4639,56 +4641,27 @@ void lru_gen_look_around(struct page_vma
83 !folio_test_swapcache(folio)))
84 folio_mark_dirty(folio);
85
86 + if (walk) {
87 + old_gen = folio_update_gen(folio, new_gen);
88 + if (old_gen >= 0 && old_gen != new_gen)
89 + update_batch_size(walk, folio, old_gen, new_gen);
90 +
91 + continue;
92 + }
93 +
94 old_gen = folio_lru_gen(folio);
95 if (old_gen < 0)
96 folio_set_referenced(folio);
97 else if (old_gen != new_gen)
98 - __set_bit(i, bitmap);
99 + folio_activate(folio);
100 }
101
102 arch_leave_lazy_mmu_mode();
103 - rcu_read_unlock();
104 + mem_cgroup_unlock_pages();
105
106 /* feedback from rmap walkers to page table walkers */
107 if (suitable_to_scan(i, young))
108 update_bloom_filter(lruvec, max_seq, pvmw->pmd);
109 -
110 - if (!walk && bitmap_weight(bitmap, MIN_LRU_BATCH) < PAGEVEC_SIZE) {
111 - for_each_set_bit(i, bitmap, MIN_LRU_BATCH) {
112 - folio = pfn_folio(pte_pfn(pte[i]));
113 - folio_activate(folio);
114 - }
115 - return;
116 - }
117 -
118 - /* folio_update_gen() requires stable folio_memcg() */
119 - if (!mem_cgroup_trylock_pages(memcg))
120 - return;
121 -
122 - if (!walk) {
123 - spin_lock_irq(&lruvec->lru_lock);
124 - new_gen = lru_gen_from_seq(lruvec->lrugen.max_seq);
125 - }
126 -
127 - for_each_set_bit(i, bitmap, MIN_LRU_BATCH) {
128 - folio = pfn_folio(pte_pfn(pte[i]));
129 - if (folio_memcg_rcu(folio) != memcg)
130 - continue;
131 -
132 - old_gen = folio_update_gen(folio, new_gen);
133 - if (old_gen < 0 || old_gen == new_gen)
134 - continue;
135 -
136 - if (walk)
137 - update_batch_size(walk, folio, old_gen, new_gen);
138 - else
139 - lru_gen_update_size(lruvec, folio, old_gen, new_gen);
140 - }
141 -
142 - if (!walk)
143 - spin_unlock_irq(&lruvec->lru_lock);
144 -
145 - mem_cgroup_unlock_pages();
146 }
147
148 /******************************************************************************