Blame view

mm/workingset.c 21.4 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
a528910e1   Johannes Weiner   mm: thrash detect...
2
3
4
5
6
7
8
  /*
   * Workingset detection
   *
   * Copyright (C) 2013 Red Hat, Inc., Johannes Weiner
   */
  
  #include <linux/memcontrol.h>
170b04b7a   Joonsoo Kim   mm/workingset: pr...
9
  #include <linux/mm_inline.h>
a528910e1   Johannes Weiner   mm: thrash detect...
10
  #include <linux/writeback.h>
3a4f8a0b3   Hugh Dickins   mm: remove shmem_...
11
  #include <linux/shmem_fs.h>
a528910e1   Johannes Weiner   mm: thrash detect...
12
13
14
15
  #include <linux/pagemap.h>
  #include <linux/atomic.h>
  #include <linux/module.h>
  #include <linux/swap.h>
14b468791   Johannes Weiner   mm: workingset: m...
16
  #include <linux/dax.h>
a528910e1   Johannes Weiner   mm: thrash detect...
17
18
19
20
21
22
  #include <linux/fs.h>
  #include <linux/mm.h>
  
  /*
   *		Double CLOCK lists
   *
1e6b10857   Mel Gorman   mm, workingset: m...
23
   * Per node, two clock lists are maintained for file pages: the
a528910e1   Johannes Weiner   mm: thrash detect...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
   * inactive and the active list.  Freshly faulted pages start out at
   * the head of the inactive list and page reclaim scans pages from the
   * tail.  Pages that are accessed multiple times on the inactive list
   * are promoted to the active list, to protect them from reclaim,
   * whereas active pages are demoted to the inactive list when the
   * active list grows too big.
   *
   *   fault ------------------------+
   *                                 |
   *              +--------------+   |            +-------------+
   *   reclaim <- |   inactive   | <-+-- demotion |    active   | <--+
   *              +--------------+                +-------------+    |
   *                     |                                           |
   *                     +-------------- promotion ------------------+
   *
   *
   *		Access frequency and refault distance
   *
   * A workload is thrashing when its pages are frequently used but they
   * are evicted from the inactive list every time before another access
   * would have promoted them to the active list.
   *
   * In cases where the average access distance between thrashing pages
   * is bigger than the size of memory there is nothing that can be
   * done - the thrashing set could never fit into memory under any
   * circumstance.
   *
   * However, the average access distance could be bigger than the
   * inactive list, yet smaller than the size of memory.  In this case,
   * the set could fit into memory if it weren't for the currently
   * active pages - which may be used more, hopefully less frequently:
   *
   *      +-memory available to cache-+
   *      |                           |
   *      +-inactive------+-active----+
   *  a b | c d e f g h i | J K L M N |
   *      +---------------+-----------+
   *
   * It is prohibitively expensive to accurately track access frequency
   * of pages.  But a reasonable approximation can be made to measure
   * thrashing on the inactive list, after which refaulting pages can be
   * activated optimistically to compete with the existing active pages.
   *
   * Approximating inactive page access frequency - Observations:
   *
   * 1. When a page is accessed for the first time, it is added to the
   *    head of the inactive list, slides every existing inactive page
   *    towards the tail by one slot, and pushes the current tail page
   *    out of memory.
   *
   * 2. When a page is accessed for the second time, it is promoted to
   *    the active list, shrinking the inactive list by one slot.  This
   *    also slides all inactive pages that were faulted into the cache
   *    more recently than the activated page towards the tail of the
   *    inactive list.
   *
   * Thus:
   *
   * 1. The sum of evictions and activations between any two points in
   *    time indicate the minimum number of inactive pages accessed in
   *    between.
   *
   * 2. Moving one inactive page N page slots towards the tail of the
   *    list requires at least N inactive page accesses.
   *
   * Combining these:
   *
   * 1. When a page is finally evicted from memory, the number of
   *    inactive pages accessed while the page was in cache is at least
   *    the number of page slots on the inactive list.
   *
   * 2. In addition, measuring the sum of evictions and activations (E)
   *    at the time of a page's eviction, and comparing it to another
   *    reading (R) at the time the page faults back into memory tells
   *    the minimum number of accesses while the page was not cached.
   *    This is called the refault distance.
   *
   * Because the first access of the page was the fault and the second
   * access the refault, we combine the in-cache distance with the
   * out-of-cache distance to get the complete minimum access distance
   * of this page:
   *
   *      NR_inactive + (R - E)
   *
   * And knowing the minimum access distance of a page, we can easily
   * tell if the page would be able to stay in cache assuming all page
   * slots in the cache were available:
   *
   *   NR_inactive + (R - E) <= NR_inactive + NR_active
   *
   * which can be further simplified to
   *
   *   (R - E) <= NR_active
   *
   * Put into words, the refault distance (out-of-cache) can be seen as
   * a deficit in inactive list space (in-cache).  If the inactive list
   * had (R - E) more page slots, the page would not have been evicted
   * in between accesses, but activated instead.  And on a full system,
   * the only thing eating into inactive list space is active pages.
   *
   *
1899ad18c   Johannes Weiner   mm: workingset: t...
125
   *		Refaulting inactive pages
a528910e1   Johannes Weiner   mm: thrash detect...
126
127
128
129
130
131
132
133
134
135
136
137
   *
   * All that is known about the active list is that the pages have been
   * accessed more than once in the past.  This means that at any given
   * time there is actually a good chance that pages on the active list
   * are no longer in active use.
   *
   * So when a refault distance of (R - E) is observed and there are at
   * least (R - E) active pages, the refaulting page is activated
   * optimistically in the hope that (R - E) active pages are actually
   * used less frequently than the refaulting page - or even not used at
   * all anymore.
   *
1899ad18c   Johannes Weiner   mm: workingset: t...
138
139
140
141
   * That means if inactive cache is refaulting with a suitable refault
   * distance, we assume the cache workingset is transitioning and put
   * pressure on the current active list.
   *
a528910e1   Johannes Weiner   mm: thrash detect...
142
143
144
145
146
147
148
   * If this is wrong and demotion kicks in, the pages which are truly
   * used more frequently will be reactivated while the less frequently
   * used once will be evicted from memory.
   *
   * But if this is right, the stale pages will be pushed out of memory
   * and the used pages get to stay in cache.
   *
1899ad18c   Johannes Weiner   mm: workingset: t...
149
150
151
152
153
154
155
156
   *		Refaulting active pages
   *
   * If on the other hand the refaulting pages have recently been
   * deactivated, it means that the active list is no longer protecting
   * actively used cache from reclaim. The cache is NOT transitioning to
   * a different workingset; the existing workingset is thrashing in the
   * space allocated to the page cache.
   *
a528910e1   Johannes Weiner   mm: thrash detect...
157
158
159
   *
   *		Implementation
   *
31d8fcac0   Johannes Weiner   mm: workingset: a...
160
161
   * For each node's LRU lists, a counter for inactive evictions and
   * activations is maintained (node->nonresident_age).
a528910e1   Johannes Weiner   mm: thrash detect...
162
163
   *
   * On eviction, a snapshot of this counter (along with some bits to
a97e7904c   Matthew Wilcox   mm: Convert worki...
164
   * identify the node) is stored in the now empty page cache
a528910e1   Johannes Weiner   mm: thrash detect...
165
166
167
168
169
   * slot of the evicted page.  This is called a shadow entry.
   *
   * On cache misses for which there are shadow entries, an eligible
   * refault distance will immediately activate the refaulting page.
   */
