Blame view

mm/workingset.c 18 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
9
  /*
   * Workingset detection
   *
   * Copyright (C) 2013 Red Hat, Inc., Johannes Weiner
   */
  
  #include <linux/memcontrol.h>
  #include <linux/writeback.h>
3a4f8a0b3   Hugh Dickins   mm: remove shmem_...
10
  #include <linux/shmem_fs.h>
a528910e1   Johannes Weiner   mm: thrash detect...
11
12
13
14
  #include <linux/pagemap.h>
  #include <linux/atomic.h>
  #include <linux/module.h>
  #include <linux/swap.h>
14b468791   Johannes Weiner   mm: workingset: m...
15
  #include <linux/dax.h>
a528910e1   Johannes Weiner   mm: thrash detect...
16
17
18
19
20
21
  #include <linux/fs.h>
  #include <linux/mm.h>
  
  /*
   *		Double CLOCK lists
   *
1e6b10857   Mel Gorman   mm, workingset: m...
22
   * Per node, two clock lists are maintained for file pages: the
a528910e1   Johannes Weiner   mm: thrash detect...
23
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
   * 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.
   *
   *
   *		Activating refaulting pages
   *
   * 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.
   *
   * 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.
   *
   *
   *		Implementation
   *
1e6b10857   Mel Gorman   mm, workingset: m...
147
148
   * For each node's file LRU lists, a counter for inactive evictions
   * and activations is maintained (node->inactive_age).
a528910e1   Johannes Weiner   mm: thrash detect...
149
150
   *
   * On eviction, a snapshot of this counter (along with some bits to
1e6b10857   Mel Gorman   mm, workingset: m...
151
   * identify the node) is stored in the now empty page cache radix tree
a528910e1   Johannes Weiner   mm: thrash detect...
152
153
154
155
156
   * 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.
   */
689c94f03   Johannes Weiner   mm: workingset: #...
157
  #define EVICTION_SHIFT	(RADIX_TREE_EXCEPTIONAL_ENTRY + \
1e6b10857   Mel Gorman   mm, workingset: m...
158
  			 NODES_SHIFT +	\
23047a96d   Johannes Weiner   mm: workingset: p...
159
  			 MEM_CGROUP_ID_SHIFT)
689c94f03   Johannes Weiner   mm: workingset: #...
160
  #define EVICTION_MASK	(~0UL >> EVICTION_SHIFT)
