1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a528910eSJohannes Weiner /*
3a528910eSJohannes Weiner * Workingset detection
4a528910eSJohannes Weiner *
5a528910eSJohannes Weiner * Copyright (C) 2013 Red Hat, Inc., Johannes Weiner
6a528910eSJohannes Weiner */
7a528910eSJohannes Weiner
8a528910eSJohannes Weiner #include <linux/memcontrol.h>
9170b04b7SJoonsoo Kim #include <linux/mm_inline.h>
10a528910eSJohannes Weiner #include <linux/writeback.h>
113a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
12a528910eSJohannes Weiner #include <linux/pagemap.h>
13a528910eSJohannes Weiner #include <linux/atomic.h>
14a528910eSJohannes Weiner #include <linux/module.h>
15a528910eSJohannes Weiner #include <linux/swap.h>
1614b46879SJohannes Weiner #include <linux/dax.h>
17a528910eSJohannes Weiner #include <linux/fs.h>
18a528910eSJohannes Weiner #include <linux/mm.h>
19f3d652b0SKairui Song #include "swap_table.h"
20b64e74e9SChristoph Hellwig #include "internal.h"
21a528910eSJohannes Weiner
22a528910eSJohannes Weiner /*
23a528910eSJohannes Weiner * Double CLOCK lists
24a528910eSJohannes Weiner *
251e6b1085SMel Gorman * Per node, two clock lists are maintained for file pages: the
26a528910eSJohannes Weiner * inactive and the active list. Freshly faulted pages start out at
27a528910eSJohannes Weiner * the head of the inactive list and page reclaim scans pages from the
28a528910eSJohannes Weiner * tail. Pages that are accessed multiple times on the inactive list
29a528910eSJohannes Weiner * are promoted to the active list, to protect them from reclaim,
30a528910eSJohannes Weiner * whereas active pages are demoted to the inactive list when the
31a528910eSJohannes Weiner * active list grows too big.
32a528910eSJohannes Weiner *
33a528910eSJohannes Weiner * fault ------------------------+
34a528910eSJohannes Weiner * |
35a528910eSJohannes Weiner * +--------------+ | +-------------+
36a528910eSJohannes Weiner * reclaim <- | inactive | <-+-- demotion | active | <--+
37a528910eSJohannes Weiner * +--------------+ +-------------+ |
38a528910eSJohannes Weiner * | |
39a528910eSJohannes Weiner * +-------------- promotion ------------------+
40a528910eSJohannes Weiner *
41a528910eSJohannes Weiner *
42a528910eSJohannes Weiner * Access frequency and refault distance
43a528910eSJohannes Weiner *
44a528910eSJohannes Weiner * A workload is thrashing when its pages are frequently used but they
45a528910eSJohannes Weiner * are evicted from the inactive list every time before another access
46a528910eSJohannes Weiner * would have promoted them to the active list.
47a528910eSJohannes Weiner *
48a528910eSJohannes Weiner * In cases where the average access distance between thrashing pages
49a528910eSJohannes Weiner * is bigger than the size of memory there is nothing that can be
50a528910eSJohannes Weiner * done - the thrashing set could never fit into memory under any
51a528910eSJohannes Weiner * circumstance.
52a528910eSJohannes Weiner *
53a528910eSJohannes Weiner * However, the average access distance could be bigger than the
54a528910eSJohannes Weiner * inactive list, yet smaller than the size of memory. In this case,
55a528910eSJohannes Weiner * the set could fit into memory if it weren't for the currently
56a528910eSJohannes Weiner * active pages - which may be used more, hopefully less frequently:
57a528910eSJohannes Weiner *
58a528910eSJohannes Weiner * +-memory available to cache-+
59a528910eSJohannes Weiner * | |
60a528910eSJohannes Weiner * +-inactive------+-active----+
61a528910eSJohannes Weiner * a b | c d e f g h i | J K L M N |
62a528910eSJohannes Weiner * +---------------+-----------+
63a528910eSJohannes Weiner *
64a528910eSJohannes Weiner * It is prohibitively expensive to accurately track access frequency
65a528910eSJohannes Weiner * of pages. But a reasonable approximation can be made to measure
66a528910eSJohannes Weiner * thrashing on the inactive list, after which refaulting pages can be
67a528910eSJohannes Weiner * activated optimistically to compete with the existing active pages.
68a528910eSJohannes Weiner *
69a528910eSJohannes Weiner * Approximating inactive page access frequency - Observations:
70a528910eSJohannes Weiner *
71a528910eSJohannes Weiner * 1. When a page is accessed for the first time, it is added to the
72a528910eSJohannes Weiner * head of the inactive list, slides every existing inactive page
73a528910eSJohannes Weiner * towards the tail by one slot, and pushes the current tail page
74a528910eSJohannes Weiner * out of memory.
75a528910eSJohannes Weiner *
76a528910eSJohannes Weiner * 2. When a page is accessed for the second time, it is promoted to
77a528910eSJohannes Weiner * the active list, shrinking the inactive list by one slot. This
78a528910eSJohannes Weiner * also slides all inactive pages that were faulted into the cache
79a528910eSJohannes Weiner * more recently than the activated page towards the tail of the
80a528910eSJohannes Weiner * inactive list.
81a528910eSJohannes Weiner *
82a528910eSJohannes Weiner * Thus:
83a528910eSJohannes Weiner *
84a528910eSJohannes Weiner * 1. The sum of evictions and activations between any two points in
85a528910eSJohannes Weiner * time indicate the minimum number of inactive pages accessed in
86a528910eSJohannes Weiner * between.
87a528910eSJohannes Weiner *
88a528910eSJohannes Weiner * 2. Moving one inactive page N page slots towards the tail of the
89a528910eSJohannes Weiner * list requires at least N inactive page accesses.
90a528910eSJohannes Weiner *
91a528910eSJohannes Weiner * Combining these:
92a528910eSJohannes Weiner *
93a528910eSJohannes Weiner * 1. When a page is finally evicted from memory, the number of
94a528910eSJohannes Weiner * inactive pages accessed while the page was in cache is at least
95a528910eSJohannes Weiner * the number of page slots on the inactive list.
96a528910eSJohannes Weiner *
97a528910eSJohannes Weiner * 2. In addition, measuring the sum of evictions and activations (E)
98a528910eSJohannes Weiner * at the time of a page's eviction, and comparing it to another
99a528910eSJohannes Weiner * reading (R) at the time the page faults back into memory tells
100a528910eSJohannes Weiner * the minimum number of accesses while the page was not cached.
101a528910eSJohannes Weiner * This is called the refault distance.
102a528910eSJohannes Weiner *
103a528910eSJohannes Weiner * Because the first access of the page was the fault and the second
104a528910eSJohannes Weiner * access the refault, we combine the in-cache distance with the
105a528910eSJohannes Weiner * out-of-cache distance to get the complete minimum access distance
106a528910eSJohannes Weiner * of this page:
107a528910eSJohannes Weiner *
108a528910eSJohannes Weiner * NR_inactive + (R - E)
109a528910eSJohannes Weiner *
110a528910eSJohannes Weiner * And knowing the minimum access distance of a page, we can easily
111a528910eSJohannes Weiner * tell if the page would be able to stay in cache assuming all page
112a528910eSJohannes Weiner * slots in the cache were available:
113a528910eSJohannes Weiner *
114a528910eSJohannes Weiner * NR_inactive + (R - E) <= NR_inactive + NR_active
115a528910eSJohannes Weiner *
116ed8f3f99SYang Yang * If we have swap we should consider about NR_inactive_anon and
117ed8f3f99SYang Yang * NR_active_anon, so for page cache and anonymous respectively:
118a528910eSJohannes Weiner *
119ed8f3f99SYang Yang * NR_inactive_file + (R - E) <= NR_inactive_file + NR_active_file
120ed8f3f99SYang Yang * + NR_inactive_anon + NR_active_anon
121ed8f3f99SYang Yang *
122ed8f3f99SYang Yang * NR_inactive_anon + (R - E) <= NR_inactive_anon + NR_active_anon
123ed8f3f99SYang Yang * + NR_inactive_file + NR_active_file
124ed8f3f99SYang Yang *
125ed8f3f99SYang Yang * Which can be further simplified to:
126ed8f3f99SYang Yang *
127ed8f3f99SYang Yang * (R - E) <= NR_active_file + NR_inactive_anon + NR_active_anon
128ed8f3f99SYang Yang *
129ed8f3f99SYang Yang * (R - E) <= NR_active_anon + NR_inactive_file + NR_active_file
130a528910eSJohannes Weiner *
131a528910eSJohannes Weiner * Put into words, the refault distance (out-of-cache) can be seen as
132a528910eSJohannes Weiner * a deficit in inactive list space (in-cache). If the inactive list
133a528910eSJohannes Weiner * had (R - E) more page slots, the page would not have been evicted
134a528910eSJohannes Weiner * in between accesses, but activated instead. And on a full system,
135a528910eSJohannes Weiner * the only thing eating into inactive list space is active pages.
136a528910eSJohannes Weiner *
137a528910eSJohannes Weiner *
1381899ad18SJohannes Weiner * Refaulting inactive pages
139a528910eSJohannes Weiner *
140a528910eSJohannes Weiner * All that is known about the active list is that the pages have been
141a528910eSJohannes Weiner * accessed more than once in the past. This means that at any given
142a528910eSJohannes Weiner * time there is actually a good chance that pages on the active list
143a528910eSJohannes Weiner * are no longer in active use.
144a528910eSJohannes Weiner *
145a528910eSJohannes Weiner * So when a refault distance of (R - E) is observed and there are at
146ed8f3f99SYang Yang * least (R - E) pages in the userspace workingset, the refaulting page
147ed8f3f99SYang Yang * is activated optimistically in the hope that (R - E) pages are actually
148a528910eSJohannes Weiner * used less frequently than the refaulting page - or even not used at
149a528910eSJohannes Weiner * all anymore.
150a528910eSJohannes Weiner *
1511899ad18SJohannes Weiner * That means if inactive cache is refaulting with a suitable refault
1521899ad18SJohannes Weiner * distance, we assume the cache workingset is transitioning and put
153ed8f3f99SYang Yang * pressure on the current workingset.
1541899ad18SJohannes Weiner *
155a528910eSJohannes Weiner * If this is wrong and demotion kicks in, the pages which are truly
156a528910eSJohannes Weiner * used more frequently will be reactivated while the less frequently
157a528910eSJohannes Weiner * used once will be evicted from memory.
158a528910eSJohannes Weiner *
159a528910eSJohannes Weiner * But if this is right, the stale pages will be pushed out of memory
160a528910eSJohannes Weiner * and the used pages get to stay in cache.
161a528910eSJohannes Weiner *
1621899ad18SJohannes Weiner * Refaulting active pages
1631899ad18SJohannes Weiner *
1641899ad18SJohannes Weiner * If on the other hand the refaulting pages have recently been
1651899ad18SJohannes Weiner * deactivated, it means that the active list is no longer protecting
1661899ad18SJohannes Weiner * actively used cache from reclaim. The cache is NOT transitioning to
1671899ad18SJohannes Weiner * a different workingset; the existing workingset is thrashing in the
1681899ad18SJohannes Weiner * space allocated to the page cache.
1691899ad18SJohannes Weiner *
170a528910eSJohannes Weiner *
171a528910eSJohannes Weiner * Implementation
172a528910eSJohannes Weiner *
17331d8fcacSJohannes Weiner * For each node's LRU lists, a counter for inactive evictions and
17431d8fcacSJohannes Weiner * activations is maintained (node->nonresident_age).
175a528910eSJohannes Weiner *
176a528910eSJohannes Weiner * On eviction, a snapshot of this counter (along with some bits to
177a97e7904SMatthew Wilcox * identify the node) is stored in the now empty page cache
178a528910eSJohannes Weiner * slot of the evicted page. This is called a shadow entry.
179a528910eSJohannes Weiner *
180a528910eSJohannes Weiner * On cache misses for which there are shadow entries, an eligible
181a528910eSJohannes Weiner * refault distance will immediately activate the refaulting page.
182a528910eSJohannes Weiner */
183a528910eSJohannes Weiner
1843ebc57f4SMiaohe Lin #define WORKINGSET_SHIFT 1
1853159f943SMatthew Wilcox #define EVICTION_SHIFT ((BITS_PER_LONG - BITS_PER_XA_VALUE) + \
1863ebc57f4SMiaohe Lin WORKINGSET_SHIFT + NODES_SHIFT + \
1873ebc57f4SMiaohe Lin MEM_CGROUP_ID_SHIFT)
188f3d652b0SKairui Song #define EVICTION_SHIFT_ANON (EVICTION_SHIFT + SWAP_COUNT_SHIFT)
189689c94f0SJohannes Weiner #define EVICTION_MASK (~0UL >> EVICTION_SHIFT)
190f3d652b0SKairui Song #define EVICTION_MASK_ANON (~0UL >> EVICTION_SHIFT_ANON)
191689c94f0SJohannes Weiner
192612e4493SJohannes Weiner /*
193612e4493SJohannes Weiner * Eviction timestamps need to be able to cover the full range of
194a97e7904SMatthew Wilcox * actionable refaults. However, bits are tight in the xarray
195612e4493SJohannes Weiner * entry, and after storing the identifier for the lruvec there might
196612e4493SJohannes Weiner * not be enough left to represent every single actionable refault. In
197612e4493SJohannes Weiner * that case, we have to sacrifice granularity for distance, and group
198612e4493SJohannes Weiner * evictions into coarser buckets by shaving off lower timestamp bits.
199612e4493SJohannes Weiner */
200f3d652b0SKairui Song static unsigned int bucket_order[ANON_AND_FILE] __read_mostly;
201612e4493SJohannes Weiner
pack_shadow(int memcgid,pg_data_t * pgdat,unsigned long eviction,bool workingset,bool file)2021899ad18SJohannes Weiner static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction,
203f3d652b0SKairui Song bool workingset, bool file)
204a528910eSJohannes Weiner {
205f3d652b0SKairui Song eviction &= file ? EVICTION_MASK : EVICTION_MASK_ANON;
20623047a96SJohannes Weiner eviction = (eviction << MEM_CGROUP_ID_SHIFT) | memcgid;
2071e6b1085SMel Gorman eviction = (eviction << NODES_SHIFT) | pgdat->node_id;
2083ebc57f4SMiaohe Lin eviction = (eviction << WORKINGSET_SHIFT) | workingset;
209a528910eSJohannes Weiner
2103159f943SMatthew Wilcox return xa_mk_value(eviction);
211a528910eSJohannes Weiner }
212a528910eSJohannes Weiner
unpack_shadow(void * shadow,int * memcgidp,pg_data_t ** pgdat,unsigned long * evictionp,bool * workingsetp)2131e6b1085SMel Gorman static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
2141899ad18SJohannes Weiner unsigned long *evictionp, bool *workingsetp)
215a528910eSJohannes Weiner {
2163159f943SMatthew Wilcox unsigned long entry = xa_to_value(shadow);
2171e6b1085SMel Gorman int memcgid, nid;
2181899ad18SJohannes Weiner bool workingset;
219a528910eSJohannes Weiner
2203ebc57f4SMiaohe Lin workingset = entry & ((1UL << WORKINGSET_SHIFT) - 1);
2213ebc57f4SMiaohe Lin entry >>= WORKINGSET_SHIFT;
222a528910eSJohannes Weiner nid = entry & ((1UL << NODES_SHIFT) - 1);
223a528910eSJohannes Weiner entry >>= NODES_SHIFT;
22423047a96SJohannes Weiner memcgid = entry & ((1UL << MEM_CGROUP_ID_SHIFT) - 1);
22523047a96SJohannes Weiner entry >>= MEM_CGROUP_ID_SHIFT;
226a528910eSJohannes Weiner
22723047a96SJohannes Weiner *memcgidp = memcgid;
2281e6b1085SMel Gorman *pgdat = NODE_DATA(nid);
229ac35a490SYu Zhao *evictionp = entry;
2301899ad18SJohannes Weiner *workingsetp = workingset;
231a528910eSJohannes Weiner }
232a528910eSJohannes Weiner
233ac35a490SYu Zhao #ifdef CONFIG_LRU_GEN
234ac35a490SYu Zhao
lru_gen_eviction(struct folio * folio)235ac35a490SYu Zhao static void *lru_gen_eviction(struct folio *folio)
236ac35a490SYu Zhao {
237ac35a490SYu Zhao int hist;
238ac35a490SYu Zhao unsigned long token;
239ac35a490SYu Zhao unsigned long min_seq;
240ac35a490SYu Zhao struct lruvec *lruvec;
241391655feSYu Zhao struct lru_gen_folio *lrugen;
242ac35a490SYu Zhao int type = folio_is_file_lru(folio);
243ac35a490SYu Zhao int delta = folio_nr_pages(folio);
244ac35a490SYu Zhao int refs = folio_lru_refs(folio);
2454d5d14a0SYu Zhao bool workingset = folio_test_workingset(folio);
2464d5d14a0SYu Zhao int tier = lru_tier_from_refs(refs, workingset);
247b3ca9829SMuchun Song struct mem_cgroup *memcg;
248ac35a490SYu Zhao struct pglist_data *pgdat = folio_pgdat(folio);
249b3ca9829SMuchun Song unsigned short memcg_id;
250ac35a490SYu Zhao
251f3d652b0SKairui Song BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_WIDTH >
252f3d652b0SKairui Song BITS_PER_LONG - max(EVICTION_SHIFT, EVICTION_SHIFT_ANON));
253ac35a490SYu Zhao
254b3ca9829SMuchun Song rcu_read_lock();
255b3ca9829SMuchun Song memcg = folio_memcg(folio);
256ac35a490SYu Zhao lruvec = mem_cgroup_lruvec(memcg, pgdat);
257ac35a490SYu Zhao lrugen = &lruvec->lrugen;
258ac35a490SYu Zhao min_seq = READ_ONCE(lrugen->min_seq[type]);
259ac35a490SYu Zhao token = (min_seq << LRU_REFS_WIDTH) | max(refs - 1, 0);
260ac35a490SYu Zhao
261ac35a490SYu Zhao hist = lru_hist_from_seq(min_seq);
262ac35a490SYu Zhao atomic_long_add(delta, &lrugen->evicted[hist][type][tier]);
263b3ca9829SMuchun Song memcg_id = mem_cgroup_private_id(memcg);
264b3ca9829SMuchun Song rcu_read_unlock();
265ac35a490SYu Zhao
266b3ca9829SMuchun Song return pack_shadow(memcg_id, pgdat, token, workingset, type);
267ac35a490SYu Zhao }
268ac35a490SYu Zhao
269ffcb5f52SNhat Pham /*
270ffcb5f52SNhat Pham * Tests if the shadow entry is for a folio that was recently evicted.
271d7f1afd0ST.J. Alumbaugh * Fills in @lruvec, @token, @workingset with the values unpacked from shadow.
272ffcb5f52SNhat Pham */
lru_gen_test_recent(void * shadow,struct lruvec ** lruvec,unsigned long * token,bool * workingset,bool file)273b1a71694SYu Zhao static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,
274f3d652b0SKairui Song unsigned long *token, bool *workingset, bool file)
275ffcb5f52SNhat Pham {
276d7f1afd0ST.J. Alumbaugh int memcg_id;
277b1a71694SYu Zhao unsigned long max_seq;
278d7f1afd0ST.J. Alumbaugh struct mem_cgroup *memcg;
279d7f1afd0ST.J. Alumbaugh struct pglist_data *pgdat;
280ffcb5f52SNhat Pham
281d7f1afd0ST.J. Alumbaugh unpack_shadow(shadow, &memcg_id, &pgdat, token, workingset);
282ffcb5f52SNhat Pham
283e77786b4SShakeel Butt memcg = mem_cgroup_from_private_id(memcg_id);
284d7f1afd0ST.J. Alumbaugh *lruvec = mem_cgroup_lruvec(memcg, pgdat);
285ffcb5f52SNhat Pham
286b1a71694SYu Zhao max_seq = READ_ONCE((*lruvec)->lrugen.max_seq);
287f3d652b0SKairui Song max_seq &= (file ? EVICTION_MASK : EVICTION_MASK_ANON) >> LRU_REFS_WIDTH;
288b1a71694SYu Zhao
289b1a71694SYu Zhao return abs_diff(max_seq, *token >> LRU_REFS_WIDTH) < MAX_NR_GENS;
290ffcb5f52SNhat Pham }
291ffcb5f52SNhat Pham
lru_gen_refault(struct folio * folio,void * shadow)292ac35a490SYu Zhao static void lru_gen_refault(struct folio *folio, void *shadow)
293ac35a490SYu Zhao {
2943af0191aSKalesh Singh bool recent;
295ac35a490SYu Zhao int hist, tier, refs;
296ac35a490SYu Zhao bool workingset;
297ac35a490SYu Zhao unsigned long token;
298ac35a490SYu Zhao struct lruvec *lruvec;
299391655feSYu Zhao struct lru_gen_folio *lrugen;
300ac35a490SYu Zhao int type = folio_is_file_lru(folio);
301ac35a490SYu Zhao int delta = folio_nr_pages(folio);
302ac35a490SYu Zhao
303ac35a490SYu Zhao rcu_read_lock();
304ac35a490SYu Zhao
305f3d652b0SKairui Song recent = lru_gen_test_recent(shadow, &lruvec, &token, &workingset, type);
3063af0191aSKalesh Singh if (lruvec != folio_lruvec(folio))
307ffcb5f52SNhat Pham goto unlock;
308ffcb5f52SNhat Pham
3093af0191aSKalesh Singh mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + type, delta);
3103af0191aSKalesh Singh
3113af0191aSKalesh Singh if (!recent)
312ac35a490SYu Zhao goto unlock;
313ac35a490SYu Zhao
314ac35a490SYu Zhao lrugen = &lruvec->lrugen;
315ac35a490SYu Zhao
316d7f1afd0ST.J. Alumbaugh hist = lru_hist_from_seq(READ_ONCE(lrugen->min_seq[type]));
3174d5d14a0SYu Zhao refs = (token & (BIT(LRU_REFS_WIDTH) - 1)) + 1;
3184d5d14a0SYu Zhao tier = lru_tier_from_refs(refs, workingset);
319ac35a490SYu Zhao
320ac35a490SYu Zhao atomic_long_add(delta, &lrugen->refaulted[hist][type][tier]);
3214d5d14a0SYu Zhao
3224d5d14a0SYu Zhao /* see folio_add_lru() where folio_set_active() will be called */
3234d5d14a0SYu Zhao if (lru_gen_in_fault())
3243af0191aSKalesh Singh mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);
325ac35a490SYu Zhao
3264d5d14a0SYu Zhao if (workingset) {
3274d5d14a0SYu Zhao folio_set_workingset(folio);
328ac35a490SYu Zhao mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta);
3294d5d14a0SYu Zhao } else
33053fbef56SMatthew Wilcox (Oracle) set_mask_bits(&folio->flags.f, LRU_REFS_MASK, (refs - 1UL) << LRU_REFS_PGOFF);
331ac35a490SYu Zhao unlock:
332ac35a490SYu Zhao rcu_read_unlock();
333ac35a490SYu Zhao }
334ac35a490SYu Zhao
335ac35a490SYu Zhao #else /* !CONFIG_LRU_GEN */
336ac35a490SYu Zhao
lru_gen_eviction(struct folio * folio)337ac35a490SYu Zhao static void *lru_gen_eviction(struct folio *folio)
338ac35a490SYu Zhao {
339ac35a490SYu Zhao return NULL;
340ac35a490SYu Zhao }
341ac35a490SYu Zhao
lru_gen_test_recent(void * shadow,struct lruvec ** lruvec,unsigned long * token,bool * workingset,bool file)342b1a71694SYu Zhao static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec,
343f3d652b0SKairui Song unsigned long *token, bool *workingset, bool file)
344ffcb5f52SNhat Pham {
345ffcb5f52SNhat Pham return false;
346ffcb5f52SNhat Pham }
347ffcb5f52SNhat Pham
lru_gen_refault(struct folio * folio,void * shadow)348ac35a490SYu Zhao static void lru_gen_refault(struct folio *folio, void *shadow)
349ac35a490SYu Zhao {
350ac35a490SYu Zhao }
351ac35a490SYu Zhao
352ac35a490SYu Zhao #endif /* CONFIG_LRU_GEN */
353ac35a490SYu Zhao
35431d8fcacSJohannes Weiner /**
35531d8fcacSJohannes Weiner * workingset_age_nonresident - age non-resident entries as LRU ages
356e755f4afSXiaofei Tan * @lruvec: the lruvec that was aged
35731d8fcacSJohannes Weiner * @nr_pages: the number of pages to count
35831d8fcacSJohannes Weiner *
35931d8fcacSJohannes Weiner * As in-memory pages are aged, non-resident pages need to be aged as
36031d8fcacSJohannes Weiner * well, in order for the refault distances later on to be comparable
36131d8fcacSJohannes Weiner * to the in-memory dimensions. This function allows reclaim and LRU
36231d8fcacSJohannes Weiner * operations to drive the non-resident aging along in parallel.
36331d8fcacSJohannes Weiner */
workingset_age_nonresident(struct lruvec * lruvec,unsigned long nr_pages)36431d8fcacSJohannes Weiner void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages)
365b910718aSJohannes Weiner {
366b910718aSJohannes Weiner /*
367b910718aSJohannes Weiner * Reclaiming a cgroup means reclaiming all its children in a
368b910718aSJohannes Weiner * round-robin fashion. That means that each cgroup has an LRU
369b910718aSJohannes Weiner * order that is composed of the LRU orders of its child
370b910718aSJohannes Weiner * cgroups; and every page has an LRU position not just in the
371b910718aSJohannes Weiner * cgroup that owns it, but in all of that group's ancestors.
372b910718aSJohannes Weiner *
373b910718aSJohannes Weiner * So when the physical inactive list of a leaf cgroup ages,
374b910718aSJohannes Weiner * the virtual inactive lists of all its parents, including
375b910718aSJohannes Weiner * the root cgroup's, age as well.
376b910718aSJohannes Weiner */
377b910718aSJohannes Weiner do {
37831d8fcacSJohannes Weiner atomic_long_add(nr_pages, &lruvec->nonresident_age);
37931d8fcacSJohannes Weiner } while ((lruvec = parent_lruvec(lruvec)));
380b910718aSJohannes Weiner }
381b910718aSJohannes Weiner
382a528910eSJohannes Weiner /**
3838927f647SMatthew Wilcox (Oracle) * workingset_eviction - note the eviction of a folio from memory
384b910718aSJohannes Weiner * @target_memcg: the cgroup that is causing the reclaim
3858927f647SMatthew Wilcox (Oracle) * @folio: the folio being evicted
386a528910eSJohannes Weiner *
3878927f647SMatthew Wilcox (Oracle) * Return: a shadow entry to be stored in @folio->mapping->i_pages in place
3888927f647SMatthew Wilcox (Oracle) * of the evicted @folio so that a later refault can be detected.
389a528910eSJohannes Weiner */
workingset_eviction(struct folio * folio,struct mem_cgroup * target_memcg)3908927f647SMatthew Wilcox (Oracle) void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg)
391a528910eSJohannes Weiner {
3928927f647SMatthew Wilcox (Oracle) struct pglist_data *pgdat = folio_pgdat(folio);
393f3d652b0SKairui Song int file = folio_is_file_lru(folio);
394a528910eSJohannes Weiner unsigned long eviction;
39523047a96SJohannes Weiner struct lruvec *lruvec;
396b910718aSJohannes Weiner int memcgid;
397a528910eSJohannes Weiner
3988927f647SMatthew Wilcox (Oracle) /* Folio is fully exclusive and pins folio's memory cgroup pointer */
3998927f647SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
4008927f647SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
4018927f647SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
40223047a96SJohannes Weiner
403ac35a490SYu Zhao if (lru_gen_enabled())
404ac35a490SYu Zhao return lru_gen_eviction(folio);
405ac35a490SYu Zhao
406b910718aSJohannes Weiner lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
407b910718aSJohannes Weiner /* XXX: target_memcg can be NULL, go through lruvec */
408e77786b4SShakeel Butt memcgid = mem_cgroup_private_id(lruvec_memcg(lruvec));
40931d8fcacSJohannes Weiner eviction = atomic_long_read(&lruvec->nonresident_age);
410f3d652b0SKairui Song eviction >>= bucket_order[file];
4118927f647SMatthew Wilcox (Oracle) workingset_age_nonresident(lruvec, folio_nr_pages(folio));
4128927f647SMatthew Wilcox (Oracle) return pack_shadow(memcgid, pgdat, eviction,
413f3d652b0SKairui Song folio_test_workingset(folio), file);
414a528910eSJohannes Weiner }
415a528910eSJohannes Weiner
416a528910eSJohannes Weiner /**
417ffcb5f52SNhat Pham * workingset_test_recent - tests if the shadow entry is for a folio that was
418ffcb5f52SNhat Pham * recently evicted. Also fills in @workingset with the value unpacked from
419ffcb5f52SNhat Pham * shadow.
420ffcb5f52SNhat Pham * @shadow: the shadow entry to be tested.
421ffcb5f52SNhat Pham * @file: whether the corresponding folio is from the file lru.
422ffcb5f52SNhat Pham * @workingset: where the workingset value unpacked from shadow should
423ffcb5f52SNhat Pham * be stored.
4245a4d8944SNhat Pham * @flush: whether to flush cgroup rstat.
425a528910eSJohannes Weiner *
426ffcb5f52SNhat Pham * Return: true if the shadow is for a recently evicted folio; false otherwise.
427a528910eSJohannes Weiner */
workingset_test_recent(void * shadow,bool file,bool * workingset,bool flush)4285a4d8944SNhat Pham bool workingset_test_recent(void *shadow, bool file, bool *workingset,
4295a4d8944SNhat Pham bool flush)
430a528910eSJohannes Weiner {
431b910718aSJohannes Weiner struct mem_cgroup *eviction_memcg;
432b910718aSJohannes Weiner struct lruvec *eviction_lruvec;
433a528910eSJohannes Weiner unsigned long refault_distance;
43434e58cacSJohannes Weiner unsigned long workingset_size;
435162453bfSJohannes Weiner unsigned long refault;
43623047a96SJohannes Weiner int memcgid;
437ffcb5f52SNhat Pham struct pglist_data *pgdat;
438ffcb5f52SNhat Pham unsigned long eviction;
439a528910eSJohannes Weiner
440b0068472SYosry Ahmed if (lru_gen_enabled()) {
4419cbfd1c3SYu Zhao bool recent;
442b0068472SYosry Ahmed
4439cbfd1c3SYu Zhao rcu_read_lock();
444f3d652b0SKairui Song recent = lru_gen_test_recent(shadow, &eviction_lruvec, &eviction,
445f3d652b0SKairui Song workingset, file);
446b0068472SYosry Ahmed rcu_read_unlock();
447b0068472SYosry Ahmed return recent;
448b0068472SYosry Ahmed }
449b0068472SYosry Ahmed
4509cbfd1c3SYu Zhao rcu_read_lock();
451ffcb5f52SNhat Pham unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset);
452f3d652b0SKairui Song eviction <<= bucket_order[file];
453162453bfSJohannes Weiner
45423047a96SJohannes Weiner /*
45523047a96SJohannes Weiner * Look up the memcg associated with the stored ID. It might
4560995d7e5SMatthew Wilcox (Oracle) * have been deleted since the folio's eviction.
45723047a96SJohannes Weiner *
45823047a96SJohannes Weiner * Note that in rare events the ID could have been recycled
4590995d7e5SMatthew Wilcox (Oracle) * for a new cgroup that refaults a shared folio. This is
46023047a96SJohannes Weiner * impossible to tell from the available data. However, this
46123047a96SJohannes Weiner * should be a rare and limited disturbance, and activations
46223047a96SJohannes Weiner * are always speculative anyway. Ultimately, it's the aging
46323047a96SJohannes Weiner * algorithm's job to shake out the minimum access frequency
46423047a96SJohannes Weiner * for the active cache.
46523047a96SJohannes Weiner *
46623047a96SJohannes Weiner * XXX: On !CONFIG_MEMCG, this will always return NULL; it
46723047a96SJohannes Weiner * would be better if the root_mem_cgroup existed in all
46823047a96SJohannes Weiner * configurations instead.
46923047a96SJohannes Weiner */
470e77786b4SShakeel Butt eviction_memcg = mem_cgroup_from_private_id(memcgid);
4719cbfd1c3SYu Zhao if (!mem_cgroup_tryget(eviction_memcg))
4729cbfd1c3SYu Zhao eviction_memcg = NULL;
473b0068472SYosry Ahmed rcu_read_unlock();
4749cbfd1c3SYu Zhao
4759cbfd1c3SYu Zhao if (!mem_cgroup_disabled() && !eviction_memcg)
476ffcb5f52SNhat Pham return false;
4777d7ef0a4SYosry Ahmed /*
4787d7ef0a4SYosry Ahmed * Flush stats (and potentially sleep) outside the RCU read section.
4795a4d8944SNhat Pham *
4805a4d8944SNhat Pham * Note that workingset_test_recent() itself might be called in RCU read
4815a4d8944SNhat Pham * section (for e.g, in cachestat) - these callers need to skip flushing
4825a4d8944SNhat Pham * stats (via the flush argument).
4835a4d8944SNhat Pham *
4847d7ef0a4SYosry Ahmed * XXX: With per-memcg flushing and thresholding, is ratelimiting
4857d7ef0a4SYosry Ahmed * still needed here?
4867d7ef0a4SYosry Ahmed */
4875a4d8944SNhat Pham if (flush)
4887d7ef0a4SYosry Ahmed mem_cgroup_flush_stats_ratelimited(eviction_memcg);
489ffcb5f52SNhat Pham
490b910718aSJohannes Weiner eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat);
49131d8fcacSJohannes Weiner refault = atomic_long_read(&eviction_lruvec->nonresident_age);
492162453bfSJohannes Weiner
493162453bfSJohannes Weiner /*
4941899ad18SJohannes Weiner * Calculate the refault distance
495162453bfSJohannes Weiner *
4961899ad18SJohannes Weiner * The unsigned subtraction here gives an accurate distance
49731d8fcacSJohannes Weiner * across nonresident_age overflows in most cases. There is a
4981899ad18SJohannes Weiner * special case: usually, shadow entries have a short lifetime
4991899ad18SJohannes Weiner * and are either refaulted or reclaimed along with the inode
5001899ad18SJohannes Weiner * before they get too old. But it is not impossible for the
50131d8fcacSJohannes Weiner * nonresident_age to lap a shadow entry in the field, which
50231d8fcacSJohannes Weiner * can then result in a false small refault distance, leading
50331d8fcacSJohannes Weiner * to a false activation should this old entry actually
50431d8fcacSJohannes Weiner * refault again. However, earlier kernels used to deactivate
5051899ad18SJohannes Weiner * unconditionally with *every* reclaim invocation for the
5061899ad18SJohannes Weiner * longest time, so the occasional inappropriate activation
5071899ad18SJohannes Weiner * leading to pressure on the active list is not a problem.
508162453bfSJohannes Weiner */
509f3d652b0SKairui Song refault_distance = ((refault - eviction) &
510f3d652b0SKairui Song (file ? EVICTION_MASK : EVICTION_MASK_ANON));
511162453bfSJohannes Weiner
512b910718aSJohannes Weiner /*
5131899ad18SJohannes Weiner * Compare the distance to the existing workingset size. We
51434e58cacSJohannes Weiner * don't activate pages that couldn't stay resident even if
515aae466b0SJoonsoo Kim * all the memory was available to the workingset. Whether
516aae466b0SJoonsoo Kim * workingset competition needs to consider anon or not depends
517ed8f3f99SYang Yang * on having free swap space.
5181899ad18SJohannes Weiner */
51934e58cacSJohannes Weiner workingset_size = lruvec_page_state(eviction_lruvec, NR_ACTIVE_FILE);
520aae466b0SJoonsoo Kim if (!file) {
521aae466b0SJoonsoo Kim workingset_size += lruvec_page_state(eviction_lruvec,
522aae466b0SJoonsoo Kim NR_INACTIVE_FILE);
523aae466b0SJoonsoo Kim }
524f78dfc7bSJohannes Weiner if (mem_cgroup_get_nr_swap_pages(eviction_memcg) > 0) {
52534e58cacSJohannes Weiner workingset_size += lruvec_page_state(eviction_lruvec,
52634e58cacSJohannes Weiner NR_ACTIVE_ANON);
527aae466b0SJoonsoo Kim if (file) {
528aae466b0SJoonsoo Kim workingset_size += lruvec_page_state(eviction_lruvec,
529aae466b0SJoonsoo Kim NR_INACTIVE_ANON);
530aae466b0SJoonsoo Kim }
53134e58cacSJohannes Weiner }
532ffcb5f52SNhat Pham
533b0068472SYosry Ahmed mem_cgroup_put(eviction_memcg);
534ffcb5f52SNhat Pham return refault_distance <= workingset_size;
535ffcb5f52SNhat Pham }
536ffcb5f52SNhat Pham
537ffcb5f52SNhat Pham /**
538ffcb5f52SNhat Pham * workingset_refault - Evaluate the refault of a previously evicted folio.
539ffcb5f52SNhat Pham * @folio: The freshly allocated replacement folio.
540ffcb5f52SNhat Pham * @shadow: Shadow entry of the evicted folio.
541ffcb5f52SNhat Pham *
542ffcb5f52SNhat Pham * Calculates and evaluates the refault distance of the previously
543ffcb5f52SNhat Pham * evicted folio in the context of the node and the memcg whose memory
544ffcb5f52SNhat Pham * pressure caused the eviction.
545ffcb5f52SNhat Pham */
workingset_refault(struct folio * folio,void * shadow)546ffcb5f52SNhat Pham void workingset_refault(struct folio *folio, void *shadow)
547ffcb5f52SNhat Pham {
548ffcb5f52SNhat Pham bool file = folio_is_file_lru(folio);
549fe132152SMuchun Song struct mem_cgroup *memcg;
550ffcb5f52SNhat Pham struct lruvec *lruvec;
551ffcb5f52SNhat Pham bool workingset;
552ffcb5f52SNhat Pham long nr;
553ffcb5f52SNhat Pham
5549cbfd1c3SYu Zhao VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
5559cbfd1c3SYu Zhao
556ffcb5f52SNhat Pham if (lru_gen_enabled()) {
557ffcb5f52SNhat Pham lru_gen_refault(folio, shadow);
558ffcb5f52SNhat Pham return;
559ffcb5f52SNhat Pham }
560ffcb5f52SNhat Pham
561ffcb5f52SNhat Pham /*
562ffcb5f52SNhat Pham * The activation decision for this folio is made at the level
563ffcb5f52SNhat Pham * where the eviction occurred, as that is where the LRU order
564ffcb5f52SNhat Pham * during folio reclaim is being determined.
565ffcb5f52SNhat Pham *
566ffcb5f52SNhat Pham * However, the cgroup that will own the folio is the one that
567b0068472SYosry Ahmed * is actually experiencing the refault event. Make sure the folio is
568b0068472SYosry Ahmed * locked to guarantee folio_memcg() stability throughout.
569ffcb5f52SNhat Pham */
570ffcb5f52SNhat Pham nr = folio_nr_pages(folio);
571fe132152SMuchun Song memcg = get_mem_cgroup_from_folio(folio);
572fe132152SMuchun Song lruvec = mem_cgroup_lruvec(memcg, folio_pgdat(folio));
573ffcb5f52SNhat Pham mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr);
574ffcb5f52SNhat Pham
5755a4d8944SNhat Pham if (!workingset_test_recent(shadow, file, &workingset, true))
576fe132152SMuchun Song goto out;
5771899ad18SJohannes Weiner
5780995d7e5SMatthew Wilcox (Oracle) folio_set_active(folio);
5790995d7e5SMatthew Wilcox (Oracle) workingset_age_nonresident(lruvec, nr);
5800995d7e5SMatthew Wilcox (Oracle) mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + file, nr);
5811899ad18SJohannes Weiner
5820995d7e5SMatthew Wilcox (Oracle) /* Folio was active prior to eviction */
5831899ad18SJohannes Weiner if (workingset) {
5840995d7e5SMatthew Wilcox (Oracle) folio_set_workingset(folio);
5856e1ca48dSVishal Moola (Oracle) /*
5866e1ca48dSVishal Moola (Oracle) * XXX: Move to folio_add_lru() when it supports new vs
5876e1ca48dSVishal Moola (Oracle) * putback
5886e1ca48dSVishal Moola (Oracle) */
5890538a82cSJohannes Weiner lru_note_cost_refault(folio);
5900995d7e5SMatthew Wilcox (Oracle) mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file, nr);
591a528910eSJohannes Weiner }
592fe132152SMuchun Song out:
593fe132152SMuchun Song mem_cgroup_put(memcg);
594a528910eSJohannes Weiner }
595a528910eSJohannes Weiner
596a528910eSJohannes Weiner /**
597a528910eSJohannes Weiner * workingset_activation - note a page activation
598c5ce619aSMatthew Wilcox (Oracle) * @folio: Folio that is being activated.
599a528910eSJohannes Weiner */
workingset_activation(struct folio * folio)600c5ce619aSMatthew Wilcox (Oracle) void workingset_activation(struct folio *folio)
601a528910eSJohannes Weiner {
60223047a96SJohannes Weiner /*
60323047a96SJohannes Weiner * Filter non-memcg pages here, e.g. unmap can call
60423047a96SJohannes Weiner * mark_page_accessed() on VDSO pages.
60523047a96SJohannes Weiner */
60650738297SMuchun Song if (mem_cgroup_disabled() || folio_memcg_charged(folio)) {
60750738297SMuchun Song rcu_read_lock();
608c5ce619aSMatthew Wilcox (Oracle) workingset_age_nonresident(folio_lruvec(folio), folio_nr_pages(folio));
60950738297SMuchun Song rcu_read_unlock();
61050738297SMuchun Song }
611a528910eSJohannes Weiner }
612449dd698SJohannes Weiner
613449dd698SJohannes Weiner /*
614449dd698SJohannes Weiner * Shadow entries reflect the share of the working set that does not
615449dd698SJohannes Weiner * fit into memory, so their number depends on the access pattern of
616449dd698SJohannes Weiner * the workload. In most cases, they will refault or get reclaimed
617449dd698SJohannes Weiner * along with the inode, but a (malicious) workload that streams
618449dd698SJohannes Weiner * through files with a total size several times that of available
619449dd698SJohannes Weiner * memory, while preventing the inodes from being reclaimed, can
620449dd698SJohannes Weiner * create excessive amounts of shadow nodes. To keep a lid on this,
621449dd698SJohannes Weiner * track shadow nodes and reclaim them when they grow way past the
622449dd698SJohannes Weiner * point where they would still be useful.
623449dd698SJohannes Weiner */
624449dd698SJohannes Weiner
6259bbdc0f3SMuchun Song struct list_lru shadow_nodes;
62614b46879SJohannes Weiner
workingset_update_node(struct xa_node * node)627a97e7904SMatthew Wilcox void workingset_update_node(struct xa_node *node)
62814b46879SJohannes Weiner {
6294715c6a7SShakeel Butt struct page *page = virt_to_page(node);
6302386eef2SSebastian Andrzej Siewior
63114b46879SJohannes Weiner /*
63214b46879SJohannes Weiner * Track non-empty nodes that contain only shadow entries;
63314b46879SJohannes Weiner * unlink those that contain pages or are being freed.
63414b46879SJohannes Weiner *
63514b46879SJohannes Weiner * Avoid acquiring the list_lru lock when the nodes are
63614b46879SJohannes Weiner * already where they should be. The list_empty() test is safe
637b93b0163SMatthew Wilcox * as node->private_list is protected by the i_pages lock.
63814b46879SJohannes Weiner */
639551c643fSPedro Falcato lockdep_assert_held(&node->array->xa_lock);
64068d48e6aSJohannes Weiner
64101959dfeSMatthew Wilcox if (node->count && node->count == node->nr_values) {
64268d48e6aSJohannes Weiner if (list_empty(&node->private_list)) {
6430a97c01cSNhat Pham list_lru_add_obj(&shadow_nodes, &node->private_list);
6444715c6a7SShakeel Butt __inc_node_page_state(page, WORKINGSET_NODES);
64568d48e6aSJohannes Weiner }
64614b46879SJohannes Weiner } else {
64768d48e6aSJohannes Weiner if (!list_empty(&node->private_list)) {
6480a97c01cSNhat Pham list_lru_del_obj(&shadow_nodes, &node->private_list);
6494715c6a7SShakeel Butt __dec_node_page_state(page, WORKINGSET_NODES);
65068d48e6aSJohannes Weiner }
65114b46879SJohannes Weiner }
65214b46879SJohannes Weiner }
653449dd698SJohannes Weiner
count_shadow_nodes(struct shrinker * shrinker,struct shrink_control * sc)654449dd698SJohannes Weiner static unsigned long count_shadow_nodes(struct shrinker *shrinker,
655449dd698SJohannes Weiner struct shrink_control *sc)
656449dd698SJohannes Weiner {
657449dd698SJohannes Weiner unsigned long max_nodes;
65814b46879SJohannes Weiner unsigned long nodes;
65995f9ab2dSJohannes Weiner unsigned long pages;
660449dd698SJohannes Weiner
66114b46879SJohannes Weiner nodes = list_lru_shrink_count(&shadow_nodes, sc);
662725cac1cSMiaohe Lin if (!nodes)
663725cac1cSMiaohe Lin return SHRINK_EMPTY;
664449dd698SJohannes Weiner
665449dd698SJohannes Weiner /*
666a97e7904SMatthew Wilcox * Approximate a reasonable limit for the nodes
667b5388998SJohannes Weiner * containing shadow entries. We don't need to keep more
668b5388998SJohannes Weiner * shadow entries than possible pages on the active list,
669b5388998SJohannes Weiner * since refault distances bigger than that are dismissed.
670b5388998SJohannes Weiner *
671b5388998SJohannes Weiner * The size of the active list converges toward 100% of
672b5388998SJohannes Weiner * overall page cache as memory grows, with only a tiny
673b5388998SJohannes Weiner * inactive list. Assume the total cache size for that.
674b5388998SJohannes Weiner *
675b5388998SJohannes Weiner * Nodes might be sparsely populated, with only one shadow
676b5388998SJohannes Weiner * entry in the extreme case. Obviously, we cannot keep one
677b5388998SJohannes Weiner * node for every eligible shadow entry, so compromise on a
678b5388998SJohannes Weiner * worst-case density of 1/8th. Below that, not all eligible
679b5388998SJohannes Weiner * refaults can be detected anymore.
680449dd698SJohannes Weiner *
681a97e7904SMatthew Wilcox * On 64-bit with 7 xa_nodes per page and 64 slots
682449dd698SJohannes Weiner * each, this will reclaim shadow entries when they consume
683b5388998SJohannes Weiner * ~1.8% of available memory:
684449dd698SJohannes Weiner *
685a97e7904SMatthew Wilcox * PAGE_SIZE / xa_nodes / node_entries * 8 / PAGE_SIZE
686449dd698SJohannes Weiner */
68795f9ab2dSJohannes Weiner #ifdef CONFIG_MEMCG
688b5388998SJohannes Weiner if (sc->memcg) {
68995f9ab2dSJohannes Weiner struct lruvec *lruvec;
6902b487e59SJohannes Weiner int i;
69195f9ab2dSJohannes Weiner
692d4a5b369SShakeel Butt mem_cgroup_flush_stats_ratelimited(sc->memcg);
693867e5e1dSJohannes Weiner lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid));
694*7404bd37SQi Zheng
6952b487e59SJohannes Weiner for (pages = 0, i = 0; i < NR_LRU_LISTS; i++)
696*7404bd37SQi Zheng pages += lruvec_lru_size(lruvec, i, MAX_NR_ZONES - 1);
697*7404bd37SQi Zheng
698d42f3245SRoman Gushchin pages += lruvec_page_state_local(
699d42f3245SRoman Gushchin lruvec, NR_SLAB_RECLAIMABLE_B) >> PAGE_SHIFT;
700d42f3245SRoman Gushchin pages += lruvec_page_state_local(
701d42f3245SRoman Gushchin lruvec, NR_SLAB_UNRECLAIMABLE_B) >> PAGE_SHIFT;
70295f9ab2dSJohannes Weiner } else
70395f9ab2dSJohannes Weiner #endif
70495f9ab2dSJohannes Weiner pages = node_present_pages(sc->nid);
70595f9ab2dSJohannes Weiner
706dad4f140SLinus Torvalds max_nodes = pages >> (XA_CHUNK_SHIFT - 3);
707449dd698SJohannes Weiner
70814b46879SJohannes Weiner if (nodes <= max_nodes)
709449dd698SJohannes Weiner return 0;
71014b46879SJohannes Weiner return nodes - max_nodes;
711449dd698SJohannes Weiner }
712449dd698SJohannes Weiner
shadow_lru_isolate(struct list_head * item,struct list_lru_one * lru,void * arg)713449dd698SJohannes Weiner static enum lru_status shadow_lru_isolate(struct list_head *item,
7143f97b163SVladimir Davydov struct list_lru_one *lru,
715da0c0251SKairui Song void *arg) __must_hold(lru->lock)
716449dd698SJohannes Weiner {
717a97e7904SMatthew Wilcox struct xa_node *node = container_of(item, struct xa_node, private_list);
718449dd698SJohannes Weiner struct address_space *mapping;
719449dd698SJohannes Weiner int ret;
720449dd698SJohannes Weiner
721449dd698SJohannes Weiner /*
722f82cd2f0SMatthew Wilcox (Oracle) * Page cache insertions and deletions synchronously maintain
723b93b0163SMatthew Wilcox * the shadow node LRU under the i_pages lock and the
724da0c0251SKairui Song * &lru->lock. Because the page cache tree is emptied before
725da0c0251SKairui Song * the inode can be destroyed, holding the &lru->lock pins any
726a97e7904SMatthew Wilcox * address_space that has nodes on the LRU.
727449dd698SJohannes Weiner *
728b93b0163SMatthew Wilcox * We can then safely transition to the i_pages lock to
729449dd698SJohannes Weiner * pin only the address_space of the particular node we want
730da0c0251SKairui Song * to reclaim, take the node off-LRU, and drop the &lru->lock.
731449dd698SJohannes Weiner */
732449dd698SJohannes Weiner
73301959dfeSMatthew Wilcox mapping = container_of(node->array, struct address_space, i_pages);
734449dd698SJohannes Weiner
735449dd698SJohannes Weiner /* Coming from the list, invert the lock order */
736b93b0163SMatthew Wilcox if (!xa_trylock(&mapping->i_pages)) {
737da0c0251SKairui Song spin_unlock_irq(&lru->lock);
738449dd698SJohannes Weiner ret = LRU_RETRY;
739449dd698SJohannes Weiner goto out;
740449dd698SJohannes Weiner }
741449dd698SJohannes Weiner
7425649d113SYang Yang /* For page cache we need to hold i_lock */
7435649d113SYang Yang if (mapping->host != NULL) {
74451b8c1feSJohannes Weiner if (!spin_trylock(&mapping->host->i_lock)) {
74551b8c1feSJohannes Weiner xa_unlock(&mapping->i_pages);
746da0c0251SKairui Song spin_unlock_irq(&lru->lock);
74751b8c1feSJohannes Weiner ret = LRU_RETRY;
74851b8c1feSJohannes Weiner goto out;
74951b8c1feSJohannes Weiner }
7505649d113SYang Yang }
75151b8c1feSJohannes Weiner
7523f97b163SVladimir Davydov list_lru_isolate(lru, item);
7534715c6a7SShakeel Butt __dec_node_page_state(virt_to_page(node), WORKINGSET_NODES);
75468d48e6aSJohannes Weiner
755da0c0251SKairui Song spin_unlock(&lru->lock);
756449dd698SJohannes Weiner
757449dd698SJohannes Weiner /*
758449dd698SJohannes Weiner * The nodes should only contain one or more shadow entries,
759449dd698SJohannes Weiner * no pages, so we expect to be able to remove them all and
760449dd698SJohannes Weiner * delete and free the empty node afterwards.
761449dd698SJohannes Weiner */
76201959dfeSMatthew Wilcox if (WARN_ON_ONCE(!node->nr_values))
763b936887eSJohannes Weiner goto out_invalid;
76401959dfeSMatthew Wilcox if (WARN_ON_ONCE(node->count != node->nr_values))
765b936887eSJohannes Weiner goto out_invalid;
766f82cd2f0SMatthew Wilcox (Oracle) xa_delete_node(node, workingset_update_node);
767f3b566d7SChen Ridong mod_lruvec_kmem_state(node, WORKINGSET_NODERECLAIM, 1);
768449dd698SJohannes Weiner
769b936887eSJohannes Weiner out_invalid:
7706ca342d0SSebastian Andrzej Siewior xa_unlock_irq(&mapping->i_pages);
7715649d113SYang Yang if (mapping->host != NULL) {
77251b8c1feSJohannes Weiner if (mapping_shrinkable(mapping))
7734c6b4087SMateusz Guzik inode_lru_list_add(mapping->host);
77451b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock);
7755649d113SYang Yang }
776449dd698SJohannes Weiner ret = LRU_REMOVED_RETRY;
777449dd698SJohannes Weiner out:
778449dd698SJohannes Weiner cond_resched();
779449dd698SJohannes Weiner return ret;
780449dd698SJohannes Weiner }
781449dd698SJohannes Weiner
scan_shadow_nodes(struct shrinker * shrinker,struct shrink_control * sc)782449dd698SJohannes Weiner static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
783449dd698SJohannes Weiner struct shrink_control *sc)
784449dd698SJohannes Weiner {
785b93b0163SMatthew Wilcox /* list_lru lock nests inside the IRQ-safe i_pages lock */
7866b51e881SSebastian Andrzej Siewior return list_lru_shrink_walk_irq(&shadow_nodes, sc, shadow_lru_isolate,
7876b51e881SSebastian Andrzej Siewior NULL);
788449dd698SJohannes Weiner }
789449dd698SJohannes Weiner
790449dd698SJohannes Weiner /*
791449dd698SJohannes Weiner * Our list_lru->lock is IRQ-safe as it nests inside the IRQ-safe
792b93b0163SMatthew Wilcox * i_pages lock.
793449dd698SJohannes Weiner */
794449dd698SJohannes Weiner static struct lock_class_key shadow_nodes_key;
795449dd698SJohannes Weiner
workingset_init(void)796449dd698SJohannes Weiner static int __init workingset_init(void)
797449dd698SJohannes Weiner {
798f3d652b0SKairui Song unsigned int timestamp_bits, timestamp_bits_anon;
799219c666eSQi Zheng struct shrinker *workingset_shadow_shrinker;
800612e4493SJohannes Weiner unsigned int max_order;
801219c666eSQi Zheng int ret = -ENOMEM;
802449dd698SJohannes Weiner
803612e4493SJohannes Weiner BUILD_BUG_ON(BITS_PER_LONG < EVICTION_SHIFT);
804612e4493SJohannes Weiner /*
805612e4493SJohannes Weiner * Calculate the eviction bucket size to cover the longest
806612e4493SJohannes Weiner * actionable refault distance, which is currently half of
807612e4493SJohannes Weiner * memory (totalram_pages/2). However, memory hotplug may add
808612e4493SJohannes Weiner * some more pages at runtime, so keep working with up to
809612e4493SJohannes Weiner * double the initial memory by using totalram_pages as-is.
810612e4493SJohannes Weiner */
811612e4493SJohannes Weiner timestamp_bits = BITS_PER_LONG - EVICTION_SHIFT;
812f3d652b0SKairui Song timestamp_bits_anon = BITS_PER_LONG - EVICTION_SHIFT_ANON;
813ca79b0c2SArun KS max_order = fls_long(totalram_pages() - 1);
814f3d652b0SKairui Song if (max_order > (BITS_PER_LONG - EVICTION_SHIFT))
815f3d652b0SKairui Song bucket_order[WORKINGSET_FILE] = max_order - timestamp_bits;
816f3d652b0SKairui Song if (max_order > timestamp_bits_anon)
817f3d652b0SKairui Song bucket_order[WORKINGSET_ANON] = max_order - timestamp_bits_anon;
818f3d652b0SKairui Song pr_info("workingset: timestamp_bits=%d (anon: %d) max_order=%d bucket_order=%u (anon: %d)\n",
819f3d652b0SKairui Song timestamp_bits, timestamp_bits_anon, max_order,
820f3d652b0SKairui Song bucket_order[WORKINGSET_FILE], bucket_order[WORKINGSET_ANON]);
821612e4493SJohannes Weiner
822219c666eSQi Zheng workingset_shadow_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE |
823219c666eSQi Zheng SHRINKER_MEMCG_AWARE,
824219c666eSQi Zheng "mm-shadow");
825219c666eSQi Zheng if (!workingset_shadow_shrinker)
826449dd698SJohannes Weiner goto err;
827219c666eSQi Zheng
8283f28bbe5SKairui Song ret = list_lru_init_memcg_key(&shadow_nodes, workingset_shadow_shrinker,
8293f28bbe5SKairui Song &shadow_nodes_key);
830449dd698SJohannes Weiner if (ret)
831449dd698SJohannes Weiner goto err_list_lru;
832219c666eSQi Zheng
833219c666eSQi Zheng workingset_shadow_shrinker->count_objects = count_shadow_nodes;
834219c666eSQi Zheng workingset_shadow_shrinker->scan_objects = scan_shadow_nodes;
835219c666eSQi Zheng /* ->count reports only fully expendable nodes */
836219c666eSQi Zheng workingset_shadow_shrinker->seeks = 0;
837219c666eSQi Zheng
838219c666eSQi Zheng shrinker_register(workingset_shadow_shrinker);
839449dd698SJohannes Weiner return 0;
840449dd698SJohannes Weiner err_list_lru:
841219c666eSQi Zheng shrinker_free(workingset_shadow_shrinker);
842449dd698SJohannes Weiner err:
843449dd698SJohannes Weiner return ret;
844449dd698SJohannes Weiner }
845449dd698SJohannes Weiner module_init(workingset_init);
846