3159f943a   Matthew Wilcox   xarray: Replace e...
170
  #define EVICTION_SHIFT	((BITS_PER_LONG - BITS_PER_XA_VALUE) +	\
1899ad18c   Johannes Weiner   mm: workingset: t...
171
  			 1 + NODES_SHIFT + MEM_CGROUP_ID_SHIFT)
689c94f03   Johannes Weiner   mm: workingset: #...
172
  #define EVICTION_MASK	(~0UL >> EVICTION_SHIFT)
612e44939   Johannes Weiner   mm: workingset: e...
173
174
  /*
   * Eviction timestamps need to be able to cover the full range of
a97e7904c   Matthew Wilcox   mm: Convert worki...
175
   * actionable refaults. However, bits are tight in the xarray
612e44939   Johannes Weiner   mm: workingset: e...
176
177
178
179
180
181
   * entry, and after storing the identifier for the lruvec there might
   * not be enough left to represent every single actionable refault. In
   * that case, we have to sacrifice granularity for distance, and group
   * evictions into coarser buckets by shaving off lower timestamp bits.
   */
  static unsigned int bucket_order __read_mostly;
1899ad18c   Johannes Weiner   mm: workingset: t...
182
183
  static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction,
  			 bool workingset)
a528910e1   Johannes Weiner   mm: thrash detect...
184
  {
612e44939   Johannes Weiner   mm: workingset: e...
185
  	eviction >>= bucket_order;
3159f943a   Matthew Wilcox   xarray: Replace e...
186
  	eviction &= EVICTION_MASK;
23047a96d   Johannes Weiner   mm: workingset: p...
187
  	eviction = (eviction << MEM_CGROUP_ID_SHIFT) | memcgid;
1e6b10857   Mel Gorman   mm, workingset: m...
188
  	eviction = (eviction << NODES_SHIFT) | pgdat->node_id;
1899ad18c   Johannes Weiner   mm: workingset: t...
189
  	eviction = (eviction << 1) | workingset;
a528910e1   Johannes Weiner   mm: thrash detect...
190

3159f943a   Matthew Wilcox   xarray: Replace e...
191
  	return xa_mk_value(eviction);
a528910e1   Johannes Weiner   mm: thrash detect...
192
  }
1e6b10857   Mel Gorman   mm, workingset: m...
193
  static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
1899ad18c   Johannes Weiner   mm: workingset: t...
194
  			  unsigned long *evictionp, bool *workingsetp)