612e44939   Johannes Weiner   mm: workingset: e...
161
162
163
164
165
166
167
168
169
  /*
   * Eviction timestamps need to be able to cover the full range of
   * actionable refaults. However, bits are tight in the radix tree
   * 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;
1e6b10857   Mel Gorman   mm, workingset: m...
170
  static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction)
a528910e1   Johannes Weiner   mm: thrash detect...
171
  {
612e44939   Johannes Weiner   mm: workingset: e...
172
  	eviction >>= bucket_order;
23047a96d   Johannes Weiner   mm: workingset: p...
173
  	eviction = (eviction << MEM_CGROUP_ID_SHIFT) | memcgid;
1e6b10857   Mel Gorman   mm, workingset: m...
174
  	eviction = (eviction << NODES_SHIFT) | pgdat->node_id;
a528910e1   Johannes Weiner   mm: thrash detect...
175
176
177
178
  	eviction = (eviction << RADIX_TREE_EXCEPTIONAL_SHIFT);
  
  	return (void *)(eviction | RADIX_TREE_EXCEPTIONAL_ENTRY);
  }
1e6b10857   Mel Gorman   mm, workingset: m...
179
  static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
162453bfb   Johannes Weiner   mm: workingset: s...
180
  			  unsigned long *evictionp)
a528910e1   Johannes Weiner   mm: thrash detect...
181
182
  {
  	unsigned long entry = (unsigned long)shadow;
1e6b10857   Mel Gorman   mm, workingset: m...
183
  	int memcgid, nid;
a528910e1   Johannes Weiner   mm: thrash detect...
184
185
  
  	entry >>= RADIX_TREE_EXCEPTIONAL_SHIFT;
a528910e1   Johannes Weiner   mm: thrash detect...
186
187
  	nid = entry & ((1UL << NODES_SHIFT) - 1);
  	entry >>= NODES_SHIFT;
23047a96d   Johannes Weiner   mm: workingset: p...
188
189
  	memcgid = entry & ((1UL << MEM_CGROUP_ID_SHIFT) - 1);
  	entry >>= MEM_CGROUP_ID_SHIFT;
a528910e1   Johannes Weiner   mm: thrash detect...
190

23047a96d   Johannes Weiner   mm: workingset: p...
191
  	*memcgidp = memcgid;
1e6b10857   Mel Gorman   mm, workingset: m...
192
  	*pgdat = NODE_DATA(nid);
612e44939   Johannes Weiner   mm: workingset: e...
193
  	*evictionp = entry << bucket_order;
a528910e1   Johannes Weiner   mm: thrash detect...
194
195
196
197
198
199
200
  }
  
  /**
   * workingset_eviction - note the eviction of a page from memory
   * @mapping: address space the page was backing
   * @page: the page being evicted
   *
b93b01631   Matthew Wilcox   page cache: use x...
201
   * Returns a shadow entry to be stored in @mapping->i_pages in place
a528910e1   Johannes Weiner   mm: thrash detect...
202
203
204
205
   * of the evicted @page so that a later refault can be detected.
   */
  void *workingset_eviction(struct address_space *mapping, struct page *page)
  {
23047a96d   Johannes Weiner   mm: workingset: p...
206
  	struct mem_cgroup *memcg = page_memcg(page);
1e6b10857   Mel Gorman   mm, workingset: m...
207
  	struct pglist_data *pgdat = page_pgdat(page);
23047a96d   Johannes Weiner   mm: workingset: p...
208
  	int memcgid = mem_cgroup_id(memcg);
a528910e1   Johannes Weiner   mm: thrash detect...
209
  	unsigned long eviction;
23047a96d   Johannes Weiner   mm: workingset: p...
210
  	struct lruvec *lruvec;
a528910e1   Johannes Weiner   mm: thrash detect...
211

23047a96d   Johannes Weiner   mm: workingset: p...
212
213
214
215
  	/* 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);
1e6b10857   Mel Gorman   mm, workingset: m...
216
  	lruvec = mem_cgroup_lruvec(pgdat, memcg);
23047a96d   Johannes Weiner   mm: workingset: p...
217
  	eviction = atomic_long_inc_return(&lruvec->inactive_age);
1e6b10857   Mel Gorman   mm, workingset: m...
218
  	return pack_shadow(memcgid, pgdat, eviction);
a528910e1   Johannes Weiner   mm: thrash detect...
219
220
221
222
223
224
225
  }
  
  /**
   * workingset_refault - evaluate the refault of a previously evicted page
   * @shadow: shadow entry of the evicted page
   *
   * Calculates and evaluates the refault distance of the previously
1e6b10857   Mel Gorman   mm, workingset: m...
226
   * evicted page in the context of the node it was allocated in.
a528910e1   Johannes Weiner   mm: thrash detect...
227
228
229
230
231
232
   *
   * Returns %true if the page should be activated, %false otherwise.
   */
  bool workingset_refault(void *shadow)
  {
  	unsigned long refault_distance;
23047a96d   Johannes Weiner   mm: workingset: p...
233
234
  	unsigned long active_file;
  	struct mem_cgroup *memcg;
162453bfb   Johannes Weiner   mm: workingset: s...
235
  	unsigned long eviction;
23047a96d   Johannes Weiner   mm: workingset: p...
236
  	struct lruvec *lruvec;
162453bfb   Johannes Weiner   mm: workingset: s...
237
  	unsigned long refault;
1e6b10857   Mel Gorman   mm, workingset: m...
238
  	struct pglist_data *pgdat;
23047a96d   Johannes Weiner   mm: workingset: p...
239
  	int memcgid;
a528910e1   Johannes Weiner   mm: thrash detect...
240

1e6b10857   Mel Gorman   mm, workingset: m...
241
  	unpack_shadow(shadow, &memcgid, &pgdat, &eviction);
162453bfb   Johannes Weiner   mm: workingset: s...
242

23047a96d   Johannes Weiner   mm: workingset: p...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  	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.
  	 */
  	memcg = mem_cgroup_from_id(memcgid);
  	if (!mem_cgroup_disabled() && !memcg) {
  		rcu_read_unlock();
  		return false;
  	}
1e6b10857   Mel Gorman   mm, workingset: m...
265
  	lruvec = mem_cgroup_lruvec(pgdat, memcg);
23047a96d   Johannes Weiner   mm: workingset: p...
266
  	refault = atomic_long_read(&lruvec->inactive_age);
fd5388037   Michal Hocko   mm, vmscan: clean...
267
  	active_file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES);
162453bfb   Johannes Weiner   mm: workingset: s...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  
  	/*
  	 * The unsigned subtraction here gives an accurate distance
  	 * across inactive_age overflows in most cases.
  	 *
  	 * There is a 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 inactive_age to lap a shadow entry in
  	 * the field, which can then can 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 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.
  	 */
  	refault_distance = (refault - eviction) & EVICTION_MASK;
00f3ca2c2   Johannes Weiner   mm: memcontrol: p...
286
  	inc_lruvec_state(lruvec, WORKINGSET_REFAULT);
a528910e1   Johannes Weiner   mm: thrash detect...
287

23047a96d   Johannes Weiner   mm: workingset: p...
288
  	if (refault_distance <= active_file) {
00f3ca2c2   Johannes Weiner   mm: memcontrol: p...
289
  		inc_lruvec_state(lruvec, WORKINGSET_ACTIVATE);
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
290
  		rcu_read_unlock();
a528910e1   Johannes Weiner   mm: thrash detect...
291
292
  		return true;
  	}
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
293
  	rcu_read_unlock();
a528910e1   Johannes Weiner   mm: thrash detect...
294
295
296
297
298
299
300
301
302
  	return false;
  }
  
  /**
   * 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...
303
  	struct mem_cgroup *memcg;
23047a96d   Johannes Weiner   mm: workingset: p...
304
  	struct lruvec *lruvec;
55779ec75   Johannes Weiner   mm: fix vm-scalab...
305
  	rcu_read_lock();
23047a96d   Johannes Weiner   mm: workingset: p...
306
307
308
309
310
311
312
  	/*
  	 * 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...
313
314
  	memcg = page_memcg_rcu(page);
  	if (!mem_cgroup_disabled() && !memcg)
23047a96d   Johannes Weiner   mm: workingset: p...
315
  		goto out;
ef8f23279   Mel Gorman   mm, memcg: move m...
316
  	lruvec = mem_cgroup_lruvec(page_pgdat(page), memcg);
23047a96d   Johannes Weiner   mm: workingset: p...
317
318
  	atomic_long_inc(&lruvec->inactive_age);
  out:
55779ec75   Johannes Weiner   mm: fix vm-scalab...
319
  	rcu_read_unlock();
a528910e1   Johannes Weiner   mm: thrash detect...
320
  }
449dd6984   Johannes Weiner   mm: keep page cac...
321
322
323
324
325
326
327
328
329
330
331
332
  
  /*
   * 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...
333
  static struct list_lru shadow_nodes;
c7df8ad29   Mel Gorman   mm, truncate: do ...
334
  void workingset_update_node(struct radix_tree_node *node)
14b468791   Johannes Weiner   mm: workingset: m...
335
  {
14b468791   Johannes Weiner   mm: workingset: m...
336
337
338
339
340
341
  	/*
  	 * 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...
342
  	 * as node->private_list is protected by the i_pages lock.
14b468791   Johannes Weiner   mm: workingset: m...
343
344
  	 */
  	if (node->count && node->count == node->exceptional) {
d58275bc9   Matthew Wilcox   radix-tree: Store...
345
  		if (list_empty(&node->private_list))
14b468791   Johannes Weiner   mm: workingset: m...
346
  			list_lru_add(&shadow_nodes, &node->private_list);
14b468791   Johannes Weiner   mm: workingset: m...
347
348
349
350
351
  	} else {
  		if (!list_empty(&node->private_list))
  			list_lru_del(&shadow_nodes, &node->private_list);
  	}
  }
