ffdebafa2cecd18b317a3aa1b221d9cb7e755a08
[openwrt/staging/981213.git] /
1 From 6f315879ad750391a0b1fab8c9170bc054a5f5d7 Mon Sep 17 00:00:00 2001
2 From: Yu Zhao <yuzhao@google.com>
3 Date: Tue, 15 Nov 2022 18:38:07 -0700
4 Subject: [PATCH 14/29] mm: multi-gen LRU: retry pages written back while
5 isolated
6
7 The page reclaim isolates a batch of pages from the tail of one of the
8 LRU lists and works on those pages one by one. For a suitable
9 swap-backed page, if the swap device is async, it queues that page for
10 writeback. After the page reclaim finishes an entire batch, it puts back
11 the pages it queued for writeback to the head of the original LRU list.
12
13 In the meantime, the page writeback flushes the queued pages also by
14 batches. Its batching logic is independent from that of the page reclaim.
15 For each of the pages it writes back, the page writeback calls
16 rotate_reclaimable_page() which tries to rotate a page to the tail.
17
18 rotate_reclaimable_page() only works for a page after the page reclaim
19 has put it back. If an async swap device is fast enough, the page
20 writeback can finish with that page while the page reclaim is still
21 working on the rest of the batch containing it. In this case, that page
22 will remain at the head and the page reclaim will not retry it before
23 reaching there.
24
25 This patch adds a retry to evict_pages(). After evict_pages() has
26 finished an entire batch and before it puts back pages it cannot free
27 immediately, it retries those that may have missed the rotation.
28
29 Before this patch, ~60% of pages swapped to an Intel Optane missed
30 rotate_reclaimable_page(). After this patch, ~99% of missed pages were
31 reclaimed upon retry.
32
33 This problem affects relatively slow async swap devices like Samsung 980
34 Pro much less and does not affect sync swap devices like zram or zswap at
35 all.
36
37 Link: https://lkml.kernel.org/r/20221116013808.3995280-1-yuzhao@google.com
38 Fixes: ac35a4902374 ("mm: multi-gen LRU: minimal implementation")
39 Signed-off-by: Yu Zhao <yuzhao@google.com>
40 Cc: "Yin, Fengwei" <fengwei.yin@intel.com>
41 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
42 ---
43 mm/vmscan.c | 48 +++++++++++++++++++++++++++++++++++++-----------
44 1 file changed, 37 insertions(+), 11 deletions(-)
45
46 diff --git a/mm/vmscan.c b/mm/vmscan.c
47 index 1c0875e6514a..27bc525380f9 100644
48 --- a/mm/vmscan.c
49 +++ b/mm/vmscan.c
50 @@ -4723,10 +4723,13 @@ static int evict_pages(struct lruvec *lruvec, struct scan_control *sc, int swapp
51 int scanned;
52 int reclaimed;
53 LIST_HEAD(list);
54 + LIST_HEAD(clean);
55 struct page *page;
56 + struct page *next;
57 enum vm_event_item item;
58 struct reclaim_stat stat;
59 struct lru_gen_mm_walk *walk;
60 + bool skip_retry = false;
61 struct mem_cgroup *memcg = lruvec_memcg(lruvec);
62 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
63
64 @@ -4743,20 +4746,37 @@ static int evict_pages(struct lruvec *lruvec, struct scan_control *sc, int swapp
65
66 if (list_empty(&list))
67 return scanned;
68 -
69 +retry:
70 reclaimed = shrink_page_list(&list, pgdat, sc, &stat, false);
71 + sc->nr_reclaimed += reclaimed;
72
73 - list_for_each_entry(page, &list, lru) {
74 - /* restore LRU_REFS_FLAGS cleared by isolate_page() */
75 - if (PageWorkingset(page))
76 - SetPageReferenced(page);
77 + list_for_each_entry_safe_reverse(page, next, &list, lru) {
78 + if (!page_evictable(page)) {
79 + list_del(&page->lru);
80 + putback_lru_page(page);
81 + continue;
82 + }
83
84 - /* don't add rejected pages to the oldest generation */
85 if (PageReclaim(page) &&
86 - (PageDirty(page) || PageWriteback(page)))
87 - ClearPageActive(page);
88 - else
89 - SetPageActive(page);
90 + (PageDirty(page) || PageWriteback(page))) {
91 + /* restore LRU_REFS_FLAGS cleared by isolate_page() */
92 + if (PageWorkingset(page))
93 + SetPageReferenced(page);
94 + continue;
95 + }
96 +
97 + if (skip_retry || PageActive(page) || PageReferenced(page) ||
98 + page_mapped(page) || PageLocked(page) ||
99 + PageDirty(page) || PageWriteback(page)) {
100 + /* don't add rejected pages to the oldest generation */
101 + set_mask_bits(&page->flags, LRU_REFS_MASK | LRU_REFS_FLAGS,
102 + BIT(PG_active));
103 + continue;
104 + }
105 +
106 + /* retry pages that may have missed rotate_reclaimable_page() */
107 + list_move(&page->lru, &clean);
108 + sc->nr_scanned -= thp_nr_pages(page);
109 }
110
111 spin_lock_irq(&lruvec->lru_lock);
112 @@ -4778,7 +4798,13 @@ static int evict_pages(struct lruvec *lruvec, struct scan_control *sc, int swapp
113 mem_cgroup_uncharge_list(&list);
114 free_unref_page_list(&list);
115
116 - sc->nr_reclaimed += reclaimed;
117 + INIT_LIST_HEAD(&list);
118 + list_splice_init(&clean, &list);
119 +
120 + if (!list_empty(&list)) {
121 + skip_retry = true;
122 + goto retry;
123 + }
124
125 if (need_swapping && type == LRU_GEN_ANON)
126 *need_swapping = true;
127 --
128 2.40.0
129