a528910e1   Johannes Weiner   mm: thrash detect...
195
  {
3159f943a   Matthew Wilcox   xarray: Replace e...
196
  	unsigned long entry = xa_to_value(shadow);
1e6b10857   Mel Gorman   mm, workingset: m...
197
  	int memcgid, nid;
1899ad18c   Johannes Weiner   mm: workingset: t...
198
  	bool workingset;
a528910e1   Johannes Weiner   mm: thrash detect...
199

1899ad18c   Johannes Weiner   mm: workingset: t...
200
201
  	workingset = entry & 1;
  	entry >>= 1;
a528910e1   Johannes Weiner   mm: thrash detect...
202
203
  	nid = entry & ((1UL << NODES_SHIFT) - 1);
  	entry >>= NODES_SHIFT;
23047a96d   Johannes Weiner   mm: workingset: p...
204
205
  	memcgid = entry & ((1UL << MEM_CGROUP_ID_SHIFT) - 1);
  	entry >>= MEM_CGROUP_ID_SHIFT;
a528910e1   Johannes Weiner   mm: thrash detect...
206

23047a96d   Johannes Weiner   mm: workingset: p...
207
  	*memcgidp = memcgid;
1e6b10857   Mel Gorman   mm, workingset: m...
208
  	*pgdat = NODE_DATA(nid);
612e44939   Johannes Weiner   mm: workingset: e...
209
  	*evictionp = entry << bucket_order;
1899ad18c   Johannes Weiner   mm: workingset: t...
210
  	*workingsetp = workingset;
a528910e1   Johannes Weiner   mm: thrash detect...
211
  }
31d8fcac0   Johannes Weiner   mm: workingset: a...
212
213
  /**
   * workingset_age_nonresident - age non-resident entries as LRU ages
e755f4af0   Xiaofei Tan   mm/workingset.c: ...
214
   * @lruvec: the lruvec that was aged
31d8fcac0   Johannes Weiner   mm: workingset: a...
215
216
217
218
219
220
221
222
   * @nr_pages: the number of pages to count
   *
   * As in-memory pages are aged, non-resident pages need to be aged as
   * well, in order for the refault distances later on to be comparable
   * to the in-memory dimensions. This function allows reclaim and LRU
   * operations to drive the non-resident aging along in parallel.
   */
  void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages)
b910718a9   Johannes Weiner   mm: vmscan: detec...
223
224
225
226
227
228
229
230
231
232
233
234
235
  {
  	/*
  	 * Reclaiming a cgroup means reclaiming all its children in a
  	 * round-robin fashion. That means that each cgroup has an LRU
  	 * order that is composed of the LRU orders of its child
  	 * cgroups; and every page has an LRU position not just in the
  	 * cgroup that owns it, but in all of that group's ancestors.
  	 *
  	 * So when the physical inactive list of a leaf cgroup ages,
  	 * the virtual inactive lists of all its parents, including
  	 * the root cgroup's, age as well.
  	 */
  	do {
31d8fcac0   Johannes Weiner   mm: workingset: a...
236
237
  		atomic_long_add(nr_pages, &lruvec->nonresident_age);
  	} while ((lruvec = parent_lruvec(lruvec)));
b910718a9   Johannes Weiner   mm: vmscan: detec...
238
  }
a528910e1   Johannes Weiner   mm: thrash detect...
239
240
  /**
   * workingset_eviction - note the eviction of a page from memory
b910718a9   Johannes Weiner   mm: vmscan: detec...
241
   * @target_memcg: the cgroup that is causing the reclaim
a528910e1   Johannes Weiner   mm: thrash detect...
242
243
   * @page: the page being evicted
   *
a7ca12f9d   Andrey Ryabinin   mm/workingset: re...
244
   * Returns a shadow entry to be stored in @page->mapping->i_pages in place
a528910e1   Johannes Weiner   mm: thrash detect...
245
246
   * of the evicted @page so that a later refault can be detected.
   */
b910718a9   Johannes Weiner   mm: vmscan: detec...
247
  void *workingset_eviction(struct page *page, struct mem_cgroup *target_memcg)
a528910e1   Johannes Weiner   mm: thrash detect...
248
  {
1e6b10857   Mel Gorman   mm, workingset: m...
249
  	struct pglist_data *pgdat = page_pgdat(page);
a528910e1   Johannes Weiner   mm: thrash detect...
250
  	unsigned long eviction;
23047a96d   Johannes Weiner   mm: workingset: p...
251
  	struct lruvec *lruvec;
b910718a9   Johannes Weiner   mm: vmscan: detec...
252
  	int memcgid;
a528910e1   Johannes Weiner   mm: thrash detect...
253

23047a96d   Johannes Weiner   mm: workingset: p...
254
255
256
257
  	/* Page is fully exclusive and pins page->mem_cgroup */
  	VM_BUG_ON_PAGE(PageLRU(page), page);
  	VM_BUG_ON_PAGE(page_count(page), page);
  	VM_BUG_ON_PAGE(!PageLocked(page), page);
b910718a9   Johannes Weiner   mm: vmscan: detec...
258
  	lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
6c357848b   Matthew Wilcox (Oracle)   mm: replace hpage...
259
  	workingset_age_nonresident(lruvec, thp_nr_pages(page));
b910718a9   Johannes Weiner   mm: vmscan: detec...
260
261
  	/* XXX: target_memcg can be NULL, go through lruvec */
  	memcgid = mem_cgroup_id(lruvec_memcg(lruvec));
31d8fcac0   Johannes Weiner   mm: workingset: a...
262
  	eviction = atomic_long_read(&lruvec->nonresident_age);
1899ad18c   Johannes Weiner   mm: workingset: t...
263
  	return pack_shadow(memcgid, pgdat, eviction, PageWorkingset(page));
a528910e1   Johannes Weiner   mm: thrash detect...
264
265
266
267
  }
  
  /**
   * workingset_refault - evaluate the refault of a previously evicted page
1899ad18c   Johannes Weiner   mm: workingset: t...
268
   * @page: the freshly allocated replacement page
a528910e1   Johannes Weiner   mm: thrash detect...
269
270
271
   * @shadow: shadow entry of the evicted page
   *
   * Calculates and evaluates the refault distance of the previously
b910718a9   Johannes Weiner   mm: vmscan: detec...
272
273
   * evicted page in the context of the node and the memcg whose memory
   * pressure caused the eviction.
a528910e1   Johannes Weiner   mm: thrash detect...
274
   */