449dd6984   Johannes Weiner   mm: keep page cac...
352
353
354
355
  
  static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  					struct shrink_control *sc)
  {
449dd6984   Johannes Weiner   mm: keep page cac...
356
  	unsigned long max_nodes;
14b468791   Johannes Weiner   mm: workingset: m...
357
  	unsigned long nodes;
b53889987   Johannes Weiner   mm: workingset: u...
358
  	unsigned long cache;
449dd6984   Johannes Weiner   mm: keep page cac...
359

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

449dd6984   Johannes Weiner   mm: keep page cac...
362
  	/*
b53889987   Johannes Weiner   mm: workingset: u...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  	 * Approximate a reasonable limit for the radix tree nodes
  	 * 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...
377
378
379
  	 *
  	 * On 64-bit with 7 radix_tree_nodes per page and 64 slots
  	 * each, this will reclaim shadow entries when they consume
b53889987   Johannes Weiner   mm: workingset: u...
380
  	 * ~1.8% of available memory:
449dd6984   Johannes Weiner   mm: keep page cac...
381
  	 *
b53889987   Johannes Weiner   mm: workingset: u...
382
  	 * PAGE_SIZE / radix_tree_nodes / node_entries * 8 / PAGE_SIZE
449dd6984   Johannes Weiner   mm: keep page cac...
383
  	 */
b53889987   Johannes Weiner   mm: workingset: u...
384
385
386
387
388
389
390
391
  	if (sc->memcg) {
  		cache = mem_cgroup_node_nr_lru_pages(sc->memcg, sc->nid,
  						     LRU_ALL_FILE);
  	} else {
  		cache = node_page_state(NODE_DATA(sc->nid), NR_ACTIVE_FILE) +
  			node_page_state(NODE_DATA(sc->nid), NR_INACTIVE_FILE);
  	}
  	max_nodes = cache >> (RADIX_TREE_MAP_SHIFT - 3);
449dd6984   Johannes Weiner   mm: keep page cac...
392

9b996468c   Kirill Tkhai   mm: add SHRINK_EM...
393
394
  	if (!nodes)
  		return SHRINK_EMPTY;
14b468791   Johannes Weiner   mm: workingset: m...
395
  	if (nodes <= max_nodes)
449dd6984   Johannes Weiner   mm: keep page cac...
396
  		return 0;
14b468791   Johannes Weiner   mm: workingset: m...
397
  	return nodes - max_nodes;
449dd6984   Johannes Weiner   mm: keep page cac...
398
399
400
  }
  
  static enum lru_status shadow_lru_isolate(struct list_head *item,
3f97b1632   Vladimir Davydov   list_lru: add hel...
401
  					  struct list_lru_one *lru,
449dd6984   Johannes Weiner   mm: keep page cac...
402
403
404
405
406
407
408
409
410
411
  					  spinlock_t *lru_lock,
  					  void *arg)
  {
  	struct address_space *mapping;
  	struct radix_tree_node *node;
  	unsigned int i;
  	int ret;
  
  	/*
  	 * Page cache insertions and deletions synchroneously maintain
b93b01631   Matthew Wilcox   page cache: use x...
412
  	 * the shadow node LRU under the i_pages lock and the
449dd6984   Johannes Weiner   mm: keep page cac...
413
414
415
416
  	 * lru_lock.  Because the page cache tree is emptied before
  	 * the inode can be destroyed, holding the lru_lock pins any
  	 * address_space that has radix tree nodes on the LRU.
  	 *
b93b01631   Matthew Wilcox   page cache: use x...
417
  	 * We can then safely transition to the i_pages lock to
449dd6984   Johannes Weiner   mm: keep page cac...
418
419
420
421
422
  	 * pin only the address_space of the particular node we want
  	 * to reclaim, take the node off-LRU, and drop the lru_lock.
  	 */
  
  	node = container_of(item, struct radix_tree_node, private_list);
b93b01631   Matthew Wilcox   page cache: use x...
423
  	mapping = container_of(node->root, struct address_space, i_pages);
449dd6984   Johannes Weiner   mm: keep page cac...
424
425
  
  	/* Coming from the list, invert the lock order */
b93b01631   Matthew Wilcox   page cache: use x...
426
  	if (!xa_trylock(&mapping->i_pages)) {
6ca342d02   Sebastian Andrzej Siewior   mm: workingset: m...
427
  		spin_unlock_irq(lru_lock);
449dd6984   Johannes Weiner   mm: keep page cac...
428
429
430
  		ret = LRU_RETRY;
  		goto out;
  	}
3f97b1632   Vladimir Davydov   list_lru: add hel...
431
  	list_lru_isolate(lru, item);
449dd6984   Johannes Weiner   mm: keep page cac...
432
433
434
435
436
437
438
  	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.
  	 */
14b468791   Johannes Weiner   mm: workingset: m...
439
  	if (WARN_ON_ONCE(!node->exceptional))
b936887e8   Johannes Weiner   mm: workingset: t...
440
  		goto out_invalid;
14b468791   Johannes Weiner   mm: workingset: m...
441
  	if (WARN_ON_ONCE(node->count != node->exceptional))
b936887e8   Johannes Weiner   mm: workingset: t...
442
  		goto out_invalid;
449dd6984   Johannes Weiner   mm: keep page cac...
443
444
  	for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
  		if (node->slots[i]) {
b936887e8   Johannes Weiner   mm: workingset: t...
445
446
  			if (WARN_ON_ONCE(!radix_tree_exceptional_entry(node->slots[i])))
  				goto out_invalid;
14b468791   Johannes Weiner   mm: workingset: m...
447
448
  			if (WARN_ON_ONCE(!node->exceptional))
  				goto out_invalid;
b936887e8   Johannes Weiner   mm: workingset: t...
449
450
  			if (WARN_ON_ONCE(!mapping->nrexceptional))
  				goto out_invalid;
449dd6984   Johannes Weiner   mm: keep page cac...
451
  			node->slots[i] = NULL;
14b468791   Johannes Weiner   mm: workingset: m...
452
453
  			node->exceptional--;
  			node->count--;
f9fe48bec   Ross Zwisler   dax: support dirt...
454
  			mapping->nrexceptional--;
449dd6984   Johannes Weiner   mm: keep page cac...
455
456
  		}
  	}
