xref: /openbmc/linux/mm/workingset.c (revision ba2929159000dc7015cc01cdf7bb72542e19952a)
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>
19a528910eSJohannes Weiner 
20a528910eSJohannes Weiner /*
21a528910eSJohannes Weiner  *		Double CLOCK lists
22a528910eSJohannes Weiner  *
231e6b1085SMel Gorman  * Per node, two clock lists are maintained for file pages: the
24a528910eSJohannes Weiner  * inactive and the active list.  Freshly faulted pages start out at
25a528910eSJohannes Weiner  * the head of the inactive list and page reclaim scans pages from the
26a528910eSJohannes Weiner  * tail.  Pages that are accessed multiple times on the inactive list
27a528910eSJohannes Weiner  * are promoted to the active list, to protect them from reclaim,
28a528910eSJohannes Weiner  * whereas active pages are demoted to the inactive list when the
29a528910eSJohannes Weiner  * active list grows too big.
30a528910eSJohannes Weiner  *
31a528910eSJohannes Weiner  *   fault ------------------------+
32a528910eSJohannes Weiner  *                                 |
33a528910eSJohannes Weiner  *              +--------------+   |            +-------------+
34a528910eSJohannes Weiner  *   reclaim <- |   inactive   | <-+-- demotion |    active   | <--+
35a528910eSJohannes Weiner  *              +--------------+                +-------------+    |
36a528910eSJohannes Weiner  *                     |                                           |
37a528910eSJohannes Weiner  *                     +-------------- promotion ------------------+
38a528910eSJohannes Weiner  *
39a528910eSJohannes Weiner  *
40a528910eSJohannes Weiner  *		Access frequency and refault distance
41a528910eSJohannes Weiner  *
42a528910eSJohannes Weiner  * A workload is thrashing when its pages are frequently used but they
43a528910eSJohannes Weiner  * are evicted from the inactive list every time before another access
44a528910eSJohannes Weiner  * would have promoted them to the active list.
45a528910eSJohannes Weiner  *
46a528910eSJohannes Weiner  * In cases where the average access distance between thrashing pages
47a528910eSJohannes Weiner  * is bigger than the size of memory there is nothing that can be
48a528910eSJohannes Weiner  * done - the thrashing set could never fit into memory under any
49a528910eSJohannes Weiner  * circumstance.
50a528910eSJohannes Weiner  *
51a528910eSJohannes Weiner  * However, the average access distance could be bigger than the
52a528910eSJohannes Weiner  * inactive list, yet smaller than the size of memory.  In this case,
53a528910eSJohannes Weiner  * the set could fit into memory if it weren't for the currently
54a528910eSJohannes Weiner  * active pages - which may be used more, hopefully less frequently:
55a528910eSJohannes Weiner  *
56a528910eSJohannes Weiner  *      +-memory available to cache-+
57a528910eSJohannes Weiner  *      |                           |
58a528910eSJohannes Weiner  *      +-inactive------+-active----+
59a528910eSJohannes Weiner  *  a b | c d e f g h i | J K L M N |
60a528910eSJohannes Weiner  *      +---------------+-----------+
61a528910eSJohannes Weiner  *
62a528910eSJohannes Weiner  * It is prohibitively expensive to accurately track access frequency
63a528910eSJohannes Weiner  * of pages.  But a reasonable approximation can be made to measure
64a528910eSJohannes Weiner  * thrashing on the inactive list, after which refaulting pages can be
65a528910eSJohannes Weiner  * activated optimistically to compete with the existing active pages.
66a528910eSJohannes Weiner  *
67a528910eSJohannes Weiner  * Approximating inactive page access frequency - Observations:
68a528910eSJohannes Weiner  *
69a528910eSJohannes Weiner  * 1. When a page is accessed for the first time, it is added to the
70a528910eSJohannes Weiner  *    head of the inactive list, slides every existing inactive page
71a528910eSJohannes Weiner  *    towards the tail by one slot, and pushes the current tail page
72a528910eSJohannes Weiner  *    out of memory.
73a528910eSJohannes Weiner  *
74a528910eSJohannes Weiner  * 2. When a page is accessed for the second time, it is promoted to
75a528910eSJohannes Weiner  *    the active list, shrinking the inactive list by one slot.  This
76a528910eSJohannes Weiner  *    also slides all inactive pages that were faulted into the cache
77a528910eSJohannes Weiner  *    more recently than the activated page towards the tail of the
78a528910eSJohannes Weiner  *    inactive list.
79a528910eSJohannes Weiner  *
80a528910eSJohannes Weiner  * Thus:
81a528910eSJohannes Weiner  *
82a528910eSJohannes Weiner  * 1. The sum of evictions and activations between any two points in
83a528910eSJohannes Weiner  *    time indicate the minimum number of inactive pages accessed in
84a528910eSJohannes Weiner  *    between.
85a528910eSJohannes Weiner  *
86a528910eSJohannes Weiner  * 2. Moving one inactive page N page slots towards the tail of the
87a528910eSJohannes Weiner  *    list requires at least N inactive page accesses.
88a528910eSJohannes Weiner  *
89a528910eSJohannes Weiner  * Combining these:
90a528910eSJohannes Weiner  *
91a528910eSJohannes Weiner  * 1. When a page is finally evicted from memory, the number of
92a528910eSJohannes Weiner  *    inactive pages accessed while the page was in cache is at least
93a528910eSJohannes Weiner  *    the number of page slots on the inactive list.
94a528910eSJohannes Weiner  *
95a528910eSJohannes Weiner  * 2. In addition, measuring the sum of evictions and activations (E)
96a528910eSJohannes Weiner  *    at the time of a page's eviction, and comparing it to another
97a528910eSJohannes Weiner  *    reading (R) at the time the page faults back into memory tells
98a528910eSJohannes Weiner  *    the minimum number of accesses while the page was not cached.
99a528910eSJohannes Weiner  *    This is called the refault distance.
100a528910eSJohannes Weiner  *
101a528910eSJohannes Weiner  * Because the first access of the page was the fault and the second
102a528910eSJohannes Weiner  * access the refault, we combine the in-cache distance with the
103a528910eSJohannes Weiner  * out-of-cache distance to get the complete minimum access distance
104a528910eSJohannes Weiner  * of this page:
105a528910eSJohannes Weiner  *
106a528910eSJohannes Weiner  *      NR_inactive + (R - E)
107a528910eSJohannes Weiner  *
108a528910eSJohannes Weiner  * And knowing the minimum access distance of a page, we can easily
109a528910eSJohannes Weiner  * tell if the page would be able to stay in cache assuming all page
110a528910eSJohannes Weiner  * slots in the cache were available:
111a528910eSJohannes Weiner  *
112a528910eSJohannes Weiner  *   NR_inactive + (R - E) <= NR_inactive + NR_active
113a528910eSJohannes Weiner  *
114ed8f3f99SYang Yang  * If we have swap we should consider about NR_inactive_anon and
115ed8f3f99SYang Yang  * NR_active_anon, so for page cache and anonymous respectively:
116a528910eSJohannes Weiner  *
117ed8f3f99SYang Yang  *   NR_inactive_file + (R - E) <= NR_inactive_file + NR_active_file
118ed8f3f99SYang Yang  *   + NR_inactive_anon + NR_active_anon
119ed8f3f99SYang Yang  *
120ed8f3f99SYang Yang  *   NR_inactive_anon + (R - E) <= NR_inactive_anon + NR_active_anon
121ed8f3f99SYang Yang  *   + NR_inactive_file + NR_active_file
122ed8f3f99SYang Yang  *
123ed8f3f99SYang Yang  * Which can be further simplified to:
124ed8f3f99SYang Yang  *
125ed8f3f99SYang Yang  *   (R - E) <= NR_active_file + NR_inactive_anon + NR_active_anon
126ed8f3f99SYang Yang  *
127ed8f3f99SYang Yang  *   (R - E) <= NR_active_anon + NR_inactive_file + NR_active_file
128a528910eSJohannes Weiner  *
129a528910eSJohannes Weiner  * Put into words, the refault distance (out-of-cache) can be seen as
130a528910eSJohannes Weiner  * a deficit in inactive list space (in-cache).  If the inactive list
131a528910eSJohannes Weiner  * had (R - E) more page slots, the page would not have been evicted
132a528910eSJohannes Weiner  * in between accesses, but activated instead.  And on a full system,
133a528910eSJohannes Weiner  * the only thing eating into inactive list space is active pages.
134a528910eSJohannes Weiner  *
135a528910eSJohannes Weiner  *
1361899ad18SJohannes Weiner  *		Refaulting inactive pages
137a528910eSJohannes Weiner  *
138a528910eSJohannes Weiner  * All that is known about the active list is that the pages have been
139a528910eSJohannes Weiner  * accessed more than once in the past.  This means that at any given
140a528910eSJohannes Weiner  * time there is actually a good chance that pages on the active list
141a528910eSJohannes Weiner  * are no longer in active use.
142a528910eSJohannes Weiner  *
143a528910eSJohannes Weiner  * So when a refault distance of (R - E) is observed and there are at
144ed8f3f99SYang Yang  * least (R - E) pages in the userspace workingset, the refaulting page
145ed8f3f99SYang Yang  * is activated optimistically in the hope that (R - E) pages are actually
146a528910eSJohannes Weiner  * used less frequently than the refaulting page - or even not used at
147a528910eSJohannes Weiner  * all anymore.
148a528910eSJohannes Weiner  *
1491899ad18SJohannes Weiner  * That means if inactive cache is refaulting with a suitable refault
1501899ad18SJohannes Weiner  * distance, we assume the cache workingset is transitioning and put
151ed8f3f99SYang Yang  * pressure on the current workingset.
1521899ad18SJohannes Weiner  *
153a528910eSJohannes Weiner  * If this is wrong and demotion kicks in, the pages which are truly
154a528910eSJohannes Weiner  * used more frequently will be reactivated while the less frequently
155a528910eSJohannes Weiner  * used once will be evicted from memory.
156a528910eSJohannes Weiner  *
157a528910eSJohannes Weiner  * But if this is right, the stale pages will be pushed out of memory
158a528910eSJohannes Weiner  * and the used pages get to stay in cache.
159a528910eSJohannes Weiner  *
1601899ad18SJohannes Weiner  *		Refaulting active pages
1611899ad18SJohannes Weiner  *
1621899ad18SJohannes Weiner  * If on the other hand the refaulting pages have recently been
1631899ad18SJohannes Weiner  * deactivated, it means that the active list is no longer protecting
1641899ad18SJohannes Weiner  * actively used cache from reclaim. The cache is NOT transitioning to
1651899ad18SJohannes Weiner  * a different workingset; the existing workingset is thrashing in the
1661899ad18SJohannes Weiner  * space allocated to the page cache.
1671899ad18SJohannes Weiner  *
168a528910eSJohannes Weiner  *
169a528910eSJohannes Weiner  *		Implementation
170a528910eSJohannes Weiner  *
17131d8fcacSJohannes Weiner  * For each node's LRU lists, a counter for inactive evictions and
17231d8fcacSJohannes Weiner  * activations is maintained (node->nonresident_age).
173a528910eSJohannes Weiner  *
174a528910eSJohannes Weiner  * On eviction, a snapshot of this counter (along with some bits to
175a97e7904SMatthew Wilcox  * identify the node) is stored in the now empty page cache
176a528910eSJohannes Weiner  * slot of the evicted page.  This is called a shadow entry.
177a528910eSJohannes Weiner  *
178a528910eSJohannes Weiner  * On cache misses for which there are shadow entries, an eligible
179a528910eSJohannes Weiner  * refault distance will immediately activate the refaulting page.
180a528910eSJohannes Weiner  */
181a528910eSJohannes Weiner 
1823ebc57f4SMiaohe Lin #define WORKINGSET_SHIFT 1
1833159f943SMatthew Wilcox #define EVICTION_SHIFT	((BITS_PER_LONG - BITS_PER_XA_VALUE) +	\
1843ebc57f4SMiaohe Lin 			 WORKINGSET_SHIFT + NODES_SHIFT + \
1853ebc57f4SMiaohe Lin 			 MEM_CGROUP_ID_SHIFT)
186689c94f0SJohannes Weiner #define EVICTION_MASK	(~0UL >> EVICTION_SHIFT)
187689c94f0SJohannes Weiner 
188612e4493SJohannes Weiner /*
189612e4493SJohannes Weiner  * Eviction timestamps need to be able to cover the full range of
190a97e7904SMatthew Wilcox  * actionable refaults. However, bits are tight in the xarray
191612e4493SJohannes Weiner  * entry, and after storing the identifier for the lruvec there might
192612e4493SJohannes Weiner  * not be enough left to represent every single actionable refault. In
193612e4493SJohannes Weiner  * that case, we have to sacrifice granularity for distance, and group
194612e4493SJohannes Weiner  * evictions into coarser buckets by shaving off lower timestamp bits.
195612e4493SJohannes Weiner  */
196612e4493SJohannes Weiner static unsigned int bucket_order __read_mostly;
197612e4493SJohannes Weiner 
pack_shadow(int memcgid,pg_data_t * pgdat,unsigned long eviction,bool workingset)1981899ad18SJohannes Weiner static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction,
1991899ad18SJohannes Weiner 			 bool workingset)
200a528910eSJohannes Weiner {
2013159f943SMatthew Wilcox 	eviction &= EVICTION_MASK;
20223047a96SJohannes Weiner 	eviction = (eviction << MEM_CGROUP_ID_SHIFT) | memcgid;
2031e6b1085SMel Gorman 	eviction = (eviction << NODES_SHIFT) | pgdat->node_id;
2043ebc57f4SMiaohe Lin 	eviction = (eviction << WORKINGSET_SHIFT) | workingset;
205a528910eSJohannes Weiner 
2063159f943SMatthew Wilcox 	return xa_mk_value(eviction);
207a528910eSJohannes Weiner }
208a528910eSJohannes Weiner 
unpack_shadow(void * shadow,int * memcgidp,pg_data_t ** pgdat,unsigned long * evictionp,bool * workingsetp)2091e6b1085SMel Gorman static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
2101899ad18SJohannes Weiner 			  unsigned long *evictionp, bool *workingsetp)
211a528910eSJohannes Weiner {
2123159f943SMatthew Wilcox 	unsigned long entry = xa_to_value(shadow);
2131e6b1085SMel Gorman 	int memcgid, nid;
2141899ad18SJohannes Weiner 	bool workingset;
215a528910eSJohannes Weiner 
2163ebc57f4SMiaohe Lin 	workingset = entry & ((1UL << WORKINGSET_SHIFT) - 1);
2173ebc57f4SMiaohe Lin 	entry >>= WORKINGSET_SHIFT;
218a528910eSJohannes Weiner 	nid = entry & ((1UL << NODES_SHIFT) - 1);
219a528910eSJohannes Weiner 	entry >>= NODES_SHIFT;
22023047a96SJohannes Weiner 	memcgid = entry & ((1UL << MEM_CGROUP_ID_SHIFT) - 1);
22123047a96SJohannes Weiner 	entry >>= MEM_CGROUP_ID_SHIFT;
222a528910eSJohannes Weiner 
22323047a96SJohannes Weiner 	*memcgidp = memcgid;
2241e6b1085SMel Gorman 	*pgdat = NODE_DATA(nid);
225ac35a490SYu Zhao 	*evictionp = entry;
2261899ad18SJohannes Weiner 	*workingsetp = workingset;
227a528910eSJohannes Weiner }
228a528910eSJohannes Weiner 
229ac35a490SYu Zhao #ifdef CONFIG_LRU_GEN
230ac35a490SYu Zhao 
lru_gen_eviction(struct folio * folio)231ac35a490SYu Zhao static void *lru_gen_eviction(struct folio *folio)
232ac35a490SYu Zhao {
233ac35a490SYu Zhao 	int hist;
234ac35a490SYu Zhao 	unsigned long token;
235ac35a490SYu Zhao 	unsigned long min_seq;
236ac35a490SYu Zhao 	struct lruvec *lruvec;
237391655feSYu Zhao 	struct lru_gen_folio *lrugen;
238ac35a490SYu Zhao 	int type = folio_is_file_lru(folio);
239ac35a490SYu Zhao 	int delta = folio_nr_pages(folio);
240ac35a490SYu Zhao 	int refs = folio_lru_refs(folio);
241ac35a490SYu Zhao 	int tier = lru_tier_from_refs(refs);
242ac35a490SYu Zhao 	struct mem_cgroup *memcg = folio_memcg(folio);
243ac35a490SYu Zhao 	struct pglist_data *pgdat = folio_pgdat(folio);
244ac35a490SYu Zhao 
245ac35a490SYu Zhao 	BUILD_BUG_ON(LRU_GEN_WIDTH + LRU_REFS_WIDTH > BITS_PER_LONG - EVICTION_SHIFT);
246ac35a490SYu Zhao 
247ac35a490SYu Zhao 	lruvec = mem_cgroup_lruvec(memcg, pgdat);
248ac35a490SYu Zhao 	lrugen = &lruvec->lrugen;
249ac35a490SYu Zhao 	min_seq = READ_ONCE(lrugen->min_seq[type]);
250ac35a490SYu Zhao 	token = (min_seq << LRU_REFS_WIDTH) | max(refs - 1, 0);
251ac35a490SYu Zhao 
252ac35a490SYu Zhao 	hist = lru_hist_from_seq(min_seq);
253ac35a490SYu Zhao 	atomic_long_add(delta, &lrugen->evicted[hist][type][tier]);
254ac35a490SYu Zhao 
255ac35a490SYu Zhao 	return pack_shadow(mem_cgroup_id(memcg), pgdat, token, refs);
256ac35a490SYu Zhao }
257ac35a490SYu Zhao 
258ffcb5f52SNhat Pham /*
259ffcb5f52SNhat Pham  * Tests if the shadow entry is for a folio that was recently evicted.
260d7f1afd0ST.J. Alumbaugh  * Fills in @lruvec, @token, @workingset with the values unpacked from shadow.
261ffcb5f52SNhat Pham  */
lru_gen_test_recent(void * shadow,bool file,struct lruvec ** lruvec,unsigned long * token,bool * workingset)262d7f1afd0ST.J. Alumbaugh static bool lru_gen_test_recent(void *shadow, bool file, struct lruvec **lruvec,
263d7f1afd0ST.J. Alumbaugh 				unsigned long *token, bool *workingset)
264ffcb5f52SNhat Pham {
265d7f1afd0ST.J. Alumbaugh 	int memcg_id;
266ffcb5f52SNhat Pham 	unsigned long min_seq;
267d7f1afd0ST.J. Alumbaugh 	struct mem_cgroup *memcg;
268d7f1afd0ST.J. Alumbaugh 	struct pglist_data *pgdat;
269ffcb5f52SNhat Pham 
270d7f1afd0ST.J. Alumbaugh 	unpack_shadow(shadow, &memcg_id, &pgdat, token, workingset);
271ffcb5f52SNhat Pham 
272d7f1afd0ST.J. Alumbaugh 	memcg = mem_cgroup_from_id(memcg_id);
273d7f1afd0ST.J. Alumbaugh 	*lruvec = mem_cgroup_lruvec(memcg, pgdat);
274ffcb5f52SNhat Pham 
275d7f1afd0ST.J. Alumbaugh 	min_seq = READ_ONCE((*lruvec)->lrugen.min_seq[file]);
276ffcb5f52SNhat Pham 	return (*token >> LRU_REFS_WIDTH) == (min_seq & (EVICTION_MASK >> LRU_REFS_WIDTH));
277ffcb5f52SNhat Pham }
278ffcb5f52SNhat Pham 
lru_gen_refault(struct folio * folio,void * shadow)279ac35a490SYu Zhao static void lru_gen_refault(struct folio *folio, void *shadow)
280ac35a490SYu Zhao {
2813af0191aSKalesh Singh 	bool recent;
282ac35a490SYu Zhao 	int hist, tier, refs;
283ac35a490SYu Zhao 	bool workingset;
284ac35a490SYu Zhao 	unsigned long token;
285ac35a490SYu Zhao 	struct lruvec *lruvec;
286391655feSYu Zhao 	struct lru_gen_folio *lrugen;
287ac35a490SYu Zhao 	int type = folio_is_file_lru(folio);
288ac35a490SYu Zhao 	int delta = folio_nr_pages(folio);
289ac35a490SYu Zhao 
290ac35a490SYu Zhao 	rcu_read_lock();
291ac35a490SYu Zhao 
2923af0191aSKalesh Singh 	recent = lru_gen_test_recent(shadow, type, &lruvec, &token, &workingset);
2933af0191aSKalesh Singh 	if (lruvec != folio_lruvec(folio))
294ffcb5f52SNhat Pham 		goto unlock;
295ffcb5f52SNhat Pham 
2963af0191aSKalesh Singh 	mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + type, delta);
2973af0191aSKalesh Singh 
2983af0191aSKalesh Singh 	if (!recent)
299ac35a490SYu Zhao 		goto unlock;
300ac35a490SYu Zhao 
301ac35a490SYu Zhao 	lrugen = &lruvec->lrugen;
302ac35a490SYu Zhao 
303d7f1afd0ST.J. Alumbaugh 	hist = lru_hist_from_seq(READ_ONCE(lrugen->min_seq[type]));
304ac35a490SYu Zhao 	/* see the comment in folio_lru_refs() */
305ac35a490SYu Zhao 	refs = (token & (BIT(LRU_REFS_WIDTH) - 1)) + workingset;
306ac35a490SYu Zhao 	tier = lru_tier_from_refs(refs);
307ac35a490SYu Zhao 
308ac35a490SYu Zhao 	atomic_long_add(delta, &lrugen->refaulted[hist][type][tier]);
3093af0191aSKalesh Singh 	mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta);
310ac35a490SYu Zhao 
311ac35a490SYu Zhao 	/*
312ac35a490SYu Zhao 	 * Count the following two cases as stalls:
313ac35a490SYu Zhao 	 * 1. For pages accessed through page tables, hotter pages pushed out
314ac35a490SYu Zhao 	 *    hot pages which refaulted immediately.
315ac35a490SYu Zhao 	 * 2. For pages accessed multiple times through file descriptors,
316b2ce691bSYu Zhao 	 *    they would have been protected by sort_folio().
317ac35a490SYu Zhao 	 */
318b2ce691bSYu Zhao 	if (lru_gen_in_fault() || refs >= BIT(LRU_REFS_WIDTH) - 1) {
319b2ce691bSYu Zhao 		set_mask_bits(&folio->flags, 0, LRU_REFS_MASK | BIT(PG_workingset));
320ac35a490SYu Zhao 		mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta);
321ac35a490SYu Zhao 	}
322ac35a490SYu Zhao unlock:
323ac35a490SYu Zhao 	rcu_read_unlock();
324ac35a490SYu Zhao }
325ac35a490SYu Zhao 
326ac35a490SYu Zhao #else /* !CONFIG_LRU_GEN */
327ac35a490SYu Zhao 
lru_gen_eviction(struct folio * folio)328ac35a490SYu Zhao static void *lru_gen_eviction(struct folio *folio)
329ac35a490SYu Zhao {
330ac35a490SYu Zhao 	return NULL;
331ac35a490SYu Zhao }
332ac35a490SYu Zhao 
lru_gen_test_recent(void * shadow,bool file,struct lruvec ** lruvec,unsigned long * token,bool * workingset)333d7f1afd0ST.J. Alumbaugh static bool lru_gen_test_recent(void *shadow, bool file, struct lruvec **lruvec,
334d7f1afd0ST.J. Alumbaugh 				unsigned long *token, bool *workingset)
335ffcb5f52SNhat Pham {
336ffcb5f52SNhat Pham 	return false;
337ffcb5f52SNhat Pham }
338ffcb5f52SNhat Pham 
lru_gen_refault(struct folio * folio,void * shadow)339ac35a490SYu Zhao static void lru_gen_refault(struct folio *folio, void *shadow)
340ac35a490SYu Zhao {
341ac35a490SYu Zhao }
342ac35a490SYu Zhao 
343ac35a490SYu Zhao #endif /* CONFIG_LRU_GEN */
344ac35a490SYu Zhao 
34531d8fcacSJohannes Weiner /**
34631d8fcacSJohannes Weiner  * workingset_age_nonresident - age non-resident entries as LRU ages
347e755f4afSXiaofei Tan  * @lruvec: the lruvec that was aged
34831d8fcacSJohannes Weiner  * @nr_pages: the number of pages to count
34931d8fcacSJohannes Weiner  *
35031d8fcacSJohannes Weiner  * As in-memory pages are aged, non-resident pages need to be aged as
35131d8fcacSJohannes Weiner  * well, in order for the refault distances later on to be comparable
35231d8fcacSJohannes Weiner  * to the in-memory dimensions. This function allows reclaim and LRU
35331d8fcacSJohannes Weiner  * operations to drive the non-resident aging along in parallel.
35431d8fcacSJohannes Weiner  */
workingset_age_nonresident(struct lruvec * lruvec,unsigned long nr_pages)35531d8fcacSJohannes Weiner void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages)
356b910718aSJohannes Weiner {
357b910718aSJohannes Weiner 	/*
358b910718aSJohannes Weiner 	 * Reclaiming a cgroup means reclaiming all its children in a
359b910718aSJohannes Weiner 	 * round-robin fashion. That means that each cgroup has an LRU
360b910718aSJohannes Weiner 	 * order that is composed of the LRU orders of its child
361b910718aSJohannes Weiner 	 * cgroups; and every page has an LRU position not just in the
362b910718aSJohannes Weiner 	 * cgroup that owns it, but in all of that group's ancestors.
363b910718aSJohannes Weiner 	 *
364b910718aSJohannes Weiner 	 * So when the physical inactive list of a leaf cgroup ages,
365b910718aSJohannes Weiner 	 * the virtual inactive lists of all its parents, including
366b910718aSJohannes Weiner 	 * the root cgroup's, age as well.
367b910718aSJohannes Weiner 	 */
368b910718aSJohannes Weiner 	do {
36931d8fcacSJohannes Weiner 		atomic_long_add(nr_pages, &lruvec->nonresident_age);
37031d8fcacSJohannes Weiner 	} while ((lruvec = parent_lruvec(lruvec)));
371b910718aSJohannes Weiner }
372b910718aSJohannes Weiner 
373a528910eSJohannes Weiner /**
3748927f647SMatthew Wilcox (Oracle)  * workingset_eviction - note the eviction of a folio from memory
375b910718aSJohannes Weiner  * @target_memcg: the cgroup that is causing the reclaim
3768927f647SMatthew Wilcox (Oracle)  * @folio: the folio being evicted
377a528910eSJohannes Weiner  *
3788927f647SMatthew Wilcox (Oracle)  * Return: a shadow entry to be stored in @folio->mapping->i_pages in place
3798927f647SMatthew Wilcox (Oracle)  * of the evicted @folio so that a later refault can be detected.
380a528910eSJohannes Weiner  */
workingset_eviction(struct folio * folio,struct mem_cgroup * target_memcg)3818927f647SMatthew Wilcox (Oracle) void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg)
382a528910eSJohannes Weiner {
3838927f647SMatthew Wilcox (Oracle) 	struct pglist_data *pgdat = folio_pgdat(folio);
384a528910eSJohannes Weiner 	unsigned long eviction;
38523047a96SJohannes Weiner 	struct lruvec *lruvec;
386b910718aSJohannes Weiner 	int memcgid;
387a528910eSJohannes Weiner 
3888927f647SMatthew Wilcox (Oracle) 	/* Folio is fully exclusive and pins folio's memory cgroup pointer */
3898927f647SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
3908927f647SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
3918927f647SMatthew Wilcox (Oracle) 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
39223047a96SJohannes Weiner 
393ac35a490SYu Zhao 	if (lru_gen_enabled())
394ac35a490SYu Zhao 		return lru_gen_eviction(folio);
395ac35a490SYu Zhao 
396b910718aSJohannes Weiner 	lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
397b910718aSJohannes Weiner 	/* XXX: target_memcg can be NULL, go through lruvec */
398b910718aSJohannes Weiner 	memcgid = mem_cgroup_id(lruvec_memcg(lruvec));
39931d8fcacSJohannes Weiner 	eviction = atomic_long_read(&lruvec->nonresident_age);
400ac35a490SYu Zhao 	eviction >>= bucket_order;
4018927f647SMatthew Wilcox (Oracle) 	workingset_age_nonresident(lruvec, folio_nr_pages(folio));
4028927f647SMatthew Wilcox (Oracle) 	return pack_shadow(memcgid, pgdat, eviction,
4038927f647SMatthew Wilcox (Oracle) 				folio_test_workingset(folio));
404a528910eSJohannes Weiner }
405a528910eSJohannes Weiner 
406a528910eSJohannes Weiner /**
407ffcb5f52SNhat Pham  * workingset_test_recent - tests if the shadow entry is for a folio that was
408ffcb5f52SNhat Pham  * recently evicted. Also fills in @workingset with the value unpacked from
409ffcb5f52SNhat Pham  * shadow.
410ffcb5f52SNhat Pham  * @shadow: the shadow entry to be tested.
411ffcb5f52SNhat Pham  * @file: whether the corresponding folio is from the file lru.
412ffcb5f52SNhat Pham  * @workingset: where the workingset value unpacked from shadow should
413ffcb5f52SNhat Pham  * be stored.
414a528910eSJohannes Weiner  *
415ffcb5f52SNhat Pham  * Return: true if the shadow is for a recently evicted folio; false otherwise.
416a528910eSJohannes Weiner  */
workingset_test_recent(void * shadow,bool file,bool * workingset)417ffcb5f52SNhat Pham bool workingset_test_recent(void *shadow, bool file, bool *workingset)
418a528910eSJohannes Weiner {
419b910718aSJohannes Weiner 	struct mem_cgroup *eviction_memcg;
420b910718aSJohannes Weiner 	struct lruvec *eviction_lruvec;
421a528910eSJohannes Weiner 	unsigned long refault_distance;
42234e58cacSJohannes Weiner 	unsigned long workingset_size;
423162453bfSJohannes Weiner 	unsigned long refault;
42423047a96SJohannes Weiner 	int memcgid;
425ffcb5f52SNhat Pham 	struct pglist_data *pgdat;
426ffcb5f52SNhat Pham 	unsigned long eviction;
427a528910eSJohannes Weiner 
428ffcb5f52SNhat Pham 	if (lru_gen_enabled())
429d7f1afd0ST.J. Alumbaugh 		return lru_gen_test_recent(shadow, file, &eviction_lruvec, &eviction, workingset);
430ac35a490SYu Zhao 
431ffcb5f52SNhat Pham 	unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset);
432ac35a490SYu Zhao 	eviction <<= bucket_order;
433162453bfSJohannes Weiner 
43423047a96SJohannes Weiner 	/*
43523047a96SJohannes Weiner 	 * Look up the memcg associated with the stored ID. It might
4360995d7e5SMatthew Wilcox (Oracle) 	 * have been deleted since the folio's eviction.
43723047a96SJohannes Weiner 	 *
43823047a96SJohannes Weiner 	 * Note that in rare events the ID could have been recycled
4390995d7e5SMatthew Wilcox (Oracle) 	 * for a new cgroup that refaults a shared folio. This is
44023047a96SJohannes Weiner 	 * impossible to tell from the available data. However, this
44123047a96SJohannes Weiner 	 * should be a rare and limited disturbance, and activations
44223047a96SJohannes Weiner 	 * are always speculative anyway. Ultimately, it's the aging
44323047a96SJohannes Weiner 	 * algorithm's job to shake out the minimum access frequency
44423047a96SJohannes Weiner 	 * for the active cache.
44523047a96SJohannes Weiner 	 *
44623047a96SJohannes Weiner 	 * XXX: On !CONFIG_MEMCG, this will always return NULL; it
44723047a96SJohannes Weiner 	 * would be better if the root_mem_cgroup existed in all
44823047a96SJohannes Weiner 	 * configurations instead.
44923047a96SJohannes Weiner 	 */
450b910718aSJohannes Weiner 	eviction_memcg = mem_cgroup_from_id(memcgid);
451b910718aSJohannes Weiner 	if (!mem_cgroup_disabled() && !eviction_memcg)
452ffcb5f52SNhat Pham 		return false;
453ffcb5f52SNhat Pham 
454b910718aSJohannes Weiner 	eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat);
45531d8fcacSJohannes Weiner 	refault = atomic_long_read(&eviction_lruvec->nonresident_age);
456162453bfSJohannes Weiner 
457162453bfSJohannes Weiner 	/*
4581899ad18SJohannes Weiner 	 * Calculate the refault distance
459162453bfSJohannes Weiner 	 *
4601899ad18SJohannes Weiner 	 * The unsigned subtraction here gives an accurate distance
46131d8fcacSJohannes Weiner 	 * across nonresident_age overflows in most cases. There is a
4621899ad18SJohannes Weiner 	 * special case: usually, shadow entries have a short lifetime
4631899ad18SJohannes Weiner 	 * and are either refaulted or reclaimed along with the inode
4641899ad18SJohannes Weiner 	 * before they get too old.  But it is not impossible for the
46531d8fcacSJohannes Weiner 	 * nonresident_age to lap a shadow entry in the field, which
46631d8fcacSJohannes Weiner 	 * can then result in a false small refault distance, leading
46731d8fcacSJohannes Weiner 	 * to a false activation should this old entry actually
46831d8fcacSJohannes Weiner 	 * refault again.  However, earlier kernels used to deactivate
4691899ad18SJohannes Weiner 	 * unconditionally with *every* reclaim invocation for the
4701899ad18SJohannes Weiner 	 * longest time, so the occasional inappropriate activation
4711899ad18SJohannes Weiner 	 * leading to pressure on the active list is not a problem.
472162453bfSJohannes Weiner 	 */
473162453bfSJohannes Weiner 	refault_distance = (refault - eviction) & EVICTION_MASK;
474162453bfSJohannes Weiner 
475b910718aSJohannes Weiner 	/*
4761899ad18SJohannes Weiner 	 * Compare the distance to the existing workingset size. We
47734e58cacSJohannes Weiner 	 * don't activate pages that couldn't stay resident even if
478aae466b0SJoonsoo Kim 	 * all the memory was available to the workingset. Whether
479aae466b0SJoonsoo Kim 	 * workingset competition needs to consider anon or not depends
480ed8f3f99SYang Yang 	 * on having free swap space.
4811899ad18SJohannes Weiner 	 */
48234e58cacSJohannes Weiner 	workingset_size = lruvec_page_state(eviction_lruvec, NR_ACTIVE_FILE);
483aae466b0SJoonsoo Kim 	if (!file) {
484aae466b0SJoonsoo Kim 		workingset_size += lruvec_page_state(eviction_lruvec,
485aae466b0SJoonsoo Kim 						     NR_INACTIVE_FILE);
486aae466b0SJoonsoo Kim 	}
487f78dfc7bSJohannes Weiner 	if (mem_cgroup_get_nr_swap_pages(eviction_memcg) > 0) {
48834e58cacSJohannes Weiner 		workingset_size += lruvec_page_state(eviction_lruvec,
48934e58cacSJohannes Weiner 						     NR_ACTIVE_ANON);
490aae466b0SJoonsoo Kim 		if (file) {
491aae466b0SJoonsoo Kim 			workingset_size += lruvec_page_state(eviction_lruvec,
492aae466b0SJoonsoo Kim 						     NR_INACTIVE_ANON);
493aae466b0SJoonsoo Kim 		}
49434e58cacSJohannes Weiner 	}
495ffcb5f52SNhat Pham 
496ffcb5f52SNhat Pham 	return refault_distance <= workingset_size;
497ffcb5f52SNhat Pham }
498ffcb5f52SNhat Pham 
499ffcb5f52SNhat Pham /**
500ffcb5f52SNhat Pham  * workingset_refault - Evaluate the refault of a previously evicted folio.
501ffcb5f52SNhat Pham  * @folio: The freshly allocated replacement folio.
502ffcb5f52SNhat Pham  * @shadow: Shadow entry of the evicted folio.
503ffcb5f52SNhat Pham  *
504ffcb5f52SNhat Pham  * Calculates and evaluates the refault distance of the previously
505ffcb5f52SNhat Pham  * evicted folio in the context of the node and the memcg whose memory
506ffcb5f52SNhat Pham  * pressure caused the eviction.
507ffcb5f52SNhat Pham  */
workingset_refault(struct folio * folio,void * shadow)508ffcb5f52SNhat Pham void workingset_refault(struct folio *folio, void *shadow)
509ffcb5f52SNhat Pham {
510ffcb5f52SNhat Pham 	bool file = folio_is_file_lru(folio);
511ffcb5f52SNhat Pham 	struct pglist_data *pgdat;
512ffcb5f52SNhat Pham 	struct mem_cgroup *memcg;
513ffcb5f52SNhat Pham 	struct lruvec *lruvec;
514ffcb5f52SNhat Pham 	bool workingset;
515ffcb5f52SNhat Pham 	long nr;
516ffcb5f52SNhat Pham 
517ffcb5f52SNhat Pham 	if (lru_gen_enabled()) {
518ffcb5f52SNhat Pham 		lru_gen_refault(folio, shadow);
519ffcb5f52SNhat Pham 		return;
520ffcb5f52SNhat Pham 	}
521ffcb5f52SNhat Pham 
522ffcb5f52SNhat Pham 	/* Flush stats (and potentially sleep) before holding RCU read lock */
523ffcb5f52SNhat Pham 	mem_cgroup_flush_stats_ratelimited();
524ffcb5f52SNhat Pham 
525ffcb5f52SNhat Pham 	rcu_read_lock();
526ffcb5f52SNhat Pham 
527ffcb5f52SNhat Pham 	/*
528ffcb5f52SNhat Pham 	 * The activation decision for this folio is made at the level
529ffcb5f52SNhat Pham 	 * where the eviction occurred, as that is where the LRU order
530ffcb5f52SNhat Pham 	 * during folio reclaim is being determined.
531ffcb5f52SNhat Pham 	 *
532ffcb5f52SNhat Pham 	 * However, the cgroup that will own the folio is the one that
533ffcb5f52SNhat Pham 	 * is actually experiencing the refault event.
534ffcb5f52SNhat Pham 	 */
535ffcb5f52SNhat Pham 	nr = folio_nr_pages(folio);
536ffcb5f52SNhat Pham 	memcg = folio_memcg(folio);
537ffcb5f52SNhat Pham 	pgdat = folio_pgdat(folio);
538ffcb5f52SNhat Pham 	lruvec = mem_cgroup_lruvec(memcg, pgdat);
539ffcb5f52SNhat Pham 
540ffcb5f52SNhat Pham 	mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr);
541ffcb5f52SNhat Pham 
542ffcb5f52SNhat Pham 	if (!workingset_test_recent(shadow, file, &workingset))
5431899ad18SJohannes Weiner 		goto out;
5441899ad18SJohannes Weiner 
5450995d7e5SMatthew Wilcox (Oracle) 	folio_set_active(folio);
5460995d7e5SMatthew Wilcox (Oracle) 	workingset_age_nonresident(lruvec, nr);
5470995d7e5SMatthew Wilcox (Oracle) 	mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + file, nr);
5481899ad18SJohannes Weiner 
5490995d7e5SMatthew Wilcox (Oracle) 	/* Folio was active prior to eviction */
5501899ad18SJohannes Weiner 	if (workingset) {
5510995d7e5SMatthew Wilcox (Oracle) 		folio_set_workingset(folio);
5526e1ca48dSVishal Moola (Oracle) 		/*
5536e1ca48dSVishal Moola (Oracle) 		 * XXX: Move to folio_add_lru() when it supports new vs
5546e1ca48dSVishal Moola (Oracle) 		 * putback
5556e1ca48dSVishal Moola (Oracle) 		 */
5560538a82cSJohannes Weiner 		lru_note_cost_refault(folio);
5570995d7e5SMatthew Wilcox (Oracle) 		mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file, nr);
558a528910eSJohannes Weiner 	}
5591899ad18SJohannes Weiner out:
5602a2e4885SJohannes Weiner 	rcu_read_unlock();
561a528910eSJohannes Weiner }
562a528910eSJohannes Weiner 
563a528910eSJohannes Weiner /**
564a528910eSJohannes Weiner  * workingset_activation - note a page activation
565c5ce619aSMatthew Wilcox (Oracle)  * @folio: Folio that is being activated.
566a528910eSJohannes Weiner  */
workingset_activation(struct folio * folio)567c5ce619aSMatthew Wilcox (Oracle) void workingset_activation(struct folio *folio)
568a528910eSJohannes Weiner {
56955779ec7SJohannes Weiner 	struct mem_cgroup *memcg;
57023047a96SJohannes Weiner 
57155779ec7SJohannes Weiner 	rcu_read_lock();
57223047a96SJohannes Weiner 	/*
57323047a96SJohannes Weiner 	 * Filter non-memcg pages here, e.g. unmap can call
57423047a96SJohannes Weiner 	 * mark_page_accessed() on VDSO pages.
57523047a96SJohannes Weiner 	 *
57623047a96SJohannes Weiner 	 * XXX: See workingset_refault() - this should return
57723047a96SJohannes Weiner 	 * root_mem_cgroup even for !CONFIG_MEMCG.
57823047a96SJohannes Weiner 	 */
579c5ce619aSMatthew Wilcox (Oracle) 	memcg = folio_memcg_rcu(folio);
58055779ec7SJohannes Weiner 	if (!mem_cgroup_disabled() && !memcg)
58123047a96SJohannes Weiner 		goto out;
582c5ce619aSMatthew Wilcox (Oracle) 	workingset_age_nonresident(folio_lruvec(folio), folio_nr_pages(folio));
58323047a96SJohannes Weiner out:
58455779ec7SJohannes Weiner 	rcu_read_unlock();
585a528910eSJohannes Weiner }
586449dd698SJohannes Weiner 
587449dd698SJohannes Weiner /*
588449dd698SJohannes Weiner  * Shadow entries reflect the share of the working set that does not
589449dd698SJohannes Weiner  * fit into memory, so their number depends on the access pattern of
590449dd698SJohannes Weiner  * the workload.  In most cases, they will refault or get reclaimed
591449dd698SJohannes Weiner  * along with the inode, but a (malicious) workload that streams
592449dd698SJohannes Weiner  * through files with a total size several times that of available
593449dd698SJohannes Weiner  * memory, while preventing the inodes from being reclaimed, can
594449dd698SJohannes Weiner  * create excessive amounts of shadow nodes.  To keep a lid on this,
595449dd698SJohannes Weiner  * track shadow nodes and reclaim them when they grow way past the
596449dd698SJohannes Weiner  * point where they would still be useful.
597449dd698SJohannes Weiner  */
598449dd698SJohannes Weiner 
5999bbdc0f3SMuchun Song struct list_lru shadow_nodes;
60014b46879SJohannes Weiner 
workingset_update_node(struct xa_node * node)601a97e7904SMatthew Wilcox void workingset_update_node(struct xa_node *node)
60214b46879SJohannes Weiner {
6032386eef2SSebastian Andrzej Siewior 	struct address_space *mapping;
6042386eef2SSebastian Andrzej Siewior 
60514b46879SJohannes Weiner 	/*
60614b46879SJohannes Weiner 	 * Track non-empty nodes that contain only shadow entries;
60714b46879SJohannes Weiner 	 * unlink those that contain pages or are being freed.
60814b46879SJohannes Weiner 	 *
60914b46879SJohannes Weiner 	 * Avoid acquiring the list_lru lock when the nodes are
61014b46879SJohannes Weiner 	 * already where they should be. The list_empty() test is safe
611b93b0163SMatthew Wilcox 	 * as node->private_list is protected by the i_pages lock.
61214b46879SJohannes Weiner 	 */
6132386eef2SSebastian Andrzej Siewior 	mapping = container_of(node->array, struct address_space, i_pages);
6142386eef2SSebastian Andrzej Siewior 	lockdep_assert_held(&mapping->i_pages.xa_lock);
61568d48e6aSJohannes Weiner 
61601959dfeSMatthew Wilcox 	if (node->count && node->count == node->nr_values) {
61768d48e6aSJohannes Weiner 		if (list_empty(&node->private_list)) {
61814b46879SJohannes Weiner 			list_lru_add(&shadow_nodes, &node->private_list);
619da3ceeffSMuchun Song 			__inc_lruvec_kmem_state(node, WORKINGSET_NODES);
62068d48e6aSJohannes Weiner 		}
62114b46879SJohannes Weiner 	} else {
62268d48e6aSJohannes Weiner 		if (!list_empty(&node->private_list)) {
62314b46879SJohannes Weiner 			list_lru_del(&shadow_nodes, &node->private_list);
624da3ceeffSMuchun Song 			__dec_lruvec_kmem_state(node, WORKINGSET_NODES);
62568d48e6aSJohannes Weiner 		}
62614b46879SJohannes Weiner 	}
62714b46879SJohannes Weiner }
628449dd698SJohannes Weiner 
count_shadow_nodes(struct shrinker * shrinker,struct shrink_control * sc)629449dd698SJohannes Weiner static unsigned long count_shadow_nodes(struct shrinker *shrinker,
630449dd698SJohannes Weiner 					struct shrink_control *sc)
631449dd698SJohannes Weiner {
632449dd698SJohannes Weiner 	unsigned long max_nodes;
63314b46879SJohannes Weiner 	unsigned long nodes;
63495f9ab2dSJohannes Weiner 	unsigned long pages;
635449dd698SJohannes Weiner 
63614b46879SJohannes Weiner 	nodes = list_lru_shrink_count(&shadow_nodes, sc);
637725cac1cSMiaohe Lin 	if (!nodes)
638725cac1cSMiaohe Lin 		return SHRINK_EMPTY;
639449dd698SJohannes Weiner 
640449dd698SJohannes Weiner 	/*
641a97e7904SMatthew Wilcox 	 * Approximate a reasonable limit for the nodes
642b5388998SJohannes Weiner 	 * containing shadow entries. We don't need to keep more
643b5388998SJohannes Weiner 	 * shadow entries than possible pages on the active list,
644b5388998SJohannes Weiner 	 * since refault distances bigger than that are dismissed.
645b5388998SJohannes Weiner 	 *
646b5388998SJohannes Weiner 	 * The size of the active list converges toward 100% of
647b5388998SJohannes Weiner 	 * overall page cache as memory grows, with only a tiny
648b5388998SJohannes Weiner 	 * inactive list. Assume the total cache size for that.
649b5388998SJohannes Weiner 	 *
650b5388998SJohannes Weiner 	 * Nodes might be sparsely populated, with only one shadow
651b5388998SJohannes Weiner 	 * entry in the extreme case. Obviously, we cannot keep one
652b5388998SJohannes Weiner 	 * node for every eligible shadow entry, so compromise on a
653b5388998SJohannes Weiner 	 * worst-case density of 1/8th. Below that, not all eligible
654b5388998SJohannes Weiner 	 * refaults can be detected anymore.
655449dd698SJohannes Weiner 	 *
656a97e7904SMatthew Wilcox 	 * On 64-bit with 7 xa_nodes per page and 64 slots
657449dd698SJohannes Weiner 	 * each, this will reclaim shadow entries when they consume
658b5388998SJohannes Weiner 	 * ~1.8% of available memory:
659449dd698SJohannes Weiner 	 *
660a97e7904SMatthew Wilcox 	 * PAGE_SIZE / xa_nodes / node_entries * 8 / PAGE_SIZE
661449dd698SJohannes Weiner 	 */
66295f9ab2dSJohannes Weiner #ifdef CONFIG_MEMCG
663b5388998SJohannes Weiner 	if (sc->memcg) {
66495f9ab2dSJohannes Weiner 		struct lruvec *lruvec;
6652b487e59SJohannes Weiner 		int i;
66695f9ab2dSJohannes Weiner 
667*417dbd7bSShakeel Butt 		mem_cgroup_flush_stats_ratelimited();
668867e5e1dSJohannes Weiner 		lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid));
6692b487e59SJohannes Weiner 		for (pages = 0, i = 0; i < NR_LRU_LISTS; i++)
670205b20ccSJohannes Weiner 			pages += lruvec_page_state_local(lruvec,
671205b20ccSJohannes Weiner 							 NR_LRU_BASE + i);
672d42f3245SRoman Gushchin 		pages += lruvec_page_state_local(
673d42f3245SRoman Gushchin 			lruvec, NR_SLAB_RECLAIMABLE_B) >> PAGE_SHIFT;
674d42f3245SRoman Gushchin 		pages += lruvec_page_state_local(
675d42f3245SRoman Gushchin 			lruvec, NR_SLAB_UNRECLAIMABLE_B) >> PAGE_SHIFT;
67695f9ab2dSJohannes Weiner 	} else
67795f9ab2dSJohannes Weiner #endif
67895f9ab2dSJohannes Weiner 		pages = node_present_pages(sc->nid);
67995f9ab2dSJohannes Weiner 
680dad4f140SLinus Torvalds 	max_nodes = pages >> (XA_CHUNK_SHIFT - 3);
681449dd698SJohannes Weiner 
68214b46879SJohannes Weiner 	if (nodes <= max_nodes)
683449dd698SJohannes Weiner 		return 0;
68414b46879SJohannes Weiner 	return nodes - max_nodes;
685449dd698SJohannes Weiner }
686449dd698SJohannes Weiner 
shadow_lru_isolate(struct list_head * item,struct list_lru_one * lru,spinlock_t * lru_lock,void * arg)687449dd698SJohannes Weiner static enum lru_status shadow_lru_isolate(struct list_head *item,
6883f97b163SVladimir Davydov 					  struct list_lru_one *lru,
689449dd698SJohannes Weiner 					  spinlock_t *lru_lock,
690a97e7904SMatthew Wilcox 					  void *arg) __must_hold(lru_lock)
691449dd698SJohannes Weiner {
692a97e7904SMatthew Wilcox 	struct xa_node *node = container_of(item, struct xa_node, private_list);
693449dd698SJohannes Weiner 	struct address_space *mapping;
694449dd698SJohannes Weiner 	int ret;
695449dd698SJohannes Weiner 
696449dd698SJohannes Weiner 	/*
697f82cd2f0SMatthew Wilcox (Oracle) 	 * Page cache insertions and deletions synchronously maintain
698b93b0163SMatthew Wilcox 	 * the shadow node LRU under the i_pages lock and the
699449dd698SJohannes Weiner 	 * lru_lock.  Because the page cache tree is emptied before
700449dd698SJohannes Weiner 	 * the inode can be destroyed, holding the lru_lock pins any
701a97e7904SMatthew Wilcox 	 * address_space that has nodes on the LRU.
702449dd698SJohannes Weiner 	 *
703b93b0163SMatthew Wilcox 	 * We can then safely transition to the i_pages lock to
704449dd698SJohannes Weiner 	 * pin only the address_space of the particular node we want
705449dd698SJohannes Weiner 	 * to reclaim, take the node off-LRU, and drop the lru_lock.
706449dd698SJohannes Weiner 	 */
707449dd698SJohannes Weiner 
70801959dfeSMatthew Wilcox 	mapping = container_of(node->array, struct address_space, i_pages);
709449dd698SJohannes Weiner 
710449dd698SJohannes Weiner 	/* Coming from the list, invert the lock order */
711b93b0163SMatthew Wilcox 	if (!xa_trylock(&mapping->i_pages)) {
7126ca342d0SSebastian Andrzej Siewior 		spin_unlock_irq(lru_lock);
713449dd698SJohannes Weiner 		ret = LRU_RETRY;
714449dd698SJohannes Weiner 		goto out;
715449dd698SJohannes Weiner 	}
716449dd698SJohannes Weiner 
7175649d113SYang Yang 	/* For page cache we need to hold i_lock */
7185649d113SYang Yang 	if (mapping->host != NULL) {
71951b8c1feSJohannes Weiner 		if (!spin_trylock(&mapping->host->i_lock)) {
72051b8c1feSJohannes Weiner 			xa_unlock(&mapping->i_pages);
72151b8c1feSJohannes Weiner 			spin_unlock_irq(lru_lock);
72251b8c1feSJohannes Weiner 			ret = LRU_RETRY;
72351b8c1feSJohannes Weiner 			goto out;
72451b8c1feSJohannes Weiner 		}
7255649d113SYang Yang 	}
72651b8c1feSJohannes Weiner 
7273f97b163SVladimir Davydov 	list_lru_isolate(lru, item);
728da3ceeffSMuchun Song 	__dec_lruvec_kmem_state(node, WORKINGSET_NODES);
72968d48e6aSJohannes Weiner 
730449dd698SJohannes Weiner 	spin_unlock(lru_lock);
731449dd698SJohannes Weiner 
732449dd698SJohannes Weiner 	/*
733449dd698SJohannes Weiner 	 * The nodes should only contain one or more shadow entries,
734449dd698SJohannes Weiner 	 * no pages, so we expect to be able to remove them all and
735449dd698SJohannes Weiner 	 * delete and free the empty node afterwards.
736449dd698SJohannes Weiner 	 */
73701959dfeSMatthew Wilcox 	if (WARN_ON_ONCE(!node->nr_values))
738b936887eSJohannes Weiner 		goto out_invalid;
73901959dfeSMatthew Wilcox 	if (WARN_ON_ONCE(node->count != node->nr_values))
740b936887eSJohannes Weiner 		goto out_invalid;
741f82cd2f0SMatthew Wilcox (Oracle) 	xa_delete_node(node, workingset_update_node);
742da3ceeffSMuchun Song 	__inc_lruvec_kmem_state(node, WORKINGSET_NODERECLAIM);
743449dd698SJohannes Weiner 
744b936887eSJohannes Weiner out_invalid:
7456ca342d0SSebastian Andrzej Siewior 	xa_unlock_irq(&mapping->i_pages);
7465649d113SYang Yang 	if (mapping->host != NULL) {
74751b8c1feSJohannes Weiner 		if (mapping_shrinkable(mapping))
74851b8c1feSJohannes Weiner 			inode_add_lru(mapping->host);
74951b8c1feSJohannes Weiner 		spin_unlock(&mapping->host->i_lock);
7505649d113SYang Yang 	}
751449dd698SJohannes Weiner 	ret = LRU_REMOVED_RETRY;
752449dd698SJohannes Weiner out:
753449dd698SJohannes Weiner 	cond_resched();
7546ca342d0SSebastian Andrzej Siewior 	spin_lock_irq(lru_lock);
755449dd698SJohannes Weiner 	return ret;
756449dd698SJohannes Weiner }
757449dd698SJohannes Weiner 
scan_shadow_nodes(struct shrinker * shrinker,struct shrink_control * sc)758449dd698SJohannes Weiner static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
759449dd698SJohannes Weiner 				       struct shrink_control *sc)
760449dd698SJohannes Weiner {
761b93b0163SMatthew Wilcox 	/* list_lru lock nests inside the IRQ-safe i_pages lock */
7626b51e881SSebastian Andrzej Siewior 	return list_lru_shrink_walk_irq(&shadow_nodes, sc, shadow_lru_isolate,
7636b51e881SSebastian Andrzej Siewior 					NULL);
764449dd698SJohannes Weiner }
765449dd698SJohannes Weiner 
766449dd698SJohannes Weiner static struct shrinker workingset_shadow_shrinker = {
767449dd698SJohannes Weiner 	.count_objects = count_shadow_nodes,
768449dd698SJohannes Weiner 	.scan_objects = scan_shadow_nodes,
7694b85afbdSJohannes Weiner 	.seeks = 0, /* ->count reports only fully expendable nodes */
7700a6b76ddSVladimir Davydov 	.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
771449dd698SJohannes Weiner };
772449dd698SJohannes Weiner 
773449dd698SJohannes Weiner /*
774449dd698SJohannes Weiner  * Our list_lru->lock is IRQ-safe as it nests inside the IRQ-safe
775b93b0163SMatthew Wilcox  * i_pages lock.
776449dd698SJohannes Weiner  */
777449dd698SJohannes Weiner static struct lock_class_key shadow_nodes_key;
778449dd698SJohannes Weiner 
workingset_init(void)779449dd698SJohannes Weiner static int __init workingset_init(void)
780449dd698SJohannes Weiner {
781612e4493SJohannes Weiner 	unsigned int timestamp_bits;
782612e4493SJohannes Weiner 	unsigned int max_order;
783449dd698SJohannes Weiner 	int ret;
784449dd698SJohannes Weiner 
785612e4493SJohannes Weiner 	BUILD_BUG_ON(BITS_PER_LONG < EVICTION_SHIFT);
786612e4493SJohannes Weiner 	/*
787612e4493SJohannes Weiner 	 * Calculate the eviction bucket size to cover the longest
788612e4493SJohannes Weiner 	 * actionable refault distance, which is currently half of
789612e4493SJohannes Weiner 	 * memory (totalram_pages/2). However, memory hotplug may add
790612e4493SJohannes Weiner 	 * some more pages at runtime, so keep working with up to
791612e4493SJohannes Weiner 	 * double the initial memory by using totalram_pages as-is.
792612e4493SJohannes Weiner 	 */
793612e4493SJohannes Weiner 	timestamp_bits = BITS_PER_LONG - EVICTION_SHIFT;
794ca79b0c2SArun KS 	max_order = fls_long(totalram_pages() - 1);
795612e4493SJohannes Weiner 	if (max_order > timestamp_bits)
796612e4493SJohannes Weiner 		bucket_order = max_order - timestamp_bits;
797d3d36c4bSAnton Blanchard 	pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u\n",
798612e4493SJohannes Weiner 	       timestamp_bits, max_order, bucket_order);
799612e4493SJohannes Weiner 
800e33c267aSRoman Gushchin 	ret = prealloc_shrinker(&workingset_shadow_shrinker, "mm-shadow");
801449dd698SJohannes Weiner 	if (ret)
802449dd698SJohannes Weiner 		goto err;
803c92e8e10SKirill Tkhai 	ret = __list_lru_init(&shadow_nodes, true, &shadow_nodes_key,
804c92e8e10SKirill Tkhai 			      &workingset_shadow_shrinker);
805449dd698SJohannes Weiner 	if (ret)
806449dd698SJohannes Weiner 		goto err_list_lru;
80739887653SKirill Tkhai 	register_shrinker_prepared(&workingset_shadow_shrinker);
808449dd698SJohannes Weiner 	return 0;
809449dd698SJohannes Weiner err_list_lru:
81039887653SKirill Tkhai 	free_prealloced_shrinker(&workingset_shadow_shrinker);
811449dd698SJohannes Weiner err:
812449dd698SJohannes Weiner 	return ret;
813449dd698SJohannes Weiner }
814449dd698SJohannes Weiner module_init(workingset_init);
815