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
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.
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.
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
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.
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
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
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>
43 mm/vmscan.c | 48 +++++++++++++++++++++++++++++++++++++-----------
44 1 file changed, 37 insertions(+), 11 deletions(-)
46 diff --git a/mm/vmscan.c b/mm/vmscan.c
47 index 1c0875e6514a..27bc525380f9 100644
50 @@ -4723,10 +4723,13 @@ static int evict_pages(struct lruvec *lruvec, struct scan_control *sc, int swapp
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);
64 @@ -4743,20 +4746,37 @@ static int evict_pages(struct lruvec *lruvec, struct scan_control *sc, int swapp
66 if (list_empty(&list))
70 reclaimed = shrink_page_list(&list, pgdat, sc, &stat, false);
71 + sc->nr_reclaimed += reclaimed;
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);
84 - /* don't add rejected pages to the oldest generation */
85 if (PageReclaim(page) &&
86 - (PageDirty(page) || PageWriteback(page)))
87 - ClearPageActive(page);
89 - SetPageActive(page);
90 + (PageDirty(page) || PageWriteback(page))) {
91 + /* restore LRU_REFS_FLAGS cleared by isolate_page() */
92 + if (PageWorkingset(page))
93 + SetPageReferenced(page);
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,
106 + /* retry pages that may have missed rotate_reclaimable_page() */
107 + list_move(&page->lru, &clean);
108 + sc->nr_scanned -= thp_nr_pages(page);
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);
116 - sc->nr_reclaimed += reclaimed;
117 + INIT_LIST_HEAD(&list);
118 + list_splice_init(&clean, &list);
120 + if (!list_empty(&list)) {
125 if (need_swapping && type == LRU_GEN_ANON)
126 *need_swapping = true;