14b468791   Johannes Weiner   mm: workingset: m...
457
  	if (WARN_ON_ONCE(node->exceptional))
b936887e8   Johannes Weiner   mm: workingset: t...
458
  		goto out_invalid;
00f3ca2c2   Johannes Weiner   mm: memcontrol: p...
459
  	inc_lruvec_page_state(virt_to_page(node), WORKINGSET_NODERECLAIM);
b93b01631   Matthew Wilcox   page cache: use x...
460
  	__radix_tree_delete_node(&mapping->i_pages, node,
c7df8ad29   Mel Gorman   mm, truncate: do ...
461
  				 workingset_lookup_update(mapping));
449dd6984   Johannes Weiner   mm: keep page cac...
462

b936887e8   Johannes Weiner   mm: workingset: t...
463
  out_invalid:
6ca342d02   Sebastian Andrzej Siewior   mm: workingset: m...
464
  	xa_unlock_irq(&mapping->i_pages);
449dd6984   Johannes Weiner   mm: keep page cac...
465
466
  	ret = LRU_REMOVED_RETRY;
  out:
449dd6984   Johannes Weiner   mm: keep page cac...
467
  	cond_resched();
6ca342d02   Sebastian Andrzej Siewior   mm: workingset: m...
468
  	spin_lock_irq(lru_lock);