1899ad18c   Johannes Weiner   mm: workingset: t...
275
  void workingset_refault(struct page *page, void *shadow)
a528910e1   Johannes Weiner   mm: thrash detect...
276
  {
170b04b7a   Joonsoo Kim   mm/workingset: pr...
277
  	bool file = page_is_file_lru(page);
b910718a9   Johannes Weiner   mm: vmscan: detec...
278
279
  	struct mem_cgroup *eviction_memcg;
  	struct lruvec *eviction_lruvec;
a528910e1   Johannes Weiner   mm: thrash detect...
280
  	unsigned long refault_distance;
34e58cac6   Johannes Weiner   mm: workingset: l...
281
  	unsigned long workingset_size;
1899ad18c   Johannes Weiner   mm: workingset: t...
282
  	struct pglist_data *pgdat;
23047a96d   Johannes Weiner   mm: workingset: p...
283
  	struct mem_cgroup *memcg;
162453bfb   Johannes Weiner   mm: workingset: s...
284
  	unsigned long eviction;
23047a96d   Johannes Weiner   mm: workingset: p...
285
  	struct lruvec *lruvec;
162453bfb   Johannes Weiner   mm: workingset: s...
286
  	unsigned long refault;
1899ad18c   Johannes Weiner   mm: workingset: t...
287
  	bool workingset;
23047a96d   Johannes Weiner   mm: workingset: p...
288
  	int memcgid;
a528910e1   Johannes Weiner   mm: thrash detect...
289

1899ad18c   Johannes Weiner   mm: workingset: t...
290
  	unpack_shadow(shadow, &memcgid, &pgdat, &eviction, &workingset);
162453bfb   Johannes Weiner   mm: workingset: s...
291

23047a96d   Johannes Weiner   mm: workingset: p...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  	rcu_read_lock();
  	/*
  	 * Look up the memcg associated with the stored ID. It might
  	 * have been deleted since the page's eviction.
  	 *
  	 * Note that in rare events the ID could have been recycled
  	 * for a new cgroup that refaults a shared page. This is
  	 * impossible to tell from the available data. However, this
  	 * should be a rare and limited disturbance, and activations
  	 * are always speculative anyway. Ultimately, it's the aging
  	 * algorithm's job to shake out the minimum access frequency
  	 * for the active cache.
  	 *
  	 * XXX: On !CONFIG_MEMCG, this will always return NULL; it
  	 * would be better if the root_mem_cgroup existed in all
  	 * configurations instead.
  	 */
b910718a9   Johannes Weiner   mm: vmscan: detec...
309
310
  	eviction_memcg = mem_cgroup_from_id(memcgid);
  	if (!mem_cgroup_disabled() && !eviction_memcg)
1899ad18c   Johannes Weiner   mm: workingset: t...
311
  		goto out;
b910718a9   Johannes Weiner   mm: vmscan: detec...
312
  	eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat);
31d8fcac0   Johannes Weiner   mm: workingset: a...
313
  	refault = atomic_long_read(&eviction_lruvec->nonresident_age);
162453bfb   Johannes Weiner   mm: workingset: s...
314
315
  
  	/*
1899ad18c   Johannes Weiner   mm: workingset: t...
316
  	 * Calculate the refault distance
162453bfb   Johannes Weiner   mm: workingset: s...
317
  	 *
1899ad18c   Johannes Weiner   mm: workingset: t...
318
  	 * The unsigned subtraction here gives an accurate distance
31d8fcac0   Johannes Weiner   mm: workingset: a...
319
  	 * across nonresident_age overflows in most cases. There is a
1899ad18c   Johannes Weiner   mm: workingset: t...
320
321
322
  	 * special case: usually, shadow entries have a short lifetime
  	 * and are either refaulted or reclaimed along with the inode
  	 * before they get too old.  But it is not impossible for the
31d8fcac0   Johannes Weiner   mm: workingset: a...
323
324
325
326
  	 * nonresident_age to lap a shadow entry in the field, which
  	 * can then result in a false small refault distance, leading
  	 * to a false activation should this old entry actually
  	 * refault again.  However, earlier kernels used to deactivate
1899ad18c   Johannes Weiner   mm: workingset: t...
327
328
329
  	 * unconditionally with *every* reclaim invocation for the
  	 * longest time, so the occasional inappropriate activation
  	 * leading to pressure on the active list is not a problem.
162453bfb   Johannes Weiner   mm: workingset: s...
330
331
  	 */
  	refault_distance = (refault - eviction) & EVICTION_MASK;
b910718a9   Johannes Weiner   mm: vmscan: detec...
332
333
334
335
336
337
338
339
340
341
  	/*
  	 * The activation decision for this page is made at the level
  	 * where the eviction occurred, as that is where the LRU order
  	 * during page reclaim is being determined.
  	 *
  	 * However, the cgroup that will own the page is the one that
  	 * is actually experiencing the refault event.
  	 */
  	memcg = page_memcg(page);
  	lruvec = mem_cgroup_lruvec(memcg, pgdat);
170b04b7a   Joonsoo Kim   mm/workingset: pr...
342
  	inc_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file);
a528910e1   Johannes Weiner   mm: thrash detect...
343

1899ad18c   Johannes Weiner   mm: workingset: t...
344
345
  	/*
  	 * Compare the distance to the existing workingset size. We
34e58cac6   Johannes Weiner   mm: workingset: l...
346
  	 * don't activate pages that couldn't stay resident even if
aae466b00   Joonsoo Kim   mm/swap: implemen...
347
348
349
  	 * all the memory was available to the workingset. Whether
  	 * workingset competition needs to consider anon or not depends
  	 * on having swap.
1899ad18c   Johannes Weiner   mm: workingset: t...
350
  	 */
34e58cac6   Johannes Weiner   mm: workingset: l...
351
  	workingset_size = lruvec_page_state(eviction_lruvec, NR_ACTIVE_FILE);
aae466b00   Joonsoo Kim   mm/swap: implemen...
352
  	if (!file) {
34e58cac6   Johannes Weiner   mm: workingset: l...
353
  		workingset_size += lruvec_page_state(eviction_lruvec,
aae466b00   Joonsoo Kim   mm/swap: implemen...
354
355
356
  						     NR_INACTIVE_FILE);
  	}
  	if (mem_cgroup_get_nr_swap_pages(memcg) > 0) {
34e58cac6   Johannes Weiner   mm: workingset: l...
357
358
  		workingset_size += lruvec_page_state(eviction_lruvec,
  						     NR_ACTIVE_ANON);
aae466b00   Joonsoo Kim   mm/swap: implemen...
359
360
361
362
  		if (file) {
  			workingset_size += lruvec_page_state(eviction_lruvec,
  						     NR_INACTIVE_ANON);
  		}
34e58cac6   Johannes Weiner   mm: workingset: l...
363
364
  	}
  	if (refault_distance > workingset_size)
1899ad18c   Johannes Weiner   mm: workingset: t...
365
366
367
  		goto out;
  
  	SetPageActive(page);
6c357848b   Matthew Wilcox (Oracle)   mm: replace hpage...
368
  	workingset_age_nonresident(lruvec, thp_nr_pages(page));
170b04b7a   Joonsoo Kim   mm/workingset: pr...
369
  	inc_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + file);
1899ad18c   Johannes Weiner   mm: workingset: t...
370
371
372
373
  
  	/* Page was active prior to eviction */
  	if (workingset) {
  		SetPageWorkingset(page);
314b57fb0   Johannes Weiner   mm: balance LRU l...
374
375
  		/* XXX: Move to lru_cache_add() when it supports new vs putback */
  		spin_lock_irq(&page_pgdat(page)->lru_lock);
96f8bf4fb   Johannes Weiner   mm: vmscan: recla...
376
  		lru_note_cost_page(page);
314b57fb0   Johannes Weiner   mm: balance LRU l...
377
  		spin_unlock_irq(&page_pgdat(page)->lru_lock);
170b04b7a   Joonsoo Kim   mm/workingset: pr...
378
  		inc_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file);
a528910e1   Johannes Weiner   mm: thrash detect...
379
  	}
1899ad18c   Johannes Weiner   mm: workingset: t...
380
  out:
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
381
  	rcu_read_unlock();
a528910e1   Johannes Weiner   mm: thrash detect...
382
383
384
385
386
387
388
389
  }
  
  /**
   * workingset_activation - note a page activation
   * @page: page that is being activated
   */
  void workingset_activation(struct page *page)
  {
55779ec75   Johannes Weiner   mm: fix vm-scalab...
390
  	struct mem_cgroup *memcg;
31d8fcac0   Johannes Weiner   mm: workingset: a...
391
  	struct lruvec *lruvec;
23047a96d   Johannes Weiner   mm: workingset: p...
392

55779ec75   Johannes Weiner   mm: fix vm-scalab...
393
  	rcu_read_lock();
23047a96d   Johannes Weiner   mm: workingset: p...
394
395
396
397
398
399
400
  	/*
  	 * Filter non-memcg pages here, e.g. unmap can call
  	 * mark_page_accessed() on VDSO pages.
  	 *
  	 * XXX: See workingset_refault() - this should return
  	 * root_mem_cgroup even for !CONFIG_MEMCG.
  	 */
55779ec75   Johannes Weiner   mm: fix vm-scalab...
401
402
  	memcg = page_memcg_rcu(page);
  	if (!mem_cgroup_disabled() && !memcg)
23047a96d   Johannes Weiner   mm: workingset: p...
403
  		goto out;
31d8fcac0   Johannes Weiner   mm: workingset: a...
404
  	lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
6c357848b   Matthew Wilcox (Oracle)   mm: replace hpage...
405
  	workingset_age_nonresident(lruvec, thp_nr_pages(page));
23047a96d   Johannes Weiner   mm: workingset: p...
406
  out:
55779ec75   Johannes Weiner   mm: fix vm-scalab...
407
  	rcu_read_unlock();
a528910e1   Johannes Weiner   mm: thrash detect...
408
  }