449dd6984   Johannes Weiner   mm: keep page cac...
469
470
471
472
473
474
  	return ret;
  }
  
  static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
  				       struct shrink_control *sc)
  {
b93b01631   Matthew Wilcox   page cache: use x...
475
  	/* list_lru lock nests inside the IRQ-safe i_pages lock */
6b51e8819   Sebastian Andrzej Siewior   mm/list_lru: intr...
476
477
  	return list_lru_shrink_walk_irq(&shadow_nodes, sc, shadow_lru_isolate,
  					NULL);
449dd6984   Johannes Weiner   mm: keep page cac...
478
479
480
481
482
483
  }
  
  static struct shrinker workingset_shadow_shrinker = {
  	.count_objects = count_shadow_nodes,
  	.scan_objects = scan_shadow_nodes,
  	.seeks = DEFAULT_SEEKS,
0a6b76dd2   Vladimir Davydov   mm: workingset: m...
484
  	.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
449dd6984   Johannes Weiner   mm: keep page cac...
485
486
487
488
  };
  
  /*
   * Our list_lru->lock is IRQ-safe as it nests inside the IRQ-safe
b93b01631   Matthew Wilcox   page cache: use x...
489
   * i_pages lock.
449dd6984   Johannes Weiner   mm: keep page cac...
490
491
492
493
494
   */
  static struct lock_class_key shadow_nodes_key;
  
  static int __init workingset_init(void)
  {
612e44939   Johannes Weiner   mm: workingset: e...
495
496
  	unsigned int timestamp_bits;
  	unsigned int max_order;
449dd6984   Johannes Weiner   mm: keep page cac...
497
  	int ret;
612e44939   Johannes Weiner   mm: workingset: e...
498
499
500
501
502
503
504
505
506
507
508
509
  	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;
  	max_order = fls_long(totalram_pages - 1);
  	if (max_order > timestamp_bits)
  		bucket_order = max_order - timestamp_bits;
d3d36c4b5   Anton Blanchard   mm: workingset: p...
510
511
  	pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u
  ",
612e44939   Johannes Weiner   mm: workingset: e...
512
  	       timestamp_bits, max_order, bucket_order);
39887653a   Kirill Tkhai   mm/workingset.c: ...
513
  	ret = prealloc_shrinker(&workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
514
515
  	if (ret)
  		goto err;
c92e8e10c   Kirill Tkhai   fs: propagate shr...
516
517
  	ret = __list_lru_init(&shadow_nodes, true, &shadow_nodes_key,
  			      &workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
518
519
  	if (ret)
  		goto err_list_lru;
39887653a   Kirill Tkhai   mm/workingset.c: ...
520
  	register_shrinker_prepared(&workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
521
522
  	return 0;
  err_list_lru:
39887653a   Kirill Tkhai   mm/workingset.c: ...
523
  	free_prealloced_shrinker(&workingset_shadow_shrinker);
449dd6984   Johannes Weiner   mm: keep page cac...
524
525
526
527
  err:
  	return ret;
  }
  module_init(workingset_init);