449dd6984   Johannes Weiner   mm: keep page cac...
409
410
411
412
413
414
415
416
417
418
419
420
  
  /*
   * Shadow entries reflect the share of the working set that does not
   * fit into memory, so their number depends on the access pattern of
   * the workload.  In most cases, they will refault or get reclaimed
   * along with the inode, but a (malicious) workload that streams
   * through files with a total size several times that of available
   * memory, while preventing the inodes from being reclaimed, can
   * create excessive amounts of shadow nodes.  To keep a lid on this,
   * track shadow nodes and reclaim them when they grow way past the
   * point where they would still be useful.
   */
14b468791   Johannes Weiner   mm: workingset: m...
421
  static struct list_lru shadow_nodes;
a97e7904c   Matthew Wilcox   mm: Convert worki...
422
  void workingset_update_node(struct xa_node *node)
14b468791   Johannes Weiner   mm: workingset: m...
423
  {
14b468791   Johannes Weiner   mm: workingset: m...
424
425
426
427
428
429
  	/*
  	 * Track non-empty nodes that contain only shadow entries;
  	 * unlink those that contain pages or are being freed.
  	 *
  	 * Avoid acquiring the list_lru lock when the nodes are
  	 * already where they should be. The list_empty() test is safe
b93b01631   Matthew Wilcox   page cache: use x...
430
  	 * as node->private_list is protected by the i_pages lock.
14b468791   Johannes Weiner   mm: workingset: m...
431
  	 */
68d48e6a2   Johannes Weiner   mm: workingset: a...
432
  	VM_WARN_ON_ONCE(!irqs_disabled());  /* For __inc_lruvec_page_state */
01959dfe7   Matthew Wilcox   xarray: Define st...
433
  	if (node->count && node->count == node->nr_values) {
68d48e6a2   Johannes Weiner   mm: workingset: a...
434
  		if (list_empty(&node->private_list)) {
14b468791   Johannes Weiner   mm: workingset: m...
435
  			list_lru_add(&shadow_nodes, &node->private_list);
ec9f02384   Roman Gushchin   mm: workingset: f...
436
  			__inc_lruvec_slab_state(node, WORKINGSET_NODES);
68d48e6a2   Johannes Weiner   mm: workingset: a...
437
  		}
14b468791   Johannes Weiner   mm: workingset: m...
438
  	} else {
68d48e6a2   Johannes Weiner   mm: workingset: a...
439
  		if (!list_empty(&node->private_list)) {
14b468791   Johannes Weiner   mm: workingset: m...
440
  			list_lru_del(&shadow_nodes, &node->private_list);
ec9f02384   Roman Gushchin   mm: workingset: f...
441
  			__dec_lruvec_slab_state(node, WORKINGSET_NODES);
68d48e6a2   Johannes Weiner   mm: workingset: a...
442
  		}
14b468791   Johannes Weiner   mm: workingset: m...
443
444
  	}
  }
449dd6984   Johannes Weiner   mm: keep page cac...
445
446
447
448
  
  static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  					struct shrink_control *sc)
  {
449dd6984   Johannes Weiner   mm: keep page cac...
449
  	unsigned long max_nodes;
14b468791   Johannes Weiner   mm: workingset: m...
450
  	unsigned long nodes;
95f9ab2d5   Johannes Weiner   mm: workingset: d...
451
  	unsigned long pages;
449dd6984   Johannes Weiner   mm: keep page cac...
452

14b468791   Johannes Weiner   mm: workingset: m...
453
  	nodes = list_lru_shrink_count(&shadow_nodes, sc);
449dd6984   Johannes Weiner   mm: keep page cac...
454

449dd6984   Johannes Weiner   mm: keep page cac...
455
  	/*
a97e7904c   Matthew Wilcox   mm: Convert worki...
456
  	 * Approximate a reasonable limit for the nodes
b53889987   Johannes Weiner   mm: workingset: u...
457
458
459
460
461
462
463
464
465
466
467
468
469
  	 * containing shadow entries. We don't need to keep more
  	 * shadow entries than possible pages on the active list,
  	 * since refault distances bigger than that are dismissed.
  	 *
  	 * The size of the active list converges toward 100% of
  	 * overall page cache as memory grows, with only a tiny
  	 * inactive list. Assume the total cache size for that.
  	 *
  	 * Nodes might be sparsely populated, with only one shadow
  	 * entry in the extreme case. Obviously, we cannot keep one
  	 * node for every eligible shadow entry, so compromise on a
  	 * worst-case density of 1/8th. Below that, not all eligible
  	 * refaults can be detected anymore.
449dd6984   Johannes Weiner   mm: keep page cac...
470
  	 *
a97e7904c   Matthew Wilcox   mm: Convert worki...
471
  	 * On 64-bit with 7 xa_nodes per page and 64 slots
449dd6984   Johannes Weiner   mm: keep page cac...
472
  	 * each, this will reclaim shadow entries when they consume
b53889987   Johannes Weiner   mm: workingset: u...
473
  	 * ~1.8% of available memory:
449dd6984   Johannes Weiner   mm: keep page cac...
474
  	 *
a97e7904c   Matthew Wilcox   mm: Convert worki...
475
  	 * PAGE_SIZE / xa_nodes / node_entries * 8 / PAGE_SIZE
449dd6984   Johannes Weiner   mm: keep page cac...
476
  	 */
95f9ab2d5   Johannes Weiner   mm: workingset: d...
477
  #ifdef CONFIG_MEMCG
b53889987   Johannes Weiner   mm: workingset: u...
478
  	if (sc->memcg) {
95f9ab2d5   Johannes Weiner   mm: workingset: d...
479
  		struct lruvec *lruvec;
2b487e59f   Johannes Weiner   mm: memcontrol: p...
480
  		int i;
95f9ab2d5   Johannes Weiner   mm: workingset: d...
481

867e5e1de   Johannes Weiner   mm: clean up and ...
482
  		lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid));
2b487e59f   Johannes Weiner   mm: memcontrol: p...
483
  		for (pages = 0, i = 0; i < NR_LRU_LISTS; i++)
205b20cc5   Johannes Weiner   mm: memcontrol: m...
484
485
  			pages += lruvec_page_state_local(lruvec,
  							 NR_LRU_BASE + i);
d42f3245c   Roman Gushchin   mm: memcg: conver...
486
487
488
489
  		pages += lruvec_page_state_local(
  			lruvec, NR_SLAB_RECLAIMABLE_B) >> PAGE_SHIFT;
  		pages += lruvec_page_state_local(
  			lruvec, NR_SLAB_UNRECLAIMABLE_B) >> PAGE_SHIFT;
95f9ab2d5   Johannes Weiner   mm: workingset: d...
490
491
492
  	} else
  #endif
  		pages = node_present_pages(sc->nid);
dad4f140e   Linus Torvalds   Merge branch 'xar...
493
  	max_nodes = pages >> (XA_CHUNK_SHIFT - 3);
449dd6984   Johannes Weiner   mm: keep page cac...
494

9b996468c   Kirill Tkhai   mm: add SHRINK_EM...
495
496
  	if (!nodes)
  		return SHRINK_EMPTY;
14b468791   Johannes Weiner   mm: workingset: m...
497
  	if (nodes <= max_nodes)
449dd6984   Johannes Weiner   mm: keep page cac...
498
  		return 0;
14b468791   Johannes Weiner   mm: workingset: m...
499
  	return nodes - max_nodes;
449dd6984   Johannes Weiner   mm: keep page cac...
500
501
502
  }
  
  static enum lru_status shadow_lru_isolate(struct list_head *item,
3f97b1632   Vladimir Davydov   list_lru: add hel...
503
  					  struct list_lru_one *lru,
449dd6984   Johannes Weiner   mm: keep page cac...
504
  					  spinlock_t *lru_lock,
a97e7904c   Matthew Wilcox   mm: Convert worki...
505
  					  void *arg) __must_hold(lru_lock)
449dd6984   Johannes Weiner   mm: keep page cac...
506
  {
a97e7904c   Matthew Wilcox   mm: Convert worki...
507
  	struct xa_node *node = container_of(item, struct xa_node, private_list);
449dd6984   Johannes Weiner   mm: keep page cac...
508
  	struct address_space *mapping;
449dd6984   Johannes Weiner   mm: keep page cac...
509
510
511
  	int ret;
  
  	/*
f82cd2f0b   Matthew Wilcox (Oracle)   XArray: Add priva...
512
  	 * Page cache insertions and deletions synchronously maintain
b93b01631   Matthew Wilcox   page cache: use x...
513
  	 * the shadow node LRU under the i_pages lock and the
449dd6984   Johannes Weiner   mm: keep page cac...
514
515
  	 * lru_lock.  Because the page cache tree is emptied before
  	 * the inode can be destroyed, holding the lru_lock pins any
a97e7904c   Matthew Wilcox   mm: Convert worki...
516
  	 * address_space that has nodes on the LRU.
449dd6984   Johannes Weiner   mm: keep page cac...
517
  	 *
b93b01631   Matthew Wilcox   page cache: use x...
518
  	 * We can then safely transition to the i_pages lock to
449dd6984   Johannes Weiner   mm: keep page cac...
519
520
521
  	 * pin only the address_space of the particular node we want
  	 * to reclaim, take the node off-LRU, and drop the lru_lock.
  	 */
01959dfe7   Matthew Wilcox   xarray: Define st...
522
  	mapping = container_of(node->array, struct address_space, i_pages);
449dd6984   Johannes Weiner   mm: keep page cac...
523
524
  
  	/* Coming from the list, invert the lock order */
b93b01631   Matthew Wilcox   page cache: use x...
525
  	if (!xa_trylock(&mapping->i_pages)) {
6ca342d02   Sebastian Andrzej Siewior   mm: workingset: m...
526
  		spin_unlock_irq(lru_lock);
449dd6984   Johannes Weiner   mm: keep page cac...
527
528
529
  		ret = LRU_RETRY;
  		goto out;
  	}
3f97b1632   Vladimir Davydov   list_lru: add hel...
530
  	list_lru_isolate(lru, item);
ec9f02384   Roman Gushchin   mm: workingset: f...
531
  	__dec_lruvec_slab_state(node, WORKINGSET_NODES);
68d48e6a2   Johannes Weiner   mm: workingset: a...
532

449dd6984   Johannes Weiner   mm: keep page cac...
533
534
535
536
537
538
539
  	spin_unlock(lru_lock);
  
  	/*
  	 * The nodes should only contain one or more shadow entries,
  	 * no pages, so we expect to be able to remove them all and
  	 * delete and free the empty node afterwards.
  	 */
01959dfe7   Matthew Wilcox   xarray: Define st...
540
  	if (WARN_ON_ONCE(!node->nr_values))
b936887e8   Johannes Weiner   mm: workingset: t...
541
  		goto out_invalid;
01959dfe7   Matthew Wilcox   xarray: Define st...
542
  	if (WARN_ON_ONCE(node->count != node->nr_values))
b936887e8   Johannes Weiner   mm: workingset: t...
543
  		goto out_invalid;
a97e7904c   Matthew Wilcox   mm: Convert worki...
544
  	mapping->nrexceptional -= node->nr_values;
f82cd2f0b   Matthew Wilcox (Oracle)   XArray: Add priva...
545
  	xa_delete_node(node, workingset_update_node);
ec9f02384   Roman Gushchin   mm: workingset: f...
546
  	__inc_lruvec_slab_state(node, WORKINGSET_NODERECLAIM);
449dd6984   Johannes Weiner   mm: keep page cac...
547

b936887e8   Johannes Weiner   mm: workingset: t...
548
  out_invalid:
6ca342d02   Sebastian Andrzej Siewior   mm: workingset: m...
549
  	xa_unlock_irq(&mapping->i_pages);
449dd6984   Johannes Weiner   mm: keep page cac...
550
551
  	ret = LRU_REMOVED_RETRY;
  out:
449dd6984   Johannes Weiner   mm: keep page cac...
552
  	cond_resched();
6ca342d02   Sebastian Andrzej Siewior   mm: workingset: m...
553
  	spin_lock_irq(lru_lock);
449dd6984   Johannes Weiner   mm: keep page cac...
554
555
556
557
558
559
  	return ret;
  }
  
  static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
  				       struct shrink_control *sc)
  {
b93b01631   Matthew Wilcox   page cache: use x...
560
  	/* list_lru lock nests inside the IRQ-safe i_pages lock */
6b51e8819   Sebastian Andrzej Siewior   mm/list_lru: intr...
561
562
  	return list_lru_shrink_walk_irq(&shadow_nodes, sc, shadow_lru_isolate,
  					NULL);
449dd6984   Johannes Weiner   mm: keep page cac...
563
564
565
566
567
  }
  
  static struct shrinker workingset_shadow_shrinker = {
  	.count_objects = count_shadow_nodes,
  	.scan_objects = scan_shadow_nodes,
4b85afbda   Johannes Weiner   mm: zero-seek shr...
568
  	.seeks = 0, /* ->count reports only fully expendable nodes */
0a6b76dd2   Vladimir Davydov   mm: workingset: m...
569
  	.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
449dd6984   Johannes Weiner   mm: keep page cac...
570
571
572
573
  };
  
  /*
   * Our list_lru->lock is IRQ-safe as it nests inside the IRQ-safe
b93b01631   Matthew Wilcox   page cache: use x...
574
   * i_pages lock.
449dd6984   Johannes Weiner   mm: keep page cac...
575
576
577
578
579
   */
  static struct lock_class_key shadow_nodes_key;
  
  static int __init workingset_init(void)
  {
612e44939   Johannes Weiner   mm: workingset: e...
580
581
  	unsigned int timestamp_bits;
  	unsigned int max_order;
449dd6984   Johannes Weiner   mm: keep page cac...
582
  	int ret;
612e44939   Johannes Weiner   mm: workingset: e...
583
584
585
586
587
588
589
590
591
  	BUILD_BUG_ON(BITS_PER_LONG < EVICTION_SHIFT);
  	/*
  	 * Calculate the eviction bucket size to cover the longest
  	 * actionable refault distance, which is currently half of
  	 * memory (totalram_pages/2). However, memory hotplug may add
  	 * some more pages at runtime, so keep working with up to
  	 * double the initial memory by using totalram_pages as-is.
  	 */
  	timestamp_bits = BITS_PER_LONG - EVICTION_SHIFT;
ca79b0c21   Arun KS   mm: convert total...
592
  	max_order = fls_long(totalram_pages() - 1);
612e44939   Johannes Weiner   mm: workingset: e...
593
594
  	if (max_order > timestamp_bits)
  		bucket_order = max_order - timestamp_bits;
d3d36c4b5   Anton Blanchard   mm: workingset: p...
595
596
  	pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u
  ",
612e44939   Johannes Weiner   mm: workingset: e...
597
  	       timestamp_bits, max_order, bucket_order);
39887653a   Kirill Tkhai   mm/workingset.c: ...
598
  	ret = prealloc_shrinker(&workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
599
600
  	if (ret)
  		goto err;
c92e8e10c   Kirill Tkhai   fs: propagate shr...
601
602
  	ret = __list_lru_init(&shadow_nodes, true, &shadow_nodes_key,
  			      &workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
603
604
  	if (ret)
  		goto err_list_lru;
39887653a   Kirill Tkhai   mm/workingset.c: ...
605
  	register_shrinker_prepared(&workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
606
607
  	return 0;
  err_list_lru:
39887653a   Kirill Tkhai   mm/workingset.c: ...
608
  	free_prealloced_shrinker(&workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
609
610
611
612
  err:
  	return ret;
  }
  module_init(workingset_init);